PR target/4198
[official-gcc.git] / gcc / c-decl.c
blob9d82b9a5b2017ed9e5825ef6986e3e8261e53b78
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
120 included in this invocation. Note that the current translation
121 unit is not included in this list. */
123 static GTY(()) tree all_translation_units;
125 /* A list of decls to be made automatically visible in each file scope. */
126 static GTY(()) tree visible_builtins;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement that specifies a return value is seen. */
131 int current_function_returns_value;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a return statement with no argument is seen. */
136 int current_function_returns_null;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a call to a noreturn function is seen. */
141 int current_function_returns_abnormally;
143 /* Set to nonzero by `grokdeclarator' for a function
144 whose return type is defaulted, if warnings for this are desired. */
146 static int warn_about_return_type;
148 /* Nonzero when starting a function declared `extern inline'. */
150 static int current_extern_inline;
152 /* Nonzero when the current toplevel function contains a declaration
153 of a nested function which is never defined. */
155 static bool undef_nested_function;
157 /* True means global_bindings_p should return false even if the scope stack
158 says we are in file scope. */
159 bool c_override_global_bindings_to_false;
162 /* Each c_binding structure describes one binding of an identifier to
163 a decl. All the decls in a scope - irrespective of namespace - are
164 chained together by the ->prev field, which (as the name implies)
165 runs in reverse order. All the decls in a given namespace bound to
166 a given identifier are chained by the ->shadowed field, which runs
167 from inner to outer scopes.
169 The ->decl field usually points to a DECL node, but there are two
170 exceptions. In the namespace of type tags, the bound entity is a
171 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
172 identifier is encountered, it is bound to error_mark_node to
173 suppress further errors about that identifier in the current
174 function.
176 The ->type field stores the type of the declaration in this scope;
177 if NULL, the type is the type of the ->decl field. This is only of
178 relevance for objects with external or internal linkage which may
179 be redeclared in inner scopes, forming composite types that only
180 persist for the duration of those scopes. In the external scope,
181 this stores the composite of all the types declared for this
182 object, visible or not. The ->inner_comp field (used only at file
183 scope) stores whether an incomplete array type at file scope was
184 completed at an inner scope to an array size other than 1.
186 The depth field is copied from the scope structure that holds this
187 decl. It is used to preserve the proper ordering of the ->shadowed
188 field (see bind()) and also for a handful of special-case checks.
189 Finally, the invisible bit is true for a decl which should be
190 ignored for purposes of normal name lookup, and the nested bit is
191 true for a decl that's been bound a second time in an inner scope;
192 in all such cases, the binding in the outer scope will have its
193 invisible bit true. */
195 struct c_binding GTY((chain_next ("%h.prev")))
197 tree decl; /* the decl bound */
198 tree type; /* the type in this scope */
199 tree id; /* the identifier it's bound to */
200 struct c_binding *prev; /* the previous decl in this scope */
201 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
202 unsigned int depth : 28; /* depth of this scope */
203 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
204 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
205 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
206 /* one free bit */
208 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
209 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
210 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
211 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
213 #define I_SYMBOL_BINDING(node) \
214 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
215 #define I_SYMBOL_DECL(node) \
216 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
218 #define I_TAG_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
220 #define I_TAG_DECL(node) \
221 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
223 #define I_LABEL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
225 #define I_LABEL_DECL(node) \
226 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
228 /* Each C symbol points to three linked lists of c_binding structures.
229 These describe the values of the identifier in the three different
230 namespaces defined by the language. */
232 struct lang_identifier GTY(())
234 struct c_common_identifier common_id;
235 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
236 struct c_binding *tag_binding; /* struct/union/enum tags */
237 struct c_binding *label_binding; /* labels */
240 /* Validate c-lang.c's assumptions. */
241 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
242 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
244 /* The resulting tree type. */
246 union lang_tree_node
247 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
248 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)")))
250 union tree_node GTY ((tag ("0"),
251 desc ("tree_node_structure (&%h)")))
252 generic;
253 struct lang_identifier GTY ((tag ("1"))) identifier;
256 /* Each c_scope structure describes the complete contents of one
257 scope. Four scopes are distinguished specially: the innermost or
258 current scope, the innermost function scope, the file scope (always
259 the second to outermost) and the outermost or external scope.
261 Most declarations are recorded in the current scope.
263 All normal label declarations are recorded in the innermost
264 function scope, as are bindings of undeclared identifiers to
265 error_mark_node. (GCC permits nested functions as an extension,
266 hence the 'innermost' qualifier.) Explicitly declared labels
267 (using the __label__ extension) appear in the current scope.
269 Being in the file scope (current_scope == file_scope) causes
270 special behavior in several places below. Also, under some
271 conditions the Objective-C front end records declarations in the
272 file scope even though that isn't the current scope.
274 All declarations with external linkage are recorded in the external
275 scope, even if they aren't visible there; this models the fact that
276 such declarations are visible to the entire program, and (with a
277 bit of cleverness, see pushdecl) allows diagnosis of some violations
278 of C99 6.2.2p7 and 6.2.7p2:
280 If, within the same translation unit, the same identifier appears
281 with both internal and external linkage, the behavior is
282 undefined.
284 All declarations that refer to the same object or function shall
285 have compatible type; otherwise, the behavior is undefined.
287 Initially only the built-in declarations, which describe compiler
288 intrinsic functions plus a subset of the standard library, are in
289 this scope.
291 The order of the blocks list matters, and it is frequently appended
292 to. To avoid having to walk all the way to the end of the list on
293 each insertion, or reverse the list later, we maintain a pointer to
294 the last list entry. (FIXME: It should be feasible to use a reversed
295 list here.)
297 The bindings list is strictly in reverse order of declarations;
298 pop_scope relies on this. */
301 struct c_scope GTY((chain_next ("%h.outer")))
303 /* The scope containing this one. */
304 struct c_scope *outer;
306 /* The next outermost function scope. */
307 struct c_scope *outer_function;
309 /* All bindings in this scope. */
310 struct c_binding *bindings;
312 /* For each scope (except the global one), a chain of BLOCK nodes
313 for all the scopes that were entered and exited one level down. */
314 tree blocks;
315 tree blocks_last;
317 /* The depth of this scope. Used to keep the ->shadowed chain of
318 bindings sorted innermost to outermost. */
319 unsigned int depth : 28;
321 /* True if we are currently filling this scope with parameter
322 declarations. */
323 BOOL_BITFIELD parm_flag : 1;
325 /* True if we already complained about forward parameter decls
326 in this scope. This prevents double warnings on
327 foo (int a; int b; ...) */
328 BOOL_BITFIELD warned_forward_parm_decls : 1;
330 /* True if this is the outermost block scope of a function body.
331 This scope contains the parameters, the local variables declared
332 in the outermost block, and all the labels (except those in
333 nested functions, or declared at block scope with __label__). */
334 BOOL_BITFIELD function_body : 1;
336 /* True means make a BLOCK for this scope no matter what. */
337 BOOL_BITFIELD keep : 1;
340 /* The scope currently in effect. */
342 static GTY(()) struct c_scope *current_scope;
344 /* The innermost function scope. Ordinary (not explicitly declared)
345 labels, bindings to error_mark_node, and the lazily-created
346 bindings of __func__ and its friends get this scope. */
348 static GTY(()) struct c_scope *current_function_scope;
350 /* The C file scope. This is reset for each input translation unit. */
352 static GTY(()) struct c_scope *file_scope;
354 /* The outermost scope. This is used for all declarations with
355 external linkage, and only these, hence the name. */
357 static GTY(()) struct c_scope *external_scope;
359 /* A chain of c_scope structures awaiting reuse. */
361 static GTY((deletable)) struct c_scope *scope_freelist;
363 /* A chain of c_binding structures awaiting reuse. */
365 static GTY((deletable)) struct c_binding *binding_freelist;
367 /* Append VAR to LIST in scope SCOPE. */
368 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
369 struct c_scope *s_ = (scope); \
370 tree d_ = (decl); \
371 if (s_->list##_last) \
372 TREE_CHAIN (s_->list##_last) = d_; \
373 else \
374 s_->list = d_; \
375 s_->list##_last = d_; \
376 } while (0)
378 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
379 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
380 struct c_scope *t_ = (tscope); \
381 struct c_scope *f_ = (fscope); \
382 if (t_->to##_last) \
383 TREE_CHAIN (t_->to##_last) = f_->from; \
384 else \
385 t_->to = f_->from; \
386 t_->to##_last = f_->from##_last; \
387 } while (0)
389 /* True means unconditionally make a BLOCK for the next scope pushed. */
391 static bool keep_next_level_flag;
393 /* True means the next call to push_scope will be the outermost scope
394 of a function body, so do not push a new scope, merely cease
395 expecting parameter decls. */
397 static bool next_is_function_body;
399 /* Functions called automatically at the beginning and end of execution. */
401 static GTY(()) tree static_ctors;
402 static GTY(()) tree static_dtors;
404 /* Forward declarations. */
405 static tree lookup_name_in_scope (tree, struct c_scope *);
406 static tree c_make_fname_decl (tree, int);
407 static tree grokdeclarator (const struct c_declarator *,
408 struct c_declspecs *,
409 enum decl_context, bool, tree *);
410 static tree grokparms (struct c_arg_info *, bool);
411 static void layout_array_type (tree);
413 /* States indicating how grokdeclarator() should handle declspecs marked
414 with __attribute__((deprecated)). An object declared as
415 __attribute__((deprecated)) suppresses warnings of uses of other
416 deprecated items. */
418 enum deprecated_states {
419 DEPRECATED_NORMAL,
420 DEPRECATED_SUPPRESS
423 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
425 void
426 c_print_identifier (FILE *file, tree node, int indent)
428 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
429 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
430 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
431 if (C_IS_RESERVED_WORD (node))
433 tree rid = ridpointers[C_RID_CODE (node)];
434 indent_to (file, indent + 4);
435 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
436 (void *) rid, IDENTIFIER_POINTER (rid));
440 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
441 which may be any of several kinds of DECL or TYPE or error_mark_node,
442 in the scope SCOPE. */
443 static void
444 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
446 struct c_binding *b, **here;
448 if (binding_freelist)
450 b = binding_freelist;
451 binding_freelist = b->prev;
453 else
454 b = GGC_NEW (struct c_binding);
456 b->shadowed = 0;
457 b->decl = decl;
458 b->id = name;
459 b->depth = scope->depth;
460 b->invisible = invisible;
461 b->nested = nested;
462 b->inner_comp = 0;
464 b->type = 0;
466 b->prev = scope->bindings;
467 scope->bindings = b;
469 if (!name)
470 return;
472 switch (TREE_CODE (decl))
474 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
475 case ENUMERAL_TYPE:
476 case UNION_TYPE:
477 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
478 case VAR_DECL:
479 case FUNCTION_DECL:
480 case TYPE_DECL:
481 case CONST_DECL:
482 case PARM_DECL:
483 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
485 default:
486 gcc_unreachable ();
489 /* Locate the appropriate place in the chain of shadowed decls
490 to insert this binding. Normally, scope == current_scope and
491 this does nothing. */
492 while (*here && (*here)->depth > scope->depth)
493 here = &(*here)->shadowed;
495 b->shadowed = *here;
496 *here = b;
499 /* Clear the binding structure B, stick it on the binding_freelist,
500 and return the former value of b->prev. This is used by pop_scope
501 and get_parm_info to iterate destructively over all the bindings
502 from a given scope. */
503 static struct c_binding *
504 free_binding_and_advance (struct c_binding *b)
506 struct c_binding *prev = b->prev;
508 memset (b, 0, sizeof (struct c_binding));
509 b->prev = binding_freelist;
510 binding_freelist = b;
512 return prev;
516 /* Hook called at end of compilation to assume 1 elt
517 for a file-scope tentative array defn that wasn't complete before. */
519 void
520 c_finish_incomplete_decl (tree decl)
522 if (TREE_CODE (decl) == VAR_DECL)
524 tree type = TREE_TYPE (decl);
525 if (type != error_mark_node
526 && TREE_CODE (type) == ARRAY_TYPE
527 && !DECL_EXTERNAL (decl)
528 && TYPE_DOMAIN (type) == 0)
530 warning ("%Jarray %qD assumed to have one element", decl, decl);
532 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
534 layout_decl (decl, 0);
539 /* The Objective-C front-end often needs to determine the current scope. */
541 void *
542 objc_get_current_scope (void)
544 return current_scope;
547 /* The following function is used only by Objective-C. It needs to live here
548 because it accesses the innards of c_scope. */
550 void
551 objc_mark_locals_volatile (void *enclosing_blk)
553 struct c_scope *scope;
554 struct c_binding *b;
556 for (scope = current_scope;
557 scope && scope != enclosing_blk;
558 scope = scope->outer)
560 for (b = scope->bindings; b; b = b->prev)
562 if (TREE_CODE (b->decl) == VAR_DECL
563 || TREE_CODE (b->decl) == PARM_DECL)
565 C_DECL_REGISTER (b->decl) = 0;
566 DECL_REGISTER (b->decl) = 0;
567 TREE_THIS_VOLATILE (b->decl) = 1;
571 /* Do not climb up past the current function. */
572 if (scope->function_body)
573 break;
577 /* Nonzero if we are currently in file scope. */
580 global_bindings_p (void)
582 return current_scope == file_scope && !c_override_global_bindings_to_false;
585 void
586 keep_next_level (void)
588 keep_next_level_flag = true;
591 /* Identify this scope as currently being filled with parameters. */
593 void
594 declare_parm_level (void)
596 current_scope->parm_flag = true;
599 void
600 push_scope (void)
602 if (next_is_function_body)
604 /* This is the transition from the parameters to the top level
605 of the function body. These are the same scope
606 (C99 6.2.1p4,6) so we do not push another scope structure.
607 next_is_function_body is set only by store_parm_decls, which
608 in turn is called when and only when we are about to
609 encounter the opening curly brace for the function body.
611 The outermost block of a function always gets a BLOCK node,
612 because the debugging output routines expect that each
613 function has at least one BLOCK. */
614 current_scope->parm_flag = false;
615 current_scope->function_body = true;
616 current_scope->keep = true;
617 current_scope->outer_function = current_function_scope;
618 current_function_scope = current_scope;
620 keep_next_level_flag = false;
621 next_is_function_body = false;
623 else
625 struct c_scope *scope;
626 if (scope_freelist)
628 scope = scope_freelist;
629 scope_freelist = scope->outer;
631 else
632 scope = GGC_CNEW (struct c_scope);
634 scope->keep = keep_next_level_flag;
635 scope->outer = current_scope;
636 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
638 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
639 possible. */
640 if (current_scope && scope->depth == 0)
642 scope->depth--;
643 sorry ("GCC supports only %u nested scopes", scope->depth);
646 current_scope = scope;
647 keep_next_level_flag = false;
651 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
653 static void
654 set_type_context (tree type, tree context)
656 for (type = TYPE_MAIN_VARIANT (type); type;
657 type = TYPE_NEXT_VARIANT (type))
658 TYPE_CONTEXT (type) = context;
661 /* Exit a scope. Restore the state of the identifier-decl mappings
662 that were in effect when this scope was entered. Return a BLOCK
663 node containing all the DECLs in this scope that are of interest
664 to debug info generation. */
666 tree
667 pop_scope (void)
669 struct c_scope *scope = current_scope;
670 tree block, context, p;
671 struct c_binding *b;
673 bool functionbody = scope->function_body;
674 bool keep = functionbody || scope->keep || scope->bindings;
676 /* If appropriate, create a BLOCK to record the decls for the life
677 of this function. */
678 block = 0;
679 if (keep)
681 block = make_node (BLOCK);
682 BLOCK_SUBBLOCKS (block) = scope->blocks;
683 TREE_USED (block) = 1;
685 /* In each subblock, record that this is its superior. */
686 for (p = scope->blocks; p; p = TREE_CHAIN (p))
687 BLOCK_SUPERCONTEXT (p) = block;
689 BLOCK_VARS (block) = 0;
692 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
693 scope must be set so that they point to the appropriate
694 construct, i.e. either to the current FUNCTION_DECL node, or
695 else to the BLOCK node we just constructed.
697 Note that for tagged types whose scope is just the formal
698 parameter list for some function type specification, we can't
699 properly set their TYPE_CONTEXTs here, because we don't have a
700 pointer to the appropriate FUNCTION_TYPE node readily available
701 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
702 type nodes get set in `grokdeclarator' as soon as we have created
703 the FUNCTION_TYPE node which will represent the "scope" for these
704 "parameter list local" tagged types. */
705 if (scope->function_body)
706 context = current_function_decl;
707 else if (scope == file_scope)
709 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
710 TREE_CHAIN (file_decl) = all_translation_units;
711 all_translation_units = file_decl;
712 context = file_decl;
714 else
715 context = block;
717 /* Clear all bindings in this scope. */
718 for (b = scope->bindings; b; b = free_binding_and_advance (b))
720 p = b->decl;
721 switch (TREE_CODE (p))
723 case LABEL_DECL:
724 /* Warnings for unused labels, errors for undefined labels. */
725 if (TREE_USED (p) && !DECL_INITIAL (p))
727 error ("%Jlabel %qD used but not defined", p, p);
728 DECL_INITIAL (p) = error_mark_node;
730 else if (!TREE_USED (p) && warn_unused_label)
732 if (DECL_INITIAL (p))
733 warning ("%Jlabel %qD defined but not used", p, p);
734 else
735 warning ("%Jlabel %qD declared but not defined", p, p);
737 /* Labels go in BLOCK_VARS. */
738 TREE_CHAIN (p) = BLOCK_VARS (block);
739 BLOCK_VARS (block) = p;
740 gcc_assert (I_LABEL_BINDING (b->id) == b);
741 I_LABEL_BINDING (b->id) = b->shadowed;
742 break;
744 case ENUMERAL_TYPE:
745 case UNION_TYPE:
746 case RECORD_TYPE:
747 set_type_context (p, context);
749 /* Types may not have tag-names, in which case the type
750 appears in the bindings list with b->id NULL. */
751 if (b->id)
753 gcc_assert (I_TAG_BINDING (b->id) == b);
754 I_TAG_BINDING (b->id) = b->shadowed;
756 break;
758 case FUNCTION_DECL:
759 /* Propagate TREE_ADDRESSABLE from nested functions to their
760 containing functions. */
761 if (!TREE_ASM_WRITTEN (p)
762 && DECL_INITIAL (p) != 0
763 && TREE_ADDRESSABLE (p)
764 && DECL_ABSTRACT_ORIGIN (p) != 0
765 && DECL_ABSTRACT_ORIGIN (p) != p)
766 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
767 if (!DECL_EXTERNAL (p)
768 && DECL_INITIAL (p) == 0)
770 error ("%Jnested function %qD declared but never defined", p, p);
771 undef_nested_function = true;
773 goto common_symbol;
775 case VAR_DECL:
776 /* Warnings for unused variables. */
777 if (warn_unused_variable
778 && !TREE_USED (p)
779 && !DECL_IN_SYSTEM_HEADER (p)
780 && DECL_NAME (p)
781 && !DECL_ARTIFICIAL (p)
782 && scope != file_scope
783 && scope != external_scope)
784 warning ("%Junused variable %qD", p, p);
786 if (b->inner_comp)
788 error ("%Jtype of array %qD completed incompatibly with"
789 " implicit initialization", p, p);
792 /* Fall through. */
793 case TYPE_DECL:
794 case CONST_DECL:
795 common_symbol:
796 /* All of these go in BLOCK_VARS, but only if this is the
797 binding in the home scope. */
798 if (!b->nested)
800 TREE_CHAIN (p) = BLOCK_VARS (block);
801 BLOCK_VARS (block) = p;
803 /* If this is the file scope, and we are processing more
804 than one translation unit in this compilation, set
805 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
806 This makes same_translation_unit_p work, and causes
807 static declarations to be given disambiguating suffixes. */
808 if (scope == file_scope && num_in_fnames > 1)
810 DECL_CONTEXT (p) = context;
811 if (TREE_CODE (p) == TYPE_DECL)
812 set_type_context (TREE_TYPE (p), context);
815 /* Fall through. */
816 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
817 already been put there by store_parm_decls. Unused-
818 parameter warnings are handled by function.c.
819 error_mark_node obviously does not go in BLOCK_VARS and
820 does not get unused-variable warnings. */
821 case PARM_DECL:
822 case ERROR_MARK:
823 /* It is possible for a decl not to have a name. We get
824 here with b->id NULL in this case. */
825 if (b->id)
827 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
828 I_SYMBOL_BINDING (b->id) = b->shadowed;
829 if (b->shadowed && b->shadowed->type)
830 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
832 break;
834 default:
835 gcc_unreachable ();
840 /* Dispose of the block that we just made inside some higher level. */
841 if ((scope->function_body || scope == file_scope) && context)
843 DECL_INITIAL (context) = block;
844 BLOCK_SUPERCONTEXT (block) = context;
846 else if (scope->outer)
848 if (block)
849 SCOPE_LIST_APPEND (scope->outer, blocks, block);
850 /* If we did not make a block for the scope just exited, any
851 blocks made for inner scopes must be carried forward so they
852 will later become subblocks of something else. */
853 else if (scope->blocks)
854 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
857 /* Pop the current scope, and free the structure for reuse. */
858 current_scope = scope->outer;
859 if (scope->function_body)
860 current_function_scope = scope->outer_function;
862 memset (scope, 0, sizeof (struct c_scope));
863 scope->outer = scope_freelist;
864 scope_freelist = scope;
866 return block;
869 void
870 push_file_scope (void)
872 tree decl;
874 if (file_scope)
875 return;
877 push_scope ();
878 file_scope = current_scope;
880 start_fname_decls ();
882 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
883 bind (DECL_NAME (decl), decl, file_scope,
884 /*invisible=*/false, /*nested=*/true);
887 void
888 pop_file_scope (void)
890 /* In case there were missing closebraces, get us back to the global
891 binding level. */
892 while (current_scope != file_scope)
893 pop_scope ();
895 /* __FUNCTION__ is defined at file scope (""). This
896 call may not be necessary as my tests indicate it
897 still works without it. */
898 finish_fname_decls ();
900 /* This is the point to write out a PCH if we're doing that.
901 In that case we do not want to do anything else. */
902 if (pch_file)
904 c_common_write_pch ();
905 return;
908 /* Pop off the file scope and close this translation unit. */
909 pop_scope ();
910 file_scope = 0;
912 maybe_apply_pending_pragma_weaks ();
913 cgraph_finalize_compilation_unit ();
916 /* Insert BLOCK at the end of the list of subblocks of the current
917 scope. This is used when a BIND_EXPR is expanded, to handle the
918 BLOCK node inside the BIND_EXPR. */
920 void
921 insert_block (tree block)
923 TREE_USED (block) = 1;
924 SCOPE_LIST_APPEND (current_scope, blocks, block);
927 /* Push a definition or a declaration of struct, union or enum tag "name".
928 "type" should be the type node.
929 We assume that the tag "name" is not already defined.
931 Note that the definition may really be just a forward reference.
932 In that case, the TYPE_SIZE will be zero. */
934 static void
935 pushtag (tree name, tree type)
937 /* Record the identifier as the type's name if it has none. */
938 if (name && !TYPE_NAME (type))
939 TYPE_NAME (type) = name;
940 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
942 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
943 tagged type we just added to the current scope. This fake
944 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
945 to output a representation of a tagged type, and it also gives
946 us a convenient place to record the "scope start" address for the
947 tagged type. */
949 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
951 /* An approximation for now, so we can tell this is a function-scope tag.
952 This will be updated in pop_scope. */
953 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
956 /* Subroutine of compare_decls. Allow harmless mismatches in return
957 and argument types provided that the type modes match. This function
958 return a unified type given a suitable match, and 0 otherwise. */
960 static tree
961 match_builtin_function_types (tree newtype, tree oldtype)
963 tree newrettype, oldrettype;
964 tree newargs, oldargs;
965 tree trytype, tryargs;
967 /* Accept the return type of the new declaration if same modes. */
968 oldrettype = TREE_TYPE (oldtype);
969 newrettype = TREE_TYPE (newtype);
971 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
972 return 0;
974 oldargs = TYPE_ARG_TYPES (oldtype);
975 newargs = TYPE_ARG_TYPES (newtype);
976 tryargs = newargs;
978 while (oldargs || newargs)
980 if (!oldargs
981 || !newargs
982 || !TREE_VALUE (oldargs)
983 || !TREE_VALUE (newargs)
984 || TYPE_MODE (TREE_VALUE (oldargs))
985 != TYPE_MODE (TREE_VALUE (newargs)))
986 return 0;
988 oldargs = TREE_CHAIN (oldargs);
989 newargs = TREE_CHAIN (newargs);
992 trytype = build_function_type (newrettype, tryargs);
993 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
996 /* Subroutine of diagnose_mismatched_decls. Check for function type
997 mismatch involving an empty arglist vs a nonempty one and give clearer
998 diagnostics. */
999 static void
1000 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1001 tree newtype, tree oldtype)
1003 tree t;
1005 if (TREE_CODE (olddecl) != FUNCTION_DECL
1006 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1007 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1009 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1010 return;
1012 t = TYPE_ARG_TYPES (oldtype);
1013 if (t == 0)
1014 t = TYPE_ARG_TYPES (newtype);
1015 for (; t; t = TREE_CHAIN (t))
1017 tree type = TREE_VALUE (t);
1019 if (TREE_CHAIN (t) == 0
1020 && TYPE_MAIN_VARIANT (type) != void_type_node)
1022 inform ("a parameter list with an ellipsis can%'t match "
1023 "an empty parameter name list declaration");
1024 break;
1027 if (c_type_promotes_to (type) != type)
1029 inform ("an argument type that has a default promotion can%'t match "
1030 "an empty parameter name list declaration");
1031 break;
1036 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1037 old-style function definition, NEWDECL is a prototype declaration.
1038 Diagnose inconsistencies in the argument list. Returns TRUE if
1039 the prototype is compatible, FALSE if not. */
1040 static bool
1041 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1043 tree newargs, oldargs;
1044 int i;
1046 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1048 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1049 newargs = TYPE_ARG_TYPES (newtype);
1050 i = 1;
1052 for (;;)
1054 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1055 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1057 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1058 break;
1060 /* Reaching the end of just one list means the two decls don't
1061 agree on the number of arguments. */
1062 if (END_OF_ARGLIST (oldargtype))
1064 error ("%Jprototype for %qD declares more arguments "
1065 "than previous old-style definition", newdecl, newdecl);
1066 return false;
1068 else if (END_OF_ARGLIST (newargtype))
1070 error ("%Jprototype for %qD declares fewer arguments "
1071 "than previous old-style definition", newdecl, newdecl);
1072 return false;
1075 /* Type for passing arg must be consistent with that declared
1076 for the arg. */
1077 else if (!comptypes (oldargtype, newargtype))
1079 error ("%Jprototype for %qD declares argument %d"
1080 " with incompatible type",
1081 newdecl, newdecl, i);
1082 return false;
1085 oldargs = TREE_CHAIN (oldargs);
1086 newargs = TREE_CHAIN (newargs);
1087 i++;
1090 /* If we get here, no errors were found, but do issue a warning
1091 for this poor-style construct. */
1092 warning ("%Jprototype for %qD follows non-prototype definition",
1093 newdecl, newdecl);
1094 return true;
1095 #undef END_OF_ARGLIST
1098 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1099 first in a pair of mismatched declarations, using the diagnostic
1100 function DIAG. */
1101 static void
1102 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1104 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1106 else if (DECL_INITIAL (decl))
1107 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1108 else if (C_DECL_IMPLICIT (decl))
1109 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1110 else
1111 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1114 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1115 Returns true if the caller should proceed to merge the two, false
1116 if OLDDECL should simply be discarded. As a side effect, issues
1117 all necessary diagnostics for invalid or poor-style combinations.
1118 If it returns true, writes the types of NEWDECL and OLDDECL to
1119 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1120 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1122 static bool
1123 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1124 tree *newtypep, tree *oldtypep)
1126 tree newtype, oldtype;
1127 bool pedwarned = false;
1128 bool warned = false;
1129 bool retval = true;
1131 /* If we have error_mark_node for either decl or type, just discard
1132 the previous decl - we're in an error cascade already. */
1133 if (olddecl == error_mark_node || newdecl == error_mark_node)
1134 return false;
1135 *oldtypep = oldtype = TREE_TYPE (olddecl);
1136 *newtypep = newtype = TREE_TYPE (newdecl);
1137 if (oldtype == error_mark_node || newtype == error_mark_node)
1138 return false;
1140 /* Two different categories of symbol altogether. This is an error
1141 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1142 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1144 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1145 && DECL_BUILT_IN (olddecl)
1146 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1148 error ("%J%qD redeclared as different kind of symbol",
1149 newdecl, newdecl);
1150 locate_old_decl (olddecl, error);
1152 else if (TREE_PUBLIC (newdecl))
1153 warning ("%Jbuilt-in function %qD declared as non-function",
1154 newdecl, newdecl);
1155 else if (warn_shadow)
1156 warning ("%Jdeclaration of %qD shadows a built-in function",
1157 newdecl, newdecl);
1158 return false;
1161 /* Enumerators have no linkage, so may only be declared once in a
1162 given scope. */
1163 if (TREE_CODE (olddecl) == CONST_DECL)
1165 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1166 locate_old_decl (olddecl, error);
1167 return false;
1170 if (!comptypes (oldtype, newtype))
1172 if (TREE_CODE (olddecl) == FUNCTION_DECL
1173 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1175 /* Accept harmless mismatch in function types.
1176 This is for the ffs and fprintf builtins. */
1177 tree trytype = match_builtin_function_types (newtype, oldtype);
1179 if (trytype && comptypes (newtype, trytype))
1180 *oldtypep = oldtype = trytype;
1181 else
1183 /* If types don't match for a built-in, throw away the
1184 built-in. No point in calling locate_old_decl here, it
1185 won't print anything. */
1186 warning ("%Jconflicting types for built-in function %qD",
1187 newdecl, newdecl);
1188 return false;
1191 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1192 && DECL_IS_BUILTIN (olddecl))
1194 /* A conflicting function declaration for a predeclared
1195 function that isn't actually built in. Objective C uses
1196 these. The new declaration silently overrides everything
1197 but the volatility (i.e. noreturn) indication. See also
1198 below. FIXME: Make Objective C use normal builtins. */
1199 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1200 return false;
1202 /* Permit void foo (...) to match int foo (...) if the latter is
1203 the definition and implicit int was used. See
1204 c-torture/compile/920625-2.c. */
1205 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1206 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1207 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1208 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1210 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1211 /* Make sure we keep void as the return type. */
1212 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1213 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1214 pedwarned = true;
1216 /* Permit void foo (...) to match an earlier call to foo (...) with
1217 no declared type (thus, implicitly int). */
1218 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1219 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1220 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1221 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1223 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1224 /* Make sure we keep void as the return type. */
1225 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1226 pedwarned = true;
1228 else
1230 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1231 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1232 else
1233 error ("%Jconflicting types for %qD", newdecl, newdecl);
1234 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1235 locate_old_decl (olddecl, error);
1236 return false;
1240 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1241 but silently ignore the redeclaration if either is in a system
1242 header. (Conflicting redeclarations were handled above.) */
1243 if (TREE_CODE (newdecl) == TYPE_DECL)
1245 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1246 return true; /* Allow OLDDECL to continue in use. */
1248 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1249 locate_old_decl (olddecl, error);
1250 return false;
1253 /* Function declarations can either be 'static' or 'extern' (no
1254 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1255 can never conflict with each other on account of linkage (6.2.2p4).
1256 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1257 two definitions if one is 'extern inline' and one is not. The non-
1258 extern-inline definition supersedes the extern-inline definition. */
1259 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1261 /* If you declare a built-in function name as static, or
1262 define the built-in with an old-style definition (so we
1263 can't validate the argument list) the built-in definition is
1264 overridden, but optionally warn this was a bad choice of name. */
1265 if (DECL_BUILT_IN (olddecl)
1266 && !C_DECL_DECLARED_BUILTIN (olddecl)
1267 && (!TREE_PUBLIC (newdecl)
1268 || (DECL_INITIAL (newdecl)
1269 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1271 if (warn_shadow)
1272 warning ("%Jdeclaration of %qD shadows a built-in function",
1273 newdecl, newdecl);
1274 /* Discard the old built-in function. */
1275 return false;
1278 if (DECL_INITIAL (newdecl))
1280 if (DECL_INITIAL (olddecl))
1282 /* If both decls have extern inline and are in the same TU,
1283 reject the new decl. */
1284 if (DECL_DECLARED_INLINE_P (olddecl)
1285 && DECL_EXTERNAL (olddecl)
1286 && DECL_DECLARED_INLINE_P (newdecl)
1287 && DECL_EXTERNAL (newdecl)
1288 && same_translation_unit_p (newdecl, olddecl))
1290 error ("%Jredefinition of %qD", newdecl, newdecl);
1291 locate_old_decl (olddecl, error);
1292 return false;
1294 /* If both decls have not extern inline, reject the new decl. */
1295 if (!DECL_DECLARED_INLINE_P (olddecl)
1296 && !DECL_EXTERNAL (olddecl)
1297 && !DECL_DECLARED_INLINE_P (newdecl)
1298 && !DECL_EXTERNAL (newdecl))
1300 error ("%Jredefinition of %qD", newdecl, newdecl);
1301 locate_old_decl (olddecl, error);
1302 return false;
1304 /* If the new decl is declared as extern inline, error if they are
1305 in the same TU, otherwise retain the old decl. */
1306 if (!DECL_DECLARED_INLINE_P (olddecl)
1307 && !DECL_EXTERNAL (olddecl)
1308 && DECL_DECLARED_INLINE_P (newdecl)
1309 && DECL_EXTERNAL (newdecl))
1311 if (same_translation_unit_p (newdecl, olddecl))
1313 error ("%Jredefinition of %qD", newdecl, newdecl);
1314 locate_old_decl (olddecl, error);
1315 return false;
1317 else
1318 retval = false;
1322 /* If we have a prototype after an old-style function definition,
1323 the argument types must be checked specially. */
1324 else if (DECL_INITIAL (olddecl)
1325 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1326 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1327 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1329 locate_old_decl (olddecl, error);
1330 return false;
1332 /* A non-static declaration (even an "extern") followed by a
1333 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1334 The same is true for a static forward declaration at block
1335 scope followed by a non-static declaration/definition at file
1336 scope. Static followed by non-static at the same scope is
1337 not undefined behavior, and is the most convenient way to get
1338 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1339 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1340 we do diagnose it if -Wtraditional. */
1341 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1343 /* Two exceptions to the rule. If olddecl is an extern
1344 inline, or a predeclared function that isn't actually
1345 built in, newdecl silently overrides olddecl. The latter
1346 occur only in Objective C; see also above. (FIXME: Make
1347 Objective C use normal builtins.) */
1348 if (!DECL_IS_BUILTIN (olddecl)
1349 && !(DECL_EXTERNAL (olddecl)
1350 && DECL_DECLARED_INLINE_P (olddecl)))
1352 error ("%Jstatic declaration of %qD follows "
1353 "non-static declaration", newdecl, newdecl);
1354 locate_old_decl (olddecl, error);
1356 return false;
1358 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1360 if (DECL_CONTEXT (olddecl))
1362 error ("%Jnon-static declaration of %qD follows "
1363 "static declaration", newdecl, newdecl);
1364 locate_old_decl (olddecl, error);
1365 return false;
1367 else if (warn_traditional)
1369 warning ("%Jnon-static declaration of %qD follows "
1370 "static declaration", newdecl, newdecl);
1371 warned = true;
1375 else if (TREE_CODE (newdecl) == VAR_DECL)
1377 /* Only variables can be thread-local, and all declarations must
1378 agree on this property. */
1379 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1381 if (DECL_THREAD_LOCAL (newdecl))
1382 error ("%Jthread-local declaration of %qD follows "
1383 "non-thread-local declaration", newdecl, newdecl);
1384 else
1385 error ("%Jnon-thread-local declaration of %qD follows "
1386 "thread-local declaration", newdecl, newdecl);
1388 locate_old_decl (olddecl, error);
1389 return false;
1392 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1393 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1395 error ("%Jredefinition of %qD", newdecl, newdecl);
1396 locate_old_decl (olddecl, error);
1397 return false;
1400 /* Objects declared at file scope: if the first declaration had
1401 external linkage (even if it was an external reference) the
1402 second must have external linkage as well, or the behavior is
1403 undefined. If the first declaration had internal linkage, then
1404 the second must too, or else be an external reference (in which
1405 case the composite declaration still has internal linkage).
1406 As for function declarations, we warn about the static-then-
1407 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1408 if (DECL_FILE_SCOPE_P (newdecl)
1409 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1411 if (DECL_EXTERNAL (newdecl))
1413 if (!DECL_FILE_SCOPE_P (olddecl))
1415 error ("%Jextern declaration of %qD follows "
1416 "declaration with no linkage", newdecl, newdecl);
1417 locate_old_decl (olddecl, error);
1418 return false;
1420 else if (warn_traditional)
1422 warning ("%Jnon-static declaration of %qD follows "
1423 "static declaration", newdecl, newdecl);
1424 warned = true;
1427 else
1429 if (TREE_PUBLIC (newdecl))
1430 error ("%Jnon-static declaration of %qD follows "
1431 "static declaration", newdecl, newdecl);
1432 else
1433 error ("%Jstatic declaration of %qD follows "
1434 "non-static declaration", newdecl, newdecl);
1436 locate_old_decl (olddecl, error);
1437 return false;
1440 /* Two objects with the same name declared at the same block
1441 scope must both be external references (6.7p3). */
1442 else if (!DECL_FILE_SCOPE_P (newdecl))
1444 if (DECL_EXTERNAL (newdecl))
1446 /* Extern with initializer at block scope, which will
1447 already have received an error. */
1449 else if (DECL_EXTERNAL (olddecl))
1451 error ("%Jdeclaration of %qD with no linkage follows "
1452 "extern declaration", newdecl, newdecl);
1453 locate_old_decl (olddecl, error);
1455 else
1457 error ("%Jredeclaration of %qD with no linkage",
1458 newdecl, newdecl);
1459 locate_old_decl (olddecl, error);
1462 return false;
1466 /* warnings */
1467 /* All decls must agree on a visibility. */
1468 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1469 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1471 warning ("%Jredeclaration of %qD with different visibility "
1472 "(old visibility preserved)", newdecl, newdecl);
1473 warned = true;
1476 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1478 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1479 if (DECL_DECLARED_INLINE_P (newdecl)
1480 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1482 warning ("%Jinline declaration of %qD follows "
1483 "declaration with attribute noinline", newdecl, newdecl);
1484 warned = true;
1486 else if (DECL_DECLARED_INLINE_P (olddecl)
1487 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1489 warning ("%Jdeclaration of %qD with attribute noinline follows "
1490 "inline declaration ", newdecl, newdecl);
1491 warned = true;
1494 /* Inline declaration after use or definition.
1495 ??? Should we still warn about this now we have unit-at-a-time
1496 mode and can get it right?
1497 Definitely don't complain if the decls are in different translation
1498 units. */
1499 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1500 && same_translation_unit_p (olddecl, newdecl))
1502 if (TREE_USED (olddecl))
1504 warning ("%J%qD declared inline after being called",
1505 olddecl, olddecl);
1506 warned = true;
1508 else if (DECL_INITIAL (olddecl))
1510 warning ("%J%qD declared inline after its definition",
1511 olddecl, olddecl);
1512 warned = true;
1516 else /* PARM_DECL, VAR_DECL */
1518 /* Redeclaration of a parameter is a constraint violation (this is
1519 not explicitly stated, but follows from C99 6.7p3 [no more than
1520 one declaration of the same identifier with no linkage in the
1521 same scope, except type tags] and 6.2.2p6 [parameters have no
1522 linkage]). We must check for a forward parameter declaration,
1523 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1524 an extension, the mandatory diagnostic for which is handled by
1525 mark_forward_parm_decls. */
1527 if (TREE_CODE (newdecl) == PARM_DECL
1528 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1530 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1531 locate_old_decl (olddecl, error);
1532 return false;
1536 /* Optional warning for completely redundant decls. */
1537 if (!warned && !pedwarned
1538 && warn_redundant_decls
1539 /* Don't warn about a function declaration followed by a
1540 definition. */
1541 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1542 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1543 /* Don't warn about redundant redeclarations of builtins. */
1544 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1545 && !DECL_BUILT_IN (newdecl)
1546 && DECL_BUILT_IN (olddecl)
1547 && !C_DECL_DECLARED_BUILTIN (olddecl))
1548 /* Don't warn about an extern followed by a definition. */
1549 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1550 /* Don't warn about forward parameter decls. */
1551 && !(TREE_CODE (newdecl) == PARM_DECL
1552 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1554 warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
1555 warned = true;
1558 /* Report location of previous decl/defn in a consistent manner. */
1559 if (warned || pedwarned)
1560 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1562 return retval;
1565 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1566 consistent with OLDDECL, but carries new information. Merge the
1567 new information into OLDDECL. This function issues no
1568 diagnostics. */
1570 static void
1571 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1573 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1574 && DECL_INITIAL (newdecl) != 0);
1576 /* For real parm decl following a forward decl, rechain the old decl
1577 in its new location and clear TREE_ASM_WRITTEN (it's not a
1578 forward decl anymore). */
1579 if (TREE_CODE (newdecl) == PARM_DECL
1580 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1582 struct c_binding *b, **here;
1584 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1585 if ((*here)->decl == olddecl)
1586 goto found;
1587 gcc_unreachable ();
1589 found:
1590 b = *here;
1591 *here = b->prev;
1592 b->prev = current_scope->bindings;
1593 current_scope->bindings = b;
1595 TREE_ASM_WRITTEN (olddecl) = 0;
1598 DECL_ATTRIBUTES (newdecl)
1599 = targetm.merge_decl_attributes (olddecl, newdecl);
1601 /* Merge the data types specified in the two decls. */
1602 TREE_TYPE (newdecl)
1603 = TREE_TYPE (olddecl)
1604 = composite_type (newtype, oldtype);
1606 /* Lay the type out, unless already done. */
1607 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1609 if (TREE_TYPE (newdecl) != error_mark_node)
1610 layout_type (TREE_TYPE (newdecl));
1611 if (TREE_CODE (newdecl) != FUNCTION_DECL
1612 && TREE_CODE (newdecl) != TYPE_DECL
1613 && TREE_CODE (newdecl) != CONST_DECL)
1614 layout_decl (newdecl, 0);
1616 else
1618 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1619 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1620 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1621 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1622 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1623 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1625 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1626 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1630 /* Keep the old rtl since we can safely use it. */
1631 COPY_DECL_RTL (olddecl, newdecl);
1633 /* Merge the type qualifiers. */
1634 if (TREE_READONLY (newdecl))
1635 TREE_READONLY (olddecl) = 1;
1637 if (TREE_THIS_VOLATILE (newdecl))
1639 TREE_THIS_VOLATILE (olddecl) = 1;
1640 if (TREE_CODE (newdecl) == VAR_DECL)
1641 make_var_volatile (newdecl);
1644 /* Merge deprecatedness. */
1645 if (TREE_DEPRECATED (newdecl))
1646 TREE_DEPRECATED (olddecl) = 1;
1648 /* Keep source location of definition rather than declaration. */
1649 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1650 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1652 /* Merge the unused-warning information. */
1653 if (DECL_IN_SYSTEM_HEADER (olddecl))
1654 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1655 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1656 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1658 /* Merge the initialization information. */
1659 if (DECL_INITIAL (newdecl) == 0)
1660 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1662 /* Merge the section attribute.
1663 We want to issue an error if the sections conflict but that must be
1664 done later in decl_attributes since we are called before attributes
1665 are assigned. */
1666 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1667 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1669 /* Copy the assembler name.
1670 Currently, it can only be defined in the prototype. */
1671 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1673 /* Use visibility of whichever declaration had it specified */
1674 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1676 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1677 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1680 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1682 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1683 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1684 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1685 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1686 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1687 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1688 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1689 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1690 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1691 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1694 /* Merge the storage class information. */
1695 merge_weak (newdecl, olddecl);
1697 /* For functions, static overrides non-static. */
1698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1700 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1701 /* This is since we don't automatically
1702 copy the attributes of NEWDECL into OLDDECL. */
1703 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1704 /* If this clears `static', clear it in the identifier too. */
1705 if (!TREE_PUBLIC (olddecl))
1706 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1708 if (DECL_EXTERNAL (newdecl))
1710 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1711 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1713 /* An extern decl does not override previous storage class. */
1714 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1715 if (!DECL_EXTERNAL (newdecl))
1717 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1718 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1721 else
1723 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1724 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1727 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1729 /* If we're redefining a function previously defined as extern
1730 inline, make sure we emit debug info for the inline before we
1731 throw it away, in case it was inlined into a function that hasn't
1732 been written out yet. */
1733 if (new_is_definition && DECL_INITIAL (olddecl))
1735 if (TREE_USED (olddecl)
1736 /* In unit-at-a-time mode we never inline re-defined extern
1737 inline functions. */
1738 && !flag_unit_at_a_time
1739 && cgraph_function_possibly_inlined_p (olddecl))
1740 (*debug_hooks->outlining_inline_function) (olddecl);
1742 /* The new defn must not be inline. */
1743 DECL_INLINE (newdecl) = 0;
1744 DECL_UNINLINABLE (newdecl) = 1;
1746 else
1748 /* If either decl says `inline', this fn is inline,
1749 unless its definition was passed already. */
1750 if (DECL_DECLARED_INLINE_P (newdecl)
1751 || DECL_DECLARED_INLINE_P (olddecl))
1752 DECL_DECLARED_INLINE_P (newdecl) = 1;
1754 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1755 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1758 if (DECL_BUILT_IN (olddecl))
1760 /* If redeclaring a builtin function, it stays built in.
1761 But it gets tagged as having been declared. */
1762 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1763 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1764 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1767 /* Also preserve various other info from the definition. */
1768 if (!new_is_definition)
1770 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1771 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1772 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1773 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1774 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1776 /* Set DECL_INLINE on the declaration if we've got a body
1777 from which to instantiate. */
1778 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1780 DECL_INLINE (newdecl) = 1;
1781 DECL_ABSTRACT_ORIGIN (newdecl)
1782 = DECL_ABSTRACT_ORIGIN (olddecl);
1785 else
1787 /* If a previous declaration said inline, mark the
1788 definition as inlinable. */
1789 if (DECL_DECLARED_INLINE_P (newdecl)
1790 && !DECL_UNINLINABLE (newdecl))
1791 DECL_INLINE (newdecl) = 1;
1795 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1796 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1798 unsigned olddecl_uid = DECL_UID (olddecl);
1799 tree olddecl_context = DECL_CONTEXT (olddecl);
1801 memcpy ((char *) olddecl + sizeof (struct tree_common),
1802 (char *) newdecl + sizeof (struct tree_common),
1803 sizeof (struct tree_decl) - sizeof (struct tree_common));
1804 DECL_UID (olddecl) = olddecl_uid;
1805 DECL_CONTEXT (olddecl) = olddecl_context;
1808 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1809 so that encode_section_info has a chance to look at the new decl
1810 flags and attributes. */
1811 if (DECL_RTL_SET_P (olddecl)
1812 && (TREE_CODE (olddecl) == FUNCTION_DECL
1813 || (TREE_CODE (olddecl) == VAR_DECL
1814 && TREE_STATIC (olddecl))))
1815 make_decl_rtl (olddecl);
1818 /* Handle when a new declaration NEWDECL has the same name as an old
1819 one OLDDECL in the same binding contour. Prints an error message
1820 if appropriate.
1822 If safely possible, alter OLDDECL to look like NEWDECL, and return
1823 true. Otherwise, return false. */
1825 static bool
1826 duplicate_decls (tree newdecl, tree olddecl)
1828 tree newtype = NULL, oldtype = NULL;
1830 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1831 return false;
1833 merge_decls (newdecl, olddecl, newtype, oldtype);
1834 return true;
1838 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1839 static void
1840 warn_if_shadowing (tree new_decl)
1842 struct c_binding *b;
1844 /* Shadow warnings wanted? */
1845 if (!warn_shadow
1846 /* No shadow warnings for internally generated vars. */
1847 || DECL_IS_BUILTIN (new_decl)
1848 /* No shadow warnings for vars made for inlining. */
1849 || DECL_FROM_INLINE (new_decl)
1850 /* Don't warn about the parm names in function declarator
1851 within a function declarator. It would be nice to avoid
1852 warning in any function declarator in a declaration, as
1853 opposed to a definition, but there is no way to tell
1854 it's not a definition at this point. */
1855 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1856 return;
1858 /* Is anything being shadowed? Invisible decls do not count. */
1859 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1860 if (b->decl && b->decl != new_decl && !b->invisible)
1862 tree old_decl = b->decl;
1864 if (old_decl == error_mark_node)
1866 warning ("%Jdeclaration of %qD shadows previous non-variable",
1867 new_decl, new_decl);
1868 break;
1870 else if (TREE_CODE (old_decl) == PARM_DECL)
1871 warning ("%Jdeclaration of %qD shadows a parameter",
1872 new_decl, new_decl);
1873 else if (DECL_FILE_SCOPE_P (old_decl))
1874 warning ("%Jdeclaration of %qD shadows a global declaration",
1875 new_decl, new_decl);
1876 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1877 && DECL_BUILT_IN (old_decl))
1879 warning ("%Jdeclaration of %qD shadows a built-in function",
1880 new_decl, new_decl);
1881 break;
1883 else
1884 warning ("%Jdeclaration of %qD shadows a previous local",
1885 new_decl, new_decl);
1887 warning ("%Jshadowed declaration is here", old_decl);
1889 break;
1894 /* Subroutine of pushdecl.
1896 X is a TYPE_DECL for a typedef statement. Create a brand new
1897 ..._TYPE node (which will be just a variant of the existing
1898 ..._TYPE node with identical properties) and then install X
1899 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1901 The whole point here is to end up with a situation where each
1902 and every ..._TYPE node the compiler creates will be uniquely
1903 associated with AT MOST one node representing a typedef name.
1904 This way, even though the compiler substitutes corresponding
1905 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1906 early on, later parts of the compiler can always do the reverse
1907 translation and get back the corresponding typedef name. For
1908 example, given:
1910 typedef struct S MY_TYPE;
1911 MY_TYPE object;
1913 Later parts of the compiler might only know that `object' was of
1914 type `struct S' if it were not for code just below. With this
1915 code however, later parts of the compiler see something like:
1917 struct S' == struct S
1918 typedef struct S' MY_TYPE;
1919 struct S' object;
1921 And they can then deduce (from the node for type struct S') that
1922 the original object declaration was:
1924 MY_TYPE object;
1926 Being able to do this is important for proper support of protoize,
1927 and also for generating precise symbolic debugging information
1928 which takes full account of the programmer's (typedef) vocabulary.
1930 Obviously, we don't want to generate a duplicate ..._TYPE node if
1931 the TYPE_DECL node that we are now processing really represents a
1932 standard built-in type.
1934 Since all standard types are effectively declared at line zero
1935 in the source file, we can easily check to see if we are working
1936 on a standard type by checking the current value of lineno. */
1938 static void
1939 clone_underlying_type (tree x)
1941 if (DECL_IS_BUILTIN (x))
1943 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1944 TYPE_NAME (TREE_TYPE (x)) = x;
1946 else if (TREE_TYPE (x) != error_mark_node
1947 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1949 tree tt = TREE_TYPE (x);
1950 DECL_ORIGINAL_TYPE (x) = tt;
1951 tt = build_variant_type_copy (tt);
1952 TYPE_NAME (tt) = x;
1953 TREE_USED (tt) = TREE_USED (x);
1954 TREE_TYPE (x) = tt;
1958 /* Record a decl-node X as belonging to the current lexical scope.
1959 Check for errors (such as an incompatible declaration for the same
1960 name already seen in the same scope).
1962 Returns either X or an old decl for the same name.
1963 If an old decl is returned, it may have been smashed
1964 to agree with what X says. */
1966 tree
1967 pushdecl (tree x)
1969 tree name = DECL_NAME (x);
1970 struct c_scope *scope = current_scope;
1971 struct c_binding *b;
1972 bool nested = false;
1974 /* Functions need the lang_decl data. */
1975 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1976 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1978 /* Must set DECL_CONTEXT for everything not at file scope or
1979 DECL_FILE_SCOPE_P won't work. Local externs don't count
1980 unless they have initializers (which generate code). */
1981 if (current_function_decl
1982 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1983 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1984 DECL_CONTEXT (x) = current_function_decl;
1986 /* Anonymous decls are just inserted in the scope. */
1987 if (!name)
1989 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1990 return x;
1993 /* First, see if there is another declaration with the same name in
1994 the current scope. If there is, duplicate_decls may do all the
1995 work for us. If duplicate_decls returns false, that indicates
1996 two incompatible decls in the same scope; we are to silently
1997 replace the old one (duplicate_decls has issued all appropriate
1998 diagnostics). In particular, we should not consider possible
1999 duplicates in the external scope, or shadowing. */
2000 b = I_SYMBOL_BINDING (name);
2001 if (b && B_IN_SCOPE (b, scope))
2003 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2004 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2005 b->inner_comp = false;
2006 if (duplicate_decls (x, b->decl))
2007 return b->decl;
2008 else
2009 goto skip_external_and_shadow_checks;
2012 /* All declarations with external linkage, and all external
2013 references, go in the external scope, no matter what scope is
2014 current. However, the binding in that scope is ignored for
2015 purposes of normal name lookup. A separate binding structure is
2016 created in the requested scope; this governs the normal
2017 visibility of the symbol.
2019 The binding in the externals scope is used exclusively for
2020 detecting duplicate declarations of the same object, no matter
2021 what scope they are in; this is what we do here. (C99 6.2.7p2:
2022 All declarations that refer to the same object or function shall
2023 have compatible type; otherwise, the behavior is undefined.) */
2024 if (DECL_EXTERNAL (x) || scope == file_scope)
2026 tree type = TREE_TYPE (x);
2027 tree vistype = 0;
2028 tree visdecl = 0;
2029 bool type_saved = false;
2030 if (b && !B_IN_EXTERNAL_SCOPE (b)
2031 && (TREE_CODE (b->decl) == FUNCTION_DECL
2032 || TREE_CODE (b->decl) == VAR_DECL)
2033 && DECL_FILE_SCOPE_P (b->decl))
2035 visdecl = b->decl;
2036 vistype = TREE_TYPE (visdecl);
2038 if (warn_nested_externs
2039 && scope != file_scope
2040 && !DECL_IN_SYSTEM_HEADER (x))
2041 warning ("nested extern declaration of %qD", x);
2043 while (b && !B_IN_EXTERNAL_SCOPE (b))
2045 /* If this decl might be modified, save its type. This is
2046 done here rather than when the decl is first bound
2047 because the type may change after first binding, through
2048 being completed or through attributes being added. If we
2049 encounter multiple such decls, only the first should have
2050 its type saved; the others will already have had their
2051 proper types saved and the types will not have changed as
2052 their scopes will not have been re-entered. */
2053 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2055 b->type = TREE_TYPE (b->decl);
2056 type_saved = true;
2058 if (B_IN_FILE_SCOPE (b)
2059 && TREE_CODE (b->decl) == VAR_DECL
2060 && TREE_STATIC (b->decl)
2061 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2062 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2063 && TREE_CODE (type) == ARRAY_TYPE
2064 && TYPE_DOMAIN (type)
2065 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2066 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2068 /* Array type completed in inner scope, which should be
2069 diagnosed if the completion does not have size 1 and
2070 it does not get completed in the file scope. */
2071 b->inner_comp = true;
2073 b = b->shadowed;
2076 /* If a matching external declaration has been found, set its
2077 type to the composite of all the types of that declaration.
2078 After the consistency checks, it will be reset to the
2079 composite of the visible types only. */
2080 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2081 && b->type)
2082 TREE_TYPE (b->decl) = b->type;
2084 /* The point of the same_translation_unit_p check here is,
2085 we want to detect a duplicate decl for a construct like
2086 foo() { extern bar(); } ... static bar(); but not if
2087 they are in different translation units. In any case,
2088 the static does not go in the externals scope. */
2089 if (b
2090 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2091 && duplicate_decls (x, b->decl))
2093 tree thistype;
2094 thistype = (vistype ? composite_type (vistype, type) : type);
2095 b->type = TREE_TYPE (b->decl);
2096 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2097 thistype
2098 = build_type_attribute_variant (thistype,
2099 TYPE_ATTRIBUTES (b->type));
2100 TREE_TYPE (b->decl) = thistype;
2101 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2102 return b->decl;
2104 else if (TREE_PUBLIC (x))
2106 if (visdecl && !b && duplicate_decls (x, visdecl))
2108 /* An external declaration at block scope referring to a
2109 visible entity with internal linkage. The composite
2110 type will already be correct for this scope, so we
2111 just need to fall through to make the declaration in
2112 this scope. */
2113 nested = true;
2114 x = visdecl;
2116 else
2118 bind (name, x, external_scope, /*invisible=*/true,
2119 /*nested=*/false);
2120 nested = true;
2125 warn_if_shadowing (x);
2127 skip_external_and_shadow_checks:
2128 if (TREE_CODE (x) == TYPE_DECL)
2129 clone_underlying_type (x);
2131 bind (name, x, scope, /*invisible=*/false, nested);
2133 /* If x's type is incomplete because it's based on a
2134 structure or union which has not yet been fully declared,
2135 attach it to that structure or union type, so we can go
2136 back and complete the variable declaration later, if the
2137 structure or union gets fully declared.
2139 If the input is erroneous, we can have error_mark in the type
2140 slot (e.g. "f(void a, ...)") - that doesn't count as an
2141 incomplete type. */
2142 if (TREE_TYPE (x) != error_mark_node
2143 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2145 tree element = TREE_TYPE (x);
2147 while (TREE_CODE (element) == ARRAY_TYPE)
2148 element = TREE_TYPE (element);
2149 element = TYPE_MAIN_VARIANT (element);
2151 if ((TREE_CODE (element) == RECORD_TYPE
2152 || TREE_CODE (element) == UNION_TYPE)
2153 && (TREE_CODE (x) != TYPE_DECL
2154 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2155 && !COMPLETE_TYPE_P (element))
2156 C_TYPE_INCOMPLETE_VARS (element)
2157 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2159 return x;
2162 /* Record X as belonging to file scope.
2163 This is used only internally by the Objective-C front end,
2164 and is limited to its needs. duplicate_decls is not called;
2165 if there is any preexisting decl for this identifier, it is an ICE. */
2167 tree
2168 pushdecl_top_level (tree x)
2170 tree name;
2171 bool nested = false;
2172 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2174 name = DECL_NAME (x);
2176 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2178 if (TREE_PUBLIC (x))
2180 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2181 nested = true;
2183 if (file_scope)
2184 bind (name, x, file_scope, /*invisible=*/false, nested);
2186 return x;
2189 static void
2190 implicit_decl_warning (tree id, tree olddecl)
2192 void (*diag) (const char *, ...);
2193 switch (mesg_implicit_function_declaration)
2195 case 0: return;
2196 case 1: diag = warning; break;
2197 case 2: diag = error; break;
2198 default: gcc_unreachable ();
2201 diag (N_("implicit declaration of function %qE"), id);
2202 if (olddecl)
2203 locate_old_decl (olddecl, diag);
2206 /* Generate an implicit declaration for identifier FUNCTIONID as a
2207 function of type int (). */
2209 tree
2210 implicitly_declare (tree functionid)
2212 struct c_binding *b;
2213 tree decl = 0;
2214 tree asmspec_tree;
2216 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2218 if (B_IN_SCOPE (b, external_scope))
2220 decl = b->decl;
2221 break;
2225 if (decl)
2227 if (decl == error_mark_node)
2228 return decl;
2230 /* FIXME: Objective-C has weird not-really-builtin functions
2231 which are supposed to be visible automatically. They wind up
2232 in the external scope because they're pushed before the file
2233 scope gets created. Catch this here and rebind them into the
2234 file scope. */
2235 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2237 bind (functionid, decl, file_scope,
2238 /*invisible=*/false, /*nested=*/true);
2239 return decl;
2241 else
2243 tree newtype = default_function_type;
2244 if (b->type)
2245 TREE_TYPE (decl) = b->type;
2246 /* Implicit declaration of a function already declared
2247 (somehow) in a different scope, or as a built-in.
2248 If this is the first time this has happened, warn;
2249 then recycle the old declaration but with the new type. */
2250 if (!C_DECL_IMPLICIT (decl))
2252 implicit_decl_warning (functionid, decl);
2253 C_DECL_IMPLICIT (decl) = 1;
2255 if (DECL_BUILT_IN (decl))
2257 newtype = build_type_attribute_variant (newtype,
2258 TYPE_ATTRIBUTES
2259 (TREE_TYPE (decl)));
2260 if (!comptypes (newtype, TREE_TYPE (decl)))
2262 warning ("incompatible implicit declaration of built-in"
2263 " function %qD", decl);
2264 newtype = TREE_TYPE (decl);
2267 else
2269 if (!comptypes (newtype, TREE_TYPE (decl)))
2271 error ("incompatible implicit declaration of function %qD",
2272 decl);
2273 locate_old_decl (decl, error);
2276 b->type = TREE_TYPE (decl);
2277 TREE_TYPE (decl) = newtype;
2278 bind (functionid, decl, current_scope,
2279 /*invisible=*/false, /*nested=*/true);
2280 return decl;
2284 /* Not seen before. */
2285 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2286 DECL_EXTERNAL (decl) = 1;
2287 TREE_PUBLIC (decl) = 1;
2288 C_DECL_IMPLICIT (decl) = 1;
2289 implicit_decl_warning (functionid, 0);
2290 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2291 if (asmspec_tree)
2292 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2294 /* C89 says implicit declarations are in the innermost block.
2295 So we record the decl in the standard fashion. */
2296 decl = pushdecl (decl);
2298 /* No need to call objc_check_decl here - it's a function type. */
2299 rest_of_decl_compilation (decl, 0, 0);
2301 /* Write a record describing this implicit function declaration
2302 to the prototypes file (if requested). */
2303 gen_aux_info_record (decl, 0, 1, 0);
2305 /* Possibly apply some default attributes to this implicit declaration. */
2306 decl_attributes (&decl, NULL_TREE, 0);
2308 return decl;
2311 /* Issue an error message for a reference to an undeclared variable
2312 ID, including a reference to a builtin outside of function-call
2313 context. Establish a binding of the identifier to error_mark_node
2314 in an appropriate scope, which will suppress further errors for the
2315 same identifier. The error message should be given location LOC. */
2316 void
2317 undeclared_variable (tree id, location_t loc)
2319 static bool already = false;
2320 struct c_scope *scope;
2322 if (current_function_decl == 0)
2324 error ("%H%qE undeclared here (not in a function)", &loc, id);
2325 scope = current_scope;
2327 else
2329 error ("%H%qE undeclared (first use in this function)", &loc, id);
2331 if (!already)
2333 error ("%H(Each undeclared identifier is reported only once", &loc);
2334 error ("%Hfor each function it appears in.)", &loc);
2335 already = true;
2338 /* If we are parsing old-style parameter decls, current_function_decl
2339 will be nonnull but current_function_scope will be null. */
2340 scope = current_function_scope ? current_function_scope : current_scope;
2342 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2345 /* Subroutine of lookup_label, declare_label, define_label: construct a
2346 LABEL_DECL with all the proper frills. */
2348 static tree
2349 make_label (tree name, location_t location)
2351 tree label = build_decl (LABEL_DECL, name, void_type_node);
2353 DECL_CONTEXT (label) = current_function_decl;
2354 DECL_MODE (label) = VOIDmode;
2355 DECL_SOURCE_LOCATION (label) = location;
2357 return label;
2360 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2361 Create one if none exists so far for the current function.
2362 This is called when a label is used in a goto expression or
2363 has its address taken. */
2365 tree
2366 lookup_label (tree name)
2368 tree label;
2370 if (current_function_decl == 0)
2372 error ("label %qE referenced outside of any function", name);
2373 return 0;
2376 /* Use a label already defined or ref'd with this name, but not if
2377 it is inherited from a containing function and wasn't declared
2378 using __label__. */
2379 label = I_LABEL_DECL (name);
2380 if (label && (DECL_CONTEXT (label) == current_function_decl
2381 || C_DECLARED_LABEL_FLAG (label)))
2383 /* If the label has only been declared, update its apparent
2384 location to point here, for better diagnostics if it
2385 turns out not to have been defined. */
2386 if (!TREE_USED (label))
2387 DECL_SOURCE_LOCATION (label) = input_location;
2388 return label;
2391 /* No label binding for that identifier; make one. */
2392 label = make_label (name, input_location);
2394 /* Ordinary labels go in the current function scope. */
2395 bind (name, label, current_function_scope,
2396 /*invisible=*/false, /*nested=*/false);
2397 return label;
2400 /* Make a label named NAME in the current function, shadowing silently
2401 any that may be inherited from containing functions or containing
2402 scopes. This is called for __label__ declarations. */
2404 tree
2405 declare_label (tree name)
2407 struct c_binding *b = I_LABEL_BINDING (name);
2408 tree label;
2410 /* Check to make sure that the label hasn't already been declared
2411 at this scope */
2412 if (b && B_IN_CURRENT_SCOPE (b))
2414 error ("duplicate label declaration %qE", name);
2415 locate_old_decl (b->decl, error);
2417 /* Just use the previous declaration. */
2418 return b->decl;
2421 label = make_label (name, input_location);
2422 C_DECLARED_LABEL_FLAG (label) = 1;
2424 /* Declared labels go in the current scope. */
2425 bind (name, label, current_scope,
2426 /*invisible=*/false, /*nested=*/false);
2427 return label;
2430 /* Define a label, specifying the location in the source file.
2431 Return the LABEL_DECL node for the label, if the definition is valid.
2432 Otherwise return 0. */
2434 tree
2435 define_label (location_t location, tree name)
2437 /* Find any preexisting label with this name. It is an error
2438 if that label has already been defined in this function, or
2439 if there is a containing function with a declared label with
2440 the same name. */
2441 tree label = I_LABEL_DECL (name);
2442 struct c_label_list *nlist;
2444 if (label
2445 && ((DECL_CONTEXT (label) == current_function_decl
2446 && DECL_INITIAL (label) != 0)
2447 || (DECL_CONTEXT (label) != current_function_decl
2448 && C_DECLARED_LABEL_FLAG (label))))
2450 error ("%Hduplicate label %qD", &location, label);
2451 locate_old_decl (label, error);
2452 return 0;
2454 else if (label && DECL_CONTEXT (label) == current_function_decl)
2456 /* The label has been used or declared already in this function,
2457 but not defined. Update its location to point to this
2458 definition. */
2459 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2460 error ("%Jjump into statement expression", label);
2461 DECL_SOURCE_LOCATION (label) = location;
2463 else
2465 /* No label binding for that identifier; make one. */
2466 label = make_label (name, location);
2468 /* Ordinary labels go in the current function scope. */
2469 bind (name, label, current_function_scope,
2470 /*invisible=*/false, /*nested=*/false);
2473 if (warn_traditional && !in_system_header && lookup_name (name))
2474 warning ("%Htraditional C lacks a separate namespace for labels, "
2475 "identifier %qE conflicts", &location, name);
2477 nlist = XOBNEW (&parser_obstack, struct c_label_list);
2478 nlist->next = label_context_stack->labels_def;
2479 nlist->label = label;
2480 label_context_stack->labels_def = nlist;
2482 /* Mark label as having been defined. */
2483 DECL_INITIAL (label) = error_mark_node;
2484 return label;
2487 /* Given NAME, an IDENTIFIER_NODE,
2488 return the structure (or union or enum) definition for that name.
2489 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2490 CODE says which kind of type the caller wants;
2491 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2492 If the wrong kind of type is found, an error is reported. */
2494 static tree
2495 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2497 struct c_binding *b = I_TAG_BINDING (name);
2498 int thislevel = 0;
2500 if (!b || !b->decl)
2501 return 0;
2503 /* We only care about whether it's in this level if
2504 thislevel_only was set or it might be a type clash. */
2505 if (thislevel_only || TREE_CODE (b->decl) != code)
2507 /* For our purposes, a tag in the external scope is the same as
2508 a tag in the file scope. (Primarily relevant to Objective-C
2509 and its builtin structure tags, which get pushed before the
2510 file scope is created.) */
2511 if (B_IN_CURRENT_SCOPE (b)
2512 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2513 thislevel = 1;
2516 if (thislevel_only && !thislevel)
2517 return 0;
2519 if (TREE_CODE (b->decl) != code)
2521 /* Definition isn't the kind we were looking for. */
2522 pending_invalid_xref = name;
2523 pending_invalid_xref_location = input_location;
2525 /* If in the same binding level as a declaration as a tag
2526 of a different type, this must not be allowed to
2527 shadow that tag, so give the error immediately.
2528 (For example, "struct foo; union foo;" is invalid.) */
2529 if (thislevel)
2530 pending_xref_error ();
2532 return b->decl;
2535 /* Print an error message now
2536 for a recent invalid struct, union or enum cross reference.
2537 We don't print them immediately because they are not invalid
2538 when used in the `struct foo;' construct for shadowing. */
2540 void
2541 pending_xref_error (void)
2543 if (pending_invalid_xref != 0)
2544 error ("%H%qE defined as wrong kind of tag",
2545 &pending_invalid_xref_location, pending_invalid_xref);
2546 pending_invalid_xref = 0;
2550 /* Look up NAME in the current scope and its superiors
2551 in the namespace of variables, functions and typedefs.
2552 Return a ..._DECL node of some kind representing its definition,
2553 or return 0 if it is undefined. */
2555 tree
2556 lookup_name (tree name)
2558 struct c_binding *b = I_SYMBOL_BINDING (name);
2559 if (b && !b->invisible)
2560 return b->decl;
2561 return 0;
2564 /* Similar to `lookup_name' but look only at the indicated scope. */
2566 static tree
2567 lookup_name_in_scope (tree name, struct c_scope *scope)
2569 struct c_binding *b;
2571 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2572 if (B_IN_SCOPE (b, scope))
2573 return b->decl;
2574 return 0;
2577 /* Create the predefined scalar types of C,
2578 and some nodes representing standard constants (0, 1, (void *) 0).
2579 Initialize the global scope.
2580 Make definitions for built-in primitive functions. */
2582 void
2583 c_init_decl_processing (void)
2585 location_t save_loc = input_location;
2587 /* Initialize reserved words for parser. */
2588 c_parse_init ();
2590 current_function_decl = 0;
2592 gcc_obstack_init (&parser_obstack);
2594 /* Make the externals scope. */
2595 push_scope ();
2596 external_scope = current_scope;
2598 /* Declarations from c_common_nodes_and_builtins must not be associated
2599 with this input file, lest we get differences between using and not
2600 using preprocessed headers. */
2601 #ifdef USE_MAPPED_LOCATION
2602 input_location = BUILTINS_LOCATION;
2603 #else
2604 input_location.file = "<built-in>";
2605 input_location.line = 0;
2606 #endif
2608 build_common_tree_nodes (flag_signed_char, false);
2610 c_common_nodes_and_builtins ();
2612 /* In C, comparisons and TRUTH_* expressions have type int. */
2613 truthvalue_type_node = integer_type_node;
2614 truthvalue_true_node = integer_one_node;
2615 truthvalue_false_node = integer_zero_node;
2617 /* Even in C99, which has a real boolean type. */
2618 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2619 boolean_type_node));
2621 input_location = save_loc;
2623 pedantic_lvalues = true;
2625 make_fname_decl = c_make_fname_decl;
2626 start_fname_decls ();
2629 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2630 decl, NAME is the initialization string and TYPE_DEP indicates whether
2631 NAME depended on the type of the function. As we don't yet implement
2632 delayed emission of static data, we mark the decl as emitted
2633 so it is not placed in the output. Anything using it must therefore pull
2634 out the STRING_CST initializer directly. FIXME. */
2636 static tree
2637 c_make_fname_decl (tree id, int type_dep)
2639 const char *name = fname_as_string (type_dep);
2640 tree decl, type, init;
2641 size_t length = strlen (name);
2643 type = build_array_type (char_type_node,
2644 build_index_type (size_int (length)));
2645 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2647 decl = build_decl (VAR_DECL, id, type);
2649 TREE_STATIC (decl) = 1;
2650 TREE_READONLY (decl) = 1;
2651 DECL_ARTIFICIAL (decl) = 1;
2653 init = build_string (length + 1, name);
2654 free ((char *) name);
2655 TREE_TYPE (init) = type;
2656 DECL_INITIAL (decl) = init;
2658 TREE_USED (decl) = 1;
2660 if (current_function_decl)
2662 DECL_CONTEXT (decl) = current_function_decl;
2663 bind (id, decl, current_function_scope,
2664 /*invisible=*/false, /*nested=*/false);
2667 finish_decl (decl, init, NULL_TREE);
2669 return decl;
2672 /* Return a definition for a builtin function named NAME and whose data type
2673 is TYPE. TYPE should be a function type with argument types.
2674 FUNCTION_CODE tells later passes how to compile calls to this function.
2675 See tree.h for its possible values.
2677 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2678 the name to be called if we can't opencode the function. If
2679 ATTRS is nonzero, use that for the function's attribute list. */
2681 tree
2682 builtin_function (const char *name, tree type, int function_code,
2683 enum built_in_class cl, const char *library_name,
2684 tree attrs)
2686 tree id = get_identifier (name);
2687 tree decl = build_decl (FUNCTION_DECL, id, type);
2688 TREE_PUBLIC (decl) = 1;
2689 DECL_EXTERNAL (decl) = 1;
2690 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2691 DECL_BUILT_IN_CLASS (decl) = cl;
2692 DECL_FUNCTION_CODE (decl) = function_code;
2693 if (library_name)
2694 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2696 /* Should never be called on a symbol with a preexisting meaning. */
2697 gcc_assert (!I_SYMBOL_BINDING (id));
2699 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2701 /* Builtins in the implementation namespace are made visible without
2702 needing to be explicitly declared. See push_file_scope. */
2703 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2705 TREE_CHAIN (decl) = visible_builtins;
2706 visible_builtins = decl;
2709 /* Possibly apply some default attributes to this built-in function. */
2710 if (attrs)
2711 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2712 else
2713 decl_attributes (&decl, NULL_TREE, 0);
2715 return decl;
2718 /* Called when a declaration is seen that contains no names to declare.
2719 If its type is a reference to a structure, union or enum inherited
2720 from a containing scope, shadow that tag name for the current scope
2721 with a forward reference.
2722 If its type defines a new named structure or union
2723 or defines an enum, it is valid but we need not do anything here.
2724 Otherwise, it is an error. */
2726 void
2727 shadow_tag (const struct c_declspecs *declspecs)
2729 shadow_tag_warned (declspecs, 0);
2732 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2733 but no pedwarn. */
2734 void
2735 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2737 bool found_tag = false;
2739 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2741 tree value = declspecs->type;
2742 enum tree_code code = TREE_CODE (value);
2744 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2745 /* Used to test also that TYPE_SIZE (value) != 0.
2746 That caused warning for `struct foo;' at top level in the file. */
2748 tree name = TYPE_NAME (value);
2749 tree t;
2751 found_tag = true;
2753 if (name == 0)
2755 if (warned != 1 && code != ENUMERAL_TYPE)
2756 /* Empty unnamed enum OK */
2758 pedwarn ("unnamed struct/union that defines no instances");
2759 warned = 1;
2762 else if (!declspecs->tag_defined_p
2763 && declspecs->storage_class != csc_none)
2765 if (warned != 1)
2766 pedwarn ("empty declaration with storage class specifier "
2767 "does not redeclare tag");
2768 warned = 1;
2769 pending_xref_error ();
2771 else if (!declspecs->tag_defined_p
2772 && (declspecs->const_p
2773 || declspecs->volatile_p
2774 || declspecs->restrict_p))
2776 if (warned != 1)
2777 pedwarn ("empty declaration with type qualifier "
2778 "does not redeclare tag");
2779 warned = 1;
2780 pending_xref_error ();
2782 else
2784 pending_invalid_xref = 0;
2785 t = lookup_tag (code, name, 1);
2787 if (t == 0)
2789 t = make_node (code);
2790 pushtag (name, t);
2794 else
2796 if (warned != 1 && !in_system_header)
2798 pedwarn ("useless type name in empty declaration");
2799 warned = 1;
2803 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2805 pedwarn ("useless type name in empty declaration");
2806 warned = 1;
2809 pending_invalid_xref = 0;
2811 if (declspecs->inline_p)
2813 error ("%<inline%> in empty declaration");
2814 warned = 1;
2817 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2819 error ("%<auto%> in file-scope empty declaration");
2820 warned = 1;
2823 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2825 error ("%<register%> in file-scope empty declaration");
2826 warned = 1;
2829 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2831 warning ("useless storage class specifier in empty declaration");
2832 warned = 2;
2835 if (!warned && !in_system_header && declspecs->thread_p)
2837 warning ("useless %<__thread%> in empty declaration");
2838 warned = 2;
2841 if (!warned && !in_system_header && (declspecs->const_p
2842 || declspecs->volatile_p
2843 || declspecs->restrict_p))
2845 warning ("useless type qualifier in empty declaration");
2846 warned = 2;
2849 if (warned != 1)
2851 if (!found_tag)
2852 pedwarn ("empty declaration");
2857 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2858 bits. SPECS represents declaration specifiers that the grammar
2859 only permits to contain type qualifiers and attributes. */
2862 quals_from_declspecs (const struct c_declspecs *specs)
2864 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2865 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2866 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2867 gcc_assert (!specs->type
2868 && !specs->decl_attr
2869 && specs->typespec_word == cts_none
2870 && specs->storage_class == csc_none
2871 && !specs->typedef_p
2872 && !specs->explicit_signed_p
2873 && !specs->deprecated_p
2874 && !specs->long_p
2875 && !specs->long_long_p
2876 && !specs->short_p
2877 && !specs->signed_p
2878 && !specs->unsigned_p
2879 && !specs->complex_p
2880 && !specs->inline_p
2881 && !specs->thread_p);
2882 return quals;
2885 /* Construct an array declarator. EXPR is the expression inside [], or
2886 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2887 to the pointer to which a parameter array is converted). STATIC_P is
2888 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2889 is true if the array is [*], a VLA of unspecified length which is
2890 nevertheless a complete type (not currently implemented by GCC),
2891 false otherwise. The field for the contained declarator is left to be
2892 filled in by set_array_declarator_inner. */
2894 struct c_declarator *
2895 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2896 bool vla_unspec_p)
2898 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2899 struct c_declarator);
2900 declarator->kind = cdk_array;
2901 declarator->declarator = 0;
2902 declarator->u.array.dimen = expr;
2903 if (quals)
2905 declarator->u.array.attrs = quals->attrs;
2906 declarator->u.array.quals = quals_from_declspecs (quals);
2908 else
2910 declarator->u.array.attrs = NULL_TREE;
2911 declarator->u.array.quals = 0;
2913 declarator->u.array.static_p = static_p;
2914 declarator->u.array.vla_unspec_p = vla_unspec_p;
2915 if (pedantic && !flag_isoc99)
2917 if (static_p || quals != NULL)
2918 pedwarn ("ISO C90 does not support %<static%> or type "
2919 "qualifiers in parameter array declarators");
2920 if (vla_unspec_p)
2921 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2923 if (vla_unspec_p)
2924 warning ("GCC does not yet properly implement %<[*]%> array declarators");
2925 return declarator;
2928 /* Set the contained declarator of an array declarator. DECL is the
2929 declarator, as constructed by build_array_declarator; INNER is what
2930 appears on the left of the []. ABSTRACT_P is true if it is an
2931 abstract declarator, false otherwise; this is used to reject static
2932 and type qualifiers in abstract declarators, where they are not in
2933 the C99 grammar (subject to possible change in DR#289). */
2935 struct c_declarator *
2936 set_array_declarator_inner (struct c_declarator *decl,
2937 struct c_declarator *inner, bool abstract_p)
2939 decl->declarator = inner;
2940 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2941 || decl->u.array.attrs != NULL_TREE
2942 || decl->u.array.static_p))
2943 error ("static or type qualifiers in abstract declarator");
2944 return decl;
2947 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2949 tree
2950 groktypename (struct c_type_name *type_name)
2952 tree type;
2953 tree attrs = type_name->specs->attrs;
2955 type_name->specs->attrs = NULL_TREE;
2957 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2958 false, NULL);
2960 /* Apply attributes. */
2961 decl_attributes (&type, attrs, 0);
2963 return type;
2966 /* Decode a declarator in an ordinary declaration or data definition.
2967 This is called as soon as the type information and variable name
2968 have been parsed, before parsing the initializer if any.
2969 Here we create the ..._DECL node, fill in its type,
2970 and put it on the list of decls for the current context.
2971 The ..._DECL node is returned as the value.
2973 Exception: for arrays where the length is not specified,
2974 the type is left null, to be filled in by `finish_decl'.
2976 Function definitions do not come here; they go to start_function
2977 instead. However, external and forward declarations of functions
2978 do go through here. Structure field declarations are done by
2979 grokfield and not through here. */
2981 tree
2982 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
2983 bool initialized, tree attributes)
2985 tree decl;
2986 tree tem;
2988 /* An object declared as __attribute__((deprecated)) suppresses
2989 warnings of uses of other deprecated items. */
2990 if (lookup_attribute ("deprecated", attributes))
2991 deprecated_state = DEPRECATED_SUPPRESS;
2993 decl = grokdeclarator (declarator, declspecs,
2994 NORMAL, initialized, NULL);
2995 if (!decl)
2996 return 0;
2998 deprecated_state = DEPRECATED_NORMAL;
3000 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3001 && MAIN_NAME_P (DECL_NAME (decl)))
3002 warning ("%J%qD is usually a function", decl, decl);
3004 if (initialized)
3005 /* Is it valid for this decl to have an initializer at all?
3006 If not, set INITIALIZED to zero, which will indirectly
3007 tell 'finish_decl' to ignore the initializer once it is parsed. */
3008 switch (TREE_CODE (decl))
3010 case TYPE_DECL:
3011 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3012 initialized = 0;
3013 break;
3015 case FUNCTION_DECL:
3016 error ("function %qD is initialized like a variable", decl);
3017 initialized = 0;
3018 break;
3020 case PARM_DECL:
3021 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3022 error ("parameter %qD is initialized", decl);
3023 initialized = 0;
3024 break;
3026 default:
3027 /* Don't allow initializations for incomplete types except for
3028 arrays which might be completed by the initialization. */
3030 /* This can happen if the array size is an undefined macro.
3031 We already gave a warning, so we don't need another one. */
3032 if (TREE_TYPE (decl) == error_mark_node)
3033 initialized = 0;
3034 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3036 /* A complete type is ok if size is fixed. */
3038 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3039 || C_DECL_VARIABLE_SIZE (decl))
3041 error ("variable-sized object may not be initialized");
3042 initialized = 0;
3045 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3047 error ("variable %qD has initializer but incomplete type", decl);
3048 initialized = 0;
3050 else if (C_DECL_VARIABLE_SIZE (decl))
3052 /* Although C99 is unclear about whether incomplete arrays
3053 of VLAs themselves count as VLAs, it does not make
3054 sense to permit them to be initialized given that
3055 ordinary VLAs may not be initialized. */
3056 error ("variable-sized object may not be initialized");
3057 initialized = 0;
3061 if (initialized)
3063 if (current_scope == file_scope)
3064 TREE_STATIC (decl) = 1;
3066 /* Tell 'pushdecl' this is an initialized decl
3067 even though we don't yet have the initializer expression.
3068 Also tell 'finish_decl' it may store the real initializer. */
3069 DECL_INITIAL (decl) = error_mark_node;
3072 /* If this is a function declaration, write a record describing it to the
3073 prototypes file (if requested). */
3075 if (TREE_CODE (decl) == FUNCTION_DECL)
3076 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3078 /* ANSI specifies that a tentative definition which is not merged with
3079 a non-tentative definition behaves exactly like a definition with an
3080 initializer equal to zero. (Section 3.7.2)
3082 -fno-common gives strict ANSI behavior, though this tends to break
3083 a large body of code that grew up without this rule.
3085 Thread-local variables are never common, since there's no entrenched
3086 body of code to break, and it allows more efficient variable references
3087 in the presence of dynamic linking. */
3089 if (TREE_CODE (decl) == VAR_DECL
3090 && !initialized
3091 && TREE_PUBLIC (decl)
3092 && !DECL_THREAD_LOCAL (decl)
3093 && !flag_no_common)
3094 DECL_COMMON (decl) = 1;
3096 /* Set attributes here so if duplicate decl, will have proper attributes. */
3097 decl_attributes (&decl, attributes, 0);
3099 if (TREE_CODE (decl) == FUNCTION_DECL
3100 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3102 struct c_declarator *ce = declarator;
3104 if (ce->kind == cdk_pointer)
3105 ce = declarator->declarator;
3106 if (ce->kind == cdk_function)
3108 tree args = ce->u.arg_info->parms;
3109 for (; args; args = TREE_CHAIN (args))
3111 tree type = TREE_TYPE (args);
3112 if (type && INTEGRAL_TYPE_P (type)
3113 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3114 DECL_ARG_TYPE (args) = integer_type_node;
3119 if (TREE_CODE (decl) == FUNCTION_DECL
3120 && DECL_DECLARED_INLINE_P (decl)
3121 && DECL_UNINLINABLE (decl)
3122 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3123 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3125 /* Add this decl to the current scope.
3126 TEM may equal DECL or it may be a previous decl of the same name. */
3127 tem = pushdecl (decl);
3129 if (initialized && DECL_EXTERNAL (tem))
3131 DECL_EXTERNAL (tem) = 0;
3132 TREE_STATIC (tem) = 1;
3135 return tem;
3138 /* Finish processing of a declaration;
3139 install its initial value.
3140 If the length of an array type is not known before,
3141 it must be determined now, from the initial value, or it is an error. */
3143 void
3144 finish_decl (tree decl, tree init, tree asmspec_tree)
3146 tree type = TREE_TYPE (decl);
3147 int was_incomplete = (DECL_SIZE (decl) == 0);
3148 const char *asmspec = 0;
3150 /* If a name was specified, get the string. */
3151 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3152 && DECL_FILE_SCOPE_P (decl))
3153 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3154 if (asmspec_tree)
3155 asmspec = TREE_STRING_POINTER (asmspec_tree);
3157 /* If `start_decl' didn't like having an initialization, ignore it now. */
3158 if (init != 0 && DECL_INITIAL (decl) == 0)
3159 init = 0;
3161 /* Don't crash if parm is initialized. */
3162 if (TREE_CODE (decl) == PARM_DECL)
3163 init = 0;
3165 if (init)
3166 store_init_value (decl, init);
3168 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3169 || TREE_CODE (decl) == FUNCTION_DECL
3170 || TREE_CODE (decl) == FIELD_DECL))
3171 objc_check_decl (decl);
3173 /* Deduce size of array from initialization, if not already known. */
3174 if (TREE_CODE (type) == ARRAY_TYPE
3175 && TYPE_DOMAIN (type) == 0
3176 && TREE_CODE (decl) != TYPE_DECL)
3178 bool do_default
3179 = (TREE_STATIC (decl)
3180 /* Even if pedantic, an external linkage array
3181 may have incomplete type at first. */
3182 ? pedantic && !TREE_PUBLIC (decl)
3183 : !DECL_EXTERNAL (decl));
3184 int failure
3185 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3186 do_default);
3188 /* Get the completed type made by complete_array_type. */
3189 type = TREE_TYPE (decl);
3191 if (failure == 1)
3192 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3194 else if (failure == 2)
3196 if (do_default)
3197 error ("%Jarray size missing in %qD", decl, decl);
3198 /* If a `static' var's size isn't known,
3199 make it extern as well as static, so it does not get
3200 allocated.
3201 If it is not `static', then do not mark extern;
3202 finish_incomplete_decl will give it a default size
3203 and it will get allocated. */
3204 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3205 DECL_EXTERNAL (decl) = 1;
3207 else if (failure == 3)
3208 error ("%Jzero or negative size array %qD", decl, decl);
3210 if (DECL_INITIAL (decl))
3211 TREE_TYPE (DECL_INITIAL (decl)) = type;
3213 layout_decl (decl, 0);
3216 if (TREE_CODE (decl) == VAR_DECL)
3218 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3219 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3220 layout_decl (decl, 0);
3222 if (DECL_SIZE (decl) == 0
3223 /* Don't give an error if we already gave one earlier. */
3224 && TREE_TYPE (decl) != error_mark_node
3225 && (TREE_STATIC (decl)
3226 /* A static variable with an incomplete type
3227 is an error if it is initialized.
3228 Also if it is not file scope.
3229 Otherwise, let it through, but if it is not `extern'
3230 then it may cause an error message later. */
3231 ? (DECL_INITIAL (decl) != 0
3232 || !DECL_FILE_SCOPE_P (decl))
3233 /* An automatic variable with an incomplete type
3234 is an error. */
3235 : !DECL_EXTERNAL (decl)))
3237 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3238 TREE_TYPE (decl) = error_mark_node;
3241 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3242 && DECL_SIZE (decl) != 0)
3244 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3245 constant_expression_warning (DECL_SIZE (decl));
3246 else
3247 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3250 if (TREE_USED (type))
3251 TREE_USED (decl) = 1;
3254 /* If this is a function and an assembler name is specified, reset DECL_RTL
3255 so we can give it its new name. Also, update built_in_decls if it
3256 was a normal built-in. */
3257 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3259 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3260 set_builtin_user_assembler_name (decl, asmspec);
3261 set_user_assembler_name (decl, asmspec);
3264 /* If #pragma weak was used, mark the decl weak now. */
3265 if (current_scope == file_scope)
3266 maybe_apply_pragma_weak (decl);
3268 /* If this is a variable definition, determine its ELF visibility. */
3269 if (TREE_CODE (decl) == VAR_DECL
3270 && TREE_STATIC (decl)
3271 && !DECL_EXTERNAL (decl))
3272 c_determine_visibility (decl);
3274 /* Output the assembler code and/or RTL code for variables and functions,
3275 unless the type is an undefined structure or union.
3276 If not, it will get done when the type is completed. */
3278 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3280 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3281 if (c_dialect_objc ())
3282 objc_check_decl (decl);
3284 if (asmspec)
3286 /* If this is not a static variable, issue a warning.
3287 It doesn't make any sense to give an ASMSPEC for an
3288 ordinary, non-register local variable. Historically,
3289 GCC has accepted -- but ignored -- the ASMSPEC in
3290 this case. */
3291 if (!DECL_FILE_SCOPE_P (decl)
3292 && TREE_CODE (decl) == VAR_DECL
3293 && !C_DECL_REGISTER (decl)
3294 && !TREE_STATIC (decl))
3295 warning ("%Jignoring asm-specifier for non-static local "
3296 "variable %qD", decl, decl);
3297 else if (C_DECL_REGISTER (decl))
3298 change_decl_assembler_name (decl, get_identifier (asmspec));
3299 else
3300 set_user_assembler_name (decl, asmspec);
3303 if (DECL_FILE_SCOPE_P (decl))
3305 if (DECL_INITIAL (decl) == NULL_TREE
3306 || DECL_INITIAL (decl) == error_mark_node)
3307 /* Don't output anything
3308 when a tentative file-scope definition is seen.
3309 But at end of compilation, do output code for them. */
3310 DECL_DEFER_OUTPUT (decl) = 1;
3311 rest_of_decl_compilation (decl, true, 0);
3313 else
3315 /* In conjunction with an ASMSPEC, the `register'
3316 keyword indicates that we should place the variable
3317 in a particular register. */
3318 if (asmspec && C_DECL_REGISTER (decl))
3320 DECL_HARD_REGISTER (decl) = 1;
3321 /* This cannot be done for a structure with volatile
3322 fields, on which DECL_REGISTER will have been
3323 reset. */
3324 if (!DECL_REGISTER (decl))
3325 error ("cannot put object with volatile field into register");
3328 if (TREE_CODE (decl) != FUNCTION_DECL)
3330 /* If we're building a variable sized type, and we might be
3331 reachable other than via the top of the current binding
3332 level, then create a new BIND_EXPR so that we deallocate
3333 the object at the right time. */
3334 /* Note that DECL_SIZE can be null due to errors. */
3335 if (DECL_SIZE (decl)
3336 && !TREE_CONSTANT (DECL_SIZE (decl))
3337 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3339 tree bind;
3340 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3341 TREE_SIDE_EFFECTS (bind) = 1;
3342 add_stmt (bind);
3343 BIND_EXPR_BODY (bind) = push_stmt_list ();
3345 add_stmt (build_stmt (DECL_EXPR, decl));
3350 if (!DECL_FILE_SCOPE_P (decl))
3352 /* Recompute the RTL of a local array now
3353 if it used to be an incomplete type. */
3354 if (was_incomplete
3355 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3357 /* If we used it already as memory, it must stay in memory. */
3358 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3359 /* If it's still incomplete now, no init will save it. */
3360 if (DECL_SIZE (decl) == 0)
3361 DECL_INITIAL (decl) = 0;
3366 /* If this was marked 'used', be sure it will be output. */
3367 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3368 mark_decl_referenced (decl);
3370 if (TREE_CODE (decl) == TYPE_DECL)
3372 if (!DECL_FILE_SCOPE_P (decl)
3373 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3374 add_stmt (build_stmt (DECL_EXPR, decl));
3376 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3379 /* At the end of a declaration, throw away any variable type sizes
3380 of types defined inside that declaration. There is no use
3381 computing them in the following function definition. */
3382 if (current_scope == file_scope)
3383 get_pending_sizes ();
3385 /* Install a cleanup (aka destructor) if one was given. */
3386 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3388 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3389 if (attr)
3391 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3392 tree cleanup_decl = lookup_name (cleanup_id);
3393 tree cleanup;
3395 /* Build "cleanup(&decl)" for the destructor. */
3396 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3397 cleanup = build_tree_list (NULL_TREE, cleanup);
3398 cleanup = build_function_call (cleanup_decl, cleanup);
3400 /* Don't warn about decl unused; the cleanup uses it. */
3401 TREE_USED (decl) = 1;
3402 TREE_USED (cleanup_decl) = 1;
3404 /* Initialize EH, if we've been told to do so. */
3405 if (flag_exceptions && !c_eh_initialized_p)
3407 c_eh_initialized_p = true;
3408 eh_personality_libfunc
3409 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3410 ? "__gcc_personality_sj0"
3411 : "__gcc_personality_v0");
3412 using_eh_for_cleanups ();
3415 push_cleanup (decl, cleanup, false);
3420 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3422 tree
3423 grokparm (const struct c_parm *parm)
3425 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3426 NULL);
3428 decl_attributes (&decl, parm->attrs, 0);
3430 return decl;
3433 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3434 and push that on the current scope. */
3436 void
3437 push_parm_decl (const struct c_parm *parm)
3439 tree decl;
3441 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3442 decl_attributes (&decl, parm->attrs, 0);
3444 decl = pushdecl (decl);
3446 finish_decl (decl, NULL_TREE, NULL_TREE);
3449 /* Mark all the parameter declarations to date as forward decls.
3450 Also diagnose use of this extension. */
3452 void
3453 mark_forward_parm_decls (void)
3455 struct c_binding *b;
3457 if (pedantic && !current_scope->warned_forward_parm_decls)
3459 pedwarn ("ISO C forbids forward parameter declarations");
3460 current_scope->warned_forward_parm_decls = true;
3463 for (b = current_scope->bindings; b; b = b->prev)
3464 if (TREE_CODE (b->decl) == PARM_DECL)
3465 TREE_ASM_WRITTEN (b->decl) = 1;
3468 static GTY(()) int compound_literal_number;
3470 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3471 literal, which may be an incomplete array type completed by the
3472 initializer; INIT is a CONSTRUCTOR that initializes the compound
3473 literal. */
3475 tree
3476 build_compound_literal (tree type, tree init)
3478 /* We do not use start_decl here because we have a type, not a declarator;
3479 and do not use finish_decl because the decl should be stored inside
3480 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3481 tree decl;
3482 tree complit;
3483 tree stmt;
3485 if (type == error_mark_node)
3486 return error_mark_node;
3488 decl = build_decl (VAR_DECL, NULL_TREE, type);
3489 DECL_EXTERNAL (decl) = 0;
3490 TREE_PUBLIC (decl) = 0;
3491 TREE_STATIC (decl) = (current_scope == file_scope);
3492 DECL_CONTEXT (decl) = current_function_decl;
3493 TREE_USED (decl) = 1;
3494 TREE_TYPE (decl) = type;
3495 TREE_READONLY (decl) = TYPE_READONLY (type);
3496 store_init_value (decl, init);
3498 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3500 int failure = complete_array_type (&TREE_TYPE (decl),
3501 DECL_INITIAL (decl), true);
3502 gcc_assert (!failure);
3504 type = TREE_TYPE (decl);
3505 TREE_TYPE (DECL_INITIAL (decl)) = type;
3508 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3509 return error_mark_node;
3511 stmt = build_stmt (DECL_EXPR, decl);
3512 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3513 TREE_SIDE_EFFECTS (complit) = 1;
3515 layout_decl (decl, 0);
3517 if (TREE_STATIC (decl))
3519 /* This decl needs a name for the assembler output. We also need
3520 a unique suffix to be added to the name. */
3521 char *name;
3523 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3524 compound_literal_number);
3525 compound_literal_number++;
3526 DECL_NAME (decl) = get_identifier (name);
3527 DECL_DEFER_OUTPUT (decl) = 1;
3528 DECL_COMDAT (decl) = 1;
3529 DECL_ARTIFICIAL (decl) = 1;
3530 DECL_IGNORED_P (decl) = 1;
3531 pushdecl (decl);
3532 rest_of_decl_compilation (decl, 1, 0);
3535 return complit;
3538 /* Determine whether TYPE is a structure with a flexible array member,
3539 or a union containing such a structure (possibly recursively). */
3541 static bool
3542 flexible_array_type_p (tree type)
3544 tree x;
3545 switch (TREE_CODE (type))
3547 case RECORD_TYPE:
3548 x = TYPE_FIELDS (type);
3549 if (x == NULL_TREE)
3550 return false;
3551 while (TREE_CHAIN (x) != NULL_TREE)
3552 x = TREE_CHAIN (x);
3553 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3554 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3555 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3556 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3557 return true;
3558 return false;
3559 case UNION_TYPE:
3560 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3562 if (flexible_array_type_p (TREE_TYPE (x)))
3563 return true;
3565 return false;
3566 default:
3567 return false;
3571 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3572 replacing with appropriate values if they are invalid. */
3573 static void
3574 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3576 tree type_mv;
3577 unsigned int max_width;
3578 unsigned HOST_WIDE_INT w;
3579 const char *name = orig_name ? orig_name: _("<anonymous>");
3581 /* Detect and ignore out of range field width and process valid
3582 field widths. */
3583 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3584 || TREE_CODE (*width) != INTEGER_CST)
3586 error ("bit-field %qs width not an integer constant", name);
3587 *width = integer_one_node;
3589 else
3591 constant_expression_warning (*width);
3592 if (tree_int_cst_sgn (*width) < 0)
3594 error ("negative width in bit-field %qs", name);
3595 *width = integer_one_node;
3597 else if (integer_zerop (*width) && orig_name)
3599 error ("zero width for bit-field %qs", name);
3600 *width = integer_one_node;
3604 /* Detect invalid bit-field type. */
3605 if (TREE_CODE (*type) != INTEGER_TYPE
3606 && TREE_CODE (*type) != BOOLEAN_TYPE
3607 && TREE_CODE (*type) != ENUMERAL_TYPE)
3609 error ("bit-field %qs has invalid type", name);
3610 *type = unsigned_type_node;
3613 type_mv = TYPE_MAIN_VARIANT (*type);
3614 if (pedantic
3615 && type_mv != integer_type_node
3616 && type_mv != unsigned_type_node
3617 && type_mv != boolean_type_node)
3618 pedwarn ("type of bit-field %qs is a GCC extension", name);
3620 if (type_mv == boolean_type_node)
3621 max_width = CHAR_TYPE_SIZE;
3622 else
3623 max_width = TYPE_PRECISION (*type);
3625 if (0 < compare_tree_int (*width, max_width))
3627 error ("width of %qs exceeds its type", name);
3628 w = max_width;
3629 *width = build_int_cst (NULL_TREE, w);
3631 else
3632 w = tree_low_cst (*width, 1);
3634 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3636 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3637 if (!lt
3638 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3639 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3640 warning ("%qs is narrower than values of its type", name);
3644 /* Given declspecs and a declarator,
3645 determine the name and type of the object declared
3646 and construct a ..._DECL node for it.
3647 (In one case we can return a ..._TYPE node instead.
3648 For invalid input we sometimes return 0.)
3650 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3652 DECL_CONTEXT says which syntactic context this declaration is in:
3653 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3654 FUNCDEF for a function definition. Like NORMAL but a few different
3655 error messages in each case. Return value may be zero meaning
3656 this definition is too screwy to try to parse.
3657 PARM for a parameter declaration (either within a function prototype
3658 or before a function body). Make a PARM_DECL, or return void_type_node.
3659 TYPENAME if for a typename (in a cast or sizeof).
3660 Don't make a DECL node; just return the ..._TYPE node.
3661 FIELD for a struct or union field; make a FIELD_DECL.
3662 INITIALIZED is true if the decl has an initializer.
3663 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3664 representing the width of the bit-field.
3666 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3667 It may also be so in the PARM case, for a prototype where the
3668 argument type is specified but not the name.
3670 This function is where the complicated C meanings of `static'
3671 and `extern' are interpreted. */
3673 static tree
3674 grokdeclarator (const struct c_declarator *declarator,
3675 struct c_declspecs *declspecs,
3676 enum decl_context decl_context, bool initialized, tree *width)
3678 tree type = declspecs->type;
3679 bool threadp = declspecs->thread_p;
3680 enum c_storage_class storage_class = declspecs->storage_class;
3681 int constp;
3682 int restrictp;
3683 int volatilep;
3684 int type_quals = TYPE_UNQUALIFIED;
3685 const char *name, *orig_name;
3686 tree typedef_type = 0;
3687 int funcdef_flag = 0;
3688 bool funcdef_syntax = false;
3689 int size_varies = 0;
3690 tree decl_attr = declspecs->decl_attr;
3691 int array_ptr_quals = TYPE_UNQUALIFIED;
3692 tree array_ptr_attrs = NULL_TREE;
3693 int array_parm_static = 0;
3694 tree returned_attrs = NULL_TREE;
3695 bool bitfield = width != NULL;
3696 tree element_type;
3697 struct c_arg_info *arg_info = 0;
3699 if (decl_context == FUNCDEF)
3700 funcdef_flag = 1, decl_context = NORMAL;
3702 /* Look inside a declarator for the name being declared
3703 and get it as a string, for an error message. */
3705 const struct c_declarator *decl = declarator;
3706 name = 0;
3708 while (decl)
3709 switch (decl->kind)
3711 case cdk_function:
3712 case cdk_array:
3713 case cdk_pointer:
3714 funcdef_syntax = (decl->kind == cdk_function);
3715 decl = decl->declarator;
3716 break;
3718 case cdk_attrs:
3719 decl = decl->declarator;
3720 break;
3722 case cdk_id:
3723 if (decl->u.id)
3724 name = IDENTIFIER_POINTER (decl->u.id);
3725 decl = 0;
3726 break;
3728 default:
3729 gcc_unreachable ();
3731 orig_name = name;
3732 if (name == 0)
3733 name = "type name";
3736 /* A function definition's declarator must have the form of
3737 a function declarator. */
3739 if (funcdef_flag && !funcdef_syntax)
3740 return 0;
3742 /* If this looks like a function definition, make it one,
3743 even if it occurs where parms are expected.
3744 Then store_parm_decls will reject it and not use it as a parm. */
3745 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3746 decl_context = PARM;
3748 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3749 warn_deprecated_use (declspecs->type);
3751 typedef_type = type;
3752 size_varies = C_TYPE_VARIABLE_SIZE (type);
3754 /* Diagnose defaulting to "int". */
3756 if (declspecs->default_int_p && !in_system_header)
3758 /* Issue a warning if this is an ISO C 99 program or if
3759 -Wreturn-type and this is a function, or if -Wimplicit;
3760 prefer the former warning since it is more explicit. */
3761 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3762 && funcdef_flag)
3763 warn_about_return_type = 1;
3764 else if (warn_implicit_int || flag_isoc99)
3765 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3768 /* Adjust the type if a bit-field is being declared,
3769 -funsigned-bitfields applied and the type is not explicitly
3770 "signed". */
3771 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3772 && TREE_CODE (type) == INTEGER_TYPE)
3773 type = c_common_unsigned_type (type);
3775 /* Figure out the type qualifiers for the declaration. There are
3776 two ways a declaration can become qualified. One is something
3777 like `const int i' where the `const' is explicit. Another is
3778 something like `typedef const int CI; CI i' where the type of the
3779 declaration contains the `const'. A third possibility is that
3780 there is a type qualifier on the element type of a typedefed
3781 array type, in which case we should extract that qualifier so
3782 that c_apply_type_quals_to_decls receives the full list of
3783 qualifiers to work with (C90 is not entirely clear about whether
3784 duplicate qualifiers should be diagnosed in this case, but it
3785 seems most appropriate to do so). */
3786 element_type = strip_array_types (type);
3787 constp = declspecs->const_p + TYPE_READONLY (element_type);
3788 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3789 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3790 if (pedantic && !flag_isoc99)
3792 if (constp > 1)
3793 pedwarn ("duplicate %<const%>");
3794 if (restrictp > 1)
3795 pedwarn ("duplicate %<restrict%>");
3796 if (volatilep > 1)
3797 pedwarn ("duplicate %<volatile%>");
3799 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3800 type = TYPE_MAIN_VARIANT (type);
3801 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3802 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3803 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3805 /* Warn about storage classes that are invalid for certain
3806 kinds of declarations (parameters, typenames, etc.). */
3808 if (funcdef_flag
3809 && (threadp
3810 || storage_class == csc_auto
3811 || storage_class == csc_register
3812 || storage_class == csc_typedef))
3814 if (storage_class == csc_auto
3815 && (pedantic || current_scope == file_scope))
3816 pedwarn ("function definition declared %<auto%>");
3817 if (storage_class == csc_register)
3818 error ("function definition declared %<register%>");
3819 if (storage_class == csc_typedef)
3820 error ("function definition declared %<typedef%>");
3821 if (threadp)
3822 error ("function definition declared %<__thread%>");
3823 threadp = false;
3824 if (storage_class == csc_auto
3825 || storage_class == csc_register
3826 || storage_class == csc_typedef)
3827 storage_class = csc_none;
3829 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3831 if (decl_context == PARM && storage_class == csc_register)
3833 else
3835 switch (decl_context)
3837 case FIELD:
3838 error ("storage class specified for structure field %qs",
3839 name);
3840 break;
3841 case PARM:
3842 error ("storage class specified for parameter %qs", name);
3843 break;
3844 default:
3845 error ("storage class specified for typename");
3846 break;
3848 storage_class = csc_none;
3849 threadp = false;
3852 else if (storage_class == csc_extern
3853 && initialized
3854 && !funcdef_flag)
3856 /* 'extern' with initialization is invalid if not at file scope. */
3857 if (current_scope == file_scope)
3858 warning ("%qs initialized and declared %<extern%>", name);
3859 else
3860 error ("%qs has both %<extern%> and initializer", name);
3862 else if (current_scope == file_scope)
3864 if (storage_class == csc_auto)
3865 error ("file-scope declaration of %qs specifies %<auto%>", name);
3866 if (pedantic && storage_class == csc_register)
3867 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3869 else
3871 if (storage_class == csc_extern && funcdef_flag)
3872 error ("nested function %qs declared %<extern%>", name);
3873 else if (threadp && storage_class == csc_none)
3875 error ("function-scope %qs implicitly auto and declared "
3876 "%<__thread%>",
3877 name);
3878 threadp = false;
3882 /* Now figure out the structure of the declarator proper.
3883 Descend through it, creating more complex types, until we reach
3884 the declared identifier (or NULL_TREE, in an absolute declarator).
3885 At each stage we maintain an unqualified version of the type
3886 together with any qualifiers that should be applied to it with
3887 c_build_qualified_type; this way, array types including
3888 multidimensional array types are first built up in unqualified
3889 form and then the qualified form is created with
3890 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3892 while (declarator && declarator->kind != cdk_id)
3894 if (type == error_mark_node)
3896 declarator = declarator->declarator;
3897 continue;
3900 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3901 a cdk_pointer (for *...),
3902 a cdk_function (for ...(...)),
3903 a cdk_attrs (for nested attributes),
3904 or a cdk_id (for the name being declared
3905 or the place in an absolute declarator
3906 where the name was omitted).
3907 For the last case, we have just exited the loop.
3909 At this point, TYPE is the type of elements of an array,
3910 or for a function to return, or for a pointer to point to.
3911 After this sequence of ifs, TYPE is the type of the
3912 array or function or pointer, and DECLARATOR has had its
3913 outermost layer removed. */
3915 if (array_ptr_quals != TYPE_UNQUALIFIED
3916 || array_ptr_attrs != NULL_TREE
3917 || array_parm_static)
3919 /* Only the innermost declarator (making a parameter be of
3920 array type which is converted to pointer type)
3921 may have static or type qualifiers. */
3922 error ("static or type qualifiers in non-parameter array declarator");
3923 array_ptr_quals = TYPE_UNQUALIFIED;
3924 array_ptr_attrs = NULL_TREE;
3925 array_parm_static = 0;
3928 switch (declarator->kind)
3930 case cdk_attrs:
3932 /* A declarator with embedded attributes. */
3933 tree attrs = declarator->u.attrs;
3934 const struct c_declarator *inner_decl;
3935 int attr_flags = 0;
3936 declarator = declarator->declarator;
3937 inner_decl = declarator;
3938 while (inner_decl->kind == cdk_attrs)
3939 inner_decl = inner_decl->declarator;
3940 if (inner_decl->kind == cdk_id)
3941 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3942 else if (inner_decl->kind == cdk_function)
3943 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3944 else if (inner_decl->kind == cdk_array)
3945 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3946 returned_attrs = decl_attributes (&type,
3947 chainon (returned_attrs, attrs),
3948 attr_flags);
3949 break;
3951 case cdk_array:
3953 tree itype = NULL_TREE;
3954 tree size = declarator->u.array.dimen;
3955 /* The index is a signed object `sizetype' bits wide. */
3956 tree index_type = c_common_signed_type (sizetype);
3958 array_ptr_quals = declarator->u.array.quals;
3959 array_ptr_attrs = declarator->u.array.attrs;
3960 array_parm_static = declarator->u.array.static_p;
3962 declarator = declarator->declarator;
3964 /* Check for some types that there cannot be arrays of. */
3966 if (VOID_TYPE_P (type))
3968 error ("declaration of %qs as array of voids", name);
3969 type = error_mark_node;
3972 if (TREE_CODE (type) == FUNCTION_TYPE)
3974 error ("declaration of %qs as array of functions", name);
3975 type = error_mark_node;
3978 if (pedantic && !in_system_header && flexible_array_type_p (type))
3979 pedwarn ("invalid use of structure with flexible array member");
3981 if (size == error_mark_node)
3982 type = error_mark_node;
3984 if (type == error_mark_node)
3985 continue;
3987 /* If size was specified, set ITYPE to a range-type for
3988 that size. Otherwise, ITYPE remains null. finish_decl
3989 may figure it out from an initial value. */
3991 if (size)
3993 /* Strip NON_LVALUE_EXPRs since we aren't using as an
3994 lvalue. */
3995 STRIP_TYPE_NOPS (size);
3997 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
3999 error ("size of array %qs has non-integer type", name);
4000 size = integer_one_node;
4003 if (pedantic && integer_zerop (size))
4004 pedwarn ("ISO C forbids zero-size array %qs", name);
4006 if (TREE_CODE (size) == INTEGER_CST)
4008 constant_expression_warning (size);
4009 if (tree_int_cst_sgn (size) < 0)
4011 error ("size of array %qs is negative", name);
4012 size = integer_one_node;
4015 else
4017 /* Make sure the array size remains visibly
4018 nonconstant even if it is (eg) a const variable
4019 with known value. */
4020 size_varies = 1;
4022 if (!flag_isoc99 && pedantic)
4024 if (TREE_CONSTANT (size))
4025 pedwarn ("ISO C90 forbids array %qs whose size "
4026 "can%'t be evaluated",
4027 name);
4028 else
4029 pedwarn ("ISO C90 forbids variable-size array %qs",
4030 name);
4034 if (integer_zerop (size))
4036 /* A zero-length array cannot be represented with
4037 an unsigned index type, which is what we'll
4038 get with build_index_type. Create an
4039 open-ended range instead. */
4040 itype = build_range_type (sizetype, size, NULL_TREE);
4042 else
4044 /* Arrange for the SAVE_EXPR on the inside of the
4045 MINUS_EXPR, which allows the -1 to get folded
4046 with the +1 that happens when building TYPE_SIZE. */
4047 if (size_varies)
4048 size = variable_size (size);
4050 /* Compute the maximum valid index, that is, size
4051 - 1. Do the calculation in index_type, so that
4052 if it is a variable the computations will be
4053 done in the proper mode. */
4054 itype = fold (build2 (MINUS_EXPR, index_type,
4055 convert (index_type, size),
4056 convert (index_type,
4057 size_one_node)));
4059 /* If that overflowed, the array is too big. ???
4060 While a size of INT_MAX+1 technically shouldn't
4061 cause an overflow (because we subtract 1), the
4062 overflow is recorded during the conversion to
4063 index_type, before the subtraction. Handling
4064 this case seems like an unnecessary
4065 complication. */
4066 if (TREE_OVERFLOW (itype))
4068 error ("size of array %qs is too large", name);
4069 type = error_mark_node;
4070 continue;
4073 itype = build_index_type (itype);
4076 else if (decl_context == FIELD)
4078 if (pedantic && !flag_isoc99 && !in_system_header)
4079 pedwarn ("ISO C90 does not support flexible array members");
4081 /* ISO C99 Flexible array members are effectively
4082 identical to GCC's zero-length array extension. */
4083 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4086 /* Complain about arrays of incomplete types. */
4087 if (!COMPLETE_TYPE_P (type))
4089 error ("array type has incomplete element type");
4090 type = error_mark_node;
4092 else
4093 type = build_array_type (type, itype);
4095 if (size_varies)
4096 C_TYPE_VARIABLE_SIZE (type) = 1;
4098 /* The GCC extension for zero-length arrays differs from
4099 ISO flexible array members in that sizeof yields
4100 zero. */
4101 if (size && integer_zerop (size))
4103 TYPE_SIZE (type) = bitsize_zero_node;
4104 TYPE_SIZE_UNIT (type) = size_zero_node;
4107 if (decl_context != PARM
4108 && (array_ptr_quals != TYPE_UNQUALIFIED
4109 || array_ptr_attrs != NULL_TREE
4110 || array_parm_static))
4112 error ("static or type qualifiers in non-parameter array declarator");
4113 array_ptr_quals = TYPE_UNQUALIFIED;
4114 array_ptr_attrs = NULL_TREE;
4115 array_parm_static = 0;
4117 break;
4119 case cdk_function:
4121 /* Say it's a definition only for the declarator closest
4122 to the identifier, apart possibly from some
4123 attributes. */
4124 bool really_funcdef = false;
4125 tree arg_types;
4126 if (funcdef_flag)
4128 const struct c_declarator *t = declarator->declarator;
4129 while (t->kind == cdk_attrs)
4130 t = t->declarator;
4131 really_funcdef = (t->kind == cdk_id);
4134 /* Declaring a function type. Make sure we have a valid
4135 type for the function to return. */
4136 if (type == error_mark_node)
4137 continue;
4139 size_varies = 0;
4141 /* Warn about some types functions can't return. */
4142 if (TREE_CODE (type) == FUNCTION_TYPE)
4144 error ("%qs declared as function returning a function", name);
4145 type = integer_type_node;
4147 if (TREE_CODE (type) == ARRAY_TYPE)
4149 error ("%qs declared as function returning an array", name);
4150 type = integer_type_node;
4153 /* Construct the function type and go to the next
4154 inner layer of declarator. */
4155 arg_info = declarator->u.arg_info;
4156 arg_types = grokparms (arg_info, really_funcdef);
4158 /* Type qualifiers before the return type of the function
4159 qualify the return type, not the function type. */
4160 if (type_quals)
4162 /* Type qualifiers on a function return type are
4163 normally permitted by the standard but have no
4164 effect, so give a warning at -Wreturn-type.
4165 Qualifiers on a void return type are banned on
4166 function definitions in ISO C; GCC used to used
4167 them for noreturn functions. */
4168 if (VOID_TYPE_P (type) && really_funcdef)
4169 pedwarn ("function definition has qualified void return type");
4170 else if (warn_return_type)
4171 warning ("type qualifiers ignored on function return type");
4173 type = c_build_qualified_type (type, type_quals);
4175 type_quals = TYPE_UNQUALIFIED;
4177 type = build_function_type (type, arg_types);
4178 declarator = declarator->declarator;
4180 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4181 the formal parameter list of this FUNCTION_TYPE to point to
4182 the FUNCTION_TYPE node itself. */
4184 tree link;
4186 for (link = arg_info->tags;
4187 link;
4188 link = TREE_CHAIN (link))
4189 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4191 break;
4193 case cdk_pointer:
4195 /* Merge any constancy or volatility into the target type
4196 for the pointer. */
4198 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4199 && type_quals)
4200 pedwarn ("ISO C forbids qualified function types");
4201 if (type_quals)
4202 type = c_build_qualified_type (type, type_quals);
4203 size_varies = 0;
4205 type = build_pointer_type (type);
4207 /* Process type qualifiers (such as const or volatile)
4208 that were given inside the `*'. */
4209 type_quals = declarator->u.pointer_quals;
4211 declarator = declarator->declarator;
4212 break;
4214 default:
4215 gcc_unreachable ();
4219 /* Now TYPE has the actual type, apart from any qualifiers in
4220 TYPE_QUALS. */
4222 /* Check the type and width of a bit-field. */
4223 if (bitfield)
4224 check_bitfield_type_and_width (&type, width, orig_name);
4226 /* Did array size calculations overflow? */
4228 if (TREE_CODE (type) == ARRAY_TYPE
4229 && COMPLETE_TYPE_P (type)
4230 && TREE_OVERFLOW (TYPE_SIZE (type)))
4232 error ("size of array %qs is too large", name);
4233 /* If we proceed with the array type as it is, we'll eventually
4234 crash in tree_low_cst(). */
4235 type = error_mark_node;
4238 /* If this is declaring a typedef name, return a TYPE_DECL. */
4240 if (storage_class == csc_typedef)
4242 tree decl;
4243 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4244 && type_quals)
4245 pedwarn ("ISO C forbids qualified function types");
4246 if (type_quals)
4247 type = c_build_qualified_type (type, type_quals);
4248 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4249 if (declspecs->explicit_signed_p)
4250 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4251 decl_attributes (&decl, returned_attrs, 0);
4252 if (declspecs->inline_p)
4253 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4254 return decl;
4257 /* Detect the case of an array type of unspecified size
4258 which came, as such, direct from a typedef name.
4259 We must copy the type, so that each identifier gets
4260 a distinct type, so that each identifier's size can be
4261 controlled separately by its own initializer. */
4263 if (type != 0 && typedef_type != 0
4264 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4265 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4267 type = build_array_type (TREE_TYPE (type), 0);
4268 if (size_varies)
4269 C_TYPE_VARIABLE_SIZE (type) = 1;
4272 /* If this is a type name (such as, in a cast or sizeof),
4273 compute the type and return it now. */
4275 if (decl_context == TYPENAME)
4277 /* Note that the grammar rejects storage classes in typenames
4278 and fields. */
4279 gcc_assert (storage_class == csc_none && !threadp
4280 && !declspecs->inline_p);
4281 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4282 && type_quals)
4283 pedwarn ("ISO C forbids const or volatile function types");
4284 if (type_quals)
4285 type = c_build_qualified_type (type, type_quals);
4286 decl_attributes (&type, returned_attrs, 0);
4287 return type;
4290 /* Aside from typedefs and type names (handle above),
4291 `void' at top level (not within pointer)
4292 is allowed only in public variables.
4293 We don't complain about parms either, but that is because
4294 a better error message can be made later. */
4296 if (VOID_TYPE_P (type) && decl_context != PARM
4297 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4298 && (storage_class == csc_extern
4299 || (current_scope == file_scope
4300 && !(storage_class == csc_static
4301 || storage_class == csc_register)))))
4303 error ("variable or field %qs declared void", name);
4304 type = integer_type_node;
4307 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4308 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4311 tree decl;
4313 if (decl_context == PARM)
4315 tree type_as_written;
4316 tree promoted_type;
4318 /* A parameter declared as an array of T is really a pointer to T.
4319 One declared as a function is really a pointer to a function. */
4321 if (TREE_CODE (type) == ARRAY_TYPE)
4323 /* Transfer const-ness of array into that of type pointed to. */
4324 type = TREE_TYPE (type);
4325 if (type_quals)
4326 type = c_build_qualified_type (type, type_quals);
4327 type = build_pointer_type (type);
4328 type_quals = array_ptr_quals;
4330 /* We don't yet implement attributes in this context. */
4331 if (array_ptr_attrs != NULL_TREE)
4332 warning ("attributes in parameter array declarator ignored");
4334 size_varies = 0;
4336 else if (TREE_CODE (type) == FUNCTION_TYPE)
4338 if (pedantic && type_quals)
4339 pedwarn ("ISO C forbids qualified function types");
4340 if (type_quals)
4341 type = c_build_qualified_type (type, type_quals);
4342 type = build_pointer_type (type);
4343 type_quals = TYPE_UNQUALIFIED;
4345 else if (type_quals)
4346 type = c_build_qualified_type (type, type_quals);
4348 type_as_written = type;
4350 decl = build_decl (PARM_DECL, declarator->u.id, type);
4351 if (size_varies)
4352 C_DECL_VARIABLE_SIZE (decl) = 1;
4354 /* Compute the type actually passed in the parmlist,
4355 for the case where there is no prototype.
4356 (For example, shorts and chars are passed as ints.)
4357 When there is a prototype, this is overridden later. */
4359 if (type == error_mark_node)
4360 promoted_type = type;
4361 else
4362 promoted_type = c_type_promotes_to (type);
4364 DECL_ARG_TYPE (decl) = promoted_type;
4365 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4366 if (declspecs->inline_p)
4367 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4369 else if (decl_context == FIELD)
4371 /* Note that the grammar rejects storage classes in typenames
4372 and fields. */
4373 gcc_assert (storage_class == csc_none && !threadp
4374 && !declspecs->inline_p);
4376 /* Structure field. It may not be a function. */
4378 if (TREE_CODE (type) == FUNCTION_TYPE)
4380 error ("field %qs declared as a function", name);
4381 type = build_pointer_type (type);
4383 else if (TREE_CODE (type) != ERROR_MARK
4384 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4386 error ("field %qs has incomplete type", name);
4387 type = error_mark_node;
4389 type = c_build_qualified_type (type, type_quals);
4390 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4391 DECL_NONADDRESSABLE_P (decl) = bitfield;
4393 if (size_varies)
4394 C_DECL_VARIABLE_SIZE (decl) = 1;
4396 else if (TREE_CODE (type) == FUNCTION_TYPE)
4398 if (storage_class == csc_register || threadp)
4400 error ("invalid storage class for function %qs", name);
4402 else if (current_scope != file_scope)
4404 /* Function declaration not at file scope. Storage
4405 classes other than `extern' are not allowed, C99
4406 6.7.1p5, and `extern' makes no difference. However,
4407 GCC allows 'auto', perhaps with 'inline', to support
4408 nested functions. */
4409 if (storage_class == csc_auto)
4411 if (pedantic)
4412 pedwarn ("invalid storage class for function %qs", name);
4414 else if (storage_class == csc_static)
4416 error ("invalid storage class for function %qs", name);
4417 if (funcdef_flag)
4418 storage_class = declspecs->storage_class = csc_none;
4419 else
4420 return 0;
4424 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4425 decl = build_decl_attribute_variant (decl, decl_attr);
4427 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4429 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4430 pedwarn ("ISO C forbids qualified function types");
4432 /* GNU C interprets a volatile-qualified function type to indicate
4433 that the function does not return. */
4434 if ((type_quals & TYPE_QUAL_VOLATILE)
4435 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4436 warning ("%<noreturn%> function returns non-void value");
4438 /* Every function declaration is an external reference
4439 (DECL_EXTERNAL) except for those which are not at file
4440 scope and are explicitly declared "auto". This is
4441 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4442 GCC to signify a forward declaration of a nested function. */
4443 if (storage_class == csc_auto && current_scope != file_scope)
4444 DECL_EXTERNAL (decl) = 0;
4445 else
4446 DECL_EXTERNAL (decl) = 1;
4448 /* Record absence of global scope for `static' or `auto'. */
4449 TREE_PUBLIC (decl)
4450 = !(storage_class == csc_static || storage_class == csc_auto);
4452 /* For a function definition, record the argument information
4453 block where store_parm_decls will look for it. */
4454 if (funcdef_flag)
4455 current_function_arg_info = arg_info;
4457 if (declspecs->default_int_p)
4458 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4460 /* Record presence of `inline', if it is reasonable. */
4461 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4463 if (declspecs->inline_p)
4464 pedwarn ("cannot inline function %<main%>");
4466 else if (declspecs->inline_p)
4468 /* Record that the function is declared `inline'. */
4469 DECL_DECLARED_INLINE_P (decl) = 1;
4471 /* Do not mark bare declarations as DECL_INLINE. Doing so
4472 in the presence of multiple declarations can result in
4473 the abstract origin pointing between the declarations,
4474 which will confuse dwarf2out. */
4475 if (initialized)
4477 DECL_INLINE (decl) = 1;
4478 if (storage_class == csc_extern)
4479 current_extern_inline = 1;
4482 /* If -finline-functions, assume it can be inlined. This does
4483 two things: let the function be deferred until it is actually
4484 needed, and let dwarf2 know that the function is inlinable. */
4485 else if (flag_inline_trees == 2 && initialized)
4486 DECL_INLINE (decl) = 1;
4488 else
4490 /* It's a variable. */
4491 /* An uninitialized decl with `extern' is a reference. */
4492 int extern_ref = !initialized && storage_class == csc_extern;
4494 type = c_build_qualified_type (type, type_quals);
4496 /* C99 6.2.2p7: It is invalid (compile-time undefined
4497 behavior) to create an 'extern' declaration for a
4498 variable if there is a global declaration that is
4499 'static' and the global declaration is not visible.
4500 (If the static declaration _is_ currently visible,
4501 the 'extern' declaration is taken to refer to that decl.) */
4502 if (extern_ref && current_scope != file_scope)
4504 tree global_decl = identifier_global_value (declarator->u.id);
4505 tree visible_decl = lookup_name (declarator->u.id);
4507 if (global_decl
4508 && global_decl != visible_decl
4509 && TREE_CODE (global_decl) == VAR_DECL
4510 && !TREE_PUBLIC (global_decl))
4511 error ("variable previously declared %<static%> redeclared "
4512 "%<extern%>");
4515 decl = build_decl (VAR_DECL, declarator->u.id, type);
4516 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4517 if (size_varies)
4518 C_DECL_VARIABLE_SIZE (decl) = 1;
4520 if (declspecs->inline_p)
4521 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4523 /* At file scope, an initialized extern declaration may follow
4524 a static declaration. In that case, DECL_EXTERNAL will be
4525 reset later in start_decl. */
4526 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4528 /* At file scope, the presence of a `static' or `register' storage
4529 class specifier, or the absence of all storage class specifiers
4530 makes this declaration a definition (perhaps tentative). Also,
4531 the absence of both `static' and `register' makes it public. */
4532 if (current_scope == file_scope)
4534 TREE_PUBLIC (decl) = !(storage_class == csc_static
4535 || storage_class == csc_register);
4536 TREE_STATIC (decl) = !extern_ref;
4538 /* Not at file scope, only `static' makes a static definition. */
4539 else
4541 TREE_STATIC (decl) = (storage_class == csc_static);
4542 TREE_PUBLIC (decl) = extern_ref;
4545 if (threadp)
4547 if (targetm.have_tls)
4548 DECL_THREAD_LOCAL (decl) = 1;
4549 else
4550 /* A mere warning is sure to result in improper semantics
4551 at runtime. Don't bother to allow this to compile. */
4552 error ("thread-local storage not supported for this target");
4556 /* Record `register' declaration for warnings on &
4557 and in case doing stupid register allocation. */
4559 if (storage_class == csc_register)
4561 C_DECL_REGISTER (decl) = 1;
4562 DECL_REGISTER (decl) = 1;
4565 /* Record constancy and volatility. */
4566 c_apply_type_quals_to_decl (type_quals, decl);
4568 /* If a type has volatile components, it should be stored in memory.
4569 Otherwise, the fact that those components are volatile
4570 will be ignored, and would even crash the compiler. */
4571 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4573 /* It is not an error for a structure with volatile fields to
4574 be declared register, but reset DECL_REGISTER since it
4575 cannot actually go in a register. */
4576 int was_reg = C_DECL_REGISTER (decl);
4577 C_DECL_REGISTER (decl) = 0;
4578 DECL_REGISTER (decl) = 0;
4579 c_mark_addressable (decl);
4580 C_DECL_REGISTER (decl) = was_reg;
4583 /* This is the earliest point at which we might know the assembler
4584 name of a variable. Thus, if it's known before this, die horribly. */
4585 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4587 decl_attributes (&decl, returned_attrs, 0);
4589 return decl;
4593 /* Decode the parameter-list info for a function type or function definition.
4594 The argument is the value returned by `get_parm_info' (or made in parse.y
4595 if there is an identifier list instead of a parameter decl list).
4596 These two functions are separate because when a function returns
4597 or receives functions then each is called multiple times but the order
4598 of calls is different. The last call to `grokparms' is always the one
4599 that contains the formal parameter names of a function definition.
4601 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4603 FUNCDEF_FLAG is true for a function definition, false for
4604 a mere declaration. A nonempty identifier-list gets an error message
4605 when FUNCDEF_FLAG is false. */
4607 static tree
4608 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4610 tree arg_types = arg_info->types;
4612 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4613 && !in_system_header)
4614 warning ("function declaration isn%'t a prototype");
4616 if (arg_types == error_mark_node)
4617 return 0; /* don't set TYPE_ARG_TYPES in this case */
4619 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4621 if (!funcdef_flag)
4622 pedwarn ("parameter names (without types) in function declaration");
4624 arg_info->parms = arg_info->types;
4625 arg_info->types = 0;
4626 return 0;
4628 else
4630 tree parm, type, typelt;
4631 unsigned int parmno;
4633 /* If there is a parameter of incomplete type in a definition,
4634 this is an error. In a declaration this is valid, and a
4635 struct or union type may be completed later, before any calls
4636 or definition of the function. In the case where the tag was
4637 first declared within the parameter list, a warning has
4638 already been given. If a parameter has void type, then
4639 however the function cannot be defined or called, so
4640 warn. */
4642 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4643 parm;
4644 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4646 type = TREE_VALUE (typelt);
4647 if (type == error_mark_node)
4648 continue;
4650 if (!COMPLETE_TYPE_P (type))
4652 if (funcdef_flag)
4654 if (DECL_NAME (parm))
4655 error ("%Jparameter %u (%qD) has incomplete type",
4656 parm, parmno, parm);
4657 else
4658 error ("%Jparameter %u has incomplete type",
4659 parm, parmno);
4661 TREE_VALUE (typelt) = error_mark_node;
4662 TREE_TYPE (parm) = error_mark_node;
4664 else if (VOID_TYPE_P (type))
4666 if (DECL_NAME (parm))
4667 warning ("%Jparameter %u (%qD) has void type",
4668 parm, parmno, parm);
4669 else
4670 warning ("%Jparameter %u has void type",
4671 parm, parmno);
4675 return arg_types;
4679 /* Take apart the current scope and return a c_arg_info structure with
4680 info on a parameter list just parsed.
4682 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4684 ELLIPSIS being true means the argument list ended in '...' so don't
4685 append a sentinel (void_list_node) to the end of the type-list. */
4687 struct c_arg_info *
4688 get_parm_info (bool ellipsis)
4690 struct c_binding *b = current_scope->bindings;
4691 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4692 struct c_arg_info);
4693 tree parms = 0;
4694 tree tags = 0;
4695 tree types = 0;
4696 tree others = 0;
4698 static bool explained_incomplete_types = false;
4699 bool gave_void_only_once_err = false;
4701 arg_info->parms = 0;
4702 arg_info->tags = 0;
4703 arg_info->types = 0;
4704 arg_info->others = 0;
4706 /* The bindings in this scope must not get put into a block.
4707 We will take care of deleting the binding nodes. */
4708 current_scope->bindings = 0;
4710 /* This function is only called if there was *something* on the
4711 parameter list. */
4712 gcc_assert (b);
4714 /* A parameter list consisting solely of 'void' indicates that the
4715 function takes no arguments. But if the 'void' is qualified
4716 (by 'const' or 'volatile'), or has a storage class specifier
4717 ('register'), then the behavior is undefined; issue an error.
4718 Typedefs for 'void' are OK (see DR#157). */
4719 if (b->prev == 0 /* one binding */
4720 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4721 && !DECL_NAME (b->decl) /* anonymous */
4722 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4724 if (TREE_THIS_VOLATILE (b->decl)
4725 || TREE_READONLY (b->decl)
4726 || C_DECL_REGISTER (b->decl))
4727 error ("%<void%> as only parameter may not be qualified");
4729 /* There cannot be an ellipsis. */
4730 if (ellipsis)
4731 error ("%<void%> must be the only parameter");
4733 arg_info->types = void_list_node;
4734 return arg_info;
4737 if (!ellipsis)
4738 types = void_list_node;
4740 /* Break up the bindings list into parms, tags, types, and others;
4741 apply sanity checks; purge the name-to-decl bindings. */
4742 while (b)
4744 tree decl = b->decl;
4745 tree type = TREE_TYPE (decl);
4746 const char *keyword;
4748 switch (TREE_CODE (decl))
4750 case PARM_DECL:
4751 if (b->id)
4753 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4754 I_SYMBOL_BINDING (b->id) = b->shadowed;
4757 /* Check for forward decls that never got their actual decl. */
4758 if (TREE_ASM_WRITTEN (decl))
4759 error ("%Jparameter %qD has just a forward declaration",
4760 decl, decl);
4761 /* Check for (..., void, ...) and issue an error. */
4762 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4764 if (!gave_void_only_once_err)
4766 error ("%<void%> must be the only parameter");
4767 gave_void_only_once_err = true;
4770 else
4772 /* Valid parameter, add it to the list. */
4773 TREE_CHAIN (decl) = parms;
4774 parms = decl;
4776 /* Since there is a prototype, args are passed in their
4777 declared types. The back end may override this later. */
4778 DECL_ARG_TYPE (decl) = type;
4779 types = tree_cons (0, type, types);
4781 break;
4783 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4784 case UNION_TYPE: keyword = "union"; goto tag;
4785 case RECORD_TYPE: keyword = "struct"; goto tag;
4786 tag:
4787 /* Types may not have tag-names, in which case the type
4788 appears in the bindings list with b->id NULL. */
4789 if (b->id)
4791 gcc_assert (I_TAG_BINDING (b->id) == b);
4792 I_TAG_BINDING (b->id) = b->shadowed;
4795 /* Warn about any struct, union or enum tags defined in a
4796 parameter list. The scope of such types is limited to
4797 the parameter list, which is rarely if ever desirable
4798 (it's impossible to call such a function with type-
4799 correct arguments). An anonymous union parm type is
4800 meaningful as a GNU extension, so don't warn for that. */
4801 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4803 if (b->id)
4804 /* The %s will be one of 'struct', 'union', or 'enum'. */
4805 warning ("%<%s %E%> declared inside parameter list",
4806 keyword, b->id);
4807 else
4808 /* The %s will be one of 'struct', 'union', or 'enum'. */
4809 warning ("anonymous %s declared inside parameter list",
4810 keyword);
4812 if (!explained_incomplete_types)
4814 warning ("its scope is only this definition or declaration,"
4815 " which is probably not what you want");
4816 explained_incomplete_types = true;
4820 tags = tree_cons (b->id, decl, tags);
4821 break;
4823 case CONST_DECL:
4824 case TYPE_DECL:
4825 case FUNCTION_DECL:
4826 /* CONST_DECLs appear here when we have an embedded enum,
4827 and TYPE_DECLs appear here when we have an embedded struct
4828 or union. No warnings for this - we already warned about the
4829 type itself. FUNCTION_DECLs appear when there is an implicit
4830 function declaration in the parameter list. */
4832 TREE_CHAIN (decl) = others;
4833 others = decl;
4834 /* fall through */
4836 case ERROR_MARK:
4837 /* error_mark_node appears here when we have an undeclared
4838 variable. Just throw it away. */
4839 if (b->id)
4841 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4842 I_SYMBOL_BINDING (b->id) = b->shadowed;
4844 break;
4846 /* Other things that might be encountered. */
4847 case LABEL_DECL:
4848 case VAR_DECL:
4849 default:
4850 gcc_unreachable ();
4853 b = free_binding_and_advance (b);
4856 arg_info->parms = parms;
4857 arg_info->tags = tags;
4858 arg_info->types = types;
4859 arg_info->others = others;
4860 return arg_info;
4863 /* Get the struct, enum or union (CODE says which) with tag NAME.
4864 Define the tag as a forward-reference if it is not defined.
4865 Return a c_typespec structure for the type specifier. */
4867 struct c_typespec
4868 parser_xref_tag (enum tree_code code, tree name)
4870 struct c_typespec ret;
4871 /* If a cross reference is requested, look up the type
4872 already defined for this tag and return it. */
4874 tree ref = lookup_tag (code, name, 0);
4875 /* If this is the right type of tag, return what we found.
4876 (This reference will be shadowed by shadow_tag later if appropriate.)
4877 If this is the wrong type of tag, do not return it. If it was the
4878 wrong type in the same scope, we will have had an error
4879 message already; if in a different scope and declaring
4880 a name, pending_xref_error will give an error message; but if in a
4881 different scope and not declaring a name, this tag should
4882 shadow the previous declaration of a different type of tag, and
4883 this would not work properly if we return the reference found.
4884 (For example, with "struct foo" in an outer scope, "union foo;"
4885 must shadow that tag with a new one of union type.) */
4886 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4887 if (ref && TREE_CODE (ref) == code)
4889 ret.spec = ref;
4890 return ret;
4893 /* If no such tag is yet defined, create a forward-reference node
4894 and record it as the "definition".
4895 When a real declaration of this type is found,
4896 the forward-reference will be altered into a real type. */
4898 ref = make_node (code);
4899 if (code == ENUMERAL_TYPE)
4901 /* Give the type a default layout like unsigned int
4902 to avoid crashing if it does not get defined. */
4903 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4904 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4905 TYPE_USER_ALIGN (ref) = 0;
4906 TYPE_UNSIGNED (ref) = 1;
4907 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4908 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4909 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4912 pushtag (name, ref);
4914 ret.spec = ref;
4915 return ret;
4918 /* Get the struct, enum or union (CODE says which) with tag NAME.
4919 Define the tag as a forward-reference if it is not defined.
4920 Return a tree for the type. */
4922 tree
4923 xref_tag (enum tree_code code, tree name)
4925 return parser_xref_tag (code, name).spec;
4928 /* Make sure that the tag NAME is defined *in the current scope*
4929 at least as a forward reference.
4930 CODE says which kind of tag NAME ought to be. */
4932 tree
4933 start_struct (enum tree_code code, tree name)
4935 /* If there is already a tag defined at this scope
4936 (as a forward reference), just return it. */
4938 tree ref = 0;
4940 if (name != 0)
4941 ref = lookup_tag (code, name, 1);
4942 if (ref && TREE_CODE (ref) == code)
4944 if (TYPE_SIZE (ref))
4946 if (code == UNION_TYPE)
4947 error ("redefinition of %<union %E%>", name);
4948 else
4949 error ("redefinition of %<struct %E%>", name);
4951 else if (C_TYPE_BEING_DEFINED (ref))
4953 if (code == UNION_TYPE)
4954 error ("nested redefinition of %<union %E%>", name);
4955 else
4956 error ("nested redefinition of %<struct %E%>", name);
4959 else
4961 /* Otherwise create a forward-reference just so the tag is in scope. */
4963 ref = make_node (code);
4964 pushtag (name, ref);
4967 C_TYPE_BEING_DEFINED (ref) = 1;
4968 TYPE_PACKED (ref) = flag_pack_struct;
4969 return ref;
4972 /* Process the specs, declarator and width (NULL if omitted)
4973 of a structure component, returning a FIELD_DECL node.
4974 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4976 This is done during the parsing of the struct declaration.
4977 The FIELD_DECL nodes are chained together and the lot of them
4978 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4980 tree
4981 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
4982 tree width)
4984 tree value;
4986 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
4987 && width == NULL_TREE)
4989 /* This is an unnamed decl.
4991 If we have something of the form "union { list } ;" then this
4992 is the anonymous union extension. Similarly for struct.
4994 If this is something of the form "struct foo;", then
4995 If MS extensions are enabled, this is handled as an
4996 anonymous struct.
4997 Otherwise this is a forward declaration of a structure tag.
4999 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5000 If MS extensions are enabled and foo names a structure, then
5001 again this is an anonymous struct.
5002 Otherwise this is an error.
5004 Oh what a horrid tangled web we weave. I wonder if MS consciously
5005 took this from Plan 9 or if it was an accident of implementation
5006 that took root before someone noticed the bug... */
5008 tree type = declspecs->type;
5009 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5010 || TREE_CODE (type) == UNION_TYPE);
5011 bool ok = false;
5013 if (type_ok
5014 && (flag_ms_extensions || !declspecs->typedef_p))
5016 if (flag_ms_extensions)
5017 ok = true;
5018 else if (flag_iso)
5019 ok = false;
5020 else if (TYPE_NAME (type) == NULL)
5021 ok = true;
5022 else
5023 ok = false;
5025 if (!ok)
5027 pedwarn ("declaration does not declare anything");
5028 return NULL_TREE;
5030 if (pedantic)
5031 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5034 value = grokdeclarator (declarator, declspecs, FIELD, false,
5035 width ? &width : NULL);
5037 finish_decl (value, NULL_TREE, NULL_TREE);
5038 DECL_INITIAL (value) = width;
5040 return value;
5043 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5044 the list such that this does not present a problem later. */
5046 static void
5047 detect_field_duplicates (tree fieldlist)
5049 tree x, y;
5050 int timeout = 10;
5052 /* First, see if there are more than "a few" fields.
5053 This is trivially true if there are zero or one fields. */
5054 if (!fieldlist)
5055 return;
5056 x = TREE_CHAIN (fieldlist);
5057 if (!x)
5058 return;
5059 do {
5060 timeout--;
5061 x = TREE_CHAIN (x);
5062 } while (timeout > 0 && x);
5064 /* If there were "few" fields, avoid the overhead of allocating
5065 a hash table. Instead just do the nested traversal thing. */
5066 if (timeout > 0)
5068 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5069 if (DECL_NAME (x))
5071 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5072 if (DECL_NAME (y) == DECL_NAME (x))
5074 error ("%Jduplicate member %qD", x, x);
5075 DECL_NAME (x) = NULL_TREE;
5079 else
5081 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5082 void **slot;
5084 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5085 if ((y = DECL_NAME (x)) != 0)
5087 slot = htab_find_slot (htab, y, INSERT);
5088 if (*slot)
5090 error ("%Jduplicate member %qD", x, x);
5091 DECL_NAME (x) = NULL_TREE;
5093 *slot = y;
5096 htab_delete (htab);
5100 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5101 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5102 ATTRIBUTES are attributes to be applied to the structure. */
5104 tree
5105 finish_struct (tree t, tree fieldlist, tree attributes)
5107 tree x;
5108 bool toplevel = file_scope == current_scope;
5109 int saw_named_field;
5111 /* If this type was previously laid out as a forward reference,
5112 make sure we lay it out again. */
5114 TYPE_SIZE (t) = 0;
5116 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5118 if (pedantic)
5120 for (x = fieldlist; x; x = TREE_CHAIN (x))
5121 if (DECL_NAME (x) != 0)
5122 break;
5124 if (x == 0)
5126 if (TREE_CODE (t) == UNION_TYPE)
5128 if (fieldlist)
5129 pedwarn ("union has no named members");
5130 else
5131 pedwarn ("union has no members");
5133 else
5135 if (fieldlist)
5136 pedwarn ("struct has no named members");
5137 else
5138 pedwarn ("struct has no members");
5143 /* Install struct as DECL_CONTEXT of each field decl.
5144 Also process specified field sizes, found in the DECL_INITIAL,
5145 storing 0 there after the type has been changed to precision equal
5146 to its width, rather than the precision of the specified standard
5147 type. (Correct layout requires the original type to have been preserved
5148 until now.) */
5150 saw_named_field = 0;
5151 for (x = fieldlist; x; x = TREE_CHAIN (x))
5153 DECL_CONTEXT (x) = t;
5154 DECL_PACKED (x) |= TYPE_PACKED (t);
5156 /* If any field is const, the structure type is pseudo-const. */
5157 if (TREE_READONLY (x))
5158 C_TYPE_FIELDS_READONLY (t) = 1;
5159 else
5161 /* A field that is pseudo-const makes the structure likewise. */
5162 tree t1 = TREE_TYPE (x);
5163 while (TREE_CODE (t1) == ARRAY_TYPE)
5164 t1 = TREE_TYPE (t1);
5165 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5166 && C_TYPE_FIELDS_READONLY (t1))
5167 C_TYPE_FIELDS_READONLY (t) = 1;
5170 /* Any field that is volatile means variables of this type must be
5171 treated in some ways as volatile. */
5172 if (TREE_THIS_VOLATILE (x))
5173 C_TYPE_FIELDS_VOLATILE (t) = 1;
5175 /* Any field of nominal variable size implies structure is too. */
5176 if (C_DECL_VARIABLE_SIZE (x))
5177 C_TYPE_VARIABLE_SIZE (t) = 1;
5179 if (DECL_INITIAL (x))
5181 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5182 DECL_SIZE (x) = bitsize_int (width);
5183 DECL_BIT_FIELD (x) = 1;
5184 SET_DECL_C_BIT_FIELD (x);
5187 /* Detect flexible array member in an invalid context. */
5188 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5189 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5190 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5191 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5193 if (TREE_CODE (t) == UNION_TYPE)
5195 error ("%Jflexible array member in union", x);
5196 TREE_TYPE (x) = error_mark_node;
5198 else if (TREE_CHAIN (x) != NULL_TREE)
5200 error ("%Jflexible array member not at end of struct", x);
5201 TREE_TYPE (x) = error_mark_node;
5203 else if (!saw_named_field)
5205 error ("%Jflexible array member in otherwise empty struct", x);
5206 TREE_TYPE (x) = error_mark_node;
5210 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5211 && flexible_array_type_p (TREE_TYPE (x)))
5212 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5214 if (DECL_NAME (x))
5215 saw_named_field = 1;
5218 detect_field_duplicates (fieldlist);
5220 /* Now we have the nearly final fieldlist. Record it,
5221 then lay out the structure or union (including the fields). */
5223 TYPE_FIELDS (t) = fieldlist;
5225 layout_type (t);
5227 /* Give bit-fields their proper types. */
5229 tree *fieldlistp = &fieldlist;
5230 while (*fieldlistp)
5231 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5232 && TREE_TYPE (*fieldlistp) != error_mark_node)
5234 unsigned HOST_WIDE_INT width
5235 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5236 tree type = TREE_TYPE (*fieldlistp);
5237 if (width != TYPE_PRECISION (type))
5239 TREE_TYPE (*fieldlistp)
5240 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5241 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5243 DECL_INITIAL (*fieldlistp) = 0;
5245 else
5246 fieldlistp = &TREE_CHAIN (*fieldlistp);
5249 /* Now we have the truly final field list.
5250 Store it in this type and in the variants. */
5252 TYPE_FIELDS (t) = fieldlist;
5254 /* If there are lots of fields, sort so we can look through them fast.
5255 We arbitrarily consider 16 or more elts to be "a lot". */
5258 int len = 0;
5260 for (x = fieldlist; x; x = TREE_CHAIN (x))
5262 if (len > 15 || DECL_NAME (x) == NULL)
5263 break;
5264 len += 1;
5267 if (len > 15)
5269 tree *field_array;
5270 struct lang_type *space;
5271 struct sorted_fields_type *space2;
5273 len += list_length (x);
5275 /* Use the same allocation policy here that make_node uses, to
5276 ensure that this lives as long as the rest of the struct decl.
5277 All decls in an inline function need to be saved. */
5279 space = GGC_CNEW (struct lang_type);
5280 space2 = GGC_NEWVAR (struct sorted_fields_type,
5281 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5283 len = 0;
5284 space->s = space2;
5285 field_array = &space2->elts[0];
5286 for (x = fieldlist; x; x = TREE_CHAIN (x))
5288 field_array[len++] = x;
5290 /* If there is anonymous struct or union, break out of the loop. */
5291 if (DECL_NAME (x) == NULL)
5292 break;
5294 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5295 if (x == NULL)
5297 TYPE_LANG_SPECIFIC (t) = space;
5298 TYPE_LANG_SPECIFIC (t)->s->len = len;
5299 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5300 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5305 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5307 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5308 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5309 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5310 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5313 /* If this was supposed to be a transparent union, but we can't
5314 make it one, warn and turn off the flag. */
5315 if (TREE_CODE (t) == UNION_TYPE
5316 && TYPE_TRANSPARENT_UNION (t)
5317 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5319 TYPE_TRANSPARENT_UNION (t) = 0;
5320 warning ("union cannot be made transparent");
5323 /* If this structure or union completes the type of any previous
5324 variable declaration, lay it out and output its rtl. */
5325 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5327 x = TREE_CHAIN (x))
5329 tree decl = TREE_VALUE (x);
5330 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5331 layout_array_type (TREE_TYPE (decl));
5332 if (TREE_CODE (decl) != TYPE_DECL)
5334 layout_decl (decl, 0);
5335 if (c_dialect_objc ())
5336 objc_check_decl (decl);
5337 rest_of_decl_compilation (decl, toplevel, 0);
5338 if (!toplevel)
5339 expand_decl (decl);
5342 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5344 /* Finish debugging output for this type. */
5345 rest_of_type_compilation (t, toplevel);
5347 /* If we're inside a function proper, i.e. not file-scope and not still
5348 parsing parameters, then arrange for the size of a variable sized type
5349 to be bound now. */
5350 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5351 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5353 return t;
5356 /* Lay out the type T, and its element type, and so on. */
5358 static void
5359 layout_array_type (tree t)
5361 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5362 layout_array_type (TREE_TYPE (t));
5363 layout_type (t);
5366 /* Begin compiling the definition of an enumeration type.
5367 NAME is its name (or null if anonymous).
5368 Returns the type object, as yet incomplete.
5369 Also records info about it so that build_enumerator
5370 may be used to declare the individual values as they are read. */
5372 tree
5373 start_enum (tree name)
5375 tree enumtype = 0;
5377 /* If this is the real definition for a previous forward reference,
5378 fill in the contents in the same object that used to be the
5379 forward reference. */
5381 if (name != 0)
5382 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5384 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5386 enumtype = make_node (ENUMERAL_TYPE);
5387 pushtag (name, enumtype);
5390 if (C_TYPE_BEING_DEFINED (enumtype))
5391 error ("nested redefinition of %<enum %E%>", name);
5393 C_TYPE_BEING_DEFINED (enumtype) = 1;
5395 if (TYPE_VALUES (enumtype) != 0)
5397 /* This enum is a named one that has been declared already. */
5398 error ("redeclaration of %<enum %E%>", name);
5400 /* Completely replace its old definition.
5401 The old enumerators remain defined, however. */
5402 TYPE_VALUES (enumtype) = 0;
5405 enum_next_value = integer_zero_node;
5406 enum_overflow = 0;
5408 if (flag_short_enums)
5409 TYPE_PACKED (enumtype) = 1;
5411 return enumtype;
5414 /* After processing and defining all the values of an enumeration type,
5415 install their decls in the enumeration type and finish it off.
5416 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5417 and ATTRIBUTES are the specified attributes.
5418 Returns ENUMTYPE. */
5420 tree
5421 finish_enum (tree enumtype, tree values, tree attributes)
5423 tree pair, tem;
5424 tree minnode = 0, maxnode = 0;
5425 int precision, unsign;
5426 bool toplevel = (file_scope == current_scope);
5427 struct lang_type *lt;
5429 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5431 /* Calculate the maximum value of any enumerator in this type. */
5433 if (values == error_mark_node)
5434 minnode = maxnode = integer_zero_node;
5435 else
5437 minnode = maxnode = TREE_VALUE (values);
5438 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5440 tree value = TREE_VALUE (pair);
5441 if (tree_int_cst_lt (maxnode, value))
5442 maxnode = value;
5443 if (tree_int_cst_lt (value, minnode))
5444 minnode = value;
5448 /* Construct the final type of this enumeration. It is the same
5449 as one of the integral types - the narrowest one that fits, except
5450 that normally we only go as narrow as int - and signed iff any of
5451 the values are negative. */
5452 unsign = (tree_int_cst_sgn (minnode) >= 0);
5453 precision = MAX (min_precision (minnode, unsign),
5454 min_precision (maxnode, unsign));
5456 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5458 tem = c_common_type_for_size (precision, unsign);
5459 if (tem == NULL)
5461 warning ("enumeration values exceed range of largest integer");
5462 tem = long_long_integer_type_node;
5465 else
5466 tem = unsign ? unsigned_type_node : integer_type_node;
5468 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5469 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5470 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5471 TYPE_SIZE (enumtype) = 0;
5473 /* If the precision of the type was specific with an attribute and it
5474 was too small, give an error. Otherwise, use it. */
5475 if (TYPE_PRECISION (enumtype))
5477 if (precision > TYPE_PRECISION (enumtype))
5478 error ("specified mode too small for enumeral values");
5480 else
5481 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5483 layout_type (enumtype);
5485 if (values != error_mark_node)
5487 /* Change the type of the enumerators to be the enum type. We
5488 need to do this irrespective of the size of the enum, for
5489 proper type checking. Replace the DECL_INITIALs of the
5490 enumerators, and the value slots of the list, with copies
5491 that have the enum type; they cannot be modified in place
5492 because they may be shared (e.g. integer_zero_node) Finally,
5493 change the purpose slots to point to the names of the decls. */
5494 for (pair = values; pair; pair = TREE_CHAIN (pair))
5496 tree enu = TREE_PURPOSE (pair);
5497 tree ini = DECL_INITIAL (enu);
5499 TREE_TYPE (enu) = enumtype;
5501 /* The ISO C Standard mandates enumerators to have type int,
5502 even though the underlying type of an enum type is
5503 unspecified. Here we convert any enumerators that fit in
5504 an int to type int, to avoid promotions to unsigned types
5505 when comparing integers with enumerators that fit in the
5506 int range. When -pedantic is given, build_enumerator()
5507 would have already taken care of those that don't fit. */
5508 if (int_fits_type_p (ini, integer_type_node))
5509 tem = integer_type_node;
5510 else
5511 tem = enumtype;
5512 ini = convert (tem, ini);
5514 DECL_INITIAL (enu) = ini;
5515 TREE_PURPOSE (pair) = DECL_NAME (enu);
5516 TREE_VALUE (pair) = ini;
5519 TYPE_VALUES (enumtype) = values;
5522 /* Record the min/max values so that we can warn about bit-field
5523 enumerations that are too small for the values. */
5524 lt = GGC_CNEW (struct lang_type);
5525 lt->enum_min = minnode;
5526 lt->enum_max = maxnode;
5527 TYPE_LANG_SPECIFIC (enumtype) = lt;
5529 /* Fix up all variant types of this enum type. */
5530 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5532 if (tem == enumtype)
5533 continue;
5534 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5535 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5536 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5537 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5538 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5539 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5540 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5541 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5542 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5543 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5544 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5547 /* Finish debugging output for this type. */
5548 rest_of_type_compilation (enumtype, toplevel);
5550 return enumtype;
5553 /* Build and install a CONST_DECL for one value of the
5554 current enumeration type (one that was begun with start_enum).
5555 Return a tree-list containing the CONST_DECL and its value.
5556 Assignment of sequential values by default is handled here. */
5558 tree
5559 build_enumerator (tree name, tree value)
5561 tree decl, type;
5563 /* Validate and default VALUE. */
5565 if (value != 0)
5567 /* Don't issue more errors for error_mark_node (i.e. an
5568 undeclared identifier) - just ignore the value expression. */
5569 if (value == error_mark_node)
5570 value = 0;
5571 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5572 || TREE_CODE (value) != INTEGER_CST)
5574 error ("enumerator value for %qE is not an integer constant", name);
5575 value = 0;
5577 else
5579 value = default_conversion (value);
5580 constant_expression_warning (value);
5584 /* Default based on previous value. */
5585 /* It should no longer be possible to have NON_LVALUE_EXPR
5586 in the default. */
5587 if (value == 0)
5589 value = enum_next_value;
5590 if (enum_overflow)
5591 error ("overflow in enumeration values");
5594 if (pedantic && !int_fits_type_p (value, integer_type_node))
5596 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5597 /* XXX This causes -pedantic to change the meaning of the program.
5598 Remove? -zw 2004-03-15 */
5599 value = convert (integer_type_node, value);
5602 /* Set basis for default for next value. */
5603 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5604 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5606 /* Now create a declaration for the enum value name. */
5608 type = TREE_TYPE (value);
5609 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5610 TYPE_PRECISION (integer_type_node)),
5611 (TYPE_PRECISION (type)
5612 >= TYPE_PRECISION (integer_type_node)
5613 && TYPE_UNSIGNED (type)));
5615 decl = build_decl (CONST_DECL, name, type);
5616 DECL_INITIAL (decl) = convert (type, value);
5617 pushdecl (decl);
5619 return tree_cons (decl, value, NULL_TREE);
5623 /* Create the FUNCTION_DECL for a function definition.
5624 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5625 the declaration; they describe the function's name and the type it returns,
5626 but twisted together in a fashion that parallels the syntax of C.
5628 This function creates a binding context for the function body
5629 as well as setting up the FUNCTION_DECL in current_function_decl.
5631 Returns 1 on success. If the DECLARATOR is not suitable for a function
5632 (it defines a datum instead), we return 0, which tells
5633 yyparse to report a parse error. */
5636 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5637 tree attributes)
5639 tree decl1, old_decl;
5640 tree restype, resdecl;
5641 struct c_label_context *nstack;
5643 current_function_returns_value = 0; /* Assume, until we see it does. */
5644 current_function_returns_null = 0;
5645 current_function_returns_abnormally = 0;
5646 warn_about_return_type = 0;
5647 current_extern_inline = 0;
5648 c_switch_stack = NULL;
5650 nstack = XOBNEW (&parser_obstack, struct c_label_context);
5651 nstack->labels_def = NULL;
5652 nstack->labels_used = NULL;
5653 nstack->next = label_context_stack;
5654 label_context_stack = nstack;
5656 /* Indicate no valid break/continue context by setting these variables
5657 to some non-null, non-label value. We'll notice and emit the proper
5658 error message in c_finish_bc_stmt. */
5659 c_break_label = c_cont_label = size_zero_node;
5661 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5663 /* If the declarator is not suitable for a function definition,
5664 cause a syntax error. */
5665 if (decl1 == 0)
5666 return 0;
5668 decl_attributes (&decl1, attributes, 0);
5670 if (DECL_DECLARED_INLINE_P (decl1)
5671 && DECL_UNINLINABLE (decl1)
5672 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5673 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5675 announce_function (decl1);
5677 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5679 error ("return type is an incomplete type");
5680 /* Make it return void instead. */
5681 TREE_TYPE (decl1)
5682 = build_function_type (void_type_node,
5683 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5686 if (warn_about_return_type)
5687 pedwarn_c99 ("return type defaults to %<int%>");
5689 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5690 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5691 DECL_INITIAL (decl1) = error_mark_node;
5693 /* If this definition isn't a prototype and we had a prototype declaration
5694 before, copy the arg type info from that prototype. */
5695 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5696 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5697 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5698 TREE_TYPE (TREE_TYPE (old_decl)))
5699 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5701 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5702 TREE_TYPE (decl1));
5703 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5706 /* Optionally warn of old-fashioned def with no previous prototype. */
5707 if (warn_strict_prototypes
5708 && old_decl != error_mark_node
5709 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5710 && C_DECL_ISNT_PROTOTYPE (old_decl))
5711 warning ("function declaration isn%'t a prototype");
5712 /* Optionally warn of any global def with no previous prototype. */
5713 else if (warn_missing_prototypes
5714 && old_decl != error_mark_node
5715 && TREE_PUBLIC (decl1)
5716 && !MAIN_NAME_P (DECL_NAME (decl1))
5717 && C_DECL_ISNT_PROTOTYPE (old_decl))
5718 warning ("%Jno previous prototype for %qD", decl1, decl1);
5719 /* Optionally warn of any def with no previous prototype
5720 if the function has already been used. */
5721 else if (warn_missing_prototypes
5722 && old_decl != 0
5723 && old_decl != error_mark_node
5724 && TREE_USED (old_decl)
5725 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5726 warning ("%J%qD was used with no prototype before its definition",
5727 decl1, decl1);
5728 /* Optionally warn of any global def with no previous declaration. */
5729 else if (warn_missing_declarations
5730 && TREE_PUBLIC (decl1)
5731 && old_decl == 0
5732 && !MAIN_NAME_P (DECL_NAME (decl1)))
5733 warning ("%Jno previous declaration for %qD", decl1, decl1);
5734 /* Optionally warn of any def with no previous declaration
5735 if the function has already been used. */
5736 else if (warn_missing_declarations
5737 && old_decl != 0
5738 && old_decl != error_mark_node
5739 && TREE_USED (old_decl)
5740 && C_DECL_IMPLICIT (old_decl))
5741 warning ("%J%qD was used with no declaration before its definition",
5742 decl1, decl1);
5744 /* This is a definition, not a reference.
5745 So normally clear DECL_EXTERNAL.
5746 However, `extern inline' acts like a declaration
5747 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5748 DECL_EXTERNAL (decl1) = current_extern_inline;
5750 /* This function exists in static storage.
5751 (This does not mean `static' in the C sense!) */
5752 TREE_STATIC (decl1) = 1;
5754 /* A nested function is not global. */
5755 if (current_function_decl != 0)
5756 TREE_PUBLIC (decl1) = 0;
5758 /* This is the earliest point at which we might know the assembler
5759 name of the function. Thus, if it's set before this, die horribly. */
5760 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5762 /* If #pragma weak was used, mark the decl weak now. */
5763 if (current_scope == file_scope)
5764 maybe_apply_pragma_weak (decl1);
5766 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5767 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5769 tree args;
5770 int argct = 0;
5772 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5773 != integer_type_node)
5774 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5776 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5777 args = TREE_CHAIN (args))
5779 tree type = args ? TREE_VALUE (args) : 0;
5781 if (type == void_type_node)
5782 break;
5784 ++argct;
5785 switch (argct)
5787 case 1:
5788 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5789 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5790 decl1, decl1);
5791 break;
5793 case 2:
5794 if (TREE_CODE (type) != POINTER_TYPE
5795 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5796 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5797 != char_type_node))
5798 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5799 decl1, decl1);
5800 break;
5802 case 3:
5803 if (TREE_CODE (type) != POINTER_TYPE
5804 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5805 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5806 != char_type_node))
5807 pedwarn ("%Jthird argument of %qD should probably be "
5808 "%<char **%>", decl1, decl1);
5809 break;
5813 /* It is intentional that this message does not mention the third
5814 argument because it's only mentioned in an appendix of the
5815 standard. */
5816 if (argct > 0 && (argct < 2 || argct > 3))
5817 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5819 if (!TREE_PUBLIC (decl1))
5820 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5823 /* Record the decl so that the function name is defined.
5824 If we already have a decl for this name, and it is a FUNCTION_DECL,
5825 use the old decl. */
5827 current_function_decl = pushdecl (decl1);
5829 push_scope ();
5830 declare_parm_level ();
5832 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5833 /* Promote the value to int before returning it. */
5834 if (c_promoting_integer_type_p (restype))
5836 /* It retains unsignedness if not really getting wider. */
5837 if (TYPE_UNSIGNED (restype)
5838 && (TYPE_PRECISION (restype)
5839 == TYPE_PRECISION (integer_type_node)))
5840 restype = unsigned_type_node;
5841 else
5842 restype = integer_type_node;
5845 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5846 DECL_ARTIFICIAL (resdecl) = 1;
5847 DECL_IGNORED_P (resdecl) = 1;
5848 DECL_RESULT (current_function_decl) = resdecl;
5850 start_fname_decls ();
5852 return 1;
5855 /* Subroutine of store_parm_decls which handles new-style function
5856 definitions (prototype format). The parms already have decls, so we
5857 need only record them as in effect and complain if any redundant
5858 old-style parm decls were written. */
5859 static void
5860 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5862 tree decl;
5864 if (current_scope->bindings)
5866 error ("%Jold-style parameter declarations in prototyped "
5867 "function definition", fndecl);
5869 /* Get rid of the old-style declarations. */
5870 pop_scope ();
5871 push_scope ();
5873 /* Don't issue this warning for nested functions, and don't issue this
5874 warning if we got here because ARG_INFO_TYPES was error_mark_node
5875 (this happens when a function definition has just an ellipsis in
5876 its parameter list). */
5877 else if (warn_traditional && !in_system_header && !current_function_scope
5878 && arg_info->types != error_mark_node)
5879 warning ("%Jtraditional C rejects ISO C style function definitions",
5880 fndecl);
5882 /* Now make all the parameter declarations visible in the function body.
5883 We can bypass most of the grunt work of pushdecl. */
5884 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5886 DECL_CONTEXT (decl) = current_function_decl;
5887 if (DECL_NAME (decl))
5888 bind (DECL_NAME (decl), decl, current_scope,
5889 /*invisible=*/false, /*nested=*/false);
5890 else
5891 error ("%Jparameter name omitted", decl);
5894 /* Record the parameter list in the function declaration. */
5895 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5897 /* Now make all the ancillary declarations visible, likewise. */
5898 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5900 DECL_CONTEXT (decl) = current_function_decl;
5901 if (DECL_NAME (decl))
5902 bind (DECL_NAME (decl), decl, current_scope,
5903 /*invisible=*/false, /*nested=*/false);
5906 /* And all the tag declarations. */
5907 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5908 if (TREE_PURPOSE (decl))
5909 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5910 /*invisible=*/false, /*nested=*/false);
5913 /* Subroutine of store_parm_decls which handles old-style function
5914 definitions (separate parameter list and declarations). */
5916 static void
5917 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5919 struct c_binding *b;
5920 tree parm, decl, last;
5921 tree parmids = arg_info->parms;
5923 /* We use DECL_WEAK as a flag to show which parameters have been
5924 seen already, since it is not used on PARM_DECL. */
5925 #ifdef ENABLE_CHECKING
5926 for (b = current_scope->bindings; b; b = b->prev)
5927 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5928 #endif
5930 if (warn_old_style_definition && !in_system_header)
5931 warning ("%Jold-style function definition", fndecl);
5933 /* Match each formal parameter name with its declaration. Save each
5934 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5935 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5937 if (TREE_VALUE (parm) == 0)
5939 error ("%Jparameter name missing from parameter list", fndecl);
5940 TREE_PURPOSE (parm) = 0;
5941 continue;
5944 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5945 if (b && B_IN_CURRENT_SCOPE (b))
5947 decl = b->decl;
5948 /* If we got something other than a PARM_DECL it is an error. */
5949 if (TREE_CODE (decl) != PARM_DECL)
5950 error ("%J%qD declared as a non-parameter", decl, decl);
5951 /* If the declaration is already marked, we have a duplicate
5952 name. Complain and ignore the duplicate. */
5953 else if (DECL_WEAK (decl))
5955 error ("%Jmultiple parameters named %qD", decl, decl);
5956 TREE_PURPOSE (parm) = 0;
5957 continue;
5959 /* If the declaration says "void", complain and turn it into
5960 an int. */
5961 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5963 error ("%Jparameter %qD declared with void type", decl, decl);
5964 TREE_TYPE (decl) = integer_type_node;
5965 DECL_ARG_TYPE (decl) = integer_type_node;
5966 layout_decl (decl, 0);
5969 /* If no declaration found, default to int. */
5970 else
5972 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5973 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5974 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5975 pushdecl (decl);
5977 if (flag_isoc99)
5978 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
5979 else if (extra_warnings)
5980 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
5983 TREE_PURPOSE (parm) = decl;
5984 DECL_WEAK (decl) = 1;
5987 /* Now examine the parms chain for incomplete declarations
5988 and declarations with no corresponding names. */
5990 for (b = current_scope->bindings; b; b = b->prev)
5992 parm = b->decl;
5993 if (TREE_CODE (parm) != PARM_DECL)
5994 continue;
5996 if (TREE_TYPE (parm) != error_mark_node
5997 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
5999 error ("%Jparameter %qD has incomplete type", parm, parm);
6000 TREE_TYPE (parm) = error_mark_node;
6003 if (!DECL_WEAK (parm))
6005 error ("%Jdeclaration for parameter %qD but no such parameter",
6006 parm, parm);
6008 /* Pretend the parameter was not missing.
6009 This gets us to a standard state and minimizes
6010 further error messages. */
6011 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6015 /* Chain the declarations together in the order of the list of
6016 names. Store that chain in the function decl, replacing the
6017 list of names. Update the current scope to match. */
6018 DECL_ARGUMENTS (fndecl) = 0;
6020 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6021 if (TREE_PURPOSE (parm))
6022 break;
6023 if (parm && TREE_PURPOSE (parm))
6025 last = TREE_PURPOSE (parm);
6026 DECL_ARGUMENTS (fndecl) = last;
6027 DECL_WEAK (last) = 0;
6029 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6030 if (TREE_PURPOSE (parm))
6032 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6033 last = TREE_PURPOSE (parm);
6034 DECL_WEAK (last) = 0;
6036 TREE_CHAIN (last) = 0;
6039 /* If there was a previous prototype,
6040 set the DECL_ARG_TYPE of each argument according to
6041 the type previously specified, and report any mismatches. */
6043 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6045 tree type;
6046 for (parm = DECL_ARGUMENTS (fndecl),
6047 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6048 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6049 != void_type_node));
6050 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6052 if (parm == 0 || type == 0
6053 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6055 error ("number of arguments doesn%'t match prototype");
6056 error ("%Hprototype declaration",
6057 &current_function_prototype_locus);
6058 break;
6060 /* Type for passing arg must be consistent with that
6061 declared for the arg. ISO C says we take the unqualified
6062 type for parameters declared with qualified type. */
6063 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6064 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6066 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6067 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6069 /* Adjust argument to match prototype. E.g. a previous
6070 `int foo(float);' prototype causes
6071 `int foo(x) float x; {...}' to be treated like
6072 `int foo(float x) {...}'. This is particularly
6073 useful for argument types like uid_t. */
6074 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6076 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6077 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6078 && TYPE_PRECISION (TREE_TYPE (parm))
6079 < TYPE_PRECISION (integer_type_node))
6080 DECL_ARG_TYPE (parm) = integer_type_node;
6082 if (pedantic)
6084 pedwarn ("promoted argument %qD "
6085 "doesn%'t match prototype", parm);
6086 pedwarn ("%Hprototype declaration",
6087 &current_function_prototype_locus);
6090 else
6092 error ("argument %qD doesn%'t match prototype", parm);
6093 error ("%Hprototype declaration",
6094 &current_function_prototype_locus);
6098 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6101 /* Otherwise, create a prototype that would match. */
6103 else
6105 tree actual = 0, last = 0, type;
6107 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6109 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6110 if (last)
6111 TREE_CHAIN (last) = type;
6112 else
6113 actual = type;
6114 last = type;
6116 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6117 if (last)
6118 TREE_CHAIN (last) = type;
6119 else
6120 actual = type;
6122 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6123 of the type of this function, but we need to avoid having this
6124 affect the types of other similarly-typed functions, so we must
6125 first force the generation of an identical (but separate) type
6126 node for the relevant function type. The new node we create
6127 will be a variant of the main variant of the original function
6128 type. */
6130 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6132 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6136 /* Store parameter declarations passed in ARG_INFO into the current
6137 function declaration. */
6139 void
6140 store_parm_decls_from (struct c_arg_info *arg_info)
6142 current_function_arg_info = arg_info;
6143 store_parm_decls ();
6146 /* Store the parameter declarations into the current function declaration.
6147 This is called after parsing the parameter declarations, before
6148 digesting the body of the function.
6150 For an old-style definition, construct a prototype out of the old-style
6151 parameter declarations and inject it into the function's type. */
6153 void
6154 store_parm_decls (void)
6156 tree fndecl = current_function_decl;
6157 bool proto;
6159 /* The argument information block for FNDECL. */
6160 struct c_arg_info *arg_info = current_function_arg_info;
6161 current_function_arg_info = 0;
6163 /* True if this definition is written with a prototype. Note:
6164 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6165 list in a function definition as equivalent to (void) -- an
6166 empty argument list specifies the function has no parameters,
6167 but only (void) sets up a prototype for future calls. */
6168 proto = arg_info->types != 0;
6170 if (proto)
6171 store_parm_decls_newstyle (fndecl, arg_info);
6172 else
6173 store_parm_decls_oldstyle (fndecl, arg_info);
6175 /* The next call to push_scope will be a function body. */
6177 next_is_function_body = true;
6179 /* Write a record describing this function definition to the prototypes
6180 file (if requested). */
6182 gen_aux_info_record (fndecl, 1, 0, proto);
6184 /* Initialize the RTL code for the function. */
6185 allocate_struct_function (fndecl);
6187 /* Begin the statement tree for this function. */
6188 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6190 /* ??? Insert the contents of the pending sizes list into the function
6191 to be evaluated. The only reason left to have this is
6192 void foo(int n, int array[n++])
6193 because we throw away the array type in favor of a pointer type, and
6194 thus won't naturally see the SAVE_EXPR containing the increment. All
6195 other pending sizes would be handled by gimplify_parameters. */
6197 tree t;
6198 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6199 add_stmt (TREE_VALUE (t));
6202 /* Even though we're inside a function body, we still don't want to
6203 call expand_expr to calculate the size of a variable-sized array.
6204 We haven't necessarily assigned RTL to all variables yet, so it's
6205 not safe to try to expand expressions involving them. */
6206 cfun->x_dont_save_pending_sizes_p = 1;
6209 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6210 functions. */
6212 static void
6213 c_warn_unused_result_recursively (tree fndecl)
6215 struct cgraph_node *cgn;
6217 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6218 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6220 /* Finalize all nested functions now. */
6221 cgn = cgraph_node (fndecl);
6222 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6223 c_warn_unused_result_recursively (cgn->decl);
6226 /* Finish up a function declaration and compile that function
6227 all the way to assembler language output. The free the storage
6228 for the function definition.
6230 This is called after parsing the body of the function definition. */
6232 void
6233 finish_function (void)
6235 tree fndecl = current_function_decl;
6237 label_context_stack = label_context_stack->next;
6239 if (TREE_CODE (fndecl) == FUNCTION_DECL
6240 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6242 tree args = DECL_ARGUMENTS (fndecl);
6243 for (; args; args = TREE_CHAIN (args))
6245 tree type = TREE_TYPE (args);
6246 if (INTEGRAL_TYPE_P (type)
6247 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6248 DECL_ARG_TYPE (args) = integer_type_node;
6252 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6253 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6255 /* Must mark the RESULT_DECL as being in this function. */
6257 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6258 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6260 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6262 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6263 != integer_type_node)
6265 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6266 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6267 if (!warn_main)
6268 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6270 else
6272 if (flag_isoc99)
6274 tree stmt = c_finish_return (integer_zero_node);
6275 /* Hack. We don't want the middle-end to warn that this
6276 return is unreachable, so put the statement on the
6277 special line 0. */
6278 #ifdef USE_MAPPED_LOCATION
6279 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
6280 #else
6281 annotate_with_file_line (stmt, input_filename, 0);
6282 #endif
6287 /* Tie off the statement tree for this function. */
6288 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6290 finish_fname_decls ();
6292 /* Complain if there's just no return statement. */
6293 if (warn_return_type
6294 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6295 && !current_function_returns_value && !current_function_returns_null
6296 /* Don't complain if we abort. */
6297 && !current_function_returns_abnormally
6298 /* Don't warn for main(). */
6299 && !MAIN_NAME_P (DECL_NAME (fndecl))
6300 /* Or if they didn't actually specify a return type. */
6301 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6302 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6303 inline function, as we might never be compiled separately. */
6304 && DECL_INLINE (fndecl))
6305 warning ("no return statement in function returning non-void");
6307 /* With just -Wextra, complain only if function returns both with
6308 and without a value. */
6309 if (extra_warnings
6310 && current_function_returns_value
6311 && current_function_returns_null)
6312 warning ("this function may return with or without a value");
6314 /* Store the end of the function, so that we get good line number
6315 info for the epilogue. */
6316 cfun->function_end_locus = input_location;
6318 /* If we don't have ctors/dtors sections, and this is a static
6319 constructor or destructor, it must be recorded now. */
6320 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6321 && !targetm.have_ctors_dtors)
6322 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6323 if (DECL_STATIC_DESTRUCTOR (fndecl)
6324 && !targetm.have_ctors_dtors)
6325 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6327 /* Finalize the ELF visibility for the function. */
6328 c_determine_visibility (fndecl);
6330 /* Genericize before inlining. Delay genericizing nested functions
6331 until their parent function is genericized. Since finalizing
6332 requires GENERIC, delay that as well. */
6334 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6335 && !undef_nested_function)
6337 if (!decl_function_context (fndecl))
6339 c_genericize (fndecl);
6340 c_warn_unused_result_recursively (fndecl);
6342 /* ??? Objc emits functions after finalizing the compilation unit.
6343 This should be cleaned up later and this conditional removed. */
6344 if (cgraph_global_info_ready)
6346 c_expand_body (fndecl);
6347 return;
6350 cgraph_finalize_function (fndecl, false);
6352 else
6354 /* Register this function with cgraph just far enough to get it
6355 added to our parent's nested function list. Handy, since the
6356 C front end doesn't have such a list. */
6357 (void) cgraph_node (fndecl);
6361 if (!decl_function_context (fndecl))
6362 undef_nested_function = false;
6364 /* We're leaving the context of this function, so zap cfun.
6365 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6366 tree_rest_of_compilation. */
6367 cfun = NULL;
6368 current_function_decl = NULL;
6371 /* Generate the RTL for the body of FNDECL. */
6373 void
6374 c_expand_body (tree fndecl)
6377 if (!DECL_INITIAL (fndecl)
6378 || DECL_INITIAL (fndecl) == error_mark_node)
6379 return;
6381 tree_rest_of_compilation (fndecl);
6383 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6384 && targetm.have_ctors_dtors)
6385 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6386 DEFAULT_INIT_PRIORITY);
6387 if (DECL_STATIC_DESTRUCTOR (fndecl)
6388 && targetm.have_ctors_dtors)
6389 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6390 DEFAULT_INIT_PRIORITY);
6393 /* Check the declarations given in a for-loop for satisfying the C99
6394 constraints. */
6395 void
6396 check_for_loop_decls (void)
6398 struct c_binding *b;
6400 if (!flag_isoc99)
6402 /* If we get here, declarations have been used in a for loop without
6403 the C99 for loop scope. This doesn't make much sense, so don't
6404 allow it. */
6405 error ("%<for%> loop initial declaration used outside C99 mode");
6406 return;
6408 /* C99 subclause 6.8.5 paragraph 3:
6410 [#3] The declaration part of a for statement shall only
6411 declare identifiers for objects having storage class auto or
6412 register.
6414 It isn't clear whether, in this sentence, "identifiers" binds to
6415 "shall only declare" or to "objects" - that is, whether all identifiers
6416 declared must be identifiers for objects, or whether the restriction
6417 only applies to those that are. (A question on this in comp.std.c
6418 in November 2000 received no answer.) We implement the strictest
6419 interpretation, to avoid creating an extension which later causes
6420 problems. */
6422 for (b = current_scope->bindings; b; b = b->prev)
6424 tree id = b->id;
6425 tree decl = b->decl;
6427 if (!id)
6428 continue;
6430 switch (TREE_CODE (decl))
6432 case VAR_DECL:
6433 if (TREE_STATIC (decl))
6434 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6435 "initial declaration", decl, decl);
6436 else if (DECL_EXTERNAL (decl))
6437 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6438 "initial declaration", decl, decl);
6439 break;
6441 case RECORD_TYPE:
6442 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6443 id);
6444 break;
6445 case UNION_TYPE:
6446 error ("%<union %E%> declared in %<for%> loop initial declaration",
6447 id);
6448 break;
6449 case ENUMERAL_TYPE:
6450 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6451 id);
6452 break;
6453 default:
6454 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6455 "initial declaration", decl, decl);
6460 /* Save and reinitialize the variables
6461 used during compilation of a C function. */
6463 void
6464 c_push_function_context (struct function *f)
6466 struct language_function *p;
6467 p = GGC_NEW (struct language_function);
6468 f->language = p;
6470 p->base.x_stmt_tree = c_stmt_tree;
6471 p->x_break_label = c_break_label;
6472 p->x_cont_label = c_cont_label;
6473 p->x_switch_stack = c_switch_stack;
6474 p->arg_info = current_function_arg_info;
6475 p->returns_value = current_function_returns_value;
6476 p->returns_null = current_function_returns_null;
6477 p->returns_abnormally = current_function_returns_abnormally;
6478 p->warn_about_return_type = warn_about_return_type;
6479 p->extern_inline = current_extern_inline;
6482 /* Restore the variables used during compilation of a C function. */
6484 void
6485 c_pop_function_context (struct function *f)
6487 struct language_function *p = f->language;
6489 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6490 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6492 /* Stop pointing to the local nodes about to be freed. */
6493 /* But DECL_INITIAL must remain nonzero so we know this
6494 was an actual function definition. */
6495 DECL_INITIAL (current_function_decl) = error_mark_node;
6496 DECL_ARGUMENTS (current_function_decl) = 0;
6499 c_stmt_tree = p->base.x_stmt_tree;
6500 c_break_label = p->x_break_label;
6501 c_cont_label = p->x_cont_label;
6502 c_switch_stack = p->x_switch_stack;
6503 current_function_arg_info = p->arg_info;
6504 current_function_returns_value = p->returns_value;
6505 current_function_returns_null = p->returns_null;
6506 current_function_returns_abnormally = p->returns_abnormally;
6507 warn_about_return_type = p->warn_about_return_type;
6508 current_extern_inline = p->extern_inline;
6510 f->language = NULL;
6513 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6515 void
6516 c_dup_lang_specific_decl (tree decl)
6518 struct lang_decl *ld;
6520 if (!DECL_LANG_SPECIFIC (decl))
6521 return;
6523 ld = GGC_NEW (struct lang_decl);
6524 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6525 DECL_LANG_SPECIFIC (decl) = ld;
6528 /* The functions below are required for functionality of doing
6529 function at once processing in the C front end. Currently these
6530 functions are not called from anywhere in the C front end, but as
6531 these changes continue, that will change. */
6533 /* Returns nonzero if the current statement is a full expression,
6534 i.e. temporaries created during that statement should be destroyed
6535 at the end of the statement. */
6538 stmts_are_full_exprs_p (void)
6540 return 0;
6543 /* Returns the stmt_tree (if any) to which statements are currently
6544 being added. If there is no active statement-tree, NULL is
6545 returned. */
6547 stmt_tree
6548 current_stmt_tree (void)
6550 return &c_stmt_tree;
6553 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6554 C. */
6557 anon_aggr_type_p (tree ARG_UNUSED (node))
6559 return 0;
6562 /* Return the global value of T as a symbol. */
6564 tree
6565 identifier_global_value (tree t)
6567 struct c_binding *b;
6569 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6570 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6571 return b->decl;
6573 return 0;
6576 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6577 otherwise the name is found in ridpointers from RID_INDEX. */
6579 void
6580 record_builtin_type (enum rid rid_index, const char *name, tree type)
6582 tree id, decl;
6583 if (name == 0)
6584 id = ridpointers[(int) rid_index];
6585 else
6586 id = get_identifier (name);
6587 decl = build_decl (TYPE_DECL, id, type);
6588 pushdecl (decl);
6589 if (debug_hooks->type_decl)
6590 debug_hooks->type_decl (decl, false);
6593 /* Build the void_list_node (void_type_node having been created). */
6594 tree
6595 build_void_list_node (void)
6597 tree t = build_tree_list (NULL_TREE, void_type_node);
6598 return t;
6601 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6603 struct c_parm *
6604 build_c_parm (struct c_declspecs *specs, tree attrs,
6605 struct c_declarator *declarator)
6607 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6608 ret->specs = specs;
6609 ret->attrs = attrs;
6610 ret->declarator = declarator;
6611 return ret;
6614 /* Return a declarator with nested attributes. TARGET is the inner
6615 declarator to which these attributes apply. ATTRS are the
6616 attributes. */
6618 struct c_declarator *
6619 build_attrs_declarator (tree attrs, struct c_declarator *target)
6621 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6622 ret->kind = cdk_attrs;
6623 ret->declarator = target;
6624 ret->u.attrs = attrs;
6625 return ret;
6628 /* Return a declarator for a function with arguments specified by ARGS
6629 and return type specified by TARGET. */
6631 struct c_declarator *
6632 build_function_declarator (struct c_arg_info *args,
6633 struct c_declarator *target)
6635 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6636 ret->kind = cdk_function;
6637 ret->declarator = target;
6638 ret->u.arg_info = args;
6639 return ret;
6642 /* Return a declarator for the identifier IDENT (which may be
6643 NULL_TREE for an abstract declarator). */
6645 struct c_declarator *
6646 build_id_declarator (tree ident)
6648 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6649 ret->kind = cdk_id;
6650 ret->declarator = 0;
6651 ret->u.id = ident;
6652 /* Default value - may get reset to a more precise location. */
6653 ret->id_loc = input_location;
6654 return ret;
6657 /* Return something to represent absolute declarators containing a *.
6658 TARGET is the absolute declarator that the * contains.
6659 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6660 to apply to the pointer type. */
6662 struct c_declarator *
6663 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6664 struct c_declarator *target)
6666 tree attrs;
6667 int quals = 0;
6668 struct c_declarator *itarget = target;
6669 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6670 if (type_quals_attrs)
6672 attrs = type_quals_attrs->attrs;
6673 quals = quals_from_declspecs (type_quals_attrs);
6674 if (attrs != NULL_TREE)
6675 itarget = build_attrs_declarator (attrs, target);
6677 ret->kind = cdk_pointer;
6678 ret->declarator = itarget;
6679 ret->u.pointer_quals = quals;
6680 return ret;
6683 /* Return a pointer to a structure for an empty list of declaration
6684 specifiers. */
6686 struct c_declspecs *
6687 build_null_declspecs (void)
6689 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6690 ret->type = 0;
6691 ret->decl_attr = 0;
6692 ret->attrs = 0;
6693 ret->typespec_word = cts_none;
6694 ret->storage_class = csc_none;
6695 ret->declspecs_seen_p = false;
6696 ret->type_seen_p = false;
6697 ret->non_sc_seen_p = false;
6698 ret->typedef_p = false;
6699 ret->tag_defined_p = false;
6700 ret->explicit_signed_p = false;
6701 ret->deprecated_p = false;
6702 ret->default_int_p = false;
6703 ret->long_p = false;
6704 ret->long_long_p = false;
6705 ret->short_p = false;
6706 ret->signed_p = false;
6707 ret->unsigned_p = false;
6708 ret->complex_p = false;
6709 ret->inline_p = false;
6710 ret->thread_p = false;
6711 ret->const_p = false;
6712 ret->volatile_p = false;
6713 ret->restrict_p = false;
6714 return ret;
6717 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6718 returning SPECS. */
6720 struct c_declspecs *
6721 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6723 enum rid i;
6724 bool dupe = false;
6725 specs->non_sc_seen_p = true;
6726 specs->declspecs_seen_p = true;
6727 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6728 && C_IS_RESERVED_WORD (qual));
6729 i = C_RID_CODE (qual);
6730 switch (i)
6732 case RID_CONST:
6733 dupe = specs->const_p;
6734 specs->const_p = true;
6735 break;
6736 case RID_VOLATILE:
6737 dupe = specs->volatile_p;
6738 specs->volatile_p = true;
6739 break;
6740 case RID_RESTRICT:
6741 dupe = specs->restrict_p;
6742 specs->restrict_p = true;
6743 break;
6744 default:
6745 gcc_unreachable ();
6747 if (dupe && pedantic && !flag_isoc99)
6748 pedwarn ("duplicate %qE", qual);
6749 return specs;
6752 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6753 returning SPECS. */
6755 struct c_declspecs *
6756 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6758 tree type = spec.spec;
6759 specs->non_sc_seen_p = true;
6760 specs->declspecs_seen_p = true;
6761 specs->type_seen_p = true;
6762 if (TREE_DEPRECATED (type))
6763 specs->deprecated_p = true;
6765 /* Handle type specifier keywords. */
6766 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6768 enum rid i = C_RID_CODE (type);
6769 if (specs->type)
6771 error ("two or more data types in declaration specifiers");
6772 return specs;
6774 if ((int) i <= (int) RID_LAST_MODIFIER)
6776 /* "long", "short", "signed", "unsigned" or "_Complex". */
6777 bool dupe = false;
6778 switch (i)
6780 case RID_LONG:
6781 if (specs->long_long_p)
6783 error ("%<long long long%> is too long for GCC");
6784 break;
6786 if (specs->long_p)
6788 if (specs->typespec_word == cts_double)
6790 error ("both %<long long%> and %<double%> in "
6791 "declaration specifiers");
6792 break;
6794 if (pedantic && !flag_isoc99 && !in_system_header
6795 && warn_long_long)
6796 pedwarn ("ISO C90 does not support %<long long%>");
6797 specs->long_long_p = 1;
6798 break;
6800 if (specs->short_p)
6801 error ("both %<long%> and %<short%> in "
6802 "declaration specifiers");
6803 else if (specs->typespec_word == cts_void)
6804 error ("both %<long%> and %<void%> in "
6805 "declaration specifiers");
6806 else if (specs->typespec_word == cts_bool)
6807 error ("both %<long%> and %<_Bool%> in "
6808 "declaration specifiers");
6809 else if (specs->typespec_word == cts_char)
6810 error ("both %<long%> and %<char%> in "
6811 "declaration specifiers");
6812 else if (specs->typespec_word == cts_float)
6813 error ("both %<long%> and %<float%> in "
6814 "declaration specifiers");
6815 else
6816 specs->long_p = true;
6817 break;
6818 case RID_SHORT:
6819 dupe = specs->short_p;
6820 if (specs->long_p)
6821 error ("both %<long%> and %<short%> in "
6822 "declaration specifiers");
6823 else if (specs->typespec_word == cts_void)
6824 error ("both %<short%> and %<void%> in "
6825 "declaration specifiers");
6826 else if (specs->typespec_word == cts_bool)
6827 error ("both %<short%> and %<_Bool%> in "
6828 "declaration specifiers");
6829 else if (specs->typespec_word == cts_char)
6830 error ("both %<short%> and %<char%> in "
6831 "declaration specifiers");
6832 else if (specs->typespec_word == cts_float)
6833 error ("both %<short%> and %<float%> in "
6834 "declaration specifiers");
6835 else if (specs->typespec_word == cts_double)
6836 error ("both %<short%> and %<double%> in "
6837 "declaration specifiers");
6838 else
6839 specs->short_p = true;
6840 break;
6841 case RID_SIGNED:
6842 dupe = specs->signed_p;
6843 if (specs->unsigned_p)
6844 error ("both %<signed%> and %<unsigned%> in "
6845 "declaration specifiers");
6846 else if (specs->typespec_word == cts_void)
6847 error ("both %<signed%> and %<void%> in "
6848 "declaration specifiers");
6849 else if (specs->typespec_word == cts_bool)
6850 error ("both %<signed%> and %<_Bool%> in "
6851 "declaration specifiers");
6852 else if (specs->typespec_word == cts_float)
6853 error ("both %<signed%> and %<float%> in "
6854 "declaration specifiers");
6855 else if (specs->typespec_word == cts_double)
6856 error ("both %<signed%> and %<double%> in "
6857 "declaration specifiers");
6858 else
6859 specs->signed_p = true;
6860 break;
6861 case RID_UNSIGNED:
6862 dupe = specs->unsigned_p;
6863 if (specs->signed_p)
6864 error ("both %<signed%> and %<unsigned%> in "
6865 "declaration specifiers");
6866 else if (specs->typespec_word == cts_void)
6867 error ("both %<unsigned%> and %<void%> in "
6868 "declaration specifiers");
6869 else if (specs->typespec_word == cts_bool)
6870 error ("both %<unsigned%> and %<_Bool%> in "
6871 "declaration specifiers");
6872 else if (specs->typespec_word == cts_float)
6873 error ("both %<unsigned%> and %<float%> in "
6874 "declaration specifiers");
6875 else if (specs->typespec_word == cts_double)
6876 error ("both %<unsigned%> and %<double%> in "
6877 "declaration specifiers");
6878 else
6879 specs->unsigned_p = true;
6880 break;
6881 case RID_COMPLEX:
6882 dupe = specs->complex_p;
6883 if (pedantic && !flag_isoc99 && !in_system_header)
6884 pedwarn ("ISO C90 does not support complex types");
6885 if (specs->typespec_word == cts_void)
6886 error ("both %<complex%> and %<void%> in "
6887 "declaration specifiers");
6888 else if (specs->typespec_word == cts_bool)
6889 error ("both %<complex%> and %<_Bool%> in "
6890 "declaration specifiers");
6891 else
6892 specs->complex_p = true;
6893 break;
6894 default:
6895 gcc_unreachable ();
6898 if (dupe)
6899 error ("duplicate %qE", type);
6901 return specs;
6903 else
6905 /* "void", "_Bool", "char", "int", "float" or "double". */
6906 if (specs->typespec_word != cts_none)
6908 error ("two or more data types in declaration specifiers");
6909 return specs;
6911 switch (i)
6913 case RID_VOID:
6914 if (specs->long_p)
6915 error ("both %<long%> and %<void%> in "
6916 "declaration specifiers");
6917 else if (specs->short_p)
6918 error ("both %<short%> and %<void%> in "
6919 "declaration specifiers");
6920 else if (specs->signed_p)
6921 error ("both %<signed%> and %<void%> in "
6922 "declaration specifiers");
6923 else if (specs->unsigned_p)
6924 error ("both %<unsigned%> and %<void%> in "
6925 "declaration specifiers");
6926 else if (specs->complex_p)
6927 error ("both %<complex%> and %<void%> in "
6928 "declaration specifiers");
6929 else
6930 specs->typespec_word = cts_void;
6931 return specs;
6932 case RID_BOOL:
6933 if (specs->long_p)
6934 error ("both %<long%> and %<_Bool%> in "
6935 "declaration specifiers");
6936 else if (specs->short_p)
6937 error ("both %<short%> and %<_Bool%> in "
6938 "declaration specifiers");
6939 else if (specs->signed_p)
6940 error ("both %<signed%> and %<_Bool%> in "
6941 "declaration specifiers");
6942 else if (specs->unsigned_p)
6943 error ("both %<unsigned%> and %<_Bool%> in "
6944 "declaration specifiers");
6945 else if (specs->complex_p)
6946 error ("both %<complex%> and %<_Bool%> in "
6947 "declaration specifiers");
6948 else
6949 specs->typespec_word = cts_bool;
6950 return specs;
6951 case RID_CHAR:
6952 if (specs->long_p)
6953 error ("both %<long%> and %<char%> in "
6954 "declaration specifiers");
6955 else if (specs->short_p)
6956 error ("both %<short%> and %<char%> in "
6957 "declaration specifiers");
6958 else
6959 specs->typespec_word = cts_char;
6960 return specs;
6961 case RID_INT:
6962 specs->typespec_word = cts_int;
6963 return specs;
6964 case RID_FLOAT:
6965 if (specs->long_p)
6966 error ("both %<long%> and %<float%> in "
6967 "declaration specifiers");
6968 else if (specs->short_p)
6969 error ("both %<short%> and %<float%> in "
6970 "declaration specifiers");
6971 else if (specs->signed_p)
6972 error ("both %<signed%> and %<float%> in "
6973 "declaration specifiers");
6974 else if (specs->unsigned_p)
6975 error ("both %<unsigned%> and %<float%> in "
6976 "declaration specifiers");
6977 else
6978 specs->typespec_word = cts_float;
6979 return specs;
6980 case RID_DOUBLE:
6981 if (specs->long_long_p)
6982 error ("both %<long long%> and %<double%> in "
6983 "declaration specifiers");
6984 else if (specs->short_p)
6985 error ("both %<short%> and %<double%> in "
6986 "declaration specifiers");
6987 else if (specs->signed_p)
6988 error ("both %<signed%> and %<double%> in "
6989 "declaration specifiers");
6990 else if (specs->unsigned_p)
6991 error ("both %<unsigned%> and %<double%> in "
6992 "declaration specifiers");
6993 else
6994 specs->typespec_word = cts_double;
6995 return specs;
6996 default:
6997 /* ObjC reserved word "id", handled below. */
6998 break;
7003 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7004 form of ObjC type, cases such as "int" and "long" being handled
7005 above), a TYPE (struct, union, enum and typeof specifiers) or an
7006 ERROR_MARK. In none of these cases may there have previously
7007 been any type specifiers. */
7008 if (specs->type || specs->typespec_word != cts_none
7009 || specs->long_p || specs->short_p || specs->signed_p
7010 || specs->unsigned_p || specs->complex_p)
7011 error ("two or more data types in declaration specifiers");
7012 else if (TREE_CODE (type) == TYPE_DECL)
7014 if (TREE_TYPE (type) == error_mark_node)
7015 ; /* Allow the type to default to int to avoid cascading errors. */
7016 else
7018 specs->type = TREE_TYPE (type);
7019 specs->decl_attr = DECL_ATTRIBUTES (type);
7020 specs->typedef_p = true;
7021 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7024 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7026 tree t = lookup_name (type);
7027 if (!t || TREE_CODE (t) != TYPE_DECL)
7028 error ("%qE fails to be a typedef or built in type", type);
7029 else if (TREE_TYPE (t) == error_mark_node)
7031 else
7032 specs->type = TREE_TYPE (t);
7034 else if (TREE_CODE (type) != ERROR_MARK)
7036 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7037 specs->tag_defined_p = true;
7038 if (spec.kind == ctsk_typeof)
7039 specs->typedef_p = true;
7040 specs->type = type;
7043 return specs;
7046 /* Add the storage class specifier or function specifier SCSPEC to the
7047 declaration specifiers SPECS, returning SPECS. */
7049 struct c_declspecs *
7050 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7052 enum rid i;
7053 enum c_storage_class n = csc_none;
7054 bool dupe = false;
7055 specs->declspecs_seen_p = true;
7056 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7057 && C_IS_RESERVED_WORD (scspec));
7058 i = C_RID_CODE (scspec);
7059 if (extra_warnings && specs->non_sc_seen_p)
7060 warning ("%qE is not at beginning of declaration", scspec);
7061 switch (i)
7063 case RID_INLINE:
7064 /* C99 permits duplicate inline. Although of doubtful utility,
7065 it seems simplest to permit it in gnu89 mode as well, as
7066 there is also little utility in maintaining this as a
7067 difference between gnu89 and C99 inline. */
7068 dupe = false;
7069 specs->inline_p = true;
7070 break;
7071 case RID_THREAD:
7072 dupe = specs->thread_p;
7073 if (specs->storage_class == csc_auto)
7074 error ("%<__thread%> used with %<auto%>");
7075 else if (specs->storage_class == csc_register)
7076 error ("%<__thread%> used with %<register%>");
7077 else if (specs->storage_class == csc_typedef)
7078 error ("%<__thread%> used with %<typedef%>");
7079 else
7080 specs->thread_p = true;
7081 break;
7082 case RID_AUTO:
7083 n = csc_auto;
7084 break;
7085 case RID_EXTERN:
7086 n = csc_extern;
7087 /* Diagnose "__thread extern". */
7088 if (specs->thread_p)
7089 error ("%<__thread%> before %<extern%>");
7090 break;
7091 case RID_REGISTER:
7092 n = csc_register;
7093 break;
7094 case RID_STATIC:
7095 n = csc_static;
7096 /* Diagnose "__thread static". */
7097 if (specs->thread_p)
7098 error ("%<__thread%> before %<static%>");
7099 break;
7100 case RID_TYPEDEF:
7101 n = csc_typedef;
7102 break;
7103 default:
7104 gcc_unreachable ();
7106 if (n != csc_none && n == specs->storage_class)
7107 dupe = true;
7108 if (dupe)
7109 error ("duplicate %qE", scspec);
7110 if (n != csc_none)
7112 if (specs->storage_class != csc_none && n != specs->storage_class)
7114 error ("multiple storage classes in declaration specifiers");
7116 else
7118 specs->storage_class = n;
7119 if (n != csc_extern && n != csc_static && specs->thread_p)
7121 error ("%<__thread%> used with %qE", scspec);
7122 specs->thread_p = false;
7126 return specs;
7129 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7130 returning SPECS. */
7132 struct c_declspecs *
7133 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7135 specs->attrs = chainon (attrs, specs->attrs);
7136 specs->declspecs_seen_p = true;
7137 return specs;
7140 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7141 specifiers with any other type specifier to determine the resulting
7142 type. This is where ISO C checks on complex types are made, since
7143 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7144 double". */
7146 struct c_declspecs *
7147 finish_declspecs (struct c_declspecs *specs)
7149 /* If a type was specified as a whole, we have no modifiers and are
7150 done. */
7151 if (specs->type != NULL_TREE)
7153 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7154 && !specs->signed_p && !specs->unsigned_p
7155 && !specs->complex_p);
7156 return specs;
7159 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7160 has been specified, treat it as "int" unless "_Complex" is
7161 present and there are no other specifiers. If we just have
7162 "_Complex", it is equivalent to "_Complex double", but e.g.
7163 "_Complex short" is equivalent to "_Complex short int". */
7164 if (specs->typespec_word == cts_none)
7166 if (specs->long_p || specs->short_p
7167 || specs->signed_p || specs->unsigned_p)
7169 specs->typespec_word = cts_int;
7171 else if (specs->complex_p)
7173 specs->typespec_word = cts_double;
7174 if (pedantic)
7175 pedwarn ("ISO C does not support plain %<complex%> meaning "
7176 "%<double complex%>");
7178 else
7180 specs->typespec_word = cts_int;
7181 specs->default_int_p = true;
7182 /* We don't diagnose this here because grokdeclarator will
7183 give more specific diagnostics according to whether it is
7184 a function definition. */
7188 /* If "signed" was specified, record this to distinguish "int" and
7189 "signed int" in the case of a bit-field with
7190 -funsigned-bitfields. */
7191 specs->explicit_signed_p = specs->signed_p;
7193 /* Now compute the actual type. */
7194 switch (specs->typespec_word)
7196 case cts_void:
7197 gcc_assert (!specs->long_p && !specs->short_p
7198 && !specs->signed_p && !specs->unsigned_p
7199 && !specs->complex_p);
7200 specs->type = void_type_node;
7201 break;
7202 case cts_bool:
7203 gcc_assert (!specs->long_p && !specs->short_p
7204 && !specs->signed_p && !specs->unsigned_p
7205 && !specs->complex_p);
7206 specs->type = boolean_type_node;
7207 break;
7208 case cts_char:
7209 gcc_assert (!specs->long_p && !specs->short_p);
7210 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7211 if (specs->signed_p)
7212 specs->type = signed_char_type_node;
7213 else if (specs->unsigned_p)
7214 specs->type = unsigned_char_type_node;
7215 else
7216 specs->type = char_type_node;
7217 if (specs->complex_p)
7219 if (pedantic)
7220 pedwarn ("ISO C does not support complex integer types");
7221 specs->type = build_complex_type (specs->type);
7223 break;
7224 case cts_int:
7225 gcc_assert (!(specs->long_p && specs->short_p));
7226 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7227 if (specs->long_long_p)
7228 specs->type = (specs->unsigned_p
7229 ? long_long_unsigned_type_node
7230 : long_long_integer_type_node);
7231 else if (specs->long_p)
7232 specs->type = (specs->unsigned_p
7233 ? long_unsigned_type_node
7234 : long_integer_type_node);
7235 else if (specs->short_p)
7236 specs->type = (specs->unsigned_p
7237 ? short_unsigned_type_node
7238 : short_integer_type_node);
7239 else
7240 specs->type = (specs->unsigned_p
7241 ? unsigned_type_node
7242 : integer_type_node);
7243 if (specs->complex_p)
7245 if (pedantic)
7246 pedwarn ("ISO C does not support complex integer types");
7247 specs->type = build_complex_type (specs->type);
7249 break;
7250 case cts_float:
7251 gcc_assert (!specs->long_p && !specs->short_p
7252 && !specs->signed_p && !specs->unsigned_p);
7253 specs->type = (specs->complex_p
7254 ? complex_float_type_node
7255 : float_type_node);
7256 break;
7257 case cts_double:
7258 gcc_assert (!specs->long_long_p && !specs->short_p
7259 && !specs->signed_p && !specs->unsigned_p);
7260 if (specs->long_p)
7262 specs->type = (specs->complex_p
7263 ? complex_long_double_type_node
7264 : long_double_type_node);
7266 else
7268 specs->type = (specs->complex_p
7269 ? complex_double_type_node
7270 : double_type_node);
7272 break;
7273 default:
7274 gcc_unreachable ();
7277 return specs;
7280 /* Synthesize a function which calls all the global ctors or global
7281 dtors in this file. This is only used for targets which do not
7282 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7283 static void
7284 build_cdtor (int method_type, tree cdtors)
7286 tree body = 0;
7288 if (!cdtors)
7289 return;
7291 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7292 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7293 &body);
7295 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7298 /* Perform final processing on one file scope's declarations (or the
7299 external scope's declarations), GLOBALS. */
7300 static void
7301 c_write_global_declarations_1 (tree globals)
7303 size_t len = list_length (globals);
7304 tree *vec = XNEWVEC (tree, len);
7305 size_t i;
7306 tree decl;
7308 /* Process the decls in the order they were written. */
7309 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7311 vec[i] = decl;
7312 /* Check for used but undefined static functions using the C
7313 standard's definition of "used", and set TREE_NO_WARNING so
7314 that check_global_declarations doesn't repeat the check. */
7315 if (TREE_CODE (decl) == FUNCTION_DECL
7316 && DECL_INITIAL (decl) == 0
7317 && DECL_EXTERNAL (decl)
7318 && !TREE_PUBLIC (decl)
7319 && C_DECL_USED (decl))
7321 pedwarn ("%J%qF used but never defined", decl, decl);
7322 TREE_NO_WARNING (decl) = 1;
7326 wrapup_global_declarations (vec, len);
7327 check_global_declarations (vec, len);
7329 free (vec);
7332 void
7333 c_write_global_declarations (void)
7335 tree ext_block, t;
7337 /* We don't want to do this if generating a PCH. */
7338 if (pch_file)
7339 return;
7341 /* Don't waste time on further processing if -fsyntax-only or we've
7342 encountered errors. */
7343 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7344 return;
7346 /* Close the external scope. */
7347 ext_block = pop_scope ();
7348 external_scope = 0;
7349 gcc_assert (!current_scope);
7351 /* Process all file scopes in this compilation, and the external_scope,
7352 through wrapup_global_declarations and check_global_declarations. */
7353 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7354 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7355 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7357 /* Generate functions to call static constructors and destructors
7358 for targets that do not support .ctors/.dtors sections. These
7359 functions have magic names which are detected by collect2. */
7360 build_cdtor ('I', static_ctors); static_ctors = 0;
7361 build_cdtor ('D', static_dtors); static_dtors = 0;
7363 /* We're done parsing; proceed to optimize and emit assembly.
7364 FIXME: shouldn't be the front end's responsibility to call this. */
7365 cgraph_optimize ();
7368 #include "gt-c-decl.h"