2005-05-13 Josh Conner <jconner@apple.com>
[official-gcc.git] / gcc / c-decl.c
blobe189d3629dd363d29338dea15bbacaac66cda8ae
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 switch (failure)
3280 case 1:
3281 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3282 break;
3284 case 2:
3285 if (do_default)
3286 error ("%Jarray size missing in %qD", decl, decl);
3287 /* If a `static' var's size isn't known,
3288 make it extern as well as static, so it does not get
3289 allocated.
3290 If it is not `static', then do not mark extern;
3291 finish_incomplete_decl will give it a default size
3292 and it will get allocated. */
3293 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3294 DECL_EXTERNAL (decl) = 1;
3295 break;
3297 case 3:
3298 error ("%Jzero or negative size array %qD", decl, decl);
3299 break;
3301 case 0:
3302 /* For global variables, update the copy of the type that
3303 exists in the binding. */
3304 if (TREE_PUBLIC (decl))
3306 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3307 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3308 b_ext = b_ext->shadowed;
3309 if (b_ext)
3311 if (b_ext->type)
3312 b_ext->type = composite_type (b_ext->type, type);
3313 else
3314 b_ext->type = type;
3317 break;
3319 default:
3320 gcc_unreachable ();
3323 if (DECL_INITIAL (decl))
3324 TREE_TYPE (DECL_INITIAL (decl)) = type;
3326 layout_decl (decl, 0);
3329 if (TREE_CODE (decl) == VAR_DECL)
3331 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3332 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3333 layout_decl (decl, 0);
3335 if (DECL_SIZE (decl) == 0
3336 /* Don't give an error if we already gave one earlier. */
3337 && TREE_TYPE (decl) != error_mark_node
3338 && (TREE_STATIC (decl)
3339 /* A static variable with an incomplete type
3340 is an error if it is initialized.
3341 Also if it is not file scope.
3342 Otherwise, let it through, but if it is not `extern'
3343 then it may cause an error message later. */
3344 ? (DECL_INITIAL (decl) != 0
3345 || !DECL_FILE_SCOPE_P (decl))
3346 /* An automatic variable with an incomplete type
3347 is an error. */
3348 : !DECL_EXTERNAL (decl)))
3350 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3351 TREE_TYPE (decl) = error_mark_node;
3354 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3355 && DECL_SIZE (decl) != 0)
3357 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3358 constant_expression_warning (DECL_SIZE (decl));
3359 else
3360 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3363 if (TREE_USED (type))
3364 TREE_USED (decl) = 1;
3367 /* If this is a function and an assembler name is specified, reset DECL_RTL
3368 so we can give it its new name. Also, update built_in_decls if it
3369 was a normal built-in. */
3370 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3372 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3373 set_builtin_user_assembler_name (decl, asmspec);
3374 set_user_assembler_name (decl, asmspec);
3377 /* If #pragma weak was used, mark the decl weak now. */
3378 maybe_apply_pragma_weak (decl);
3380 /* If this is a variable definition, determine its ELF visibility. */
3381 if (TREE_CODE (decl) == VAR_DECL
3382 && TREE_STATIC (decl)
3383 && !DECL_EXTERNAL (decl))
3384 c_determine_visibility (decl);
3386 /* Output the assembler code and/or RTL code for variables and functions,
3387 unless the type is an undefined structure or union.
3388 If not, it will get done when the type is completed. */
3390 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3392 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3393 if (c_dialect_objc ())
3394 objc_check_decl (decl);
3396 if (asmspec)
3398 /* If this is not a static variable, issue a warning.
3399 It doesn't make any sense to give an ASMSPEC for an
3400 ordinary, non-register local variable. Historically,
3401 GCC has accepted -- but ignored -- the ASMSPEC in
3402 this case. */
3403 if (!DECL_FILE_SCOPE_P (decl)
3404 && TREE_CODE (decl) == VAR_DECL
3405 && !C_DECL_REGISTER (decl)
3406 && !TREE_STATIC (decl))
3407 warning (0, "%Jignoring asm-specifier for non-static local "
3408 "variable %qD", decl, decl);
3409 else if (C_DECL_REGISTER (decl))
3410 change_decl_assembler_name (decl, get_identifier (asmspec));
3411 else
3412 set_user_assembler_name (decl, asmspec);
3415 if (DECL_FILE_SCOPE_P (decl))
3417 if (DECL_INITIAL (decl) == NULL_TREE
3418 || DECL_INITIAL (decl) == error_mark_node)
3419 /* Don't output anything
3420 when a tentative file-scope definition is seen.
3421 But at end of compilation, do output code for them. */
3422 DECL_DEFER_OUTPUT (decl) = 1;
3423 rest_of_decl_compilation (decl, true, 0);
3425 else
3427 /* In conjunction with an ASMSPEC, the `register'
3428 keyword indicates that we should place the variable
3429 in a particular register. */
3430 if (asmspec && C_DECL_REGISTER (decl))
3432 DECL_HARD_REGISTER (decl) = 1;
3433 /* This cannot be done for a structure with volatile
3434 fields, on which DECL_REGISTER will have been
3435 reset. */
3436 if (!DECL_REGISTER (decl))
3437 error ("cannot put object with volatile field into register");
3440 if (TREE_CODE (decl) != FUNCTION_DECL)
3442 /* If we're building a variable sized type, and we might be
3443 reachable other than via the top of the current binding
3444 level, then create a new BIND_EXPR so that we deallocate
3445 the object at the right time. */
3446 /* Note that DECL_SIZE can be null due to errors. */
3447 if (DECL_SIZE (decl)
3448 && !TREE_CONSTANT (DECL_SIZE (decl))
3449 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3451 tree bind;
3452 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3453 TREE_SIDE_EFFECTS (bind) = 1;
3454 add_stmt (bind);
3455 BIND_EXPR_BODY (bind) = push_stmt_list ();
3457 add_stmt (build_stmt (DECL_EXPR, decl));
3462 if (!DECL_FILE_SCOPE_P (decl))
3464 /* Recompute the RTL of a local array now
3465 if it used to be an incomplete type. */
3466 if (was_incomplete
3467 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3469 /* If we used it already as memory, it must stay in memory. */
3470 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3471 /* If it's still incomplete now, no init will save it. */
3472 if (DECL_SIZE (decl) == 0)
3473 DECL_INITIAL (decl) = 0;
3478 /* If this was marked 'used', be sure it will be output. */
3479 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3480 mark_decl_referenced (decl);
3482 if (TREE_CODE (decl) == TYPE_DECL)
3484 if (!DECL_FILE_SCOPE_P (decl)
3485 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3486 add_stmt (build_stmt (DECL_EXPR, decl));
3488 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3491 /* At the end of a declaration, throw away any variable type sizes
3492 of types defined inside that declaration. There is no use
3493 computing them in the following function definition. */
3494 if (current_scope == file_scope)
3495 get_pending_sizes ();
3497 /* Install a cleanup (aka destructor) if one was given. */
3498 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3500 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3501 if (attr)
3503 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3504 tree cleanup_decl = lookup_name (cleanup_id);
3505 tree cleanup;
3507 /* Build "cleanup(&decl)" for the destructor. */
3508 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3509 cleanup = build_tree_list (NULL_TREE, cleanup);
3510 cleanup = build_function_call (cleanup_decl, cleanup);
3512 /* Don't warn about decl unused; the cleanup uses it. */
3513 TREE_USED (decl) = 1;
3514 TREE_USED (cleanup_decl) = 1;
3516 /* Initialize EH, if we've been told to do so. */
3517 if (flag_exceptions && !c_eh_initialized_p)
3519 c_eh_initialized_p = true;
3520 eh_personality_libfunc
3521 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3522 ? "__gcc_personality_sj0"
3523 : "__gcc_personality_v0");
3524 using_eh_for_cleanups ();
3527 push_cleanup (decl, cleanup, false);
3532 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3534 tree
3535 grokparm (const struct c_parm *parm)
3537 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3538 NULL);
3540 decl_attributes (&decl, parm->attrs, 0);
3542 return decl;
3545 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3546 and push that on the current scope. */
3548 void
3549 push_parm_decl (const struct c_parm *parm)
3551 tree decl;
3553 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3554 decl_attributes (&decl, parm->attrs, 0);
3556 decl = pushdecl (decl);
3558 finish_decl (decl, NULL_TREE, NULL_TREE);
3561 /* Mark all the parameter declarations to date as forward decls.
3562 Also diagnose use of this extension. */
3564 void
3565 mark_forward_parm_decls (void)
3567 struct c_binding *b;
3569 if (pedantic && !current_scope->warned_forward_parm_decls)
3571 pedwarn ("ISO C forbids forward parameter declarations");
3572 current_scope->warned_forward_parm_decls = true;
3575 for (b = current_scope->bindings; b; b = b->prev)
3576 if (TREE_CODE (b->decl) == PARM_DECL)
3577 TREE_ASM_WRITTEN (b->decl) = 1;
3580 static GTY(()) int compound_literal_number;
3582 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3583 literal, which may be an incomplete array type completed by the
3584 initializer; INIT is a CONSTRUCTOR that initializes the compound
3585 literal. */
3587 tree
3588 build_compound_literal (tree type, tree init)
3590 /* We do not use start_decl here because we have a type, not a declarator;
3591 and do not use finish_decl because the decl should be stored inside
3592 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3593 tree decl;
3594 tree complit;
3595 tree stmt;
3597 if (type == error_mark_node)
3598 return error_mark_node;
3600 decl = build_decl (VAR_DECL, NULL_TREE, type);
3601 DECL_EXTERNAL (decl) = 0;
3602 TREE_PUBLIC (decl) = 0;
3603 TREE_STATIC (decl) = (current_scope == file_scope);
3604 DECL_CONTEXT (decl) = current_function_decl;
3605 TREE_USED (decl) = 1;
3606 TREE_TYPE (decl) = type;
3607 TREE_READONLY (decl) = TYPE_READONLY (type);
3608 store_init_value (decl, init);
3610 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3612 int failure = complete_array_type (&TREE_TYPE (decl),
3613 DECL_INITIAL (decl), true);
3614 gcc_assert (!failure);
3616 type = TREE_TYPE (decl);
3617 TREE_TYPE (DECL_INITIAL (decl)) = type;
3620 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3621 return error_mark_node;
3623 stmt = build_stmt (DECL_EXPR, decl);
3624 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3625 TREE_SIDE_EFFECTS (complit) = 1;
3627 layout_decl (decl, 0);
3629 if (TREE_STATIC (decl))
3631 /* This decl needs a name for the assembler output. We also need
3632 a unique suffix to be added to the name. */
3633 char *name;
3635 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3636 compound_literal_number);
3637 compound_literal_number++;
3638 DECL_NAME (decl) = get_identifier (name);
3639 DECL_DEFER_OUTPUT (decl) = 1;
3640 DECL_COMDAT (decl) = 1;
3641 DECL_ARTIFICIAL (decl) = 1;
3642 DECL_IGNORED_P (decl) = 1;
3643 pushdecl (decl);
3644 rest_of_decl_compilation (decl, 1, 0);
3647 return complit;
3650 /* Determine whether TYPE is a structure with a flexible array member,
3651 or a union containing such a structure (possibly recursively). */
3653 static bool
3654 flexible_array_type_p (tree type)
3656 tree x;
3657 switch (TREE_CODE (type))
3659 case RECORD_TYPE:
3660 x = TYPE_FIELDS (type);
3661 if (x == NULL_TREE)
3662 return false;
3663 while (TREE_CHAIN (x) != NULL_TREE)
3664 x = TREE_CHAIN (x);
3665 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3666 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3667 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3668 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3669 return true;
3670 return false;
3671 case UNION_TYPE:
3672 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3674 if (flexible_array_type_p (TREE_TYPE (x)))
3675 return true;
3677 return false;
3678 default:
3679 return false;
3683 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3684 replacing with appropriate values if they are invalid. */
3685 static void
3686 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3688 tree type_mv;
3689 unsigned int max_width;
3690 unsigned HOST_WIDE_INT w;
3691 const char *name = orig_name ? orig_name: _("<anonymous>");
3693 /* Detect and ignore out of range field width and process valid
3694 field widths. */
3695 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3696 || TREE_CODE (*width) != INTEGER_CST)
3698 error ("bit-field %qs width not an integer constant", name);
3699 *width = integer_one_node;
3701 else
3703 constant_expression_warning (*width);
3704 if (tree_int_cst_sgn (*width) < 0)
3706 error ("negative width in bit-field %qs", name);
3707 *width = integer_one_node;
3709 else if (integer_zerop (*width) && orig_name)
3711 error ("zero width for bit-field %qs", name);
3712 *width = integer_one_node;
3716 /* Detect invalid bit-field type. */
3717 if (TREE_CODE (*type) != INTEGER_TYPE
3718 && TREE_CODE (*type) != BOOLEAN_TYPE
3719 && TREE_CODE (*type) != ENUMERAL_TYPE)
3721 error ("bit-field %qs has invalid type", name);
3722 *type = unsigned_type_node;
3725 type_mv = TYPE_MAIN_VARIANT (*type);
3726 if (pedantic
3727 && type_mv != integer_type_node
3728 && type_mv != unsigned_type_node
3729 && type_mv != boolean_type_node)
3730 pedwarn ("type of bit-field %qs is a GCC extension", name);
3732 if (type_mv == boolean_type_node)
3733 max_width = CHAR_TYPE_SIZE;
3734 else
3735 max_width = TYPE_PRECISION (*type);
3737 if (0 < compare_tree_int (*width, max_width))
3739 error ("width of %qs exceeds its type", name);
3740 w = max_width;
3741 *width = build_int_cst (NULL_TREE, w);
3743 else
3744 w = tree_low_cst (*width, 1);
3746 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3748 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3749 if (!lt
3750 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3751 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3752 warning (0, "%qs is narrower than values of its type", name);
3756 /* Given declspecs and a declarator,
3757 determine the name and type of the object declared
3758 and construct a ..._DECL node for it.
3759 (In one case we can return a ..._TYPE node instead.
3760 For invalid input we sometimes return 0.)
3762 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3764 DECL_CONTEXT says which syntactic context this declaration is in:
3765 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3766 FUNCDEF for a function definition. Like NORMAL but a few different
3767 error messages in each case. Return value may be zero meaning
3768 this definition is too screwy to try to parse.
3769 PARM for a parameter declaration (either within a function prototype
3770 or before a function body). Make a PARM_DECL, or return void_type_node.
3771 TYPENAME if for a typename (in a cast or sizeof).
3772 Don't make a DECL node; just return the ..._TYPE node.
3773 FIELD for a struct or union field; make a FIELD_DECL.
3774 INITIALIZED is true if the decl has an initializer.
3775 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3776 representing the width of the bit-field.
3778 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3779 It may also be so in the PARM case, for a prototype where the
3780 argument type is specified but not the name.
3782 This function is where the complicated C meanings of `static'
3783 and `extern' are interpreted. */
3785 static tree
3786 grokdeclarator (const struct c_declarator *declarator,
3787 struct c_declspecs *declspecs,
3788 enum decl_context decl_context, bool initialized, tree *width)
3790 tree type = declspecs->type;
3791 bool threadp = declspecs->thread_p;
3792 enum c_storage_class storage_class = declspecs->storage_class;
3793 int constp;
3794 int restrictp;
3795 int volatilep;
3796 int type_quals = TYPE_UNQUALIFIED;
3797 const char *name, *orig_name;
3798 tree typedef_type = 0;
3799 int funcdef_flag = 0;
3800 bool funcdef_syntax = false;
3801 int size_varies = 0;
3802 tree decl_attr = declspecs->decl_attr;
3803 int array_ptr_quals = TYPE_UNQUALIFIED;
3804 tree array_ptr_attrs = NULL_TREE;
3805 int array_parm_static = 0;
3806 tree returned_attrs = NULL_TREE;
3807 bool bitfield = width != NULL;
3808 tree element_type;
3809 struct c_arg_info *arg_info = 0;
3811 if (decl_context == FUNCDEF)
3812 funcdef_flag = 1, decl_context = NORMAL;
3814 /* Look inside a declarator for the name being declared
3815 and get it as a string, for an error message. */
3817 const struct c_declarator *decl = declarator;
3818 name = 0;
3820 while (decl)
3821 switch (decl->kind)
3823 case cdk_function:
3824 case cdk_array:
3825 case cdk_pointer:
3826 funcdef_syntax = (decl->kind == cdk_function);
3827 decl = decl->declarator;
3828 break;
3830 case cdk_attrs:
3831 decl = decl->declarator;
3832 break;
3834 case cdk_id:
3835 if (decl->u.id)
3836 name = IDENTIFIER_POINTER (decl->u.id);
3837 decl = 0;
3838 break;
3840 default:
3841 gcc_unreachable ();
3843 orig_name = name;
3844 if (name == 0)
3845 name = "type name";
3848 /* A function definition's declarator must have the form of
3849 a function declarator. */
3851 if (funcdef_flag && !funcdef_syntax)
3852 return 0;
3854 /* If this looks like a function definition, make it one,
3855 even if it occurs where parms are expected.
3856 Then store_parm_decls will reject it and not use it as a parm. */
3857 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3858 decl_context = PARM;
3860 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3861 warn_deprecated_use (declspecs->type);
3863 typedef_type = type;
3864 size_varies = C_TYPE_VARIABLE_SIZE (type);
3866 /* Diagnose defaulting to "int". */
3868 if (declspecs->default_int_p && !in_system_header)
3870 /* Issue a warning if this is an ISO C 99 program or if
3871 -Wreturn-type and this is a function, or if -Wimplicit;
3872 prefer the former warning since it is more explicit. */
3873 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3874 && funcdef_flag)
3875 warn_about_return_type = 1;
3876 else if (warn_implicit_int || flag_isoc99)
3877 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3880 /* Adjust the type if a bit-field is being declared,
3881 -funsigned-bitfields applied and the type is not explicitly
3882 "signed". */
3883 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3884 && TREE_CODE (type) == INTEGER_TYPE)
3885 type = c_common_unsigned_type (type);
3887 /* Figure out the type qualifiers for the declaration. There are
3888 two ways a declaration can become qualified. One is something
3889 like `const int i' where the `const' is explicit. Another is
3890 something like `typedef const int CI; CI i' where the type of the
3891 declaration contains the `const'. A third possibility is that
3892 there is a type qualifier on the element type of a typedefed
3893 array type, in which case we should extract that qualifier so
3894 that c_apply_type_quals_to_decls receives the full list of
3895 qualifiers to work with (C90 is not entirely clear about whether
3896 duplicate qualifiers should be diagnosed in this case, but it
3897 seems most appropriate to do so). */
3898 element_type = strip_array_types (type);
3899 constp = declspecs->const_p + TYPE_READONLY (element_type);
3900 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3901 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3902 if (pedantic && !flag_isoc99)
3904 if (constp > 1)
3905 pedwarn ("duplicate %<const%>");
3906 if (restrictp > 1)
3907 pedwarn ("duplicate %<restrict%>");
3908 if (volatilep > 1)
3909 pedwarn ("duplicate %<volatile%>");
3911 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3912 type = TYPE_MAIN_VARIANT (type);
3913 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3914 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3915 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3917 /* Warn about storage classes that are invalid for certain
3918 kinds of declarations (parameters, typenames, etc.). */
3920 if (funcdef_flag
3921 && (threadp
3922 || storage_class == csc_auto
3923 || storage_class == csc_register
3924 || storage_class == csc_typedef))
3926 if (storage_class == csc_auto
3927 && (pedantic || current_scope == file_scope))
3928 pedwarn ("function definition declared %<auto%>");
3929 if (storage_class == csc_register)
3930 error ("function definition declared %<register%>");
3931 if (storage_class == csc_typedef)
3932 error ("function definition declared %<typedef%>");
3933 if (threadp)
3934 error ("function definition declared %<__thread%>");
3935 threadp = false;
3936 if (storage_class == csc_auto
3937 || storage_class == csc_register
3938 || storage_class == csc_typedef)
3939 storage_class = csc_none;
3941 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3943 if (decl_context == PARM && storage_class == csc_register)
3945 else
3947 switch (decl_context)
3949 case FIELD:
3950 error ("storage class specified for structure field %qs",
3951 name);
3952 break;
3953 case PARM:
3954 error ("storage class specified for parameter %qs", name);
3955 break;
3956 default:
3957 error ("storage class specified for typename");
3958 break;
3960 storage_class = csc_none;
3961 threadp = false;
3964 else if (storage_class == csc_extern
3965 && initialized
3966 && !funcdef_flag)
3968 /* 'extern' with initialization is invalid if not at file scope. */
3969 if (current_scope == file_scope)
3970 warning (0, "%qs initialized and declared %<extern%>", name);
3971 else
3972 error ("%qs has both %<extern%> and initializer", name);
3974 else if (current_scope == file_scope)
3976 if (storage_class == csc_auto)
3977 error ("file-scope declaration of %qs specifies %<auto%>", name);
3978 if (pedantic && storage_class == csc_register)
3979 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3981 else
3983 if (storage_class == csc_extern && funcdef_flag)
3984 error ("nested function %qs declared %<extern%>", name);
3985 else if (threadp && storage_class == csc_none)
3987 error ("function-scope %qs implicitly auto and declared "
3988 "%<__thread%>",
3989 name);
3990 threadp = false;
3994 /* Now figure out the structure of the declarator proper.
3995 Descend through it, creating more complex types, until we reach
3996 the declared identifier (or NULL_TREE, in an absolute declarator).
3997 At each stage we maintain an unqualified version of the type
3998 together with any qualifiers that should be applied to it with
3999 c_build_qualified_type; this way, array types including
4000 multidimensional array types are first built up in unqualified
4001 form and then the qualified form is created with
4002 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4004 while (declarator && declarator->kind != cdk_id)
4006 if (type == error_mark_node)
4008 declarator = declarator->declarator;
4009 continue;
4012 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4013 a cdk_pointer (for *...),
4014 a cdk_function (for ...(...)),
4015 a cdk_attrs (for nested attributes),
4016 or a cdk_id (for the name being declared
4017 or the place in an absolute declarator
4018 where the name was omitted).
4019 For the last case, we have just exited the loop.
4021 At this point, TYPE is the type of elements of an array,
4022 or for a function to return, or for a pointer to point to.
4023 After this sequence of ifs, TYPE is the type of the
4024 array or function or pointer, and DECLARATOR has had its
4025 outermost layer removed. */
4027 if (array_ptr_quals != TYPE_UNQUALIFIED
4028 || array_ptr_attrs != NULL_TREE
4029 || array_parm_static)
4031 /* Only the innermost declarator (making a parameter be of
4032 array type which is converted to pointer type)
4033 may have static or type qualifiers. */
4034 error ("static or type qualifiers in non-parameter array declarator");
4035 array_ptr_quals = TYPE_UNQUALIFIED;
4036 array_ptr_attrs = NULL_TREE;
4037 array_parm_static = 0;
4040 switch (declarator->kind)
4042 case cdk_attrs:
4044 /* A declarator with embedded attributes. */
4045 tree attrs = declarator->u.attrs;
4046 const struct c_declarator *inner_decl;
4047 int attr_flags = 0;
4048 declarator = declarator->declarator;
4049 inner_decl = declarator;
4050 while (inner_decl->kind == cdk_attrs)
4051 inner_decl = inner_decl->declarator;
4052 if (inner_decl->kind == cdk_id)
4053 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4054 else if (inner_decl->kind == cdk_function)
4055 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4056 else if (inner_decl->kind == cdk_array)
4057 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4058 returned_attrs = decl_attributes (&type,
4059 chainon (returned_attrs, attrs),
4060 attr_flags);
4061 break;
4063 case cdk_array:
4065 tree itype = NULL_TREE;
4066 tree size = declarator->u.array.dimen;
4067 /* The index is a signed object `sizetype' bits wide. */
4068 tree index_type = c_common_signed_type (sizetype);
4070 array_ptr_quals = declarator->u.array.quals;
4071 array_ptr_attrs = declarator->u.array.attrs;
4072 array_parm_static = declarator->u.array.static_p;
4074 declarator = declarator->declarator;
4076 /* Check for some types that there cannot be arrays of. */
4078 if (VOID_TYPE_P (type))
4080 error ("declaration of %qs as array of voids", name);
4081 type = error_mark_node;
4084 if (TREE_CODE (type) == FUNCTION_TYPE)
4086 error ("declaration of %qs as array of functions", name);
4087 type = error_mark_node;
4090 if (pedantic && !in_system_header && flexible_array_type_p (type))
4091 pedwarn ("invalid use of structure with flexible array member");
4093 if (size == error_mark_node)
4094 type = error_mark_node;
4096 if (type == error_mark_node)
4097 continue;
4099 /* If size was specified, set ITYPE to a range-type for
4100 that size. Otherwise, ITYPE remains null. finish_decl
4101 may figure it out from an initial value. */
4103 if (size)
4105 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4106 lvalue. */
4107 STRIP_TYPE_NOPS (size);
4109 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4111 error ("size of array %qs has non-integer type", name);
4112 size = integer_one_node;
4115 if (pedantic && integer_zerop (size))
4116 pedwarn ("ISO C forbids zero-size array %qs", name);
4118 if (TREE_CODE (size) == INTEGER_CST)
4120 constant_expression_warning (size);
4121 if (tree_int_cst_sgn (size) < 0)
4123 error ("size of array %qs is negative", name);
4124 size = integer_one_node;
4127 else
4129 /* Make sure the array size remains visibly
4130 nonconstant even if it is (eg) a const variable
4131 with known value. */
4132 size_varies = 1;
4134 if (!flag_isoc99 && pedantic)
4136 if (TREE_CONSTANT (size))
4137 pedwarn ("ISO C90 forbids array %qs whose size "
4138 "can%'t be evaluated",
4139 name);
4140 else
4141 pedwarn ("ISO C90 forbids variable-size array %qs",
4142 name);
4146 if (integer_zerop (size))
4148 /* A zero-length array cannot be represented with
4149 an unsigned index type, which is what we'll
4150 get with build_index_type. Create an
4151 open-ended range instead. */
4152 itype = build_range_type (sizetype, size, NULL_TREE);
4154 else
4156 /* Arrange for the SAVE_EXPR on the inside of the
4157 MINUS_EXPR, which allows the -1 to get folded
4158 with the +1 that happens when building TYPE_SIZE. */
4159 if (size_varies)
4160 size = variable_size (size);
4162 /* Compute the maximum valid index, that is, size
4163 - 1. Do the calculation in index_type, so that
4164 if it is a variable the computations will be
4165 done in the proper mode. */
4166 itype = fold (build2 (MINUS_EXPR, index_type,
4167 convert (index_type, size),
4168 convert (index_type,
4169 size_one_node)));
4171 /* If that overflowed, the array is too big. ???
4172 While a size of INT_MAX+1 technically shouldn't
4173 cause an overflow (because we subtract 1), the
4174 overflow is recorded during the conversion to
4175 index_type, before the subtraction. Handling
4176 this case seems like an unnecessary
4177 complication. */
4178 if (TREE_OVERFLOW (itype))
4180 error ("size of array %qs is too large", name);
4181 type = error_mark_node;
4182 continue;
4185 itype = build_index_type (itype);
4188 else if (decl_context == FIELD)
4190 if (pedantic && !flag_isoc99 && !in_system_header)
4191 pedwarn ("ISO C90 does not support flexible array members");
4193 /* ISO C99 Flexible array members are effectively
4194 identical to GCC's zero-length array extension. */
4195 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4198 /* Complain about arrays of incomplete types. */
4199 if (!COMPLETE_TYPE_P (type))
4201 error ("array type has incomplete element type");
4202 type = error_mark_node;
4204 else
4205 type = build_array_type (type, itype);
4207 if (size_varies)
4208 C_TYPE_VARIABLE_SIZE (type) = 1;
4210 /* The GCC extension for zero-length arrays differs from
4211 ISO flexible array members in that sizeof yields
4212 zero. */
4213 if (size && integer_zerop (size))
4215 TYPE_SIZE (type) = bitsize_zero_node;
4216 TYPE_SIZE_UNIT (type) = size_zero_node;
4219 if (decl_context != PARM
4220 && (array_ptr_quals != TYPE_UNQUALIFIED
4221 || array_ptr_attrs != NULL_TREE
4222 || array_parm_static))
4224 error ("static or type qualifiers in non-parameter array declarator");
4225 array_ptr_quals = TYPE_UNQUALIFIED;
4226 array_ptr_attrs = NULL_TREE;
4227 array_parm_static = 0;
4229 break;
4231 case cdk_function:
4233 /* Say it's a definition only for the declarator closest
4234 to the identifier, apart possibly from some
4235 attributes. */
4236 bool really_funcdef = false;
4237 tree arg_types;
4238 if (funcdef_flag)
4240 const struct c_declarator *t = declarator->declarator;
4241 while (t->kind == cdk_attrs)
4242 t = t->declarator;
4243 really_funcdef = (t->kind == cdk_id);
4246 /* Declaring a function type. Make sure we have a valid
4247 type for the function to return. */
4248 if (type == error_mark_node)
4249 continue;
4251 size_varies = 0;
4253 /* Warn about some types functions can't return. */
4254 if (TREE_CODE (type) == FUNCTION_TYPE)
4256 error ("%qs declared as function returning a function", name);
4257 type = integer_type_node;
4259 if (TREE_CODE (type) == ARRAY_TYPE)
4261 error ("%qs declared as function returning an array", name);
4262 type = integer_type_node;
4265 /* Construct the function type and go to the next
4266 inner layer of declarator. */
4267 arg_info = declarator->u.arg_info;
4268 arg_types = grokparms (arg_info, really_funcdef);
4270 /* Type qualifiers before the return type of the function
4271 qualify the return type, not the function type. */
4272 if (type_quals)
4274 /* Type qualifiers on a function return type are
4275 normally permitted by the standard but have no
4276 effect, so give a warning at -Wreturn-type.
4277 Qualifiers on a void return type are banned on
4278 function definitions in ISO C; GCC used to used
4279 them for noreturn functions. */
4280 if (VOID_TYPE_P (type) && really_funcdef)
4281 pedwarn ("function definition has qualified void return type");
4282 else if (warn_return_type)
4283 warning (0, "type qualifiers ignored on function return type");
4285 type = c_build_qualified_type (type, type_quals);
4287 type_quals = TYPE_UNQUALIFIED;
4289 type = build_function_type (type, arg_types);
4290 declarator = declarator->declarator;
4292 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4293 the formal parameter list of this FUNCTION_TYPE to point to
4294 the FUNCTION_TYPE node itself. */
4296 tree link;
4298 for (link = arg_info->tags;
4299 link;
4300 link = TREE_CHAIN (link))
4301 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4303 break;
4305 case cdk_pointer:
4307 /* Merge any constancy or volatility into the target type
4308 for the pointer. */
4310 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4311 && type_quals)
4312 pedwarn ("ISO C forbids qualified function types");
4313 if (type_quals)
4314 type = c_build_qualified_type (type, type_quals);
4315 size_varies = 0;
4317 type = build_pointer_type (type);
4319 /* Process type qualifiers (such as const or volatile)
4320 that were given inside the `*'. */
4321 type_quals = declarator->u.pointer_quals;
4323 declarator = declarator->declarator;
4324 break;
4326 default:
4327 gcc_unreachable ();
4331 /* Now TYPE has the actual type, apart from any qualifiers in
4332 TYPE_QUALS. */
4334 /* Check the type and width of a bit-field. */
4335 if (bitfield)
4336 check_bitfield_type_and_width (&type, width, orig_name);
4338 /* Did array size calculations overflow? */
4340 if (TREE_CODE (type) == ARRAY_TYPE
4341 && COMPLETE_TYPE_P (type)
4342 && TREE_OVERFLOW (TYPE_SIZE (type)))
4344 error ("size of array %qs is too large", name);
4345 /* If we proceed with the array type as it is, we'll eventually
4346 crash in tree_low_cst(). */
4347 type = error_mark_node;
4350 /* If this is declaring a typedef name, return a TYPE_DECL. */
4352 if (storage_class == csc_typedef)
4354 tree decl;
4355 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4356 && type_quals)
4357 pedwarn ("ISO C forbids qualified function types");
4358 if (type_quals)
4359 type = c_build_qualified_type (type, type_quals);
4360 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4361 if (declspecs->explicit_signed_p)
4362 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4363 decl_attributes (&decl, returned_attrs, 0);
4364 if (declspecs->inline_p)
4365 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4366 return decl;
4369 /* Detect the case of an array type of unspecified size
4370 which came, as such, direct from a typedef name.
4371 We must copy the type, so that each identifier gets
4372 a distinct type, so that each identifier's size can be
4373 controlled separately by its own initializer. */
4375 if (type != 0 && typedef_type != 0
4376 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4377 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4379 type = build_array_type (TREE_TYPE (type), 0);
4380 if (size_varies)
4381 C_TYPE_VARIABLE_SIZE (type) = 1;
4384 /* If this is a type name (such as, in a cast or sizeof),
4385 compute the type and return it now. */
4387 if (decl_context == TYPENAME)
4389 /* Note that the grammar rejects storage classes in typenames
4390 and fields. */
4391 gcc_assert (storage_class == csc_none && !threadp
4392 && !declspecs->inline_p);
4393 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4394 && type_quals)
4395 pedwarn ("ISO C forbids const or volatile function types");
4396 if (type_quals)
4397 type = c_build_qualified_type (type, type_quals);
4398 decl_attributes (&type, returned_attrs, 0);
4399 return type;
4402 /* Aside from typedefs and type names (handle above),
4403 `void' at top level (not within pointer)
4404 is allowed only in public variables.
4405 We don't complain about parms either, but that is because
4406 a better error message can be made later. */
4408 if (VOID_TYPE_P (type) && decl_context != PARM
4409 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4410 && (storage_class == csc_extern
4411 || (current_scope == file_scope
4412 && !(storage_class == csc_static
4413 || storage_class == csc_register)))))
4415 error ("variable or field %qs declared void", name);
4416 type = integer_type_node;
4419 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4420 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4423 tree decl;
4425 if (decl_context == PARM)
4427 tree type_as_written;
4428 tree promoted_type;
4430 /* A parameter declared as an array of T is really a pointer to T.
4431 One declared as a function is really a pointer to a function. */
4433 if (TREE_CODE (type) == ARRAY_TYPE)
4435 /* Transfer const-ness of array into that of type pointed to. */
4436 type = TREE_TYPE (type);
4437 if (type_quals)
4438 type = c_build_qualified_type (type, type_quals);
4439 type = build_pointer_type (type);
4440 type_quals = array_ptr_quals;
4442 /* We don't yet implement attributes in this context. */
4443 if (array_ptr_attrs != NULL_TREE)
4444 warning (0, "attributes in parameter array declarator ignored");
4446 size_varies = 0;
4448 else if (TREE_CODE (type) == FUNCTION_TYPE)
4450 if (pedantic && type_quals)
4451 pedwarn ("ISO C forbids qualified function types");
4452 if (type_quals)
4453 type = c_build_qualified_type (type, type_quals);
4454 type = build_pointer_type (type);
4455 type_quals = TYPE_UNQUALIFIED;
4457 else if (type_quals)
4458 type = c_build_qualified_type (type, type_quals);
4460 type_as_written = type;
4462 decl = build_decl (PARM_DECL, declarator->u.id, type);
4463 if (size_varies)
4464 C_DECL_VARIABLE_SIZE (decl) = 1;
4466 /* Compute the type actually passed in the parmlist,
4467 for the case where there is no prototype.
4468 (For example, shorts and chars are passed as ints.)
4469 When there is a prototype, this is overridden later. */
4471 if (type == error_mark_node)
4472 promoted_type = type;
4473 else
4474 promoted_type = c_type_promotes_to (type);
4476 DECL_ARG_TYPE (decl) = promoted_type;
4477 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4478 if (declspecs->inline_p)
4479 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4481 else if (decl_context == FIELD)
4483 /* Note that the grammar rejects storage classes in typenames
4484 and fields. */
4485 gcc_assert (storage_class == csc_none && !threadp
4486 && !declspecs->inline_p);
4488 /* Structure field. It may not be a function. */
4490 if (TREE_CODE (type) == FUNCTION_TYPE)
4492 error ("field %qs declared as a function", name);
4493 type = build_pointer_type (type);
4495 else if (TREE_CODE (type) != ERROR_MARK
4496 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4498 error ("field %qs has incomplete type", name);
4499 type = error_mark_node;
4501 type = c_build_qualified_type (type, type_quals);
4502 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4503 DECL_NONADDRESSABLE_P (decl) = bitfield;
4505 if (size_varies)
4506 C_DECL_VARIABLE_SIZE (decl) = 1;
4508 else if (TREE_CODE (type) == FUNCTION_TYPE)
4510 if (storage_class == csc_register || threadp)
4512 error ("invalid storage class for function %qs", name);
4514 else if (current_scope != file_scope)
4516 /* Function declaration not at file scope. Storage
4517 classes other than `extern' are not allowed, C99
4518 6.7.1p5, and `extern' makes no difference. However,
4519 GCC allows 'auto', perhaps with 'inline', to support
4520 nested functions. */
4521 if (storage_class == csc_auto)
4523 if (pedantic)
4524 pedwarn ("invalid storage class for function %qs", name);
4526 else if (storage_class == csc_static)
4528 error ("invalid storage class for function %qs", name);
4529 if (funcdef_flag)
4530 storage_class = declspecs->storage_class = csc_none;
4531 else
4532 return 0;
4536 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4537 decl = build_decl_attribute_variant (decl, decl_attr);
4539 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4541 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4542 pedwarn ("ISO C forbids qualified function types");
4544 /* GNU C interprets a volatile-qualified function type to indicate
4545 that the function does not return. */
4546 if ((type_quals & TYPE_QUAL_VOLATILE)
4547 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4548 warning (0, "%<noreturn%> function returns non-void value");
4550 /* Every function declaration is an external reference
4551 (DECL_EXTERNAL) except for those which are not at file
4552 scope and are explicitly declared "auto". This is
4553 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4554 GCC to signify a forward declaration of a nested function. */
4555 if (storage_class == csc_auto && current_scope != file_scope)
4556 DECL_EXTERNAL (decl) = 0;
4557 else
4558 DECL_EXTERNAL (decl) = 1;
4560 /* Record absence of global scope for `static' or `auto'. */
4561 TREE_PUBLIC (decl)
4562 = !(storage_class == csc_static || storage_class == csc_auto);
4564 /* For a function definition, record the argument information
4565 block where store_parm_decls will look for it. */
4566 if (funcdef_flag)
4567 current_function_arg_info = arg_info;
4569 if (declspecs->default_int_p)
4570 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4572 /* Record presence of `inline', if it is reasonable. */
4573 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4575 if (declspecs->inline_p)
4576 pedwarn ("cannot inline function %<main%>");
4578 else if (declspecs->inline_p)
4580 /* Record that the function is declared `inline'. */
4581 DECL_DECLARED_INLINE_P (decl) = 1;
4583 /* Do not mark bare declarations as DECL_INLINE. Doing so
4584 in the presence of multiple declarations can result in
4585 the abstract origin pointing between the declarations,
4586 which will confuse dwarf2out. */
4587 if (initialized)
4589 DECL_INLINE (decl) = 1;
4590 if (storage_class == csc_extern)
4591 current_extern_inline = 1;
4594 /* If -finline-functions, assume it can be inlined. This does
4595 two things: let the function be deferred until it is actually
4596 needed, and let dwarf2 know that the function is inlinable. */
4597 else if (flag_inline_trees == 2 && initialized)
4598 DECL_INLINE (decl) = 1;
4600 else
4602 /* It's a variable. */
4603 /* An uninitialized decl with `extern' is a reference. */
4604 int extern_ref = !initialized && storage_class == csc_extern;
4606 type = c_build_qualified_type (type, type_quals);
4608 /* C99 6.2.2p7: It is invalid (compile-time undefined
4609 behavior) to create an 'extern' declaration for a
4610 variable if there is a global declaration that is
4611 'static' and the global declaration is not visible.
4612 (If the static declaration _is_ currently visible,
4613 the 'extern' declaration is taken to refer to that decl.) */
4614 if (extern_ref && current_scope != file_scope)
4616 tree global_decl = identifier_global_value (declarator->u.id);
4617 tree visible_decl = lookup_name (declarator->u.id);
4619 if (global_decl
4620 && global_decl != visible_decl
4621 && TREE_CODE (global_decl) == VAR_DECL
4622 && !TREE_PUBLIC (global_decl))
4623 error ("variable previously declared %<static%> redeclared "
4624 "%<extern%>");
4627 decl = build_decl (VAR_DECL, declarator->u.id, type);
4628 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4629 if (size_varies)
4630 C_DECL_VARIABLE_SIZE (decl) = 1;
4632 if (declspecs->inline_p)
4633 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4635 /* At file scope, an initialized extern declaration may follow
4636 a static declaration. In that case, DECL_EXTERNAL will be
4637 reset later in start_decl. */
4638 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4640 /* At file scope, the presence of a `static' or `register' storage
4641 class specifier, or the absence of all storage class specifiers
4642 makes this declaration a definition (perhaps tentative). Also,
4643 the absence of both `static' and `register' makes it public. */
4644 if (current_scope == file_scope)
4646 TREE_PUBLIC (decl) = !(storage_class == csc_static
4647 || storage_class == csc_register);
4648 TREE_STATIC (decl) = !extern_ref;
4650 /* Not at file scope, only `static' makes a static definition. */
4651 else
4653 TREE_STATIC (decl) = (storage_class == csc_static);
4654 TREE_PUBLIC (decl) = extern_ref;
4657 if (threadp)
4659 if (targetm.have_tls)
4660 DECL_THREAD_LOCAL (decl) = 1;
4661 else
4662 /* A mere warning is sure to result in improper semantics
4663 at runtime. Don't bother to allow this to compile. */
4664 error ("thread-local storage not supported for this target");
4668 /* Record `register' declaration for warnings on &
4669 and in case doing stupid register allocation. */
4671 if (storage_class == csc_register)
4673 C_DECL_REGISTER (decl) = 1;
4674 DECL_REGISTER (decl) = 1;
4677 /* Record constancy and volatility. */
4678 c_apply_type_quals_to_decl (type_quals, decl);
4680 /* If a type has volatile components, it should be stored in memory.
4681 Otherwise, the fact that those components are volatile
4682 will be ignored, and would even crash the compiler. */
4683 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4685 /* It is not an error for a structure with volatile fields to
4686 be declared register, but reset DECL_REGISTER since it
4687 cannot actually go in a register. */
4688 int was_reg = C_DECL_REGISTER (decl);
4689 C_DECL_REGISTER (decl) = 0;
4690 DECL_REGISTER (decl) = 0;
4691 c_mark_addressable (decl);
4692 C_DECL_REGISTER (decl) = was_reg;
4695 /* This is the earliest point at which we might know the assembler
4696 name of a variable. Thus, if it's known before this, die horribly. */
4697 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4699 decl_attributes (&decl, returned_attrs, 0);
4701 return decl;
4705 /* Decode the parameter-list info for a function type or function definition.
4706 The argument is the value returned by `get_parm_info' (or made in parse.y
4707 if there is an identifier list instead of a parameter decl list).
4708 These two functions are separate because when a function returns
4709 or receives functions then each is called multiple times but the order
4710 of calls is different. The last call to `grokparms' is always the one
4711 that contains the formal parameter names of a function definition.
4713 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4715 FUNCDEF_FLAG is true for a function definition, false for
4716 a mere declaration. A nonempty identifier-list gets an error message
4717 when FUNCDEF_FLAG is false. */
4719 static tree
4720 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4722 tree arg_types = arg_info->types;
4724 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4725 && !in_system_header)
4726 warning (0, "function declaration isn%'t a prototype");
4728 if (arg_types == error_mark_node)
4729 return 0; /* don't set TYPE_ARG_TYPES in this case */
4731 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4733 if (!funcdef_flag)
4734 pedwarn ("parameter names (without types) in function declaration");
4736 arg_info->parms = arg_info->types;
4737 arg_info->types = 0;
4738 return 0;
4740 else
4742 tree parm, type, typelt;
4743 unsigned int parmno;
4745 /* If there is a parameter of incomplete type in a definition,
4746 this is an error. In a declaration this is valid, and a
4747 struct or union type may be completed later, before any calls
4748 or definition of the function. In the case where the tag was
4749 first declared within the parameter list, a warning has
4750 already been given. If a parameter has void type, then
4751 however the function cannot be defined or called, so
4752 warn. */
4754 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4755 parm;
4756 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4758 type = TREE_VALUE (typelt);
4759 if (type == error_mark_node)
4760 continue;
4762 if (!COMPLETE_TYPE_P (type))
4764 if (funcdef_flag)
4766 if (DECL_NAME (parm))
4767 error ("%Jparameter %u (%qD) has incomplete type",
4768 parm, parmno, parm);
4769 else
4770 error ("%Jparameter %u has incomplete type",
4771 parm, parmno);
4773 TREE_VALUE (typelt) = error_mark_node;
4774 TREE_TYPE (parm) = error_mark_node;
4776 else if (VOID_TYPE_P (type))
4778 if (DECL_NAME (parm))
4779 warning (0, "%Jparameter %u (%qD) has void type",
4780 parm, parmno, parm);
4781 else
4782 warning (0, "%Jparameter %u has void type",
4783 parm, parmno);
4787 return arg_types;
4791 /* Take apart the current scope and return a c_arg_info structure with
4792 info on a parameter list just parsed.
4794 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4796 ELLIPSIS being true means the argument list ended in '...' so don't
4797 append a sentinel (void_list_node) to the end of the type-list. */
4799 struct c_arg_info *
4800 get_parm_info (bool ellipsis)
4802 struct c_binding *b = current_scope->bindings;
4803 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4804 struct c_arg_info);
4805 tree parms = 0;
4806 tree tags = 0;
4807 tree types = 0;
4808 tree others = 0;
4810 static bool explained_incomplete_types = false;
4811 bool gave_void_only_once_err = false;
4813 arg_info->parms = 0;
4814 arg_info->tags = 0;
4815 arg_info->types = 0;
4816 arg_info->others = 0;
4818 /* The bindings in this scope must not get put into a block.
4819 We will take care of deleting the binding nodes. */
4820 current_scope->bindings = 0;
4822 /* This function is only called if there was *something* on the
4823 parameter list. */
4824 gcc_assert (b);
4826 /* A parameter list consisting solely of 'void' indicates that the
4827 function takes no arguments. But if the 'void' is qualified
4828 (by 'const' or 'volatile'), or has a storage class specifier
4829 ('register'), then the behavior is undefined; issue an error.
4830 Typedefs for 'void' are OK (see DR#157). */
4831 if (b->prev == 0 /* one binding */
4832 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4833 && !DECL_NAME (b->decl) /* anonymous */
4834 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4836 if (TREE_THIS_VOLATILE (b->decl)
4837 || TREE_READONLY (b->decl)
4838 || C_DECL_REGISTER (b->decl))
4839 error ("%<void%> as only parameter may not be qualified");
4841 /* There cannot be an ellipsis. */
4842 if (ellipsis)
4843 error ("%<void%> must be the only parameter");
4845 arg_info->types = void_list_node;
4846 return arg_info;
4849 if (!ellipsis)
4850 types = void_list_node;
4852 /* Break up the bindings list into parms, tags, types, and others;
4853 apply sanity checks; purge the name-to-decl bindings. */
4854 while (b)
4856 tree decl = b->decl;
4857 tree type = TREE_TYPE (decl);
4858 const char *keyword;
4860 switch (TREE_CODE (decl))
4862 case PARM_DECL:
4863 if (b->id)
4865 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4866 I_SYMBOL_BINDING (b->id) = b->shadowed;
4869 /* Check for forward decls that never got their actual decl. */
4870 if (TREE_ASM_WRITTEN (decl))
4871 error ("%Jparameter %qD has just a forward declaration",
4872 decl, decl);
4873 /* Check for (..., void, ...) and issue an error. */
4874 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4876 if (!gave_void_only_once_err)
4878 error ("%<void%> must be the only parameter");
4879 gave_void_only_once_err = true;
4882 else
4884 /* Valid parameter, add it to the list. */
4885 TREE_CHAIN (decl) = parms;
4886 parms = decl;
4888 /* Since there is a prototype, args are passed in their
4889 declared types. The back end may override this later. */
4890 DECL_ARG_TYPE (decl) = type;
4891 types = tree_cons (0, type, types);
4893 break;
4895 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4896 case UNION_TYPE: keyword = "union"; goto tag;
4897 case RECORD_TYPE: keyword = "struct"; goto tag;
4898 tag:
4899 /* Types may not have tag-names, in which case the type
4900 appears in the bindings list with b->id NULL. */
4901 if (b->id)
4903 gcc_assert (I_TAG_BINDING (b->id) == b);
4904 I_TAG_BINDING (b->id) = b->shadowed;
4907 /* Warn about any struct, union or enum tags defined in a
4908 parameter list. The scope of such types is limited to
4909 the parameter list, which is rarely if ever desirable
4910 (it's impossible to call such a function with type-
4911 correct arguments). An anonymous union parm type is
4912 meaningful as a GNU extension, so don't warn for that. */
4913 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4915 if (b->id)
4916 /* The %s will be one of 'struct', 'union', or 'enum'. */
4917 warning (0, "%<%s %E%> declared inside parameter list",
4918 keyword, b->id);
4919 else
4920 /* The %s will be one of 'struct', 'union', or 'enum'. */
4921 warning (0, "anonymous %s declared inside parameter list",
4922 keyword);
4924 if (!explained_incomplete_types)
4926 warning (0, "its scope is only this definition or declaration,"
4927 " which is probably not what you want");
4928 explained_incomplete_types = true;
4932 tags = tree_cons (b->id, decl, tags);
4933 break;
4935 case CONST_DECL:
4936 case TYPE_DECL:
4937 case FUNCTION_DECL:
4938 /* CONST_DECLs appear here when we have an embedded enum,
4939 and TYPE_DECLs appear here when we have an embedded struct
4940 or union. No warnings for this - we already warned about the
4941 type itself. FUNCTION_DECLs appear when there is an implicit
4942 function declaration in the parameter list. */
4944 TREE_CHAIN (decl) = others;
4945 others = decl;
4946 /* fall through */
4948 case ERROR_MARK:
4949 /* error_mark_node appears here when we have an undeclared
4950 variable. Just throw it away. */
4951 if (b->id)
4953 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4954 I_SYMBOL_BINDING (b->id) = b->shadowed;
4956 break;
4958 /* Other things that might be encountered. */
4959 case LABEL_DECL:
4960 case VAR_DECL:
4961 default:
4962 gcc_unreachable ();
4965 b = free_binding_and_advance (b);
4968 arg_info->parms = parms;
4969 arg_info->tags = tags;
4970 arg_info->types = types;
4971 arg_info->others = others;
4972 return arg_info;
4975 /* Get the struct, enum or union (CODE says which) with tag NAME.
4976 Define the tag as a forward-reference if it is not defined.
4977 Return a c_typespec structure for the type specifier. */
4979 struct c_typespec
4980 parser_xref_tag (enum tree_code code, tree name)
4982 struct c_typespec ret;
4983 /* If a cross reference is requested, look up the type
4984 already defined for this tag and return it. */
4986 tree ref = lookup_tag (code, name, 0);
4987 /* If this is the right type of tag, return what we found.
4988 (This reference will be shadowed by shadow_tag later if appropriate.)
4989 If this is the wrong type of tag, do not return it. If it was the
4990 wrong type in the same scope, we will have had an error
4991 message already; if in a different scope and declaring
4992 a name, pending_xref_error will give an error message; but if in a
4993 different scope and not declaring a name, this tag should
4994 shadow the previous declaration of a different type of tag, and
4995 this would not work properly if we return the reference found.
4996 (For example, with "struct foo" in an outer scope, "union foo;"
4997 must shadow that tag with a new one of union type.) */
4998 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4999 if (ref && TREE_CODE (ref) == code)
5001 ret.spec = ref;
5002 return ret;
5005 /* If no such tag is yet defined, create a forward-reference node
5006 and record it as the "definition".
5007 When a real declaration of this type is found,
5008 the forward-reference will be altered into a real type. */
5010 ref = make_node (code);
5011 if (code == ENUMERAL_TYPE)
5013 /* Give the type a default layout like unsigned int
5014 to avoid crashing if it does not get defined. */
5015 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5016 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5017 TYPE_USER_ALIGN (ref) = 0;
5018 TYPE_UNSIGNED (ref) = 1;
5019 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5020 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5021 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5024 pushtag (name, ref);
5026 ret.spec = ref;
5027 return ret;
5030 /* Get the struct, enum or union (CODE says which) with tag NAME.
5031 Define the tag as a forward-reference if it is not defined.
5032 Return a tree for the type. */
5034 tree
5035 xref_tag (enum tree_code code, tree name)
5037 return parser_xref_tag (code, name).spec;
5040 /* Make sure that the tag NAME is defined *in the current scope*
5041 at least as a forward reference.
5042 CODE says which kind of tag NAME ought to be. */
5044 tree
5045 start_struct (enum tree_code code, tree name)
5047 /* If there is already a tag defined at this scope
5048 (as a forward reference), just return it. */
5050 tree ref = 0;
5052 if (name != 0)
5053 ref = lookup_tag (code, name, 1);
5054 if (ref && TREE_CODE (ref) == code)
5056 if (TYPE_SIZE (ref))
5058 if (code == UNION_TYPE)
5059 error ("redefinition of %<union %E%>", name);
5060 else
5061 error ("redefinition of %<struct %E%>", name);
5063 else if (C_TYPE_BEING_DEFINED (ref))
5065 if (code == UNION_TYPE)
5066 error ("nested redefinition of %<union %E%>", name);
5067 else
5068 error ("nested redefinition of %<struct %E%>", name);
5071 else
5073 /* Otherwise create a forward-reference just so the tag is in scope. */
5075 ref = make_node (code);
5076 pushtag (name, ref);
5079 C_TYPE_BEING_DEFINED (ref) = 1;
5080 TYPE_PACKED (ref) = flag_pack_struct;
5081 return ref;
5084 /* Process the specs, declarator and width (NULL if omitted)
5085 of a structure component, returning a FIELD_DECL node.
5086 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5088 This is done during the parsing of the struct declaration.
5089 The FIELD_DECL nodes are chained together and the lot of them
5090 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5092 tree
5093 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5094 tree width)
5096 tree value;
5098 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5099 && width == NULL_TREE)
5101 /* This is an unnamed decl.
5103 If we have something of the form "union { list } ;" then this
5104 is the anonymous union extension. Similarly for struct.
5106 If this is something of the form "struct foo;", then
5107 If MS extensions are enabled, this is handled as an
5108 anonymous struct.
5109 Otherwise this is a forward declaration of a structure tag.
5111 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5112 If MS extensions are enabled and foo names a structure, then
5113 again this is an anonymous struct.
5114 Otherwise this is an error.
5116 Oh what a horrid tangled web we weave. I wonder if MS consciously
5117 took this from Plan 9 or if it was an accident of implementation
5118 that took root before someone noticed the bug... */
5120 tree type = declspecs->type;
5121 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5122 || TREE_CODE (type) == UNION_TYPE);
5123 bool ok = false;
5125 if (type_ok
5126 && (flag_ms_extensions || !declspecs->typedef_p))
5128 if (flag_ms_extensions)
5129 ok = true;
5130 else if (flag_iso)
5131 ok = false;
5132 else if (TYPE_NAME (type) == NULL)
5133 ok = true;
5134 else
5135 ok = false;
5137 if (!ok)
5139 pedwarn ("declaration does not declare anything");
5140 return NULL_TREE;
5142 if (pedantic)
5143 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5146 value = grokdeclarator (declarator, declspecs, FIELD, false,
5147 width ? &width : NULL);
5149 finish_decl (value, NULL_TREE, NULL_TREE);
5150 DECL_INITIAL (value) = width;
5152 return value;
5155 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5156 the list such that this does not present a problem later. */
5158 static void
5159 detect_field_duplicates (tree fieldlist)
5161 tree x, y;
5162 int timeout = 10;
5164 /* First, see if there are more than "a few" fields.
5165 This is trivially true if there are zero or one fields. */
5166 if (!fieldlist)
5167 return;
5168 x = TREE_CHAIN (fieldlist);
5169 if (!x)
5170 return;
5171 do {
5172 timeout--;
5173 x = TREE_CHAIN (x);
5174 } while (timeout > 0 && x);
5176 /* If there were "few" fields, avoid the overhead of allocating
5177 a hash table. Instead just do the nested traversal thing. */
5178 if (timeout > 0)
5180 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5181 if (DECL_NAME (x))
5183 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5184 if (DECL_NAME (y) == DECL_NAME (x))
5186 error ("%Jduplicate member %qD", x, x);
5187 DECL_NAME (x) = NULL_TREE;
5191 else
5193 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5194 void **slot;
5196 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5197 if ((y = DECL_NAME (x)) != 0)
5199 slot = htab_find_slot (htab, y, INSERT);
5200 if (*slot)
5202 error ("%Jduplicate member %qD", x, x);
5203 DECL_NAME (x) = NULL_TREE;
5205 *slot = y;
5208 htab_delete (htab);
5212 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5213 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5214 ATTRIBUTES are attributes to be applied to the structure. */
5216 tree
5217 finish_struct (tree t, tree fieldlist, tree attributes)
5219 tree x;
5220 bool toplevel = file_scope == current_scope;
5221 int saw_named_field;
5223 /* If this type was previously laid out as a forward reference,
5224 make sure we lay it out again. */
5226 TYPE_SIZE (t) = 0;
5228 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5230 if (pedantic)
5232 for (x = fieldlist; x; x = TREE_CHAIN (x))
5233 if (DECL_NAME (x) != 0)
5234 break;
5236 if (x == 0)
5238 if (TREE_CODE (t) == UNION_TYPE)
5240 if (fieldlist)
5241 pedwarn ("union has no named members");
5242 else
5243 pedwarn ("union has no members");
5245 else
5247 if (fieldlist)
5248 pedwarn ("struct has no named members");
5249 else
5250 pedwarn ("struct has no members");
5255 /* Install struct as DECL_CONTEXT of each field decl.
5256 Also process specified field sizes, found in the DECL_INITIAL,
5257 storing 0 there after the type has been changed to precision equal
5258 to its width, rather than the precision of the specified standard
5259 type. (Correct layout requires the original type to have been preserved
5260 until now.) */
5262 saw_named_field = 0;
5263 for (x = fieldlist; x; x = TREE_CHAIN (x))
5265 DECL_CONTEXT (x) = t;
5266 DECL_PACKED (x) |= TYPE_PACKED (t);
5268 /* If any field is const, the structure type is pseudo-const. */
5269 if (TREE_READONLY (x))
5270 C_TYPE_FIELDS_READONLY (t) = 1;
5271 else
5273 /* A field that is pseudo-const makes the structure likewise. */
5274 tree t1 = TREE_TYPE (x);
5275 while (TREE_CODE (t1) == ARRAY_TYPE)
5276 t1 = TREE_TYPE (t1);
5277 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5278 && C_TYPE_FIELDS_READONLY (t1))
5279 C_TYPE_FIELDS_READONLY (t) = 1;
5282 /* Any field that is volatile means variables of this type must be
5283 treated in some ways as volatile. */
5284 if (TREE_THIS_VOLATILE (x))
5285 C_TYPE_FIELDS_VOLATILE (t) = 1;
5287 /* Any field of nominal variable size implies structure is too. */
5288 if (C_DECL_VARIABLE_SIZE (x))
5289 C_TYPE_VARIABLE_SIZE (t) = 1;
5291 if (DECL_INITIAL (x))
5293 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5294 DECL_SIZE (x) = bitsize_int (width);
5295 DECL_BIT_FIELD (x) = 1;
5296 SET_DECL_C_BIT_FIELD (x);
5299 /* Detect flexible array member in an invalid context. */
5300 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5301 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5302 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5303 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5305 if (TREE_CODE (t) == UNION_TYPE)
5307 error ("%Jflexible array member in union", x);
5308 TREE_TYPE (x) = error_mark_node;
5310 else if (TREE_CHAIN (x) != NULL_TREE)
5312 error ("%Jflexible array member not at end of struct", x);
5313 TREE_TYPE (x) = error_mark_node;
5315 else if (!saw_named_field)
5317 error ("%Jflexible array member in otherwise empty struct", x);
5318 TREE_TYPE (x) = error_mark_node;
5322 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5323 && flexible_array_type_p (TREE_TYPE (x)))
5324 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5326 if (DECL_NAME (x))
5327 saw_named_field = 1;
5330 detect_field_duplicates (fieldlist);
5332 /* Now we have the nearly final fieldlist. Record it,
5333 then lay out the structure or union (including the fields). */
5335 TYPE_FIELDS (t) = fieldlist;
5337 layout_type (t);
5339 /* Give bit-fields their proper types. */
5341 tree *fieldlistp = &fieldlist;
5342 while (*fieldlistp)
5343 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5344 && TREE_TYPE (*fieldlistp) != error_mark_node)
5346 unsigned HOST_WIDE_INT width
5347 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5348 tree type = TREE_TYPE (*fieldlistp);
5349 if (width != TYPE_PRECISION (type))
5351 TREE_TYPE (*fieldlistp)
5352 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5353 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5355 DECL_INITIAL (*fieldlistp) = 0;
5357 else
5358 fieldlistp = &TREE_CHAIN (*fieldlistp);
5361 /* Now we have the truly final field list.
5362 Store it in this type and in the variants. */
5364 TYPE_FIELDS (t) = fieldlist;
5366 /* If there are lots of fields, sort so we can look through them fast.
5367 We arbitrarily consider 16 or more elts to be "a lot". */
5370 int len = 0;
5372 for (x = fieldlist; x; x = TREE_CHAIN (x))
5374 if (len > 15 || DECL_NAME (x) == NULL)
5375 break;
5376 len += 1;
5379 if (len > 15)
5381 tree *field_array;
5382 struct lang_type *space;
5383 struct sorted_fields_type *space2;
5385 len += list_length (x);
5387 /* Use the same allocation policy here that make_node uses, to
5388 ensure that this lives as long as the rest of the struct decl.
5389 All decls in an inline function need to be saved. */
5391 space = GGC_CNEW (struct lang_type);
5392 space2 = GGC_NEWVAR (struct sorted_fields_type,
5393 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5395 len = 0;
5396 space->s = space2;
5397 field_array = &space2->elts[0];
5398 for (x = fieldlist; x; x = TREE_CHAIN (x))
5400 field_array[len++] = x;
5402 /* If there is anonymous struct or union, break out of the loop. */
5403 if (DECL_NAME (x) == NULL)
5404 break;
5406 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5407 if (x == NULL)
5409 TYPE_LANG_SPECIFIC (t) = space;
5410 TYPE_LANG_SPECIFIC (t)->s->len = len;
5411 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5412 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5417 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5419 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5420 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5421 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5422 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5425 /* If this was supposed to be a transparent union, but we can't
5426 make it one, warn and turn off the flag. */
5427 if (TREE_CODE (t) == UNION_TYPE
5428 && TYPE_TRANSPARENT_UNION (t)
5429 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5431 TYPE_TRANSPARENT_UNION (t) = 0;
5432 warning (0, "union cannot be made transparent");
5435 /* If this structure or union completes the type of any previous
5436 variable declaration, lay it out and output its rtl. */
5437 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5439 x = TREE_CHAIN (x))
5441 tree decl = TREE_VALUE (x);
5442 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5443 layout_array_type (TREE_TYPE (decl));
5444 if (TREE_CODE (decl) != TYPE_DECL)
5446 layout_decl (decl, 0);
5447 if (c_dialect_objc ())
5448 objc_check_decl (decl);
5449 rest_of_decl_compilation (decl, toplevel, 0);
5450 if (!toplevel)
5451 expand_decl (decl);
5454 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5456 /* Finish debugging output for this type. */
5457 rest_of_type_compilation (t, toplevel);
5459 /* If we're inside a function proper, i.e. not file-scope and not still
5460 parsing parameters, then arrange for the size of a variable sized type
5461 to be bound now. */
5462 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5463 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5465 return t;
5468 /* Lay out the type T, and its element type, and so on. */
5470 static void
5471 layout_array_type (tree t)
5473 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5474 layout_array_type (TREE_TYPE (t));
5475 layout_type (t);
5478 /* Begin compiling the definition of an enumeration type.
5479 NAME is its name (or null if anonymous).
5480 Returns the type object, as yet incomplete.
5481 Also records info about it so that build_enumerator
5482 may be used to declare the individual values as they are read. */
5484 tree
5485 start_enum (tree name)
5487 tree enumtype = 0;
5489 /* If this is the real definition for a previous forward reference,
5490 fill in the contents in the same object that used to be the
5491 forward reference. */
5493 if (name != 0)
5494 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5496 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5498 enumtype = make_node (ENUMERAL_TYPE);
5499 pushtag (name, enumtype);
5502 if (C_TYPE_BEING_DEFINED (enumtype))
5503 error ("nested redefinition of %<enum %E%>", name);
5505 C_TYPE_BEING_DEFINED (enumtype) = 1;
5507 if (TYPE_VALUES (enumtype) != 0)
5509 /* This enum is a named one that has been declared already. */
5510 error ("redeclaration of %<enum %E%>", name);
5512 /* Completely replace its old definition.
5513 The old enumerators remain defined, however. */
5514 TYPE_VALUES (enumtype) = 0;
5517 enum_next_value = integer_zero_node;
5518 enum_overflow = 0;
5520 if (flag_short_enums)
5521 TYPE_PACKED (enumtype) = 1;
5523 return enumtype;
5526 /* After processing and defining all the values of an enumeration type,
5527 install their decls in the enumeration type and finish it off.
5528 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5529 and ATTRIBUTES are the specified attributes.
5530 Returns ENUMTYPE. */
5532 tree
5533 finish_enum (tree enumtype, tree values, tree attributes)
5535 tree pair, tem;
5536 tree minnode = 0, maxnode = 0;
5537 int precision, unsign;
5538 bool toplevel = (file_scope == current_scope);
5539 struct lang_type *lt;
5541 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5543 /* Calculate the maximum value of any enumerator in this type. */
5545 if (values == error_mark_node)
5546 minnode = maxnode = integer_zero_node;
5547 else
5549 minnode = maxnode = TREE_VALUE (values);
5550 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5552 tree value = TREE_VALUE (pair);
5553 if (tree_int_cst_lt (maxnode, value))
5554 maxnode = value;
5555 if (tree_int_cst_lt (value, minnode))
5556 minnode = value;
5560 /* Construct the final type of this enumeration. It is the same
5561 as one of the integral types - the narrowest one that fits, except
5562 that normally we only go as narrow as int - and signed iff any of
5563 the values are negative. */
5564 unsign = (tree_int_cst_sgn (minnode) >= 0);
5565 precision = MAX (min_precision (minnode, unsign),
5566 min_precision (maxnode, unsign));
5568 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5570 tem = c_common_type_for_size (precision, unsign);
5571 if (tem == NULL)
5573 warning (0, "enumeration values exceed range of largest integer");
5574 tem = long_long_integer_type_node;
5577 else
5578 tem = unsign ? unsigned_type_node : integer_type_node;
5580 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5581 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5582 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5583 TYPE_SIZE (enumtype) = 0;
5585 /* If the precision of the type was specific with an attribute and it
5586 was too small, give an error. Otherwise, use it. */
5587 if (TYPE_PRECISION (enumtype))
5589 if (precision > TYPE_PRECISION (enumtype))
5590 error ("specified mode too small for enumeral values");
5592 else
5593 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5595 layout_type (enumtype);
5597 if (values != error_mark_node)
5599 /* Change the type of the enumerators to be the enum type. We
5600 need to do this irrespective of the size of the enum, for
5601 proper type checking. Replace the DECL_INITIALs of the
5602 enumerators, and the value slots of the list, with copies
5603 that have the enum type; they cannot be modified in place
5604 because they may be shared (e.g. integer_zero_node) Finally,
5605 change the purpose slots to point to the names of the decls. */
5606 for (pair = values; pair; pair = TREE_CHAIN (pair))
5608 tree enu = TREE_PURPOSE (pair);
5609 tree ini = DECL_INITIAL (enu);
5611 TREE_TYPE (enu) = enumtype;
5613 /* The ISO C Standard mandates enumerators to have type int,
5614 even though the underlying type of an enum type is
5615 unspecified. Here we convert any enumerators that fit in
5616 an int to type int, to avoid promotions to unsigned types
5617 when comparing integers with enumerators that fit in the
5618 int range. When -pedantic is given, build_enumerator()
5619 would have already taken care of those that don't fit. */
5620 if (int_fits_type_p (ini, integer_type_node))
5621 tem = integer_type_node;
5622 else
5623 tem = enumtype;
5624 ini = convert (tem, ini);
5626 DECL_INITIAL (enu) = ini;
5627 TREE_PURPOSE (pair) = DECL_NAME (enu);
5628 TREE_VALUE (pair) = ini;
5631 TYPE_VALUES (enumtype) = values;
5634 /* Record the min/max values so that we can warn about bit-field
5635 enumerations that are too small for the values. */
5636 lt = GGC_CNEW (struct lang_type);
5637 lt->enum_min = minnode;
5638 lt->enum_max = maxnode;
5639 TYPE_LANG_SPECIFIC (enumtype) = lt;
5641 /* Fix up all variant types of this enum type. */
5642 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5644 if (tem == enumtype)
5645 continue;
5646 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5647 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5648 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5649 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5650 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5651 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5652 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5653 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5654 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5655 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5656 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5659 /* Finish debugging output for this type. */
5660 rest_of_type_compilation (enumtype, toplevel);
5662 return enumtype;
5665 /* Build and install a CONST_DECL for one value of the
5666 current enumeration type (one that was begun with start_enum).
5667 Return a tree-list containing the CONST_DECL and its value.
5668 Assignment of sequential values by default is handled here. */
5670 tree
5671 build_enumerator (tree name, tree value)
5673 tree decl, type;
5675 /* Validate and default VALUE. */
5677 if (value != 0)
5679 /* Don't issue more errors for error_mark_node (i.e. an
5680 undeclared identifier) - just ignore the value expression. */
5681 if (value == error_mark_node)
5682 value = 0;
5683 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5684 || TREE_CODE (value) != INTEGER_CST)
5686 error ("enumerator value for %qE is not an integer constant", name);
5687 value = 0;
5689 else
5691 value = default_conversion (value);
5692 constant_expression_warning (value);
5696 /* Default based on previous value. */
5697 /* It should no longer be possible to have NON_LVALUE_EXPR
5698 in the default. */
5699 if (value == 0)
5701 value = enum_next_value;
5702 if (enum_overflow)
5703 error ("overflow in enumeration values");
5706 if (pedantic && !int_fits_type_p (value, integer_type_node))
5708 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5709 /* XXX This causes -pedantic to change the meaning of the program.
5710 Remove? -zw 2004-03-15 */
5711 value = convert (integer_type_node, value);
5714 /* Set basis for default for next value. */
5715 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5716 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5718 /* Now create a declaration for the enum value name. */
5720 type = TREE_TYPE (value);
5721 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5722 TYPE_PRECISION (integer_type_node)),
5723 (TYPE_PRECISION (type)
5724 >= TYPE_PRECISION (integer_type_node)
5725 && TYPE_UNSIGNED (type)));
5727 decl = build_decl (CONST_DECL, name, type);
5728 DECL_INITIAL (decl) = convert (type, value);
5729 pushdecl (decl);
5731 return tree_cons (decl, value, NULL_TREE);
5735 /* Create the FUNCTION_DECL for a function definition.
5736 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5737 the declaration; they describe the function's name and the type it returns,
5738 but twisted together in a fashion that parallels the syntax of C.
5740 This function creates a binding context for the function body
5741 as well as setting up the FUNCTION_DECL in current_function_decl.
5743 Returns 1 on success. If the DECLARATOR is not suitable for a function
5744 (it defines a datum instead), we return 0, which tells
5745 yyparse to report a parse error. */
5748 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5749 tree attributes)
5751 tree decl1, old_decl;
5752 tree restype, resdecl;
5753 struct c_label_context_se *nstack_se;
5754 struct c_label_context_vm *nstack_vm;
5756 current_function_returns_value = 0; /* Assume, until we see it does. */
5757 current_function_returns_null = 0;
5758 current_function_returns_abnormally = 0;
5759 warn_about_return_type = 0;
5760 current_extern_inline = 0;
5761 c_switch_stack = NULL;
5763 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5764 nstack_se->labels_def = NULL;
5765 nstack_se->labels_used = NULL;
5766 nstack_se->next = label_context_stack_se;
5767 label_context_stack_se = nstack_se;
5769 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5770 nstack_vm->labels_def = NULL;
5771 nstack_vm->labels_used = NULL;
5772 nstack_vm->scope = 0;
5773 nstack_vm->next = label_context_stack_vm;
5774 label_context_stack_vm = nstack_vm;
5776 /* Indicate no valid break/continue context by setting these variables
5777 to some non-null, non-label value. We'll notice and emit the proper
5778 error message in c_finish_bc_stmt. */
5779 c_break_label = c_cont_label = size_zero_node;
5781 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5783 /* If the declarator is not suitable for a function definition,
5784 cause a syntax error. */
5785 if (decl1 == 0)
5786 return 0;
5788 decl_attributes (&decl1, attributes, 0);
5790 if (DECL_DECLARED_INLINE_P (decl1)
5791 && DECL_UNINLINABLE (decl1)
5792 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5793 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5795 announce_function (decl1);
5797 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5799 error ("return type is an incomplete type");
5800 /* Make it return void instead. */
5801 TREE_TYPE (decl1)
5802 = build_function_type (void_type_node,
5803 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5806 if (warn_about_return_type)
5807 pedwarn_c99 ("return type defaults to %<int%>");
5809 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5810 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5811 DECL_INITIAL (decl1) = error_mark_node;
5813 /* If this definition isn't a prototype and we had a prototype declaration
5814 before, copy the arg type info from that prototype. */
5815 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5816 current_function_prototype_locus = UNKNOWN_LOCATION;
5817 current_function_prototype_built_in = false;
5818 current_function_prototype_arg_types = NULL_TREE;
5819 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5821 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5822 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5823 TREE_TYPE (TREE_TYPE (old_decl))))
5825 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5826 TREE_TYPE (decl1));
5827 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5828 current_function_prototype_built_in
5829 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5830 current_function_prototype_arg_types
5831 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5833 if (TREE_PUBLIC (decl1))
5835 /* If there is an external prototype declaration of this
5836 function, record its location but do not copy information
5837 to this decl. This may be an invisible declaration
5838 (built-in or in a scope which has finished) or simply
5839 have more refined argument types than any declaration
5840 found above. */
5841 struct c_binding *b;
5842 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5843 if (B_IN_SCOPE (b, external_scope))
5844 break;
5845 if (b)
5847 tree ext_decl, ext_type;
5848 ext_decl = b->decl;
5849 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5850 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5851 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5852 TREE_TYPE (ext_type)))
5854 current_function_prototype_locus
5855 = DECL_SOURCE_LOCATION (ext_decl);
5856 current_function_prototype_built_in
5857 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5858 current_function_prototype_arg_types
5859 = TYPE_ARG_TYPES (ext_type);
5865 /* Optionally warn of old-fashioned def with no previous prototype. */
5866 if (warn_strict_prototypes
5867 && old_decl != error_mark_node
5868 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5869 && C_DECL_ISNT_PROTOTYPE (old_decl))
5870 warning (0, "function declaration isn%'t a prototype");
5871 /* Optionally warn of any global def with no previous prototype. */
5872 else if (warn_missing_prototypes
5873 && old_decl != error_mark_node
5874 && TREE_PUBLIC (decl1)
5875 && !MAIN_NAME_P (DECL_NAME (decl1))
5876 && C_DECL_ISNT_PROTOTYPE (old_decl))
5877 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5878 /* Optionally warn of any def with no previous prototype
5879 if the function has already been used. */
5880 else if (warn_missing_prototypes
5881 && old_decl != 0
5882 && old_decl != error_mark_node
5883 && TREE_USED (old_decl)
5884 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5885 warning (0, "%J%qD was used with no prototype before its definition",
5886 decl1, decl1);
5887 /* Optionally warn of any global def with no previous declaration. */
5888 else if (warn_missing_declarations
5889 && TREE_PUBLIC (decl1)
5890 && old_decl == 0
5891 && !MAIN_NAME_P (DECL_NAME (decl1)))
5892 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5893 /* Optionally warn of any def with no previous declaration
5894 if the function has already been used. */
5895 else if (warn_missing_declarations
5896 && old_decl != 0
5897 && old_decl != error_mark_node
5898 && TREE_USED (old_decl)
5899 && C_DECL_IMPLICIT (old_decl))
5900 warning (0, "%J%qD was used with no declaration before its definition",
5901 decl1, decl1);
5903 /* This is a definition, not a reference.
5904 So normally clear DECL_EXTERNAL.
5905 However, `extern inline' acts like a declaration
5906 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5907 DECL_EXTERNAL (decl1) = current_extern_inline;
5909 /* This function exists in static storage.
5910 (This does not mean `static' in the C sense!) */
5911 TREE_STATIC (decl1) = 1;
5913 /* A nested function is not global. */
5914 if (current_function_decl != 0)
5915 TREE_PUBLIC (decl1) = 0;
5917 /* This is the earliest point at which we might know the assembler
5918 name of the function. Thus, if it's set before this, die horribly. */
5919 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5921 /* If #pragma weak was used, mark the decl weak now. */
5922 if (current_scope == file_scope)
5923 maybe_apply_pragma_weak (decl1);
5925 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5926 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5928 tree args;
5929 int argct = 0;
5931 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5932 != integer_type_node)
5933 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5935 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5936 args = TREE_CHAIN (args))
5938 tree type = args ? TREE_VALUE (args) : 0;
5940 if (type == void_type_node)
5941 break;
5943 ++argct;
5944 switch (argct)
5946 case 1:
5947 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5948 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5949 decl1, decl1);
5950 break;
5952 case 2:
5953 if (TREE_CODE (type) != POINTER_TYPE
5954 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5955 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5956 != char_type_node))
5957 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5958 decl1, decl1);
5959 break;
5961 case 3:
5962 if (TREE_CODE (type) != POINTER_TYPE
5963 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5964 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5965 != char_type_node))
5966 pedwarn ("%Jthird argument of %qD should probably be "
5967 "%<char **%>", decl1, decl1);
5968 break;
5972 /* It is intentional that this message does not mention the third
5973 argument because it's only mentioned in an appendix of the
5974 standard. */
5975 if (argct > 0 && (argct < 2 || argct > 3))
5976 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5978 if (!TREE_PUBLIC (decl1))
5979 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5982 /* Record the decl so that the function name is defined.
5983 If we already have a decl for this name, and it is a FUNCTION_DECL,
5984 use the old decl. */
5986 current_function_decl = pushdecl (decl1);
5988 push_scope ();
5989 declare_parm_level ();
5991 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5992 /* Promote the value to int before returning it. */
5993 if (c_promoting_integer_type_p (restype))
5995 /* It retains unsignedness if not really getting wider. */
5996 if (TYPE_UNSIGNED (restype)
5997 && (TYPE_PRECISION (restype)
5998 == TYPE_PRECISION (integer_type_node)))
5999 restype = unsigned_type_node;
6000 else
6001 restype = integer_type_node;
6004 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6005 DECL_ARTIFICIAL (resdecl) = 1;
6006 DECL_IGNORED_P (resdecl) = 1;
6007 DECL_RESULT (current_function_decl) = resdecl;
6009 start_fname_decls ();
6011 return 1;
6014 /* Subroutine of store_parm_decls which handles new-style function
6015 definitions (prototype format). The parms already have decls, so we
6016 need only record them as in effect and complain if any redundant
6017 old-style parm decls were written. */
6018 static void
6019 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6021 tree decl;
6023 if (current_scope->bindings)
6025 error ("%Jold-style parameter declarations in prototyped "
6026 "function definition", fndecl);
6028 /* Get rid of the old-style declarations. */
6029 pop_scope ();
6030 push_scope ();
6032 /* Don't issue this warning for nested functions, and don't issue this
6033 warning if we got here because ARG_INFO_TYPES was error_mark_node
6034 (this happens when a function definition has just an ellipsis in
6035 its parameter list). */
6036 else if (warn_traditional && !in_system_header && !current_function_scope
6037 && arg_info->types != error_mark_node)
6038 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6039 fndecl);
6041 /* Now make all the parameter declarations visible in the function body.
6042 We can bypass most of the grunt work of pushdecl. */
6043 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6045 DECL_CONTEXT (decl) = current_function_decl;
6046 if (DECL_NAME (decl))
6047 bind (DECL_NAME (decl), decl, current_scope,
6048 /*invisible=*/false, /*nested=*/false);
6049 else
6050 error ("%Jparameter name omitted", decl);
6053 /* Record the parameter list in the function declaration. */
6054 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6056 /* Now make all the ancillary declarations visible, likewise. */
6057 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6059 DECL_CONTEXT (decl) = current_function_decl;
6060 if (DECL_NAME (decl))
6061 bind (DECL_NAME (decl), decl, current_scope,
6062 /*invisible=*/false, /*nested=*/false);
6065 /* And all the tag declarations. */
6066 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6067 if (TREE_PURPOSE (decl))
6068 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6069 /*invisible=*/false, /*nested=*/false);
6072 /* Subroutine of store_parm_decls which handles old-style function
6073 definitions (separate parameter list and declarations). */
6075 static void
6076 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6078 struct c_binding *b;
6079 tree parm, decl, last;
6080 tree parmids = arg_info->parms;
6082 /* We use DECL_WEAK as a flag to show which parameters have been
6083 seen already, since it is not used on PARM_DECL. */
6084 #ifdef ENABLE_CHECKING
6085 for (b = current_scope->bindings; b; b = b->prev)
6086 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6087 #endif
6089 if (!in_system_header)
6090 warning (OPT_Wold_style_definition, "%Jold-style function definition", fndecl);
6092 /* Match each formal parameter name with its declaration. Save each
6093 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6094 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6096 if (TREE_VALUE (parm) == 0)
6098 error ("%Jparameter name missing from parameter list", fndecl);
6099 TREE_PURPOSE (parm) = 0;
6100 continue;
6103 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6104 if (b && B_IN_CURRENT_SCOPE (b))
6106 decl = b->decl;
6107 /* If we got something other than a PARM_DECL it is an error. */
6108 if (TREE_CODE (decl) != PARM_DECL)
6109 error ("%J%qD declared as a non-parameter", decl, decl);
6110 /* If the declaration is already marked, we have a duplicate
6111 name. Complain and ignore the duplicate. */
6112 else if (DECL_WEAK (decl))
6114 error ("%Jmultiple parameters named %qD", decl, decl);
6115 TREE_PURPOSE (parm) = 0;
6116 continue;
6118 /* If the declaration says "void", complain and turn it into
6119 an int. */
6120 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6122 error ("%Jparameter %qD declared with void type", decl, decl);
6123 TREE_TYPE (decl) = integer_type_node;
6124 DECL_ARG_TYPE (decl) = integer_type_node;
6125 layout_decl (decl, 0);
6128 /* If no declaration found, default to int. */
6129 else
6131 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6132 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6133 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6134 pushdecl (decl);
6136 if (flag_isoc99)
6137 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6138 else if (extra_warnings)
6139 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6142 TREE_PURPOSE (parm) = decl;
6143 DECL_WEAK (decl) = 1;
6146 /* Now examine the parms chain for incomplete declarations
6147 and declarations with no corresponding names. */
6149 for (b = current_scope->bindings; b; b = b->prev)
6151 parm = b->decl;
6152 if (TREE_CODE (parm) != PARM_DECL)
6153 continue;
6155 if (TREE_TYPE (parm) != error_mark_node
6156 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6158 error ("%Jparameter %qD has incomplete type", parm, parm);
6159 TREE_TYPE (parm) = error_mark_node;
6162 if (!DECL_WEAK (parm))
6164 error ("%Jdeclaration for parameter %qD but no such parameter",
6165 parm, parm);
6167 /* Pretend the parameter was not missing.
6168 This gets us to a standard state and minimizes
6169 further error messages. */
6170 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6174 /* Chain the declarations together in the order of the list of
6175 names. Store that chain in the function decl, replacing the
6176 list of names. Update the current scope to match. */
6177 DECL_ARGUMENTS (fndecl) = 0;
6179 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6180 if (TREE_PURPOSE (parm))
6181 break;
6182 if (parm && TREE_PURPOSE (parm))
6184 last = TREE_PURPOSE (parm);
6185 DECL_ARGUMENTS (fndecl) = last;
6186 DECL_WEAK (last) = 0;
6188 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6189 if (TREE_PURPOSE (parm))
6191 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6192 last = TREE_PURPOSE (parm);
6193 DECL_WEAK (last) = 0;
6195 TREE_CHAIN (last) = 0;
6198 /* If there was a previous prototype,
6199 set the DECL_ARG_TYPE of each argument according to
6200 the type previously specified, and report any mismatches. */
6202 if (current_function_prototype_arg_types)
6204 tree type;
6205 for (parm = DECL_ARGUMENTS (fndecl),
6206 type = current_function_prototype_arg_types;
6207 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6208 != void_type_node));
6209 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6211 if (parm == 0 || type == 0
6212 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6214 if (current_function_prototype_built_in)
6215 warning (0, "number of arguments doesn%'t match "
6216 "built-in prototype");
6217 else
6219 error ("number of arguments doesn%'t match prototype");
6220 error ("%Hprototype declaration",
6221 &current_function_prototype_locus);
6223 break;
6225 /* Type for passing arg must be consistent with that
6226 declared for the arg. ISO C says we take the unqualified
6227 type for parameters declared with qualified type. */
6228 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6229 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6231 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6232 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6234 /* Adjust argument to match prototype. E.g. a previous
6235 `int foo(float);' prototype causes
6236 `int foo(x) float x; {...}' to be treated like
6237 `int foo(float x) {...}'. This is particularly
6238 useful for argument types like uid_t. */
6239 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6241 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6242 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6243 && TYPE_PRECISION (TREE_TYPE (parm))
6244 < TYPE_PRECISION (integer_type_node))
6245 DECL_ARG_TYPE (parm) = integer_type_node;
6247 if (pedantic)
6249 /* ??? Is it possible to get here with a
6250 built-in prototype or will it always have
6251 been diagnosed as conflicting with an
6252 old-style definition and discarded? */
6253 if (current_function_prototype_built_in)
6254 warning (0, "promoted argument %qD "
6255 "doesn%'t match built-in prototype", parm);
6256 else
6258 pedwarn ("promoted argument %qD "
6259 "doesn%'t match prototype", parm);
6260 pedwarn ("%Hprototype declaration",
6261 &current_function_prototype_locus);
6265 else
6267 if (current_function_prototype_built_in)
6268 warning (0, "argument %qD doesn%'t match "
6269 "built-in prototype", parm);
6270 else
6272 error ("argument %qD doesn%'t match prototype", parm);
6273 error ("%Hprototype declaration",
6274 &current_function_prototype_locus);
6279 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6282 /* Otherwise, create a prototype that would match. */
6284 else
6286 tree actual = 0, last = 0, type;
6288 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6290 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6291 if (last)
6292 TREE_CHAIN (last) = type;
6293 else
6294 actual = type;
6295 last = type;
6297 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6298 if (last)
6299 TREE_CHAIN (last) = type;
6300 else
6301 actual = type;
6303 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6304 of the type of this function, but we need to avoid having this
6305 affect the types of other similarly-typed functions, so we must
6306 first force the generation of an identical (but separate) type
6307 node for the relevant function type. The new node we create
6308 will be a variant of the main variant of the original function
6309 type. */
6311 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6313 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6317 /* Store parameter declarations passed in ARG_INFO into the current
6318 function declaration. */
6320 void
6321 store_parm_decls_from (struct c_arg_info *arg_info)
6323 current_function_arg_info = arg_info;
6324 store_parm_decls ();
6327 /* Store the parameter declarations into the current function declaration.
6328 This is called after parsing the parameter declarations, before
6329 digesting the body of the function.
6331 For an old-style definition, construct a prototype out of the old-style
6332 parameter declarations and inject it into the function's type. */
6334 void
6335 store_parm_decls (void)
6337 tree fndecl = current_function_decl;
6338 bool proto;
6340 /* The argument information block for FNDECL. */
6341 struct c_arg_info *arg_info = current_function_arg_info;
6342 current_function_arg_info = 0;
6344 /* True if this definition is written with a prototype. Note:
6345 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6346 list in a function definition as equivalent to (void) -- an
6347 empty argument list specifies the function has no parameters,
6348 but only (void) sets up a prototype for future calls. */
6349 proto = arg_info->types != 0;
6351 if (proto)
6352 store_parm_decls_newstyle (fndecl, arg_info);
6353 else
6354 store_parm_decls_oldstyle (fndecl, arg_info);
6356 /* The next call to push_scope will be a function body. */
6358 next_is_function_body = true;
6360 /* Write a record describing this function definition to the prototypes
6361 file (if requested). */
6363 gen_aux_info_record (fndecl, 1, 0, proto);
6365 /* Initialize the RTL code for the function. */
6366 allocate_struct_function (fndecl);
6368 /* Begin the statement tree for this function. */
6369 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6371 /* ??? Insert the contents of the pending sizes list into the function
6372 to be evaluated. The only reason left to have this is
6373 void foo(int n, int array[n++])
6374 because we throw away the array type in favor of a pointer type, and
6375 thus won't naturally see the SAVE_EXPR containing the increment. All
6376 other pending sizes would be handled by gimplify_parameters. */
6378 tree t;
6379 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6380 add_stmt (TREE_VALUE (t));
6383 /* Even though we're inside a function body, we still don't want to
6384 call expand_expr to calculate the size of a variable-sized array.
6385 We haven't necessarily assigned RTL to all variables yet, so it's
6386 not safe to try to expand expressions involving them. */
6387 cfun->x_dont_save_pending_sizes_p = 1;
6390 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6391 functions. */
6393 static void
6394 c_warn_unused_result_recursively (tree fndecl)
6396 struct cgraph_node *cgn;
6398 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6399 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6401 /* Finalize all nested functions now. */
6402 cgn = cgraph_node (fndecl);
6403 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6404 c_warn_unused_result_recursively (cgn->decl);
6407 /* Finish up a function declaration and compile that function
6408 all the way to assembler language output. The free the storage
6409 for the function definition.
6411 This is called after parsing the body of the function definition. */
6413 void
6414 finish_function (void)
6416 tree fndecl = current_function_decl;
6418 label_context_stack_se = label_context_stack_se->next;
6419 label_context_stack_vm = label_context_stack_vm->next;
6421 if (TREE_CODE (fndecl) == FUNCTION_DECL
6422 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6424 tree args = DECL_ARGUMENTS (fndecl);
6425 for (; args; args = TREE_CHAIN (args))
6427 tree type = TREE_TYPE (args);
6428 if (INTEGRAL_TYPE_P (type)
6429 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6430 DECL_ARG_TYPE (args) = integer_type_node;
6434 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6435 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6437 /* Must mark the RESULT_DECL as being in this function. */
6439 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6440 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6442 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6444 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6445 != integer_type_node)
6447 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6448 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6449 if (!warn_main)
6450 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6452 else
6454 if (flag_isoc99)
6456 tree stmt = c_finish_return (integer_zero_node);
6457 #ifdef USE_MAPPED_LOCATION
6458 /* Hack. We don't want the middle-end to warn that this return
6459 is unreachable, so we mark its location as special. Using
6460 UNKNOWN_LOCATION has the problem that it gets clobbered in
6461 annotate_one_with_locus. A cleaner solution might be to
6462 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6464 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6465 #else
6466 /* Hack. We don't want the middle-end to warn that this
6467 return is unreachable, so put the statement on the
6468 special line 0. */
6469 annotate_with_file_line (stmt, input_filename, 0);
6470 #endif
6475 /* Tie off the statement tree for this function. */
6476 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6478 finish_fname_decls ();
6480 /* Complain if there's just no return statement. */
6481 if (warn_return_type
6482 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6483 && !current_function_returns_value && !current_function_returns_null
6484 /* Don't complain if we are no-return. */
6485 && !current_function_returns_abnormally
6486 /* Don't warn for main(). */
6487 && !MAIN_NAME_P (DECL_NAME (fndecl))
6488 /* Or if they didn't actually specify a return type. */
6489 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6490 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6491 inline function, as we might never be compiled separately. */
6492 && DECL_INLINE (fndecl))
6494 warning (0, "no return statement in function returning non-void");
6495 TREE_NO_WARNING (fndecl) = 1;
6498 /* With just -Wextra, complain only if function returns both with
6499 and without a value. */
6500 if (extra_warnings
6501 && current_function_returns_value
6502 && current_function_returns_null)
6503 warning (0, "this function may return with or without a value");
6505 /* Store the end of the function, so that we get good line number
6506 info for the epilogue. */
6507 cfun->function_end_locus = input_location;
6509 /* If we don't have ctors/dtors sections, and this is a static
6510 constructor or destructor, it must be recorded now. */
6511 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6512 && !targetm.have_ctors_dtors)
6513 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6514 if (DECL_STATIC_DESTRUCTOR (fndecl)
6515 && !targetm.have_ctors_dtors)
6516 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6518 /* Finalize the ELF visibility for the function. */
6519 c_determine_visibility (fndecl);
6521 /* Genericize before inlining. Delay genericizing nested functions
6522 until their parent function is genericized. Since finalizing
6523 requires GENERIC, delay that as well. */
6525 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6526 && !undef_nested_function)
6528 if (!decl_function_context (fndecl))
6530 c_genericize (fndecl);
6531 c_warn_unused_result_recursively (fndecl);
6533 /* ??? Objc emits functions after finalizing the compilation unit.
6534 This should be cleaned up later and this conditional removed. */
6535 if (cgraph_global_info_ready)
6537 c_expand_body (fndecl);
6538 return;
6541 cgraph_finalize_function (fndecl, false);
6543 else
6545 /* Register this function with cgraph just far enough to get it
6546 added to our parent's nested function list. Handy, since the
6547 C front end doesn't have such a list. */
6548 (void) cgraph_node (fndecl);
6552 if (!decl_function_context (fndecl))
6553 undef_nested_function = false;
6555 /* We're leaving the context of this function, so zap cfun.
6556 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6557 tree_rest_of_compilation. */
6558 cfun = NULL;
6559 current_function_decl = NULL;
6562 /* Generate the RTL for the body of FNDECL. */
6564 void
6565 c_expand_body (tree fndecl)
6568 if (!DECL_INITIAL (fndecl)
6569 || DECL_INITIAL (fndecl) == error_mark_node)
6570 return;
6572 tree_rest_of_compilation (fndecl);
6574 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6575 && targetm.have_ctors_dtors)
6576 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6577 DEFAULT_INIT_PRIORITY);
6578 if (DECL_STATIC_DESTRUCTOR (fndecl)
6579 && targetm.have_ctors_dtors)
6580 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6581 DEFAULT_INIT_PRIORITY);
6584 /* Check the declarations given in a for-loop for satisfying the C99
6585 constraints. */
6586 void
6587 check_for_loop_decls (void)
6589 struct c_binding *b;
6591 if (!flag_isoc99)
6593 /* If we get here, declarations have been used in a for loop without
6594 the C99 for loop scope. This doesn't make much sense, so don't
6595 allow it. */
6596 error ("%<for%> loop initial declaration used outside C99 mode");
6597 return;
6599 /* C99 subclause 6.8.5 paragraph 3:
6601 [#3] The declaration part of a for statement shall only
6602 declare identifiers for objects having storage class auto or
6603 register.
6605 It isn't clear whether, in this sentence, "identifiers" binds to
6606 "shall only declare" or to "objects" - that is, whether all identifiers
6607 declared must be identifiers for objects, or whether the restriction
6608 only applies to those that are. (A question on this in comp.std.c
6609 in November 2000 received no answer.) We implement the strictest
6610 interpretation, to avoid creating an extension which later causes
6611 problems. */
6613 for (b = current_scope->bindings; b; b = b->prev)
6615 tree id = b->id;
6616 tree decl = b->decl;
6618 if (!id)
6619 continue;
6621 switch (TREE_CODE (decl))
6623 case VAR_DECL:
6624 if (TREE_STATIC (decl))
6625 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6626 "initial declaration", decl, decl);
6627 else if (DECL_EXTERNAL (decl))
6628 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6629 "initial declaration", decl, decl);
6630 break;
6632 case RECORD_TYPE:
6633 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6634 id);
6635 break;
6636 case UNION_TYPE:
6637 error ("%<union %E%> declared in %<for%> loop initial declaration",
6638 id);
6639 break;
6640 case ENUMERAL_TYPE:
6641 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6642 id);
6643 break;
6644 default:
6645 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6646 "initial declaration", decl, decl);
6651 /* Save and reinitialize the variables
6652 used during compilation of a C function. */
6654 void
6655 c_push_function_context (struct function *f)
6657 struct language_function *p;
6658 p = GGC_NEW (struct language_function);
6659 f->language = p;
6661 p->base.x_stmt_tree = c_stmt_tree;
6662 p->x_break_label = c_break_label;
6663 p->x_cont_label = c_cont_label;
6664 p->x_switch_stack = c_switch_stack;
6665 p->arg_info = current_function_arg_info;
6666 p->returns_value = current_function_returns_value;
6667 p->returns_null = current_function_returns_null;
6668 p->returns_abnormally = current_function_returns_abnormally;
6669 p->warn_about_return_type = warn_about_return_type;
6670 p->extern_inline = current_extern_inline;
6673 /* Restore the variables used during compilation of a C function. */
6675 void
6676 c_pop_function_context (struct function *f)
6678 struct language_function *p = f->language;
6680 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6681 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6683 /* Stop pointing to the local nodes about to be freed. */
6684 /* But DECL_INITIAL must remain nonzero so we know this
6685 was an actual function definition. */
6686 DECL_INITIAL (current_function_decl) = error_mark_node;
6687 DECL_ARGUMENTS (current_function_decl) = 0;
6690 c_stmt_tree = p->base.x_stmt_tree;
6691 c_break_label = p->x_break_label;
6692 c_cont_label = p->x_cont_label;
6693 c_switch_stack = p->x_switch_stack;
6694 current_function_arg_info = p->arg_info;
6695 current_function_returns_value = p->returns_value;
6696 current_function_returns_null = p->returns_null;
6697 current_function_returns_abnormally = p->returns_abnormally;
6698 warn_about_return_type = p->warn_about_return_type;
6699 current_extern_inline = p->extern_inline;
6701 f->language = NULL;
6704 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6706 void
6707 c_dup_lang_specific_decl (tree decl)
6709 struct lang_decl *ld;
6711 if (!DECL_LANG_SPECIFIC (decl))
6712 return;
6714 ld = GGC_NEW (struct lang_decl);
6715 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6716 DECL_LANG_SPECIFIC (decl) = ld;
6719 /* The functions below are required for functionality of doing
6720 function at once processing in the C front end. Currently these
6721 functions are not called from anywhere in the C front end, but as
6722 these changes continue, that will change. */
6724 /* Returns nonzero if the current statement is a full expression,
6725 i.e. temporaries created during that statement should be destroyed
6726 at the end of the statement. */
6729 stmts_are_full_exprs_p (void)
6731 return 0;
6734 /* Returns the stmt_tree (if any) to which statements are currently
6735 being added. If there is no active statement-tree, NULL is
6736 returned. */
6738 stmt_tree
6739 current_stmt_tree (void)
6741 return &c_stmt_tree;
6744 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6745 C. */
6748 anon_aggr_type_p (tree ARG_UNUSED (node))
6750 return 0;
6753 /* Return the global value of T as a symbol. */
6755 tree
6756 identifier_global_value (tree t)
6758 struct c_binding *b;
6760 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6761 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6762 return b->decl;
6764 return 0;
6767 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6768 otherwise the name is found in ridpointers from RID_INDEX. */
6770 void
6771 record_builtin_type (enum rid rid_index, const char *name, tree type)
6773 tree id, decl;
6774 if (name == 0)
6775 id = ridpointers[(int) rid_index];
6776 else
6777 id = get_identifier (name);
6778 decl = build_decl (TYPE_DECL, id, type);
6779 pushdecl (decl);
6780 if (debug_hooks->type_decl)
6781 debug_hooks->type_decl (decl, false);
6784 /* Build the void_list_node (void_type_node having been created). */
6785 tree
6786 build_void_list_node (void)
6788 tree t = build_tree_list (NULL_TREE, void_type_node);
6789 return t;
6792 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6794 struct c_parm *
6795 build_c_parm (struct c_declspecs *specs, tree attrs,
6796 struct c_declarator *declarator)
6798 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6799 ret->specs = specs;
6800 ret->attrs = attrs;
6801 ret->declarator = declarator;
6802 return ret;
6805 /* Return a declarator with nested attributes. TARGET is the inner
6806 declarator to which these attributes apply. ATTRS are the
6807 attributes. */
6809 struct c_declarator *
6810 build_attrs_declarator (tree attrs, struct c_declarator *target)
6812 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6813 ret->kind = cdk_attrs;
6814 ret->declarator = target;
6815 ret->u.attrs = attrs;
6816 return ret;
6819 /* Return a declarator for a function with arguments specified by ARGS
6820 and return type specified by TARGET. */
6822 struct c_declarator *
6823 build_function_declarator (struct c_arg_info *args,
6824 struct c_declarator *target)
6826 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6827 ret->kind = cdk_function;
6828 ret->declarator = target;
6829 ret->u.arg_info = args;
6830 return ret;
6833 /* Return a declarator for the identifier IDENT (which may be
6834 NULL_TREE for an abstract declarator). */
6836 struct c_declarator *
6837 build_id_declarator (tree ident)
6839 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6840 ret->kind = cdk_id;
6841 ret->declarator = 0;
6842 ret->u.id = ident;
6843 /* Default value - may get reset to a more precise location. */
6844 ret->id_loc = input_location;
6845 return ret;
6848 /* Return something to represent absolute declarators containing a *.
6849 TARGET is the absolute declarator that the * contains.
6850 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6851 to apply to the pointer type. */
6853 struct c_declarator *
6854 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6855 struct c_declarator *target)
6857 tree attrs;
6858 int quals = 0;
6859 struct c_declarator *itarget = target;
6860 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6861 if (type_quals_attrs)
6863 attrs = type_quals_attrs->attrs;
6864 quals = quals_from_declspecs (type_quals_attrs);
6865 if (attrs != NULL_TREE)
6866 itarget = build_attrs_declarator (attrs, target);
6868 ret->kind = cdk_pointer;
6869 ret->declarator = itarget;
6870 ret->u.pointer_quals = quals;
6871 return ret;
6874 /* Return a pointer to a structure for an empty list of declaration
6875 specifiers. */
6877 struct c_declspecs *
6878 build_null_declspecs (void)
6880 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6881 ret->type = 0;
6882 ret->decl_attr = 0;
6883 ret->attrs = 0;
6884 ret->typespec_word = cts_none;
6885 ret->storage_class = csc_none;
6886 ret->declspecs_seen_p = false;
6887 ret->type_seen_p = false;
6888 ret->non_sc_seen_p = false;
6889 ret->typedef_p = false;
6890 ret->tag_defined_p = false;
6891 ret->explicit_signed_p = false;
6892 ret->deprecated_p = false;
6893 ret->default_int_p = false;
6894 ret->long_p = false;
6895 ret->long_long_p = false;
6896 ret->short_p = false;
6897 ret->signed_p = false;
6898 ret->unsigned_p = false;
6899 ret->complex_p = false;
6900 ret->inline_p = false;
6901 ret->thread_p = false;
6902 ret->const_p = false;
6903 ret->volatile_p = false;
6904 ret->restrict_p = false;
6905 return ret;
6908 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6909 returning SPECS. */
6911 struct c_declspecs *
6912 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6914 enum rid i;
6915 bool dupe = false;
6916 specs->non_sc_seen_p = true;
6917 specs->declspecs_seen_p = true;
6918 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6919 && C_IS_RESERVED_WORD (qual));
6920 i = C_RID_CODE (qual);
6921 switch (i)
6923 case RID_CONST:
6924 dupe = specs->const_p;
6925 specs->const_p = true;
6926 break;
6927 case RID_VOLATILE:
6928 dupe = specs->volatile_p;
6929 specs->volatile_p = true;
6930 break;
6931 case RID_RESTRICT:
6932 dupe = specs->restrict_p;
6933 specs->restrict_p = true;
6934 break;
6935 default:
6936 gcc_unreachable ();
6938 if (dupe && pedantic && !flag_isoc99)
6939 pedwarn ("duplicate %qE", qual);
6940 return specs;
6943 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6944 returning SPECS. */
6946 struct c_declspecs *
6947 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6949 tree type = spec.spec;
6950 specs->non_sc_seen_p = true;
6951 specs->declspecs_seen_p = true;
6952 specs->type_seen_p = true;
6953 if (TREE_DEPRECATED (type))
6954 specs->deprecated_p = true;
6956 /* Handle type specifier keywords. */
6957 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6959 enum rid i = C_RID_CODE (type);
6960 if (specs->type)
6962 error ("two or more data types in declaration specifiers");
6963 return specs;
6965 if ((int) i <= (int) RID_LAST_MODIFIER)
6967 /* "long", "short", "signed", "unsigned" or "_Complex". */
6968 bool dupe = false;
6969 switch (i)
6971 case RID_LONG:
6972 if (specs->long_long_p)
6974 error ("%<long long long%> is too long for GCC");
6975 break;
6977 if (specs->long_p)
6979 if (specs->typespec_word == cts_double)
6981 error ("both %<long long%> and %<double%> in "
6982 "declaration specifiers");
6983 break;
6985 if (pedantic && !flag_isoc99 && !in_system_header
6986 && warn_long_long)
6987 pedwarn ("ISO C90 does not support %<long long%>");
6988 specs->long_long_p = 1;
6989 break;
6991 if (specs->short_p)
6992 error ("both %<long%> and %<short%> in "
6993 "declaration specifiers");
6994 else if (specs->typespec_word == cts_void)
6995 error ("both %<long%> and %<void%> in "
6996 "declaration specifiers");
6997 else if (specs->typespec_word == cts_bool)
6998 error ("both %<long%> and %<_Bool%> in "
6999 "declaration specifiers");
7000 else if (specs->typespec_word == cts_char)
7001 error ("both %<long%> and %<char%> in "
7002 "declaration specifiers");
7003 else if (specs->typespec_word == cts_float)
7004 error ("both %<long%> and %<float%> in "
7005 "declaration specifiers");
7006 else
7007 specs->long_p = true;
7008 break;
7009 case RID_SHORT:
7010 dupe = specs->short_p;
7011 if (specs->long_p)
7012 error ("both %<long%> and %<short%> in "
7013 "declaration specifiers");
7014 else if (specs->typespec_word == cts_void)
7015 error ("both %<short%> and %<void%> in "
7016 "declaration specifiers");
7017 else if (specs->typespec_word == cts_bool)
7018 error ("both %<short%> and %<_Bool%> in "
7019 "declaration specifiers");
7020 else if (specs->typespec_word == cts_char)
7021 error ("both %<short%> and %<char%> in "
7022 "declaration specifiers");
7023 else if (specs->typespec_word == cts_float)
7024 error ("both %<short%> and %<float%> in "
7025 "declaration specifiers");
7026 else if (specs->typespec_word == cts_double)
7027 error ("both %<short%> and %<double%> in "
7028 "declaration specifiers");
7029 else
7030 specs->short_p = true;
7031 break;
7032 case RID_SIGNED:
7033 dupe = specs->signed_p;
7034 if (specs->unsigned_p)
7035 error ("both %<signed%> and %<unsigned%> in "
7036 "declaration specifiers");
7037 else if (specs->typespec_word == cts_void)
7038 error ("both %<signed%> and %<void%> in "
7039 "declaration specifiers");
7040 else if (specs->typespec_word == cts_bool)
7041 error ("both %<signed%> and %<_Bool%> in "
7042 "declaration specifiers");
7043 else if (specs->typespec_word == cts_float)
7044 error ("both %<signed%> and %<float%> in "
7045 "declaration specifiers");
7046 else if (specs->typespec_word == cts_double)
7047 error ("both %<signed%> and %<double%> in "
7048 "declaration specifiers");
7049 else
7050 specs->signed_p = true;
7051 break;
7052 case RID_UNSIGNED:
7053 dupe = specs->unsigned_p;
7054 if (specs->signed_p)
7055 error ("both %<signed%> and %<unsigned%> in "
7056 "declaration specifiers");
7057 else if (specs->typespec_word == cts_void)
7058 error ("both %<unsigned%> and %<void%> in "
7059 "declaration specifiers");
7060 else if (specs->typespec_word == cts_bool)
7061 error ("both %<unsigned%> and %<_Bool%> in "
7062 "declaration specifiers");
7063 else if (specs->typespec_word == cts_float)
7064 error ("both %<unsigned%> and %<float%> in "
7065 "declaration specifiers");
7066 else if (specs->typespec_word == cts_double)
7067 error ("both %<unsigned%> and %<double%> in "
7068 "declaration specifiers");
7069 else
7070 specs->unsigned_p = true;
7071 break;
7072 case RID_COMPLEX:
7073 dupe = specs->complex_p;
7074 if (pedantic && !flag_isoc99 && !in_system_header)
7075 pedwarn ("ISO C90 does not support complex types");
7076 if (specs->typespec_word == cts_void)
7077 error ("both %<complex%> and %<void%> in "
7078 "declaration specifiers");
7079 else if (specs->typespec_word == cts_bool)
7080 error ("both %<complex%> and %<_Bool%> in "
7081 "declaration specifiers");
7082 else
7083 specs->complex_p = true;
7084 break;
7085 default:
7086 gcc_unreachable ();
7089 if (dupe)
7090 error ("duplicate %qE", type);
7092 return specs;
7094 else
7096 /* "void", "_Bool", "char", "int", "float" or "double". */
7097 if (specs->typespec_word != cts_none)
7099 error ("two or more data types in declaration specifiers");
7100 return specs;
7102 switch (i)
7104 case RID_VOID:
7105 if (specs->long_p)
7106 error ("both %<long%> and %<void%> in "
7107 "declaration specifiers");
7108 else if (specs->short_p)
7109 error ("both %<short%> and %<void%> in "
7110 "declaration specifiers");
7111 else if (specs->signed_p)
7112 error ("both %<signed%> and %<void%> in "
7113 "declaration specifiers");
7114 else if (specs->unsigned_p)
7115 error ("both %<unsigned%> and %<void%> in "
7116 "declaration specifiers");
7117 else if (specs->complex_p)
7118 error ("both %<complex%> and %<void%> in "
7119 "declaration specifiers");
7120 else
7121 specs->typespec_word = cts_void;
7122 return specs;
7123 case RID_BOOL:
7124 if (specs->long_p)
7125 error ("both %<long%> and %<_Bool%> in "
7126 "declaration specifiers");
7127 else if (specs->short_p)
7128 error ("both %<short%> and %<_Bool%> in "
7129 "declaration specifiers");
7130 else if (specs->signed_p)
7131 error ("both %<signed%> and %<_Bool%> in "
7132 "declaration specifiers");
7133 else if (specs->unsigned_p)
7134 error ("both %<unsigned%> and %<_Bool%> in "
7135 "declaration specifiers");
7136 else if (specs->complex_p)
7137 error ("both %<complex%> and %<_Bool%> in "
7138 "declaration specifiers");
7139 else
7140 specs->typespec_word = cts_bool;
7141 return specs;
7142 case RID_CHAR:
7143 if (specs->long_p)
7144 error ("both %<long%> and %<char%> in "
7145 "declaration specifiers");
7146 else if (specs->short_p)
7147 error ("both %<short%> and %<char%> in "
7148 "declaration specifiers");
7149 else
7150 specs->typespec_word = cts_char;
7151 return specs;
7152 case RID_INT:
7153 specs->typespec_word = cts_int;
7154 return specs;
7155 case RID_FLOAT:
7156 if (specs->long_p)
7157 error ("both %<long%> and %<float%> in "
7158 "declaration specifiers");
7159 else if (specs->short_p)
7160 error ("both %<short%> and %<float%> in "
7161 "declaration specifiers");
7162 else if (specs->signed_p)
7163 error ("both %<signed%> and %<float%> in "
7164 "declaration specifiers");
7165 else if (specs->unsigned_p)
7166 error ("both %<unsigned%> and %<float%> in "
7167 "declaration specifiers");
7168 else
7169 specs->typespec_word = cts_float;
7170 return specs;
7171 case RID_DOUBLE:
7172 if (specs->long_long_p)
7173 error ("both %<long long%> and %<double%> in "
7174 "declaration specifiers");
7175 else if (specs->short_p)
7176 error ("both %<short%> and %<double%> in "
7177 "declaration specifiers");
7178 else if (specs->signed_p)
7179 error ("both %<signed%> and %<double%> in "
7180 "declaration specifiers");
7181 else if (specs->unsigned_p)
7182 error ("both %<unsigned%> and %<double%> in "
7183 "declaration specifiers");
7184 else
7185 specs->typespec_word = cts_double;
7186 return specs;
7187 default:
7188 /* ObjC reserved word "id", handled below. */
7189 break;
7194 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7195 form of ObjC type, cases such as "int" and "long" being handled
7196 above), a TYPE (struct, union, enum and typeof specifiers) or an
7197 ERROR_MARK. In none of these cases may there have previously
7198 been any type specifiers. */
7199 if (specs->type || specs->typespec_word != cts_none
7200 || specs->long_p || specs->short_p || specs->signed_p
7201 || specs->unsigned_p || specs->complex_p)
7202 error ("two or more data types in declaration specifiers");
7203 else if (TREE_CODE (type) == TYPE_DECL)
7205 if (TREE_TYPE (type) == error_mark_node)
7206 ; /* Allow the type to default to int to avoid cascading errors. */
7207 else
7209 specs->type = TREE_TYPE (type);
7210 specs->decl_attr = DECL_ATTRIBUTES (type);
7211 specs->typedef_p = true;
7212 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7215 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7217 tree t = lookup_name (type);
7218 if (!t || TREE_CODE (t) != TYPE_DECL)
7219 error ("%qE fails to be a typedef or built in type", type);
7220 else if (TREE_TYPE (t) == error_mark_node)
7222 else
7223 specs->type = TREE_TYPE (t);
7225 else if (TREE_CODE (type) != ERROR_MARK)
7227 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7228 specs->tag_defined_p = true;
7229 if (spec.kind == ctsk_typeof)
7230 specs->typedef_p = true;
7231 specs->type = type;
7234 return specs;
7237 /* Add the storage class specifier or function specifier SCSPEC to the
7238 declaration specifiers SPECS, returning SPECS. */
7240 struct c_declspecs *
7241 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7243 enum rid i;
7244 enum c_storage_class n = csc_none;
7245 bool dupe = false;
7246 specs->declspecs_seen_p = true;
7247 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7248 && C_IS_RESERVED_WORD (scspec));
7249 i = C_RID_CODE (scspec);
7250 if (extra_warnings && specs->non_sc_seen_p)
7251 warning (0, "%qE is not at beginning of declaration", scspec);
7252 switch (i)
7254 case RID_INLINE:
7255 /* C99 permits duplicate inline. Although of doubtful utility,
7256 it seems simplest to permit it in gnu89 mode as well, as
7257 there is also little utility in maintaining this as a
7258 difference between gnu89 and C99 inline. */
7259 dupe = false;
7260 specs->inline_p = true;
7261 break;
7262 case RID_THREAD:
7263 dupe = specs->thread_p;
7264 if (specs->storage_class == csc_auto)
7265 error ("%<__thread%> used with %<auto%>");
7266 else if (specs->storage_class == csc_register)
7267 error ("%<__thread%> used with %<register%>");
7268 else if (specs->storage_class == csc_typedef)
7269 error ("%<__thread%> used with %<typedef%>");
7270 else
7271 specs->thread_p = true;
7272 break;
7273 case RID_AUTO:
7274 n = csc_auto;
7275 break;
7276 case RID_EXTERN:
7277 n = csc_extern;
7278 /* Diagnose "__thread extern". */
7279 if (specs->thread_p)
7280 error ("%<__thread%> before %<extern%>");
7281 break;
7282 case RID_REGISTER:
7283 n = csc_register;
7284 break;
7285 case RID_STATIC:
7286 n = csc_static;
7287 /* Diagnose "__thread static". */
7288 if (specs->thread_p)
7289 error ("%<__thread%> before %<static%>");
7290 break;
7291 case RID_TYPEDEF:
7292 n = csc_typedef;
7293 break;
7294 default:
7295 gcc_unreachable ();
7297 if (n != csc_none && n == specs->storage_class)
7298 dupe = true;
7299 if (dupe)
7300 error ("duplicate %qE", scspec);
7301 if (n != csc_none)
7303 if (specs->storage_class != csc_none && n != specs->storage_class)
7305 error ("multiple storage classes in declaration specifiers");
7307 else
7309 specs->storage_class = n;
7310 if (n != csc_extern && n != csc_static && specs->thread_p)
7312 error ("%<__thread%> used with %qE", scspec);
7313 specs->thread_p = false;
7317 return specs;
7320 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7321 returning SPECS. */
7323 struct c_declspecs *
7324 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7326 specs->attrs = chainon (attrs, specs->attrs);
7327 specs->declspecs_seen_p = true;
7328 return specs;
7331 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7332 specifiers with any other type specifier to determine the resulting
7333 type. This is where ISO C checks on complex types are made, since
7334 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7335 double". */
7337 struct c_declspecs *
7338 finish_declspecs (struct c_declspecs *specs)
7340 /* If a type was specified as a whole, we have no modifiers and are
7341 done. */
7342 if (specs->type != NULL_TREE)
7344 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7345 && !specs->signed_p && !specs->unsigned_p
7346 && !specs->complex_p);
7347 return specs;
7350 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7351 has been specified, treat it as "int" unless "_Complex" is
7352 present and there are no other specifiers. If we just have
7353 "_Complex", it is equivalent to "_Complex double", but e.g.
7354 "_Complex short" is equivalent to "_Complex short int". */
7355 if (specs->typespec_word == cts_none)
7357 if (specs->long_p || specs->short_p
7358 || specs->signed_p || specs->unsigned_p)
7360 specs->typespec_word = cts_int;
7362 else if (specs->complex_p)
7364 specs->typespec_word = cts_double;
7365 if (pedantic)
7366 pedwarn ("ISO C does not support plain %<complex%> meaning "
7367 "%<double complex%>");
7369 else
7371 specs->typespec_word = cts_int;
7372 specs->default_int_p = true;
7373 /* We don't diagnose this here because grokdeclarator will
7374 give more specific diagnostics according to whether it is
7375 a function definition. */
7379 /* If "signed" was specified, record this to distinguish "int" and
7380 "signed int" in the case of a bit-field with
7381 -funsigned-bitfields. */
7382 specs->explicit_signed_p = specs->signed_p;
7384 /* Now compute the actual type. */
7385 switch (specs->typespec_word)
7387 case cts_void:
7388 gcc_assert (!specs->long_p && !specs->short_p
7389 && !specs->signed_p && !specs->unsigned_p
7390 && !specs->complex_p);
7391 specs->type = void_type_node;
7392 break;
7393 case cts_bool:
7394 gcc_assert (!specs->long_p && !specs->short_p
7395 && !specs->signed_p && !specs->unsigned_p
7396 && !specs->complex_p);
7397 specs->type = boolean_type_node;
7398 break;
7399 case cts_char:
7400 gcc_assert (!specs->long_p && !specs->short_p);
7401 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7402 if (specs->signed_p)
7403 specs->type = signed_char_type_node;
7404 else if (specs->unsigned_p)
7405 specs->type = unsigned_char_type_node;
7406 else
7407 specs->type = char_type_node;
7408 if (specs->complex_p)
7410 if (pedantic)
7411 pedwarn ("ISO C does not support complex integer types");
7412 specs->type = build_complex_type (specs->type);
7414 break;
7415 case cts_int:
7416 gcc_assert (!(specs->long_p && specs->short_p));
7417 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7418 if (specs->long_long_p)
7419 specs->type = (specs->unsigned_p
7420 ? long_long_unsigned_type_node
7421 : long_long_integer_type_node);
7422 else if (specs->long_p)
7423 specs->type = (specs->unsigned_p
7424 ? long_unsigned_type_node
7425 : long_integer_type_node);
7426 else if (specs->short_p)
7427 specs->type = (specs->unsigned_p
7428 ? short_unsigned_type_node
7429 : short_integer_type_node);
7430 else
7431 specs->type = (specs->unsigned_p
7432 ? unsigned_type_node
7433 : integer_type_node);
7434 if (specs->complex_p)
7436 if (pedantic)
7437 pedwarn ("ISO C does not support complex integer types");
7438 specs->type = build_complex_type (specs->type);
7440 break;
7441 case cts_float:
7442 gcc_assert (!specs->long_p && !specs->short_p
7443 && !specs->signed_p && !specs->unsigned_p);
7444 specs->type = (specs->complex_p
7445 ? complex_float_type_node
7446 : float_type_node);
7447 break;
7448 case cts_double:
7449 gcc_assert (!specs->long_long_p && !specs->short_p
7450 && !specs->signed_p && !specs->unsigned_p);
7451 if (specs->long_p)
7453 specs->type = (specs->complex_p
7454 ? complex_long_double_type_node
7455 : long_double_type_node);
7457 else
7459 specs->type = (specs->complex_p
7460 ? complex_double_type_node
7461 : double_type_node);
7463 break;
7464 default:
7465 gcc_unreachable ();
7468 return specs;
7471 /* Synthesize a function which calls all the global ctors or global
7472 dtors in this file. This is only used for targets which do not
7473 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7474 static void
7475 build_cdtor (int method_type, tree cdtors)
7477 tree body = 0;
7479 if (!cdtors)
7480 return;
7482 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7483 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7484 &body);
7486 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7489 /* Perform final processing on one file scope's declarations (or the
7490 external scope's declarations), GLOBALS. */
7491 static void
7492 c_write_global_declarations_1 (tree globals)
7494 size_t len = list_length (globals);
7495 tree *vec = XNEWVEC (tree, len);
7496 size_t i;
7497 tree decl;
7499 /* Process the decls in the order they were written. */
7500 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7502 vec[i] = decl;
7503 /* Check for used but undefined static functions using the C
7504 standard's definition of "used", and set TREE_NO_WARNING so
7505 that check_global_declarations doesn't repeat the check. */
7506 if (TREE_CODE (decl) == FUNCTION_DECL
7507 && DECL_INITIAL (decl) == 0
7508 && DECL_EXTERNAL (decl)
7509 && !TREE_PUBLIC (decl)
7510 && C_DECL_USED (decl))
7512 pedwarn ("%J%qF used but never defined", decl, decl);
7513 TREE_NO_WARNING (decl) = 1;
7517 wrapup_global_declarations (vec, len);
7518 check_global_declarations (vec, len);
7520 free (vec);
7523 void
7524 c_write_global_declarations (void)
7526 tree ext_block, t;
7528 /* We don't want to do this if generating a PCH. */
7529 if (pch_file)
7530 return;
7532 /* Don't waste time on further processing if -fsyntax-only or we've
7533 encountered errors. */
7534 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7535 return;
7537 /* Close the external scope. */
7538 ext_block = pop_scope ();
7539 external_scope = 0;
7540 gcc_assert (!current_scope);
7542 /* Process all file scopes in this compilation, and the external_scope,
7543 through wrapup_global_declarations and check_global_declarations. */
7544 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7545 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7546 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7548 /* Generate functions to call static constructors and destructors
7549 for targets that do not support .ctors/.dtors sections. These
7550 functions have magic names which are detected by collect2. */
7551 build_cdtor ('I', static_ctors); static_ctors = 0;
7552 build_cdtor ('D', static_dtors); static_dtors = 0;
7554 /* We're done parsing; proceed to optimize and emit assembly.
7555 FIXME: shouldn't be the front end's responsibility to call this. */
7556 cgraph_optimize ();
7559 #include "gt-c-decl.h"