* arm.c (FL_WBUF): Define.
[official-gcc.git] / gcc / c-decl.c
blobd02d7429badd9ff63d8bc0af8fc1555ef07061e6
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 maybe_apply_pragma_weak (decl);
3267 /* If this is a variable definition, determine its ELF visibility. */
3268 if (TREE_CODE (decl) == VAR_DECL
3269 && TREE_STATIC (decl)
3270 && !DECL_EXTERNAL (decl))
3271 c_determine_visibility (decl);
3273 /* Output the assembler code and/or RTL code for variables and functions,
3274 unless the type is an undefined structure or union.
3275 If not, it will get done when the type is completed. */
3277 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3279 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3280 if (c_dialect_objc ())
3281 objc_check_decl (decl);
3283 if (asmspec)
3285 /* If this is not a static variable, issue a warning.
3286 It doesn't make any sense to give an ASMSPEC for an
3287 ordinary, non-register local variable. Historically,
3288 GCC has accepted -- but ignored -- the ASMSPEC in
3289 this case. */
3290 if (!DECL_FILE_SCOPE_P (decl)
3291 && TREE_CODE (decl) == VAR_DECL
3292 && !C_DECL_REGISTER (decl)
3293 && !TREE_STATIC (decl))
3294 warning ("%Jignoring asm-specifier for non-static local "
3295 "variable %qD", decl, decl);
3296 else if (C_DECL_REGISTER (decl))
3297 change_decl_assembler_name (decl, get_identifier (asmspec));
3298 else
3299 set_user_assembler_name (decl, asmspec);
3302 if (DECL_FILE_SCOPE_P (decl))
3304 if (DECL_INITIAL (decl) == NULL_TREE
3305 || DECL_INITIAL (decl) == error_mark_node)
3306 /* Don't output anything
3307 when a tentative file-scope definition is seen.
3308 But at end of compilation, do output code for them. */
3309 DECL_DEFER_OUTPUT (decl) = 1;
3310 rest_of_decl_compilation (decl, true, 0);
3312 else
3314 /* In conjunction with an ASMSPEC, the `register'
3315 keyword indicates that we should place the variable
3316 in a particular register. */
3317 if (asmspec && C_DECL_REGISTER (decl))
3319 DECL_HARD_REGISTER (decl) = 1;
3320 /* This cannot be done for a structure with volatile
3321 fields, on which DECL_REGISTER will have been
3322 reset. */
3323 if (!DECL_REGISTER (decl))
3324 error ("cannot put object with volatile field into register");
3327 if (TREE_CODE (decl) != FUNCTION_DECL)
3329 /* If we're building a variable sized type, and we might be
3330 reachable other than via the top of the current binding
3331 level, then create a new BIND_EXPR so that we deallocate
3332 the object at the right time. */
3333 /* Note that DECL_SIZE can be null due to errors. */
3334 if (DECL_SIZE (decl)
3335 && !TREE_CONSTANT (DECL_SIZE (decl))
3336 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3338 tree bind;
3339 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3340 TREE_SIDE_EFFECTS (bind) = 1;
3341 add_stmt (bind);
3342 BIND_EXPR_BODY (bind) = push_stmt_list ();
3344 add_stmt (build_stmt (DECL_EXPR, decl));
3349 if (!DECL_FILE_SCOPE_P (decl))
3351 /* Recompute the RTL of a local array now
3352 if it used to be an incomplete type. */
3353 if (was_incomplete
3354 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3356 /* If we used it already as memory, it must stay in memory. */
3357 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3358 /* If it's still incomplete now, no init will save it. */
3359 if (DECL_SIZE (decl) == 0)
3360 DECL_INITIAL (decl) = 0;
3365 /* If this was marked 'used', be sure it will be output. */
3366 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3367 mark_decl_referenced (decl);
3369 if (TREE_CODE (decl) == TYPE_DECL)
3371 if (!DECL_FILE_SCOPE_P (decl)
3372 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3373 add_stmt (build_stmt (DECL_EXPR, decl));
3375 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3378 /* At the end of a declaration, throw away any variable type sizes
3379 of types defined inside that declaration. There is no use
3380 computing them in the following function definition. */
3381 if (current_scope == file_scope)
3382 get_pending_sizes ();
3384 /* Install a cleanup (aka destructor) if one was given. */
3385 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3387 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3388 if (attr)
3390 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3391 tree cleanup_decl = lookup_name (cleanup_id);
3392 tree cleanup;
3394 /* Build "cleanup(&decl)" for the destructor. */
3395 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3396 cleanup = build_tree_list (NULL_TREE, cleanup);
3397 cleanup = build_function_call (cleanup_decl, cleanup);
3399 /* Don't warn about decl unused; the cleanup uses it. */
3400 TREE_USED (decl) = 1;
3401 TREE_USED (cleanup_decl) = 1;
3403 /* Initialize EH, if we've been told to do so. */
3404 if (flag_exceptions && !c_eh_initialized_p)
3406 c_eh_initialized_p = true;
3407 eh_personality_libfunc
3408 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3409 ? "__gcc_personality_sj0"
3410 : "__gcc_personality_v0");
3411 using_eh_for_cleanups ();
3414 push_cleanup (decl, cleanup, false);
3419 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3421 tree
3422 grokparm (const struct c_parm *parm)
3424 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3425 NULL);
3427 decl_attributes (&decl, parm->attrs, 0);
3429 return decl;
3432 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3433 and push that on the current scope. */
3435 void
3436 push_parm_decl (const struct c_parm *parm)
3438 tree decl;
3440 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3441 decl_attributes (&decl, parm->attrs, 0);
3443 decl = pushdecl (decl);
3445 finish_decl (decl, NULL_TREE, NULL_TREE);
3448 /* Mark all the parameter declarations to date as forward decls.
3449 Also diagnose use of this extension. */
3451 void
3452 mark_forward_parm_decls (void)
3454 struct c_binding *b;
3456 if (pedantic && !current_scope->warned_forward_parm_decls)
3458 pedwarn ("ISO C forbids forward parameter declarations");
3459 current_scope->warned_forward_parm_decls = true;
3462 for (b = current_scope->bindings; b; b = b->prev)
3463 if (TREE_CODE (b->decl) == PARM_DECL)
3464 TREE_ASM_WRITTEN (b->decl) = 1;
3467 static GTY(()) int compound_literal_number;
3469 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3470 literal, which may be an incomplete array type completed by the
3471 initializer; INIT is a CONSTRUCTOR that initializes the compound
3472 literal. */
3474 tree
3475 build_compound_literal (tree type, tree init)
3477 /* We do not use start_decl here because we have a type, not a declarator;
3478 and do not use finish_decl because the decl should be stored inside
3479 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3480 tree decl;
3481 tree complit;
3482 tree stmt;
3484 if (type == error_mark_node)
3485 return error_mark_node;
3487 decl = build_decl (VAR_DECL, NULL_TREE, type);
3488 DECL_EXTERNAL (decl) = 0;
3489 TREE_PUBLIC (decl) = 0;
3490 TREE_STATIC (decl) = (current_scope == file_scope);
3491 DECL_CONTEXT (decl) = current_function_decl;
3492 TREE_USED (decl) = 1;
3493 TREE_TYPE (decl) = type;
3494 TREE_READONLY (decl) = TYPE_READONLY (type);
3495 store_init_value (decl, init);
3497 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3499 int failure = complete_array_type (&TREE_TYPE (decl),
3500 DECL_INITIAL (decl), true);
3501 gcc_assert (!failure);
3503 type = TREE_TYPE (decl);
3504 TREE_TYPE (DECL_INITIAL (decl)) = type;
3507 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3508 return error_mark_node;
3510 stmt = build_stmt (DECL_EXPR, decl);
3511 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3512 TREE_SIDE_EFFECTS (complit) = 1;
3514 layout_decl (decl, 0);
3516 if (TREE_STATIC (decl))
3518 /* This decl needs a name for the assembler output. We also need
3519 a unique suffix to be added to the name. */
3520 char *name;
3522 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3523 compound_literal_number);
3524 compound_literal_number++;
3525 DECL_NAME (decl) = get_identifier (name);
3526 DECL_DEFER_OUTPUT (decl) = 1;
3527 DECL_COMDAT (decl) = 1;
3528 DECL_ARTIFICIAL (decl) = 1;
3529 DECL_IGNORED_P (decl) = 1;
3530 pushdecl (decl);
3531 rest_of_decl_compilation (decl, 1, 0);
3534 return complit;
3537 /* Determine whether TYPE is a structure with a flexible array member,
3538 or a union containing such a structure (possibly recursively). */
3540 static bool
3541 flexible_array_type_p (tree type)
3543 tree x;
3544 switch (TREE_CODE (type))
3546 case RECORD_TYPE:
3547 x = TYPE_FIELDS (type);
3548 if (x == NULL_TREE)
3549 return false;
3550 while (TREE_CHAIN (x) != NULL_TREE)
3551 x = TREE_CHAIN (x);
3552 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3553 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3554 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3555 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3556 return true;
3557 return false;
3558 case UNION_TYPE:
3559 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3561 if (flexible_array_type_p (TREE_TYPE (x)))
3562 return true;
3564 return false;
3565 default:
3566 return false;
3570 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3571 replacing with appropriate values if they are invalid. */
3572 static void
3573 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3575 tree type_mv;
3576 unsigned int max_width;
3577 unsigned HOST_WIDE_INT w;
3578 const char *name = orig_name ? orig_name: _("<anonymous>");
3580 /* Detect and ignore out of range field width and process valid
3581 field widths. */
3582 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3583 || TREE_CODE (*width) != INTEGER_CST)
3585 error ("bit-field %qs width not an integer constant", name);
3586 *width = integer_one_node;
3588 else
3590 constant_expression_warning (*width);
3591 if (tree_int_cst_sgn (*width) < 0)
3593 error ("negative width in bit-field %qs", name);
3594 *width = integer_one_node;
3596 else if (integer_zerop (*width) && orig_name)
3598 error ("zero width for bit-field %qs", name);
3599 *width = integer_one_node;
3603 /* Detect invalid bit-field type. */
3604 if (TREE_CODE (*type) != INTEGER_TYPE
3605 && TREE_CODE (*type) != BOOLEAN_TYPE
3606 && TREE_CODE (*type) != ENUMERAL_TYPE)
3608 error ("bit-field %qs has invalid type", name);
3609 *type = unsigned_type_node;
3612 type_mv = TYPE_MAIN_VARIANT (*type);
3613 if (pedantic
3614 && type_mv != integer_type_node
3615 && type_mv != unsigned_type_node
3616 && type_mv != boolean_type_node)
3617 pedwarn ("type of bit-field %qs is a GCC extension", name);
3619 if (type_mv == boolean_type_node)
3620 max_width = CHAR_TYPE_SIZE;
3621 else
3622 max_width = TYPE_PRECISION (*type);
3624 if (0 < compare_tree_int (*width, max_width))
3626 error ("width of %qs exceeds its type", name);
3627 w = max_width;
3628 *width = build_int_cst (NULL_TREE, w);
3630 else
3631 w = tree_low_cst (*width, 1);
3633 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3635 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3636 if (!lt
3637 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3638 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3639 warning ("%qs is narrower than values of its type", name);
3643 /* Given declspecs and a declarator,
3644 determine the name and type of the object declared
3645 and construct a ..._DECL node for it.
3646 (In one case we can return a ..._TYPE node instead.
3647 For invalid input we sometimes return 0.)
3649 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3651 DECL_CONTEXT says which syntactic context this declaration is in:
3652 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3653 FUNCDEF for a function definition. Like NORMAL but a few different
3654 error messages in each case. Return value may be zero meaning
3655 this definition is too screwy to try to parse.
3656 PARM for a parameter declaration (either within a function prototype
3657 or before a function body). Make a PARM_DECL, or return void_type_node.
3658 TYPENAME if for a typename (in a cast or sizeof).
3659 Don't make a DECL node; just return the ..._TYPE node.
3660 FIELD for a struct or union field; make a FIELD_DECL.
3661 INITIALIZED is true if the decl has an initializer.
3662 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3663 representing the width of the bit-field.
3665 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3666 It may also be so in the PARM case, for a prototype where the
3667 argument type is specified but not the name.
3669 This function is where the complicated C meanings of `static'
3670 and `extern' are interpreted. */
3672 static tree
3673 grokdeclarator (const struct c_declarator *declarator,
3674 struct c_declspecs *declspecs,
3675 enum decl_context decl_context, bool initialized, tree *width)
3677 tree type = declspecs->type;
3678 bool threadp = declspecs->thread_p;
3679 enum c_storage_class storage_class = declspecs->storage_class;
3680 int constp;
3681 int restrictp;
3682 int volatilep;
3683 int type_quals = TYPE_UNQUALIFIED;
3684 const char *name, *orig_name;
3685 tree typedef_type = 0;
3686 int funcdef_flag = 0;
3687 bool funcdef_syntax = false;
3688 int size_varies = 0;
3689 tree decl_attr = declspecs->decl_attr;
3690 int array_ptr_quals = TYPE_UNQUALIFIED;
3691 tree array_ptr_attrs = NULL_TREE;
3692 int array_parm_static = 0;
3693 tree returned_attrs = NULL_TREE;
3694 bool bitfield = width != NULL;
3695 tree element_type;
3696 struct c_arg_info *arg_info = 0;
3698 if (decl_context == FUNCDEF)
3699 funcdef_flag = 1, decl_context = NORMAL;
3701 /* Look inside a declarator for the name being declared
3702 and get it as a string, for an error message. */
3704 const struct c_declarator *decl = declarator;
3705 name = 0;
3707 while (decl)
3708 switch (decl->kind)
3710 case cdk_function:
3711 case cdk_array:
3712 case cdk_pointer:
3713 funcdef_syntax = (decl->kind == cdk_function);
3714 decl = decl->declarator;
3715 break;
3717 case cdk_attrs:
3718 decl = decl->declarator;
3719 break;
3721 case cdk_id:
3722 if (decl->u.id)
3723 name = IDENTIFIER_POINTER (decl->u.id);
3724 decl = 0;
3725 break;
3727 default:
3728 gcc_unreachable ();
3730 orig_name = name;
3731 if (name == 0)
3732 name = "type name";
3735 /* A function definition's declarator must have the form of
3736 a function declarator. */
3738 if (funcdef_flag && !funcdef_syntax)
3739 return 0;
3741 /* If this looks like a function definition, make it one,
3742 even if it occurs where parms are expected.
3743 Then store_parm_decls will reject it and not use it as a parm. */
3744 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3745 decl_context = PARM;
3747 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3748 warn_deprecated_use (declspecs->type);
3750 typedef_type = type;
3751 size_varies = C_TYPE_VARIABLE_SIZE (type);
3753 /* Diagnose defaulting to "int". */
3755 if (declspecs->default_int_p && !in_system_header)
3757 /* Issue a warning if this is an ISO C 99 program or if
3758 -Wreturn-type and this is a function, or if -Wimplicit;
3759 prefer the former warning since it is more explicit. */
3760 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3761 && funcdef_flag)
3762 warn_about_return_type = 1;
3763 else if (warn_implicit_int || flag_isoc99)
3764 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3767 /* Adjust the type if a bit-field is being declared,
3768 -funsigned-bitfields applied and the type is not explicitly
3769 "signed". */
3770 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3771 && TREE_CODE (type) == INTEGER_TYPE)
3772 type = c_common_unsigned_type (type);
3774 /* Figure out the type qualifiers for the declaration. There are
3775 two ways a declaration can become qualified. One is something
3776 like `const int i' where the `const' is explicit. Another is
3777 something like `typedef const int CI; CI i' where the type of the
3778 declaration contains the `const'. A third possibility is that
3779 there is a type qualifier on the element type of a typedefed
3780 array type, in which case we should extract that qualifier so
3781 that c_apply_type_quals_to_decls receives the full list of
3782 qualifiers to work with (C90 is not entirely clear about whether
3783 duplicate qualifiers should be diagnosed in this case, but it
3784 seems most appropriate to do so). */
3785 element_type = strip_array_types (type);
3786 constp = declspecs->const_p + TYPE_READONLY (element_type);
3787 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3788 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3789 if (pedantic && !flag_isoc99)
3791 if (constp > 1)
3792 pedwarn ("duplicate %<const%>");
3793 if (restrictp > 1)
3794 pedwarn ("duplicate %<restrict%>");
3795 if (volatilep > 1)
3796 pedwarn ("duplicate %<volatile%>");
3798 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3799 type = TYPE_MAIN_VARIANT (type);
3800 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3801 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3802 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3804 /* Warn about storage classes that are invalid for certain
3805 kinds of declarations (parameters, typenames, etc.). */
3807 if (funcdef_flag
3808 && (threadp
3809 || storage_class == csc_auto
3810 || storage_class == csc_register
3811 || storage_class == csc_typedef))
3813 if (storage_class == csc_auto
3814 && (pedantic || current_scope == file_scope))
3815 pedwarn ("function definition declared %<auto%>");
3816 if (storage_class == csc_register)
3817 error ("function definition declared %<register%>");
3818 if (storage_class == csc_typedef)
3819 error ("function definition declared %<typedef%>");
3820 if (threadp)
3821 error ("function definition declared %<__thread%>");
3822 threadp = false;
3823 if (storage_class == csc_auto
3824 || storage_class == csc_register
3825 || storage_class == csc_typedef)
3826 storage_class = csc_none;
3828 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3830 if (decl_context == PARM && storage_class == csc_register)
3832 else
3834 switch (decl_context)
3836 case FIELD:
3837 error ("storage class specified for structure field %qs",
3838 name);
3839 break;
3840 case PARM:
3841 error ("storage class specified for parameter %qs", name);
3842 break;
3843 default:
3844 error ("storage class specified for typename");
3845 break;
3847 storage_class = csc_none;
3848 threadp = false;
3851 else if (storage_class == csc_extern
3852 && initialized
3853 && !funcdef_flag)
3855 /* 'extern' with initialization is invalid if not at file scope. */
3856 if (current_scope == file_scope)
3857 warning ("%qs initialized and declared %<extern%>", name);
3858 else
3859 error ("%qs has both %<extern%> and initializer", name);
3861 else if (current_scope == file_scope)
3863 if (storage_class == csc_auto)
3864 error ("file-scope declaration of %qs specifies %<auto%>", name);
3865 if (pedantic && storage_class == csc_register)
3866 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3868 else
3870 if (storage_class == csc_extern && funcdef_flag)
3871 error ("nested function %qs declared %<extern%>", name);
3872 else if (threadp && storage_class == csc_none)
3874 error ("function-scope %qs implicitly auto and declared "
3875 "%<__thread%>",
3876 name);
3877 threadp = false;
3881 /* Now figure out the structure of the declarator proper.
3882 Descend through it, creating more complex types, until we reach
3883 the declared identifier (or NULL_TREE, in an absolute declarator).
3884 At each stage we maintain an unqualified version of the type
3885 together with any qualifiers that should be applied to it with
3886 c_build_qualified_type; this way, array types including
3887 multidimensional array types are first built up in unqualified
3888 form and then the qualified form is created with
3889 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3891 while (declarator && declarator->kind != cdk_id)
3893 if (type == error_mark_node)
3895 declarator = declarator->declarator;
3896 continue;
3899 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3900 a cdk_pointer (for *...),
3901 a cdk_function (for ...(...)),
3902 a cdk_attrs (for nested attributes),
3903 or a cdk_id (for the name being declared
3904 or the place in an absolute declarator
3905 where the name was omitted).
3906 For the last case, we have just exited the loop.
3908 At this point, TYPE is the type of elements of an array,
3909 or for a function to return, or for a pointer to point to.
3910 After this sequence of ifs, TYPE is the type of the
3911 array or function or pointer, and DECLARATOR has had its
3912 outermost layer removed. */
3914 if (array_ptr_quals != TYPE_UNQUALIFIED
3915 || array_ptr_attrs != NULL_TREE
3916 || array_parm_static)
3918 /* Only the innermost declarator (making a parameter be of
3919 array type which is converted to pointer type)
3920 may have static or type qualifiers. */
3921 error ("static or type qualifiers in non-parameter array declarator");
3922 array_ptr_quals = TYPE_UNQUALIFIED;
3923 array_ptr_attrs = NULL_TREE;
3924 array_parm_static = 0;
3927 switch (declarator->kind)
3929 case cdk_attrs:
3931 /* A declarator with embedded attributes. */
3932 tree attrs = declarator->u.attrs;
3933 const struct c_declarator *inner_decl;
3934 int attr_flags = 0;
3935 declarator = declarator->declarator;
3936 inner_decl = declarator;
3937 while (inner_decl->kind == cdk_attrs)
3938 inner_decl = inner_decl->declarator;
3939 if (inner_decl->kind == cdk_id)
3940 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3941 else if (inner_decl->kind == cdk_function)
3942 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3943 else if (inner_decl->kind == cdk_array)
3944 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3945 returned_attrs = decl_attributes (&type,
3946 chainon (returned_attrs, attrs),
3947 attr_flags);
3948 break;
3950 case cdk_array:
3952 tree itype = NULL_TREE;
3953 tree size = declarator->u.array.dimen;
3954 /* The index is a signed object `sizetype' bits wide. */
3955 tree index_type = c_common_signed_type (sizetype);
3957 array_ptr_quals = declarator->u.array.quals;
3958 array_ptr_attrs = declarator->u.array.attrs;
3959 array_parm_static = declarator->u.array.static_p;
3961 declarator = declarator->declarator;
3963 /* Check for some types that there cannot be arrays of. */
3965 if (VOID_TYPE_P (type))
3967 error ("declaration of %qs as array of voids", name);
3968 type = error_mark_node;
3971 if (TREE_CODE (type) == FUNCTION_TYPE)
3973 error ("declaration of %qs as array of functions", name);
3974 type = error_mark_node;
3977 if (pedantic && !in_system_header && flexible_array_type_p (type))
3978 pedwarn ("invalid use of structure with flexible array member");
3980 if (size == error_mark_node)
3981 type = error_mark_node;
3983 if (type == error_mark_node)
3984 continue;
3986 /* If size was specified, set ITYPE to a range-type for
3987 that size. Otherwise, ITYPE remains null. finish_decl
3988 may figure it out from an initial value. */
3990 if (size)
3992 /* Strip NON_LVALUE_EXPRs since we aren't using as an
3993 lvalue. */
3994 STRIP_TYPE_NOPS (size);
3996 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
3998 error ("size of array %qs has non-integer type", name);
3999 size = integer_one_node;
4002 if (pedantic && integer_zerop (size))
4003 pedwarn ("ISO C forbids zero-size array %qs", name);
4005 if (TREE_CODE (size) == INTEGER_CST)
4007 constant_expression_warning (size);
4008 if (tree_int_cst_sgn (size) < 0)
4010 error ("size of array %qs is negative", name);
4011 size = integer_one_node;
4014 else
4016 /* Make sure the array size remains visibly
4017 nonconstant even if it is (eg) a const variable
4018 with known value. */
4019 size_varies = 1;
4021 if (!flag_isoc99 && pedantic)
4023 if (TREE_CONSTANT (size))
4024 pedwarn ("ISO C90 forbids array %qs whose size "
4025 "can%'t be evaluated",
4026 name);
4027 else
4028 pedwarn ("ISO C90 forbids variable-size array %qs",
4029 name);
4033 if (integer_zerop (size))
4035 /* A zero-length array cannot be represented with
4036 an unsigned index type, which is what we'll
4037 get with build_index_type. Create an
4038 open-ended range instead. */
4039 itype = build_range_type (sizetype, size, NULL_TREE);
4041 else
4043 /* Arrange for the SAVE_EXPR on the inside of the
4044 MINUS_EXPR, which allows the -1 to get folded
4045 with the +1 that happens when building TYPE_SIZE. */
4046 if (size_varies)
4047 size = variable_size (size);
4049 /* Compute the maximum valid index, that is, size
4050 - 1. Do the calculation in index_type, so that
4051 if it is a variable the computations will be
4052 done in the proper mode. */
4053 itype = fold (build2 (MINUS_EXPR, index_type,
4054 convert (index_type, size),
4055 convert (index_type,
4056 size_one_node)));
4058 /* If that overflowed, the array is too big. ???
4059 While a size of INT_MAX+1 technically shouldn't
4060 cause an overflow (because we subtract 1), the
4061 overflow is recorded during the conversion to
4062 index_type, before the subtraction. Handling
4063 this case seems like an unnecessary
4064 complication. */
4065 if (TREE_OVERFLOW (itype))
4067 error ("size of array %qs is too large", name);
4068 type = error_mark_node;
4069 continue;
4072 itype = build_index_type (itype);
4075 else if (decl_context == FIELD)
4077 if (pedantic && !flag_isoc99 && !in_system_header)
4078 pedwarn ("ISO C90 does not support flexible array members");
4080 /* ISO C99 Flexible array members are effectively
4081 identical to GCC's zero-length array extension. */
4082 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4085 /* Complain about arrays of incomplete types. */
4086 if (!COMPLETE_TYPE_P (type))
4088 error ("array type has incomplete element type");
4089 type = error_mark_node;
4091 else
4092 type = build_array_type (type, itype);
4094 if (size_varies)
4095 C_TYPE_VARIABLE_SIZE (type) = 1;
4097 /* The GCC extension for zero-length arrays differs from
4098 ISO flexible array members in that sizeof yields
4099 zero. */
4100 if (size && integer_zerop (size))
4102 TYPE_SIZE (type) = bitsize_zero_node;
4103 TYPE_SIZE_UNIT (type) = size_zero_node;
4106 if (decl_context != PARM
4107 && (array_ptr_quals != TYPE_UNQUALIFIED
4108 || array_ptr_attrs != NULL_TREE
4109 || array_parm_static))
4111 error ("static or type qualifiers in non-parameter array declarator");
4112 array_ptr_quals = TYPE_UNQUALIFIED;
4113 array_ptr_attrs = NULL_TREE;
4114 array_parm_static = 0;
4116 break;
4118 case cdk_function:
4120 /* Say it's a definition only for the declarator closest
4121 to the identifier, apart possibly from some
4122 attributes. */
4123 bool really_funcdef = false;
4124 tree arg_types;
4125 if (funcdef_flag)
4127 const struct c_declarator *t = declarator->declarator;
4128 while (t->kind == cdk_attrs)
4129 t = t->declarator;
4130 really_funcdef = (t->kind == cdk_id);
4133 /* Declaring a function type. Make sure we have a valid
4134 type for the function to return. */
4135 if (type == error_mark_node)
4136 continue;
4138 size_varies = 0;
4140 /* Warn about some types functions can't return. */
4141 if (TREE_CODE (type) == FUNCTION_TYPE)
4143 error ("%qs declared as function returning a function", name);
4144 type = integer_type_node;
4146 if (TREE_CODE (type) == ARRAY_TYPE)
4148 error ("%qs declared as function returning an array", name);
4149 type = integer_type_node;
4152 /* Construct the function type and go to the next
4153 inner layer of declarator. */
4154 arg_info = declarator->u.arg_info;
4155 arg_types = grokparms (arg_info, really_funcdef);
4157 /* Type qualifiers before the return type of the function
4158 qualify the return type, not the function type. */
4159 if (type_quals)
4161 /* Type qualifiers on a function return type are
4162 normally permitted by the standard but have no
4163 effect, so give a warning at -Wreturn-type.
4164 Qualifiers on a void return type are banned on
4165 function definitions in ISO C; GCC used to used
4166 them for noreturn functions. */
4167 if (VOID_TYPE_P (type) && really_funcdef)
4168 pedwarn ("function definition has qualified void return type");
4169 else if (warn_return_type)
4170 warning ("type qualifiers ignored on function return type");
4172 type = c_build_qualified_type (type, type_quals);
4174 type_quals = TYPE_UNQUALIFIED;
4176 type = build_function_type (type, arg_types);
4177 declarator = declarator->declarator;
4179 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4180 the formal parameter list of this FUNCTION_TYPE to point to
4181 the FUNCTION_TYPE node itself. */
4183 tree link;
4185 for (link = arg_info->tags;
4186 link;
4187 link = TREE_CHAIN (link))
4188 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4190 break;
4192 case cdk_pointer:
4194 /* Merge any constancy or volatility into the target type
4195 for the pointer. */
4197 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4198 && type_quals)
4199 pedwarn ("ISO C forbids qualified function types");
4200 if (type_quals)
4201 type = c_build_qualified_type (type, type_quals);
4202 size_varies = 0;
4204 type = build_pointer_type (type);
4206 /* Process type qualifiers (such as const or volatile)
4207 that were given inside the `*'. */
4208 type_quals = declarator->u.pointer_quals;
4210 declarator = declarator->declarator;
4211 break;
4213 default:
4214 gcc_unreachable ();
4218 /* Now TYPE has the actual type, apart from any qualifiers in
4219 TYPE_QUALS. */
4221 /* Check the type and width of a bit-field. */
4222 if (bitfield)
4223 check_bitfield_type_and_width (&type, width, orig_name);
4225 /* Did array size calculations overflow? */
4227 if (TREE_CODE (type) == ARRAY_TYPE
4228 && COMPLETE_TYPE_P (type)
4229 && TREE_OVERFLOW (TYPE_SIZE (type)))
4231 error ("size of array %qs is too large", name);
4232 /* If we proceed with the array type as it is, we'll eventually
4233 crash in tree_low_cst(). */
4234 type = error_mark_node;
4237 /* If this is declaring a typedef name, return a TYPE_DECL. */
4239 if (storage_class == csc_typedef)
4241 tree decl;
4242 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4243 && type_quals)
4244 pedwarn ("ISO C forbids qualified function types");
4245 if (type_quals)
4246 type = c_build_qualified_type (type, type_quals);
4247 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4248 if (declspecs->explicit_signed_p)
4249 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4250 decl_attributes (&decl, returned_attrs, 0);
4251 if (declspecs->inline_p)
4252 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4253 return decl;
4256 /* Detect the case of an array type of unspecified size
4257 which came, as such, direct from a typedef name.
4258 We must copy the type, so that each identifier gets
4259 a distinct type, so that each identifier's size can be
4260 controlled separately by its own initializer. */
4262 if (type != 0 && typedef_type != 0
4263 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4264 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4266 type = build_array_type (TREE_TYPE (type), 0);
4267 if (size_varies)
4268 C_TYPE_VARIABLE_SIZE (type) = 1;
4271 /* If this is a type name (such as, in a cast or sizeof),
4272 compute the type and return it now. */
4274 if (decl_context == TYPENAME)
4276 /* Note that the grammar rejects storage classes in typenames
4277 and fields. */
4278 gcc_assert (storage_class == csc_none && !threadp
4279 && !declspecs->inline_p);
4280 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4281 && type_quals)
4282 pedwarn ("ISO C forbids const or volatile function types");
4283 if (type_quals)
4284 type = c_build_qualified_type (type, type_quals);
4285 decl_attributes (&type, returned_attrs, 0);
4286 return type;
4289 /* Aside from typedefs and type names (handle above),
4290 `void' at top level (not within pointer)
4291 is allowed only in public variables.
4292 We don't complain about parms either, but that is because
4293 a better error message can be made later. */
4295 if (VOID_TYPE_P (type) && decl_context != PARM
4296 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4297 && (storage_class == csc_extern
4298 || (current_scope == file_scope
4299 && !(storage_class == csc_static
4300 || storage_class == csc_register)))))
4302 error ("variable or field %qs declared void", name);
4303 type = integer_type_node;
4306 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4307 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4310 tree decl;
4312 if (decl_context == PARM)
4314 tree type_as_written;
4315 tree promoted_type;
4317 /* A parameter declared as an array of T is really a pointer to T.
4318 One declared as a function is really a pointer to a function. */
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4322 /* Transfer const-ness of array into that of type pointed to. */
4323 type = TREE_TYPE (type);
4324 if (type_quals)
4325 type = c_build_qualified_type (type, type_quals);
4326 type = build_pointer_type (type);
4327 type_quals = array_ptr_quals;
4329 /* We don't yet implement attributes in this context. */
4330 if (array_ptr_attrs != NULL_TREE)
4331 warning ("attributes in parameter array declarator ignored");
4333 size_varies = 0;
4335 else if (TREE_CODE (type) == FUNCTION_TYPE)
4337 if (pedantic && type_quals)
4338 pedwarn ("ISO C forbids qualified function types");
4339 if (type_quals)
4340 type = c_build_qualified_type (type, type_quals);
4341 type = build_pointer_type (type);
4342 type_quals = TYPE_UNQUALIFIED;
4344 else if (type_quals)
4345 type = c_build_qualified_type (type, type_quals);
4347 type_as_written = type;
4349 decl = build_decl (PARM_DECL, declarator->u.id, type);
4350 if (size_varies)
4351 C_DECL_VARIABLE_SIZE (decl) = 1;
4353 /* Compute the type actually passed in the parmlist,
4354 for the case where there is no prototype.
4355 (For example, shorts and chars are passed as ints.)
4356 When there is a prototype, this is overridden later. */
4358 if (type == error_mark_node)
4359 promoted_type = type;
4360 else
4361 promoted_type = c_type_promotes_to (type);
4363 DECL_ARG_TYPE (decl) = promoted_type;
4364 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4365 if (declspecs->inline_p)
4366 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4368 else if (decl_context == FIELD)
4370 /* Note that the grammar rejects storage classes in typenames
4371 and fields. */
4372 gcc_assert (storage_class == csc_none && !threadp
4373 && !declspecs->inline_p);
4375 /* Structure field. It may not be a function. */
4377 if (TREE_CODE (type) == FUNCTION_TYPE)
4379 error ("field %qs declared as a function", name);
4380 type = build_pointer_type (type);
4382 else if (TREE_CODE (type) != ERROR_MARK
4383 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4385 error ("field %qs has incomplete type", name);
4386 type = error_mark_node;
4388 type = c_build_qualified_type (type, type_quals);
4389 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4390 DECL_NONADDRESSABLE_P (decl) = bitfield;
4392 if (size_varies)
4393 C_DECL_VARIABLE_SIZE (decl) = 1;
4395 else if (TREE_CODE (type) == FUNCTION_TYPE)
4397 if (storage_class == csc_register || threadp)
4399 error ("invalid storage class for function %qs", name);
4401 else if (current_scope != file_scope)
4403 /* Function declaration not at file scope. Storage
4404 classes other than `extern' are not allowed, C99
4405 6.7.1p5, and `extern' makes no difference. However,
4406 GCC allows 'auto', perhaps with 'inline', to support
4407 nested functions. */
4408 if (storage_class == csc_auto)
4410 if (pedantic)
4411 pedwarn ("invalid storage class for function %qs", name);
4413 else if (storage_class == csc_static)
4415 error ("invalid storage class for function %qs", name);
4416 if (funcdef_flag)
4417 storage_class = declspecs->storage_class = csc_none;
4418 else
4419 return 0;
4423 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4424 decl = build_decl_attribute_variant (decl, decl_attr);
4426 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4428 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4429 pedwarn ("ISO C forbids qualified function types");
4431 /* GNU C interprets a volatile-qualified function type to indicate
4432 that the function does not return. */
4433 if ((type_quals & TYPE_QUAL_VOLATILE)
4434 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4435 warning ("%<noreturn%> function returns non-void value");
4437 /* Every function declaration is an external reference
4438 (DECL_EXTERNAL) except for those which are not at file
4439 scope and are explicitly declared "auto". This is
4440 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4441 GCC to signify a forward declaration of a nested function. */
4442 if (storage_class == csc_auto && current_scope != file_scope)
4443 DECL_EXTERNAL (decl) = 0;
4444 else
4445 DECL_EXTERNAL (decl) = 1;
4447 /* Record absence of global scope for `static' or `auto'. */
4448 TREE_PUBLIC (decl)
4449 = !(storage_class == csc_static || storage_class == csc_auto);
4451 /* For a function definition, record the argument information
4452 block where store_parm_decls will look for it. */
4453 if (funcdef_flag)
4454 current_function_arg_info = arg_info;
4456 if (declspecs->default_int_p)
4457 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4459 /* Record presence of `inline', if it is reasonable. */
4460 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4462 if (declspecs->inline_p)
4463 pedwarn ("cannot inline function %<main%>");
4465 else if (declspecs->inline_p)
4467 /* Record that the function is declared `inline'. */
4468 DECL_DECLARED_INLINE_P (decl) = 1;
4470 /* Do not mark bare declarations as DECL_INLINE. Doing so
4471 in the presence of multiple declarations can result in
4472 the abstract origin pointing between the declarations,
4473 which will confuse dwarf2out. */
4474 if (initialized)
4476 DECL_INLINE (decl) = 1;
4477 if (storage_class == csc_extern)
4478 current_extern_inline = 1;
4481 /* If -finline-functions, assume it can be inlined. This does
4482 two things: let the function be deferred until it is actually
4483 needed, and let dwarf2 know that the function is inlinable. */
4484 else if (flag_inline_trees == 2 && initialized)
4485 DECL_INLINE (decl) = 1;
4487 else
4489 /* It's a variable. */
4490 /* An uninitialized decl with `extern' is a reference. */
4491 int extern_ref = !initialized && storage_class == csc_extern;
4493 type = c_build_qualified_type (type, type_quals);
4495 /* C99 6.2.2p7: It is invalid (compile-time undefined
4496 behavior) to create an 'extern' declaration for a
4497 variable if there is a global declaration that is
4498 'static' and the global declaration is not visible.
4499 (If the static declaration _is_ currently visible,
4500 the 'extern' declaration is taken to refer to that decl.) */
4501 if (extern_ref && current_scope != file_scope)
4503 tree global_decl = identifier_global_value (declarator->u.id);
4504 tree visible_decl = lookup_name (declarator->u.id);
4506 if (global_decl
4507 && global_decl != visible_decl
4508 && TREE_CODE (global_decl) == VAR_DECL
4509 && !TREE_PUBLIC (global_decl))
4510 error ("variable previously declared %<static%> redeclared "
4511 "%<extern%>");
4514 decl = build_decl (VAR_DECL, declarator->u.id, type);
4515 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4516 if (size_varies)
4517 C_DECL_VARIABLE_SIZE (decl) = 1;
4519 if (declspecs->inline_p)
4520 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4522 /* At file scope, an initialized extern declaration may follow
4523 a static declaration. In that case, DECL_EXTERNAL will be
4524 reset later in start_decl. */
4525 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4527 /* At file scope, the presence of a `static' or `register' storage
4528 class specifier, or the absence of all storage class specifiers
4529 makes this declaration a definition (perhaps tentative). Also,
4530 the absence of both `static' and `register' makes it public. */
4531 if (current_scope == file_scope)
4533 TREE_PUBLIC (decl) = !(storage_class == csc_static
4534 || storage_class == csc_register);
4535 TREE_STATIC (decl) = !extern_ref;
4537 /* Not at file scope, only `static' makes a static definition. */
4538 else
4540 TREE_STATIC (decl) = (storage_class == csc_static);
4541 TREE_PUBLIC (decl) = extern_ref;
4544 if (threadp)
4546 if (targetm.have_tls)
4547 DECL_THREAD_LOCAL (decl) = 1;
4548 else
4549 /* A mere warning is sure to result in improper semantics
4550 at runtime. Don't bother to allow this to compile. */
4551 error ("thread-local storage not supported for this target");
4555 /* Record `register' declaration for warnings on &
4556 and in case doing stupid register allocation. */
4558 if (storage_class == csc_register)
4560 C_DECL_REGISTER (decl) = 1;
4561 DECL_REGISTER (decl) = 1;
4564 /* Record constancy and volatility. */
4565 c_apply_type_quals_to_decl (type_quals, decl);
4567 /* If a type has volatile components, it should be stored in memory.
4568 Otherwise, the fact that those components are volatile
4569 will be ignored, and would even crash the compiler. */
4570 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4572 /* It is not an error for a structure with volatile fields to
4573 be declared register, but reset DECL_REGISTER since it
4574 cannot actually go in a register. */
4575 int was_reg = C_DECL_REGISTER (decl);
4576 C_DECL_REGISTER (decl) = 0;
4577 DECL_REGISTER (decl) = 0;
4578 c_mark_addressable (decl);
4579 C_DECL_REGISTER (decl) = was_reg;
4582 /* This is the earliest point at which we might know the assembler
4583 name of a variable. Thus, if it's known before this, die horribly. */
4584 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4586 decl_attributes (&decl, returned_attrs, 0);
4588 return decl;
4592 /* Decode the parameter-list info for a function type or function definition.
4593 The argument is the value returned by `get_parm_info' (or made in parse.y
4594 if there is an identifier list instead of a parameter decl list).
4595 These two functions are separate because when a function returns
4596 or receives functions then each is called multiple times but the order
4597 of calls is different. The last call to `grokparms' is always the one
4598 that contains the formal parameter names of a function definition.
4600 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4602 FUNCDEF_FLAG is true for a function definition, false for
4603 a mere declaration. A nonempty identifier-list gets an error message
4604 when FUNCDEF_FLAG is false. */
4606 static tree
4607 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4609 tree arg_types = arg_info->types;
4611 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4612 && !in_system_header)
4613 warning ("function declaration isn%'t a prototype");
4615 if (arg_types == error_mark_node)
4616 return 0; /* don't set TYPE_ARG_TYPES in this case */
4618 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4620 if (!funcdef_flag)
4621 pedwarn ("parameter names (without types) in function declaration");
4623 arg_info->parms = arg_info->types;
4624 arg_info->types = 0;
4625 return 0;
4627 else
4629 tree parm, type, typelt;
4630 unsigned int parmno;
4632 /* If there is a parameter of incomplete type in a definition,
4633 this is an error. In a declaration this is valid, and a
4634 struct or union type may be completed later, before any calls
4635 or definition of the function. In the case where the tag was
4636 first declared within the parameter list, a warning has
4637 already been given. If a parameter has void type, then
4638 however the function cannot be defined or called, so
4639 warn. */
4641 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4642 parm;
4643 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4645 type = TREE_VALUE (typelt);
4646 if (type == error_mark_node)
4647 continue;
4649 if (!COMPLETE_TYPE_P (type))
4651 if (funcdef_flag)
4653 if (DECL_NAME (parm))
4654 error ("%Jparameter %u (%qD) has incomplete type",
4655 parm, parmno, parm);
4656 else
4657 error ("%Jparameter %u has incomplete type",
4658 parm, parmno);
4660 TREE_VALUE (typelt) = error_mark_node;
4661 TREE_TYPE (parm) = error_mark_node;
4663 else if (VOID_TYPE_P (type))
4665 if (DECL_NAME (parm))
4666 warning ("%Jparameter %u (%qD) has void type",
4667 parm, parmno, parm);
4668 else
4669 warning ("%Jparameter %u has void type",
4670 parm, parmno);
4674 return arg_types;
4678 /* Take apart the current scope and return a c_arg_info structure with
4679 info on a parameter list just parsed.
4681 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4683 ELLIPSIS being true means the argument list ended in '...' so don't
4684 append a sentinel (void_list_node) to the end of the type-list. */
4686 struct c_arg_info *
4687 get_parm_info (bool ellipsis)
4689 struct c_binding *b = current_scope->bindings;
4690 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4691 struct c_arg_info);
4692 tree parms = 0;
4693 tree tags = 0;
4694 tree types = 0;
4695 tree others = 0;
4697 static bool explained_incomplete_types = false;
4698 bool gave_void_only_once_err = false;
4700 arg_info->parms = 0;
4701 arg_info->tags = 0;
4702 arg_info->types = 0;
4703 arg_info->others = 0;
4705 /* The bindings in this scope must not get put into a block.
4706 We will take care of deleting the binding nodes. */
4707 current_scope->bindings = 0;
4709 /* This function is only called if there was *something* on the
4710 parameter list. */
4711 gcc_assert (b);
4713 /* A parameter list consisting solely of 'void' indicates that the
4714 function takes no arguments. But if the 'void' is qualified
4715 (by 'const' or 'volatile'), or has a storage class specifier
4716 ('register'), then the behavior is undefined; issue an error.
4717 Typedefs for 'void' are OK (see DR#157). */
4718 if (b->prev == 0 /* one binding */
4719 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4720 && !DECL_NAME (b->decl) /* anonymous */
4721 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4723 if (TREE_THIS_VOLATILE (b->decl)
4724 || TREE_READONLY (b->decl)
4725 || C_DECL_REGISTER (b->decl))
4726 error ("%<void%> as only parameter may not be qualified");
4728 /* There cannot be an ellipsis. */
4729 if (ellipsis)
4730 error ("%<void%> must be the only parameter");
4732 arg_info->types = void_list_node;
4733 return arg_info;
4736 if (!ellipsis)
4737 types = void_list_node;
4739 /* Break up the bindings list into parms, tags, types, and others;
4740 apply sanity checks; purge the name-to-decl bindings. */
4741 while (b)
4743 tree decl = b->decl;
4744 tree type = TREE_TYPE (decl);
4745 const char *keyword;
4747 switch (TREE_CODE (decl))
4749 case PARM_DECL:
4750 if (b->id)
4752 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4753 I_SYMBOL_BINDING (b->id) = b->shadowed;
4756 /* Check for forward decls that never got their actual decl. */
4757 if (TREE_ASM_WRITTEN (decl))
4758 error ("%Jparameter %qD has just a forward declaration",
4759 decl, decl);
4760 /* Check for (..., void, ...) and issue an error. */
4761 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4763 if (!gave_void_only_once_err)
4765 error ("%<void%> must be the only parameter");
4766 gave_void_only_once_err = true;
4769 else
4771 /* Valid parameter, add it to the list. */
4772 TREE_CHAIN (decl) = parms;
4773 parms = decl;
4775 /* Since there is a prototype, args are passed in their
4776 declared types. The back end may override this later. */
4777 DECL_ARG_TYPE (decl) = type;
4778 types = tree_cons (0, type, types);
4780 break;
4782 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4783 case UNION_TYPE: keyword = "union"; goto tag;
4784 case RECORD_TYPE: keyword = "struct"; goto tag;
4785 tag:
4786 /* Types may not have tag-names, in which case the type
4787 appears in the bindings list with b->id NULL. */
4788 if (b->id)
4790 gcc_assert (I_TAG_BINDING (b->id) == b);
4791 I_TAG_BINDING (b->id) = b->shadowed;
4794 /* Warn about any struct, union or enum tags defined in a
4795 parameter list. The scope of such types is limited to
4796 the parameter list, which is rarely if ever desirable
4797 (it's impossible to call such a function with type-
4798 correct arguments). An anonymous union parm type is
4799 meaningful as a GNU extension, so don't warn for that. */
4800 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4802 if (b->id)
4803 /* The %s will be one of 'struct', 'union', or 'enum'. */
4804 warning ("%<%s %E%> declared inside parameter list",
4805 keyword, b->id);
4806 else
4807 /* The %s will be one of 'struct', 'union', or 'enum'. */
4808 warning ("anonymous %s declared inside parameter list",
4809 keyword);
4811 if (!explained_incomplete_types)
4813 warning ("its scope is only this definition or declaration,"
4814 " which is probably not what you want");
4815 explained_incomplete_types = true;
4819 tags = tree_cons (b->id, decl, tags);
4820 break;
4822 case CONST_DECL:
4823 case TYPE_DECL:
4824 case FUNCTION_DECL:
4825 /* CONST_DECLs appear here when we have an embedded enum,
4826 and TYPE_DECLs appear here when we have an embedded struct
4827 or union. No warnings for this - we already warned about the
4828 type itself. FUNCTION_DECLs appear when there is an implicit
4829 function declaration in the parameter list. */
4831 TREE_CHAIN (decl) = others;
4832 others = decl;
4833 /* fall through */
4835 case ERROR_MARK:
4836 /* error_mark_node appears here when we have an undeclared
4837 variable. Just throw it away. */
4838 if (b->id)
4840 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4841 I_SYMBOL_BINDING (b->id) = b->shadowed;
4843 break;
4845 /* Other things that might be encountered. */
4846 case LABEL_DECL:
4847 case VAR_DECL:
4848 default:
4849 gcc_unreachable ();
4852 b = free_binding_and_advance (b);
4855 arg_info->parms = parms;
4856 arg_info->tags = tags;
4857 arg_info->types = types;
4858 arg_info->others = others;
4859 return arg_info;
4862 /* Get the struct, enum or union (CODE says which) with tag NAME.
4863 Define the tag as a forward-reference if it is not defined.
4864 Return a c_typespec structure for the type specifier. */
4866 struct c_typespec
4867 parser_xref_tag (enum tree_code code, tree name)
4869 struct c_typespec ret;
4870 /* If a cross reference is requested, look up the type
4871 already defined for this tag and return it. */
4873 tree ref = lookup_tag (code, name, 0);
4874 /* If this is the right type of tag, return what we found.
4875 (This reference will be shadowed by shadow_tag later if appropriate.)
4876 If this is the wrong type of tag, do not return it. If it was the
4877 wrong type in the same scope, we will have had an error
4878 message already; if in a different scope and declaring
4879 a name, pending_xref_error will give an error message; but if in a
4880 different scope and not declaring a name, this tag should
4881 shadow the previous declaration of a different type of tag, and
4882 this would not work properly if we return the reference found.
4883 (For example, with "struct foo" in an outer scope, "union foo;"
4884 must shadow that tag with a new one of union type.) */
4885 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4886 if (ref && TREE_CODE (ref) == code)
4888 ret.spec = ref;
4889 return ret;
4892 /* If no such tag is yet defined, create a forward-reference node
4893 and record it as the "definition".
4894 When a real declaration of this type is found,
4895 the forward-reference will be altered into a real type. */
4897 ref = make_node (code);
4898 if (code == ENUMERAL_TYPE)
4900 /* Give the type a default layout like unsigned int
4901 to avoid crashing if it does not get defined. */
4902 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4903 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4904 TYPE_USER_ALIGN (ref) = 0;
4905 TYPE_UNSIGNED (ref) = 1;
4906 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4907 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4908 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4911 pushtag (name, ref);
4913 ret.spec = ref;
4914 return ret;
4917 /* Get the struct, enum or union (CODE says which) with tag NAME.
4918 Define the tag as a forward-reference if it is not defined.
4919 Return a tree for the type. */
4921 tree
4922 xref_tag (enum tree_code code, tree name)
4924 return parser_xref_tag (code, name).spec;
4927 /* Make sure that the tag NAME is defined *in the current scope*
4928 at least as a forward reference.
4929 CODE says which kind of tag NAME ought to be. */
4931 tree
4932 start_struct (enum tree_code code, tree name)
4934 /* If there is already a tag defined at this scope
4935 (as a forward reference), just return it. */
4937 tree ref = 0;
4939 if (name != 0)
4940 ref = lookup_tag (code, name, 1);
4941 if (ref && TREE_CODE (ref) == code)
4943 if (TYPE_SIZE (ref))
4945 if (code == UNION_TYPE)
4946 error ("redefinition of %<union %E%>", name);
4947 else
4948 error ("redefinition of %<struct %E%>", name);
4950 else if (C_TYPE_BEING_DEFINED (ref))
4952 if (code == UNION_TYPE)
4953 error ("nested redefinition of %<union %E%>", name);
4954 else
4955 error ("nested redefinition of %<struct %E%>", name);
4958 else
4960 /* Otherwise create a forward-reference just so the tag is in scope. */
4962 ref = make_node (code);
4963 pushtag (name, ref);
4966 C_TYPE_BEING_DEFINED (ref) = 1;
4967 TYPE_PACKED (ref) = flag_pack_struct;
4968 return ref;
4971 /* Process the specs, declarator and width (NULL if omitted)
4972 of a structure component, returning a FIELD_DECL node.
4973 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4975 This is done during the parsing of the struct declaration.
4976 The FIELD_DECL nodes are chained together and the lot of them
4977 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4979 tree
4980 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
4981 tree width)
4983 tree value;
4985 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
4986 && width == NULL_TREE)
4988 /* This is an unnamed decl.
4990 If we have something of the form "union { list } ;" then this
4991 is the anonymous union extension. Similarly for struct.
4993 If this is something of the form "struct foo;", then
4994 If MS extensions are enabled, this is handled as an
4995 anonymous struct.
4996 Otherwise this is a forward declaration of a structure tag.
4998 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4999 If MS extensions are enabled and foo names a structure, then
5000 again this is an anonymous struct.
5001 Otherwise this is an error.
5003 Oh what a horrid tangled web we weave. I wonder if MS consciously
5004 took this from Plan 9 or if it was an accident of implementation
5005 that took root before someone noticed the bug... */
5007 tree type = declspecs->type;
5008 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5009 || TREE_CODE (type) == UNION_TYPE);
5010 bool ok = false;
5012 if (type_ok
5013 && (flag_ms_extensions || !declspecs->typedef_p))
5015 if (flag_ms_extensions)
5016 ok = true;
5017 else if (flag_iso)
5018 ok = false;
5019 else if (TYPE_NAME (type) == NULL)
5020 ok = true;
5021 else
5022 ok = false;
5024 if (!ok)
5026 pedwarn ("declaration does not declare anything");
5027 return NULL_TREE;
5029 if (pedantic)
5030 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5033 value = grokdeclarator (declarator, declspecs, FIELD, false,
5034 width ? &width : NULL);
5036 finish_decl (value, NULL_TREE, NULL_TREE);
5037 DECL_INITIAL (value) = width;
5039 return value;
5042 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5043 the list such that this does not present a problem later. */
5045 static void
5046 detect_field_duplicates (tree fieldlist)
5048 tree x, y;
5049 int timeout = 10;
5051 /* First, see if there are more than "a few" fields.
5052 This is trivially true if there are zero or one fields. */
5053 if (!fieldlist)
5054 return;
5055 x = TREE_CHAIN (fieldlist);
5056 if (!x)
5057 return;
5058 do {
5059 timeout--;
5060 x = TREE_CHAIN (x);
5061 } while (timeout > 0 && x);
5063 /* If there were "few" fields, avoid the overhead of allocating
5064 a hash table. Instead just do the nested traversal thing. */
5065 if (timeout > 0)
5067 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5068 if (DECL_NAME (x))
5070 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5071 if (DECL_NAME (y) == DECL_NAME (x))
5073 error ("%Jduplicate member %qD", x, x);
5074 DECL_NAME (x) = NULL_TREE;
5078 else
5080 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5081 void **slot;
5083 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5084 if ((y = DECL_NAME (x)) != 0)
5086 slot = htab_find_slot (htab, y, INSERT);
5087 if (*slot)
5089 error ("%Jduplicate member %qD", x, x);
5090 DECL_NAME (x) = NULL_TREE;
5092 *slot = y;
5095 htab_delete (htab);
5099 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5100 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5101 ATTRIBUTES are attributes to be applied to the structure. */
5103 tree
5104 finish_struct (tree t, tree fieldlist, tree attributes)
5106 tree x;
5107 bool toplevel = file_scope == current_scope;
5108 int saw_named_field;
5110 /* If this type was previously laid out as a forward reference,
5111 make sure we lay it out again. */
5113 TYPE_SIZE (t) = 0;
5115 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5117 if (pedantic)
5119 for (x = fieldlist; x; x = TREE_CHAIN (x))
5120 if (DECL_NAME (x) != 0)
5121 break;
5123 if (x == 0)
5125 if (TREE_CODE (t) == UNION_TYPE)
5127 if (fieldlist)
5128 pedwarn ("union has no named members");
5129 else
5130 pedwarn ("union has no members");
5132 else
5134 if (fieldlist)
5135 pedwarn ("struct has no named members");
5136 else
5137 pedwarn ("struct has no members");
5142 /* Install struct as DECL_CONTEXT of each field decl.
5143 Also process specified field sizes, found in the DECL_INITIAL,
5144 storing 0 there after the type has been changed to precision equal
5145 to its width, rather than the precision of the specified standard
5146 type. (Correct layout requires the original type to have been preserved
5147 until now.) */
5149 saw_named_field = 0;
5150 for (x = fieldlist; x; x = TREE_CHAIN (x))
5152 DECL_CONTEXT (x) = t;
5153 DECL_PACKED (x) |= TYPE_PACKED (t);
5155 /* If any field is const, the structure type is pseudo-const. */
5156 if (TREE_READONLY (x))
5157 C_TYPE_FIELDS_READONLY (t) = 1;
5158 else
5160 /* A field that is pseudo-const makes the structure likewise. */
5161 tree t1 = TREE_TYPE (x);
5162 while (TREE_CODE (t1) == ARRAY_TYPE)
5163 t1 = TREE_TYPE (t1);
5164 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5165 && C_TYPE_FIELDS_READONLY (t1))
5166 C_TYPE_FIELDS_READONLY (t) = 1;
5169 /* Any field that is volatile means variables of this type must be
5170 treated in some ways as volatile. */
5171 if (TREE_THIS_VOLATILE (x))
5172 C_TYPE_FIELDS_VOLATILE (t) = 1;
5174 /* Any field of nominal variable size implies structure is too. */
5175 if (C_DECL_VARIABLE_SIZE (x))
5176 C_TYPE_VARIABLE_SIZE (t) = 1;
5178 if (DECL_INITIAL (x))
5180 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5181 DECL_SIZE (x) = bitsize_int (width);
5182 DECL_BIT_FIELD (x) = 1;
5183 SET_DECL_C_BIT_FIELD (x);
5186 /* Detect flexible array member in an invalid context. */
5187 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5188 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5189 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5190 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5192 if (TREE_CODE (t) == UNION_TYPE)
5194 error ("%Jflexible array member in union", x);
5195 TREE_TYPE (x) = error_mark_node;
5197 else if (TREE_CHAIN (x) != NULL_TREE)
5199 error ("%Jflexible array member not at end of struct", x);
5200 TREE_TYPE (x) = error_mark_node;
5202 else if (!saw_named_field)
5204 error ("%Jflexible array member in otherwise empty struct", x);
5205 TREE_TYPE (x) = error_mark_node;
5209 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5210 && flexible_array_type_p (TREE_TYPE (x)))
5211 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5213 if (DECL_NAME (x))
5214 saw_named_field = 1;
5217 detect_field_duplicates (fieldlist);
5219 /* Now we have the nearly final fieldlist. Record it,
5220 then lay out the structure or union (including the fields). */
5222 TYPE_FIELDS (t) = fieldlist;
5224 layout_type (t);
5226 /* Give bit-fields their proper types. */
5228 tree *fieldlistp = &fieldlist;
5229 while (*fieldlistp)
5230 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5231 && TREE_TYPE (*fieldlistp) != error_mark_node)
5233 unsigned HOST_WIDE_INT width
5234 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5235 tree type = TREE_TYPE (*fieldlistp);
5236 if (width != TYPE_PRECISION (type))
5238 TREE_TYPE (*fieldlistp)
5239 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5240 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5242 DECL_INITIAL (*fieldlistp) = 0;
5244 else
5245 fieldlistp = &TREE_CHAIN (*fieldlistp);
5248 /* Now we have the truly final field list.
5249 Store it in this type and in the variants. */
5251 TYPE_FIELDS (t) = fieldlist;
5253 /* If there are lots of fields, sort so we can look through them fast.
5254 We arbitrarily consider 16 or more elts to be "a lot". */
5257 int len = 0;
5259 for (x = fieldlist; x; x = TREE_CHAIN (x))
5261 if (len > 15 || DECL_NAME (x) == NULL)
5262 break;
5263 len += 1;
5266 if (len > 15)
5268 tree *field_array;
5269 struct lang_type *space;
5270 struct sorted_fields_type *space2;
5272 len += list_length (x);
5274 /* Use the same allocation policy here that make_node uses, to
5275 ensure that this lives as long as the rest of the struct decl.
5276 All decls in an inline function need to be saved. */
5278 space = GGC_CNEW (struct lang_type);
5279 space2 = GGC_NEWVAR (struct sorted_fields_type,
5280 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5282 len = 0;
5283 space->s = space2;
5284 field_array = &space2->elts[0];
5285 for (x = fieldlist; x; x = TREE_CHAIN (x))
5287 field_array[len++] = x;
5289 /* If there is anonymous struct or union, break out of the loop. */
5290 if (DECL_NAME (x) == NULL)
5291 break;
5293 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5294 if (x == NULL)
5296 TYPE_LANG_SPECIFIC (t) = space;
5297 TYPE_LANG_SPECIFIC (t)->s->len = len;
5298 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5299 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5304 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5306 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5307 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5308 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5309 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5312 /* If this was supposed to be a transparent union, but we can't
5313 make it one, warn and turn off the flag. */
5314 if (TREE_CODE (t) == UNION_TYPE
5315 && TYPE_TRANSPARENT_UNION (t)
5316 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5318 TYPE_TRANSPARENT_UNION (t) = 0;
5319 warning ("union cannot be made transparent");
5322 /* If this structure or union completes the type of any previous
5323 variable declaration, lay it out and output its rtl. */
5324 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5326 x = TREE_CHAIN (x))
5328 tree decl = TREE_VALUE (x);
5329 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5330 layout_array_type (TREE_TYPE (decl));
5331 if (TREE_CODE (decl) != TYPE_DECL)
5333 layout_decl (decl, 0);
5334 if (c_dialect_objc ())
5335 objc_check_decl (decl);
5336 rest_of_decl_compilation (decl, toplevel, 0);
5337 if (!toplevel)
5338 expand_decl (decl);
5341 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5343 /* Finish debugging output for this type. */
5344 rest_of_type_compilation (t, toplevel);
5346 /* If we're inside a function proper, i.e. not file-scope and not still
5347 parsing parameters, then arrange for the size of a variable sized type
5348 to be bound now. */
5349 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5350 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5352 return t;
5355 /* Lay out the type T, and its element type, and so on. */
5357 static void
5358 layout_array_type (tree t)
5360 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5361 layout_array_type (TREE_TYPE (t));
5362 layout_type (t);
5365 /* Begin compiling the definition of an enumeration type.
5366 NAME is its name (or null if anonymous).
5367 Returns the type object, as yet incomplete.
5368 Also records info about it so that build_enumerator
5369 may be used to declare the individual values as they are read. */
5371 tree
5372 start_enum (tree name)
5374 tree enumtype = 0;
5376 /* If this is the real definition for a previous forward reference,
5377 fill in the contents in the same object that used to be the
5378 forward reference. */
5380 if (name != 0)
5381 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5383 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5385 enumtype = make_node (ENUMERAL_TYPE);
5386 pushtag (name, enumtype);
5389 if (C_TYPE_BEING_DEFINED (enumtype))
5390 error ("nested redefinition of %<enum %E%>", name);
5392 C_TYPE_BEING_DEFINED (enumtype) = 1;
5394 if (TYPE_VALUES (enumtype) != 0)
5396 /* This enum is a named one that has been declared already. */
5397 error ("redeclaration of %<enum %E%>", name);
5399 /* Completely replace its old definition.
5400 The old enumerators remain defined, however. */
5401 TYPE_VALUES (enumtype) = 0;
5404 enum_next_value = integer_zero_node;
5405 enum_overflow = 0;
5407 if (flag_short_enums)
5408 TYPE_PACKED (enumtype) = 1;
5410 return enumtype;
5413 /* After processing and defining all the values of an enumeration type,
5414 install their decls in the enumeration type and finish it off.
5415 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5416 and ATTRIBUTES are the specified attributes.
5417 Returns ENUMTYPE. */
5419 tree
5420 finish_enum (tree enumtype, tree values, tree attributes)
5422 tree pair, tem;
5423 tree minnode = 0, maxnode = 0;
5424 int precision, unsign;
5425 bool toplevel = (file_scope == current_scope);
5426 struct lang_type *lt;
5428 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5430 /* Calculate the maximum value of any enumerator in this type. */
5432 if (values == error_mark_node)
5433 minnode = maxnode = integer_zero_node;
5434 else
5436 minnode = maxnode = TREE_VALUE (values);
5437 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5439 tree value = TREE_VALUE (pair);
5440 if (tree_int_cst_lt (maxnode, value))
5441 maxnode = value;
5442 if (tree_int_cst_lt (value, minnode))
5443 minnode = value;
5447 /* Construct the final type of this enumeration. It is the same
5448 as one of the integral types - the narrowest one that fits, except
5449 that normally we only go as narrow as int - and signed iff any of
5450 the values are negative. */
5451 unsign = (tree_int_cst_sgn (minnode) >= 0);
5452 precision = MAX (min_precision (minnode, unsign),
5453 min_precision (maxnode, unsign));
5455 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5457 tem = c_common_type_for_size (precision, unsign);
5458 if (tem == NULL)
5460 warning ("enumeration values exceed range of largest integer");
5461 tem = long_long_integer_type_node;
5464 else
5465 tem = unsign ? unsigned_type_node : integer_type_node;
5467 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5468 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5469 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5470 TYPE_SIZE (enumtype) = 0;
5472 /* If the precision of the type was specific with an attribute and it
5473 was too small, give an error. Otherwise, use it. */
5474 if (TYPE_PRECISION (enumtype))
5476 if (precision > TYPE_PRECISION (enumtype))
5477 error ("specified mode too small for enumeral values");
5479 else
5480 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5482 layout_type (enumtype);
5484 if (values != error_mark_node)
5486 /* Change the type of the enumerators to be the enum type. We
5487 need to do this irrespective of the size of the enum, for
5488 proper type checking. Replace the DECL_INITIALs of the
5489 enumerators, and the value slots of the list, with copies
5490 that have the enum type; they cannot be modified in place
5491 because they may be shared (e.g. integer_zero_node) Finally,
5492 change the purpose slots to point to the names of the decls. */
5493 for (pair = values; pair; pair = TREE_CHAIN (pair))
5495 tree enu = TREE_PURPOSE (pair);
5496 tree ini = DECL_INITIAL (enu);
5498 TREE_TYPE (enu) = enumtype;
5500 /* The ISO C Standard mandates enumerators to have type int,
5501 even though the underlying type of an enum type is
5502 unspecified. Here we convert any enumerators that fit in
5503 an int to type int, to avoid promotions to unsigned types
5504 when comparing integers with enumerators that fit in the
5505 int range. When -pedantic is given, build_enumerator()
5506 would have already taken care of those that don't fit. */
5507 if (int_fits_type_p (ini, integer_type_node))
5508 tem = integer_type_node;
5509 else
5510 tem = enumtype;
5511 ini = convert (tem, ini);
5513 DECL_INITIAL (enu) = ini;
5514 TREE_PURPOSE (pair) = DECL_NAME (enu);
5515 TREE_VALUE (pair) = ini;
5518 TYPE_VALUES (enumtype) = values;
5521 /* Record the min/max values so that we can warn about bit-field
5522 enumerations that are too small for the values. */
5523 lt = GGC_CNEW (struct lang_type);
5524 lt->enum_min = minnode;
5525 lt->enum_max = maxnode;
5526 TYPE_LANG_SPECIFIC (enumtype) = lt;
5528 /* Fix up all variant types of this enum type. */
5529 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5531 if (tem == enumtype)
5532 continue;
5533 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5534 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5535 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5536 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5537 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5538 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5539 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5540 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5541 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5542 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5543 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5546 /* Finish debugging output for this type. */
5547 rest_of_type_compilation (enumtype, toplevel);
5549 return enumtype;
5552 /* Build and install a CONST_DECL for one value of the
5553 current enumeration type (one that was begun with start_enum).
5554 Return a tree-list containing the CONST_DECL and its value.
5555 Assignment of sequential values by default is handled here. */
5557 tree
5558 build_enumerator (tree name, tree value)
5560 tree decl, type;
5562 /* Validate and default VALUE. */
5564 if (value != 0)
5566 /* Don't issue more errors for error_mark_node (i.e. an
5567 undeclared identifier) - just ignore the value expression. */
5568 if (value == error_mark_node)
5569 value = 0;
5570 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5571 || TREE_CODE (value) != INTEGER_CST)
5573 error ("enumerator value for %qE is not an integer constant", name);
5574 value = 0;
5576 else
5578 value = default_conversion (value);
5579 constant_expression_warning (value);
5583 /* Default based on previous value. */
5584 /* It should no longer be possible to have NON_LVALUE_EXPR
5585 in the default. */
5586 if (value == 0)
5588 value = enum_next_value;
5589 if (enum_overflow)
5590 error ("overflow in enumeration values");
5593 if (pedantic && !int_fits_type_p (value, integer_type_node))
5595 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5596 /* XXX This causes -pedantic to change the meaning of the program.
5597 Remove? -zw 2004-03-15 */
5598 value = convert (integer_type_node, value);
5601 /* Set basis for default for next value. */
5602 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5603 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5605 /* Now create a declaration for the enum value name. */
5607 type = TREE_TYPE (value);
5608 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5609 TYPE_PRECISION (integer_type_node)),
5610 (TYPE_PRECISION (type)
5611 >= TYPE_PRECISION (integer_type_node)
5612 && TYPE_UNSIGNED (type)));
5614 decl = build_decl (CONST_DECL, name, type);
5615 DECL_INITIAL (decl) = convert (type, value);
5616 pushdecl (decl);
5618 return tree_cons (decl, value, NULL_TREE);
5622 /* Create the FUNCTION_DECL for a function definition.
5623 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5624 the declaration; they describe the function's name and the type it returns,
5625 but twisted together in a fashion that parallels the syntax of C.
5627 This function creates a binding context for the function body
5628 as well as setting up the FUNCTION_DECL in current_function_decl.
5630 Returns 1 on success. If the DECLARATOR is not suitable for a function
5631 (it defines a datum instead), we return 0, which tells
5632 yyparse to report a parse error. */
5635 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5636 tree attributes)
5638 tree decl1, old_decl;
5639 tree restype, resdecl;
5640 struct c_label_context *nstack;
5642 current_function_returns_value = 0; /* Assume, until we see it does. */
5643 current_function_returns_null = 0;
5644 current_function_returns_abnormally = 0;
5645 warn_about_return_type = 0;
5646 current_extern_inline = 0;
5647 c_switch_stack = NULL;
5649 nstack = XOBNEW (&parser_obstack, struct c_label_context);
5650 nstack->labels_def = NULL;
5651 nstack->labels_used = NULL;
5652 nstack->next = label_context_stack;
5653 label_context_stack = nstack;
5655 /* Indicate no valid break/continue context by setting these variables
5656 to some non-null, non-label value. We'll notice and emit the proper
5657 error message in c_finish_bc_stmt. */
5658 c_break_label = c_cont_label = size_zero_node;
5660 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5662 /* If the declarator is not suitable for a function definition,
5663 cause a syntax error. */
5664 if (decl1 == 0)
5665 return 0;
5667 decl_attributes (&decl1, attributes, 0);
5669 if (DECL_DECLARED_INLINE_P (decl1)
5670 && DECL_UNINLINABLE (decl1)
5671 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5672 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5674 announce_function (decl1);
5676 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5678 error ("return type is an incomplete type");
5679 /* Make it return void instead. */
5680 TREE_TYPE (decl1)
5681 = build_function_type (void_type_node,
5682 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5685 if (warn_about_return_type)
5686 pedwarn_c99 ("return type defaults to %<int%>");
5688 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5689 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5690 DECL_INITIAL (decl1) = error_mark_node;
5692 /* If this definition isn't a prototype and we had a prototype declaration
5693 before, copy the arg type info from that prototype. */
5694 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5695 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5696 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5697 TREE_TYPE (TREE_TYPE (old_decl)))
5698 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5700 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5701 TREE_TYPE (decl1));
5702 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5705 /* Optionally warn of old-fashioned def with no previous prototype. */
5706 if (warn_strict_prototypes
5707 && old_decl != error_mark_node
5708 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5709 && C_DECL_ISNT_PROTOTYPE (old_decl))
5710 warning ("function declaration isn%'t a prototype");
5711 /* Optionally warn of any global def with no previous prototype. */
5712 else if (warn_missing_prototypes
5713 && old_decl != error_mark_node
5714 && TREE_PUBLIC (decl1)
5715 && !MAIN_NAME_P (DECL_NAME (decl1))
5716 && C_DECL_ISNT_PROTOTYPE (old_decl))
5717 warning ("%Jno previous prototype for %qD", decl1, decl1);
5718 /* Optionally warn of any def with no previous prototype
5719 if the function has already been used. */
5720 else if (warn_missing_prototypes
5721 && old_decl != 0
5722 && old_decl != error_mark_node
5723 && TREE_USED (old_decl)
5724 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5725 warning ("%J%qD was used with no prototype before its definition",
5726 decl1, decl1);
5727 /* Optionally warn of any global def with no previous declaration. */
5728 else if (warn_missing_declarations
5729 && TREE_PUBLIC (decl1)
5730 && old_decl == 0
5731 && !MAIN_NAME_P (DECL_NAME (decl1)))
5732 warning ("%Jno previous declaration for %qD", decl1, decl1);
5733 /* Optionally warn of any def with no previous declaration
5734 if the function has already been used. */
5735 else if (warn_missing_declarations
5736 && old_decl != 0
5737 && old_decl != error_mark_node
5738 && TREE_USED (old_decl)
5739 && C_DECL_IMPLICIT (old_decl))
5740 warning ("%J%qD was used with no declaration before its definition",
5741 decl1, decl1);
5743 /* This is a definition, not a reference.
5744 So normally clear DECL_EXTERNAL.
5745 However, `extern inline' acts like a declaration
5746 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5747 DECL_EXTERNAL (decl1) = current_extern_inline;
5749 /* This function exists in static storage.
5750 (This does not mean `static' in the C sense!) */
5751 TREE_STATIC (decl1) = 1;
5753 /* A nested function is not global. */
5754 if (current_function_decl != 0)
5755 TREE_PUBLIC (decl1) = 0;
5757 /* This is the earliest point at which we might know the assembler
5758 name of the function. Thus, if it's set before this, die horribly. */
5759 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5761 /* If #pragma weak was used, mark the decl weak now. */
5762 if (current_scope == file_scope)
5763 maybe_apply_pragma_weak (decl1);
5765 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5766 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5768 tree args;
5769 int argct = 0;
5771 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5772 != integer_type_node)
5773 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5775 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5776 args = TREE_CHAIN (args))
5778 tree type = args ? TREE_VALUE (args) : 0;
5780 if (type == void_type_node)
5781 break;
5783 ++argct;
5784 switch (argct)
5786 case 1:
5787 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5788 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5789 decl1, decl1);
5790 break;
5792 case 2:
5793 if (TREE_CODE (type) != POINTER_TYPE
5794 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5795 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5796 != char_type_node))
5797 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5798 decl1, decl1);
5799 break;
5801 case 3:
5802 if (TREE_CODE (type) != POINTER_TYPE
5803 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5804 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5805 != char_type_node))
5806 pedwarn ("%Jthird argument of %qD should probably be "
5807 "%<char **%>", decl1, decl1);
5808 break;
5812 /* It is intentional that this message does not mention the third
5813 argument because it's only mentioned in an appendix of the
5814 standard. */
5815 if (argct > 0 && (argct < 2 || argct > 3))
5816 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5818 if (!TREE_PUBLIC (decl1))
5819 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5822 /* Record the decl so that the function name is defined.
5823 If we already have a decl for this name, and it is a FUNCTION_DECL,
5824 use the old decl. */
5826 current_function_decl = pushdecl (decl1);
5828 push_scope ();
5829 declare_parm_level ();
5831 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5832 /* Promote the value to int before returning it. */
5833 if (c_promoting_integer_type_p (restype))
5835 /* It retains unsignedness if not really getting wider. */
5836 if (TYPE_UNSIGNED (restype)
5837 && (TYPE_PRECISION (restype)
5838 == TYPE_PRECISION (integer_type_node)))
5839 restype = unsigned_type_node;
5840 else
5841 restype = integer_type_node;
5844 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5845 DECL_ARTIFICIAL (resdecl) = 1;
5846 DECL_IGNORED_P (resdecl) = 1;
5847 DECL_RESULT (current_function_decl) = resdecl;
5849 start_fname_decls ();
5851 return 1;
5854 /* Subroutine of store_parm_decls which handles new-style function
5855 definitions (prototype format). The parms already have decls, so we
5856 need only record them as in effect and complain if any redundant
5857 old-style parm decls were written. */
5858 static void
5859 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5861 tree decl;
5863 if (current_scope->bindings)
5865 error ("%Jold-style parameter declarations in prototyped "
5866 "function definition", fndecl);
5868 /* Get rid of the old-style declarations. */
5869 pop_scope ();
5870 push_scope ();
5872 /* Don't issue this warning for nested functions, and don't issue this
5873 warning if we got here because ARG_INFO_TYPES was error_mark_node
5874 (this happens when a function definition has just an ellipsis in
5875 its parameter list). */
5876 else if (warn_traditional && !in_system_header && !current_function_scope
5877 && arg_info->types != error_mark_node)
5878 warning ("%Jtraditional C rejects ISO C style function definitions",
5879 fndecl);
5881 /* Now make all the parameter declarations visible in the function body.
5882 We can bypass most of the grunt work of pushdecl. */
5883 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5885 DECL_CONTEXT (decl) = current_function_decl;
5886 if (DECL_NAME (decl))
5887 bind (DECL_NAME (decl), decl, current_scope,
5888 /*invisible=*/false, /*nested=*/false);
5889 else
5890 error ("%Jparameter name omitted", decl);
5893 /* Record the parameter list in the function declaration. */
5894 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5896 /* Now make all the ancillary declarations visible, likewise. */
5897 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5899 DECL_CONTEXT (decl) = current_function_decl;
5900 if (DECL_NAME (decl))
5901 bind (DECL_NAME (decl), decl, current_scope,
5902 /*invisible=*/false, /*nested=*/false);
5905 /* And all the tag declarations. */
5906 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5907 if (TREE_PURPOSE (decl))
5908 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5909 /*invisible=*/false, /*nested=*/false);
5912 /* Subroutine of store_parm_decls which handles old-style function
5913 definitions (separate parameter list and declarations). */
5915 static void
5916 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5918 struct c_binding *b;
5919 tree parm, decl, last;
5920 tree parmids = arg_info->parms;
5922 /* We use DECL_WEAK as a flag to show which parameters have been
5923 seen already, since it is not used on PARM_DECL. */
5924 #ifdef ENABLE_CHECKING
5925 for (b = current_scope->bindings; b; b = b->prev)
5926 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5927 #endif
5929 if (warn_old_style_definition && !in_system_header)
5930 warning ("%Jold-style function definition", fndecl);
5932 /* Match each formal parameter name with its declaration. Save each
5933 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5934 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5936 if (TREE_VALUE (parm) == 0)
5938 error ("%Jparameter name missing from parameter list", fndecl);
5939 TREE_PURPOSE (parm) = 0;
5940 continue;
5943 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5944 if (b && B_IN_CURRENT_SCOPE (b))
5946 decl = b->decl;
5947 /* If we got something other than a PARM_DECL it is an error. */
5948 if (TREE_CODE (decl) != PARM_DECL)
5949 error ("%J%qD declared as a non-parameter", decl, decl);
5950 /* If the declaration is already marked, we have a duplicate
5951 name. Complain and ignore the duplicate. */
5952 else if (DECL_WEAK (decl))
5954 error ("%Jmultiple parameters named %qD", decl, decl);
5955 TREE_PURPOSE (parm) = 0;
5956 continue;
5958 /* If the declaration says "void", complain and turn it into
5959 an int. */
5960 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5962 error ("%Jparameter %qD declared with void type", decl, decl);
5963 TREE_TYPE (decl) = integer_type_node;
5964 DECL_ARG_TYPE (decl) = integer_type_node;
5965 layout_decl (decl, 0);
5968 /* If no declaration found, default to int. */
5969 else
5971 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5972 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5973 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5974 pushdecl (decl);
5976 if (flag_isoc99)
5977 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
5978 else if (extra_warnings)
5979 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
5982 TREE_PURPOSE (parm) = decl;
5983 DECL_WEAK (decl) = 1;
5986 /* Now examine the parms chain for incomplete declarations
5987 and declarations with no corresponding names. */
5989 for (b = current_scope->bindings; b; b = b->prev)
5991 parm = b->decl;
5992 if (TREE_CODE (parm) != PARM_DECL)
5993 continue;
5995 if (TREE_TYPE (parm) != error_mark_node
5996 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
5998 error ("%Jparameter %qD has incomplete type", parm, parm);
5999 TREE_TYPE (parm) = error_mark_node;
6002 if (!DECL_WEAK (parm))
6004 error ("%Jdeclaration for parameter %qD but no such parameter",
6005 parm, parm);
6007 /* Pretend the parameter was not missing.
6008 This gets us to a standard state and minimizes
6009 further error messages. */
6010 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6014 /* Chain the declarations together in the order of the list of
6015 names. Store that chain in the function decl, replacing the
6016 list of names. Update the current scope to match. */
6017 DECL_ARGUMENTS (fndecl) = 0;
6019 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6020 if (TREE_PURPOSE (parm))
6021 break;
6022 if (parm && TREE_PURPOSE (parm))
6024 last = TREE_PURPOSE (parm);
6025 DECL_ARGUMENTS (fndecl) = last;
6026 DECL_WEAK (last) = 0;
6028 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6029 if (TREE_PURPOSE (parm))
6031 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6032 last = TREE_PURPOSE (parm);
6033 DECL_WEAK (last) = 0;
6035 TREE_CHAIN (last) = 0;
6038 /* If there was a previous prototype,
6039 set the DECL_ARG_TYPE of each argument according to
6040 the type previously specified, and report any mismatches. */
6042 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6044 tree type;
6045 for (parm = DECL_ARGUMENTS (fndecl),
6046 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6047 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6048 != void_type_node));
6049 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6051 if (parm == 0 || type == 0
6052 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6054 error ("number of arguments doesn%'t match prototype");
6055 error ("%Hprototype declaration",
6056 &current_function_prototype_locus);
6057 break;
6059 /* Type for passing arg must be consistent with that
6060 declared for the arg. ISO C says we take the unqualified
6061 type for parameters declared with qualified type. */
6062 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6063 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6065 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6066 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6068 /* Adjust argument to match prototype. E.g. a previous
6069 `int foo(float);' prototype causes
6070 `int foo(x) float x; {...}' to be treated like
6071 `int foo(float x) {...}'. This is particularly
6072 useful for argument types like uid_t. */
6073 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6075 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6076 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6077 && TYPE_PRECISION (TREE_TYPE (parm))
6078 < TYPE_PRECISION (integer_type_node))
6079 DECL_ARG_TYPE (parm) = integer_type_node;
6081 if (pedantic)
6083 pedwarn ("promoted argument %qD "
6084 "doesn%'t match prototype", parm);
6085 pedwarn ("%Hprototype declaration",
6086 &current_function_prototype_locus);
6089 else
6091 error ("argument %qD doesn%'t match prototype", parm);
6092 error ("%Hprototype declaration",
6093 &current_function_prototype_locus);
6097 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6100 /* Otherwise, create a prototype that would match. */
6102 else
6104 tree actual = 0, last = 0, type;
6106 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6108 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6109 if (last)
6110 TREE_CHAIN (last) = type;
6111 else
6112 actual = type;
6113 last = type;
6115 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6116 if (last)
6117 TREE_CHAIN (last) = type;
6118 else
6119 actual = type;
6121 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6122 of the type of this function, but we need to avoid having this
6123 affect the types of other similarly-typed functions, so we must
6124 first force the generation of an identical (but separate) type
6125 node for the relevant function type. The new node we create
6126 will be a variant of the main variant of the original function
6127 type. */
6129 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6131 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6135 /* Store parameter declarations passed in ARG_INFO into the current
6136 function declaration. */
6138 void
6139 store_parm_decls_from (struct c_arg_info *arg_info)
6141 current_function_arg_info = arg_info;
6142 store_parm_decls ();
6145 /* Store the parameter declarations into the current function declaration.
6146 This is called after parsing the parameter declarations, before
6147 digesting the body of the function.
6149 For an old-style definition, construct a prototype out of the old-style
6150 parameter declarations and inject it into the function's type. */
6152 void
6153 store_parm_decls (void)
6155 tree fndecl = current_function_decl;
6156 bool proto;
6158 /* The argument information block for FNDECL. */
6159 struct c_arg_info *arg_info = current_function_arg_info;
6160 current_function_arg_info = 0;
6162 /* True if this definition is written with a prototype. Note:
6163 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6164 list in a function definition as equivalent to (void) -- an
6165 empty argument list specifies the function has no parameters,
6166 but only (void) sets up a prototype for future calls. */
6167 proto = arg_info->types != 0;
6169 if (proto)
6170 store_parm_decls_newstyle (fndecl, arg_info);
6171 else
6172 store_parm_decls_oldstyle (fndecl, arg_info);
6174 /* The next call to push_scope will be a function body. */
6176 next_is_function_body = true;
6178 /* Write a record describing this function definition to the prototypes
6179 file (if requested). */
6181 gen_aux_info_record (fndecl, 1, 0, proto);
6183 /* Initialize the RTL code for the function. */
6184 allocate_struct_function (fndecl);
6186 /* Begin the statement tree for this function. */
6187 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6189 /* ??? Insert the contents of the pending sizes list into the function
6190 to be evaluated. The only reason left to have this is
6191 void foo(int n, int array[n++])
6192 because we throw away the array type in favor of a pointer type, and
6193 thus won't naturally see the SAVE_EXPR containing the increment. All
6194 other pending sizes would be handled by gimplify_parameters. */
6196 tree t;
6197 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6198 add_stmt (TREE_VALUE (t));
6201 /* Even though we're inside a function body, we still don't want to
6202 call expand_expr to calculate the size of a variable-sized array.
6203 We haven't necessarily assigned RTL to all variables yet, so it's
6204 not safe to try to expand expressions involving them. */
6205 cfun->x_dont_save_pending_sizes_p = 1;
6208 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6209 functions. */
6211 static void
6212 c_warn_unused_result_recursively (tree fndecl)
6214 struct cgraph_node *cgn;
6216 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6217 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6219 /* Finalize all nested functions now. */
6220 cgn = cgraph_node (fndecl);
6221 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6222 c_warn_unused_result_recursively (cgn->decl);
6225 /* Finish up a function declaration and compile that function
6226 all the way to assembler language output. The free the storage
6227 for the function definition.
6229 This is called after parsing the body of the function definition. */
6231 void
6232 finish_function (void)
6234 tree fndecl = current_function_decl;
6236 label_context_stack = label_context_stack->next;
6238 if (TREE_CODE (fndecl) == FUNCTION_DECL
6239 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6241 tree args = DECL_ARGUMENTS (fndecl);
6242 for (; args; args = TREE_CHAIN (args))
6244 tree type = TREE_TYPE (args);
6245 if (INTEGRAL_TYPE_P (type)
6246 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6247 DECL_ARG_TYPE (args) = integer_type_node;
6251 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6252 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6254 /* Must mark the RESULT_DECL as being in this function. */
6256 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6257 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6259 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6261 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6262 != integer_type_node)
6264 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6265 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6266 if (!warn_main)
6267 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6269 else
6271 if (flag_isoc99)
6273 tree stmt = c_finish_return (integer_zero_node);
6274 #ifdef USE_MAPPED_LOCATION
6275 /* Hack. We don't want the middle-end to warn that this return
6276 is unreachable, so we mark its location as special. Using
6277 UNKNOWN_LOCATION has the problem that it gets clobbered in
6278 annotate_one_with_locus. A cleaner solution might be to
6279 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6281 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6282 #else
6283 /* Hack. We don't want the middle-end to warn that this
6284 return is unreachable, so put the statement on the
6285 special line 0. */
6286 annotate_with_file_line (stmt, input_filename, 0);
6287 #endif
6292 /* Tie off the statement tree for this function. */
6293 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6295 finish_fname_decls ();
6297 /* Complain if there's just no return statement. */
6298 if (warn_return_type
6299 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6300 && !current_function_returns_value && !current_function_returns_null
6301 /* Don't complain if we abort. */
6302 && !current_function_returns_abnormally
6303 /* Don't warn for main(). */
6304 && !MAIN_NAME_P (DECL_NAME (fndecl))
6305 /* Or if they didn't actually specify a return type. */
6306 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6307 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6308 inline function, as we might never be compiled separately. */
6309 && DECL_INLINE (fndecl))
6310 warning ("no return statement in function returning non-void");
6312 /* With just -Wextra, complain only if function returns both with
6313 and without a value. */
6314 if (extra_warnings
6315 && current_function_returns_value
6316 && current_function_returns_null)
6317 warning ("this function may return with or without a value");
6319 /* Store the end of the function, so that we get good line number
6320 info for the epilogue. */
6321 cfun->function_end_locus = input_location;
6323 /* If we don't have ctors/dtors sections, and this is a static
6324 constructor or destructor, it must be recorded now. */
6325 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6326 && !targetm.have_ctors_dtors)
6327 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6328 if (DECL_STATIC_DESTRUCTOR (fndecl)
6329 && !targetm.have_ctors_dtors)
6330 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6332 /* Finalize the ELF visibility for the function. */
6333 c_determine_visibility (fndecl);
6335 /* Genericize before inlining. Delay genericizing nested functions
6336 until their parent function is genericized. Since finalizing
6337 requires GENERIC, delay that as well. */
6339 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6340 && !undef_nested_function)
6342 if (!decl_function_context (fndecl))
6344 c_genericize (fndecl);
6345 c_warn_unused_result_recursively (fndecl);
6347 /* ??? Objc emits functions after finalizing the compilation unit.
6348 This should be cleaned up later and this conditional removed. */
6349 if (cgraph_global_info_ready)
6351 c_expand_body (fndecl);
6352 return;
6355 cgraph_finalize_function (fndecl, false);
6357 else
6359 /* Register this function with cgraph just far enough to get it
6360 added to our parent's nested function list. Handy, since the
6361 C front end doesn't have such a list. */
6362 (void) cgraph_node (fndecl);
6366 if (!decl_function_context (fndecl))
6367 undef_nested_function = false;
6369 /* We're leaving the context of this function, so zap cfun.
6370 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6371 tree_rest_of_compilation. */
6372 cfun = NULL;
6373 current_function_decl = NULL;
6376 /* Generate the RTL for the body of FNDECL. */
6378 void
6379 c_expand_body (tree fndecl)
6382 if (!DECL_INITIAL (fndecl)
6383 || DECL_INITIAL (fndecl) == error_mark_node)
6384 return;
6386 tree_rest_of_compilation (fndecl);
6388 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6389 && targetm.have_ctors_dtors)
6390 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6391 DEFAULT_INIT_PRIORITY);
6392 if (DECL_STATIC_DESTRUCTOR (fndecl)
6393 && targetm.have_ctors_dtors)
6394 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6395 DEFAULT_INIT_PRIORITY);
6398 /* Check the declarations given in a for-loop for satisfying the C99
6399 constraints. */
6400 void
6401 check_for_loop_decls (void)
6403 struct c_binding *b;
6405 if (!flag_isoc99)
6407 /* If we get here, declarations have been used in a for loop without
6408 the C99 for loop scope. This doesn't make much sense, so don't
6409 allow it. */
6410 error ("%<for%> loop initial declaration used outside C99 mode");
6411 return;
6413 /* C99 subclause 6.8.5 paragraph 3:
6415 [#3] The declaration part of a for statement shall only
6416 declare identifiers for objects having storage class auto or
6417 register.
6419 It isn't clear whether, in this sentence, "identifiers" binds to
6420 "shall only declare" or to "objects" - that is, whether all identifiers
6421 declared must be identifiers for objects, or whether the restriction
6422 only applies to those that are. (A question on this in comp.std.c
6423 in November 2000 received no answer.) We implement the strictest
6424 interpretation, to avoid creating an extension which later causes
6425 problems. */
6427 for (b = current_scope->bindings; b; b = b->prev)
6429 tree id = b->id;
6430 tree decl = b->decl;
6432 if (!id)
6433 continue;
6435 switch (TREE_CODE (decl))
6437 case VAR_DECL:
6438 if (TREE_STATIC (decl))
6439 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6440 "initial declaration", decl, decl);
6441 else if (DECL_EXTERNAL (decl))
6442 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6443 "initial declaration", decl, decl);
6444 break;
6446 case RECORD_TYPE:
6447 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6448 id);
6449 break;
6450 case UNION_TYPE:
6451 error ("%<union %E%> declared in %<for%> loop initial declaration",
6452 id);
6453 break;
6454 case ENUMERAL_TYPE:
6455 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6456 id);
6457 break;
6458 default:
6459 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6460 "initial declaration", decl, decl);
6465 /* Save and reinitialize the variables
6466 used during compilation of a C function. */
6468 void
6469 c_push_function_context (struct function *f)
6471 struct language_function *p;
6472 p = GGC_NEW (struct language_function);
6473 f->language = p;
6475 p->base.x_stmt_tree = c_stmt_tree;
6476 p->x_break_label = c_break_label;
6477 p->x_cont_label = c_cont_label;
6478 p->x_switch_stack = c_switch_stack;
6479 p->arg_info = current_function_arg_info;
6480 p->returns_value = current_function_returns_value;
6481 p->returns_null = current_function_returns_null;
6482 p->returns_abnormally = current_function_returns_abnormally;
6483 p->warn_about_return_type = warn_about_return_type;
6484 p->extern_inline = current_extern_inline;
6487 /* Restore the variables used during compilation of a C function. */
6489 void
6490 c_pop_function_context (struct function *f)
6492 struct language_function *p = f->language;
6494 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6495 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6497 /* Stop pointing to the local nodes about to be freed. */
6498 /* But DECL_INITIAL must remain nonzero so we know this
6499 was an actual function definition. */
6500 DECL_INITIAL (current_function_decl) = error_mark_node;
6501 DECL_ARGUMENTS (current_function_decl) = 0;
6504 c_stmt_tree = p->base.x_stmt_tree;
6505 c_break_label = p->x_break_label;
6506 c_cont_label = p->x_cont_label;
6507 c_switch_stack = p->x_switch_stack;
6508 current_function_arg_info = p->arg_info;
6509 current_function_returns_value = p->returns_value;
6510 current_function_returns_null = p->returns_null;
6511 current_function_returns_abnormally = p->returns_abnormally;
6512 warn_about_return_type = p->warn_about_return_type;
6513 current_extern_inline = p->extern_inline;
6515 f->language = NULL;
6518 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6520 void
6521 c_dup_lang_specific_decl (tree decl)
6523 struct lang_decl *ld;
6525 if (!DECL_LANG_SPECIFIC (decl))
6526 return;
6528 ld = GGC_NEW (struct lang_decl);
6529 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6530 DECL_LANG_SPECIFIC (decl) = ld;
6533 /* The functions below are required for functionality of doing
6534 function at once processing in the C front end. Currently these
6535 functions are not called from anywhere in the C front end, but as
6536 these changes continue, that will change. */
6538 /* Returns nonzero if the current statement is a full expression,
6539 i.e. temporaries created during that statement should be destroyed
6540 at the end of the statement. */
6543 stmts_are_full_exprs_p (void)
6545 return 0;
6548 /* Returns the stmt_tree (if any) to which statements are currently
6549 being added. If there is no active statement-tree, NULL is
6550 returned. */
6552 stmt_tree
6553 current_stmt_tree (void)
6555 return &c_stmt_tree;
6558 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6559 C. */
6562 anon_aggr_type_p (tree ARG_UNUSED (node))
6564 return 0;
6567 /* Return the global value of T as a symbol. */
6569 tree
6570 identifier_global_value (tree t)
6572 struct c_binding *b;
6574 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6575 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6576 return b->decl;
6578 return 0;
6581 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6582 otherwise the name is found in ridpointers from RID_INDEX. */
6584 void
6585 record_builtin_type (enum rid rid_index, const char *name, tree type)
6587 tree id, decl;
6588 if (name == 0)
6589 id = ridpointers[(int) rid_index];
6590 else
6591 id = get_identifier (name);
6592 decl = build_decl (TYPE_DECL, id, type);
6593 pushdecl (decl);
6594 if (debug_hooks->type_decl)
6595 debug_hooks->type_decl (decl, false);
6598 /* Build the void_list_node (void_type_node having been created). */
6599 tree
6600 build_void_list_node (void)
6602 tree t = build_tree_list (NULL_TREE, void_type_node);
6603 return t;
6606 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6608 struct c_parm *
6609 build_c_parm (struct c_declspecs *specs, tree attrs,
6610 struct c_declarator *declarator)
6612 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6613 ret->specs = specs;
6614 ret->attrs = attrs;
6615 ret->declarator = declarator;
6616 return ret;
6619 /* Return a declarator with nested attributes. TARGET is the inner
6620 declarator to which these attributes apply. ATTRS are the
6621 attributes. */
6623 struct c_declarator *
6624 build_attrs_declarator (tree attrs, struct c_declarator *target)
6626 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6627 ret->kind = cdk_attrs;
6628 ret->declarator = target;
6629 ret->u.attrs = attrs;
6630 return ret;
6633 /* Return a declarator for a function with arguments specified by ARGS
6634 and return type specified by TARGET. */
6636 struct c_declarator *
6637 build_function_declarator (struct c_arg_info *args,
6638 struct c_declarator *target)
6640 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6641 ret->kind = cdk_function;
6642 ret->declarator = target;
6643 ret->u.arg_info = args;
6644 return ret;
6647 /* Return a declarator for the identifier IDENT (which may be
6648 NULL_TREE for an abstract declarator). */
6650 struct c_declarator *
6651 build_id_declarator (tree ident)
6653 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6654 ret->kind = cdk_id;
6655 ret->declarator = 0;
6656 ret->u.id = ident;
6657 /* Default value - may get reset to a more precise location. */
6658 ret->id_loc = input_location;
6659 return ret;
6662 /* Return something to represent absolute declarators containing a *.
6663 TARGET is the absolute declarator that the * contains.
6664 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6665 to apply to the pointer type. */
6667 struct c_declarator *
6668 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6669 struct c_declarator *target)
6671 tree attrs;
6672 int quals = 0;
6673 struct c_declarator *itarget = target;
6674 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6675 if (type_quals_attrs)
6677 attrs = type_quals_attrs->attrs;
6678 quals = quals_from_declspecs (type_quals_attrs);
6679 if (attrs != NULL_TREE)
6680 itarget = build_attrs_declarator (attrs, target);
6682 ret->kind = cdk_pointer;
6683 ret->declarator = itarget;
6684 ret->u.pointer_quals = quals;
6685 return ret;
6688 /* Return a pointer to a structure for an empty list of declaration
6689 specifiers. */
6691 struct c_declspecs *
6692 build_null_declspecs (void)
6694 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6695 ret->type = 0;
6696 ret->decl_attr = 0;
6697 ret->attrs = 0;
6698 ret->typespec_word = cts_none;
6699 ret->storage_class = csc_none;
6700 ret->declspecs_seen_p = false;
6701 ret->type_seen_p = false;
6702 ret->non_sc_seen_p = false;
6703 ret->typedef_p = false;
6704 ret->tag_defined_p = false;
6705 ret->explicit_signed_p = false;
6706 ret->deprecated_p = false;
6707 ret->default_int_p = false;
6708 ret->long_p = false;
6709 ret->long_long_p = false;
6710 ret->short_p = false;
6711 ret->signed_p = false;
6712 ret->unsigned_p = false;
6713 ret->complex_p = false;
6714 ret->inline_p = false;
6715 ret->thread_p = false;
6716 ret->const_p = false;
6717 ret->volatile_p = false;
6718 ret->restrict_p = false;
6719 return ret;
6722 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6723 returning SPECS. */
6725 struct c_declspecs *
6726 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6728 enum rid i;
6729 bool dupe = false;
6730 specs->non_sc_seen_p = true;
6731 specs->declspecs_seen_p = true;
6732 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6733 && C_IS_RESERVED_WORD (qual));
6734 i = C_RID_CODE (qual);
6735 switch (i)
6737 case RID_CONST:
6738 dupe = specs->const_p;
6739 specs->const_p = true;
6740 break;
6741 case RID_VOLATILE:
6742 dupe = specs->volatile_p;
6743 specs->volatile_p = true;
6744 break;
6745 case RID_RESTRICT:
6746 dupe = specs->restrict_p;
6747 specs->restrict_p = true;
6748 break;
6749 default:
6750 gcc_unreachable ();
6752 if (dupe && pedantic && !flag_isoc99)
6753 pedwarn ("duplicate %qE", qual);
6754 return specs;
6757 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6758 returning SPECS. */
6760 struct c_declspecs *
6761 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6763 tree type = spec.spec;
6764 specs->non_sc_seen_p = true;
6765 specs->declspecs_seen_p = true;
6766 specs->type_seen_p = true;
6767 if (TREE_DEPRECATED (type))
6768 specs->deprecated_p = true;
6770 /* Handle type specifier keywords. */
6771 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6773 enum rid i = C_RID_CODE (type);
6774 if (specs->type)
6776 error ("two or more data types in declaration specifiers");
6777 return specs;
6779 if ((int) i <= (int) RID_LAST_MODIFIER)
6781 /* "long", "short", "signed", "unsigned" or "_Complex". */
6782 bool dupe = false;
6783 switch (i)
6785 case RID_LONG:
6786 if (specs->long_long_p)
6788 error ("%<long long long%> is too long for GCC");
6789 break;
6791 if (specs->long_p)
6793 if (specs->typespec_word == cts_double)
6795 error ("both %<long long%> and %<double%> in "
6796 "declaration specifiers");
6797 break;
6799 if (pedantic && !flag_isoc99 && !in_system_header
6800 && warn_long_long)
6801 pedwarn ("ISO C90 does not support %<long long%>");
6802 specs->long_long_p = 1;
6803 break;
6805 if (specs->short_p)
6806 error ("both %<long%> and %<short%> in "
6807 "declaration specifiers");
6808 else if (specs->typespec_word == cts_void)
6809 error ("both %<long%> and %<void%> in "
6810 "declaration specifiers");
6811 else if (specs->typespec_word == cts_bool)
6812 error ("both %<long%> and %<_Bool%> in "
6813 "declaration specifiers");
6814 else if (specs->typespec_word == cts_char)
6815 error ("both %<long%> and %<char%> in "
6816 "declaration specifiers");
6817 else if (specs->typespec_word == cts_float)
6818 error ("both %<long%> and %<float%> in "
6819 "declaration specifiers");
6820 else
6821 specs->long_p = true;
6822 break;
6823 case RID_SHORT:
6824 dupe = specs->short_p;
6825 if (specs->long_p)
6826 error ("both %<long%> and %<short%> in "
6827 "declaration specifiers");
6828 else if (specs->typespec_word == cts_void)
6829 error ("both %<short%> and %<void%> in "
6830 "declaration specifiers");
6831 else if (specs->typespec_word == cts_bool)
6832 error ("both %<short%> and %<_Bool%> in "
6833 "declaration specifiers");
6834 else if (specs->typespec_word == cts_char)
6835 error ("both %<short%> and %<char%> in "
6836 "declaration specifiers");
6837 else if (specs->typespec_word == cts_float)
6838 error ("both %<short%> and %<float%> in "
6839 "declaration specifiers");
6840 else if (specs->typespec_word == cts_double)
6841 error ("both %<short%> and %<double%> in "
6842 "declaration specifiers");
6843 else
6844 specs->short_p = true;
6845 break;
6846 case RID_SIGNED:
6847 dupe = specs->signed_p;
6848 if (specs->unsigned_p)
6849 error ("both %<signed%> and %<unsigned%> in "
6850 "declaration specifiers");
6851 else if (specs->typespec_word == cts_void)
6852 error ("both %<signed%> and %<void%> in "
6853 "declaration specifiers");
6854 else if (specs->typespec_word == cts_bool)
6855 error ("both %<signed%> and %<_Bool%> in "
6856 "declaration specifiers");
6857 else if (specs->typespec_word == cts_float)
6858 error ("both %<signed%> and %<float%> in "
6859 "declaration specifiers");
6860 else if (specs->typespec_word == cts_double)
6861 error ("both %<signed%> and %<double%> in "
6862 "declaration specifiers");
6863 else
6864 specs->signed_p = true;
6865 break;
6866 case RID_UNSIGNED:
6867 dupe = specs->unsigned_p;
6868 if (specs->signed_p)
6869 error ("both %<signed%> and %<unsigned%> in "
6870 "declaration specifiers");
6871 else if (specs->typespec_word == cts_void)
6872 error ("both %<unsigned%> and %<void%> in "
6873 "declaration specifiers");
6874 else if (specs->typespec_word == cts_bool)
6875 error ("both %<unsigned%> and %<_Bool%> in "
6876 "declaration specifiers");
6877 else if (specs->typespec_word == cts_float)
6878 error ("both %<unsigned%> and %<float%> in "
6879 "declaration specifiers");
6880 else if (specs->typespec_word == cts_double)
6881 error ("both %<unsigned%> and %<double%> in "
6882 "declaration specifiers");
6883 else
6884 specs->unsigned_p = true;
6885 break;
6886 case RID_COMPLEX:
6887 dupe = specs->complex_p;
6888 if (pedantic && !flag_isoc99 && !in_system_header)
6889 pedwarn ("ISO C90 does not support complex types");
6890 if (specs->typespec_word == cts_void)
6891 error ("both %<complex%> and %<void%> in "
6892 "declaration specifiers");
6893 else if (specs->typespec_word == cts_bool)
6894 error ("both %<complex%> and %<_Bool%> in "
6895 "declaration specifiers");
6896 else
6897 specs->complex_p = true;
6898 break;
6899 default:
6900 gcc_unreachable ();
6903 if (dupe)
6904 error ("duplicate %qE", type);
6906 return specs;
6908 else
6910 /* "void", "_Bool", "char", "int", "float" or "double". */
6911 if (specs->typespec_word != cts_none)
6913 error ("two or more data types in declaration specifiers");
6914 return specs;
6916 switch (i)
6918 case RID_VOID:
6919 if (specs->long_p)
6920 error ("both %<long%> and %<void%> in "
6921 "declaration specifiers");
6922 else if (specs->short_p)
6923 error ("both %<short%> and %<void%> in "
6924 "declaration specifiers");
6925 else if (specs->signed_p)
6926 error ("both %<signed%> and %<void%> in "
6927 "declaration specifiers");
6928 else if (specs->unsigned_p)
6929 error ("both %<unsigned%> and %<void%> in "
6930 "declaration specifiers");
6931 else if (specs->complex_p)
6932 error ("both %<complex%> and %<void%> in "
6933 "declaration specifiers");
6934 else
6935 specs->typespec_word = cts_void;
6936 return specs;
6937 case RID_BOOL:
6938 if (specs->long_p)
6939 error ("both %<long%> and %<_Bool%> in "
6940 "declaration specifiers");
6941 else if (specs->short_p)
6942 error ("both %<short%> and %<_Bool%> in "
6943 "declaration specifiers");
6944 else if (specs->signed_p)
6945 error ("both %<signed%> and %<_Bool%> in "
6946 "declaration specifiers");
6947 else if (specs->unsigned_p)
6948 error ("both %<unsigned%> and %<_Bool%> in "
6949 "declaration specifiers");
6950 else if (specs->complex_p)
6951 error ("both %<complex%> and %<_Bool%> in "
6952 "declaration specifiers");
6953 else
6954 specs->typespec_word = cts_bool;
6955 return specs;
6956 case RID_CHAR:
6957 if (specs->long_p)
6958 error ("both %<long%> and %<char%> in "
6959 "declaration specifiers");
6960 else if (specs->short_p)
6961 error ("both %<short%> and %<char%> in "
6962 "declaration specifiers");
6963 else
6964 specs->typespec_word = cts_char;
6965 return specs;
6966 case RID_INT:
6967 specs->typespec_word = cts_int;
6968 return specs;
6969 case RID_FLOAT:
6970 if (specs->long_p)
6971 error ("both %<long%> and %<float%> in "
6972 "declaration specifiers");
6973 else if (specs->short_p)
6974 error ("both %<short%> and %<float%> in "
6975 "declaration specifiers");
6976 else if (specs->signed_p)
6977 error ("both %<signed%> and %<float%> in "
6978 "declaration specifiers");
6979 else if (specs->unsigned_p)
6980 error ("both %<unsigned%> and %<float%> in "
6981 "declaration specifiers");
6982 else
6983 specs->typespec_word = cts_float;
6984 return specs;
6985 case RID_DOUBLE:
6986 if (specs->long_long_p)
6987 error ("both %<long long%> and %<double%> in "
6988 "declaration specifiers");
6989 else if (specs->short_p)
6990 error ("both %<short%> and %<double%> in "
6991 "declaration specifiers");
6992 else if (specs->signed_p)
6993 error ("both %<signed%> and %<double%> in "
6994 "declaration specifiers");
6995 else if (specs->unsigned_p)
6996 error ("both %<unsigned%> and %<double%> in "
6997 "declaration specifiers");
6998 else
6999 specs->typespec_word = cts_double;
7000 return specs;
7001 default:
7002 /* ObjC reserved word "id", handled below. */
7003 break;
7008 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7009 form of ObjC type, cases such as "int" and "long" being handled
7010 above), a TYPE (struct, union, enum and typeof specifiers) or an
7011 ERROR_MARK. In none of these cases may there have previously
7012 been any type specifiers. */
7013 if (specs->type || specs->typespec_word != cts_none
7014 || specs->long_p || specs->short_p || specs->signed_p
7015 || specs->unsigned_p || specs->complex_p)
7016 error ("two or more data types in declaration specifiers");
7017 else if (TREE_CODE (type) == TYPE_DECL)
7019 if (TREE_TYPE (type) == error_mark_node)
7020 ; /* Allow the type to default to int to avoid cascading errors. */
7021 else
7023 specs->type = TREE_TYPE (type);
7024 specs->decl_attr = DECL_ATTRIBUTES (type);
7025 specs->typedef_p = true;
7026 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7029 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7031 tree t = lookup_name (type);
7032 if (!t || TREE_CODE (t) != TYPE_DECL)
7033 error ("%qE fails to be a typedef or built in type", type);
7034 else if (TREE_TYPE (t) == error_mark_node)
7036 else
7037 specs->type = TREE_TYPE (t);
7039 else if (TREE_CODE (type) != ERROR_MARK)
7041 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7042 specs->tag_defined_p = true;
7043 if (spec.kind == ctsk_typeof)
7044 specs->typedef_p = true;
7045 specs->type = type;
7048 return specs;
7051 /* Add the storage class specifier or function specifier SCSPEC to the
7052 declaration specifiers SPECS, returning SPECS. */
7054 struct c_declspecs *
7055 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7057 enum rid i;
7058 enum c_storage_class n = csc_none;
7059 bool dupe = false;
7060 specs->declspecs_seen_p = true;
7061 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7062 && C_IS_RESERVED_WORD (scspec));
7063 i = C_RID_CODE (scspec);
7064 if (extra_warnings && specs->non_sc_seen_p)
7065 warning ("%qE is not at beginning of declaration", scspec);
7066 switch (i)
7068 case RID_INLINE:
7069 /* C99 permits duplicate inline. Although of doubtful utility,
7070 it seems simplest to permit it in gnu89 mode as well, as
7071 there is also little utility in maintaining this as a
7072 difference between gnu89 and C99 inline. */
7073 dupe = false;
7074 specs->inline_p = true;
7075 break;
7076 case RID_THREAD:
7077 dupe = specs->thread_p;
7078 if (specs->storage_class == csc_auto)
7079 error ("%<__thread%> used with %<auto%>");
7080 else if (specs->storage_class == csc_register)
7081 error ("%<__thread%> used with %<register%>");
7082 else if (specs->storage_class == csc_typedef)
7083 error ("%<__thread%> used with %<typedef%>");
7084 else
7085 specs->thread_p = true;
7086 break;
7087 case RID_AUTO:
7088 n = csc_auto;
7089 break;
7090 case RID_EXTERN:
7091 n = csc_extern;
7092 /* Diagnose "__thread extern". */
7093 if (specs->thread_p)
7094 error ("%<__thread%> before %<extern%>");
7095 break;
7096 case RID_REGISTER:
7097 n = csc_register;
7098 break;
7099 case RID_STATIC:
7100 n = csc_static;
7101 /* Diagnose "__thread static". */
7102 if (specs->thread_p)
7103 error ("%<__thread%> before %<static%>");
7104 break;
7105 case RID_TYPEDEF:
7106 n = csc_typedef;
7107 break;
7108 default:
7109 gcc_unreachable ();
7111 if (n != csc_none && n == specs->storage_class)
7112 dupe = true;
7113 if (dupe)
7114 error ("duplicate %qE", scspec);
7115 if (n != csc_none)
7117 if (specs->storage_class != csc_none && n != specs->storage_class)
7119 error ("multiple storage classes in declaration specifiers");
7121 else
7123 specs->storage_class = n;
7124 if (n != csc_extern && n != csc_static && specs->thread_p)
7126 error ("%<__thread%> used with %qE", scspec);
7127 specs->thread_p = false;
7131 return specs;
7134 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7135 returning SPECS. */
7137 struct c_declspecs *
7138 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7140 specs->attrs = chainon (attrs, specs->attrs);
7141 specs->declspecs_seen_p = true;
7142 return specs;
7145 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7146 specifiers with any other type specifier to determine the resulting
7147 type. This is where ISO C checks on complex types are made, since
7148 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7149 double". */
7151 struct c_declspecs *
7152 finish_declspecs (struct c_declspecs *specs)
7154 /* If a type was specified as a whole, we have no modifiers and are
7155 done. */
7156 if (specs->type != NULL_TREE)
7158 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7159 && !specs->signed_p && !specs->unsigned_p
7160 && !specs->complex_p);
7161 return specs;
7164 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7165 has been specified, treat it as "int" unless "_Complex" is
7166 present and there are no other specifiers. If we just have
7167 "_Complex", it is equivalent to "_Complex double", but e.g.
7168 "_Complex short" is equivalent to "_Complex short int". */
7169 if (specs->typespec_word == cts_none)
7171 if (specs->long_p || specs->short_p
7172 || specs->signed_p || specs->unsigned_p)
7174 specs->typespec_word = cts_int;
7176 else if (specs->complex_p)
7178 specs->typespec_word = cts_double;
7179 if (pedantic)
7180 pedwarn ("ISO C does not support plain %<complex%> meaning "
7181 "%<double complex%>");
7183 else
7185 specs->typespec_word = cts_int;
7186 specs->default_int_p = true;
7187 /* We don't diagnose this here because grokdeclarator will
7188 give more specific diagnostics according to whether it is
7189 a function definition. */
7193 /* If "signed" was specified, record this to distinguish "int" and
7194 "signed int" in the case of a bit-field with
7195 -funsigned-bitfields. */
7196 specs->explicit_signed_p = specs->signed_p;
7198 /* Now compute the actual type. */
7199 switch (specs->typespec_word)
7201 case cts_void:
7202 gcc_assert (!specs->long_p && !specs->short_p
7203 && !specs->signed_p && !specs->unsigned_p
7204 && !specs->complex_p);
7205 specs->type = void_type_node;
7206 break;
7207 case cts_bool:
7208 gcc_assert (!specs->long_p && !specs->short_p
7209 && !specs->signed_p && !specs->unsigned_p
7210 && !specs->complex_p);
7211 specs->type = boolean_type_node;
7212 break;
7213 case cts_char:
7214 gcc_assert (!specs->long_p && !specs->short_p);
7215 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7216 if (specs->signed_p)
7217 specs->type = signed_char_type_node;
7218 else if (specs->unsigned_p)
7219 specs->type = unsigned_char_type_node;
7220 else
7221 specs->type = char_type_node;
7222 if (specs->complex_p)
7224 if (pedantic)
7225 pedwarn ("ISO C does not support complex integer types");
7226 specs->type = build_complex_type (specs->type);
7228 break;
7229 case cts_int:
7230 gcc_assert (!(specs->long_p && specs->short_p));
7231 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7232 if (specs->long_long_p)
7233 specs->type = (specs->unsigned_p
7234 ? long_long_unsigned_type_node
7235 : long_long_integer_type_node);
7236 else if (specs->long_p)
7237 specs->type = (specs->unsigned_p
7238 ? long_unsigned_type_node
7239 : long_integer_type_node);
7240 else if (specs->short_p)
7241 specs->type = (specs->unsigned_p
7242 ? short_unsigned_type_node
7243 : short_integer_type_node);
7244 else
7245 specs->type = (specs->unsigned_p
7246 ? unsigned_type_node
7247 : integer_type_node);
7248 if (specs->complex_p)
7250 if (pedantic)
7251 pedwarn ("ISO C does not support complex integer types");
7252 specs->type = build_complex_type (specs->type);
7254 break;
7255 case cts_float:
7256 gcc_assert (!specs->long_p && !specs->short_p
7257 && !specs->signed_p && !specs->unsigned_p);
7258 specs->type = (specs->complex_p
7259 ? complex_float_type_node
7260 : float_type_node);
7261 break;
7262 case cts_double:
7263 gcc_assert (!specs->long_long_p && !specs->short_p
7264 && !specs->signed_p && !specs->unsigned_p);
7265 if (specs->long_p)
7267 specs->type = (specs->complex_p
7268 ? complex_long_double_type_node
7269 : long_double_type_node);
7271 else
7273 specs->type = (specs->complex_p
7274 ? complex_double_type_node
7275 : double_type_node);
7277 break;
7278 default:
7279 gcc_unreachable ();
7282 return specs;
7285 /* Synthesize a function which calls all the global ctors or global
7286 dtors in this file. This is only used for targets which do not
7287 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7288 static void
7289 build_cdtor (int method_type, tree cdtors)
7291 tree body = 0;
7293 if (!cdtors)
7294 return;
7296 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7297 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7298 &body);
7300 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7303 /* Perform final processing on one file scope's declarations (or the
7304 external scope's declarations), GLOBALS. */
7305 static void
7306 c_write_global_declarations_1 (tree globals)
7308 size_t len = list_length (globals);
7309 tree *vec = XNEWVEC (tree, len);
7310 size_t i;
7311 tree decl;
7313 /* Process the decls in the order they were written. */
7314 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7316 vec[i] = decl;
7317 /* Check for used but undefined static functions using the C
7318 standard's definition of "used", and set TREE_NO_WARNING so
7319 that check_global_declarations doesn't repeat the check. */
7320 if (TREE_CODE (decl) == FUNCTION_DECL
7321 && DECL_INITIAL (decl) == 0
7322 && DECL_EXTERNAL (decl)
7323 && !TREE_PUBLIC (decl)
7324 && C_DECL_USED (decl))
7326 pedwarn ("%J%qF used but never defined", decl, decl);
7327 TREE_NO_WARNING (decl) = 1;
7331 wrapup_global_declarations (vec, len);
7332 check_global_declarations (vec, len);
7334 free (vec);
7337 void
7338 c_write_global_declarations (void)
7340 tree ext_block, t;
7342 /* We don't want to do this if generating a PCH. */
7343 if (pch_file)
7344 return;
7346 /* Don't waste time on further processing if -fsyntax-only or we've
7347 encountered errors. */
7348 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7349 return;
7351 /* Close the external scope. */
7352 ext_block = pop_scope ();
7353 external_scope = 0;
7354 gcc_assert (!current_scope);
7356 /* Process all file scopes in this compilation, and the external_scope,
7357 through wrapup_global_declarations and check_global_declarations. */
7358 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7359 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7360 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7362 /* Generate functions to call static constructors and destructors
7363 for targets that do not support .ctors/.dtors sections. These
7364 functions have magic names which are detected by collect2. */
7365 build_cdtor ('I', static_ctors); static_ctors = 0;
7366 build_cdtor ('D', static_dtors); static_dtors = 0;
7368 /* We're done parsing; proceed to optimize and emit assembly.
7369 FIXME: shouldn't be the front end's responsibility to call this. */
7370 cgraph_optimize ();
7373 #include "gt-c-decl.h"