2004-09-23 H.J. Lu <hongjiu.lu@intel.com>
[official-gcc.git] / gcc / c-decl.c
blob98f1580ed5f6845f99f2631bd0e104fbe4110b91
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 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 "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "libfuncs.h"
60 #include "except.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 enum decl_context
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
74 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* True means we've initialized exception handling. */
80 bool c_eh_initialized_p;
82 /* While defining an enum type, this is 1 plus the last enumerator
83 constant value. Note that will do not have to save this or `enum_overflow'
84 around nested function definition since such a definition could only
85 occur in an enum value expression and we don't use these variables in
86 that case. */
88 static tree enum_next_value;
90 /* Nonzero means that there was overflow computing enum_next_value. */
92 static int enum_overflow;
94 /* The file and line that the prototype came from if this is an
95 old-style definition; used for diagnostics in
96 store_parm_decls_oldstyle. */
98 static location_t current_function_prototype_locus;
100 /* The argument information structure for the function currently being
101 defined. */
103 static struct c_arg_info *current_function_arg_info;
105 /* The obstack on which parser and related data structures, which are
106 not live beyond their top-level declaration or definition, are
107 allocated. */
108 struct obstack parser_obstack;
110 /* The current statement tree. */
112 static GTY(()) struct stmt_tree_s c_stmt_tree;
114 /* State saving variables. */
115 tree c_break_label;
116 tree c_cont_label;
118 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
119 included in this invocation. Note that the current translation
120 unit is not included in this list. */
122 static GTY(()) tree all_translation_units;
124 /* A list of decls to be made automatically visible in each file scope. */
125 static GTY(()) tree visible_builtins;
127 /* Set to 0 at beginning of a function definition, set to 1 if
128 a return statement that specifies a return value is seen. */
130 int current_function_returns_value;
132 /* Set to 0 at beginning of a function definition, set to 1 if
133 a return statement with no argument is seen. */
135 int current_function_returns_null;
137 /* Set to 0 at beginning of a function definition, set to 1 if
138 a call to a noreturn function is seen. */
140 int current_function_returns_abnormally;
142 /* Set to nonzero by `grokdeclarator' for a function
143 whose return type is defaulted, if warnings for this are desired. */
145 static int warn_about_return_type;
147 /* Nonzero when starting a function declared `extern inline'. */
149 static int current_extern_inline;
151 /* True means global_bindings_p should return false even if the scope stack
152 says we are in file scope. */
153 bool c_override_global_bindings_to_false;
156 /* Each c_binding structure describes one binding of an identifier to
157 a decl. All the decls in a scope - irrespective of namespace - are
158 chained together by the ->prev field, which (as the name implies)
159 runs in reverse order. All the decls in a given namespace bound to
160 a given identifier are chained by the ->shadowed field, which runs
161 from inner to outer scopes.
163 The ->decl field usually points to a DECL node, but there are two
164 exceptions. In the namespace of type tags, the bound entity is a
165 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
166 identifier is encountered, it is bound to error_mark_node to
167 suppress further errors about that identifier in the current
168 function.
170 The ->type field stores the type of the declaration in this scope;
171 if NULL, the type is the type of the ->decl field. This is only of
172 relevance for objects with external or internal linkage which may
173 be redeclared in inner scopes, forming composite types that only
174 persist for the duration of those scopes. In the external scope,
175 this stores the composite of all the types declared for this
176 object, visible or not. The ->inner_comp field (used only at file
177 scope) stores whether an incomplete array type at file scope was
178 completed at an inner scope to an array size other than 1.
180 The depth field is copied from the scope structure that holds this
181 decl. It is used to preserve the proper ordering of the ->shadowed
182 field (see bind()) and also for a handful of special-case checks.
183 Finally, the invisible bit is true for a decl which should be
184 ignored for purposes of normal name lookup, and the nested bit is
185 true for a decl that's been bound a second time in an inner scope;
186 in all such cases, the binding in the outer scope will have its
187 invisible bit true. */
189 struct c_binding GTY((chain_next ("%h.prev")))
191 tree decl; /* the decl bound */
192 tree type; /* the type in this scope */
193 tree id; /* the identifier it's bound to */
194 struct c_binding *prev; /* the previous decl in this scope */
195 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
196 unsigned int depth : 28; /* depth of this scope */
197 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
198 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
199 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
200 /* one free bit */
202 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
203 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
204 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
205 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
207 #define I_SYMBOL_BINDING(node) \
208 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
209 #define I_SYMBOL_DECL(node) \
210 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
212 #define I_TAG_BINDING(node) \
213 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
214 #define I_TAG_DECL(node) \
215 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
217 #define I_LABEL_BINDING(node) \
218 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
219 #define I_LABEL_DECL(node) \
220 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
222 /* Each C symbol points to three linked lists of c_binding structures.
223 These describe the values of the identifier in the three different
224 namespaces defined by the language. */
226 struct lang_identifier GTY(())
228 struct c_common_identifier common_id;
229 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
230 struct c_binding *tag_binding; /* struct/union/enum tags */
231 struct c_binding *label_binding; /* labels */
234 /* Validate c-lang.c's assumptions. */
235 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
236 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
238 /* The resulting tree type. */
240 union lang_tree_node
241 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
242 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)")))
244 union tree_node GTY ((tag ("0"),
245 desc ("tree_node_structure (&%h)")))
246 generic;
247 struct lang_identifier GTY ((tag ("1"))) identifier;
250 /* Each c_scope structure describes the complete contents of one
251 scope. Four scopes are distinguished specially: the innermost or
252 current scope, the innermost function scope, the file scope (always
253 the second to outermost) and the outermost or external scope.
255 Most declarations are recorded in the current scope.
257 All normal label declarations are recorded in the innermost
258 function scope, as are bindings of undeclared identifiers to
259 error_mark_node. (GCC permits nested functions as an extension,
260 hence the 'innermost' qualifier.) Explicitly declared labels
261 (using the __label__ extension) appear in the current scope.
263 Being in the file scope (current_scope == file_scope) causes
264 special behavior in several places below. Also, under some
265 conditions the Objective-C front end records declarations in the
266 file scope even though that isn't the current scope.
268 All declarations with external linkage are recorded in the external
269 scope, even if they aren't visible there; this models the fact that
270 such declarations are visible to the entire program, and (with a
271 bit of cleverness, see pushdecl) allows diagnosis of some violations
272 of C99 6.2.2p7 and 6.2.7p2:
274 If, within the same translation unit, the same identifier appears
275 with both internal and external linkage, the behavior is
276 undefined.
278 All declarations that refer to the same object or function shall
279 have compatible type; otherwise, the behavior is undefined.
281 Initially only the built-in declarations, which describe compiler
282 intrinsic functions plus a subset of the standard library, are in
283 this scope.
285 The order of the blocks list matters, and it is frequently appended
286 to. To avoid having to walk all the way to the end of the list on
287 each insertion, or reverse the list later, we maintain a pointer to
288 the last list entry. (FIXME: It should be feasible to use a reversed
289 list here.)
291 The bindings list is strictly in reverse order of declarations;
292 pop_scope relies on this. */
295 struct c_scope GTY((chain_next ("%h.outer")))
297 /* The scope containing this one. */
298 struct c_scope *outer;
300 /* The next outermost function scope. */
301 struct c_scope *outer_function;
303 /* All bindings in this scope. */
304 struct c_binding *bindings;
306 /* For each scope (except the global one), a chain of BLOCK nodes
307 for all the scopes that were entered and exited one level down. */
308 tree blocks;
309 tree blocks_last;
311 /* The depth of this scope. Used to keep the ->shadowed chain of
312 bindings sorted innermost to outermost. */
313 unsigned int depth : 28;
315 /* True if we are currently filling this scope with parameter
316 declarations. */
317 BOOL_BITFIELD parm_flag : 1;
319 /* True if we already complained about forward parameter decls
320 in this scope. This prevents double warnings on
321 foo (int a; int b; ...) */
322 BOOL_BITFIELD warned_forward_parm_decls : 1;
324 /* True if this is the outermost block scope of a function body.
325 This scope contains the parameters, the local variables declared
326 in the outermost block, and all the labels (except those in
327 nested functions, or declared at block scope with __label__). */
328 BOOL_BITFIELD function_body : 1;
330 /* True means make a BLOCK for this scope no matter what. */
331 BOOL_BITFIELD keep : 1;
334 /* The scope currently in effect. */
336 static GTY(()) struct c_scope *current_scope;
338 /* The innermost function scope. Ordinary (not explicitly declared)
339 labels, bindings to error_mark_node, and the lazily-created
340 bindings of __func__ and its friends get this scope. */
342 static GTY(()) struct c_scope *current_function_scope;
344 /* The C file scope. This is reset for each input translation unit. */
346 static GTY(()) struct c_scope *file_scope;
348 /* The outermost scope. This is used for all declarations with
349 external linkage, and only these, hence the name. */
351 static GTY(()) struct c_scope *external_scope;
353 /* A chain of c_scope structures awaiting reuse. */
355 static GTY((deletable)) struct c_scope *scope_freelist;
357 /* A chain of c_binding structures awaiting reuse. */
359 static GTY((deletable)) struct c_binding *binding_freelist;
361 /* Append VAR to LIST in scope SCOPE. */
362 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
363 struct c_scope *s_ = (scope); \
364 tree d_ = (decl); \
365 if (s_->list##_last) \
366 TREE_CHAIN (s_->list##_last) = d_; \
367 else \
368 s_->list = d_; \
369 s_->list##_last = d_; \
370 } while (0)
372 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
373 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
374 struct c_scope *t_ = (tscope); \
375 struct c_scope *f_ = (fscope); \
376 if (t_->to##_last) \
377 TREE_CHAIN (t_->to##_last) = f_->from; \
378 else \
379 t_->to = f_->from; \
380 t_->to##_last = f_->from##_last; \
381 } while (0)
383 /* True means unconditionally make a BLOCK for the next scope pushed. */
385 static bool keep_next_level_flag;
387 /* True means the next call to push_scope will be the outermost scope
388 of a function body, so do not push a new scope, merely cease
389 expecting parameter decls. */
391 static bool next_is_function_body;
393 /* Functions called automatically at the beginning and end of execution. */
395 static GTY(()) tree static_ctors;
396 static GTY(()) tree static_dtors;
398 /* Forward declarations. */
399 static tree lookup_name_in_scope (tree, struct c_scope *);
400 static tree c_make_fname_decl (tree, int);
401 static tree grokdeclarator (const struct c_declarator *,
402 struct c_declspecs *,
403 enum decl_context, bool, tree *);
404 static tree grokparms (struct c_arg_info *, bool);
405 static void layout_array_type (tree);
407 /* States indicating how grokdeclarator() should handle declspecs marked
408 with __attribute__((deprecated)). An object declared as
409 __attribute__((deprecated)) suppresses warnings of uses of other
410 deprecated items. */
412 enum deprecated_states {
413 DEPRECATED_NORMAL,
414 DEPRECATED_SUPPRESS
417 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
419 void
420 c_print_identifier (FILE *file, tree node, int indent)
422 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
423 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
424 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
425 if (C_IS_RESERVED_WORD (node))
427 tree rid = ridpointers[C_RID_CODE (node)];
428 indent_to (file, indent + 4);
429 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
430 (void *) rid, IDENTIFIER_POINTER (rid));
434 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
435 which may be any of several kinds of DECL or TYPE or error_mark_node,
436 in the scope SCOPE. */
437 static void
438 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
440 struct c_binding *b, **here;
442 if (binding_freelist)
444 b = binding_freelist;
445 binding_freelist = b->prev;
447 else
448 b = GGC_NEW (struct c_binding);
450 b->shadowed = 0;
451 b->decl = decl;
452 b->id = name;
453 b->depth = scope->depth;
454 b->invisible = invisible;
455 b->nested = nested;
456 b->inner_comp = 0;
458 b->type = 0;
460 b->prev = scope->bindings;
461 scope->bindings = b;
463 if (!name)
464 return;
466 switch (TREE_CODE (decl))
468 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
469 case ENUMERAL_TYPE:
470 case UNION_TYPE:
471 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
472 case VAR_DECL:
473 case FUNCTION_DECL:
474 case TYPE_DECL:
475 case CONST_DECL:
476 case PARM_DECL:
477 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
479 default:
480 gcc_unreachable ();
483 /* Locate the appropriate place in the chain of shadowed decls
484 to insert this binding. Normally, scope == current_scope and
485 this does nothing. */
486 while (*here && (*here)->depth > scope->depth)
487 here = &(*here)->shadowed;
489 b->shadowed = *here;
490 *here = b;
493 /* Clear the binding structure B, stick it on the binding_freelist,
494 and return the former value of b->prev. This is used by pop_scope
495 and get_parm_info to iterate destructively over all the bindings
496 from a given scope. */
497 static struct c_binding *
498 free_binding_and_advance (struct c_binding *b)
500 struct c_binding *prev = b->prev;
502 memset (b, 0, sizeof (struct c_binding));
503 b->prev = binding_freelist;
504 binding_freelist = b;
506 return prev;
510 /* Hook called at end of compilation to assume 1 elt
511 for a file-scope tentative array defn that wasn't complete before. */
513 void
514 c_finish_incomplete_decl (tree decl)
516 if (TREE_CODE (decl) == VAR_DECL)
518 tree type = TREE_TYPE (decl);
519 if (type != error_mark_node
520 && TREE_CODE (type) == ARRAY_TYPE
521 && ! DECL_EXTERNAL (decl)
522 && TYPE_DOMAIN (type) == 0)
524 warning ("%Jarray %qD assumed to have one element", decl, decl);
526 complete_array_type (type, NULL_TREE, 1);
528 layout_decl (decl, 0);
533 /* The Objective-C front-end often needs to determine the current scope. */
535 void *
536 objc_get_current_scope (void)
538 return current_scope;
541 /* The following function is used only by Objective-C. It needs to live here
542 because it accesses the innards of c_scope. */
544 void
545 objc_mark_locals_volatile (void *enclosing_blk)
547 struct c_scope *scope;
548 struct c_binding *b;
550 for (scope = current_scope;
551 scope && scope != enclosing_blk;
552 scope = scope->outer)
554 for (b = scope->bindings; b; b = b->prev)
556 if (TREE_CODE (b->decl) == VAR_DECL
557 || TREE_CODE (b->decl) == PARM_DECL)
559 C_DECL_REGISTER (b->decl) = 0;
560 DECL_REGISTER (b->decl) = 0;
561 TREE_THIS_VOLATILE (b->decl) = 1;
565 /* Do not climb up past the current function. */
566 if (scope->function_body)
567 break;
571 /* Nonzero if we are currently in file scope. */
574 global_bindings_p (void)
576 return current_scope == file_scope && !c_override_global_bindings_to_false;
579 void
580 keep_next_level (void)
582 keep_next_level_flag = true;
585 /* Identify this scope as currently being filled with parameters. */
587 void
588 declare_parm_level (void)
590 current_scope->parm_flag = true;
593 void
594 push_scope (void)
596 if (next_is_function_body)
598 /* This is the transition from the parameters to the top level
599 of the function body. These are the same scope
600 (C99 6.2.1p4,6) so we do not push another scope structure.
601 next_is_function_body is set only by store_parm_decls, which
602 in turn is called when and only when we are about to
603 encounter the opening curly brace for the function body.
605 The outermost block of a function always gets a BLOCK node,
606 because the debugging output routines expect that each
607 function has at least one BLOCK. */
608 current_scope->parm_flag = false;
609 current_scope->function_body = true;
610 current_scope->keep = true;
611 current_scope->outer_function = current_function_scope;
612 current_function_scope = current_scope;
614 keep_next_level_flag = false;
615 next_is_function_body = false;
617 else
619 struct c_scope *scope;
620 if (scope_freelist)
622 scope = scope_freelist;
623 scope_freelist = scope->outer;
625 else
626 scope = GGC_CNEW (struct c_scope);
628 scope->keep = keep_next_level_flag;
629 scope->outer = current_scope;
630 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
632 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
633 possible. */
634 if (current_scope && scope->depth == 0)
636 scope->depth--;
637 sorry ("GCC supports only %u nested scopes\n", scope->depth);
640 current_scope = scope;
641 keep_next_level_flag = false;
645 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
647 static void
648 set_type_context (tree type, tree context)
650 for (type = TYPE_MAIN_VARIANT (type); type;
651 type = TYPE_NEXT_VARIANT (type))
652 TYPE_CONTEXT (type) = context;
655 /* Exit a scope. Restore the state of the identifier-decl mappings
656 that were in effect when this scope was entered. Return a BLOCK
657 node containing all the DECLs in this scope that are of interest
658 to debug info generation. */
660 tree
661 pop_scope (void)
663 struct c_scope *scope = current_scope;
664 tree block, context, p;
665 struct c_binding *b;
667 bool functionbody = scope->function_body;
668 bool keep = functionbody || scope->keep || scope->bindings;
670 /* If appropriate, create a BLOCK to record the decls for the life
671 of this function. */
672 block = 0;
673 if (keep)
675 block = make_node (BLOCK);
676 BLOCK_SUBBLOCKS (block) = scope->blocks;
677 TREE_USED (block) = 1;
679 /* In each subblock, record that this is its superior. */
680 for (p = scope->blocks; p; p = TREE_CHAIN (p))
681 BLOCK_SUPERCONTEXT (p) = block;
683 BLOCK_VARS (block) = 0;
686 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
687 scope must be set so that they point to the appropriate
688 construct, i.e. either to the current FUNCTION_DECL node, or
689 else to the BLOCK node we just constructed.
691 Note that for tagged types whose scope is just the formal
692 parameter list for some function type specification, we can't
693 properly set their TYPE_CONTEXTs here, because we don't have a
694 pointer to the appropriate FUNCTION_TYPE node readily available
695 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
696 type nodes get set in `grokdeclarator' as soon as we have created
697 the FUNCTION_TYPE node which will represent the "scope" for these
698 "parameter list local" tagged types. */
699 if (scope->function_body)
700 context = current_function_decl;
701 else if (scope == file_scope)
703 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
704 TREE_CHAIN (file_decl) = all_translation_units;
705 all_translation_units = file_decl;
706 context = file_decl;
708 else
709 context = block;
711 /* Clear all bindings in this scope. */
712 for (b = scope->bindings; b; b = free_binding_and_advance (b))
714 p = b->decl;
715 switch (TREE_CODE (p))
717 case LABEL_DECL:
718 /* Warnings for unused labels, errors for undefined labels. */
719 if (TREE_USED (p) && !DECL_INITIAL (p))
721 error ("%Jlabel %qD used but not defined", p, p);
722 DECL_INITIAL (p) = error_mark_node;
724 else if (!TREE_USED (p) && warn_unused_label)
726 if (DECL_INITIAL (p))
727 warning ("%Jlabel %qD defined but not used", p, p);
728 else
729 warning ("%Jlabel %qD declared but not defined", p, p);
731 /* Labels go in BLOCK_VARS. */
732 TREE_CHAIN (p) = BLOCK_VARS (block);
733 BLOCK_VARS (block) = p;
734 gcc_assert (I_LABEL_BINDING (b->id) == b);
735 I_LABEL_BINDING (b->id) = b->shadowed;
736 break;
738 case ENUMERAL_TYPE:
739 case UNION_TYPE:
740 case RECORD_TYPE:
741 set_type_context (p, context);
743 /* Types may not have tag-names, in which case the type
744 appears in the bindings list with b->id NULL. */
745 if (b->id)
747 gcc_assert (I_TAG_BINDING (b->id) == b);
748 I_TAG_BINDING (b->id) = b->shadowed;
750 break;
752 case FUNCTION_DECL:
753 /* Propagate TREE_ADDRESSABLE from nested functions to their
754 containing functions. */
755 if (! TREE_ASM_WRITTEN (p)
756 && DECL_INITIAL (p) != 0
757 && TREE_ADDRESSABLE (p)
758 && DECL_ABSTRACT_ORIGIN (p) != 0
759 && DECL_ABSTRACT_ORIGIN (p) != p)
760 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
761 goto common_symbol;
763 case VAR_DECL:
764 /* Warnings for unused variables. */
765 if (warn_unused_variable
766 && !TREE_USED (p)
767 && !DECL_IN_SYSTEM_HEADER (p)
768 && DECL_NAME (p)
769 && !DECL_ARTIFICIAL (p)
770 && (scope != file_scope
771 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
772 && !TREE_THIS_VOLATILE (p)))
773 && scope != external_scope)
774 warning ("%Junused variable %qD", p, p);
776 if (b->inner_comp)
778 error ("%Jtype of array %qD completed incompatibly with"
779 " implicit initialization", p, p);
782 /* Fall through. */
783 case TYPE_DECL:
784 case CONST_DECL:
785 common_symbol:
786 /* All of these go in BLOCK_VARS, but only if this is the
787 binding in the home scope. */
788 if (!b->nested)
790 TREE_CHAIN (p) = BLOCK_VARS (block);
791 BLOCK_VARS (block) = p;
793 /* If this is the file scope, and we are processing more
794 than one translation unit in this compilation, set
795 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
796 This makes same_translation_unit_p work, and causes
797 static declarations to be given disambiguating suffixes. */
798 if (scope == file_scope && num_in_fnames > 1)
800 DECL_CONTEXT (p) = context;
801 if (TREE_CODE (p) == TYPE_DECL)
802 set_type_context (TREE_TYPE (p), context);
805 /* Fall through. */
806 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
807 already been put there by store_parm_decls. Unused-
808 parameter warnings are handled by function.c.
809 error_mark_node obviously does not go in BLOCK_VARS and
810 does not get unused-variable warnings. */
811 case PARM_DECL:
812 case ERROR_MARK:
813 /* It is possible for a decl not to have a name. We get
814 here with b->id NULL in this case. */
815 if (b->id)
817 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
818 I_SYMBOL_BINDING (b->id) = b->shadowed;
819 if (b->shadowed && b->shadowed->type)
820 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
822 break;
824 default:
825 gcc_unreachable ();
830 /* Dispose of the block that we just made inside some higher level. */
831 if ((scope->function_body || scope == file_scope) && context)
833 DECL_INITIAL (context) = block;
834 BLOCK_SUPERCONTEXT (block) = context;
836 else if (scope->outer)
838 if (block)
839 SCOPE_LIST_APPEND (scope->outer, blocks, block);
840 /* If we did not make a block for the scope just exited, any
841 blocks made for inner scopes must be carried forward so they
842 will later become subblocks of something else. */
843 else if (scope->blocks)
844 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
847 /* Pop the current scope, and free the structure for reuse. */
848 current_scope = scope->outer;
849 if (scope->function_body)
850 current_function_scope = scope->outer_function;
852 memset (scope, 0, sizeof (struct c_scope));
853 scope->outer = scope_freelist;
854 scope_freelist = scope;
856 return block;
859 void
860 push_file_scope (void)
862 tree decl;
864 if (file_scope)
865 return;
867 push_scope ();
868 file_scope = current_scope;
870 start_fname_decls ();
872 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
873 bind (DECL_NAME (decl), decl, file_scope,
874 /*invisible=*/false, /*nested=*/true);
877 void
878 pop_file_scope (void)
880 /* In case there were missing closebraces, get us back to the global
881 binding level. */
882 while (current_scope != file_scope)
883 pop_scope ();
885 /* __FUNCTION__ is defined at file scope (""). This
886 call may not be necessary as my tests indicate it
887 still works without it. */
888 finish_fname_decls ();
890 /* This is the point to write out a PCH if we're doing that.
891 In that case we do not want to do anything else. */
892 if (pch_file)
894 c_common_write_pch ();
895 return;
898 /* Pop off the file scope and close this translation unit. */
899 pop_scope ();
900 file_scope = 0;
901 cgraph_finalize_compilation_unit ();
904 /* Insert BLOCK at the end of the list of subblocks of the current
905 scope. This is used when a BIND_EXPR is expanded, to handle the
906 BLOCK node inside the BIND_EXPR. */
908 void
909 insert_block (tree block)
911 TREE_USED (block) = 1;
912 SCOPE_LIST_APPEND (current_scope, blocks, block);
915 /* Push a definition or a declaration of struct, union or enum tag "name".
916 "type" should be the type node.
917 We assume that the tag "name" is not already defined.
919 Note that the definition may really be just a forward reference.
920 In that case, the TYPE_SIZE will be zero. */
922 static void
923 pushtag (tree name, tree type)
925 /* Record the identifier as the type's name if it has none. */
926 if (name && !TYPE_NAME (type))
927 TYPE_NAME (type) = name;
928 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
930 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
931 tagged type we just added to the current scope. This fake
932 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
933 to output a representation of a tagged type, and it also gives
934 us a convenient place to record the "scope start" address for the
935 tagged type. */
937 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
939 /* An approximation for now, so we can tell this is a function-scope tag.
940 This will be updated in pop_scope. */
941 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
944 /* Subroutine of compare_decls. Allow harmless mismatches in return
945 and argument types provided that the type modes match. This function
946 return a unified type given a suitable match, and 0 otherwise. */
948 static tree
949 match_builtin_function_types (tree newtype, tree oldtype)
951 tree newrettype, oldrettype;
952 tree newargs, oldargs;
953 tree trytype, tryargs;
955 /* Accept the return type of the new declaration if same modes. */
956 oldrettype = TREE_TYPE (oldtype);
957 newrettype = TREE_TYPE (newtype);
959 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
960 return 0;
962 oldargs = TYPE_ARG_TYPES (oldtype);
963 newargs = TYPE_ARG_TYPES (newtype);
964 tryargs = newargs;
966 while (oldargs || newargs)
968 if (! oldargs
969 || ! newargs
970 || ! TREE_VALUE (oldargs)
971 || ! TREE_VALUE (newargs)
972 || TYPE_MODE (TREE_VALUE (oldargs))
973 != TYPE_MODE (TREE_VALUE (newargs)))
974 return 0;
976 oldargs = TREE_CHAIN (oldargs);
977 newargs = TREE_CHAIN (newargs);
980 trytype = build_function_type (newrettype, tryargs);
981 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
984 /* Subroutine of diagnose_mismatched_decls. Check for function type
985 mismatch involving an empty arglist vs a nonempty one and give clearer
986 diagnostics. */
987 static void
988 diagnose_arglist_conflict (tree newdecl, tree olddecl,
989 tree newtype, tree oldtype)
991 tree t;
993 if (TREE_CODE (olddecl) != FUNCTION_DECL
994 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
995 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
997 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
998 return;
1000 t = TYPE_ARG_TYPES (oldtype);
1001 if (t == 0)
1002 t = TYPE_ARG_TYPES (newtype);
1003 for (; t; t = TREE_CHAIN (t))
1005 tree type = TREE_VALUE (t);
1007 if (TREE_CHAIN (t) == 0
1008 && TYPE_MAIN_VARIANT (type) != void_type_node)
1010 inform ("a parameter list with an ellipsis can't match "
1011 "an empty parameter name list declaration");
1012 break;
1015 if (c_type_promotes_to (type) != type)
1017 inform ("an argument type that has a default promotion can't match "
1018 "an empty parameter name list declaration");
1019 break;
1024 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1025 old-style function definition, NEWDECL is a prototype declaration.
1026 Diagnose inconsistencies in the argument list. Returns TRUE if
1027 the prototype is compatible, FALSE if not. */
1028 static bool
1029 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1031 tree newargs, oldargs;
1032 int i;
1034 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1035 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1037 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1038 newargs = TYPE_ARG_TYPES (newtype);
1039 i = 1;
1041 for (;;)
1043 tree oldargtype = TREE_VALUE (oldargs);
1044 tree newargtype = TREE_VALUE (newargs);
1046 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1047 break;
1049 /* Reaching the end of just one list means the two decls don't
1050 agree on the number of arguments. */
1051 if (END_OF_ARGLIST (oldargtype))
1053 error ("%Jprototype for %qD declares more arguments "
1054 "than previous old-style definition", newdecl, newdecl);
1055 return false;
1057 else if (END_OF_ARGLIST (newargtype))
1059 error ("%Jprototype for %qD declares fewer arguments "
1060 "than previous old-style definition", newdecl, newdecl);
1061 return false;
1064 /* Type for passing arg must be consistent with that declared
1065 for the arg. */
1066 else if (! comptypes (oldargtype, newargtype))
1068 error ("%Jprototype for %qD declares arg %d with incompatible type",
1069 newdecl, newdecl, i);
1070 return false;
1073 oldargs = TREE_CHAIN (oldargs);
1074 newargs = TREE_CHAIN (newargs);
1075 i++;
1078 /* If we get here, no errors were found, but do issue a warning
1079 for this poor-style construct. */
1080 warning ("%Jprototype for %qD follows non-prototype definition",
1081 newdecl, newdecl);
1082 return true;
1083 #undef END_OF_ARGLIST
1086 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1087 first in a pair of mismatched declarations, using the diagnostic
1088 function DIAG. */
1089 static void
1090 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1092 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1094 else if (DECL_INITIAL (decl))
1095 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1096 else if (C_DECL_IMPLICIT (decl))
1097 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1098 else
1099 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1102 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1103 Returns true if the caller should proceed to merge the two, false
1104 if OLDDECL should simply be discarded. As a side effect, issues
1105 all necessary diagnostics for invalid or poor-style combinations.
1106 If it returns true, writes the types of NEWDECL and OLDDECL to
1107 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1108 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1110 static bool
1111 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1112 tree *newtypep, tree *oldtypep)
1114 tree newtype, oldtype;
1115 bool pedwarned = false;
1116 bool warned = false;
1118 /* If we have error_mark_node for either decl or type, just discard
1119 the previous decl - we're in an error cascade already. */
1120 if (olddecl == error_mark_node || newdecl == error_mark_node)
1121 return false;
1122 *oldtypep = oldtype = TREE_TYPE (olddecl);
1123 *newtypep = newtype = TREE_TYPE (newdecl);
1124 if (oldtype == error_mark_node || newtype == error_mark_node)
1125 return false;
1127 /* Two different categories of symbol altogether. This is an error
1128 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1129 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1131 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1132 && DECL_BUILT_IN (olddecl)
1133 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1135 error ("%J%qD redeclared as different kind of symbol",
1136 newdecl, newdecl);
1137 locate_old_decl (olddecl, error);
1139 else if (TREE_PUBLIC (newdecl))
1140 warning ("%Jbuilt-in function %qD declared as non-function",
1141 newdecl, newdecl);
1142 else if (warn_shadow)
1143 warning ("%Jdeclaration of %qD shadows a built-in function",
1144 newdecl, newdecl);
1145 return false;
1148 if (!comptypes (oldtype, newtype))
1150 if (TREE_CODE (olddecl) == FUNCTION_DECL
1151 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1153 /* Accept harmless mismatch in function types.
1154 This is for the ffs and fprintf builtins. */
1155 tree trytype = match_builtin_function_types (newtype, oldtype);
1157 if (trytype && comptypes (newtype, trytype))
1158 *oldtypep = oldtype = trytype;
1159 else
1161 /* If types don't match for a built-in, throw away the
1162 built-in. No point in calling locate_old_decl here, it
1163 won't print anything. */
1164 warning ("%Jconflicting types for built-in function %qD",
1165 newdecl, newdecl);
1166 return false;
1169 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1170 && DECL_IS_BUILTIN (olddecl))
1172 /* A conflicting function declaration for a predeclared
1173 function that isn't actually built in. Objective C uses
1174 these. The new declaration silently overrides everything
1175 but the volatility (i.e. noreturn) indication. See also
1176 below. FIXME: Make Objective C use normal builtins. */
1177 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1178 return false;
1180 /* Permit void foo (...) to match int foo (...) if the latter is
1181 the definition and implicit int was used. See
1182 c-torture/compile/920625-2.c. */
1183 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1184 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1185 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1186 && C_FUNCTION_IMPLICIT_INT (newdecl))
1188 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1189 /* Make sure we keep void as the return type. */
1190 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1191 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1192 pedwarned = true;
1194 else
1196 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1197 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1198 else
1199 error ("%Jconflicting types for %qD", newdecl, newdecl);
1200 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1201 locate_old_decl (olddecl, error);
1202 return false;
1206 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1207 but silently ignore the redeclaration if either is in a system
1208 header. (Conflicting redeclarations were handled above.) */
1209 if (TREE_CODE (newdecl) == TYPE_DECL)
1211 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1212 return true; /* Allow OLDDECL to continue in use. */
1214 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1215 locate_old_decl (olddecl, error);
1216 return false;
1219 /* Function declarations can either be 'static' or 'extern' (no
1220 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1221 can never conflict with each other on account of linkage (6.2.2p4).
1222 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1223 two definitions if one is 'extern inline' and one is not. The non-
1224 extern-inline definition supersedes the extern-inline definition. */
1225 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1227 /* If you declare a built-in function name as static, or
1228 define the built-in with an old-style definition (so we
1229 can't validate the argument list) the built-in definition is
1230 overridden, but optionally warn this was a bad choice of name. */
1231 if (DECL_BUILT_IN (olddecl)
1232 && !C_DECL_DECLARED_BUILTIN (olddecl)
1233 && (!TREE_PUBLIC (newdecl)
1234 || (DECL_INITIAL (newdecl)
1235 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1237 if (warn_shadow)
1238 warning ("%Jdeclaration of %qD shadows a built-in function",
1239 newdecl, newdecl);
1240 /* Discard the old built-in function. */
1241 return false;
1244 if (DECL_INITIAL (newdecl))
1246 if (DECL_INITIAL (olddecl)
1247 && !(DECL_DECLARED_INLINE_P (olddecl)
1248 && DECL_EXTERNAL (olddecl)
1249 && !(DECL_DECLARED_INLINE_P (newdecl)
1250 && DECL_EXTERNAL (newdecl)
1251 && same_translation_unit_p (olddecl, newdecl))))
1253 error ("%Jredefinition of %qD", newdecl, newdecl);
1254 locate_old_decl (olddecl, error);
1255 return false;
1258 /* If we have a prototype after an old-style function definition,
1259 the argument types must be checked specially. */
1260 else if (DECL_INITIAL (olddecl)
1261 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1262 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1263 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1265 locate_old_decl (olddecl, error);
1266 return false;
1268 /* A non-static declaration (even an "extern") followed by a
1269 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1270 The same is true for a static forward declaration at block
1271 scope followed by a non-static declaration/definition at file
1272 scope. Static followed by non-static at the same scope is
1273 not undefined behavior, and is the most convenient way to get
1274 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1275 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1276 we do diagnose it if -Wtraditional. */
1277 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1279 /* Two exceptions to the rule. If olddecl is an extern
1280 inline, or a predeclared function that isn't actually
1281 built in, newdecl silently overrides olddecl. The latter
1282 occur only in Objective C; see also above. (FIXME: Make
1283 Objective C use normal builtins.) */
1284 if (!DECL_IS_BUILTIN (olddecl)
1285 && !(DECL_EXTERNAL (olddecl)
1286 && DECL_DECLARED_INLINE_P (olddecl)))
1288 error ("%Jstatic declaration of %qD follows "
1289 "non-static declaration", newdecl, newdecl);
1290 locate_old_decl (olddecl, error);
1292 return false;
1294 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1296 if (DECL_CONTEXT (olddecl))
1298 error ("%Jnon-static declaration of %qD follows "
1299 "static declaration", newdecl, newdecl);
1300 locate_old_decl (olddecl, error);
1301 return false;
1303 else if (warn_traditional)
1305 warning ("%Jnon-static declaration of %qD follows "
1306 "static declaration", newdecl, newdecl);
1307 warned = true;
1311 else if (TREE_CODE (newdecl) == VAR_DECL)
1313 /* Only variables can be thread-local, and all declarations must
1314 agree on this property. */
1315 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1317 if (DECL_THREAD_LOCAL (newdecl))
1318 error ("%Jthread-local declaration of %qD follows "
1319 "non-thread-local declaration", newdecl, newdecl);
1320 else
1321 error ("%Jnon-thread-local declaration of %qD follows "
1322 "thread-local declaration", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1325 return false;
1328 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1329 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1331 error ("%Jredefinition of %qD", newdecl, newdecl);
1332 locate_old_decl (olddecl, error);
1333 return false;
1336 /* Objects declared at file scope: if the first declaration had
1337 external linkage (even if it was an external reference) the
1338 second must have external linkage as well, or the behavior is
1339 undefined. If the first declaration had internal linkage, then
1340 the second must too, or else be an external reference (in which
1341 case the composite declaration still has internal linkage).
1342 As for function declarations, we warn about the static-then-
1343 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1344 if (DECL_FILE_SCOPE_P (newdecl)
1345 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1347 if (DECL_EXTERNAL (newdecl))
1349 if (!DECL_FILE_SCOPE_P (olddecl))
1351 error ("%Jextern declaration of %qD follows "
1352 "declaration with no linkage", newdecl, newdecl);
1353 locate_old_decl (olddecl, error);
1354 return false;
1356 else if (warn_traditional)
1358 warning ("%Jnon-static declaration of %qD follows "
1359 "static declaration", newdecl, newdecl);
1360 warned = true;
1363 else
1365 if (TREE_PUBLIC (newdecl))
1366 error ("%Jnon-static declaration of %qD follows "
1367 "static declaration", newdecl, newdecl);
1368 else
1369 error ("%Jstatic declaration of %qD follows "
1370 "non-static declaration", newdecl, newdecl);
1372 locate_old_decl (olddecl, error);
1373 return false;
1376 /* Two objects with the same name declared at the same block
1377 scope must both be external references (6.7p3). */
1378 else if (!DECL_FILE_SCOPE_P (newdecl))
1380 if (DECL_EXTERNAL (newdecl))
1382 /* Extern with initializer at block scope, which will
1383 already have received an error. */
1385 else if (DECL_EXTERNAL (olddecl))
1387 error ("%Jdeclaration of %qD with no linkage follows "
1388 "extern declaration", newdecl, newdecl);
1389 locate_old_decl (olddecl, error);
1391 else
1393 error ("%Jredeclaration of %qD with no linkage",
1394 newdecl, newdecl);
1395 locate_old_decl (olddecl, error);
1398 return false;
1402 /* warnings */
1403 /* All decls must agree on a visibility. */
1404 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1405 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1407 warning ("%Jredeclaration of %qD with different visibility "
1408 "(old visibility preserved)", newdecl, newdecl);
1409 warned = true;
1412 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1414 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1415 if (DECL_DECLARED_INLINE_P (newdecl)
1416 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1418 warning ("%Jinline declaration of %qD follows "
1419 "declaration with attribute noinline", newdecl, newdecl);
1420 warned = true;
1422 else if (DECL_DECLARED_INLINE_P (olddecl)
1423 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1425 warning ("%Jdeclaration of %qD with attribute noinline follows "
1426 "inline declaration ", newdecl, newdecl);
1427 warned = true;
1430 /* Inline declaration after use or definition.
1431 ??? Should we still warn about this now we have unit-at-a-time
1432 mode and can get it right?
1433 Definitely don't complain if the decls are in different translation
1434 units. */
1435 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1436 && same_translation_unit_p (olddecl, newdecl))
1438 if (TREE_USED (olddecl))
1440 warning ("%J%qD declared inline after being called",
1441 olddecl, olddecl);
1442 warned = true;
1444 else if (DECL_INITIAL (olddecl))
1446 warning ("%J%qD declared inline after its definition",
1447 olddecl, olddecl);
1448 warned = true;
1452 else /* PARM_DECL, VAR_DECL */
1454 /* Redeclaration of a parameter is a constraint violation (this is
1455 not explicitly stated, but follows from C99 6.7p3 [no more than
1456 one declaration of the same identifier with no linkage in the
1457 same scope, except type tags] and 6.2.2p6 [parameters have no
1458 linkage]). We must check for a forward parameter declaration,
1459 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1460 an extension, the mandatory diagnostic for which is handled by
1461 mark_forward_parm_decls. */
1463 if (TREE_CODE (newdecl) == PARM_DECL
1464 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1466 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1467 locate_old_decl (olddecl, error);
1468 return false;
1472 /* Optional warning for completely redundant decls. */
1473 if (!warned && !pedwarned
1474 && warn_redundant_decls
1475 /* Don't warn about a function declaration followed by a
1476 definition. */
1477 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1478 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1479 /* Don't warn about redundant redeclarations of builtins. */
1480 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1481 && !DECL_BUILT_IN (newdecl)
1482 && DECL_BUILT_IN (olddecl)
1483 && !C_DECL_DECLARED_BUILTIN (olddecl))
1484 /* Don't warn about an extern followed by a definition. */
1485 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1486 /* Don't warn about forward parameter decls. */
1487 && !(TREE_CODE (newdecl) == PARM_DECL
1488 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1490 warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
1491 warned = true;
1494 /* Report location of previous decl/defn in a consistent manner. */
1495 if (warned || pedwarned)
1496 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1498 return true;
1501 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1502 consistent with OLDDECL, but carries new information. Merge the
1503 new information into OLDDECL. This function issues no
1504 diagnostics. */
1506 static void
1507 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1509 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1510 && DECL_INITIAL (newdecl) != 0);
1512 /* For real parm decl following a forward decl, rechain the old decl
1513 in its new location and clear TREE_ASM_WRITTEN (it's not a
1514 forward decl anymore). */
1515 if (TREE_CODE (newdecl) == PARM_DECL
1516 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1518 struct c_binding *b, **here;
1520 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1521 if ((*here)->decl == olddecl)
1522 goto found;
1523 gcc_unreachable ();
1525 found:
1526 b = *here;
1527 *here = b->prev;
1528 b->prev = current_scope->bindings;
1529 current_scope->bindings = b;
1531 TREE_ASM_WRITTEN (olddecl) = 0;
1534 DECL_ATTRIBUTES (newdecl)
1535 = targetm.merge_decl_attributes (olddecl, newdecl);
1537 /* Merge the data types specified in the two decls. */
1538 TREE_TYPE (newdecl)
1539 = TREE_TYPE (olddecl)
1540 = composite_type (newtype, oldtype);
1542 /* Lay the type out, unless already done. */
1543 if (oldtype != TREE_TYPE (newdecl))
1545 if (TREE_TYPE (newdecl) != error_mark_node)
1546 layout_type (TREE_TYPE (newdecl));
1547 if (TREE_CODE (newdecl) != FUNCTION_DECL
1548 && TREE_CODE (newdecl) != TYPE_DECL
1549 && TREE_CODE (newdecl) != CONST_DECL)
1550 layout_decl (newdecl, 0);
1552 else
1554 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1555 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1556 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1557 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1558 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1559 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1561 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1562 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1566 /* Keep the old rtl since we can safely use it. */
1567 COPY_DECL_RTL (olddecl, newdecl);
1569 /* Merge the type qualifiers. */
1570 if (TREE_READONLY (newdecl))
1571 TREE_READONLY (olddecl) = 1;
1573 if (TREE_THIS_VOLATILE (newdecl))
1575 TREE_THIS_VOLATILE (olddecl) = 1;
1576 if (TREE_CODE (newdecl) == VAR_DECL)
1577 make_var_volatile (newdecl);
1580 /* Keep source location of definition rather than declaration. */
1581 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1582 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1584 /* Merge the unused-warning information. */
1585 if (DECL_IN_SYSTEM_HEADER (olddecl))
1586 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1587 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1588 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1590 /* Merge the initialization information. */
1591 if (DECL_INITIAL (newdecl) == 0)
1592 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1594 /* Merge the section attribute.
1595 We want to issue an error if the sections conflict but that must be
1596 done later in decl_attributes since we are called before attributes
1597 are assigned. */
1598 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1599 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1601 /* Copy the assembler name.
1602 Currently, it can only be defined in the prototype. */
1603 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1605 /* Use visibility of whichever declaration had it specified */
1606 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1608 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1609 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1612 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1614 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1615 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1616 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1617 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1618 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1619 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1620 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1621 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1622 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1625 /* Merge the storage class information. */
1626 merge_weak (newdecl, olddecl);
1628 /* For functions, static overrides non-static. */
1629 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1631 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1632 /* This is since we don't automatically
1633 copy the attributes of NEWDECL into OLDDECL. */
1634 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1635 /* If this clears `static', clear it in the identifier too. */
1636 if (! TREE_PUBLIC (olddecl))
1637 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1639 if (DECL_EXTERNAL (newdecl))
1641 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1642 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1644 /* An extern decl does not override previous storage class. */
1645 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1646 if (! DECL_EXTERNAL (newdecl))
1648 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1649 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1652 else
1654 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1655 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1658 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1660 /* If we're redefining a function previously defined as extern
1661 inline, make sure we emit debug info for the inline before we
1662 throw it away, in case it was inlined into a function that hasn't
1663 been written out yet. */
1664 if (new_is_definition && DECL_INITIAL (olddecl))
1666 if (TREE_USED (olddecl)
1667 /* In unit-at-a-time mode we never inline re-defined extern
1668 inline functions. */
1669 && !flag_unit_at_a_time
1670 && cgraph_function_possibly_inlined_p (olddecl))
1671 (*debug_hooks->outlining_inline_function) (olddecl);
1673 /* The new defn must not be inline. */
1674 DECL_INLINE (newdecl) = 0;
1675 DECL_UNINLINABLE (newdecl) = 1;
1677 else
1679 /* If either decl says `inline', this fn is inline,
1680 unless its definition was passed already. */
1681 if (DECL_DECLARED_INLINE_P (newdecl)
1682 || DECL_DECLARED_INLINE_P (olddecl))
1683 DECL_DECLARED_INLINE_P (newdecl) = 1;
1685 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1686 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1689 if (DECL_BUILT_IN (olddecl))
1691 /* If redeclaring a builtin function, it stays built in.
1692 But it gets tagged as having been declared. */
1693 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1694 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1695 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1698 /* Also preserve various other info from the definition. */
1699 if (! new_is_definition)
1701 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1702 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1703 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1704 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1705 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1707 /* Set DECL_INLINE on the declaration if we've got a body
1708 from which to instantiate. */
1709 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1711 DECL_INLINE (newdecl) = 1;
1712 DECL_ABSTRACT_ORIGIN (newdecl)
1713 = DECL_ABSTRACT_ORIGIN (olddecl);
1716 else
1718 /* If a previous declaration said inline, mark the
1719 definition as inlinable. */
1720 if (DECL_DECLARED_INLINE_P (newdecl)
1721 && ! DECL_UNINLINABLE (newdecl))
1722 DECL_INLINE (newdecl) = 1;
1726 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1727 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1729 unsigned olddecl_uid = DECL_UID (olddecl);
1730 tree olddecl_context = DECL_CONTEXT (olddecl);
1732 memcpy ((char *) olddecl + sizeof (struct tree_common),
1733 (char *) newdecl + sizeof (struct tree_common),
1734 sizeof (struct tree_decl) - sizeof (struct tree_common));
1735 DECL_UID (olddecl) = olddecl_uid;
1736 DECL_CONTEXT (olddecl) = olddecl_context;
1739 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1740 so that encode_section_info has a chance to look at the new decl
1741 flags and attributes. */
1742 if (DECL_RTL_SET_P (olddecl)
1743 && (TREE_CODE (olddecl) == FUNCTION_DECL
1744 || (TREE_CODE (olddecl) == VAR_DECL
1745 && TREE_STATIC (olddecl))))
1746 make_decl_rtl (olddecl);
1749 /* Handle when a new declaration NEWDECL has the same name as an old
1750 one OLDDECL in the same binding contour. Prints an error message
1751 if appropriate.
1753 If safely possible, alter OLDDECL to look like NEWDECL, and return
1754 true. Otherwise, return false. */
1756 static bool
1757 duplicate_decls (tree newdecl, tree olddecl)
1759 tree newtype = NULL, oldtype = NULL;
1761 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1762 return false;
1764 merge_decls (newdecl, olddecl, newtype, oldtype);
1765 return true;
1769 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1770 static void
1771 warn_if_shadowing (tree new_decl)
1773 struct c_binding *b;
1775 /* Shadow warnings wanted? */
1776 if (!warn_shadow
1777 /* No shadow warnings for internally generated vars. */
1778 || DECL_IS_BUILTIN (new_decl)
1779 /* No shadow warnings for vars made for inlining. */
1780 || DECL_FROM_INLINE (new_decl)
1781 /* Don't warn about the parm names in function declarator
1782 within a function declarator. It would be nice to avoid
1783 warning in any function declarator in a declaration, as
1784 opposed to a definition, but there is no way to tell
1785 it's not a definition at this point. */
1786 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1787 return;
1789 /* Is anything being shadowed? Invisible decls do not count. */
1790 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1791 if (b->decl && b->decl != new_decl && !b->invisible)
1793 tree old_decl = b->decl;
1795 if (TREE_CODE (old_decl) == PARM_DECL)
1796 warning ("%Jdeclaration of %qD shadows a parameter",
1797 new_decl, new_decl);
1798 else if (DECL_FILE_SCOPE_P (old_decl))
1799 warning ("%Jdeclaration of %qD shadows a global declaration",
1800 new_decl, new_decl);
1801 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1802 && DECL_BUILT_IN (old_decl))
1803 warning ("%Jdeclaration of %qD shadows a built-in function",
1804 new_decl, new_decl);
1805 else
1806 warning ("%Jdeclaration of %qD shadows a previous local",
1807 new_decl, new_decl);
1809 if (TREE_CODE (old_decl) != FUNCTION_DECL
1810 || ! DECL_BUILT_IN (old_decl))
1811 warning ("%Jshadowed declaration is here", old_decl);
1813 break;
1818 /* Subroutine of pushdecl.
1820 X is a TYPE_DECL for a typedef statement. Create a brand new
1821 ..._TYPE node (which will be just a variant of the existing
1822 ..._TYPE node with identical properties) and then install X
1823 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1825 The whole point here is to end up with a situation where each
1826 and every ..._TYPE node the compiler creates will be uniquely
1827 associated with AT MOST one node representing a typedef name.
1828 This way, even though the compiler substitutes corresponding
1829 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1830 early on, later parts of the compiler can always do the reverse
1831 translation and get back the corresponding typedef name. For
1832 example, given:
1834 typedef struct S MY_TYPE;
1835 MY_TYPE object;
1837 Later parts of the compiler might only know that `object' was of
1838 type `struct S' if it were not for code just below. With this
1839 code however, later parts of the compiler see something like:
1841 struct S' == struct S
1842 typedef struct S' MY_TYPE;
1843 struct S' object;
1845 And they can then deduce (from the node for type struct S') that
1846 the original object declaration was:
1848 MY_TYPE object;
1850 Being able to do this is important for proper support of protoize,
1851 and also for generating precise symbolic debugging information
1852 which takes full account of the programmer's (typedef) vocabulary.
1854 Obviously, we don't want to generate a duplicate ..._TYPE node if
1855 the TYPE_DECL node that we are now processing really represents a
1856 standard built-in type.
1858 Since all standard types are effectively declared at line zero
1859 in the source file, we can easily check to see if we are working
1860 on a standard type by checking the current value of lineno. */
1862 static void
1863 clone_underlying_type (tree x)
1865 if (DECL_IS_BUILTIN (x))
1867 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1868 TYPE_NAME (TREE_TYPE (x)) = x;
1870 else if (TREE_TYPE (x) != error_mark_node
1871 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1873 tree tt = TREE_TYPE (x);
1874 DECL_ORIGINAL_TYPE (x) = tt;
1875 tt = build_variant_type_copy (tt);
1876 TYPE_NAME (tt) = x;
1877 TREE_USED (tt) = TREE_USED (x);
1878 TREE_TYPE (x) = tt;
1882 /* Record a decl-node X as belonging to the current lexical scope.
1883 Check for errors (such as an incompatible declaration for the same
1884 name already seen in the same scope).
1886 Returns either X or an old decl for the same name.
1887 If an old decl is returned, it may have been smashed
1888 to agree with what X says. */
1890 tree
1891 pushdecl (tree x)
1893 tree name = DECL_NAME (x);
1894 struct c_scope *scope = current_scope;
1895 struct c_binding *b;
1896 bool nested = false;
1898 /* Functions need the lang_decl data. */
1899 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1900 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1902 /* Must set DECL_CONTEXT for everything not at file scope or
1903 DECL_FILE_SCOPE_P won't work. Local externs don't count
1904 unless they have initializers (which generate code). */
1905 if (current_function_decl
1906 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1907 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1908 DECL_CONTEXT (x) = current_function_decl;
1910 /* Anonymous decls are just inserted in the scope. */
1911 if (!name)
1913 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1914 return x;
1917 /* First, see if there is another declaration with the same name in
1918 the current scope. If there is, duplicate_decls may do all the
1919 work for us. If duplicate_decls returns false, that indicates
1920 two incompatible decls in the same scope; we are to silently
1921 replace the old one (duplicate_decls has issued all appropriate
1922 diagnostics). In particular, we should not consider possible
1923 duplicates in the external scope, or shadowing. */
1924 b = I_SYMBOL_BINDING (name);
1925 if (b && B_IN_SCOPE (b, scope))
1927 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
1928 && COMPLETE_TYPE_P (TREE_TYPE (x)))
1929 b->inner_comp = false;
1930 if (duplicate_decls (x, b->decl))
1931 return b->decl;
1932 else
1933 goto skip_external_and_shadow_checks;
1936 /* All declarations with external linkage, and all external
1937 references, go in the external scope, no matter what scope is
1938 current. However, the binding in that scope is ignored for
1939 purposes of normal name lookup. A separate binding structure is
1940 created in the requested scope; this governs the normal
1941 visibility of the symbol.
1943 The binding in the externals scope is used exclusively for
1944 detecting duplicate declarations of the same object, no matter
1945 what scope they are in; this is what we do here. (C99 6.2.7p2:
1946 All declarations that refer to the same object or function shall
1947 have compatible type; otherwise, the behavior is undefined.) */
1948 if (DECL_EXTERNAL (x) || scope == file_scope)
1950 tree type = TREE_TYPE (x);
1951 tree vistype = 0;
1952 tree visdecl = 0;
1953 bool type_saved = false;
1954 if (b && !B_IN_EXTERNAL_SCOPE (b)
1955 && (TREE_CODE (b->decl) == FUNCTION_DECL
1956 || TREE_CODE (b->decl) == VAR_DECL)
1957 && DECL_FILE_SCOPE_P (b->decl))
1959 visdecl = b->decl;
1960 vistype = TREE_TYPE (visdecl);
1962 if (warn_nested_externs
1963 && scope != file_scope
1964 && !DECL_IN_SYSTEM_HEADER (x))
1965 warning ("nested extern declaration of %qD", x);
1967 while (b && !B_IN_EXTERNAL_SCOPE (b))
1969 /* If this decl might be modified, save its type. This is
1970 done here rather than when the decl is first bound
1971 because the type may change after first binding, through
1972 being completed or through attributes being added. If we
1973 encounter multiple such decls, only the first should have
1974 its type saved; the others will already have had their
1975 proper types saved and the types will not have changed as
1976 their scopes will not have been re-entered. */
1977 if (DECL_FILE_SCOPE_P (b->decl) && !type_saved)
1979 b->type = TREE_TYPE (b->decl);
1980 type_saved = true;
1982 if (B_IN_FILE_SCOPE (b)
1983 && TREE_CODE (b->decl) == VAR_DECL
1984 && TREE_STATIC (b->decl)
1985 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
1986 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
1987 && TREE_CODE (type) == ARRAY_TYPE
1988 && TYPE_DOMAIN (type)
1989 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
1990 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1992 /* Array type completed in inner scope, which should be
1993 diagnosed if the completion does not have size 1 and
1994 it does not get completed in the file scope. */
1995 b->inner_comp = true;
1997 b = b->shadowed;
2000 /* If a matching external declaration has been found, set its
2001 type to the composite of all the types of that declaration.
2002 After the consistency checks, it will be reset to the
2003 composite of the visible types only. */
2004 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2005 && b->type)
2006 TREE_TYPE (b->decl) = b->type;
2008 /* The point of the same_translation_unit_p check here is,
2009 we want to detect a duplicate decl for a construct like
2010 foo() { extern bar(); } ... static bar(); but not if
2011 they are in different translation units. In any case,
2012 the static does not go in the externals scope. */
2013 if (b
2014 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2015 && duplicate_decls (x, b->decl))
2017 tree thistype;
2018 thistype = (vistype ? composite_type (vistype, type) : type);
2019 b->type = TREE_TYPE (b->decl);
2020 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2021 thistype
2022 = build_type_attribute_variant (thistype,
2023 TYPE_ATTRIBUTES (b->type));
2024 TREE_TYPE (b->decl) = thistype;
2025 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2026 return b->decl;
2028 else if (TREE_PUBLIC (x))
2030 if (visdecl && !b && duplicate_decls (x, visdecl))
2032 /* An external declaration at block scope referring to a
2033 visible entity with internal linkage. The composite
2034 type will already be correct for this scope, so we
2035 just need to fall through to make the declaration in
2036 this scope. */
2037 nested = true;
2039 else
2041 bind (name, x, external_scope, /*invisible=*/true,
2042 /*nested=*/false);
2043 nested = true;
2047 /* Similarly, a declaration of a function with static linkage at
2048 block scope must be checked against any existing declaration
2049 of that function at file scope. */
2050 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
2051 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
2053 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
2054 warning ("nested static declaration of %qD", x);
2056 while (b && !B_IN_FILE_SCOPE (b))
2057 b = b->shadowed;
2059 if (b && same_translation_unit_p (x, b->decl)
2060 && duplicate_decls (x, b->decl))
2062 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2063 return b->decl;
2065 else
2067 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
2068 nested = true;
2072 warn_if_shadowing (x);
2074 skip_external_and_shadow_checks:
2075 if (TREE_CODE (x) == TYPE_DECL)
2076 clone_underlying_type (x);
2078 bind (name, x, scope, /*invisible=*/false, nested);
2080 /* If x's type is incomplete because it's based on a
2081 structure or union which has not yet been fully declared,
2082 attach it to that structure or union type, so we can go
2083 back and complete the variable declaration later, if the
2084 structure or union gets fully declared.
2086 If the input is erroneous, we can have error_mark in the type
2087 slot (e.g. "f(void a, ...)") - that doesn't count as an
2088 incomplete type. */
2089 if (TREE_TYPE (x) != error_mark_node
2090 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2092 tree element = TREE_TYPE (x);
2094 while (TREE_CODE (element) == ARRAY_TYPE)
2095 element = TREE_TYPE (element);
2096 element = TYPE_MAIN_VARIANT (element);
2098 if ((TREE_CODE (element) == RECORD_TYPE
2099 || TREE_CODE (element) == UNION_TYPE)
2100 && (TREE_CODE (x) != TYPE_DECL
2101 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2102 && !COMPLETE_TYPE_P (element))
2103 C_TYPE_INCOMPLETE_VARS (element)
2104 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2106 return x;
2109 /* Record X as belonging to file scope.
2110 This is used only internally by the Objective-C front end,
2111 and is limited to its needs. duplicate_decls is not called;
2112 if there is any preexisting decl for this identifier, it is an ICE. */
2114 tree
2115 pushdecl_top_level (tree x)
2117 tree name;
2118 bool nested = false;
2120 gcc_assert (TREE_CODE (x) == VAR_DECL);
2122 name = DECL_NAME (x);
2124 gcc_assert (!I_SYMBOL_BINDING (name));
2126 if (TREE_PUBLIC (x))
2128 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2129 nested = true;
2131 if (file_scope)
2132 bind (name, x, file_scope, /*invisible=*/false, nested);
2134 return x;
2137 static void
2138 implicit_decl_warning (tree id, tree olddecl)
2140 void (*diag) (const char *, ...);
2141 switch (mesg_implicit_function_declaration)
2143 case 0: return;
2144 case 1: diag = warning; break;
2145 case 2: diag = error; break;
2146 default: gcc_unreachable ();
2149 diag (N_("implicit declaration of function %qE"), id);
2150 if (olddecl)
2151 locate_old_decl (olddecl, diag);
2154 /* Generate an implicit declaration for identifier FUNCTIONID as a
2155 function of type int (). */
2157 tree
2158 implicitly_declare (tree functionid)
2160 struct c_binding *b;
2161 tree decl = 0;
2162 tree asmspec_tree;
2164 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2166 if (B_IN_SCOPE (b, external_scope))
2168 decl = b->decl;
2169 break;
2173 if (decl)
2175 /* FIXME: Objective-C has weird not-really-builtin functions
2176 which are supposed to be visible automatically. They wind up
2177 in the external scope because they're pushed before the file
2178 scope gets created. Catch this here and rebind them into the
2179 file scope. */
2180 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2182 bind (functionid, decl, file_scope,
2183 /*invisible=*/false, /*nested=*/true);
2184 return decl;
2186 else
2188 tree newtype = default_function_type;
2189 if (b->type)
2190 TREE_TYPE (decl) = b->type;
2191 /* Implicit declaration of a function already declared
2192 (somehow) in a different scope, or as a built-in.
2193 If this is the first time this has happened, warn;
2194 then recycle the old declaration but with the new type. */
2195 if (!C_DECL_IMPLICIT (decl))
2197 implicit_decl_warning (functionid, decl);
2198 C_DECL_IMPLICIT (decl) = 1;
2200 if (DECL_BUILT_IN (decl))
2202 newtype = build_type_attribute_variant (newtype,
2203 TYPE_ATTRIBUTES
2204 (TREE_TYPE (decl)));
2205 if (!comptypes (newtype, TREE_TYPE (decl)))
2207 warning ("incompatible implicit declaration of built-in"
2208 " function %qD", decl);
2209 newtype = TREE_TYPE (decl);
2212 else
2214 if (!comptypes (newtype, TREE_TYPE (decl)))
2216 error ("incompatible implicit declaration of function %qD",
2217 decl);
2218 locate_old_decl (decl, error);
2221 b->type = TREE_TYPE (decl);
2222 TREE_TYPE (decl) = newtype;
2223 bind (functionid, decl, current_scope,
2224 /*invisible=*/false, /*nested=*/true);
2225 return decl;
2229 /* Not seen before. */
2230 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2231 DECL_EXTERNAL (decl) = 1;
2232 TREE_PUBLIC (decl) = 1;
2233 C_DECL_IMPLICIT (decl) = 1;
2234 implicit_decl_warning (functionid, 0);
2235 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2236 if (asmspec_tree)
2237 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2239 /* C89 says implicit declarations are in the innermost block.
2240 So we record the decl in the standard fashion. */
2241 decl = pushdecl (decl);
2243 /* No need to call objc_check_decl here - it's a function type. */
2244 rest_of_decl_compilation (decl, 0, 0);
2246 /* Write a record describing this implicit function declaration
2247 to the prototypes file (if requested). */
2248 gen_aux_info_record (decl, 0, 1, 0);
2250 /* Possibly apply some default attributes to this implicit declaration. */
2251 decl_attributes (&decl, NULL_TREE, 0);
2253 return decl;
2256 /* Issue an error message for a reference to an undeclared variable
2257 ID, including a reference to a builtin outside of function-call
2258 context. Establish a binding of the identifier to error_mark_node
2259 in an appropriate scope, which will suppress further errors for the
2260 same identifier. */
2261 void
2262 undeclared_variable (tree id)
2264 static bool already = false;
2265 struct c_scope *scope;
2267 if (current_function_decl == 0)
2269 error ("%qE undeclared here (not in a function)", id);
2270 scope = current_scope;
2272 else
2274 error ("%qE undeclared (first use in this function)", id);
2276 if (! already)
2278 error ("(Each undeclared identifier is reported only once");
2279 error ("for each function it appears in.)");
2280 already = true;
2283 /* If we are parsing old-style parameter decls, current_function_decl
2284 will be nonnull but current_function_scope will be null. */
2285 scope = current_function_scope ? current_function_scope : current_scope;
2287 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2290 /* Subroutine of lookup_label, declare_label, define_label: construct a
2291 LABEL_DECL with all the proper frills. */
2293 static tree
2294 make_label (tree name, location_t location)
2296 tree label = build_decl (LABEL_DECL, name, void_type_node);
2298 DECL_CONTEXT (label) = current_function_decl;
2299 DECL_MODE (label) = VOIDmode;
2300 DECL_SOURCE_LOCATION (label) = location;
2302 return label;
2305 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2306 Create one if none exists so far for the current function.
2307 This is called when a label is used in a goto expression or
2308 has its address taken. */
2310 tree
2311 lookup_label (tree name)
2313 tree label;
2315 if (current_function_decl == 0)
2317 error ("label %qs referenced outside of any function",
2318 IDENTIFIER_POINTER (name));
2319 return 0;
2322 /* Use a label already defined or ref'd with this name, but not if
2323 it is inherited from a containing function and wasn't declared
2324 using __label__. */
2325 label = I_LABEL_DECL (name);
2326 if (label && (DECL_CONTEXT (label) == current_function_decl
2327 || C_DECLARED_LABEL_FLAG (label)))
2329 /* If the label has only been declared, update its apparent
2330 location to point here, for better diagnostics if it
2331 turns out not to have been defined. */
2332 if (!TREE_USED (label))
2333 DECL_SOURCE_LOCATION (label) = input_location;
2334 return label;
2337 /* No label binding for that identifier; make one. */
2338 label = make_label (name, input_location);
2340 /* Ordinary labels go in the current function scope. */
2341 bind (name, label, current_function_scope,
2342 /*invisible=*/false, /*nested=*/false);
2343 return label;
2346 /* Make a label named NAME in the current function, shadowing silently
2347 any that may be inherited from containing functions or containing
2348 scopes. This is called for __label__ declarations. */
2350 tree
2351 declare_label (tree name)
2353 struct c_binding *b = I_LABEL_BINDING (name);
2354 tree label;
2356 /* Check to make sure that the label hasn't already been declared
2357 at this scope */
2358 if (b && B_IN_CURRENT_SCOPE (b))
2360 error ("duplicate label declaration %qs", IDENTIFIER_POINTER (name));
2361 locate_old_decl (b->decl, error);
2363 /* Just use the previous declaration. */
2364 return b->decl;
2367 label = make_label (name, input_location);
2368 C_DECLARED_LABEL_FLAG (label) = 1;
2370 /* Declared labels go in the current scope. */
2371 bind (name, label, current_scope,
2372 /*invisible=*/false, /*nested=*/false);
2373 return label;
2376 /* Define a label, specifying the location in the source file.
2377 Return the LABEL_DECL node for the label, if the definition is valid.
2378 Otherwise return 0. */
2380 tree
2381 define_label (location_t location, tree name)
2383 /* Find any preexisting label with this name. It is an error
2384 if that label has already been defined in this function, or
2385 if there is a containing function with a declared label with
2386 the same name. */
2387 tree label = I_LABEL_DECL (name);
2389 if (label
2390 && ((DECL_CONTEXT (label) == current_function_decl
2391 && DECL_INITIAL (label) != 0)
2392 || (DECL_CONTEXT (label) != current_function_decl
2393 && C_DECLARED_LABEL_FLAG (label))))
2395 error ("%Hduplicate label %qD", &location, label);
2396 locate_old_decl (label, error);
2397 return 0;
2399 else if (label && DECL_CONTEXT (label) == current_function_decl)
2401 /* The label has been used or declared already in this function,
2402 but not defined. Update its location to point to this
2403 definition. */
2404 DECL_SOURCE_LOCATION (label) = location;
2406 else
2408 /* No label binding for that identifier; make one. */
2409 label = make_label (name, location);
2411 /* Ordinary labels go in the current function scope. */
2412 bind (name, label, current_function_scope,
2413 /*invisible=*/false, /*nested=*/false);
2416 if (warn_traditional && !in_system_header && lookup_name (name))
2417 warning ("%Htraditional C lacks a separate namespace for labels, "
2418 "identifier %qs conflicts", &location,
2419 IDENTIFIER_POINTER (name));
2421 /* Mark label as having been defined. */
2422 DECL_INITIAL (label) = error_mark_node;
2423 return label;
2426 /* Given NAME, an IDENTIFIER_NODE,
2427 return the structure (or union or enum) definition for that name.
2428 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2429 CODE says which kind of type the caller wants;
2430 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2431 If the wrong kind of type is found, an error is reported. */
2433 static tree
2434 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2436 struct c_binding *b = I_TAG_BINDING (name);
2437 int thislevel = 0;
2439 if (!b || !b->decl)
2440 return 0;
2442 /* We only care about whether it's in this level if
2443 thislevel_only was set or it might be a type clash. */
2444 if (thislevel_only || TREE_CODE (b->decl) != code)
2446 /* For our purposes, a tag in the external scope is the same as
2447 a tag in the file scope. (Primarily relevant to Objective-C
2448 and its builtin structure tags, which get pushed before the
2449 file scope is created.) */
2450 if (B_IN_CURRENT_SCOPE (b)
2451 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2452 thislevel = 1;
2455 if (thislevel_only && !thislevel)
2456 return 0;
2458 if (TREE_CODE (b->decl) != code)
2460 /* Definition isn't the kind we were looking for. */
2461 pending_invalid_xref = name;
2462 pending_invalid_xref_location = input_location;
2464 /* If in the same binding level as a declaration as a tag
2465 of a different type, this must not be allowed to
2466 shadow that tag, so give the error immediately.
2467 (For example, "struct foo; union foo;" is invalid.) */
2468 if (thislevel)
2469 pending_xref_error ();
2471 return b->decl;
2474 /* Print an error message now
2475 for a recent invalid struct, union or enum cross reference.
2476 We don't print them immediately because they are not invalid
2477 when used in the `struct foo;' construct for shadowing. */
2479 void
2480 pending_xref_error (void)
2482 if (pending_invalid_xref != 0)
2483 error ("%H%qs defined as wrong kind of tag",
2484 &pending_invalid_xref_location,
2485 IDENTIFIER_POINTER (pending_invalid_xref));
2486 pending_invalid_xref = 0;
2490 /* Look up NAME in the current scope and its superiors
2491 in the namespace of variables, functions and typedefs.
2492 Return a ..._DECL node of some kind representing its definition,
2493 or return 0 if it is undefined. */
2495 tree
2496 lookup_name (tree name)
2498 struct c_binding *b = I_SYMBOL_BINDING (name);
2499 if (b && !b->invisible)
2500 return b->decl;
2501 return 0;
2504 /* Similar to `lookup_name' but look only at the indicated scope. */
2506 static tree
2507 lookup_name_in_scope (tree name, struct c_scope *scope)
2509 struct c_binding *b;
2511 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2512 if (B_IN_SCOPE (b, scope))
2513 return b->decl;
2514 return 0;
2517 /* Create the predefined scalar types of C,
2518 and some nodes representing standard constants (0, 1, (void *) 0).
2519 Initialize the global scope.
2520 Make definitions for built-in primitive functions. */
2522 void
2523 c_init_decl_processing (void)
2525 tree endlink;
2526 tree ptr_ftype_void, ptr_ftype_ptr;
2527 location_t save_loc = input_location;
2529 /* Adds some ggc roots, and reserved words for c-parse.in. */
2530 c_parse_init ();
2532 current_function_decl = 0;
2534 gcc_obstack_init (&parser_obstack);
2536 /* Make the externals scope. */
2537 push_scope ();
2538 external_scope = current_scope;
2540 /* Declarations from c_common_nodes_and_builtins must not be associated
2541 with this input file, lest we get differences between using and not
2542 using preprocessed headers. */
2543 #ifdef USE_MAPPED_LOCATION
2544 input_location = BUILTINS_LOCATION;
2545 #else
2546 input_location.file = "<built-in>";
2547 input_location.line = 0;
2548 #endif
2550 build_common_tree_nodes (flag_signed_char, false);
2552 c_common_nodes_and_builtins ();
2554 /* In C, comparisons and TRUTH_* expressions have type int. */
2555 truthvalue_type_node = integer_type_node;
2556 truthvalue_true_node = integer_one_node;
2557 truthvalue_false_node = integer_zero_node;
2559 /* Even in C99, which has a real boolean type. */
2560 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2561 boolean_type_node));
2563 endlink = void_list_node;
2564 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2565 ptr_ftype_ptr
2566 = build_function_type (ptr_type_node,
2567 tree_cons (NULL_TREE, ptr_type_node, endlink));
2569 input_location = save_loc;
2571 pedantic_lvalues = true;
2573 make_fname_decl = c_make_fname_decl;
2574 start_fname_decls ();
2577 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2578 decl, NAME is the initialization string and TYPE_DEP indicates whether
2579 NAME depended on the type of the function. As we don't yet implement
2580 delayed emission of static data, we mark the decl as emitted
2581 so it is not placed in the output. Anything using it must therefore pull
2582 out the STRING_CST initializer directly. FIXME. */
2584 static tree
2585 c_make_fname_decl (tree id, int type_dep)
2587 const char *name = fname_as_string (type_dep);
2588 tree decl, type, init;
2589 size_t length = strlen (name);
2591 type = build_array_type
2592 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2593 build_index_type (size_int (length)));
2595 decl = build_decl (VAR_DECL, id, type);
2597 TREE_STATIC (decl) = 1;
2598 TREE_READONLY (decl) = 1;
2599 DECL_ARTIFICIAL (decl) = 1;
2601 init = build_string (length + 1, name);
2602 free ((char *) name);
2603 TREE_TYPE (init) = type;
2604 DECL_INITIAL (decl) = init;
2606 TREE_USED (decl) = 1;
2608 if (current_function_decl)
2610 DECL_CONTEXT (decl) = current_function_decl;
2611 bind (id, decl, current_function_scope,
2612 /*invisible=*/false, /*nested=*/false);
2615 finish_decl (decl, init, NULL_TREE);
2617 return decl;
2620 /* Return a definition for a builtin function named NAME and whose data type
2621 is TYPE. TYPE should be a function type with argument types.
2622 FUNCTION_CODE tells later passes how to compile calls to this function.
2623 See tree.h for its possible values.
2625 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2626 the name to be called if we can't opencode the function. If
2627 ATTRS is nonzero, use that for the function's attribute list. */
2629 tree
2630 builtin_function (const char *name, tree type, int function_code,
2631 enum built_in_class cl, const char *library_name,
2632 tree attrs)
2634 tree id = get_identifier (name);
2635 tree decl = build_decl (FUNCTION_DECL, id, type);
2636 TREE_PUBLIC (decl) = 1;
2637 DECL_EXTERNAL (decl) = 1;
2638 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2639 DECL_BUILT_IN_CLASS (decl) = cl;
2640 DECL_FUNCTION_CODE (decl) = function_code;
2641 if (library_name)
2642 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2644 /* Should never be called on a symbol with a preexisting meaning. */
2645 gcc_assert (!I_SYMBOL_BINDING (id));
2647 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2649 /* Builtins in the implementation namespace are made visible without
2650 needing to be explicitly declared. See push_file_scope. */
2651 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2653 TREE_CHAIN (decl) = visible_builtins;
2654 visible_builtins = decl;
2657 /* Possibly apply some default attributes to this built-in function. */
2658 if (attrs)
2659 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2660 else
2661 decl_attributes (&decl, NULL_TREE, 0);
2663 return decl;
2666 /* Called when a declaration is seen that contains no names to declare.
2667 If its type is a reference to a structure, union or enum inherited
2668 from a containing scope, shadow that tag name for the current scope
2669 with a forward reference.
2670 If its type defines a new named structure or union
2671 or defines an enum, it is valid but we need not do anything here.
2672 Otherwise, it is an error. */
2674 void
2675 shadow_tag (const struct c_declspecs *declspecs)
2677 shadow_tag_warned (declspecs, 0);
2680 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2681 but no pedwarn. */
2682 void
2683 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2685 bool found_tag = false;
2687 pending_invalid_xref = 0;
2689 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2691 tree value = declspecs->type;
2692 enum tree_code code = TREE_CODE (value);
2694 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2695 /* Used to test also that TYPE_SIZE (value) != 0.
2696 That caused warning for `struct foo;' at top level in the file. */
2698 tree name = TYPE_NAME (value);
2699 tree t;
2701 found_tag = true;
2703 if (name == 0)
2705 if (warned != 1 && code != ENUMERAL_TYPE)
2706 /* Empty unnamed enum OK */
2708 pedwarn ("unnamed struct/union that defines no instances");
2709 warned = 1;
2712 else
2714 t = lookup_tag (code, name, 1);
2716 if (t == 0)
2718 t = make_node (code);
2719 pushtag (name, t);
2723 else
2725 if (warned != 1 && !in_system_header)
2727 pedwarn ("useless type name in empty declaration");
2728 warned = 1;
2732 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2734 pedwarn ("useless type name in empty declaration");
2735 warned = 1;
2738 if (declspecs->inline_p)
2740 error ("%<inline%> in empty declaration");
2741 warned = 1;
2744 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2746 error ("%<auto%> in file-scope empty declaration");
2747 warned = 1;
2750 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2752 error ("%<register%> in file-scope empty declaration");
2753 warned = 1;
2756 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2758 warning ("useless storage class specifier in empty declaration");
2759 warned = 2;
2762 if (!warned && !in_system_header && declspecs->thread_p)
2764 warning ("useless %<__thread%> in empty declaration");
2765 warned = 2;
2768 if (!warned && !in_system_header && (declspecs->const_p
2769 || declspecs->volatile_p
2770 || declspecs->restrict_p))
2772 warning ("useless type qualifier in empty declaration");
2773 warned = 2;
2776 if (warned != 1)
2778 if (!found_tag)
2779 pedwarn ("empty declaration");
2784 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2785 bits. SPECS represents declaration specifiers that the grammar
2786 only permits to contain type qualifiers and attributes. */
2789 quals_from_declspecs (const struct c_declspecs *specs)
2791 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2792 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2793 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2794 gcc_assert (!specs->type
2795 && !specs->decl_attr
2796 && specs->typespec_word == cts_none
2797 && specs->storage_class == csc_none
2798 && !specs->typedef_p
2799 && !specs->explicit_signed_p
2800 && !specs->deprecated_p
2801 && !specs->long_p
2802 && !specs->long_long_p
2803 && !specs->short_p
2804 && !specs->signed_p
2805 && !specs->unsigned_p
2806 && !specs->complex_p
2807 && !specs->inline_p
2808 && !specs->thread_p);
2809 return quals;
2812 /* Construct an array declarator. EXPR is the expression inside [], or
2813 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2814 to the pointer to which a parameter array is converted). STATIC_P is
2815 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2816 is true if the array is [*], a VLA of unspecified length which is
2817 nevertheless a complete type (not currently implemented by GCC),
2818 false otherwise. The field for the contained declarator is left to be
2819 filled in by set_array_declarator_inner. */
2821 struct c_declarator *
2822 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2823 bool vla_unspec_p)
2825 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2826 struct c_declarator);
2827 declarator->kind = cdk_array;
2828 declarator->declarator = 0;
2829 declarator->u.array.dimen = expr;
2830 if (quals)
2832 declarator->u.array.attrs = quals->attrs;
2833 declarator->u.array.quals = quals_from_declspecs (quals);
2835 else
2837 declarator->u.array.attrs = NULL_TREE;
2838 declarator->u.array.quals = 0;
2840 declarator->u.array.static_p = static_p;
2841 declarator->u.array.vla_unspec_p = vla_unspec_p;
2842 if (pedantic && !flag_isoc99)
2844 if (static_p || quals != NULL)
2845 pedwarn ("ISO C90 does not support %<static%> or type "
2846 "qualifiers in parameter array declarators");
2847 if (vla_unspec_p)
2848 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2850 if (vla_unspec_p)
2851 warning ("GCC does not yet properly implement %<[*]%> array declarators");
2852 return declarator;
2855 /* Set the contained declarator of an array declarator. DECL is the
2856 declarator, as constructed by build_array_declarator; INNER is what
2857 appears on the left of the []. ABSTRACT_P is true if it is an
2858 abstract declarator, false otherwise; this is used to reject static
2859 and type qualifiers in abstract declarators, where they are not in
2860 the C99 grammar (subject to possible change in DR#289). */
2862 struct c_declarator *
2863 set_array_declarator_inner (struct c_declarator *decl,
2864 struct c_declarator *inner, bool abstract_p)
2866 decl->declarator = inner;
2867 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2868 || decl->u.array.attrs != NULL_TREE
2869 || decl->u.array.static_p))
2870 error ("static or type qualifiers in abstract declarator");
2871 return decl;
2874 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2876 tree
2877 groktypename (struct c_type_name *type_name)
2879 tree type;
2880 tree attrs = type_name->specs->attrs;
2882 type_name->specs->attrs = NULL_TREE;
2884 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2885 false, NULL);
2887 /* Apply attributes. */
2888 decl_attributes (&type, attrs, 0);
2890 return type;
2893 /* Decode a declarator in an ordinary declaration or data definition.
2894 This is called as soon as the type information and variable name
2895 have been parsed, before parsing the initializer if any.
2896 Here we create the ..._DECL node, fill in its type,
2897 and put it on the list of decls for the current context.
2898 The ..._DECL node is returned as the value.
2900 Exception: for arrays where the length is not specified,
2901 the type is left null, to be filled in by `finish_decl'.
2903 Function definitions do not come here; they go to start_function
2904 instead. However, external and forward declarations of functions
2905 do go through here. Structure field declarations are done by
2906 grokfield and not through here. */
2908 tree
2909 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
2910 bool initialized, tree attributes)
2912 tree decl;
2913 tree tem;
2915 /* An object declared as __attribute__((deprecated)) suppresses
2916 warnings of uses of other deprecated items. */
2917 if (lookup_attribute ("deprecated", attributes))
2918 deprecated_state = DEPRECATED_SUPPRESS;
2920 decl = grokdeclarator (declarator, declspecs,
2921 NORMAL, initialized, NULL);
2923 deprecated_state = DEPRECATED_NORMAL;
2925 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2926 && MAIN_NAME_P (DECL_NAME (decl)))
2927 warning ("%J%qD is usually a function", decl, decl);
2929 if (initialized)
2930 /* Is it valid for this decl to have an initializer at all?
2931 If not, set INITIALIZED to zero, which will indirectly
2932 tell 'finish_decl' to ignore the initializer once it is parsed. */
2933 switch (TREE_CODE (decl))
2935 case TYPE_DECL:
2936 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
2937 initialized = 0;
2938 break;
2940 case FUNCTION_DECL:
2941 error ("function %qD is initialized like a variable", decl);
2942 initialized = 0;
2943 break;
2945 case PARM_DECL:
2946 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2947 error ("parameter %qD is initialized", decl);
2948 initialized = 0;
2949 break;
2951 default:
2952 /* Don't allow initializations for incomplete types except for
2953 arrays which might be completed by the initialization. */
2955 /* This can happen if the array size is an undefined macro.
2956 We already gave a warning, so we don't need another one. */
2957 if (TREE_TYPE (decl) == error_mark_node)
2958 initialized = 0;
2959 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2961 /* A complete type is ok if size is fixed. */
2963 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2964 || C_DECL_VARIABLE_SIZE (decl))
2966 error ("variable-sized object may not be initialized");
2967 initialized = 0;
2970 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2972 error ("variable %qD has initializer but incomplete type", decl);
2973 initialized = 0;
2975 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2977 error ("elements of array %qD have incomplete type", decl);
2978 initialized = 0;
2982 if (initialized)
2984 if (current_scope == file_scope)
2985 TREE_STATIC (decl) = 1;
2987 /* Tell 'pushdecl' this is an initialized decl
2988 even though we don't yet have the initializer expression.
2989 Also tell 'finish_decl' it may store the real initializer. */
2990 DECL_INITIAL (decl) = error_mark_node;
2993 /* If this is a function declaration, write a record describing it to the
2994 prototypes file (if requested). */
2996 if (TREE_CODE (decl) == FUNCTION_DECL)
2997 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2999 /* ANSI specifies that a tentative definition which is not merged with
3000 a non-tentative definition behaves exactly like a definition with an
3001 initializer equal to zero. (Section 3.7.2)
3003 -fno-common gives strict ANSI behavior, though this tends to break
3004 a large body of code that grew up without this rule.
3006 Thread-local variables are never common, since there's no entrenched
3007 body of code to break, and it allows more efficient variable references
3008 in the presence of dynamic linking. */
3010 if (TREE_CODE (decl) == VAR_DECL
3011 && !initialized
3012 && TREE_PUBLIC (decl)
3013 && !DECL_THREAD_LOCAL (decl)
3014 && !flag_no_common)
3015 DECL_COMMON (decl) = 1;
3017 /* Set attributes here so if duplicate decl, will have proper attributes. */
3018 decl_attributes (&decl, attributes, 0);
3020 if (TREE_CODE (decl) == FUNCTION_DECL
3021 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3023 struct c_declarator *ce = declarator;
3025 if (ce->kind == cdk_pointer)
3026 ce = declarator->declarator;
3027 if (ce->kind == cdk_function)
3029 tree args = ce->u.arg_info->parms;
3030 for (; args; args = TREE_CHAIN (args))
3032 tree type = TREE_TYPE (args);
3033 if (type && INTEGRAL_TYPE_P (type)
3034 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3035 DECL_ARG_TYPE (args) = integer_type_node;
3040 if (TREE_CODE (decl) == FUNCTION_DECL
3041 && DECL_DECLARED_INLINE_P (decl)
3042 && DECL_UNINLINABLE (decl)
3043 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3044 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3046 /* Add this decl to the current scope.
3047 TEM may equal DECL or it may be a previous decl of the same name. */
3048 tem = pushdecl (decl);
3050 if (initialized && DECL_EXTERNAL (tem))
3052 DECL_EXTERNAL (tem) = 0;
3053 TREE_STATIC (tem) = 1;
3056 return tem;
3059 /* Finish processing of a declaration;
3060 install its initial value.
3061 If the length of an array type is not known before,
3062 it must be determined now, from the initial value, or it is an error. */
3064 void
3065 finish_decl (tree decl, tree init, tree asmspec_tree)
3067 tree type = TREE_TYPE (decl);
3068 int was_incomplete = (DECL_SIZE (decl) == 0);
3069 const char *asmspec = 0;
3071 /* If a name was specified, get the string. */
3072 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3073 && DECL_FILE_SCOPE_P (decl))
3074 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3075 if (asmspec_tree)
3076 asmspec = TREE_STRING_POINTER (asmspec_tree);
3078 /* If `start_decl' didn't like having an initialization, ignore it now. */
3079 if (init != 0 && DECL_INITIAL (decl) == 0)
3080 init = 0;
3082 /* Don't crash if parm is initialized. */
3083 if (TREE_CODE (decl) == PARM_DECL)
3084 init = 0;
3086 if (init)
3087 store_init_value (decl, init);
3089 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3090 || TREE_CODE (decl) == FUNCTION_DECL
3091 || TREE_CODE (decl) == FIELD_DECL))
3092 objc_check_decl (decl);
3094 /* Deduce size of array from initialization, if not already known. */
3095 if (TREE_CODE (type) == ARRAY_TYPE
3096 && TYPE_DOMAIN (type) == 0
3097 && TREE_CODE (decl) != TYPE_DECL)
3099 int do_default
3100 = (TREE_STATIC (decl)
3101 /* Even if pedantic, an external linkage array
3102 may have incomplete type at first. */
3103 ? pedantic && !TREE_PUBLIC (decl)
3104 : !DECL_EXTERNAL (decl));
3105 int failure
3106 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3108 /* Get the completed type made by complete_array_type. */
3109 type = TREE_TYPE (decl);
3111 if (failure == 1)
3112 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3114 else if (failure == 2)
3116 if (do_default)
3117 error ("%Jarray size missing in %qD", decl, decl);
3118 /* If a `static' var's size isn't known,
3119 make it extern as well as static, so it does not get
3120 allocated.
3121 If it is not `static', then do not mark extern;
3122 finish_incomplete_decl will give it a default size
3123 and it will get allocated. */
3124 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3125 DECL_EXTERNAL (decl) = 1;
3128 /* TYPE_MAX_VALUE is always one less than the number of elements
3129 in the array, because we start counting at zero. Therefore,
3130 warn only if the value is less than zero. */
3131 else if (pedantic && TYPE_DOMAIN (type) != 0
3132 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3133 error ("%Jzero or negative size array %qD", decl, decl);
3135 layout_decl (decl, 0);
3138 if (TREE_CODE (decl) == VAR_DECL)
3140 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3141 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3142 layout_decl (decl, 0);
3144 if (DECL_SIZE (decl) == 0
3145 /* Don't give an error if we already gave one earlier. */
3146 && TREE_TYPE (decl) != error_mark_node
3147 && (TREE_STATIC (decl)
3148 /* A static variable with an incomplete type
3149 is an error if it is initialized.
3150 Also if it is not file scope.
3151 Otherwise, let it through, but if it is not `extern'
3152 then it may cause an error message later. */
3153 ? (DECL_INITIAL (decl) != 0
3154 || !DECL_FILE_SCOPE_P (decl))
3155 /* An automatic variable with an incomplete type
3156 is an error. */
3157 : !DECL_EXTERNAL (decl)))
3159 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3160 TREE_TYPE (decl) = error_mark_node;
3163 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3164 && DECL_SIZE (decl) != 0)
3166 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3167 constant_expression_warning (DECL_SIZE (decl));
3168 else
3169 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3172 if (TREE_USED (type))
3173 TREE_USED (decl) = 1;
3176 /* If this is a function and an assembler name is specified, reset DECL_RTL
3177 so we can give it its new name. Also, update built_in_decls if it
3178 was a normal built-in. */
3179 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3181 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3183 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
3184 set_user_assembler_name (builtin, asmspec);
3185 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
3186 init_block_move_fn (asmspec);
3187 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
3188 init_block_clear_fn (asmspec);
3190 set_user_assembler_name (decl, asmspec);
3193 /* If #pragma weak was used, mark the decl weak now. */
3194 if (current_scope == file_scope)
3195 maybe_apply_pragma_weak (decl);
3197 /* If this is a variable definition, determine its ELF visibility. */
3198 if (TREE_CODE (decl) == VAR_DECL
3199 && TREE_STATIC (decl)
3200 && !DECL_EXTERNAL (decl))
3201 c_determine_visibility (decl);
3203 /* Output the assembler code and/or RTL code for variables and functions,
3204 unless the type is an undefined structure or union.
3205 If not, it will get done when the type is completed. */
3207 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3209 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3210 if (c_dialect_objc ())
3211 objc_check_decl (decl);
3213 if (asmspec)
3215 /* If this is not a static variable, issue a warning.
3216 It doesn't make any sense to give an ASMSPEC for an
3217 ordinary, non-register local variable. Historically,
3218 GCC has accepted -- but ignored -- the ASMSPEC in
3219 this case. */
3220 if (! DECL_FILE_SCOPE_P (decl)
3221 && TREE_CODE (decl) == VAR_DECL
3222 && !C_DECL_REGISTER (decl)
3223 && !TREE_STATIC (decl))
3224 warning ("%Jignoring asm-specifier for non-static local "
3225 "variable %qD", decl, decl);
3226 else if (C_DECL_REGISTER (decl))
3227 change_decl_assembler_name (decl, get_identifier (asmspec));
3228 else
3229 set_user_assembler_name (decl, asmspec);
3232 if (DECL_FILE_SCOPE_P (decl))
3234 if (DECL_INITIAL (decl) == NULL_TREE
3235 || DECL_INITIAL (decl) == error_mark_node)
3236 /* Don't output anything
3237 when a tentative file-scope definition is seen.
3238 But at end of compilation, do output code for them. */
3239 DECL_DEFER_OUTPUT (decl) = 1;
3240 rest_of_decl_compilation (decl, true, 0);
3242 else
3244 /* In conjunction with an ASMSPEC, the `register'
3245 keyword indicates that we should place the variable
3246 in a particular register. */
3247 if (asmspec && C_DECL_REGISTER (decl))
3249 DECL_HARD_REGISTER (decl) = 1;
3250 /* This cannot be done for a structure with volatile
3251 fields, on which DECL_REGISTER will have been
3252 reset. */
3253 if (!DECL_REGISTER (decl))
3254 error ("cannot put object with volatile field into register");
3257 if (TREE_CODE (decl) != FUNCTION_DECL)
3259 /* If we're building a variable sized type, and we might be
3260 reachable other than via the top of the current binding
3261 level, then create a new BIND_EXPR so that we deallocate
3262 the object at the right time. */
3263 /* Note that DECL_SIZE can be null due to errors. */
3264 if (DECL_SIZE (decl)
3265 && !TREE_CONSTANT (DECL_SIZE (decl))
3266 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3268 tree bind;
3269 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3270 TREE_SIDE_EFFECTS (bind) = 1;
3271 add_stmt (bind);
3272 BIND_EXPR_BODY (bind) = push_stmt_list ();
3274 add_stmt (build_stmt (DECL_EXPR, decl));
3279 if (!DECL_FILE_SCOPE_P (decl))
3281 /* Recompute the RTL of a local array now
3282 if it used to be an incomplete type. */
3283 if (was_incomplete
3284 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3286 /* If we used it already as memory, it must stay in memory. */
3287 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3288 /* If it's still incomplete now, no init will save it. */
3289 if (DECL_SIZE (decl) == 0)
3290 DECL_INITIAL (decl) = 0;
3295 /* If this was marked 'used', be sure it will be output. */
3296 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3297 mark_decl_referenced (decl);
3299 if (TREE_CODE (decl) == TYPE_DECL)
3301 if (!DECL_FILE_SCOPE_P (decl)
3302 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3303 add_stmt (build_stmt (DECL_EXPR, decl));
3305 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3308 /* At the end of a declaration, throw away any variable type sizes
3309 of types defined inside that declaration. There is no use
3310 computing them in the following function definition. */
3311 if (current_scope == file_scope)
3312 get_pending_sizes ();
3314 /* Install a cleanup (aka destructor) if one was given. */
3315 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3317 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3318 if (attr)
3320 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3321 tree cleanup_decl = lookup_name (cleanup_id);
3322 tree cleanup;
3324 /* Build "cleanup(&decl)" for the destructor. */
3325 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3326 cleanup = build_tree_list (NULL_TREE, cleanup);
3327 cleanup = build_function_call (cleanup_decl, cleanup);
3329 /* Don't warn about decl unused; the cleanup uses it. */
3330 TREE_USED (decl) = 1;
3331 TREE_USED (cleanup_decl) = 1;
3333 /* Initialize EH, if we've been told to do so. */
3334 if (flag_exceptions && !c_eh_initialized_p)
3336 c_eh_initialized_p = true;
3337 eh_personality_libfunc
3338 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3339 ? "__gcc_personality_sj0"
3340 : "__gcc_personality_v0");
3341 using_eh_for_cleanups ();
3344 push_cleanup (decl, cleanup, false);
3349 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3351 tree
3352 grokparm (const struct c_parm *parm)
3354 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3355 NULL);
3357 decl_attributes (&decl, parm->attrs, 0);
3359 return decl;
3362 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3363 and push that on the current scope. */
3365 void
3366 push_parm_decl (const struct c_parm *parm)
3368 tree decl;
3370 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3371 decl_attributes (&decl, parm->attrs, 0);
3373 decl = pushdecl (decl);
3375 finish_decl (decl, NULL_TREE, NULL_TREE);
3378 /* Mark all the parameter declarations to date as forward decls.
3379 Also diagnose use of this extension. */
3381 void
3382 mark_forward_parm_decls (void)
3384 struct c_binding *b;
3386 if (pedantic && !current_scope->warned_forward_parm_decls)
3388 pedwarn ("ISO C forbids forward parameter declarations");
3389 current_scope->warned_forward_parm_decls = true;
3392 for (b = current_scope->bindings; b; b = b->prev)
3393 if (TREE_CODE (b->decl) == PARM_DECL)
3394 TREE_ASM_WRITTEN (b->decl) = 1;
3397 static GTY(()) int compound_literal_number;
3399 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3400 literal, which may be an incomplete array type completed by the
3401 initializer; INIT is a CONSTRUCTOR that initializes the compound
3402 literal. */
3404 tree
3405 build_compound_literal (tree type, tree init)
3407 /* We do not use start_decl here because we have a type, not a declarator;
3408 and do not use finish_decl because the decl should be stored inside
3409 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3410 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3411 tree complit;
3412 tree stmt;
3413 DECL_EXTERNAL (decl) = 0;
3414 TREE_PUBLIC (decl) = 0;
3415 TREE_STATIC (decl) = (current_scope == file_scope);
3416 DECL_CONTEXT (decl) = current_function_decl;
3417 TREE_USED (decl) = 1;
3418 TREE_TYPE (decl) = type;
3419 TREE_READONLY (decl) = TYPE_READONLY (type);
3420 store_init_value (decl, init);
3422 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3424 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3426 gcc_assert (!failure);
3429 type = TREE_TYPE (decl);
3430 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3431 return error_mark_node;
3433 stmt = build_stmt (DECL_EXPR, decl);
3434 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3435 TREE_SIDE_EFFECTS (complit) = 1;
3437 layout_decl (decl, 0);
3439 if (TREE_STATIC (decl))
3441 /* This decl needs a name for the assembler output. We also need
3442 a unique suffix to be added to the name. */
3443 char *name;
3445 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3446 compound_literal_number);
3447 compound_literal_number++;
3448 DECL_NAME (decl) = get_identifier (name);
3449 DECL_DEFER_OUTPUT (decl) = 1;
3450 DECL_COMDAT (decl) = 1;
3451 DECL_ARTIFICIAL (decl) = 1;
3452 pushdecl (decl);
3453 rest_of_decl_compilation (decl, 1, 0);
3456 return complit;
3459 /* Make TYPE a complete type based on INITIAL_VALUE.
3460 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3461 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3464 complete_array_type (tree type, tree initial_value, int do_default)
3466 tree maxindex = NULL_TREE;
3467 int value = 0;
3469 if (initial_value)
3471 /* Note MAXINDEX is really the maximum index,
3472 one less than the size. */
3473 if (TREE_CODE (initial_value) == STRING_CST)
3475 int eltsize
3476 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3477 maxindex = build_int_cst (NULL_TREE,
3478 (TREE_STRING_LENGTH (initial_value)
3479 / eltsize) - 1);
3481 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3483 tree elts = CONSTRUCTOR_ELTS (initial_value);
3484 maxindex = build_int_cst (NULL_TREE, -1);
3485 for (; elts; elts = TREE_CHAIN (elts))
3487 if (TREE_PURPOSE (elts))
3488 maxindex = TREE_PURPOSE (elts);
3489 else
3490 maxindex = fold (build2 (PLUS_EXPR, integer_type_node,
3491 maxindex, integer_one_node));
3494 else
3496 /* Make an error message unless that happened already. */
3497 if (initial_value != error_mark_node)
3498 value = 1;
3500 /* Prevent further error messages. */
3501 maxindex = build_int_cst (NULL_TREE, 0);
3505 if (!maxindex)
3507 if (do_default)
3508 maxindex = build_int_cst (NULL_TREE, 0);
3509 value = 2;
3512 if (maxindex)
3514 TYPE_DOMAIN (type) = build_index_type (maxindex);
3516 gcc_assert (TREE_TYPE (maxindex));
3519 /* Lay out the type now that we can get the real answer. */
3521 layout_type (type);
3523 return value;
3526 /* Determine whether TYPE is a structure with a flexible array member,
3527 or a union containing such a structure (possibly recursively). */
3529 static bool
3530 flexible_array_type_p (tree type)
3532 tree x;
3533 switch (TREE_CODE (type))
3535 case RECORD_TYPE:
3536 x = TYPE_FIELDS (type);
3537 if (x == NULL_TREE)
3538 return false;
3539 while (TREE_CHAIN (x) != NULL_TREE)
3540 x = TREE_CHAIN (x);
3541 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3542 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3543 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3544 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3545 return true;
3546 return false;
3547 case UNION_TYPE:
3548 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3550 if (flexible_array_type_p (TREE_TYPE (x)))
3551 return true;
3553 return false;
3554 default:
3555 return false;
3559 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3560 replacing with appropriate values if they are invalid. */
3561 static void
3562 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3564 tree type_mv;
3565 unsigned int max_width;
3566 unsigned HOST_WIDE_INT w;
3567 const char *name = orig_name ? orig_name: _("<anonymous>");
3569 /* Necessary? */
3570 STRIP_NOPS (*width);
3572 /* Detect and ignore out of range field width and process valid
3573 field widths. */
3574 if (TREE_CODE (*width) != INTEGER_CST)
3576 error ("bit-field %qs width not an integer constant", name);
3577 *width = integer_one_node;
3579 else
3581 constant_expression_warning (*width);
3582 if (tree_int_cst_sgn (*width) < 0)
3584 error ("negative width in bit-field %qs", name);
3585 *width = integer_one_node;
3587 else if (integer_zerop (*width) && orig_name)
3589 error ("zero width for bit-field %qs", name);
3590 *width = integer_one_node;
3594 /* Detect invalid bit-field type. */
3595 if (TREE_CODE (*type) != INTEGER_TYPE
3596 && TREE_CODE (*type) != BOOLEAN_TYPE
3597 && TREE_CODE (*type) != ENUMERAL_TYPE)
3599 error ("bit-field %qs has invalid type", name);
3600 *type = unsigned_type_node;
3603 type_mv = TYPE_MAIN_VARIANT (*type);
3604 if (pedantic
3605 && type_mv != integer_type_node
3606 && type_mv != unsigned_type_node
3607 && type_mv != boolean_type_node)
3608 pedwarn ("type of bit-field %qs is a GCC extension", name);
3610 if (type_mv == boolean_type_node)
3611 max_width = CHAR_TYPE_SIZE;
3612 else
3613 max_width = TYPE_PRECISION (*type);
3615 if (0 < compare_tree_int (*width, max_width))
3617 error ("width of %qs exceeds its type", name);
3618 w = max_width;
3619 *width = build_int_cst (NULL_TREE, w);
3621 else
3622 w = tree_low_cst (*width, 1);
3624 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3626 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3627 if (!lt
3628 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3629 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3630 warning ("%qs is narrower than values of its type", name);
3634 /* Given declspecs and a declarator,
3635 determine the name and type of the object declared
3636 and construct a ..._DECL node for it.
3637 (In one case we can return a ..._TYPE node instead.
3638 For invalid input we sometimes return 0.)
3640 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3642 DECL_CONTEXT says which syntactic context this declaration is in:
3643 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3644 FUNCDEF for a function definition. Like NORMAL but a few different
3645 error messages in each case. Return value may be zero meaning
3646 this definition is too screwy to try to parse.
3647 PARM for a parameter declaration (either within a function prototype
3648 or before a function body). Make a PARM_DECL, or return void_type_node.
3649 TYPENAME if for a typename (in a cast or sizeof).
3650 Don't make a DECL node; just return the ..._TYPE node.
3651 FIELD for a struct or union field; make a FIELD_DECL.
3652 INITIALIZED is true if the decl has an initializer.
3653 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3654 representing the width of the bit-field.
3656 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3657 It may also be so in the PARM case, for a prototype where the
3658 argument type is specified but not the name.
3660 This function is where the complicated C meanings of `static'
3661 and `extern' are interpreted. */
3663 static tree
3664 grokdeclarator (const struct c_declarator *declarator,
3665 struct c_declspecs *declspecs,
3666 enum decl_context decl_context, bool initialized, tree *width)
3668 tree type = declspecs->type;
3669 bool threadp = declspecs->thread_p;
3670 enum c_storage_class storage_class = declspecs->storage_class;
3671 int constp;
3672 int restrictp;
3673 int volatilep;
3674 int type_quals = TYPE_UNQUALIFIED;
3675 const char *name, *orig_name;
3676 tree typedef_type = 0;
3677 int funcdef_flag = 0;
3678 bool funcdef_syntax = false;
3679 int size_varies = 0;
3680 tree decl_attr = declspecs->decl_attr;
3681 int array_ptr_quals = TYPE_UNQUALIFIED;
3682 tree array_ptr_attrs = NULL_TREE;
3683 int array_parm_static = 0;
3684 tree returned_attrs = NULL_TREE;
3685 bool bitfield = width != NULL;
3686 tree element_type;
3687 struct c_arg_info *arg_info = 0;
3689 if (decl_context == FUNCDEF)
3690 funcdef_flag = 1, decl_context = NORMAL;
3692 /* Look inside a declarator for the name being declared
3693 and get it as a string, for an error message. */
3695 const struct c_declarator *decl = declarator;
3696 name = 0;
3698 while (decl)
3699 switch (decl->kind)
3701 case cdk_function:
3702 case cdk_array:
3703 case cdk_pointer:
3704 funcdef_syntax = (decl->kind == cdk_function);
3705 decl = decl->declarator;
3706 break;
3708 case cdk_attrs:
3709 decl = decl->declarator;
3710 break;
3712 case cdk_id:
3713 if (decl->u.id)
3714 name = IDENTIFIER_POINTER (decl->u.id);
3715 decl = 0;
3716 break;
3718 default:
3719 gcc_unreachable ();
3721 orig_name = name;
3722 if (name == 0)
3723 name = "type name";
3726 /* A function definition's declarator must have the form of
3727 a function declarator. */
3729 if (funcdef_flag && !funcdef_syntax)
3730 return 0;
3732 /* If this looks like a function definition, make it one,
3733 even if it occurs where parms are expected.
3734 Then store_parm_decls will reject it and not use it as a parm. */
3735 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3736 decl_context = PARM;
3738 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3739 warn_deprecated_use (declspecs->type);
3741 typedef_type = type;
3742 size_varies = C_TYPE_VARIABLE_SIZE (type);
3744 /* Diagnose defaulting to "int". */
3746 if (declspecs->default_int_p && !in_system_header)
3748 /* Issue a warning if this is an ISO C 99 program or if
3749 -Wreturn-type and this is a function, or if -Wimplicit;
3750 prefer the former warning since it is more explicit. */
3751 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3752 && funcdef_flag)
3753 warn_about_return_type = 1;
3754 else if (warn_implicit_int || flag_isoc99)
3755 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3758 /* Adjust the type if a bit-field is being declared,
3759 -funsigned-bitfields applied and the type is not explicitly
3760 "signed". */
3761 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3762 && TREE_CODE (type) == INTEGER_TYPE)
3763 type = c_common_unsigned_type (type);
3765 /* Check the type and width of a bit-field. */
3766 if (bitfield)
3767 check_bitfield_type_and_width (&type, width, orig_name);
3769 /* Figure out the type qualifiers for the declaration. There are
3770 two ways a declaration can become qualified. One is something
3771 like `const int i' where the `const' is explicit. Another is
3772 something like `typedef const int CI; CI i' where the type of the
3773 declaration contains the `const'. A third possibility is that
3774 there is a type qualifier on the element type of a typedefed
3775 array type, in which case we should extract that qualifier so
3776 that c_apply_type_quals_to_decls receives the full list of
3777 qualifiers to work with (C90 is not entirely clear about whether
3778 duplicate qualifiers should be diagnosed in this case, but it
3779 seems most appropriate to do so). */
3780 element_type = strip_array_types (type);
3781 constp = declspecs->const_p + TYPE_READONLY (element_type);
3782 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3783 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3784 if (pedantic && !flag_isoc99)
3786 if (constp > 1)
3787 pedwarn ("duplicate %<const%>");
3788 if (restrictp > 1)
3789 pedwarn ("duplicate %<restrict%>");
3790 if (volatilep > 1)
3791 pedwarn ("duplicate %<volatile%>");
3793 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3794 type = TYPE_MAIN_VARIANT (type);
3795 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3796 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3797 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3799 /* Warn about storage classes that are invalid for certain
3800 kinds of declarations (parameters, typenames, etc.). */
3802 if (funcdef_flag
3803 && (threadp
3804 || storage_class == csc_auto
3805 || storage_class == csc_register
3806 || storage_class == csc_typedef))
3808 if (storage_class == csc_auto
3809 && (pedantic || current_scope == file_scope))
3810 pedwarn ("function definition declared %<auto%>");
3811 if (storage_class == csc_register)
3812 error ("function definition declared %<register%>");
3813 if (storage_class == csc_typedef)
3814 error ("function definition declared %<typedef%>");
3815 if (threadp)
3816 error ("function definition declared %<__thread%>");
3817 threadp = false;
3818 if (storage_class == csc_auto
3819 || storage_class == csc_register
3820 || storage_class == csc_typedef)
3821 storage_class = csc_none;
3823 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3825 if (decl_context == PARM && storage_class == csc_register)
3827 else
3829 switch (decl_context)
3831 case FIELD:
3832 error ("storage class specified for structure field %qs",
3833 name);
3834 break;
3835 case PARM:
3836 error ("storage class specified for parameter %qs", name);
3837 break;
3838 default:
3839 error ("storage class specified for typename");
3840 break;
3842 storage_class = csc_none;
3843 threadp = false;
3846 else if (storage_class == csc_extern
3847 && initialized
3848 && !funcdef_flag)
3850 /* 'extern' with initialization is invalid if not at file scope. */
3851 if (current_scope == file_scope)
3852 warning ("%qs initialized and declared %<extern%>", name);
3853 else
3854 error ("%qs has both %<extern%> and initializer", name);
3856 else if (current_scope == file_scope)
3858 if (storage_class == csc_auto)
3859 error ("file-scope declaration of %qs specifies %<auto%>", name);
3860 if (pedantic && storage_class == csc_register)
3861 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3863 else
3865 if (storage_class == csc_extern && funcdef_flag)
3866 error ("nested function %qs declared %<extern%>", name);
3867 else if (threadp && storage_class == csc_none)
3869 error ("function-scope %qs implicitly auto and declared "
3870 "%<__thread%>",
3871 name);
3872 threadp = false;
3876 /* Now figure out the structure of the declarator proper.
3877 Descend through it, creating more complex types, until we reach
3878 the declared identifier (or NULL_TREE, in an absolute declarator). */
3880 while (declarator && declarator->kind != cdk_id)
3882 if (type == error_mark_node)
3884 declarator = declarator->declarator;
3885 continue;
3888 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3889 a cdk_pointer (for *...),
3890 a cdk_function (for ...(...)),
3891 a cdk_attrs (for nested attributes),
3892 or a cdk_id (for the name being declared
3893 or the place in an absolute declarator
3894 where the name was omitted).
3895 For the last case, we have just exited the loop.
3897 At this point, TYPE is the type of elements of an array,
3898 or for a function to return, or for a pointer to point to.
3899 After this sequence of ifs, TYPE is the type of the
3900 array or function or pointer, and DECLARATOR has had its
3901 outermost layer removed. */
3903 if (array_ptr_quals != TYPE_UNQUALIFIED
3904 || array_ptr_attrs != NULL_TREE
3905 || array_parm_static)
3907 /* Only the innermost declarator (making a parameter be of
3908 array type which is converted to pointer type)
3909 may have static or type qualifiers. */
3910 error ("static or type qualifiers in non-parameter array declarator");
3911 array_ptr_quals = TYPE_UNQUALIFIED;
3912 array_ptr_attrs = NULL_TREE;
3913 array_parm_static = 0;
3916 switch (declarator->kind)
3918 case cdk_attrs:
3920 /* A declarator with embedded attributes. */
3921 tree attrs = declarator->u.attrs;
3922 const struct c_declarator *inner_decl;
3923 int attr_flags = 0;
3924 declarator = declarator->declarator;
3925 inner_decl = declarator;
3926 while (inner_decl->kind == cdk_attrs)
3927 inner_decl = inner_decl->declarator;
3928 if (inner_decl->kind == cdk_id)
3929 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3930 else if (inner_decl->kind == cdk_function)
3931 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3932 else if (inner_decl->kind == cdk_array)
3933 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3934 returned_attrs = decl_attributes (&type,
3935 chainon (returned_attrs, attrs),
3936 attr_flags);
3937 break;
3939 case cdk_array:
3941 tree itype = NULL_TREE;
3942 tree size = declarator->u.array.dimen;
3943 /* The index is a signed object `sizetype' bits wide. */
3944 tree index_type = c_common_signed_type (sizetype);
3946 array_ptr_quals = declarator->u.array.quals;
3947 array_ptr_attrs = declarator->u.array.attrs;
3948 array_parm_static = declarator->u.array.static_p;
3950 declarator = declarator->declarator;
3952 /* Check for some types that there cannot be arrays of. */
3954 if (VOID_TYPE_P (type))
3956 error ("declaration of %qs as array of voids", name);
3957 type = error_mark_node;
3960 if (TREE_CODE (type) == FUNCTION_TYPE)
3962 error ("declaration of %qs as array of functions", name);
3963 type = error_mark_node;
3966 if (pedantic && !in_system_header && flexible_array_type_p (type))
3967 pedwarn ("invalid use of structure with flexible array member");
3969 if (size == error_mark_node)
3970 type = error_mark_node;
3972 if (type == error_mark_node)
3973 continue;
3975 /* If size was specified, set ITYPE to a range-type for
3976 that size. Otherwise, ITYPE remains null. finish_decl
3977 may figure it out from an initial value. */
3979 if (size)
3981 /* Strip NON_LVALUE_EXPRs since we aren't using as an
3982 lvalue. */
3983 STRIP_TYPE_NOPS (size);
3985 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3987 error ("size of array %qs has non-integer type", name);
3988 size = integer_one_node;
3991 if (pedantic && integer_zerop (size))
3992 pedwarn ("ISO C forbids zero-size array %qs", name);
3994 if (TREE_CODE (size) == INTEGER_CST)
3996 constant_expression_warning (size);
3997 if (tree_int_cst_sgn (size) < 0)
3999 error ("size of array %qs is negative", name);
4000 size = integer_one_node;
4003 else
4005 /* Make sure the array size remains visibly
4006 nonconstant even if it is (eg) a const variable
4007 with known value. */
4008 size_varies = 1;
4010 if (!flag_isoc99 && pedantic)
4012 if (TREE_CONSTANT (size))
4013 pedwarn ("ISO C90 forbids array %qs whose size "
4014 "can%'t be evaluated",
4015 name);
4016 else
4017 pedwarn ("ISO C90 forbids variable-size array %qs",
4018 name);
4022 if (integer_zerop (size))
4024 /* A zero-length array cannot be represented with
4025 an unsigned index type, which is what we'll
4026 get with build_index_type. Create an
4027 open-ended range instead. */
4028 itype = build_range_type (sizetype, size, NULL_TREE);
4030 else
4032 /* Compute the maximum valid index, that is, size
4033 - 1. Do the calculation in index_type, so that
4034 if it is a variable the computations will be
4035 done in the proper mode. */
4036 itype = fold (build2 (MINUS_EXPR, index_type,
4037 convert (index_type, size),
4038 convert (index_type,
4039 size_one_node)));
4041 /* If that overflowed, the array is too big. ???
4042 While a size of INT_MAX+1 technically shouldn't
4043 cause an overflow (because we subtract 1), the
4044 overflow is recorded during the conversion to
4045 index_type, before the subtraction. Handling
4046 this case seems like an unnecessary
4047 complication. */
4048 if (TREE_OVERFLOW (itype))
4050 error ("size of array %qs is too large", name);
4051 type = error_mark_node;
4052 continue;
4055 if (size_varies)
4056 itype = variable_size (itype);
4057 itype = build_index_type (itype);
4060 else if (decl_context == FIELD)
4062 if (pedantic && !flag_isoc99 && !in_system_header)
4063 pedwarn ("ISO C90 does not support flexible array members");
4065 /* ISO C99 Flexible array members are effectively
4066 identical to GCC's zero-length array extension. */
4067 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4070 /* If pedantic, complain about arrays of incomplete types. */
4071 if (pedantic && !COMPLETE_TYPE_P (type))
4072 pedwarn ("array type has incomplete element type");
4074 /* Build the array type itself, then merge any constancy
4075 or volatility into the target type. We must do it in
4076 this order to ensure that the TYPE_MAIN_VARIANT field
4077 of the array type is set correctly. */
4078 type = build_array_type (type, itype);
4079 if (type_quals)
4080 type = c_build_qualified_type (type, type_quals);
4082 if (size_varies)
4083 C_TYPE_VARIABLE_SIZE (type) = 1;
4085 /* The GCC extension for zero-length arrays differs from
4086 ISO flexible array members in that sizeof yields
4087 zero. */
4088 if (size && integer_zerop (size))
4090 layout_type (type);
4091 TYPE_SIZE (type) = bitsize_zero_node;
4092 TYPE_SIZE_UNIT (type) = size_zero_node;
4094 else if (declarator->kind == cdk_pointer)
4095 /* We can never complete an array type which is the
4096 target of a pointer, so go ahead and lay it out. */
4097 layout_type (type);
4099 if (decl_context != PARM
4100 && (array_ptr_quals != TYPE_UNQUALIFIED
4101 || array_ptr_attrs != NULL_TREE
4102 || array_parm_static))
4104 error ("static or type qualifiers in non-parameter array declarator");
4105 array_ptr_quals = TYPE_UNQUALIFIED;
4106 array_ptr_attrs = NULL_TREE;
4107 array_parm_static = 0;
4109 break;
4111 case cdk_function:
4113 /* Say it's a definition only for the declarator closest
4114 to the identifier, apart possibly from some
4115 attributes. */
4116 bool really_funcdef = false;
4117 tree arg_types;
4118 if (funcdef_flag)
4120 const struct c_declarator *t = declarator->declarator;
4121 while (t->kind == cdk_attrs)
4122 t = t->declarator;
4123 really_funcdef = (t->kind == cdk_id);
4126 /* Declaring a function type. Make sure we have a valid
4127 type for the function to return. */
4128 if (type == error_mark_node)
4129 continue;
4131 size_varies = 0;
4133 /* Warn about some types functions can't return. */
4134 if (TREE_CODE (type) == FUNCTION_TYPE)
4136 error ("%qs declared as function returning a function", name);
4137 type = integer_type_node;
4139 if (TREE_CODE (type) == ARRAY_TYPE)
4141 error ("%qs declared as function returning an array", name);
4142 type = integer_type_node;
4145 /* Construct the function type and go to the next
4146 inner layer of declarator. */
4147 arg_info = declarator->u.arg_info;
4148 arg_types = grokparms (arg_info, really_funcdef);
4150 /* Type qualifiers before the return type of the function
4151 qualify the return type, not the function type. */
4152 if (type_quals)
4154 /* Type qualifiers on a function return type are
4155 normally permitted by the standard but have no
4156 effect, so give a warning at -Wreturn-type.
4157 Qualifiers on a void return type are banned on
4158 function definitions in ISO C; GCC used to used
4159 them for noreturn functions. */
4160 if (VOID_TYPE_P (type) && really_funcdef)
4161 pedwarn ("function definition has qualified void return type");
4162 else if (warn_return_type)
4163 warning ("type qualifiers ignored on function return type");
4165 type = c_build_qualified_type (type, type_quals);
4167 type_quals = TYPE_UNQUALIFIED;
4169 type = build_function_type (type, arg_types);
4170 declarator = declarator->declarator;
4172 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4173 the formal parameter list of this FUNCTION_TYPE to point to
4174 the FUNCTION_TYPE node itself. */
4176 tree link;
4178 for (link = arg_info->tags;
4179 link;
4180 link = TREE_CHAIN (link))
4181 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4183 break;
4185 case cdk_pointer:
4187 /* Merge any constancy or volatility into the target type
4188 for the pointer. */
4190 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4191 && type_quals)
4192 pedwarn ("ISO C forbids qualified function types");
4193 if (type_quals)
4194 type = c_build_qualified_type (type, type_quals);
4195 size_varies = 0;
4197 type = build_pointer_type (type);
4199 /* Process type qualifiers (such as const or volatile)
4200 that were given inside the `*'. */
4201 type_quals = declarator->u.pointer_quals;
4203 declarator = declarator->declarator;
4204 break;
4206 default:
4207 gcc_unreachable ();
4211 /* Now TYPE has the actual type. */
4213 /* Did array size calculations overflow? */
4215 if (TREE_CODE (type) == ARRAY_TYPE
4216 && COMPLETE_TYPE_P (type)
4217 && TREE_OVERFLOW (TYPE_SIZE (type)))
4219 error ("size of array %qs is too large", name);
4220 /* If we proceed with the array type as it is, we'll eventually
4221 crash in tree_low_cst(). */
4222 type = error_mark_node;
4225 /* If this is declaring a typedef name, return a TYPE_DECL. */
4227 if (storage_class == csc_typedef)
4229 tree decl;
4230 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4231 && type_quals)
4232 pedwarn ("ISO C forbids qualified function types");
4233 if (type_quals)
4234 type = c_build_qualified_type (type, type_quals);
4235 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4236 if (declspecs->explicit_signed_p)
4237 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4238 decl_attributes (&decl, returned_attrs, 0);
4239 if (declspecs->inline_p)
4240 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4241 return decl;
4244 /* Detect the case of an array type of unspecified size
4245 which came, as such, direct from a typedef name.
4246 We must copy the type, so that each identifier gets
4247 a distinct type, so that each identifier's size can be
4248 controlled separately by its own initializer. */
4250 if (type != 0 && typedef_type != 0
4251 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4252 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4254 type = build_array_type (TREE_TYPE (type), 0);
4255 if (size_varies)
4256 C_TYPE_VARIABLE_SIZE (type) = 1;
4259 /* If this is a type name (such as, in a cast or sizeof),
4260 compute the type and return it now. */
4262 if (decl_context == TYPENAME)
4264 /* Note that the grammar rejects storage classes in typenames
4265 and fields. */
4266 gcc_assert (storage_class == csc_none && !threadp
4267 && !declspecs->inline_p);
4268 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4269 && type_quals)
4270 pedwarn ("ISO C forbids const or volatile function types");
4271 if (type_quals)
4272 type = c_build_qualified_type (type, type_quals);
4273 decl_attributes (&type, returned_attrs, 0);
4274 return type;
4277 /* Aside from typedefs and type names (handle above),
4278 `void' at top level (not within pointer)
4279 is allowed only in public variables.
4280 We don't complain about parms either, but that is because
4281 a better error message can be made later. */
4283 if (VOID_TYPE_P (type) && decl_context != PARM
4284 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4285 && (storage_class == csc_extern
4286 || (current_scope == file_scope
4287 && !(storage_class == csc_static
4288 || storage_class == csc_register)))))
4290 error ("variable or field %qs declared void", name);
4291 type = integer_type_node;
4294 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4295 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4298 tree decl;
4300 if (decl_context == PARM)
4302 tree type_as_written;
4303 tree promoted_type;
4305 /* A parameter declared as an array of T is really a pointer to T.
4306 One declared as a function is really a pointer to a function. */
4308 if (TREE_CODE (type) == ARRAY_TYPE)
4310 /* Transfer const-ness of array into that of type pointed to. */
4311 type = TREE_TYPE (type);
4312 if (type_quals)
4313 type = c_build_qualified_type (type, type_quals);
4314 type = build_pointer_type (type);
4315 type_quals = array_ptr_quals;
4317 /* We don't yet implement attributes in this context. */
4318 if (array_ptr_attrs != NULL_TREE)
4319 warning ("attributes in parameter array declarator ignored");
4321 size_varies = 0;
4323 else if (TREE_CODE (type) == FUNCTION_TYPE)
4325 if (pedantic && type_quals)
4326 pedwarn ("ISO C forbids qualified function types");
4327 if (type_quals)
4328 type = c_build_qualified_type (type, type_quals);
4329 type = build_pointer_type (type);
4330 type_quals = TYPE_UNQUALIFIED;
4332 else if (type_quals)
4333 type = c_build_qualified_type (type, type_quals);
4335 type_as_written = type;
4337 decl = build_decl (PARM_DECL, declarator->u.id, type);
4338 if (size_varies)
4339 C_DECL_VARIABLE_SIZE (decl) = 1;
4341 /* Compute the type actually passed in the parmlist,
4342 for the case where there is no prototype.
4343 (For example, shorts and chars are passed as ints.)
4344 When there is a prototype, this is overridden later. */
4346 if (type == error_mark_node)
4347 promoted_type = type;
4348 else
4349 promoted_type = c_type_promotes_to (type);
4351 DECL_ARG_TYPE (decl) = promoted_type;
4352 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4353 if (declspecs->inline_p)
4354 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4356 else if (decl_context == FIELD)
4358 /* Note that the grammar rejects storage classes in typenames
4359 and fields. */
4360 gcc_assert (storage_class == csc_none && !threadp
4361 && !declspecs->inline_p);
4363 /* Structure field. It may not be a function. */
4365 if (TREE_CODE (type) == FUNCTION_TYPE)
4367 error ("field %qs declared as a function", name);
4368 type = build_pointer_type (type);
4370 else if (TREE_CODE (type) != ERROR_MARK
4371 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4373 error ("field %qs has incomplete type", name);
4374 type = error_mark_node;
4376 /* Move type qualifiers down to element of an array. */
4377 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4378 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4379 type_quals),
4380 TYPE_DOMAIN (type));
4381 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4382 DECL_NONADDRESSABLE_P (decl) = bitfield;
4384 if (size_varies)
4385 C_DECL_VARIABLE_SIZE (decl) = 1;
4387 else if (TREE_CODE (type) == FUNCTION_TYPE)
4389 if (storage_class == csc_register || threadp)
4390 error ("invalid storage class for function %qs", name);
4391 else if (current_scope != file_scope)
4393 /* Function declaration not at file scope. Storage
4394 classes other than `extern' are not allowed, C99
4395 6.7.1p5, and `extern' makes no difference. However,
4396 GCC allows 'auto', perhaps with 'inline', to support
4397 nested functions. */
4398 if (storage_class == csc_auto)
4400 if (pedantic)
4401 pedwarn ("invalid storage class for function %qs", name);
4403 if (storage_class == csc_static)
4404 error ("invalid storage class for function %qs", name);
4407 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4408 decl = build_decl_attribute_variant (decl, decl_attr);
4410 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4412 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4413 pedwarn ("ISO C forbids qualified function types");
4415 /* GNU C interprets a volatile-qualified function type to indicate
4416 that the function does not return. */
4417 if ((type_quals & TYPE_QUAL_VOLATILE)
4418 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4419 warning ("%<noreturn%> function returns non-void value");
4421 /* Every function declaration is an external reference
4422 (DECL_EXTERNAL) except for those which are not at file
4423 scope and are explicitly declared "auto". This is
4424 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4425 GCC to signify a forward declaration of a nested function. */
4426 if (storage_class == csc_auto && current_scope != file_scope)
4427 DECL_EXTERNAL (decl) = 0;
4428 else
4429 DECL_EXTERNAL (decl) = 1;
4431 /* Record absence of global scope for `static' or `auto'. */
4432 TREE_PUBLIC (decl)
4433 = !(storage_class == csc_static || storage_class == csc_auto);
4435 /* For a function definition, record the argument information
4436 block where store_parm_decls will look for it. */
4437 if (funcdef_flag)
4438 current_function_arg_info = arg_info;
4440 if (declspecs->default_int_p)
4441 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4443 /* Record presence of `inline', if it is reasonable. */
4444 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4446 if (declspecs->inline_p)
4447 pedwarn ("cannot inline function %<main%>");
4449 else if (declspecs->inline_p)
4451 /* Record that the function is declared `inline'. */
4452 DECL_DECLARED_INLINE_P (decl) = 1;
4454 /* Do not mark bare declarations as DECL_INLINE. Doing so
4455 in the presence of multiple declarations can result in
4456 the abstract origin pointing between the declarations,
4457 which will confuse dwarf2out. */
4458 if (initialized)
4460 DECL_INLINE (decl) = 1;
4461 if (storage_class == csc_extern)
4462 current_extern_inline = 1;
4465 /* If -finline-functions, assume it can be inlined. This does
4466 two things: let the function be deferred until it is actually
4467 needed, and let dwarf2 know that the function is inlinable. */
4468 else if (flag_inline_trees == 2 && initialized)
4469 DECL_INLINE (decl) = 1;
4471 else
4473 /* It's a variable. */
4474 /* An uninitialized decl with `extern' is a reference. */
4475 int extern_ref = !initialized && storage_class == csc_extern;
4477 /* Move type qualifiers down to element of an array. */
4478 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4480 int saved_align = TYPE_ALIGN(type);
4481 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4482 type_quals),
4483 TYPE_DOMAIN (type));
4484 TYPE_ALIGN (type) = saved_align;
4486 else if (type_quals)
4487 type = c_build_qualified_type (type, type_quals);
4489 /* C99 6.2.2p7: It is invalid (compile-time undefined
4490 behavior) to create an 'extern' declaration for a
4491 variable if there is a global declaration that is
4492 'static' and the global declaration is not visible.
4493 (If the static declaration _is_ currently visible,
4494 the 'extern' declaration is taken to refer to that decl.) */
4495 if (extern_ref && current_scope != file_scope)
4497 tree global_decl = identifier_global_value (declarator->u.id);
4498 tree visible_decl = lookup_name (declarator->u.id);
4500 if (global_decl
4501 && global_decl != visible_decl
4502 && TREE_CODE (global_decl) == VAR_DECL
4503 && !TREE_PUBLIC (global_decl))
4504 error ("variable previously declared %<static%> redeclared "
4505 "%<extern%>");
4508 decl = build_decl (VAR_DECL, declarator->u.id, type);
4509 if (size_varies)
4510 C_DECL_VARIABLE_SIZE (decl) = 1;
4512 if (declspecs->inline_p)
4513 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4515 /* At file scope, an initialized extern declaration may follow
4516 a static declaration. In that case, DECL_EXTERNAL will be
4517 reset later in start_decl. */
4518 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4520 /* At file scope, the presence of a `static' or `register' storage
4521 class specifier, or the absence of all storage class specifiers
4522 makes this declaration a definition (perhaps tentative). Also,
4523 the absence of both `static' and `register' makes it public. */
4524 if (current_scope == file_scope)
4526 TREE_PUBLIC (decl) = !(storage_class == csc_static
4527 || storage_class == csc_register);
4528 TREE_STATIC (decl) = !extern_ref;
4530 /* Not at file scope, only `static' makes a static definition. */
4531 else
4533 TREE_STATIC (decl) = (storage_class == csc_static);
4534 TREE_PUBLIC (decl) = extern_ref;
4537 if (threadp)
4539 if (targetm.have_tls)
4540 DECL_THREAD_LOCAL (decl) = 1;
4541 else
4542 /* A mere warning is sure to result in improper semantics
4543 at runtime. Don't bother to allow this to compile. */
4544 error ("thread-local storage not supported for this target");
4548 /* Record `register' declaration for warnings on &
4549 and in case doing stupid register allocation. */
4551 if (storage_class == csc_register)
4553 C_DECL_REGISTER (decl) = 1;
4554 DECL_REGISTER (decl) = 1;
4557 /* Record constancy and volatility. */
4558 c_apply_type_quals_to_decl (type_quals, decl);
4560 /* If a type has volatile components, it should be stored in memory.
4561 Otherwise, the fact that those components are volatile
4562 will be ignored, and would even crash the compiler. */
4563 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4565 /* It is not an error for a structure with volatile fields to
4566 be declared register, but reset DECL_REGISTER since it
4567 cannot actually go in a register. */
4568 int was_reg = C_DECL_REGISTER (decl);
4569 C_DECL_REGISTER (decl) = 0;
4570 DECL_REGISTER (decl) = 0;
4571 c_mark_addressable (decl);
4572 C_DECL_REGISTER (decl) = was_reg;
4575 /* This is the earliest point at which we might know the assembler
4576 name of a variable. Thus, if it's known before this, die horribly. */
4577 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4579 decl_attributes (&decl, returned_attrs, 0);
4581 return decl;
4585 /* Decode the parameter-list info for a function type or function definition.
4586 The argument is the value returned by `get_parm_info' (or made in parse.y
4587 if there is an identifier list instead of a parameter decl list).
4588 These two functions are separate because when a function returns
4589 or receives functions then each is called multiple times but the order
4590 of calls is different. The last call to `grokparms' is always the one
4591 that contains the formal parameter names of a function definition.
4593 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4595 FUNCDEF_FLAG is true for a function definition, false for
4596 a mere declaration. A nonempty identifier-list gets an error message
4597 when FUNCDEF_FLAG is false. */
4599 static tree
4600 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4602 tree arg_types = arg_info->types;
4604 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4605 && !in_system_header)
4606 warning ("function declaration isn%'t a prototype");
4608 if (arg_types == error_mark_node)
4609 return 0; /* don't set TYPE_ARG_TYPES in this case */
4611 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4613 if (! funcdef_flag)
4614 pedwarn ("parameter names (without types) in function declaration");
4616 arg_info->parms = arg_info->types;
4617 arg_info->types = 0;
4618 return 0;
4620 else
4622 tree parm, type, typelt;
4623 unsigned int parmno;
4625 /* If the arg types are incomplete in a declaration, they must
4626 include undefined tags. These tags can never be defined in
4627 the scope of the declaration, so the types can never be
4628 completed, and no call can be compiled successfully. */
4630 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4631 parm;
4632 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4634 type = TREE_VALUE (typelt);
4635 if (type == error_mark_node)
4636 continue;
4638 if (!COMPLETE_TYPE_P (type))
4640 if (funcdef_flag)
4642 if (DECL_NAME (parm))
4643 error ("%Jparameter %u (%qD) has incomplete type",
4644 parm, parmno, parm);
4645 else
4646 error ("%Jparameter %u has incomplete type",
4647 parm, parmno);
4649 TREE_VALUE (typelt) = error_mark_node;
4650 TREE_TYPE (parm) = error_mark_node;
4652 else
4654 if (DECL_NAME (parm))
4655 warning ("%Jparameter %u (%qD) has incomplete type",
4656 parm, parmno, parm);
4657 else
4658 warning ("%Jparameter %u has incomplete type",
4659 parm, parmno);
4663 return arg_types;
4667 /* Take apart the current scope and return a c_arg_info structure with
4668 info on a parameter list just parsed.
4670 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4672 ELLIPSIS being true means the argument list ended in '...' so don't
4673 append a sentinel (void_list_node) to the end of the type-list. */
4675 struct c_arg_info *
4676 get_parm_info (bool ellipsis)
4678 struct c_binding *b = current_scope->bindings;
4679 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4680 struct c_arg_info);
4681 tree parms = 0;
4682 tree tags = 0;
4683 tree types = 0;
4684 tree others = 0;
4686 static bool explained_incomplete_types = false;
4687 bool gave_void_only_once_err = false;
4689 arg_info->parms = 0;
4690 arg_info->tags = 0;
4691 arg_info->types = 0;
4692 arg_info->others = 0;
4694 /* The bindings in this scope must not get put into a block.
4695 We will take care of deleting the binding nodes. */
4696 current_scope->bindings = 0;
4698 /* This function is only called if there was *something* on the
4699 parameter list. */
4700 gcc_assert (b);
4702 /* A parameter list consisting solely of 'void' indicates that the
4703 function takes no arguments. But if the 'void' is qualified
4704 (by 'const' or 'volatile'), or has a storage class specifier
4705 ('register'), then the behavior is undefined; issue an error.
4706 Typedefs for 'void' are OK (see DR#157). */
4707 if (b->prev == 0 /* one binding */
4708 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4709 && !DECL_NAME (b->decl) /* anonymous */
4710 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4712 if (TREE_THIS_VOLATILE (b->decl)
4713 || TREE_READONLY (b->decl)
4714 || C_DECL_REGISTER (b->decl))
4715 error ("%<void%> as only parameter may not be qualified");
4717 /* There cannot be an ellipsis. */
4718 if (ellipsis)
4719 error ("%<void%> must be the only parameter");
4721 arg_info->types = void_list_node;
4722 return arg_info;
4725 if (!ellipsis)
4726 types = void_list_node;
4728 /* Break up the bindings list into parms, tags, types, and others;
4729 apply sanity checks; purge the name-to-decl bindings. */
4730 while (b)
4732 tree decl = b->decl;
4733 tree type = TREE_TYPE (decl);
4734 const char *keyword;
4736 switch (TREE_CODE (decl))
4738 case PARM_DECL:
4739 if (b->id)
4741 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4742 I_SYMBOL_BINDING (b->id) = b->shadowed;
4745 /* Check for forward decls that never got their actual decl. */
4746 if (TREE_ASM_WRITTEN (decl))
4747 error ("%Jparameter %qD has just a forward declaration",
4748 decl, decl);
4749 /* Check for (..., void, ...) and issue an error. */
4750 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4752 if (!gave_void_only_once_err)
4754 error ("%<void%> must be the only parameter");
4755 gave_void_only_once_err = true;
4758 else
4760 /* Valid parameter, add it to the list. */
4761 TREE_CHAIN (decl) = parms;
4762 parms = decl;
4764 /* Since there is a prototype, args are passed in their
4765 declared types. The back end may override this later. */
4766 DECL_ARG_TYPE (decl) = type;
4767 types = tree_cons (0, type, types);
4769 break;
4771 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4772 case UNION_TYPE: keyword = "union"; goto tag;
4773 case RECORD_TYPE: keyword = "struct"; goto tag;
4774 tag:
4775 /* Types may not have tag-names, in which case the type
4776 appears in the bindings list with b->id NULL. */
4777 if (b->id)
4779 gcc_assert (I_TAG_BINDING (b->id) == b);
4780 I_TAG_BINDING (b->id) = b->shadowed;
4783 /* Warn about any struct, union or enum tags defined in a
4784 parameter list. The scope of such types is limited to
4785 the parameter list, which is rarely if ever desirable
4786 (it's impossible to call such a function with type-
4787 correct arguments). An anonymous union parm type is
4788 meaningful as a GNU extension, so don't warn for that. */
4789 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4791 if (b->id)
4792 /* The %s will be one of 'struct', 'union', or 'enum'. */
4793 warning ("%<%s %E%> declared inside parameter list",
4794 keyword, b->id);
4795 else
4796 /* The %s will be one of 'struct', 'union', or 'enum'. */
4797 warning ("anonymous %s declared inside parameter list",
4798 keyword);
4800 if (! explained_incomplete_types)
4802 warning ("its scope is only this definition or declaration,"
4803 " which is probably not what you want");
4804 explained_incomplete_types = true;
4808 tags = tree_cons (b->id, decl, tags);
4809 break;
4811 case CONST_DECL:
4812 case TYPE_DECL:
4813 /* CONST_DECLs appear here when we have an embedded enum,
4814 and TYPE_DECLs appear here when we have an embedded struct
4815 or union. No warnings for this - we already warned about the
4816 type itself. */
4817 TREE_CHAIN (decl) = others;
4818 others = decl;
4819 /* fall through */
4821 case ERROR_MARK:
4822 /* error_mark_node appears here when we have an undeclared
4823 variable. Just throw it away. */
4824 if (b->id)
4826 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4827 I_SYMBOL_BINDING (b->id) = b->shadowed;
4829 break;
4831 /* Other things that might be encountered. */
4832 case LABEL_DECL:
4833 case FUNCTION_DECL:
4834 case VAR_DECL:
4835 default:
4836 gcc_unreachable ();
4839 b = free_binding_and_advance (b);
4842 arg_info->parms = parms;
4843 arg_info->tags = tags;
4844 arg_info->types = types;
4845 arg_info->others = others;
4846 return arg_info;
4849 /* Get the struct, enum or union (CODE says which) with tag NAME.
4850 Define the tag as a forward-reference if it is not defined. */
4852 tree
4853 xref_tag (enum tree_code code, tree name)
4855 /* If a cross reference is requested, look up the type
4856 already defined for this tag and return it. */
4858 tree ref = lookup_tag (code, name, 0);
4859 /* If this is the right type of tag, return what we found.
4860 (This reference will be shadowed by shadow_tag later if appropriate.)
4861 If this is the wrong type of tag, do not return it. If it was the
4862 wrong type in the same scope, we will have had an error
4863 message already; if in a different scope and declaring
4864 a name, pending_xref_error will give an error message; but if in a
4865 different scope and not declaring a name, this tag should
4866 shadow the previous declaration of a different type of tag, and
4867 this would not work properly if we return the reference found.
4868 (For example, with "struct foo" in an outer scope, "union foo;"
4869 must shadow that tag with a new one of union type.) */
4870 if (ref && TREE_CODE (ref) == code)
4871 return ref;
4873 /* If no such tag is yet defined, create a forward-reference node
4874 and record it as the "definition".
4875 When a real declaration of this type is found,
4876 the forward-reference will be altered into a real type. */
4878 ref = make_node (code);
4879 if (code == ENUMERAL_TYPE)
4881 /* Give the type a default layout like unsigned int
4882 to avoid crashing if it does not get defined. */
4883 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4884 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4885 TYPE_USER_ALIGN (ref) = 0;
4886 TYPE_UNSIGNED (ref) = 1;
4887 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4888 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4889 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4892 pushtag (name, ref);
4894 return ref;
4897 /* Make sure that the tag NAME is defined *in the current scope*
4898 at least as a forward reference.
4899 CODE says which kind of tag NAME ought to be. */
4901 tree
4902 start_struct (enum tree_code code, tree name)
4904 /* If there is already a tag defined at this scope
4905 (as a forward reference), just return it. */
4907 tree ref = 0;
4909 if (name != 0)
4910 ref = lookup_tag (code, name, 1);
4911 if (ref && TREE_CODE (ref) == code)
4913 if (TYPE_FIELDS (ref))
4915 if (code == UNION_TYPE)
4916 error ("redefinition of %<union %s%>", IDENTIFIER_POINTER (name));
4917 else
4918 error ("redefinition of %<struct %s%>", IDENTIFIER_POINTER (name));
4921 else
4923 /* Otherwise create a forward-reference just so the tag is in scope. */
4925 ref = make_node (code);
4926 pushtag (name, ref);
4929 C_TYPE_BEING_DEFINED (ref) = 1;
4930 TYPE_PACKED (ref) = flag_pack_struct;
4931 return ref;
4934 /* Process the specs, declarator and width (NULL if omitted)
4935 of a structure component, returning a FIELD_DECL node.
4936 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4938 This is done during the parsing of the struct declaration.
4939 The FIELD_DECL nodes are chained together and the lot of them
4940 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4942 tree
4943 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
4944 tree width)
4946 tree value;
4948 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
4949 && width == NULL_TREE)
4951 /* This is an unnamed decl.
4953 If we have something of the form "union { list } ;" then this
4954 is the anonymous union extension. Similarly for struct.
4956 If this is something of the form "struct foo;", then
4957 If MS extensions are enabled, this is handled as an
4958 anonymous struct.
4959 Otherwise this is a forward declaration of a structure tag.
4961 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4962 If MS extensions are enabled and foo names a structure, then
4963 again this is an anonymous struct.
4964 Otherwise this is an error.
4966 Oh what a horrid tangled web we weave. I wonder if MS consciously
4967 took this from Plan 9 or if it was an accident of implementation
4968 that took root before someone noticed the bug... */
4970 tree type = declspecs->type;
4972 if (type
4973 && (TREE_CODE (type) == RECORD_TYPE
4974 || TREE_CODE (type) == UNION_TYPE)
4975 && (flag_ms_extensions || !declspecs->typedef_p))
4977 if (flag_ms_extensions)
4978 ; /* ok */
4979 else if (flag_iso)
4980 goto warn_unnamed_field;
4981 else if (TYPE_NAME (type) == NULL)
4982 ; /* ok */
4983 else
4984 goto warn_unnamed_field;
4986 else
4988 warn_unnamed_field:
4989 warning ("declaration does not declare anything");
4990 return NULL_TREE;
4994 value = grokdeclarator (declarator, declspecs, FIELD, false,
4995 width ? &width : NULL);
4997 finish_decl (value, NULL_TREE, NULL_TREE);
4998 DECL_INITIAL (value) = width;
5000 return value;
5003 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5004 the list such that this does not present a problem later. */
5006 static void
5007 detect_field_duplicates (tree fieldlist)
5009 tree x, y;
5010 int timeout = 10;
5012 /* First, see if there are more than "a few" fields.
5013 This is trivially true if there are zero or one fields. */
5014 if (!fieldlist)
5015 return;
5016 x = TREE_CHAIN (fieldlist);
5017 if (!x)
5018 return;
5019 do {
5020 timeout--;
5021 x = TREE_CHAIN (x);
5022 } while (timeout > 0 && x);
5024 /* If there were "few" fields, avoid the overhead of allocating
5025 a hash table. Instead just do the nested traversal thing. */
5026 if (timeout > 0)
5028 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5029 if (DECL_NAME (x))
5031 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5032 if (DECL_NAME (y) == DECL_NAME (x))
5034 error ("%Jduplicate member %qD", x, x);
5035 DECL_NAME (x) = NULL_TREE;
5039 else
5041 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5042 void **slot;
5044 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5045 if ((y = DECL_NAME (x)) != 0)
5047 slot = htab_find_slot (htab, y, INSERT);
5048 if (*slot)
5050 error ("%Jduplicate member %qD", x, x);
5051 DECL_NAME (x) = NULL_TREE;
5053 *slot = y;
5056 htab_delete (htab);
5060 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5061 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5062 ATTRIBUTES are attributes to be applied to the structure. */
5064 tree
5065 finish_struct (tree t, tree fieldlist, tree attributes)
5067 tree x;
5068 bool toplevel = file_scope == current_scope;
5069 int saw_named_field;
5071 /* If this type was previously laid out as a forward reference,
5072 make sure we lay it out again. */
5074 TYPE_SIZE (t) = 0;
5076 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5078 if (pedantic)
5080 for (x = fieldlist; x; x = TREE_CHAIN (x))
5081 if (DECL_NAME (x) != 0)
5082 break;
5084 if (x == 0)
5085 pedwarn ("%s has no %s",
5086 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5087 fieldlist ? _("named members") : _("members"));
5090 /* Install struct as DECL_CONTEXT of each field decl.
5091 Also process specified field sizes, found in the DECL_INITIAL,
5092 storing 0 there after the type has been changed to precision equal
5093 to its width, rather than the precision of the specified standard
5094 type. (Correct layout requires the original type to have been preserved
5095 until now.) */
5097 saw_named_field = 0;
5098 for (x = fieldlist; x; x = TREE_CHAIN (x))
5100 DECL_CONTEXT (x) = t;
5101 DECL_PACKED (x) |= TYPE_PACKED (t);
5103 /* If any field is const, the structure type is pseudo-const. */
5104 if (TREE_READONLY (x))
5105 C_TYPE_FIELDS_READONLY (t) = 1;
5106 else
5108 /* A field that is pseudo-const makes the structure likewise. */
5109 tree t1 = TREE_TYPE (x);
5110 while (TREE_CODE (t1) == ARRAY_TYPE)
5111 t1 = TREE_TYPE (t1);
5112 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5113 && C_TYPE_FIELDS_READONLY (t1))
5114 C_TYPE_FIELDS_READONLY (t) = 1;
5117 /* Any field that is volatile means variables of this type must be
5118 treated in some ways as volatile. */
5119 if (TREE_THIS_VOLATILE (x))
5120 C_TYPE_FIELDS_VOLATILE (t) = 1;
5122 /* Any field of nominal variable size implies structure is too. */
5123 if (C_DECL_VARIABLE_SIZE (x))
5124 C_TYPE_VARIABLE_SIZE (t) = 1;
5126 /* Detect invalid nested redefinition. */
5127 if (TREE_TYPE (x) == t)
5128 error ("nested redefinition of %qs",
5129 IDENTIFIER_POINTER (TYPE_NAME (t)));
5131 if (DECL_INITIAL (x))
5133 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5134 DECL_SIZE (x) = bitsize_int (width);
5135 DECL_BIT_FIELD (x) = 1;
5136 SET_DECL_C_BIT_FIELD (x);
5139 /* Detect flexible array member in an invalid context. */
5140 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5141 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5142 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5143 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5145 if (TREE_CODE (t) == UNION_TYPE)
5147 error ("%Jflexible array member in union", x);
5148 TREE_TYPE (x) = error_mark_node;
5150 else if (TREE_CHAIN (x) != NULL_TREE)
5152 error ("%Jflexible array member not at end of struct", x);
5153 TREE_TYPE (x) = error_mark_node;
5155 else if (! saw_named_field)
5157 error ("%Jflexible array member in otherwise empty struct", x);
5158 TREE_TYPE (x) = error_mark_node;
5162 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5163 && flexible_array_type_p (TREE_TYPE (x)))
5164 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5166 if (DECL_NAME (x))
5167 saw_named_field = 1;
5170 detect_field_duplicates (fieldlist);
5172 /* Now we have the nearly final fieldlist. Record it,
5173 then lay out the structure or union (including the fields). */
5175 TYPE_FIELDS (t) = fieldlist;
5177 layout_type (t);
5179 /* Give bit-fields their proper types. */
5181 tree *fieldlistp = &fieldlist;
5182 while (*fieldlistp)
5183 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5184 && TREE_TYPE (*fieldlistp) != error_mark_node)
5186 unsigned HOST_WIDE_INT width
5187 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5188 tree type = TREE_TYPE (*fieldlistp);
5189 if (width != TYPE_PRECISION (type))
5190 TREE_TYPE (*fieldlistp)
5191 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5192 DECL_INITIAL (*fieldlistp) = 0;
5194 else
5195 fieldlistp = &TREE_CHAIN (*fieldlistp);
5198 /* Now we have the truly final field list.
5199 Store it in this type and in the variants. */
5201 TYPE_FIELDS (t) = fieldlist;
5203 /* If there are lots of fields, sort so we can look through them fast.
5204 We arbitrarily consider 16 or more elts to be "a lot". */
5207 int len = 0;
5209 for (x = fieldlist; x; x = TREE_CHAIN (x))
5211 if (len > 15 || DECL_NAME (x) == NULL)
5212 break;
5213 len += 1;
5216 if (len > 15)
5218 tree *field_array;
5219 struct lang_type *space;
5220 struct sorted_fields_type *space2;
5222 len += list_length (x);
5224 /* Use the same allocation policy here that make_node uses, to
5225 ensure that this lives as long as the rest of the struct decl.
5226 All decls in an inline function need to be saved. */
5228 space = GGC_CNEW (struct lang_type);
5229 space2 = GGC_NEWVAR (struct sorted_fields_type,
5230 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5232 len = 0;
5233 space->s = space2;
5234 field_array = &space2->elts[0];
5235 for (x = fieldlist; x; x = TREE_CHAIN (x))
5237 field_array[len++] = x;
5239 /* If there is anonymous struct or union, break out of the loop. */
5240 if (DECL_NAME (x) == NULL)
5241 break;
5243 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5244 if (x == NULL)
5246 TYPE_LANG_SPECIFIC (t) = space;
5247 TYPE_LANG_SPECIFIC (t)->s->len = len;
5248 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5249 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5254 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5256 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5257 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5258 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5259 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5262 /* If this was supposed to be a transparent union, but we can't
5263 make it one, warn and turn off the flag. */
5264 if (TREE_CODE (t) == UNION_TYPE
5265 && TYPE_TRANSPARENT_UNION (t)
5266 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5268 TYPE_TRANSPARENT_UNION (t) = 0;
5269 warning ("union cannot be made transparent");
5272 /* If this structure or union completes the type of any previous
5273 variable declaration, lay it out and output its rtl. */
5274 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5276 x = TREE_CHAIN (x))
5278 tree decl = TREE_VALUE (x);
5279 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5280 layout_array_type (TREE_TYPE (decl));
5281 if (TREE_CODE (decl) != TYPE_DECL)
5283 layout_decl (decl, 0);
5284 if (c_dialect_objc ())
5285 objc_check_decl (decl);
5286 rest_of_decl_compilation (decl, toplevel, 0);
5287 if (! toplevel)
5288 expand_decl (decl);
5291 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5293 /* Finish debugging output for this type. */
5294 rest_of_type_compilation (t, toplevel);
5296 return t;
5299 /* Lay out the type T, and its element type, and so on. */
5301 static void
5302 layout_array_type (tree t)
5304 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5305 layout_array_type (TREE_TYPE (t));
5306 layout_type (t);
5309 /* Begin compiling the definition of an enumeration type.
5310 NAME is its name (or null if anonymous).
5311 Returns the type object, as yet incomplete.
5312 Also records info about it so that build_enumerator
5313 may be used to declare the individual values as they are read. */
5315 tree
5316 start_enum (tree name)
5318 tree enumtype = 0;
5320 /* If this is the real definition for a previous forward reference,
5321 fill in the contents in the same object that used to be the
5322 forward reference. */
5324 if (name != 0)
5325 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5327 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5329 enumtype = make_node (ENUMERAL_TYPE);
5330 pushtag (name, enumtype);
5333 C_TYPE_BEING_DEFINED (enumtype) = 1;
5335 if (TYPE_VALUES (enumtype) != 0)
5337 /* This enum is a named one that has been declared already. */
5338 error ("redeclaration of %<enum %s%>", IDENTIFIER_POINTER (name));
5340 /* Completely replace its old definition.
5341 The old enumerators remain defined, however. */
5342 TYPE_VALUES (enumtype) = 0;
5345 enum_next_value = integer_zero_node;
5346 enum_overflow = 0;
5348 if (flag_short_enums)
5349 TYPE_PACKED (enumtype) = 1;
5351 return enumtype;
5354 /* After processing and defining all the values of an enumeration type,
5355 install their decls in the enumeration type and finish it off.
5356 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5357 and ATTRIBUTES are the specified attributes.
5358 Returns ENUMTYPE. */
5360 tree
5361 finish_enum (tree enumtype, tree values, tree attributes)
5363 tree pair, tem;
5364 tree minnode = 0, maxnode = 0;
5365 int precision, unsign;
5366 bool toplevel = (file_scope == current_scope);
5367 struct lang_type *lt;
5369 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5371 /* Calculate the maximum value of any enumerator in this type. */
5373 if (values == error_mark_node)
5374 minnode = maxnode = integer_zero_node;
5375 else
5377 minnode = maxnode = TREE_VALUE (values);
5378 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5380 tree value = TREE_VALUE (pair);
5381 if (tree_int_cst_lt (maxnode, value))
5382 maxnode = value;
5383 if (tree_int_cst_lt (value, minnode))
5384 minnode = value;
5388 /* Construct the final type of this enumeration. It is the same
5389 as one of the integral types - the narrowest one that fits, except
5390 that normally we only go as narrow as int - and signed iff any of
5391 the values are negative. */
5392 unsign = (tree_int_cst_sgn (minnode) >= 0);
5393 precision = MAX (min_precision (minnode, unsign),
5394 min_precision (maxnode, unsign));
5396 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5398 tem = c_common_type_for_size (precision, unsign);
5399 if (tem == NULL)
5401 warning ("enumeration values exceed range of largest integer");
5402 tem = long_long_integer_type_node;
5405 else
5406 tem = unsign ? unsigned_type_node : integer_type_node;
5408 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5409 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5410 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5411 TYPE_SIZE (enumtype) = 0;
5413 /* If the precision of the type was specific with an attribute and it
5414 was too small, give an error. Otherwise, use it. */
5415 if (TYPE_PRECISION (enumtype))
5417 if (precision > TYPE_PRECISION (enumtype))
5418 error ("specified mode too small for enumeral values");
5420 else
5421 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5423 layout_type (enumtype);
5425 if (values != error_mark_node)
5427 /* Change the type of the enumerators to be the enum type. We
5428 need to do this irrespective of the size of the enum, for
5429 proper type checking. Replace the DECL_INITIALs of the
5430 enumerators, and the value slots of the list, with copies
5431 that have the enum type; they cannot be modified in place
5432 because they may be shared (e.g. integer_zero_node) Finally,
5433 change the purpose slots to point to the names of the decls. */
5434 for (pair = values; pair; pair = TREE_CHAIN (pair))
5436 tree enu = TREE_PURPOSE (pair);
5437 tree ini = DECL_INITIAL (enu);
5439 TREE_TYPE (enu) = enumtype;
5441 /* The ISO C Standard mandates enumerators to have type int,
5442 even though the underlying type of an enum type is
5443 unspecified. Here we convert any enumerators that fit in
5444 an int to type int, to avoid promotions to unsigned types
5445 when comparing integers with enumerators that fit in the
5446 int range. When -pedantic is given, build_enumerator()
5447 would have already taken care of those that don't fit. */
5448 if (int_fits_type_p (ini, integer_type_node))
5449 tem = integer_type_node;
5450 else
5451 tem = enumtype;
5452 ini = convert (tem, ini);
5454 DECL_INITIAL (enu) = ini;
5455 TREE_PURPOSE (pair) = DECL_NAME (enu);
5456 TREE_VALUE (pair) = ini;
5459 TYPE_VALUES (enumtype) = values;
5462 /* Record the min/max values so that we can warn about bit-field
5463 enumerations that are too small for the values. */
5464 lt = GGC_CNEW (struct lang_type);
5465 lt->enum_min = minnode;
5466 lt->enum_max = maxnode;
5467 TYPE_LANG_SPECIFIC (enumtype) = lt;
5469 /* Fix up all variant types of this enum type. */
5470 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5472 if (tem == enumtype)
5473 continue;
5474 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5475 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5476 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5477 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5478 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5479 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5480 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5481 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5482 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5483 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5484 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5487 /* Finish debugging output for this type. */
5488 rest_of_type_compilation (enumtype, toplevel);
5490 return enumtype;
5493 /* Build and install a CONST_DECL for one value of the
5494 current enumeration type (one that was begun with start_enum).
5495 Return a tree-list containing the CONST_DECL and its value.
5496 Assignment of sequential values by default is handled here. */
5498 tree
5499 build_enumerator (tree name, tree value)
5501 tree decl, type;
5503 /* Validate and default VALUE. */
5505 /* Remove no-op casts from the value. */
5506 if (value)
5507 STRIP_TYPE_NOPS (value);
5509 if (value != 0)
5511 /* Don't issue more errors for error_mark_node (i.e. an
5512 undeclared identifier) - just ignore the value expression. */
5513 if (value == error_mark_node)
5514 value = 0;
5515 else if (TREE_CODE (value) != INTEGER_CST)
5517 error ("enumerator value for %qE is not an integer constant", name);
5518 value = 0;
5520 else
5522 value = default_conversion (value);
5523 constant_expression_warning (value);
5527 /* Default based on previous value. */
5528 /* It should no longer be possible to have NON_LVALUE_EXPR
5529 in the default. */
5530 if (value == 0)
5532 value = enum_next_value;
5533 if (enum_overflow)
5534 error ("overflow in enumeration values");
5537 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5539 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5540 /* XXX This causes -pedantic to change the meaning of the program.
5541 Remove? -zw 2004-03-15 */
5542 value = convert (integer_type_node, value);
5545 /* Set basis for default for next value. */
5546 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5547 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5549 /* Now create a declaration for the enum value name. */
5551 type = TREE_TYPE (value);
5552 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5553 TYPE_PRECISION (integer_type_node)),
5554 (TYPE_PRECISION (type)
5555 >= TYPE_PRECISION (integer_type_node)
5556 && TYPE_UNSIGNED (type)));
5558 decl = build_decl (CONST_DECL, name, type);
5559 DECL_INITIAL (decl) = convert (type, value);
5560 pushdecl (decl);
5562 return tree_cons (decl, value, NULL_TREE);
5566 /* Create the FUNCTION_DECL for a function definition.
5567 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5568 the declaration; they describe the function's name and the type it returns,
5569 but twisted together in a fashion that parallels the syntax of C.
5571 This function creates a binding context for the function body
5572 as well as setting up the FUNCTION_DECL in current_function_decl.
5574 Returns 1 on success. If the DECLARATOR is not suitable for a function
5575 (it defines a datum instead), we return 0, which tells
5576 yyparse to report a parse error. */
5579 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5580 tree attributes)
5582 tree decl1, old_decl;
5583 tree restype, resdecl;
5585 current_function_returns_value = 0; /* Assume, until we see it does. */
5586 current_function_returns_null = 0;
5587 current_function_returns_abnormally = 0;
5588 warn_about_return_type = 0;
5589 current_extern_inline = 0;
5590 c_switch_stack = NULL;
5592 /* Indicate no valid break/continue context by setting these variables
5593 to some non-null, non-label value. We'll notice and emit the proper
5594 error message in c_finish_bc_stmt. */
5595 c_break_label = c_cont_label = size_zero_node;
5597 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5599 /* If the declarator is not suitable for a function definition,
5600 cause a syntax error. */
5601 if (decl1 == 0)
5602 return 0;
5604 decl_attributes (&decl1, attributes, 0);
5606 if (DECL_DECLARED_INLINE_P (decl1)
5607 && DECL_UNINLINABLE (decl1)
5608 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5609 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5611 announce_function (decl1);
5613 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5615 error ("return type is an incomplete type");
5616 /* Make it return void instead. */
5617 TREE_TYPE (decl1)
5618 = build_function_type (void_type_node,
5619 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5622 if (warn_about_return_type)
5623 pedwarn_c99 ("return type defaults to %<int%>");
5625 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5626 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5627 DECL_INITIAL (decl1) = error_mark_node;
5629 /* If this definition isn't a prototype and we had a prototype declaration
5630 before, copy the arg type info from that prototype.
5631 But not if what we had before was a builtin function. */
5632 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5633 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5634 && !DECL_BUILT_IN (old_decl)
5635 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5636 TREE_TYPE (TREE_TYPE (old_decl)))
5637 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5639 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5640 TREE_TYPE (decl1));
5641 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5644 /* Optionally warn of old-fashioned def with no previous prototype. */
5645 if (warn_strict_prototypes
5646 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5647 && C_DECL_ISNT_PROTOTYPE (old_decl))
5648 warning ("function declaration isn%'t a prototype");
5649 /* Optionally warn of any global def with no previous prototype. */
5650 else if (warn_missing_prototypes
5651 && TREE_PUBLIC (decl1)
5652 && ! MAIN_NAME_P (DECL_NAME (decl1))
5653 && C_DECL_ISNT_PROTOTYPE (old_decl))
5654 warning ("%Jno previous prototype for %qD", decl1, decl1);
5655 /* Optionally warn of any def with no previous prototype
5656 if the function has already been used. */
5657 else if (warn_missing_prototypes
5658 && old_decl != 0 && TREE_USED (old_decl)
5659 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5660 warning ("%J%qD was used with no prototype before its definition",
5661 decl1, decl1);
5662 /* Optionally warn of any global def with no previous declaration. */
5663 else if (warn_missing_declarations
5664 && TREE_PUBLIC (decl1)
5665 && old_decl == 0
5666 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5667 warning ("%Jno previous declaration for %qD", decl1, decl1);
5668 /* Optionally warn of any def with no previous declaration
5669 if the function has already been used. */
5670 else if (warn_missing_declarations
5671 && old_decl != 0 && TREE_USED (old_decl)
5672 && C_DECL_IMPLICIT (old_decl))
5673 warning ("%J%qD was used with no declaration before its definition",
5674 decl1, decl1);
5676 /* This is a definition, not a reference.
5677 So normally clear DECL_EXTERNAL.
5678 However, `extern inline' acts like a declaration
5679 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5680 DECL_EXTERNAL (decl1) = current_extern_inline;
5682 /* This function exists in static storage.
5683 (This does not mean `static' in the C sense!) */
5684 TREE_STATIC (decl1) = 1;
5686 /* A nested function is not global. */
5687 if (current_function_decl != 0)
5688 TREE_PUBLIC (decl1) = 0;
5690 /* This is the earliest point at which we might know the assembler
5691 name of the function. Thus, if it's set before this, die horribly. */
5692 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5694 /* If #pragma weak was used, mark the decl weak now. */
5695 if (current_scope == file_scope)
5696 maybe_apply_pragma_weak (decl1);
5698 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5699 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5701 tree args;
5702 int argct = 0;
5704 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5705 != integer_type_node)
5706 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5708 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5709 args = TREE_CHAIN (args))
5711 tree type = args ? TREE_VALUE (args) : 0;
5713 if (type == void_type_node)
5714 break;
5716 ++argct;
5717 switch (argct)
5719 case 1:
5720 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5721 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5722 decl1, decl1);
5723 break;
5725 case 2:
5726 if (TREE_CODE (type) != POINTER_TYPE
5727 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5728 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5729 != char_type_node))
5730 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5731 decl1, decl1);
5732 break;
5734 case 3:
5735 if (TREE_CODE (type) != POINTER_TYPE
5736 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5737 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5738 != char_type_node))
5739 pedwarn ("%Jthird argument of %qD should probably be "
5740 "%<char **%>", decl1, decl1);
5741 break;
5745 /* It is intentional that this message does not mention the third
5746 argument because it's only mentioned in an appendix of the
5747 standard. */
5748 if (argct > 0 && (argct < 2 || argct > 3))
5749 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5751 if (! TREE_PUBLIC (decl1))
5752 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5755 /* Record the decl so that the function name is defined.
5756 If we already have a decl for this name, and it is a FUNCTION_DECL,
5757 use the old decl. */
5759 current_function_decl = pushdecl (decl1);
5761 push_scope ();
5762 declare_parm_level ();
5764 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5765 /* Promote the value to int before returning it. */
5766 if (c_promoting_integer_type_p (restype))
5768 /* It retains unsignedness if not really getting wider. */
5769 if (TYPE_UNSIGNED (restype)
5770 && (TYPE_PRECISION (restype)
5771 == TYPE_PRECISION (integer_type_node)))
5772 restype = unsigned_type_node;
5773 else
5774 restype = integer_type_node;
5777 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5778 DECL_ARTIFICIAL (resdecl) = 1;
5779 DECL_IGNORED_P (resdecl) = 1;
5780 DECL_RESULT (current_function_decl) = resdecl;
5782 start_fname_decls ();
5784 return 1;
5787 /* Subroutine of store_parm_decls which handles new-style function
5788 definitions (prototype format). The parms already have decls, so we
5789 need only record them as in effect and complain if any redundant
5790 old-style parm decls were written. */
5791 static void
5792 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5794 tree decl;
5796 if (current_scope->bindings)
5798 error ("%Jold-style parameter declarations in prototyped "
5799 "function definition", fndecl);
5801 /* Get rid of the old-style declarations. */
5802 pop_scope ();
5803 push_scope ();
5805 /* Don't issue this warning for nested functions, and don't issue this
5806 warning if we got here because ARG_INFO_TYPES was error_mark_node
5807 (this happens when a function definition has just an ellipsis in
5808 its parameter list). */
5809 else if (warn_traditional && !in_system_header && !current_function_scope
5810 && arg_info->types != error_mark_node)
5811 warning ("%Jtraditional C rejects ISO C style function definitions",
5812 fndecl);
5814 /* Now make all the parameter declarations visible in the function body.
5815 We can bypass most of the grunt work of pushdecl. */
5816 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5818 DECL_CONTEXT (decl) = current_function_decl;
5819 if (DECL_NAME (decl))
5820 bind (DECL_NAME (decl), decl, current_scope,
5821 /*invisible=*/false, /*nested=*/false);
5822 else
5823 error ("%Jparameter name omitted", decl);
5826 /* Record the parameter list in the function declaration. */
5827 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5829 /* Now make all the ancillary declarations visible, likewise. */
5830 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5832 DECL_CONTEXT (decl) = current_function_decl;
5833 if (DECL_NAME (decl))
5834 bind (DECL_NAME (decl), decl, current_scope,
5835 /*invisible=*/false, /*nested=*/false);
5838 /* And all the tag declarations. */
5839 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5840 if (TREE_PURPOSE (decl))
5841 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5842 /*invisible=*/false, /*nested=*/false);
5845 /* Subroutine of store_parm_decls which handles old-style function
5846 definitions (separate parameter list and declarations). */
5848 static void
5849 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5851 struct c_binding *b;
5852 tree parm, decl, last;
5853 tree parmids = arg_info->parms;
5855 /* We use DECL_WEAK as a flag to show which parameters have been
5856 seen already, since it is not used on PARM_DECL. */
5857 #ifdef ENABLE_CHECKING
5858 for (b = current_scope->bindings; b; b = b->prev)
5859 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5860 #endif
5862 if (warn_old_style_definition && !in_system_header)
5863 warning ("%Jold-style function definition", fndecl);
5865 /* Match each formal parameter name with its declaration. Save each
5866 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5867 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5869 if (TREE_VALUE (parm) == 0)
5871 error ("%Jparameter name missing from parameter list", fndecl);
5872 TREE_PURPOSE (parm) = 0;
5873 continue;
5876 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5877 if (b && B_IN_CURRENT_SCOPE (b))
5879 decl = b->decl;
5880 /* If we got something other than a PARM_DECL it is an error. */
5881 if (TREE_CODE (decl) != PARM_DECL)
5882 error ("%J%qD declared as a non-parameter", decl, decl);
5883 /* If the declaration is already marked, we have a duplicate
5884 name. Complain and ignore the duplicate. */
5885 else if (DECL_WEAK (decl))
5887 error ("%Jmultiple parameters named %qD", decl, decl);
5888 TREE_PURPOSE (parm) = 0;
5889 continue;
5891 /* If the declaration says "void", complain and turn it into
5892 an int. */
5893 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5895 error ("%Jparameter %qD declared with void type", decl, decl);
5896 TREE_TYPE (decl) = integer_type_node;
5897 DECL_ARG_TYPE (decl) = integer_type_node;
5898 layout_decl (decl, 0);
5901 /* If no declaration found, default to int. */
5902 else
5904 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5905 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5906 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5907 pushdecl (decl);
5909 if (flag_isoc99)
5910 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
5911 else if (extra_warnings)
5912 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
5915 TREE_PURPOSE (parm) = decl;
5916 DECL_WEAK (decl) = 1;
5919 /* Now examine the parms chain for incomplete declarations
5920 and declarations with no corresponding names. */
5922 for (b = current_scope->bindings; b; b = b->prev)
5924 parm = b->decl;
5925 if (TREE_CODE (parm) != PARM_DECL)
5926 continue;
5928 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5930 error ("%Jparameter %qD has incomplete type", parm, parm);
5931 TREE_TYPE (parm) = error_mark_node;
5934 if (! DECL_WEAK (parm))
5936 error ("%Jdeclaration for parameter %qD but no such parameter",
5937 parm, parm);
5939 /* Pretend the parameter was not missing.
5940 This gets us to a standard state and minimizes
5941 further error messages. */
5942 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5946 /* Chain the declarations together in the order of the list of
5947 names. Store that chain in the function decl, replacing the
5948 list of names. Update the current scope to match. */
5949 DECL_ARGUMENTS (fndecl) = 0;
5951 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5952 if (TREE_PURPOSE (parm))
5953 break;
5954 if (parm && TREE_PURPOSE (parm))
5956 last = TREE_PURPOSE (parm);
5957 DECL_ARGUMENTS (fndecl) = last;
5958 DECL_WEAK (last) = 0;
5960 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5961 if (TREE_PURPOSE (parm))
5963 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5964 last = TREE_PURPOSE (parm);
5965 DECL_WEAK (last) = 0;
5967 TREE_CHAIN (last) = 0;
5970 /* If there was a previous prototype,
5971 set the DECL_ARG_TYPE of each argument according to
5972 the type previously specified, and report any mismatches. */
5974 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5976 tree type;
5977 for (parm = DECL_ARGUMENTS (fndecl),
5978 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5979 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5980 != void_type_node));
5981 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5983 if (parm == 0 || type == 0
5984 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5986 error ("number of arguments doesn%'t match prototype");
5987 error ("%Hprototype declaration",
5988 &current_function_prototype_locus);
5989 break;
5991 /* Type for passing arg must be consistent with that
5992 declared for the arg. ISO C says we take the unqualified
5993 type for parameters declared with qualified type. */
5994 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5995 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
5997 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5998 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6000 /* Adjust argument to match prototype. E.g. a previous
6001 `int foo(float);' prototype causes
6002 `int foo(x) float x; {...}' to be treated like
6003 `int foo(float x) {...}'. This is particularly
6004 useful for argument types like uid_t. */
6005 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6007 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6008 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6009 && TYPE_PRECISION (TREE_TYPE (parm))
6010 < TYPE_PRECISION (integer_type_node))
6011 DECL_ARG_TYPE (parm) = integer_type_node;
6013 if (pedantic)
6015 pedwarn ("promoted argument %qD "
6016 "doesn%'t match prototype", parm);
6017 pedwarn ("%Hprototype declaration",
6018 &current_function_prototype_locus);
6021 else
6023 error ("argument %qD doesn%'t match prototype", parm);
6024 error ("%Hprototype declaration",
6025 &current_function_prototype_locus);
6029 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6032 /* Otherwise, create a prototype that would match. */
6034 else
6036 tree actual = 0, last = 0, type;
6038 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6040 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6041 if (last)
6042 TREE_CHAIN (last) = type;
6043 else
6044 actual = type;
6045 last = type;
6047 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6048 if (last)
6049 TREE_CHAIN (last) = type;
6050 else
6051 actual = type;
6053 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6054 of the type of this function, but we need to avoid having this
6055 affect the types of other similarly-typed functions, so we must
6056 first force the generation of an identical (but separate) type
6057 node for the relevant function type. The new node we create
6058 will be a variant of the main variant of the original function
6059 type. */
6061 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6063 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6067 /* Store parameter declarations passed in ARG_INFO into the current
6068 function declaration. */
6070 void
6071 store_parm_decls_from (struct c_arg_info *arg_info)
6073 current_function_arg_info = arg_info;
6074 store_parm_decls ();
6077 /* Store the parameter declarations into the current function declaration.
6078 This is called after parsing the parameter declarations, before
6079 digesting the body of the function.
6081 For an old-style definition, construct a prototype out of the old-style
6082 parameter declarations and inject it into the function's type. */
6084 void
6085 store_parm_decls (void)
6087 tree fndecl = current_function_decl;
6088 bool proto;
6090 /* The argument information block for FNDECL. */
6091 struct c_arg_info *arg_info = current_function_arg_info;
6092 current_function_arg_info = 0;
6094 /* True if this definition is written with a prototype. Note:
6095 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6096 list in a function definition as equivalent to (void) -- an
6097 empty argument list specifies the function has no parameters,
6098 but only (void) sets up a prototype for future calls. */
6099 proto = arg_info->types != 0;
6101 if (proto)
6102 store_parm_decls_newstyle (fndecl, arg_info);
6103 else
6104 store_parm_decls_oldstyle (fndecl, arg_info);
6106 /* The next call to push_scope will be a function body. */
6108 next_is_function_body = true;
6110 /* Write a record describing this function definition to the prototypes
6111 file (if requested). */
6113 gen_aux_info_record (fndecl, 1, 0, proto);
6115 /* Initialize the RTL code for the function. */
6116 allocate_struct_function (fndecl);
6118 /* Begin the statement tree for this function. */
6119 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6121 /* ??? Insert the contents of the pending sizes list into the function
6122 to be evaluated. This just changes mis-behavior until assign_parms
6123 phase ordering problems are resolved. */
6125 tree t;
6126 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6127 add_stmt (TREE_VALUE (t));
6130 /* Even though we're inside a function body, we still don't want to
6131 call expand_expr to calculate the size of a variable-sized array.
6132 We haven't necessarily assigned RTL to all variables yet, so it's
6133 not safe to try to expand expressions involving them. */
6134 cfun->x_dont_save_pending_sizes_p = 1;
6137 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6138 functions. */
6140 static void
6141 c_warn_unused_result_recursively (tree fndecl)
6143 struct cgraph_node *cgn;
6145 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6146 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6148 /* Finalize all nested functions now. */
6149 cgn = cgraph_node (fndecl);
6150 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6151 c_warn_unused_result_recursively (cgn->decl);
6154 /* Finish up a function declaration and compile that function
6155 all the way to assembler language output. The free the storage
6156 for the function definition.
6158 This is called after parsing the body of the function definition. */
6160 void
6161 finish_function (void)
6163 tree fndecl = current_function_decl;
6165 if (TREE_CODE (fndecl) == FUNCTION_DECL
6166 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6168 tree args = DECL_ARGUMENTS (fndecl);
6169 for (; args; args = TREE_CHAIN (args))
6171 tree type = TREE_TYPE (args);
6172 if (INTEGRAL_TYPE_P (type)
6173 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6174 DECL_ARG_TYPE (args) = integer_type_node;
6178 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6179 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6181 /* Must mark the RESULT_DECL as being in this function. */
6183 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6184 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6186 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6188 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6189 != integer_type_node)
6191 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6192 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6193 if (!warn_main)
6194 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6196 else
6198 if (flag_isoc99)
6199 c_finish_return (integer_zero_node);
6203 /* Tie off the statement tree for this function. */
6204 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6206 finish_fname_decls ();
6208 /* Complain if there's just no return statement. */
6209 if (warn_return_type
6210 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6211 && !current_function_returns_value && !current_function_returns_null
6212 /* Don't complain if we abort. */
6213 && !current_function_returns_abnormally
6214 /* Don't warn for main(). */
6215 && !MAIN_NAME_P (DECL_NAME (fndecl))
6216 /* Or if they didn't actually specify a return type. */
6217 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6218 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6219 inline function, as we might never be compiled separately. */
6220 && DECL_INLINE (fndecl))
6221 warning ("no return statement in function returning non-void");
6223 /* With just -Wextra, complain only if function returns both with
6224 and without a value. */
6225 if (extra_warnings
6226 && current_function_returns_value
6227 && current_function_returns_null)
6228 warning ("this function may return with or without a value");
6230 /* Store the end of the function, so that we get good line number
6231 info for the epilogue. */
6232 cfun->function_end_locus = input_location;
6234 /* If we don't have ctors/dtors sections, and this is a static
6235 constructor or destructor, it must be recorded now. */
6236 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6237 && !targetm.have_ctors_dtors)
6238 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6239 if (DECL_STATIC_DESTRUCTOR (fndecl)
6240 && !targetm.have_ctors_dtors)
6241 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6243 /* Finalize the ELF visibility for the function. */
6244 c_determine_visibility (fndecl);
6246 /* Genericize before inlining. Delay genericizing nested functions
6247 until their parent function is genericized. Since finalizing
6248 requires GENERIC, delay that as well. */
6250 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6252 if (!decl_function_context (fndecl))
6254 c_genericize (fndecl);
6255 c_warn_unused_result_recursively (fndecl);
6257 /* ??? Objc emits functions after finalizing the compilation unit.
6258 This should be cleaned up later and this conditional removed. */
6259 if (cgraph_global_info_ready)
6261 c_expand_body (fndecl);
6262 return;
6265 cgraph_finalize_function (fndecl, false);
6267 else
6269 /* Register this function with cgraph just far enough to get it
6270 added to our parent's nested function list. Handy, since the
6271 C front end doesn't have such a list. */
6272 (void) cgraph_node (fndecl);
6276 /* We're leaving the context of this function, so zap cfun.
6277 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6278 tree_rest_of_compilation. */
6279 cfun = NULL;
6280 current_function_decl = NULL;
6283 /* Generate the RTL for the body of FNDECL. */
6285 void
6286 c_expand_body (tree fndecl)
6289 if (!DECL_INITIAL (fndecl)
6290 || DECL_INITIAL (fndecl) == error_mark_node)
6291 return;
6293 tree_rest_of_compilation (fndecl, false);
6295 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6296 && targetm.have_ctors_dtors)
6297 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6298 DEFAULT_INIT_PRIORITY);
6299 if (DECL_STATIC_DESTRUCTOR (fndecl)
6300 && targetm.have_ctors_dtors)
6301 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6302 DEFAULT_INIT_PRIORITY);
6305 /* Check the declarations given in a for-loop for satisfying the C99
6306 constraints. */
6307 void
6308 check_for_loop_decls (void)
6310 struct c_binding *b;
6312 if (!flag_isoc99)
6314 /* If we get here, declarations have been used in a for loop without
6315 the C99 for loop scope. This doesn't make much sense, so don't
6316 allow it. */
6317 error ("%<for%> loop initial declaration used outside C99 mode");
6318 return;
6320 /* C99 subclause 6.8.5 paragraph 3:
6322 [#3] The declaration part of a for statement shall only
6323 declare identifiers for objects having storage class auto or
6324 register.
6326 It isn't clear whether, in this sentence, "identifiers" binds to
6327 "shall only declare" or to "objects" - that is, whether all identifiers
6328 declared must be identifiers for objects, or whether the restriction
6329 only applies to those that are. (A question on this in comp.std.c
6330 in November 2000 received no answer.) We implement the strictest
6331 interpretation, to avoid creating an extension which later causes
6332 problems. */
6334 for (b = current_scope->bindings; b; b = b->prev)
6336 tree id = b->id;
6337 tree decl = b->decl;
6339 if (!id)
6340 continue;
6342 switch (TREE_CODE (decl))
6344 case VAR_DECL:
6345 if (TREE_STATIC (decl))
6346 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6347 "initial declaration", decl, decl);
6348 else if (DECL_EXTERNAL (decl))
6349 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6350 "initial declaration", decl, decl);
6351 break;
6353 case RECORD_TYPE:
6354 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6355 id);
6356 break;
6357 case UNION_TYPE:
6358 error ("%<union %E%> declared in %<for%> loop initial declaration",
6359 id);
6360 break;
6361 case ENUMERAL_TYPE:
6362 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6363 id);
6364 break;
6365 default:
6366 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6367 "initial declaration", decl, decl);
6372 /* Save and reinitialize the variables
6373 used during compilation of a C function. */
6375 void
6376 c_push_function_context (struct function *f)
6378 struct language_function *p;
6379 p = GGC_NEW (struct language_function);
6380 f->language = p;
6382 p->base.x_stmt_tree = c_stmt_tree;
6383 p->x_break_label = c_break_label;
6384 p->x_cont_label = c_cont_label;
6385 p->x_switch_stack = c_switch_stack;
6386 p->arg_info = current_function_arg_info;
6387 p->returns_value = current_function_returns_value;
6388 p->returns_null = current_function_returns_null;
6389 p->returns_abnormally = current_function_returns_abnormally;
6390 p->warn_about_return_type = warn_about_return_type;
6391 p->extern_inline = current_extern_inline;
6394 /* Restore the variables used during compilation of a C function. */
6396 void
6397 c_pop_function_context (struct function *f)
6399 struct language_function *p = f->language;
6401 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6402 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6404 /* Stop pointing to the local nodes about to be freed. */
6405 /* But DECL_INITIAL must remain nonzero so we know this
6406 was an actual function definition. */
6407 DECL_INITIAL (current_function_decl) = error_mark_node;
6408 DECL_ARGUMENTS (current_function_decl) = 0;
6411 c_stmt_tree = p->base.x_stmt_tree;
6412 c_break_label = p->x_break_label;
6413 c_cont_label = p->x_cont_label;
6414 c_switch_stack = p->x_switch_stack;
6415 current_function_arg_info = p->arg_info;
6416 current_function_returns_value = p->returns_value;
6417 current_function_returns_null = p->returns_null;
6418 current_function_returns_abnormally = p->returns_abnormally;
6419 warn_about_return_type = p->warn_about_return_type;
6420 current_extern_inline = p->extern_inline;
6422 f->language = NULL;
6425 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6427 void
6428 c_dup_lang_specific_decl (tree decl)
6430 struct lang_decl *ld;
6432 if (!DECL_LANG_SPECIFIC (decl))
6433 return;
6435 ld = GGC_NEW (struct lang_decl);
6436 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6437 DECL_LANG_SPECIFIC (decl) = ld;
6440 /* The functions below are required for functionality of doing
6441 function at once processing in the C front end. Currently these
6442 functions are not called from anywhere in the C front end, but as
6443 these changes continue, that will change. */
6445 /* Returns nonzero if the current statement is a full expression,
6446 i.e. temporaries created during that statement should be destroyed
6447 at the end of the statement. */
6450 stmts_are_full_exprs_p (void)
6452 return 0;
6455 /* Returns the stmt_tree (if any) to which statements are currently
6456 being added. If there is no active statement-tree, NULL is
6457 returned. */
6459 stmt_tree
6460 current_stmt_tree (void)
6462 return &c_stmt_tree;
6465 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6466 C. */
6469 anon_aggr_type_p (tree ARG_UNUSED (node))
6471 return 0;
6474 /* Return the global value of T as a symbol. */
6476 tree
6477 identifier_global_value (tree t)
6479 struct c_binding *b;
6481 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6482 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6483 return b->decl;
6485 return 0;
6488 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6489 otherwise the name is found in ridpointers from RID_INDEX. */
6491 void
6492 record_builtin_type (enum rid rid_index, const char *name, tree type)
6494 tree id, decl;
6495 if (name == 0)
6496 id = ridpointers[(int) rid_index];
6497 else
6498 id = get_identifier (name);
6499 decl = build_decl (TYPE_DECL, id, type);
6500 pushdecl (decl);
6501 if (debug_hooks->type_decl)
6502 debug_hooks->type_decl (decl, false);
6505 /* Build the void_list_node (void_type_node having been created). */
6506 tree
6507 build_void_list_node (void)
6509 tree t = build_tree_list (NULL_TREE, void_type_node);
6510 return t;
6513 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6515 struct c_parm *
6516 build_c_parm (struct c_declspecs *specs, tree attrs,
6517 struct c_declarator *declarator)
6519 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6520 ret->specs = specs;
6521 ret->attrs = attrs;
6522 ret->declarator = declarator;
6523 return ret;
6526 /* Return a declarator with nested attributes. TARGET is the inner
6527 declarator to which these attributes apply. ATTRS are the
6528 attributes. */
6530 struct c_declarator *
6531 build_attrs_declarator (tree attrs, struct c_declarator *target)
6533 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6534 ret->kind = cdk_attrs;
6535 ret->declarator = target;
6536 ret->u.attrs = attrs;
6537 return ret;
6540 /* Return a declarator for a function with arguments specified by ARGS
6541 and return type specified by TARGET. */
6543 struct c_declarator *
6544 build_function_declarator (struct c_arg_info *args,
6545 struct c_declarator *target)
6547 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6548 ret->kind = cdk_function;
6549 ret->declarator = target;
6550 ret->u.arg_info = args;
6551 return ret;
6554 /* Return a declarator for the identifier IDENT (which may be
6555 NULL_TREE for an abstract declarator). */
6557 struct c_declarator *
6558 build_id_declarator (tree ident)
6560 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6561 ret->kind = cdk_id;
6562 ret->declarator = 0;
6563 ret->u.id = ident;
6564 return ret;
6567 /* Return something to represent absolute declarators containing a *.
6568 TARGET is the absolute declarator that the * contains.
6569 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6570 to apply to the pointer type. */
6572 struct c_declarator *
6573 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6574 struct c_declarator *target)
6576 tree attrs;
6577 int quals = 0;
6578 struct c_declarator *itarget = target;
6579 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6580 if (type_quals_attrs)
6582 attrs = type_quals_attrs->attrs;
6583 quals = quals_from_declspecs (type_quals_attrs);
6584 if (attrs != NULL_TREE)
6585 itarget = build_attrs_declarator (attrs, target);
6587 ret->kind = cdk_pointer;
6588 ret->declarator = itarget;
6589 ret->u.pointer_quals = quals;
6590 return ret;
6593 /* Return a pointer to a structure for an empty list of declaration
6594 specifiers. */
6596 struct c_declspecs *
6597 build_null_declspecs (void)
6599 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6600 ret->type = 0;
6601 ret->decl_attr = 0;
6602 ret->attrs = 0;
6603 ret->typespec_word = cts_none;
6604 ret->storage_class = csc_none;
6605 ret->non_sc_seen_p = false;
6606 ret->typedef_p = false;
6607 ret->explicit_signed_p = false;
6608 ret->deprecated_p = false;
6609 ret->default_int_p = false;
6610 ret->long_p = false;
6611 ret->long_long_p = false;
6612 ret->short_p = false;
6613 ret->signed_p = false;
6614 ret->unsigned_p = false;
6615 ret->complex_p = false;
6616 ret->inline_p = false;
6617 ret->thread_p = false;
6618 ret->const_p = false;
6619 ret->volatile_p = false;
6620 ret->restrict_p = false;
6621 return ret;
6624 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6625 returning SPECS. */
6627 struct c_declspecs *
6628 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6630 enum rid i;
6631 bool dupe = false;
6632 specs->non_sc_seen_p = true;
6633 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6634 && C_IS_RESERVED_WORD (qual));
6635 i = C_RID_CODE (qual);
6636 switch (i)
6638 case RID_CONST:
6639 dupe = specs->const_p;
6640 specs->const_p = true;
6641 break;
6642 case RID_VOLATILE:
6643 dupe = specs->volatile_p;
6644 specs->volatile_p = true;
6645 break;
6646 case RID_RESTRICT:
6647 dupe = specs->restrict_p;
6648 specs->restrict_p = true;
6649 break;
6650 default:
6651 gcc_unreachable ();
6653 if (dupe && pedantic && !flag_isoc99)
6654 pedwarn ("duplicate %qs", IDENTIFIER_POINTER (qual));
6655 return specs;
6658 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6659 returning SPECS. */
6661 struct c_declspecs *
6662 declspecs_add_type (struct c_declspecs *specs, tree type)
6664 specs->non_sc_seen_p = true;
6665 if (TREE_DEPRECATED (type))
6666 specs->deprecated_p = true;
6668 /* Handle type specifier keywords. */
6669 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6671 enum rid i = C_RID_CODE (type);
6672 if (specs->type)
6674 error ("two or more data types in declaration specifiers");
6675 return specs;
6677 if ((int) i <= (int) RID_LAST_MODIFIER)
6679 /* "long", "short", "signed", "unsigned" or "_Complex". */
6680 bool dupe = false;
6681 switch (i)
6683 case RID_LONG:
6684 if (specs->long_long_p)
6686 error ("%<long long long%> is too long for GCC");
6687 break;
6689 if (specs->long_p)
6691 if (specs->typespec_word == cts_double)
6693 error ("both %<long long%> and %<double%> in "
6694 "declaration specifiers");
6695 break;
6697 if (pedantic && !flag_isoc99 && !in_system_header
6698 && warn_long_long)
6699 pedwarn ("ISO C90 does not support %<long long%>");
6700 specs->long_long_p = 1;
6701 break;
6703 if (specs->short_p)
6704 error ("both %<long%> and %<short%> in "
6705 "declaration specifiers");
6706 else if (specs->typespec_word == cts_void)
6707 error ("both %<long%> and %<void%> in "
6708 "declaration specifiers");
6709 else if (specs->typespec_word == cts_bool)
6710 error ("both %<long%> and %<_Bool%> in "
6711 "declaration specifiers");
6712 else if (specs->typespec_word == cts_char)
6713 error ("both %<long%> and %<char%> in "
6714 "declaration specifiers");
6715 else if (specs->typespec_word == cts_float)
6716 error ("both %<long%> and %<float%> in "
6717 "declaration specifiers");
6718 else
6719 specs->long_p = true;
6720 break;
6721 case RID_SHORT:
6722 dupe = specs->short_p;
6723 if (specs->long_p)
6724 error ("both %<long%> and %<short%> in "
6725 "declaration specifiers");
6726 else if (specs->typespec_word == cts_void)
6727 error ("both %<short%> and %<void%> in "
6728 "declaration specifiers");
6729 else if (specs->typespec_word == cts_bool)
6730 error ("both %<short%> and %<_Bool%> in "
6731 "declaration specifiers");
6732 else if (specs->typespec_word == cts_char)
6733 error ("both %<short%> and %<char%> in "
6734 "declaration specifiers");
6735 else if (specs->typespec_word == cts_float)
6736 error ("both %<short%> and %<float%> in "
6737 "declaration specifiers");
6738 else if (specs->typespec_word == cts_double)
6739 error ("both %<short%> and %<double%> in "
6740 "declaration specifiers");
6741 else
6742 specs->short_p = true;
6743 break;
6744 case RID_SIGNED:
6745 dupe = specs->signed_p;
6746 if (specs->unsigned_p)
6747 error ("both %<signed%> and %<unsigned%> in "
6748 "declaration specifiers");
6749 else if (specs->typespec_word == cts_void)
6750 error ("both %<signed%> and %<void%> in "
6751 "declaration specifiers");
6752 else if (specs->typespec_word == cts_bool)
6753 error ("both %<signed%> and %<_Bool%> in "
6754 "declaration specifiers");
6755 else if (specs->typespec_word == cts_float)
6756 error ("both %<signed%> and %<float%> in "
6757 "declaration specifiers");
6758 else if (specs->typespec_word == cts_double)
6759 error ("both %<signed%> and %<double%> in "
6760 "declaration specifiers");
6761 else
6762 specs->signed_p = true;
6763 break;
6764 case RID_UNSIGNED:
6765 dupe = specs->unsigned_p;
6766 if (specs->signed_p)
6767 error ("both %<signed%> and %<unsigned%> in "
6768 "declaration specifiers");
6769 else if (specs->typespec_word == cts_void)
6770 error ("both %<unsigned%> and %<void%> in "
6771 "declaration specifiers");
6772 else if (specs->typespec_word == cts_bool)
6773 error ("both %<unsigned%> and %<_Bool%> in "
6774 "declaration specifiers");
6775 else if (specs->typespec_word == cts_float)
6776 error ("both %<unsigned%> and %<float%> in "
6777 "declaration specifiers");
6778 else if (specs->typespec_word == cts_double)
6779 error ("both %<unsigned%> and %<double%> in "
6780 "declaration specifiers");
6781 else
6782 specs->unsigned_p = true;
6783 break;
6784 case RID_COMPLEX:
6785 dupe = specs->complex_p;
6786 if (pedantic && !flag_isoc99)
6787 pedwarn ("ISO C90 does not support complex types");
6788 if (specs->typespec_word == cts_void)
6789 error ("both %<complex%> and %<void%> in "
6790 "declaration specifiers");
6791 else if (specs->typespec_word == cts_bool)
6792 error ("both %<complex%> and %<_Bool%> in "
6793 "declaration specifiers");
6794 else
6795 specs->complex_p = true;
6796 break;
6797 default:
6798 gcc_unreachable ();
6801 if (dupe)
6802 error ("duplicate %qs", IDENTIFIER_POINTER (type));
6804 return specs;
6806 else
6808 /* "void", "_Bool", "char", "int", "float" or "double". */
6809 if (specs->typespec_word != cts_none)
6811 error ("two or more data types in declaration specifiers");
6812 return specs;
6814 switch (i)
6816 case RID_VOID:
6817 if (specs->long_p)
6818 error ("both %<long%> and %<void%> in "
6819 "declaration specifiers");
6820 else if (specs->short_p)
6821 error ("both %<short%> and %<void%> in "
6822 "declaration specifiers");
6823 else if (specs->signed_p)
6824 error ("both %<signed%> and %<void%> in "
6825 "declaration specifiers");
6826 else if (specs->unsigned_p)
6827 error ("both %<unsigned%> and %<void%> in "
6828 "declaration specifiers");
6829 else if (specs->complex_p)
6830 error ("both %<complex%> and %<void%> in "
6831 "declaration specifiers");
6832 else
6833 specs->typespec_word = cts_void;
6834 return specs;
6835 case RID_BOOL:
6836 if (specs->long_p)
6837 error ("both %<long%> and %<_Bool%> in "
6838 "declaration specifiers");
6839 else if (specs->short_p)
6840 error ("both %<short%> and %<_Bool%> in "
6841 "declaration specifiers");
6842 else if (specs->signed_p)
6843 error ("both %<signed%> and %<_Bool%> in "
6844 "declaration specifiers");
6845 else if (specs->unsigned_p)
6846 error ("both %<unsigned%> and %<_Bool%> in "
6847 "declaration specifiers");
6848 else if (specs->complex_p)
6849 error ("both %<complex%> and %<_Bool%> in "
6850 "declaration specifiers");
6851 else
6852 specs->typespec_word = cts_bool;
6853 return specs;
6854 case RID_CHAR:
6855 if (specs->long_p)
6856 error ("both %<long%> and %<char%> in "
6857 "declaration specifiers");
6858 else if (specs->short_p)
6859 error ("both %<short%> and %<char%> in "
6860 "declaration specifiers");
6861 else
6862 specs->typespec_word = cts_char;
6863 return specs;
6864 case RID_INT:
6865 specs->typespec_word = cts_int;
6866 return specs;
6867 case RID_FLOAT:
6868 if (specs->long_p)
6869 error ("both %<long%> and %<float%> in "
6870 "declaration specifiers");
6871 else if (specs->short_p)
6872 error ("both %<short%> and %<float%> in "
6873 "declaration specifiers");
6874 else if (specs->signed_p)
6875 error ("both %<signed%> and %<float%> in "
6876 "declaration specifiers");
6877 else if (specs->unsigned_p)
6878 error ("both %<unsigned%> and %<float%> in "
6879 "declaration specifiers");
6880 else
6881 specs->typespec_word = cts_float;
6882 return specs;
6883 case RID_DOUBLE:
6884 if (specs->long_long_p)
6885 error ("both %<long long%> and %<double%> in "
6886 "declaration specifiers");
6887 else if (specs->short_p)
6888 error ("both %<short%> and %<double%> in "
6889 "declaration specifiers");
6890 else if (specs->signed_p)
6891 error ("both %<signed%> and %<double%> in "
6892 "declaration specifiers");
6893 else if (specs->unsigned_p)
6894 error ("both %<unsigned%> and %<double%> in "
6895 "declaration specifiers");
6896 else
6897 specs->typespec_word = cts_double;
6898 return specs;
6899 default:
6900 /* ObjC reserved word "id", handled below. */
6901 break;
6906 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
6907 form of ObjC type, cases such as "int" and "long" being handled
6908 above), a TYPE (struct, union, enum and typeof specifiers) or an
6909 ERROR_MARK. In none of these cases may there have previously
6910 been any type specifiers. */
6911 if (specs->type || specs->typespec_word != cts_none
6912 || specs->long_p || specs->short_p || specs->signed_p
6913 || specs->unsigned_p || specs->complex_p)
6914 error ("two or more data types in declaration specifiers");
6915 else if (TREE_CODE (type) == TYPE_DECL)
6917 if (TREE_TYPE (type) == error_mark_node)
6918 ; /* Allow the type to default to int to avoid cascading errors. */
6919 else
6921 specs->type = TREE_TYPE (type);
6922 specs->decl_attr = DECL_ATTRIBUTES (type);
6923 specs->typedef_p = true;
6924 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
6927 else if (TREE_CODE (type) == IDENTIFIER_NODE)
6929 tree t = lookup_name (type);
6930 if (!t || TREE_CODE (t) != TYPE_DECL)
6931 error ("%qs fails to be a typedef or built in type",
6932 IDENTIFIER_POINTER (type));
6933 else if (TREE_TYPE (t) == error_mark_node)
6935 else
6936 specs->type = TREE_TYPE (t);
6938 else if (TREE_CODE (type) != ERROR_MARK)
6939 specs->type = type;
6941 return specs;
6944 /* Add the storage class specifier or function specifier SCSPEC to the
6945 declaration specifiers SPECS, returning SPECS. */
6947 struct c_declspecs *
6948 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
6950 enum rid i;
6951 enum c_storage_class n = csc_none;
6952 bool dupe = false;
6953 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
6954 && C_IS_RESERVED_WORD (scspec));
6955 i = C_RID_CODE (scspec);
6956 if (extra_warnings && specs->non_sc_seen_p)
6957 warning ("%qs is not at beginning of declaration",
6958 IDENTIFIER_POINTER (scspec));
6959 switch (i)
6961 case RID_INLINE:
6962 /* C99 permits duplicate inline. Although of doubtful utility,
6963 it seems simplest to permit it in gnu89 mode as well, as
6964 there is also little utility in maintaining this as a
6965 difference between gnu89 and C99 inline. */
6966 dupe = false;
6967 specs->inline_p = true;
6968 break;
6969 case RID_THREAD:
6970 dupe = specs->thread_p;
6971 if (specs->storage_class == csc_auto)
6972 error ("%<__thread%> used with %<auto%>");
6973 else if (specs->storage_class == csc_register)
6974 error ("%<__thread%> used with %<register%>");
6975 else if (specs->storage_class == csc_typedef)
6976 error ("%<__thread%> used with %<typedef%>");
6977 else
6978 specs->thread_p = true;
6979 break;
6980 case RID_AUTO:
6981 n = csc_auto;
6982 break;
6983 case RID_EXTERN:
6984 n = csc_extern;
6985 /* Diagnose "__thread extern". */
6986 if (specs->thread_p)
6987 error ("%<__thread%> before %<extern%>");
6988 break;
6989 case RID_REGISTER:
6990 n = csc_register;
6991 break;
6992 case RID_STATIC:
6993 n = csc_static;
6994 /* Diagnose "__thread static". */
6995 if (specs->thread_p)
6996 error ("%<__thread%> before %<static%>");
6997 break;
6998 case RID_TYPEDEF:
6999 n = csc_typedef;
7000 break;
7001 default:
7002 gcc_unreachable ();
7004 if (n != csc_none && n == specs->storage_class)
7005 dupe = true;
7006 if (dupe)
7007 error ("duplicate %qs", IDENTIFIER_POINTER (scspec));
7008 if (n != csc_none)
7010 if (specs->storage_class != csc_none && n != specs->storage_class)
7012 error ("multiple storage classes in declaration specifiers");
7014 else
7016 specs->storage_class = n;
7017 if (n != csc_extern && n != csc_static && specs->thread_p)
7019 error ("%<__thread%> used with %qs",
7020 IDENTIFIER_POINTER (scspec));
7021 specs->thread_p = false;
7025 return specs;
7028 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7029 returning SPECS. */
7031 struct c_declspecs *
7032 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7034 specs->attrs = chainon (attrs, specs->attrs);
7035 return specs;
7038 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7039 specifiers with any other type specifier to determine the resulting
7040 type. This is where ISO C checks on complex types are made, since
7041 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7042 double". */
7044 struct c_declspecs *
7045 finish_declspecs (struct c_declspecs *specs)
7047 /* If a type was specified as a whole, we have no modifiers and are
7048 done. */
7049 if (specs->type != NULL_TREE)
7051 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7052 && !specs->signed_p && !specs->unsigned_p
7053 && !specs->complex_p);
7054 return specs;
7057 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7058 has been specified, treat it as "int" unless "_Complex" is
7059 present and there are no other specifiers. If we just have
7060 "_Complex", it is equivalent to "_Complex double", but e.g.
7061 "_Complex short" is equivalent to "_Complex short int". */
7062 if (specs->typespec_word == cts_none)
7064 if (specs->long_p || specs->short_p
7065 || specs->signed_p || specs->unsigned_p)
7067 specs->typespec_word = cts_int;
7069 else if (specs->complex_p)
7071 specs->typespec_word = cts_double;
7072 if (pedantic)
7073 pedwarn ("ISO C does not support plain %<complex%> meaning "
7074 "%<double complex%>");
7076 else
7078 specs->typespec_word = cts_int;
7079 specs->default_int_p = true;
7080 /* We don't diagnose this here because grokdeclarator will
7081 give more specific diagnostics according to whether it is
7082 a function definition. */
7086 /* If "signed" was specified, record this to distinguish "int" and
7087 "signed int" in the case of a bit-field with
7088 -funsigned-bitfields. */
7089 specs->explicit_signed_p = specs->signed_p;
7091 /* Now compute the actual type. */
7092 switch (specs->typespec_word)
7094 case cts_void:
7095 gcc_assert (!specs->long_p && !specs->short_p
7096 && !specs->signed_p && !specs->unsigned_p
7097 && !specs->complex_p);
7098 specs->type = void_type_node;
7099 break;
7100 case cts_bool:
7101 gcc_assert (!specs->long_p && !specs->short_p
7102 && !specs->signed_p && !specs->unsigned_p
7103 && !specs->complex_p);
7104 specs->type = boolean_type_node;
7105 break;
7106 case cts_char:
7107 gcc_assert (!specs->long_p && !specs->short_p);
7108 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7109 if (specs->signed_p)
7110 specs->type = signed_char_type_node;
7111 else if (specs->unsigned_p)
7112 specs->type = unsigned_char_type_node;
7113 else
7114 specs->type = char_type_node;
7115 if (specs->complex_p)
7117 if (pedantic)
7118 pedwarn ("ISO C does not support complex integer types");
7119 specs->type = build_complex_type (specs->type);
7121 break;
7122 case cts_int:
7123 gcc_assert (!(specs->long_p && specs->short_p));
7124 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7125 if (specs->long_long_p)
7126 specs->type = (specs->unsigned_p
7127 ? long_long_unsigned_type_node
7128 : long_long_integer_type_node);
7129 else if (specs->long_p)
7130 specs->type = (specs->unsigned_p
7131 ? long_unsigned_type_node
7132 : long_integer_type_node);
7133 else if (specs->short_p)
7134 specs->type = (specs->unsigned_p
7135 ? short_unsigned_type_node
7136 : short_integer_type_node);
7137 else
7138 specs->type = (specs->unsigned_p
7139 ? unsigned_type_node
7140 : integer_type_node);
7141 if (specs->complex_p)
7143 if (pedantic)
7144 pedwarn ("ISO C does not support complex integer types");
7145 specs->type = build_complex_type (specs->type);
7147 break;
7148 case cts_float:
7149 gcc_assert (!specs->long_p && !specs->short_p
7150 && !specs->signed_p && !specs->unsigned_p);
7151 specs->type = (specs->complex_p
7152 ? complex_float_type_node
7153 : float_type_node);
7154 break;
7155 case cts_double:
7156 gcc_assert (!specs->long_long_p && !specs->short_p
7157 && !specs->signed_p && !specs->unsigned_p);
7158 if (specs->long_p)
7160 specs->type = (specs->complex_p
7161 ? complex_long_double_type_node
7162 : long_double_type_node);
7164 else
7166 specs->type = (specs->complex_p
7167 ? complex_double_type_node
7168 : double_type_node);
7170 break;
7171 default:
7172 gcc_unreachable ();
7175 return specs;
7178 /* Synthesize a function which calls all the global ctors or global
7179 dtors in this file. This is only used for targets which do not
7180 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7181 static void
7182 build_cdtor (int method_type, tree cdtors)
7184 tree body = 0;
7186 if (!cdtors)
7187 return;
7189 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7190 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7191 &body);
7193 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7196 /* Perform final processing on one file scope's declarations (or the
7197 external scope's declarations), GLOBALS. */
7198 static void
7199 c_write_global_declarations_1 (tree globals)
7201 size_t len = list_length (globals);
7202 tree *vec = XNEWVEC (tree, len);
7203 size_t i;
7204 tree decl;
7206 /* Process the decls in the order they were written. */
7207 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7209 vec[i] = decl;
7210 /* Check for used but undefined static functions using the C
7211 standard's definition of "used", and set TREE_NO_WARNING so
7212 that check_global_declarations doesn't repeat the check. */
7213 if (TREE_CODE (decl) == FUNCTION_DECL
7214 && DECL_INITIAL (decl) == 0
7215 && DECL_EXTERNAL (decl)
7216 && !TREE_PUBLIC (decl)
7217 && C_DECL_USED (decl))
7219 pedwarn ("%J%qF used but never defined", decl, decl);
7220 TREE_NO_WARNING (decl) = 1;
7224 wrapup_global_declarations (vec, len);
7225 check_global_declarations (vec, len);
7227 free (vec);
7230 void
7231 c_write_global_declarations (void)
7233 tree ext_block, t;
7235 /* We don't want to do this if generating a PCH. */
7236 if (pch_file)
7237 return;
7239 /* Don't waste time on further processing if -fsyntax-only or we've
7240 encountered errors. */
7241 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7242 return;
7244 /* Close the external scope. */
7245 ext_block = pop_scope ();
7246 external_scope = 0;
7247 gcc_assert (!current_scope);
7249 /* Process all file scopes in this compilation, and the external_scope,
7250 through wrapup_global_declarations and check_global_declarations. */
7251 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7252 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7253 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7255 /* Generate functions to call static constructors and destructors
7256 for targets that do not support .ctors/.dtors sections. These
7257 functions have magic names which are detected by collect2. */
7258 build_cdtor ('I', static_ctors); static_ctors = 0;
7259 build_cdtor ('D', static_dtors); static_dtors = 0;
7261 /* We're done parsing; proceed to optimize and emit assembly.
7262 FIXME: shouldn't be the front end's responsibility to call this. */
7263 cgraph_optimize ();
7265 /* Presently this has to happen after cgraph_optimize.
7266 FIXME: shouldn't be the front end's responsibility to call this. */
7267 if (flag_mudflap)
7268 mudflap_finish_file ();
7271 #include "gt-c-decl.h"