* gcc.dg/const-elim-1.c: Remove xfail for xtensa-*-*.
[official-gcc.git] / gcc / c-decl.c
blob0ca4500021ecc44f02a2e06b0ff68e65657fc232
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
110 defined. */
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type;
156 /* Nonzero when starting a function declared `extern inline'. */
158 static int current_extern_inline;
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
163 static bool undef_nested_function;
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
182 function.
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
203 struct c_binding GTY((chain_next ("%h.prev")))
205 tree decl; /* the decl bound */
206 tree type; /* the type in this scope */
207 tree id; /* the identifier it's bound to */
208 struct c_binding *prev; /* the previous decl in this scope */
209 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
210 unsigned int depth : 28; /* depth of this scope */
211 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214 /* one free bit */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
240 struct lang_identifier GTY(())
242 struct c_common_identifier common_id;
243 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244 struct c_binding *tag_binding; /* struct/union/enum tags */
245 struct c_binding *label_binding; /* labels */
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
252 /* The resulting tree type. */
254 union lang_tree_node
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 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)")))
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
269 Most declarations are recorded in the current scope.
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
290 undefined.
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
297 this scope.
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
303 list here.)
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
309 struct c_scope GTY((chain_next ("%h.outer")))
311 /* The scope containing this one. */
312 struct c_scope *outer;
314 /* The next outermost function scope. */
315 struct c_scope *outer_function;
317 /* All bindings in this scope. */
318 struct c_binding *bindings;
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
322 tree blocks;
323 tree blocks_last;
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth : 28;
329 /* True if we are currently filling this scope with parameter
330 declarations. */
331 BOOL_BITFIELD parm_flag : 1;
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls : 1;
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body : 1;
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep : 1;
348 /* The scope currently in effect. */
350 static GTY(()) struct c_scope *current_scope;
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
356 static GTY(()) struct c_scope *current_function_scope;
358 /* The C file scope. This is reset for each input translation unit. */
360 static GTY(()) struct c_scope *file_scope;
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
365 static GTY(()) struct c_scope *external_scope;
367 /* A chain of c_scope structures awaiting reuse. */
369 static GTY((deletable)) struct c_scope *scope_freelist;
371 /* A chain of c_binding structures awaiting reuse. */
373 static GTY((deletable)) struct c_binding *binding_freelist;
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
378 tree d_ = (decl); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
381 else \
382 s_->list = d_; \
383 s_->list##_last = d_; \
384 } while (0)
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
390 if (t_->to##_last) \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
392 else \
393 t_->to = f_->from; \
394 t_->to##_last = f_->from##_last; \
395 } while (0)
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
399 static bool keep_next_level_flag;
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
405 static bool next_is_function_body;
407 /* Functions called automatically at the beginning and end of execution. */
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
412 /* Forward declarations. */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416 struct c_declspecs *,
417 enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
424 deprecated items. */
426 enum deprecated_states {
427 DEPRECATED_NORMAL,
428 DEPRECATED_SUPPRESS
431 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
433 void
434 c_print_identifier (FILE *file, tree node, int indent)
436 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
437 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
438 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
439 if (C_IS_RESERVED_WORD (node))
441 tree rid = ridpointers[C_RID_CODE (node)];
442 indent_to (file, indent + 4);
443 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
444 (void *) rid, IDENTIFIER_POINTER (rid));
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
451 static void
452 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
454 struct c_binding *b, **here;
456 if (binding_freelist)
458 b = binding_freelist;
459 binding_freelist = b->prev;
461 else
462 b = GGC_NEW (struct c_binding);
464 b->shadowed = 0;
465 b->decl = decl;
466 b->id = name;
467 b->depth = scope->depth;
468 b->invisible = invisible;
469 b->nested = nested;
470 b->inner_comp = 0;
472 b->type = 0;
474 b->prev = scope->bindings;
475 scope->bindings = b;
477 if (!name)
478 return;
480 switch (TREE_CODE (decl))
482 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
483 case ENUMERAL_TYPE:
484 case UNION_TYPE:
485 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
486 case VAR_DECL:
487 case FUNCTION_DECL:
488 case TYPE_DECL:
489 case CONST_DECL:
490 case PARM_DECL:
491 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
493 default:
494 gcc_unreachable ();
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here && (*here)->depth > scope->depth)
501 here = &(*here)->shadowed;
503 b->shadowed = *here;
504 *here = b;
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding *
512 free_binding_and_advance (struct c_binding *b)
514 struct c_binding *prev = b->prev;
516 memset (b, 0, sizeof (struct c_binding));
517 b->prev = binding_freelist;
518 binding_freelist = b;
520 return prev;
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
527 void
528 c_finish_incomplete_decl (tree decl)
530 if (TREE_CODE (decl) == VAR_DECL)
532 tree type = TREE_TYPE (decl);
533 if (type != error_mark_node
534 && TREE_CODE (type) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl)
536 && TYPE_DOMAIN (type) == 0)
538 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
540 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
542 layout_decl (decl, 0);
547 /* The Objective-C front-end often needs to determine the current scope. */
549 void *
550 objc_get_current_scope (void)
552 return current_scope;
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
558 void
559 objc_mark_locals_volatile (void *enclosing_blk)
561 struct c_scope *scope;
562 struct c_binding *b;
564 for (scope = current_scope;
565 scope && scope != enclosing_blk;
566 scope = scope->outer)
568 for (b = scope->bindings; b; b = b->prev)
570 if (TREE_CODE (b->decl) == VAR_DECL
571 || TREE_CODE (b->decl) == PARM_DECL)
573 C_DECL_REGISTER (b->decl) = 0;
574 DECL_REGISTER (b->decl) = 0;
575 TREE_THIS_VOLATILE (b->decl) = 1;
579 /* Do not climb up past the current function. */
580 if (scope->function_body)
581 break;
585 /* Nonzero if we are currently in file scope. */
588 global_bindings_p (void)
590 return current_scope == file_scope && !c_override_global_bindings_to_false;
593 void
594 keep_next_level (void)
596 keep_next_level_flag = true;
599 /* Identify this scope as currently being filled with parameters. */
601 void
602 declare_parm_level (void)
604 current_scope->parm_flag = true;
607 void
608 push_scope (void)
610 if (next_is_function_body)
612 /* This is the transition from the parameters to the top level
613 of the function body. These are the same scope
614 (C99 6.2.1p4,6) so we do not push another scope structure.
615 next_is_function_body is set only by store_parm_decls, which
616 in turn is called when and only when we are about to
617 encounter the opening curly brace for the function body.
619 The outermost block of a function always gets a BLOCK node,
620 because the debugging output routines expect that each
621 function has at least one BLOCK. */
622 current_scope->parm_flag = false;
623 current_scope->function_body = true;
624 current_scope->keep = true;
625 current_scope->outer_function = current_function_scope;
626 current_function_scope = current_scope;
628 keep_next_level_flag = false;
629 next_is_function_body = false;
631 else
633 struct c_scope *scope;
634 if (scope_freelist)
636 scope = scope_freelist;
637 scope_freelist = scope->outer;
639 else
640 scope = GGC_CNEW (struct c_scope);
642 scope->keep = keep_next_level_flag;
643 scope->outer = current_scope;
644 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
646 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
647 possible. */
648 if (current_scope && scope->depth == 0)
650 scope->depth--;
651 sorry ("GCC supports only %u nested scopes", scope->depth);
654 current_scope = scope;
655 keep_next_level_flag = false;
659 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
661 static void
662 set_type_context (tree type, tree context)
664 for (type = TYPE_MAIN_VARIANT (type); type;
665 type = TYPE_NEXT_VARIANT (type))
666 TYPE_CONTEXT (type) = context;
669 /* Exit a scope. Restore the state of the identifier-decl mappings
670 that were in effect when this scope was entered. Return a BLOCK
671 node containing all the DECLs in this scope that are of interest
672 to debug info generation. */
674 tree
675 pop_scope (void)
677 struct c_scope *scope = current_scope;
678 tree block, context, p;
679 struct c_binding *b;
681 bool functionbody = scope->function_body;
682 bool keep = functionbody || scope->keep || scope->bindings;
684 c_end_vm_scope (scope->depth);
686 /* If appropriate, create a BLOCK to record the decls for the life
687 of this function. */
688 block = 0;
689 if (keep)
691 block = make_node (BLOCK);
692 BLOCK_SUBBLOCKS (block) = scope->blocks;
693 TREE_USED (block) = 1;
695 /* In each subblock, record that this is its superior. */
696 for (p = scope->blocks; p; p = TREE_CHAIN (p))
697 BLOCK_SUPERCONTEXT (p) = block;
699 BLOCK_VARS (block) = 0;
702 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
703 scope must be set so that they point to the appropriate
704 construct, i.e. either to the current FUNCTION_DECL node, or
705 else to the BLOCK node we just constructed.
707 Note that for tagged types whose scope is just the formal
708 parameter list for some function type specification, we can't
709 properly set their TYPE_CONTEXTs here, because we don't have a
710 pointer to the appropriate FUNCTION_TYPE node readily available
711 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
712 type nodes get set in `grokdeclarator' as soon as we have created
713 the FUNCTION_TYPE node which will represent the "scope" for these
714 "parameter list local" tagged types. */
715 if (scope->function_body)
716 context = current_function_decl;
717 else if (scope == file_scope)
719 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
720 TREE_CHAIN (file_decl) = all_translation_units;
721 all_translation_units = file_decl;
722 context = file_decl;
724 else
725 context = block;
727 /* Clear all bindings in this scope. */
728 for (b = scope->bindings; b; b = free_binding_and_advance (b))
730 p = b->decl;
731 switch (TREE_CODE (p))
733 case LABEL_DECL:
734 /* Warnings for unused labels, errors for undefined labels. */
735 if (TREE_USED (p) && !DECL_INITIAL (p))
737 error ("%Jlabel %qD used but not defined", p, p);
738 DECL_INITIAL (p) = error_mark_node;
740 else if (!TREE_USED (p) && warn_unused_label)
742 if (DECL_INITIAL (p))
743 warning (0, "%Jlabel %qD defined but not used", p, p);
744 else
745 warning (0, "%Jlabel %qD declared but not defined", p, p);
747 /* Labels go in BLOCK_VARS. */
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
750 gcc_assert (I_LABEL_BINDING (b->id) == b);
751 I_LABEL_BINDING (b->id) = b->shadowed;
752 break;
754 case ENUMERAL_TYPE:
755 case UNION_TYPE:
756 case RECORD_TYPE:
757 set_type_context (p, context);
759 /* Types may not have tag-names, in which case the type
760 appears in the bindings list with b->id NULL. */
761 if (b->id)
763 gcc_assert (I_TAG_BINDING (b->id) == b);
764 I_TAG_BINDING (b->id) = b->shadowed;
766 break;
768 case FUNCTION_DECL:
769 /* Propagate TREE_ADDRESSABLE from nested functions to their
770 containing functions. */
771 if (!TREE_ASM_WRITTEN (p)
772 && DECL_INITIAL (p) != 0
773 && TREE_ADDRESSABLE (p)
774 && DECL_ABSTRACT_ORIGIN (p) != 0
775 && DECL_ABSTRACT_ORIGIN (p) != p)
776 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
777 if (!DECL_EXTERNAL (p)
778 && DECL_INITIAL (p) == 0)
780 error ("%Jnested function %qD declared but never defined", p, p);
781 undef_nested_function = true;
783 goto common_symbol;
785 case VAR_DECL:
786 /* Warnings for unused variables. */
787 if (warn_unused_variable
788 && !TREE_USED (p)
789 && !DECL_IN_SYSTEM_HEADER (p)
790 && DECL_NAME (p)
791 && !DECL_ARTIFICIAL (p)
792 && scope != file_scope
793 && scope != external_scope)
794 warning (0, "%Junused variable %qD", p, p);
796 if (b->inner_comp)
798 error ("%Jtype of array %qD completed incompatibly with"
799 " implicit initialization", p, p);
802 /* Fall through. */
803 case TYPE_DECL:
804 case CONST_DECL:
805 common_symbol:
806 /* All of these go in BLOCK_VARS, but only if this is the
807 binding in the home scope. */
808 if (!b->nested)
810 TREE_CHAIN (p) = BLOCK_VARS (block);
811 BLOCK_VARS (block) = p;
813 /* If this is the file scope, and we are processing more
814 than one translation unit in this compilation, set
815 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
816 This makes same_translation_unit_p work, and causes
817 static declarations to be given disambiguating suffixes. */
818 if (scope == file_scope && num_in_fnames > 1)
820 DECL_CONTEXT (p) = context;
821 if (TREE_CODE (p) == TYPE_DECL)
822 set_type_context (TREE_TYPE (p), context);
825 /* Fall through. */
826 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
827 already been put there by store_parm_decls. Unused-
828 parameter warnings are handled by function.c.
829 error_mark_node obviously does not go in BLOCK_VARS and
830 does not get unused-variable warnings. */
831 case PARM_DECL:
832 case ERROR_MARK:
833 /* It is possible for a decl not to have a name. We get
834 here with b->id NULL in this case. */
835 if (b->id)
837 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
838 I_SYMBOL_BINDING (b->id) = b->shadowed;
839 if (b->shadowed && b->shadowed->type)
840 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
842 break;
844 default:
845 gcc_unreachable ();
850 /* Dispose of the block that we just made inside some higher level. */
851 if ((scope->function_body || scope == file_scope) && context)
853 DECL_INITIAL (context) = block;
854 BLOCK_SUPERCONTEXT (block) = context;
856 else if (scope->outer)
858 if (block)
859 SCOPE_LIST_APPEND (scope->outer, blocks, block);
860 /* If we did not make a block for the scope just exited, any
861 blocks made for inner scopes must be carried forward so they
862 will later become subblocks of something else. */
863 else if (scope->blocks)
864 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
867 /* Pop the current scope, and free the structure for reuse. */
868 current_scope = scope->outer;
869 if (scope->function_body)
870 current_function_scope = scope->outer_function;
872 memset (scope, 0, sizeof (struct c_scope));
873 scope->outer = scope_freelist;
874 scope_freelist = scope;
876 return block;
879 void
880 push_file_scope (void)
882 tree decl;
884 if (file_scope)
885 return;
887 push_scope ();
888 file_scope = current_scope;
890 start_fname_decls ();
892 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
893 bind (DECL_NAME (decl), decl, file_scope,
894 /*invisible=*/false, /*nested=*/true);
897 void
898 pop_file_scope (void)
900 /* In case there were missing closebraces, get us back to the global
901 binding level. */
902 while (current_scope != file_scope)
903 pop_scope ();
905 /* __FUNCTION__ is defined at file scope (""). This
906 call may not be necessary as my tests indicate it
907 still works without it. */
908 finish_fname_decls ();
910 /* This is the point to write out a PCH if we're doing that.
911 In that case we do not want to do anything else. */
912 if (pch_file)
914 c_common_write_pch ();
915 return;
918 /* Pop off the file scope and close this translation unit. */
919 pop_scope ();
920 file_scope = 0;
922 maybe_apply_pending_pragma_weaks ();
923 cgraph_finalize_compilation_unit ();
926 /* Insert BLOCK at the end of the list of subblocks of the current
927 scope. This is used when a BIND_EXPR is expanded, to handle the
928 BLOCK node inside the BIND_EXPR. */
930 void
931 insert_block (tree block)
933 TREE_USED (block) = 1;
934 SCOPE_LIST_APPEND (current_scope, blocks, block);
937 /* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
944 static void
945 pushtag (tree name, tree type)
947 /* Record the identifier as the type's name if it has none. */
948 if (name && !TYPE_NAME (type))
949 TYPE_NAME (type) = name;
950 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
953 tagged type we just added to the current scope. This fake
954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
955 to output a representation of a tagged type, and it also gives
956 us a convenient place to record the "scope start" address for the
957 tagged type. */
959 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
961 /* An approximation for now, so we can tell this is a function-scope tag.
962 This will be updated in pop_scope. */
963 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
966 /* Subroutine of compare_decls. Allow harmless mismatches in return
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
970 static tree
971 match_builtin_function_types (tree newtype, tree oldtype)
973 tree newrettype, oldrettype;
974 tree newargs, oldargs;
975 tree trytype, tryargs;
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype = TREE_TYPE (oldtype);
979 newrettype = TREE_TYPE (newtype);
981 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
982 return 0;
984 oldargs = TYPE_ARG_TYPES (oldtype);
985 newargs = TYPE_ARG_TYPES (newtype);
986 tryargs = newargs;
988 while (oldargs || newargs)
990 if (!oldargs
991 || !newargs
992 || !TREE_VALUE (oldargs)
993 || !TREE_VALUE (newargs)
994 || TYPE_MODE (TREE_VALUE (oldargs))
995 != TYPE_MODE (TREE_VALUE (newargs)))
996 return 0;
998 oldargs = TREE_CHAIN (oldargs);
999 newargs = TREE_CHAIN (newargs);
1002 trytype = build_function_type (newrettype, tryargs);
1003 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1006 /* Subroutine of diagnose_mismatched_decls. Check for function type
1007 mismatch involving an empty arglist vs a nonempty one and give clearer
1008 diagnostics. */
1009 static void
1010 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1011 tree newtype, tree oldtype)
1013 tree t;
1015 if (TREE_CODE (olddecl) != FUNCTION_DECL
1016 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1017 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1019 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1020 return;
1022 t = TYPE_ARG_TYPES (oldtype);
1023 if (t == 0)
1024 t = TYPE_ARG_TYPES (newtype);
1025 for (; t; t = TREE_CHAIN (t))
1027 tree type = TREE_VALUE (t);
1029 if (TREE_CHAIN (t) == 0
1030 && TYPE_MAIN_VARIANT (type) != void_type_node)
1032 inform ("a parameter list with an ellipsis can%'t match "
1033 "an empty parameter name list declaration");
1034 break;
1037 if (c_type_promotes_to (type) != type)
1039 inform ("an argument type that has a default promotion can%'t match "
1040 "an empty parameter name list declaration");
1041 break;
1046 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1050 static bool
1051 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1053 tree newargs, oldargs;
1054 int i;
1056 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1058 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1059 newargs = TYPE_ARG_TYPES (newtype);
1060 i = 1;
1062 for (;;)
1064 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1065 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1067 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1068 break;
1070 /* Reaching the end of just one list means the two decls don't
1071 agree on the number of arguments. */
1072 if (END_OF_ARGLIST (oldargtype))
1074 error ("%Jprototype for %qD declares more arguments "
1075 "than previous old-style definition", newdecl, newdecl);
1076 return false;
1078 else if (END_OF_ARGLIST (newargtype))
1080 error ("%Jprototype for %qD declares fewer arguments "
1081 "than previous old-style definition", newdecl, newdecl);
1082 return false;
1085 /* Type for passing arg must be consistent with that declared
1086 for the arg. */
1087 else if (!comptypes (oldargtype, newargtype))
1089 error ("%Jprototype for %qD declares argument %d"
1090 " with incompatible type",
1091 newdecl, newdecl, i);
1092 return false;
1095 oldargs = TREE_CHAIN (oldargs);
1096 newargs = TREE_CHAIN (newargs);
1097 i++;
1100 /* If we get here, no errors were found, but do issue a warning
1101 for this poor-style construct. */
1102 warning (0, "%Jprototype for %qD follows non-prototype definition",
1103 newdecl, newdecl);
1104 return true;
1105 #undef END_OF_ARGLIST
1108 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1109 first in a pair of mismatched declarations, using the diagnostic
1110 function DIAG. */
1111 static void
1112 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1114 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1116 else if (DECL_INITIAL (decl))
1117 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1118 else if (C_DECL_IMPLICIT (decl))
1119 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1120 else
1121 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1124 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1125 Returns true if the caller should proceed to merge the two, false
1126 if OLDDECL should simply be discarded. As a side effect, issues
1127 all necessary diagnostics for invalid or poor-style combinations.
1128 If it returns true, writes the types of NEWDECL and OLDDECL to
1129 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1130 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1132 static bool
1133 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1134 tree *newtypep, tree *oldtypep)
1136 tree newtype, oldtype;
1137 bool pedwarned = false;
1138 bool warned = false;
1139 bool retval = true;
1141 /* If we have error_mark_node for either decl or type, just discard
1142 the previous decl - we're in an error cascade already. */
1143 if (olddecl == error_mark_node || newdecl == error_mark_node)
1144 return false;
1145 *oldtypep = oldtype = TREE_TYPE (olddecl);
1146 *newtypep = newtype = TREE_TYPE (newdecl);
1147 if (oldtype == error_mark_node || newtype == error_mark_node)
1148 return false;
1150 /* Two different categories of symbol altogether. This is an error
1151 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1152 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1154 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1155 && DECL_BUILT_IN (olddecl)
1156 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1158 error ("%J%qD redeclared as different kind of symbol",
1159 newdecl, newdecl);
1160 locate_old_decl (olddecl, error);
1162 else if (TREE_PUBLIC (newdecl))
1163 warning (0, "%Jbuilt-in function %qD declared as non-function",
1164 newdecl, newdecl);
1165 else if (warn_shadow)
1166 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1167 newdecl, newdecl);
1168 return false;
1171 /* Enumerators have no linkage, so may only be declared once in a
1172 given scope. */
1173 if (TREE_CODE (olddecl) == CONST_DECL)
1175 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1176 locate_old_decl (olddecl, error);
1177 return false;
1180 if (!comptypes (oldtype, newtype))
1182 if (TREE_CODE (olddecl) == FUNCTION_DECL
1183 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1185 /* Accept harmless mismatch in function types.
1186 This is for the ffs and fprintf builtins. */
1187 tree trytype = match_builtin_function_types (newtype, oldtype);
1189 if (trytype && comptypes (newtype, trytype))
1190 *oldtypep = oldtype = trytype;
1191 else
1193 /* If types don't match for a built-in, throw away the
1194 built-in. No point in calling locate_old_decl here, it
1195 won't print anything. */
1196 warning (0, "%Jconflicting types for built-in function %qD",
1197 newdecl, newdecl);
1198 return false;
1201 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_IS_BUILTIN (olddecl))
1204 /* A conflicting function declaration for a predeclared
1205 function that isn't actually built in. Objective C uses
1206 these. The new declaration silently overrides everything
1207 but the volatility (i.e. noreturn) indication. See also
1208 below. FIXME: Make Objective C use normal builtins. */
1209 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1210 return false;
1212 /* Permit void foo (...) to match int foo (...) if the latter is
1213 the definition and implicit int was used. See
1214 c-torture/compile/920625-2.c. */
1215 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1216 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1217 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1218 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1220 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1221 /* Make sure we keep void as the return type. */
1222 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1223 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1224 pedwarned = true;
1226 /* Permit void foo (...) to match an earlier call to foo (...) with
1227 no declared type (thus, implicitly int). */
1228 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1229 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1230 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1231 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1233 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1234 /* Make sure we keep void as the return type. */
1235 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1236 pedwarned = true;
1238 else
1240 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1241 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1242 else
1243 error ("%Jconflicting types for %qD", newdecl, newdecl);
1244 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1245 locate_old_decl (olddecl, error);
1246 return false;
1250 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1251 but silently ignore the redeclaration if either is in a system
1252 header. (Conflicting redeclarations were handled above.) */
1253 if (TREE_CODE (newdecl) == TYPE_DECL)
1255 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1256 return true; /* Allow OLDDECL to continue in use. */
1258 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1260 return false;
1263 /* Function declarations can either be 'static' or 'extern' (no
1264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1265 can never conflict with each other on account of linkage (6.2.2p4).
1266 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1267 two definitions if one is 'extern inline' and one is not. The non-
1268 extern-inline definition supersedes the extern-inline definition. */
1269 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1271 /* If you declare a built-in function name as static, or
1272 define the built-in with an old-style definition (so we
1273 can't validate the argument list) the built-in definition is
1274 overridden, but optionally warn this was a bad choice of name. */
1275 if (DECL_BUILT_IN (olddecl)
1276 && !C_DECL_DECLARED_BUILTIN (olddecl)
1277 && (!TREE_PUBLIC (newdecl)
1278 || (DECL_INITIAL (newdecl)
1279 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1281 if (warn_shadow)
1282 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1283 newdecl, newdecl);
1284 /* Discard the old built-in function. */
1285 return false;
1288 if (DECL_INITIAL (newdecl))
1290 if (DECL_INITIAL (olddecl))
1292 /* If both decls have extern inline and are in the same TU,
1293 reject the new decl. */
1294 if (DECL_DECLARED_INLINE_P (olddecl)
1295 && DECL_EXTERNAL (olddecl)
1296 && DECL_DECLARED_INLINE_P (newdecl)
1297 && DECL_EXTERNAL (newdecl)
1298 && same_translation_unit_p (newdecl, olddecl))
1300 error ("%Jredefinition of %qD", newdecl, newdecl);
1301 locate_old_decl (olddecl, error);
1302 return false;
1304 /* If both decls have not extern inline, reject the new decl. */
1305 if (!DECL_DECLARED_INLINE_P (olddecl)
1306 && !DECL_EXTERNAL (olddecl)
1307 && !DECL_DECLARED_INLINE_P (newdecl)
1308 && !DECL_EXTERNAL (newdecl))
1310 error ("%Jredefinition of %qD", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1312 return false;
1314 /* If the new decl is declared as extern inline, error if they are
1315 in the same TU, otherwise retain the old decl. */
1316 if (!DECL_DECLARED_INLINE_P (olddecl)
1317 && !DECL_EXTERNAL (olddecl)
1318 && DECL_DECLARED_INLINE_P (newdecl)
1319 && DECL_EXTERNAL (newdecl))
1321 if (same_translation_unit_p (newdecl, olddecl))
1323 error ("%Jredefinition of %qD", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1325 return false;
1327 else
1328 retval = false;
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl)
1335 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1337 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1339 locate_old_decl (olddecl, error);
1340 return false;
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl)
1359 && !(DECL_EXTERNAL (olddecl)
1360 && DECL_DECLARED_INLINE_P (olddecl)))
1362 error ("%Jstatic declaration of %qD follows "
1363 "non-static declaration", newdecl, newdecl);
1364 locate_old_decl (olddecl, error);
1366 return false;
1368 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1370 if (DECL_CONTEXT (olddecl))
1372 error ("%Jnon-static declaration of %qD follows "
1373 "static declaration", newdecl, newdecl);
1374 locate_old_decl (olddecl, error);
1375 return false;
1377 else if (warn_traditional)
1379 warning (0, "%Jnon-static declaration of %qD follows "
1380 "static declaration", newdecl, newdecl);
1381 warned = true;
1385 else if (TREE_CODE (newdecl) == VAR_DECL)
1387 /* Only variables can be thread-local, and all declarations must
1388 agree on this property. */
1389 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1391 if (DECL_THREAD_LOCAL (newdecl))
1392 error ("%Jthread-local declaration of %qD follows "
1393 "non-thread-local declaration", newdecl, newdecl);
1394 else
1395 error ("%Jnon-thread-local declaration of %qD follows "
1396 "thread-local declaration", newdecl, newdecl);
1398 locate_old_decl (olddecl, error);
1399 return false;
1402 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1403 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1405 error ("%Jredefinition of %qD", newdecl, newdecl);
1406 locate_old_decl (olddecl, error);
1407 return false;
1410 /* Objects declared at file scope: if the first declaration had
1411 external linkage (even if it was an external reference) the
1412 second must have external linkage as well, or the behavior is
1413 undefined. If the first declaration had internal linkage, then
1414 the second must too, or else be an external reference (in which
1415 case the composite declaration still has internal linkage).
1416 As for function declarations, we warn about the static-then-
1417 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1418 if (DECL_FILE_SCOPE_P (newdecl)
1419 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1421 if (DECL_EXTERNAL (newdecl))
1423 if (!DECL_FILE_SCOPE_P (olddecl))
1425 error ("%Jextern declaration of %qD follows "
1426 "declaration with no linkage", newdecl, newdecl);
1427 locate_old_decl (olddecl, error);
1428 return false;
1430 else if (warn_traditional)
1432 warning (0, "%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1434 warned = true;
1437 else
1439 if (TREE_PUBLIC (newdecl))
1440 error ("%Jnon-static declaration of %qD follows "
1441 "static declaration", newdecl, newdecl);
1442 else
1443 error ("%Jstatic declaration of %qD follows "
1444 "non-static declaration", newdecl, newdecl);
1446 locate_old_decl (olddecl, error);
1447 return false;
1450 /* Two objects with the same name declared at the same block
1451 scope must both be external references (6.7p3). */
1452 else if (!DECL_FILE_SCOPE_P (newdecl))
1454 if (DECL_EXTERNAL (newdecl))
1456 /* Extern with initializer at block scope, which will
1457 already have received an error. */
1459 else if (DECL_EXTERNAL (olddecl))
1461 error ("%Jdeclaration of %qD with no linkage follows "
1462 "extern declaration", newdecl, newdecl);
1463 locate_old_decl (olddecl, error);
1465 else
1467 error ("%Jredeclaration of %qD with no linkage",
1468 newdecl, newdecl);
1469 locate_old_decl (olddecl, error);
1472 return false;
1476 /* warnings */
1477 /* All decls must agree on a visibility. */
1478 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1479 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1481 warning (0, "%Jredeclaration of %qD with different visibility "
1482 "(old visibility preserved)", newdecl, newdecl);
1483 warned = true;
1486 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1488 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1489 if (DECL_DECLARED_INLINE_P (newdecl)
1490 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1492 warning (0, "%Jinline declaration of %qD follows "
1493 "declaration with attribute noinline", newdecl, newdecl);
1494 warned = true;
1496 else if (DECL_DECLARED_INLINE_P (olddecl)
1497 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1499 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1500 "inline declaration ", newdecl, newdecl);
1501 warned = true;
1504 /* Inline declaration after use or definition.
1505 ??? Should we still warn about this now we have unit-at-a-time
1506 mode and can get it right?
1507 Definitely don't complain if the decls are in different translation
1508 units. */
1509 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1510 && same_translation_unit_p (olddecl, newdecl))
1512 if (TREE_USED (olddecl))
1514 warning (0, "%J%qD declared inline after being called",
1515 olddecl, olddecl);
1516 warned = true;
1518 else if (DECL_INITIAL (olddecl))
1520 warning (0, "%J%qD declared inline after its definition",
1521 olddecl, olddecl);
1522 warned = true;
1526 else /* PARM_DECL, VAR_DECL */
1528 /* Redeclaration of a parameter is a constraint violation (this is
1529 not explicitly stated, but follows from C99 6.7p3 [no more than
1530 one declaration of the same identifier with no linkage in the
1531 same scope, except type tags] and 6.2.2p6 [parameters have no
1532 linkage]). We must check for a forward parameter declaration,
1533 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1534 an extension, the mandatory diagnostic for which is handled by
1535 mark_forward_parm_decls. */
1537 if (TREE_CODE (newdecl) == PARM_DECL
1538 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1540 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1541 locate_old_decl (olddecl, error);
1542 return false;
1546 /* Optional warning for completely redundant decls. */
1547 if (!warned && !pedwarned
1548 && warn_redundant_decls
1549 /* Don't warn about a function declaration followed by a
1550 definition. */
1551 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1552 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1553 /* Don't warn about redundant redeclarations of builtins. */
1554 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1555 && !DECL_BUILT_IN (newdecl)
1556 && DECL_BUILT_IN (olddecl)
1557 && !C_DECL_DECLARED_BUILTIN (olddecl))
1558 /* Don't warn about an extern followed by a definition. */
1559 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1560 /* Don't warn about forward parameter decls. */
1561 && !(TREE_CODE (newdecl) == PARM_DECL
1562 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1564 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1565 warned = true;
1568 /* Report location of previous decl/defn in a consistent manner. */
1569 if (warned || pedwarned)
1570 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1572 return retval;
1575 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1576 consistent with OLDDECL, but carries new information. Merge the
1577 new information into OLDDECL. This function issues no
1578 diagnostics. */
1580 static void
1581 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1583 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1584 && DECL_INITIAL (newdecl) != 0);
1585 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1586 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1587 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1588 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1590 /* For real parm decl following a forward decl, rechain the old decl
1591 in its new location and clear TREE_ASM_WRITTEN (it's not a
1592 forward decl anymore). */
1593 if (TREE_CODE (newdecl) == PARM_DECL
1594 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1596 struct c_binding *b, **here;
1598 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1599 if ((*here)->decl == olddecl)
1600 goto found;
1601 gcc_unreachable ();
1603 found:
1604 b = *here;
1605 *here = b->prev;
1606 b->prev = current_scope->bindings;
1607 current_scope->bindings = b;
1609 TREE_ASM_WRITTEN (olddecl) = 0;
1612 DECL_ATTRIBUTES (newdecl)
1613 = targetm.merge_decl_attributes (olddecl, newdecl);
1615 /* Merge the data types specified in the two decls. */
1616 TREE_TYPE (newdecl)
1617 = TREE_TYPE (olddecl)
1618 = composite_type (newtype, oldtype);
1620 /* Lay the type out, unless already done. */
1621 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1623 if (TREE_TYPE (newdecl) != error_mark_node)
1624 layout_type (TREE_TYPE (newdecl));
1625 if (TREE_CODE (newdecl) != FUNCTION_DECL
1626 && TREE_CODE (newdecl) != TYPE_DECL
1627 && TREE_CODE (newdecl) != CONST_DECL)
1628 layout_decl (newdecl, 0);
1630 else
1632 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1633 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1634 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1635 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1636 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1637 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1639 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1640 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1644 /* Keep the old rtl since we can safely use it. */
1645 COPY_DECL_RTL (olddecl, newdecl);
1647 /* Merge the type qualifiers. */
1648 if (TREE_READONLY (newdecl))
1649 TREE_READONLY (olddecl) = 1;
1651 if (TREE_THIS_VOLATILE (newdecl))
1653 TREE_THIS_VOLATILE (olddecl) = 1;
1654 if (TREE_CODE (newdecl) == VAR_DECL)
1655 make_var_volatile (newdecl);
1658 /* Merge deprecatedness. */
1659 if (TREE_DEPRECATED (newdecl))
1660 TREE_DEPRECATED (olddecl) = 1;
1662 /* Keep source location of definition rather than declaration and of
1663 prototype rather than non-prototype unless that prototype is
1664 built-in. */
1665 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1666 || (old_is_prototype && !new_is_prototype
1667 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1668 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1670 /* Merge the unused-warning information. */
1671 if (DECL_IN_SYSTEM_HEADER (olddecl))
1672 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1673 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1674 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1676 /* Merge the initialization information. */
1677 if (DECL_INITIAL (newdecl) == 0)
1678 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1680 /* Merge the section attribute.
1681 We want to issue an error if the sections conflict but that must be
1682 done later in decl_attributes since we are called before attributes
1683 are assigned. */
1684 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1685 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1687 /* Copy the assembler name.
1688 Currently, it can only be defined in the prototype. */
1689 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1691 /* Use visibility of whichever declaration had it specified */
1692 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1694 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1695 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1700 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1701 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1702 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1703 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1704 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1705 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1706 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1707 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1708 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1709 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1712 /* Merge the storage class information. */
1713 merge_weak (newdecl, olddecl);
1715 /* For functions, static overrides non-static. */
1716 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1718 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1719 /* This is since we don't automatically
1720 copy the attributes of NEWDECL into OLDDECL. */
1721 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1722 /* If this clears `static', clear it in the identifier too. */
1723 if (!TREE_PUBLIC (olddecl))
1724 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1726 if (DECL_EXTERNAL (newdecl))
1728 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1729 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1731 /* An extern decl does not override previous storage class. */
1732 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1733 if (!DECL_EXTERNAL (newdecl))
1735 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1736 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1739 else
1741 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1742 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1745 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1747 /* If we're redefining a function previously defined as extern
1748 inline, make sure we emit debug info for the inline before we
1749 throw it away, in case it was inlined into a function that hasn't
1750 been written out yet. */
1751 if (new_is_definition && DECL_INITIAL (olddecl))
1753 if (TREE_USED (olddecl)
1754 /* In unit-at-a-time mode we never inline re-defined extern
1755 inline functions. */
1756 && !flag_unit_at_a_time
1757 && cgraph_function_possibly_inlined_p (olddecl))
1758 (*debug_hooks->outlining_inline_function) (olddecl);
1760 /* The new defn must not be inline. */
1761 DECL_INLINE (newdecl) = 0;
1762 DECL_UNINLINABLE (newdecl) = 1;
1764 else
1766 /* If either decl says `inline', this fn is inline,
1767 unless its definition was passed already. */
1768 if (DECL_DECLARED_INLINE_P (newdecl)
1769 || DECL_DECLARED_INLINE_P (olddecl))
1770 DECL_DECLARED_INLINE_P (newdecl) = 1;
1772 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1773 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1776 if (DECL_BUILT_IN (olddecl))
1778 /* If redeclaring a builtin function, it stays built in.
1779 But it gets tagged as having been declared. */
1780 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1781 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1782 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1783 if (new_is_prototype)
1784 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1785 else
1786 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1787 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1790 /* Also preserve various other info from the definition. */
1791 if (!new_is_definition)
1793 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1794 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1795 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1796 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1797 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1799 /* Set DECL_INLINE on the declaration if we've got a body
1800 from which to instantiate. */
1801 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1803 DECL_INLINE (newdecl) = 1;
1804 DECL_ABSTRACT_ORIGIN (newdecl)
1805 = DECL_ABSTRACT_ORIGIN (olddecl);
1808 else
1810 /* If a previous declaration said inline, mark the
1811 definition as inlinable. */
1812 if (DECL_DECLARED_INLINE_P (newdecl)
1813 && !DECL_UNINLINABLE (newdecl))
1814 DECL_INLINE (newdecl) = 1;
1818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1819 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1821 unsigned olddecl_uid = DECL_UID (olddecl);
1822 tree olddecl_context = DECL_CONTEXT (olddecl);
1824 memcpy ((char *) olddecl + sizeof (struct tree_common),
1825 (char *) newdecl + sizeof (struct tree_common),
1826 sizeof (struct tree_decl) - sizeof (struct tree_common));
1827 DECL_UID (olddecl) = olddecl_uid;
1828 DECL_CONTEXT (olddecl) = olddecl_context;
1831 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1832 so that encode_section_info has a chance to look at the new decl
1833 flags and attributes. */
1834 if (DECL_RTL_SET_P (olddecl)
1835 && (TREE_CODE (olddecl) == FUNCTION_DECL
1836 || (TREE_CODE (olddecl) == VAR_DECL
1837 && TREE_STATIC (olddecl))))
1838 make_decl_rtl (olddecl);
1841 /* Handle when a new declaration NEWDECL has the same name as an old
1842 one OLDDECL in the same binding contour. Prints an error message
1843 if appropriate.
1845 If safely possible, alter OLDDECL to look like NEWDECL, and return
1846 true. Otherwise, return false. */
1848 static bool
1849 duplicate_decls (tree newdecl, tree olddecl)
1851 tree newtype = NULL, oldtype = NULL;
1853 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1854 return false;
1856 merge_decls (newdecl, olddecl, newtype, oldtype);
1857 return true;
1861 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1862 static void
1863 warn_if_shadowing (tree new_decl)
1865 struct c_binding *b;
1867 /* Shadow warnings wanted? */
1868 if (!warn_shadow
1869 /* No shadow warnings for internally generated vars. */
1870 || DECL_IS_BUILTIN (new_decl)
1871 /* No shadow warnings for vars made for inlining. */
1872 || DECL_FROM_INLINE (new_decl)
1873 /* Don't warn about the parm names in function declarator
1874 within a function declarator. It would be nice to avoid
1875 warning in any function declarator in a declaration, as
1876 opposed to a definition, but there is no way to tell
1877 it's not a definition at this point. */
1878 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1879 return;
1881 /* Is anything being shadowed? Invisible decls do not count. */
1882 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1883 if (b->decl && b->decl != new_decl && !b->invisible)
1885 tree old_decl = b->decl;
1887 if (old_decl == error_mark_node)
1889 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1890 new_decl, new_decl);
1891 break;
1893 else if (TREE_CODE (old_decl) == PARM_DECL)
1894 warning (0, "%Jdeclaration of %qD shadows a parameter",
1895 new_decl, new_decl);
1896 else if (DECL_FILE_SCOPE_P (old_decl))
1897 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1898 new_decl, new_decl);
1899 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1900 && DECL_BUILT_IN (old_decl))
1902 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1903 new_decl, new_decl);
1904 break;
1906 else
1907 warning (0, "%Jdeclaration of %qD shadows a previous local",
1908 new_decl, new_decl);
1910 warning (0, "%Jshadowed declaration is here", old_decl);
1912 break;
1917 /* Subroutine of pushdecl.
1919 X is a TYPE_DECL for a typedef statement. Create a brand new
1920 ..._TYPE node (which will be just a variant of the existing
1921 ..._TYPE node with identical properties) and then install X
1922 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1924 The whole point here is to end up with a situation where each
1925 and every ..._TYPE node the compiler creates will be uniquely
1926 associated with AT MOST one node representing a typedef name.
1927 This way, even though the compiler substitutes corresponding
1928 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1929 early on, later parts of the compiler can always do the reverse
1930 translation and get back the corresponding typedef name. For
1931 example, given:
1933 typedef struct S MY_TYPE;
1934 MY_TYPE object;
1936 Later parts of the compiler might only know that `object' was of
1937 type `struct S' if it were not for code just below. With this
1938 code however, later parts of the compiler see something like:
1940 struct S' == struct S
1941 typedef struct S' MY_TYPE;
1942 struct S' object;
1944 And they can then deduce (from the node for type struct S') that
1945 the original object declaration was:
1947 MY_TYPE object;
1949 Being able to do this is important for proper support of protoize,
1950 and also for generating precise symbolic debugging information
1951 which takes full account of the programmer's (typedef) vocabulary.
1953 Obviously, we don't want to generate a duplicate ..._TYPE node if
1954 the TYPE_DECL node that we are now processing really represents a
1955 standard built-in type.
1957 Since all standard types are effectively declared at line zero
1958 in the source file, we can easily check to see if we are working
1959 on a standard type by checking the current value of lineno. */
1961 static void
1962 clone_underlying_type (tree x)
1964 if (DECL_IS_BUILTIN (x))
1966 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1967 TYPE_NAME (TREE_TYPE (x)) = x;
1969 else if (TREE_TYPE (x) != error_mark_node
1970 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1972 tree tt = TREE_TYPE (x);
1973 DECL_ORIGINAL_TYPE (x) = tt;
1974 tt = build_variant_type_copy (tt);
1975 TYPE_NAME (tt) = x;
1976 TREE_USED (tt) = TREE_USED (x);
1977 TREE_TYPE (x) = tt;
1981 /* Record a decl-node X as belonging to the current lexical scope.
1982 Check for errors (such as an incompatible declaration for the same
1983 name already seen in the same scope).
1985 Returns either X or an old decl for the same name.
1986 If an old decl is returned, it may have been smashed
1987 to agree with what X says. */
1989 tree
1990 pushdecl (tree x)
1992 tree name = DECL_NAME (x);
1993 struct c_scope *scope = current_scope;
1994 struct c_binding *b;
1995 bool nested = false;
1997 /* Functions need the lang_decl data. */
1998 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1999 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2001 /* Must set DECL_CONTEXT for everything not at file scope or
2002 DECL_FILE_SCOPE_P won't work. Local externs don't count
2003 unless they have initializers (which generate code). */
2004 if (current_function_decl
2005 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2006 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2007 DECL_CONTEXT (x) = current_function_decl;
2009 /* If this is of variably modified type, prevent jumping into its
2010 scope. */
2011 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2012 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2013 c_begin_vm_scope (scope->depth);
2015 /* Anonymous decls are just inserted in the scope. */
2016 if (!name)
2018 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2019 return x;
2022 /* First, see if there is another declaration with the same name in
2023 the current scope. If there is, duplicate_decls may do all the
2024 work for us. If duplicate_decls returns false, that indicates
2025 two incompatible decls in the same scope; we are to silently
2026 replace the old one (duplicate_decls has issued all appropriate
2027 diagnostics). In particular, we should not consider possible
2028 duplicates in the external scope, or shadowing. */
2029 b = I_SYMBOL_BINDING (name);
2030 if (b && B_IN_SCOPE (b, scope))
2032 struct c_binding *b_ext, *b_use;
2033 tree type = TREE_TYPE (x);
2034 tree visdecl = b->decl;
2035 tree vistype = TREE_TYPE (visdecl);
2036 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2037 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2038 b->inner_comp = false;
2039 b_use = b;
2040 b_ext = b;
2041 /* If this is an external linkage declaration, we should check
2042 for compatibility with the type in the external scope before
2043 setting the type at this scope based on the visible
2044 information only. */
2045 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2047 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2048 b_ext = b_ext->shadowed;
2049 if (b_ext)
2051 b_use = b_ext;
2052 if (b_use->type)
2053 TREE_TYPE (b_use->decl) = b_use->type;
2056 if (duplicate_decls (x, b_use->decl))
2058 if (b_use != b)
2060 /* Save the updated type in the external scope and
2061 restore the proper type for this scope. */
2062 tree thistype;
2063 if (comptypes (vistype, type))
2064 thistype = composite_type (vistype, type);
2065 else
2066 thistype = TREE_TYPE (b_use->decl);
2067 b_use->type = TREE_TYPE (b_use->decl);
2068 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2069 && DECL_BUILT_IN (b_use->decl))
2070 thistype
2071 = build_type_attribute_variant (thistype,
2072 TYPE_ATTRIBUTES
2073 (b_use->type));
2074 TREE_TYPE (b_use->decl) = thistype;
2076 return b_use->decl;
2078 else
2079 goto skip_external_and_shadow_checks;
2082 /* All declarations with external linkage, and all external
2083 references, go in the external scope, no matter what scope is
2084 current. However, the binding in that scope is ignored for
2085 purposes of normal name lookup. A separate binding structure is
2086 created in the requested scope; this governs the normal
2087 visibility of the symbol.
2089 The binding in the externals scope is used exclusively for
2090 detecting duplicate declarations of the same object, no matter
2091 what scope they are in; this is what we do here. (C99 6.2.7p2:
2092 All declarations that refer to the same object or function shall
2093 have compatible type; otherwise, the behavior is undefined.) */
2094 if (DECL_EXTERNAL (x) || scope == file_scope)
2096 tree type = TREE_TYPE (x);
2097 tree vistype = 0;
2098 tree visdecl = 0;
2099 bool type_saved = false;
2100 if (b && !B_IN_EXTERNAL_SCOPE (b)
2101 && (TREE_CODE (b->decl) == FUNCTION_DECL
2102 || TREE_CODE (b->decl) == VAR_DECL)
2103 && DECL_FILE_SCOPE_P (b->decl))
2105 visdecl = b->decl;
2106 vistype = TREE_TYPE (visdecl);
2108 if (warn_nested_externs
2109 && scope != file_scope
2110 && !DECL_IN_SYSTEM_HEADER (x))
2111 warning (0, "nested extern declaration of %qD", x);
2113 while (b && !B_IN_EXTERNAL_SCOPE (b))
2115 /* If this decl might be modified, save its type. This is
2116 done here rather than when the decl is first bound
2117 because the type may change after first binding, through
2118 being completed or through attributes being added. If we
2119 encounter multiple such decls, only the first should have
2120 its type saved; the others will already have had their
2121 proper types saved and the types will not have changed as
2122 their scopes will not have been re-entered. */
2123 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2125 b->type = TREE_TYPE (b->decl);
2126 type_saved = true;
2128 if (B_IN_FILE_SCOPE (b)
2129 && TREE_CODE (b->decl) == VAR_DECL
2130 && TREE_STATIC (b->decl)
2131 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2132 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2133 && TREE_CODE (type) == ARRAY_TYPE
2134 && TYPE_DOMAIN (type)
2135 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2136 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2138 /* Array type completed in inner scope, which should be
2139 diagnosed if the completion does not have size 1 and
2140 it does not get completed in the file scope. */
2141 b->inner_comp = true;
2143 b = b->shadowed;
2146 /* If a matching external declaration has been found, set its
2147 type to the composite of all the types of that declaration.
2148 After the consistency checks, it will be reset to the
2149 composite of the visible types only. */
2150 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2151 && b->type)
2152 TREE_TYPE (b->decl) = b->type;
2154 /* The point of the same_translation_unit_p check here is,
2155 we want to detect a duplicate decl for a construct like
2156 foo() { extern bar(); } ... static bar(); but not if
2157 they are in different translation units. In any case,
2158 the static does not go in the externals scope. */
2159 if (b
2160 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2161 && duplicate_decls (x, b->decl))
2163 tree thistype;
2164 if (vistype)
2166 if (comptypes (vistype, type))
2167 thistype = composite_type (vistype, type);
2168 else
2169 thistype = TREE_TYPE (b->decl);
2171 else
2172 thistype = type;
2173 b->type = TREE_TYPE (b->decl);
2174 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2175 thistype
2176 = build_type_attribute_variant (thistype,
2177 TYPE_ATTRIBUTES (b->type));
2178 TREE_TYPE (b->decl) = thistype;
2179 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2180 return b->decl;
2182 else if (TREE_PUBLIC (x))
2184 if (visdecl && !b && duplicate_decls (x, visdecl))
2186 /* An external declaration at block scope referring to a
2187 visible entity with internal linkage. The composite
2188 type will already be correct for this scope, so we
2189 just need to fall through to make the declaration in
2190 this scope. */
2191 nested = true;
2192 x = visdecl;
2194 else
2196 bind (name, x, external_scope, /*invisible=*/true,
2197 /*nested=*/false);
2198 nested = true;
2203 warn_if_shadowing (x);
2205 skip_external_and_shadow_checks:
2206 if (TREE_CODE (x) == TYPE_DECL)
2207 clone_underlying_type (x);
2209 bind (name, x, scope, /*invisible=*/false, nested);
2211 /* If x's type is incomplete because it's based on a
2212 structure or union which has not yet been fully declared,
2213 attach it to that structure or union type, so we can go
2214 back and complete the variable declaration later, if the
2215 structure or union gets fully declared.
2217 If the input is erroneous, we can have error_mark in the type
2218 slot (e.g. "f(void a, ...)") - that doesn't count as an
2219 incomplete type. */
2220 if (TREE_TYPE (x) != error_mark_node
2221 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2223 tree element = TREE_TYPE (x);
2225 while (TREE_CODE (element) == ARRAY_TYPE)
2226 element = TREE_TYPE (element);
2227 element = TYPE_MAIN_VARIANT (element);
2229 if ((TREE_CODE (element) == RECORD_TYPE
2230 || TREE_CODE (element) == UNION_TYPE)
2231 && (TREE_CODE (x) != TYPE_DECL
2232 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2233 && !COMPLETE_TYPE_P (element))
2234 C_TYPE_INCOMPLETE_VARS (element)
2235 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2237 return x;
2240 /* Record X as belonging to file scope.
2241 This is used only internally by the Objective-C front end,
2242 and is limited to its needs. duplicate_decls is not called;
2243 if there is any preexisting decl for this identifier, it is an ICE. */
2245 tree
2246 pushdecl_top_level (tree x)
2248 tree name;
2249 bool nested = false;
2250 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2252 name = DECL_NAME (x);
2254 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2256 if (TREE_PUBLIC (x))
2258 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2259 nested = true;
2261 if (file_scope)
2262 bind (name, x, file_scope, /*invisible=*/false, nested);
2264 return x;
2267 static void
2268 implicit_decl_warning (tree id, tree olddecl)
2270 void (*diag) (const char *, ...);
2271 switch (mesg_implicit_function_declaration)
2273 case 0: return;
2274 case 1: diag = warning0; break;
2275 case 2: diag = error; break;
2276 default: gcc_unreachable ();
2279 diag (N_("implicit declaration of function %qE"), id);
2280 if (olddecl)
2281 locate_old_decl (olddecl, diag);
2284 /* Generate an implicit declaration for identifier FUNCTIONID as a
2285 function of type int (). */
2287 tree
2288 implicitly_declare (tree functionid)
2290 struct c_binding *b;
2291 tree decl = 0;
2292 tree asmspec_tree;
2294 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2296 if (B_IN_SCOPE (b, external_scope))
2298 decl = b->decl;
2299 break;
2303 if (decl)
2305 if (decl == error_mark_node)
2306 return decl;
2308 /* FIXME: Objective-C has weird not-really-builtin functions
2309 which are supposed to be visible automatically. They wind up
2310 in the external scope because they're pushed before the file
2311 scope gets created. Catch this here and rebind them into the
2312 file scope. */
2313 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2315 bind (functionid, decl, file_scope,
2316 /*invisible=*/false, /*nested=*/true);
2317 return decl;
2319 else
2321 tree newtype = default_function_type;
2322 if (b->type)
2323 TREE_TYPE (decl) = b->type;
2324 /* Implicit declaration of a function already declared
2325 (somehow) in a different scope, or as a built-in.
2326 If this is the first time this has happened, warn;
2327 then recycle the old declaration but with the new type. */
2328 if (!C_DECL_IMPLICIT (decl))
2330 implicit_decl_warning (functionid, decl);
2331 C_DECL_IMPLICIT (decl) = 1;
2333 if (DECL_BUILT_IN (decl))
2335 newtype = build_type_attribute_variant (newtype,
2336 TYPE_ATTRIBUTES
2337 (TREE_TYPE (decl)));
2338 if (!comptypes (newtype, TREE_TYPE (decl)))
2340 warning (0, "incompatible implicit declaration of built-in"
2341 " function %qD", decl);
2342 newtype = TREE_TYPE (decl);
2345 else
2347 if (!comptypes (newtype, TREE_TYPE (decl)))
2349 error ("incompatible implicit declaration of function %qD",
2350 decl);
2351 locate_old_decl (decl, error);
2354 b->type = TREE_TYPE (decl);
2355 TREE_TYPE (decl) = newtype;
2356 bind (functionid, decl, current_scope,
2357 /*invisible=*/false, /*nested=*/true);
2358 return decl;
2362 /* Not seen before. */
2363 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2364 DECL_EXTERNAL (decl) = 1;
2365 TREE_PUBLIC (decl) = 1;
2366 C_DECL_IMPLICIT (decl) = 1;
2367 implicit_decl_warning (functionid, 0);
2368 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2369 if (asmspec_tree)
2370 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2372 /* C89 says implicit declarations are in the innermost block.
2373 So we record the decl in the standard fashion. */
2374 decl = pushdecl (decl);
2376 /* No need to call objc_check_decl here - it's a function type. */
2377 rest_of_decl_compilation (decl, 0, 0);
2379 /* Write a record describing this implicit function declaration
2380 to the prototypes file (if requested). */
2381 gen_aux_info_record (decl, 0, 1, 0);
2383 /* Possibly apply some default attributes to this implicit declaration. */
2384 decl_attributes (&decl, NULL_TREE, 0);
2386 return decl;
2389 /* Issue an error message for a reference to an undeclared variable
2390 ID, including a reference to a builtin outside of function-call
2391 context. Establish a binding of the identifier to error_mark_node
2392 in an appropriate scope, which will suppress further errors for the
2393 same identifier. The error message should be given location LOC. */
2394 void
2395 undeclared_variable (tree id, location_t loc)
2397 static bool already = false;
2398 struct c_scope *scope;
2400 if (current_function_decl == 0)
2402 error ("%H%qE undeclared here (not in a function)", &loc, id);
2403 scope = current_scope;
2405 else
2407 error ("%H%qE undeclared (first use in this function)", &loc, id);
2409 if (!already)
2411 error ("%H(Each undeclared identifier is reported only once", &loc);
2412 error ("%Hfor each function it appears in.)", &loc);
2413 already = true;
2416 /* If we are parsing old-style parameter decls, current_function_decl
2417 will be nonnull but current_function_scope will be null. */
2418 scope = current_function_scope ? current_function_scope : current_scope;
2420 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2423 /* Subroutine of lookup_label, declare_label, define_label: construct a
2424 LABEL_DECL with all the proper frills. */
2426 static tree
2427 make_label (tree name, location_t location)
2429 tree label = build_decl (LABEL_DECL, name, void_type_node);
2431 DECL_CONTEXT (label) = current_function_decl;
2432 DECL_MODE (label) = VOIDmode;
2433 DECL_SOURCE_LOCATION (label) = location;
2435 return label;
2438 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2439 Create one if none exists so far for the current function.
2440 This is called when a label is used in a goto expression or
2441 has its address taken. */
2443 tree
2444 lookup_label (tree name)
2446 tree label;
2448 if (current_function_decl == 0)
2450 error ("label %qE referenced outside of any function", name);
2451 return 0;
2454 /* Use a label already defined or ref'd with this name, but not if
2455 it is inherited from a containing function and wasn't declared
2456 using __label__. */
2457 label = I_LABEL_DECL (name);
2458 if (label && (DECL_CONTEXT (label) == current_function_decl
2459 || C_DECLARED_LABEL_FLAG (label)))
2461 /* If the label has only been declared, update its apparent
2462 location to point here, for better diagnostics if it
2463 turns out not to have been defined. */
2464 if (!TREE_USED (label))
2465 DECL_SOURCE_LOCATION (label) = input_location;
2466 return label;
2469 /* No label binding for that identifier; make one. */
2470 label = make_label (name, input_location);
2472 /* Ordinary labels go in the current function scope. */
2473 bind (name, label, current_function_scope,
2474 /*invisible=*/false, /*nested=*/false);
2475 return label;
2478 /* Make a label named NAME in the current function, shadowing silently
2479 any that may be inherited from containing functions or containing
2480 scopes. This is called for __label__ declarations. */
2482 tree
2483 declare_label (tree name)
2485 struct c_binding *b = I_LABEL_BINDING (name);
2486 tree label;
2488 /* Check to make sure that the label hasn't already been declared
2489 at this scope */
2490 if (b && B_IN_CURRENT_SCOPE (b))
2492 error ("duplicate label declaration %qE", name);
2493 locate_old_decl (b->decl, error);
2495 /* Just use the previous declaration. */
2496 return b->decl;
2499 label = make_label (name, input_location);
2500 C_DECLARED_LABEL_FLAG (label) = 1;
2502 /* Declared labels go in the current scope. */
2503 bind (name, label, current_scope,
2504 /*invisible=*/false, /*nested=*/false);
2505 return label;
2508 /* Define a label, specifying the location in the source file.
2509 Return the LABEL_DECL node for the label, if the definition is valid.
2510 Otherwise return 0. */
2512 tree
2513 define_label (location_t location, tree name)
2515 /* Find any preexisting label with this name. It is an error
2516 if that label has already been defined in this function, or
2517 if there is a containing function with a declared label with
2518 the same name. */
2519 tree label = I_LABEL_DECL (name);
2520 struct c_label_list *nlist_se, *nlist_vm;
2522 if (label
2523 && ((DECL_CONTEXT (label) == current_function_decl
2524 && DECL_INITIAL (label) != 0)
2525 || (DECL_CONTEXT (label) != current_function_decl
2526 && C_DECLARED_LABEL_FLAG (label))))
2528 error ("%Hduplicate label %qD", &location, label);
2529 locate_old_decl (label, error);
2530 return 0;
2532 else if (label && DECL_CONTEXT (label) == current_function_decl)
2534 /* The label has been used or declared already in this function,
2535 but not defined. Update its location to point to this
2536 definition. */
2537 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2538 error ("%Jjump into statement expression", label);
2539 if (C_DECL_UNDEFINABLE_VM (label))
2540 error ("%Jjump into scope of identifier with variably modified type",
2541 label);
2542 DECL_SOURCE_LOCATION (label) = location;
2544 else
2546 /* No label binding for that identifier; make one. */
2547 label = make_label (name, location);
2549 /* Ordinary labels go in the current function scope. */
2550 bind (name, label, current_function_scope,
2551 /*invisible=*/false, /*nested=*/false);
2554 if (warn_traditional && !in_system_header && lookup_name (name))
2555 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2556 "identifier %qE conflicts", &location, name);
2558 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2559 nlist_se->next = label_context_stack_se->labels_def;
2560 nlist_se->label = label;
2561 label_context_stack_se->labels_def = nlist_se;
2563 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2564 nlist_vm->next = label_context_stack_vm->labels_def;
2565 nlist_vm->label = label;
2566 label_context_stack_vm->labels_def = nlist_vm;
2568 /* Mark label as having been defined. */
2569 DECL_INITIAL (label) = error_mark_node;
2570 return label;
2573 /* Given NAME, an IDENTIFIER_NODE,
2574 return the structure (or union or enum) definition for that name.
2575 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2576 CODE says which kind of type the caller wants;
2577 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2578 If the wrong kind of type is found, an error is reported. */
2580 static tree
2581 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2583 struct c_binding *b = I_TAG_BINDING (name);
2584 int thislevel = 0;
2586 if (!b || !b->decl)
2587 return 0;
2589 /* We only care about whether it's in this level if
2590 thislevel_only was set or it might be a type clash. */
2591 if (thislevel_only || TREE_CODE (b->decl) != code)
2593 /* For our purposes, a tag in the external scope is the same as
2594 a tag in the file scope. (Primarily relevant to Objective-C
2595 and its builtin structure tags, which get pushed before the
2596 file scope is created.) */
2597 if (B_IN_CURRENT_SCOPE (b)
2598 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2599 thislevel = 1;
2602 if (thislevel_only && !thislevel)
2603 return 0;
2605 if (TREE_CODE (b->decl) != code)
2607 /* Definition isn't the kind we were looking for. */
2608 pending_invalid_xref = name;
2609 pending_invalid_xref_location = input_location;
2611 /* If in the same binding level as a declaration as a tag
2612 of a different type, this must not be allowed to
2613 shadow that tag, so give the error immediately.
2614 (For example, "struct foo; union foo;" is invalid.) */
2615 if (thislevel)
2616 pending_xref_error ();
2618 return b->decl;
2621 /* Print an error message now
2622 for a recent invalid struct, union or enum cross reference.
2623 We don't print them immediately because they are not invalid
2624 when used in the `struct foo;' construct for shadowing. */
2626 void
2627 pending_xref_error (void)
2629 if (pending_invalid_xref != 0)
2630 error ("%H%qE defined as wrong kind of tag",
2631 &pending_invalid_xref_location, pending_invalid_xref);
2632 pending_invalid_xref = 0;
2636 /* Look up NAME in the current scope and its superiors
2637 in the namespace of variables, functions and typedefs.
2638 Return a ..._DECL node of some kind representing its definition,
2639 or return 0 if it is undefined. */
2641 tree
2642 lookup_name (tree name)
2644 struct c_binding *b = I_SYMBOL_BINDING (name);
2645 if (b && !b->invisible)
2646 return b->decl;
2647 return 0;
2650 /* Similar to `lookup_name' but look only at the indicated scope. */
2652 static tree
2653 lookup_name_in_scope (tree name, struct c_scope *scope)
2655 struct c_binding *b;
2657 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2658 if (B_IN_SCOPE (b, scope))
2659 return b->decl;
2660 return 0;
2663 /* Create the predefined scalar types of C,
2664 and some nodes representing standard constants (0, 1, (void *) 0).
2665 Initialize the global scope.
2666 Make definitions for built-in primitive functions. */
2668 void
2669 c_init_decl_processing (void)
2671 location_t save_loc = input_location;
2673 /* Initialize reserved words for parser. */
2674 c_parse_init ();
2676 current_function_decl = 0;
2678 gcc_obstack_init (&parser_obstack);
2680 /* Make the externals scope. */
2681 push_scope ();
2682 external_scope = current_scope;
2684 /* Declarations from c_common_nodes_and_builtins must not be associated
2685 with this input file, lest we get differences between using and not
2686 using preprocessed headers. */
2687 #ifdef USE_MAPPED_LOCATION
2688 input_location = BUILTINS_LOCATION;
2689 #else
2690 input_location.file = "<built-in>";
2691 input_location.line = 0;
2692 #endif
2694 build_common_tree_nodes (flag_signed_char, false);
2696 c_common_nodes_and_builtins ();
2698 /* In C, comparisons and TRUTH_* expressions have type int. */
2699 truthvalue_type_node = integer_type_node;
2700 truthvalue_true_node = integer_one_node;
2701 truthvalue_false_node = integer_zero_node;
2703 /* Even in C99, which has a real boolean type. */
2704 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2705 boolean_type_node));
2707 input_location = save_loc;
2709 pedantic_lvalues = true;
2711 make_fname_decl = c_make_fname_decl;
2712 start_fname_decls ();
2715 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2716 decl, NAME is the initialization string and TYPE_DEP indicates whether
2717 NAME depended on the type of the function. As we don't yet implement
2718 delayed emission of static data, we mark the decl as emitted
2719 so it is not placed in the output. Anything using it must therefore pull
2720 out the STRING_CST initializer directly. FIXME. */
2722 static tree
2723 c_make_fname_decl (tree id, int type_dep)
2725 const char *name = fname_as_string (type_dep);
2726 tree decl, type, init;
2727 size_t length = strlen (name);
2729 type = build_array_type (char_type_node,
2730 build_index_type (size_int (length)));
2731 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2733 decl = build_decl (VAR_DECL, id, type);
2735 TREE_STATIC (decl) = 1;
2736 TREE_READONLY (decl) = 1;
2737 DECL_ARTIFICIAL (decl) = 1;
2739 init = build_string (length + 1, name);
2740 free ((char *) name);
2741 TREE_TYPE (init) = type;
2742 DECL_INITIAL (decl) = init;
2744 TREE_USED (decl) = 1;
2746 if (current_function_decl)
2748 DECL_CONTEXT (decl) = current_function_decl;
2749 bind (id, decl, current_function_scope,
2750 /*invisible=*/false, /*nested=*/false);
2753 finish_decl (decl, init, NULL_TREE);
2755 return decl;
2758 /* Return a definition for a builtin function named NAME and whose data type
2759 is TYPE. TYPE should be a function type with argument types.
2760 FUNCTION_CODE tells later passes how to compile calls to this function.
2761 See tree.h for its possible values.
2763 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2764 the name to be called if we can't opencode the function. If
2765 ATTRS is nonzero, use that for the function's attribute list. */
2767 tree
2768 builtin_function (const char *name, tree type, int function_code,
2769 enum built_in_class cl, const char *library_name,
2770 tree attrs)
2772 tree id = get_identifier (name);
2773 tree decl = build_decl (FUNCTION_DECL, id, type);
2774 TREE_PUBLIC (decl) = 1;
2775 DECL_EXTERNAL (decl) = 1;
2776 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2777 DECL_BUILT_IN_CLASS (decl) = cl;
2778 DECL_FUNCTION_CODE (decl) = function_code;
2779 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2780 if (library_name)
2781 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2783 /* Should never be called on a symbol with a preexisting meaning. */
2784 gcc_assert (!I_SYMBOL_BINDING (id));
2786 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2788 /* Builtins in the implementation namespace are made visible without
2789 needing to be explicitly declared. See push_file_scope. */
2790 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2792 TREE_CHAIN (decl) = visible_builtins;
2793 visible_builtins = decl;
2796 /* Possibly apply some default attributes to this built-in function. */
2797 if (attrs)
2798 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2799 else
2800 decl_attributes (&decl, NULL_TREE, 0);
2802 return decl;
2805 /* Called when a declaration is seen that contains no names to declare.
2806 If its type is a reference to a structure, union or enum inherited
2807 from a containing scope, shadow that tag name for the current scope
2808 with a forward reference.
2809 If its type defines a new named structure or union
2810 or defines an enum, it is valid but we need not do anything here.
2811 Otherwise, it is an error. */
2813 void
2814 shadow_tag (const struct c_declspecs *declspecs)
2816 shadow_tag_warned (declspecs, 0);
2819 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2820 but no pedwarn. */
2821 void
2822 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2824 bool found_tag = false;
2826 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2828 tree value = declspecs->type;
2829 enum tree_code code = TREE_CODE (value);
2831 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2832 /* Used to test also that TYPE_SIZE (value) != 0.
2833 That caused warning for `struct foo;' at top level in the file. */
2835 tree name = TYPE_NAME (value);
2836 tree t;
2838 found_tag = true;
2840 if (name == 0)
2842 if (warned != 1 && code != ENUMERAL_TYPE)
2843 /* Empty unnamed enum OK */
2845 pedwarn ("unnamed struct/union that defines no instances");
2846 warned = 1;
2849 else if (!declspecs->tag_defined_p
2850 && declspecs->storage_class != csc_none)
2852 if (warned != 1)
2853 pedwarn ("empty declaration with storage class specifier "
2854 "does not redeclare tag");
2855 warned = 1;
2856 pending_xref_error ();
2858 else if (!declspecs->tag_defined_p
2859 && (declspecs->const_p
2860 || declspecs->volatile_p
2861 || declspecs->restrict_p))
2863 if (warned != 1)
2864 pedwarn ("empty declaration with type qualifier "
2865 "does not redeclare tag");
2866 warned = 1;
2867 pending_xref_error ();
2869 else
2871 pending_invalid_xref = 0;
2872 t = lookup_tag (code, name, 1);
2874 if (t == 0)
2876 t = make_node (code);
2877 pushtag (name, t);
2881 else
2883 if (warned != 1 && !in_system_header)
2885 pedwarn ("useless type name in empty declaration");
2886 warned = 1;
2890 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2892 pedwarn ("useless type name in empty declaration");
2893 warned = 1;
2896 pending_invalid_xref = 0;
2898 if (declspecs->inline_p)
2900 error ("%<inline%> in empty declaration");
2901 warned = 1;
2904 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2906 error ("%<auto%> in file-scope empty declaration");
2907 warned = 1;
2910 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2912 error ("%<register%> in file-scope empty declaration");
2913 warned = 1;
2916 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2918 warning (0, "useless storage class specifier in empty declaration");
2919 warned = 2;
2922 if (!warned && !in_system_header && declspecs->thread_p)
2924 warning (0, "useless %<__thread%> in empty declaration");
2925 warned = 2;
2928 if (!warned && !in_system_header && (declspecs->const_p
2929 || declspecs->volatile_p
2930 || declspecs->restrict_p))
2932 warning (0, "useless type qualifier in empty declaration");
2933 warned = 2;
2936 if (warned != 1)
2938 if (!found_tag)
2939 pedwarn ("empty declaration");
2944 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2945 bits. SPECS represents declaration specifiers that the grammar
2946 only permits to contain type qualifiers and attributes. */
2949 quals_from_declspecs (const struct c_declspecs *specs)
2951 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2952 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2953 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2954 gcc_assert (!specs->type
2955 && !specs->decl_attr
2956 && specs->typespec_word == cts_none
2957 && specs->storage_class == csc_none
2958 && !specs->typedef_p
2959 && !specs->explicit_signed_p
2960 && !specs->deprecated_p
2961 && !specs->long_p
2962 && !specs->long_long_p
2963 && !specs->short_p
2964 && !specs->signed_p
2965 && !specs->unsigned_p
2966 && !specs->complex_p
2967 && !specs->inline_p
2968 && !specs->thread_p);
2969 return quals;
2972 /* Construct an array declarator. EXPR is the expression inside [], or
2973 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2974 to the pointer to which a parameter array is converted). STATIC_P is
2975 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2976 is true if the array is [*], a VLA of unspecified length which is
2977 nevertheless a complete type (not currently implemented by GCC),
2978 false otherwise. The field for the contained declarator is left to be
2979 filled in by set_array_declarator_inner. */
2981 struct c_declarator *
2982 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2983 bool vla_unspec_p)
2985 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2986 struct c_declarator);
2987 declarator->kind = cdk_array;
2988 declarator->declarator = 0;
2989 declarator->u.array.dimen = expr;
2990 if (quals)
2992 declarator->u.array.attrs = quals->attrs;
2993 declarator->u.array.quals = quals_from_declspecs (quals);
2995 else
2997 declarator->u.array.attrs = NULL_TREE;
2998 declarator->u.array.quals = 0;
3000 declarator->u.array.static_p = static_p;
3001 declarator->u.array.vla_unspec_p = vla_unspec_p;
3002 if (pedantic && !flag_isoc99)
3004 if (static_p || quals != NULL)
3005 pedwarn ("ISO C90 does not support %<static%> or type "
3006 "qualifiers in parameter array declarators");
3007 if (vla_unspec_p)
3008 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3010 if (vla_unspec_p)
3011 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3012 return declarator;
3015 /* Set the contained declarator of an array declarator. DECL is the
3016 declarator, as constructed by build_array_declarator; INNER is what
3017 appears on the left of the []. ABSTRACT_P is true if it is an
3018 abstract declarator, false otherwise; this is used to reject static
3019 and type qualifiers in abstract declarators, where they are not in
3020 the C99 grammar (subject to possible change in DR#289). */
3022 struct c_declarator *
3023 set_array_declarator_inner (struct c_declarator *decl,
3024 struct c_declarator *inner, bool abstract_p)
3026 decl->declarator = inner;
3027 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3028 || decl->u.array.attrs != NULL_TREE
3029 || decl->u.array.static_p))
3030 error ("static or type qualifiers in abstract declarator");
3031 return decl;
3034 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3036 tree
3037 groktypename (struct c_type_name *type_name)
3039 tree type;
3040 tree attrs = type_name->specs->attrs;
3042 type_name->specs->attrs = NULL_TREE;
3044 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3045 false, NULL);
3047 /* Apply attributes. */
3048 decl_attributes (&type, attrs, 0);
3050 return type;
3053 /* Decode a declarator in an ordinary declaration or data definition.
3054 This is called as soon as the type information and variable name
3055 have been parsed, before parsing the initializer if any.
3056 Here we create the ..._DECL node, fill in its type,
3057 and put it on the list of decls for the current context.
3058 The ..._DECL node is returned as the value.
3060 Exception: for arrays where the length is not specified,
3061 the type is left null, to be filled in by `finish_decl'.
3063 Function definitions do not come here; they go to start_function
3064 instead. However, external and forward declarations of functions
3065 do go through here. Structure field declarations are done by
3066 grokfield and not through here. */
3068 tree
3069 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3070 bool initialized, tree attributes)
3072 tree decl;
3073 tree tem;
3075 /* An object declared as __attribute__((deprecated)) suppresses
3076 warnings of uses of other deprecated items. */
3077 if (lookup_attribute ("deprecated", attributes))
3078 deprecated_state = DEPRECATED_SUPPRESS;
3080 decl = grokdeclarator (declarator, declspecs,
3081 NORMAL, initialized, NULL);
3082 if (!decl)
3083 return 0;
3085 deprecated_state = DEPRECATED_NORMAL;
3087 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3088 && MAIN_NAME_P (DECL_NAME (decl)))
3089 warning (0, "%J%qD is usually a function", decl, decl);
3091 if (initialized)
3092 /* Is it valid for this decl to have an initializer at all?
3093 If not, set INITIALIZED to zero, which will indirectly
3094 tell 'finish_decl' to ignore the initializer once it is parsed. */
3095 switch (TREE_CODE (decl))
3097 case TYPE_DECL:
3098 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3099 initialized = 0;
3100 break;
3102 case FUNCTION_DECL:
3103 error ("function %qD is initialized like a variable", decl);
3104 initialized = 0;
3105 break;
3107 case PARM_DECL:
3108 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3109 error ("parameter %qD is initialized", decl);
3110 initialized = 0;
3111 break;
3113 default:
3114 /* Don't allow initializations for incomplete types except for
3115 arrays which might be completed by the initialization. */
3117 /* This can happen if the array size is an undefined macro.
3118 We already gave a warning, so we don't need another one. */
3119 if (TREE_TYPE (decl) == error_mark_node)
3120 initialized = 0;
3121 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3123 /* A complete type is ok if size is fixed. */
3125 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3126 || C_DECL_VARIABLE_SIZE (decl))
3128 error ("variable-sized object may not be initialized");
3129 initialized = 0;
3132 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3134 error ("variable %qD has initializer but incomplete type", decl);
3135 initialized = 0;
3137 else if (C_DECL_VARIABLE_SIZE (decl))
3139 /* Although C99 is unclear about whether incomplete arrays
3140 of VLAs themselves count as VLAs, it does not make
3141 sense to permit them to be initialized given that
3142 ordinary VLAs may not be initialized. */
3143 error ("variable-sized object may not be initialized");
3144 initialized = 0;
3148 if (initialized)
3150 if (current_scope == file_scope)
3151 TREE_STATIC (decl) = 1;
3153 /* Tell 'pushdecl' this is an initialized decl
3154 even though we don't yet have the initializer expression.
3155 Also tell 'finish_decl' it may store the real initializer. */
3156 DECL_INITIAL (decl) = error_mark_node;
3159 /* If this is a function declaration, write a record describing it to the
3160 prototypes file (if requested). */
3162 if (TREE_CODE (decl) == FUNCTION_DECL)
3163 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3165 /* ANSI specifies that a tentative definition which is not merged with
3166 a non-tentative definition behaves exactly like a definition with an
3167 initializer equal to zero. (Section 3.7.2)
3169 -fno-common gives strict ANSI behavior, though this tends to break
3170 a large body of code that grew up without this rule.
3172 Thread-local variables are never common, since there's no entrenched
3173 body of code to break, and it allows more efficient variable references
3174 in the presence of dynamic linking. */
3176 if (TREE_CODE (decl) == VAR_DECL
3177 && !initialized
3178 && TREE_PUBLIC (decl)
3179 && !DECL_THREAD_LOCAL (decl)
3180 && !flag_no_common)
3181 DECL_COMMON (decl) = 1;
3183 /* Set attributes here so if duplicate decl, will have proper attributes. */
3184 decl_attributes (&decl, attributes, 0);
3186 if (TREE_CODE (decl) == FUNCTION_DECL
3187 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3189 struct c_declarator *ce = declarator;
3191 if (ce->kind == cdk_pointer)
3192 ce = declarator->declarator;
3193 if (ce->kind == cdk_function)
3195 tree args = ce->u.arg_info->parms;
3196 for (; args; args = TREE_CHAIN (args))
3198 tree type = TREE_TYPE (args);
3199 if (type && INTEGRAL_TYPE_P (type)
3200 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3201 DECL_ARG_TYPE (args) = integer_type_node;
3206 if (TREE_CODE (decl) == FUNCTION_DECL
3207 && DECL_DECLARED_INLINE_P (decl)
3208 && DECL_UNINLINABLE (decl)
3209 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3210 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3212 /* Add this decl to the current scope.
3213 TEM may equal DECL or it may be a previous decl of the same name. */
3214 tem = pushdecl (decl);
3216 if (initialized && DECL_EXTERNAL (tem))
3218 DECL_EXTERNAL (tem) = 0;
3219 TREE_STATIC (tem) = 1;
3222 return tem;
3225 /* Finish processing of a declaration;
3226 install its initial value.
3227 If the length of an array type is not known before,
3228 it must be determined now, from the initial value, or it is an error. */
3230 void
3231 finish_decl (tree decl, tree init, tree asmspec_tree)
3233 tree type = TREE_TYPE (decl);
3234 int was_incomplete = (DECL_SIZE (decl) == 0);
3235 const char *asmspec = 0;
3237 /* If a name was specified, get the string. */
3238 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3239 && DECL_FILE_SCOPE_P (decl))
3240 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3241 if (asmspec_tree)
3242 asmspec = TREE_STRING_POINTER (asmspec_tree);
3244 /* If `start_decl' didn't like having an initialization, ignore it now. */
3245 if (init != 0 && DECL_INITIAL (decl) == 0)
3246 init = 0;
3248 /* Don't crash if parm is initialized. */
3249 if (TREE_CODE (decl) == PARM_DECL)
3250 init = 0;
3252 if (init)
3253 store_init_value (decl, init);
3255 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3256 || TREE_CODE (decl) == FUNCTION_DECL
3257 || TREE_CODE (decl) == FIELD_DECL))
3258 objc_check_decl (decl);
3260 /* Deduce size of array from initialization, if not already known. */
3261 if (TREE_CODE (type) == ARRAY_TYPE
3262 && TYPE_DOMAIN (type) == 0
3263 && TREE_CODE (decl) != TYPE_DECL)
3265 bool do_default
3266 = (TREE_STATIC (decl)
3267 /* Even if pedantic, an external linkage array
3268 may have incomplete type at first. */
3269 ? pedantic && !TREE_PUBLIC (decl)
3270 : !DECL_EXTERNAL (decl));
3271 int failure
3272 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3273 do_default);
3275 /* Get the completed type made by complete_array_type. */
3276 type = TREE_TYPE (decl);
3278 if (failure == 1)
3279 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3281 else if (failure == 2)
3283 if (do_default)
3284 error ("%Jarray size missing in %qD", decl, decl);
3285 /* If a `static' var's size isn't known,
3286 make it extern as well as static, so it does not get
3287 allocated.
3288 If it is not `static', then do not mark extern;
3289 finish_incomplete_decl will give it a default size
3290 and it will get allocated. */
3291 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3292 DECL_EXTERNAL (decl) = 1;
3294 else if (failure == 3)
3295 error ("%Jzero or negative size array %qD", decl, decl);
3297 if (DECL_INITIAL (decl))
3298 TREE_TYPE (DECL_INITIAL (decl)) = type;
3300 layout_decl (decl, 0);
3303 if (TREE_CODE (decl) == VAR_DECL)
3305 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3306 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3307 layout_decl (decl, 0);
3309 if (DECL_SIZE (decl) == 0
3310 /* Don't give an error if we already gave one earlier. */
3311 && TREE_TYPE (decl) != error_mark_node
3312 && (TREE_STATIC (decl)
3313 /* A static variable with an incomplete type
3314 is an error if it is initialized.
3315 Also if it is not file scope.
3316 Otherwise, let it through, but if it is not `extern'
3317 then it may cause an error message later. */
3318 ? (DECL_INITIAL (decl) != 0
3319 || !DECL_FILE_SCOPE_P (decl))
3320 /* An automatic variable with an incomplete type
3321 is an error. */
3322 : !DECL_EXTERNAL (decl)))
3324 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3325 TREE_TYPE (decl) = error_mark_node;
3328 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3329 && DECL_SIZE (decl) != 0)
3331 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3332 constant_expression_warning (DECL_SIZE (decl));
3333 else
3334 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3337 if (TREE_USED (type))
3338 TREE_USED (decl) = 1;
3341 /* If this is a function and an assembler name is specified, reset DECL_RTL
3342 so we can give it its new name. Also, update built_in_decls if it
3343 was a normal built-in. */
3344 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3346 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3347 set_builtin_user_assembler_name (decl, asmspec);
3348 set_user_assembler_name (decl, asmspec);
3351 /* If #pragma weak was used, mark the decl weak now. */
3352 maybe_apply_pragma_weak (decl);
3354 /* If this is a variable definition, determine its ELF visibility. */
3355 if (TREE_CODE (decl) == VAR_DECL
3356 && TREE_STATIC (decl)
3357 && !DECL_EXTERNAL (decl))
3358 c_determine_visibility (decl);
3360 /* Output the assembler code and/or RTL code for variables and functions,
3361 unless the type is an undefined structure or union.
3362 If not, it will get done when the type is completed. */
3364 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3366 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3367 if (c_dialect_objc ())
3368 objc_check_decl (decl);
3370 if (asmspec)
3372 /* If this is not a static variable, issue a warning.
3373 It doesn't make any sense to give an ASMSPEC for an
3374 ordinary, non-register local variable. Historically,
3375 GCC has accepted -- but ignored -- the ASMSPEC in
3376 this case. */
3377 if (!DECL_FILE_SCOPE_P (decl)
3378 && TREE_CODE (decl) == VAR_DECL
3379 && !C_DECL_REGISTER (decl)
3380 && !TREE_STATIC (decl))
3381 warning (0, "%Jignoring asm-specifier for non-static local "
3382 "variable %qD", decl, decl);
3383 else if (C_DECL_REGISTER (decl))
3384 change_decl_assembler_name (decl, get_identifier (asmspec));
3385 else
3386 set_user_assembler_name (decl, asmspec);
3389 if (DECL_FILE_SCOPE_P (decl))
3391 if (DECL_INITIAL (decl) == NULL_TREE
3392 || DECL_INITIAL (decl) == error_mark_node)
3393 /* Don't output anything
3394 when a tentative file-scope definition is seen.
3395 But at end of compilation, do output code for them. */
3396 DECL_DEFER_OUTPUT (decl) = 1;
3397 rest_of_decl_compilation (decl, true, 0);
3399 else
3401 /* In conjunction with an ASMSPEC, the `register'
3402 keyword indicates that we should place the variable
3403 in a particular register. */
3404 if (asmspec && C_DECL_REGISTER (decl))
3406 DECL_HARD_REGISTER (decl) = 1;
3407 /* This cannot be done for a structure with volatile
3408 fields, on which DECL_REGISTER will have been
3409 reset. */
3410 if (!DECL_REGISTER (decl))
3411 error ("cannot put object with volatile field into register");
3414 if (TREE_CODE (decl) != FUNCTION_DECL)
3416 /* If we're building a variable sized type, and we might be
3417 reachable other than via the top of the current binding
3418 level, then create a new BIND_EXPR so that we deallocate
3419 the object at the right time. */
3420 /* Note that DECL_SIZE can be null due to errors. */
3421 if (DECL_SIZE (decl)
3422 && !TREE_CONSTANT (DECL_SIZE (decl))
3423 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3425 tree bind;
3426 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3427 TREE_SIDE_EFFECTS (bind) = 1;
3428 add_stmt (bind);
3429 BIND_EXPR_BODY (bind) = push_stmt_list ();
3431 add_stmt (build_stmt (DECL_EXPR, decl));
3436 if (!DECL_FILE_SCOPE_P (decl))
3438 /* Recompute the RTL of a local array now
3439 if it used to be an incomplete type. */
3440 if (was_incomplete
3441 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3443 /* If we used it already as memory, it must stay in memory. */
3444 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3445 /* If it's still incomplete now, no init will save it. */
3446 if (DECL_SIZE (decl) == 0)
3447 DECL_INITIAL (decl) = 0;
3452 /* If this was marked 'used', be sure it will be output. */
3453 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3454 mark_decl_referenced (decl);
3456 if (TREE_CODE (decl) == TYPE_DECL)
3458 if (!DECL_FILE_SCOPE_P (decl)
3459 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3460 add_stmt (build_stmt (DECL_EXPR, decl));
3462 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3465 /* At the end of a declaration, throw away any variable type sizes
3466 of types defined inside that declaration. There is no use
3467 computing them in the following function definition. */
3468 if (current_scope == file_scope)
3469 get_pending_sizes ();
3471 /* Install a cleanup (aka destructor) if one was given. */
3472 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3474 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3475 if (attr)
3477 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3478 tree cleanup_decl = lookup_name (cleanup_id);
3479 tree cleanup;
3481 /* Build "cleanup(&decl)" for the destructor. */
3482 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3483 cleanup = build_tree_list (NULL_TREE, cleanup);
3484 cleanup = build_function_call (cleanup_decl, cleanup);
3486 /* Don't warn about decl unused; the cleanup uses it. */
3487 TREE_USED (decl) = 1;
3488 TREE_USED (cleanup_decl) = 1;
3490 /* Initialize EH, if we've been told to do so. */
3491 if (flag_exceptions && !c_eh_initialized_p)
3493 c_eh_initialized_p = true;
3494 eh_personality_libfunc
3495 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3496 ? "__gcc_personality_sj0"
3497 : "__gcc_personality_v0");
3498 using_eh_for_cleanups ();
3501 push_cleanup (decl, cleanup, false);
3506 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3508 tree
3509 grokparm (const struct c_parm *parm)
3511 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3512 NULL);
3514 decl_attributes (&decl, parm->attrs, 0);
3516 return decl;
3519 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3520 and push that on the current scope. */
3522 void
3523 push_parm_decl (const struct c_parm *parm)
3525 tree decl;
3527 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3528 decl_attributes (&decl, parm->attrs, 0);
3530 decl = pushdecl (decl);
3532 finish_decl (decl, NULL_TREE, NULL_TREE);
3535 /* Mark all the parameter declarations to date as forward decls.
3536 Also diagnose use of this extension. */
3538 void
3539 mark_forward_parm_decls (void)
3541 struct c_binding *b;
3543 if (pedantic && !current_scope->warned_forward_parm_decls)
3545 pedwarn ("ISO C forbids forward parameter declarations");
3546 current_scope->warned_forward_parm_decls = true;
3549 for (b = current_scope->bindings; b; b = b->prev)
3550 if (TREE_CODE (b->decl) == PARM_DECL)
3551 TREE_ASM_WRITTEN (b->decl) = 1;
3554 static GTY(()) int compound_literal_number;
3556 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3557 literal, which may be an incomplete array type completed by the
3558 initializer; INIT is a CONSTRUCTOR that initializes the compound
3559 literal. */
3561 tree
3562 build_compound_literal (tree type, tree init)
3564 /* We do not use start_decl here because we have a type, not a declarator;
3565 and do not use finish_decl because the decl should be stored inside
3566 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3567 tree decl;
3568 tree complit;
3569 tree stmt;
3571 if (type == error_mark_node)
3572 return error_mark_node;
3574 decl = build_decl (VAR_DECL, NULL_TREE, type);
3575 DECL_EXTERNAL (decl) = 0;
3576 TREE_PUBLIC (decl) = 0;
3577 TREE_STATIC (decl) = (current_scope == file_scope);
3578 DECL_CONTEXT (decl) = current_function_decl;
3579 TREE_USED (decl) = 1;
3580 TREE_TYPE (decl) = type;
3581 TREE_READONLY (decl) = TYPE_READONLY (type);
3582 store_init_value (decl, init);
3584 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3586 int failure = complete_array_type (&TREE_TYPE (decl),
3587 DECL_INITIAL (decl), true);
3588 gcc_assert (!failure);
3590 type = TREE_TYPE (decl);
3591 TREE_TYPE (DECL_INITIAL (decl)) = type;
3594 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3595 return error_mark_node;
3597 stmt = build_stmt (DECL_EXPR, decl);
3598 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3599 TREE_SIDE_EFFECTS (complit) = 1;
3601 layout_decl (decl, 0);
3603 if (TREE_STATIC (decl))
3605 /* This decl needs a name for the assembler output. We also need
3606 a unique suffix to be added to the name. */
3607 char *name;
3609 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3610 compound_literal_number);
3611 compound_literal_number++;
3612 DECL_NAME (decl) = get_identifier (name);
3613 DECL_DEFER_OUTPUT (decl) = 1;
3614 DECL_COMDAT (decl) = 1;
3615 DECL_ARTIFICIAL (decl) = 1;
3616 DECL_IGNORED_P (decl) = 1;
3617 pushdecl (decl);
3618 rest_of_decl_compilation (decl, 1, 0);
3621 return complit;
3624 /* Determine whether TYPE is a structure with a flexible array member,
3625 or a union containing such a structure (possibly recursively). */
3627 static bool
3628 flexible_array_type_p (tree type)
3630 tree x;
3631 switch (TREE_CODE (type))
3633 case RECORD_TYPE:
3634 x = TYPE_FIELDS (type);
3635 if (x == NULL_TREE)
3636 return false;
3637 while (TREE_CHAIN (x) != NULL_TREE)
3638 x = TREE_CHAIN (x);
3639 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3640 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3641 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3642 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3643 return true;
3644 return false;
3645 case UNION_TYPE:
3646 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3648 if (flexible_array_type_p (TREE_TYPE (x)))
3649 return true;
3651 return false;
3652 default:
3653 return false;
3657 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3658 replacing with appropriate values if they are invalid. */
3659 static void
3660 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3662 tree type_mv;
3663 unsigned int max_width;
3664 unsigned HOST_WIDE_INT w;
3665 const char *name = orig_name ? orig_name: _("<anonymous>");
3667 /* Detect and ignore out of range field width and process valid
3668 field widths. */
3669 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3670 || TREE_CODE (*width) != INTEGER_CST)
3672 error ("bit-field %qs width not an integer constant", name);
3673 *width = integer_one_node;
3675 else
3677 constant_expression_warning (*width);
3678 if (tree_int_cst_sgn (*width) < 0)
3680 error ("negative width in bit-field %qs", name);
3681 *width = integer_one_node;
3683 else if (integer_zerop (*width) && orig_name)
3685 error ("zero width for bit-field %qs", name);
3686 *width = integer_one_node;
3690 /* Detect invalid bit-field type. */
3691 if (TREE_CODE (*type) != INTEGER_TYPE
3692 && TREE_CODE (*type) != BOOLEAN_TYPE
3693 && TREE_CODE (*type) != ENUMERAL_TYPE)
3695 error ("bit-field %qs has invalid type", name);
3696 *type = unsigned_type_node;
3699 type_mv = TYPE_MAIN_VARIANT (*type);
3700 if (pedantic
3701 && type_mv != integer_type_node
3702 && type_mv != unsigned_type_node
3703 && type_mv != boolean_type_node)
3704 pedwarn ("type of bit-field %qs is a GCC extension", name);
3706 if (type_mv == boolean_type_node)
3707 max_width = CHAR_TYPE_SIZE;
3708 else
3709 max_width = TYPE_PRECISION (*type);
3711 if (0 < compare_tree_int (*width, max_width))
3713 error ("width of %qs exceeds its type", name);
3714 w = max_width;
3715 *width = build_int_cst (NULL_TREE, w);
3717 else
3718 w = tree_low_cst (*width, 1);
3720 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3722 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3723 if (!lt
3724 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3725 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3726 warning (0, "%qs is narrower than values of its type", name);
3730 /* Given declspecs and a declarator,
3731 determine the name and type of the object declared
3732 and construct a ..._DECL node for it.
3733 (In one case we can return a ..._TYPE node instead.
3734 For invalid input we sometimes return 0.)
3736 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3738 DECL_CONTEXT says which syntactic context this declaration is in:
3739 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3740 FUNCDEF for a function definition. Like NORMAL but a few different
3741 error messages in each case. Return value may be zero meaning
3742 this definition is too screwy to try to parse.
3743 PARM for a parameter declaration (either within a function prototype
3744 or before a function body). Make a PARM_DECL, or return void_type_node.
3745 TYPENAME if for a typename (in a cast or sizeof).
3746 Don't make a DECL node; just return the ..._TYPE node.
3747 FIELD for a struct or union field; make a FIELD_DECL.
3748 INITIALIZED is true if the decl has an initializer.
3749 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3750 representing the width of the bit-field.
3752 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3753 It may also be so in the PARM case, for a prototype where the
3754 argument type is specified but not the name.
3756 This function is where the complicated C meanings of `static'
3757 and `extern' are interpreted. */
3759 static tree
3760 grokdeclarator (const struct c_declarator *declarator,
3761 struct c_declspecs *declspecs,
3762 enum decl_context decl_context, bool initialized, tree *width)
3764 tree type = declspecs->type;
3765 bool threadp = declspecs->thread_p;
3766 enum c_storage_class storage_class = declspecs->storage_class;
3767 int constp;
3768 int restrictp;
3769 int volatilep;
3770 int type_quals = TYPE_UNQUALIFIED;
3771 const char *name, *orig_name;
3772 tree typedef_type = 0;
3773 int funcdef_flag = 0;
3774 bool funcdef_syntax = false;
3775 int size_varies = 0;
3776 tree decl_attr = declspecs->decl_attr;
3777 int array_ptr_quals = TYPE_UNQUALIFIED;
3778 tree array_ptr_attrs = NULL_TREE;
3779 int array_parm_static = 0;
3780 tree returned_attrs = NULL_TREE;
3781 bool bitfield = width != NULL;
3782 tree element_type;
3783 struct c_arg_info *arg_info = 0;
3785 if (decl_context == FUNCDEF)
3786 funcdef_flag = 1, decl_context = NORMAL;
3788 /* Look inside a declarator for the name being declared
3789 and get it as a string, for an error message. */
3791 const struct c_declarator *decl = declarator;
3792 name = 0;
3794 while (decl)
3795 switch (decl->kind)
3797 case cdk_function:
3798 case cdk_array:
3799 case cdk_pointer:
3800 funcdef_syntax = (decl->kind == cdk_function);
3801 decl = decl->declarator;
3802 break;
3804 case cdk_attrs:
3805 decl = decl->declarator;
3806 break;
3808 case cdk_id:
3809 if (decl->u.id)
3810 name = IDENTIFIER_POINTER (decl->u.id);
3811 decl = 0;
3812 break;
3814 default:
3815 gcc_unreachable ();
3817 orig_name = name;
3818 if (name == 0)
3819 name = "type name";
3822 /* A function definition's declarator must have the form of
3823 a function declarator. */
3825 if (funcdef_flag && !funcdef_syntax)
3826 return 0;
3828 /* If this looks like a function definition, make it one,
3829 even if it occurs where parms are expected.
3830 Then store_parm_decls will reject it and not use it as a parm. */
3831 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3832 decl_context = PARM;
3834 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3835 warn_deprecated_use (declspecs->type);
3837 typedef_type = type;
3838 size_varies = C_TYPE_VARIABLE_SIZE (type);
3840 /* Diagnose defaulting to "int". */
3842 if (declspecs->default_int_p && !in_system_header)
3844 /* Issue a warning if this is an ISO C 99 program or if
3845 -Wreturn-type and this is a function, or if -Wimplicit;
3846 prefer the former warning since it is more explicit. */
3847 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3848 && funcdef_flag)
3849 warn_about_return_type = 1;
3850 else if (warn_implicit_int || flag_isoc99)
3851 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3854 /* Adjust the type if a bit-field is being declared,
3855 -funsigned-bitfields applied and the type is not explicitly
3856 "signed". */
3857 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3858 && TREE_CODE (type) == INTEGER_TYPE)
3859 type = c_common_unsigned_type (type);
3861 /* Figure out the type qualifiers for the declaration. There are
3862 two ways a declaration can become qualified. One is something
3863 like `const int i' where the `const' is explicit. Another is
3864 something like `typedef const int CI; CI i' where the type of the
3865 declaration contains the `const'. A third possibility is that
3866 there is a type qualifier on the element type of a typedefed
3867 array type, in which case we should extract that qualifier so
3868 that c_apply_type_quals_to_decls receives the full list of
3869 qualifiers to work with (C90 is not entirely clear about whether
3870 duplicate qualifiers should be diagnosed in this case, but it
3871 seems most appropriate to do so). */
3872 element_type = strip_array_types (type);
3873 constp = declspecs->const_p + TYPE_READONLY (element_type);
3874 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3875 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3876 if (pedantic && !flag_isoc99)
3878 if (constp > 1)
3879 pedwarn ("duplicate %<const%>");
3880 if (restrictp > 1)
3881 pedwarn ("duplicate %<restrict%>");
3882 if (volatilep > 1)
3883 pedwarn ("duplicate %<volatile%>");
3885 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3886 type = TYPE_MAIN_VARIANT (type);
3887 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3888 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3889 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3891 /* Warn about storage classes that are invalid for certain
3892 kinds of declarations (parameters, typenames, etc.). */
3894 if (funcdef_flag
3895 && (threadp
3896 || storage_class == csc_auto
3897 || storage_class == csc_register
3898 || storage_class == csc_typedef))
3900 if (storage_class == csc_auto
3901 && (pedantic || current_scope == file_scope))
3902 pedwarn ("function definition declared %<auto%>");
3903 if (storage_class == csc_register)
3904 error ("function definition declared %<register%>");
3905 if (storage_class == csc_typedef)
3906 error ("function definition declared %<typedef%>");
3907 if (threadp)
3908 error ("function definition declared %<__thread%>");
3909 threadp = false;
3910 if (storage_class == csc_auto
3911 || storage_class == csc_register
3912 || storage_class == csc_typedef)
3913 storage_class = csc_none;
3915 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3917 if (decl_context == PARM && storage_class == csc_register)
3919 else
3921 switch (decl_context)
3923 case FIELD:
3924 error ("storage class specified for structure field %qs",
3925 name);
3926 break;
3927 case PARM:
3928 error ("storage class specified for parameter %qs", name);
3929 break;
3930 default:
3931 error ("storage class specified for typename");
3932 break;
3934 storage_class = csc_none;
3935 threadp = false;
3938 else if (storage_class == csc_extern
3939 && initialized
3940 && !funcdef_flag)
3942 /* 'extern' with initialization is invalid if not at file scope. */
3943 if (current_scope == file_scope)
3944 warning (0, "%qs initialized and declared %<extern%>", name);
3945 else
3946 error ("%qs has both %<extern%> and initializer", name);
3948 else if (current_scope == file_scope)
3950 if (storage_class == csc_auto)
3951 error ("file-scope declaration of %qs specifies %<auto%>", name);
3952 if (pedantic && storage_class == csc_register)
3953 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3955 else
3957 if (storage_class == csc_extern && funcdef_flag)
3958 error ("nested function %qs declared %<extern%>", name);
3959 else if (threadp && storage_class == csc_none)
3961 error ("function-scope %qs implicitly auto and declared "
3962 "%<__thread%>",
3963 name);
3964 threadp = false;
3968 /* Now figure out the structure of the declarator proper.
3969 Descend through it, creating more complex types, until we reach
3970 the declared identifier (or NULL_TREE, in an absolute declarator).
3971 At each stage we maintain an unqualified version of the type
3972 together with any qualifiers that should be applied to it with
3973 c_build_qualified_type; this way, array types including
3974 multidimensional array types are first built up in unqualified
3975 form and then the qualified form is created with
3976 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3978 while (declarator && declarator->kind != cdk_id)
3980 if (type == error_mark_node)
3982 declarator = declarator->declarator;
3983 continue;
3986 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3987 a cdk_pointer (for *...),
3988 a cdk_function (for ...(...)),
3989 a cdk_attrs (for nested attributes),
3990 or a cdk_id (for the name being declared
3991 or the place in an absolute declarator
3992 where the name was omitted).
3993 For the last case, we have just exited the loop.
3995 At this point, TYPE is the type of elements of an array,
3996 or for a function to return, or for a pointer to point to.
3997 After this sequence of ifs, TYPE is the type of the
3998 array or function or pointer, and DECLARATOR has had its
3999 outermost layer removed. */
4001 if (array_ptr_quals != TYPE_UNQUALIFIED
4002 || array_ptr_attrs != NULL_TREE
4003 || array_parm_static)
4005 /* Only the innermost declarator (making a parameter be of
4006 array type which is converted to pointer type)
4007 may have static or type qualifiers. */
4008 error ("static or type qualifiers in non-parameter array declarator");
4009 array_ptr_quals = TYPE_UNQUALIFIED;
4010 array_ptr_attrs = NULL_TREE;
4011 array_parm_static = 0;
4014 switch (declarator->kind)
4016 case cdk_attrs:
4018 /* A declarator with embedded attributes. */
4019 tree attrs = declarator->u.attrs;
4020 const struct c_declarator *inner_decl;
4021 int attr_flags = 0;
4022 declarator = declarator->declarator;
4023 inner_decl = declarator;
4024 while (inner_decl->kind == cdk_attrs)
4025 inner_decl = inner_decl->declarator;
4026 if (inner_decl->kind == cdk_id)
4027 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4028 else if (inner_decl->kind == cdk_function)
4029 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4030 else if (inner_decl->kind == cdk_array)
4031 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4032 returned_attrs = decl_attributes (&type,
4033 chainon (returned_attrs, attrs),
4034 attr_flags);
4035 break;
4037 case cdk_array:
4039 tree itype = NULL_TREE;
4040 tree size = declarator->u.array.dimen;
4041 /* The index is a signed object `sizetype' bits wide. */
4042 tree index_type = c_common_signed_type (sizetype);
4044 array_ptr_quals = declarator->u.array.quals;
4045 array_ptr_attrs = declarator->u.array.attrs;
4046 array_parm_static = declarator->u.array.static_p;
4048 declarator = declarator->declarator;
4050 /* Check for some types that there cannot be arrays of. */
4052 if (VOID_TYPE_P (type))
4054 error ("declaration of %qs as array of voids", name);
4055 type = error_mark_node;
4058 if (TREE_CODE (type) == FUNCTION_TYPE)
4060 error ("declaration of %qs as array of functions", name);
4061 type = error_mark_node;
4064 if (pedantic && !in_system_header && flexible_array_type_p (type))
4065 pedwarn ("invalid use of structure with flexible array member");
4067 if (size == error_mark_node)
4068 type = error_mark_node;
4070 if (type == error_mark_node)
4071 continue;
4073 /* If size was specified, set ITYPE to a range-type for
4074 that size. Otherwise, ITYPE remains null. finish_decl
4075 may figure it out from an initial value. */
4077 if (size)
4079 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4080 lvalue. */
4081 STRIP_TYPE_NOPS (size);
4083 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4085 error ("size of array %qs has non-integer type", name);
4086 size = integer_one_node;
4089 if (pedantic && integer_zerop (size))
4090 pedwarn ("ISO C forbids zero-size array %qs", name);
4092 if (TREE_CODE (size) == INTEGER_CST)
4094 constant_expression_warning (size);
4095 if (tree_int_cst_sgn (size) < 0)
4097 error ("size of array %qs is negative", name);
4098 size = integer_one_node;
4101 else
4103 /* Make sure the array size remains visibly
4104 nonconstant even if it is (eg) a const variable
4105 with known value. */
4106 size_varies = 1;
4108 if (!flag_isoc99 && pedantic)
4110 if (TREE_CONSTANT (size))
4111 pedwarn ("ISO C90 forbids array %qs whose size "
4112 "can%'t be evaluated",
4113 name);
4114 else
4115 pedwarn ("ISO C90 forbids variable-size array %qs",
4116 name);
4120 if (integer_zerop (size))
4122 /* A zero-length array cannot be represented with
4123 an unsigned index type, which is what we'll
4124 get with build_index_type. Create an
4125 open-ended range instead. */
4126 itype = build_range_type (sizetype, size, NULL_TREE);
4128 else
4130 /* Arrange for the SAVE_EXPR on the inside of the
4131 MINUS_EXPR, which allows the -1 to get folded
4132 with the +1 that happens when building TYPE_SIZE. */
4133 if (size_varies)
4134 size = variable_size (size);
4136 /* Compute the maximum valid index, that is, size
4137 - 1. Do the calculation in index_type, so that
4138 if it is a variable the computations will be
4139 done in the proper mode. */
4140 itype = fold (build2 (MINUS_EXPR, index_type,
4141 convert (index_type, size),
4142 convert (index_type,
4143 size_one_node)));
4145 /* If that overflowed, the array is too big. ???
4146 While a size of INT_MAX+1 technically shouldn't
4147 cause an overflow (because we subtract 1), the
4148 overflow is recorded during the conversion to
4149 index_type, before the subtraction. Handling
4150 this case seems like an unnecessary
4151 complication. */
4152 if (TREE_OVERFLOW (itype))
4154 error ("size of array %qs is too large", name);
4155 type = error_mark_node;
4156 continue;
4159 itype = build_index_type (itype);
4162 else if (decl_context == FIELD)
4164 if (pedantic && !flag_isoc99 && !in_system_header)
4165 pedwarn ("ISO C90 does not support flexible array members");
4167 /* ISO C99 Flexible array members are effectively
4168 identical to GCC's zero-length array extension. */
4169 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4172 /* Complain about arrays of incomplete types. */
4173 if (!COMPLETE_TYPE_P (type))
4175 error ("array type has incomplete element type");
4176 type = error_mark_node;
4178 else
4179 type = build_array_type (type, itype);
4181 if (size_varies)
4182 C_TYPE_VARIABLE_SIZE (type) = 1;
4184 /* The GCC extension for zero-length arrays differs from
4185 ISO flexible array members in that sizeof yields
4186 zero. */
4187 if (size && integer_zerop (size))
4189 TYPE_SIZE (type) = bitsize_zero_node;
4190 TYPE_SIZE_UNIT (type) = size_zero_node;
4193 if (decl_context != PARM
4194 && (array_ptr_quals != TYPE_UNQUALIFIED
4195 || array_ptr_attrs != NULL_TREE
4196 || array_parm_static))
4198 error ("static or type qualifiers in non-parameter array declarator");
4199 array_ptr_quals = TYPE_UNQUALIFIED;
4200 array_ptr_attrs = NULL_TREE;
4201 array_parm_static = 0;
4203 break;
4205 case cdk_function:
4207 /* Say it's a definition only for the declarator closest
4208 to the identifier, apart possibly from some
4209 attributes. */
4210 bool really_funcdef = false;
4211 tree arg_types;
4212 if (funcdef_flag)
4214 const struct c_declarator *t = declarator->declarator;
4215 while (t->kind == cdk_attrs)
4216 t = t->declarator;
4217 really_funcdef = (t->kind == cdk_id);
4220 /* Declaring a function type. Make sure we have a valid
4221 type for the function to return. */
4222 if (type == error_mark_node)
4223 continue;
4225 size_varies = 0;
4227 /* Warn about some types functions can't return. */
4228 if (TREE_CODE (type) == FUNCTION_TYPE)
4230 error ("%qs declared as function returning a function", name);
4231 type = integer_type_node;
4233 if (TREE_CODE (type) == ARRAY_TYPE)
4235 error ("%qs declared as function returning an array", name);
4236 type = integer_type_node;
4239 /* Construct the function type and go to the next
4240 inner layer of declarator. */
4241 arg_info = declarator->u.arg_info;
4242 arg_types = grokparms (arg_info, really_funcdef);
4244 /* Type qualifiers before the return type of the function
4245 qualify the return type, not the function type. */
4246 if (type_quals)
4248 /* Type qualifiers on a function return type are
4249 normally permitted by the standard but have no
4250 effect, so give a warning at -Wreturn-type.
4251 Qualifiers on a void return type are banned on
4252 function definitions in ISO C; GCC used to used
4253 them for noreturn functions. */
4254 if (VOID_TYPE_P (type) && really_funcdef)
4255 pedwarn ("function definition has qualified void return type");
4256 else if (warn_return_type)
4257 warning (0, "type qualifiers ignored on function return type");
4259 type = c_build_qualified_type (type, type_quals);
4261 type_quals = TYPE_UNQUALIFIED;
4263 type = build_function_type (type, arg_types);
4264 declarator = declarator->declarator;
4266 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4267 the formal parameter list of this FUNCTION_TYPE to point to
4268 the FUNCTION_TYPE node itself. */
4270 tree link;
4272 for (link = arg_info->tags;
4273 link;
4274 link = TREE_CHAIN (link))
4275 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4277 break;
4279 case cdk_pointer:
4281 /* Merge any constancy or volatility into the target type
4282 for the pointer. */
4284 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4285 && type_quals)
4286 pedwarn ("ISO C forbids qualified function types");
4287 if (type_quals)
4288 type = c_build_qualified_type (type, type_quals);
4289 size_varies = 0;
4291 type = build_pointer_type (type);
4293 /* Process type qualifiers (such as const or volatile)
4294 that were given inside the `*'. */
4295 type_quals = declarator->u.pointer_quals;
4297 declarator = declarator->declarator;
4298 break;
4300 default:
4301 gcc_unreachable ();
4305 /* Now TYPE has the actual type, apart from any qualifiers in
4306 TYPE_QUALS. */
4308 /* Check the type and width of a bit-field. */
4309 if (bitfield)
4310 check_bitfield_type_and_width (&type, width, orig_name);
4312 /* Did array size calculations overflow? */
4314 if (TREE_CODE (type) == ARRAY_TYPE
4315 && COMPLETE_TYPE_P (type)
4316 && TREE_OVERFLOW (TYPE_SIZE (type)))
4318 error ("size of array %qs is too large", name);
4319 /* If we proceed with the array type as it is, we'll eventually
4320 crash in tree_low_cst(). */
4321 type = error_mark_node;
4324 /* If this is declaring a typedef name, return a TYPE_DECL. */
4326 if (storage_class == csc_typedef)
4328 tree decl;
4329 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4330 && type_quals)
4331 pedwarn ("ISO C forbids qualified function types");
4332 if (type_quals)
4333 type = c_build_qualified_type (type, type_quals);
4334 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4335 if (declspecs->explicit_signed_p)
4336 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4337 decl_attributes (&decl, returned_attrs, 0);
4338 if (declspecs->inline_p)
4339 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4340 return decl;
4343 /* Detect the case of an array type of unspecified size
4344 which came, as such, direct from a typedef name.
4345 We must copy the type, so that each identifier gets
4346 a distinct type, so that each identifier's size can be
4347 controlled separately by its own initializer. */
4349 if (type != 0 && typedef_type != 0
4350 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4351 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4353 type = build_array_type (TREE_TYPE (type), 0);
4354 if (size_varies)
4355 C_TYPE_VARIABLE_SIZE (type) = 1;
4358 /* If this is a type name (such as, in a cast or sizeof),
4359 compute the type and return it now. */
4361 if (decl_context == TYPENAME)
4363 /* Note that the grammar rejects storage classes in typenames
4364 and fields. */
4365 gcc_assert (storage_class == csc_none && !threadp
4366 && !declspecs->inline_p);
4367 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4368 && type_quals)
4369 pedwarn ("ISO C forbids const or volatile function types");
4370 if (type_quals)
4371 type = c_build_qualified_type (type, type_quals);
4372 decl_attributes (&type, returned_attrs, 0);
4373 return type;
4376 /* Aside from typedefs and type names (handle above),
4377 `void' at top level (not within pointer)
4378 is allowed only in public variables.
4379 We don't complain about parms either, but that is because
4380 a better error message can be made later. */
4382 if (VOID_TYPE_P (type) && decl_context != PARM
4383 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4384 && (storage_class == csc_extern
4385 || (current_scope == file_scope
4386 && !(storage_class == csc_static
4387 || storage_class == csc_register)))))
4389 error ("variable or field %qs declared void", name);
4390 type = integer_type_node;
4393 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4394 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4397 tree decl;
4399 if (decl_context == PARM)
4401 tree type_as_written;
4402 tree promoted_type;
4404 /* A parameter declared as an array of T is really a pointer to T.
4405 One declared as a function is really a pointer to a function. */
4407 if (TREE_CODE (type) == ARRAY_TYPE)
4409 /* Transfer const-ness of array into that of type pointed to. */
4410 type = TREE_TYPE (type);
4411 if (type_quals)
4412 type = c_build_qualified_type (type, type_quals);
4413 type = build_pointer_type (type);
4414 type_quals = array_ptr_quals;
4416 /* We don't yet implement attributes in this context. */
4417 if (array_ptr_attrs != NULL_TREE)
4418 warning (0, "attributes in parameter array declarator ignored");
4420 size_varies = 0;
4422 else if (TREE_CODE (type) == FUNCTION_TYPE)
4424 if (pedantic && type_quals)
4425 pedwarn ("ISO C forbids qualified function types");
4426 if (type_quals)
4427 type = c_build_qualified_type (type, type_quals);
4428 type = build_pointer_type (type);
4429 type_quals = TYPE_UNQUALIFIED;
4431 else if (type_quals)
4432 type = c_build_qualified_type (type, type_quals);
4434 type_as_written = type;
4436 decl = build_decl (PARM_DECL, declarator->u.id, type);
4437 if (size_varies)
4438 C_DECL_VARIABLE_SIZE (decl) = 1;
4440 /* Compute the type actually passed in the parmlist,
4441 for the case where there is no prototype.
4442 (For example, shorts and chars are passed as ints.)
4443 When there is a prototype, this is overridden later. */
4445 if (type == error_mark_node)
4446 promoted_type = type;
4447 else
4448 promoted_type = c_type_promotes_to (type);
4450 DECL_ARG_TYPE (decl) = promoted_type;
4451 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4452 if (declspecs->inline_p)
4453 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4455 else if (decl_context == FIELD)
4457 /* Note that the grammar rejects storage classes in typenames
4458 and fields. */
4459 gcc_assert (storage_class == csc_none && !threadp
4460 && !declspecs->inline_p);
4462 /* Structure field. It may not be a function. */
4464 if (TREE_CODE (type) == FUNCTION_TYPE)
4466 error ("field %qs declared as a function", name);
4467 type = build_pointer_type (type);
4469 else if (TREE_CODE (type) != ERROR_MARK
4470 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4472 error ("field %qs has incomplete type", name);
4473 type = error_mark_node;
4475 type = c_build_qualified_type (type, type_quals);
4476 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4477 DECL_NONADDRESSABLE_P (decl) = bitfield;
4479 if (size_varies)
4480 C_DECL_VARIABLE_SIZE (decl) = 1;
4482 else if (TREE_CODE (type) == FUNCTION_TYPE)
4484 if (storage_class == csc_register || threadp)
4486 error ("invalid storage class for function %qs", name);
4488 else if (current_scope != file_scope)
4490 /* Function declaration not at file scope. Storage
4491 classes other than `extern' are not allowed, C99
4492 6.7.1p5, and `extern' makes no difference. However,
4493 GCC allows 'auto', perhaps with 'inline', to support
4494 nested functions. */
4495 if (storage_class == csc_auto)
4497 if (pedantic)
4498 pedwarn ("invalid storage class for function %qs", name);
4500 else if (storage_class == csc_static)
4502 error ("invalid storage class for function %qs", name);
4503 if (funcdef_flag)
4504 storage_class = declspecs->storage_class = csc_none;
4505 else
4506 return 0;
4510 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4511 decl = build_decl_attribute_variant (decl, decl_attr);
4513 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4515 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4516 pedwarn ("ISO C forbids qualified function types");
4518 /* GNU C interprets a volatile-qualified function type to indicate
4519 that the function does not return. */
4520 if ((type_quals & TYPE_QUAL_VOLATILE)
4521 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4522 warning (0, "%<noreturn%> function returns non-void value");
4524 /* Every function declaration is an external reference
4525 (DECL_EXTERNAL) except for those which are not at file
4526 scope and are explicitly declared "auto". This is
4527 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4528 GCC to signify a forward declaration of a nested function. */
4529 if (storage_class == csc_auto && current_scope != file_scope)
4530 DECL_EXTERNAL (decl) = 0;
4531 else
4532 DECL_EXTERNAL (decl) = 1;
4534 /* Record absence of global scope for `static' or `auto'. */
4535 TREE_PUBLIC (decl)
4536 = !(storage_class == csc_static || storage_class == csc_auto);
4538 /* For a function definition, record the argument information
4539 block where store_parm_decls will look for it. */
4540 if (funcdef_flag)
4541 current_function_arg_info = arg_info;
4543 if (declspecs->default_int_p)
4544 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4546 /* Record presence of `inline', if it is reasonable. */
4547 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4549 if (declspecs->inline_p)
4550 pedwarn ("cannot inline function %<main%>");
4552 else if (declspecs->inline_p)
4554 /* Record that the function is declared `inline'. */
4555 DECL_DECLARED_INLINE_P (decl) = 1;
4557 /* Do not mark bare declarations as DECL_INLINE. Doing so
4558 in the presence of multiple declarations can result in
4559 the abstract origin pointing between the declarations,
4560 which will confuse dwarf2out. */
4561 if (initialized)
4563 DECL_INLINE (decl) = 1;
4564 if (storage_class == csc_extern)
4565 current_extern_inline = 1;
4568 /* If -finline-functions, assume it can be inlined. This does
4569 two things: let the function be deferred until it is actually
4570 needed, and let dwarf2 know that the function is inlinable. */
4571 else if (flag_inline_trees == 2 && initialized)
4572 DECL_INLINE (decl) = 1;
4574 else
4576 /* It's a variable. */
4577 /* An uninitialized decl with `extern' is a reference. */
4578 int extern_ref = !initialized && storage_class == csc_extern;
4580 type = c_build_qualified_type (type, type_quals);
4582 /* C99 6.2.2p7: It is invalid (compile-time undefined
4583 behavior) to create an 'extern' declaration for a
4584 variable if there is a global declaration that is
4585 'static' and the global declaration is not visible.
4586 (If the static declaration _is_ currently visible,
4587 the 'extern' declaration is taken to refer to that decl.) */
4588 if (extern_ref && current_scope != file_scope)
4590 tree global_decl = identifier_global_value (declarator->u.id);
4591 tree visible_decl = lookup_name (declarator->u.id);
4593 if (global_decl
4594 && global_decl != visible_decl
4595 && TREE_CODE (global_decl) == VAR_DECL
4596 && !TREE_PUBLIC (global_decl))
4597 error ("variable previously declared %<static%> redeclared "
4598 "%<extern%>");
4601 decl = build_decl (VAR_DECL, declarator->u.id, type);
4602 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4603 if (size_varies)
4604 C_DECL_VARIABLE_SIZE (decl) = 1;
4606 if (declspecs->inline_p)
4607 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4609 /* At file scope, an initialized extern declaration may follow
4610 a static declaration. In that case, DECL_EXTERNAL will be
4611 reset later in start_decl. */
4612 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4614 /* At file scope, the presence of a `static' or `register' storage
4615 class specifier, or the absence of all storage class specifiers
4616 makes this declaration a definition (perhaps tentative). Also,
4617 the absence of both `static' and `register' makes it public. */
4618 if (current_scope == file_scope)
4620 TREE_PUBLIC (decl) = !(storage_class == csc_static
4621 || storage_class == csc_register);
4622 TREE_STATIC (decl) = !extern_ref;
4624 /* Not at file scope, only `static' makes a static definition. */
4625 else
4627 TREE_STATIC (decl) = (storage_class == csc_static);
4628 TREE_PUBLIC (decl) = extern_ref;
4631 if (threadp)
4633 if (targetm.have_tls)
4634 DECL_THREAD_LOCAL (decl) = 1;
4635 else
4636 /* A mere warning is sure to result in improper semantics
4637 at runtime. Don't bother to allow this to compile. */
4638 error ("thread-local storage not supported for this target");
4642 /* Record `register' declaration for warnings on &
4643 and in case doing stupid register allocation. */
4645 if (storage_class == csc_register)
4647 C_DECL_REGISTER (decl) = 1;
4648 DECL_REGISTER (decl) = 1;
4651 /* Record constancy and volatility. */
4652 c_apply_type_quals_to_decl (type_quals, decl);
4654 /* If a type has volatile components, it should be stored in memory.
4655 Otherwise, the fact that those components are volatile
4656 will be ignored, and would even crash the compiler. */
4657 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4659 /* It is not an error for a structure with volatile fields to
4660 be declared register, but reset DECL_REGISTER since it
4661 cannot actually go in a register. */
4662 int was_reg = C_DECL_REGISTER (decl);
4663 C_DECL_REGISTER (decl) = 0;
4664 DECL_REGISTER (decl) = 0;
4665 c_mark_addressable (decl);
4666 C_DECL_REGISTER (decl) = was_reg;
4669 /* This is the earliest point at which we might know the assembler
4670 name of a variable. Thus, if it's known before this, die horribly. */
4671 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4673 decl_attributes (&decl, returned_attrs, 0);
4675 return decl;
4679 /* Decode the parameter-list info for a function type or function definition.
4680 The argument is the value returned by `get_parm_info' (or made in parse.y
4681 if there is an identifier list instead of a parameter decl list).
4682 These two functions are separate because when a function returns
4683 or receives functions then each is called multiple times but the order
4684 of calls is different. The last call to `grokparms' is always the one
4685 that contains the formal parameter names of a function definition.
4687 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4689 FUNCDEF_FLAG is true for a function definition, false for
4690 a mere declaration. A nonempty identifier-list gets an error message
4691 when FUNCDEF_FLAG is false. */
4693 static tree
4694 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4696 tree arg_types = arg_info->types;
4698 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4699 && !in_system_header)
4700 warning (0, "function declaration isn%'t a prototype");
4702 if (arg_types == error_mark_node)
4703 return 0; /* don't set TYPE_ARG_TYPES in this case */
4705 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4707 if (!funcdef_flag)
4708 pedwarn ("parameter names (without types) in function declaration");
4710 arg_info->parms = arg_info->types;
4711 arg_info->types = 0;
4712 return 0;
4714 else
4716 tree parm, type, typelt;
4717 unsigned int parmno;
4719 /* If there is a parameter of incomplete type in a definition,
4720 this is an error. In a declaration this is valid, and a
4721 struct or union type may be completed later, before any calls
4722 or definition of the function. In the case where the tag was
4723 first declared within the parameter list, a warning has
4724 already been given. If a parameter has void type, then
4725 however the function cannot be defined or called, so
4726 warn. */
4728 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4729 parm;
4730 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4732 type = TREE_VALUE (typelt);
4733 if (type == error_mark_node)
4734 continue;
4736 if (!COMPLETE_TYPE_P (type))
4738 if (funcdef_flag)
4740 if (DECL_NAME (parm))
4741 error ("%Jparameter %u (%qD) has incomplete type",
4742 parm, parmno, parm);
4743 else
4744 error ("%Jparameter %u has incomplete type",
4745 parm, parmno);
4747 TREE_VALUE (typelt) = error_mark_node;
4748 TREE_TYPE (parm) = error_mark_node;
4750 else if (VOID_TYPE_P (type))
4752 if (DECL_NAME (parm))
4753 warning (0, "%Jparameter %u (%qD) has void type",
4754 parm, parmno, parm);
4755 else
4756 warning (0, "%Jparameter %u has void type",
4757 parm, parmno);
4761 return arg_types;
4765 /* Take apart the current scope and return a c_arg_info structure with
4766 info on a parameter list just parsed.
4768 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4770 ELLIPSIS being true means the argument list ended in '...' so don't
4771 append a sentinel (void_list_node) to the end of the type-list. */
4773 struct c_arg_info *
4774 get_parm_info (bool ellipsis)
4776 struct c_binding *b = current_scope->bindings;
4777 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4778 struct c_arg_info);
4779 tree parms = 0;
4780 tree tags = 0;
4781 tree types = 0;
4782 tree others = 0;
4784 static bool explained_incomplete_types = false;
4785 bool gave_void_only_once_err = false;
4787 arg_info->parms = 0;
4788 arg_info->tags = 0;
4789 arg_info->types = 0;
4790 arg_info->others = 0;
4792 /* The bindings in this scope must not get put into a block.
4793 We will take care of deleting the binding nodes. */
4794 current_scope->bindings = 0;
4796 /* This function is only called if there was *something* on the
4797 parameter list. */
4798 gcc_assert (b);
4800 /* A parameter list consisting solely of 'void' indicates that the
4801 function takes no arguments. But if the 'void' is qualified
4802 (by 'const' or 'volatile'), or has a storage class specifier
4803 ('register'), then the behavior is undefined; issue an error.
4804 Typedefs for 'void' are OK (see DR#157). */
4805 if (b->prev == 0 /* one binding */
4806 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4807 && !DECL_NAME (b->decl) /* anonymous */
4808 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4810 if (TREE_THIS_VOLATILE (b->decl)
4811 || TREE_READONLY (b->decl)
4812 || C_DECL_REGISTER (b->decl))
4813 error ("%<void%> as only parameter may not be qualified");
4815 /* There cannot be an ellipsis. */
4816 if (ellipsis)
4817 error ("%<void%> must be the only parameter");
4819 arg_info->types = void_list_node;
4820 return arg_info;
4823 if (!ellipsis)
4824 types = void_list_node;
4826 /* Break up the bindings list into parms, tags, types, and others;
4827 apply sanity checks; purge the name-to-decl bindings. */
4828 while (b)
4830 tree decl = b->decl;
4831 tree type = TREE_TYPE (decl);
4832 const char *keyword;
4834 switch (TREE_CODE (decl))
4836 case PARM_DECL:
4837 if (b->id)
4839 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4840 I_SYMBOL_BINDING (b->id) = b->shadowed;
4843 /* Check for forward decls that never got their actual decl. */
4844 if (TREE_ASM_WRITTEN (decl))
4845 error ("%Jparameter %qD has just a forward declaration",
4846 decl, decl);
4847 /* Check for (..., void, ...) and issue an error. */
4848 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4850 if (!gave_void_only_once_err)
4852 error ("%<void%> must be the only parameter");
4853 gave_void_only_once_err = true;
4856 else
4858 /* Valid parameter, add it to the list. */
4859 TREE_CHAIN (decl) = parms;
4860 parms = decl;
4862 /* Since there is a prototype, args are passed in their
4863 declared types. The back end may override this later. */
4864 DECL_ARG_TYPE (decl) = type;
4865 types = tree_cons (0, type, types);
4867 break;
4869 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4870 case UNION_TYPE: keyword = "union"; goto tag;
4871 case RECORD_TYPE: keyword = "struct"; goto tag;
4872 tag:
4873 /* Types may not have tag-names, in which case the type
4874 appears in the bindings list with b->id NULL. */
4875 if (b->id)
4877 gcc_assert (I_TAG_BINDING (b->id) == b);
4878 I_TAG_BINDING (b->id) = b->shadowed;
4881 /* Warn about any struct, union or enum tags defined in a
4882 parameter list. The scope of such types is limited to
4883 the parameter list, which is rarely if ever desirable
4884 (it's impossible to call such a function with type-
4885 correct arguments). An anonymous union parm type is
4886 meaningful as a GNU extension, so don't warn for that. */
4887 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4889 if (b->id)
4890 /* The %s will be one of 'struct', 'union', or 'enum'. */
4891 warning (0, "%<%s %E%> declared inside parameter list",
4892 keyword, b->id);
4893 else
4894 /* The %s will be one of 'struct', 'union', or 'enum'. */
4895 warning (0, "anonymous %s declared inside parameter list",
4896 keyword);
4898 if (!explained_incomplete_types)
4900 warning (0, "its scope is only this definition or declaration,"
4901 " which is probably not what you want");
4902 explained_incomplete_types = true;
4906 tags = tree_cons (b->id, decl, tags);
4907 break;
4909 case CONST_DECL:
4910 case TYPE_DECL:
4911 case FUNCTION_DECL:
4912 /* CONST_DECLs appear here when we have an embedded enum,
4913 and TYPE_DECLs appear here when we have an embedded struct
4914 or union. No warnings for this - we already warned about the
4915 type itself. FUNCTION_DECLs appear when there is an implicit
4916 function declaration in the parameter list. */
4918 TREE_CHAIN (decl) = others;
4919 others = decl;
4920 /* fall through */
4922 case ERROR_MARK:
4923 /* error_mark_node appears here when we have an undeclared
4924 variable. Just throw it away. */
4925 if (b->id)
4927 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4928 I_SYMBOL_BINDING (b->id) = b->shadowed;
4930 break;
4932 /* Other things that might be encountered. */
4933 case LABEL_DECL:
4934 case VAR_DECL:
4935 default:
4936 gcc_unreachable ();
4939 b = free_binding_and_advance (b);
4942 arg_info->parms = parms;
4943 arg_info->tags = tags;
4944 arg_info->types = types;
4945 arg_info->others = others;
4946 return arg_info;
4949 /* Get the struct, enum or union (CODE says which) with tag NAME.
4950 Define the tag as a forward-reference if it is not defined.
4951 Return a c_typespec structure for the type specifier. */
4953 struct c_typespec
4954 parser_xref_tag (enum tree_code code, tree name)
4956 struct c_typespec ret;
4957 /* If a cross reference is requested, look up the type
4958 already defined for this tag and return it. */
4960 tree ref = lookup_tag (code, name, 0);
4961 /* If this is the right type of tag, return what we found.
4962 (This reference will be shadowed by shadow_tag later if appropriate.)
4963 If this is the wrong type of tag, do not return it. If it was the
4964 wrong type in the same scope, we will have had an error
4965 message already; if in a different scope and declaring
4966 a name, pending_xref_error will give an error message; but if in a
4967 different scope and not declaring a name, this tag should
4968 shadow the previous declaration of a different type of tag, and
4969 this would not work properly if we return the reference found.
4970 (For example, with "struct foo" in an outer scope, "union foo;"
4971 must shadow that tag with a new one of union type.) */
4972 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4973 if (ref && TREE_CODE (ref) == code)
4975 ret.spec = ref;
4976 return ret;
4979 /* If no such tag is yet defined, create a forward-reference node
4980 and record it as the "definition".
4981 When a real declaration of this type is found,
4982 the forward-reference will be altered into a real type. */
4984 ref = make_node (code);
4985 if (code == ENUMERAL_TYPE)
4987 /* Give the type a default layout like unsigned int
4988 to avoid crashing if it does not get defined. */
4989 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4990 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4991 TYPE_USER_ALIGN (ref) = 0;
4992 TYPE_UNSIGNED (ref) = 1;
4993 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4994 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4995 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4998 pushtag (name, ref);
5000 ret.spec = ref;
5001 return ret;
5004 /* Get the struct, enum or union (CODE says which) with tag NAME.
5005 Define the tag as a forward-reference if it is not defined.
5006 Return a tree for the type. */
5008 tree
5009 xref_tag (enum tree_code code, tree name)
5011 return parser_xref_tag (code, name).spec;
5014 /* Make sure that the tag NAME is defined *in the current scope*
5015 at least as a forward reference.
5016 CODE says which kind of tag NAME ought to be. */
5018 tree
5019 start_struct (enum tree_code code, tree name)
5021 /* If there is already a tag defined at this scope
5022 (as a forward reference), just return it. */
5024 tree ref = 0;
5026 if (name != 0)
5027 ref = lookup_tag (code, name, 1);
5028 if (ref && TREE_CODE (ref) == code)
5030 if (TYPE_SIZE (ref))
5032 if (code == UNION_TYPE)
5033 error ("redefinition of %<union %E%>", name);
5034 else
5035 error ("redefinition of %<struct %E%>", name);
5037 else if (C_TYPE_BEING_DEFINED (ref))
5039 if (code == UNION_TYPE)
5040 error ("nested redefinition of %<union %E%>", name);
5041 else
5042 error ("nested redefinition of %<struct %E%>", name);
5045 else
5047 /* Otherwise create a forward-reference just so the tag is in scope. */
5049 ref = make_node (code);
5050 pushtag (name, ref);
5053 C_TYPE_BEING_DEFINED (ref) = 1;
5054 TYPE_PACKED (ref) = flag_pack_struct;
5055 return ref;
5058 /* Process the specs, declarator and width (NULL if omitted)
5059 of a structure component, returning a FIELD_DECL node.
5060 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5062 This is done during the parsing of the struct declaration.
5063 The FIELD_DECL nodes are chained together and the lot of them
5064 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5066 tree
5067 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5068 tree width)
5070 tree value;
5072 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5073 && width == NULL_TREE)
5075 /* This is an unnamed decl.
5077 If we have something of the form "union { list } ;" then this
5078 is the anonymous union extension. Similarly for struct.
5080 If this is something of the form "struct foo;", then
5081 If MS extensions are enabled, this is handled as an
5082 anonymous struct.
5083 Otherwise this is a forward declaration of a structure tag.
5085 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5086 If MS extensions are enabled and foo names a structure, then
5087 again this is an anonymous struct.
5088 Otherwise this is an error.
5090 Oh what a horrid tangled web we weave. I wonder if MS consciously
5091 took this from Plan 9 or if it was an accident of implementation
5092 that took root before someone noticed the bug... */
5094 tree type = declspecs->type;
5095 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5096 || TREE_CODE (type) == UNION_TYPE);
5097 bool ok = false;
5099 if (type_ok
5100 && (flag_ms_extensions || !declspecs->typedef_p))
5102 if (flag_ms_extensions)
5103 ok = true;
5104 else if (flag_iso)
5105 ok = false;
5106 else if (TYPE_NAME (type) == NULL)
5107 ok = true;
5108 else
5109 ok = false;
5111 if (!ok)
5113 pedwarn ("declaration does not declare anything");
5114 return NULL_TREE;
5116 if (pedantic)
5117 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5120 value = grokdeclarator (declarator, declspecs, FIELD, false,
5121 width ? &width : NULL);
5123 finish_decl (value, NULL_TREE, NULL_TREE);
5124 DECL_INITIAL (value) = width;
5126 return value;
5129 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5130 the list such that this does not present a problem later. */
5132 static void
5133 detect_field_duplicates (tree fieldlist)
5135 tree x, y;
5136 int timeout = 10;
5138 /* First, see if there are more than "a few" fields.
5139 This is trivially true if there are zero or one fields. */
5140 if (!fieldlist)
5141 return;
5142 x = TREE_CHAIN (fieldlist);
5143 if (!x)
5144 return;
5145 do {
5146 timeout--;
5147 x = TREE_CHAIN (x);
5148 } while (timeout > 0 && x);
5150 /* If there were "few" fields, avoid the overhead of allocating
5151 a hash table. Instead just do the nested traversal thing. */
5152 if (timeout > 0)
5154 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5155 if (DECL_NAME (x))
5157 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5158 if (DECL_NAME (y) == DECL_NAME (x))
5160 error ("%Jduplicate member %qD", x, x);
5161 DECL_NAME (x) = NULL_TREE;
5165 else
5167 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5168 void **slot;
5170 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5171 if ((y = DECL_NAME (x)) != 0)
5173 slot = htab_find_slot (htab, y, INSERT);
5174 if (*slot)
5176 error ("%Jduplicate member %qD", x, x);
5177 DECL_NAME (x) = NULL_TREE;
5179 *slot = y;
5182 htab_delete (htab);
5186 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5187 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5188 ATTRIBUTES are attributes to be applied to the structure. */
5190 tree
5191 finish_struct (tree t, tree fieldlist, tree attributes)
5193 tree x;
5194 bool toplevel = file_scope == current_scope;
5195 int saw_named_field;
5197 /* If this type was previously laid out as a forward reference,
5198 make sure we lay it out again. */
5200 TYPE_SIZE (t) = 0;
5202 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5204 if (pedantic)
5206 for (x = fieldlist; x; x = TREE_CHAIN (x))
5207 if (DECL_NAME (x) != 0)
5208 break;
5210 if (x == 0)
5212 if (TREE_CODE (t) == UNION_TYPE)
5214 if (fieldlist)
5215 pedwarn ("union has no named members");
5216 else
5217 pedwarn ("union has no members");
5219 else
5221 if (fieldlist)
5222 pedwarn ("struct has no named members");
5223 else
5224 pedwarn ("struct has no members");
5229 /* Install struct as DECL_CONTEXT of each field decl.
5230 Also process specified field sizes, found in the DECL_INITIAL,
5231 storing 0 there after the type has been changed to precision equal
5232 to its width, rather than the precision of the specified standard
5233 type. (Correct layout requires the original type to have been preserved
5234 until now.) */
5236 saw_named_field = 0;
5237 for (x = fieldlist; x; x = TREE_CHAIN (x))
5239 DECL_CONTEXT (x) = t;
5240 DECL_PACKED (x) |= TYPE_PACKED (t);
5242 /* If any field is const, the structure type is pseudo-const. */
5243 if (TREE_READONLY (x))
5244 C_TYPE_FIELDS_READONLY (t) = 1;
5245 else
5247 /* A field that is pseudo-const makes the structure likewise. */
5248 tree t1 = TREE_TYPE (x);
5249 while (TREE_CODE (t1) == ARRAY_TYPE)
5250 t1 = TREE_TYPE (t1);
5251 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5252 && C_TYPE_FIELDS_READONLY (t1))
5253 C_TYPE_FIELDS_READONLY (t) = 1;
5256 /* Any field that is volatile means variables of this type must be
5257 treated in some ways as volatile. */
5258 if (TREE_THIS_VOLATILE (x))
5259 C_TYPE_FIELDS_VOLATILE (t) = 1;
5261 /* Any field of nominal variable size implies structure is too. */
5262 if (C_DECL_VARIABLE_SIZE (x))
5263 C_TYPE_VARIABLE_SIZE (t) = 1;
5265 if (DECL_INITIAL (x))
5267 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5268 DECL_SIZE (x) = bitsize_int (width);
5269 DECL_BIT_FIELD (x) = 1;
5270 SET_DECL_C_BIT_FIELD (x);
5273 /* Detect flexible array member in an invalid context. */
5274 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5275 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5276 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5277 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5279 if (TREE_CODE (t) == UNION_TYPE)
5281 error ("%Jflexible array member in union", x);
5282 TREE_TYPE (x) = error_mark_node;
5284 else if (TREE_CHAIN (x) != NULL_TREE)
5286 error ("%Jflexible array member not at end of struct", x);
5287 TREE_TYPE (x) = error_mark_node;
5289 else if (!saw_named_field)
5291 error ("%Jflexible array member in otherwise empty struct", x);
5292 TREE_TYPE (x) = error_mark_node;
5296 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5297 && flexible_array_type_p (TREE_TYPE (x)))
5298 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5300 if (DECL_NAME (x))
5301 saw_named_field = 1;
5304 detect_field_duplicates (fieldlist);
5306 /* Now we have the nearly final fieldlist. Record it,
5307 then lay out the structure or union (including the fields). */
5309 TYPE_FIELDS (t) = fieldlist;
5311 layout_type (t);
5313 /* Give bit-fields their proper types. */
5315 tree *fieldlistp = &fieldlist;
5316 while (*fieldlistp)
5317 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5318 && TREE_TYPE (*fieldlistp) != error_mark_node)
5320 unsigned HOST_WIDE_INT width
5321 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5322 tree type = TREE_TYPE (*fieldlistp);
5323 if (width != TYPE_PRECISION (type))
5325 TREE_TYPE (*fieldlistp)
5326 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5327 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5329 DECL_INITIAL (*fieldlistp) = 0;
5331 else
5332 fieldlistp = &TREE_CHAIN (*fieldlistp);
5335 /* Now we have the truly final field list.
5336 Store it in this type and in the variants. */
5338 TYPE_FIELDS (t) = fieldlist;
5340 /* If there are lots of fields, sort so we can look through them fast.
5341 We arbitrarily consider 16 or more elts to be "a lot". */
5344 int len = 0;
5346 for (x = fieldlist; x; x = TREE_CHAIN (x))
5348 if (len > 15 || DECL_NAME (x) == NULL)
5349 break;
5350 len += 1;
5353 if (len > 15)
5355 tree *field_array;
5356 struct lang_type *space;
5357 struct sorted_fields_type *space2;
5359 len += list_length (x);
5361 /* Use the same allocation policy here that make_node uses, to
5362 ensure that this lives as long as the rest of the struct decl.
5363 All decls in an inline function need to be saved. */
5365 space = GGC_CNEW (struct lang_type);
5366 space2 = GGC_NEWVAR (struct sorted_fields_type,
5367 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5369 len = 0;
5370 space->s = space2;
5371 field_array = &space2->elts[0];
5372 for (x = fieldlist; x; x = TREE_CHAIN (x))
5374 field_array[len++] = x;
5376 /* If there is anonymous struct or union, break out of the loop. */
5377 if (DECL_NAME (x) == NULL)
5378 break;
5380 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5381 if (x == NULL)
5383 TYPE_LANG_SPECIFIC (t) = space;
5384 TYPE_LANG_SPECIFIC (t)->s->len = len;
5385 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5386 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5391 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5393 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5394 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5395 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5396 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5399 /* If this was supposed to be a transparent union, but we can't
5400 make it one, warn and turn off the flag. */
5401 if (TREE_CODE (t) == UNION_TYPE
5402 && TYPE_TRANSPARENT_UNION (t)
5403 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5405 TYPE_TRANSPARENT_UNION (t) = 0;
5406 warning (0, "union cannot be made transparent");
5409 /* If this structure or union completes the type of any previous
5410 variable declaration, lay it out and output its rtl. */
5411 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5413 x = TREE_CHAIN (x))
5415 tree decl = TREE_VALUE (x);
5416 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5417 layout_array_type (TREE_TYPE (decl));
5418 if (TREE_CODE (decl) != TYPE_DECL)
5420 layout_decl (decl, 0);
5421 if (c_dialect_objc ())
5422 objc_check_decl (decl);
5423 rest_of_decl_compilation (decl, toplevel, 0);
5424 if (!toplevel)
5425 expand_decl (decl);
5428 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5430 /* Finish debugging output for this type. */
5431 rest_of_type_compilation (t, toplevel);
5433 /* If we're inside a function proper, i.e. not file-scope and not still
5434 parsing parameters, then arrange for the size of a variable sized type
5435 to be bound now. */
5436 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5437 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5439 return t;
5442 /* Lay out the type T, and its element type, and so on. */
5444 static void
5445 layout_array_type (tree t)
5447 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5448 layout_array_type (TREE_TYPE (t));
5449 layout_type (t);
5452 /* Begin compiling the definition of an enumeration type.
5453 NAME is its name (or null if anonymous).
5454 Returns the type object, as yet incomplete.
5455 Also records info about it so that build_enumerator
5456 may be used to declare the individual values as they are read. */
5458 tree
5459 start_enum (tree name)
5461 tree enumtype = 0;
5463 /* If this is the real definition for a previous forward reference,
5464 fill in the contents in the same object that used to be the
5465 forward reference. */
5467 if (name != 0)
5468 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5470 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5472 enumtype = make_node (ENUMERAL_TYPE);
5473 pushtag (name, enumtype);
5476 if (C_TYPE_BEING_DEFINED (enumtype))
5477 error ("nested redefinition of %<enum %E%>", name);
5479 C_TYPE_BEING_DEFINED (enumtype) = 1;
5481 if (TYPE_VALUES (enumtype) != 0)
5483 /* This enum is a named one that has been declared already. */
5484 error ("redeclaration of %<enum %E%>", name);
5486 /* Completely replace its old definition.
5487 The old enumerators remain defined, however. */
5488 TYPE_VALUES (enumtype) = 0;
5491 enum_next_value = integer_zero_node;
5492 enum_overflow = 0;
5494 if (flag_short_enums)
5495 TYPE_PACKED (enumtype) = 1;
5497 return enumtype;
5500 /* After processing and defining all the values of an enumeration type,
5501 install their decls in the enumeration type and finish it off.
5502 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5503 and ATTRIBUTES are the specified attributes.
5504 Returns ENUMTYPE. */
5506 tree
5507 finish_enum (tree enumtype, tree values, tree attributes)
5509 tree pair, tem;
5510 tree minnode = 0, maxnode = 0;
5511 int precision, unsign;
5512 bool toplevel = (file_scope == current_scope);
5513 struct lang_type *lt;
5515 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5517 /* Calculate the maximum value of any enumerator in this type. */
5519 if (values == error_mark_node)
5520 minnode = maxnode = integer_zero_node;
5521 else
5523 minnode = maxnode = TREE_VALUE (values);
5524 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5526 tree value = TREE_VALUE (pair);
5527 if (tree_int_cst_lt (maxnode, value))
5528 maxnode = value;
5529 if (tree_int_cst_lt (value, minnode))
5530 minnode = value;
5534 /* Construct the final type of this enumeration. It is the same
5535 as one of the integral types - the narrowest one that fits, except
5536 that normally we only go as narrow as int - and signed iff any of
5537 the values are negative. */
5538 unsign = (tree_int_cst_sgn (minnode) >= 0);
5539 precision = MAX (min_precision (minnode, unsign),
5540 min_precision (maxnode, unsign));
5542 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5544 tem = c_common_type_for_size (precision, unsign);
5545 if (tem == NULL)
5547 warning (0, "enumeration values exceed range of largest integer");
5548 tem = long_long_integer_type_node;
5551 else
5552 tem = unsign ? unsigned_type_node : integer_type_node;
5554 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5555 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5556 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5557 TYPE_SIZE (enumtype) = 0;
5559 /* If the precision of the type was specific with an attribute and it
5560 was too small, give an error. Otherwise, use it. */
5561 if (TYPE_PRECISION (enumtype))
5563 if (precision > TYPE_PRECISION (enumtype))
5564 error ("specified mode too small for enumeral values");
5566 else
5567 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5569 layout_type (enumtype);
5571 if (values != error_mark_node)
5573 /* Change the type of the enumerators to be the enum type. We
5574 need to do this irrespective of the size of the enum, for
5575 proper type checking. Replace the DECL_INITIALs of the
5576 enumerators, and the value slots of the list, with copies
5577 that have the enum type; they cannot be modified in place
5578 because they may be shared (e.g. integer_zero_node) Finally,
5579 change the purpose slots to point to the names of the decls. */
5580 for (pair = values; pair; pair = TREE_CHAIN (pair))
5582 tree enu = TREE_PURPOSE (pair);
5583 tree ini = DECL_INITIAL (enu);
5585 TREE_TYPE (enu) = enumtype;
5587 /* The ISO C Standard mandates enumerators to have type int,
5588 even though the underlying type of an enum type is
5589 unspecified. Here we convert any enumerators that fit in
5590 an int to type int, to avoid promotions to unsigned types
5591 when comparing integers with enumerators that fit in the
5592 int range. When -pedantic is given, build_enumerator()
5593 would have already taken care of those that don't fit. */
5594 if (int_fits_type_p (ini, integer_type_node))
5595 tem = integer_type_node;
5596 else
5597 tem = enumtype;
5598 ini = convert (tem, ini);
5600 DECL_INITIAL (enu) = ini;
5601 TREE_PURPOSE (pair) = DECL_NAME (enu);
5602 TREE_VALUE (pair) = ini;
5605 TYPE_VALUES (enumtype) = values;
5608 /* Record the min/max values so that we can warn about bit-field
5609 enumerations that are too small for the values. */
5610 lt = GGC_CNEW (struct lang_type);
5611 lt->enum_min = minnode;
5612 lt->enum_max = maxnode;
5613 TYPE_LANG_SPECIFIC (enumtype) = lt;
5615 /* Fix up all variant types of this enum type. */
5616 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5618 if (tem == enumtype)
5619 continue;
5620 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5621 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5622 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5623 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5624 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5625 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5626 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5627 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5628 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5629 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5630 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5633 /* Finish debugging output for this type. */
5634 rest_of_type_compilation (enumtype, toplevel);
5636 return enumtype;
5639 /* Build and install a CONST_DECL for one value of the
5640 current enumeration type (one that was begun with start_enum).
5641 Return a tree-list containing the CONST_DECL and its value.
5642 Assignment of sequential values by default is handled here. */
5644 tree
5645 build_enumerator (tree name, tree value)
5647 tree decl, type;
5649 /* Validate and default VALUE. */
5651 if (value != 0)
5653 /* Don't issue more errors for error_mark_node (i.e. an
5654 undeclared identifier) - just ignore the value expression. */
5655 if (value == error_mark_node)
5656 value = 0;
5657 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5658 || TREE_CODE (value) != INTEGER_CST)
5660 error ("enumerator value for %qE is not an integer constant", name);
5661 value = 0;
5663 else
5665 value = default_conversion (value);
5666 constant_expression_warning (value);
5670 /* Default based on previous value. */
5671 /* It should no longer be possible to have NON_LVALUE_EXPR
5672 in the default. */
5673 if (value == 0)
5675 value = enum_next_value;
5676 if (enum_overflow)
5677 error ("overflow in enumeration values");
5680 if (pedantic && !int_fits_type_p (value, integer_type_node))
5682 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5683 /* XXX This causes -pedantic to change the meaning of the program.
5684 Remove? -zw 2004-03-15 */
5685 value = convert (integer_type_node, value);
5688 /* Set basis for default for next value. */
5689 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5690 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5692 /* Now create a declaration for the enum value name. */
5694 type = TREE_TYPE (value);
5695 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5696 TYPE_PRECISION (integer_type_node)),
5697 (TYPE_PRECISION (type)
5698 >= TYPE_PRECISION (integer_type_node)
5699 && TYPE_UNSIGNED (type)));
5701 decl = build_decl (CONST_DECL, name, type);
5702 DECL_INITIAL (decl) = convert (type, value);
5703 pushdecl (decl);
5705 return tree_cons (decl, value, NULL_TREE);
5709 /* Create the FUNCTION_DECL for a function definition.
5710 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5711 the declaration; they describe the function's name and the type it returns,
5712 but twisted together in a fashion that parallels the syntax of C.
5714 This function creates a binding context for the function body
5715 as well as setting up the FUNCTION_DECL in current_function_decl.
5717 Returns 1 on success. If the DECLARATOR is not suitable for a function
5718 (it defines a datum instead), we return 0, which tells
5719 yyparse to report a parse error. */
5722 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5723 tree attributes)
5725 tree decl1, old_decl;
5726 tree restype, resdecl;
5727 struct c_label_context_se *nstack_se;
5728 struct c_label_context_vm *nstack_vm;
5730 current_function_returns_value = 0; /* Assume, until we see it does. */
5731 current_function_returns_null = 0;
5732 current_function_returns_abnormally = 0;
5733 warn_about_return_type = 0;
5734 current_extern_inline = 0;
5735 c_switch_stack = NULL;
5737 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5738 nstack_se->labels_def = NULL;
5739 nstack_se->labels_used = NULL;
5740 nstack_se->next = label_context_stack_se;
5741 label_context_stack_se = nstack_se;
5743 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5744 nstack_vm->labels_def = NULL;
5745 nstack_vm->labels_used = NULL;
5746 nstack_vm->scope = 0;
5747 nstack_vm->next = label_context_stack_vm;
5748 label_context_stack_vm = nstack_vm;
5750 /* Indicate no valid break/continue context by setting these variables
5751 to some non-null, non-label value. We'll notice and emit the proper
5752 error message in c_finish_bc_stmt. */
5753 c_break_label = c_cont_label = size_zero_node;
5755 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5757 /* If the declarator is not suitable for a function definition,
5758 cause a syntax error. */
5759 if (decl1 == 0)
5760 return 0;
5762 decl_attributes (&decl1, attributes, 0);
5764 if (DECL_DECLARED_INLINE_P (decl1)
5765 && DECL_UNINLINABLE (decl1)
5766 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5767 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5769 announce_function (decl1);
5771 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5773 error ("return type is an incomplete type");
5774 /* Make it return void instead. */
5775 TREE_TYPE (decl1)
5776 = build_function_type (void_type_node,
5777 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5780 if (warn_about_return_type)
5781 pedwarn_c99 ("return type defaults to %<int%>");
5783 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5784 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5785 DECL_INITIAL (decl1) = error_mark_node;
5787 /* If this definition isn't a prototype and we had a prototype declaration
5788 before, copy the arg type info from that prototype. */
5789 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5790 current_function_prototype_locus = UNKNOWN_LOCATION;
5791 current_function_prototype_built_in = false;
5792 current_function_prototype_arg_types = NULL_TREE;
5793 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5795 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5796 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5797 TREE_TYPE (TREE_TYPE (old_decl))))
5799 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5800 TREE_TYPE (decl1));
5801 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5802 current_function_prototype_built_in
5803 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5804 current_function_prototype_arg_types
5805 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5807 if (TREE_PUBLIC (decl1))
5809 /* If there is an external prototype declaration of this
5810 function, record its location but do not copy information
5811 to this decl. This may be an invisible declaration
5812 (built-in or in a scope which has finished) or simply
5813 have more refined argument types than any declaration
5814 found above. */
5815 struct c_binding *b;
5816 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5817 if (B_IN_SCOPE (b, external_scope))
5818 break;
5819 if (b)
5821 tree ext_decl, ext_type;
5822 ext_decl = b->decl;
5823 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5824 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5825 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5826 TREE_TYPE (ext_type)))
5828 current_function_prototype_locus
5829 = DECL_SOURCE_LOCATION (ext_decl);
5830 current_function_prototype_built_in
5831 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5832 current_function_prototype_arg_types
5833 = TYPE_ARG_TYPES (ext_type);
5839 /* Optionally warn of old-fashioned def with no previous prototype. */
5840 if (warn_strict_prototypes
5841 && old_decl != error_mark_node
5842 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5843 && C_DECL_ISNT_PROTOTYPE (old_decl))
5844 warning (0, "function declaration isn%'t a prototype");
5845 /* Optionally warn of any global def with no previous prototype. */
5846 else if (warn_missing_prototypes
5847 && old_decl != error_mark_node
5848 && TREE_PUBLIC (decl1)
5849 && !MAIN_NAME_P (DECL_NAME (decl1))
5850 && C_DECL_ISNT_PROTOTYPE (old_decl))
5851 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5852 /* Optionally warn of any def with no previous prototype
5853 if the function has already been used. */
5854 else if (warn_missing_prototypes
5855 && old_decl != 0
5856 && old_decl != error_mark_node
5857 && TREE_USED (old_decl)
5858 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5859 warning (0, "%J%qD was used with no prototype before its definition",
5860 decl1, decl1);
5861 /* Optionally warn of any global def with no previous declaration. */
5862 else if (warn_missing_declarations
5863 && TREE_PUBLIC (decl1)
5864 && old_decl == 0
5865 && !MAIN_NAME_P (DECL_NAME (decl1)))
5866 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5867 /* Optionally warn of any def with no previous declaration
5868 if the function has already been used. */
5869 else if (warn_missing_declarations
5870 && old_decl != 0
5871 && old_decl != error_mark_node
5872 && TREE_USED (old_decl)
5873 && C_DECL_IMPLICIT (old_decl))
5874 warning (0, "%J%qD was used with no declaration before its definition",
5875 decl1, decl1);
5877 /* This is a definition, not a reference.
5878 So normally clear DECL_EXTERNAL.
5879 However, `extern inline' acts like a declaration
5880 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5881 DECL_EXTERNAL (decl1) = current_extern_inline;
5883 /* This function exists in static storage.
5884 (This does not mean `static' in the C sense!) */
5885 TREE_STATIC (decl1) = 1;
5887 /* A nested function is not global. */
5888 if (current_function_decl != 0)
5889 TREE_PUBLIC (decl1) = 0;
5891 /* This is the earliest point at which we might know the assembler
5892 name of the function. Thus, if it's set before this, die horribly. */
5893 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5895 /* If #pragma weak was used, mark the decl weak now. */
5896 if (current_scope == file_scope)
5897 maybe_apply_pragma_weak (decl1);
5899 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5900 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5902 tree args;
5903 int argct = 0;
5905 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5906 != integer_type_node)
5907 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5909 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5910 args = TREE_CHAIN (args))
5912 tree type = args ? TREE_VALUE (args) : 0;
5914 if (type == void_type_node)
5915 break;
5917 ++argct;
5918 switch (argct)
5920 case 1:
5921 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5922 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5923 decl1, decl1);
5924 break;
5926 case 2:
5927 if (TREE_CODE (type) != POINTER_TYPE
5928 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5929 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5930 != char_type_node))
5931 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5932 decl1, decl1);
5933 break;
5935 case 3:
5936 if (TREE_CODE (type) != POINTER_TYPE
5937 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5938 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5939 != char_type_node))
5940 pedwarn ("%Jthird argument of %qD should probably be "
5941 "%<char **%>", decl1, decl1);
5942 break;
5946 /* It is intentional that this message does not mention the third
5947 argument because it's only mentioned in an appendix of the
5948 standard. */
5949 if (argct > 0 && (argct < 2 || argct > 3))
5950 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5952 if (!TREE_PUBLIC (decl1))
5953 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5956 /* Record the decl so that the function name is defined.
5957 If we already have a decl for this name, and it is a FUNCTION_DECL,
5958 use the old decl. */
5960 current_function_decl = pushdecl (decl1);
5962 push_scope ();
5963 declare_parm_level ();
5965 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5966 /* Promote the value to int before returning it. */
5967 if (c_promoting_integer_type_p (restype))
5969 /* It retains unsignedness if not really getting wider. */
5970 if (TYPE_UNSIGNED (restype)
5971 && (TYPE_PRECISION (restype)
5972 == TYPE_PRECISION (integer_type_node)))
5973 restype = unsigned_type_node;
5974 else
5975 restype = integer_type_node;
5978 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5979 DECL_ARTIFICIAL (resdecl) = 1;
5980 DECL_IGNORED_P (resdecl) = 1;
5981 DECL_RESULT (current_function_decl) = resdecl;
5983 start_fname_decls ();
5985 return 1;
5988 /* Subroutine of store_parm_decls which handles new-style function
5989 definitions (prototype format). The parms already have decls, so we
5990 need only record them as in effect and complain if any redundant
5991 old-style parm decls were written. */
5992 static void
5993 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5995 tree decl;
5997 if (current_scope->bindings)
5999 error ("%Jold-style parameter declarations in prototyped "
6000 "function definition", fndecl);
6002 /* Get rid of the old-style declarations. */
6003 pop_scope ();
6004 push_scope ();
6006 /* Don't issue this warning for nested functions, and don't issue this
6007 warning if we got here because ARG_INFO_TYPES was error_mark_node
6008 (this happens when a function definition has just an ellipsis in
6009 its parameter list). */
6010 else if (warn_traditional && !in_system_header && !current_function_scope
6011 && arg_info->types != error_mark_node)
6012 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6013 fndecl);
6015 /* Now make all the parameter declarations visible in the function body.
6016 We can bypass most of the grunt work of pushdecl. */
6017 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6019 DECL_CONTEXT (decl) = current_function_decl;
6020 if (DECL_NAME (decl))
6021 bind (DECL_NAME (decl), decl, current_scope,
6022 /*invisible=*/false, /*nested=*/false);
6023 else
6024 error ("%Jparameter name omitted", decl);
6027 /* Record the parameter list in the function declaration. */
6028 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6030 /* Now make all the ancillary declarations visible, likewise. */
6031 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6033 DECL_CONTEXT (decl) = current_function_decl;
6034 if (DECL_NAME (decl))
6035 bind (DECL_NAME (decl), decl, current_scope,
6036 /*invisible=*/false, /*nested=*/false);
6039 /* And all the tag declarations. */
6040 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6041 if (TREE_PURPOSE (decl))
6042 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6043 /*invisible=*/false, /*nested=*/false);
6046 /* Subroutine of store_parm_decls which handles old-style function
6047 definitions (separate parameter list and declarations). */
6049 static void
6050 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6052 struct c_binding *b;
6053 tree parm, decl, last;
6054 tree parmids = arg_info->parms;
6056 /* We use DECL_WEAK as a flag to show which parameters have been
6057 seen already, since it is not used on PARM_DECL. */
6058 #ifdef ENABLE_CHECKING
6059 for (b = current_scope->bindings; b; b = b->prev)
6060 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6061 #endif
6063 if (!in_system_header)
6064 warning (OPT_Wold_style_definition, "%Jold-style function definition", fndecl);
6066 /* Match each formal parameter name with its declaration. Save each
6067 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6068 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6070 if (TREE_VALUE (parm) == 0)
6072 error ("%Jparameter name missing from parameter list", fndecl);
6073 TREE_PURPOSE (parm) = 0;
6074 continue;
6077 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6078 if (b && B_IN_CURRENT_SCOPE (b))
6080 decl = b->decl;
6081 /* If we got something other than a PARM_DECL it is an error. */
6082 if (TREE_CODE (decl) != PARM_DECL)
6083 error ("%J%qD declared as a non-parameter", decl, decl);
6084 /* If the declaration is already marked, we have a duplicate
6085 name. Complain and ignore the duplicate. */
6086 else if (DECL_WEAK (decl))
6088 error ("%Jmultiple parameters named %qD", decl, decl);
6089 TREE_PURPOSE (parm) = 0;
6090 continue;
6092 /* If the declaration says "void", complain and turn it into
6093 an int. */
6094 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6096 error ("%Jparameter %qD declared with void type", decl, decl);
6097 TREE_TYPE (decl) = integer_type_node;
6098 DECL_ARG_TYPE (decl) = integer_type_node;
6099 layout_decl (decl, 0);
6102 /* If no declaration found, default to int. */
6103 else
6105 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6106 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6107 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6108 pushdecl (decl);
6110 if (flag_isoc99)
6111 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6112 else if (extra_warnings)
6113 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6116 TREE_PURPOSE (parm) = decl;
6117 DECL_WEAK (decl) = 1;
6120 /* Now examine the parms chain for incomplete declarations
6121 and declarations with no corresponding names. */
6123 for (b = current_scope->bindings; b; b = b->prev)
6125 parm = b->decl;
6126 if (TREE_CODE (parm) != PARM_DECL)
6127 continue;
6129 if (TREE_TYPE (parm) != error_mark_node
6130 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6132 error ("%Jparameter %qD has incomplete type", parm, parm);
6133 TREE_TYPE (parm) = error_mark_node;
6136 if (!DECL_WEAK (parm))
6138 error ("%Jdeclaration for parameter %qD but no such parameter",
6139 parm, parm);
6141 /* Pretend the parameter was not missing.
6142 This gets us to a standard state and minimizes
6143 further error messages. */
6144 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6148 /* Chain the declarations together in the order of the list of
6149 names. Store that chain in the function decl, replacing the
6150 list of names. Update the current scope to match. */
6151 DECL_ARGUMENTS (fndecl) = 0;
6153 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6154 if (TREE_PURPOSE (parm))
6155 break;
6156 if (parm && TREE_PURPOSE (parm))
6158 last = TREE_PURPOSE (parm);
6159 DECL_ARGUMENTS (fndecl) = last;
6160 DECL_WEAK (last) = 0;
6162 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6163 if (TREE_PURPOSE (parm))
6165 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6166 last = TREE_PURPOSE (parm);
6167 DECL_WEAK (last) = 0;
6169 TREE_CHAIN (last) = 0;
6172 /* If there was a previous prototype,
6173 set the DECL_ARG_TYPE of each argument according to
6174 the type previously specified, and report any mismatches. */
6176 if (current_function_prototype_arg_types)
6178 tree type;
6179 for (parm = DECL_ARGUMENTS (fndecl),
6180 type = current_function_prototype_arg_types;
6181 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6182 != void_type_node));
6183 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6185 if (parm == 0 || type == 0
6186 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6188 if (current_function_prototype_built_in)
6189 warning (0, "number of arguments doesn%'t match "
6190 "built-in prototype");
6191 else
6193 error ("number of arguments doesn%'t match prototype");
6194 error ("%Hprototype declaration",
6195 &current_function_prototype_locus);
6197 break;
6199 /* Type for passing arg must be consistent with that
6200 declared for the arg. ISO C says we take the unqualified
6201 type for parameters declared with qualified type. */
6202 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6203 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6205 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6206 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6208 /* Adjust argument to match prototype. E.g. a previous
6209 `int foo(float);' prototype causes
6210 `int foo(x) float x; {...}' to be treated like
6211 `int foo(float x) {...}'. This is particularly
6212 useful for argument types like uid_t. */
6213 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6215 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6216 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6217 && TYPE_PRECISION (TREE_TYPE (parm))
6218 < TYPE_PRECISION (integer_type_node))
6219 DECL_ARG_TYPE (parm) = integer_type_node;
6221 if (pedantic)
6223 /* ??? Is it possible to get here with a
6224 built-in prototype or will it always have
6225 been diagnosed as conflicting with an
6226 old-style definition and discarded? */
6227 if (current_function_prototype_built_in)
6228 warning (0, "promoted argument %qD "
6229 "doesn%'t match built-in prototype", parm);
6230 else
6232 pedwarn ("promoted argument %qD "
6233 "doesn%'t match prototype", parm);
6234 pedwarn ("%Hprototype declaration",
6235 &current_function_prototype_locus);
6239 else
6241 if (current_function_prototype_built_in)
6242 warning (0, "argument %qD doesn%'t match "
6243 "built-in prototype", parm);
6244 else
6246 error ("argument %qD doesn%'t match prototype", parm);
6247 error ("%Hprototype declaration",
6248 &current_function_prototype_locus);
6253 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6256 /* Otherwise, create a prototype that would match. */
6258 else
6260 tree actual = 0, last = 0, type;
6262 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6264 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6265 if (last)
6266 TREE_CHAIN (last) = type;
6267 else
6268 actual = type;
6269 last = type;
6271 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6272 if (last)
6273 TREE_CHAIN (last) = type;
6274 else
6275 actual = type;
6277 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6278 of the type of this function, but we need to avoid having this
6279 affect the types of other similarly-typed functions, so we must
6280 first force the generation of an identical (but separate) type
6281 node for the relevant function type. The new node we create
6282 will be a variant of the main variant of the original function
6283 type. */
6285 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6287 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6291 /* Store parameter declarations passed in ARG_INFO into the current
6292 function declaration. */
6294 void
6295 store_parm_decls_from (struct c_arg_info *arg_info)
6297 current_function_arg_info = arg_info;
6298 store_parm_decls ();
6301 /* Store the parameter declarations into the current function declaration.
6302 This is called after parsing the parameter declarations, before
6303 digesting the body of the function.
6305 For an old-style definition, construct a prototype out of the old-style
6306 parameter declarations and inject it into the function's type. */
6308 void
6309 store_parm_decls (void)
6311 tree fndecl = current_function_decl;
6312 bool proto;
6314 /* The argument information block for FNDECL. */
6315 struct c_arg_info *arg_info = current_function_arg_info;
6316 current_function_arg_info = 0;
6318 /* True if this definition is written with a prototype. Note:
6319 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6320 list in a function definition as equivalent to (void) -- an
6321 empty argument list specifies the function has no parameters,
6322 but only (void) sets up a prototype for future calls. */
6323 proto = arg_info->types != 0;
6325 if (proto)
6326 store_parm_decls_newstyle (fndecl, arg_info);
6327 else
6328 store_parm_decls_oldstyle (fndecl, arg_info);
6330 /* The next call to push_scope will be a function body. */
6332 next_is_function_body = true;
6334 /* Write a record describing this function definition to the prototypes
6335 file (if requested). */
6337 gen_aux_info_record (fndecl, 1, 0, proto);
6339 /* Initialize the RTL code for the function. */
6340 allocate_struct_function (fndecl);
6342 /* Begin the statement tree for this function. */
6343 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6345 /* ??? Insert the contents of the pending sizes list into the function
6346 to be evaluated. The only reason left to have this is
6347 void foo(int n, int array[n++])
6348 because we throw away the array type in favor of a pointer type, and
6349 thus won't naturally see the SAVE_EXPR containing the increment. All
6350 other pending sizes would be handled by gimplify_parameters. */
6352 tree t;
6353 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6354 add_stmt (TREE_VALUE (t));
6357 /* Even though we're inside a function body, we still don't want to
6358 call expand_expr to calculate the size of a variable-sized array.
6359 We haven't necessarily assigned RTL to all variables yet, so it's
6360 not safe to try to expand expressions involving them. */
6361 cfun->x_dont_save_pending_sizes_p = 1;
6364 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6365 functions. */
6367 static void
6368 c_warn_unused_result_recursively (tree fndecl)
6370 struct cgraph_node *cgn;
6372 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6373 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6375 /* Finalize all nested functions now. */
6376 cgn = cgraph_node (fndecl);
6377 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6378 c_warn_unused_result_recursively (cgn->decl);
6381 /* Finish up a function declaration and compile that function
6382 all the way to assembler language output. The free the storage
6383 for the function definition.
6385 This is called after parsing the body of the function definition. */
6387 void
6388 finish_function (void)
6390 tree fndecl = current_function_decl;
6392 label_context_stack_se = label_context_stack_se->next;
6393 label_context_stack_vm = label_context_stack_vm->next;
6395 if (TREE_CODE (fndecl) == FUNCTION_DECL
6396 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6398 tree args = DECL_ARGUMENTS (fndecl);
6399 for (; args; args = TREE_CHAIN (args))
6401 tree type = TREE_TYPE (args);
6402 if (INTEGRAL_TYPE_P (type)
6403 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6404 DECL_ARG_TYPE (args) = integer_type_node;
6408 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6409 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6411 /* Must mark the RESULT_DECL as being in this function. */
6413 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6414 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6416 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6418 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6419 != integer_type_node)
6421 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6422 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6423 if (!warn_main)
6424 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6426 else
6428 if (flag_isoc99)
6430 tree stmt = c_finish_return (integer_zero_node);
6431 #ifdef USE_MAPPED_LOCATION
6432 /* Hack. We don't want the middle-end to warn that this return
6433 is unreachable, so we mark its location as special. Using
6434 UNKNOWN_LOCATION has the problem that it gets clobbered in
6435 annotate_one_with_locus. A cleaner solution might be to
6436 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6438 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6439 #else
6440 /* Hack. We don't want the middle-end to warn that this
6441 return is unreachable, so put the statement on the
6442 special line 0. */
6443 annotate_with_file_line (stmt, input_filename, 0);
6444 #endif
6449 /* Tie off the statement tree for this function. */
6450 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6452 finish_fname_decls ();
6454 /* Complain if there's just no return statement. */
6455 if (warn_return_type
6456 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6457 && !current_function_returns_value && !current_function_returns_null
6458 /* Don't complain if we are no-return. */
6459 && !current_function_returns_abnormally
6460 /* Don't warn for main(). */
6461 && !MAIN_NAME_P (DECL_NAME (fndecl))
6462 /* Or if they didn't actually specify a return type. */
6463 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6464 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6465 inline function, as we might never be compiled separately. */
6466 && DECL_INLINE (fndecl))
6467 warning (0, "no return statement in function returning non-void");
6469 /* With just -Wextra, complain only if function returns both with
6470 and without a value. */
6471 if (extra_warnings
6472 && current_function_returns_value
6473 && current_function_returns_null)
6474 warning (0, "this function may return with or without a value");
6476 /* Store the end of the function, so that we get good line number
6477 info for the epilogue. */
6478 cfun->function_end_locus = input_location;
6480 /* If we don't have ctors/dtors sections, and this is a static
6481 constructor or destructor, it must be recorded now. */
6482 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6483 && !targetm.have_ctors_dtors)
6484 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6485 if (DECL_STATIC_DESTRUCTOR (fndecl)
6486 && !targetm.have_ctors_dtors)
6487 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6489 /* Finalize the ELF visibility for the function. */
6490 c_determine_visibility (fndecl);
6492 /* Genericize before inlining. Delay genericizing nested functions
6493 until their parent function is genericized. Since finalizing
6494 requires GENERIC, delay that as well. */
6496 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6497 && !undef_nested_function)
6499 if (!decl_function_context (fndecl))
6501 c_genericize (fndecl);
6502 c_warn_unused_result_recursively (fndecl);
6504 /* ??? Objc emits functions after finalizing the compilation unit.
6505 This should be cleaned up later and this conditional removed. */
6506 if (cgraph_global_info_ready)
6508 c_expand_body (fndecl);
6509 return;
6512 cgraph_finalize_function (fndecl, false);
6514 else
6516 /* Register this function with cgraph just far enough to get it
6517 added to our parent's nested function list. Handy, since the
6518 C front end doesn't have such a list. */
6519 (void) cgraph_node (fndecl);
6523 if (!decl_function_context (fndecl))
6524 undef_nested_function = false;
6526 /* We're leaving the context of this function, so zap cfun.
6527 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6528 tree_rest_of_compilation. */
6529 cfun = NULL;
6530 current_function_decl = NULL;
6533 /* Generate the RTL for the body of FNDECL. */
6535 void
6536 c_expand_body (tree fndecl)
6539 if (!DECL_INITIAL (fndecl)
6540 || DECL_INITIAL (fndecl) == error_mark_node)
6541 return;
6543 tree_rest_of_compilation (fndecl);
6545 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6546 && targetm.have_ctors_dtors)
6547 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6548 DEFAULT_INIT_PRIORITY);
6549 if (DECL_STATIC_DESTRUCTOR (fndecl)
6550 && targetm.have_ctors_dtors)
6551 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6552 DEFAULT_INIT_PRIORITY);
6555 /* Check the declarations given in a for-loop for satisfying the C99
6556 constraints. */
6557 void
6558 check_for_loop_decls (void)
6560 struct c_binding *b;
6562 if (!flag_isoc99)
6564 /* If we get here, declarations have been used in a for loop without
6565 the C99 for loop scope. This doesn't make much sense, so don't
6566 allow it. */
6567 error ("%<for%> loop initial declaration used outside C99 mode");
6568 return;
6570 /* C99 subclause 6.8.5 paragraph 3:
6572 [#3] The declaration part of a for statement shall only
6573 declare identifiers for objects having storage class auto or
6574 register.
6576 It isn't clear whether, in this sentence, "identifiers" binds to
6577 "shall only declare" or to "objects" - that is, whether all identifiers
6578 declared must be identifiers for objects, or whether the restriction
6579 only applies to those that are. (A question on this in comp.std.c
6580 in November 2000 received no answer.) We implement the strictest
6581 interpretation, to avoid creating an extension which later causes
6582 problems. */
6584 for (b = current_scope->bindings; b; b = b->prev)
6586 tree id = b->id;
6587 tree decl = b->decl;
6589 if (!id)
6590 continue;
6592 switch (TREE_CODE (decl))
6594 case VAR_DECL:
6595 if (TREE_STATIC (decl))
6596 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6597 "initial declaration", decl, decl);
6598 else if (DECL_EXTERNAL (decl))
6599 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6600 "initial declaration", decl, decl);
6601 break;
6603 case RECORD_TYPE:
6604 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6605 id);
6606 break;
6607 case UNION_TYPE:
6608 error ("%<union %E%> declared in %<for%> loop initial declaration",
6609 id);
6610 break;
6611 case ENUMERAL_TYPE:
6612 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6613 id);
6614 break;
6615 default:
6616 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6617 "initial declaration", decl, decl);
6622 /* Save and reinitialize the variables
6623 used during compilation of a C function. */
6625 void
6626 c_push_function_context (struct function *f)
6628 struct language_function *p;
6629 p = GGC_NEW (struct language_function);
6630 f->language = p;
6632 p->base.x_stmt_tree = c_stmt_tree;
6633 p->x_break_label = c_break_label;
6634 p->x_cont_label = c_cont_label;
6635 p->x_switch_stack = c_switch_stack;
6636 p->arg_info = current_function_arg_info;
6637 p->returns_value = current_function_returns_value;
6638 p->returns_null = current_function_returns_null;
6639 p->returns_abnormally = current_function_returns_abnormally;
6640 p->warn_about_return_type = warn_about_return_type;
6641 p->extern_inline = current_extern_inline;
6644 /* Restore the variables used during compilation of a C function. */
6646 void
6647 c_pop_function_context (struct function *f)
6649 struct language_function *p = f->language;
6651 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6652 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6654 /* Stop pointing to the local nodes about to be freed. */
6655 /* But DECL_INITIAL must remain nonzero so we know this
6656 was an actual function definition. */
6657 DECL_INITIAL (current_function_decl) = error_mark_node;
6658 DECL_ARGUMENTS (current_function_decl) = 0;
6661 c_stmt_tree = p->base.x_stmt_tree;
6662 c_break_label = p->x_break_label;
6663 c_cont_label = p->x_cont_label;
6664 c_switch_stack = p->x_switch_stack;
6665 current_function_arg_info = p->arg_info;
6666 current_function_returns_value = p->returns_value;
6667 current_function_returns_null = p->returns_null;
6668 current_function_returns_abnormally = p->returns_abnormally;
6669 warn_about_return_type = p->warn_about_return_type;
6670 current_extern_inline = p->extern_inline;
6672 f->language = NULL;
6675 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6677 void
6678 c_dup_lang_specific_decl (tree decl)
6680 struct lang_decl *ld;
6682 if (!DECL_LANG_SPECIFIC (decl))
6683 return;
6685 ld = GGC_NEW (struct lang_decl);
6686 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6687 DECL_LANG_SPECIFIC (decl) = ld;
6690 /* The functions below are required for functionality of doing
6691 function at once processing in the C front end. Currently these
6692 functions are not called from anywhere in the C front end, but as
6693 these changes continue, that will change. */
6695 /* Returns nonzero if the current statement is a full expression,
6696 i.e. temporaries created during that statement should be destroyed
6697 at the end of the statement. */
6700 stmts_are_full_exprs_p (void)
6702 return 0;
6705 /* Returns the stmt_tree (if any) to which statements are currently
6706 being added. If there is no active statement-tree, NULL is
6707 returned. */
6709 stmt_tree
6710 current_stmt_tree (void)
6712 return &c_stmt_tree;
6715 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6716 C. */
6719 anon_aggr_type_p (tree ARG_UNUSED (node))
6721 return 0;
6724 /* Return the global value of T as a symbol. */
6726 tree
6727 identifier_global_value (tree t)
6729 struct c_binding *b;
6731 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6732 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6733 return b->decl;
6735 return 0;
6738 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6739 otherwise the name is found in ridpointers from RID_INDEX. */
6741 void
6742 record_builtin_type (enum rid rid_index, const char *name, tree type)
6744 tree id, decl;
6745 if (name == 0)
6746 id = ridpointers[(int) rid_index];
6747 else
6748 id = get_identifier (name);
6749 decl = build_decl (TYPE_DECL, id, type);
6750 pushdecl (decl);
6751 if (debug_hooks->type_decl)
6752 debug_hooks->type_decl (decl, false);
6755 /* Build the void_list_node (void_type_node having been created). */
6756 tree
6757 build_void_list_node (void)
6759 tree t = build_tree_list (NULL_TREE, void_type_node);
6760 return t;
6763 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6765 struct c_parm *
6766 build_c_parm (struct c_declspecs *specs, tree attrs,
6767 struct c_declarator *declarator)
6769 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6770 ret->specs = specs;
6771 ret->attrs = attrs;
6772 ret->declarator = declarator;
6773 return ret;
6776 /* Return a declarator with nested attributes. TARGET is the inner
6777 declarator to which these attributes apply. ATTRS are the
6778 attributes. */
6780 struct c_declarator *
6781 build_attrs_declarator (tree attrs, struct c_declarator *target)
6783 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6784 ret->kind = cdk_attrs;
6785 ret->declarator = target;
6786 ret->u.attrs = attrs;
6787 return ret;
6790 /* Return a declarator for a function with arguments specified by ARGS
6791 and return type specified by TARGET. */
6793 struct c_declarator *
6794 build_function_declarator (struct c_arg_info *args,
6795 struct c_declarator *target)
6797 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6798 ret->kind = cdk_function;
6799 ret->declarator = target;
6800 ret->u.arg_info = args;
6801 return ret;
6804 /* Return a declarator for the identifier IDENT (which may be
6805 NULL_TREE for an abstract declarator). */
6807 struct c_declarator *
6808 build_id_declarator (tree ident)
6810 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6811 ret->kind = cdk_id;
6812 ret->declarator = 0;
6813 ret->u.id = ident;
6814 /* Default value - may get reset to a more precise location. */
6815 ret->id_loc = input_location;
6816 return ret;
6819 /* Return something to represent absolute declarators containing a *.
6820 TARGET is the absolute declarator that the * contains.
6821 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6822 to apply to the pointer type. */
6824 struct c_declarator *
6825 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6826 struct c_declarator *target)
6828 tree attrs;
6829 int quals = 0;
6830 struct c_declarator *itarget = target;
6831 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6832 if (type_quals_attrs)
6834 attrs = type_quals_attrs->attrs;
6835 quals = quals_from_declspecs (type_quals_attrs);
6836 if (attrs != NULL_TREE)
6837 itarget = build_attrs_declarator (attrs, target);
6839 ret->kind = cdk_pointer;
6840 ret->declarator = itarget;
6841 ret->u.pointer_quals = quals;
6842 return ret;
6845 /* Return a pointer to a structure for an empty list of declaration
6846 specifiers. */
6848 struct c_declspecs *
6849 build_null_declspecs (void)
6851 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6852 ret->type = 0;
6853 ret->decl_attr = 0;
6854 ret->attrs = 0;
6855 ret->typespec_word = cts_none;
6856 ret->storage_class = csc_none;
6857 ret->declspecs_seen_p = false;
6858 ret->type_seen_p = false;
6859 ret->non_sc_seen_p = false;
6860 ret->typedef_p = false;
6861 ret->tag_defined_p = false;
6862 ret->explicit_signed_p = false;
6863 ret->deprecated_p = false;
6864 ret->default_int_p = false;
6865 ret->long_p = false;
6866 ret->long_long_p = false;
6867 ret->short_p = false;
6868 ret->signed_p = false;
6869 ret->unsigned_p = false;
6870 ret->complex_p = false;
6871 ret->inline_p = false;
6872 ret->thread_p = false;
6873 ret->const_p = false;
6874 ret->volatile_p = false;
6875 ret->restrict_p = false;
6876 return ret;
6879 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6880 returning SPECS. */
6882 struct c_declspecs *
6883 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6885 enum rid i;
6886 bool dupe = false;
6887 specs->non_sc_seen_p = true;
6888 specs->declspecs_seen_p = true;
6889 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6890 && C_IS_RESERVED_WORD (qual));
6891 i = C_RID_CODE (qual);
6892 switch (i)
6894 case RID_CONST:
6895 dupe = specs->const_p;
6896 specs->const_p = true;
6897 break;
6898 case RID_VOLATILE:
6899 dupe = specs->volatile_p;
6900 specs->volatile_p = true;
6901 break;
6902 case RID_RESTRICT:
6903 dupe = specs->restrict_p;
6904 specs->restrict_p = true;
6905 break;
6906 default:
6907 gcc_unreachable ();
6909 if (dupe && pedantic && !flag_isoc99)
6910 pedwarn ("duplicate %qE", qual);
6911 return specs;
6914 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6915 returning SPECS. */
6917 struct c_declspecs *
6918 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6920 tree type = spec.spec;
6921 specs->non_sc_seen_p = true;
6922 specs->declspecs_seen_p = true;
6923 specs->type_seen_p = true;
6924 if (TREE_DEPRECATED (type))
6925 specs->deprecated_p = true;
6927 /* Handle type specifier keywords. */
6928 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6930 enum rid i = C_RID_CODE (type);
6931 if (specs->type)
6933 error ("two or more data types in declaration specifiers");
6934 return specs;
6936 if ((int) i <= (int) RID_LAST_MODIFIER)
6938 /* "long", "short", "signed", "unsigned" or "_Complex". */
6939 bool dupe = false;
6940 switch (i)
6942 case RID_LONG:
6943 if (specs->long_long_p)
6945 error ("%<long long long%> is too long for GCC");
6946 break;
6948 if (specs->long_p)
6950 if (specs->typespec_word == cts_double)
6952 error ("both %<long long%> and %<double%> in "
6953 "declaration specifiers");
6954 break;
6956 if (pedantic && !flag_isoc99 && !in_system_header
6957 && warn_long_long)
6958 pedwarn ("ISO C90 does not support %<long long%>");
6959 specs->long_long_p = 1;
6960 break;
6962 if (specs->short_p)
6963 error ("both %<long%> and %<short%> in "
6964 "declaration specifiers");
6965 else if (specs->typespec_word == cts_void)
6966 error ("both %<long%> and %<void%> in "
6967 "declaration specifiers");
6968 else if (specs->typespec_word == cts_bool)
6969 error ("both %<long%> and %<_Bool%> in "
6970 "declaration specifiers");
6971 else if (specs->typespec_word == cts_char)
6972 error ("both %<long%> and %<char%> in "
6973 "declaration specifiers");
6974 else if (specs->typespec_word == cts_float)
6975 error ("both %<long%> and %<float%> in "
6976 "declaration specifiers");
6977 else
6978 specs->long_p = true;
6979 break;
6980 case RID_SHORT:
6981 dupe = specs->short_p;
6982 if (specs->long_p)
6983 error ("both %<long%> and %<short%> in "
6984 "declaration specifiers");
6985 else if (specs->typespec_word == cts_void)
6986 error ("both %<short%> and %<void%> in "
6987 "declaration specifiers");
6988 else if (specs->typespec_word == cts_bool)
6989 error ("both %<short%> and %<_Bool%> in "
6990 "declaration specifiers");
6991 else if (specs->typespec_word == cts_char)
6992 error ("both %<short%> and %<char%> in "
6993 "declaration specifiers");
6994 else if (specs->typespec_word == cts_float)
6995 error ("both %<short%> and %<float%> in "
6996 "declaration specifiers");
6997 else if (specs->typespec_word == cts_double)
6998 error ("both %<short%> and %<double%> in "
6999 "declaration specifiers");
7000 else
7001 specs->short_p = true;
7002 break;
7003 case RID_SIGNED:
7004 dupe = specs->signed_p;
7005 if (specs->unsigned_p)
7006 error ("both %<signed%> and %<unsigned%> in "
7007 "declaration specifiers");
7008 else if (specs->typespec_word == cts_void)
7009 error ("both %<signed%> and %<void%> in "
7010 "declaration specifiers");
7011 else if (specs->typespec_word == cts_bool)
7012 error ("both %<signed%> and %<_Bool%> in "
7013 "declaration specifiers");
7014 else if (specs->typespec_word == cts_float)
7015 error ("both %<signed%> and %<float%> in "
7016 "declaration specifiers");
7017 else if (specs->typespec_word == cts_double)
7018 error ("both %<signed%> and %<double%> in "
7019 "declaration specifiers");
7020 else
7021 specs->signed_p = true;
7022 break;
7023 case RID_UNSIGNED:
7024 dupe = specs->unsigned_p;
7025 if (specs->signed_p)
7026 error ("both %<signed%> and %<unsigned%> in "
7027 "declaration specifiers");
7028 else if (specs->typespec_word == cts_void)
7029 error ("both %<unsigned%> and %<void%> in "
7030 "declaration specifiers");
7031 else if (specs->typespec_word == cts_bool)
7032 error ("both %<unsigned%> and %<_Bool%> in "
7033 "declaration specifiers");
7034 else if (specs->typespec_word == cts_float)
7035 error ("both %<unsigned%> and %<float%> in "
7036 "declaration specifiers");
7037 else if (specs->typespec_word == cts_double)
7038 error ("both %<unsigned%> and %<double%> in "
7039 "declaration specifiers");
7040 else
7041 specs->unsigned_p = true;
7042 break;
7043 case RID_COMPLEX:
7044 dupe = specs->complex_p;
7045 if (pedantic && !flag_isoc99 && !in_system_header)
7046 pedwarn ("ISO C90 does not support complex types");
7047 if (specs->typespec_word == cts_void)
7048 error ("both %<complex%> and %<void%> in "
7049 "declaration specifiers");
7050 else if (specs->typespec_word == cts_bool)
7051 error ("both %<complex%> and %<_Bool%> in "
7052 "declaration specifiers");
7053 else
7054 specs->complex_p = true;
7055 break;
7056 default:
7057 gcc_unreachable ();
7060 if (dupe)
7061 error ("duplicate %qE", type);
7063 return specs;
7065 else
7067 /* "void", "_Bool", "char", "int", "float" or "double". */
7068 if (specs->typespec_word != cts_none)
7070 error ("two or more data types in declaration specifiers");
7071 return specs;
7073 switch (i)
7075 case RID_VOID:
7076 if (specs->long_p)
7077 error ("both %<long%> and %<void%> in "
7078 "declaration specifiers");
7079 else if (specs->short_p)
7080 error ("both %<short%> and %<void%> in "
7081 "declaration specifiers");
7082 else if (specs->signed_p)
7083 error ("both %<signed%> and %<void%> in "
7084 "declaration specifiers");
7085 else if (specs->unsigned_p)
7086 error ("both %<unsigned%> and %<void%> in "
7087 "declaration specifiers");
7088 else if (specs->complex_p)
7089 error ("both %<complex%> and %<void%> in "
7090 "declaration specifiers");
7091 else
7092 specs->typespec_word = cts_void;
7093 return specs;
7094 case RID_BOOL:
7095 if (specs->long_p)
7096 error ("both %<long%> and %<_Bool%> in "
7097 "declaration specifiers");
7098 else if (specs->short_p)
7099 error ("both %<short%> and %<_Bool%> in "
7100 "declaration specifiers");
7101 else if (specs->signed_p)
7102 error ("both %<signed%> and %<_Bool%> in "
7103 "declaration specifiers");
7104 else if (specs->unsigned_p)
7105 error ("both %<unsigned%> and %<_Bool%> in "
7106 "declaration specifiers");
7107 else if (specs->complex_p)
7108 error ("both %<complex%> and %<_Bool%> in "
7109 "declaration specifiers");
7110 else
7111 specs->typespec_word = cts_bool;
7112 return specs;
7113 case RID_CHAR:
7114 if (specs->long_p)
7115 error ("both %<long%> and %<char%> in "
7116 "declaration specifiers");
7117 else if (specs->short_p)
7118 error ("both %<short%> and %<char%> in "
7119 "declaration specifiers");
7120 else
7121 specs->typespec_word = cts_char;
7122 return specs;
7123 case RID_INT:
7124 specs->typespec_word = cts_int;
7125 return specs;
7126 case RID_FLOAT:
7127 if (specs->long_p)
7128 error ("both %<long%> and %<float%> in "
7129 "declaration specifiers");
7130 else if (specs->short_p)
7131 error ("both %<short%> and %<float%> in "
7132 "declaration specifiers");
7133 else if (specs->signed_p)
7134 error ("both %<signed%> and %<float%> in "
7135 "declaration specifiers");
7136 else if (specs->unsigned_p)
7137 error ("both %<unsigned%> and %<float%> in "
7138 "declaration specifiers");
7139 else
7140 specs->typespec_word = cts_float;
7141 return specs;
7142 case RID_DOUBLE:
7143 if (specs->long_long_p)
7144 error ("both %<long long%> and %<double%> in "
7145 "declaration specifiers");
7146 else if (specs->short_p)
7147 error ("both %<short%> and %<double%> in "
7148 "declaration specifiers");
7149 else if (specs->signed_p)
7150 error ("both %<signed%> and %<double%> in "
7151 "declaration specifiers");
7152 else if (specs->unsigned_p)
7153 error ("both %<unsigned%> and %<double%> in "
7154 "declaration specifiers");
7155 else
7156 specs->typespec_word = cts_double;
7157 return specs;
7158 default:
7159 /* ObjC reserved word "id", handled below. */
7160 break;
7165 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7166 form of ObjC type, cases such as "int" and "long" being handled
7167 above), a TYPE (struct, union, enum and typeof specifiers) or an
7168 ERROR_MARK. In none of these cases may there have previously
7169 been any type specifiers. */
7170 if (specs->type || specs->typespec_word != cts_none
7171 || specs->long_p || specs->short_p || specs->signed_p
7172 || specs->unsigned_p || specs->complex_p)
7173 error ("two or more data types in declaration specifiers");
7174 else if (TREE_CODE (type) == TYPE_DECL)
7176 if (TREE_TYPE (type) == error_mark_node)
7177 ; /* Allow the type to default to int to avoid cascading errors. */
7178 else
7180 specs->type = TREE_TYPE (type);
7181 specs->decl_attr = DECL_ATTRIBUTES (type);
7182 specs->typedef_p = true;
7183 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7186 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7188 tree t = lookup_name (type);
7189 if (!t || TREE_CODE (t) != TYPE_DECL)
7190 error ("%qE fails to be a typedef or built in type", type);
7191 else if (TREE_TYPE (t) == error_mark_node)
7193 else
7194 specs->type = TREE_TYPE (t);
7196 else if (TREE_CODE (type) != ERROR_MARK)
7198 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7199 specs->tag_defined_p = true;
7200 if (spec.kind == ctsk_typeof)
7201 specs->typedef_p = true;
7202 specs->type = type;
7205 return specs;
7208 /* Add the storage class specifier or function specifier SCSPEC to the
7209 declaration specifiers SPECS, returning SPECS. */
7211 struct c_declspecs *
7212 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7214 enum rid i;
7215 enum c_storage_class n = csc_none;
7216 bool dupe = false;
7217 specs->declspecs_seen_p = true;
7218 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7219 && C_IS_RESERVED_WORD (scspec));
7220 i = C_RID_CODE (scspec);
7221 if (extra_warnings && specs->non_sc_seen_p)
7222 warning (0, "%qE is not at beginning of declaration", scspec);
7223 switch (i)
7225 case RID_INLINE:
7226 /* C99 permits duplicate inline. Although of doubtful utility,
7227 it seems simplest to permit it in gnu89 mode as well, as
7228 there is also little utility in maintaining this as a
7229 difference between gnu89 and C99 inline. */
7230 dupe = false;
7231 specs->inline_p = true;
7232 break;
7233 case RID_THREAD:
7234 dupe = specs->thread_p;
7235 if (specs->storage_class == csc_auto)
7236 error ("%<__thread%> used with %<auto%>");
7237 else if (specs->storage_class == csc_register)
7238 error ("%<__thread%> used with %<register%>");
7239 else if (specs->storage_class == csc_typedef)
7240 error ("%<__thread%> used with %<typedef%>");
7241 else
7242 specs->thread_p = true;
7243 break;
7244 case RID_AUTO:
7245 n = csc_auto;
7246 break;
7247 case RID_EXTERN:
7248 n = csc_extern;
7249 /* Diagnose "__thread extern". */
7250 if (specs->thread_p)
7251 error ("%<__thread%> before %<extern%>");
7252 break;
7253 case RID_REGISTER:
7254 n = csc_register;
7255 break;
7256 case RID_STATIC:
7257 n = csc_static;
7258 /* Diagnose "__thread static". */
7259 if (specs->thread_p)
7260 error ("%<__thread%> before %<static%>");
7261 break;
7262 case RID_TYPEDEF:
7263 n = csc_typedef;
7264 break;
7265 default:
7266 gcc_unreachable ();
7268 if (n != csc_none && n == specs->storage_class)
7269 dupe = true;
7270 if (dupe)
7271 error ("duplicate %qE", scspec);
7272 if (n != csc_none)
7274 if (specs->storage_class != csc_none && n != specs->storage_class)
7276 error ("multiple storage classes in declaration specifiers");
7278 else
7280 specs->storage_class = n;
7281 if (n != csc_extern && n != csc_static && specs->thread_p)
7283 error ("%<__thread%> used with %qE", scspec);
7284 specs->thread_p = false;
7288 return specs;
7291 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7292 returning SPECS. */
7294 struct c_declspecs *
7295 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7297 specs->attrs = chainon (attrs, specs->attrs);
7298 specs->declspecs_seen_p = true;
7299 return specs;
7302 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7303 specifiers with any other type specifier to determine the resulting
7304 type. This is where ISO C checks on complex types are made, since
7305 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7306 double". */
7308 struct c_declspecs *
7309 finish_declspecs (struct c_declspecs *specs)
7311 /* If a type was specified as a whole, we have no modifiers and are
7312 done. */
7313 if (specs->type != NULL_TREE)
7315 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7316 && !specs->signed_p && !specs->unsigned_p
7317 && !specs->complex_p);
7318 return specs;
7321 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7322 has been specified, treat it as "int" unless "_Complex" is
7323 present and there are no other specifiers. If we just have
7324 "_Complex", it is equivalent to "_Complex double", but e.g.
7325 "_Complex short" is equivalent to "_Complex short int". */
7326 if (specs->typespec_word == cts_none)
7328 if (specs->long_p || specs->short_p
7329 || specs->signed_p || specs->unsigned_p)
7331 specs->typespec_word = cts_int;
7333 else if (specs->complex_p)
7335 specs->typespec_word = cts_double;
7336 if (pedantic)
7337 pedwarn ("ISO C does not support plain %<complex%> meaning "
7338 "%<double complex%>");
7340 else
7342 specs->typespec_word = cts_int;
7343 specs->default_int_p = true;
7344 /* We don't diagnose this here because grokdeclarator will
7345 give more specific diagnostics according to whether it is
7346 a function definition. */
7350 /* If "signed" was specified, record this to distinguish "int" and
7351 "signed int" in the case of a bit-field with
7352 -funsigned-bitfields. */
7353 specs->explicit_signed_p = specs->signed_p;
7355 /* Now compute the actual type. */
7356 switch (specs->typespec_word)
7358 case cts_void:
7359 gcc_assert (!specs->long_p && !specs->short_p
7360 && !specs->signed_p && !specs->unsigned_p
7361 && !specs->complex_p);
7362 specs->type = void_type_node;
7363 break;
7364 case cts_bool:
7365 gcc_assert (!specs->long_p && !specs->short_p
7366 && !specs->signed_p && !specs->unsigned_p
7367 && !specs->complex_p);
7368 specs->type = boolean_type_node;
7369 break;
7370 case cts_char:
7371 gcc_assert (!specs->long_p && !specs->short_p);
7372 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7373 if (specs->signed_p)
7374 specs->type = signed_char_type_node;
7375 else if (specs->unsigned_p)
7376 specs->type = unsigned_char_type_node;
7377 else
7378 specs->type = char_type_node;
7379 if (specs->complex_p)
7381 if (pedantic)
7382 pedwarn ("ISO C does not support complex integer types");
7383 specs->type = build_complex_type (specs->type);
7385 break;
7386 case cts_int:
7387 gcc_assert (!(specs->long_p && specs->short_p));
7388 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7389 if (specs->long_long_p)
7390 specs->type = (specs->unsigned_p
7391 ? long_long_unsigned_type_node
7392 : long_long_integer_type_node);
7393 else if (specs->long_p)
7394 specs->type = (specs->unsigned_p
7395 ? long_unsigned_type_node
7396 : long_integer_type_node);
7397 else if (specs->short_p)
7398 specs->type = (specs->unsigned_p
7399 ? short_unsigned_type_node
7400 : short_integer_type_node);
7401 else
7402 specs->type = (specs->unsigned_p
7403 ? unsigned_type_node
7404 : integer_type_node);
7405 if (specs->complex_p)
7407 if (pedantic)
7408 pedwarn ("ISO C does not support complex integer types");
7409 specs->type = build_complex_type (specs->type);
7411 break;
7412 case cts_float:
7413 gcc_assert (!specs->long_p && !specs->short_p
7414 && !specs->signed_p && !specs->unsigned_p);
7415 specs->type = (specs->complex_p
7416 ? complex_float_type_node
7417 : float_type_node);
7418 break;
7419 case cts_double:
7420 gcc_assert (!specs->long_long_p && !specs->short_p
7421 && !specs->signed_p && !specs->unsigned_p);
7422 if (specs->long_p)
7424 specs->type = (specs->complex_p
7425 ? complex_long_double_type_node
7426 : long_double_type_node);
7428 else
7430 specs->type = (specs->complex_p
7431 ? complex_double_type_node
7432 : double_type_node);
7434 break;
7435 default:
7436 gcc_unreachable ();
7439 return specs;
7442 /* Synthesize a function which calls all the global ctors or global
7443 dtors in this file. This is only used for targets which do not
7444 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7445 static void
7446 build_cdtor (int method_type, tree cdtors)
7448 tree body = 0;
7450 if (!cdtors)
7451 return;
7453 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7454 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7455 &body);
7457 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7460 /* Perform final processing on one file scope's declarations (or the
7461 external scope's declarations), GLOBALS. */
7462 static void
7463 c_write_global_declarations_1 (tree globals)
7465 size_t len = list_length (globals);
7466 tree *vec = XNEWVEC (tree, len);
7467 size_t i;
7468 tree decl;
7470 /* Process the decls in the order they were written. */
7471 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7473 vec[i] = decl;
7474 /* Check for used but undefined static functions using the C
7475 standard's definition of "used", and set TREE_NO_WARNING so
7476 that check_global_declarations doesn't repeat the check. */
7477 if (TREE_CODE (decl) == FUNCTION_DECL
7478 && DECL_INITIAL (decl) == 0
7479 && DECL_EXTERNAL (decl)
7480 && !TREE_PUBLIC (decl)
7481 && C_DECL_USED (decl))
7483 pedwarn ("%J%qF used but never defined", decl, decl);
7484 TREE_NO_WARNING (decl) = 1;
7488 wrapup_global_declarations (vec, len);
7489 check_global_declarations (vec, len);
7491 free (vec);
7494 void
7495 c_write_global_declarations (void)
7497 tree ext_block, t;
7499 /* We don't want to do this if generating a PCH. */
7500 if (pch_file)
7501 return;
7503 /* Don't waste time on further processing if -fsyntax-only or we've
7504 encountered errors. */
7505 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7506 return;
7508 /* Close the external scope. */
7509 ext_block = pop_scope ();
7510 external_scope = 0;
7511 gcc_assert (!current_scope);
7513 /* Process all file scopes in this compilation, and the external_scope,
7514 through wrapup_global_declarations and check_global_declarations. */
7515 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7516 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7517 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7519 /* Generate functions to call static constructors and destructors
7520 for targets that do not support .ctors/.dtors sections. These
7521 functions have magic names which are detected by collect2. */
7522 build_cdtor ('I', static_ctors); static_ctors = 0;
7523 build_cdtor ('D', static_dtors); static_dtors = 0;
7525 /* We're done parsing; proceed to optimize and emit assembly.
7526 FIXME: shouldn't be the front end's responsibility to call this. */
7527 cgraph_optimize ();
7530 #include "gt-c-decl.h"