* tree-data-ref.c (free_data_refs): Free each data_reference
[official-gcc.git] / gcc / c-decl.c
blobea26da44a3c048ba7673d59d75639f8d549d983e
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", 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 && scope != external_scope)
772 warning ("%Junused variable %qD", p, p);
774 if (b->inner_comp)
776 error ("%Jtype of array %qD completed incompatibly with"
777 " implicit initialization", p, p);
780 /* Fall through. */
781 case TYPE_DECL:
782 case CONST_DECL:
783 common_symbol:
784 /* All of these go in BLOCK_VARS, but only if this is the
785 binding in the home scope. */
786 if (!b->nested)
788 TREE_CHAIN (p) = BLOCK_VARS (block);
789 BLOCK_VARS (block) = p;
791 /* If this is the file scope, and we are processing more
792 than one translation unit in this compilation, set
793 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
794 This makes same_translation_unit_p work, and causes
795 static declarations to be given disambiguating suffixes. */
796 if (scope == file_scope && num_in_fnames > 1)
798 DECL_CONTEXT (p) = context;
799 if (TREE_CODE (p) == TYPE_DECL)
800 set_type_context (TREE_TYPE (p), context);
803 /* Fall through. */
804 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
805 already been put there by store_parm_decls. Unused-
806 parameter warnings are handled by function.c.
807 error_mark_node obviously does not go in BLOCK_VARS and
808 does not get unused-variable warnings. */
809 case PARM_DECL:
810 case ERROR_MARK:
811 /* It is possible for a decl not to have a name. We get
812 here with b->id NULL in this case. */
813 if (b->id)
815 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
816 I_SYMBOL_BINDING (b->id) = b->shadowed;
817 if (b->shadowed && b->shadowed->type)
818 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
820 break;
822 default:
823 gcc_unreachable ();
828 /* Dispose of the block that we just made inside some higher level. */
829 if ((scope->function_body || scope == file_scope) && context)
831 DECL_INITIAL (context) = block;
832 BLOCK_SUPERCONTEXT (block) = context;
834 else if (scope->outer)
836 if (block)
837 SCOPE_LIST_APPEND (scope->outer, blocks, block);
838 /* If we did not make a block for the scope just exited, any
839 blocks made for inner scopes must be carried forward so they
840 will later become subblocks of something else. */
841 else if (scope->blocks)
842 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
845 /* Pop the current scope, and free the structure for reuse. */
846 current_scope = scope->outer;
847 if (scope->function_body)
848 current_function_scope = scope->outer_function;
850 memset (scope, 0, sizeof (struct c_scope));
851 scope->outer = scope_freelist;
852 scope_freelist = scope;
854 return block;
857 void
858 push_file_scope (void)
860 tree decl;
862 if (file_scope)
863 return;
865 push_scope ();
866 file_scope = current_scope;
868 start_fname_decls ();
870 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
871 bind (DECL_NAME (decl), decl, file_scope,
872 /*invisible=*/false, /*nested=*/true);
875 void
876 pop_file_scope (void)
878 /* In case there were missing closebraces, get us back to the global
879 binding level. */
880 while (current_scope != file_scope)
881 pop_scope ();
883 /* __FUNCTION__ is defined at file scope (""). This
884 call may not be necessary as my tests indicate it
885 still works without it. */
886 finish_fname_decls ();
888 /* This is the point to write out a PCH if we're doing that.
889 In that case we do not want to do anything else. */
890 if (pch_file)
892 c_common_write_pch ();
893 return;
896 /* Pop off the file scope and close this translation unit. */
897 pop_scope ();
898 file_scope = 0;
899 cgraph_finalize_compilation_unit ();
902 /* Insert BLOCK at the end of the list of subblocks of the current
903 scope. This is used when a BIND_EXPR is expanded, to handle the
904 BLOCK node inside the BIND_EXPR. */
906 void
907 insert_block (tree block)
909 TREE_USED (block) = 1;
910 SCOPE_LIST_APPEND (current_scope, blocks, block);
913 /* Push a definition or a declaration of struct, union or enum tag "name".
914 "type" should be the type node.
915 We assume that the tag "name" is not already defined.
917 Note that the definition may really be just a forward reference.
918 In that case, the TYPE_SIZE will be zero. */
920 static void
921 pushtag (tree name, tree type)
923 /* Record the identifier as the type's name if it has none. */
924 if (name && !TYPE_NAME (type))
925 TYPE_NAME (type) = name;
926 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
928 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
929 tagged type we just added to the current scope. This fake
930 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
931 to output a representation of a tagged type, and it also gives
932 us a convenient place to record the "scope start" address for the
933 tagged type. */
935 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
937 /* An approximation for now, so we can tell this is a function-scope tag.
938 This will be updated in pop_scope. */
939 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
942 /* Subroutine of compare_decls. Allow harmless mismatches in return
943 and argument types provided that the type modes match. This function
944 return a unified type given a suitable match, and 0 otherwise. */
946 static tree
947 match_builtin_function_types (tree newtype, tree oldtype)
949 tree newrettype, oldrettype;
950 tree newargs, oldargs;
951 tree trytype, tryargs;
953 /* Accept the return type of the new declaration if same modes. */
954 oldrettype = TREE_TYPE (oldtype);
955 newrettype = TREE_TYPE (newtype);
957 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
958 return 0;
960 oldargs = TYPE_ARG_TYPES (oldtype);
961 newargs = TYPE_ARG_TYPES (newtype);
962 tryargs = newargs;
964 while (oldargs || newargs)
966 if (!oldargs
967 || !newargs
968 || !TREE_VALUE (oldargs)
969 || !TREE_VALUE (newargs)
970 || TYPE_MODE (TREE_VALUE (oldargs))
971 != TYPE_MODE (TREE_VALUE (newargs)))
972 return 0;
974 oldargs = TREE_CHAIN (oldargs);
975 newargs = TREE_CHAIN (newargs);
978 trytype = build_function_type (newrettype, tryargs);
979 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
982 /* Subroutine of diagnose_mismatched_decls. Check for function type
983 mismatch involving an empty arglist vs a nonempty one and give clearer
984 diagnostics. */
985 static void
986 diagnose_arglist_conflict (tree newdecl, tree olddecl,
987 tree newtype, tree oldtype)
989 tree t;
991 if (TREE_CODE (olddecl) != FUNCTION_DECL
992 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
993 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
995 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
996 return;
998 t = TYPE_ARG_TYPES (oldtype);
999 if (t == 0)
1000 t = TYPE_ARG_TYPES (newtype);
1001 for (; t; t = TREE_CHAIN (t))
1003 tree type = TREE_VALUE (t);
1005 if (TREE_CHAIN (t) == 0
1006 && TYPE_MAIN_VARIANT (type) != void_type_node)
1008 inform ("a parameter list with an ellipsis can%'t match "
1009 "an empty parameter name list declaration");
1010 break;
1013 if (c_type_promotes_to (type) != type)
1015 inform ("an argument type that has a default promotion can%'t match "
1016 "an empty parameter name list declaration");
1017 break;
1022 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1023 old-style function definition, NEWDECL is a prototype declaration.
1024 Diagnose inconsistencies in the argument list. Returns TRUE if
1025 the prototype is compatible, FALSE if not. */
1026 static bool
1027 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1029 tree newargs, oldargs;
1030 int i;
1032 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1033 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1035 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1036 newargs = TYPE_ARG_TYPES (newtype);
1037 i = 1;
1039 for (;;)
1041 tree oldargtype = TREE_VALUE (oldargs);
1042 tree newargtype = TREE_VALUE (newargs);
1044 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1045 break;
1047 /* Reaching the end of just one list means the two decls don't
1048 agree on the number of arguments. */
1049 if (END_OF_ARGLIST (oldargtype))
1051 error ("%Jprototype for %qD declares more arguments "
1052 "than previous old-style definition", newdecl, newdecl);
1053 return false;
1055 else if (END_OF_ARGLIST (newargtype))
1057 error ("%Jprototype for %qD declares fewer arguments "
1058 "than previous old-style definition", newdecl, newdecl);
1059 return false;
1062 /* Type for passing arg must be consistent with that declared
1063 for the arg. */
1064 else if (!comptypes (oldargtype, newargtype))
1066 error ("%Jprototype for %qD declares argument %d"
1067 " with incompatible type",
1068 newdecl, newdecl, i);
1069 return false;
1072 oldargs = TREE_CHAIN (oldargs);
1073 newargs = TREE_CHAIN (newargs);
1074 i++;
1077 /* If we get here, no errors were found, but do issue a warning
1078 for this poor-style construct. */
1079 warning ("%Jprototype for %qD follows non-prototype definition",
1080 newdecl, newdecl);
1081 return true;
1082 #undef END_OF_ARGLIST
1085 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1086 first in a pair of mismatched declarations, using the diagnostic
1087 function DIAG. */
1088 static void
1089 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1091 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1093 else if (DECL_INITIAL (decl))
1094 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1095 else if (C_DECL_IMPLICIT (decl))
1096 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1097 else
1098 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1101 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1102 Returns true if the caller should proceed to merge the two, false
1103 if OLDDECL should simply be discarded. As a side effect, issues
1104 all necessary diagnostics for invalid or poor-style combinations.
1105 If it returns true, writes the types of NEWDECL and OLDDECL to
1106 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1107 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1109 static bool
1110 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1111 tree *newtypep, tree *oldtypep)
1113 tree newtype, oldtype;
1114 bool pedwarned = false;
1115 bool warned = false;
1117 /* If we have error_mark_node for either decl or type, just discard
1118 the previous decl - we're in an error cascade already. */
1119 if (olddecl == error_mark_node || newdecl == error_mark_node)
1120 return false;
1121 *oldtypep = oldtype = TREE_TYPE (olddecl);
1122 *newtypep = newtype = TREE_TYPE (newdecl);
1123 if (oldtype == error_mark_node || newtype == error_mark_node)
1124 return false;
1126 /* Two different categories of symbol altogether. This is an error
1127 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1128 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1130 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1131 && DECL_BUILT_IN (olddecl)
1132 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1134 error ("%J%qD redeclared as different kind of symbol",
1135 newdecl, newdecl);
1136 locate_old_decl (olddecl, error);
1138 else if (TREE_PUBLIC (newdecl))
1139 warning ("%Jbuilt-in function %qD declared as non-function",
1140 newdecl, newdecl);
1141 else if (warn_shadow)
1142 warning ("%Jdeclaration of %qD shadows a built-in function",
1143 newdecl, newdecl);
1144 return false;
1147 /* Enumerators have no linkage, so may only be declared once in a
1148 given scope. */
1149 if (TREE_CODE (olddecl) == CONST_DECL)
1151 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1152 locate_old_decl (olddecl, error);
1153 return false;
1156 if (!comptypes (oldtype, newtype))
1158 if (TREE_CODE (olddecl) == FUNCTION_DECL
1159 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1161 /* Accept harmless mismatch in function types.
1162 This is for the ffs and fprintf builtins. */
1163 tree trytype = match_builtin_function_types (newtype, oldtype);
1165 if (trytype && comptypes (newtype, trytype))
1166 *oldtypep = oldtype = trytype;
1167 else
1169 /* If types don't match for a built-in, throw away the
1170 built-in. No point in calling locate_old_decl here, it
1171 won't print anything. */
1172 warning ("%Jconflicting types for built-in function %qD",
1173 newdecl, newdecl);
1174 return false;
1177 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1178 && DECL_IS_BUILTIN (olddecl))
1180 /* A conflicting function declaration for a predeclared
1181 function that isn't actually built in. Objective C uses
1182 these. The new declaration silently overrides everything
1183 but the volatility (i.e. noreturn) indication. See also
1184 below. FIXME: Make Objective C use normal builtins. */
1185 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1186 return false;
1188 /* Permit void foo (...) to match int foo (...) if the latter is
1189 the definition and implicit int was used. See
1190 c-torture/compile/920625-2.c. */
1191 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1192 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1193 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1194 && C_FUNCTION_IMPLICIT_INT (newdecl))
1196 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1197 /* Make sure we keep void as the return type. */
1198 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1199 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1200 pedwarned = true;
1202 else
1204 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1205 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1206 else
1207 error ("%Jconflicting types for %qD", newdecl, newdecl);
1208 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1209 locate_old_decl (olddecl, error);
1210 return false;
1214 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1215 but silently ignore the redeclaration if either is in a system
1216 header. (Conflicting redeclarations were handled above.) */
1217 if (TREE_CODE (newdecl) == TYPE_DECL)
1219 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1220 return true; /* Allow OLDDECL to continue in use. */
1222 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1223 locate_old_decl (olddecl, error);
1224 return false;
1227 /* Function declarations can either be 'static' or 'extern' (no
1228 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1229 can never conflict with each other on account of linkage (6.2.2p4).
1230 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1231 two definitions if one is 'extern inline' and one is not. The non-
1232 extern-inline definition supersedes the extern-inline definition. */
1233 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1235 /* If you declare a built-in function name as static, or
1236 define the built-in with an old-style definition (so we
1237 can't validate the argument list) the built-in definition is
1238 overridden, but optionally warn this was a bad choice of name. */
1239 if (DECL_BUILT_IN (olddecl)
1240 && !C_DECL_DECLARED_BUILTIN (olddecl)
1241 && (!TREE_PUBLIC (newdecl)
1242 || (DECL_INITIAL (newdecl)
1243 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1245 if (warn_shadow)
1246 warning ("%Jdeclaration of %qD shadows a built-in function",
1247 newdecl, newdecl);
1248 /* Discard the old built-in function. */
1249 return false;
1252 if (DECL_INITIAL (newdecl))
1254 if (DECL_INITIAL (olddecl)
1255 && !(DECL_DECLARED_INLINE_P (olddecl)
1256 && DECL_EXTERNAL (olddecl)
1257 && !(DECL_DECLARED_INLINE_P (newdecl)
1258 && DECL_EXTERNAL (newdecl)
1259 && same_translation_unit_p (olddecl, newdecl))))
1261 error ("%Jredefinition of %qD", newdecl, newdecl);
1262 locate_old_decl (olddecl, error);
1263 return false;
1266 /* If we have a prototype after an old-style function definition,
1267 the argument types must be checked specially. */
1268 else if (DECL_INITIAL (olddecl)
1269 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1270 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1271 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1273 locate_old_decl (olddecl, error);
1274 return false;
1276 /* A non-static declaration (even an "extern") followed by a
1277 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1278 The same is true for a static forward declaration at block
1279 scope followed by a non-static declaration/definition at file
1280 scope. Static followed by non-static at the same scope is
1281 not undefined behavior, and is the most convenient way to get
1282 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1283 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1284 we do diagnose it if -Wtraditional. */
1285 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1287 /* Two exceptions to the rule. If olddecl is an extern
1288 inline, or a predeclared function that isn't actually
1289 built in, newdecl silently overrides olddecl. The latter
1290 occur only in Objective C; see also above. (FIXME: Make
1291 Objective C use normal builtins.) */
1292 if (!DECL_IS_BUILTIN (olddecl)
1293 && !(DECL_EXTERNAL (olddecl)
1294 && DECL_DECLARED_INLINE_P (olddecl)))
1296 error ("%Jstatic declaration of %qD follows "
1297 "non-static declaration", newdecl, newdecl);
1298 locate_old_decl (olddecl, error);
1300 return false;
1302 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1304 if (DECL_CONTEXT (olddecl))
1306 error ("%Jnon-static declaration of %qD follows "
1307 "static declaration", newdecl, newdecl);
1308 locate_old_decl (olddecl, error);
1309 return false;
1311 else if (warn_traditional)
1313 warning ("%Jnon-static declaration of %qD follows "
1314 "static declaration", newdecl, newdecl);
1315 warned = true;
1319 else if (TREE_CODE (newdecl) == VAR_DECL)
1321 /* Only variables can be thread-local, and all declarations must
1322 agree on this property. */
1323 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1325 if (DECL_THREAD_LOCAL (newdecl))
1326 error ("%Jthread-local declaration of %qD follows "
1327 "non-thread-local declaration", newdecl, newdecl);
1328 else
1329 error ("%Jnon-thread-local declaration of %qD follows "
1330 "thread-local declaration", newdecl, newdecl);
1332 locate_old_decl (olddecl, error);
1333 return false;
1336 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1337 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1339 error ("%Jredefinition of %qD", newdecl, newdecl);
1340 locate_old_decl (olddecl, error);
1341 return false;
1344 /* Objects declared at file scope: if the first declaration had
1345 external linkage (even if it was an external reference) the
1346 second must have external linkage as well, or the behavior is
1347 undefined. If the first declaration had internal linkage, then
1348 the second must too, or else be an external reference (in which
1349 case the composite declaration still has internal linkage).
1350 As for function declarations, we warn about the static-then-
1351 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1352 if (DECL_FILE_SCOPE_P (newdecl)
1353 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1355 if (DECL_EXTERNAL (newdecl))
1357 if (!DECL_FILE_SCOPE_P (olddecl))
1359 error ("%Jextern declaration of %qD follows "
1360 "declaration with no linkage", newdecl, newdecl);
1361 locate_old_decl (olddecl, error);
1362 return false;
1364 else if (warn_traditional)
1366 warning ("%Jnon-static declaration of %qD follows "
1367 "static declaration", newdecl, newdecl);
1368 warned = true;
1371 else
1373 if (TREE_PUBLIC (newdecl))
1374 error ("%Jnon-static declaration of %qD follows "
1375 "static declaration", newdecl, newdecl);
1376 else
1377 error ("%Jstatic declaration of %qD follows "
1378 "non-static declaration", newdecl, newdecl);
1380 locate_old_decl (olddecl, error);
1381 return false;
1384 /* Two objects with the same name declared at the same block
1385 scope must both be external references (6.7p3). */
1386 else if (!DECL_FILE_SCOPE_P (newdecl))
1388 if (DECL_EXTERNAL (newdecl))
1390 /* Extern with initializer at block scope, which will
1391 already have received an error. */
1393 else if (DECL_EXTERNAL (olddecl))
1395 error ("%Jdeclaration of %qD with no linkage follows "
1396 "extern declaration", newdecl, newdecl);
1397 locate_old_decl (olddecl, error);
1399 else
1401 error ("%Jredeclaration of %qD with no linkage",
1402 newdecl, newdecl);
1403 locate_old_decl (olddecl, error);
1406 return false;
1410 /* warnings */
1411 /* All decls must agree on a visibility. */
1412 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1413 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1415 warning ("%Jredeclaration of %qD with different visibility "
1416 "(old visibility preserved)", newdecl, newdecl);
1417 warned = true;
1420 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1422 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1423 if (DECL_DECLARED_INLINE_P (newdecl)
1424 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1426 warning ("%Jinline declaration of %qD follows "
1427 "declaration with attribute noinline", newdecl, newdecl);
1428 warned = true;
1430 else if (DECL_DECLARED_INLINE_P (olddecl)
1431 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1433 warning ("%Jdeclaration of %qD with attribute noinline follows "
1434 "inline declaration ", newdecl, newdecl);
1435 warned = true;
1438 /* Inline declaration after use or definition.
1439 ??? Should we still warn about this now we have unit-at-a-time
1440 mode and can get it right?
1441 Definitely don't complain if the decls are in different translation
1442 units. */
1443 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1444 && same_translation_unit_p (olddecl, newdecl))
1446 if (TREE_USED (olddecl))
1448 warning ("%J%qD declared inline after being called",
1449 olddecl, olddecl);
1450 warned = true;
1452 else if (DECL_INITIAL (olddecl))
1454 warning ("%J%qD declared inline after its definition",
1455 olddecl, olddecl);
1456 warned = true;
1460 else /* PARM_DECL, VAR_DECL */
1462 /* Redeclaration of a parameter is a constraint violation (this is
1463 not explicitly stated, but follows from C99 6.7p3 [no more than
1464 one declaration of the same identifier with no linkage in the
1465 same scope, except type tags] and 6.2.2p6 [parameters have no
1466 linkage]). We must check for a forward parameter declaration,
1467 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1468 an extension, the mandatory diagnostic for which is handled by
1469 mark_forward_parm_decls. */
1471 if (TREE_CODE (newdecl) == PARM_DECL
1472 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1474 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1475 locate_old_decl (olddecl, error);
1476 return false;
1480 /* Optional warning for completely redundant decls. */
1481 if (!warned && !pedwarned
1482 && warn_redundant_decls
1483 /* Don't warn about a function declaration followed by a
1484 definition. */
1485 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1486 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1487 /* Don't warn about redundant redeclarations of builtins. */
1488 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1489 && !DECL_BUILT_IN (newdecl)
1490 && DECL_BUILT_IN (olddecl)
1491 && !C_DECL_DECLARED_BUILTIN (olddecl))
1492 /* Don't warn about an extern followed by a definition. */
1493 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1494 /* Don't warn about forward parameter decls. */
1495 && !(TREE_CODE (newdecl) == PARM_DECL
1496 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1498 warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
1499 warned = true;
1502 /* Report location of previous decl/defn in a consistent manner. */
1503 if (warned || pedwarned)
1504 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1506 return true;
1509 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1510 consistent with OLDDECL, but carries new information. Merge the
1511 new information into OLDDECL. This function issues no
1512 diagnostics. */
1514 static void
1515 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1517 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1518 && DECL_INITIAL (newdecl) != 0);
1520 /* For real parm decl following a forward decl, rechain the old decl
1521 in its new location and clear TREE_ASM_WRITTEN (it's not a
1522 forward decl anymore). */
1523 if (TREE_CODE (newdecl) == PARM_DECL
1524 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1526 struct c_binding *b, **here;
1528 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1529 if ((*here)->decl == olddecl)
1530 goto found;
1531 gcc_unreachable ();
1533 found:
1534 b = *here;
1535 *here = b->prev;
1536 b->prev = current_scope->bindings;
1537 current_scope->bindings = b;
1539 TREE_ASM_WRITTEN (olddecl) = 0;
1542 DECL_ATTRIBUTES (newdecl)
1543 = targetm.merge_decl_attributes (olddecl, newdecl);
1545 /* Merge the data types specified in the two decls. */
1546 TREE_TYPE (newdecl)
1547 = TREE_TYPE (olddecl)
1548 = composite_type (newtype, oldtype);
1550 /* Lay the type out, unless already done. */
1551 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1553 if (TREE_TYPE (newdecl) != error_mark_node)
1554 layout_type (TREE_TYPE (newdecl));
1555 if (TREE_CODE (newdecl) != FUNCTION_DECL
1556 && TREE_CODE (newdecl) != TYPE_DECL
1557 && TREE_CODE (newdecl) != CONST_DECL)
1558 layout_decl (newdecl, 0);
1560 else
1562 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1563 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1564 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1565 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1566 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1567 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1569 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1570 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1574 /* Keep the old rtl since we can safely use it. */
1575 COPY_DECL_RTL (olddecl, newdecl);
1577 /* Merge the type qualifiers. */
1578 if (TREE_READONLY (newdecl))
1579 TREE_READONLY (olddecl) = 1;
1581 if (TREE_THIS_VOLATILE (newdecl))
1583 TREE_THIS_VOLATILE (olddecl) = 1;
1584 if (TREE_CODE (newdecl) == VAR_DECL)
1585 make_var_volatile (newdecl);
1588 /* Merge deprecatedness. */
1589 if (TREE_DEPRECATED (newdecl))
1590 TREE_DEPRECATED (olddecl) = 1;
1592 /* Keep source location of definition rather than declaration. */
1593 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1594 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1596 /* Merge the unused-warning information. */
1597 if (DECL_IN_SYSTEM_HEADER (olddecl))
1598 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1599 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1600 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1602 /* Merge the initialization information. */
1603 if (DECL_INITIAL (newdecl) == 0)
1604 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1606 /* Merge the section attribute.
1607 We want to issue an error if the sections conflict but that must be
1608 done later in decl_attributes since we are called before attributes
1609 are assigned. */
1610 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1611 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1613 /* Copy the assembler name.
1614 Currently, it can only be defined in the prototype. */
1615 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1617 /* Use visibility of whichever declaration had it specified */
1618 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1620 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1621 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1624 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1626 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1627 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1628 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1629 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1630 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1631 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1632 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1633 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1634 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1637 /* Merge the storage class information. */
1638 merge_weak (newdecl, olddecl);
1640 /* For functions, static overrides non-static. */
1641 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1643 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1644 /* This is since we don't automatically
1645 copy the attributes of NEWDECL into OLDDECL. */
1646 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1647 /* If this clears `static', clear it in the identifier too. */
1648 if (!TREE_PUBLIC (olddecl))
1649 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1651 if (DECL_EXTERNAL (newdecl))
1653 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1654 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1656 /* An extern decl does not override previous storage class. */
1657 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1658 if (!DECL_EXTERNAL (newdecl))
1660 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1661 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1664 else
1666 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1667 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1670 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1672 /* If we're redefining a function previously defined as extern
1673 inline, make sure we emit debug info for the inline before we
1674 throw it away, in case it was inlined into a function that hasn't
1675 been written out yet. */
1676 if (new_is_definition && DECL_INITIAL (olddecl))
1678 if (TREE_USED (olddecl)
1679 /* In unit-at-a-time mode we never inline re-defined extern
1680 inline functions. */
1681 && !flag_unit_at_a_time
1682 && cgraph_function_possibly_inlined_p (olddecl))
1683 (*debug_hooks->outlining_inline_function) (olddecl);
1685 /* The new defn must not be inline. */
1686 DECL_INLINE (newdecl) = 0;
1687 DECL_UNINLINABLE (newdecl) = 1;
1689 else
1691 /* If either decl says `inline', this fn is inline,
1692 unless its definition was passed already. */
1693 if (DECL_DECLARED_INLINE_P (newdecl)
1694 || DECL_DECLARED_INLINE_P (olddecl))
1695 DECL_DECLARED_INLINE_P (newdecl) = 1;
1697 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1698 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1701 if (DECL_BUILT_IN (olddecl))
1703 /* If redeclaring a builtin function, it stays built in.
1704 But it gets tagged as having been declared. */
1705 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1706 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1707 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1710 /* Also preserve various other info from the definition. */
1711 if (!new_is_definition)
1713 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1714 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1715 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1716 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1717 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1719 /* Set DECL_INLINE on the declaration if we've got a body
1720 from which to instantiate. */
1721 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1723 DECL_INLINE (newdecl) = 1;
1724 DECL_ABSTRACT_ORIGIN (newdecl)
1725 = DECL_ABSTRACT_ORIGIN (olddecl);
1728 else
1730 /* If a previous declaration said inline, mark the
1731 definition as inlinable. */
1732 if (DECL_DECLARED_INLINE_P (newdecl)
1733 && !DECL_UNINLINABLE (newdecl))
1734 DECL_INLINE (newdecl) = 1;
1738 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1739 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1741 unsigned olddecl_uid = DECL_UID (olddecl);
1742 tree olddecl_context = DECL_CONTEXT (olddecl);
1744 memcpy ((char *) olddecl + sizeof (struct tree_common),
1745 (char *) newdecl + sizeof (struct tree_common),
1746 sizeof (struct tree_decl) - sizeof (struct tree_common));
1747 DECL_UID (olddecl) = olddecl_uid;
1748 DECL_CONTEXT (olddecl) = olddecl_context;
1751 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1752 so that encode_section_info has a chance to look at the new decl
1753 flags and attributes. */
1754 if (DECL_RTL_SET_P (olddecl)
1755 && (TREE_CODE (olddecl) == FUNCTION_DECL
1756 || (TREE_CODE (olddecl) == VAR_DECL
1757 && TREE_STATIC (olddecl))))
1758 make_decl_rtl (olddecl);
1761 /* Handle when a new declaration NEWDECL has the same name as an old
1762 one OLDDECL in the same binding contour. Prints an error message
1763 if appropriate.
1765 If safely possible, alter OLDDECL to look like NEWDECL, and return
1766 true. Otherwise, return false. */
1768 static bool
1769 duplicate_decls (tree newdecl, tree olddecl)
1771 tree newtype = NULL, oldtype = NULL;
1773 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1774 return false;
1776 merge_decls (newdecl, olddecl, newtype, oldtype);
1777 return true;
1781 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1782 static void
1783 warn_if_shadowing (tree new_decl)
1785 struct c_binding *b;
1787 /* Shadow warnings wanted? */
1788 if (!warn_shadow
1789 /* No shadow warnings for internally generated vars. */
1790 || DECL_IS_BUILTIN (new_decl)
1791 /* No shadow warnings for vars made for inlining. */
1792 || DECL_FROM_INLINE (new_decl)
1793 /* Don't warn about the parm names in function declarator
1794 within a function declarator. It would be nice to avoid
1795 warning in any function declarator in a declaration, as
1796 opposed to a definition, but there is no way to tell
1797 it's not a definition at this point. */
1798 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1799 return;
1801 /* Is anything being shadowed? Invisible decls do not count. */
1802 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1803 if (b->decl && b->decl != new_decl && !b->invisible)
1805 tree old_decl = b->decl;
1807 if (TREE_CODE (old_decl) == PARM_DECL)
1808 warning ("%Jdeclaration of %qD shadows a parameter",
1809 new_decl, new_decl);
1810 else if (DECL_FILE_SCOPE_P (old_decl))
1811 warning ("%Jdeclaration of %qD shadows a global declaration",
1812 new_decl, new_decl);
1813 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1814 && DECL_BUILT_IN (old_decl))
1815 warning ("%Jdeclaration of %qD shadows a built-in function",
1816 new_decl, new_decl);
1817 else
1818 warning ("%Jdeclaration of %qD shadows a previous local",
1819 new_decl, new_decl);
1821 if (TREE_CODE (old_decl) != FUNCTION_DECL
1822 || !DECL_BUILT_IN (old_decl))
1823 warning ("%Jshadowed declaration is here", old_decl);
1825 break;
1830 /* Subroutine of pushdecl.
1832 X is a TYPE_DECL for a typedef statement. Create a brand new
1833 ..._TYPE node (which will be just a variant of the existing
1834 ..._TYPE node with identical properties) and then install X
1835 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1837 The whole point here is to end up with a situation where each
1838 and every ..._TYPE node the compiler creates will be uniquely
1839 associated with AT MOST one node representing a typedef name.
1840 This way, even though the compiler substitutes corresponding
1841 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1842 early on, later parts of the compiler can always do the reverse
1843 translation and get back the corresponding typedef name. For
1844 example, given:
1846 typedef struct S MY_TYPE;
1847 MY_TYPE object;
1849 Later parts of the compiler might only know that `object' was of
1850 type `struct S' if it were not for code just below. With this
1851 code however, later parts of the compiler see something like:
1853 struct S' == struct S
1854 typedef struct S' MY_TYPE;
1855 struct S' object;
1857 And they can then deduce (from the node for type struct S') that
1858 the original object declaration was:
1860 MY_TYPE object;
1862 Being able to do this is important for proper support of protoize,
1863 and also for generating precise symbolic debugging information
1864 which takes full account of the programmer's (typedef) vocabulary.
1866 Obviously, we don't want to generate a duplicate ..._TYPE node if
1867 the TYPE_DECL node that we are now processing really represents a
1868 standard built-in type.
1870 Since all standard types are effectively declared at line zero
1871 in the source file, we can easily check to see if we are working
1872 on a standard type by checking the current value of lineno. */
1874 static void
1875 clone_underlying_type (tree x)
1877 if (DECL_IS_BUILTIN (x))
1879 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1880 TYPE_NAME (TREE_TYPE (x)) = x;
1882 else if (TREE_TYPE (x) != error_mark_node
1883 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1885 tree tt = TREE_TYPE (x);
1886 DECL_ORIGINAL_TYPE (x) = tt;
1887 tt = build_variant_type_copy (tt);
1888 TYPE_NAME (tt) = x;
1889 TREE_USED (tt) = TREE_USED (x);
1890 TREE_TYPE (x) = tt;
1894 /* Record a decl-node X as belonging to the current lexical scope.
1895 Check for errors (such as an incompatible declaration for the same
1896 name already seen in the same scope).
1898 Returns either X or an old decl for the same name.
1899 If an old decl is returned, it may have been smashed
1900 to agree with what X says. */
1902 tree
1903 pushdecl (tree x)
1905 tree name = DECL_NAME (x);
1906 struct c_scope *scope = current_scope;
1907 struct c_binding *b;
1908 bool nested = false;
1910 /* Functions need the lang_decl data. */
1911 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1912 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1914 /* Must set DECL_CONTEXT for everything not at file scope or
1915 DECL_FILE_SCOPE_P won't work. Local externs don't count
1916 unless they have initializers (which generate code). */
1917 if (current_function_decl
1918 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1919 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1920 DECL_CONTEXT (x) = current_function_decl;
1922 /* Anonymous decls are just inserted in the scope. */
1923 if (!name)
1925 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1926 return x;
1929 /* First, see if there is another declaration with the same name in
1930 the current scope. If there is, duplicate_decls may do all the
1931 work for us. If duplicate_decls returns false, that indicates
1932 two incompatible decls in the same scope; we are to silently
1933 replace the old one (duplicate_decls has issued all appropriate
1934 diagnostics). In particular, we should not consider possible
1935 duplicates in the external scope, or shadowing. */
1936 b = I_SYMBOL_BINDING (name);
1937 if (b && B_IN_SCOPE (b, scope))
1939 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
1940 && COMPLETE_TYPE_P (TREE_TYPE (x)))
1941 b->inner_comp = false;
1942 if (duplicate_decls (x, b->decl))
1943 return b->decl;
1944 else
1945 goto skip_external_and_shadow_checks;
1948 /* All declarations with external linkage, and all external
1949 references, go in the external scope, no matter what scope is
1950 current. However, the binding in that scope is ignored for
1951 purposes of normal name lookup. A separate binding structure is
1952 created in the requested scope; this governs the normal
1953 visibility of the symbol.
1955 The binding in the externals scope is used exclusively for
1956 detecting duplicate declarations of the same object, no matter
1957 what scope they are in; this is what we do here. (C99 6.2.7p2:
1958 All declarations that refer to the same object or function shall
1959 have compatible type; otherwise, the behavior is undefined.) */
1960 if (DECL_EXTERNAL (x) || scope == file_scope)
1962 tree type = TREE_TYPE (x);
1963 tree vistype = 0;
1964 tree visdecl = 0;
1965 bool type_saved = false;
1966 if (b && !B_IN_EXTERNAL_SCOPE (b)
1967 && (TREE_CODE (b->decl) == FUNCTION_DECL
1968 || TREE_CODE (b->decl) == VAR_DECL)
1969 && DECL_FILE_SCOPE_P (b->decl))
1971 visdecl = b->decl;
1972 vistype = TREE_TYPE (visdecl);
1974 if (warn_nested_externs
1975 && scope != file_scope
1976 && !DECL_IN_SYSTEM_HEADER (x))
1977 warning ("nested extern declaration of %qD", x);
1979 while (b && !B_IN_EXTERNAL_SCOPE (b))
1981 /* If this decl might be modified, save its type. This is
1982 done here rather than when the decl is first bound
1983 because the type may change after first binding, through
1984 being completed or through attributes being added. If we
1985 encounter multiple such decls, only the first should have
1986 its type saved; the others will already have had their
1987 proper types saved and the types will not have changed as
1988 their scopes will not have been re-entered. */
1989 if (DECL_FILE_SCOPE_P (b->decl) && !type_saved)
1991 b->type = TREE_TYPE (b->decl);
1992 type_saved = true;
1994 if (B_IN_FILE_SCOPE (b)
1995 && TREE_CODE (b->decl) == VAR_DECL
1996 && TREE_STATIC (b->decl)
1997 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
1998 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
1999 && TREE_CODE (type) == ARRAY_TYPE
2000 && TYPE_DOMAIN (type)
2001 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2002 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2004 /* Array type completed in inner scope, which should be
2005 diagnosed if the completion does not have size 1 and
2006 it does not get completed in the file scope. */
2007 b->inner_comp = true;
2009 b = b->shadowed;
2012 /* If a matching external declaration has been found, set its
2013 type to the composite of all the types of that declaration.
2014 After the consistency checks, it will be reset to the
2015 composite of the visible types only. */
2016 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2017 && b->type)
2018 TREE_TYPE (b->decl) = b->type;
2020 /* The point of the same_translation_unit_p check here is,
2021 we want to detect a duplicate decl for a construct like
2022 foo() { extern bar(); } ... static bar(); but not if
2023 they are in different translation units. In any case,
2024 the static does not go in the externals scope. */
2025 if (b
2026 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2027 && duplicate_decls (x, b->decl))
2029 tree thistype;
2030 thistype = (vistype ? composite_type (vistype, type) : type);
2031 b->type = TREE_TYPE (b->decl);
2032 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2033 thistype
2034 = build_type_attribute_variant (thistype,
2035 TYPE_ATTRIBUTES (b->type));
2036 TREE_TYPE (b->decl) = thistype;
2037 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2038 return b->decl;
2040 else if (TREE_PUBLIC (x))
2042 if (visdecl && !b && duplicate_decls (x, visdecl))
2044 /* An external declaration at block scope referring to a
2045 visible entity with internal linkage. The composite
2046 type will already be correct for this scope, so we
2047 just need to fall through to make the declaration in
2048 this scope. */
2049 nested = true;
2050 x = visdecl;
2052 else
2054 bind (name, x, external_scope, /*invisible=*/true,
2055 /*nested=*/false);
2056 nested = true;
2060 /* Similarly, a declaration of a function with static linkage at
2061 block scope must be checked against any existing declaration
2062 of that function at file scope. */
2063 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
2064 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
2066 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
2067 warning ("nested static declaration of %qD", x);
2069 while (b && !B_IN_FILE_SCOPE (b))
2070 b = b->shadowed;
2072 if (b && same_translation_unit_p (x, b->decl)
2073 && duplicate_decls (x, b->decl))
2075 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2076 return b->decl;
2078 else
2080 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
2081 nested = true;
2085 warn_if_shadowing (x);
2087 skip_external_and_shadow_checks:
2088 if (TREE_CODE (x) == TYPE_DECL)
2089 clone_underlying_type (x);
2091 bind (name, x, scope, /*invisible=*/false, nested);
2093 /* If x's type is incomplete because it's based on a
2094 structure or union which has not yet been fully declared,
2095 attach it to that structure or union type, so we can go
2096 back and complete the variable declaration later, if the
2097 structure or union gets fully declared.
2099 If the input is erroneous, we can have error_mark in the type
2100 slot (e.g. "f(void a, ...)") - that doesn't count as an
2101 incomplete type. */
2102 if (TREE_TYPE (x) != error_mark_node
2103 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2105 tree element = TREE_TYPE (x);
2107 while (TREE_CODE (element) == ARRAY_TYPE)
2108 element = TREE_TYPE (element);
2109 element = TYPE_MAIN_VARIANT (element);
2111 if ((TREE_CODE (element) == RECORD_TYPE
2112 || TREE_CODE (element) == UNION_TYPE)
2113 && (TREE_CODE (x) != TYPE_DECL
2114 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2115 && !COMPLETE_TYPE_P (element))
2116 C_TYPE_INCOMPLETE_VARS (element)
2117 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2119 return x;
2122 /* Record X as belonging to file scope.
2123 This is used only internally by the Objective-C front end,
2124 and is limited to its needs. duplicate_decls is not called;
2125 if there is any preexisting decl for this identifier, it is an ICE. */
2127 tree
2128 pushdecl_top_level (tree x)
2130 tree name;
2131 bool nested = false;
2132 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2134 name = DECL_NAME (x);
2136 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2138 if (TREE_PUBLIC (x))
2140 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2141 nested = true;
2143 if (file_scope)
2144 bind (name, x, file_scope, /*invisible=*/false, nested);
2146 return x;
2149 static void
2150 implicit_decl_warning (tree id, tree olddecl)
2152 void (*diag) (const char *, ...);
2153 switch (mesg_implicit_function_declaration)
2155 case 0: return;
2156 case 1: diag = warning; break;
2157 case 2: diag = error; break;
2158 default: gcc_unreachable ();
2161 diag (N_("implicit declaration of function %qE"), id);
2162 if (olddecl)
2163 locate_old_decl (olddecl, diag);
2166 /* Generate an implicit declaration for identifier FUNCTIONID as a
2167 function of type int (). */
2169 tree
2170 implicitly_declare (tree functionid)
2172 struct c_binding *b;
2173 tree decl = 0;
2174 tree asmspec_tree;
2176 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2178 if (B_IN_SCOPE (b, external_scope))
2180 decl = b->decl;
2181 break;
2185 if (decl)
2187 /* FIXME: Objective-C has weird not-really-builtin functions
2188 which are supposed to be visible automatically. They wind up
2189 in the external scope because they're pushed before the file
2190 scope gets created. Catch this here and rebind them into the
2191 file scope. */
2192 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2194 bind (functionid, decl, file_scope,
2195 /*invisible=*/false, /*nested=*/true);
2196 return decl;
2198 else
2200 tree newtype = default_function_type;
2201 if (b->type)
2202 TREE_TYPE (decl) = b->type;
2203 /* Implicit declaration of a function already declared
2204 (somehow) in a different scope, or as a built-in.
2205 If this is the first time this has happened, warn;
2206 then recycle the old declaration but with the new type. */
2207 if (!C_DECL_IMPLICIT (decl))
2209 implicit_decl_warning (functionid, decl);
2210 C_DECL_IMPLICIT (decl) = 1;
2212 if (DECL_BUILT_IN (decl))
2214 newtype = build_type_attribute_variant (newtype,
2215 TYPE_ATTRIBUTES
2216 (TREE_TYPE (decl)));
2217 if (!comptypes (newtype, TREE_TYPE (decl)))
2219 warning ("incompatible implicit declaration of built-in"
2220 " function %qD", decl);
2221 newtype = TREE_TYPE (decl);
2224 else
2226 if (!comptypes (newtype, TREE_TYPE (decl)))
2228 error ("incompatible implicit declaration of function %qD",
2229 decl);
2230 locate_old_decl (decl, error);
2233 b->type = TREE_TYPE (decl);
2234 TREE_TYPE (decl) = newtype;
2235 bind (functionid, decl, current_scope,
2236 /*invisible=*/false, /*nested=*/true);
2237 return decl;
2241 /* Not seen before. */
2242 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2243 DECL_EXTERNAL (decl) = 1;
2244 TREE_PUBLIC (decl) = 1;
2245 C_DECL_IMPLICIT (decl) = 1;
2246 implicit_decl_warning (functionid, 0);
2247 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2248 if (asmspec_tree)
2249 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2251 /* C89 says implicit declarations are in the innermost block.
2252 So we record the decl in the standard fashion. */
2253 decl = pushdecl (decl);
2255 /* No need to call objc_check_decl here - it's a function type. */
2256 rest_of_decl_compilation (decl, 0, 0);
2258 /* Write a record describing this implicit function declaration
2259 to the prototypes file (if requested). */
2260 gen_aux_info_record (decl, 0, 1, 0);
2262 /* Possibly apply some default attributes to this implicit declaration. */
2263 decl_attributes (&decl, NULL_TREE, 0);
2265 return decl;
2268 /* Issue an error message for a reference to an undeclared variable
2269 ID, including a reference to a builtin outside of function-call
2270 context. Establish a binding of the identifier to error_mark_node
2271 in an appropriate scope, which will suppress further errors for the
2272 same identifier. */
2273 void
2274 undeclared_variable (tree id)
2276 static bool already = false;
2277 struct c_scope *scope;
2279 if (current_function_decl == 0)
2281 error ("%qE undeclared here (not in a function)", id);
2282 scope = current_scope;
2284 else
2286 error ("%qE undeclared (first use in this function)", id);
2288 if (!already)
2290 error ("(Each undeclared identifier is reported only once");
2291 error ("for each function it appears in.)");
2292 already = true;
2295 /* If we are parsing old-style parameter decls, current_function_decl
2296 will be nonnull but current_function_scope will be null. */
2297 scope = current_function_scope ? current_function_scope : current_scope;
2299 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2302 /* Subroutine of lookup_label, declare_label, define_label: construct a
2303 LABEL_DECL with all the proper frills. */
2305 static tree
2306 make_label (tree name, location_t location)
2308 tree label = build_decl (LABEL_DECL, name, void_type_node);
2310 DECL_CONTEXT (label) = current_function_decl;
2311 DECL_MODE (label) = VOIDmode;
2312 DECL_SOURCE_LOCATION (label) = location;
2314 return label;
2317 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2318 Create one if none exists so far for the current function.
2319 This is called when a label is used in a goto expression or
2320 has its address taken. */
2322 tree
2323 lookup_label (tree name)
2325 tree label;
2327 if (current_function_decl == 0)
2329 error ("label %qs referenced outside of any function",
2330 IDENTIFIER_POINTER (name));
2331 return 0;
2334 /* Use a label already defined or ref'd with this name, but not if
2335 it is inherited from a containing function and wasn't declared
2336 using __label__. */
2337 label = I_LABEL_DECL (name);
2338 if (label && (DECL_CONTEXT (label) == current_function_decl
2339 || C_DECLARED_LABEL_FLAG (label)))
2341 /* If the label has only been declared, update its apparent
2342 location to point here, for better diagnostics if it
2343 turns out not to have been defined. */
2344 if (!TREE_USED (label))
2345 DECL_SOURCE_LOCATION (label) = input_location;
2346 return label;
2349 /* No label binding for that identifier; make one. */
2350 label = make_label (name, input_location);
2352 /* Ordinary labels go in the current function scope. */
2353 bind (name, label, current_function_scope,
2354 /*invisible=*/false, /*nested=*/false);
2355 return label;
2358 /* Make a label named NAME in the current function, shadowing silently
2359 any that may be inherited from containing functions or containing
2360 scopes. This is called for __label__ declarations. */
2362 tree
2363 declare_label (tree name)
2365 struct c_binding *b = I_LABEL_BINDING (name);
2366 tree label;
2368 /* Check to make sure that the label hasn't already been declared
2369 at this scope */
2370 if (b && B_IN_CURRENT_SCOPE (b))
2372 error ("duplicate label declaration %qs", IDENTIFIER_POINTER (name));
2373 locate_old_decl (b->decl, error);
2375 /* Just use the previous declaration. */
2376 return b->decl;
2379 label = make_label (name, input_location);
2380 C_DECLARED_LABEL_FLAG (label) = 1;
2382 /* Declared labels go in the current scope. */
2383 bind (name, label, current_scope,
2384 /*invisible=*/false, /*nested=*/false);
2385 return label;
2388 /* Define a label, specifying the location in the source file.
2389 Return the LABEL_DECL node for the label, if the definition is valid.
2390 Otherwise return 0. */
2392 tree
2393 define_label (location_t location, tree name)
2395 /* Find any preexisting label with this name. It is an error
2396 if that label has already been defined in this function, or
2397 if there is a containing function with a declared label with
2398 the same name. */
2399 tree label = I_LABEL_DECL (name);
2401 if (label
2402 && ((DECL_CONTEXT (label) == current_function_decl
2403 && DECL_INITIAL (label) != 0)
2404 || (DECL_CONTEXT (label) != current_function_decl
2405 && C_DECLARED_LABEL_FLAG (label))))
2407 error ("%Hduplicate label %qD", &location, label);
2408 locate_old_decl (label, error);
2409 return 0;
2411 else if (label && DECL_CONTEXT (label) == current_function_decl)
2413 /* The label has been used or declared already in this function,
2414 but not defined. Update its location to point to this
2415 definition. */
2416 DECL_SOURCE_LOCATION (label) = location;
2418 else
2420 /* No label binding for that identifier; make one. */
2421 label = make_label (name, location);
2423 /* Ordinary labels go in the current function scope. */
2424 bind (name, label, current_function_scope,
2425 /*invisible=*/false, /*nested=*/false);
2428 if (warn_traditional && !in_system_header && lookup_name (name))
2429 warning ("%Htraditional C lacks a separate namespace for labels, "
2430 "identifier %qs conflicts", &location,
2431 IDENTIFIER_POINTER (name));
2433 /* Mark label as having been defined. */
2434 DECL_INITIAL (label) = error_mark_node;
2435 return label;
2438 /* Given NAME, an IDENTIFIER_NODE,
2439 return the structure (or union or enum) definition for that name.
2440 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2441 CODE says which kind of type the caller wants;
2442 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2443 If the wrong kind of type is found, an error is reported. */
2445 static tree
2446 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2448 struct c_binding *b = I_TAG_BINDING (name);
2449 int thislevel = 0;
2451 if (!b || !b->decl)
2452 return 0;
2454 /* We only care about whether it's in this level if
2455 thislevel_only was set or it might be a type clash. */
2456 if (thislevel_only || TREE_CODE (b->decl) != code)
2458 /* For our purposes, a tag in the external scope is the same as
2459 a tag in the file scope. (Primarily relevant to Objective-C
2460 and its builtin structure tags, which get pushed before the
2461 file scope is created.) */
2462 if (B_IN_CURRENT_SCOPE (b)
2463 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2464 thislevel = 1;
2467 if (thislevel_only && !thislevel)
2468 return 0;
2470 if (TREE_CODE (b->decl) != code)
2472 /* Definition isn't the kind we were looking for. */
2473 pending_invalid_xref = name;
2474 pending_invalid_xref_location = input_location;
2476 /* If in the same binding level as a declaration as a tag
2477 of a different type, this must not be allowed to
2478 shadow that tag, so give the error immediately.
2479 (For example, "struct foo; union foo;" is invalid.) */
2480 if (thislevel)
2481 pending_xref_error ();
2483 return b->decl;
2486 /* Print an error message now
2487 for a recent invalid struct, union or enum cross reference.
2488 We don't print them immediately because they are not invalid
2489 when used in the `struct foo;' construct for shadowing. */
2491 void
2492 pending_xref_error (void)
2494 if (pending_invalid_xref != 0)
2495 error ("%H%qs defined as wrong kind of tag",
2496 &pending_invalid_xref_location,
2497 IDENTIFIER_POINTER (pending_invalid_xref));
2498 pending_invalid_xref = 0;
2502 /* Look up NAME in the current scope and its superiors
2503 in the namespace of variables, functions and typedefs.
2504 Return a ..._DECL node of some kind representing its definition,
2505 or return 0 if it is undefined. */
2507 tree
2508 lookup_name (tree name)
2510 struct c_binding *b = I_SYMBOL_BINDING (name);
2511 if (b && !b->invisible)
2512 return b->decl;
2513 return 0;
2516 /* Similar to `lookup_name' but look only at the indicated scope. */
2518 static tree
2519 lookup_name_in_scope (tree name, struct c_scope *scope)
2521 struct c_binding *b;
2523 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2524 if (B_IN_SCOPE (b, scope))
2525 return b->decl;
2526 return 0;
2529 /* Create the predefined scalar types of C,
2530 and some nodes representing standard constants (0, 1, (void *) 0).
2531 Initialize the global scope.
2532 Make definitions for built-in primitive functions. */
2534 void
2535 c_init_decl_processing (void)
2537 tree endlink;
2538 tree ptr_ftype_void, ptr_ftype_ptr;
2539 location_t save_loc = input_location;
2541 /* Adds some ggc roots, and reserved words for c-parse.in. */
2542 c_parse_init ();
2544 current_function_decl = 0;
2546 gcc_obstack_init (&parser_obstack);
2548 /* Make the externals scope. */
2549 push_scope ();
2550 external_scope = current_scope;
2552 /* Declarations from c_common_nodes_and_builtins must not be associated
2553 with this input file, lest we get differences between using and not
2554 using preprocessed headers. */
2555 #ifdef USE_MAPPED_LOCATION
2556 input_location = BUILTINS_LOCATION;
2557 #else
2558 input_location.file = "<built-in>";
2559 input_location.line = 0;
2560 #endif
2562 build_common_tree_nodes (flag_signed_char, false);
2564 c_common_nodes_and_builtins ();
2566 /* In C, comparisons and TRUTH_* expressions have type int. */
2567 truthvalue_type_node = integer_type_node;
2568 truthvalue_true_node = integer_one_node;
2569 truthvalue_false_node = integer_zero_node;
2571 /* Even in C99, which has a real boolean type. */
2572 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2573 boolean_type_node));
2575 endlink = void_list_node;
2576 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2577 ptr_ftype_ptr
2578 = build_function_type (ptr_type_node,
2579 tree_cons (NULL_TREE, ptr_type_node, endlink));
2581 input_location = save_loc;
2583 pedantic_lvalues = true;
2585 make_fname_decl = c_make_fname_decl;
2586 start_fname_decls ();
2589 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2590 decl, NAME is the initialization string and TYPE_DEP indicates whether
2591 NAME depended on the type of the function. As we don't yet implement
2592 delayed emission of static data, we mark the decl as emitted
2593 so it is not placed in the output. Anything using it must therefore pull
2594 out the STRING_CST initializer directly. FIXME. */
2596 static tree
2597 c_make_fname_decl (tree id, int type_dep)
2599 const char *name = fname_as_string (type_dep);
2600 tree decl, type, init;
2601 size_t length = strlen (name);
2603 type = build_array_type
2604 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2605 build_index_type (size_int (length)));
2607 decl = build_decl (VAR_DECL, id, type);
2609 TREE_STATIC (decl) = 1;
2610 TREE_READONLY (decl) = 1;
2611 DECL_ARTIFICIAL (decl) = 1;
2613 init = build_string (length + 1, name);
2614 free ((char *) name);
2615 TREE_TYPE (init) = type;
2616 DECL_INITIAL (decl) = init;
2618 TREE_USED (decl) = 1;
2620 if (current_function_decl)
2622 DECL_CONTEXT (decl) = current_function_decl;
2623 bind (id, decl, current_function_scope,
2624 /*invisible=*/false, /*nested=*/false);
2627 finish_decl (decl, init, NULL_TREE);
2629 return decl;
2632 /* Return a definition for a builtin function named NAME and whose data type
2633 is TYPE. TYPE should be a function type with argument types.
2634 FUNCTION_CODE tells later passes how to compile calls to this function.
2635 See tree.h for its possible values.
2637 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2638 the name to be called if we can't opencode the function. If
2639 ATTRS is nonzero, use that for the function's attribute list. */
2641 tree
2642 builtin_function (const char *name, tree type, int function_code,
2643 enum built_in_class cl, const char *library_name,
2644 tree attrs)
2646 tree id = get_identifier (name);
2647 tree decl = build_decl (FUNCTION_DECL, id, type);
2648 TREE_PUBLIC (decl) = 1;
2649 DECL_EXTERNAL (decl) = 1;
2650 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2651 DECL_BUILT_IN_CLASS (decl) = cl;
2652 DECL_FUNCTION_CODE (decl) = function_code;
2653 if (library_name)
2654 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2656 /* Should never be called on a symbol with a preexisting meaning. */
2657 gcc_assert (!I_SYMBOL_BINDING (id));
2659 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2661 /* Builtins in the implementation namespace are made visible without
2662 needing to be explicitly declared. See push_file_scope. */
2663 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2665 TREE_CHAIN (decl) = visible_builtins;
2666 visible_builtins = decl;
2669 /* Possibly apply some default attributes to this built-in function. */
2670 if (attrs)
2671 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2672 else
2673 decl_attributes (&decl, NULL_TREE, 0);
2675 return decl;
2678 /* Called when a declaration is seen that contains no names to declare.
2679 If its type is a reference to a structure, union or enum inherited
2680 from a containing scope, shadow that tag name for the current scope
2681 with a forward reference.
2682 If its type defines a new named structure or union
2683 or defines an enum, it is valid but we need not do anything here.
2684 Otherwise, it is an error. */
2686 void
2687 shadow_tag (const struct c_declspecs *declspecs)
2689 shadow_tag_warned (declspecs, 0);
2692 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2693 but no pedwarn. */
2694 void
2695 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2697 bool found_tag = false;
2699 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2701 tree value = declspecs->type;
2702 enum tree_code code = TREE_CODE (value);
2704 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2705 /* Used to test also that TYPE_SIZE (value) != 0.
2706 That caused warning for `struct foo;' at top level in the file. */
2708 tree name = TYPE_NAME (value);
2709 tree t;
2711 found_tag = true;
2713 if (name == 0)
2715 if (warned != 1 && code != ENUMERAL_TYPE)
2716 /* Empty unnamed enum OK */
2718 pedwarn ("unnamed struct/union that defines no instances");
2719 warned = 1;
2722 else if (!declspecs->tag_defined_p
2723 && declspecs->storage_class != csc_none)
2725 if (warned != 1)
2726 pedwarn ("empty declaration with storage class specifier "
2727 "does not redeclare tag");
2728 warned = 1;
2729 pending_xref_error ();
2731 else if (!declspecs->tag_defined_p
2732 && (declspecs->const_p
2733 || declspecs->volatile_p
2734 || declspecs->restrict_p))
2736 if (warned != 1)
2737 pedwarn ("empty declaration with type qualifier "
2738 "does not redeclare tag");
2739 warned = 1;
2740 pending_xref_error ();
2742 else
2744 pending_invalid_xref = 0;
2745 t = lookup_tag (code, name, 1);
2747 if (t == 0)
2749 t = make_node (code);
2750 pushtag (name, t);
2754 else
2756 if (warned != 1 && !in_system_header)
2758 pedwarn ("useless type name in empty declaration");
2759 warned = 1;
2763 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2765 pedwarn ("useless type name in empty declaration");
2766 warned = 1;
2769 pending_invalid_xref = 0;
2771 if (declspecs->inline_p)
2773 error ("%<inline%> in empty declaration");
2774 warned = 1;
2777 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2779 error ("%<auto%> in file-scope empty declaration");
2780 warned = 1;
2783 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2785 error ("%<register%> in file-scope empty declaration");
2786 warned = 1;
2789 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2791 warning ("useless storage class specifier in empty declaration");
2792 warned = 2;
2795 if (!warned && !in_system_header && declspecs->thread_p)
2797 warning ("useless %<__thread%> in empty declaration");
2798 warned = 2;
2801 if (!warned && !in_system_header && (declspecs->const_p
2802 || declspecs->volatile_p
2803 || declspecs->restrict_p))
2805 warning ("useless type qualifier in empty declaration");
2806 warned = 2;
2809 if (warned != 1)
2811 if (!found_tag)
2812 pedwarn ("empty declaration");
2817 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2818 bits. SPECS represents declaration specifiers that the grammar
2819 only permits to contain type qualifiers and attributes. */
2822 quals_from_declspecs (const struct c_declspecs *specs)
2824 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2825 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2826 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2827 gcc_assert (!specs->type
2828 && !specs->decl_attr
2829 && specs->typespec_word == cts_none
2830 && specs->storage_class == csc_none
2831 && !specs->typedef_p
2832 && !specs->explicit_signed_p
2833 && !specs->deprecated_p
2834 && !specs->long_p
2835 && !specs->long_long_p
2836 && !specs->short_p
2837 && !specs->signed_p
2838 && !specs->unsigned_p
2839 && !specs->complex_p
2840 && !specs->inline_p
2841 && !specs->thread_p);
2842 return quals;
2845 /* Construct an array declarator. EXPR is the expression inside [], or
2846 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2847 to the pointer to which a parameter array is converted). STATIC_P is
2848 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2849 is true if the array is [*], a VLA of unspecified length which is
2850 nevertheless a complete type (not currently implemented by GCC),
2851 false otherwise. The field for the contained declarator is left to be
2852 filled in by set_array_declarator_inner. */
2854 struct c_declarator *
2855 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2856 bool vla_unspec_p)
2858 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2859 struct c_declarator);
2860 declarator->kind = cdk_array;
2861 declarator->declarator = 0;
2862 declarator->u.array.dimen = expr;
2863 if (quals)
2865 declarator->u.array.attrs = quals->attrs;
2866 declarator->u.array.quals = quals_from_declspecs (quals);
2868 else
2870 declarator->u.array.attrs = NULL_TREE;
2871 declarator->u.array.quals = 0;
2873 declarator->u.array.static_p = static_p;
2874 declarator->u.array.vla_unspec_p = vla_unspec_p;
2875 if (pedantic && !flag_isoc99)
2877 if (static_p || quals != NULL)
2878 pedwarn ("ISO C90 does not support %<static%> or type "
2879 "qualifiers in parameter array declarators");
2880 if (vla_unspec_p)
2881 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2883 if (vla_unspec_p)
2884 warning ("GCC does not yet properly implement %<[*]%> array declarators");
2885 return declarator;
2888 /* Set the contained declarator of an array declarator. DECL is the
2889 declarator, as constructed by build_array_declarator; INNER is what
2890 appears on the left of the []. ABSTRACT_P is true if it is an
2891 abstract declarator, false otherwise; this is used to reject static
2892 and type qualifiers in abstract declarators, where they are not in
2893 the C99 grammar (subject to possible change in DR#289). */
2895 struct c_declarator *
2896 set_array_declarator_inner (struct c_declarator *decl,
2897 struct c_declarator *inner, bool abstract_p)
2899 decl->declarator = inner;
2900 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2901 || decl->u.array.attrs != NULL_TREE
2902 || decl->u.array.static_p))
2903 error ("static or type qualifiers in abstract declarator");
2904 return decl;
2907 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2909 tree
2910 groktypename (struct c_type_name *type_name)
2912 tree type;
2913 tree attrs = type_name->specs->attrs;
2915 type_name->specs->attrs = NULL_TREE;
2917 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2918 false, NULL);
2920 /* Apply attributes. */
2921 decl_attributes (&type, attrs, 0);
2923 return type;
2926 /* Decode a declarator in an ordinary declaration or data definition.
2927 This is called as soon as the type information and variable name
2928 have been parsed, before parsing the initializer if any.
2929 Here we create the ..._DECL node, fill in its type,
2930 and put it on the list of decls for the current context.
2931 The ..._DECL node is returned as the value.
2933 Exception: for arrays where the length is not specified,
2934 the type is left null, to be filled in by `finish_decl'.
2936 Function definitions do not come here; they go to start_function
2937 instead. However, external and forward declarations of functions
2938 do go through here. Structure field declarations are done by
2939 grokfield and not through here. */
2941 tree
2942 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
2943 bool initialized, tree attributes)
2945 tree decl;
2946 tree tem;
2948 /* An object declared as __attribute__((deprecated)) suppresses
2949 warnings of uses of other deprecated items. */
2950 if (lookup_attribute ("deprecated", attributes))
2951 deprecated_state = DEPRECATED_SUPPRESS;
2953 decl = grokdeclarator (declarator, declspecs,
2954 NORMAL, initialized, NULL);
2956 deprecated_state = DEPRECATED_NORMAL;
2958 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2959 && MAIN_NAME_P (DECL_NAME (decl)))
2960 warning ("%J%qD is usually a function", decl, decl);
2962 if (initialized)
2963 /* Is it valid for this decl to have an initializer at all?
2964 If not, set INITIALIZED to zero, which will indirectly
2965 tell 'finish_decl' to ignore the initializer once it is parsed. */
2966 switch (TREE_CODE (decl))
2968 case TYPE_DECL:
2969 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
2970 initialized = 0;
2971 break;
2973 case FUNCTION_DECL:
2974 error ("function %qD is initialized like a variable", decl);
2975 initialized = 0;
2976 break;
2978 case PARM_DECL:
2979 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2980 error ("parameter %qD is initialized", decl);
2981 initialized = 0;
2982 break;
2984 default:
2985 /* Don't allow initializations for incomplete types except for
2986 arrays which might be completed by the initialization. */
2988 /* This can happen if the array size is an undefined macro.
2989 We already gave a warning, so we don't need another one. */
2990 if (TREE_TYPE (decl) == error_mark_node)
2991 initialized = 0;
2992 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2994 /* A complete type is ok if size is fixed. */
2996 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2997 || C_DECL_VARIABLE_SIZE (decl))
2999 error ("variable-sized object may not be initialized");
3000 initialized = 0;
3003 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3005 error ("variable %qD has initializer but incomplete type", decl);
3006 initialized = 0;
3008 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3010 error ("elements of array %qD have incomplete type", decl);
3011 initialized = 0;
3013 else if (C_DECL_VARIABLE_SIZE (decl))
3015 /* Although C99 is unclear about whether incomplete arrays
3016 of VLAs themselves count as VLAs, it does not make
3017 sense to permit them to be initialized given that
3018 ordinary VLAs may not be initialized. */
3019 error ("variable-sized object may not be initialized");
3020 initialized = 0;
3024 if (initialized)
3026 if (current_scope == file_scope)
3027 TREE_STATIC (decl) = 1;
3029 /* Tell 'pushdecl' this is an initialized decl
3030 even though we don't yet have the initializer expression.
3031 Also tell 'finish_decl' it may store the real initializer. */
3032 DECL_INITIAL (decl) = error_mark_node;
3035 /* If this is a function declaration, write a record describing it to the
3036 prototypes file (if requested). */
3038 if (TREE_CODE (decl) == FUNCTION_DECL)
3039 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3041 /* ANSI specifies that a tentative definition which is not merged with
3042 a non-tentative definition behaves exactly like a definition with an
3043 initializer equal to zero. (Section 3.7.2)
3045 -fno-common gives strict ANSI behavior, though this tends to break
3046 a large body of code that grew up without this rule.
3048 Thread-local variables are never common, since there's no entrenched
3049 body of code to break, and it allows more efficient variable references
3050 in the presence of dynamic linking. */
3052 if (TREE_CODE (decl) == VAR_DECL
3053 && !initialized
3054 && TREE_PUBLIC (decl)
3055 && !DECL_THREAD_LOCAL (decl)
3056 && !flag_no_common)
3057 DECL_COMMON (decl) = 1;
3059 /* Set attributes here so if duplicate decl, will have proper attributes. */
3060 decl_attributes (&decl, attributes, 0);
3062 if (TREE_CODE (decl) == FUNCTION_DECL
3063 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3065 struct c_declarator *ce = declarator;
3067 if (ce->kind == cdk_pointer)
3068 ce = declarator->declarator;
3069 if (ce->kind == cdk_function)
3071 tree args = ce->u.arg_info->parms;
3072 for (; args; args = TREE_CHAIN (args))
3074 tree type = TREE_TYPE (args);
3075 if (type && INTEGRAL_TYPE_P (type)
3076 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3077 DECL_ARG_TYPE (args) = integer_type_node;
3082 if (TREE_CODE (decl) == FUNCTION_DECL
3083 && DECL_DECLARED_INLINE_P (decl)
3084 && DECL_UNINLINABLE (decl)
3085 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3086 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3088 /* Add this decl to the current scope.
3089 TEM may equal DECL or it may be a previous decl of the same name. */
3090 tem = pushdecl (decl);
3092 if (initialized && DECL_EXTERNAL (tem))
3094 DECL_EXTERNAL (tem) = 0;
3095 TREE_STATIC (tem) = 1;
3098 return tem;
3101 /* Finish processing of a declaration;
3102 install its initial value.
3103 If the length of an array type is not known before,
3104 it must be determined now, from the initial value, or it is an error. */
3106 void
3107 finish_decl (tree decl, tree init, tree asmspec_tree)
3109 tree type = TREE_TYPE (decl);
3110 int was_incomplete = (DECL_SIZE (decl) == 0);
3111 const char *asmspec = 0;
3113 /* If a name was specified, get the string. */
3114 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3115 && DECL_FILE_SCOPE_P (decl))
3116 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3117 if (asmspec_tree)
3118 asmspec = TREE_STRING_POINTER (asmspec_tree);
3120 /* If `start_decl' didn't like having an initialization, ignore it now. */
3121 if (init != 0 && DECL_INITIAL (decl) == 0)
3122 init = 0;
3124 /* Don't crash if parm is initialized. */
3125 if (TREE_CODE (decl) == PARM_DECL)
3126 init = 0;
3128 if (init)
3129 store_init_value (decl, init);
3131 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3132 || TREE_CODE (decl) == FUNCTION_DECL
3133 || TREE_CODE (decl) == FIELD_DECL))
3134 objc_check_decl (decl);
3136 /* Deduce size of array from initialization, if not already known. */
3137 if (TREE_CODE (type) == ARRAY_TYPE
3138 && TYPE_DOMAIN (type) == 0
3139 && TREE_CODE (decl) != TYPE_DECL)
3141 int do_default
3142 = (TREE_STATIC (decl)
3143 /* Even if pedantic, an external linkage array
3144 may have incomplete type at first. */
3145 ? pedantic && !TREE_PUBLIC (decl)
3146 : !DECL_EXTERNAL (decl));
3147 int failure
3148 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3150 /* Get the completed type made by complete_array_type. */
3151 type = TREE_TYPE (decl);
3153 if (failure == 1)
3154 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3156 else if (failure == 2)
3158 if (do_default)
3159 error ("%Jarray size missing in %qD", decl, decl);
3160 /* If a `static' var's size isn't known,
3161 make it extern as well as static, so it does not get
3162 allocated.
3163 If it is not `static', then do not mark extern;
3164 finish_incomplete_decl will give it a default size
3165 and it will get allocated. */
3166 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3167 DECL_EXTERNAL (decl) = 1;
3170 /* TYPE_MAX_VALUE is always one less than the number of elements
3171 in the array, because we start counting at zero. Therefore,
3172 warn only if the value is less than zero. */
3173 else if (pedantic && TYPE_DOMAIN (type) != 0
3174 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3175 error ("%Jzero or negative size array %qD", decl, decl);
3177 layout_decl (decl, 0);
3180 if (TREE_CODE (decl) == VAR_DECL)
3182 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3183 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3184 layout_decl (decl, 0);
3186 if (DECL_SIZE (decl) == 0
3187 /* Don't give an error if we already gave one earlier. */
3188 && TREE_TYPE (decl) != error_mark_node
3189 && (TREE_STATIC (decl)
3190 /* A static variable with an incomplete type
3191 is an error if it is initialized.
3192 Also if it is not file scope.
3193 Otherwise, let it through, but if it is not `extern'
3194 then it may cause an error message later. */
3195 ? (DECL_INITIAL (decl) != 0
3196 || !DECL_FILE_SCOPE_P (decl))
3197 /* An automatic variable with an incomplete type
3198 is an error. */
3199 : !DECL_EXTERNAL (decl)))
3201 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3202 TREE_TYPE (decl) = error_mark_node;
3205 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3206 && DECL_SIZE (decl) != 0)
3208 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3209 constant_expression_warning (DECL_SIZE (decl));
3210 else
3211 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3214 if (TREE_USED (type))
3215 TREE_USED (decl) = 1;
3218 /* If this is a function and an assembler name is specified, reset DECL_RTL
3219 so we can give it its new name. Also, update built_in_decls if it
3220 was a normal built-in. */
3221 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3223 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3225 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
3226 set_user_assembler_name (builtin, asmspec);
3227 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
3228 init_block_move_fn (asmspec);
3229 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
3230 init_block_clear_fn (asmspec);
3232 set_user_assembler_name (decl, asmspec);
3235 /* If #pragma weak was used, mark the decl weak now. */
3236 if (current_scope == file_scope)
3237 maybe_apply_pragma_weak (decl);
3239 /* If this is a variable definition, determine its ELF visibility. */
3240 if (TREE_CODE (decl) == VAR_DECL
3241 && TREE_STATIC (decl)
3242 && !DECL_EXTERNAL (decl))
3243 c_determine_visibility (decl);
3245 /* Output the assembler code and/or RTL code for variables and functions,
3246 unless the type is an undefined structure or union.
3247 If not, it will get done when the type is completed. */
3249 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3251 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3252 if (c_dialect_objc ())
3253 objc_check_decl (decl);
3255 if (asmspec)
3257 /* If this is not a static variable, issue a warning.
3258 It doesn't make any sense to give an ASMSPEC for an
3259 ordinary, non-register local variable. Historically,
3260 GCC has accepted -- but ignored -- the ASMSPEC in
3261 this case. */
3262 if (!DECL_FILE_SCOPE_P (decl)
3263 && TREE_CODE (decl) == VAR_DECL
3264 && !C_DECL_REGISTER (decl)
3265 && !TREE_STATIC (decl))
3266 warning ("%Jignoring asm-specifier for non-static local "
3267 "variable %qD", decl, decl);
3268 else if (C_DECL_REGISTER (decl))
3269 change_decl_assembler_name (decl, get_identifier (asmspec));
3270 else
3271 set_user_assembler_name (decl, asmspec);
3274 if (DECL_FILE_SCOPE_P (decl))
3276 if (DECL_INITIAL (decl) == NULL_TREE
3277 || DECL_INITIAL (decl) == error_mark_node)
3278 /* Don't output anything
3279 when a tentative file-scope definition is seen.
3280 But at end of compilation, do output code for them. */
3281 DECL_DEFER_OUTPUT (decl) = 1;
3282 rest_of_decl_compilation (decl, true, 0);
3284 else
3286 /* In conjunction with an ASMSPEC, the `register'
3287 keyword indicates that we should place the variable
3288 in a particular register. */
3289 if (asmspec && C_DECL_REGISTER (decl))
3291 DECL_HARD_REGISTER (decl) = 1;
3292 /* This cannot be done for a structure with volatile
3293 fields, on which DECL_REGISTER will have been
3294 reset. */
3295 if (!DECL_REGISTER (decl))
3296 error ("cannot put object with volatile field into register");
3299 if (TREE_CODE (decl) != FUNCTION_DECL)
3301 /* If we're building a variable sized type, and we might be
3302 reachable other than via the top of the current binding
3303 level, then create a new BIND_EXPR so that we deallocate
3304 the object at the right time. */
3305 /* Note that DECL_SIZE can be null due to errors. */
3306 if (DECL_SIZE (decl)
3307 && !TREE_CONSTANT (DECL_SIZE (decl))
3308 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3310 tree bind;
3311 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3312 TREE_SIDE_EFFECTS (bind) = 1;
3313 add_stmt (bind);
3314 BIND_EXPR_BODY (bind) = push_stmt_list ();
3316 add_stmt (build_stmt (DECL_EXPR, decl));
3321 if (!DECL_FILE_SCOPE_P (decl))
3323 /* Recompute the RTL of a local array now
3324 if it used to be an incomplete type. */
3325 if (was_incomplete
3326 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3328 /* If we used it already as memory, it must stay in memory. */
3329 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3330 /* If it's still incomplete now, no init will save it. */
3331 if (DECL_SIZE (decl) == 0)
3332 DECL_INITIAL (decl) = 0;
3337 /* If this was marked 'used', be sure it will be output. */
3338 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3339 mark_decl_referenced (decl);
3341 if (TREE_CODE (decl) == TYPE_DECL)
3343 if (!DECL_FILE_SCOPE_P (decl)
3344 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3345 add_stmt (build_stmt (DECL_EXPR, decl));
3347 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3350 /* At the end of a declaration, throw away any variable type sizes
3351 of types defined inside that declaration. There is no use
3352 computing them in the following function definition. */
3353 if (current_scope == file_scope)
3354 get_pending_sizes ();
3356 /* Install a cleanup (aka destructor) if one was given. */
3357 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3359 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3360 if (attr)
3362 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3363 tree cleanup_decl = lookup_name (cleanup_id);
3364 tree cleanup;
3366 /* Build "cleanup(&decl)" for the destructor. */
3367 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3368 cleanup = build_tree_list (NULL_TREE, cleanup);
3369 cleanup = build_function_call (cleanup_decl, cleanup);
3371 /* Don't warn about decl unused; the cleanup uses it. */
3372 TREE_USED (decl) = 1;
3373 TREE_USED (cleanup_decl) = 1;
3375 /* Initialize EH, if we've been told to do so. */
3376 if (flag_exceptions && !c_eh_initialized_p)
3378 c_eh_initialized_p = true;
3379 eh_personality_libfunc
3380 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3381 ? "__gcc_personality_sj0"
3382 : "__gcc_personality_v0");
3383 using_eh_for_cleanups ();
3386 push_cleanup (decl, cleanup, false);
3391 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3393 tree
3394 grokparm (const struct c_parm *parm)
3396 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3397 NULL);
3399 decl_attributes (&decl, parm->attrs, 0);
3401 return decl;
3404 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3405 and push that on the current scope. */
3407 void
3408 push_parm_decl (const struct c_parm *parm)
3410 tree decl;
3412 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3413 decl_attributes (&decl, parm->attrs, 0);
3415 decl = pushdecl (decl);
3417 finish_decl (decl, NULL_TREE, NULL_TREE);
3420 /* Mark all the parameter declarations to date as forward decls.
3421 Also diagnose use of this extension. */
3423 void
3424 mark_forward_parm_decls (void)
3426 struct c_binding *b;
3428 if (pedantic && !current_scope->warned_forward_parm_decls)
3430 pedwarn ("ISO C forbids forward parameter declarations");
3431 current_scope->warned_forward_parm_decls = true;
3434 for (b = current_scope->bindings; b; b = b->prev)
3435 if (TREE_CODE (b->decl) == PARM_DECL)
3436 TREE_ASM_WRITTEN (b->decl) = 1;
3439 static GTY(()) int compound_literal_number;
3441 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3442 literal, which may be an incomplete array type completed by the
3443 initializer; INIT is a CONSTRUCTOR that initializes the compound
3444 literal. */
3446 tree
3447 build_compound_literal (tree type, tree init)
3449 /* We do not use start_decl here because we have a type, not a declarator;
3450 and do not use finish_decl because the decl should be stored inside
3451 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3452 tree decl;
3453 tree complit;
3454 tree stmt;
3456 if (type == error_mark_node)
3457 return error_mark_node;
3459 decl = build_decl (VAR_DECL, NULL_TREE, type);
3460 DECL_EXTERNAL (decl) = 0;
3461 TREE_PUBLIC (decl) = 0;
3462 TREE_STATIC (decl) = (current_scope == file_scope);
3463 DECL_CONTEXT (decl) = current_function_decl;
3464 TREE_USED (decl) = 1;
3465 TREE_TYPE (decl) = type;
3466 TREE_READONLY (decl) = TYPE_READONLY (type);
3467 store_init_value (decl, init);
3469 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3471 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3473 gcc_assert (!failure);
3476 type = TREE_TYPE (decl);
3477 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3478 return error_mark_node;
3480 stmt = build_stmt (DECL_EXPR, decl);
3481 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3482 TREE_SIDE_EFFECTS (complit) = 1;
3484 layout_decl (decl, 0);
3486 if (TREE_STATIC (decl))
3488 /* This decl needs a name for the assembler output. We also need
3489 a unique suffix to be added to the name. */
3490 char *name;
3492 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3493 compound_literal_number);
3494 compound_literal_number++;
3495 DECL_NAME (decl) = get_identifier (name);
3496 DECL_DEFER_OUTPUT (decl) = 1;
3497 DECL_COMDAT (decl) = 1;
3498 DECL_ARTIFICIAL (decl) = 1;
3499 pushdecl (decl);
3500 rest_of_decl_compilation (decl, 1, 0);
3503 return complit;
3506 /* Make TYPE a complete type based on INITIAL_VALUE.
3507 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3508 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3511 complete_array_type (tree type, tree initial_value, int do_default)
3513 tree maxindex = NULL_TREE;
3514 int value = 0;
3516 if (initial_value)
3518 /* Note MAXINDEX is really the maximum index,
3519 one less than the size. */
3520 if (TREE_CODE (initial_value) == STRING_CST)
3522 int eltsize
3523 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3524 maxindex = build_int_cst (NULL_TREE,
3525 (TREE_STRING_LENGTH (initial_value)
3526 / eltsize) - 1);
3528 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3530 tree elts = CONSTRUCTOR_ELTS (initial_value);
3531 maxindex = build_int_cst (NULL_TREE, -1);
3532 for (; elts; elts = TREE_CHAIN (elts))
3534 if (TREE_PURPOSE (elts))
3535 maxindex = TREE_PURPOSE (elts);
3536 else
3537 maxindex = fold (build2 (PLUS_EXPR, integer_type_node,
3538 maxindex, integer_one_node));
3541 else
3543 /* Make an error message unless that happened already. */
3544 if (initial_value != error_mark_node)
3545 value = 1;
3547 /* Prevent further error messages. */
3548 maxindex = build_int_cst (NULL_TREE, 0);
3552 if (!maxindex)
3554 if (do_default)
3555 maxindex = build_int_cst (NULL_TREE, 0);
3556 value = 2;
3559 if (maxindex)
3561 TYPE_DOMAIN (type) = build_index_type (maxindex);
3563 gcc_assert (TREE_TYPE (maxindex));
3566 /* Lay out the type now that we can get the real answer. */
3568 layout_type (type);
3570 return value;
3573 /* Determine whether TYPE is a structure with a flexible array member,
3574 or a union containing such a structure (possibly recursively). */
3576 static bool
3577 flexible_array_type_p (tree type)
3579 tree x;
3580 switch (TREE_CODE (type))
3582 case RECORD_TYPE:
3583 x = TYPE_FIELDS (type);
3584 if (x == NULL_TREE)
3585 return false;
3586 while (TREE_CHAIN (x) != NULL_TREE)
3587 x = TREE_CHAIN (x);
3588 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3589 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3590 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3591 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3592 return true;
3593 return false;
3594 case UNION_TYPE:
3595 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3597 if (flexible_array_type_p (TREE_TYPE (x)))
3598 return true;
3600 return false;
3601 default:
3602 return false;
3606 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3607 replacing with appropriate values if they are invalid. */
3608 static void
3609 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3611 tree type_mv;
3612 unsigned int max_width;
3613 unsigned HOST_WIDE_INT w;
3614 const char *name = orig_name ? orig_name: _("<anonymous>");
3616 /* Necessary? */
3617 STRIP_NOPS (*width);
3619 /* Detect and ignore out of range field width and process valid
3620 field widths. */
3621 if (TREE_CODE (*width) != INTEGER_CST)
3623 error ("bit-field %qs width not an integer constant", name);
3624 *width = integer_one_node;
3626 else
3628 constant_expression_warning (*width);
3629 if (tree_int_cst_sgn (*width) < 0)
3631 error ("negative width in bit-field %qs", name);
3632 *width = integer_one_node;
3634 else if (integer_zerop (*width) && orig_name)
3636 error ("zero width for bit-field %qs", name);
3637 *width = integer_one_node;
3641 /* Detect invalid bit-field type. */
3642 if (TREE_CODE (*type) != INTEGER_TYPE
3643 && TREE_CODE (*type) != BOOLEAN_TYPE
3644 && TREE_CODE (*type) != ENUMERAL_TYPE)
3646 error ("bit-field %qs has invalid type", name);
3647 *type = unsigned_type_node;
3650 type_mv = TYPE_MAIN_VARIANT (*type);
3651 if (pedantic
3652 && type_mv != integer_type_node
3653 && type_mv != unsigned_type_node
3654 && type_mv != boolean_type_node)
3655 pedwarn ("type of bit-field %qs is a GCC extension", name);
3657 if (type_mv == boolean_type_node)
3658 max_width = CHAR_TYPE_SIZE;
3659 else
3660 max_width = TYPE_PRECISION (*type);
3662 if (0 < compare_tree_int (*width, max_width))
3664 error ("width of %qs exceeds its type", name);
3665 w = max_width;
3666 *width = build_int_cst (NULL_TREE, w);
3668 else
3669 w = tree_low_cst (*width, 1);
3671 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3673 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3674 if (!lt
3675 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3676 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3677 warning ("%qs is narrower than values of its type", name);
3681 /* Given declspecs and a declarator,
3682 determine the name and type of the object declared
3683 and construct a ..._DECL node for it.
3684 (In one case we can return a ..._TYPE node instead.
3685 For invalid input we sometimes return 0.)
3687 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3689 DECL_CONTEXT says which syntactic context this declaration is in:
3690 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3691 FUNCDEF for a function definition. Like NORMAL but a few different
3692 error messages in each case. Return value may be zero meaning
3693 this definition is too screwy to try to parse.
3694 PARM for a parameter declaration (either within a function prototype
3695 or before a function body). Make a PARM_DECL, or return void_type_node.
3696 TYPENAME if for a typename (in a cast or sizeof).
3697 Don't make a DECL node; just return the ..._TYPE node.
3698 FIELD for a struct or union field; make a FIELD_DECL.
3699 INITIALIZED is true if the decl has an initializer.
3700 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3701 representing the width of the bit-field.
3703 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3704 It may also be so in the PARM case, for a prototype where the
3705 argument type is specified but not the name.
3707 This function is where the complicated C meanings of `static'
3708 and `extern' are interpreted. */
3710 static tree
3711 grokdeclarator (const struct c_declarator *declarator,
3712 struct c_declspecs *declspecs,
3713 enum decl_context decl_context, bool initialized, tree *width)
3715 tree type = declspecs->type;
3716 bool threadp = declspecs->thread_p;
3717 enum c_storage_class storage_class = declspecs->storage_class;
3718 int constp;
3719 int restrictp;
3720 int volatilep;
3721 int type_quals = TYPE_UNQUALIFIED;
3722 const char *name, *orig_name;
3723 tree typedef_type = 0;
3724 int funcdef_flag = 0;
3725 bool funcdef_syntax = false;
3726 int size_varies = 0;
3727 tree decl_attr = declspecs->decl_attr;
3728 int array_ptr_quals = TYPE_UNQUALIFIED;
3729 tree array_ptr_attrs = NULL_TREE;
3730 int array_parm_static = 0;
3731 tree returned_attrs = NULL_TREE;
3732 bool bitfield = width != NULL;
3733 tree element_type;
3734 struct c_arg_info *arg_info = 0;
3736 if (decl_context == FUNCDEF)
3737 funcdef_flag = 1, decl_context = NORMAL;
3739 /* Look inside a declarator for the name being declared
3740 and get it as a string, for an error message. */
3742 const struct c_declarator *decl = declarator;
3743 name = 0;
3745 while (decl)
3746 switch (decl->kind)
3748 case cdk_function:
3749 case cdk_array:
3750 case cdk_pointer:
3751 funcdef_syntax = (decl->kind == cdk_function);
3752 decl = decl->declarator;
3753 break;
3755 case cdk_attrs:
3756 decl = decl->declarator;
3757 break;
3759 case cdk_id:
3760 if (decl->u.id)
3761 name = IDENTIFIER_POINTER (decl->u.id);
3762 decl = 0;
3763 break;
3765 default:
3766 gcc_unreachable ();
3768 orig_name = name;
3769 if (name == 0)
3770 name = "type name";
3773 /* A function definition's declarator must have the form of
3774 a function declarator. */
3776 if (funcdef_flag && !funcdef_syntax)
3777 return 0;
3779 /* If this looks like a function definition, make it one,
3780 even if it occurs where parms are expected.
3781 Then store_parm_decls will reject it and not use it as a parm. */
3782 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3783 decl_context = PARM;
3785 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3786 warn_deprecated_use (declspecs->type);
3788 typedef_type = type;
3789 size_varies = C_TYPE_VARIABLE_SIZE (type);
3791 /* Diagnose defaulting to "int". */
3793 if (declspecs->default_int_p && !in_system_header)
3795 /* Issue a warning if this is an ISO C 99 program or if
3796 -Wreturn-type and this is a function, or if -Wimplicit;
3797 prefer the former warning since it is more explicit. */
3798 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3799 && funcdef_flag)
3800 warn_about_return_type = 1;
3801 else if (warn_implicit_int || flag_isoc99)
3802 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3805 /* Adjust the type if a bit-field is being declared,
3806 -funsigned-bitfields applied and the type is not explicitly
3807 "signed". */
3808 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3809 && TREE_CODE (type) == INTEGER_TYPE)
3810 type = c_common_unsigned_type (type);
3812 /* Figure out the type qualifiers for the declaration. There are
3813 two ways a declaration can become qualified. One is something
3814 like `const int i' where the `const' is explicit. Another is
3815 something like `typedef const int CI; CI i' where the type of the
3816 declaration contains the `const'. A third possibility is that
3817 there is a type qualifier on the element type of a typedefed
3818 array type, in which case we should extract that qualifier so
3819 that c_apply_type_quals_to_decls receives the full list of
3820 qualifiers to work with (C90 is not entirely clear about whether
3821 duplicate qualifiers should be diagnosed in this case, but it
3822 seems most appropriate to do so). */
3823 element_type = strip_array_types (type);
3824 constp = declspecs->const_p + TYPE_READONLY (element_type);
3825 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3826 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3827 if (pedantic && !flag_isoc99)
3829 if (constp > 1)
3830 pedwarn ("duplicate %<const%>");
3831 if (restrictp > 1)
3832 pedwarn ("duplicate %<restrict%>");
3833 if (volatilep > 1)
3834 pedwarn ("duplicate %<volatile%>");
3836 if (!flag_gen_aux_info && (TYPE_QUALS (type)))
3837 type = TYPE_MAIN_VARIANT (type);
3838 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3839 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3840 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3842 /* Warn about storage classes that are invalid for certain
3843 kinds of declarations (parameters, typenames, etc.). */
3845 if (funcdef_flag
3846 && (threadp
3847 || storage_class == csc_auto
3848 || storage_class == csc_register
3849 || storage_class == csc_typedef))
3851 if (storage_class == csc_auto
3852 && (pedantic || current_scope == file_scope))
3853 pedwarn ("function definition declared %<auto%>");
3854 if (storage_class == csc_register)
3855 error ("function definition declared %<register%>");
3856 if (storage_class == csc_typedef)
3857 error ("function definition declared %<typedef%>");
3858 if (threadp)
3859 error ("function definition declared %<__thread%>");
3860 threadp = false;
3861 if (storage_class == csc_auto
3862 || storage_class == csc_register
3863 || storage_class == csc_typedef)
3864 storage_class = csc_none;
3866 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3868 if (decl_context == PARM && storage_class == csc_register)
3870 else
3872 switch (decl_context)
3874 case FIELD:
3875 error ("storage class specified for structure field %qs",
3876 name);
3877 break;
3878 case PARM:
3879 error ("storage class specified for parameter %qs", name);
3880 break;
3881 default:
3882 error ("storage class specified for typename");
3883 break;
3885 storage_class = csc_none;
3886 threadp = false;
3889 else if (storage_class == csc_extern
3890 && initialized
3891 && !funcdef_flag)
3893 /* 'extern' with initialization is invalid if not at file scope. */
3894 if (current_scope == file_scope)
3895 warning ("%qs initialized and declared %<extern%>", name);
3896 else
3897 error ("%qs has both %<extern%> and initializer", name);
3899 else if (current_scope == file_scope)
3901 if (storage_class == csc_auto)
3902 error ("file-scope declaration of %qs specifies %<auto%>", name);
3903 if (pedantic && storage_class == csc_register)
3904 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3906 else
3908 if (storage_class == csc_extern && funcdef_flag)
3909 error ("nested function %qs declared %<extern%>", name);
3910 else if (threadp && storage_class == csc_none)
3912 error ("function-scope %qs implicitly auto and declared "
3913 "%<__thread%>",
3914 name);
3915 threadp = false;
3919 /* Now figure out the structure of the declarator proper.
3920 Descend through it, creating more complex types, until we reach
3921 the declared identifier (or NULL_TREE, in an absolute declarator). */
3923 while (declarator && declarator->kind != cdk_id)
3925 if (type == error_mark_node)
3927 declarator = declarator->declarator;
3928 continue;
3931 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3932 a cdk_pointer (for *...),
3933 a cdk_function (for ...(...)),
3934 a cdk_attrs (for nested attributes),
3935 or a cdk_id (for the name being declared
3936 or the place in an absolute declarator
3937 where the name was omitted).
3938 For the last case, we have just exited the loop.
3940 At this point, TYPE is the type of elements of an array,
3941 or for a function to return, or for a pointer to point to.
3942 After this sequence of ifs, TYPE is the type of the
3943 array or function or pointer, and DECLARATOR has had its
3944 outermost layer removed. */
3946 if (array_ptr_quals != TYPE_UNQUALIFIED
3947 || array_ptr_attrs != NULL_TREE
3948 || array_parm_static)
3950 /* Only the innermost declarator (making a parameter be of
3951 array type which is converted to pointer type)
3952 may have static or type qualifiers. */
3953 error ("static or type qualifiers in non-parameter array declarator");
3954 array_ptr_quals = TYPE_UNQUALIFIED;
3955 array_ptr_attrs = NULL_TREE;
3956 array_parm_static = 0;
3959 switch (declarator->kind)
3961 case cdk_attrs:
3963 /* A declarator with embedded attributes. */
3964 tree attrs = declarator->u.attrs;
3965 const struct c_declarator *inner_decl;
3966 int attr_flags = 0;
3967 declarator = declarator->declarator;
3968 inner_decl = declarator;
3969 while (inner_decl->kind == cdk_attrs)
3970 inner_decl = inner_decl->declarator;
3971 if (inner_decl->kind == cdk_id)
3972 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3973 else if (inner_decl->kind == cdk_function)
3974 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3975 else if (inner_decl->kind == cdk_array)
3976 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3977 returned_attrs = decl_attributes (&type,
3978 chainon (returned_attrs, attrs),
3979 attr_flags);
3980 break;
3982 case cdk_array:
3984 tree itype = NULL_TREE;
3985 tree size = declarator->u.array.dimen;
3986 /* The index is a signed object `sizetype' bits wide. */
3987 tree index_type = c_common_signed_type (sizetype);
3989 array_ptr_quals = declarator->u.array.quals;
3990 array_ptr_attrs = declarator->u.array.attrs;
3991 array_parm_static = declarator->u.array.static_p;
3993 declarator = declarator->declarator;
3995 /* Check for some types that there cannot be arrays of. */
3997 if (VOID_TYPE_P (type))
3999 error ("declaration of %qs as array of voids", name);
4000 type = error_mark_node;
4003 if (TREE_CODE (type) == FUNCTION_TYPE)
4005 error ("declaration of %qs as array of functions", name);
4006 type = error_mark_node;
4009 if (pedantic && !in_system_header && flexible_array_type_p (type))
4010 pedwarn ("invalid use of structure with flexible array member");
4012 if (size == error_mark_node)
4013 type = error_mark_node;
4015 if (type == error_mark_node)
4016 continue;
4018 /* If size was specified, set ITYPE to a range-type for
4019 that size. Otherwise, ITYPE remains null. finish_decl
4020 may figure it out from an initial value. */
4022 if (size)
4024 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4025 lvalue. */
4026 STRIP_TYPE_NOPS (size);
4028 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4030 error ("size of array %qs has non-integer type", name);
4031 size = integer_one_node;
4034 if (pedantic && integer_zerop (size))
4035 pedwarn ("ISO C forbids zero-size array %qs", name);
4037 if (TREE_CODE (size) == INTEGER_CST)
4039 constant_expression_warning (size);
4040 if (tree_int_cst_sgn (size) < 0)
4042 error ("size of array %qs is negative", name);
4043 size = integer_one_node;
4046 else
4048 /* Make sure the array size remains visibly
4049 nonconstant even if it is (eg) a const variable
4050 with known value. */
4051 size_varies = 1;
4053 if (!flag_isoc99 && pedantic)
4055 if (TREE_CONSTANT (size))
4056 pedwarn ("ISO C90 forbids array %qs whose size "
4057 "can%'t be evaluated",
4058 name);
4059 else
4060 pedwarn ("ISO C90 forbids variable-size array %qs",
4061 name);
4065 if (integer_zerop (size))
4067 /* A zero-length array cannot be represented with
4068 an unsigned index type, which is what we'll
4069 get with build_index_type. Create an
4070 open-ended range instead. */
4071 itype = build_range_type (sizetype, size, NULL_TREE);
4073 else
4075 /* Compute the maximum valid index, that is, size
4076 - 1. Do the calculation in index_type, so that
4077 if it is a variable the computations will be
4078 done in the proper mode. */
4079 itype = fold (build2 (MINUS_EXPR, index_type,
4080 convert (index_type, size),
4081 convert (index_type,
4082 size_one_node)));
4084 /* If that overflowed, the array is too big. ???
4085 While a size of INT_MAX+1 technically shouldn't
4086 cause an overflow (because we subtract 1), the
4087 overflow is recorded during the conversion to
4088 index_type, before the subtraction. Handling
4089 this case seems like an unnecessary
4090 complication. */
4091 if (TREE_OVERFLOW (itype))
4093 error ("size of array %qs is too large", name);
4094 type = error_mark_node;
4095 continue;
4098 if (size_varies)
4099 itype = variable_size (itype);
4100 itype = build_index_type (itype);
4103 else if (decl_context == FIELD)
4105 if (pedantic && !flag_isoc99 && !in_system_header)
4106 pedwarn ("ISO C90 does not support flexible array members");
4108 /* ISO C99 Flexible array members are effectively
4109 identical to GCC's zero-length array extension. */
4110 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4113 /* If pedantic, complain about arrays of incomplete types. */
4114 if (pedantic && !COMPLETE_TYPE_P (type))
4115 pedwarn ("array type has incomplete element type");
4117 /* Build the array type itself, then merge any constancy
4118 or volatility into the target type. We must do it in
4119 this order to ensure that the TYPE_MAIN_VARIANT field
4120 of the array type is set correctly. */
4121 type = build_array_type (type, itype);
4122 if (type_quals)
4123 type = c_build_qualified_type (type, type_quals);
4125 if (size_varies)
4126 C_TYPE_VARIABLE_SIZE (type) = 1;
4128 /* The GCC extension for zero-length arrays differs from
4129 ISO flexible array members in that sizeof yields
4130 zero. */
4131 if (size && integer_zerop (size))
4133 TYPE_SIZE (type) = bitsize_zero_node;
4134 TYPE_SIZE_UNIT (type) = size_zero_node;
4137 if (decl_context != PARM
4138 && (array_ptr_quals != TYPE_UNQUALIFIED
4139 || array_ptr_attrs != NULL_TREE
4140 || array_parm_static))
4142 error ("static or type qualifiers in non-parameter array declarator");
4143 array_ptr_quals = TYPE_UNQUALIFIED;
4144 array_ptr_attrs = NULL_TREE;
4145 array_parm_static = 0;
4147 break;
4149 case cdk_function:
4151 /* Say it's a definition only for the declarator closest
4152 to the identifier, apart possibly from some
4153 attributes. */
4154 bool really_funcdef = false;
4155 tree arg_types;
4156 if (funcdef_flag)
4158 const struct c_declarator *t = declarator->declarator;
4159 while (t->kind == cdk_attrs)
4160 t = t->declarator;
4161 really_funcdef = (t->kind == cdk_id);
4164 /* Declaring a function type. Make sure we have a valid
4165 type for the function to return. */
4166 if (type == error_mark_node)
4167 continue;
4169 size_varies = 0;
4171 /* Warn about some types functions can't return. */
4172 if (TREE_CODE (type) == FUNCTION_TYPE)
4174 error ("%qs declared as function returning a function", name);
4175 type = integer_type_node;
4177 if (TREE_CODE (type) == ARRAY_TYPE)
4179 error ("%qs declared as function returning an array", name);
4180 type = integer_type_node;
4183 /* Construct the function type and go to the next
4184 inner layer of declarator. */
4185 arg_info = declarator->u.arg_info;
4186 arg_types = grokparms (arg_info, really_funcdef);
4188 /* Type qualifiers before the return type of the function
4189 qualify the return type, not the function type. */
4190 if (type_quals)
4192 /* Type qualifiers on a function return type are
4193 normally permitted by the standard but have no
4194 effect, so give a warning at -Wreturn-type.
4195 Qualifiers on a void return type are banned on
4196 function definitions in ISO C; GCC used to used
4197 them for noreturn functions. */
4198 if (VOID_TYPE_P (type) && really_funcdef)
4199 pedwarn ("function definition has qualified void return type");
4200 else if (warn_return_type)
4201 warning ("type qualifiers ignored on function return type");
4203 type = c_build_qualified_type (type, type_quals);
4205 type_quals = TYPE_UNQUALIFIED;
4207 type = build_function_type (type, arg_types);
4208 declarator = declarator->declarator;
4210 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4211 the formal parameter list of this FUNCTION_TYPE to point to
4212 the FUNCTION_TYPE node itself. */
4214 tree link;
4216 for (link = arg_info->tags;
4217 link;
4218 link = TREE_CHAIN (link))
4219 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4221 break;
4223 case cdk_pointer:
4225 /* Merge any constancy or volatility into the target type
4226 for the pointer. */
4228 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4229 && type_quals)
4230 pedwarn ("ISO C forbids qualified function types");
4231 if (type_quals)
4232 type = c_build_qualified_type (type, type_quals);
4233 size_varies = 0;
4235 type = build_pointer_type (type);
4237 /* Process type qualifiers (such as const or volatile)
4238 that were given inside the `*'. */
4239 type_quals = declarator->u.pointer_quals;
4241 declarator = declarator->declarator;
4242 break;
4244 default:
4245 gcc_unreachable ();
4249 /* Now TYPE has the actual type. */
4251 /* Check the type and width of a bit-field. */
4252 if (bitfield)
4253 check_bitfield_type_and_width (&type, width, orig_name);
4255 /* Did array size calculations overflow? */
4257 if (TREE_CODE (type) == ARRAY_TYPE
4258 && COMPLETE_TYPE_P (type)
4259 && TREE_OVERFLOW (TYPE_SIZE (type)))
4261 error ("size of array %qs is too large", name);
4262 /* If we proceed with the array type as it is, we'll eventually
4263 crash in tree_low_cst(). */
4264 type = error_mark_node;
4267 /* If this is declaring a typedef name, return a TYPE_DECL. */
4269 if (storage_class == csc_typedef)
4271 tree decl;
4272 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4273 && type_quals)
4274 pedwarn ("ISO C forbids qualified function types");
4275 if (type_quals)
4276 type = c_build_qualified_type (type, type_quals);
4277 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4278 if (declspecs->explicit_signed_p)
4279 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4280 decl_attributes (&decl, returned_attrs, 0);
4281 if (declspecs->inline_p)
4282 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4283 return decl;
4286 /* Detect the case of an array type of unspecified size
4287 which came, as such, direct from a typedef name.
4288 We must copy the type, so that each identifier gets
4289 a distinct type, so that each identifier's size can be
4290 controlled separately by its own initializer. */
4292 if (type != 0 && typedef_type != 0
4293 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4294 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4296 type = build_array_type (TREE_TYPE (type), 0);
4297 if (size_varies)
4298 C_TYPE_VARIABLE_SIZE (type) = 1;
4301 /* If this is a type name (such as, in a cast or sizeof),
4302 compute the type and return it now. */
4304 if (decl_context == TYPENAME)
4306 /* Note that the grammar rejects storage classes in typenames
4307 and fields. */
4308 gcc_assert (storage_class == csc_none && !threadp
4309 && !declspecs->inline_p);
4310 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4311 && type_quals)
4312 pedwarn ("ISO C forbids const or volatile function types");
4313 if (type_quals)
4314 type = c_build_qualified_type (type, type_quals);
4315 decl_attributes (&type, returned_attrs, 0);
4316 return type;
4319 /* Aside from typedefs and type names (handle above),
4320 `void' at top level (not within pointer)
4321 is allowed only in public variables.
4322 We don't complain about parms either, but that is because
4323 a better error message can be made later. */
4325 if (VOID_TYPE_P (type) && decl_context != PARM
4326 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4327 && (storage_class == csc_extern
4328 || (current_scope == file_scope
4329 && !(storage_class == csc_static
4330 || storage_class == csc_register)))))
4332 error ("variable or field %qs declared void", name);
4333 type = integer_type_node;
4336 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4337 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4340 tree decl;
4342 if (decl_context == PARM)
4344 tree type_as_written;
4345 tree promoted_type;
4347 /* A parameter declared as an array of T is really a pointer to T.
4348 One declared as a function is really a pointer to a function. */
4350 if (TREE_CODE (type) == ARRAY_TYPE)
4352 /* Transfer const-ness of array into that of type pointed to. */
4353 type = TREE_TYPE (type);
4354 if (type_quals)
4355 type = c_build_qualified_type (type, type_quals);
4356 type = build_pointer_type (type);
4357 type_quals = array_ptr_quals;
4359 /* We don't yet implement attributes in this context. */
4360 if (array_ptr_attrs != NULL_TREE)
4361 warning ("attributes in parameter array declarator ignored");
4363 size_varies = 0;
4365 else if (TREE_CODE (type) == FUNCTION_TYPE)
4367 if (pedantic && type_quals)
4368 pedwarn ("ISO C forbids qualified function types");
4369 if (type_quals)
4370 type = c_build_qualified_type (type, type_quals);
4371 type = build_pointer_type (type);
4372 type_quals = TYPE_UNQUALIFIED;
4374 else if (type_quals)
4375 type = c_build_qualified_type (type, type_quals);
4377 type_as_written = type;
4379 decl = build_decl (PARM_DECL, declarator->u.id, type);
4380 if (size_varies)
4381 C_DECL_VARIABLE_SIZE (decl) = 1;
4383 /* Compute the type actually passed in the parmlist,
4384 for the case where there is no prototype.
4385 (For example, shorts and chars are passed as ints.)
4386 When there is a prototype, this is overridden later. */
4388 if (type == error_mark_node)
4389 promoted_type = type;
4390 else
4391 promoted_type = c_type_promotes_to (type);
4393 DECL_ARG_TYPE (decl) = promoted_type;
4394 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4395 if (declspecs->inline_p)
4396 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4398 else if (decl_context == FIELD)
4400 /* Note that the grammar rejects storage classes in typenames
4401 and fields. */
4402 gcc_assert (storage_class == csc_none && !threadp
4403 && !declspecs->inline_p);
4405 /* Structure field. It may not be a function. */
4407 if (TREE_CODE (type) == FUNCTION_TYPE)
4409 error ("field %qs declared as a function", name);
4410 type = build_pointer_type (type);
4412 else if (TREE_CODE (type) != ERROR_MARK
4413 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4415 error ("field %qs has incomplete type", name);
4416 type = error_mark_node;
4418 /* Move type qualifiers down to element of an array. */
4419 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4420 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4421 type_quals),
4422 TYPE_DOMAIN (type));
4423 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4424 DECL_NONADDRESSABLE_P (decl) = bitfield;
4426 if (size_varies)
4427 C_DECL_VARIABLE_SIZE (decl) = 1;
4429 else if (TREE_CODE (type) == FUNCTION_TYPE)
4431 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4432 decl = build_decl_attribute_variant (decl, decl_attr);
4434 if (storage_class == csc_register || threadp)
4436 error ("invalid storage class for function %qs", name);
4437 if (DECL_INITIAL (decl) != NULL_TREE)
4438 DECL_INITIAL (decl) = error_mark_node;
4440 else if (current_scope != file_scope)
4442 /* Function declaration not at file scope. Storage
4443 classes other than `extern' are not allowed, C99
4444 6.7.1p5, and `extern' makes no difference. However,
4445 GCC allows 'auto', perhaps with 'inline', to support
4446 nested functions. */
4447 if (storage_class == csc_auto)
4449 if (pedantic)
4450 pedwarn ("invalid storage class for function %qs", name);
4452 if (storage_class == csc_static)
4454 error ("invalid storage class for function %qs", name);
4455 if (DECL_INITIAL (decl) != NULL_TREE)
4456 DECL_INITIAL (decl) = error_mark_node;
4460 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4462 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4463 pedwarn ("ISO C forbids qualified function types");
4465 /* GNU C interprets a volatile-qualified function type to indicate
4466 that the function does not return. */
4467 if ((type_quals & TYPE_QUAL_VOLATILE)
4468 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4469 warning ("%<noreturn%> function returns non-void value");
4471 /* Every function declaration is an external reference
4472 (DECL_EXTERNAL) except for those which are not at file
4473 scope and are explicitly declared "auto". This is
4474 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4475 GCC to signify a forward declaration of a nested function. */
4476 if (storage_class == csc_auto && current_scope != file_scope)
4477 DECL_EXTERNAL (decl) = 0;
4478 else
4479 DECL_EXTERNAL (decl) = 1;
4481 /* Record absence of global scope for `static' or `auto'. */
4482 TREE_PUBLIC (decl)
4483 = !(storage_class == csc_static || storage_class == csc_auto);
4485 /* For a function definition, record the argument information
4486 block where store_parm_decls will look for it. */
4487 if (funcdef_flag)
4488 current_function_arg_info = arg_info;
4490 if (declspecs->default_int_p)
4491 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4493 /* Record presence of `inline', if it is reasonable. */
4494 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4496 if (declspecs->inline_p)
4497 pedwarn ("cannot inline function %<main%>");
4499 else if (declspecs->inline_p)
4501 /* Record that the function is declared `inline'. */
4502 DECL_DECLARED_INLINE_P (decl) = 1;
4504 /* Do not mark bare declarations as DECL_INLINE. Doing so
4505 in the presence of multiple declarations can result in
4506 the abstract origin pointing between the declarations,
4507 which will confuse dwarf2out. */
4508 if (initialized)
4510 DECL_INLINE (decl) = 1;
4511 if (storage_class == csc_extern)
4512 current_extern_inline = 1;
4515 /* If -finline-functions, assume it can be inlined. This does
4516 two things: let the function be deferred until it is actually
4517 needed, and let dwarf2 know that the function is inlinable. */
4518 else if (flag_inline_trees == 2 && initialized)
4519 DECL_INLINE (decl) = 1;
4521 else
4523 /* It's a variable. */
4524 /* An uninitialized decl with `extern' is a reference. */
4525 int extern_ref = !initialized && storage_class == csc_extern;
4527 /* Move type qualifiers down to element of an array. */
4528 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4530 int saved_align = TYPE_ALIGN(type);
4531 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4532 type_quals),
4533 TYPE_DOMAIN (type));
4534 TYPE_ALIGN (type) = saved_align;
4536 else if (type_quals)
4537 type = c_build_qualified_type (type, type_quals);
4539 /* C99 6.2.2p7: It is invalid (compile-time undefined
4540 behavior) to create an 'extern' declaration for a
4541 variable if there is a global declaration that is
4542 'static' and the global declaration is not visible.
4543 (If the static declaration _is_ currently visible,
4544 the 'extern' declaration is taken to refer to that decl.) */
4545 if (extern_ref && current_scope != file_scope)
4547 tree global_decl = identifier_global_value (declarator->u.id);
4548 tree visible_decl = lookup_name (declarator->u.id);
4550 if (global_decl
4551 && global_decl != visible_decl
4552 && TREE_CODE (global_decl) == VAR_DECL
4553 && !TREE_PUBLIC (global_decl))
4554 error ("variable previously declared %<static%> redeclared "
4555 "%<extern%>");
4558 decl = build_decl (VAR_DECL, declarator->u.id, type);
4559 if (size_varies)
4560 C_DECL_VARIABLE_SIZE (decl) = 1;
4562 if (declspecs->inline_p)
4563 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4565 /* At file scope, an initialized extern declaration may follow
4566 a static declaration. In that case, DECL_EXTERNAL will be
4567 reset later in start_decl. */
4568 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4570 /* At file scope, the presence of a `static' or `register' storage
4571 class specifier, or the absence of all storage class specifiers
4572 makes this declaration a definition (perhaps tentative). Also,
4573 the absence of both `static' and `register' makes it public. */
4574 if (current_scope == file_scope)
4576 TREE_PUBLIC (decl) = !(storage_class == csc_static
4577 || storage_class == csc_register);
4578 TREE_STATIC (decl) = !extern_ref;
4580 /* Not at file scope, only `static' makes a static definition. */
4581 else
4583 TREE_STATIC (decl) = (storage_class == csc_static);
4584 TREE_PUBLIC (decl) = extern_ref;
4587 if (threadp)
4589 if (targetm.have_tls)
4590 DECL_THREAD_LOCAL (decl) = 1;
4591 else
4592 /* A mere warning is sure to result in improper semantics
4593 at runtime. Don't bother to allow this to compile. */
4594 error ("thread-local storage not supported for this target");
4598 /* Record `register' declaration for warnings on &
4599 and in case doing stupid register allocation. */
4601 if (storage_class == csc_register)
4603 C_DECL_REGISTER (decl) = 1;
4604 DECL_REGISTER (decl) = 1;
4607 /* Record constancy and volatility. */
4608 c_apply_type_quals_to_decl (type_quals, decl);
4610 /* If a type has volatile components, it should be stored in memory.
4611 Otherwise, the fact that those components are volatile
4612 will be ignored, and would even crash the compiler. */
4613 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4615 /* It is not an error for a structure with volatile fields to
4616 be declared register, but reset DECL_REGISTER since it
4617 cannot actually go in a register. */
4618 int was_reg = C_DECL_REGISTER (decl);
4619 C_DECL_REGISTER (decl) = 0;
4620 DECL_REGISTER (decl) = 0;
4621 c_mark_addressable (decl);
4622 C_DECL_REGISTER (decl) = was_reg;
4625 /* This is the earliest point at which we might know the assembler
4626 name of a variable. Thus, if it's known before this, die horribly. */
4627 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4629 decl_attributes (&decl, returned_attrs, 0);
4631 return decl;
4635 /* Decode the parameter-list info for a function type or function definition.
4636 The argument is the value returned by `get_parm_info' (or made in parse.y
4637 if there is an identifier list instead of a parameter decl list).
4638 These two functions are separate because when a function returns
4639 or receives functions then each is called multiple times but the order
4640 of calls is different. The last call to `grokparms' is always the one
4641 that contains the formal parameter names of a function definition.
4643 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4645 FUNCDEF_FLAG is true for a function definition, false for
4646 a mere declaration. A nonempty identifier-list gets an error message
4647 when FUNCDEF_FLAG is false. */
4649 static tree
4650 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4652 tree arg_types = arg_info->types;
4654 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4655 && !in_system_header)
4656 warning ("function declaration isn%'t a prototype");
4658 if (arg_types == error_mark_node)
4659 return 0; /* don't set TYPE_ARG_TYPES in this case */
4661 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4663 if (!funcdef_flag)
4664 pedwarn ("parameter names (without types) in function declaration");
4666 arg_info->parms = arg_info->types;
4667 arg_info->types = 0;
4668 return 0;
4670 else
4672 tree parm, type, typelt;
4673 unsigned int parmno;
4675 /* If there is a parameter of incomplete type in a definition,
4676 this is an error. In a declaration this is valid, and a
4677 struct or union type may be completed later, before any calls
4678 or definition of the function. In the case where the tag was
4679 first declared within the parameter list, a warning has
4680 already been given. If a parameter has void type, then
4681 however the function cannot be defined or called, so
4682 warn. */
4684 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4685 parm;
4686 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4688 type = TREE_VALUE (typelt);
4689 if (type == error_mark_node)
4690 continue;
4692 if (!COMPLETE_TYPE_P (type))
4694 if (funcdef_flag)
4696 if (DECL_NAME (parm))
4697 error ("%Jparameter %u (%qD) has incomplete type",
4698 parm, parmno, parm);
4699 else
4700 error ("%Jparameter %u has incomplete type",
4701 parm, parmno);
4703 TREE_VALUE (typelt) = error_mark_node;
4704 TREE_TYPE (parm) = error_mark_node;
4706 else if (VOID_TYPE_P (type))
4708 if (DECL_NAME (parm))
4709 warning ("%Jparameter %u (%qD) has void type",
4710 parm, parmno, parm);
4711 else
4712 warning ("%Jparameter %u has void type",
4713 parm, parmno);
4717 return arg_types;
4721 /* Take apart the current scope and return a c_arg_info structure with
4722 info on a parameter list just parsed.
4724 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4726 ELLIPSIS being true means the argument list ended in '...' so don't
4727 append a sentinel (void_list_node) to the end of the type-list. */
4729 struct c_arg_info *
4730 get_parm_info (bool ellipsis)
4732 struct c_binding *b = current_scope->bindings;
4733 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4734 struct c_arg_info);
4735 tree parms = 0;
4736 tree tags = 0;
4737 tree types = 0;
4738 tree others = 0;
4740 static bool explained_incomplete_types = false;
4741 bool gave_void_only_once_err = false;
4743 arg_info->parms = 0;
4744 arg_info->tags = 0;
4745 arg_info->types = 0;
4746 arg_info->others = 0;
4748 /* The bindings in this scope must not get put into a block.
4749 We will take care of deleting the binding nodes. */
4750 current_scope->bindings = 0;
4752 /* This function is only called if there was *something* on the
4753 parameter list. */
4754 gcc_assert (b);
4756 /* A parameter list consisting solely of 'void' indicates that the
4757 function takes no arguments. But if the 'void' is qualified
4758 (by 'const' or 'volatile'), or has a storage class specifier
4759 ('register'), then the behavior is undefined; issue an error.
4760 Typedefs for 'void' are OK (see DR#157). */
4761 if (b->prev == 0 /* one binding */
4762 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4763 && !DECL_NAME (b->decl) /* anonymous */
4764 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4766 if (TREE_THIS_VOLATILE (b->decl)
4767 || TREE_READONLY (b->decl)
4768 || C_DECL_REGISTER (b->decl))
4769 error ("%<void%> as only parameter may not be qualified");
4771 /* There cannot be an ellipsis. */
4772 if (ellipsis)
4773 error ("%<void%> must be the only parameter");
4775 arg_info->types = void_list_node;
4776 return arg_info;
4779 if (!ellipsis)
4780 types = void_list_node;
4782 /* Break up the bindings list into parms, tags, types, and others;
4783 apply sanity checks; purge the name-to-decl bindings. */
4784 while (b)
4786 tree decl = b->decl;
4787 tree type = TREE_TYPE (decl);
4788 const char *keyword;
4790 switch (TREE_CODE (decl))
4792 case PARM_DECL:
4793 if (b->id)
4795 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4796 I_SYMBOL_BINDING (b->id) = b->shadowed;
4799 /* Check for forward decls that never got their actual decl. */
4800 if (TREE_ASM_WRITTEN (decl))
4801 error ("%Jparameter %qD has just a forward declaration",
4802 decl, decl);
4803 /* Check for (..., void, ...) and issue an error. */
4804 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4806 if (!gave_void_only_once_err)
4808 error ("%<void%> must be the only parameter");
4809 gave_void_only_once_err = true;
4812 else
4814 /* Valid parameter, add it to the list. */
4815 TREE_CHAIN (decl) = parms;
4816 parms = decl;
4818 /* Since there is a prototype, args are passed in their
4819 declared types. The back end may override this later. */
4820 DECL_ARG_TYPE (decl) = type;
4821 types = tree_cons (0, type, types);
4823 break;
4825 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4826 case UNION_TYPE: keyword = "union"; goto tag;
4827 case RECORD_TYPE: keyword = "struct"; goto tag;
4828 tag:
4829 /* Types may not have tag-names, in which case the type
4830 appears in the bindings list with b->id NULL. */
4831 if (b->id)
4833 gcc_assert (I_TAG_BINDING (b->id) == b);
4834 I_TAG_BINDING (b->id) = b->shadowed;
4837 /* Warn about any struct, union or enum tags defined in a
4838 parameter list. The scope of such types is limited to
4839 the parameter list, which is rarely if ever desirable
4840 (it's impossible to call such a function with type-
4841 correct arguments). An anonymous union parm type is
4842 meaningful as a GNU extension, so don't warn for that. */
4843 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4845 if (b->id)
4846 /* The %s will be one of 'struct', 'union', or 'enum'. */
4847 warning ("%<%s %E%> declared inside parameter list",
4848 keyword, b->id);
4849 else
4850 /* The %s will be one of 'struct', 'union', or 'enum'. */
4851 warning ("anonymous %s declared inside parameter list",
4852 keyword);
4854 if (!explained_incomplete_types)
4856 warning ("its scope is only this definition or declaration,"
4857 " which is probably not what you want");
4858 explained_incomplete_types = true;
4862 tags = tree_cons (b->id, decl, tags);
4863 break;
4865 case CONST_DECL:
4866 case TYPE_DECL:
4867 case FUNCTION_DECL:
4868 /* CONST_DECLs appear here when we have an embedded enum,
4869 and TYPE_DECLs appear here when we have an embedded struct
4870 or union. No warnings for this - we already warned about the
4871 type itself. FUNCTION_DECLs appear when there is an implicit
4872 function declaration in the parameter list. */
4874 TREE_CHAIN (decl) = others;
4875 others = decl;
4876 /* fall through */
4878 case ERROR_MARK:
4879 /* error_mark_node appears here when we have an undeclared
4880 variable. Just throw it away. */
4881 if (b->id)
4883 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4884 I_SYMBOL_BINDING (b->id) = b->shadowed;
4886 break;
4888 /* Other things that might be encountered. */
4889 case LABEL_DECL:
4890 case VAR_DECL:
4891 default:
4892 gcc_unreachable ();
4895 b = free_binding_and_advance (b);
4898 arg_info->parms = parms;
4899 arg_info->tags = tags;
4900 arg_info->types = types;
4901 arg_info->others = others;
4902 return arg_info;
4905 /* Get the struct, enum or union (CODE says which) with tag NAME.
4906 Define the tag as a forward-reference if it is not defined.
4907 Return a c_typespec structure for the type specifier. */
4909 struct c_typespec
4910 parser_xref_tag (enum tree_code code, tree name)
4912 struct c_typespec ret;
4913 /* If a cross reference is requested, look up the type
4914 already defined for this tag and return it. */
4916 tree ref = lookup_tag (code, name, 0);
4917 /* If this is the right type of tag, return what we found.
4918 (This reference will be shadowed by shadow_tag later if appropriate.)
4919 If this is the wrong type of tag, do not return it. If it was the
4920 wrong type in the same scope, we will have had an error
4921 message already; if in a different scope and declaring
4922 a name, pending_xref_error will give an error message; but if in a
4923 different scope and not declaring a name, this tag should
4924 shadow the previous declaration of a different type of tag, and
4925 this would not work properly if we return the reference found.
4926 (For example, with "struct foo" in an outer scope, "union foo;"
4927 must shadow that tag with a new one of union type.) */
4928 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4929 if (ref && TREE_CODE (ref) == code)
4931 ret.spec = ref;
4932 return ret;
4935 /* If no such tag is yet defined, create a forward-reference node
4936 and record it as the "definition".
4937 When a real declaration of this type is found,
4938 the forward-reference will be altered into a real type. */
4940 ref = make_node (code);
4941 if (code == ENUMERAL_TYPE)
4943 /* Give the type a default layout like unsigned int
4944 to avoid crashing if it does not get defined. */
4945 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4946 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4947 TYPE_USER_ALIGN (ref) = 0;
4948 TYPE_UNSIGNED (ref) = 1;
4949 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4950 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4951 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4954 pushtag (name, ref);
4956 ret.spec = ref;
4957 return ret;
4960 /* Get the struct, enum or union (CODE says which) with tag NAME.
4961 Define the tag as a forward-reference if it is not defined.
4962 Return a tree for the type. */
4964 tree
4965 xref_tag (enum tree_code code, tree name)
4967 return parser_xref_tag (code, name).spec;
4970 /* Make sure that the tag NAME is defined *in the current scope*
4971 at least as a forward reference.
4972 CODE says which kind of tag NAME ought to be. */
4974 tree
4975 start_struct (enum tree_code code, tree name)
4977 /* If there is already a tag defined at this scope
4978 (as a forward reference), just return it. */
4980 tree ref = 0;
4982 if (name != 0)
4983 ref = lookup_tag (code, name, 1);
4984 if (ref && TREE_CODE (ref) == code)
4986 if (TYPE_SIZE (ref))
4988 if (code == UNION_TYPE)
4989 error ("redefinition of %<union %s%>", IDENTIFIER_POINTER (name));
4990 else
4991 error ("redefinition of %<struct %s%>", IDENTIFIER_POINTER (name));
4993 else if (C_TYPE_BEING_DEFINED (ref))
4995 if (code == UNION_TYPE)
4996 error ("nested redefinition of %<union %s%>",
4997 IDENTIFIER_POINTER (name));
4998 else
4999 error ("nested redefinition of %<struct %s%>",
5000 IDENTIFIER_POINTER (name));
5003 else
5005 /* Otherwise create a forward-reference just so the tag is in scope. */
5007 ref = make_node (code);
5008 pushtag (name, ref);
5011 C_TYPE_BEING_DEFINED (ref) = 1;
5012 TYPE_PACKED (ref) = flag_pack_struct;
5013 return ref;
5016 /* Process the specs, declarator and width (NULL if omitted)
5017 of a structure component, returning a FIELD_DECL node.
5018 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5020 This is done during the parsing of the struct declaration.
5021 The FIELD_DECL nodes are chained together and the lot of them
5022 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5024 tree
5025 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5026 tree width)
5028 tree value;
5030 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5031 && width == NULL_TREE)
5033 /* This is an unnamed decl.
5035 If we have something of the form "union { list } ;" then this
5036 is the anonymous union extension. Similarly for struct.
5038 If this is something of the form "struct foo;", then
5039 If MS extensions are enabled, this is handled as an
5040 anonymous struct.
5041 Otherwise this is a forward declaration of a structure tag.
5043 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5044 If MS extensions are enabled and foo names a structure, then
5045 again this is an anonymous struct.
5046 Otherwise this is an error.
5048 Oh what a horrid tangled web we weave. I wonder if MS consciously
5049 took this from Plan 9 or if it was an accident of implementation
5050 that took root before someone noticed the bug... */
5052 tree type = declspecs->type;
5053 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5054 || TREE_CODE (type) == UNION_TYPE);
5055 bool ok = false;
5057 if (type_ok
5058 && (flag_ms_extensions || !declspecs->typedef_p))
5060 if (flag_ms_extensions)
5061 ok = true;
5062 else if (flag_iso)
5063 ok = false;
5064 else if (TYPE_NAME (type) == NULL)
5065 ok = true;
5066 else
5067 ok = false;
5069 if (!ok)
5071 pedwarn ("declaration does not declare anything");
5072 return NULL_TREE;
5074 if (pedantic)
5075 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5078 value = grokdeclarator (declarator, declspecs, FIELD, false,
5079 width ? &width : NULL);
5081 finish_decl (value, NULL_TREE, NULL_TREE);
5082 DECL_INITIAL (value) = width;
5084 return value;
5087 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5088 the list such that this does not present a problem later. */
5090 static void
5091 detect_field_duplicates (tree fieldlist)
5093 tree x, y;
5094 int timeout = 10;
5096 /* First, see if there are more than "a few" fields.
5097 This is trivially true if there are zero or one fields. */
5098 if (!fieldlist)
5099 return;
5100 x = TREE_CHAIN (fieldlist);
5101 if (!x)
5102 return;
5103 do {
5104 timeout--;
5105 x = TREE_CHAIN (x);
5106 } while (timeout > 0 && x);
5108 /* If there were "few" fields, avoid the overhead of allocating
5109 a hash table. Instead just do the nested traversal thing. */
5110 if (timeout > 0)
5112 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5113 if (DECL_NAME (x))
5115 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5116 if (DECL_NAME (y) == DECL_NAME (x))
5118 error ("%Jduplicate member %qD", x, x);
5119 DECL_NAME (x) = NULL_TREE;
5123 else
5125 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5126 void **slot;
5128 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5129 if ((y = DECL_NAME (x)) != 0)
5131 slot = htab_find_slot (htab, y, INSERT);
5132 if (*slot)
5134 error ("%Jduplicate member %qD", x, x);
5135 DECL_NAME (x) = NULL_TREE;
5137 *slot = y;
5140 htab_delete (htab);
5144 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5145 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5146 ATTRIBUTES are attributes to be applied to the structure. */
5148 tree
5149 finish_struct (tree t, tree fieldlist, tree attributes)
5151 tree x;
5152 bool toplevel = file_scope == current_scope;
5153 int saw_named_field;
5155 /* If this type was previously laid out as a forward reference,
5156 make sure we lay it out again. */
5158 TYPE_SIZE (t) = 0;
5160 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5162 if (pedantic)
5164 for (x = fieldlist; x; x = TREE_CHAIN (x))
5165 if (DECL_NAME (x) != 0)
5166 break;
5168 if (x == 0)
5170 if (TREE_CODE (t) == UNION_TYPE)
5172 if (fieldlist)
5173 pedwarn ("union has no named members");
5174 else
5175 pedwarn ("union has no members");
5177 else
5179 if (fieldlist)
5180 pedwarn ("struct has no named members");
5181 else
5182 pedwarn ("struct has no members");
5187 /* Install struct as DECL_CONTEXT of each field decl.
5188 Also process specified field sizes, found in the DECL_INITIAL,
5189 storing 0 there after the type has been changed to precision equal
5190 to its width, rather than the precision of the specified standard
5191 type. (Correct layout requires the original type to have been preserved
5192 until now.) */
5194 saw_named_field = 0;
5195 for (x = fieldlist; x; x = TREE_CHAIN (x))
5197 DECL_CONTEXT (x) = t;
5198 DECL_PACKED (x) |= TYPE_PACKED (t);
5200 /* If any field is const, the structure type is pseudo-const. */
5201 if (TREE_READONLY (x))
5202 C_TYPE_FIELDS_READONLY (t) = 1;
5203 else
5205 /* A field that is pseudo-const makes the structure likewise. */
5206 tree t1 = TREE_TYPE (x);
5207 while (TREE_CODE (t1) == ARRAY_TYPE)
5208 t1 = TREE_TYPE (t1);
5209 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5210 && C_TYPE_FIELDS_READONLY (t1))
5211 C_TYPE_FIELDS_READONLY (t) = 1;
5214 /* Any field that is volatile means variables of this type must be
5215 treated in some ways as volatile. */
5216 if (TREE_THIS_VOLATILE (x))
5217 C_TYPE_FIELDS_VOLATILE (t) = 1;
5219 /* Any field of nominal variable size implies structure is too. */
5220 if (C_DECL_VARIABLE_SIZE (x))
5221 C_TYPE_VARIABLE_SIZE (t) = 1;
5223 if (DECL_INITIAL (x))
5225 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5226 DECL_SIZE (x) = bitsize_int (width);
5227 DECL_BIT_FIELD (x) = 1;
5228 SET_DECL_C_BIT_FIELD (x);
5231 /* Detect flexible array member in an invalid context. */
5232 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5233 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5234 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5235 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5237 if (TREE_CODE (t) == UNION_TYPE)
5239 error ("%Jflexible array member in union", x);
5240 TREE_TYPE (x) = error_mark_node;
5242 else if (TREE_CHAIN (x) != NULL_TREE)
5244 error ("%Jflexible array member not at end of struct", x);
5245 TREE_TYPE (x) = error_mark_node;
5247 else if (!saw_named_field)
5249 error ("%Jflexible array member in otherwise empty struct", x);
5250 TREE_TYPE (x) = error_mark_node;
5254 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5255 && flexible_array_type_p (TREE_TYPE (x)))
5256 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5258 if (DECL_NAME (x))
5259 saw_named_field = 1;
5262 detect_field_duplicates (fieldlist);
5264 /* Now we have the nearly final fieldlist. Record it,
5265 then lay out the structure or union (including the fields). */
5267 TYPE_FIELDS (t) = fieldlist;
5269 layout_type (t);
5271 /* Give bit-fields their proper types. */
5273 tree *fieldlistp = &fieldlist;
5274 while (*fieldlistp)
5275 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5276 && TREE_TYPE (*fieldlistp) != error_mark_node)
5278 unsigned HOST_WIDE_INT width
5279 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5280 tree type = TREE_TYPE (*fieldlistp);
5281 if (width != TYPE_PRECISION (type))
5282 TREE_TYPE (*fieldlistp)
5283 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5284 DECL_INITIAL (*fieldlistp) = 0;
5286 else
5287 fieldlistp = &TREE_CHAIN (*fieldlistp);
5290 /* Now we have the truly final field list.
5291 Store it in this type and in the variants. */
5293 TYPE_FIELDS (t) = fieldlist;
5295 /* If there are lots of fields, sort so we can look through them fast.
5296 We arbitrarily consider 16 or more elts to be "a lot". */
5299 int len = 0;
5301 for (x = fieldlist; x; x = TREE_CHAIN (x))
5303 if (len > 15 || DECL_NAME (x) == NULL)
5304 break;
5305 len += 1;
5308 if (len > 15)
5310 tree *field_array;
5311 struct lang_type *space;
5312 struct sorted_fields_type *space2;
5314 len += list_length (x);
5316 /* Use the same allocation policy here that make_node uses, to
5317 ensure that this lives as long as the rest of the struct decl.
5318 All decls in an inline function need to be saved. */
5320 space = GGC_CNEW (struct lang_type);
5321 space2 = GGC_NEWVAR (struct sorted_fields_type,
5322 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5324 len = 0;
5325 space->s = space2;
5326 field_array = &space2->elts[0];
5327 for (x = fieldlist; x; x = TREE_CHAIN (x))
5329 field_array[len++] = x;
5331 /* If there is anonymous struct or union, break out of the loop. */
5332 if (DECL_NAME (x) == NULL)
5333 break;
5335 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5336 if (x == NULL)
5338 TYPE_LANG_SPECIFIC (t) = space;
5339 TYPE_LANG_SPECIFIC (t)->s->len = len;
5340 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5341 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5346 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5348 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5349 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5350 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5351 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5354 /* If this was supposed to be a transparent union, but we can't
5355 make it one, warn and turn off the flag. */
5356 if (TREE_CODE (t) == UNION_TYPE
5357 && TYPE_TRANSPARENT_UNION (t)
5358 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5360 TYPE_TRANSPARENT_UNION (t) = 0;
5361 warning ("union cannot be made transparent");
5364 /* If this structure or union completes the type of any previous
5365 variable declaration, lay it out and output its rtl. */
5366 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5368 x = TREE_CHAIN (x))
5370 tree decl = TREE_VALUE (x);
5371 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5372 layout_array_type (TREE_TYPE (decl));
5373 if (TREE_CODE (decl) != TYPE_DECL)
5375 layout_decl (decl, 0);
5376 if (c_dialect_objc ())
5377 objc_check_decl (decl);
5378 rest_of_decl_compilation (decl, toplevel, 0);
5379 if (!toplevel)
5380 expand_decl (decl);
5383 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5385 /* Finish debugging output for this type. */
5386 rest_of_type_compilation (t, toplevel);
5388 return t;
5391 /* Lay out the type T, and its element type, and so on. */
5393 static void
5394 layout_array_type (tree t)
5396 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5397 layout_array_type (TREE_TYPE (t));
5398 layout_type (t);
5401 /* Begin compiling the definition of an enumeration type.
5402 NAME is its name (or null if anonymous).
5403 Returns the type object, as yet incomplete.
5404 Also records info about it so that build_enumerator
5405 may be used to declare the individual values as they are read. */
5407 tree
5408 start_enum (tree name)
5410 tree enumtype = 0;
5412 /* If this is the real definition for a previous forward reference,
5413 fill in the contents in the same object that used to be the
5414 forward reference. */
5416 if (name != 0)
5417 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5419 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5421 enumtype = make_node (ENUMERAL_TYPE);
5422 pushtag (name, enumtype);
5425 if (C_TYPE_BEING_DEFINED (enumtype))
5426 error ("nested redefinition of %<enum %s%>", IDENTIFIER_POINTER (name));
5428 C_TYPE_BEING_DEFINED (enumtype) = 1;
5430 if (TYPE_VALUES (enumtype) != 0)
5432 /* This enum is a named one that has been declared already. */
5433 error ("redeclaration of %<enum %s%>", IDENTIFIER_POINTER (name));
5435 /* Completely replace its old definition.
5436 The old enumerators remain defined, however. */
5437 TYPE_VALUES (enumtype) = 0;
5440 enum_next_value = integer_zero_node;
5441 enum_overflow = 0;
5443 if (flag_short_enums)
5444 TYPE_PACKED (enumtype) = 1;
5446 return enumtype;
5449 /* After processing and defining all the values of an enumeration type,
5450 install their decls in the enumeration type and finish it off.
5451 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5452 and ATTRIBUTES are the specified attributes.
5453 Returns ENUMTYPE. */
5455 tree
5456 finish_enum (tree enumtype, tree values, tree attributes)
5458 tree pair, tem;
5459 tree minnode = 0, maxnode = 0;
5460 int precision, unsign;
5461 bool toplevel = (file_scope == current_scope);
5462 struct lang_type *lt;
5464 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5466 /* Calculate the maximum value of any enumerator in this type. */
5468 if (values == error_mark_node)
5469 minnode = maxnode = integer_zero_node;
5470 else
5472 minnode = maxnode = TREE_VALUE (values);
5473 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5475 tree value = TREE_VALUE (pair);
5476 if (tree_int_cst_lt (maxnode, value))
5477 maxnode = value;
5478 if (tree_int_cst_lt (value, minnode))
5479 minnode = value;
5483 /* Construct the final type of this enumeration. It is the same
5484 as one of the integral types - the narrowest one that fits, except
5485 that normally we only go as narrow as int - and signed iff any of
5486 the values are negative. */
5487 unsign = (tree_int_cst_sgn (minnode) >= 0);
5488 precision = MAX (min_precision (minnode, unsign),
5489 min_precision (maxnode, unsign));
5491 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5493 tem = c_common_type_for_size (precision, unsign);
5494 if (tem == NULL)
5496 warning ("enumeration values exceed range of largest integer");
5497 tem = long_long_integer_type_node;
5500 else
5501 tem = unsign ? unsigned_type_node : integer_type_node;
5503 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5504 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5505 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5506 TYPE_SIZE (enumtype) = 0;
5508 /* If the precision of the type was specific with an attribute and it
5509 was too small, give an error. Otherwise, use it. */
5510 if (TYPE_PRECISION (enumtype))
5512 if (precision > TYPE_PRECISION (enumtype))
5513 error ("specified mode too small for enumeral values");
5515 else
5516 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5518 layout_type (enumtype);
5520 if (values != error_mark_node)
5522 /* Change the type of the enumerators to be the enum type. We
5523 need to do this irrespective of the size of the enum, for
5524 proper type checking. Replace the DECL_INITIALs of the
5525 enumerators, and the value slots of the list, with copies
5526 that have the enum type; they cannot be modified in place
5527 because they may be shared (e.g. integer_zero_node) Finally,
5528 change the purpose slots to point to the names of the decls. */
5529 for (pair = values; pair; pair = TREE_CHAIN (pair))
5531 tree enu = TREE_PURPOSE (pair);
5532 tree ini = DECL_INITIAL (enu);
5534 TREE_TYPE (enu) = enumtype;
5536 /* The ISO C Standard mandates enumerators to have type int,
5537 even though the underlying type of an enum type is
5538 unspecified. Here we convert any enumerators that fit in
5539 an int to type int, to avoid promotions to unsigned types
5540 when comparing integers with enumerators that fit in the
5541 int range. When -pedantic is given, build_enumerator()
5542 would have already taken care of those that don't fit. */
5543 if (int_fits_type_p (ini, integer_type_node))
5544 tem = integer_type_node;
5545 else
5546 tem = enumtype;
5547 ini = convert (tem, ini);
5549 DECL_INITIAL (enu) = ini;
5550 TREE_PURPOSE (pair) = DECL_NAME (enu);
5551 TREE_VALUE (pair) = ini;
5554 TYPE_VALUES (enumtype) = values;
5557 /* Record the min/max values so that we can warn about bit-field
5558 enumerations that are too small for the values. */
5559 lt = GGC_CNEW (struct lang_type);
5560 lt->enum_min = minnode;
5561 lt->enum_max = maxnode;
5562 TYPE_LANG_SPECIFIC (enumtype) = lt;
5564 /* Fix up all variant types of this enum type. */
5565 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5567 if (tem == enumtype)
5568 continue;
5569 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5570 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5571 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5572 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5573 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5574 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5575 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5576 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5577 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5578 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5579 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5582 /* Finish debugging output for this type. */
5583 rest_of_type_compilation (enumtype, toplevel);
5585 return enumtype;
5588 /* Build and install a CONST_DECL for one value of the
5589 current enumeration type (one that was begun with start_enum).
5590 Return a tree-list containing the CONST_DECL and its value.
5591 Assignment of sequential values by default is handled here. */
5593 tree
5594 build_enumerator (tree name, tree value)
5596 tree decl, type;
5598 /* Validate and default VALUE. */
5600 /* Remove no-op casts from the value. */
5601 if (value)
5602 STRIP_TYPE_NOPS (value);
5604 if (value != 0)
5606 /* Don't issue more errors for error_mark_node (i.e. an
5607 undeclared identifier) - just ignore the value expression. */
5608 if (value == error_mark_node)
5609 value = 0;
5610 else if (TREE_CODE (value) != INTEGER_CST)
5612 error ("enumerator value for %qE is not an integer constant", name);
5613 value = 0;
5615 else
5617 value = default_conversion (value);
5618 constant_expression_warning (value);
5622 /* Default based on previous value. */
5623 /* It should no longer be possible to have NON_LVALUE_EXPR
5624 in the default. */
5625 if (value == 0)
5627 value = enum_next_value;
5628 if (enum_overflow)
5629 error ("overflow in enumeration values");
5632 if (pedantic && !int_fits_type_p (value, integer_type_node))
5634 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5635 /* XXX This causes -pedantic to change the meaning of the program.
5636 Remove? -zw 2004-03-15 */
5637 value = convert (integer_type_node, value);
5640 /* Set basis for default for next value. */
5641 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5642 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5644 /* Now create a declaration for the enum value name. */
5646 type = TREE_TYPE (value);
5647 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5648 TYPE_PRECISION (integer_type_node)),
5649 (TYPE_PRECISION (type)
5650 >= TYPE_PRECISION (integer_type_node)
5651 && TYPE_UNSIGNED (type)));
5653 decl = build_decl (CONST_DECL, name, type);
5654 DECL_INITIAL (decl) = convert (type, value);
5655 pushdecl (decl);
5657 return tree_cons (decl, value, NULL_TREE);
5661 /* Create the FUNCTION_DECL for a function definition.
5662 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5663 the declaration; they describe the function's name and the type it returns,
5664 but twisted together in a fashion that parallels the syntax of C.
5666 This function creates a binding context for the function body
5667 as well as setting up the FUNCTION_DECL in current_function_decl.
5669 Returns 1 on success. If the DECLARATOR is not suitable for a function
5670 (it defines a datum instead), we return 0, which tells
5671 yyparse to report a parse error. */
5674 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5675 tree attributes)
5677 tree decl1, old_decl;
5678 tree restype, resdecl;
5680 current_function_returns_value = 0; /* Assume, until we see it does. */
5681 current_function_returns_null = 0;
5682 current_function_returns_abnormally = 0;
5683 warn_about_return_type = 0;
5684 current_extern_inline = 0;
5685 c_switch_stack = NULL;
5687 /* Indicate no valid break/continue context by setting these variables
5688 to some non-null, non-label value. We'll notice and emit the proper
5689 error message in c_finish_bc_stmt. */
5690 c_break_label = c_cont_label = size_zero_node;
5692 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5694 /* If the declarator is not suitable for a function definition,
5695 cause a syntax error. */
5696 if (decl1 == 0)
5697 return 0;
5699 decl_attributes (&decl1, attributes, 0);
5701 if (DECL_DECLARED_INLINE_P (decl1)
5702 && DECL_UNINLINABLE (decl1)
5703 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5704 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5706 announce_function (decl1);
5708 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5710 error ("return type is an incomplete type");
5711 /* Make it return void instead. */
5712 TREE_TYPE (decl1)
5713 = build_function_type (void_type_node,
5714 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5717 if (warn_about_return_type)
5718 pedwarn_c99 ("return type defaults to %<int%>");
5720 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5721 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5722 DECL_INITIAL (decl1) = error_mark_node;
5724 /* If this definition isn't a prototype and we had a prototype declaration
5725 before, copy the arg type info from that prototype. */
5726 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5727 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5728 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5729 TREE_TYPE (TREE_TYPE (old_decl)))
5730 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5732 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5733 TREE_TYPE (decl1));
5734 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5737 /* Optionally warn of old-fashioned def with no previous prototype. */
5738 if (warn_strict_prototypes
5739 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5740 && C_DECL_ISNT_PROTOTYPE (old_decl))
5741 warning ("function declaration isn%'t a prototype");
5742 /* Optionally warn of any global def with no previous prototype. */
5743 else if (warn_missing_prototypes
5744 && TREE_PUBLIC (decl1)
5745 && !MAIN_NAME_P (DECL_NAME (decl1))
5746 && C_DECL_ISNT_PROTOTYPE (old_decl))
5747 warning ("%Jno previous prototype for %qD", decl1, decl1);
5748 /* Optionally warn of any def with no previous prototype
5749 if the function has already been used. */
5750 else if (warn_missing_prototypes
5751 && old_decl != 0 && TREE_USED (old_decl)
5752 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5753 warning ("%J%qD was used with no prototype before its definition",
5754 decl1, decl1);
5755 /* Optionally warn of any global def with no previous declaration. */
5756 else if (warn_missing_declarations
5757 && TREE_PUBLIC (decl1)
5758 && old_decl == 0
5759 && !MAIN_NAME_P (DECL_NAME (decl1)))
5760 warning ("%Jno previous declaration for %qD", decl1, decl1);
5761 /* Optionally warn of any def with no previous declaration
5762 if the function has already been used. */
5763 else if (warn_missing_declarations
5764 && old_decl != 0 && TREE_USED (old_decl)
5765 && C_DECL_IMPLICIT (old_decl))
5766 warning ("%J%qD was used with no declaration before its definition",
5767 decl1, decl1);
5769 /* This is a definition, not a reference.
5770 So normally clear DECL_EXTERNAL.
5771 However, `extern inline' acts like a declaration
5772 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5773 DECL_EXTERNAL (decl1) = current_extern_inline;
5775 /* This function exists in static storage.
5776 (This does not mean `static' in the C sense!) */
5777 TREE_STATIC (decl1) = 1;
5779 /* A nested function is not global. */
5780 if (current_function_decl != 0)
5781 TREE_PUBLIC (decl1) = 0;
5783 /* This is the earliest point at which we might know the assembler
5784 name of the function. Thus, if it's set before this, die horribly. */
5785 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5787 /* If #pragma weak was used, mark the decl weak now. */
5788 if (current_scope == file_scope)
5789 maybe_apply_pragma_weak (decl1);
5791 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5792 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5794 tree args;
5795 int argct = 0;
5797 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5798 != integer_type_node)
5799 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5801 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5802 args = TREE_CHAIN (args))
5804 tree type = args ? TREE_VALUE (args) : 0;
5806 if (type == void_type_node)
5807 break;
5809 ++argct;
5810 switch (argct)
5812 case 1:
5813 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5814 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5815 decl1, decl1);
5816 break;
5818 case 2:
5819 if (TREE_CODE (type) != POINTER_TYPE
5820 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5821 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5822 != char_type_node))
5823 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5824 decl1, decl1);
5825 break;
5827 case 3:
5828 if (TREE_CODE (type) != POINTER_TYPE
5829 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5830 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5831 != char_type_node))
5832 pedwarn ("%Jthird argument of %qD should probably be "
5833 "%<char **%>", decl1, decl1);
5834 break;
5838 /* It is intentional that this message does not mention the third
5839 argument because it's only mentioned in an appendix of the
5840 standard. */
5841 if (argct > 0 && (argct < 2 || argct > 3))
5842 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5844 if (!TREE_PUBLIC (decl1))
5845 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5848 /* Record the decl so that the function name is defined.
5849 If we already have a decl for this name, and it is a FUNCTION_DECL,
5850 use the old decl. */
5852 current_function_decl = pushdecl (decl1);
5854 push_scope ();
5855 declare_parm_level ();
5857 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5858 /* Promote the value to int before returning it. */
5859 if (c_promoting_integer_type_p (restype))
5861 /* It retains unsignedness if not really getting wider. */
5862 if (TYPE_UNSIGNED (restype)
5863 && (TYPE_PRECISION (restype)
5864 == TYPE_PRECISION (integer_type_node)))
5865 restype = unsigned_type_node;
5866 else
5867 restype = integer_type_node;
5870 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5871 DECL_ARTIFICIAL (resdecl) = 1;
5872 DECL_IGNORED_P (resdecl) = 1;
5873 DECL_RESULT (current_function_decl) = resdecl;
5875 start_fname_decls ();
5877 return 1;
5880 /* Subroutine of store_parm_decls which handles new-style function
5881 definitions (prototype format). The parms already have decls, so we
5882 need only record them as in effect and complain if any redundant
5883 old-style parm decls were written. */
5884 static void
5885 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5887 tree decl;
5889 if (current_scope->bindings)
5891 error ("%Jold-style parameter declarations in prototyped "
5892 "function definition", fndecl);
5894 /* Get rid of the old-style declarations. */
5895 pop_scope ();
5896 push_scope ();
5898 /* Don't issue this warning for nested functions, and don't issue this
5899 warning if we got here because ARG_INFO_TYPES was error_mark_node
5900 (this happens when a function definition has just an ellipsis in
5901 its parameter list). */
5902 else if (warn_traditional && !in_system_header && !current_function_scope
5903 && arg_info->types != error_mark_node)
5904 warning ("%Jtraditional C rejects ISO C style function definitions",
5905 fndecl);
5907 /* Now make all the parameter declarations visible in the function body.
5908 We can bypass most of the grunt work of pushdecl. */
5909 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5911 DECL_CONTEXT (decl) = current_function_decl;
5912 if (DECL_NAME (decl))
5913 bind (DECL_NAME (decl), decl, current_scope,
5914 /*invisible=*/false, /*nested=*/false);
5915 else
5916 error ("%Jparameter name omitted", decl);
5919 /* Record the parameter list in the function declaration. */
5920 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5922 /* Now make all the ancillary declarations visible, likewise. */
5923 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5925 DECL_CONTEXT (decl) = current_function_decl;
5926 if (DECL_NAME (decl))
5927 bind (DECL_NAME (decl), decl, current_scope,
5928 /*invisible=*/false, /*nested=*/false);
5931 /* And all the tag declarations. */
5932 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5933 if (TREE_PURPOSE (decl))
5934 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5935 /*invisible=*/false, /*nested=*/false);
5938 /* Subroutine of store_parm_decls which handles old-style function
5939 definitions (separate parameter list and declarations). */
5941 static void
5942 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5944 struct c_binding *b;
5945 tree parm, decl, last;
5946 tree parmids = arg_info->parms;
5948 /* We use DECL_WEAK as a flag to show which parameters have been
5949 seen already, since it is not used on PARM_DECL. */
5950 #ifdef ENABLE_CHECKING
5951 for (b = current_scope->bindings; b; b = b->prev)
5952 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5953 #endif
5955 if (warn_old_style_definition && !in_system_header)
5956 warning ("%Jold-style function definition", fndecl);
5958 /* Match each formal parameter name with its declaration. Save each
5959 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5960 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5962 if (TREE_VALUE (parm) == 0)
5964 error ("%Jparameter name missing from parameter list", fndecl);
5965 TREE_PURPOSE (parm) = 0;
5966 continue;
5969 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5970 if (b && B_IN_CURRENT_SCOPE (b))
5972 decl = b->decl;
5973 /* If we got something other than a PARM_DECL it is an error. */
5974 if (TREE_CODE (decl) != PARM_DECL)
5975 error ("%J%qD declared as a non-parameter", decl, decl);
5976 /* If the declaration is already marked, we have a duplicate
5977 name. Complain and ignore the duplicate. */
5978 else if (DECL_WEAK (decl))
5980 error ("%Jmultiple parameters named %qD", decl, decl);
5981 TREE_PURPOSE (parm) = 0;
5982 continue;
5984 /* If the declaration says "void", complain and turn it into
5985 an int. */
5986 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5988 error ("%Jparameter %qD declared with void type", decl, decl);
5989 TREE_TYPE (decl) = integer_type_node;
5990 DECL_ARG_TYPE (decl) = integer_type_node;
5991 layout_decl (decl, 0);
5994 /* If no declaration found, default to int. */
5995 else
5997 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5998 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5999 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6000 pushdecl (decl);
6002 if (flag_isoc99)
6003 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6004 else if (extra_warnings)
6005 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
6008 TREE_PURPOSE (parm) = decl;
6009 DECL_WEAK (decl) = 1;
6012 /* Now examine the parms chain for incomplete declarations
6013 and declarations with no corresponding names. */
6015 for (b = current_scope->bindings; b; b = b->prev)
6017 parm = b->decl;
6018 if (TREE_CODE (parm) != PARM_DECL)
6019 continue;
6021 if (TREE_TYPE (parm) != error_mark_node
6022 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6024 error ("%Jparameter %qD has incomplete type", parm, parm);
6025 TREE_TYPE (parm) = error_mark_node;
6028 if (!DECL_WEAK (parm))
6030 error ("%Jdeclaration for parameter %qD but no such parameter",
6031 parm, parm);
6033 /* Pretend the parameter was not missing.
6034 This gets us to a standard state and minimizes
6035 further error messages. */
6036 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6040 /* Chain the declarations together in the order of the list of
6041 names. Store that chain in the function decl, replacing the
6042 list of names. Update the current scope to match. */
6043 DECL_ARGUMENTS (fndecl) = 0;
6045 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6046 if (TREE_PURPOSE (parm))
6047 break;
6048 if (parm && TREE_PURPOSE (parm))
6050 last = TREE_PURPOSE (parm);
6051 DECL_ARGUMENTS (fndecl) = last;
6052 DECL_WEAK (last) = 0;
6054 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6055 if (TREE_PURPOSE (parm))
6057 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6058 last = TREE_PURPOSE (parm);
6059 DECL_WEAK (last) = 0;
6061 TREE_CHAIN (last) = 0;
6064 /* If there was a previous prototype,
6065 set the DECL_ARG_TYPE of each argument according to
6066 the type previously specified, and report any mismatches. */
6068 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6070 tree type;
6071 for (parm = DECL_ARGUMENTS (fndecl),
6072 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6073 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6074 != void_type_node));
6075 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6077 if (parm == 0 || type == 0
6078 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6080 error ("number of arguments doesn%'t match prototype");
6081 error ("%Hprototype declaration",
6082 &current_function_prototype_locus);
6083 break;
6085 /* Type for passing arg must be consistent with that
6086 declared for the arg. ISO C says we take the unqualified
6087 type for parameters declared with qualified type. */
6088 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6089 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6091 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6092 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6094 /* Adjust argument to match prototype. E.g. a previous
6095 `int foo(float);' prototype causes
6096 `int foo(x) float x; {...}' to be treated like
6097 `int foo(float x) {...}'. This is particularly
6098 useful for argument types like uid_t. */
6099 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6101 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6102 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6103 && TYPE_PRECISION (TREE_TYPE (parm))
6104 < TYPE_PRECISION (integer_type_node))
6105 DECL_ARG_TYPE (parm) = integer_type_node;
6107 if (pedantic)
6109 pedwarn ("promoted argument %qD "
6110 "doesn%'t match prototype", parm);
6111 pedwarn ("%Hprototype declaration",
6112 &current_function_prototype_locus);
6115 else
6117 error ("argument %qD doesn%'t match prototype", parm);
6118 error ("%Hprototype declaration",
6119 &current_function_prototype_locus);
6123 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6126 /* Otherwise, create a prototype that would match. */
6128 else
6130 tree actual = 0, last = 0, type;
6132 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6134 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6135 if (last)
6136 TREE_CHAIN (last) = type;
6137 else
6138 actual = type;
6139 last = type;
6141 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6142 if (last)
6143 TREE_CHAIN (last) = type;
6144 else
6145 actual = type;
6147 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6148 of the type of this function, but we need to avoid having this
6149 affect the types of other similarly-typed functions, so we must
6150 first force the generation of an identical (but separate) type
6151 node for the relevant function type. The new node we create
6152 will be a variant of the main variant of the original function
6153 type. */
6155 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6157 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6161 /* Store parameter declarations passed in ARG_INFO into the current
6162 function declaration. */
6164 void
6165 store_parm_decls_from (struct c_arg_info *arg_info)
6167 current_function_arg_info = arg_info;
6168 store_parm_decls ();
6171 /* Store the parameter declarations into the current function declaration.
6172 This is called after parsing the parameter declarations, before
6173 digesting the body of the function.
6175 For an old-style definition, construct a prototype out of the old-style
6176 parameter declarations and inject it into the function's type. */
6178 void
6179 store_parm_decls (void)
6181 tree fndecl = current_function_decl;
6182 bool proto;
6184 /* The argument information block for FNDECL. */
6185 struct c_arg_info *arg_info = current_function_arg_info;
6186 current_function_arg_info = 0;
6188 /* True if this definition is written with a prototype. Note:
6189 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6190 list in a function definition as equivalent to (void) -- an
6191 empty argument list specifies the function has no parameters,
6192 but only (void) sets up a prototype for future calls. */
6193 proto = arg_info->types != 0;
6195 if (proto)
6196 store_parm_decls_newstyle (fndecl, arg_info);
6197 else
6198 store_parm_decls_oldstyle (fndecl, arg_info);
6200 /* The next call to push_scope will be a function body. */
6202 next_is_function_body = true;
6204 /* Write a record describing this function definition to the prototypes
6205 file (if requested). */
6207 gen_aux_info_record (fndecl, 1, 0, proto);
6209 /* Initialize the RTL code for the function. */
6210 allocate_struct_function (fndecl);
6212 /* Begin the statement tree for this function. */
6213 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6215 /* ??? Insert the contents of the pending sizes list into the function
6216 to be evaluated. This just changes mis-behavior until assign_parms
6217 phase ordering problems are resolved. */
6219 tree t;
6220 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6221 add_stmt (TREE_VALUE (t));
6224 /* Even though we're inside a function body, we still don't want to
6225 call expand_expr to calculate the size of a variable-sized array.
6226 We haven't necessarily assigned RTL to all variables yet, so it's
6227 not safe to try to expand expressions involving them. */
6228 cfun->x_dont_save_pending_sizes_p = 1;
6231 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6232 functions. */
6234 static void
6235 c_warn_unused_result_recursively (tree fndecl)
6237 struct cgraph_node *cgn;
6239 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6240 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6242 /* Finalize all nested functions now. */
6243 cgn = cgraph_node (fndecl);
6244 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6245 c_warn_unused_result_recursively (cgn->decl);
6248 /* Finish up a function declaration and compile that function
6249 all the way to assembler language output. The free the storage
6250 for the function definition.
6252 This is called after parsing the body of the function definition. */
6254 void
6255 finish_function (void)
6257 tree fndecl = current_function_decl;
6259 if (TREE_CODE (fndecl) == FUNCTION_DECL
6260 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6262 tree args = DECL_ARGUMENTS (fndecl);
6263 for (; args; args = TREE_CHAIN (args))
6265 tree type = TREE_TYPE (args);
6266 if (INTEGRAL_TYPE_P (type)
6267 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6268 DECL_ARG_TYPE (args) = integer_type_node;
6272 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6273 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6275 /* Must mark the RESULT_DECL as being in this function. */
6277 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6278 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6280 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6282 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6283 != integer_type_node)
6285 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6286 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6287 if (!warn_main)
6288 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6290 else
6292 if (flag_isoc99)
6293 c_finish_return (integer_zero_node);
6297 /* Tie off the statement tree for this function. */
6298 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6300 finish_fname_decls ();
6302 /* Complain if there's just no return statement. */
6303 if (warn_return_type
6304 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6305 && !current_function_returns_value && !current_function_returns_null
6306 /* Don't complain if we abort. */
6307 && !current_function_returns_abnormally
6308 /* Don't warn for main(). */
6309 && !MAIN_NAME_P (DECL_NAME (fndecl))
6310 /* Or if they didn't actually specify a return type. */
6311 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6312 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6313 inline function, as we might never be compiled separately. */
6314 && DECL_INLINE (fndecl))
6315 warning ("no return statement in function returning non-void");
6317 /* With just -Wextra, complain only if function returns both with
6318 and without a value. */
6319 if (extra_warnings
6320 && current_function_returns_value
6321 && current_function_returns_null)
6322 warning ("this function may return with or without a value");
6324 /* Store the end of the function, so that we get good line number
6325 info for the epilogue. */
6326 cfun->function_end_locus = input_location;
6328 /* If we don't have ctors/dtors sections, and this is a static
6329 constructor or destructor, it must be recorded now. */
6330 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6331 && !targetm.have_ctors_dtors)
6332 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6333 if (DECL_STATIC_DESTRUCTOR (fndecl)
6334 && !targetm.have_ctors_dtors)
6335 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6337 /* Finalize the ELF visibility for the function. */
6338 c_determine_visibility (fndecl);
6340 /* Genericize before inlining. Delay genericizing nested functions
6341 until their parent function is genericized. Since finalizing
6342 requires GENERIC, delay that as well. */
6344 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6346 if (!decl_function_context (fndecl))
6348 c_genericize (fndecl);
6349 c_warn_unused_result_recursively (fndecl);
6351 /* ??? Objc emits functions after finalizing the compilation unit.
6352 This should be cleaned up later and this conditional removed. */
6353 if (cgraph_global_info_ready)
6355 c_expand_body (fndecl);
6356 return;
6359 cgraph_finalize_function (fndecl, false);
6361 else
6363 /* Register this function with cgraph just far enough to get it
6364 added to our parent's nested function list. Handy, since the
6365 C front end doesn't have such a list. */
6366 (void) cgraph_node (fndecl);
6370 /* We're leaving the context of this function, so zap cfun.
6371 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6372 tree_rest_of_compilation. */
6373 cfun = NULL;
6374 current_function_decl = NULL;
6377 /* Generate the RTL for the body of FNDECL. */
6379 void
6380 c_expand_body (tree fndecl)
6383 if (!DECL_INITIAL (fndecl)
6384 || DECL_INITIAL (fndecl) == error_mark_node)
6385 return;
6387 tree_rest_of_compilation (fndecl);
6389 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6390 && targetm.have_ctors_dtors)
6391 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6392 DEFAULT_INIT_PRIORITY);
6393 if (DECL_STATIC_DESTRUCTOR (fndecl)
6394 && targetm.have_ctors_dtors)
6395 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6396 DEFAULT_INIT_PRIORITY);
6399 /* Check the declarations given in a for-loop for satisfying the C99
6400 constraints. */
6401 void
6402 check_for_loop_decls (void)
6404 struct c_binding *b;
6406 if (!flag_isoc99)
6408 /* If we get here, declarations have been used in a for loop without
6409 the C99 for loop scope. This doesn't make much sense, so don't
6410 allow it. */
6411 error ("%<for%> loop initial declaration used outside C99 mode");
6412 return;
6414 /* C99 subclause 6.8.5 paragraph 3:
6416 [#3] The declaration part of a for statement shall only
6417 declare identifiers for objects having storage class auto or
6418 register.
6420 It isn't clear whether, in this sentence, "identifiers" binds to
6421 "shall only declare" or to "objects" - that is, whether all identifiers
6422 declared must be identifiers for objects, or whether the restriction
6423 only applies to those that are. (A question on this in comp.std.c
6424 in November 2000 received no answer.) We implement the strictest
6425 interpretation, to avoid creating an extension which later causes
6426 problems. */
6428 for (b = current_scope->bindings; b; b = b->prev)
6430 tree id = b->id;
6431 tree decl = b->decl;
6433 if (!id)
6434 continue;
6436 switch (TREE_CODE (decl))
6438 case VAR_DECL:
6439 if (TREE_STATIC (decl))
6440 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6441 "initial declaration", decl, decl);
6442 else if (DECL_EXTERNAL (decl))
6443 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6444 "initial declaration", decl, decl);
6445 break;
6447 case RECORD_TYPE:
6448 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6449 id);
6450 break;
6451 case UNION_TYPE:
6452 error ("%<union %E%> declared in %<for%> loop initial declaration",
6453 id);
6454 break;
6455 case ENUMERAL_TYPE:
6456 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6457 id);
6458 break;
6459 default:
6460 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6461 "initial declaration", decl, decl);
6466 /* Save and reinitialize the variables
6467 used during compilation of a C function. */
6469 void
6470 c_push_function_context (struct function *f)
6472 struct language_function *p;
6473 p = GGC_NEW (struct language_function);
6474 f->language = p;
6476 p->base.x_stmt_tree = c_stmt_tree;
6477 p->x_break_label = c_break_label;
6478 p->x_cont_label = c_cont_label;
6479 p->x_switch_stack = c_switch_stack;
6480 p->arg_info = current_function_arg_info;
6481 p->returns_value = current_function_returns_value;
6482 p->returns_null = current_function_returns_null;
6483 p->returns_abnormally = current_function_returns_abnormally;
6484 p->warn_about_return_type = warn_about_return_type;
6485 p->extern_inline = current_extern_inline;
6488 /* Restore the variables used during compilation of a C function. */
6490 void
6491 c_pop_function_context (struct function *f)
6493 struct language_function *p = f->language;
6495 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6496 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6498 /* Stop pointing to the local nodes about to be freed. */
6499 /* But DECL_INITIAL must remain nonzero so we know this
6500 was an actual function definition. */
6501 DECL_INITIAL (current_function_decl) = error_mark_node;
6502 DECL_ARGUMENTS (current_function_decl) = 0;
6505 c_stmt_tree = p->base.x_stmt_tree;
6506 c_break_label = p->x_break_label;
6507 c_cont_label = p->x_cont_label;
6508 c_switch_stack = p->x_switch_stack;
6509 current_function_arg_info = p->arg_info;
6510 current_function_returns_value = p->returns_value;
6511 current_function_returns_null = p->returns_null;
6512 current_function_returns_abnormally = p->returns_abnormally;
6513 warn_about_return_type = p->warn_about_return_type;
6514 current_extern_inline = p->extern_inline;
6516 f->language = NULL;
6519 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6521 void
6522 c_dup_lang_specific_decl (tree decl)
6524 struct lang_decl *ld;
6526 if (!DECL_LANG_SPECIFIC (decl))
6527 return;
6529 ld = GGC_NEW (struct lang_decl);
6530 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6531 DECL_LANG_SPECIFIC (decl) = ld;
6534 /* The functions below are required for functionality of doing
6535 function at once processing in the C front end. Currently these
6536 functions are not called from anywhere in the C front end, but as
6537 these changes continue, that will change. */
6539 /* Returns nonzero if the current statement is a full expression,
6540 i.e. temporaries created during that statement should be destroyed
6541 at the end of the statement. */
6544 stmts_are_full_exprs_p (void)
6546 return 0;
6549 /* Returns the stmt_tree (if any) to which statements are currently
6550 being added. If there is no active statement-tree, NULL is
6551 returned. */
6553 stmt_tree
6554 current_stmt_tree (void)
6556 return &c_stmt_tree;
6559 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6560 C. */
6563 anon_aggr_type_p (tree ARG_UNUSED (node))
6565 return 0;
6568 /* Return the global value of T as a symbol. */
6570 tree
6571 identifier_global_value (tree t)
6573 struct c_binding *b;
6575 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6576 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6577 return b->decl;
6579 return 0;
6582 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6583 otherwise the name is found in ridpointers from RID_INDEX. */
6585 void
6586 record_builtin_type (enum rid rid_index, const char *name, tree type)
6588 tree id, decl;
6589 if (name == 0)
6590 id = ridpointers[(int) rid_index];
6591 else
6592 id = get_identifier (name);
6593 decl = build_decl (TYPE_DECL, id, type);
6594 pushdecl (decl);
6595 if (debug_hooks->type_decl)
6596 debug_hooks->type_decl (decl, false);
6599 /* Build the void_list_node (void_type_node having been created). */
6600 tree
6601 build_void_list_node (void)
6603 tree t = build_tree_list (NULL_TREE, void_type_node);
6604 return t;
6607 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6609 struct c_parm *
6610 build_c_parm (struct c_declspecs *specs, tree attrs,
6611 struct c_declarator *declarator)
6613 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6614 ret->specs = specs;
6615 ret->attrs = attrs;
6616 ret->declarator = declarator;
6617 return ret;
6620 /* Return a declarator with nested attributes. TARGET is the inner
6621 declarator to which these attributes apply. ATTRS are the
6622 attributes. */
6624 struct c_declarator *
6625 build_attrs_declarator (tree attrs, struct c_declarator *target)
6627 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6628 ret->kind = cdk_attrs;
6629 ret->declarator = target;
6630 ret->u.attrs = attrs;
6631 return ret;
6634 /* Return a declarator for a function with arguments specified by ARGS
6635 and return type specified by TARGET. */
6637 struct c_declarator *
6638 build_function_declarator (struct c_arg_info *args,
6639 struct c_declarator *target)
6641 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6642 ret->kind = cdk_function;
6643 ret->declarator = target;
6644 ret->u.arg_info = args;
6645 return ret;
6648 /* Return a declarator for the identifier IDENT (which may be
6649 NULL_TREE for an abstract declarator). */
6651 struct c_declarator *
6652 build_id_declarator (tree ident)
6654 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6655 ret->kind = cdk_id;
6656 ret->declarator = 0;
6657 ret->u.id = ident;
6658 return ret;
6661 /* Return something to represent absolute declarators containing a *.
6662 TARGET is the absolute declarator that the * contains.
6663 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6664 to apply to the pointer type. */
6666 struct c_declarator *
6667 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6668 struct c_declarator *target)
6670 tree attrs;
6671 int quals = 0;
6672 struct c_declarator *itarget = target;
6673 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6674 if (type_quals_attrs)
6676 attrs = type_quals_attrs->attrs;
6677 quals = quals_from_declspecs (type_quals_attrs);
6678 if (attrs != NULL_TREE)
6679 itarget = build_attrs_declarator (attrs, target);
6681 ret->kind = cdk_pointer;
6682 ret->declarator = itarget;
6683 ret->u.pointer_quals = quals;
6684 return ret;
6687 /* Return a pointer to a structure for an empty list of declaration
6688 specifiers. */
6690 struct c_declspecs *
6691 build_null_declspecs (void)
6693 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6694 ret->type = 0;
6695 ret->decl_attr = 0;
6696 ret->attrs = 0;
6697 ret->typespec_word = cts_none;
6698 ret->storage_class = csc_none;
6699 ret->non_sc_seen_p = false;
6700 ret->typedef_p = false;
6701 ret->tag_defined_p = false;
6702 ret->explicit_signed_p = false;
6703 ret->deprecated_p = false;
6704 ret->default_int_p = false;
6705 ret->long_p = false;
6706 ret->long_long_p = false;
6707 ret->short_p = false;
6708 ret->signed_p = false;
6709 ret->unsigned_p = false;
6710 ret->complex_p = false;
6711 ret->inline_p = false;
6712 ret->thread_p = false;
6713 ret->const_p = false;
6714 ret->volatile_p = false;
6715 ret->restrict_p = false;
6716 return ret;
6719 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6720 returning SPECS. */
6722 struct c_declspecs *
6723 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6725 enum rid i;
6726 bool dupe = false;
6727 specs->non_sc_seen_p = true;
6728 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6729 && C_IS_RESERVED_WORD (qual));
6730 i = C_RID_CODE (qual);
6731 switch (i)
6733 case RID_CONST:
6734 dupe = specs->const_p;
6735 specs->const_p = true;
6736 break;
6737 case RID_VOLATILE:
6738 dupe = specs->volatile_p;
6739 specs->volatile_p = true;
6740 break;
6741 case RID_RESTRICT:
6742 dupe = specs->restrict_p;
6743 specs->restrict_p = true;
6744 break;
6745 default:
6746 gcc_unreachable ();
6748 if (dupe && pedantic && !flag_isoc99)
6749 pedwarn ("duplicate %qs", IDENTIFIER_POINTER (qual));
6750 return specs;
6753 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6754 returning SPECS. */
6756 struct c_declspecs *
6757 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6759 tree type = spec.spec;
6760 specs->non_sc_seen_p = true;
6761 if (TREE_DEPRECATED (type))
6762 specs->deprecated_p = true;
6764 /* Handle type specifier keywords. */
6765 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6767 enum rid i = C_RID_CODE (type);
6768 if (specs->type)
6770 error ("two or more data types in declaration specifiers");
6771 return specs;
6773 if ((int) i <= (int) RID_LAST_MODIFIER)
6775 /* "long", "short", "signed", "unsigned" or "_Complex". */
6776 bool dupe = false;
6777 switch (i)
6779 case RID_LONG:
6780 if (specs->long_long_p)
6782 error ("%<long long long%> is too long for GCC");
6783 break;
6785 if (specs->long_p)
6787 if (specs->typespec_word == cts_double)
6789 error ("both %<long long%> and %<double%> in "
6790 "declaration specifiers");
6791 break;
6793 if (pedantic && !flag_isoc99 && !in_system_header
6794 && warn_long_long)
6795 pedwarn ("ISO C90 does not support %<long long%>");
6796 specs->long_long_p = 1;
6797 break;
6799 if (specs->short_p)
6800 error ("both %<long%> and %<short%> in "
6801 "declaration specifiers");
6802 else if (specs->typespec_word == cts_void)
6803 error ("both %<long%> and %<void%> in "
6804 "declaration specifiers");
6805 else if (specs->typespec_word == cts_bool)
6806 error ("both %<long%> and %<_Bool%> in "
6807 "declaration specifiers");
6808 else if (specs->typespec_word == cts_char)
6809 error ("both %<long%> and %<char%> in "
6810 "declaration specifiers");
6811 else if (specs->typespec_word == cts_float)
6812 error ("both %<long%> and %<float%> in "
6813 "declaration specifiers");
6814 else
6815 specs->long_p = true;
6816 break;
6817 case RID_SHORT:
6818 dupe = specs->short_p;
6819 if (specs->long_p)
6820 error ("both %<long%> and %<short%> in "
6821 "declaration specifiers");
6822 else if (specs->typespec_word == cts_void)
6823 error ("both %<short%> and %<void%> in "
6824 "declaration specifiers");
6825 else if (specs->typespec_word == cts_bool)
6826 error ("both %<short%> and %<_Bool%> in "
6827 "declaration specifiers");
6828 else if (specs->typespec_word == cts_char)
6829 error ("both %<short%> and %<char%> in "
6830 "declaration specifiers");
6831 else if (specs->typespec_word == cts_float)
6832 error ("both %<short%> and %<float%> in "
6833 "declaration specifiers");
6834 else if (specs->typespec_word == cts_double)
6835 error ("both %<short%> and %<double%> in "
6836 "declaration specifiers");
6837 else
6838 specs->short_p = true;
6839 break;
6840 case RID_SIGNED:
6841 dupe = specs->signed_p;
6842 if (specs->unsigned_p)
6843 error ("both %<signed%> and %<unsigned%> in "
6844 "declaration specifiers");
6845 else if (specs->typespec_word == cts_void)
6846 error ("both %<signed%> and %<void%> in "
6847 "declaration specifiers");
6848 else if (specs->typespec_word == cts_bool)
6849 error ("both %<signed%> and %<_Bool%> in "
6850 "declaration specifiers");
6851 else if (specs->typespec_word == cts_float)
6852 error ("both %<signed%> and %<float%> in "
6853 "declaration specifiers");
6854 else if (specs->typespec_word == cts_double)
6855 error ("both %<signed%> and %<double%> in "
6856 "declaration specifiers");
6857 else
6858 specs->signed_p = true;
6859 break;
6860 case RID_UNSIGNED:
6861 dupe = specs->unsigned_p;
6862 if (specs->signed_p)
6863 error ("both %<signed%> and %<unsigned%> in "
6864 "declaration specifiers");
6865 else if (specs->typespec_word == cts_void)
6866 error ("both %<unsigned%> and %<void%> in "
6867 "declaration specifiers");
6868 else if (specs->typespec_word == cts_bool)
6869 error ("both %<unsigned%> and %<_Bool%> in "
6870 "declaration specifiers");
6871 else if (specs->typespec_word == cts_float)
6872 error ("both %<unsigned%> and %<float%> in "
6873 "declaration specifiers");
6874 else if (specs->typespec_word == cts_double)
6875 error ("both %<unsigned%> and %<double%> in "
6876 "declaration specifiers");
6877 else
6878 specs->unsigned_p = true;
6879 break;
6880 case RID_COMPLEX:
6881 dupe = specs->complex_p;
6882 if (pedantic && !flag_isoc99 && !in_system_header)
6883 pedwarn ("ISO C90 does not support complex types");
6884 if (specs->typespec_word == cts_void)
6885 error ("both %<complex%> and %<void%> in "
6886 "declaration specifiers");
6887 else if (specs->typespec_word == cts_bool)
6888 error ("both %<complex%> and %<_Bool%> in "
6889 "declaration specifiers");
6890 else
6891 specs->complex_p = true;
6892 break;
6893 default:
6894 gcc_unreachable ();
6897 if (dupe)
6898 error ("duplicate %qs", IDENTIFIER_POINTER (type));
6900 return specs;
6902 else
6904 /* "void", "_Bool", "char", "int", "float" or "double". */
6905 if (specs->typespec_word != cts_none)
6907 error ("two or more data types in declaration specifiers");
6908 return specs;
6910 switch (i)
6912 case RID_VOID:
6913 if (specs->long_p)
6914 error ("both %<long%> and %<void%> in "
6915 "declaration specifiers");
6916 else if (specs->short_p)
6917 error ("both %<short%> and %<void%> in "
6918 "declaration specifiers");
6919 else if (specs->signed_p)
6920 error ("both %<signed%> and %<void%> in "
6921 "declaration specifiers");
6922 else if (specs->unsigned_p)
6923 error ("both %<unsigned%> and %<void%> in "
6924 "declaration specifiers");
6925 else if (specs->complex_p)
6926 error ("both %<complex%> and %<void%> in "
6927 "declaration specifiers");
6928 else
6929 specs->typespec_word = cts_void;
6930 return specs;
6931 case RID_BOOL:
6932 if (specs->long_p)
6933 error ("both %<long%> and %<_Bool%> in "
6934 "declaration specifiers");
6935 else if (specs->short_p)
6936 error ("both %<short%> and %<_Bool%> in "
6937 "declaration specifiers");
6938 else if (specs->signed_p)
6939 error ("both %<signed%> and %<_Bool%> in "
6940 "declaration specifiers");
6941 else if (specs->unsigned_p)
6942 error ("both %<unsigned%> and %<_Bool%> in "
6943 "declaration specifiers");
6944 else if (specs->complex_p)
6945 error ("both %<complex%> and %<_Bool%> in "
6946 "declaration specifiers");
6947 else
6948 specs->typespec_word = cts_bool;
6949 return specs;
6950 case RID_CHAR:
6951 if (specs->long_p)
6952 error ("both %<long%> and %<char%> in "
6953 "declaration specifiers");
6954 else if (specs->short_p)
6955 error ("both %<short%> and %<char%> in "
6956 "declaration specifiers");
6957 else
6958 specs->typespec_word = cts_char;
6959 return specs;
6960 case RID_INT:
6961 specs->typespec_word = cts_int;
6962 return specs;
6963 case RID_FLOAT:
6964 if (specs->long_p)
6965 error ("both %<long%> and %<float%> in "
6966 "declaration specifiers");
6967 else if (specs->short_p)
6968 error ("both %<short%> and %<float%> in "
6969 "declaration specifiers");
6970 else if (specs->signed_p)
6971 error ("both %<signed%> and %<float%> in "
6972 "declaration specifiers");
6973 else if (specs->unsigned_p)
6974 error ("both %<unsigned%> and %<float%> in "
6975 "declaration specifiers");
6976 else
6977 specs->typespec_word = cts_float;
6978 return specs;
6979 case RID_DOUBLE:
6980 if (specs->long_long_p)
6981 error ("both %<long long%> and %<double%> in "
6982 "declaration specifiers");
6983 else if (specs->short_p)
6984 error ("both %<short%> and %<double%> in "
6985 "declaration specifiers");
6986 else if (specs->signed_p)
6987 error ("both %<signed%> and %<double%> in "
6988 "declaration specifiers");
6989 else if (specs->unsigned_p)
6990 error ("both %<unsigned%> and %<double%> in "
6991 "declaration specifiers");
6992 else
6993 specs->typespec_word = cts_double;
6994 return specs;
6995 default:
6996 /* ObjC reserved word "id", handled below. */
6997 break;
7002 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7003 form of ObjC type, cases such as "int" and "long" being handled
7004 above), a TYPE (struct, union, enum and typeof specifiers) or an
7005 ERROR_MARK. In none of these cases may there have previously
7006 been any type specifiers. */
7007 if (specs->type || specs->typespec_word != cts_none
7008 || specs->long_p || specs->short_p || specs->signed_p
7009 || specs->unsigned_p || specs->complex_p)
7010 error ("two or more data types in declaration specifiers");
7011 else if (TREE_CODE (type) == TYPE_DECL)
7013 if (TREE_TYPE (type) == error_mark_node)
7014 ; /* Allow the type to default to int to avoid cascading errors. */
7015 else
7017 specs->type = TREE_TYPE (type);
7018 specs->decl_attr = DECL_ATTRIBUTES (type);
7019 specs->typedef_p = true;
7020 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7023 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7025 tree t = lookup_name (type);
7026 if (!t || TREE_CODE (t) != TYPE_DECL)
7027 error ("%qs fails to be a typedef or built in type",
7028 IDENTIFIER_POINTER (type));
7029 else if (TREE_TYPE (t) == error_mark_node)
7031 else
7032 specs->type = TREE_TYPE (t);
7034 else if (TREE_CODE (type) != ERROR_MARK)
7036 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7037 specs->tag_defined_p = true;
7038 if (spec.kind == ctsk_typeof)
7039 specs->typedef_p = true;
7040 specs->type = type;
7043 return specs;
7046 /* Add the storage class specifier or function specifier SCSPEC to the
7047 declaration specifiers SPECS, returning SPECS. */
7049 struct c_declspecs *
7050 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7052 enum rid i;
7053 enum c_storage_class n = csc_none;
7054 bool dupe = false;
7055 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7056 && C_IS_RESERVED_WORD (scspec));
7057 i = C_RID_CODE (scspec);
7058 if (extra_warnings && specs->non_sc_seen_p)
7059 warning ("%qs is not at beginning of declaration",
7060 IDENTIFIER_POINTER (scspec));
7061 switch (i)
7063 case RID_INLINE:
7064 /* C99 permits duplicate inline. Although of doubtful utility,
7065 it seems simplest to permit it in gnu89 mode as well, as
7066 there is also little utility in maintaining this as a
7067 difference between gnu89 and C99 inline. */
7068 dupe = false;
7069 specs->inline_p = true;
7070 break;
7071 case RID_THREAD:
7072 dupe = specs->thread_p;
7073 if (specs->storage_class == csc_auto)
7074 error ("%<__thread%> used with %<auto%>");
7075 else if (specs->storage_class == csc_register)
7076 error ("%<__thread%> used with %<register%>");
7077 else if (specs->storage_class == csc_typedef)
7078 error ("%<__thread%> used with %<typedef%>");
7079 else
7080 specs->thread_p = true;
7081 break;
7082 case RID_AUTO:
7083 n = csc_auto;
7084 break;
7085 case RID_EXTERN:
7086 n = csc_extern;
7087 /* Diagnose "__thread extern". */
7088 if (specs->thread_p)
7089 error ("%<__thread%> before %<extern%>");
7090 break;
7091 case RID_REGISTER:
7092 n = csc_register;
7093 break;
7094 case RID_STATIC:
7095 n = csc_static;
7096 /* Diagnose "__thread static". */
7097 if (specs->thread_p)
7098 error ("%<__thread%> before %<static%>");
7099 break;
7100 case RID_TYPEDEF:
7101 n = csc_typedef;
7102 break;
7103 default:
7104 gcc_unreachable ();
7106 if (n != csc_none && n == specs->storage_class)
7107 dupe = true;
7108 if (dupe)
7109 error ("duplicate %qs", IDENTIFIER_POINTER (scspec));
7110 if (n != csc_none)
7112 if (specs->storage_class != csc_none && n != specs->storage_class)
7114 error ("multiple storage classes in declaration specifiers");
7116 else
7118 specs->storage_class = n;
7119 if (n != csc_extern && n != csc_static && specs->thread_p)
7121 error ("%<__thread%> used with %qs",
7122 IDENTIFIER_POINTER (scspec));
7123 specs->thread_p = false;
7127 return specs;
7130 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7131 returning SPECS. */
7133 struct c_declspecs *
7134 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7136 specs->attrs = chainon (attrs, specs->attrs);
7137 return specs;
7140 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7141 specifiers with any other type specifier to determine the resulting
7142 type. This is where ISO C checks on complex types are made, since
7143 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7144 double". */
7146 struct c_declspecs *
7147 finish_declspecs (struct c_declspecs *specs)
7149 /* If a type was specified as a whole, we have no modifiers and are
7150 done. */
7151 if (specs->type != NULL_TREE)
7153 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7154 && !specs->signed_p && !specs->unsigned_p
7155 && !specs->complex_p);
7156 return specs;
7159 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7160 has been specified, treat it as "int" unless "_Complex" is
7161 present and there are no other specifiers. If we just have
7162 "_Complex", it is equivalent to "_Complex double", but e.g.
7163 "_Complex short" is equivalent to "_Complex short int". */
7164 if (specs->typespec_word == cts_none)
7166 if (specs->long_p || specs->short_p
7167 || specs->signed_p || specs->unsigned_p)
7169 specs->typespec_word = cts_int;
7171 else if (specs->complex_p)
7173 specs->typespec_word = cts_double;
7174 if (pedantic)
7175 pedwarn ("ISO C does not support plain %<complex%> meaning "
7176 "%<double complex%>");
7178 else
7180 specs->typespec_word = cts_int;
7181 specs->default_int_p = true;
7182 /* We don't diagnose this here because grokdeclarator will
7183 give more specific diagnostics according to whether it is
7184 a function definition. */
7188 /* If "signed" was specified, record this to distinguish "int" and
7189 "signed int" in the case of a bit-field with
7190 -funsigned-bitfields. */
7191 specs->explicit_signed_p = specs->signed_p;
7193 /* Now compute the actual type. */
7194 switch (specs->typespec_word)
7196 case cts_void:
7197 gcc_assert (!specs->long_p && !specs->short_p
7198 && !specs->signed_p && !specs->unsigned_p
7199 && !specs->complex_p);
7200 specs->type = void_type_node;
7201 break;
7202 case cts_bool:
7203 gcc_assert (!specs->long_p && !specs->short_p
7204 && !specs->signed_p && !specs->unsigned_p
7205 && !specs->complex_p);
7206 specs->type = boolean_type_node;
7207 break;
7208 case cts_char:
7209 gcc_assert (!specs->long_p && !specs->short_p);
7210 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7211 if (specs->signed_p)
7212 specs->type = signed_char_type_node;
7213 else if (specs->unsigned_p)
7214 specs->type = unsigned_char_type_node;
7215 else
7216 specs->type = char_type_node;
7217 if (specs->complex_p)
7219 if (pedantic)
7220 pedwarn ("ISO C does not support complex integer types");
7221 specs->type = build_complex_type (specs->type);
7223 break;
7224 case cts_int:
7225 gcc_assert (!(specs->long_p && specs->short_p));
7226 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7227 if (specs->long_long_p)
7228 specs->type = (specs->unsigned_p
7229 ? long_long_unsigned_type_node
7230 : long_long_integer_type_node);
7231 else if (specs->long_p)
7232 specs->type = (specs->unsigned_p
7233 ? long_unsigned_type_node
7234 : long_integer_type_node);
7235 else if (specs->short_p)
7236 specs->type = (specs->unsigned_p
7237 ? short_unsigned_type_node
7238 : short_integer_type_node);
7239 else
7240 specs->type = (specs->unsigned_p
7241 ? unsigned_type_node
7242 : integer_type_node);
7243 if (specs->complex_p)
7245 if (pedantic)
7246 pedwarn ("ISO C does not support complex integer types");
7247 specs->type = build_complex_type (specs->type);
7249 break;
7250 case cts_float:
7251 gcc_assert (!specs->long_p && !specs->short_p
7252 && !specs->signed_p && !specs->unsigned_p);
7253 specs->type = (specs->complex_p
7254 ? complex_float_type_node
7255 : float_type_node);
7256 break;
7257 case cts_double:
7258 gcc_assert (!specs->long_long_p && !specs->short_p
7259 && !specs->signed_p && !specs->unsigned_p);
7260 if (specs->long_p)
7262 specs->type = (specs->complex_p
7263 ? complex_long_double_type_node
7264 : long_double_type_node);
7266 else
7268 specs->type = (specs->complex_p
7269 ? complex_double_type_node
7270 : double_type_node);
7272 break;
7273 default:
7274 gcc_unreachable ();
7277 return specs;
7280 /* Synthesize a function which calls all the global ctors or global
7281 dtors in this file. This is only used for targets which do not
7282 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7283 static void
7284 build_cdtor (int method_type, tree cdtors)
7286 tree body = 0;
7288 if (!cdtors)
7289 return;
7291 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7292 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7293 &body);
7295 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7298 /* Perform final processing on one file scope's declarations (or the
7299 external scope's declarations), GLOBALS. */
7300 static void
7301 c_write_global_declarations_1 (tree globals)
7303 size_t len = list_length (globals);
7304 tree *vec = XNEWVEC (tree, len);
7305 size_t i;
7306 tree decl;
7308 /* Process the decls in the order they were written. */
7309 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7311 vec[i] = decl;
7312 /* Check for used but undefined static functions using the C
7313 standard's definition of "used", and set TREE_NO_WARNING so
7314 that check_global_declarations doesn't repeat the check. */
7315 if (TREE_CODE (decl) == FUNCTION_DECL
7316 && DECL_INITIAL (decl) == 0
7317 && DECL_EXTERNAL (decl)
7318 && !TREE_PUBLIC (decl)
7319 && C_DECL_USED (decl))
7321 pedwarn ("%J%qF used but never defined", decl, decl);
7322 TREE_NO_WARNING (decl) = 1;
7326 wrapup_global_declarations (vec, len);
7327 check_global_declarations (vec, len);
7329 free (vec);
7332 void
7333 c_write_global_declarations (void)
7335 tree ext_block, t;
7337 /* We don't want to do this if generating a PCH. */
7338 if (pch_file)
7339 return;
7341 /* Don't waste time on further processing if -fsyntax-only or we've
7342 encountered errors. */
7343 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7344 return;
7346 /* Close the external scope. */
7347 ext_block = pop_scope ();
7348 external_scope = 0;
7349 gcc_assert (!current_scope);
7351 /* Process all file scopes in this compilation, and the external_scope,
7352 through wrapup_global_declarations and check_global_declarations. */
7353 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7354 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7355 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7357 /* Generate functions to call static constructors and destructors
7358 for targets that do not support .ctors/.dtors sections. These
7359 functions have magic names which are detected by collect2. */
7360 build_cdtor ('I', static_ctors); static_ctors = 0;
7361 build_cdtor ('D', static_dtors); static_dtors = 0;
7363 /* We're done parsing; proceed to optimize and emit assembly.
7364 FIXME: shouldn't be the front end's responsibility to call this. */
7365 cgraph_optimize ();
7368 #include "gt-c-decl.h"