Daily bump.
[official-gcc.git] / gcc / c-decl.c
blobfb03afb175b99992ec9f3570465f57fcbf5cb363
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 (type, NULL_TREE, 1);
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 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1047 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1049 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1050 newargs = TYPE_ARG_TYPES (newtype);
1051 i = 1;
1053 for (;;)
1055 tree oldargtype = TREE_VALUE (oldargs);
1056 tree newargtype = TREE_VALUE (newargs);
1058 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1059 break;
1061 /* Reaching the end of just one list means the two decls don't
1062 agree on the number of arguments. */
1063 if (END_OF_ARGLIST (oldargtype))
1065 error ("%Jprototype for %qD declares more arguments "
1066 "than previous old-style definition", newdecl, newdecl);
1067 return false;
1069 else if (END_OF_ARGLIST (newargtype))
1071 error ("%Jprototype for %qD declares fewer arguments "
1072 "than previous old-style definition", newdecl, newdecl);
1073 return false;
1076 /* Type for passing arg must be consistent with that declared
1077 for the arg. */
1078 else if (!comptypes (oldargtype, newargtype))
1080 error ("%Jprototype for %qD declares argument %d"
1081 " with incompatible type",
1082 newdecl, newdecl, i);
1083 return false;
1086 oldargs = TREE_CHAIN (oldargs);
1087 newargs = TREE_CHAIN (newargs);
1088 i++;
1091 /* If we get here, no errors were found, but do issue a warning
1092 for this poor-style construct. */
1093 warning ("%Jprototype for %qD follows non-prototype definition",
1094 newdecl, newdecl);
1095 return true;
1096 #undef END_OF_ARGLIST
1099 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1100 first in a pair of mismatched declarations, using the diagnostic
1101 function DIAG. */
1102 static void
1103 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1105 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1107 else if (DECL_INITIAL (decl))
1108 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1109 else if (C_DECL_IMPLICIT (decl))
1110 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1111 else
1112 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1115 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1116 Returns true if the caller should proceed to merge the two, false
1117 if OLDDECL should simply be discarded. As a side effect, issues
1118 all necessary diagnostics for invalid or poor-style combinations.
1119 If it returns true, writes the types of NEWDECL and OLDDECL to
1120 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1121 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1123 static bool
1124 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1125 tree *newtypep, tree *oldtypep)
1127 tree newtype, oldtype;
1128 bool pedwarned = false;
1129 bool warned = false;
1130 bool retval = true;
1132 /* If we have error_mark_node for either decl or type, just discard
1133 the previous decl - we're in an error cascade already. */
1134 if (olddecl == error_mark_node || newdecl == error_mark_node)
1135 return false;
1136 *oldtypep = oldtype = TREE_TYPE (olddecl);
1137 *newtypep = newtype = TREE_TYPE (newdecl);
1138 if (oldtype == error_mark_node || newtype == error_mark_node)
1139 return false;
1141 /* Two different categories of symbol altogether. This is an error
1142 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1143 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1145 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1146 && DECL_BUILT_IN (olddecl)
1147 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1149 error ("%J%qD redeclared as different kind of symbol",
1150 newdecl, newdecl);
1151 locate_old_decl (olddecl, error);
1153 else if (TREE_PUBLIC (newdecl))
1154 warning ("%Jbuilt-in function %qD declared as non-function",
1155 newdecl, newdecl);
1156 else if (warn_shadow)
1157 warning ("%Jdeclaration of %qD shadows a built-in function",
1158 newdecl, newdecl);
1159 return false;
1162 /* Enumerators have no linkage, so may only be declared once in a
1163 given scope. */
1164 if (TREE_CODE (olddecl) == CONST_DECL)
1166 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1167 locate_old_decl (olddecl, error);
1168 return false;
1171 if (!comptypes (oldtype, newtype))
1173 if (TREE_CODE (olddecl) == FUNCTION_DECL
1174 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1176 /* Accept harmless mismatch in function types.
1177 This is for the ffs and fprintf builtins. */
1178 tree trytype = match_builtin_function_types (newtype, oldtype);
1180 if (trytype && comptypes (newtype, trytype))
1181 *oldtypep = oldtype = trytype;
1182 else
1184 /* If types don't match for a built-in, throw away the
1185 built-in. No point in calling locate_old_decl here, it
1186 won't print anything. */
1187 warning ("%Jconflicting types for built-in function %qD",
1188 newdecl, newdecl);
1189 return false;
1192 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1193 && DECL_IS_BUILTIN (olddecl))
1195 /* A conflicting function declaration for a predeclared
1196 function that isn't actually built in. Objective C uses
1197 these. The new declaration silently overrides everything
1198 but the volatility (i.e. noreturn) indication. See also
1199 below. FIXME: Make Objective C use normal builtins. */
1200 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1201 return false;
1203 /* Permit void foo (...) to match int foo (...) if the latter is
1204 the definition and implicit int was used. See
1205 c-torture/compile/920625-2.c. */
1206 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1207 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1208 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1209 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1211 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1212 /* Make sure we keep void as the return type. */
1213 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1214 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1215 pedwarned = true;
1217 /* Permit void foo (...) to match an earlier call to foo (...) with
1218 no declared type (thus, implicitly int). */
1219 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1220 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1221 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1222 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1224 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1225 /* Make sure we keep void as the return type. */
1226 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1227 pedwarned = true;
1229 else
1231 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1232 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1233 else
1234 error ("%Jconflicting types for %qD", newdecl, newdecl);
1235 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1236 locate_old_decl (olddecl, error);
1237 return false;
1241 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1242 but silently ignore the redeclaration if either is in a system
1243 header. (Conflicting redeclarations were handled above.) */
1244 if (TREE_CODE (newdecl) == TYPE_DECL)
1246 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1247 return true; /* Allow OLDDECL to continue in use. */
1249 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1250 locate_old_decl (olddecl, error);
1251 return false;
1254 /* Function declarations can either be 'static' or 'extern' (no
1255 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1256 can never conflict with each other on account of linkage (6.2.2p4).
1257 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1258 two definitions if one is 'extern inline' and one is not. The non-
1259 extern-inline definition supersedes the extern-inline definition. */
1260 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1262 /* If you declare a built-in function name as static, or
1263 define the built-in with an old-style definition (so we
1264 can't validate the argument list) the built-in definition is
1265 overridden, but optionally warn this was a bad choice of name. */
1266 if (DECL_BUILT_IN (olddecl)
1267 && !C_DECL_DECLARED_BUILTIN (olddecl)
1268 && (!TREE_PUBLIC (newdecl)
1269 || (DECL_INITIAL (newdecl)
1270 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1272 if (warn_shadow)
1273 warning ("%Jdeclaration of %qD shadows a built-in function",
1274 newdecl, newdecl);
1275 /* Discard the old built-in function. */
1276 return false;
1279 if (DECL_INITIAL (newdecl))
1281 if (DECL_INITIAL (olddecl))
1283 /* If both decls have extern inline and are in the same TU,
1284 reject the new decl. */
1285 if (DECL_DECLARED_INLINE_P (olddecl)
1286 && DECL_EXTERNAL (olddecl)
1287 && DECL_DECLARED_INLINE_P (newdecl)
1288 && DECL_EXTERNAL (newdecl)
1289 && same_translation_unit_p (newdecl, olddecl))
1291 error ("%Jredefinition of %qD", newdecl, newdecl);
1292 locate_old_decl (olddecl, error);
1293 return false;
1295 /* If both decls have not extern inline, reject the new decl. */
1296 if (!DECL_DECLARED_INLINE_P (olddecl)
1297 && !DECL_EXTERNAL (olddecl)
1298 && !DECL_DECLARED_INLINE_P (newdecl)
1299 && !DECL_EXTERNAL (newdecl))
1301 error ("%Jredefinition of %qD", newdecl, newdecl);
1302 locate_old_decl (olddecl, error);
1303 return false;
1305 /* If the new decl is declared as extern inline, error if they are
1306 in the same TU, otherwise retain the old decl. */
1307 if (!DECL_DECLARED_INLINE_P (olddecl)
1308 && !DECL_EXTERNAL (olddecl)
1309 && DECL_DECLARED_INLINE_P (newdecl)
1310 && DECL_EXTERNAL (newdecl))
1312 if (same_translation_unit_p (newdecl, olddecl))
1314 error ("%Jredefinition of %qD", newdecl, newdecl);
1315 locate_old_decl (olddecl, error);
1316 return false;
1318 else
1319 retval = false;
1323 /* If we have a prototype after an old-style function definition,
1324 the argument types must be checked specially. */
1325 else if (DECL_INITIAL (olddecl)
1326 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1327 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1328 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1330 locate_old_decl (olddecl, error);
1331 return false;
1333 /* A non-static declaration (even an "extern") followed by a
1334 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1335 The same is true for a static forward declaration at block
1336 scope followed by a non-static declaration/definition at file
1337 scope. Static followed by non-static at the same scope is
1338 not undefined behavior, and is the most convenient way to get
1339 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1340 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1341 we do diagnose it if -Wtraditional. */
1342 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1344 /* Two exceptions to the rule. If olddecl is an extern
1345 inline, or a predeclared function that isn't actually
1346 built in, newdecl silently overrides olddecl. The latter
1347 occur only in Objective C; see also above. (FIXME: Make
1348 Objective C use normal builtins.) */
1349 if (!DECL_IS_BUILTIN (olddecl)
1350 && !(DECL_EXTERNAL (olddecl)
1351 && DECL_DECLARED_INLINE_P (olddecl)))
1353 error ("%Jstatic declaration of %qD follows "
1354 "non-static declaration", newdecl, newdecl);
1355 locate_old_decl (olddecl, error);
1357 return false;
1359 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1361 if (DECL_CONTEXT (olddecl))
1363 error ("%Jnon-static declaration of %qD follows "
1364 "static declaration", newdecl, newdecl);
1365 locate_old_decl (olddecl, error);
1366 return false;
1368 else if (warn_traditional)
1370 warning ("%Jnon-static declaration of %qD follows "
1371 "static declaration", newdecl, newdecl);
1372 warned = true;
1376 else if (TREE_CODE (newdecl) == VAR_DECL)
1378 /* Only variables can be thread-local, and all declarations must
1379 agree on this property. */
1380 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1382 if (DECL_THREAD_LOCAL (newdecl))
1383 error ("%Jthread-local declaration of %qD follows "
1384 "non-thread-local declaration", newdecl, newdecl);
1385 else
1386 error ("%Jnon-thread-local declaration of %qD follows "
1387 "thread-local declaration", newdecl, newdecl);
1389 locate_old_decl (olddecl, error);
1390 return false;
1393 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1394 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1396 error ("%Jredefinition of %qD", newdecl, newdecl);
1397 locate_old_decl (olddecl, error);
1398 return false;
1401 /* Objects declared at file scope: if the first declaration had
1402 external linkage (even if it was an external reference) the
1403 second must have external linkage as well, or the behavior is
1404 undefined. If the first declaration had internal linkage, then
1405 the second must too, or else be an external reference (in which
1406 case the composite declaration still has internal linkage).
1407 As for function declarations, we warn about the static-then-
1408 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1409 if (DECL_FILE_SCOPE_P (newdecl)
1410 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1412 if (DECL_EXTERNAL (newdecl))
1414 if (!DECL_FILE_SCOPE_P (olddecl))
1416 error ("%Jextern declaration of %qD follows "
1417 "declaration with no linkage", newdecl, newdecl);
1418 locate_old_decl (olddecl, error);
1419 return false;
1421 else if (warn_traditional)
1423 warning ("%Jnon-static declaration of %qD follows "
1424 "static declaration", newdecl, newdecl);
1425 warned = true;
1428 else
1430 if (TREE_PUBLIC (newdecl))
1431 error ("%Jnon-static declaration of %qD follows "
1432 "static declaration", newdecl, newdecl);
1433 else
1434 error ("%Jstatic declaration of %qD follows "
1435 "non-static declaration", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1438 return false;
1441 /* Two objects with the same name declared at the same block
1442 scope must both be external references (6.7p3). */
1443 else if (!DECL_FILE_SCOPE_P (newdecl))
1445 if (DECL_EXTERNAL (newdecl))
1447 /* Extern with initializer at block scope, which will
1448 already have received an error. */
1450 else if (DECL_EXTERNAL (olddecl))
1452 error ("%Jdeclaration of %qD with no linkage follows "
1453 "extern declaration", newdecl, newdecl);
1454 locate_old_decl (olddecl, error);
1456 else
1458 error ("%Jredeclaration of %qD with no linkage",
1459 newdecl, newdecl);
1460 locate_old_decl (olddecl, error);
1463 return false;
1467 /* warnings */
1468 /* All decls must agree on a visibility. */
1469 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1470 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1472 warning ("%Jredeclaration of %qD with different visibility "
1473 "(old visibility preserved)", newdecl, newdecl);
1474 warned = true;
1477 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1479 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1480 if (DECL_DECLARED_INLINE_P (newdecl)
1481 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1483 warning ("%Jinline declaration of %qD follows "
1484 "declaration with attribute noinline", newdecl, newdecl);
1485 warned = true;
1487 else if (DECL_DECLARED_INLINE_P (olddecl)
1488 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1490 warning ("%Jdeclaration of %qD with attribute noinline follows "
1491 "inline declaration ", newdecl, newdecl);
1492 warned = true;
1495 /* Inline declaration after use or definition.
1496 ??? Should we still warn about this now we have unit-at-a-time
1497 mode and can get it right?
1498 Definitely don't complain if the decls are in different translation
1499 units. */
1500 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1501 && same_translation_unit_p (olddecl, newdecl))
1503 if (TREE_USED (olddecl))
1505 warning ("%J%qD declared inline after being called",
1506 olddecl, olddecl);
1507 warned = true;
1509 else if (DECL_INITIAL (olddecl))
1511 warning ("%J%qD declared inline after its definition",
1512 olddecl, olddecl);
1513 warned = true;
1517 else /* PARM_DECL, VAR_DECL */
1519 /* Redeclaration of a parameter is a constraint violation (this is
1520 not explicitly stated, but follows from C99 6.7p3 [no more than
1521 one declaration of the same identifier with no linkage in the
1522 same scope, except type tags] and 6.2.2p6 [parameters have no
1523 linkage]). We must check for a forward parameter declaration,
1524 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1525 an extension, the mandatory diagnostic for which is handled by
1526 mark_forward_parm_decls. */
1528 if (TREE_CODE (newdecl) == PARM_DECL
1529 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1531 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1532 locate_old_decl (olddecl, error);
1533 return false;
1537 /* Optional warning for completely redundant decls. */
1538 if (!warned && !pedwarned
1539 && warn_redundant_decls
1540 /* Don't warn about a function declaration followed by a
1541 definition. */
1542 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1543 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1544 /* Don't warn about redundant redeclarations of builtins. */
1545 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1546 && !DECL_BUILT_IN (newdecl)
1547 && DECL_BUILT_IN (olddecl)
1548 && !C_DECL_DECLARED_BUILTIN (olddecl))
1549 /* Don't warn about an extern followed by a definition. */
1550 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1551 /* Don't warn about forward parameter decls. */
1552 && !(TREE_CODE (newdecl) == PARM_DECL
1553 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1555 warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
1556 warned = true;
1559 /* Report location of previous decl/defn in a consistent manner. */
1560 if (warned || pedwarned)
1561 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1563 return retval;
1566 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1567 consistent with OLDDECL, but carries new information. Merge the
1568 new information into OLDDECL. This function issues no
1569 diagnostics. */
1571 static void
1572 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1574 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1575 && DECL_INITIAL (newdecl) != 0);
1577 /* For real parm decl following a forward decl, rechain the old decl
1578 in its new location and clear TREE_ASM_WRITTEN (it's not a
1579 forward decl anymore). */
1580 if (TREE_CODE (newdecl) == PARM_DECL
1581 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1583 struct c_binding *b, **here;
1585 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1586 if ((*here)->decl == olddecl)
1587 goto found;
1588 gcc_unreachable ();
1590 found:
1591 b = *here;
1592 *here = b->prev;
1593 b->prev = current_scope->bindings;
1594 current_scope->bindings = b;
1596 TREE_ASM_WRITTEN (olddecl) = 0;
1599 DECL_ATTRIBUTES (newdecl)
1600 = targetm.merge_decl_attributes (olddecl, newdecl);
1602 /* Merge the data types specified in the two decls. */
1603 TREE_TYPE (newdecl)
1604 = TREE_TYPE (olddecl)
1605 = composite_type (newtype, oldtype);
1607 /* Lay the type out, unless already done. */
1608 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1610 if (TREE_TYPE (newdecl) != error_mark_node)
1611 layout_type (TREE_TYPE (newdecl));
1612 if (TREE_CODE (newdecl) != FUNCTION_DECL
1613 && TREE_CODE (newdecl) != TYPE_DECL
1614 && TREE_CODE (newdecl) != CONST_DECL)
1615 layout_decl (newdecl, 0);
1617 else
1619 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1620 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1621 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1622 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1623 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1624 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1626 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1627 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1631 /* Keep the old rtl since we can safely use it. */
1632 COPY_DECL_RTL (olddecl, newdecl);
1634 /* Merge the type qualifiers. */
1635 if (TREE_READONLY (newdecl))
1636 TREE_READONLY (olddecl) = 1;
1638 if (TREE_THIS_VOLATILE (newdecl))
1640 TREE_THIS_VOLATILE (olddecl) = 1;
1641 if (TREE_CODE (newdecl) == VAR_DECL)
1642 make_var_volatile (newdecl);
1645 /* Merge deprecatedness. */
1646 if (TREE_DEPRECATED (newdecl))
1647 TREE_DEPRECATED (olddecl) = 1;
1649 /* Keep source location of definition rather than declaration. */
1650 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1651 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1653 /* Merge the unused-warning information. */
1654 if (DECL_IN_SYSTEM_HEADER (olddecl))
1655 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1656 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1657 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1659 /* Merge the initialization information. */
1660 if (DECL_INITIAL (newdecl) == 0)
1661 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1663 /* Merge the section attribute.
1664 We want to issue an error if the sections conflict but that must be
1665 done later in decl_attributes since we are called before attributes
1666 are assigned. */
1667 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1668 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1670 /* Copy the assembler name.
1671 Currently, it can only be defined in the prototype. */
1672 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1674 /* Use visibility of whichever declaration had it specified */
1675 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1677 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1678 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1681 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1683 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1684 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1685 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1686 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1687 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1688 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1689 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1690 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1691 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (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 %qs referenced outside of any function",
2373 IDENTIFIER_POINTER (name));
2374 return 0;
2377 /* Use a label already defined or ref'd with this name, but not if
2378 it is inherited from a containing function and wasn't declared
2379 using __label__. */
2380 label = I_LABEL_DECL (name);
2381 if (label && (DECL_CONTEXT (label) == current_function_decl
2382 || C_DECLARED_LABEL_FLAG (label)))
2384 /* If the label has only been declared, update its apparent
2385 location to point here, for better diagnostics if it
2386 turns out not to have been defined. */
2387 if (!TREE_USED (label))
2388 DECL_SOURCE_LOCATION (label) = input_location;
2389 return label;
2392 /* No label binding for that identifier; make one. */
2393 label = make_label (name, input_location);
2395 /* Ordinary labels go in the current function scope. */
2396 bind (name, label, current_function_scope,
2397 /*invisible=*/false, /*nested=*/false);
2398 return label;
2401 /* Make a label named NAME in the current function, shadowing silently
2402 any that may be inherited from containing functions or containing
2403 scopes. This is called for __label__ declarations. */
2405 tree
2406 declare_label (tree name)
2408 struct c_binding *b = I_LABEL_BINDING (name);
2409 tree label;
2411 /* Check to make sure that the label hasn't already been declared
2412 at this scope */
2413 if (b && B_IN_CURRENT_SCOPE (b))
2415 error ("duplicate label declaration %qs", IDENTIFIER_POINTER (name));
2416 locate_old_decl (b->decl, error);
2418 /* Just use the previous declaration. */
2419 return b->decl;
2422 label = make_label (name, input_location);
2423 C_DECLARED_LABEL_FLAG (label) = 1;
2425 /* Declared labels go in the current scope. */
2426 bind (name, label, current_scope,
2427 /*invisible=*/false, /*nested=*/false);
2428 return label;
2431 /* Define a label, specifying the location in the source file.
2432 Return the LABEL_DECL node for the label, if the definition is valid.
2433 Otherwise return 0. */
2435 tree
2436 define_label (location_t location, tree name)
2438 /* Find any preexisting label with this name. It is an error
2439 if that label has already been defined in this function, or
2440 if there is a containing function with a declared label with
2441 the same name. */
2442 tree label = I_LABEL_DECL (name);
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 DECL_SOURCE_LOCATION (label) = location;
2461 else
2463 /* No label binding for that identifier; make one. */
2464 label = make_label (name, location);
2466 /* Ordinary labels go in the current function scope. */
2467 bind (name, label, current_function_scope,
2468 /*invisible=*/false, /*nested=*/false);
2471 if (warn_traditional && !in_system_header && lookup_name (name))
2472 warning ("%Htraditional C lacks a separate namespace for labels, "
2473 "identifier %qs conflicts", &location,
2474 IDENTIFIER_POINTER (name));
2476 /* Mark label as having been defined. */
2477 DECL_INITIAL (label) = error_mark_node;
2478 return label;
2481 /* Given NAME, an IDENTIFIER_NODE,
2482 return the structure (or union or enum) definition for that name.
2483 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2484 CODE says which kind of type the caller wants;
2485 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2486 If the wrong kind of type is found, an error is reported. */
2488 static tree
2489 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2491 struct c_binding *b = I_TAG_BINDING (name);
2492 int thislevel = 0;
2494 if (!b || !b->decl)
2495 return 0;
2497 /* We only care about whether it's in this level if
2498 thislevel_only was set or it might be a type clash. */
2499 if (thislevel_only || TREE_CODE (b->decl) != code)
2501 /* For our purposes, a tag in the external scope is the same as
2502 a tag in the file scope. (Primarily relevant to Objective-C
2503 and its builtin structure tags, which get pushed before the
2504 file scope is created.) */
2505 if (B_IN_CURRENT_SCOPE (b)
2506 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2507 thislevel = 1;
2510 if (thislevel_only && !thislevel)
2511 return 0;
2513 if (TREE_CODE (b->decl) != code)
2515 /* Definition isn't the kind we were looking for. */
2516 pending_invalid_xref = name;
2517 pending_invalid_xref_location = input_location;
2519 /* If in the same binding level as a declaration as a tag
2520 of a different type, this must not be allowed to
2521 shadow that tag, so give the error immediately.
2522 (For example, "struct foo; union foo;" is invalid.) */
2523 if (thislevel)
2524 pending_xref_error ();
2526 return b->decl;
2529 /* Print an error message now
2530 for a recent invalid struct, union or enum cross reference.
2531 We don't print them immediately because they are not invalid
2532 when used in the `struct foo;' construct for shadowing. */
2534 void
2535 pending_xref_error (void)
2537 if (pending_invalid_xref != 0)
2538 error ("%H%qs defined as wrong kind of tag",
2539 &pending_invalid_xref_location,
2540 IDENTIFIER_POINTER (pending_invalid_xref));
2541 pending_invalid_xref = 0;
2545 /* Look up NAME in the current scope and its superiors
2546 in the namespace of variables, functions and typedefs.
2547 Return a ..._DECL node of some kind representing its definition,
2548 or return 0 if it is undefined. */
2550 tree
2551 lookup_name (tree name)
2553 struct c_binding *b = I_SYMBOL_BINDING (name);
2554 if (b && !b->invisible)
2555 return b->decl;
2556 return 0;
2559 /* Similar to `lookup_name' but look only at the indicated scope. */
2561 static tree
2562 lookup_name_in_scope (tree name, struct c_scope *scope)
2564 struct c_binding *b;
2566 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2567 if (B_IN_SCOPE (b, scope))
2568 return b->decl;
2569 return 0;
2572 /* Create the predefined scalar types of C,
2573 and some nodes representing standard constants (0, 1, (void *) 0).
2574 Initialize the global scope.
2575 Make definitions for built-in primitive functions. */
2577 void
2578 c_init_decl_processing (void)
2580 tree endlink;
2581 location_t save_loc = input_location;
2583 /* Initialize reserved words for parser. */
2584 c_parse_init ();
2586 current_function_decl = 0;
2588 gcc_obstack_init (&parser_obstack);
2590 /* Make the externals scope. */
2591 push_scope ();
2592 external_scope = current_scope;
2594 /* Declarations from c_common_nodes_and_builtins must not be associated
2595 with this input file, lest we get differences between using and not
2596 using preprocessed headers. */
2597 #ifdef USE_MAPPED_LOCATION
2598 input_location = BUILTINS_LOCATION;
2599 #else
2600 input_location.file = "<built-in>";
2601 input_location.line = 0;
2602 #endif
2604 build_common_tree_nodes (flag_signed_char, false);
2606 c_common_nodes_and_builtins ();
2608 /* In C, comparisons and TRUTH_* expressions have type int. */
2609 truthvalue_type_node = integer_type_node;
2610 truthvalue_true_node = integer_one_node;
2611 truthvalue_false_node = integer_zero_node;
2613 /* Even in C99, which has a real boolean type. */
2614 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2615 boolean_type_node));
2617 endlink = void_list_node;
2618 build_function_type (ptr_type_node, endlink);
2619 build_function_type (ptr_type_node,
2620 tree_cons (NULL_TREE, ptr_type_node, endlink));
2622 input_location = save_loc;
2624 pedantic_lvalues = true;
2626 make_fname_decl = c_make_fname_decl;
2627 start_fname_decls ();
2630 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2631 decl, NAME is the initialization string and TYPE_DEP indicates whether
2632 NAME depended on the type of the function. As we don't yet implement
2633 delayed emission of static data, we mark the decl as emitted
2634 so it is not placed in the output. Anything using it must therefore pull
2635 out the STRING_CST initializer directly. FIXME. */
2637 static tree
2638 c_make_fname_decl (tree id, int type_dep)
2640 const char *name = fname_as_string (type_dep);
2641 tree decl, type, init;
2642 size_t length = strlen (name);
2644 type = build_array_type (char_type_node,
2645 build_index_type (size_int (length)));
2646 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2648 decl = build_decl (VAR_DECL, id, type);
2650 TREE_STATIC (decl) = 1;
2651 TREE_READONLY (decl) = 1;
2652 DECL_ARTIFICIAL (decl) = 1;
2654 init = build_string (length + 1, name);
2655 free ((char *) name);
2656 TREE_TYPE (init) = type;
2657 DECL_INITIAL (decl) = init;
2659 TREE_USED (decl) = 1;
2661 if (current_function_decl)
2663 DECL_CONTEXT (decl) = current_function_decl;
2664 bind (id, decl, current_function_scope,
2665 /*invisible=*/false, /*nested=*/false);
2668 finish_decl (decl, init, NULL_TREE);
2670 return decl;
2673 /* Return a definition for a builtin function named NAME and whose data type
2674 is TYPE. TYPE should be a function type with argument types.
2675 FUNCTION_CODE tells later passes how to compile calls to this function.
2676 See tree.h for its possible values.
2678 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2679 the name to be called if we can't opencode the function. If
2680 ATTRS is nonzero, use that for the function's attribute list. */
2682 tree
2683 builtin_function (const char *name, tree type, int function_code,
2684 enum built_in_class cl, const char *library_name,
2685 tree attrs)
2687 tree id = get_identifier (name);
2688 tree decl = build_decl (FUNCTION_DECL, id, type);
2689 TREE_PUBLIC (decl) = 1;
2690 DECL_EXTERNAL (decl) = 1;
2691 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2692 DECL_BUILT_IN_CLASS (decl) = cl;
2693 DECL_FUNCTION_CODE (decl) = function_code;
2694 if (library_name)
2695 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2697 /* Should never be called on a symbol with a preexisting meaning. */
2698 gcc_assert (!I_SYMBOL_BINDING (id));
2700 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2702 /* Builtins in the implementation namespace are made visible without
2703 needing to be explicitly declared. See push_file_scope. */
2704 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2706 TREE_CHAIN (decl) = visible_builtins;
2707 visible_builtins = decl;
2710 /* Possibly apply some default attributes to this built-in function. */
2711 if (attrs)
2712 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2713 else
2714 decl_attributes (&decl, NULL_TREE, 0);
2716 return decl;
2719 /* Called when a declaration is seen that contains no names to declare.
2720 If its type is a reference to a structure, union or enum inherited
2721 from a containing scope, shadow that tag name for the current scope
2722 with a forward reference.
2723 If its type defines a new named structure or union
2724 or defines an enum, it is valid but we need not do anything here.
2725 Otherwise, it is an error. */
2727 void
2728 shadow_tag (const struct c_declspecs *declspecs)
2730 shadow_tag_warned (declspecs, 0);
2733 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2734 but no pedwarn. */
2735 void
2736 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2738 bool found_tag = false;
2740 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2742 tree value = declspecs->type;
2743 enum tree_code code = TREE_CODE (value);
2745 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2746 /* Used to test also that TYPE_SIZE (value) != 0.
2747 That caused warning for `struct foo;' at top level in the file. */
2749 tree name = TYPE_NAME (value);
2750 tree t;
2752 found_tag = true;
2754 if (name == 0)
2756 if (warned != 1 && code != ENUMERAL_TYPE)
2757 /* Empty unnamed enum OK */
2759 pedwarn ("unnamed struct/union that defines no instances");
2760 warned = 1;
2763 else if (!declspecs->tag_defined_p
2764 && declspecs->storage_class != csc_none)
2766 if (warned != 1)
2767 pedwarn ("empty declaration with storage class specifier "
2768 "does not redeclare tag");
2769 warned = 1;
2770 pending_xref_error ();
2772 else if (!declspecs->tag_defined_p
2773 && (declspecs->const_p
2774 || declspecs->volatile_p
2775 || declspecs->restrict_p))
2777 if (warned != 1)
2778 pedwarn ("empty declaration with type qualifier "
2779 "does not redeclare tag");
2780 warned = 1;
2781 pending_xref_error ();
2783 else
2785 pending_invalid_xref = 0;
2786 t = lookup_tag (code, name, 1);
2788 if (t == 0)
2790 t = make_node (code);
2791 pushtag (name, t);
2795 else
2797 if (warned != 1 && !in_system_header)
2799 pedwarn ("useless type name in empty declaration");
2800 warned = 1;
2804 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2806 pedwarn ("useless type name in empty declaration");
2807 warned = 1;
2810 pending_invalid_xref = 0;
2812 if (declspecs->inline_p)
2814 error ("%<inline%> in empty declaration");
2815 warned = 1;
2818 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2820 error ("%<auto%> in file-scope empty declaration");
2821 warned = 1;
2824 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2826 error ("%<register%> in file-scope empty declaration");
2827 warned = 1;
2830 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2832 warning ("useless storage class specifier in empty declaration");
2833 warned = 2;
2836 if (!warned && !in_system_header && declspecs->thread_p)
2838 warning ("useless %<__thread%> in empty declaration");
2839 warned = 2;
2842 if (!warned && !in_system_header && (declspecs->const_p
2843 || declspecs->volatile_p
2844 || declspecs->restrict_p))
2846 warning ("useless type qualifier in empty declaration");
2847 warned = 2;
2850 if (warned != 1)
2852 if (!found_tag)
2853 pedwarn ("empty declaration");
2858 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2859 bits. SPECS represents declaration specifiers that the grammar
2860 only permits to contain type qualifiers and attributes. */
2863 quals_from_declspecs (const struct c_declspecs *specs)
2865 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2866 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2867 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2868 gcc_assert (!specs->type
2869 && !specs->decl_attr
2870 && specs->typespec_word == cts_none
2871 && specs->storage_class == csc_none
2872 && !specs->typedef_p
2873 && !specs->explicit_signed_p
2874 && !specs->deprecated_p
2875 && !specs->long_p
2876 && !specs->long_long_p
2877 && !specs->short_p
2878 && !specs->signed_p
2879 && !specs->unsigned_p
2880 && !specs->complex_p
2881 && !specs->inline_p
2882 && !specs->thread_p);
2883 return quals;
2886 /* Construct an array declarator. EXPR is the expression inside [], or
2887 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2888 to the pointer to which a parameter array is converted). STATIC_P is
2889 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2890 is true if the array is [*], a VLA of unspecified length which is
2891 nevertheless a complete type (not currently implemented by GCC),
2892 false otherwise. The field for the contained declarator is left to be
2893 filled in by set_array_declarator_inner. */
2895 struct c_declarator *
2896 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2897 bool vla_unspec_p)
2899 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2900 struct c_declarator);
2901 declarator->kind = cdk_array;
2902 declarator->declarator = 0;
2903 declarator->u.array.dimen = expr;
2904 if (quals)
2906 declarator->u.array.attrs = quals->attrs;
2907 declarator->u.array.quals = quals_from_declspecs (quals);
2909 else
2911 declarator->u.array.attrs = NULL_TREE;
2912 declarator->u.array.quals = 0;
2914 declarator->u.array.static_p = static_p;
2915 declarator->u.array.vla_unspec_p = vla_unspec_p;
2916 if (pedantic && !flag_isoc99)
2918 if (static_p || quals != NULL)
2919 pedwarn ("ISO C90 does not support %<static%> or type "
2920 "qualifiers in parameter array declarators");
2921 if (vla_unspec_p)
2922 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2924 if (vla_unspec_p)
2925 warning ("GCC does not yet properly implement %<[*]%> array declarators");
2926 return declarator;
2929 /* Set the contained declarator of an array declarator. DECL is the
2930 declarator, as constructed by build_array_declarator; INNER is what
2931 appears on the left of the []. ABSTRACT_P is true if it is an
2932 abstract declarator, false otherwise; this is used to reject static
2933 and type qualifiers in abstract declarators, where they are not in
2934 the C99 grammar (subject to possible change in DR#289). */
2936 struct c_declarator *
2937 set_array_declarator_inner (struct c_declarator *decl,
2938 struct c_declarator *inner, bool abstract_p)
2940 decl->declarator = inner;
2941 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2942 || decl->u.array.attrs != NULL_TREE
2943 || decl->u.array.static_p))
2944 error ("static or type qualifiers in abstract declarator");
2945 return decl;
2948 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2950 tree
2951 groktypename (struct c_type_name *type_name)
2953 tree type;
2954 tree attrs = type_name->specs->attrs;
2956 type_name->specs->attrs = NULL_TREE;
2958 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2959 false, NULL);
2961 /* Apply attributes. */
2962 decl_attributes (&type, attrs, 0);
2964 return type;
2967 /* Decode a declarator in an ordinary declaration or data definition.
2968 This is called as soon as the type information and variable name
2969 have been parsed, before parsing the initializer if any.
2970 Here we create the ..._DECL node, fill in its type,
2971 and put it on the list of decls for the current context.
2972 The ..._DECL node is returned as the value.
2974 Exception: for arrays where the length is not specified,
2975 the type is left null, to be filled in by `finish_decl'.
2977 Function definitions do not come here; they go to start_function
2978 instead. However, external and forward declarations of functions
2979 do go through here. Structure field declarations are done by
2980 grokfield and not through here. */
2982 tree
2983 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
2984 bool initialized, tree attributes)
2986 tree decl;
2987 tree tem;
2989 /* An object declared as __attribute__((deprecated)) suppresses
2990 warnings of uses of other deprecated items. */
2991 if (lookup_attribute ("deprecated", attributes))
2992 deprecated_state = DEPRECATED_SUPPRESS;
2994 decl = grokdeclarator (declarator, declspecs,
2995 NORMAL, initialized, NULL);
2996 if (!decl)
2997 return 0;
2999 deprecated_state = DEPRECATED_NORMAL;
3001 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3002 && MAIN_NAME_P (DECL_NAME (decl)))
3003 warning ("%J%qD is usually a function", decl, decl);
3005 if (initialized)
3006 /* Is it valid for this decl to have an initializer at all?
3007 If not, set INITIALIZED to zero, which will indirectly
3008 tell 'finish_decl' to ignore the initializer once it is parsed. */
3009 switch (TREE_CODE (decl))
3011 case TYPE_DECL:
3012 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3013 initialized = 0;
3014 break;
3016 case FUNCTION_DECL:
3017 error ("function %qD is initialized like a variable", decl);
3018 initialized = 0;
3019 break;
3021 case PARM_DECL:
3022 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3023 error ("parameter %qD is initialized", decl);
3024 initialized = 0;
3025 break;
3027 default:
3028 /* Don't allow initializations for incomplete types except for
3029 arrays which might be completed by the initialization. */
3031 /* This can happen if the array size is an undefined macro.
3032 We already gave a warning, so we don't need another one. */
3033 if (TREE_TYPE (decl) == error_mark_node)
3034 initialized = 0;
3035 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3037 /* A complete type is ok if size is fixed. */
3039 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3040 || C_DECL_VARIABLE_SIZE (decl))
3042 error ("variable-sized object may not be initialized");
3043 initialized = 0;
3046 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3048 error ("variable %qD has initializer but incomplete type", decl);
3049 initialized = 0;
3051 else if (C_DECL_VARIABLE_SIZE (decl))
3053 /* Although C99 is unclear about whether incomplete arrays
3054 of VLAs themselves count as VLAs, it does not make
3055 sense to permit them to be initialized given that
3056 ordinary VLAs may not be initialized. */
3057 error ("variable-sized object may not be initialized");
3058 initialized = 0;
3062 if (initialized)
3064 if (current_scope == file_scope)
3065 TREE_STATIC (decl) = 1;
3067 /* Tell 'pushdecl' this is an initialized decl
3068 even though we don't yet have the initializer expression.
3069 Also tell 'finish_decl' it may store the real initializer. */
3070 DECL_INITIAL (decl) = error_mark_node;
3073 /* If this is a function declaration, write a record describing it to the
3074 prototypes file (if requested). */
3076 if (TREE_CODE (decl) == FUNCTION_DECL)
3077 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3079 /* ANSI specifies that a tentative definition which is not merged with
3080 a non-tentative definition behaves exactly like a definition with an
3081 initializer equal to zero. (Section 3.7.2)
3083 -fno-common gives strict ANSI behavior, though this tends to break
3084 a large body of code that grew up without this rule.
3086 Thread-local variables are never common, since there's no entrenched
3087 body of code to break, and it allows more efficient variable references
3088 in the presence of dynamic linking. */
3090 if (TREE_CODE (decl) == VAR_DECL
3091 && !initialized
3092 && TREE_PUBLIC (decl)
3093 && !DECL_THREAD_LOCAL (decl)
3094 && !flag_no_common)
3095 DECL_COMMON (decl) = 1;
3097 /* Set attributes here so if duplicate decl, will have proper attributes. */
3098 decl_attributes (&decl, attributes, 0);
3100 if (TREE_CODE (decl) == FUNCTION_DECL
3101 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3103 struct c_declarator *ce = declarator;
3105 if (ce->kind == cdk_pointer)
3106 ce = declarator->declarator;
3107 if (ce->kind == cdk_function)
3109 tree args = ce->u.arg_info->parms;
3110 for (; args; args = TREE_CHAIN (args))
3112 tree type = TREE_TYPE (args);
3113 if (type && INTEGRAL_TYPE_P (type)
3114 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3115 DECL_ARG_TYPE (args) = integer_type_node;
3120 if (TREE_CODE (decl) == FUNCTION_DECL
3121 && DECL_DECLARED_INLINE_P (decl)
3122 && DECL_UNINLINABLE (decl)
3123 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3124 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3126 /* Add this decl to the current scope.
3127 TEM may equal DECL or it may be a previous decl of the same name. */
3128 tem = pushdecl (decl);
3130 if (initialized && DECL_EXTERNAL (tem))
3132 DECL_EXTERNAL (tem) = 0;
3133 TREE_STATIC (tem) = 1;
3136 return tem;
3139 /* Finish processing of a declaration;
3140 install its initial value.
3141 If the length of an array type is not known before,
3142 it must be determined now, from the initial value, or it is an error. */
3144 void
3145 finish_decl (tree decl, tree init, tree asmspec_tree)
3147 tree type = TREE_TYPE (decl);
3148 int was_incomplete = (DECL_SIZE (decl) == 0);
3149 const char *asmspec = 0;
3151 /* If a name was specified, get the string. */
3152 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3153 && DECL_FILE_SCOPE_P (decl))
3154 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3155 if (asmspec_tree)
3156 asmspec = TREE_STRING_POINTER (asmspec_tree);
3158 /* If `start_decl' didn't like having an initialization, ignore it now. */
3159 if (init != 0 && DECL_INITIAL (decl) == 0)
3160 init = 0;
3162 /* Don't crash if parm is initialized. */
3163 if (TREE_CODE (decl) == PARM_DECL)
3164 init = 0;
3166 if (init)
3167 store_init_value (decl, init);
3169 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3170 || TREE_CODE (decl) == FUNCTION_DECL
3171 || TREE_CODE (decl) == FIELD_DECL))
3172 objc_check_decl (decl);
3174 /* Deduce size of array from initialization, if not already known. */
3175 if (TREE_CODE (type) == ARRAY_TYPE
3176 && TYPE_DOMAIN (type) == 0
3177 && TREE_CODE (decl) != TYPE_DECL)
3179 int do_default
3180 = (TREE_STATIC (decl)
3181 /* Even if pedantic, an external linkage array
3182 may have incomplete type at first. */
3183 ? pedantic && !TREE_PUBLIC (decl)
3184 : !DECL_EXTERNAL (decl));
3185 int failure
3186 = complete_array_type (type, DECL_INITIAL (decl), 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;
3208 /* TYPE_MAX_VALUE is always one less than the number of elements
3209 in the array, because we start counting at zero. Therefore,
3210 warn only if the value is less than zero. */
3211 else if (pedantic && TYPE_DOMAIN (type) != 0
3212 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3213 error ("%Jzero or negative size array %qD", decl, decl);
3215 layout_decl (decl, 0);
3218 if (TREE_CODE (decl) == VAR_DECL)
3220 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3221 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3222 layout_decl (decl, 0);
3224 if (DECL_SIZE (decl) == 0
3225 /* Don't give an error if we already gave one earlier. */
3226 && TREE_TYPE (decl) != error_mark_node
3227 && (TREE_STATIC (decl)
3228 /* A static variable with an incomplete type
3229 is an error if it is initialized.
3230 Also if it is not file scope.
3231 Otherwise, let it through, but if it is not `extern'
3232 then it may cause an error message later. */
3233 ? (DECL_INITIAL (decl) != 0
3234 || !DECL_FILE_SCOPE_P (decl))
3235 /* An automatic variable with an incomplete type
3236 is an error. */
3237 : !DECL_EXTERNAL (decl)))
3239 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3240 TREE_TYPE (decl) = error_mark_node;
3243 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3244 && DECL_SIZE (decl) != 0)
3246 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3247 constant_expression_warning (DECL_SIZE (decl));
3248 else
3249 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3252 if (TREE_USED (type))
3253 TREE_USED (decl) = 1;
3256 /* If this is a function and an assembler name is specified, reset DECL_RTL
3257 so we can give it its new name. Also, update built_in_decls if it
3258 was a normal built-in. */
3259 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3261 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3262 set_builtin_user_assembler_name (decl, asmspec);
3263 set_user_assembler_name (decl, asmspec);
3266 /* If #pragma weak was used, mark the decl weak now. */
3267 if (current_scope == file_scope)
3268 maybe_apply_pragma_weak (decl);
3270 /* If this is a variable definition, determine its ELF visibility. */
3271 if (TREE_CODE (decl) == VAR_DECL
3272 && TREE_STATIC (decl)
3273 && !DECL_EXTERNAL (decl))
3274 c_determine_visibility (decl);
3276 /* Output the assembler code and/or RTL code for variables and functions,
3277 unless the type is an undefined structure or union.
3278 If not, it will get done when the type is completed. */
3280 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3282 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3283 if (c_dialect_objc ())
3284 objc_check_decl (decl);
3286 if (asmspec)
3288 /* If this is not a static variable, issue a warning.
3289 It doesn't make any sense to give an ASMSPEC for an
3290 ordinary, non-register local variable. Historically,
3291 GCC has accepted -- but ignored -- the ASMSPEC in
3292 this case. */
3293 if (!DECL_FILE_SCOPE_P (decl)
3294 && TREE_CODE (decl) == VAR_DECL
3295 && !C_DECL_REGISTER (decl)
3296 && !TREE_STATIC (decl))
3297 warning ("%Jignoring asm-specifier for non-static local "
3298 "variable %qD", decl, decl);
3299 else if (C_DECL_REGISTER (decl))
3300 change_decl_assembler_name (decl, get_identifier (asmspec));
3301 else
3302 set_user_assembler_name (decl, asmspec);
3305 if (DECL_FILE_SCOPE_P (decl))
3307 if (DECL_INITIAL (decl) == NULL_TREE
3308 || DECL_INITIAL (decl) == error_mark_node)
3309 /* Don't output anything
3310 when a tentative file-scope definition is seen.
3311 But at end of compilation, do output code for them. */
3312 DECL_DEFER_OUTPUT (decl) = 1;
3313 rest_of_decl_compilation (decl, true, 0);
3315 else
3317 /* In conjunction with an ASMSPEC, the `register'
3318 keyword indicates that we should place the variable
3319 in a particular register. */
3320 if (asmspec && C_DECL_REGISTER (decl))
3322 DECL_HARD_REGISTER (decl) = 1;
3323 /* This cannot be done for a structure with volatile
3324 fields, on which DECL_REGISTER will have been
3325 reset. */
3326 if (!DECL_REGISTER (decl))
3327 error ("cannot put object with volatile field into register");
3330 if (TREE_CODE (decl) != FUNCTION_DECL)
3332 /* If we're building a variable sized type, and we might be
3333 reachable other than via the top of the current binding
3334 level, then create a new BIND_EXPR so that we deallocate
3335 the object at the right time. */
3336 /* Note that DECL_SIZE can be null due to errors. */
3337 if (DECL_SIZE (decl)
3338 && !TREE_CONSTANT (DECL_SIZE (decl))
3339 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3341 tree bind;
3342 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3343 TREE_SIDE_EFFECTS (bind) = 1;
3344 add_stmt (bind);
3345 BIND_EXPR_BODY (bind) = push_stmt_list ();
3347 add_stmt (build_stmt (DECL_EXPR, decl));
3352 if (!DECL_FILE_SCOPE_P (decl))
3354 /* Recompute the RTL of a local array now
3355 if it used to be an incomplete type. */
3356 if (was_incomplete
3357 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3359 /* If we used it already as memory, it must stay in memory. */
3360 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3361 /* If it's still incomplete now, no init will save it. */
3362 if (DECL_SIZE (decl) == 0)
3363 DECL_INITIAL (decl) = 0;
3368 /* If this was marked 'used', be sure it will be output. */
3369 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3370 mark_decl_referenced (decl);
3372 if (TREE_CODE (decl) == TYPE_DECL)
3374 if (!DECL_FILE_SCOPE_P (decl)
3375 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3376 add_stmt (build_stmt (DECL_EXPR, decl));
3378 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3381 /* At the end of a declaration, throw away any variable type sizes
3382 of types defined inside that declaration. There is no use
3383 computing them in the following function definition. */
3384 if (current_scope == file_scope)
3385 get_pending_sizes ();
3387 /* Install a cleanup (aka destructor) if one was given. */
3388 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3390 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3391 if (attr)
3393 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3394 tree cleanup_decl = lookup_name (cleanup_id);
3395 tree cleanup;
3397 /* Build "cleanup(&decl)" for the destructor. */
3398 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3399 cleanup = build_tree_list (NULL_TREE, cleanup);
3400 cleanup = build_function_call (cleanup_decl, cleanup);
3402 /* Don't warn about decl unused; the cleanup uses it. */
3403 TREE_USED (decl) = 1;
3404 TREE_USED (cleanup_decl) = 1;
3406 /* Initialize EH, if we've been told to do so. */
3407 if (flag_exceptions && !c_eh_initialized_p)
3409 c_eh_initialized_p = true;
3410 eh_personality_libfunc
3411 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3412 ? "__gcc_personality_sj0"
3413 : "__gcc_personality_v0");
3414 using_eh_for_cleanups ();
3417 push_cleanup (decl, cleanup, false);
3422 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3424 tree
3425 grokparm (const struct c_parm *parm)
3427 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3428 NULL);
3430 decl_attributes (&decl, parm->attrs, 0);
3432 return decl;
3435 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3436 and push that on the current scope. */
3438 void
3439 push_parm_decl (const struct c_parm *parm)
3441 tree decl;
3443 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3444 decl_attributes (&decl, parm->attrs, 0);
3446 decl = pushdecl (decl);
3448 finish_decl (decl, NULL_TREE, NULL_TREE);
3451 /* Mark all the parameter declarations to date as forward decls.
3452 Also diagnose use of this extension. */
3454 void
3455 mark_forward_parm_decls (void)
3457 struct c_binding *b;
3459 if (pedantic && !current_scope->warned_forward_parm_decls)
3461 pedwarn ("ISO C forbids forward parameter declarations");
3462 current_scope->warned_forward_parm_decls = true;
3465 for (b = current_scope->bindings; b; b = b->prev)
3466 if (TREE_CODE (b->decl) == PARM_DECL)
3467 TREE_ASM_WRITTEN (b->decl) = 1;
3470 static GTY(()) int compound_literal_number;
3472 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3473 literal, which may be an incomplete array type completed by the
3474 initializer; INIT is a CONSTRUCTOR that initializes the compound
3475 literal. */
3477 tree
3478 build_compound_literal (tree type, tree init)
3480 /* We do not use start_decl here because we have a type, not a declarator;
3481 and do not use finish_decl because the decl should be stored inside
3482 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3483 tree decl;
3484 tree complit;
3485 tree stmt;
3487 if (type == error_mark_node)
3488 return error_mark_node;
3490 decl = build_decl (VAR_DECL, NULL_TREE, type);
3491 DECL_EXTERNAL (decl) = 0;
3492 TREE_PUBLIC (decl) = 0;
3493 TREE_STATIC (decl) = (current_scope == file_scope);
3494 DECL_CONTEXT (decl) = current_function_decl;
3495 TREE_USED (decl) = 1;
3496 TREE_TYPE (decl) = type;
3497 TREE_READONLY (decl) = TYPE_READONLY (type);
3498 store_init_value (decl, init);
3500 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3502 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3504 gcc_assert (!failure);
3507 type = TREE_TYPE (decl);
3508 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3509 return error_mark_node;
3511 stmt = build_stmt (DECL_EXPR, decl);
3512 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3513 TREE_SIDE_EFFECTS (complit) = 1;
3515 layout_decl (decl, 0);
3517 if (TREE_STATIC (decl))
3519 /* This decl needs a name for the assembler output. We also need
3520 a unique suffix to be added to the name. */
3521 char *name;
3523 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3524 compound_literal_number);
3525 compound_literal_number++;
3526 DECL_NAME (decl) = get_identifier (name);
3527 DECL_DEFER_OUTPUT (decl) = 1;
3528 DECL_COMDAT (decl) = 1;
3529 DECL_ARTIFICIAL (decl) = 1;
3530 DECL_IGNORED_P (decl) = 1;
3531 pushdecl (decl);
3532 rest_of_decl_compilation (decl, 1, 0);
3535 return complit;
3538 /* Make TYPE a complete type based on INITIAL_VALUE.
3539 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3540 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3543 complete_array_type (tree type, tree initial_value, int do_default)
3545 tree maxindex = NULL_TREE;
3546 int value = 0;
3548 if (initial_value)
3550 /* Note MAXINDEX is really the maximum index,
3551 one less than the size. */
3552 if (TREE_CODE (initial_value) == STRING_CST)
3554 int eltsize
3555 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3556 maxindex = build_int_cst (NULL_TREE,
3557 (TREE_STRING_LENGTH (initial_value)
3558 / eltsize) - 1);
3560 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3562 tree elts = CONSTRUCTOR_ELTS (initial_value);
3563 maxindex = build_int_cst (NULL_TREE, -1);
3564 for (; elts; elts = TREE_CHAIN (elts))
3566 if (TREE_PURPOSE (elts))
3567 maxindex = TREE_PURPOSE (elts);
3568 else
3569 maxindex = fold (build2 (PLUS_EXPR, integer_type_node,
3570 maxindex, integer_one_node));
3573 else
3575 /* Make an error message unless that happened already. */
3576 if (initial_value != error_mark_node)
3577 value = 1;
3579 /* Prevent further error messages. */
3580 maxindex = build_int_cst (NULL_TREE, 0);
3584 if (!maxindex)
3586 if (do_default)
3587 maxindex = build_int_cst (NULL_TREE, 0);
3588 value = 2;
3591 if (maxindex)
3593 TYPE_DOMAIN (type) = build_index_type (maxindex);
3595 gcc_assert (TREE_TYPE (maxindex));
3598 /* Lay out the type now that we can get the real answer. */
3600 layout_type (type);
3602 return value;
3605 /* Determine whether TYPE is a structure with a flexible array member,
3606 or a union containing such a structure (possibly recursively). */
3608 static bool
3609 flexible_array_type_p (tree type)
3611 tree x;
3612 switch (TREE_CODE (type))
3614 case RECORD_TYPE:
3615 x = TYPE_FIELDS (type);
3616 if (x == NULL_TREE)
3617 return false;
3618 while (TREE_CHAIN (x) != NULL_TREE)
3619 x = TREE_CHAIN (x);
3620 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3621 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3622 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3623 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3624 return true;
3625 return false;
3626 case UNION_TYPE:
3627 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3629 if (flexible_array_type_p (TREE_TYPE (x)))
3630 return true;
3632 return false;
3633 default:
3634 return false;
3638 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3639 replacing with appropriate values if they are invalid. */
3640 static void
3641 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3643 tree type_mv;
3644 unsigned int max_width;
3645 unsigned HOST_WIDE_INT w;
3646 const char *name = orig_name ? orig_name: _("<anonymous>");
3648 /* Necessary? */
3649 STRIP_NOPS (*width);
3651 /* Detect and ignore out of range field width and process valid
3652 field widths. */
3653 if (TREE_CODE (*width) != INTEGER_CST)
3655 error ("bit-field %qs width not an integer constant", name);
3656 *width = integer_one_node;
3658 else
3660 constant_expression_warning (*width);
3661 if (tree_int_cst_sgn (*width) < 0)
3663 error ("negative width in bit-field %qs", name);
3664 *width = integer_one_node;
3666 else if (integer_zerop (*width) && orig_name)
3668 error ("zero width for bit-field %qs", name);
3669 *width = integer_one_node;
3673 /* Detect invalid bit-field type. */
3674 if (TREE_CODE (*type) != INTEGER_TYPE
3675 && TREE_CODE (*type) != BOOLEAN_TYPE
3676 && TREE_CODE (*type) != ENUMERAL_TYPE)
3678 error ("bit-field %qs has invalid type", name);
3679 *type = unsigned_type_node;
3682 type_mv = TYPE_MAIN_VARIANT (*type);
3683 if (pedantic
3684 && type_mv != integer_type_node
3685 && type_mv != unsigned_type_node
3686 && type_mv != boolean_type_node)
3687 pedwarn ("type of bit-field %qs is a GCC extension", name);
3689 if (type_mv == boolean_type_node)
3690 max_width = CHAR_TYPE_SIZE;
3691 else
3692 max_width = TYPE_PRECISION (*type);
3694 if (0 < compare_tree_int (*width, max_width))
3696 error ("width of %qs exceeds its type", name);
3697 w = max_width;
3698 *width = build_int_cst (NULL_TREE, w);
3700 else
3701 w = tree_low_cst (*width, 1);
3703 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3705 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3706 if (!lt
3707 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3708 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3709 warning ("%qs is narrower than values of its type", name);
3713 /* Given declspecs and a declarator,
3714 determine the name and type of the object declared
3715 and construct a ..._DECL node for it.
3716 (In one case we can return a ..._TYPE node instead.
3717 For invalid input we sometimes return 0.)
3719 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3721 DECL_CONTEXT says which syntactic context this declaration is in:
3722 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3723 FUNCDEF for a function definition. Like NORMAL but a few different
3724 error messages in each case. Return value may be zero meaning
3725 this definition is too screwy to try to parse.
3726 PARM for a parameter declaration (either within a function prototype
3727 or before a function body). Make a PARM_DECL, or return void_type_node.
3728 TYPENAME if for a typename (in a cast or sizeof).
3729 Don't make a DECL node; just return the ..._TYPE node.
3730 FIELD for a struct or union field; make a FIELD_DECL.
3731 INITIALIZED is true if the decl has an initializer.
3732 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3733 representing the width of the bit-field.
3735 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3736 It may also be so in the PARM case, for a prototype where the
3737 argument type is specified but not the name.
3739 This function is where the complicated C meanings of `static'
3740 and `extern' are interpreted. */
3742 static tree
3743 grokdeclarator (const struct c_declarator *declarator,
3744 struct c_declspecs *declspecs,
3745 enum decl_context decl_context, bool initialized, tree *width)
3747 tree type = declspecs->type;
3748 bool threadp = declspecs->thread_p;
3749 enum c_storage_class storage_class = declspecs->storage_class;
3750 int constp;
3751 int restrictp;
3752 int volatilep;
3753 int type_quals = TYPE_UNQUALIFIED;
3754 const char *name, *orig_name;
3755 tree typedef_type = 0;
3756 int funcdef_flag = 0;
3757 bool funcdef_syntax = false;
3758 int size_varies = 0;
3759 tree decl_attr = declspecs->decl_attr;
3760 int array_ptr_quals = TYPE_UNQUALIFIED;
3761 tree array_ptr_attrs = NULL_TREE;
3762 int array_parm_static = 0;
3763 tree returned_attrs = NULL_TREE;
3764 bool bitfield = width != NULL;
3765 tree element_type;
3766 struct c_arg_info *arg_info = 0;
3768 if (decl_context == FUNCDEF)
3769 funcdef_flag = 1, decl_context = NORMAL;
3771 /* Look inside a declarator for the name being declared
3772 and get it as a string, for an error message. */
3774 const struct c_declarator *decl = declarator;
3775 name = 0;
3777 while (decl)
3778 switch (decl->kind)
3780 case cdk_function:
3781 case cdk_array:
3782 case cdk_pointer:
3783 funcdef_syntax = (decl->kind == cdk_function);
3784 decl = decl->declarator;
3785 break;
3787 case cdk_attrs:
3788 decl = decl->declarator;
3789 break;
3791 case cdk_id:
3792 if (decl->u.id)
3793 name = IDENTIFIER_POINTER (decl->u.id);
3794 decl = 0;
3795 break;
3797 default:
3798 gcc_unreachable ();
3800 orig_name = name;
3801 if (name == 0)
3802 name = "type name";
3805 /* A function definition's declarator must have the form of
3806 a function declarator. */
3808 if (funcdef_flag && !funcdef_syntax)
3809 return 0;
3811 /* If this looks like a function definition, make it one,
3812 even if it occurs where parms are expected.
3813 Then store_parm_decls will reject it and not use it as a parm. */
3814 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3815 decl_context = PARM;
3817 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3818 warn_deprecated_use (declspecs->type);
3820 typedef_type = type;
3821 size_varies = C_TYPE_VARIABLE_SIZE (type);
3823 /* Diagnose defaulting to "int". */
3825 if (declspecs->default_int_p && !in_system_header)
3827 /* Issue a warning if this is an ISO C 99 program or if
3828 -Wreturn-type and this is a function, or if -Wimplicit;
3829 prefer the former warning since it is more explicit. */
3830 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3831 && funcdef_flag)
3832 warn_about_return_type = 1;
3833 else if (warn_implicit_int || flag_isoc99)
3834 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3837 /* Adjust the type if a bit-field is being declared,
3838 -funsigned-bitfields applied and the type is not explicitly
3839 "signed". */
3840 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3841 && TREE_CODE (type) == INTEGER_TYPE)
3842 type = c_common_unsigned_type (type);
3844 /* Figure out the type qualifiers for the declaration. There are
3845 two ways a declaration can become qualified. One is something
3846 like `const int i' where the `const' is explicit. Another is
3847 something like `typedef const int CI; CI i' where the type of the
3848 declaration contains the `const'. A third possibility is that
3849 there is a type qualifier on the element type of a typedefed
3850 array type, in which case we should extract that qualifier so
3851 that c_apply_type_quals_to_decls receives the full list of
3852 qualifiers to work with (C90 is not entirely clear about whether
3853 duplicate qualifiers should be diagnosed in this case, but it
3854 seems most appropriate to do so). */
3855 element_type = strip_array_types (type);
3856 constp = declspecs->const_p + TYPE_READONLY (element_type);
3857 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3858 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3859 if (pedantic && !flag_isoc99)
3861 if (constp > 1)
3862 pedwarn ("duplicate %<const%>");
3863 if (restrictp > 1)
3864 pedwarn ("duplicate %<restrict%>");
3865 if (volatilep > 1)
3866 pedwarn ("duplicate %<volatile%>");
3868 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3869 type = TYPE_MAIN_VARIANT (type);
3870 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3871 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3872 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3874 /* Warn about storage classes that are invalid for certain
3875 kinds of declarations (parameters, typenames, etc.). */
3877 if (funcdef_flag
3878 && (threadp
3879 || storage_class == csc_auto
3880 || storage_class == csc_register
3881 || storage_class == csc_typedef))
3883 if (storage_class == csc_auto
3884 && (pedantic || current_scope == file_scope))
3885 pedwarn ("function definition declared %<auto%>");
3886 if (storage_class == csc_register)
3887 error ("function definition declared %<register%>");
3888 if (storage_class == csc_typedef)
3889 error ("function definition declared %<typedef%>");
3890 if (threadp)
3891 error ("function definition declared %<__thread%>");
3892 threadp = false;
3893 if (storage_class == csc_auto
3894 || storage_class == csc_register
3895 || storage_class == csc_typedef)
3896 storage_class = csc_none;
3898 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3900 if (decl_context == PARM && storage_class == csc_register)
3902 else
3904 switch (decl_context)
3906 case FIELD:
3907 error ("storage class specified for structure field %qs",
3908 name);
3909 break;
3910 case PARM:
3911 error ("storage class specified for parameter %qs", name);
3912 break;
3913 default:
3914 error ("storage class specified for typename");
3915 break;
3917 storage_class = csc_none;
3918 threadp = false;
3921 else if (storage_class == csc_extern
3922 && initialized
3923 && !funcdef_flag)
3925 /* 'extern' with initialization is invalid if not at file scope. */
3926 if (current_scope == file_scope)
3927 warning ("%qs initialized and declared %<extern%>", name);
3928 else
3929 error ("%qs has both %<extern%> and initializer", name);
3931 else if (current_scope == file_scope)
3933 if (storage_class == csc_auto)
3934 error ("file-scope declaration of %qs specifies %<auto%>", name);
3935 if (pedantic && storage_class == csc_register)
3936 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3938 else
3940 if (storage_class == csc_extern && funcdef_flag)
3941 error ("nested function %qs declared %<extern%>", name);
3942 else if (threadp && storage_class == csc_none)
3944 error ("function-scope %qs implicitly auto and declared "
3945 "%<__thread%>",
3946 name);
3947 threadp = false;
3951 /* Now figure out the structure of the declarator proper.
3952 Descend through it, creating more complex types, until we reach
3953 the declared identifier (or NULL_TREE, in an absolute declarator).
3954 At each stage we maintain an unqualified version of the type
3955 together with any qualifiers that should be applied to it with
3956 c_build_qualified_type; this way, array types including
3957 multidimensional array types are first built up in unqualified
3958 form and then the qualified form is created with
3959 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3961 while (declarator && declarator->kind != cdk_id)
3963 if (type == error_mark_node)
3965 declarator = declarator->declarator;
3966 continue;
3969 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3970 a cdk_pointer (for *...),
3971 a cdk_function (for ...(...)),
3972 a cdk_attrs (for nested attributes),
3973 or a cdk_id (for the name being declared
3974 or the place in an absolute declarator
3975 where the name was omitted).
3976 For the last case, we have just exited the loop.
3978 At this point, TYPE is the type of elements of an array,
3979 or for a function to return, or for a pointer to point to.
3980 After this sequence of ifs, TYPE is the type of the
3981 array or function or pointer, and DECLARATOR has had its
3982 outermost layer removed. */
3984 if (array_ptr_quals != TYPE_UNQUALIFIED
3985 || array_ptr_attrs != NULL_TREE
3986 || array_parm_static)
3988 /* Only the innermost declarator (making a parameter be of
3989 array type which is converted to pointer type)
3990 may have static or type qualifiers. */
3991 error ("static or type qualifiers in non-parameter array declarator");
3992 array_ptr_quals = TYPE_UNQUALIFIED;
3993 array_ptr_attrs = NULL_TREE;
3994 array_parm_static = 0;
3997 switch (declarator->kind)
3999 case cdk_attrs:
4001 /* A declarator with embedded attributes. */
4002 tree attrs = declarator->u.attrs;
4003 const struct c_declarator *inner_decl;
4004 int attr_flags = 0;
4005 declarator = declarator->declarator;
4006 inner_decl = declarator;
4007 while (inner_decl->kind == cdk_attrs)
4008 inner_decl = inner_decl->declarator;
4009 if (inner_decl->kind == cdk_id)
4010 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4011 else if (inner_decl->kind == cdk_function)
4012 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4013 else if (inner_decl->kind == cdk_array)
4014 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4015 returned_attrs = decl_attributes (&type,
4016 chainon (returned_attrs, attrs),
4017 attr_flags);
4018 break;
4020 case cdk_array:
4022 tree itype = NULL_TREE;
4023 tree size = declarator->u.array.dimen;
4024 /* The index is a signed object `sizetype' bits wide. */
4025 tree index_type = c_common_signed_type (sizetype);
4027 array_ptr_quals = declarator->u.array.quals;
4028 array_ptr_attrs = declarator->u.array.attrs;
4029 array_parm_static = declarator->u.array.static_p;
4031 declarator = declarator->declarator;
4033 /* Check for some types that there cannot be arrays of. */
4035 if (VOID_TYPE_P (type))
4037 error ("declaration of %qs as array of voids", name);
4038 type = error_mark_node;
4041 if (TREE_CODE (type) == FUNCTION_TYPE)
4043 error ("declaration of %qs as array of functions", name);
4044 type = error_mark_node;
4047 if (pedantic && !in_system_header && flexible_array_type_p (type))
4048 pedwarn ("invalid use of structure with flexible array member");
4050 if (size == error_mark_node)
4051 type = error_mark_node;
4053 if (type == error_mark_node)
4054 continue;
4056 /* If size was specified, set ITYPE to a range-type for
4057 that size. Otherwise, ITYPE remains null. finish_decl
4058 may figure it out from an initial value. */
4060 if (size)
4062 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4063 lvalue. */
4064 STRIP_TYPE_NOPS (size);
4066 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4068 error ("size of array %qs has non-integer type", name);
4069 size = integer_one_node;
4072 if (pedantic && integer_zerop (size))
4073 pedwarn ("ISO C forbids zero-size array %qs", name);
4075 if (TREE_CODE (size) == INTEGER_CST)
4077 constant_expression_warning (size);
4078 if (tree_int_cst_sgn (size) < 0)
4080 error ("size of array %qs is negative", name);
4081 size = integer_one_node;
4084 else
4086 /* Make sure the array size remains visibly
4087 nonconstant even if it is (eg) a const variable
4088 with known value. */
4089 size_varies = 1;
4091 if (!flag_isoc99 && pedantic)
4093 if (TREE_CONSTANT (size))
4094 pedwarn ("ISO C90 forbids array %qs whose size "
4095 "can%'t be evaluated",
4096 name);
4097 else
4098 pedwarn ("ISO C90 forbids variable-size array %qs",
4099 name);
4103 if (integer_zerop (size))
4105 /* A zero-length array cannot be represented with
4106 an unsigned index type, which is what we'll
4107 get with build_index_type. Create an
4108 open-ended range instead. */
4109 itype = build_range_type (sizetype, size, NULL_TREE);
4111 else
4113 /* Arrange for the SAVE_EXPR on the inside of the
4114 MINUS_EXPR, which allows the -1 to get folded
4115 with the +1 that happens when building TYPE_SIZE. */
4116 if (size_varies)
4117 size = variable_size (size);
4119 /* Compute the maximum valid index, that is, size
4120 - 1. Do the calculation in index_type, so that
4121 if it is a variable the computations will be
4122 done in the proper mode. */
4123 itype = fold (build2 (MINUS_EXPR, index_type,
4124 convert (index_type, size),
4125 convert (index_type,
4126 size_one_node)));
4128 /* If that overflowed, the array is too big. ???
4129 While a size of INT_MAX+1 technically shouldn't
4130 cause an overflow (because we subtract 1), the
4131 overflow is recorded during the conversion to
4132 index_type, before the subtraction. Handling
4133 this case seems like an unnecessary
4134 complication. */
4135 if (TREE_OVERFLOW (itype))
4137 error ("size of array %qs is too large", name);
4138 type = error_mark_node;
4139 continue;
4142 itype = build_index_type (itype);
4145 else if (decl_context == FIELD)
4147 if (pedantic && !flag_isoc99 && !in_system_header)
4148 pedwarn ("ISO C90 does not support flexible array members");
4150 /* ISO C99 Flexible array members are effectively
4151 identical to GCC's zero-length array extension. */
4152 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4155 /* Complain about arrays of incomplete types. */
4156 if (!COMPLETE_TYPE_P (type))
4158 error ("array type has incomplete element type");
4159 type = error_mark_node;
4161 else
4162 type = build_array_type (type, itype);
4164 if (size_varies)
4165 C_TYPE_VARIABLE_SIZE (type) = 1;
4167 /* The GCC extension for zero-length arrays differs from
4168 ISO flexible array members in that sizeof yields
4169 zero. */
4170 if (size && integer_zerop (size))
4172 TYPE_SIZE (type) = bitsize_zero_node;
4173 TYPE_SIZE_UNIT (type) = size_zero_node;
4176 if (decl_context != PARM
4177 && (array_ptr_quals != TYPE_UNQUALIFIED
4178 || array_ptr_attrs != NULL_TREE
4179 || array_parm_static))
4181 error ("static or type qualifiers in non-parameter array declarator");
4182 array_ptr_quals = TYPE_UNQUALIFIED;
4183 array_ptr_attrs = NULL_TREE;
4184 array_parm_static = 0;
4186 break;
4188 case cdk_function:
4190 /* Say it's a definition only for the declarator closest
4191 to the identifier, apart possibly from some
4192 attributes. */
4193 bool really_funcdef = false;
4194 tree arg_types;
4195 if (funcdef_flag)
4197 const struct c_declarator *t = declarator->declarator;
4198 while (t->kind == cdk_attrs)
4199 t = t->declarator;
4200 really_funcdef = (t->kind == cdk_id);
4203 /* Declaring a function type. Make sure we have a valid
4204 type for the function to return. */
4205 if (type == error_mark_node)
4206 continue;
4208 size_varies = 0;
4210 /* Warn about some types functions can't return. */
4211 if (TREE_CODE (type) == FUNCTION_TYPE)
4213 error ("%qs declared as function returning a function", name);
4214 type = integer_type_node;
4216 if (TREE_CODE (type) == ARRAY_TYPE)
4218 error ("%qs declared as function returning an array", name);
4219 type = integer_type_node;
4222 /* Construct the function type and go to the next
4223 inner layer of declarator. */
4224 arg_info = declarator->u.arg_info;
4225 arg_types = grokparms (arg_info, really_funcdef);
4227 /* Type qualifiers before the return type of the function
4228 qualify the return type, not the function type. */
4229 if (type_quals)
4231 /* Type qualifiers on a function return type are
4232 normally permitted by the standard but have no
4233 effect, so give a warning at -Wreturn-type.
4234 Qualifiers on a void return type are banned on
4235 function definitions in ISO C; GCC used to used
4236 them for noreturn functions. */
4237 if (VOID_TYPE_P (type) && really_funcdef)
4238 pedwarn ("function definition has qualified void return type");
4239 else if (warn_return_type)
4240 warning ("type qualifiers ignored on function return type");
4242 type = c_build_qualified_type (type, type_quals);
4244 type_quals = TYPE_UNQUALIFIED;
4246 type = build_function_type (type, arg_types);
4247 declarator = declarator->declarator;
4249 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4250 the formal parameter list of this FUNCTION_TYPE to point to
4251 the FUNCTION_TYPE node itself. */
4253 tree link;
4255 for (link = arg_info->tags;
4256 link;
4257 link = TREE_CHAIN (link))
4258 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4260 break;
4262 case cdk_pointer:
4264 /* Merge any constancy or volatility into the target type
4265 for the pointer. */
4267 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4268 && type_quals)
4269 pedwarn ("ISO C forbids qualified function types");
4270 if (type_quals)
4271 type = c_build_qualified_type (type, type_quals);
4272 size_varies = 0;
4274 type = build_pointer_type (type);
4276 /* Process type qualifiers (such as const or volatile)
4277 that were given inside the `*'. */
4278 type_quals = declarator->u.pointer_quals;
4280 declarator = declarator->declarator;
4281 break;
4283 default:
4284 gcc_unreachable ();
4288 /* Now TYPE has the actual type, apart from any qualifiers in
4289 TYPE_QUALS. */
4291 /* Check the type and width of a bit-field. */
4292 if (bitfield)
4293 check_bitfield_type_and_width (&type, width, orig_name);
4295 /* Did array size calculations overflow? */
4297 if (TREE_CODE (type) == ARRAY_TYPE
4298 && COMPLETE_TYPE_P (type)
4299 && TREE_OVERFLOW (TYPE_SIZE (type)))
4301 error ("size of array %qs is too large", name);
4302 /* If we proceed with the array type as it is, we'll eventually
4303 crash in tree_low_cst(). */
4304 type = error_mark_node;
4307 /* If this is declaring a typedef name, return a TYPE_DECL. */
4309 if (storage_class == csc_typedef)
4311 tree decl;
4312 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4313 && type_quals)
4314 pedwarn ("ISO C forbids qualified function types");
4315 if (type_quals)
4316 type = c_build_qualified_type (type, type_quals);
4317 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4318 if (declspecs->explicit_signed_p)
4319 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4320 decl_attributes (&decl, returned_attrs, 0);
4321 if (declspecs->inline_p)
4322 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4323 return decl;
4326 /* Detect the case of an array type of unspecified size
4327 which came, as such, direct from a typedef name.
4328 We must copy the type, so that each identifier gets
4329 a distinct type, so that each identifier's size can be
4330 controlled separately by its own initializer. */
4332 if (type != 0 && typedef_type != 0
4333 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4334 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4336 type = build_array_type (TREE_TYPE (type), 0);
4337 if (size_varies)
4338 C_TYPE_VARIABLE_SIZE (type) = 1;
4341 /* If this is a type name (such as, in a cast or sizeof),
4342 compute the type and return it now. */
4344 if (decl_context == TYPENAME)
4346 /* Note that the grammar rejects storage classes in typenames
4347 and fields. */
4348 gcc_assert (storage_class == csc_none && !threadp
4349 && !declspecs->inline_p);
4350 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4351 && type_quals)
4352 pedwarn ("ISO C forbids const or volatile function types");
4353 if (type_quals)
4354 type = c_build_qualified_type (type, type_quals);
4355 decl_attributes (&type, returned_attrs, 0);
4356 return type;
4359 /* Aside from typedefs and type names (handle above),
4360 `void' at top level (not within pointer)
4361 is allowed only in public variables.
4362 We don't complain about parms either, but that is because
4363 a better error message can be made later. */
4365 if (VOID_TYPE_P (type) && decl_context != PARM
4366 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4367 && (storage_class == csc_extern
4368 || (current_scope == file_scope
4369 && !(storage_class == csc_static
4370 || storage_class == csc_register)))))
4372 error ("variable or field %qs declared void", name);
4373 type = integer_type_node;
4376 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4377 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4380 tree decl;
4382 if (decl_context == PARM)
4384 tree type_as_written;
4385 tree promoted_type;
4387 /* A parameter declared as an array of T is really a pointer to T.
4388 One declared as a function is really a pointer to a function. */
4390 if (TREE_CODE (type) == ARRAY_TYPE)
4392 /* Transfer const-ness of array into that of type pointed to. */
4393 type = TREE_TYPE (type);
4394 if (type_quals)
4395 type = c_build_qualified_type (type, type_quals);
4396 type = build_pointer_type (type);
4397 type_quals = array_ptr_quals;
4399 /* We don't yet implement attributes in this context. */
4400 if (array_ptr_attrs != NULL_TREE)
4401 warning ("attributes in parameter array declarator ignored");
4403 size_varies = 0;
4405 else if (TREE_CODE (type) == FUNCTION_TYPE)
4407 if (pedantic && type_quals)
4408 pedwarn ("ISO C forbids qualified function types");
4409 if (type_quals)
4410 type = c_build_qualified_type (type, type_quals);
4411 type = build_pointer_type (type);
4412 type_quals = TYPE_UNQUALIFIED;
4414 else if (type_quals)
4415 type = c_build_qualified_type (type, type_quals);
4417 type_as_written = type;
4419 decl = build_decl (PARM_DECL, declarator->u.id, type);
4420 if (size_varies)
4421 C_DECL_VARIABLE_SIZE (decl) = 1;
4423 /* Compute the type actually passed in the parmlist,
4424 for the case where there is no prototype.
4425 (For example, shorts and chars are passed as ints.)
4426 When there is a prototype, this is overridden later. */
4428 if (type == error_mark_node)
4429 promoted_type = type;
4430 else
4431 promoted_type = c_type_promotes_to (type);
4433 DECL_ARG_TYPE (decl) = promoted_type;
4434 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4435 if (declspecs->inline_p)
4436 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4438 else if (decl_context == FIELD)
4440 /* Note that the grammar rejects storage classes in typenames
4441 and fields. */
4442 gcc_assert (storage_class == csc_none && !threadp
4443 && !declspecs->inline_p);
4445 /* Structure field. It may not be a function. */
4447 if (TREE_CODE (type) == FUNCTION_TYPE)
4449 error ("field %qs declared as a function", name);
4450 type = build_pointer_type (type);
4452 else if (TREE_CODE (type) != ERROR_MARK
4453 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4455 error ("field %qs has incomplete type", name);
4456 type = error_mark_node;
4458 type = c_build_qualified_type (type, type_quals);
4459 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4460 DECL_NONADDRESSABLE_P (decl) = bitfield;
4462 if (size_varies)
4463 C_DECL_VARIABLE_SIZE (decl) = 1;
4465 else if (TREE_CODE (type) == FUNCTION_TYPE)
4467 if (storage_class == csc_register || threadp)
4469 error ("invalid storage class for function %qs", name);
4471 else if (current_scope != file_scope)
4473 /* Function declaration not at file scope. Storage
4474 classes other than `extern' are not allowed, C99
4475 6.7.1p5, and `extern' makes no difference. However,
4476 GCC allows 'auto', perhaps with 'inline', to support
4477 nested functions. */
4478 if (storage_class == csc_auto)
4480 if (pedantic)
4481 pedwarn ("invalid storage class for function %qs", name);
4483 else if (storage_class == csc_static)
4485 error ("invalid storage class for function %qs", name);
4486 if (funcdef_flag)
4487 storage_class = declspecs->storage_class = csc_none;
4488 else
4489 return 0;
4493 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4494 decl = build_decl_attribute_variant (decl, decl_attr);
4496 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4498 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4499 pedwarn ("ISO C forbids qualified function types");
4501 /* GNU C interprets a volatile-qualified function type to indicate
4502 that the function does not return. */
4503 if ((type_quals & TYPE_QUAL_VOLATILE)
4504 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4505 warning ("%<noreturn%> function returns non-void value");
4507 /* Every function declaration is an external reference
4508 (DECL_EXTERNAL) except for those which are not at file
4509 scope and are explicitly declared "auto". This is
4510 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4511 GCC to signify a forward declaration of a nested function. */
4512 if (storage_class == csc_auto && current_scope != file_scope)
4513 DECL_EXTERNAL (decl) = 0;
4514 else
4515 DECL_EXTERNAL (decl) = 1;
4517 /* Record absence of global scope for `static' or `auto'. */
4518 TREE_PUBLIC (decl)
4519 = !(storage_class == csc_static || storage_class == csc_auto);
4521 /* For a function definition, record the argument information
4522 block where store_parm_decls will look for it. */
4523 if (funcdef_flag)
4524 current_function_arg_info = arg_info;
4526 if (declspecs->default_int_p)
4527 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4529 /* Record presence of `inline', if it is reasonable. */
4530 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4532 if (declspecs->inline_p)
4533 pedwarn ("cannot inline function %<main%>");
4535 else if (declspecs->inline_p)
4537 /* Record that the function is declared `inline'. */
4538 DECL_DECLARED_INLINE_P (decl) = 1;
4540 /* Do not mark bare declarations as DECL_INLINE. Doing so
4541 in the presence of multiple declarations can result in
4542 the abstract origin pointing between the declarations,
4543 which will confuse dwarf2out. */
4544 if (initialized)
4546 DECL_INLINE (decl) = 1;
4547 if (storage_class == csc_extern)
4548 current_extern_inline = 1;
4551 /* If -finline-functions, assume it can be inlined. This does
4552 two things: let the function be deferred until it is actually
4553 needed, and let dwarf2 know that the function is inlinable. */
4554 else if (flag_inline_trees == 2 && initialized)
4555 DECL_INLINE (decl) = 1;
4557 else
4559 /* It's a variable. */
4560 /* An uninitialized decl with `extern' is a reference. */
4561 int extern_ref = !initialized && storage_class == csc_extern;
4563 type = c_build_qualified_type (type, type_quals);
4565 /* C99 6.2.2p7: It is invalid (compile-time undefined
4566 behavior) to create an 'extern' declaration for a
4567 variable if there is a global declaration that is
4568 'static' and the global declaration is not visible.
4569 (If the static declaration _is_ currently visible,
4570 the 'extern' declaration is taken to refer to that decl.) */
4571 if (extern_ref && current_scope != file_scope)
4573 tree global_decl = identifier_global_value (declarator->u.id);
4574 tree visible_decl = lookup_name (declarator->u.id);
4576 if (global_decl
4577 && global_decl != visible_decl
4578 && TREE_CODE (global_decl) == VAR_DECL
4579 && !TREE_PUBLIC (global_decl))
4580 error ("variable previously declared %<static%> redeclared "
4581 "%<extern%>");
4584 decl = build_decl (VAR_DECL, declarator->u.id, type);
4585 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4586 if (size_varies)
4587 C_DECL_VARIABLE_SIZE (decl) = 1;
4589 if (declspecs->inline_p)
4590 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4592 /* At file scope, an initialized extern declaration may follow
4593 a static declaration. In that case, DECL_EXTERNAL will be
4594 reset later in start_decl. */
4595 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4597 /* At file scope, the presence of a `static' or `register' storage
4598 class specifier, or the absence of all storage class specifiers
4599 makes this declaration a definition (perhaps tentative). Also,
4600 the absence of both `static' and `register' makes it public. */
4601 if (current_scope == file_scope)
4603 TREE_PUBLIC (decl) = !(storage_class == csc_static
4604 || storage_class == csc_register);
4605 TREE_STATIC (decl) = !extern_ref;
4607 /* Not at file scope, only `static' makes a static definition. */
4608 else
4610 TREE_STATIC (decl) = (storage_class == csc_static);
4611 TREE_PUBLIC (decl) = extern_ref;
4614 if (threadp)
4616 if (targetm.have_tls)
4617 DECL_THREAD_LOCAL (decl) = 1;
4618 else
4619 /* A mere warning is sure to result in improper semantics
4620 at runtime. Don't bother to allow this to compile. */
4621 error ("thread-local storage not supported for this target");
4625 /* Record `register' declaration for warnings on &
4626 and in case doing stupid register allocation. */
4628 if (storage_class == csc_register)
4630 C_DECL_REGISTER (decl) = 1;
4631 DECL_REGISTER (decl) = 1;
4634 /* Record constancy and volatility. */
4635 c_apply_type_quals_to_decl (type_quals, decl);
4637 /* If a type has volatile components, it should be stored in memory.
4638 Otherwise, the fact that those components are volatile
4639 will be ignored, and would even crash the compiler. */
4640 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4642 /* It is not an error for a structure with volatile fields to
4643 be declared register, but reset DECL_REGISTER since it
4644 cannot actually go in a register. */
4645 int was_reg = C_DECL_REGISTER (decl);
4646 C_DECL_REGISTER (decl) = 0;
4647 DECL_REGISTER (decl) = 0;
4648 c_mark_addressable (decl);
4649 C_DECL_REGISTER (decl) = was_reg;
4652 /* This is the earliest point at which we might know the assembler
4653 name of a variable. Thus, if it's known before this, die horribly. */
4654 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4656 decl_attributes (&decl, returned_attrs, 0);
4658 return decl;
4662 /* Decode the parameter-list info for a function type or function definition.
4663 The argument is the value returned by `get_parm_info' (or made in parse.y
4664 if there is an identifier list instead of a parameter decl list).
4665 These two functions are separate because when a function returns
4666 or receives functions then each is called multiple times but the order
4667 of calls is different. The last call to `grokparms' is always the one
4668 that contains the formal parameter names of a function definition.
4670 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4672 FUNCDEF_FLAG is true for a function definition, false for
4673 a mere declaration. A nonempty identifier-list gets an error message
4674 when FUNCDEF_FLAG is false. */
4676 static tree
4677 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4679 tree arg_types = arg_info->types;
4681 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4682 && !in_system_header)
4683 warning ("function declaration isn%'t a prototype");
4685 if (arg_types == error_mark_node)
4686 return 0; /* don't set TYPE_ARG_TYPES in this case */
4688 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4690 if (!funcdef_flag)
4691 pedwarn ("parameter names (without types) in function declaration");
4693 arg_info->parms = arg_info->types;
4694 arg_info->types = 0;
4695 return 0;
4697 else
4699 tree parm, type, typelt;
4700 unsigned int parmno;
4702 /* If there is a parameter of incomplete type in a definition,
4703 this is an error. In a declaration this is valid, and a
4704 struct or union type may be completed later, before any calls
4705 or definition of the function. In the case where the tag was
4706 first declared within the parameter list, a warning has
4707 already been given. If a parameter has void type, then
4708 however the function cannot be defined or called, so
4709 warn. */
4711 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4712 parm;
4713 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4715 type = TREE_VALUE (typelt);
4716 if (type == error_mark_node)
4717 continue;
4719 if (!COMPLETE_TYPE_P (type))
4721 if (funcdef_flag)
4723 if (DECL_NAME (parm))
4724 error ("%Jparameter %u (%qD) has incomplete type",
4725 parm, parmno, parm);
4726 else
4727 error ("%Jparameter %u has incomplete type",
4728 parm, parmno);
4730 TREE_VALUE (typelt) = error_mark_node;
4731 TREE_TYPE (parm) = error_mark_node;
4733 else if (VOID_TYPE_P (type))
4735 if (DECL_NAME (parm))
4736 warning ("%Jparameter %u (%qD) has void type",
4737 parm, parmno, parm);
4738 else
4739 warning ("%Jparameter %u has void type",
4740 parm, parmno);
4744 return arg_types;
4748 /* Take apart the current scope and return a c_arg_info structure with
4749 info on a parameter list just parsed.
4751 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4753 ELLIPSIS being true means the argument list ended in '...' so don't
4754 append a sentinel (void_list_node) to the end of the type-list. */
4756 struct c_arg_info *
4757 get_parm_info (bool ellipsis)
4759 struct c_binding *b = current_scope->bindings;
4760 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4761 struct c_arg_info);
4762 tree parms = 0;
4763 tree tags = 0;
4764 tree types = 0;
4765 tree others = 0;
4767 static bool explained_incomplete_types = false;
4768 bool gave_void_only_once_err = false;
4770 arg_info->parms = 0;
4771 arg_info->tags = 0;
4772 arg_info->types = 0;
4773 arg_info->others = 0;
4775 /* The bindings in this scope must not get put into a block.
4776 We will take care of deleting the binding nodes. */
4777 current_scope->bindings = 0;
4779 /* This function is only called if there was *something* on the
4780 parameter list. */
4781 gcc_assert (b);
4783 /* A parameter list consisting solely of 'void' indicates that the
4784 function takes no arguments. But if the 'void' is qualified
4785 (by 'const' or 'volatile'), or has a storage class specifier
4786 ('register'), then the behavior is undefined; issue an error.
4787 Typedefs for 'void' are OK (see DR#157). */
4788 if (b->prev == 0 /* one binding */
4789 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4790 && !DECL_NAME (b->decl) /* anonymous */
4791 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4793 if (TREE_THIS_VOLATILE (b->decl)
4794 || TREE_READONLY (b->decl)
4795 || C_DECL_REGISTER (b->decl))
4796 error ("%<void%> as only parameter may not be qualified");
4798 /* There cannot be an ellipsis. */
4799 if (ellipsis)
4800 error ("%<void%> must be the only parameter");
4802 arg_info->types = void_list_node;
4803 return arg_info;
4806 if (!ellipsis)
4807 types = void_list_node;
4809 /* Break up the bindings list into parms, tags, types, and others;
4810 apply sanity checks; purge the name-to-decl bindings. */
4811 while (b)
4813 tree decl = b->decl;
4814 tree type = TREE_TYPE (decl);
4815 const char *keyword;
4817 switch (TREE_CODE (decl))
4819 case PARM_DECL:
4820 if (b->id)
4822 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4823 I_SYMBOL_BINDING (b->id) = b->shadowed;
4826 /* Check for forward decls that never got their actual decl. */
4827 if (TREE_ASM_WRITTEN (decl))
4828 error ("%Jparameter %qD has just a forward declaration",
4829 decl, decl);
4830 /* Check for (..., void, ...) and issue an error. */
4831 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4833 if (!gave_void_only_once_err)
4835 error ("%<void%> must be the only parameter");
4836 gave_void_only_once_err = true;
4839 else
4841 /* Valid parameter, add it to the list. */
4842 TREE_CHAIN (decl) = parms;
4843 parms = decl;
4845 /* Since there is a prototype, args are passed in their
4846 declared types. The back end may override this later. */
4847 DECL_ARG_TYPE (decl) = type;
4848 types = tree_cons (0, type, types);
4850 break;
4852 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4853 case UNION_TYPE: keyword = "union"; goto tag;
4854 case RECORD_TYPE: keyword = "struct"; goto tag;
4855 tag:
4856 /* Types may not have tag-names, in which case the type
4857 appears in the bindings list with b->id NULL. */
4858 if (b->id)
4860 gcc_assert (I_TAG_BINDING (b->id) == b);
4861 I_TAG_BINDING (b->id) = b->shadowed;
4864 /* Warn about any struct, union or enum tags defined in a
4865 parameter list. The scope of such types is limited to
4866 the parameter list, which is rarely if ever desirable
4867 (it's impossible to call such a function with type-
4868 correct arguments). An anonymous union parm type is
4869 meaningful as a GNU extension, so don't warn for that. */
4870 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4872 if (b->id)
4873 /* The %s will be one of 'struct', 'union', or 'enum'. */
4874 warning ("%<%s %E%> declared inside parameter list",
4875 keyword, b->id);
4876 else
4877 /* The %s will be one of 'struct', 'union', or 'enum'. */
4878 warning ("anonymous %s declared inside parameter list",
4879 keyword);
4881 if (!explained_incomplete_types)
4883 warning ("its scope is only this definition or declaration,"
4884 " which is probably not what you want");
4885 explained_incomplete_types = true;
4889 tags = tree_cons (b->id, decl, tags);
4890 break;
4892 case CONST_DECL:
4893 case TYPE_DECL:
4894 case FUNCTION_DECL:
4895 /* CONST_DECLs appear here when we have an embedded enum,
4896 and TYPE_DECLs appear here when we have an embedded struct
4897 or union. No warnings for this - we already warned about the
4898 type itself. FUNCTION_DECLs appear when there is an implicit
4899 function declaration in the parameter list. */
4901 TREE_CHAIN (decl) = others;
4902 others = decl;
4903 /* fall through */
4905 case ERROR_MARK:
4906 /* error_mark_node appears here when we have an undeclared
4907 variable. Just throw it away. */
4908 if (b->id)
4910 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4911 I_SYMBOL_BINDING (b->id) = b->shadowed;
4913 break;
4915 /* Other things that might be encountered. */
4916 case LABEL_DECL:
4917 case VAR_DECL:
4918 default:
4919 gcc_unreachable ();
4922 b = free_binding_and_advance (b);
4925 arg_info->parms = parms;
4926 arg_info->tags = tags;
4927 arg_info->types = types;
4928 arg_info->others = others;
4929 return arg_info;
4932 /* Get the struct, enum or union (CODE says which) with tag NAME.
4933 Define the tag as a forward-reference if it is not defined.
4934 Return a c_typespec structure for the type specifier. */
4936 struct c_typespec
4937 parser_xref_tag (enum tree_code code, tree name)
4939 struct c_typespec ret;
4940 /* If a cross reference is requested, look up the type
4941 already defined for this tag and return it. */
4943 tree ref = lookup_tag (code, name, 0);
4944 /* If this is the right type of tag, return what we found.
4945 (This reference will be shadowed by shadow_tag later if appropriate.)
4946 If this is the wrong type of tag, do not return it. If it was the
4947 wrong type in the same scope, we will have had an error
4948 message already; if in a different scope and declaring
4949 a name, pending_xref_error will give an error message; but if in a
4950 different scope and not declaring a name, this tag should
4951 shadow the previous declaration of a different type of tag, and
4952 this would not work properly if we return the reference found.
4953 (For example, with "struct foo" in an outer scope, "union foo;"
4954 must shadow that tag with a new one of union type.) */
4955 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4956 if (ref && TREE_CODE (ref) == code)
4958 ret.spec = ref;
4959 return ret;
4962 /* If no such tag is yet defined, create a forward-reference node
4963 and record it as the "definition".
4964 When a real declaration of this type is found,
4965 the forward-reference will be altered into a real type. */
4967 ref = make_node (code);
4968 if (code == ENUMERAL_TYPE)
4970 /* Give the type a default layout like unsigned int
4971 to avoid crashing if it does not get defined. */
4972 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4973 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4974 TYPE_USER_ALIGN (ref) = 0;
4975 TYPE_UNSIGNED (ref) = 1;
4976 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4977 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4978 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4981 pushtag (name, ref);
4983 ret.spec = ref;
4984 return ret;
4987 /* Get the struct, enum or union (CODE says which) with tag NAME.
4988 Define the tag as a forward-reference if it is not defined.
4989 Return a tree for the type. */
4991 tree
4992 xref_tag (enum tree_code code, tree name)
4994 return parser_xref_tag (code, name).spec;
4997 /* Make sure that the tag NAME is defined *in the current scope*
4998 at least as a forward reference.
4999 CODE says which kind of tag NAME ought to be. */
5001 tree
5002 start_struct (enum tree_code code, tree name)
5004 /* If there is already a tag defined at this scope
5005 (as a forward reference), just return it. */
5007 tree ref = 0;
5009 if (name != 0)
5010 ref = lookup_tag (code, name, 1);
5011 if (ref && TREE_CODE (ref) == code)
5013 if (TYPE_SIZE (ref))
5015 if (code == UNION_TYPE)
5016 error ("redefinition of %<union %s%>", IDENTIFIER_POINTER (name));
5017 else
5018 error ("redefinition of %<struct %s%>", IDENTIFIER_POINTER (name));
5020 else if (C_TYPE_BEING_DEFINED (ref))
5022 if (code == UNION_TYPE)
5023 error ("nested redefinition of %<union %s%>",
5024 IDENTIFIER_POINTER (name));
5025 else
5026 error ("nested redefinition of %<struct %s%>",
5027 IDENTIFIER_POINTER (name));
5030 else
5032 /* Otherwise create a forward-reference just so the tag is in scope. */
5034 ref = make_node (code);
5035 pushtag (name, ref);
5038 C_TYPE_BEING_DEFINED (ref) = 1;
5039 TYPE_PACKED (ref) = flag_pack_struct;
5040 return ref;
5043 /* Process the specs, declarator and width (NULL if omitted)
5044 of a structure component, returning a FIELD_DECL node.
5045 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5047 This is done during the parsing of the struct declaration.
5048 The FIELD_DECL nodes are chained together and the lot of them
5049 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5051 tree
5052 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5053 tree width)
5055 tree value;
5057 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5058 && width == NULL_TREE)
5060 /* This is an unnamed decl.
5062 If we have something of the form "union { list } ;" then this
5063 is the anonymous union extension. Similarly for struct.
5065 If this is something of the form "struct foo;", then
5066 If MS extensions are enabled, this is handled as an
5067 anonymous struct.
5068 Otherwise this is a forward declaration of a structure tag.
5070 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5071 If MS extensions are enabled and foo names a structure, then
5072 again this is an anonymous struct.
5073 Otherwise this is an error.
5075 Oh what a horrid tangled web we weave. I wonder if MS consciously
5076 took this from Plan 9 or if it was an accident of implementation
5077 that took root before someone noticed the bug... */
5079 tree type = declspecs->type;
5080 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5081 || TREE_CODE (type) == UNION_TYPE);
5082 bool ok = false;
5084 if (type_ok
5085 && (flag_ms_extensions || !declspecs->typedef_p))
5087 if (flag_ms_extensions)
5088 ok = true;
5089 else if (flag_iso)
5090 ok = false;
5091 else if (TYPE_NAME (type) == NULL)
5092 ok = true;
5093 else
5094 ok = false;
5096 if (!ok)
5098 pedwarn ("declaration does not declare anything");
5099 return NULL_TREE;
5101 if (pedantic)
5102 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5105 value = grokdeclarator (declarator, declspecs, FIELD, false,
5106 width ? &width : NULL);
5108 finish_decl (value, NULL_TREE, NULL_TREE);
5109 DECL_INITIAL (value) = width;
5111 return value;
5114 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5115 the list such that this does not present a problem later. */
5117 static void
5118 detect_field_duplicates (tree fieldlist)
5120 tree x, y;
5121 int timeout = 10;
5123 /* First, see if there are more than "a few" fields.
5124 This is trivially true if there are zero or one fields. */
5125 if (!fieldlist)
5126 return;
5127 x = TREE_CHAIN (fieldlist);
5128 if (!x)
5129 return;
5130 do {
5131 timeout--;
5132 x = TREE_CHAIN (x);
5133 } while (timeout > 0 && x);
5135 /* If there were "few" fields, avoid the overhead of allocating
5136 a hash table. Instead just do the nested traversal thing. */
5137 if (timeout > 0)
5139 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5140 if (DECL_NAME (x))
5142 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5143 if (DECL_NAME (y) == DECL_NAME (x))
5145 error ("%Jduplicate member %qD", x, x);
5146 DECL_NAME (x) = NULL_TREE;
5150 else
5152 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5153 void **slot;
5155 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5156 if ((y = DECL_NAME (x)) != 0)
5158 slot = htab_find_slot (htab, y, INSERT);
5159 if (*slot)
5161 error ("%Jduplicate member %qD", x, x);
5162 DECL_NAME (x) = NULL_TREE;
5164 *slot = y;
5167 htab_delete (htab);
5171 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5172 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5173 ATTRIBUTES are attributes to be applied to the structure. */
5175 tree
5176 finish_struct (tree t, tree fieldlist, tree attributes)
5178 tree x;
5179 bool toplevel = file_scope == current_scope;
5180 int saw_named_field;
5182 /* If this type was previously laid out as a forward reference,
5183 make sure we lay it out again. */
5185 TYPE_SIZE (t) = 0;
5187 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5189 if (pedantic)
5191 for (x = fieldlist; x; x = TREE_CHAIN (x))
5192 if (DECL_NAME (x) != 0)
5193 break;
5195 if (x == 0)
5197 if (TREE_CODE (t) == UNION_TYPE)
5199 if (fieldlist)
5200 pedwarn ("union has no named members");
5201 else
5202 pedwarn ("union has no members");
5204 else
5206 if (fieldlist)
5207 pedwarn ("struct has no named members");
5208 else
5209 pedwarn ("struct has no members");
5214 /* Install struct as DECL_CONTEXT of each field decl.
5215 Also process specified field sizes, found in the DECL_INITIAL,
5216 storing 0 there after the type has been changed to precision equal
5217 to its width, rather than the precision of the specified standard
5218 type. (Correct layout requires the original type to have been preserved
5219 until now.) */
5221 saw_named_field = 0;
5222 for (x = fieldlist; x; x = TREE_CHAIN (x))
5224 DECL_CONTEXT (x) = t;
5225 DECL_PACKED (x) |= TYPE_PACKED (t);
5227 /* If any field is const, the structure type is pseudo-const. */
5228 if (TREE_READONLY (x))
5229 C_TYPE_FIELDS_READONLY (t) = 1;
5230 else
5232 /* A field that is pseudo-const makes the structure likewise. */
5233 tree t1 = TREE_TYPE (x);
5234 while (TREE_CODE (t1) == ARRAY_TYPE)
5235 t1 = TREE_TYPE (t1);
5236 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5237 && C_TYPE_FIELDS_READONLY (t1))
5238 C_TYPE_FIELDS_READONLY (t) = 1;
5241 /* Any field that is volatile means variables of this type must be
5242 treated in some ways as volatile. */
5243 if (TREE_THIS_VOLATILE (x))
5244 C_TYPE_FIELDS_VOLATILE (t) = 1;
5246 /* Any field of nominal variable size implies structure is too. */
5247 if (C_DECL_VARIABLE_SIZE (x))
5248 C_TYPE_VARIABLE_SIZE (t) = 1;
5250 if (DECL_INITIAL (x))
5252 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5253 DECL_SIZE (x) = bitsize_int (width);
5254 DECL_BIT_FIELD (x) = 1;
5255 SET_DECL_C_BIT_FIELD (x);
5258 /* Detect flexible array member in an invalid context. */
5259 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5260 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5261 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5262 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5264 if (TREE_CODE (t) == UNION_TYPE)
5266 error ("%Jflexible array member in union", x);
5267 TREE_TYPE (x) = error_mark_node;
5269 else if (TREE_CHAIN (x) != NULL_TREE)
5271 error ("%Jflexible array member not at end of struct", x);
5272 TREE_TYPE (x) = error_mark_node;
5274 else if (!saw_named_field)
5276 error ("%Jflexible array member in otherwise empty struct", x);
5277 TREE_TYPE (x) = error_mark_node;
5281 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5282 && flexible_array_type_p (TREE_TYPE (x)))
5283 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5285 if (DECL_NAME (x))
5286 saw_named_field = 1;
5289 detect_field_duplicates (fieldlist);
5291 /* Now we have the nearly final fieldlist. Record it,
5292 then lay out the structure or union (including the fields). */
5294 TYPE_FIELDS (t) = fieldlist;
5296 layout_type (t);
5298 /* Give bit-fields their proper types. */
5300 tree *fieldlistp = &fieldlist;
5301 while (*fieldlistp)
5302 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5303 && TREE_TYPE (*fieldlistp) != error_mark_node)
5305 unsigned HOST_WIDE_INT width
5306 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5307 tree type = TREE_TYPE (*fieldlistp);
5308 if (width != TYPE_PRECISION (type))
5310 TREE_TYPE (*fieldlistp)
5311 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5312 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5314 DECL_INITIAL (*fieldlistp) = 0;
5316 else
5317 fieldlistp = &TREE_CHAIN (*fieldlistp);
5320 /* Now we have the truly final field list.
5321 Store it in this type and in the variants. */
5323 TYPE_FIELDS (t) = fieldlist;
5325 /* If there are lots of fields, sort so we can look through them fast.
5326 We arbitrarily consider 16 or more elts to be "a lot". */
5329 int len = 0;
5331 for (x = fieldlist; x; x = TREE_CHAIN (x))
5333 if (len > 15 || DECL_NAME (x) == NULL)
5334 break;
5335 len += 1;
5338 if (len > 15)
5340 tree *field_array;
5341 struct lang_type *space;
5342 struct sorted_fields_type *space2;
5344 len += list_length (x);
5346 /* Use the same allocation policy here that make_node uses, to
5347 ensure that this lives as long as the rest of the struct decl.
5348 All decls in an inline function need to be saved. */
5350 space = GGC_CNEW (struct lang_type);
5351 space2 = GGC_NEWVAR (struct sorted_fields_type,
5352 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5354 len = 0;
5355 space->s = space2;
5356 field_array = &space2->elts[0];
5357 for (x = fieldlist; x; x = TREE_CHAIN (x))
5359 field_array[len++] = x;
5361 /* If there is anonymous struct or union, break out of the loop. */
5362 if (DECL_NAME (x) == NULL)
5363 break;
5365 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5366 if (x == NULL)
5368 TYPE_LANG_SPECIFIC (t) = space;
5369 TYPE_LANG_SPECIFIC (t)->s->len = len;
5370 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5371 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5376 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5378 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5379 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5380 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5381 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5384 /* If this was supposed to be a transparent union, but we can't
5385 make it one, warn and turn off the flag. */
5386 if (TREE_CODE (t) == UNION_TYPE
5387 && TYPE_TRANSPARENT_UNION (t)
5388 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5390 TYPE_TRANSPARENT_UNION (t) = 0;
5391 warning ("union cannot be made transparent");
5394 /* If this structure or union completes the type of any previous
5395 variable declaration, lay it out and output its rtl. */
5396 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5398 x = TREE_CHAIN (x))
5400 tree decl = TREE_VALUE (x);
5401 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5402 layout_array_type (TREE_TYPE (decl));
5403 if (TREE_CODE (decl) != TYPE_DECL)
5405 layout_decl (decl, 0);
5406 if (c_dialect_objc ())
5407 objc_check_decl (decl);
5408 rest_of_decl_compilation (decl, toplevel, 0);
5409 if (!toplevel)
5410 expand_decl (decl);
5413 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5415 /* Finish debugging output for this type. */
5416 rest_of_type_compilation (t, toplevel);
5418 /* If we're inside a function proper, i.e. not file-scope and not still
5419 parsing parameters, then arrange for the size of a variable sized type
5420 to be bound now. */
5421 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5422 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5424 return t;
5427 /* Lay out the type T, and its element type, and so on. */
5429 static void
5430 layout_array_type (tree t)
5432 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5433 layout_array_type (TREE_TYPE (t));
5434 layout_type (t);
5437 /* Begin compiling the definition of an enumeration type.
5438 NAME is its name (or null if anonymous).
5439 Returns the type object, as yet incomplete.
5440 Also records info about it so that build_enumerator
5441 may be used to declare the individual values as they are read. */
5443 tree
5444 start_enum (tree name)
5446 tree enumtype = 0;
5448 /* If this is the real definition for a previous forward reference,
5449 fill in the contents in the same object that used to be the
5450 forward reference. */
5452 if (name != 0)
5453 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5455 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5457 enumtype = make_node (ENUMERAL_TYPE);
5458 pushtag (name, enumtype);
5461 if (C_TYPE_BEING_DEFINED (enumtype))
5462 error ("nested redefinition of %<enum %s%>", IDENTIFIER_POINTER (name));
5464 C_TYPE_BEING_DEFINED (enumtype) = 1;
5466 if (TYPE_VALUES (enumtype) != 0)
5468 /* This enum is a named one that has been declared already. */
5469 error ("redeclaration of %<enum %s%>", IDENTIFIER_POINTER (name));
5471 /* Completely replace its old definition.
5472 The old enumerators remain defined, however. */
5473 TYPE_VALUES (enumtype) = 0;
5476 enum_next_value = integer_zero_node;
5477 enum_overflow = 0;
5479 if (flag_short_enums)
5480 TYPE_PACKED (enumtype) = 1;
5482 return enumtype;
5485 /* After processing and defining all the values of an enumeration type,
5486 install their decls in the enumeration type and finish it off.
5487 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5488 and ATTRIBUTES are the specified attributes.
5489 Returns ENUMTYPE. */
5491 tree
5492 finish_enum (tree enumtype, tree values, tree attributes)
5494 tree pair, tem;
5495 tree minnode = 0, maxnode = 0;
5496 int precision, unsign;
5497 bool toplevel = (file_scope == current_scope);
5498 struct lang_type *lt;
5500 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5502 /* Calculate the maximum value of any enumerator in this type. */
5504 if (values == error_mark_node)
5505 minnode = maxnode = integer_zero_node;
5506 else
5508 minnode = maxnode = TREE_VALUE (values);
5509 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5511 tree value = TREE_VALUE (pair);
5512 if (tree_int_cst_lt (maxnode, value))
5513 maxnode = value;
5514 if (tree_int_cst_lt (value, minnode))
5515 minnode = value;
5519 /* Construct the final type of this enumeration. It is the same
5520 as one of the integral types - the narrowest one that fits, except
5521 that normally we only go as narrow as int - and signed iff any of
5522 the values are negative. */
5523 unsign = (tree_int_cst_sgn (minnode) >= 0);
5524 precision = MAX (min_precision (minnode, unsign),
5525 min_precision (maxnode, unsign));
5527 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5529 tem = c_common_type_for_size (precision, unsign);
5530 if (tem == NULL)
5532 warning ("enumeration values exceed range of largest integer");
5533 tem = long_long_integer_type_node;
5536 else
5537 tem = unsign ? unsigned_type_node : integer_type_node;
5539 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5540 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5541 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5542 TYPE_SIZE (enumtype) = 0;
5544 /* If the precision of the type was specific with an attribute and it
5545 was too small, give an error. Otherwise, use it. */
5546 if (TYPE_PRECISION (enumtype))
5548 if (precision > TYPE_PRECISION (enumtype))
5549 error ("specified mode too small for enumeral values");
5551 else
5552 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5554 layout_type (enumtype);
5556 if (values != error_mark_node)
5558 /* Change the type of the enumerators to be the enum type. We
5559 need to do this irrespective of the size of the enum, for
5560 proper type checking. Replace the DECL_INITIALs of the
5561 enumerators, and the value slots of the list, with copies
5562 that have the enum type; they cannot be modified in place
5563 because they may be shared (e.g. integer_zero_node) Finally,
5564 change the purpose slots to point to the names of the decls. */
5565 for (pair = values; pair; pair = TREE_CHAIN (pair))
5567 tree enu = TREE_PURPOSE (pair);
5568 tree ini = DECL_INITIAL (enu);
5570 TREE_TYPE (enu) = enumtype;
5572 /* The ISO C Standard mandates enumerators to have type int,
5573 even though the underlying type of an enum type is
5574 unspecified. Here we convert any enumerators that fit in
5575 an int to type int, to avoid promotions to unsigned types
5576 when comparing integers with enumerators that fit in the
5577 int range. When -pedantic is given, build_enumerator()
5578 would have already taken care of those that don't fit. */
5579 if (int_fits_type_p (ini, integer_type_node))
5580 tem = integer_type_node;
5581 else
5582 tem = enumtype;
5583 ini = convert (tem, ini);
5585 DECL_INITIAL (enu) = ini;
5586 TREE_PURPOSE (pair) = DECL_NAME (enu);
5587 TREE_VALUE (pair) = ini;
5590 TYPE_VALUES (enumtype) = values;
5593 /* Record the min/max values so that we can warn about bit-field
5594 enumerations that are too small for the values. */
5595 lt = GGC_CNEW (struct lang_type);
5596 lt->enum_min = minnode;
5597 lt->enum_max = maxnode;
5598 TYPE_LANG_SPECIFIC (enumtype) = lt;
5600 /* Fix up all variant types of this enum type. */
5601 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5603 if (tem == enumtype)
5604 continue;
5605 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5606 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5607 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5608 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5609 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5610 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5611 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5612 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5613 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5614 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5615 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5618 /* Finish debugging output for this type. */
5619 rest_of_type_compilation (enumtype, toplevel);
5621 return enumtype;
5624 /* Build and install a CONST_DECL for one value of the
5625 current enumeration type (one that was begun with start_enum).
5626 Return a tree-list containing the CONST_DECL and its value.
5627 Assignment of sequential values by default is handled here. */
5629 tree
5630 build_enumerator (tree name, tree value)
5632 tree decl, type;
5634 /* Validate and default VALUE. */
5636 /* Remove no-op casts from the value. */
5637 if (value)
5638 STRIP_TYPE_NOPS (value);
5640 if (value != 0)
5642 /* Don't issue more errors for error_mark_node (i.e. an
5643 undeclared identifier) - just ignore the value expression. */
5644 if (value == error_mark_node)
5645 value = 0;
5646 else if (TREE_CODE (value) != INTEGER_CST)
5648 error ("enumerator value for %qE is not an integer constant", name);
5649 value = 0;
5651 else
5653 value = default_conversion (value);
5654 constant_expression_warning (value);
5658 /* Default based on previous value. */
5659 /* It should no longer be possible to have NON_LVALUE_EXPR
5660 in the default. */
5661 if (value == 0)
5663 value = enum_next_value;
5664 if (enum_overflow)
5665 error ("overflow in enumeration values");
5668 if (pedantic && !int_fits_type_p (value, integer_type_node))
5670 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5671 /* XXX This causes -pedantic to change the meaning of the program.
5672 Remove? -zw 2004-03-15 */
5673 value = convert (integer_type_node, value);
5676 /* Set basis for default for next value. */
5677 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5678 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5680 /* Now create a declaration for the enum value name. */
5682 type = TREE_TYPE (value);
5683 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5684 TYPE_PRECISION (integer_type_node)),
5685 (TYPE_PRECISION (type)
5686 >= TYPE_PRECISION (integer_type_node)
5687 && TYPE_UNSIGNED (type)));
5689 decl = build_decl (CONST_DECL, name, type);
5690 DECL_INITIAL (decl) = convert (type, value);
5691 pushdecl (decl);
5693 return tree_cons (decl, value, NULL_TREE);
5697 /* Create the FUNCTION_DECL for a function definition.
5698 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5699 the declaration; they describe the function's name and the type it returns,
5700 but twisted together in a fashion that parallels the syntax of C.
5702 This function creates a binding context for the function body
5703 as well as setting up the FUNCTION_DECL in current_function_decl.
5705 Returns 1 on success. If the DECLARATOR is not suitable for a function
5706 (it defines a datum instead), we return 0, which tells
5707 yyparse to report a parse error. */
5710 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5711 tree attributes)
5713 tree decl1, old_decl;
5714 tree restype, resdecl;
5716 current_function_returns_value = 0; /* Assume, until we see it does. */
5717 current_function_returns_null = 0;
5718 current_function_returns_abnormally = 0;
5719 warn_about_return_type = 0;
5720 current_extern_inline = 0;
5721 c_switch_stack = NULL;
5723 /* Indicate no valid break/continue context by setting these variables
5724 to some non-null, non-label value. We'll notice and emit the proper
5725 error message in c_finish_bc_stmt. */
5726 c_break_label = c_cont_label = size_zero_node;
5728 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5730 /* If the declarator is not suitable for a function definition,
5731 cause a syntax error. */
5732 if (decl1 == 0)
5733 return 0;
5735 decl_attributes (&decl1, attributes, 0);
5737 if (DECL_DECLARED_INLINE_P (decl1)
5738 && DECL_UNINLINABLE (decl1)
5739 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5740 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5742 announce_function (decl1);
5744 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5746 error ("return type is an incomplete type");
5747 /* Make it return void instead. */
5748 TREE_TYPE (decl1)
5749 = build_function_type (void_type_node,
5750 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5753 if (warn_about_return_type)
5754 pedwarn_c99 ("return type defaults to %<int%>");
5756 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5757 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5758 DECL_INITIAL (decl1) = error_mark_node;
5760 /* If this definition isn't a prototype and we had a prototype declaration
5761 before, copy the arg type info from that prototype. */
5762 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5763 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5764 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5765 TREE_TYPE (TREE_TYPE (old_decl)))
5766 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5768 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5769 TREE_TYPE (decl1));
5770 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5773 /* Optionally warn of old-fashioned def with no previous prototype. */
5774 if (warn_strict_prototypes
5775 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5776 && C_DECL_ISNT_PROTOTYPE (old_decl))
5777 warning ("function declaration isn%'t a prototype");
5778 /* Optionally warn of any global def with no previous prototype. */
5779 else if (warn_missing_prototypes
5780 && TREE_PUBLIC (decl1)
5781 && !MAIN_NAME_P (DECL_NAME (decl1))
5782 && C_DECL_ISNT_PROTOTYPE (old_decl))
5783 warning ("%Jno previous prototype for %qD", decl1, decl1);
5784 /* Optionally warn of any def with no previous prototype
5785 if the function has already been used. */
5786 else if (warn_missing_prototypes
5787 && old_decl != 0 && TREE_USED (old_decl)
5788 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5789 warning ("%J%qD was used with no prototype before its definition",
5790 decl1, decl1);
5791 /* Optionally warn of any global def with no previous declaration. */
5792 else if (warn_missing_declarations
5793 && TREE_PUBLIC (decl1)
5794 && old_decl == 0
5795 && !MAIN_NAME_P (DECL_NAME (decl1)))
5796 warning ("%Jno previous declaration for %qD", decl1, decl1);
5797 /* Optionally warn of any def with no previous declaration
5798 if the function has already been used. */
5799 else if (warn_missing_declarations
5800 && old_decl != 0 && TREE_USED (old_decl)
5801 && C_DECL_IMPLICIT (old_decl))
5802 warning ("%J%qD was used with no declaration before its definition",
5803 decl1, decl1);
5805 /* This is a definition, not a reference.
5806 So normally clear DECL_EXTERNAL.
5807 However, `extern inline' acts like a declaration
5808 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5809 DECL_EXTERNAL (decl1) = current_extern_inline;
5811 /* This function exists in static storage.
5812 (This does not mean `static' in the C sense!) */
5813 TREE_STATIC (decl1) = 1;
5815 /* A nested function is not global. */
5816 if (current_function_decl != 0)
5817 TREE_PUBLIC (decl1) = 0;
5819 /* This is the earliest point at which we might know the assembler
5820 name of the function. Thus, if it's set before this, die horribly. */
5821 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5823 /* If #pragma weak was used, mark the decl weak now. */
5824 if (current_scope == file_scope)
5825 maybe_apply_pragma_weak (decl1);
5827 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5828 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5830 tree args;
5831 int argct = 0;
5833 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5834 != integer_type_node)
5835 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5837 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5838 args = TREE_CHAIN (args))
5840 tree type = args ? TREE_VALUE (args) : 0;
5842 if (type == void_type_node)
5843 break;
5845 ++argct;
5846 switch (argct)
5848 case 1:
5849 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5850 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5851 decl1, decl1);
5852 break;
5854 case 2:
5855 if (TREE_CODE (type) != POINTER_TYPE
5856 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5857 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5858 != char_type_node))
5859 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5860 decl1, decl1);
5861 break;
5863 case 3:
5864 if (TREE_CODE (type) != POINTER_TYPE
5865 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5866 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5867 != char_type_node))
5868 pedwarn ("%Jthird argument of %qD should probably be "
5869 "%<char **%>", decl1, decl1);
5870 break;
5874 /* It is intentional that this message does not mention the third
5875 argument because it's only mentioned in an appendix of the
5876 standard. */
5877 if (argct > 0 && (argct < 2 || argct > 3))
5878 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5880 if (!TREE_PUBLIC (decl1))
5881 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5884 /* Record the decl so that the function name is defined.
5885 If we already have a decl for this name, and it is a FUNCTION_DECL,
5886 use the old decl. */
5888 current_function_decl = pushdecl (decl1);
5890 push_scope ();
5891 declare_parm_level ();
5893 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5894 /* Promote the value to int before returning it. */
5895 if (c_promoting_integer_type_p (restype))
5897 /* It retains unsignedness if not really getting wider. */
5898 if (TYPE_UNSIGNED (restype)
5899 && (TYPE_PRECISION (restype)
5900 == TYPE_PRECISION (integer_type_node)))
5901 restype = unsigned_type_node;
5902 else
5903 restype = integer_type_node;
5906 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5907 DECL_ARTIFICIAL (resdecl) = 1;
5908 DECL_IGNORED_P (resdecl) = 1;
5909 DECL_RESULT (current_function_decl) = resdecl;
5911 start_fname_decls ();
5913 return 1;
5916 /* Subroutine of store_parm_decls which handles new-style function
5917 definitions (prototype format). The parms already have decls, so we
5918 need only record them as in effect and complain if any redundant
5919 old-style parm decls were written. */
5920 static void
5921 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5923 tree decl;
5925 if (current_scope->bindings)
5927 error ("%Jold-style parameter declarations in prototyped "
5928 "function definition", fndecl);
5930 /* Get rid of the old-style declarations. */
5931 pop_scope ();
5932 push_scope ();
5934 /* Don't issue this warning for nested functions, and don't issue this
5935 warning if we got here because ARG_INFO_TYPES was error_mark_node
5936 (this happens when a function definition has just an ellipsis in
5937 its parameter list). */
5938 else if (warn_traditional && !in_system_header && !current_function_scope
5939 && arg_info->types != error_mark_node)
5940 warning ("%Jtraditional C rejects ISO C style function definitions",
5941 fndecl);
5943 /* Now make all the parameter declarations visible in the function body.
5944 We can bypass most of the grunt work of pushdecl. */
5945 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5947 DECL_CONTEXT (decl) = current_function_decl;
5948 if (DECL_NAME (decl))
5949 bind (DECL_NAME (decl), decl, current_scope,
5950 /*invisible=*/false, /*nested=*/false);
5951 else
5952 error ("%Jparameter name omitted", decl);
5955 /* Record the parameter list in the function declaration. */
5956 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5958 /* Now make all the ancillary declarations visible, likewise. */
5959 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5961 DECL_CONTEXT (decl) = current_function_decl;
5962 if (DECL_NAME (decl))
5963 bind (DECL_NAME (decl), decl, current_scope,
5964 /*invisible=*/false, /*nested=*/false);
5967 /* And all the tag declarations. */
5968 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5969 if (TREE_PURPOSE (decl))
5970 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5971 /*invisible=*/false, /*nested=*/false);
5974 /* Subroutine of store_parm_decls which handles old-style function
5975 definitions (separate parameter list and declarations). */
5977 static void
5978 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5980 struct c_binding *b;
5981 tree parm, decl, last;
5982 tree parmids = arg_info->parms;
5984 /* We use DECL_WEAK as a flag to show which parameters have been
5985 seen already, since it is not used on PARM_DECL. */
5986 #ifdef ENABLE_CHECKING
5987 for (b = current_scope->bindings; b; b = b->prev)
5988 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5989 #endif
5991 if (warn_old_style_definition && !in_system_header)
5992 warning ("%Jold-style function definition", fndecl);
5994 /* Match each formal parameter name with its declaration. Save each
5995 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5996 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5998 if (TREE_VALUE (parm) == 0)
6000 error ("%Jparameter name missing from parameter list", fndecl);
6001 TREE_PURPOSE (parm) = 0;
6002 continue;
6005 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6006 if (b && B_IN_CURRENT_SCOPE (b))
6008 decl = b->decl;
6009 /* If we got something other than a PARM_DECL it is an error. */
6010 if (TREE_CODE (decl) != PARM_DECL)
6011 error ("%J%qD declared as a non-parameter", decl, decl);
6012 /* If the declaration is already marked, we have a duplicate
6013 name. Complain and ignore the duplicate. */
6014 else if (DECL_WEAK (decl))
6016 error ("%Jmultiple parameters named %qD", decl, decl);
6017 TREE_PURPOSE (parm) = 0;
6018 continue;
6020 /* If the declaration says "void", complain and turn it into
6021 an int. */
6022 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6024 error ("%Jparameter %qD declared with void type", decl, decl);
6025 TREE_TYPE (decl) = integer_type_node;
6026 DECL_ARG_TYPE (decl) = integer_type_node;
6027 layout_decl (decl, 0);
6030 /* If no declaration found, default to int. */
6031 else
6033 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6034 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6035 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6036 pushdecl (decl);
6038 if (flag_isoc99)
6039 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6040 else if (extra_warnings)
6041 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
6044 TREE_PURPOSE (parm) = decl;
6045 DECL_WEAK (decl) = 1;
6048 /* Now examine the parms chain for incomplete declarations
6049 and declarations with no corresponding names. */
6051 for (b = current_scope->bindings; b; b = b->prev)
6053 parm = b->decl;
6054 if (TREE_CODE (parm) != PARM_DECL)
6055 continue;
6057 if (TREE_TYPE (parm) != error_mark_node
6058 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6060 error ("%Jparameter %qD has incomplete type", parm, parm);
6061 TREE_TYPE (parm) = error_mark_node;
6064 if (!DECL_WEAK (parm))
6066 error ("%Jdeclaration for parameter %qD but no such parameter",
6067 parm, parm);
6069 /* Pretend the parameter was not missing.
6070 This gets us to a standard state and minimizes
6071 further error messages. */
6072 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6076 /* Chain the declarations together in the order of the list of
6077 names. Store that chain in the function decl, replacing the
6078 list of names. Update the current scope to match. */
6079 DECL_ARGUMENTS (fndecl) = 0;
6081 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6082 if (TREE_PURPOSE (parm))
6083 break;
6084 if (parm && TREE_PURPOSE (parm))
6086 last = TREE_PURPOSE (parm);
6087 DECL_ARGUMENTS (fndecl) = last;
6088 DECL_WEAK (last) = 0;
6090 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6091 if (TREE_PURPOSE (parm))
6093 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6094 last = TREE_PURPOSE (parm);
6095 DECL_WEAK (last) = 0;
6097 TREE_CHAIN (last) = 0;
6100 /* If there was a previous prototype,
6101 set the DECL_ARG_TYPE of each argument according to
6102 the type previously specified, and report any mismatches. */
6104 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6106 tree type;
6107 for (parm = DECL_ARGUMENTS (fndecl),
6108 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6109 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6110 != void_type_node));
6111 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6113 if (parm == 0 || type == 0
6114 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6116 error ("number of arguments doesn%'t match prototype");
6117 error ("%Hprototype declaration",
6118 &current_function_prototype_locus);
6119 break;
6121 /* Type for passing arg must be consistent with that
6122 declared for the arg. ISO C says we take the unqualified
6123 type for parameters declared with qualified type. */
6124 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6125 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6127 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6128 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6130 /* Adjust argument to match prototype. E.g. a previous
6131 `int foo(float);' prototype causes
6132 `int foo(x) float x; {...}' to be treated like
6133 `int foo(float x) {...}'. This is particularly
6134 useful for argument types like uid_t. */
6135 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6137 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6138 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6139 && TYPE_PRECISION (TREE_TYPE (parm))
6140 < TYPE_PRECISION (integer_type_node))
6141 DECL_ARG_TYPE (parm) = integer_type_node;
6143 if (pedantic)
6145 pedwarn ("promoted argument %qD "
6146 "doesn%'t match prototype", parm);
6147 pedwarn ("%Hprototype declaration",
6148 &current_function_prototype_locus);
6151 else
6153 error ("argument %qD doesn%'t match prototype", parm);
6154 error ("%Hprototype declaration",
6155 &current_function_prototype_locus);
6159 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6162 /* Otherwise, create a prototype that would match. */
6164 else
6166 tree actual = 0, last = 0, type;
6168 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6170 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6171 if (last)
6172 TREE_CHAIN (last) = type;
6173 else
6174 actual = type;
6175 last = type;
6177 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6178 if (last)
6179 TREE_CHAIN (last) = type;
6180 else
6181 actual = type;
6183 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6184 of the type of this function, but we need to avoid having this
6185 affect the types of other similarly-typed functions, so we must
6186 first force the generation of an identical (but separate) type
6187 node for the relevant function type. The new node we create
6188 will be a variant of the main variant of the original function
6189 type. */
6191 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6193 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6197 /* Store parameter declarations passed in ARG_INFO into the current
6198 function declaration. */
6200 void
6201 store_parm_decls_from (struct c_arg_info *arg_info)
6203 current_function_arg_info = arg_info;
6204 store_parm_decls ();
6207 /* Store the parameter declarations into the current function declaration.
6208 This is called after parsing the parameter declarations, before
6209 digesting the body of the function.
6211 For an old-style definition, construct a prototype out of the old-style
6212 parameter declarations and inject it into the function's type. */
6214 void
6215 store_parm_decls (void)
6217 tree fndecl = current_function_decl;
6218 bool proto;
6220 /* The argument information block for FNDECL. */
6221 struct c_arg_info *arg_info = current_function_arg_info;
6222 current_function_arg_info = 0;
6224 /* True if this definition is written with a prototype. Note:
6225 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6226 list in a function definition as equivalent to (void) -- an
6227 empty argument list specifies the function has no parameters,
6228 but only (void) sets up a prototype for future calls. */
6229 proto = arg_info->types != 0;
6231 if (proto)
6232 store_parm_decls_newstyle (fndecl, arg_info);
6233 else
6234 store_parm_decls_oldstyle (fndecl, arg_info);
6236 /* The next call to push_scope will be a function body. */
6238 next_is_function_body = true;
6240 /* Write a record describing this function definition to the prototypes
6241 file (if requested). */
6243 gen_aux_info_record (fndecl, 1, 0, proto);
6245 /* Initialize the RTL code for the function. */
6246 allocate_struct_function (fndecl);
6248 /* Begin the statement tree for this function. */
6249 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6251 /* ??? Insert the contents of the pending sizes list into the function
6252 to be evaluated. The only reason left to have this is
6253 void foo(int n, int array[n++])
6254 because we throw away the array type in favor of a pointer type, and
6255 thus won't naturally see the SAVE_EXPR containing the increment. All
6256 other pending sizes would be handled by gimplify_parameters. */
6258 tree t;
6259 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6260 add_stmt (TREE_VALUE (t));
6263 /* Even though we're inside a function body, we still don't want to
6264 call expand_expr to calculate the size of a variable-sized array.
6265 We haven't necessarily assigned RTL to all variables yet, so it's
6266 not safe to try to expand expressions involving them. */
6267 cfun->x_dont_save_pending_sizes_p = 1;
6270 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6271 functions. */
6273 static void
6274 c_warn_unused_result_recursively (tree fndecl)
6276 struct cgraph_node *cgn;
6278 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6279 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6281 /* Finalize all nested functions now. */
6282 cgn = cgraph_node (fndecl);
6283 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6284 c_warn_unused_result_recursively (cgn->decl);
6287 /* Finish up a function declaration and compile that function
6288 all the way to assembler language output. The free the storage
6289 for the function definition.
6291 This is called after parsing the body of the function definition. */
6293 void
6294 finish_function (void)
6296 tree fndecl = current_function_decl;
6298 if (TREE_CODE (fndecl) == FUNCTION_DECL
6299 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6301 tree args = DECL_ARGUMENTS (fndecl);
6302 for (; args; args = TREE_CHAIN (args))
6304 tree type = TREE_TYPE (args);
6305 if (INTEGRAL_TYPE_P (type)
6306 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6307 DECL_ARG_TYPE (args) = integer_type_node;
6311 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6312 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6314 /* Must mark the RESULT_DECL as being in this function. */
6316 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6317 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6319 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6321 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6322 != integer_type_node)
6324 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6325 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6326 if (!warn_main)
6327 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6329 else
6331 if (flag_isoc99)
6333 tree stmt = c_finish_return (integer_zero_node);
6334 /* Hack. We don't want the middle-end to warn that this
6335 return is unreachable, so put the statement on the
6336 special line 0. */
6337 #ifdef USE_MAPPED_LOCATION
6338 SET_EXPR_LOCATION (stmt, UNKNOWN_LOCATION);
6339 #else
6340 annotate_with_file_line (stmt, input_filename, 0);
6341 #endif
6346 /* Tie off the statement tree for this function. */
6347 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6349 finish_fname_decls ();
6351 /* Complain if there's just no return statement. */
6352 if (warn_return_type
6353 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6354 && !current_function_returns_value && !current_function_returns_null
6355 /* Don't complain if we abort. */
6356 && !current_function_returns_abnormally
6357 /* Don't warn for main(). */
6358 && !MAIN_NAME_P (DECL_NAME (fndecl))
6359 /* Or if they didn't actually specify a return type. */
6360 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6361 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6362 inline function, as we might never be compiled separately. */
6363 && DECL_INLINE (fndecl))
6364 warning ("no return statement in function returning non-void");
6366 /* With just -Wextra, complain only if function returns both with
6367 and without a value. */
6368 if (extra_warnings
6369 && current_function_returns_value
6370 && current_function_returns_null)
6371 warning ("this function may return with or without a value");
6373 /* Store the end of the function, so that we get good line number
6374 info for the epilogue. */
6375 cfun->function_end_locus = input_location;
6377 /* If we don't have ctors/dtors sections, and this is a static
6378 constructor or destructor, it must be recorded now. */
6379 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6380 && !targetm.have_ctors_dtors)
6381 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6382 if (DECL_STATIC_DESTRUCTOR (fndecl)
6383 && !targetm.have_ctors_dtors)
6384 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6386 /* Finalize the ELF visibility for the function. */
6387 c_determine_visibility (fndecl);
6389 /* Genericize before inlining. Delay genericizing nested functions
6390 until their parent function is genericized. Since finalizing
6391 requires GENERIC, delay that as well. */
6393 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6394 && !undef_nested_function)
6396 if (!decl_function_context (fndecl))
6398 c_genericize (fndecl);
6399 c_warn_unused_result_recursively (fndecl);
6401 /* ??? Objc emits functions after finalizing the compilation unit.
6402 This should be cleaned up later and this conditional removed. */
6403 if (cgraph_global_info_ready)
6405 c_expand_body (fndecl);
6406 return;
6409 cgraph_finalize_function (fndecl, false);
6411 else
6413 /* Register this function with cgraph just far enough to get it
6414 added to our parent's nested function list. Handy, since the
6415 C front end doesn't have such a list. */
6416 (void) cgraph_node (fndecl);
6420 if (!decl_function_context (fndecl))
6421 undef_nested_function = false;
6423 /* We're leaving the context of this function, so zap cfun.
6424 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6425 tree_rest_of_compilation. */
6426 cfun = NULL;
6427 current_function_decl = NULL;
6430 /* Generate the RTL for the body of FNDECL. */
6432 void
6433 c_expand_body (tree fndecl)
6436 if (!DECL_INITIAL (fndecl)
6437 || DECL_INITIAL (fndecl) == error_mark_node)
6438 return;
6440 tree_rest_of_compilation (fndecl);
6442 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6443 && targetm.have_ctors_dtors)
6444 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6445 DEFAULT_INIT_PRIORITY);
6446 if (DECL_STATIC_DESTRUCTOR (fndecl)
6447 && targetm.have_ctors_dtors)
6448 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6449 DEFAULT_INIT_PRIORITY);
6452 /* Check the declarations given in a for-loop for satisfying the C99
6453 constraints. */
6454 void
6455 check_for_loop_decls (void)
6457 struct c_binding *b;
6459 if (!flag_isoc99)
6461 /* If we get here, declarations have been used in a for loop without
6462 the C99 for loop scope. This doesn't make much sense, so don't
6463 allow it. */
6464 error ("%<for%> loop initial declaration used outside C99 mode");
6465 return;
6467 /* C99 subclause 6.8.5 paragraph 3:
6469 [#3] The declaration part of a for statement shall only
6470 declare identifiers for objects having storage class auto or
6471 register.
6473 It isn't clear whether, in this sentence, "identifiers" binds to
6474 "shall only declare" or to "objects" - that is, whether all identifiers
6475 declared must be identifiers for objects, or whether the restriction
6476 only applies to those that are. (A question on this in comp.std.c
6477 in November 2000 received no answer.) We implement the strictest
6478 interpretation, to avoid creating an extension which later causes
6479 problems. */
6481 for (b = current_scope->bindings; b; b = b->prev)
6483 tree id = b->id;
6484 tree decl = b->decl;
6486 if (!id)
6487 continue;
6489 switch (TREE_CODE (decl))
6491 case VAR_DECL:
6492 if (TREE_STATIC (decl))
6493 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6494 "initial declaration", decl, decl);
6495 else if (DECL_EXTERNAL (decl))
6496 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6497 "initial declaration", decl, decl);
6498 break;
6500 case RECORD_TYPE:
6501 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6502 id);
6503 break;
6504 case UNION_TYPE:
6505 error ("%<union %E%> declared in %<for%> loop initial declaration",
6506 id);
6507 break;
6508 case ENUMERAL_TYPE:
6509 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6510 id);
6511 break;
6512 default:
6513 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6514 "initial declaration", decl, decl);
6519 /* Save and reinitialize the variables
6520 used during compilation of a C function. */
6522 void
6523 c_push_function_context (struct function *f)
6525 struct language_function *p;
6526 p = GGC_NEW (struct language_function);
6527 f->language = p;
6529 p->base.x_stmt_tree = c_stmt_tree;
6530 p->x_break_label = c_break_label;
6531 p->x_cont_label = c_cont_label;
6532 p->x_switch_stack = c_switch_stack;
6533 p->arg_info = current_function_arg_info;
6534 p->returns_value = current_function_returns_value;
6535 p->returns_null = current_function_returns_null;
6536 p->returns_abnormally = current_function_returns_abnormally;
6537 p->warn_about_return_type = warn_about_return_type;
6538 p->extern_inline = current_extern_inline;
6541 /* Restore the variables used during compilation of a C function. */
6543 void
6544 c_pop_function_context (struct function *f)
6546 struct language_function *p = f->language;
6548 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6549 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6551 /* Stop pointing to the local nodes about to be freed. */
6552 /* But DECL_INITIAL must remain nonzero so we know this
6553 was an actual function definition. */
6554 DECL_INITIAL (current_function_decl) = error_mark_node;
6555 DECL_ARGUMENTS (current_function_decl) = 0;
6558 c_stmt_tree = p->base.x_stmt_tree;
6559 c_break_label = p->x_break_label;
6560 c_cont_label = p->x_cont_label;
6561 c_switch_stack = p->x_switch_stack;
6562 current_function_arg_info = p->arg_info;
6563 current_function_returns_value = p->returns_value;
6564 current_function_returns_null = p->returns_null;
6565 current_function_returns_abnormally = p->returns_abnormally;
6566 warn_about_return_type = p->warn_about_return_type;
6567 current_extern_inline = p->extern_inline;
6569 f->language = NULL;
6572 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6574 void
6575 c_dup_lang_specific_decl (tree decl)
6577 struct lang_decl *ld;
6579 if (!DECL_LANG_SPECIFIC (decl))
6580 return;
6582 ld = GGC_NEW (struct lang_decl);
6583 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6584 DECL_LANG_SPECIFIC (decl) = ld;
6587 /* The functions below are required for functionality of doing
6588 function at once processing in the C front end. Currently these
6589 functions are not called from anywhere in the C front end, but as
6590 these changes continue, that will change. */
6592 /* Returns nonzero if the current statement is a full expression,
6593 i.e. temporaries created during that statement should be destroyed
6594 at the end of the statement. */
6597 stmts_are_full_exprs_p (void)
6599 return 0;
6602 /* Returns the stmt_tree (if any) to which statements are currently
6603 being added. If there is no active statement-tree, NULL is
6604 returned. */
6606 stmt_tree
6607 current_stmt_tree (void)
6609 return &c_stmt_tree;
6612 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6613 C. */
6616 anon_aggr_type_p (tree ARG_UNUSED (node))
6618 return 0;
6621 /* Return the global value of T as a symbol. */
6623 tree
6624 identifier_global_value (tree t)
6626 struct c_binding *b;
6628 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6629 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6630 return b->decl;
6632 return 0;
6635 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6636 otherwise the name is found in ridpointers from RID_INDEX. */
6638 void
6639 record_builtin_type (enum rid rid_index, const char *name, tree type)
6641 tree id, decl;
6642 if (name == 0)
6643 id = ridpointers[(int) rid_index];
6644 else
6645 id = get_identifier (name);
6646 decl = build_decl (TYPE_DECL, id, type);
6647 pushdecl (decl);
6648 if (debug_hooks->type_decl)
6649 debug_hooks->type_decl (decl, false);
6652 /* Build the void_list_node (void_type_node having been created). */
6653 tree
6654 build_void_list_node (void)
6656 tree t = build_tree_list (NULL_TREE, void_type_node);
6657 return t;
6660 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6662 struct c_parm *
6663 build_c_parm (struct c_declspecs *specs, tree attrs,
6664 struct c_declarator *declarator)
6666 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6667 ret->specs = specs;
6668 ret->attrs = attrs;
6669 ret->declarator = declarator;
6670 return ret;
6673 /* Return a declarator with nested attributes. TARGET is the inner
6674 declarator to which these attributes apply. ATTRS are the
6675 attributes. */
6677 struct c_declarator *
6678 build_attrs_declarator (tree attrs, struct c_declarator *target)
6680 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6681 ret->kind = cdk_attrs;
6682 ret->declarator = target;
6683 ret->u.attrs = attrs;
6684 return ret;
6687 /* Return a declarator for a function with arguments specified by ARGS
6688 and return type specified by TARGET. */
6690 struct c_declarator *
6691 build_function_declarator (struct c_arg_info *args,
6692 struct c_declarator *target)
6694 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6695 ret->kind = cdk_function;
6696 ret->declarator = target;
6697 ret->u.arg_info = args;
6698 return ret;
6701 /* Return a declarator for the identifier IDENT (which may be
6702 NULL_TREE for an abstract declarator). */
6704 struct c_declarator *
6705 build_id_declarator (tree ident)
6707 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6708 ret->kind = cdk_id;
6709 ret->declarator = 0;
6710 ret->u.id = ident;
6711 /* Default value - may get reset to a more precise location. */
6712 ret->id_loc = input_location;
6713 return ret;
6716 /* Return something to represent absolute declarators containing a *.
6717 TARGET is the absolute declarator that the * contains.
6718 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6719 to apply to the pointer type. */
6721 struct c_declarator *
6722 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6723 struct c_declarator *target)
6725 tree attrs;
6726 int quals = 0;
6727 struct c_declarator *itarget = target;
6728 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6729 if (type_quals_attrs)
6731 attrs = type_quals_attrs->attrs;
6732 quals = quals_from_declspecs (type_quals_attrs);
6733 if (attrs != NULL_TREE)
6734 itarget = build_attrs_declarator (attrs, target);
6736 ret->kind = cdk_pointer;
6737 ret->declarator = itarget;
6738 ret->u.pointer_quals = quals;
6739 return ret;
6742 /* Return a pointer to a structure for an empty list of declaration
6743 specifiers. */
6745 struct c_declspecs *
6746 build_null_declspecs (void)
6748 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6749 ret->type = 0;
6750 ret->decl_attr = 0;
6751 ret->attrs = 0;
6752 ret->typespec_word = cts_none;
6753 ret->storage_class = csc_none;
6754 ret->declspecs_seen_p = false;
6755 ret->type_seen_p = false;
6756 ret->non_sc_seen_p = false;
6757 ret->typedef_p = false;
6758 ret->tag_defined_p = false;
6759 ret->explicit_signed_p = false;
6760 ret->deprecated_p = false;
6761 ret->default_int_p = false;
6762 ret->long_p = false;
6763 ret->long_long_p = false;
6764 ret->short_p = false;
6765 ret->signed_p = false;
6766 ret->unsigned_p = false;
6767 ret->complex_p = false;
6768 ret->inline_p = false;
6769 ret->thread_p = false;
6770 ret->const_p = false;
6771 ret->volatile_p = false;
6772 ret->restrict_p = false;
6773 return ret;
6776 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6777 returning SPECS. */
6779 struct c_declspecs *
6780 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6782 enum rid i;
6783 bool dupe = false;
6784 specs->non_sc_seen_p = true;
6785 specs->declspecs_seen_p = true;
6786 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6787 && C_IS_RESERVED_WORD (qual));
6788 i = C_RID_CODE (qual);
6789 switch (i)
6791 case RID_CONST:
6792 dupe = specs->const_p;
6793 specs->const_p = true;
6794 break;
6795 case RID_VOLATILE:
6796 dupe = specs->volatile_p;
6797 specs->volatile_p = true;
6798 break;
6799 case RID_RESTRICT:
6800 dupe = specs->restrict_p;
6801 specs->restrict_p = true;
6802 break;
6803 default:
6804 gcc_unreachable ();
6806 if (dupe && pedantic && !flag_isoc99)
6807 pedwarn ("duplicate %qs", IDENTIFIER_POINTER (qual));
6808 return specs;
6811 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6812 returning SPECS. */
6814 struct c_declspecs *
6815 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6817 tree type = spec.spec;
6818 specs->non_sc_seen_p = true;
6819 specs->declspecs_seen_p = true;
6820 specs->type_seen_p = true;
6821 if (TREE_DEPRECATED (type))
6822 specs->deprecated_p = true;
6824 /* Handle type specifier keywords. */
6825 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6827 enum rid i = C_RID_CODE (type);
6828 if (specs->type)
6830 error ("two or more data types in declaration specifiers");
6831 return specs;
6833 if ((int) i <= (int) RID_LAST_MODIFIER)
6835 /* "long", "short", "signed", "unsigned" or "_Complex". */
6836 bool dupe = false;
6837 switch (i)
6839 case RID_LONG:
6840 if (specs->long_long_p)
6842 error ("%<long long long%> is too long for GCC");
6843 break;
6845 if (specs->long_p)
6847 if (specs->typespec_word == cts_double)
6849 error ("both %<long long%> and %<double%> in "
6850 "declaration specifiers");
6851 break;
6853 if (pedantic && !flag_isoc99 && !in_system_header
6854 && warn_long_long)
6855 pedwarn ("ISO C90 does not support %<long long%>");
6856 specs->long_long_p = 1;
6857 break;
6859 if (specs->short_p)
6860 error ("both %<long%> and %<short%> in "
6861 "declaration specifiers");
6862 else if (specs->typespec_word == cts_void)
6863 error ("both %<long%> and %<void%> in "
6864 "declaration specifiers");
6865 else if (specs->typespec_word == cts_bool)
6866 error ("both %<long%> and %<_Bool%> in "
6867 "declaration specifiers");
6868 else if (specs->typespec_word == cts_char)
6869 error ("both %<long%> and %<char%> in "
6870 "declaration specifiers");
6871 else if (specs->typespec_word == cts_float)
6872 error ("both %<long%> and %<float%> in "
6873 "declaration specifiers");
6874 else
6875 specs->long_p = true;
6876 break;
6877 case RID_SHORT:
6878 dupe = specs->short_p;
6879 if (specs->long_p)
6880 error ("both %<long%> and %<short%> in "
6881 "declaration specifiers");
6882 else if (specs->typespec_word == cts_void)
6883 error ("both %<short%> and %<void%> in "
6884 "declaration specifiers");
6885 else if (specs->typespec_word == cts_bool)
6886 error ("both %<short%> and %<_Bool%> in "
6887 "declaration specifiers");
6888 else if (specs->typespec_word == cts_char)
6889 error ("both %<short%> and %<char%> in "
6890 "declaration specifiers");
6891 else if (specs->typespec_word == cts_float)
6892 error ("both %<short%> and %<float%> in "
6893 "declaration specifiers");
6894 else if (specs->typespec_word == cts_double)
6895 error ("both %<short%> and %<double%> in "
6896 "declaration specifiers");
6897 else
6898 specs->short_p = true;
6899 break;
6900 case RID_SIGNED:
6901 dupe = specs->signed_p;
6902 if (specs->unsigned_p)
6903 error ("both %<signed%> and %<unsigned%> in "
6904 "declaration specifiers");
6905 else if (specs->typespec_word == cts_void)
6906 error ("both %<signed%> and %<void%> in "
6907 "declaration specifiers");
6908 else if (specs->typespec_word == cts_bool)
6909 error ("both %<signed%> and %<_Bool%> in "
6910 "declaration specifiers");
6911 else if (specs->typespec_word == cts_float)
6912 error ("both %<signed%> and %<float%> in "
6913 "declaration specifiers");
6914 else if (specs->typespec_word == cts_double)
6915 error ("both %<signed%> and %<double%> in "
6916 "declaration specifiers");
6917 else
6918 specs->signed_p = true;
6919 break;
6920 case RID_UNSIGNED:
6921 dupe = specs->unsigned_p;
6922 if (specs->signed_p)
6923 error ("both %<signed%> and %<unsigned%> in "
6924 "declaration specifiers");
6925 else if (specs->typespec_word == cts_void)
6926 error ("both %<unsigned%> and %<void%> in "
6927 "declaration specifiers");
6928 else if (specs->typespec_word == cts_bool)
6929 error ("both %<unsigned%> and %<_Bool%> in "
6930 "declaration specifiers");
6931 else if (specs->typespec_word == cts_float)
6932 error ("both %<unsigned%> and %<float%> in "
6933 "declaration specifiers");
6934 else if (specs->typespec_word == cts_double)
6935 error ("both %<unsigned%> and %<double%> in "
6936 "declaration specifiers");
6937 else
6938 specs->unsigned_p = true;
6939 break;
6940 case RID_COMPLEX:
6941 dupe = specs->complex_p;
6942 if (pedantic && !flag_isoc99 && !in_system_header)
6943 pedwarn ("ISO C90 does not support complex types");
6944 if (specs->typespec_word == cts_void)
6945 error ("both %<complex%> and %<void%> in "
6946 "declaration specifiers");
6947 else if (specs->typespec_word == cts_bool)
6948 error ("both %<complex%> and %<_Bool%> in "
6949 "declaration specifiers");
6950 else
6951 specs->complex_p = true;
6952 break;
6953 default:
6954 gcc_unreachable ();
6957 if (dupe)
6958 error ("duplicate %qs", IDENTIFIER_POINTER (type));
6960 return specs;
6962 else
6964 /* "void", "_Bool", "char", "int", "float" or "double". */
6965 if (specs->typespec_word != cts_none)
6967 error ("two or more data types in declaration specifiers");
6968 return specs;
6970 switch (i)
6972 case RID_VOID:
6973 if (specs->long_p)
6974 error ("both %<long%> and %<void%> in "
6975 "declaration specifiers");
6976 else if (specs->short_p)
6977 error ("both %<short%> and %<void%> in "
6978 "declaration specifiers");
6979 else if (specs->signed_p)
6980 error ("both %<signed%> and %<void%> in "
6981 "declaration specifiers");
6982 else if (specs->unsigned_p)
6983 error ("both %<unsigned%> and %<void%> in "
6984 "declaration specifiers");
6985 else if (specs->complex_p)
6986 error ("both %<complex%> and %<void%> in "
6987 "declaration specifiers");
6988 else
6989 specs->typespec_word = cts_void;
6990 return specs;
6991 case RID_BOOL:
6992 if (specs->long_p)
6993 error ("both %<long%> and %<_Bool%> in "
6994 "declaration specifiers");
6995 else if (specs->short_p)
6996 error ("both %<short%> and %<_Bool%> in "
6997 "declaration specifiers");
6998 else if (specs->signed_p)
6999 error ("both %<signed%> and %<_Bool%> in "
7000 "declaration specifiers");
7001 else if (specs->unsigned_p)
7002 error ("both %<unsigned%> and %<_Bool%> in "
7003 "declaration specifiers");
7004 else if (specs->complex_p)
7005 error ("both %<complex%> and %<_Bool%> in "
7006 "declaration specifiers");
7007 else
7008 specs->typespec_word = cts_bool;
7009 return specs;
7010 case RID_CHAR:
7011 if (specs->long_p)
7012 error ("both %<long%> and %<char%> in "
7013 "declaration specifiers");
7014 else if (specs->short_p)
7015 error ("both %<short%> and %<char%> in "
7016 "declaration specifiers");
7017 else
7018 specs->typespec_word = cts_char;
7019 return specs;
7020 case RID_INT:
7021 specs->typespec_word = cts_int;
7022 return specs;
7023 case RID_FLOAT:
7024 if (specs->long_p)
7025 error ("both %<long%> and %<float%> in "
7026 "declaration specifiers");
7027 else if (specs->short_p)
7028 error ("both %<short%> and %<float%> in "
7029 "declaration specifiers");
7030 else if (specs->signed_p)
7031 error ("both %<signed%> and %<float%> in "
7032 "declaration specifiers");
7033 else if (specs->unsigned_p)
7034 error ("both %<unsigned%> and %<float%> in "
7035 "declaration specifiers");
7036 else
7037 specs->typespec_word = cts_float;
7038 return specs;
7039 case RID_DOUBLE:
7040 if (specs->long_long_p)
7041 error ("both %<long long%> and %<double%> in "
7042 "declaration specifiers");
7043 else if (specs->short_p)
7044 error ("both %<short%> and %<double%> in "
7045 "declaration specifiers");
7046 else if (specs->signed_p)
7047 error ("both %<signed%> and %<double%> in "
7048 "declaration specifiers");
7049 else if (specs->unsigned_p)
7050 error ("both %<unsigned%> and %<double%> in "
7051 "declaration specifiers");
7052 else
7053 specs->typespec_word = cts_double;
7054 return specs;
7055 default:
7056 /* ObjC reserved word "id", handled below. */
7057 break;
7062 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7063 form of ObjC type, cases such as "int" and "long" being handled
7064 above), a TYPE (struct, union, enum and typeof specifiers) or an
7065 ERROR_MARK. In none of these cases may there have previously
7066 been any type specifiers. */
7067 if (specs->type || specs->typespec_word != cts_none
7068 || specs->long_p || specs->short_p || specs->signed_p
7069 || specs->unsigned_p || specs->complex_p)
7070 error ("two or more data types in declaration specifiers");
7071 else if (TREE_CODE (type) == TYPE_DECL)
7073 if (TREE_TYPE (type) == error_mark_node)
7074 ; /* Allow the type to default to int to avoid cascading errors. */
7075 else
7077 specs->type = TREE_TYPE (type);
7078 specs->decl_attr = DECL_ATTRIBUTES (type);
7079 specs->typedef_p = true;
7080 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7083 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7085 tree t = lookup_name (type);
7086 if (!t || TREE_CODE (t) != TYPE_DECL)
7087 error ("%qs fails to be a typedef or built in type",
7088 IDENTIFIER_POINTER (type));
7089 else if (TREE_TYPE (t) == error_mark_node)
7091 else
7092 specs->type = TREE_TYPE (t);
7094 else if (TREE_CODE (type) != ERROR_MARK)
7096 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7097 specs->tag_defined_p = true;
7098 if (spec.kind == ctsk_typeof)
7099 specs->typedef_p = true;
7100 specs->type = type;
7103 return specs;
7106 /* Add the storage class specifier or function specifier SCSPEC to the
7107 declaration specifiers SPECS, returning SPECS. */
7109 struct c_declspecs *
7110 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7112 enum rid i;
7113 enum c_storage_class n = csc_none;
7114 bool dupe = false;
7115 specs->declspecs_seen_p = true;
7116 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7117 && C_IS_RESERVED_WORD (scspec));
7118 i = C_RID_CODE (scspec);
7119 if (extra_warnings && specs->non_sc_seen_p)
7120 warning ("%qs is not at beginning of declaration",
7121 IDENTIFIER_POINTER (scspec));
7122 switch (i)
7124 case RID_INLINE:
7125 /* C99 permits duplicate inline. Although of doubtful utility,
7126 it seems simplest to permit it in gnu89 mode as well, as
7127 there is also little utility in maintaining this as a
7128 difference between gnu89 and C99 inline. */
7129 dupe = false;
7130 specs->inline_p = true;
7131 break;
7132 case RID_THREAD:
7133 dupe = specs->thread_p;
7134 if (specs->storage_class == csc_auto)
7135 error ("%<__thread%> used with %<auto%>");
7136 else if (specs->storage_class == csc_register)
7137 error ("%<__thread%> used with %<register%>");
7138 else if (specs->storage_class == csc_typedef)
7139 error ("%<__thread%> used with %<typedef%>");
7140 else
7141 specs->thread_p = true;
7142 break;
7143 case RID_AUTO:
7144 n = csc_auto;
7145 break;
7146 case RID_EXTERN:
7147 n = csc_extern;
7148 /* Diagnose "__thread extern". */
7149 if (specs->thread_p)
7150 error ("%<__thread%> before %<extern%>");
7151 break;
7152 case RID_REGISTER:
7153 n = csc_register;
7154 break;
7155 case RID_STATIC:
7156 n = csc_static;
7157 /* Diagnose "__thread static". */
7158 if (specs->thread_p)
7159 error ("%<__thread%> before %<static%>");
7160 break;
7161 case RID_TYPEDEF:
7162 n = csc_typedef;
7163 break;
7164 default:
7165 gcc_unreachable ();
7167 if (n != csc_none && n == specs->storage_class)
7168 dupe = true;
7169 if (dupe)
7170 error ("duplicate %qs", IDENTIFIER_POINTER (scspec));
7171 if (n != csc_none)
7173 if (specs->storage_class != csc_none && n != specs->storage_class)
7175 error ("multiple storage classes in declaration specifiers");
7177 else
7179 specs->storage_class = n;
7180 if (n != csc_extern && n != csc_static && specs->thread_p)
7182 error ("%<__thread%> used with %qs",
7183 IDENTIFIER_POINTER (scspec));
7184 specs->thread_p = false;
7188 return specs;
7191 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7192 returning SPECS. */
7194 struct c_declspecs *
7195 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7197 specs->attrs = chainon (attrs, specs->attrs);
7198 specs->declspecs_seen_p = true;
7199 return specs;
7202 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7203 specifiers with any other type specifier to determine the resulting
7204 type. This is where ISO C checks on complex types are made, since
7205 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7206 double". */
7208 struct c_declspecs *
7209 finish_declspecs (struct c_declspecs *specs)
7211 /* If a type was specified as a whole, we have no modifiers and are
7212 done. */
7213 if (specs->type != NULL_TREE)
7215 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7216 && !specs->signed_p && !specs->unsigned_p
7217 && !specs->complex_p);
7218 return specs;
7221 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7222 has been specified, treat it as "int" unless "_Complex" is
7223 present and there are no other specifiers. If we just have
7224 "_Complex", it is equivalent to "_Complex double", but e.g.
7225 "_Complex short" is equivalent to "_Complex short int". */
7226 if (specs->typespec_word == cts_none)
7228 if (specs->long_p || specs->short_p
7229 || specs->signed_p || specs->unsigned_p)
7231 specs->typespec_word = cts_int;
7233 else if (specs->complex_p)
7235 specs->typespec_word = cts_double;
7236 if (pedantic)
7237 pedwarn ("ISO C does not support plain %<complex%> meaning "
7238 "%<double complex%>");
7240 else
7242 specs->typespec_word = cts_int;
7243 specs->default_int_p = true;
7244 /* We don't diagnose this here because grokdeclarator will
7245 give more specific diagnostics according to whether it is
7246 a function definition. */
7250 /* If "signed" was specified, record this to distinguish "int" and
7251 "signed int" in the case of a bit-field with
7252 -funsigned-bitfields. */
7253 specs->explicit_signed_p = specs->signed_p;
7255 /* Now compute the actual type. */
7256 switch (specs->typespec_word)
7258 case cts_void:
7259 gcc_assert (!specs->long_p && !specs->short_p
7260 && !specs->signed_p && !specs->unsigned_p
7261 && !specs->complex_p);
7262 specs->type = void_type_node;
7263 break;
7264 case cts_bool:
7265 gcc_assert (!specs->long_p && !specs->short_p
7266 && !specs->signed_p && !specs->unsigned_p
7267 && !specs->complex_p);
7268 specs->type = boolean_type_node;
7269 break;
7270 case cts_char:
7271 gcc_assert (!specs->long_p && !specs->short_p);
7272 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7273 if (specs->signed_p)
7274 specs->type = signed_char_type_node;
7275 else if (specs->unsigned_p)
7276 specs->type = unsigned_char_type_node;
7277 else
7278 specs->type = char_type_node;
7279 if (specs->complex_p)
7281 if (pedantic)
7282 pedwarn ("ISO C does not support complex integer types");
7283 specs->type = build_complex_type (specs->type);
7285 break;
7286 case cts_int:
7287 gcc_assert (!(specs->long_p && specs->short_p));
7288 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7289 if (specs->long_long_p)
7290 specs->type = (specs->unsigned_p
7291 ? long_long_unsigned_type_node
7292 : long_long_integer_type_node);
7293 else if (specs->long_p)
7294 specs->type = (specs->unsigned_p
7295 ? long_unsigned_type_node
7296 : long_integer_type_node);
7297 else if (specs->short_p)
7298 specs->type = (specs->unsigned_p
7299 ? short_unsigned_type_node
7300 : short_integer_type_node);
7301 else
7302 specs->type = (specs->unsigned_p
7303 ? unsigned_type_node
7304 : integer_type_node);
7305 if (specs->complex_p)
7307 if (pedantic)
7308 pedwarn ("ISO C does not support complex integer types");
7309 specs->type = build_complex_type (specs->type);
7311 break;
7312 case cts_float:
7313 gcc_assert (!specs->long_p && !specs->short_p
7314 && !specs->signed_p && !specs->unsigned_p);
7315 specs->type = (specs->complex_p
7316 ? complex_float_type_node
7317 : float_type_node);
7318 break;
7319 case cts_double:
7320 gcc_assert (!specs->long_long_p && !specs->short_p
7321 && !specs->signed_p && !specs->unsigned_p);
7322 if (specs->long_p)
7324 specs->type = (specs->complex_p
7325 ? complex_long_double_type_node
7326 : long_double_type_node);
7328 else
7330 specs->type = (specs->complex_p
7331 ? complex_double_type_node
7332 : double_type_node);
7334 break;
7335 default:
7336 gcc_unreachable ();
7339 return specs;
7342 /* Synthesize a function which calls all the global ctors or global
7343 dtors in this file. This is only used for targets which do not
7344 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7345 static void
7346 build_cdtor (int method_type, tree cdtors)
7348 tree body = 0;
7350 if (!cdtors)
7351 return;
7353 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7354 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7355 &body);
7357 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7360 /* Perform final processing on one file scope's declarations (or the
7361 external scope's declarations), GLOBALS. */
7362 static void
7363 c_write_global_declarations_1 (tree globals)
7365 size_t len = list_length (globals);
7366 tree *vec = XNEWVEC (tree, len);
7367 size_t i;
7368 tree decl;
7370 /* Process the decls in the order they were written. */
7371 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7373 vec[i] = decl;
7374 /* Check for used but undefined static functions using the C
7375 standard's definition of "used", and set TREE_NO_WARNING so
7376 that check_global_declarations doesn't repeat the check. */
7377 if (TREE_CODE (decl) == FUNCTION_DECL
7378 && DECL_INITIAL (decl) == 0
7379 && DECL_EXTERNAL (decl)
7380 && !TREE_PUBLIC (decl)
7381 && C_DECL_USED (decl))
7383 pedwarn ("%J%qF used but never defined", decl, decl);
7384 TREE_NO_WARNING (decl) = 1;
7388 wrapup_global_declarations (vec, len);
7389 check_global_declarations (vec, len);
7391 free (vec);
7394 void
7395 c_write_global_declarations (void)
7397 tree ext_block, t;
7399 /* We don't want to do this if generating a PCH. */
7400 if (pch_file)
7401 return;
7403 /* Don't waste time on further processing if -fsyntax-only or we've
7404 encountered errors. */
7405 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7406 return;
7408 /* Close the external scope. */
7409 ext_block = pop_scope ();
7410 external_scope = 0;
7411 gcc_assert (!current_scope);
7413 /* Process all file scopes in this compilation, and the external_scope,
7414 through wrapup_global_declarations and check_global_declarations. */
7415 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7416 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7417 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7419 /* Generate functions to call static constructors and destructors
7420 for targets that do not support .ctors/.dtors sections. These
7421 functions have magic names which are detected by collect2. */
7422 build_cdtor ('I', static_ctors); static_ctors = 0;
7423 build_cdtor ('D', static_dtors); static_dtors = 0;
7425 /* We're done parsing; proceed to optimize and emit assembly.
7426 FIXME: shouldn't be the front end's responsibility to call this. */
7427 cgraph_optimize ();
7430 #include "gt-c-decl.h"