2005-04-29 Jim Tison <jtison@us.ibm.com>
[official-gcc.git] / gcc / c-decl.c
blob3578862269f0e629218fcaee55cf2e2db3cc0b1c
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* The argument information structure for the function currently being
102 defined. */
104 static struct c_arg_info *current_function_arg_info;
106 /* The obstack on which parser and related data structures, which are
107 not live beyond their top-level declaration or definition, are
108 allocated. */
109 struct obstack parser_obstack;
111 /* The current statement tree. */
113 static GTY(()) struct stmt_tree_s c_stmt_tree;
115 /* State saving variables. */
116 tree c_break_label;
117 tree c_cont_label;
119 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
120 included in this invocation. Note that the current translation
121 unit is not included in this list. */
123 static GTY(()) tree all_translation_units;
125 /* A list of decls to be made automatically visible in each file scope. */
126 static GTY(()) tree visible_builtins;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a return statement that specifies a return value is seen. */
131 int current_function_returns_value;
133 /* Set to 0 at beginning of a function definition, set to 1 if
134 a return statement with no argument is seen. */
136 int current_function_returns_null;
138 /* Set to 0 at beginning of a function definition, set to 1 if
139 a call to a noreturn function is seen. */
141 int current_function_returns_abnormally;
143 /* Set to nonzero by `grokdeclarator' for a function
144 whose return type is defaulted, if warnings for this are desired. */
146 static int warn_about_return_type;
148 /* Nonzero when starting a function declared `extern inline'. */
150 static int current_extern_inline;
152 /* Nonzero when the current toplevel function contains a declaration
153 of a nested function which is never defined. */
155 static bool undef_nested_function;
157 /* True means global_bindings_p should return false even if the scope stack
158 says we are in file scope. */
159 bool c_override_global_bindings_to_false;
162 /* Each c_binding structure describes one binding of an identifier to
163 a decl. All the decls in a scope - irrespective of namespace - are
164 chained together by the ->prev field, which (as the name implies)
165 runs in reverse order. All the decls in a given namespace bound to
166 a given identifier are chained by the ->shadowed field, which runs
167 from inner to outer scopes.
169 The ->decl field usually points to a DECL node, but there are two
170 exceptions. In the namespace of type tags, the bound entity is a
171 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
172 identifier is encountered, it is bound to error_mark_node to
173 suppress further errors about that identifier in the current
174 function.
176 The ->type field stores the type of the declaration in this scope;
177 if NULL, the type is the type of the ->decl field. This is only of
178 relevance for objects with external or internal linkage which may
179 be redeclared in inner scopes, forming composite types that only
180 persist for the duration of those scopes. In the external scope,
181 this stores the composite of all the types declared for this
182 object, visible or not. The ->inner_comp field (used only at file
183 scope) stores whether an incomplete array type at file scope was
184 completed at an inner scope to an array size other than 1.
186 The depth field is copied from the scope structure that holds this
187 decl. It is used to preserve the proper ordering of the ->shadowed
188 field (see bind()) and also for a handful of special-case checks.
189 Finally, the invisible bit is true for a decl which should be
190 ignored for purposes of normal name lookup, and the nested bit is
191 true for a decl that's been bound a second time in an inner scope;
192 in all such cases, the binding in the outer scope will have its
193 invisible bit true. */
195 struct c_binding GTY((chain_next ("%h.prev")))
197 tree decl; /* the decl bound */
198 tree type; /* the type in this scope */
199 tree id; /* the identifier it's bound to */
200 struct c_binding *prev; /* the previous decl in this scope */
201 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
202 unsigned int depth : 28; /* depth of this scope */
203 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
204 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
205 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
206 /* one free bit */
208 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
209 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
210 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
211 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
213 #define I_SYMBOL_BINDING(node) \
214 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
215 #define I_SYMBOL_DECL(node) \
216 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
218 #define I_TAG_BINDING(node) \
219 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
220 #define I_TAG_DECL(node) \
221 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
223 #define I_LABEL_BINDING(node) \
224 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
225 #define I_LABEL_DECL(node) \
226 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
228 /* Each C symbol points to three linked lists of c_binding structures.
229 These describe the values of the identifier in the three different
230 namespaces defined by the language. */
232 struct lang_identifier GTY(())
234 struct c_common_identifier common_id;
235 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
236 struct c_binding *tag_binding; /* struct/union/enum tags */
237 struct c_binding *label_binding; /* labels */
240 /* Validate c-lang.c's assumptions. */
241 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
242 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
244 /* The resulting tree type. */
246 union lang_tree_node
247 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
248 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *) TREE_CHAIN (&%h.generic)")))
250 union tree_node GTY ((tag ("0"),
251 desc ("tree_node_structure (&%h)")))
252 generic;
253 struct lang_identifier GTY ((tag ("1"))) identifier;
256 /* Each c_scope structure describes the complete contents of one
257 scope. Four scopes are distinguished specially: the innermost or
258 current scope, the innermost function scope, the file scope (always
259 the second to outermost) and the outermost or external scope.
261 Most declarations are recorded in the current scope.
263 All normal label declarations are recorded in the innermost
264 function scope, as are bindings of undeclared identifiers to
265 error_mark_node. (GCC permits nested functions as an extension,
266 hence the 'innermost' qualifier.) Explicitly declared labels
267 (using the __label__ extension) appear in the current scope.
269 Being in the file scope (current_scope == file_scope) causes
270 special behavior in several places below. Also, under some
271 conditions the Objective-C front end records declarations in the
272 file scope even though that isn't the current scope.
274 All declarations with external linkage are recorded in the external
275 scope, even if they aren't visible there; this models the fact that
276 such declarations are visible to the entire program, and (with a
277 bit of cleverness, see pushdecl) allows diagnosis of some violations
278 of C99 6.2.2p7 and 6.2.7p2:
280 If, within the same translation unit, the same identifier appears
281 with both internal and external linkage, the behavior is
282 undefined.
284 All declarations that refer to the same object or function shall
285 have compatible type; otherwise, the behavior is undefined.
287 Initially only the built-in declarations, which describe compiler
288 intrinsic functions plus a subset of the standard library, are in
289 this scope.
291 The order of the blocks list matters, and it is frequently appended
292 to. To avoid having to walk all the way to the end of the list on
293 each insertion, or reverse the list later, we maintain a pointer to
294 the last list entry. (FIXME: It should be feasible to use a reversed
295 list here.)
297 The bindings list is strictly in reverse order of declarations;
298 pop_scope relies on this. */
301 struct c_scope GTY((chain_next ("%h.outer")))
303 /* The scope containing this one. */
304 struct c_scope *outer;
306 /* The next outermost function scope. */
307 struct c_scope *outer_function;
309 /* All bindings in this scope. */
310 struct c_binding *bindings;
312 /* For each scope (except the global one), a chain of BLOCK nodes
313 for all the scopes that were entered and exited one level down. */
314 tree blocks;
315 tree blocks_last;
317 /* The depth of this scope. Used to keep the ->shadowed chain of
318 bindings sorted innermost to outermost. */
319 unsigned int depth : 28;
321 /* True if we are currently filling this scope with parameter
322 declarations. */
323 BOOL_BITFIELD parm_flag : 1;
325 /* True if we already complained about forward parameter decls
326 in this scope. This prevents double warnings on
327 foo (int a; int b; ...) */
328 BOOL_BITFIELD warned_forward_parm_decls : 1;
330 /* True if this is the outermost block scope of a function body.
331 This scope contains the parameters, the local variables declared
332 in the outermost block, and all the labels (except those in
333 nested functions, or declared at block scope with __label__). */
334 BOOL_BITFIELD function_body : 1;
336 /* True means make a BLOCK for this scope no matter what. */
337 BOOL_BITFIELD keep : 1;
340 /* The scope currently in effect. */
342 static GTY(()) struct c_scope *current_scope;
344 /* The innermost function scope. Ordinary (not explicitly declared)
345 labels, bindings to error_mark_node, and the lazily-created
346 bindings of __func__ and its friends get this scope. */
348 static GTY(()) struct c_scope *current_function_scope;
350 /* The C file scope. This is reset for each input translation unit. */
352 static GTY(()) struct c_scope *file_scope;
354 /* The outermost scope. This is used for all declarations with
355 external linkage, and only these, hence the name. */
357 static GTY(()) struct c_scope *external_scope;
359 /* A chain of c_scope structures awaiting reuse. */
361 static GTY((deletable)) struct c_scope *scope_freelist;
363 /* A chain of c_binding structures awaiting reuse. */
365 static GTY((deletable)) struct c_binding *binding_freelist;
367 /* Append VAR to LIST in scope SCOPE. */
368 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
369 struct c_scope *s_ = (scope); \
370 tree d_ = (decl); \
371 if (s_->list##_last) \
372 TREE_CHAIN (s_->list##_last) = d_; \
373 else \
374 s_->list = d_; \
375 s_->list##_last = d_; \
376 } while (0)
378 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
379 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
380 struct c_scope *t_ = (tscope); \
381 struct c_scope *f_ = (fscope); \
382 if (t_->to##_last) \
383 TREE_CHAIN (t_->to##_last) = f_->from; \
384 else \
385 t_->to = f_->from; \
386 t_->to##_last = f_->from##_last; \
387 } while (0)
389 /* True means unconditionally make a BLOCK for the next scope pushed. */
391 static bool keep_next_level_flag;
393 /* True means the next call to push_scope will be the outermost scope
394 of a function body, so do not push a new scope, merely cease
395 expecting parameter decls. */
397 static bool next_is_function_body;
399 /* Functions called automatically at the beginning and end of execution. */
401 static GTY(()) tree static_ctors;
402 static GTY(()) tree static_dtors;
404 /* Forward declarations. */
405 static tree lookup_name_in_scope (tree, struct c_scope *);
406 static tree c_make_fname_decl (tree, int);
407 static tree grokdeclarator (const struct c_declarator *,
408 struct c_declspecs *,
409 enum decl_context, bool, tree *);
410 static tree grokparms (struct c_arg_info *, bool);
411 static void layout_array_type (tree);
413 /* States indicating how grokdeclarator() should handle declspecs marked
414 with __attribute__((deprecated)). An object declared as
415 __attribute__((deprecated)) suppresses warnings of uses of other
416 deprecated items. */
418 enum deprecated_states {
419 DEPRECATED_NORMAL,
420 DEPRECATED_SUPPRESS
423 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
425 void
426 c_print_identifier (FILE *file, tree node, int indent)
428 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
429 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
430 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
431 if (C_IS_RESERVED_WORD (node))
433 tree rid = ridpointers[C_RID_CODE (node)];
434 indent_to (file, indent + 4);
435 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
436 (void *) rid, IDENTIFIER_POINTER (rid));
440 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
441 which may be any of several kinds of DECL or TYPE or error_mark_node,
442 in the scope SCOPE. */
443 static void
444 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
446 struct c_binding *b, **here;
448 if (binding_freelist)
450 b = binding_freelist;
451 binding_freelist = b->prev;
453 else
454 b = GGC_NEW (struct c_binding);
456 b->shadowed = 0;
457 b->decl = decl;
458 b->id = name;
459 b->depth = scope->depth;
460 b->invisible = invisible;
461 b->nested = nested;
462 b->inner_comp = 0;
464 b->type = 0;
466 b->prev = scope->bindings;
467 scope->bindings = b;
469 if (!name)
470 return;
472 switch (TREE_CODE (decl))
474 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
475 case ENUMERAL_TYPE:
476 case UNION_TYPE:
477 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
478 case VAR_DECL:
479 case FUNCTION_DECL:
480 case TYPE_DECL:
481 case CONST_DECL:
482 case PARM_DECL:
483 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
485 default:
486 gcc_unreachable ();
489 /* Locate the appropriate place in the chain of shadowed decls
490 to insert this binding. Normally, scope == current_scope and
491 this does nothing. */
492 while (*here && (*here)->depth > scope->depth)
493 here = &(*here)->shadowed;
495 b->shadowed = *here;
496 *here = b;
499 /* Clear the binding structure B, stick it on the binding_freelist,
500 and return the former value of b->prev. This is used by pop_scope
501 and get_parm_info to iterate destructively over all the bindings
502 from a given scope. */
503 static struct c_binding *
504 free_binding_and_advance (struct c_binding *b)
506 struct c_binding *prev = b->prev;
508 memset (b, 0, sizeof (struct c_binding));
509 b->prev = binding_freelist;
510 binding_freelist = b;
512 return prev;
516 /* Hook called at end of compilation to assume 1 elt
517 for a file-scope tentative array defn that wasn't complete before. */
519 void
520 c_finish_incomplete_decl (tree decl)
522 if (TREE_CODE (decl) == VAR_DECL)
524 tree type = TREE_TYPE (decl);
525 if (type != error_mark_node
526 && TREE_CODE (type) == ARRAY_TYPE
527 && !DECL_EXTERNAL (decl)
528 && TYPE_DOMAIN (type) == 0)
530 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
532 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
534 layout_decl (decl, 0);
539 /* The Objective-C front-end often needs to determine the current scope. */
541 void *
542 objc_get_current_scope (void)
544 return current_scope;
547 /* The following function is used only by Objective-C. It needs to live here
548 because it accesses the innards of c_scope. */
550 void
551 objc_mark_locals_volatile (void *enclosing_blk)
553 struct c_scope *scope;
554 struct c_binding *b;
556 for (scope = current_scope;
557 scope && scope != enclosing_blk;
558 scope = scope->outer)
560 for (b = scope->bindings; b; b = b->prev)
562 if (TREE_CODE (b->decl) == VAR_DECL
563 || TREE_CODE (b->decl) == PARM_DECL)
565 C_DECL_REGISTER (b->decl) = 0;
566 DECL_REGISTER (b->decl) = 0;
567 TREE_THIS_VOLATILE (b->decl) = 1;
571 /* Do not climb up past the current function. */
572 if (scope->function_body)
573 break;
577 /* Nonzero if we are currently in file scope. */
580 global_bindings_p (void)
582 return current_scope == file_scope && !c_override_global_bindings_to_false;
585 void
586 keep_next_level (void)
588 keep_next_level_flag = true;
591 /* Identify this scope as currently being filled with parameters. */
593 void
594 declare_parm_level (void)
596 current_scope->parm_flag = true;
599 void
600 push_scope (void)
602 if (next_is_function_body)
604 /* This is the transition from the parameters to the top level
605 of the function body. These are the same scope
606 (C99 6.2.1p4,6) so we do not push another scope structure.
607 next_is_function_body is set only by store_parm_decls, which
608 in turn is called when and only when we are about to
609 encounter the opening curly brace for the function body.
611 The outermost block of a function always gets a BLOCK node,
612 because the debugging output routines expect that each
613 function has at least one BLOCK. */
614 current_scope->parm_flag = false;
615 current_scope->function_body = true;
616 current_scope->keep = true;
617 current_scope->outer_function = current_function_scope;
618 current_function_scope = current_scope;
620 keep_next_level_flag = false;
621 next_is_function_body = false;
623 else
625 struct c_scope *scope;
626 if (scope_freelist)
628 scope = scope_freelist;
629 scope_freelist = scope->outer;
631 else
632 scope = GGC_CNEW (struct c_scope);
634 scope->keep = keep_next_level_flag;
635 scope->outer = current_scope;
636 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
638 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
639 possible. */
640 if (current_scope && scope->depth == 0)
642 scope->depth--;
643 sorry ("GCC supports only %u nested scopes", scope->depth);
646 current_scope = scope;
647 keep_next_level_flag = false;
651 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
653 static void
654 set_type_context (tree type, tree context)
656 for (type = TYPE_MAIN_VARIANT (type); type;
657 type = TYPE_NEXT_VARIANT (type))
658 TYPE_CONTEXT (type) = context;
661 /* Exit a scope. Restore the state of the identifier-decl mappings
662 that were in effect when this scope was entered. Return a BLOCK
663 node containing all the DECLs in this scope that are of interest
664 to debug info generation. */
666 tree
667 pop_scope (void)
669 struct c_scope *scope = current_scope;
670 tree block, context, p;
671 struct c_binding *b;
673 bool functionbody = scope->function_body;
674 bool keep = functionbody || scope->keep || scope->bindings;
676 c_end_vm_scope (scope->depth);
678 /* If appropriate, create a BLOCK to record the decls for the life
679 of this function. */
680 block = 0;
681 if (keep)
683 block = make_node (BLOCK);
684 BLOCK_SUBBLOCKS (block) = scope->blocks;
685 TREE_USED (block) = 1;
687 /* In each subblock, record that this is its superior. */
688 for (p = scope->blocks; p; p = TREE_CHAIN (p))
689 BLOCK_SUPERCONTEXT (p) = block;
691 BLOCK_VARS (block) = 0;
694 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
695 scope must be set so that they point to the appropriate
696 construct, i.e. either to the current FUNCTION_DECL node, or
697 else to the BLOCK node we just constructed.
699 Note that for tagged types whose scope is just the formal
700 parameter list for some function type specification, we can't
701 properly set their TYPE_CONTEXTs here, because we don't have a
702 pointer to the appropriate FUNCTION_TYPE node readily available
703 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
704 type nodes get set in `grokdeclarator' as soon as we have created
705 the FUNCTION_TYPE node which will represent the "scope" for these
706 "parameter list local" tagged types. */
707 if (scope->function_body)
708 context = current_function_decl;
709 else if (scope == file_scope)
711 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
712 TREE_CHAIN (file_decl) = all_translation_units;
713 all_translation_units = file_decl;
714 context = file_decl;
716 else
717 context = block;
719 /* Clear all bindings in this scope. */
720 for (b = scope->bindings; b; b = free_binding_and_advance (b))
722 p = b->decl;
723 switch (TREE_CODE (p))
725 case LABEL_DECL:
726 /* Warnings for unused labels, errors for undefined labels. */
727 if (TREE_USED (p) && !DECL_INITIAL (p))
729 error ("%Jlabel %qD used but not defined", p, p);
730 DECL_INITIAL (p) = error_mark_node;
732 else if (!TREE_USED (p) && warn_unused_label)
734 if (DECL_INITIAL (p))
735 warning (0, "%Jlabel %qD defined but not used", p, p);
736 else
737 warning (0, "%Jlabel %qD declared but not defined", p, p);
739 /* Labels go in BLOCK_VARS. */
740 TREE_CHAIN (p) = BLOCK_VARS (block);
741 BLOCK_VARS (block) = p;
742 gcc_assert (I_LABEL_BINDING (b->id) == b);
743 I_LABEL_BINDING (b->id) = b->shadowed;
744 break;
746 case ENUMERAL_TYPE:
747 case UNION_TYPE:
748 case RECORD_TYPE:
749 set_type_context (p, context);
751 /* Types may not have tag-names, in which case the type
752 appears in the bindings list with b->id NULL. */
753 if (b->id)
755 gcc_assert (I_TAG_BINDING (b->id) == b);
756 I_TAG_BINDING (b->id) = b->shadowed;
758 break;
760 case FUNCTION_DECL:
761 /* Propagate TREE_ADDRESSABLE from nested functions to their
762 containing functions. */
763 if (!TREE_ASM_WRITTEN (p)
764 && DECL_INITIAL (p) != 0
765 && TREE_ADDRESSABLE (p)
766 && DECL_ABSTRACT_ORIGIN (p) != 0
767 && DECL_ABSTRACT_ORIGIN (p) != p)
768 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
769 if (!DECL_EXTERNAL (p)
770 && DECL_INITIAL (p) == 0)
772 error ("%Jnested function %qD declared but never defined", p, p);
773 undef_nested_function = true;
775 goto common_symbol;
777 case VAR_DECL:
778 /* Warnings for unused variables. */
779 if (warn_unused_variable
780 && !TREE_USED (p)
781 && !DECL_IN_SYSTEM_HEADER (p)
782 && DECL_NAME (p)
783 && !DECL_ARTIFICIAL (p)
784 && scope != file_scope
785 && scope != external_scope)
786 warning (0, "%Junused variable %qD", p, p);
788 if (b->inner_comp)
790 error ("%Jtype of array %qD completed incompatibly with"
791 " implicit initialization", p, p);
794 /* Fall through. */
795 case TYPE_DECL:
796 case CONST_DECL:
797 common_symbol:
798 /* All of these go in BLOCK_VARS, but only if this is the
799 binding in the home scope. */
800 if (!b->nested)
802 TREE_CHAIN (p) = BLOCK_VARS (block);
803 BLOCK_VARS (block) = p;
805 /* If this is the file scope, and we are processing more
806 than one translation unit in this compilation, set
807 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
808 This makes same_translation_unit_p work, and causes
809 static declarations to be given disambiguating suffixes. */
810 if (scope == file_scope && num_in_fnames > 1)
812 DECL_CONTEXT (p) = context;
813 if (TREE_CODE (p) == TYPE_DECL)
814 set_type_context (TREE_TYPE (p), context);
817 /* Fall through. */
818 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
819 already been put there by store_parm_decls. Unused-
820 parameter warnings are handled by function.c.
821 error_mark_node obviously does not go in BLOCK_VARS and
822 does not get unused-variable warnings. */
823 case PARM_DECL:
824 case ERROR_MARK:
825 /* It is possible for a decl not to have a name. We get
826 here with b->id NULL in this case. */
827 if (b->id)
829 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
830 I_SYMBOL_BINDING (b->id) = b->shadowed;
831 if (b->shadowed && b->shadowed->type)
832 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
834 break;
836 default:
837 gcc_unreachable ();
842 /* Dispose of the block that we just made inside some higher level. */
843 if ((scope->function_body || scope == file_scope) && context)
845 DECL_INITIAL (context) = block;
846 BLOCK_SUPERCONTEXT (block) = context;
848 else if (scope->outer)
850 if (block)
851 SCOPE_LIST_APPEND (scope->outer, blocks, block);
852 /* If we did not make a block for the scope just exited, any
853 blocks made for inner scopes must be carried forward so they
854 will later become subblocks of something else. */
855 else if (scope->blocks)
856 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
859 /* Pop the current scope, and free the structure for reuse. */
860 current_scope = scope->outer;
861 if (scope->function_body)
862 current_function_scope = scope->outer_function;
864 memset (scope, 0, sizeof (struct c_scope));
865 scope->outer = scope_freelist;
866 scope_freelist = scope;
868 return block;
871 void
872 push_file_scope (void)
874 tree decl;
876 if (file_scope)
877 return;
879 push_scope ();
880 file_scope = current_scope;
882 start_fname_decls ();
884 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
885 bind (DECL_NAME (decl), decl, file_scope,
886 /*invisible=*/false, /*nested=*/true);
889 void
890 pop_file_scope (void)
892 /* In case there were missing closebraces, get us back to the global
893 binding level. */
894 while (current_scope != file_scope)
895 pop_scope ();
897 /* __FUNCTION__ is defined at file scope (""). This
898 call may not be necessary as my tests indicate it
899 still works without it. */
900 finish_fname_decls ();
902 /* This is the point to write out a PCH if we're doing that.
903 In that case we do not want to do anything else. */
904 if (pch_file)
906 c_common_write_pch ();
907 return;
910 /* Pop off the file scope and close this translation unit. */
911 pop_scope ();
912 file_scope = 0;
914 maybe_apply_pending_pragma_weaks ();
915 cgraph_finalize_compilation_unit ();
918 /* Insert BLOCK at the end of the list of subblocks of the current
919 scope. This is used when a BIND_EXPR is expanded, to handle the
920 BLOCK node inside the BIND_EXPR. */
922 void
923 insert_block (tree block)
925 TREE_USED (block) = 1;
926 SCOPE_LIST_APPEND (current_scope, blocks, block);
929 /* Push a definition or a declaration of struct, union or enum tag "name".
930 "type" should be the type node.
931 We assume that the tag "name" is not already defined.
933 Note that the definition may really be just a forward reference.
934 In that case, the TYPE_SIZE will be zero. */
936 static void
937 pushtag (tree name, tree type)
939 /* Record the identifier as the type's name if it has none. */
940 if (name && !TYPE_NAME (type))
941 TYPE_NAME (type) = name;
942 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
944 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
945 tagged type we just added to the current scope. This fake
946 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
947 to output a representation of a tagged type, and it also gives
948 us a convenient place to record the "scope start" address for the
949 tagged type. */
951 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
953 /* An approximation for now, so we can tell this is a function-scope tag.
954 This will be updated in pop_scope. */
955 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
958 /* Subroutine of compare_decls. Allow harmless mismatches in return
959 and argument types provided that the type modes match. This function
960 return a unified type given a suitable match, and 0 otherwise. */
962 static tree
963 match_builtin_function_types (tree newtype, tree oldtype)
965 tree newrettype, oldrettype;
966 tree newargs, oldargs;
967 tree trytype, tryargs;
969 /* Accept the return type of the new declaration if same modes. */
970 oldrettype = TREE_TYPE (oldtype);
971 newrettype = TREE_TYPE (newtype);
973 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
974 return 0;
976 oldargs = TYPE_ARG_TYPES (oldtype);
977 newargs = TYPE_ARG_TYPES (newtype);
978 tryargs = newargs;
980 while (oldargs || newargs)
982 if (!oldargs
983 || !newargs
984 || !TREE_VALUE (oldargs)
985 || !TREE_VALUE (newargs)
986 || TYPE_MODE (TREE_VALUE (oldargs))
987 != TYPE_MODE (TREE_VALUE (newargs)))
988 return 0;
990 oldargs = TREE_CHAIN (oldargs);
991 newargs = TREE_CHAIN (newargs);
994 trytype = build_function_type (newrettype, tryargs);
995 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
998 /* Subroutine of diagnose_mismatched_decls. Check for function type
999 mismatch involving an empty arglist vs a nonempty one and give clearer
1000 diagnostics. */
1001 static void
1002 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1003 tree newtype, tree oldtype)
1005 tree t;
1007 if (TREE_CODE (olddecl) != FUNCTION_DECL
1008 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1009 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1011 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1012 return;
1014 t = TYPE_ARG_TYPES (oldtype);
1015 if (t == 0)
1016 t = TYPE_ARG_TYPES (newtype);
1017 for (; t; t = TREE_CHAIN (t))
1019 tree type = TREE_VALUE (t);
1021 if (TREE_CHAIN (t) == 0
1022 && TYPE_MAIN_VARIANT (type) != void_type_node)
1024 inform ("a parameter list with an ellipsis can%'t match "
1025 "an empty parameter name list declaration");
1026 break;
1029 if (c_type_promotes_to (type) != type)
1031 inform ("an argument type that has a default promotion can%'t match "
1032 "an empty parameter name list declaration");
1033 break;
1038 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1039 old-style function definition, NEWDECL is a prototype declaration.
1040 Diagnose inconsistencies in the argument list. Returns TRUE if
1041 the prototype is compatible, FALSE if not. */
1042 static bool
1043 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1045 tree newargs, oldargs;
1046 int i;
1048 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1050 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1051 newargs = TYPE_ARG_TYPES (newtype);
1052 i = 1;
1054 for (;;)
1056 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1057 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1059 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1060 break;
1062 /* Reaching the end of just one list means the two decls don't
1063 agree on the number of arguments. */
1064 if (END_OF_ARGLIST (oldargtype))
1066 error ("%Jprototype for %qD declares more arguments "
1067 "than previous old-style definition", newdecl, newdecl);
1068 return false;
1070 else if (END_OF_ARGLIST (newargtype))
1072 error ("%Jprototype for %qD declares fewer arguments "
1073 "than previous old-style definition", newdecl, newdecl);
1074 return false;
1077 /* Type for passing arg must be consistent with that declared
1078 for the arg. */
1079 else if (!comptypes (oldargtype, newargtype))
1081 error ("%Jprototype for %qD declares argument %d"
1082 " with incompatible type",
1083 newdecl, newdecl, i);
1084 return false;
1087 oldargs = TREE_CHAIN (oldargs);
1088 newargs = TREE_CHAIN (newargs);
1089 i++;
1092 /* If we get here, no errors were found, but do issue a warning
1093 for this poor-style construct. */
1094 warning (0, "%Jprototype for %qD follows non-prototype definition",
1095 newdecl, newdecl);
1096 return true;
1097 #undef END_OF_ARGLIST
1100 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1101 first in a pair of mismatched declarations, using the diagnostic
1102 function DIAG. */
1103 static void
1104 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1106 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1108 else if (DECL_INITIAL (decl))
1109 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1110 else if (C_DECL_IMPLICIT (decl))
1111 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1112 else
1113 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1116 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1117 Returns true if the caller should proceed to merge the two, false
1118 if OLDDECL should simply be discarded. As a side effect, issues
1119 all necessary diagnostics for invalid or poor-style combinations.
1120 If it returns true, writes the types of NEWDECL and OLDDECL to
1121 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1122 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1124 static bool
1125 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1126 tree *newtypep, tree *oldtypep)
1128 tree newtype, oldtype;
1129 bool pedwarned = false;
1130 bool warned = false;
1131 bool retval = true;
1133 /* If we have error_mark_node for either decl or type, just discard
1134 the previous decl - we're in an error cascade already. */
1135 if (olddecl == error_mark_node || newdecl == error_mark_node)
1136 return false;
1137 *oldtypep = oldtype = TREE_TYPE (olddecl);
1138 *newtypep = newtype = TREE_TYPE (newdecl);
1139 if (oldtype == error_mark_node || newtype == error_mark_node)
1140 return false;
1142 /* Two different categories of symbol altogether. This is an error
1143 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1144 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1146 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1147 && DECL_BUILT_IN (olddecl)
1148 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1150 error ("%J%qD redeclared as different kind of symbol",
1151 newdecl, newdecl);
1152 locate_old_decl (olddecl, error);
1154 else if (TREE_PUBLIC (newdecl))
1155 warning (0, "%Jbuilt-in function %qD declared as non-function",
1156 newdecl, newdecl);
1157 else if (warn_shadow)
1158 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1159 newdecl, newdecl);
1160 return false;
1163 /* Enumerators have no linkage, so may only be declared once in a
1164 given scope. */
1165 if (TREE_CODE (olddecl) == CONST_DECL)
1167 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1168 locate_old_decl (olddecl, error);
1169 return false;
1172 if (!comptypes (oldtype, newtype))
1174 if (TREE_CODE (olddecl) == FUNCTION_DECL
1175 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1177 /* Accept harmless mismatch in function types.
1178 This is for the ffs and fprintf builtins. */
1179 tree trytype = match_builtin_function_types (newtype, oldtype);
1181 if (trytype && comptypes (newtype, trytype))
1182 *oldtypep = oldtype = trytype;
1183 else
1185 /* If types don't match for a built-in, throw away the
1186 built-in. No point in calling locate_old_decl here, it
1187 won't print anything. */
1188 warning (0, "%Jconflicting types for built-in function %qD",
1189 newdecl, newdecl);
1190 return false;
1193 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1194 && DECL_IS_BUILTIN (olddecl))
1196 /* A conflicting function declaration for a predeclared
1197 function that isn't actually built in. Objective C uses
1198 these. The new declaration silently overrides everything
1199 but the volatility (i.e. noreturn) indication. See also
1200 below. FIXME: Make Objective C use normal builtins. */
1201 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1202 return false;
1204 /* Permit void foo (...) to match int foo (...) if the latter is
1205 the definition and implicit int was used. See
1206 c-torture/compile/920625-2.c. */
1207 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1208 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1209 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1210 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1212 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1213 /* Make sure we keep void as the return type. */
1214 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1215 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1216 pedwarned = true;
1218 /* Permit void foo (...) to match an earlier call to foo (...) with
1219 no declared type (thus, implicitly int). */
1220 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1221 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1222 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1223 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1225 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1226 /* Make sure we keep void as the return type. */
1227 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1228 pedwarned = true;
1230 else
1232 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1233 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1234 else
1235 error ("%Jconflicting types for %qD", newdecl, newdecl);
1236 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1237 locate_old_decl (olddecl, error);
1238 return false;
1242 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1243 but silently ignore the redeclaration if either is in a system
1244 header. (Conflicting redeclarations were handled above.) */
1245 if (TREE_CODE (newdecl) == TYPE_DECL)
1247 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1248 return true; /* Allow OLDDECL to continue in use. */
1250 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1251 locate_old_decl (olddecl, error);
1252 return false;
1255 /* Function declarations can either be 'static' or 'extern' (no
1256 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1257 can never conflict with each other on account of linkage (6.2.2p4).
1258 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1259 two definitions if one is 'extern inline' and one is not. The non-
1260 extern-inline definition supersedes the extern-inline definition. */
1261 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1263 /* If you declare a built-in function name as static, or
1264 define the built-in with an old-style definition (so we
1265 can't validate the argument list) the built-in definition is
1266 overridden, but optionally warn this was a bad choice of name. */
1267 if (DECL_BUILT_IN (olddecl)
1268 && !C_DECL_DECLARED_BUILTIN (olddecl)
1269 && (!TREE_PUBLIC (newdecl)
1270 || (DECL_INITIAL (newdecl)
1271 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1273 if (warn_shadow)
1274 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1275 newdecl, newdecl);
1276 /* Discard the old built-in function. */
1277 return false;
1280 if (DECL_INITIAL (newdecl))
1282 if (DECL_INITIAL (olddecl))
1284 /* If both decls have extern inline and are in the same TU,
1285 reject the new decl. */
1286 if (DECL_DECLARED_INLINE_P (olddecl)
1287 && DECL_EXTERNAL (olddecl)
1288 && DECL_DECLARED_INLINE_P (newdecl)
1289 && DECL_EXTERNAL (newdecl)
1290 && same_translation_unit_p (newdecl, olddecl))
1292 error ("%Jredefinition of %qD", newdecl, newdecl);
1293 locate_old_decl (olddecl, error);
1294 return false;
1296 /* If both decls have not extern inline, reject the new decl. */
1297 if (!DECL_DECLARED_INLINE_P (olddecl)
1298 && !DECL_EXTERNAL (olddecl)
1299 && !DECL_DECLARED_INLINE_P (newdecl)
1300 && !DECL_EXTERNAL (newdecl))
1302 error ("%Jredefinition of %qD", newdecl, newdecl);
1303 locate_old_decl (olddecl, error);
1304 return false;
1306 /* If the new decl is declared as extern inline, error if they are
1307 in the same TU, otherwise retain the old decl. */
1308 if (!DECL_DECLARED_INLINE_P (olddecl)
1309 && !DECL_EXTERNAL (olddecl)
1310 && DECL_DECLARED_INLINE_P (newdecl)
1311 && DECL_EXTERNAL (newdecl))
1313 if (same_translation_unit_p (newdecl, olddecl))
1315 error ("%Jredefinition of %qD", newdecl, newdecl);
1316 locate_old_decl (olddecl, error);
1317 return false;
1319 else
1320 retval = false;
1324 /* If we have a prototype after an old-style function definition,
1325 the argument types must be checked specially. */
1326 else if (DECL_INITIAL (olddecl)
1327 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1328 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1329 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1331 locate_old_decl (olddecl, error);
1332 return false;
1334 /* A non-static declaration (even an "extern") followed by a
1335 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1336 The same is true for a static forward declaration at block
1337 scope followed by a non-static declaration/definition at file
1338 scope. Static followed by non-static at the same scope is
1339 not undefined behavior, and is the most convenient way to get
1340 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1341 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1342 we do diagnose it if -Wtraditional. */
1343 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1345 /* Two exceptions to the rule. If olddecl is an extern
1346 inline, or a predeclared function that isn't actually
1347 built in, newdecl silently overrides olddecl. The latter
1348 occur only in Objective C; see also above. (FIXME: Make
1349 Objective C use normal builtins.) */
1350 if (!DECL_IS_BUILTIN (olddecl)
1351 && !(DECL_EXTERNAL (olddecl)
1352 && DECL_DECLARED_INLINE_P (olddecl)))
1354 error ("%Jstatic declaration of %qD follows "
1355 "non-static declaration", newdecl, newdecl);
1356 locate_old_decl (olddecl, error);
1358 return false;
1360 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1362 if (DECL_CONTEXT (olddecl))
1364 error ("%Jnon-static declaration of %qD follows "
1365 "static declaration", newdecl, newdecl);
1366 locate_old_decl (olddecl, error);
1367 return false;
1369 else if (warn_traditional)
1371 warning (0, "%Jnon-static declaration of %qD follows "
1372 "static declaration", newdecl, newdecl);
1373 warned = true;
1377 else if (TREE_CODE (newdecl) == VAR_DECL)
1379 /* Only variables can be thread-local, and all declarations must
1380 agree on this property. */
1381 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1383 if (DECL_THREAD_LOCAL (newdecl))
1384 error ("%Jthread-local declaration of %qD follows "
1385 "non-thread-local declaration", newdecl, newdecl);
1386 else
1387 error ("%Jnon-thread-local declaration of %qD follows "
1388 "thread-local declaration", newdecl, newdecl);
1390 locate_old_decl (olddecl, error);
1391 return false;
1394 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1395 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1397 error ("%Jredefinition of %qD", newdecl, newdecl);
1398 locate_old_decl (olddecl, error);
1399 return false;
1402 /* Objects declared at file scope: if the first declaration had
1403 external linkage (even if it was an external reference) the
1404 second must have external linkage as well, or the behavior is
1405 undefined. If the first declaration had internal linkage, then
1406 the second must too, or else be an external reference (in which
1407 case the composite declaration still has internal linkage).
1408 As for function declarations, we warn about the static-then-
1409 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1410 if (DECL_FILE_SCOPE_P (newdecl)
1411 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1413 if (DECL_EXTERNAL (newdecl))
1415 if (!DECL_FILE_SCOPE_P (olddecl))
1417 error ("%Jextern declaration of %qD follows "
1418 "declaration with no linkage", newdecl, newdecl);
1419 locate_old_decl (olddecl, error);
1420 return false;
1422 else if (warn_traditional)
1424 warning (0, "%Jnon-static declaration of %qD follows "
1425 "static declaration", newdecl, newdecl);
1426 warned = true;
1429 else
1431 if (TREE_PUBLIC (newdecl))
1432 error ("%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1434 else
1435 error ("%Jstatic declaration of %qD follows "
1436 "non-static declaration", newdecl, newdecl);
1438 locate_old_decl (olddecl, error);
1439 return false;
1442 /* Two objects with the same name declared at the same block
1443 scope must both be external references (6.7p3). */
1444 else if (!DECL_FILE_SCOPE_P (newdecl))
1446 if (DECL_EXTERNAL (newdecl))
1448 /* Extern with initializer at block scope, which will
1449 already have received an error. */
1451 else if (DECL_EXTERNAL (olddecl))
1453 error ("%Jdeclaration of %qD with no linkage follows "
1454 "extern declaration", newdecl, newdecl);
1455 locate_old_decl (olddecl, error);
1457 else
1459 error ("%Jredeclaration of %qD with no linkage",
1460 newdecl, newdecl);
1461 locate_old_decl (olddecl, error);
1464 return false;
1468 /* warnings */
1469 /* All decls must agree on a visibility. */
1470 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1471 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1473 warning (0, "%Jredeclaration of %qD with different visibility "
1474 "(old visibility preserved)", newdecl, newdecl);
1475 warned = true;
1478 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1480 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1481 if (DECL_DECLARED_INLINE_P (newdecl)
1482 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1484 warning (0, "%Jinline declaration of %qD follows "
1485 "declaration with attribute noinline", newdecl, newdecl);
1486 warned = true;
1488 else if (DECL_DECLARED_INLINE_P (olddecl)
1489 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1491 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1492 "inline declaration ", newdecl, newdecl);
1493 warned = true;
1496 /* Inline declaration after use or definition.
1497 ??? Should we still warn about this now we have unit-at-a-time
1498 mode and can get it right?
1499 Definitely don't complain if the decls are in different translation
1500 units. */
1501 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1502 && same_translation_unit_p (olddecl, newdecl))
1504 if (TREE_USED (olddecl))
1506 warning (0, "%J%qD declared inline after being called",
1507 olddecl, olddecl);
1508 warned = true;
1510 else if (DECL_INITIAL (olddecl))
1512 warning (0, "%J%qD declared inline after its definition",
1513 olddecl, olddecl);
1514 warned = true;
1518 else /* PARM_DECL, VAR_DECL */
1520 /* Redeclaration of a parameter is a constraint violation (this is
1521 not explicitly stated, but follows from C99 6.7p3 [no more than
1522 one declaration of the same identifier with no linkage in the
1523 same scope, except type tags] and 6.2.2p6 [parameters have no
1524 linkage]). We must check for a forward parameter declaration,
1525 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1526 an extension, the mandatory diagnostic for which is handled by
1527 mark_forward_parm_decls. */
1529 if (TREE_CODE (newdecl) == PARM_DECL
1530 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1532 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1533 locate_old_decl (olddecl, error);
1534 return false;
1538 /* Optional warning for completely redundant decls. */
1539 if (!warned && !pedwarned
1540 && warn_redundant_decls
1541 /* Don't warn about a function declaration followed by a
1542 definition. */
1543 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1544 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1545 /* Don't warn about redundant redeclarations of builtins. */
1546 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1547 && !DECL_BUILT_IN (newdecl)
1548 && DECL_BUILT_IN (olddecl)
1549 && !C_DECL_DECLARED_BUILTIN (olddecl))
1550 /* Don't warn about an extern followed by a definition. */
1551 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1552 /* Don't warn about forward parameter decls. */
1553 && !(TREE_CODE (newdecl) == PARM_DECL
1554 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1556 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1557 warned = true;
1560 /* Report location of previous decl/defn in a consistent manner. */
1561 if (warned || pedwarned)
1562 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1564 return retval;
1567 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1568 consistent with OLDDECL, but carries new information. Merge the
1569 new information into OLDDECL. This function issues no
1570 diagnostics. */
1572 static void
1573 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1575 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1576 && DECL_INITIAL (newdecl) != 0);
1578 /* For real parm decl following a forward decl, rechain the old decl
1579 in its new location and clear TREE_ASM_WRITTEN (it's not a
1580 forward decl anymore). */
1581 if (TREE_CODE (newdecl) == PARM_DECL
1582 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1584 struct c_binding *b, **here;
1586 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1587 if ((*here)->decl == olddecl)
1588 goto found;
1589 gcc_unreachable ();
1591 found:
1592 b = *here;
1593 *here = b->prev;
1594 b->prev = current_scope->bindings;
1595 current_scope->bindings = b;
1597 TREE_ASM_WRITTEN (olddecl) = 0;
1600 DECL_ATTRIBUTES (newdecl)
1601 = targetm.merge_decl_attributes (olddecl, newdecl);
1603 /* Merge the data types specified in the two decls. */
1604 TREE_TYPE (newdecl)
1605 = TREE_TYPE (olddecl)
1606 = composite_type (newtype, oldtype);
1608 /* Lay the type out, unless already done. */
1609 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1611 if (TREE_TYPE (newdecl) != error_mark_node)
1612 layout_type (TREE_TYPE (newdecl));
1613 if (TREE_CODE (newdecl) != FUNCTION_DECL
1614 && TREE_CODE (newdecl) != TYPE_DECL
1615 && TREE_CODE (newdecl) != CONST_DECL)
1616 layout_decl (newdecl, 0);
1618 else
1620 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1621 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1622 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1623 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1624 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1625 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1627 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1628 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1632 /* Keep the old rtl since we can safely use it. */
1633 COPY_DECL_RTL (olddecl, newdecl);
1635 /* Merge the type qualifiers. */
1636 if (TREE_READONLY (newdecl))
1637 TREE_READONLY (olddecl) = 1;
1639 if (TREE_THIS_VOLATILE (newdecl))
1641 TREE_THIS_VOLATILE (olddecl) = 1;
1642 if (TREE_CODE (newdecl) == VAR_DECL)
1643 make_var_volatile (newdecl);
1646 /* Merge deprecatedness. */
1647 if (TREE_DEPRECATED (newdecl))
1648 TREE_DEPRECATED (olddecl) = 1;
1650 /* Keep source location of definition rather than declaration. */
1651 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1652 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1654 /* Merge the unused-warning information. */
1655 if (DECL_IN_SYSTEM_HEADER (olddecl))
1656 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1657 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1658 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1660 /* Merge the initialization information. */
1661 if (DECL_INITIAL (newdecl) == 0)
1662 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1664 /* Merge the section attribute.
1665 We want to issue an error if the sections conflict but that must be
1666 done later in decl_attributes since we are called before attributes
1667 are assigned. */
1668 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1669 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1671 /* Copy the assembler name.
1672 Currently, it can only be defined in the prototype. */
1673 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1675 /* Use visibility of whichever declaration had it specified */
1676 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1678 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1679 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1682 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1684 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1685 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1686 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1687 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1688 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1689 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1690 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1691 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1692 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1693 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1696 /* Merge the storage class information. */
1697 merge_weak (newdecl, olddecl);
1699 /* For functions, static overrides non-static. */
1700 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1702 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1703 /* This is since we don't automatically
1704 copy the attributes of NEWDECL into OLDDECL. */
1705 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1706 /* If this clears `static', clear it in the identifier too. */
1707 if (!TREE_PUBLIC (olddecl))
1708 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1710 if (DECL_EXTERNAL (newdecl))
1712 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1713 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1715 /* An extern decl does not override previous storage class. */
1716 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1717 if (!DECL_EXTERNAL (newdecl))
1719 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1720 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1723 else
1725 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1726 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1729 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1731 /* If we're redefining a function previously defined as extern
1732 inline, make sure we emit debug info for the inline before we
1733 throw it away, in case it was inlined into a function that hasn't
1734 been written out yet. */
1735 if (new_is_definition && DECL_INITIAL (olddecl))
1737 if (TREE_USED (olddecl)
1738 /* In unit-at-a-time mode we never inline re-defined extern
1739 inline functions. */
1740 && !flag_unit_at_a_time
1741 && cgraph_function_possibly_inlined_p (olddecl))
1742 (*debug_hooks->outlining_inline_function) (olddecl);
1744 /* The new defn must not be inline. */
1745 DECL_INLINE (newdecl) = 0;
1746 DECL_UNINLINABLE (newdecl) = 1;
1748 else
1750 /* If either decl says `inline', this fn is inline,
1751 unless its definition was passed already. */
1752 if (DECL_DECLARED_INLINE_P (newdecl)
1753 || DECL_DECLARED_INLINE_P (olddecl))
1754 DECL_DECLARED_INLINE_P (newdecl) = 1;
1756 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1757 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1760 if (DECL_BUILT_IN (olddecl))
1762 /* If redeclaring a builtin function, it stays built in.
1763 But it gets tagged as having been declared. */
1764 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1765 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1766 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1769 /* Also preserve various other info from the definition. */
1770 if (!new_is_definition)
1772 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1773 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1774 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1775 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1776 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1778 /* Set DECL_INLINE on the declaration if we've got a body
1779 from which to instantiate. */
1780 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1782 DECL_INLINE (newdecl) = 1;
1783 DECL_ABSTRACT_ORIGIN (newdecl)
1784 = DECL_ABSTRACT_ORIGIN (olddecl);
1787 else
1789 /* If a previous declaration said inline, mark the
1790 definition as inlinable. */
1791 if (DECL_DECLARED_INLINE_P (newdecl)
1792 && !DECL_UNINLINABLE (newdecl))
1793 DECL_INLINE (newdecl) = 1;
1797 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1798 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1800 unsigned olddecl_uid = DECL_UID (olddecl);
1801 tree olddecl_context = DECL_CONTEXT (olddecl);
1803 memcpy ((char *) olddecl + sizeof (struct tree_common),
1804 (char *) newdecl + sizeof (struct tree_common),
1805 sizeof (struct tree_decl) - sizeof (struct tree_common));
1806 DECL_UID (olddecl) = olddecl_uid;
1807 DECL_CONTEXT (olddecl) = olddecl_context;
1810 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1811 so that encode_section_info has a chance to look at the new decl
1812 flags and attributes. */
1813 if (DECL_RTL_SET_P (olddecl)
1814 && (TREE_CODE (olddecl) == FUNCTION_DECL
1815 || (TREE_CODE (olddecl) == VAR_DECL
1816 && TREE_STATIC (olddecl))))
1817 make_decl_rtl (olddecl);
1820 /* Handle when a new declaration NEWDECL has the same name as an old
1821 one OLDDECL in the same binding contour. Prints an error message
1822 if appropriate.
1824 If safely possible, alter OLDDECL to look like NEWDECL, and return
1825 true. Otherwise, return false. */
1827 static bool
1828 duplicate_decls (tree newdecl, tree olddecl)
1830 tree newtype = NULL, oldtype = NULL;
1832 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1833 return false;
1835 merge_decls (newdecl, olddecl, newtype, oldtype);
1836 return true;
1840 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1841 static void
1842 warn_if_shadowing (tree new_decl)
1844 struct c_binding *b;
1846 /* Shadow warnings wanted? */
1847 if (!warn_shadow
1848 /* No shadow warnings for internally generated vars. */
1849 || DECL_IS_BUILTIN (new_decl)
1850 /* No shadow warnings for vars made for inlining. */
1851 || DECL_FROM_INLINE (new_decl)
1852 /* Don't warn about the parm names in function declarator
1853 within a function declarator. It would be nice to avoid
1854 warning in any function declarator in a declaration, as
1855 opposed to a definition, but there is no way to tell
1856 it's not a definition at this point. */
1857 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1858 return;
1860 /* Is anything being shadowed? Invisible decls do not count. */
1861 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1862 if (b->decl && b->decl != new_decl && !b->invisible)
1864 tree old_decl = b->decl;
1866 if (old_decl == error_mark_node)
1868 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1869 new_decl, new_decl);
1870 break;
1872 else if (TREE_CODE (old_decl) == PARM_DECL)
1873 warning (0, "%Jdeclaration of %qD shadows a parameter",
1874 new_decl, new_decl);
1875 else if (DECL_FILE_SCOPE_P (old_decl))
1876 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1877 new_decl, new_decl);
1878 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1879 && DECL_BUILT_IN (old_decl))
1881 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1882 new_decl, new_decl);
1883 break;
1885 else
1886 warning (0, "%Jdeclaration of %qD shadows a previous local",
1887 new_decl, new_decl);
1889 warning (0, "%Jshadowed declaration is here", old_decl);
1891 break;
1896 /* Subroutine of pushdecl.
1898 X is a TYPE_DECL for a typedef statement. Create a brand new
1899 ..._TYPE node (which will be just a variant of the existing
1900 ..._TYPE node with identical properties) and then install X
1901 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1903 The whole point here is to end up with a situation where each
1904 and every ..._TYPE node the compiler creates will be uniquely
1905 associated with AT MOST one node representing a typedef name.
1906 This way, even though the compiler substitutes corresponding
1907 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1908 early on, later parts of the compiler can always do the reverse
1909 translation and get back the corresponding typedef name. For
1910 example, given:
1912 typedef struct S MY_TYPE;
1913 MY_TYPE object;
1915 Later parts of the compiler might only know that `object' was of
1916 type `struct S' if it were not for code just below. With this
1917 code however, later parts of the compiler see something like:
1919 struct S' == struct S
1920 typedef struct S' MY_TYPE;
1921 struct S' object;
1923 And they can then deduce (from the node for type struct S') that
1924 the original object declaration was:
1926 MY_TYPE object;
1928 Being able to do this is important for proper support of protoize,
1929 and also for generating precise symbolic debugging information
1930 which takes full account of the programmer's (typedef) vocabulary.
1932 Obviously, we don't want to generate a duplicate ..._TYPE node if
1933 the TYPE_DECL node that we are now processing really represents a
1934 standard built-in type.
1936 Since all standard types are effectively declared at line zero
1937 in the source file, we can easily check to see if we are working
1938 on a standard type by checking the current value of lineno. */
1940 static void
1941 clone_underlying_type (tree x)
1943 if (DECL_IS_BUILTIN (x))
1945 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1946 TYPE_NAME (TREE_TYPE (x)) = x;
1948 else if (TREE_TYPE (x) != error_mark_node
1949 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1951 tree tt = TREE_TYPE (x);
1952 DECL_ORIGINAL_TYPE (x) = tt;
1953 tt = build_variant_type_copy (tt);
1954 TYPE_NAME (tt) = x;
1955 TREE_USED (tt) = TREE_USED (x);
1956 TREE_TYPE (x) = tt;
1960 /* Record a decl-node X as belonging to the current lexical scope.
1961 Check for errors (such as an incompatible declaration for the same
1962 name already seen in the same scope).
1964 Returns either X or an old decl for the same name.
1965 If an old decl is returned, it may have been smashed
1966 to agree with what X says. */
1968 tree
1969 pushdecl (tree x)
1971 tree name = DECL_NAME (x);
1972 struct c_scope *scope = current_scope;
1973 struct c_binding *b;
1974 bool nested = false;
1976 /* Functions need the lang_decl data. */
1977 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1978 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1980 /* Must set DECL_CONTEXT for everything not at file scope or
1981 DECL_FILE_SCOPE_P won't work. Local externs don't count
1982 unless they have initializers (which generate code). */
1983 if (current_function_decl
1984 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1985 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1986 DECL_CONTEXT (x) = current_function_decl;
1988 /* If this is of variably modified type, prevent jumping into its
1989 scope. */
1990 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
1991 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
1992 c_begin_vm_scope (scope->depth);
1994 /* Anonymous decls are just inserted in the scope. */
1995 if (!name)
1997 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1998 return x;
2001 /* First, see if there is another declaration with the same name in
2002 the current scope. If there is, duplicate_decls may do all the
2003 work for us. If duplicate_decls returns false, that indicates
2004 two incompatible decls in the same scope; we are to silently
2005 replace the old one (duplicate_decls has issued all appropriate
2006 diagnostics). In particular, we should not consider possible
2007 duplicates in the external scope, or shadowing. */
2008 b = I_SYMBOL_BINDING (name);
2009 if (b && B_IN_SCOPE (b, scope))
2011 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2012 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2013 b->inner_comp = false;
2014 if (duplicate_decls (x, b->decl))
2015 return b->decl;
2016 else
2017 goto skip_external_and_shadow_checks;
2020 /* All declarations with external linkage, and all external
2021 references, go in the external scope, no matter what scope is
2022 current. However, the binding in that scope is ignored for
2023 purposes of normal name lookup. A separate binding structure is
2024 created in the requested scope; this governs the normal
2025 visibility of the symbol.
2027 The binding in the externals scope is used exclusively for
2028 detecting duplicate declarations of the same object, no matter
2029 what scope they are in; this is what we do here. (C99 6.2.7p2:
2030 All declarations that refer to the same object or function shall
2031 have compatible type; otherwise, the behavior is undefined.) */
2032 if (DECL_EXTERNAL (x) || scope == file_scope)
2034 tree type = TREE_TYPE (x);
2035 tree vistype = 0;
2036 tree visdecl = 0;
2037 bool type_saved = false;
2038 if (b && !B_IN_EXTERNAL_SCOPE (b)
2039 && (TREE_CODE (b->decl) == FUNCTION_DECL
2040 || TREE_CODE (b->decl) == VAR_DECL)
2041 && DECL_FILE_SCOPE_P (b->decl))
2043 visdecl = b->decl;
2044 vistype = TREE_TYPE (visdecl);
2046 if (warn_nested_externs
2047 && scope != file_scope
2048 && !DECL_IN_SYSTEM_HEADER (x))
2049 warning (0, "nested extern declaration of %qD", x);
2051 while (b && !B_IN_EXTERNAL_SCOPE (b))
2053 /* If this decl might be modified, save its type. This is
2054 done here rather than when the decl is first bound
2055 because the type may change after first binding, through
2056 being completed or through attributes being added. If we
2057 encounter multiple such decls, only the first should have
2058 its type saved; the others will already have had their
2059 proper types saved and the types will not have changed as
2060 their scopes will not have been re-entered. */
2061 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2063 b->type = TREE_TYPE (b->decl);
2064 type_saved = true;
2066 if (B_IN_FILE_SCOPE (b)
2067 && TREE_CODE (b->decl) == VAR_DECL
2068 && TREE_STATIC (b->decl)
2069 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2070 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2071 && TREE_CODE (type) == ARRAY_TYPE
2072 && TYPE_DOMAIN (type)
2073 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2074 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2076 /* Array type completed in inner scope, which should be
2077 diagnosed if the completion does not have size 1 and
2078 it does not get completed in the file scope. */
2079 b->inner_comp = true;
2081 b = b->shadowed;
2084 /* If a matching external declaration has been found, set its
2085 type to the composite of all the types of that declaration.
2086 After the consistency checks, it will be reset to the
2087 composite of the visible types only. */
2088 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2089 && b->type)
2090 TREE_TYPE (b->decl) = b->type;
2092 /* The point of the same_translation_unit_p check here is,
2093 we want to detect a duplicate decl for a construct like
2094 foo() { extern bar(); } ... static bar(); but not if
2095 they are in different translation units. In any case,
2096 the static does not go in the externals scope. */
2097 if (b
2098 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2099 && duplicate_decls (x, b->decl))
2101 tree thistype;
2102 thistype = (vistype ? composite_type (vistype, type) : type);
2103 b->type = TREE_TYPE (b->decl);
2104 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2105 thistype
2106 = build_type_attribute_variant (thistype,
2107 TYPE_ATTRIBUTES (b->type));
2108 TREE_TYPE (b->decl) = thistype;
2109 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2110 return b->decl;
2112 else if (TREE_PUBLIC (x))
2114 if (visdecl && !b && duplicate_decls (x, visdecl))
2116 /* An external declaration at block scope referring to a
2117 visible entity with internal linkage. The composite
2118 type will already be correct for this scope, so we
2119 just need to fall through to make the declaration in
2120 this scope. */
2121 nested = true;
2122 x = visdecl;
2124 else
2126 bind (name, x, external_scope, /*invisible=*/true,
2127 /*nested=*/false);
2128 nested = true;
2133 warn_if_shadowing (x);
2135 skip_external_and_shadow_checks:
2136 if (TREE_CODE (x) == TYPE_DECL)
2137 clone_underlying_type (x);
2139 bind (name, x, scope, /*invisible=*/false, nested);
2141 /* If x's type is incomplete because it's based on a
2142 structure or union which has not yet been fully declared,
2143 attach it to that structure or union type, so we can go
2144 back and complete the variable declaration later, if the
2145 structure or union gets fully declared.
2147 If the input is erroneous, we can have error_mark in the type
2148 slot (e.g. "f(void a, ...)") - that doesn't count as an
2149 incomplete type. */
2150 if (TREE_TYPE (x) != error_mark_node
2151 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2153 tree element = TREE_TYPE (x);
2155 while (TREE_CODE (element) == ARRAY_TYPE)
2156 element = TREE_TYPE (element);
2157 element = TYPE_MAIN_VARIANT (element);
2159 if ((TREE_CODE (element) == RECORD_TYPE
2160 || TREE_CODE (element) == UNION_TYPE)
2161 && (TREE_CODE (x) != TYPE_DECL
2162 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2163 && !COMPLETE_TYPE_P (element))
2164 C_TYPE_INCOMPLETE_VARS (element)
2165 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2167 return x;
2170 /* Record X as belonging to file scope.
2171 This is used only internally by the Objective-C front end,
2172 and is limited to its needs. duplicate_decls is not called;
2173 if there is any preexisting decl for this identifier, it is an ICE. */
2175 tree
2176 pushdecl_top_level (tree x)
2178 tree name;
2179 bool nested = false;
2180 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2182 name = DECL_NAME (x);
2184 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2186 if (TREE_PUBLIC (x))
2188 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2189 nested = true;
2191 if (file_scope)
2192 bind (name, x, file_scope, /*invisible=*/false, nested);
2194 return x;
2197 static void
2198 implicit_decl_warning (tree id, tree olddecl)
2200 void (*diag) (const char *, ...);
2201 switch (mesg_implicit_function_declaration)
2203 case 0: return;
2204 case 1: diag = warning0; break;
2205 case 2: diag = error; break;
2206 default: gcc_unreachable ();
2209 diag (N_("implicit declaration of function %qE"), id);
2210 if (olddecl)
2211 locate_old_decl (olddecl, diag);
2214 /* Generate an implicit declaration for identifier FUNCTIONID as a
2215 function of type int (). */
2217 tree
2218 implicitly_declare (tree functionid)
2220 struct c_binding *b;
2221 tree decl = 0;
2222 tree asmspec_tree;
2224 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2226 if (B_IN_SCOPE (b, external_scope))
2228 decl = b->decl;
2229 break;
2233 if (decl)
2235 if (decl == error_mark_node)
2236 return decl;
2238 /* FIXME: Objective-C has weird not-really-builtin functions
2239 which are supposed to be visible automatically. They wind up
2240 in the external scope because they're pushed before the file
2241 scope gets created. Catch this here and rebind them into the
2242 file scope. */
2243 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2245 bind (functionid, decl, file_scope,
2246 /*invisible=*/false, /*nested=*/true);
2247 return decl;
2249 else
2251 tree newtype = default_function_type;
2252 if (b->type)
2253 TREE_TYPE (decl) = b->type;
2254 /* Implicit declaration of a function already declared
2255 (somehow) in a different scope, or as a built-in.
2256 If this is the first time this has happened, warn;
2257 then recycle the old declaration but with the new type. */
2258 if (!C_DECL_IMPLICIT (decl))
2260 implicit_decl_warning (functionid, decl);
2261 C_DECL_IMPLICIT (decl) = 1;
2263 if (DECL_BUILT_IN (decl))
2265 newtype = build_type_attribute_variant (newtype,
2266 TYPE_ATTRIBUTES
2267 (TREE_TYPE (decl)));
2268 if (!comptypes (newtype, TREE_TYPE (decl)))
2270 warning (0, "incompatible implicit declaration of built-in"
2271 " function %qD", decl);
2272 newtype = TREE_TYPE (decl);
2275 else
2277 if (!comptypes (newtype, TREE_TYPE (decl)))
2279 error ("incompatible implicit declaration of function %qD",
2280 decl);
2281 locate_old_decl (decl, error);
2284 b->type = TREE_TYPE (decl);
2285 TREE_TYPE (decl) = newtype;
2286 bind (functionid, decl, current_scope,
2287 /*invisible=*/false, /*nested=*/true);
2288 return decl;
2292 /* Not seen before. */
2293 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2294 DECL_EXTERNAL (decl) = 1;
2295 TREE_PUBLIC (decl) = 1;
2296 C_DECL_IMPLICIT (decl) = 1;
2297 implicit_decl_warning (functionid, 0);
2298 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2299 if (asmspec_tree)
2300 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2302 /* C89 says implicit declarations are in the innermost block.
2303 So we record the decl in the standard fashion. */
2304 decl = pushdecl (decl);
2306 /* No need to call objc_check_decl here - it's a function type. */
2307 rest_of_decl_compilation (decl, 0, 0);
2309 /* Write a record describing this implicit function declaration
2310 to the prototypes file (if requested). */
2311 gen_aux_info_record (decl, 0, 1, 0);
2313 /* Possibly apply some default attributes to this implicit declaration. */
2314 decl_attributes (&decl, NULL_TREE, 0);
2316 return decl;
2319 /* Issue an error message for a reference to an undeclared variable
2320 ID, including a reference to a builtin outside of function-call
2321 context. Establish a binding of the identifier to error_mark_node
2322 in an appropriate scope, which will suppress further errors for the
2323 same identifier. The error message should be given location LOC. */
2324 void
2325 undeclared_variable (tree id, location_t loc)
2327 static bool already = false;
2328 struct c_scope *scope;
2330 if (current_function_decl == 0)
2332 error ("%H%qE undeclared here (not in a function)", &loc, id);
2333 scope = current_scope;
2335 else
2337 error ("%H%qE undeclared (first use in this function)", &loc, id);
2339 if (!already)
2341 error ("%H(Each undeclared identifier is reported only once", &loc);
2342 error ("%Hfor each function it appears in.)", &loc);
2343 already = true;
2346 /* If we are parsing old-style parameter decls, current_function_decl
2347 will be nonnull but current_function_scope will be null. */
2348 scope = current_function_scope ? current_function_scope : current_scope;
2350 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2353 /* Subroutine of lookup_label, declare_label, define_label: construct a
2354 LABEL_DECL with all the proper frills. */
2356 static tree
2357 make_label (tree name, location_t location)
2359 tree label = build_decl (LABEL_DECL, name, void_type_node);
2361 DECL_CONTEXT (label) = current_function_decl;
2362 DECL_MODE (label) = VOIDmode;
2363 DECL_SOURCE_LOCATION (label) = location;
2365 return label;
2368 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2369 Create one if none exists so far for the current function.
2370 This is called when a label is used in a goto expression or
2371 has its address taken. */
2373 tree
2374 lookup_label (tree name)
2376 tree label;
2378 if (current_function_decl == 0)
2380 error ("label %qE referenced outside of any function", name);
2381 return 0;
2384 /* Use a label already defined or ref'd with this name, but not if
2385 it is inherited from a containing function and wasn't declared
2386 using __label__. */
2387 label = I_LABEL_DECL (name);
2388 if (label && (DECL_CONTEXT (label) == current_function_decl
2389 || C_DECLARED_LABEL_FLAG (label)))
2391 /* If the label has only been declared, update its apparent
2392 location to point here, for better diagnostics if it
2393 turns out not to have been defined. */
2394 if (!TREE_USED (label))
2395 DECL_SOURCE_LOCATION (label) = input_location;
2396 return label;
2399 /* No label binding for that identifier; make one. */
2400 label = make_label (name, input_location);
2402 /* Ordinary labels go in the current function scope. */
2403 bind (name, label, current_function_scope,
2404 /*invisible=*/false, /*nested=*/false);
2405 return label;
2408 /* Make a label named NAME in the current function, shadowing silently
2409 any that may be inherited from containing functions or containing
2410 scopes. This is called for __label__ declarations. */
2412 tree
2413 declare_label (tree name)
2415 struct c_binding *b = I_LABEL_BINDING (name);
2416 tree label;
2418 /* Check to make sure that the label hasn't already been declared
2419 at this scope */
2420 if (b && B_IN_CURRENT_SCOPE (b))
2422 error ("duplicate label declaration %qE", name);
2423 locate_old_decl (b->decl, error);
2425 /* Just use the previous declaration. */
2426 return b->decl;
2429 label = make_label (name, input_location);
2430 C_DECLARED_LABEL_FLAG (label) = 1;
2432 /* Declared labels go in the current scope. */
2433 bind (name, label, current_scope,
2434 /*invisible=*/false, /*nested=*/false);
2435 return label;
2438 /* Define a label, specifying the location in the source file.
2439 Return the LABEL_DECL node for the label, if the definition is valid.
2440 Otherwise return 0. */
2442 tree
2443 define_label (location_t location, tree name)
2445 /* Find any preexisting label with this name. It is an error
2446 if that label has already been defined in this function, or
2447 if there is a containing function with a declared label with
2448 the same name. */
2449 tree label = I_LABEL_DECL (name);
2450 struct c_label_list *nlist_se, *nlist_vm;
2452 if (label
2453 && ((DECL_CONTEXT (label) == current_function_decl
2454 && DECL_INITIAL (label) != 0)
2455 || (DECL_CONTEXT (label) != current_function_decl
2456 && C_DECLARED_LABEL_FLAG (label))))
2458 error ("%Hduplicate label %qD", &location, label);
2459 locate_old_decl (label, error);
2460 return 0;
2462 else if (label && DECL_CONTEXT (label) == current_function_decl)
2464 /* The label has been used or declared already in this function,
2465 but not defined. Update its location to point to this
2466 definition. */
2467 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2468 error ("%Jjump into statement expression", label);
2469 if (C_DECL_UNDEFINABLE_VM (label))
2470 error ("%Jjump into scope of identifier with variably modified type",
2471 label);
2472 DECL_SOURCE_LOCATION (label) = location;
2474 else
2476 /* No label binding for that identifier; make one. */
2477 label = make_label (name, location);
2479 /* Ordinary labels go in the current function scope. */
2480 bind (name, label, current_function_scope,
2481 /*invisible=*/false, /*nested=*/false);
2484 if (warn_traditional && !in_system_header && lookup_name (name))
2485 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2486 "identifier %qE conflicts", &location, name);
2488 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2489 nlist_se->next = label_context_stack_se->labels_def;
2490 nlist_se->label = label;
2491 label_context_stack_se->labels_def = nlist_se;
2493 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2494 nlist_vm->next = label_context_stack_vm->labels_def;
2495 nlist_vm->label = label;
2496 label_context_stack_vm->labels_def = nlist_vm;
2498 /* Mark label as having been defined. */
2499 DECL_INITIAL (label) = error_mark_node;
2500 return label;
2503 /* Given NAME, an IDENTIFIER_NODE,
2504 return the structure (or union or enum) definition for that name.
2505 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2506 CODE says which kind of type the caller wants;
2507 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2508 If the wrong kind of type is found, an error is reported. */
2510 static tree
2511 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2513 struct c_binding *b = I_TAG_BINDING (name);
2514 int thislevel = 0;
2516 if (!b || !b->decl)
2517 return 0;
2519 /* We only care about whether it's in this level if
2520 thislevel_only was set or it might be a type clash. */
2521 if (thislevel_only || TREE_CODE (b->decl) != code)
2523 /* For our purposes, a tag in the external scope is the same as
2524 a tag in the file scope. (Primarily relevant to Objective-C
2525 and its builtin structure tags, which get pushed before the
2526 file scope is created.) */
2527 if (B_IN_CURRENT_SCOPE (b)
2528 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2529 thislevel = 1;
2532 if (thislevel_only && !thislevel)
2533 return 0;
2535 if (TREE_CODE (b->decl) != code)
2537 /* Definition isn't the kind we were looking for. */
2538 pending_invalid_xref = name;
2539 pending_invalid_xref_location = input_location;
2541 /* If in the same binding level as a declaration as a tag
2542 of a different type, this must not be allowed to
2543 shadow that tag, so give the error immediately.
2544 (For example, "struct foo; union foo;" is invalid.) */
2545 if (thislevel)
2546 pending_xref_error ();
2548 return b->decl;
2551 /* Print an error message now
2552 for a recent invalid struct, union or enum cross reference.
2553 We don't print them immediately because they are not invalid
2554 when used in the `struct foo;' construct for shadowing. */
2556 void
2557 pending_xref_error (void)
2559 if (pending_invalid_xref != 0)
2560 error ("%H%qE defined as wrong kind of tag",
2561 &pending_invalid_xref_location, pending_invalid_xref);
2562 pending_invalid_xref = 0;
2566 /* Look up NAME in the current scope and its superiors
2567 in the namespace of variables, functions and typedefs.
2568 Return a ..._DECL node of some kind representing its definition,
2569 or return 0 if it is undefined. */
2571 tree
2572 lookup_name (tree name)
2574 struct c_binding *b = I_SYMBOL_BINDING (name);
2575 if (b && !b->invisible)
2576 return b->decl;
2577 return 0;
2580 /* Similar to `lookup_name' but look only at the indicated scope. */
2582 static tree
2583 lookup_name_in_scope (tree name, struct c_scope *scope)
2585 struct c_binding *b;
2587 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2588 if (B_IN_SCOPE (b, scope))
2589 return b->decl;
2590 return 0;
2593 /* Create the predefined scalar types of C,
2594 and some nodes representing standard constants (0, 1, (void *) 0).
2595 Initialize the global scope.
2596 Make definitions for built-in primitive functions. */
2598 void
2599 c_init_decl_processing (void)
2601 location_t save_loc = input_location;
2603 /* Initialize reserved words for parser. */
2604 c_parse_init ();
2606 current_function_decl = 0;
2608 gcc_obstack_init (&parser_obstack);
2610 /* Make the externals scope. */
2611 push_scope ();
2612 external_scope = current_scope;
2614 /* Declarations from c_common_nodes_and_builtins must not be associated
2615 with this input file, lest we get differences between using and not
2616 using preprocessed headers. */
2617 #ifdef USE_MAPPED_LOCATION
2618 input_location = BUILTINS_LOCATION;
2619 #else
2620 input_location.file = "<built-in>";
2621 input_location.line = 0;
2622 #endif
2624 build_common_tree_nodes (flag_signed_char, false);
2626 c_common_nodes_and_builtins ();
2628 /* In C, comparisons and TRUTH_* expressions have type int. */
2629 truthvalue_type_node = integer_type_node;
2630 truthvalue_true_node = integer_one_node;
2631 truthvalue_false_node = integer_zero_node;
2633 /* Even in C99, which has a real boolean type. */
2634 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2635 boolean_type_node));
2637 input_location = save_loc;
2639 pedantic_lvalues = true;
2641 make_fname_decl = c_make_fname_decl;
2642 start_fname_decls ();
2645 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2646 decl, NAME is the initialization string and TYPE_DEP indicates whether
2647 NAME depended on the type of the function. As we don't yet implement
2648 delayed emission of static data, we mark the decl as emitted
2649 so it is not placed in the output. Anything using it must therefore pull
2650 out the STRING_CST initializer directly. FIXME. */
2652 static tree
2653 c_make_fname_decl (tree id, int type_dep)
2655 const char *name = fname_as_string (type_dep);
2656 tree decl, type, init;
2657 size_t length = strlen (name);
2659 type = build_array_type (char_type_node,
2660 build_index_type (size_int (length)));
2661 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2663 decl = build_decl (VAR_DECL, id, type);
2665 TREE_STATIC (decl) = 1;
2666 TREE_READONLY (decl) = 1;
2667 DECL_ARTIFICIAL (decl) = 1;
2669 init = build_string (length + 1, name);
2670 free ((char *) name);
2671 TREE_TYPE (init) = type;
2672 DECL_INITIAL (decl) = init;
2674 TREE_USED (decl) = 1;
2676 if (current_function_decl)
2678 DECL_CONTEXT (decl) = current_function_decl;
2679 bind (id, decl, current_function_scope,
2680 /*invisible=*/false, /*nested=*/false);
2683 finish_decl (decl, init, NULL_TREE);
2685 return decl;
2688 /* Return a definition for a builtin function named NAME and whose data type
2689 is TYPE. TYPE should be a function type with argument types.
2690 FUNCTION_CODE tells later passes how to compile calls to this function.
2691 See tree.h for its possible values.
2693 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2694 the name to be called if we can't opencode the function. If
2695 ATTRS is nonzero, use that for the function's attribute list. */
2697 tree
2698 builtin_function (const char *name, tree type, int function_code,
2699 enum built_in_class cl, const char *library_name,
2700 tree attrs)
2702 tree id = get_identifier (name);
2703 tree decl = build_decl (FUNCTION_DECL, id, type);
2704 TREE_PUBLIC (decl) = 1;
2705 DECL_EXTERNAL (decl) = 1;
2706 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2707 DECL_BUILT_IN_CLASS (decl) = cl;
2708 DECL_FUNCTION_CODE (decl) = function_code;
2709 if (library_name)
2710 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2712 /* Should never be called on a symbol with a preexisting meaning. */
2713 gcc_assert (!I_SYMBOL_BINDING (id));
2715 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2717 /* Builtins in the implementation namespace are made visible without
2718 needing to be explicitly declared. See push_file_scope. */
2719 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2721 TREE_CHAIN (decl) = visible_builtins;
2722 visible_builtins = decl;
2725 /* Possibly apply some default attributes to this built-in function. */
2726 if (attrs)
2727 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2728 else
2729 decl_attributes (&decl, NULL_TREE, 0);
2731 return decl;
2734 /* Called when a declaration is seen that contains no names to declare.
2735 If its type is a reference to a structure, union or enum inherited
2736 from a containing scope, shadow that tag name for the current scope
2737 with a forward reference.
2738 If its type defines a new named structure or union
2739 or defines an enum, it is valid but we need not do anything here.
2740 Otherwise, it is an error. */
2742 void
2743 shadow_tag (const struct c_declspecs *declspecs)
2745 shadow_tag_warned (declspecs, 0);
2748 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2749 but no pedwarn. */
2750 void
2751 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2753 bool found_tag = false;
2755 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2757 tree value = declspecs->type;
2758 enum tree_code code = TREE_CODE (value);
2760 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2761 /* Used to test also that TYPE_SIZE (value) != 0.
2762 That caused warning for `struct foo;' at top level in the file. */
2764 tree name = TYPE_NAME (value);
2765 tree t;
2767 found_tag = true;
2769 if (name == 0)
2771 if (warned != 1 && code != ENUMERAL_TYPE)
2772 /* Empty unnamed enum OK */
2774 pedwarn ("unnamed struct/union that defines no instances");
2775 warned = 1;
2778 else if (!declspecs->tag_defined_p
2779 && declspecs->storage_class != csc_none)
2781 if (warned != 1)
2782 pedwarn ("empty declaration with storage class specifier "
2783 "does not redeclare tag");
2784 warned = 1;
2785 pending_xref_error ();
2787 else if (!declspecs->tag_defined_p
2788 && (declspecs->const_p
2789 || declspecs->volatile_p
2790 || declspecs->restrict_p))
2792 if (warned != 1)
2793 pedwarn ("empty declaration with type qualifier "
2794 "does not redeclare tag");
2795 warned = 1;
2796 pending_xref_error ();
2798 else
2800 pending_invalid_xref = 0;
2801 t = lookup_tag (code, name, 1);
2803 if (t == 0)
2805 t = make_node (code);
2806 pushtag (name, t);
2810 else
2812 if (warned != 1 && !in_system_header)
2814 pedwarn ("useless type name in empty declaration");
2815 warned = 1;
2819 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2821 pedwarn ("useless type name in empty declaration");
2822 warned = 1;
2825 pending_invalid_xref = 0;
2827 if (declspecs->inline_p)
2829 error ("%<inline%> in empty declaration");
2830 warned = 1;
2833 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2835 error ("%<auto%> in file-scope empty declaration");
2836 warned = 1;
2839 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2841 error ("%<register%> in file-scope empty declaration");
2842 warned = 1;
2845 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2847 warning (0, "useless storage class specifier in empty declaration");
2848 warned = 2;
2851 if (!warned && !in_system_header && declspecs->thread_p)
2853 warning (0, "useless %<__thread%> in empty declaration");
2854 warned = 2;
2857 if (!warned && !in_system_header && (declspecs->const_p
2858 || declspecs->volatile_p
2859 || declspecs->restrict_p))
2861 warning (0, "useless type qualifier in empty declaration");
2862 warned = 2;
2865 if (warned != 1)
2867 if (!found_tag)
2868 pedwarn ("empty declaration");
2873 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2874 bits. SPECS represents declaration specifiers that the grammar
2875 only permits to contain type qualifiers and attributes. */
2878 quals_from_declspecs (const struct c_declspecs *specs)
2880 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2881 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2882 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2883 gcc_assert (!specs->type
2884 && !specs->decl_attr
2885 && specs->typespec_word == cts_none
2886 && specs->storage_class == csc_none
2887 && !specs->typedef_p
2888 && !specs->explicit_signed_p
2889 && !specs->deprecated_p
2890 && !specs->long_p
2891 && !specs->long_long_p
2892 && !specs->short_p
2893 && !specs->signed_p
2894 && !specs->unsigned_p
2895 && !specs->complex_p
2896 && !specs->inline_p
2897 && !specs->thread_p);
2898 return quals;
2901 /* Construct an array declarator. EXPR is the expression inside [], or
2902 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2903 to the pointer to which a parameter array is converted). STATIC_P is
2904 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2905 is true if the array is [*], a VLA of unspecified length which is
2906 nevertheless a complete type (not currently implemented by GCC),
2907 false otherwise. The field for the contained declarator is left to be
2908 filled in by set_array_declarator_inner. */
2910 struct c_declarator *
2911 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2912 bool vla_unspec_p)
2914 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2915 struct c_declarator);
2916 declarator->kind = cdk_array;
2917 declarator->declarator = 0;
2918 declarator->u.array.dimen = expr;
2919 if (quals)
2921 declarator->u.array.attrs = quals->attrs;
2922 declarator->u.array.quals = quals_from_declspecs (quals);
2924 else
2926 declarator->u.array.attrs = NULL_TREE;
2927 declarator->u.array.quals = 0;
2929 declarator->u.array.static_p = static_p;
2930 declarator->u.array.vla_unspec_p = vla_unspec_p;
2931 if (pedantic && !flag_isoc99)
2933 if (static_p || quals != NULL)
2934 pedwarn ("ISO C90 does not support %<static%> or type "
2935 "qualifiers in parameter array declarators");
2936 if (vla_unspec_p)
2937 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
2939 if (vla_unspec_p)
2940 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
2941 return declarator;
2944 /* Set the contained declarator of an array declarator. DECL is the
2945 declarator, as constructed by build_array_declarator; INNER is what
2946 appears on the left of the []. ABSTRACT_P is true if it is an
2947 abstract declarator, false otherwise; this is used to reject static
2948 and type qualifiers in abstract declarators, where they are not in
2949 the C99 grammar (subject to possible change in DR#289). */
2951 struct c_declarator *
2952 set_array_declarator_inner (struct c_declarator *decl,
2953 struct c_declarator *inner, bool abstract_p)
2955 decl->declarator = inner;
2956 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
2957 || decl->u.array.attrs != NULL_TREE
2958 || decl->u.array.static_p))
2959 error ("static or type qualifiers in abstract declarator");
2960 return decl;
2963 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2965 tree
2966 groktypename (struct c_type_name *type_name)
2968 tree type;
2969 tree attrs = type_name->specs->attrs;
2971 type_name->specs->attrs = NULL_TREE;
2973 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
2974 false, NULL);
2976 /* Apply attributes. */
2977 decl_attributes (&type, attrs, 0);
2979 return type;
2982 /* Decode a declarator in an ordinary declaration or data definition.
2983 This is called as soon as the type information and variable name
2984 have been parsed, before parsing the initializer if any.
2985 Here we create the ..._DECL node, fill in its type,
2986 and put it on the list of decls for the current context.
2987 The ..._DECL node is returned as the value.
2989 Exception: for arrays where the length is not specified,
2990 the type is left null, to be filled in by `finish_decl'.
2992 Function definitions do not come here; they go to start_function
2993 instead. However, external and forward declarations of functions
2994 do go through here. Structure field declarations are done by
2995 grokfield and not through here. */
2997 tree
2998 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
2999 bool initialized, tree attributes)
3001 tree decl;
3002 tree tem;
3004 /* An object declared as __attribute__((deprecated)) suppresses
3005 warnings of uses of other deprecated items. */
3006 if (lookup_attribute ("deprecated", attributes))
3007 deprecated_state = DEPRECATED_SUPPRESS;
3009 decl = grokdeclarator (declarator, declspecs,
3010 NORMAL, initialized, NULL);
3011 if (!decl)
3012 return 0;
3014 deprecated_state = DEPRECATED_NORMAL;
3016 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3017 && MAIN_NAME_P (DECL_NAME (decl)))
3018 warning (0, "%J%qD is usually a function", decl, decl);
3020 if (initialized)
3021 /* Is it valid for this decl to have an initializer at all?
3022 If not, set INITIALIZED to zero, which will indirectly
3023 tell 'finish_decl' to ignore the initializer once it is parsed. */
3024 switch (TREE_CODE (decl))
3026 case TYPE_DECL:
3027 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3028 initialized = 0;
3029 break;
3031 case FUNCTION_DECL:
3032 error ("function %qD is initialized like a variable", decl);
3033 initialized = 0;
3034 break;
3036 case PARM_DECL:
3037 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3038 error ("parameter %qD is initialized", decl);
3039 initialized = 0;
3040 break;
3042 default:
3043 /* Don't allow initializations for incomplete types except for
3044 arrays which might be completed by the initialization. */
3046 /* This can happen if the array size is an undefined macro.
3047 We already gave a warning, so we don't need another one. */
3048 if (TREE_TYPE (decl) == error_mark_node)
3049 initialized = 0;
3050 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3052 /* A complete type is ok if size is fixed. */
3054 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3055 || C_DECL_VARIABLE_SIZE (decl))
3057 error ("variable-sized object may not be initialized");
3058 initialized = 0;
3061 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3063 error ("variable %qD has initializer but incomplete type", decl);
3064 initialized = 0;
3066 else if (C_DECL_VARIABLE_SIZE (decl))
3068 /* Although C99 is unclear about whether incomplete arrays
3069 of VLAs themselves count as VLAs, it does not make
3070 sense to permit them to be initialized given that
3071 ordinary VLAs may not be initialized. */
3072 error ("variable-sized object may not be initialized");
3073 initialized = 0;
3077 if (initialized)
3079 if (current_scope == file_scope)
3080 TREE_STATIC (decl) = 1;
3082 /* Tell 'pushdecl' this is an initialized decl
3083 even though we don't yet have the initializer expression.
3084 Also tell 'finish_decl' it may store the real initializer. */
3085 DECL_INITIAL (decl) = error_mark_node;
3088 /* If this is a function declaration, write a record describing it to the
3089 prototypes file (if requested). */
3091 if (TREE_CODE (decl) == FUNCTION_DECL)
3092 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3094 /* ANSI specifies that a tentative definition which is not merged with
3095 a non-tentative definition behaves exactly like a definition with an
3096 initializer equal to zero. (Section 3.7.2)
3098 -fno-common gives strict ANSI behavior, though this tends to break
3099 a large body of code that grew up without this rule.
3101 Thread-local variables are never common, since there's no entrenched
3102 body of code to break, and it allows more efficient variable references
3103 in the presence of dynamic linking. */
3105 if (TREE_CODE (decl) == VAR_DECL
3106 && !initialized
3107 && TREE_PUBLIC (decl)
3108 && !DECL_THREAD_LOCAL (decl)
3109 && !flag_no_common)
3110 DECL_COMMON (decl) = 1;
3112 /* Set attributes here so if duplicate decl, will have proper attributes. */
3113 decl_attributes (&decl, attributes, 0);
3115 if (TREE_CODE (decl) == FUNCTION_DECL
3116 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3118 struct c_declarator *ce = declarator;
3120 if (ce->kind == cdk_pointer)
3121 ce = declarator->declarator;
3122 if (ce->kind == cdk_function)
3124 tree args = ce->u.arg_info->parms;
3125 for (; args; args = TREE_CHAIN (args))
3127 tree type = TREE_TYPE (args);
3128 if (type && INTEGRAL_TYPE_P (type)
3129 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3130 DECL_ARG_TYPE (args) = integer_type_node;
3135 if (TREE_CODE (decl) == FUNCTION_DECL
3136 && DECL_DECLARED_INLINE_P (decl)
3137 && DECL_UNINLINABLE (decl)
3138 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3139 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3141 /* Add this decl to the current scope.
3142 TEM may equal DECL or it may be a previous decl of the same name. */
3143 tem = pushdecl (decl);
3145 if (initialized && DECL_EXTERNAL (tem))
3147 DECL_EXTERNAL (tem) = 0;
3148 TREE_STATIC (tem) = 1;
3151 return tem;
3154 /* Finish processing of a declaration;
3155 install its initial value.
3156 If the length of an array type is not known before,
3157 it must be determined now, from the initial value, or it is an error. */
3159 void
3160 finish_decl (tree decl, tree init, tree asmspec_tree)
3162 tree type = TREE_TYPE (decl);
3163 int was_incomplete = (DECL_SIZE (decl) == 0);
3164 const char *asmspec = 0;
3166 /* If a name was specified, get the string. */
3167 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3168 && DECL_FILE_SCOPE_P (decl))
3169 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3170 if (asmspec_tree)
3171 asmspec = TREE_STRING_POINTER (asmspec_tree);
3173 /* If `start_decl' didn't like having an initialization, ignore it now. */
3174 if (init != 0 && DECL_INITIAL (decl) == 0)
3175 init = 0;
3177 /* Don't crash if parm is initialized. */
3178 if (TREE_CODE (decl) == PARM_DECL)
3179 init = 0;
3181 if (init)
3182 store_init_value (decl, init);
3184 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3185 || TREE_CODE (decl) == FUNCTION_DECL
3186 || TREE_CODE (decl) == FIELD_DECL))
3187 objc_check_decl (decl);
3189 /* Deduce size of array from initialization, if not already known. */
3190 if (TREE_CODE (type) == ARRAY_TYPE
3191 && TYPE_DOMAIN (type) == 0
3192 && TREE_CODE (decl) != TYPE_DECL)
3194 bool do_default
3195 = (TREE_STATIC (decl)
3196 /* Even if pedantic, an external linkage array
3197 may have incomplete type at first. */
3198 ? pedantic && !TREE_PUBLIC (decl)
3199 : !DECL_EXTERNAL (decl));
3200 int failure
3201 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3202 do_default);
3204 /* Get the completed type made by complete_array_type. */
3205 type = TREE_TYPE (decl);
3207 if (failure == 1)
3208 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3210 else if (failure == 2)
3212 if (do_default)
3213 error ("%Jarray size missing in %qD", decl, decl);
3214 /* If a `static' var's size isn't known,
3215 make it extern as well as static, so it does not get
3216 allocated.
3217 If it is not `static', then do not mark extern;
3218 finish_incomplete_decl will give it a default size
3219 and it will get allocated. */
3220 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3221 DECL_EXTERNAL (decl) = 1;
3223 else if (failure == 3)
3224 error ("%Jzero or negative size array %qD", decl, decl);
3226 if (DECL_INITIAL (decl))
3227 TREE_TYPE (DECL_INITIAL (decl)) = type;
3229 layout_decl (decl, 0);
3232 if (TREE_CODE (decl) == VAR_DECL)
3234 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3235 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3236 layout_decl (decl, 0);
3238 if (DECL_SIZE (decl) == 0
3239 /* Don't give an error if we already gave one earlier. */
3240 && TREE_TYPE (decl) != error_mark_node
3241 && (TREE_STATIC (decl)
3242 /* A static variable with an incomplete type
3243 is an error if it is initialized.
3244 Also if it is not file scope.
3245 Otherwise, let it through, but if it is not `extern'
3246 then it may cause an error message later. */
3247 ? (DECL_INITIAL (decl) != 0
3248 || !DECL_FILE_SCOPE_P (decl))
3249 /* An automatic variable with an incomplete type
3250 is an error. */
3251 : !DECL_EXTERNAL (decl)))
3253 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3254 TREE_TYPE (decl) = error_mark_node;
3257 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3258 && DECL_SIZE (decl) != 0)
3260 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3261 constant_expression_warning (DECL_SIZE (decl));
3262 else
3263 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3266 if (TREE_USED (type))
3267 TREE_USED (decl) = 1;
3270 /* If this is a function and an assembler name is specified, reset DECL_RTL
3271 so we can give it its new name. Also, update built_in_decls if it
3272 was a normal built-in. */
3273 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3275 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3276 set_builtin_user_assembler_name (decl, asmspec);
3277 set_user_assembler_name (decl, asmspec);
3280 /* If #pragma weak was used, mark the decl weak now. */
3281 maybe_apply_pragma_weak (decl);
3283 /* If this is a variable definition, determine its ELF visibility. */
3284 if (TREE_CODE (decl) == VAR_DECL
3285 && TREE_STATIC (decl)
3286 && !DECL_EXTERNAL (decl))
3287 c_determine_visibility (decl);
3289 /* Output the assembler code and/or RTL code for variables and functions,
3290 unless the type is an undefined structure or union.
3291 If not, it will get done when the type is completed. */
3293 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3295 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3296 if (c_dialect_objc ())
3297 objc_check_decl (decl);
3299 if (asmspec)
3301 /* If this is not a static variable, issue a warning.
3302 It doesn't make any sense to give an ASMSPEC for an
3303 ordinary, non-register local variable. Historically,
3304 GCC has accepted -- but ignored -- the ASMSPEC in
3305 this case. */
3306 if (!DECL_FILE_SCOPE_P (decl)
3307 && TREE_CODE (decl) == VAR_DECL
3308 && !C_DECL_REGISTER (decl)
3309 && !TREE_STATIC (decl))
3310 warning (0, "%Jignoring asm-specifier for non-static local "
3311 "variable %qD", decl, decl);
3312 else if (C_DECL_REGISTER (decl))
3313 change_decl_assembler_name (decl, get_identifier (asmspec));
3314 else
3315 set_user_assembler_name (decl, asmspec);
3318 if (DECL_FILE_SCOPE_P (decl))
3320 if (DECL_INITIAL (decl) == NULL_TREE
3321 || DECL_INITIAL (decl) == error_mark_node)
3322 /* Don't output anything
3323 when a tentative file-scope definition is seen.
3324 But at end of compilation, do output code for them. */
3325 DECL_DEFER_OUTPUT (decl) = 1;
3326 rest_of_decl_compilation (decl, true, 0);
3328 else
3330 /* In conjunction with an ASMSPEC, the `register'
3331 keyword indicates that we should place the variable
3332 in a particular register. */
3333 if (asmspec && C_DECL_REGISTER (decl))
3335 DECL_HARD_REGISTER (decl) = 1;
3336 /* This cannot be done for a structure with volatile
3337 fields, on which DECL_REGISTER will have been
3338 reset. */
3339 if (!DECL_REGISTER (decl))
3340 error ("cannot put object with volatile field into register");
3343 if (TREE_CODE (decl) != FUNCTION_DECL)
3345 /* If we're building a variable sized type, and we might be
3346 reachable other than via the top of the current binding
3347 level, then create a new BIND_EXPR so that we deallocate
3348 the object at the right time. */
3349 /* Note that DECL_SIZE can be null due to errors. */
3350 if (DECL_SIZE (decl)
3351 && !TREE_CONSTANT (DECL_SIZE (decl))
3352 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3354 tree bind;
3355 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3356 TREE_SIDE_EFFECTS (bind) = 1;
3357 add_stmt (bind);
3358 BIND_EXPR_BODY (bind) = push_stmt_list ();
3360 add_stmt (build_stmt (DECL_EXPR, decl));
3365 if (!DECL_FILE_SCOPE_P (decl))
3367 /* Recompute the RTL of a local array now
3368 if it used to be an incomplete type. */
3369 if (was_incomplete
3370 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3372 /* If we used it already as memory, it must stay in memory. */
3373 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3374 /* If it's still incomplete now, no init will save it. */
3375 if (DECL_SIZE (decl) == 0)
3376 DECL_INITIAL (decl) = 0;
3381 /* If this was marked 'used', be sure it will be output. */
3382 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3383 mark_decl_referenced (decl);
3385 if (TREE_CODE (decl) == TYPE_DECL)
3387 if (!DECL_FILE_SCOPE_P (decl)
3388 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3389 add_stmt (build_stmt (DECL_EXPR, decl));
3391 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3394 /* At the end of a declaration, throw away any variable type sizes
3395 of types defined inside that declaration. There is no use
3396 computing them in the following function definition. */
3397 if (current_scope == file_scope)
3398 get_pending_sizes ();
3400 /* Install a cleanup (aka destructor) if one was given. */
3401 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3403 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3404 if (attr)
3406 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3407 tree cleanup_decl = lookup_name (cleanup_id);
3408 tree cleanup;
3410 /* Build "cleanup(&decl)" for the destructor. */
3411 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3412 cleanup = build_tree_list (NULL_TREE, cleanup);
3413 cleanup = build_function_call (cleanup_decl, cleanup);
3415 /* Don't warn about decl unused; the cleanup uses it. */
3416 TREE_USED (decl) = 1;
3417 TREE_USED (cleanup_decl) = 1;
3419 /* Initialize EH, if we've been told to do so. */
3420 if (flag_exceptions && !c_eh_initialized_p)
3422 c_eh_initialized_p = true;
3423 eh_personality_libfunc
3424 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3425 ? "__gcc_personality_sj0"
3426 : "__gcc_personality_v0");
3427 using_eh_for_cleanups ();
3430 push_cleanup (decl, cleanup, false);
3435 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3437 tree
3438 grokparm (const struct c_parm *parm)
3440 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3441 NULL);
3443 decl_attributes (&decl, parm->attrs, 0);
3445 return decl;
3448 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3449 and push that on the current scope. */
3451 void
3452 push_parm_decl (const struct c_parm *parm)
3454 tree decl;
3456 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3457 decl_attributes (&decl, parm->attrs, 0);
3459 decl = pushdecl (decl);
3461 finish_decl (decl, NULL_TREE, NULL_TREE);
3464 /* Mark all the parameter declarations to date as forward decls.
3465 Also diagnose use of this extension. */
3467 void
3468 mark_forward_parm_decls (void)
3470 struct c_binding *b;
3472 if (pedantic && !current_scope->warned_forward_parm_decls)
3474 pedwarn ("ISO C forbids forward parameter declarations");
3475 current_scope->warned_forward_parm_decls = true;
3478 for (b = current_scope->bindings; b; b = b->prev)
3479 if (TREE_CODE (b->decl) == PARM_DECL)
3480 TREE_ASM_WRITTEN (b->decl) = 1;
3483 static GTY(()) int compound_literal_number;
3485 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3486 literal, which may be an incomplete array type completed by the
3487 initializer; INIT is a CONSTRUCTOR that initializes the compound
3488 literal. */
3490 tree
3491 build_compound_literal (tree type, tree init)
3493 /* We do not use start_decl here because we have a type, not a declarator;
3494 and do not use finish_decl because the decl should be stored inside
3495 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3496 tree decl;
3497 tree complit;
3498 tree stmt;
3500 if (type == error_mark_node)
3501 return error_mark_node;
3503 decl = build_decl (VAR_DECL, NULL_TREE, type);
3504 DECL_EXTERNAL (decl) = 0;
3505 TREE_PUBLIC (decl) = 0;
3506 TREE_STATIC (decl) = (current_scope == file_scope);
3507 DECL_CONTEXT (decl) = current_function_decl;
3508 TREE_USED (decl) = 1;
3509 TREE_TYPE (decl) = type;
3510 TREE_READONLY (decl) = TYPE_READONLY (type);
3511 store_init_value (decl, init);
3513 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3515 int failure = complete_array_type (&TREE_TYPE (decl),
3516 DECL_INITIAL (decl), true);
3517 gcc_assert (!failure);
3519 type = TREE_TYPE (decl);
3520 TREE_TYPE (DECL_INITIAL (decl)) = type;
3523 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3524 return error_mark_node;
3526 stmt = build_stmt (DECL_EXPR, decl);
3527 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3528 TREE_SIDE_EFFECTS (complit) = 1;
3530 layout_decl (decl, 0);
3532 if (TREE_STATIC (decl))
3534 /* This decl needs a name for the assembler output. We also need
3535 a unique suffix to be added to the name. */
3536 char *name;
3538 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3539 compound_literal_number);
3540 compound_literal_number++;
3541 DECL_NAME (decl) = get_identifier (name);
3542 DECL_DEFER_OUTPUT (decl) = 1;
3543 DECL_COMDAT (decl) = 1;
3544 DECL_ARTIFICIAL (decl) = 1;
3545 DECL_IGNORED_P (decl) = 1;
3546 pushdecl (decl);
3547 rest_of_decl_compilation (decl, 1, 0);
3550 return complit;
3553 /* Determine whether TYPE is a structure with a flexible array member,
3554 or a union containing such a structure (possibly recursively). */
3556 static bool
3557 flexible_array_type_p (tree type)
3559 tree x;
3560 switch (TREE_CODE (type))
3562 case RECORD_TYPE:
3563 x = TYPE_FIELDS (type);
3564 if (x == NULL_TREE)
3565 return false;
3566 while (TREE_CHAIN (x) != NULL_TREE)
3567 x = TREE_CHAIN (x);
3568 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3569 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3570 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3571 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3572 return true;
3573 return false;
3574 case UNION_TYPE:
3575 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3577 if (flexible_array_type_p (TREE_TYPE (x)))
3578 return true;
3580 return false;
3581 default:
3582 return false;
3586 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3587 replacing with appropriate values if they are invalid. */
3588 static void
3589 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3591 tree type_mv;
3592 unsigned int max_width;
3593 unsigned HOST_WIDE_INT w;
3594 const char *name = orig_name ? orig_name: _("<anonymous>");
3596 /* Detect and ignore out of range field width and process valid
3597 field widths. */
3598 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3599 || TREE_CODE (*width) != INTEGER_CST)
3601 error ("bit-field %qs width not an integer constant", name);
3602 *width = integer_one_node;
3604 else
3606 constant_expression_warning (*width);
3607 if (tree_int_cst_sgn (*width) < 0)
3609 error ("negative width in bit-field %qs", name);
3610 *width = integer_one_node;
3612 else if (integer_zerop (*width) && orig_name)
3614 error ("zero width for bit-field %qs", name);
3615 *width = integer_one_node;
3619 /* Detect invalid bit-field type. */
3620 if (TREE_CODE (*type) != INTEGER_TYPE
3621 && TREE_CODE (*type) != BOOLEAN_TYPE
3622 && TREE_CODE (*type) != ENUMERAL_TYPE)
3624 error ("bit-field %qs has invalid type", name);
3625 *type = unsigned_type_node;
3628 type_mv = TYPE_MAIN_VARIANT (*type);
3629 if (pedantic
3630 && type_mv != integer_type_node
3631 && type_mv != unsigned_type_node
3632 && type_mv != boolean_type_node)
3633 pedwarn ("type of bit-field %qs is a GCC extension", name);
3635 if (type_mv == boolean_type_node)
3636 max_width = CHAR_TYPE_SIZE;
3637 else
3638 max_width = TYPE_PRECISION (*type);
3640 if (0 < compare_tree_int (*width, max_width))
3642 error ("width of %qs exceeds its type", name);
3643 w = max_width;
3644 *width = build_int_cst (NULL_TREE, w);
3646 else
3647 w = tree_low_cst (*width, 1);
3649 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3651 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3652 if (!lt
3653 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3654 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3655 warning (0, "%qs is narrower than values of its type", name);
3659 /* Given declspecs and a declarator,
3660 determine the name and type of the object declared
3661 and construct a ..._DECL node for it.
3662 (In one case we can return a ..._TYPE node instead.
3663 For invalid input we sometimes return 0.)
3665 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3667 DECL_CONTEXT says which syntactic context this declaration is in:
3668 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3669 FUNCDEF for a function definition. Like NORMAL but a few different
3670 error messages in each case. Return value may be zero meaning
3671 this definition is too screwy to try to parse.
3672 PARM for a parameter declaration (either within a function prototype
3673 or before a function body). Make a PARM_DECL, or return void_type_node.
3674 TYPENAME if for a typename (in a cast or sizeof).
3675 Don't make a DECL node; just return the ..._TYPE node.
3676 FIELD for a struct or union field; make a FIELD_DECL.
3677 INITIALIZED is true if the decl has an initializer.
3678 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3679 representing the width of the bit-field.
3681 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3682 It may also be so in the PARM case, for a prototype where the
3683 argument type is specified but not the name.
3685 This function is where the complicated C meanings of `static'
3686 and `extern' are interpreted. */
3688 static tree
3689 grokdeclarator (const struct c_declarator *declarator,
3690 struct c_declspecs *declspecs,
3691 enum decl_context decl_context, bool initialized, tree *width)
3693 tree type = declspecs->type;
3694 bool threadp = declspecs->thread_p;
3695 enum c_storage_class storage_class = declspecs->storage_class;
3696 int constp;
3697 int restrictp;
3698 int volatilep;
3699 int type_quals = TYPE_UNQUALIFIED;
3700 const char *name, *orig_name;
3701 tree typedef_type = 0;
3702 int funcdef_flag = 0;
3703 bool funcdef_syntax = false;
3704 int size_varies = 0;
3705 tree decl_attr = declspecs->decl_attr;
3706 int array_ptr_quals = TYPE_UNQUALIFIED;
3707 tree array_ptr_attrs = NULL_TREE;
3708 int array_parm_static = 0;
3709 tree returned_attrs = NULL_TREE;
3710 bool bitfield = width != NULL;
3711 tree element_type;
3712 struct c_arg_info *arg_info = 0;
3714 if (decl_context == FUNCDEF)
3715 funcdef_flag = 1, decl_context = NORMAL;
3717 /* Look inside a declarator for the name being declared
3718 and get it as a string, for an error message. */
3720 const struct c_declarator *decl = declarator;
3721 name = 0;
3723 while (decl)
3724 switch (decl->kind)
3726 case cdk_function:
3727 case cdk_array:
3728 case cdk_pointer:
3729 funcdef_syntax = (decl->kind == cdk_function);
3730 decl = decl->declarator;
3731 break;
3733 case cdk_attrs:
3734 decl = decl->declarator;
3735 break;
3737 case cdk_id:
3738 if (decl->u.id)
3739 name = IDENTIFIER_POINTER (decl->u.id);
3740 decl = 0;
3741 break;
3743 default:
3744 gcc_unreachable ();
3746 orig_name = name;
3747 if (name == 0)
3748 name = "type name";
3751 /* A function definition's declarator must have the form of
3752 a function declarator. */
3754 if (funcdef_flag && !funcdef_syntax)
3755 return 0;
3757 /* If this looks like a function definition, make it one,
3758 even if it occurs where parms are expected.
3759 Then store_parm_decls will reject it and not use it as a parm. */
3760 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3761 decl_context = PARM;
3763 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3764 warn_deprecated_use (declspecs->type);
3766 typedef_type = type;
3767 size_varies = C_TYPE_VARIABLE_SIZE (type);
3769 /* Diagnose defaulting to "int". */
3771 if (declspecs->default_int_p && !in_system_header)
3773 /* Issue a warning if this is an ISO C 99 program or if
3774 -Wreturn-type and this is a function, or if -Wimplicit;
3775 prefer the former warning since it is more explicit. */
3776 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3777 && funcdef_flag)
3778 warn_about_return_type = 1;
3779 else if (warn_implicit_int || flag_isoc99)
3780 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3783 /* Adjust the type if a bit-field is being declared,
3784 -funsigned-bitfields applied and the type is not explicitly
3785 "signed". */
3786 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3787 && TREE_CODE (type) == INTEGER_TYPE)
3788 type = c_common_unsigned_type (type);
3790 /* Figure out the type qualifiers for the declaration. There are
3791 two ways a declaration can become qualified. One is something
3792 like `const int i' where the `const' is explicit. Another is
3793 something like `typedef const int CI; CI i' where the type of the
3794 declaration contains the `const'. A third possibility is that
3795 there is a type qualifier on the element type of a typedefed
3796 array type, in which case we should extract that qualifier so
3797 that c_apply_type_quals_to_decls receives the full list of
3798 qualifiers to work with (C90 is not entirely clear about whether
3799 duplicate qualifiers should be diagnosed in this case, but it
3800 seems most appropriate to do so). */
3801 element_type = strip_array_types (type);
3802 constp = declspecs->const_p + TYPE_READONLY (element_type);
3803 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3804 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3805 if (pedantic && !flag_isoc99)
3807 if (constp > 1)
3808 pedwarn ("duplicate %<const%>");
3809 if (restrictp > 1)
3810 pedwarn ("duplicate %<restrict%>");
3811 if (volatilep > 1)
3812 pedwarn ("duplicate %<volatile%>");
3814 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3815 type = TYPE_MAIN_VARIANT (type);
3816 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3817 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3818 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3820 /* Warn about storage classes that are invalid for certain
3821 kinds of declarations (parameters, typenames, etc.). */
3823 if (funcdef_flag
3824 && (threadp
3825 || storage_class == csc_auto
3826 || storage_class == csc_register
3827 || storage_class == csc_typedef))
3829 if (storage_class == csc_auto
3830 && (pedantic || current_scope == file_scope))
3831 pedwarn ("function definition declared %<auto%>");
3832 if (storage_class == csc_register)
3833 error ("function definition declared %<register%>");
3834 if (storage_class == csc_typedef)
3835 error ("function definition declared %<typedef%>");
3836 if (threadp)
3837 error ("function definition declared %<__thread%>");
3838 threadp = false;
3839 if (storage_class == csc_auto
3840 || storage_class == csc_register
3841 || storage_class == csc_typedef)
3842 storage_class = csc_none;
3844 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3846 if (decl_context == PARM && storage_class == csc_register)
3848 else
3850 switch (decl_context)
3852 case FIELD:
3853 error ("storage class specified for structure field %qs",
3854 name);
3855 break;
3856 case PARM:
3857 error ("storage class specified for parameter %qs", name);
3858 break;
3859 default:
3860 error ("storage class specified for typename");
3861 break;
3863 storage_class = csc_none;
3864 threadp = false;
3867 else if (storage_class == csc_extern
3868 && initialized
3869 && !funcdef_flag)
3871 /* 'extern' with initialization is invalid if not at file scope. */
3872 if (current_scope == file_scope)
3873 warning (0, "%qs initialized and declared %<extern%>", name);
3874 else
3875 error ("%qs has both %<extern%> and initializer", name);
3877 else if (current_scope == file_scope)
3879 if (storage_class == csc_auto)
3880 error ("file-scope declaration of %qs specifies %<auto%>", name);
3881 if (pedantic && storage_class == csc_register)
3882 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3884 else
3886 if (storage_class == csc_extern && funcdef_flag)
3887 error ("nested function %qs declared %<extern%>", name);
3888 else if (threadp && storage_class == csc_none)
3890 error ("function-scope %qs implicitly auto and declared "
3891 "%<__thread%>",
3892 name);
3893 threadp = false;
3897 /* Now figure out the structure of the declarator proper.
3898 Descend through it, creating more complex types, until we reach
3899 the declared identifier (or NULL_TREE, in an absolute declarator).
3900 At each stage we maintain an unqualified version of the type
3901 together with any qualifiers that should be applied to it with
3902 c_build_qualified_type; this way, array types including
3903 multidimensional array types are first built up in unqualified
3904 form and then the qualified form is created with
3905 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3907 while (declarator && declarator->kind != cdk_id)
3909 if (type == error_mark_node)
3911 declarator = declarator->declarator;
3912 continue;
3915 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
3916 a cdk_pointer (for *...),
3917 a cdk_function (for ...(...)),
3918 a cdk_attrs (for nested attributes),
3919 or a cdk_id (for the name being declared
3920 or the place in an absolute declarator
3921 where the name was omitted).
3922 For the last case, we have just exited the loop.
3924 At this point, TYPE is the type of elements of an array,
3925 or for a function to return, or for a pointer to point to.
3926 After this sequence of ifs, TYPE is the type of the
3927 array or function or pointer, and DECLARATOR has had its
3928 outermost layer removed. */
3930 if (array_ptr_quals != TYPE_UNQUALIFIED
3931 || array_ptr_attrs != NULL_TREE
3932 || array_parm_static)
3934 /* Only the innermost declarator (making a parameter be of
3935 array type which is converted to pointer type)
3936 may have static or type qualifiers. */
3937 error ("static or type qualifiers in non-parameter array declarator");
3938 array_ptr_quals = TYPE_UNQUALIFIED;
3939 array_ptr_attrs = NULL_TREE;
3940 array_parm_static = 0;
3943 switch (declarator->kind)
3945 case cdk_attrs:
3947 /* A declarator with embedded attributes. */
3948 tree attrs = declarator->u.attrs;
3949 const struct c_declarator *inner_decl;
3950 int attr_flags = 0;
3951 declarator = declarator->declarator;
3952 inner_decl = declarator;
3953 while (inner_decl->kind == cdk_attrs)
3954 inner_decl = inner_decl->declarator;
3955 if (inner_decl->kind == cdk_id)
3956 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3957 else if (inner_decl->kind == cdk_function)
3958 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3959 else if (inner_decl->kind == cdk_array)
3960 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3961 returned_attrs = decl_attributes (&type,
3962 chainon (returned_attrs, attrs),
3963 attr_flags);
3964 break;
3966 case cdk_array:
3968 tree itype = NULL_TREE;
3969 tree size = declarator->u.array.dimen;
3970 /* The index is a signed object `sizetype' bits wide. */
3971 tree index_type = c_common_signed_type (sizetype);
3973 array_ptr_quals = declarator->u.array.quals;
3974 array_ptr_attrs = declarator->u.array.attrs;
3975 array_parm_static = declarator->u.array.static_p;
3977 declarator = declarator->declarator;
3979 /* Check for some types that there cannot be arrays of. */
3981 if (VOID_TYPE_P (type))
3983 error ("declaration of %qs as array of voids", name);
3984 type = error_mark_node;
3987 if (TREE_CODE (type) == FUNCTION_TYPE)
3989 error ("declaration of %qs as array of functions", name);
3990 type = error_mark_node;
3993 if (pedantic && !in_system_header && flexible_array_type_p (type))
3994 pedwarn ("invalid use of structure with flexible array member");
3996 if (size == error_mark_node)
3997 type = error_mark_node;
3999 if (type == error_mark_node)
4000 continue;
4002 /* If size was specified, set ITYPE to a range-type for
4003 that size. Otherwise, ITYPE remains null. finish_decl
4004 may figure it out from an initial value. */
4006 if (size)
4008 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4009 lvalue. */
4010 STRIP_TYPE_NOPS (size);
4012 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4014 error ("size of array %qs has non-integer type", name);
4015 size = integer_one_node;
4018 if (pedantic && integer_zerop (size))
4019 pedwarn ("ISO C forbids zero-size array %qs", name);
4021 if (TREE_CODE (size) == INTEGER_CST)
4023 constant_expression_warning (size);
4024 if (tree_int_cst_sgn (size) < 0)
4026 error ("size of array %qs is negative", name);
4027 size = integer_one_node;
4030 else
4032 /* Make sure the array size remains visibly
4033 nonconstant even if it is (eg) a const variable
4034 with known value. */
4035 size_varies = 1;
4037 if (!flag_isoc99 && pedantic)
4039 if (TREE_CONSTANT (size))
4040 pedwarn ("ISO C90 forbids array %qs whose size "
4041 "can%'t be evaluated",
4042 name);
4043 else
4044 pedwarn ("ISO C90 forbids variable-size array %qs",
4045 name);
4049 if (integer_zerop (size))
4051 /* A zero-length array cannot be represented with
4052 an unsigned index type, which is what we'll
4053 get with build_index_type. Create an
4054 open-ended range instead. */
4055 itype = build_range_type (sizetype, size, NULL_TREE);
4057 else
4059 /* Arrange for the SAVE_EXPR on the inside of the
4060 MINUS_EXPR, which allows the -1 to get folded
4061 with the +1 that happens when building TYPE_SIZE. */
4062 if (size_varies)
4063 size = variable_size (size);
4065 /* Compute the maximum valid index, that is, size
4066 - 1. Do the calculation in index_type, so that
4067 if it is a variable the computations will be
4068 done in the proper mode. */
4069 itype = fold (build2 (MINUS_EXPR, index_type,
4070 convert (index_type, size),
4071 convert (index_type,
4072 size_one_node)));
4074 /* If that overflowed, the array is too big. ???
4075 While a size of INT_MAX+1 technically shouldn't
4076 cause an overflow (because we subtract 1), the
4077 overflow is recorded during the conversion to
4078 index_type, before the subtraction. Handling
4079 this case seems like an unnecessary
4080 complication. */
4081 if (TREE_OVERFLOW (itype))
4083 error ("size of array %qs is too large", name);
4084 type = error_mark_node;
4085 continue;
4088 itype = build_index_type (itype);
4091 else if (decl_context == FIELD)
4093 if (pedantic && !flag_isoc99 && !in_system_header)
4094 pedwarn ("ISO C90 does not support flexible array members");
4096 /* ISO C99 Flexible array members are effectively
4097 identical to GCC's zero-length array extension. */
4098 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4101 /* Complain about arrays of incomplete types. */
4102 if (!COMPLETE_TYPE_P (type))
4104 error ("array type has incomplete element type");
4105 type = error_mark_node;
4107 else
4108 type = build_array_type (type, itype);
4110 if (size_varies)
4111 C_TYPE_VARIABLE_SIZE (type) = 1;
4113 /* The GCC extension for zero-length arrays differs from
4114 ISO flexible array members in that sizeof yields
4115 zero. */
4116 if (size && integer_zerop (size))
4118 TYPE_SIZE (type) = bitsize_zero_node;
4119 TYPE_SIZE_UNIT (type) = size_zero_node;
4122 if (decl_context != PARM
4123 && (array_ptr_quals != TYPE_UNQUALIFIED
4124 || array_ptr_attrs != NULL_TREE
4125 || array_parm_static))
4127 error ("static or type qualifiers in non-parameter array declarator");
4128 array_ptr_quals = TYPE_UNQUALIFIED;
4129 array_ptr_attrs = NULL_TREE;
4130 array_parm_static = 0;
4132 break;
4134 case cdk_function:
4136 /* Say it's a definition only for the declarator closest
4137 to the identifier, apart possibly from some
4138 attributes. */
4139 bool really_funcdef = false;
4140 tree arg_types;
4141 if (funcdef_flag)
4143 const struct c_declarator *t = declarator->declarator;
4144 while (t->kind == cdk_attrs)
4145 t = t->declarator;
4146 really_funcdef = (t->kind == cdk_id);
4149 /* Declaring a function type. Make sure we have a valid
4150 type for the function to return. */
4151 if (type == error_mark_node)
4152 continue;
4154 size_varies = 0;
4156 /* Warn about some types functions can't return. */
4157 if (TREE_CODE (type) == FUNCTION_TYPE)
4159 error ("%qs declared as function returning a function", name);
4160 type = integer_type_node;
4162 if (TREE_CODE (type) == ARRAY_TYPE)
4164 error ("%qs declared as function returning an array", name);
4165 type = integer_type_node;
4168 /* Construct the function type and go to the next
4169 inner layer of declarator. */
4170 arg_info = declarator->u.arg_info;
4171 arg_types = grokparms (arg_info, really_funcdef);
4173 /* Type qualifiers before the return type of the function
4174 qualify the return type, not the function type. */
4175 if (type_quals)
4177 /* Type qualifiers on a function return type are
4178 normally permitted by the standard but have no
4179 effect, so give a warning at -Wreturn-type.
4180 Qualifiers on a void return type are banned on
4181 function definitions in ISO C; GCC used to used
4182 them for noreturn functions. */
4183 if (VOID_TYPE_P (type) && really_funcdef)
4184 pedwarn ("function definition has qualified void return type");
4185 else if (warn_return_type)
4186 warning (0, "type qualifiers ignored on function return type");
4188 type = c_build_qualified_type (type, type_quals);
4190 type_quals = TYPE_UNQUALIFIED;
4192 type = build_function_type (type, arg_types);
4193 declarator = declarator->declarator;
4195 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4196 the formal parameter list of this FUNCTION_TYPE to point to
4197 the FUNCTION_TYPE node itself. */
4199 tree link;
4201 for (link = arg_info->tags;
4202 link;
4203 link = TREE_CHAIN (link))
4204 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4206 break;
4208 case cdk_pointer:
4210 /* Merge any constancy or volatility into the target type
4211 for the pointer. */
4213 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4214 && type_quals)
4215 pedwarn ("ISO C forbids qualified function types");
4216 if (type_quals)
4217 type = c_build_qualified_type (type, type_quals);
4218 size_varies = 0;
4220 type = build_pointer_type (type);
4222 /* Process type qualifiers (such as const or volatile)
4223 that were given inside the `*'. */
4224 type_quals = declarator->u.pointer_quals;
4226 declarator = declarator->declarator;
4227 break;
4229 default:
4230 gcc_unreachable ();
4234 /* Now TYPE has the actual type, apart from any qualifiers in
4235 TYPE_QUALS. */
4237 /* Check the type and width of a bit-field. */
4238 if (bitfield)
4239 check_bitfield_type_and_width (&type, width, orig_name);
4241 /* Did array size calculations overflow? */
4243 if (TREE_CODE (type) == ARRAY_TYPE
4244 && COMPLETE_TYPE_P (type)
4245 && TREE_OVERFLOW (TYPE_SIZE (type)))
4247 error ("size of array %qs is too large", name);
4248 /* If we proceed with the array type as it is, we'll eventually
4249 crash in tree_low_cst(). */
4250 type = error_mark_node;
4253 /* If this is declaring a typedef name, return a TYPE_DECL. */
4255 if (storage_class == csc_typedef)
4257 tree decl;
4258 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4259 && type_quals)
4260 pedwarn ("ISO C forbids qualified function types");
4261 if (type_quals)
4262 type = c_build_qualified_type (type, type_quals);
4263 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4264 if (declspecs->explicit_signed_p)
4265 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4266 decl_attributes (&decl, returned_attrs, 0);
4267 if (declspecs->inline_p)
4268 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4269 return decl;
4272 /* Detect the case of an array type of unspecified size
4273 which came, as such, direct from a typedef name.
4274 We must copy the type, so that each identifier gets
4275 a distinct type, so that each identifier's size can be
4276 controlled separately by its own initializer. */
4278 if (type != 0 && typedef_type != 0
4279 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4280 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4282 type = build_array_type (TREE_TYPE (type), 0);
4283 if (size_varies)
4284 C_TYPE_VARIABLE_SIZE (type) = 1;
4287 /* If this is a type name (such as, in a cast or sizeof),
4288 compute the type and return it now. */
4290 if (decl_context == TYPENAME)
4292 /* Note that the grammar rejects storage classes in typenames
4293 and fields. */
4294 gcc_assert (storage_class == csc_none && !threadp
4295 && !declspecs->inline_p);
4296 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4297 && type_quals)
4298 pedwarn ("ISO C forbids const or volatile function types");
4299 if (type_quals)
4300 type = c_build_qualified_type (type, type_quals);
4301 decl_attributes (&type, returned_attrs, 0);
4302 return type;
4305 /* Aside from typedefs and type names (handle above),
4306 `void' at top level (not within pointer)
4307 is allowed only in public variables.
4308 We don't complain about parms either, but that is because
4309 a better error message can be made later. */
4311 if (VOID_TYPE_P (type) && decl_context != PARM
4312 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4313 && (storage_class == csc_extern
4314 || (current_scope == file_scope
4315 && !(storage_class == csc_static
4316 || storage_class == csc_register)))))
4318 error ("variable or field %qs declared void", name);
4319 type = integer_type_node;
4322 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4323 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4326 tree decl;
4328 if (decl_context == PARM)
4330 tree type_as_written;
4331 tree promoted_type;
4333 /* A parameter declared as an array of T is really a pointer to T.
4334 One declared as a function is really a pointer to a function. */
4336 if (TREE_CODE (type) == ARRAY_TYPE)
4338 /* Transfer const-ness of array into that of type pointed to. */
4339 type = TREE_TYPE (type);
4340 if (type_quals)
4341 type = c_build_qualified_type (type, type_quals);
4342 type = build_pointer_type (type);
4343 type_quals = array_ptr_quals;
4345 /* We don't yet implement attributes in this context. */
4346 if (array_ptr_attrs != NULL_TREE)
4347 warning (0, "attributes in parameter array declarator ignored");
4349 size_varies = 0;
4351 else if (TREE_CODE (type) == FUNCTION_TYPE)
4353 if (pedantic && type_quals)
4354 pedwarn ("ISO C forbids qualified function types");
4355 if (type_quals)
4356 type = c_build_qualified_type (type, type_quals);
4357 type = build_pointer_type (type);
4358 type_quals = TYPE_UNQUALIFIED;
4360 else if (type_quals)
4361 type = c_build_qualified_type (type, type_quals);
4363 type_as_written = type;
4365 decl = build_decl (PARM_DECL, declarator->u.id, type);
4366 if (size_varies)
4367 C_DECL_VARIABLE_SIZE (decl) = 1;
4369 /* Compute the type actually passed in the parmlist,
4370 for the case where there is no prototype.
4371 (For example, shorts and chars are passed as ints.)
4372 When there is a prototype, this is overridden later. */
4374 if (type == error_mark_node)
4375 promoted_type = type;
4376 else
4377 promoted_type = c_type_promotes_to (type);
4379 DECL_ARG_TYPE (decl) = promoted_type;
4380 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4381 if (declspecs->inline_p)
4382 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4384 else if (decl_context == FIELD)
4386 /* Note that the grammar rejects storage classes in typenames
4387 and fields. */
4388 gcc_assert (storage_class == csc_none && !threadp
4389 && !declspecs->inline_p);
4391 /* Structure field. It may not be a function. */
4393 if (TREE_CODE (type) == FUNCTION_TYPE)
4395 error ("field %qs declared as a function", name);
4396 type = build_pointer_type (type);
4398 else if (TREE_CODE (type) != ERROR_MARK
4399 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4401 error ("field %qs has incomplete type", name);
4402 type = error_mark_node;
4404 type = c_build_qualified_type (type, type_quals);
4405 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4406 DECL_NONADDRESSABLE_P (decl) = bitfield;
4408 if (size_varies)
4409 C_DECL_VARIABLE_SIZE (decl) = 1;
4411 else if (TREE_CODE (type) == FUNCTION_TYPE)
4413 if (storage_class == csc_register || threadp)
4415 error ("invalid storage class for function %qs", name);
4417 else if (current_scope != file_scope)
4419 /* Function declaration not at file scope. Storage
4420 classes other than `extern' are not allowed, C99
4421 6.7.1p5, and `extern' makes no difference. However,
4422 GCC allows 'auto', perhaps with 'inline', to support
4423 nested functions. */
4424 if (storage_class == csc_auto)
4426 if (pedantic)
4427 pedwarn ("invalid storage class for function %qs", name);
4429 else if (storage_class == csc_static)
4431 error ("invalid storage class for function %qs", name);
4432 if (funcdef_flag)
4433 storage_class = declspecs->storage_class = csc_none;
4434 else
4435 return 0;
4439 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4440 decl = build_decl_attribute_variant (decl, decl_attr);
4442 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4444 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4445 pedwarn ("ISO C forbids qualified function types");
4447 /* GNU C interprets a volatile-qualified function type to indicate
4448 that the function does not return. */
4449 if ((type_quals & TYPE_QUAL_VOLATILE)
4450 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4451 warning (0, "%<noreturn%> function returns non-void value");
4453 /* Every function declaration is an external reference
4454 (DECL_EXTERNAL) except for those which are not at file
4455 scope and are explicitly declared "auto". This is
4456 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4457 GCC to signify a forward declaration of a nested function. */
4458 if (storage_class == csc_auto && current_scope != file_scope)
4459 DECL_EXTERNAL (decl) = 0;
4460 else
4461 DECL_EXTERNAL (decl) = 1;
4463 /* Record absence of global scope for `static' or `auto'. */
4464 TREE_PUBLIC (decl)
4465 = !(storage_class == csc_static || storage_class == csc_auto);
4467 /* For a function definition, record the argument information
4468 block where store_parm_decls will look for it. */
4469 if (funcdef_flag)
4470 current_function_arg_info = arg_info;
4472 if (declspecs->default_int_p)
4473 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4475 /* Record presence of `inline', if it is reasonable. */
4476 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4478 if (declspecs->inline_p)
4479 pedwarn ("cannot inline function %<main%>");
4481 else if (declspecs->inline_p)
4483 /* Record that the function is declared `inline'. */
4484 DECL_DECLARED_INLINE_P (decl) = 1;
4486 /* Do not mark bare declarations as DECL_INLINE. Doing so
4487 in the presence of multiple declarations can result in
4488 the abstract origin pointing between the declarations,
4489 which will confuse dwarf2out. */
4490 if (initialized)
4492 DECL_INLINE (decl) = 1;
4493 if (storage_class == csc_extern)
4494 current_extern_inline = 1;
4497 /* If -finline-functions, assume it can be inlined. This does
4498 two things: let the function be deferred until it is actually
4499 needed, and let dwarf2 know that the function is inlinable. */
4500 else if (flag_inline_trees == 2 && initialized)
4501 DECL_INLINE (decl) = 1;
4503 else
4505 /* It's a variable. */
4506 /* An uninitialized decl with `extern' is a reference. */
4507 int extern_ref = !initialized && storage_class == csc_extern;
4509 type = c_build_qualified_type (type, type_quals);
4511 /* C99 6.2.2p7: It is invalid (compile-time undefined
4512 behavior) to create an 'extern' declaration for a
4513 variable if there is a global declaration that is
4514 'static' and the global declaration is not visible.
4515 (If the static declaration _is_ currently visible,
4516 the 'extern' declaration is taken to refer to that decl.) */
4517 if (extern_ref && current_scope != file_scope)
4519 tree global_decl = identifier_global_value (declarator->u.id);
4520 tree visible_decl = lookup_name (declarator->u.id);
4522 if (global_decl
4523 && global_decl != visible_decl
4524 && TREE_CODE (global_decl) == VAR_DECL
4525 && !TREE_PUBLIC (global_decl))
4526 error ("variable previously declared %<static%> redeclared "
4527 "%<extern%>");
4530 decl = build_decl (VAR_DECL, declarator->u.id, type);
4531 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4532 if (size_varies)
4533 C_DECL_VARIABLE_SIZE (decl) = 1;
4535 if (declspecs->inline_p)
4536 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4538 /* At file scope, an initialized extern declaration may follow
4539 a static declaration. In that case, DECL_EXTERNAL will be
4540 reset later in start_decl. */
4541 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4543 /* At file scope, the presence of a `static' or `register' storage
4544 class specifier, or the absence of all storage class specifiers
4545 makes this declaration a definition (perhaps tentative). Also,
4546 the absence of both `static' and `register' makes it public. */
4547 if (current_scope == file_scope)
4549 TREE_PUBLIC (decl) = !(storage_class == csc_static
4550 || storage_class == csc_register);
4551 TREE_STATIC (decl) = !extern_ref;
4553 /* Not at file scope, only `static' makes a static definition. */
4554 else
4556 TREE_STATIC (decl) = (storage_class == csc_static);
4557 TREE_PUBLIC (decl) = extern_ref;
4560 if (threadp)
4562 if (targetm.have_tls)
4563 DECL_THREAD_LOCAL (decl) = 1;
4564 else
4565 /* A mere warning is sure to result in improper semantics
4566 at runtime. Don't bother to allow this to compile. */
4567 error ("thread-local storage not supported for this target");
4571 /* Record `register' declaration for warnings on &
4572 and in case doing stupid register allocation. */
4574 if (storage_class == csc_register)
4576 C_DECL_REGISTER (decl) = 1;
4577 DECL_REGISTER (decl) = 1;
4580 /* Record constancy and volatility. */
4581 c_apply_type_quals_to_decl (type_quals, decl);
4583 /* If a type has volatile components, it should be stored in memory.
4584 Otherwise, the fact that those components are volatile
4585 will be ignored, and would even crash the compiler. */
4586 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4588 /* It is not an error for a structure with volatile fields to
4589 be declared register, but reset DECL_REGISTER since it
4590 cannot actually go in a register. */
4591 int was_reg = C_DECL_REGISTER (decl);
4592 C_DECL_REGISTER (decl) = 0;
4593 DECL_REGISTER (decl) = 0;
4594 c_mark_addressable (decl);
4595 C_DECL_REGISTER (decl) = was_reg;
4598 /* This is the earliest point at which we might know the assembler
4599 name of a variable. Thus, if it's known before this, die horribly. */
4600 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4602 decl_attributes (&decl, returned_attrs, 0);
4604 return decl;
4608 /* Decode the parameter-list info for a function type or function definition.
4609 The argument is the value returned by `get_parm_info' (or made in parse.y
4610 if there is an identifier list instead of a parameter decl list).
4611 These two functions are separate because when a function returns
4612 or receives functions then each is called multiple times but the order
4613 of calls is different. The last call to `grokparms' is always the one
4614 that contains the formal parameter names of a function definition.
4616 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4618 FUNCDEF_FLAG is true for a function definition, false for
4619 a mere declaration. A nonempty identifier-list gets an error message
4620 when FUNCDEF_FLAG is false. */
4622 static tree
4623 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4625 tree arg_types = arg_info->types;
4627 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4628 && !in_system_header)
4629 warning (0, "function declaration isn%'t a prototype");
4631 if (arg_types == error_mark_node)
4632 return 0; /* don't set TYPE_ARG_TYPES in this case */
4634 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4636 if (!funcdef_flag)
4637 pedwarn ("parameter names (without types) in function declaration");
4639 arg_info->parms = arg_info->types;
4640 arg_info->types = 0;
4641 return 0;
4643 else
4645 tree parm, type, typelt;
4646 unsigned int parmno;
4648 /* If there is a parameter of incomplete type in a definition,
4649 this is an error. In a declaration this is valid, and a
4650 struct or union type may be completed later, before any calls
4651 or definition of the function. In the case where the tag was
4652 first declared within the parameter list, a warning has
4653 already been given. If a parameter has void type, then
4654 however the function cannot be defined or called, so
4655 warn. */
4657 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4658 parm;
4659 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4661 type = TREE_VALUE (typelt);
4662 if (type == error_mark_node)
4663 continue;
4665 if (!COMPLETE_TYPE_P (type))
4667 if (funcdef_flag)
4669 if (DECL_NAME (parm))
4670 error ("%Jparameter %u (%qD) has incomplete type",
4671 parm, parmno, parm);
4672 else
4673 error ("%Jparameter %u has incomplete type",
4674 parm, parmno);
4676 TREE_VALUE (typelt) = error_mark_node;
4677 TREE_TYPE (parm) = error_mark_node;
4679 else if (VOID_TYPE_P (type))
4681 if (DECL_NAME (parm))
4682 warning (0, "%Jparameter %u (%qD) has void type",
4683 parm, parmno, parm);
4684 else
4685 warning (0, "%Jparameter %u has void type",
4686 parm, parmno);
4690 return arg_types;
4694 /* Take apart the current scope and return a c_arg_info structure with
4695 info on a parameter list just parsed.
4697 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4699 ELLIPSIS being true means the argument list ended in '...' so don't
4700 append a sentinel (void_list_node) to the end of the type-list. */
4702 struct c_arg_info *
4703 get_parm_info (bool ellipsis)
4705 struct c_binding *b = current_scope->bindings;
4706 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4707 struct c_arg_info);
4708 tree parms = 0;
4709 tree tags = 0;
4710 tree types = 0;
4711 tree others = 0;
4713 static bool explained_incomplete_types = false;
4714 bool gave_void_only_once_err = false;
4716 arg_info->parms = 0;
4717 arg_info->tags = 0;
4718 arg_info->types = 0;
4719 arg_info->others = 0;
4721 /* The bindings in this scope must not get put into a block.
4722 We will take care of deleting the binding nodes. */
4723 current_scope->bindings = 0;
4725 /* This function is only called if there was *something* on the
4726 parameter list. */
4727 gcc_assert (b);
4729 /* A parameter list consisting solely of 'void' indicates that the
4730 function takes no arguments. But if the 'void' is qualified
4731 (by 'const' or 'volatile'), or has a storage class specifier
4732 ('register'), then the behavior is undefined; issue an error.
4733 Typedefs for 'void' are OK (see DR#157). */
4734 if (b->prev == 0 /* one binding */
4735 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4736 && !DECL_NAME (b->decl) /* anonymous */
4737 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4739 if (TREE_THIS_VOLATILE (b->decl)
4740 || TREE_READONLY (b->decl)
4741 || C_DECL_REGISTER (b->decl))
4742 error ("%<void%> as only parameter may not be qualified");
4744 /* There cannot be an ellipsis. */
4745 if (ellipsis)
4746 error ("%<void%> must be the only parameter");
4748 arg_info->types = void_list_node;
4749 return arg_info;
4752 if (!ellipsis)
4753 types = void_list_node;
4755 /* Break up the bindings list into parms, tags, types, and others;
4756 apply sanity checks; purge the name-to-decl bindings. */
4757 while (b)
4759 tree decl = b->decl;
4760 tree type = TREE_TYPE (decl);
4761 const char *keyword;
4763 switch (TREE_CODE (decl))
4765 case PARM_DECL:
4766 if (b->id)
4768 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4769 I_SYMBOL_BINDING (b->id) = b->shadowed;
4772 /* Check for forward decls that never got their actual decl. */
4773 if (TREE_ASM_WRITTEN (decl))
4774 error ("%Jparameter %qD has just a forward declaration",
4775 decl, decl);
4776 /* Check for (..., void, ...) and issue an error. */
4777 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4779 if (!gave_void_only_once_err)
4781 error ("%<void%> must be the only parameter");
4782 gave_void_only_once_err = true;
4785 else
4787 /* Valid parameter, add it to the list. */
4788 TREE_CHAIN (decl) = parms;
4789 parms = decl;
4791 /* Since there is a prototype, args are passed in their
4792 declared types. The back end may override this later. */
4793 DECL_ARG_TYPE (decl) = type;
4794 types = tree_cons (0, type, types);
4796 break;
4798 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4799 case UNION_TYPE: keyword = "union"; goto tag;
4800 case RECORD_TYPE: keyword = "struct"; goto tag;
4801 tag:
4802 /* Types may not have tag-names, in which case the type
4803 appears in the bindings list with b->id NULL. */
4804 if (b->id)
4806 gcc_assert (I_TAG_BINDING (b->id) == b);
4807 I_TAG_BINDING (b->id) = b->shadowed;
4810 /* Warn about any struct, union or enum tags defined in a
4811 parameter list. The scope of such types is limited to
4812 the parameter list, which is rarely if ever desirable
4813 (it's impossible to call such a function with type-
4814 correct arguments). An anonymous union parm type is
4815 meaningful as a GNU extension, so don't warn for that. */
4816 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4818 if (b->id)
4819 /* The %s will be one of 'struct', 'union', or 'enum'. */
4820 warning (0, "%<%s %E%> declared inside parameter list",
4821 keyword, b->id);
4822 else
4823 /* The %s will be one of 'struct', 'union', or 'enum'. */
4824 warning (0, "anonymous %s declared inside parameter list",
4825 keyword);
4827 if (!explained_incomplete_types)
4829 warning (0, "its scope is only this definition or declaration,"
4830 " which is probably not what you want");
4831 explained_incomplete_types = true;
4835 tags = tree_cons (b->id, decl, tags);
4836 break;
4838 case CONST_DECL:
4839 case TYPE_DECL:
4840 case FUNCTION_DECL:
4841 /* CONST_DECLs appear here when we have an embedded enum,
4842 and TYPE_DECLs appear here when we have an embedded struct
4843 or union. No warnings for this - we already warned about the
4844 type itself. FUNCTION_DECLs appear when there is an implicit
4845 function declaration in the parameter list. */
4847 TREE_CHAIN (decl) = others;
4848 others = decl;
4849 /* fall through */
4851 case ERROR_MARK:
4852 /* error_mark_node appears here when we have an undeclared
4853 variable. Just throw it away. */
4854 if (b->id)
4856 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4857 I_SYMBOL_BINDING (b->id) = b->shadowed;
4859 break;
4861 /* Other things that might be encountered. */
4862 case LABEL_DECL:
4863 case VAR_DECL:
4864 default:
4865 gcc_unreachable ();
4868 b = free_binding_and_advance (b);
4871 arg_info->parms = parms;
4872 arg_info->tags = tags;
4873 arg_info->types = types;
4874 arg_info->others = others;
4875 return arg_info;
4878 /* Get the struct, enum or union (CODE says which) with tag NAME.
4879 Define the tag as a forward-reference if it is not defined.
4880 Return a c_typespec structure for the type specifier. */
4882 struct c_typespec
4883 parser_xref_tag (enum tree_code code, tree name)
4885 struct c_typespec ret;
4886 /* If a cross reference is requested, look up the type
4887 already defined for this tag and return it. */
4889 tree ref = lookup_tag (code, name, 0);
4890 /* If this is the right type of tag, return what we found.
4891 (This reference will be shadowed by shadow_tag later if appropriate.)
4892 If this is the wrong type of tag, do not return it. If it was the
4893 wrong type in the same scope, we will have had an error
4894 message already; if in a different scope and declaring
4895 a name, pending_xref_error will give an error message; but if in a
4896 different scope and not declaring a name, this tag should
4897 shadow the previous declaration of a different type of tag, and
4898 this would not work properly if we return the reference found.
4899 (For example, with "struct foo" in an outer scope, "union foo;"
4900 must shadow that tag with a new one of union type.) */
4901 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4902 if (ref && TREE_CODE (ref) == code)
4904 ret.spec = ref;
4905 return ret;
4908 /* If no such tag is yet defined, create a forward-reference node
4909 and record it as the "definition".
4910 When a real declaration of this type is found,
4911 the forward-reference will be altered into a real type. */
4913 ref = make_node (code);
4914 if (code == ENUMERAL_TYPE)
4916 /* Give the type a default layout like unsigned int
4917 to avoid crashing if it does not get defined. */
4918 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4919 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4920 TYPE_USER_ALIGN (ref) = 0;
4921 TYPE_UNSIGNED (ref) = 1;
4922 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4923 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4924 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4927 pushtag (name, ref);
4929 ret.spec = ref;
4930 return ret;
4933 /* Get the struct, enum or union (CODE says which) with tag NAME.
4934 Define the tag as a forward-reference if it is not defined.
4935 Return a tree for the type. */
4937 tree
4938 xref_tag (enum tree_code code, tree name)
4940 return parser_xref_tag (code, name).spec;
4943 /* Make sure that the tag NAME is defined *in the current scope*
4944 at least as a forward reference.
4945 CODE says which kind of tag NAME ought to be. */
4947 tree
4948 start_struct (enum tree_code code, tree name)
4950 /* If there is already a tag defined at this scope
4951 (as a forward reference), just return it. */
4953 tree ref = 0;
4955 if (name != 0)
4956 ref = lookup_tag (code, name, 1);
4957 if (ref && TREE_CODE (ref) == code)
4959 if (TYPE_SIZE (ref))
4961 if (code == UNION_TYPE)
4962 error ("redefinition of %<union %E%>", name);
4963 else
4964 error ("redefinition of %<struct %E%>", name);
4966 else if (C_TYPE_BEING_DEFINED (ref))
4968 if (code == UNION_TYPE)
4969 error ("nested redefinition of %<union %E%>", name);
4970 else
4971 error ("nested redefinition of %<struct %E%>", name);
4974 else
4976 /* Otherwise create a forward-reference just so the tag is in scope. */
4978 ref = make_node (code);
4979 pushtag (name, ref);
4982 C_TYPE_BEING_DEFINED (ref) = 1;
4983 TYPE_PACKED (ref) = flag_pack_struct;
4984 return ref;
4987 /* Process the specs, declarator and width (NULL if omitted)
4988 of a structure component, returning a FIELD_DECL node.
4989 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4991 This is done during the parsing of the struct declaration.
4992 The FIELD_DECL nodes are chained together and the lot of them
4993 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4995 tree
4996 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
4997 tree width)
4999 tree value;
5001 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5002 && width == NULL_TREE)
5004 /* This is an unnamed decl.
5006 If we have something of the form "union { list } ;" then this
5007 is the anonymous union extension. Similarly for struct.
5009 If this is something of the form "struct foo;", then
5010 If MS extensions are enabled, this is handled as an
5011 anonymous struct.
5012 Otherwise this is a forward declaration of a structure tag.
5014 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5015 If MS extensions are enabled and foo names a structure, then
5016 again this is an anonymous struct.
5017 Otherwise this is an error.
5019 Oh what a horrid tangled web we weave. I wonder if MS consciously
5020 took this from Plan 9 or if it was an accident of implementation
5021 that took root before someone noticed the bug... */
5023 tree type = declspecs->type;
5024 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5025 || TREE_CODE (type) == UNION_TYPE);
5026 bool ok = false;
5028 if (type_ok
5029 && (flag_ms_extensions || !declspecs->typedef_p))
5031 if (flag_ms_extensions)
5032 ok = true;
5033 else if (flag_iso)
5034 ok = false;
5035 else if (TYPE_NAME (type) == NULL)
5036 ok = true;
5037 else
5038 ok = false;
5040 if (!ok)
5042 pedwarn ("declaration does not declare anything");
5043 return NULL_TREE;
5045 if (pedantic)
5046 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5049 value = grokdeclarator (declarator, declspecs, FIELD, false,
5050 width ? &width : NULL);
5052 finish_decl (value, NULL_TREE, NULL_TREE);
5053 DECL_INITIAL (value) = width;
5055 return value;
5058 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5059 the list such that this does not present a problem later. */
5061 static void
5062 detect_field_duplicates (tree fieldlist)
5064 tree x, y;
5065 int timeout = 10;
5067 /* First, see if there are more than "a few" fields.
5068 This is trivially true if there are zero or one fields. */
5069 if (!fieldlist)
5070 return;
5071 x = TREE_CHAIN (fieldlist);
5072 if (!x)
5073 return;
5074 do {
5075 timeout--;
5076 x = TREE_CHAIN (x);
5077 } while (timeout > 0 && x);
5079 /* If there were "few" fields, avoid the overhead of allocating
5080 a hash table. Instead just do the nested traversal thing. */
5081 if (timeout > 0)
5083 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5084 if (DECL_NAME (x))
5086 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5087 if (DECL_NAME (y) == DECL_NAME (x))
5089 error ("%Jduplicate member %qD", x, x);
5090 DECL_NAME (x) = NULL_TREE;
5094 else
5096 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5097 void **slot;
5099 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5100 if ((y = DECL_NAME (x)) != 0)
5102 slot = htab_find_slot (htab, y, INSERT);
5103 if (*slot)
5105 error ("%Jduplicate member %qD", x, x);
5106 DECL_NAME (x) = NULL_TREE;
5108 *slot = y;
5111 htab_delete (htab);
5115 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5116 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5117 ATTRIBUTES are attributes to be applied to the structure. */
5119 tree
5120 finish_struct (tree t, tree fieldlist, tree attributes)
5122 tree x;
5123 bool toplevel = file_scope == current_scope;
5124 int saw_named_field;
5126 /* If this type was previously laid out as a forward reference,
5127 make sure we lay it out again. */
5129 TYPE_SIZE (t) = 0;
5131 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5133 if (pedantic)
5135 for (x = fieldlist; x; x = TREE_CHAIN (x))
5136 if (DECL_NAME (x) != 0)
5137 break;
5139 if (x == 0)
5141 if (TREE_CODE (t) == UNION_TYPE)
5143 if (fieldlist)
5144 pedwarn ("union has no named members");
5145 else
5146 pedwarn ("union has no members");
5148 else
5150 if (fieldlist)
5151 pedwarn ("struct has no named members");
5152 else
5153 pedwarn ("struct has no members");
5158 /* Install struct as DECL_CONTEXT of each field decl.
5159 Also process specified field sizes, found in the DECL_INITIAL,
5160 storing 0 there after the type has been changed to precision equal
5161 to its width, rather than the precision of the specified standard
5162 type. (Correct layout requires the original type to have been preserved
5163 until now.) */
5165 saw_named_field = 0;
5166 for (x = fieldlist; x; x = TREE_CHAIN (x))
5168 DECL_CONTEXT (x) = t;
5169 DECL_PACKED (x) |= TYPE_PACKED (t);
5171 /* If any field is const, the structure type is pseudo-const. */
5172 if (TREE_READONLY (x))
5173 C_TYPE_FIELDS_READONLY (t) = 1;
5174 else
5176 /* A field that is pseudo-const makes the structure likewise. */
5177 tree t1 = TREE_TYPE (x);
5178 while (TREE_CODE (t1) == ARRAY_TYPE)
5179 t1 = TREE_TYPE (t1);
5180 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5181 && C_TYPE_FIELDS_READONLY (t1))
5182 C_TYPE_FIELDS_READONLY (t) = 1;
5185 /* Any field that is volatile means variables of this type must be
5186 treated in some ways as volatile. */
5187 if (TREE_THIS_VOLATILE (x))
5188 C_TYPE_FIELDS_VOLATILE (t) = 1;
5190 /* Any field of nominal variable size implies structure is too. */
5191 if (C_DECL_VARIABLE_SIZE (x))
5192 C_TYPE_VARIABLE_SIZE (t) = 1;
5194 if (DECL_INITIAL (x))
5196 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5197 DECL_SIZE (x) = bitsize_int (width);
5198 DECL_BIT_FIELD (x) = 1;
5199 SET_DECL_C_BIT_FIELD (x);
5202 /* Detect flexible array member in an invalid context. */
5203 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5204 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5205 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5206 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5208 if (TREE_CODE (t) == UNION_TYPE)
5210 error ("%Jflexible array member in union", x);
5211 TREE_TYPE (x) = error_mark_node;
5213 else if (TREE_CHAIN (x) != NULL_TREE)
5215 error ("%Jflexible array member not at end of struct", x);
5216 TREE_TYPE (x) = error_mark_node;
5218 else if (!saw_named_field)
5220 error ("%Jflexible array member in otherwise empty struct", x);
5221 TREE_TYPE (x) = error_mark_node;
5225 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5226 && flexible_array_type_p (TREE_TYPE (x)))
5227 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5229 if (DECL_NAME (x))
5230 saw_named_field = 1;
5233 detect_field_duplicates (fieldlist);
5235 /* Now we have the nearly final fieldlist. Record it,
5236 then lay out the structure or union (including the fields). */
5238 TYPE_FIELDS (t) = fieldlist;
5240 layout_type (t);
5242 /* Give bit-fields their proper types. */
5244 tree *fieldlistp = &fieldlist;
5245 while (*fieldlistp)
5246 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5247 && TREE_TYPE (*fieldlistp) != error_mark_node)
5249 unsigned HOST_WIDE_INT width
5250 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5251 tree type = TREE_TYPE (*fieldlistp);
5252 if (width != TYPE_PRECISION (type))
5254 TREE_TYPE (*fieldlistp)
5255 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5256 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5258 DECL_INITIAL (*fieldlistp) = 0;
5260 else
5261 fieldlistp = &TREE_CHAIN (*fieldlistp);
5264 /* Now we have the truly final field list.
5265 Store it in this type and in the variants. */
5267 TYPE_FIELDS (t) = fieldlist;
5269 /* If there are lots of fields, sort so we can look through them fast.
5270 We arbitrarily consider 16 or more elts to be "a lot". */
5273 int len = 0;
5275 for (x = fieldlist; x; x = TREE_CHAIN (x))
5277 if (len > 15 || DECL_NAME (x) == NULL)
5278 break;
5279 len += 1;
5282 if (len > 15)
5284 tree *field_array;
5285 struct lang_type *space;
5286 struct sorted_fields_type *space2;
5288 len += list_length (x);
5290 /* Use the same allocation policy here that make_node uses, to
5291 ensure that this lives as long as the rest of the struct decl.
5292 All decls in an inline function need to be saved. */
5294 space = GGC_CNEW (struct lang_type);
5295 space2 = GGC_NEWVAR (struct sorted_fields_type,
5296 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5298 len = 0;
5299 space->s = space2;
5300 field_array = &space2->elts[0];
5301 for (x = fieldlist; x; x = TREE_CHAIN (x))
5303 field_array[len++] = x;
5305 /* If there is anonymous struct or union, break out of the loop. */
5306 if (DECL_NAME (x) == NULL)
5307 break;
5309 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5310 if (x == NULL)
5312 TYPE_LANG_SPECIFIC (t) = space;
5313 TYPE_LANG_SPECIFIC (t)->s->len = len;
5314 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5315 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5320 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5322 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5323 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5324 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5325 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5328 /* If this was supposed to be a transparent union, but we can't
5329 make it one, warn and turn off the flag. */
5330 if (TREE_CODE (t) == UNION_TYPE
5331 && TYPE_TRANSPARENT_UNION (t)
5332 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5334 TYPE_TRANSPARENT_UNION (t) = 0;
5335 warning (0, "union cannot be made transparent");
5338 /* If this structure or union completes the type of any previous
5339 variable declaration, lay it out and output its rtl. */
5340 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5342 x = TREE_CHAIN (x))
5344 tree decl = TREE_VALUE (x);
5345 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5346 layout_array_type (TREE_TYPE (decl));
5347 if (TREE_CODE (decl) != TYPE_DECL)
5349 layout_decl (decl, 0);
5350 if (c_dialect_objc ())
5351 objc_check_decl (decl);
5352 rest_of_decl_compilation (decl, toplevel, 0);
5353 if (!toplevel)
5354 expand_decl (decl);
5357 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5359 /* Finish debugging output for this type. */
5360 rest_of_type_compilation (t, toplevel);
5362 /* If we're inside a function proper, i.e. not file-scope and not still
5363 parsing parameters, then arrange for the size of a variable sized type
5364 to be bound now. */
5365 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5366 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5368 return t;
5371 /* Lay out the type T, and its element type, and so on. */
5373 static void
5374 layout_array_type (tree t)
5376 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5377 layout_array_type (TREE_TYPE (t));
5378 layout_type (t);
5381 /* Begin compiling the definition of an enumeration type.
5382 NAME is its name (or null if anonymous).
5383 Returns the type object, as yet incomplete.
5384 Also records info about it so that build_enumerator
5385 may be used to declare the individual values as they are read. */
5387 tree
5388 start_enum (tree name)
5390 tree enumtype = 0;
5392 /* If this is the real definition for a previous forward reference,
5393 fill in the contents in the same object that used to be the
5394 forward reference. */
5396 if (name != 0)
5397 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5399 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5401 enumtype = make_node (ENUMERAL_TYPE);
5402 pushtag (name, enumtype);
5405 if (C_TYPE_BEING_DEFINED (enumtype))
5406 error ("nested redefinition of %<enum %E%>", name);
5408 C_TYPE_BEING_DEFINED (enumtype) = 1;
5410 if (TYPE_VALUES (enumtype) != 0)
5412 /* This enum is a named one that has been declared already. */
5413 error ("redeclaration of %<enum %E%>", name);
5415 /* Completely replace its old definition.
5416 The old enumerators remain defined, however. */
5417 TYPE_VALUES (enumtype) = 0;
5420 enum_next_value = integer_zero_node;
5421 enum_overflow = 0;
5423 if (flag_short_enums)
5424 TYPE_PACKED (enumtype) = 1;
5426 return enumtype;
5429 /* After processing and defining all the values of an enumeration type,
5430 install their decls in the enumeration type and finish it off.
5431 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5432 and ATTRIBUTES are the specified attributes.
5433 Returns ENUMTYPE. */
5435 tree
5436 finish_enum (tree enumtype, tree values, tree attributes)
5438 tree pair, tem;
5439 tree minnode = 0, maxnode = 0;
5440 int precision, unsign;
5441 bool toplevel = (file_scope == current_scope);
5442 struct lang_type *lt;
5444 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5446 /* Calculate the maximum value of any enumerator in this type. */
5448 if (values == error_mark_node)
5449 minnode = maxnode = integer_zero_node;
5450 else
5452 minnode = maxnode = TREE_VALUE (values);
5453 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5455 tree value = TREE_VALUE (pair);
5456 if (tree_int_cst_lt (maxnode, value))
5457 maxnode = value;
5458 if (tree_int_cst_lt (value, minnode))
5459 minnode = value;
5463 /* Construct the final type of this enumeration. It is the same
5464 as one of the integral types - the narrowest one that fits, except
5465 that normally we only go as narrow as int - and signed iff any of
5466 the values are negative. */
5467 unsign = (tree_int_cst_sgn (minnode) >= 0);
5468 precision = MAX (min_precision (minnode, unsign),
5469 min_precision (maxnode, unsign));
5471 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5473 tem = c_common_type_for_size (precision, unsign);
5474 if (tem == NULL)
5476 warning (0, "enumeration values exceed range of largest integer");
5477 tem = long_long_integer_type_node;
5480 else
5481 tem = unsign ? unsigned_type_node : integer_type_node;
5483 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5484 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5485 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5486 TYPE_SIZE (enumtype) = 0;
5488 /* If the precision of the type was specific with an attribute and it
5489 was too small, give an error. Otherwise, use it. */
5490 if (TYPE_PRECISION (enumtype))
5492 if (precision > TYPE_PRECISION (enumtype))
5493 error ("specified mode too small for enumeral values");
5495 else
5496 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5498 layout_type (enumtype);
5500 if (values != error_mark_node)
5502 /* Change the type of the enumerators to be the enum type. We
5503 need to do this irrespective of the size of the enum, for
5504 proper type checking. Replace the DECL_INITIALs of the
5505 enumerators, and the value slots of the list, with copies
5506 that have the enum type; they cannot be modified in place
5507 because they may be shared (e.g. integer_zero_node) Finally,
5508 change the purpose slots to point to the names of the decls. */
5509 for (pair = values; pair; pair = TREE_CHAIN (pair))
5511 tree enu = TREE_PURPOSE (pair);
5512 tree ini = DECL_INITIAL (enu);
5514 TREE_TYPE (enu) = enumtype;
5516 /* The ISO C Standard mandates enumerators to have type int,
5517 even though the underlying type of an enum type is
5518 unspecified. Here we convert any enumerators that fit in
5519 an int to type int, to avoid promotions to unsigned types
5520 when comparing integers with enumerators that fit in the
5521 int range. When -pedantic is given, build_enumerator()
5522 would have already taken care of those that don't fit. */
5523 if (int_fits_type_p (ini, integer_type_node))
5524 tem = integer_type_node;
5525 else
5526 tem = enumtype;
5527 ini = convert (tem, ini);
5529 DECL_INITIAL (enu) = ini;
5530 TREE_PURPOSE (pair) = DECL_NAME (enu);
5531 TREE_VALUE (pair) = ini;
5534 TYPE_VALUES (enumtype) = values;
5537 /* Record the min/max values so that we can warn about bit-field
5538 enumerations that are too small for the values. */
5539 lt = GGC_CNEW (struct lang_type);
5540 lt->enum_min = minnode;
5541 lt->enum_max = maxnode;
5542 TYPE_LANG_SPECIFIC (enumtype) = lt;
5544 /* Fix up all variant types of this enum type. */
5545 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5547 if (tem == enumtype)
5548 continue;
5549 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5550 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5551 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5552 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5553 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5554 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5555 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5556 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5557 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5558 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5559 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5562 /* Finish debugging output for this type. */
5563 rest_of_type_compilation (enumtype, toplevel);
5565 return enumtype;
5568 /* Build and install a CONST_DECL for one value of the
5569 current enumeration type (one that was begun with start_enum).
5570 Return a tree-list containing the CONST_DECL and its value.
5571 Assignment of sequential values by default is handled here. */
5573 tree
5574 build_enumerator (tree name, tree value)
5576 tree decl, type;
5578 /* Validate and default VALUE. */
5580 if (value != 0)
5582 /* Don't issue more errors for error_mark_node (i.e. an
5583 undeclared identifier) - just ignore the value expression. */
5584 if (value == error_mark_node)
5585 value = 0;
5586 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5587 || TREE_CODE (value) != INTEGER_CST)
5589 error ("enumerator value for %qE is not an integer constant", name);
5590 value = 0;
5592 else
5594 value = default_conversion (value);
5595 constant_expression_warning (value);
5599 /* Default based on previous value. */
5600 /* It should no longer be possible to have NON_LVALUE_EXPR
5601 in the default. */
5602 if (value == 0)
5604 value = enum_next_value;
5605 if (enum_overflow)
5606 error ("overflow in enumeration values");
5609 if (pedantic && !int_fits_type_p (value, integer_type_node))
5611 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5612 /* XXX This causes -pedantic to change the meaning of the program.
5613 Remove? -zw 2004-03-15 */
5614 value = convert (integer_type_node, value);
5617 /* Set basis for default for next value. */
5618 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5619 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5621 /* Now create a declaration for the enum value name. */
5623 type = TREE_TYPE (value);
5624 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5625 TYPE_PRECISION (integer_type_node)),
5626 (TYPE_PRECISION (type)
5627 >= TYPE_PRECISION (integer_type_node)
5628 && TYPE_UNSIGNED (type)));
5630 decl = build_decl (CONST_DECL, name, type);
5631 DECL_INITIAL (decl) = convert (type, value);
5632 pushdecl (decl);
5634 return tree_cons (decl, value, NULL_TREE);
5638 /* Create the FUNCTION_DECL for a function definition.
5639 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5640 the declaration; they describe the function's name and the type it returns,
5641 but twisted together in a fashion that parallels the syntax of C.
5643 This function creates a binding context for the function body
5644 as well as setting up the FUNCTION_DECL in current_function_decl.
5646 Returns 1 on success. If the DECLARATOR is not suitable for a function
5647 (it defines a datum instead), we return 0, which tells
5648 yyparse to report a parse error. */
5651 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5652 tree attributes)
5654 tree decl1, old_decl;
5655 tree restype, resdecl;
5656 struct c_label_context_se *nstack_se;
5657 struct c_label_context_vm *nstack_vm;
5659 current_function_returns_value = 0; /* Assume, until we see it does. */
5660 current_function_returns_null = 0;
5661 current_function_returns_abnormally = 0;
5662 warn_about_return_type = 0;
5663 current_extern_inline = 0;
5664 c_switch_stack = NULL;
5666 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5667 nstack_se->labels_def = NULL;
5668 nstack_se->labels_used = NULL;
5669 nstack_se->next = label_context_stack_se;
5670 label_context_stack_se = nstack_se;
5672 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5673 nstack_vm->labels_def = NULL;
5674 nstack_vm->labels_used = NULL;
5675 nstack_vm->scope = 0;
5676 nstack_vm->next = label_context_stack_vm;
5677 label_context_stack_vm = nstack_vm;
5679 /* Indicate no valid break/continue context by setting these variables
5680 to some non-null, non-label value. We'll notice and emit the proper
5681 error message in c_finish_bc_stmt. */
5682 c_break_label = c_cont_label = size_zero_node;
5684 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5686 /* If the declarator is not suitable for a function definition,
5687 cause a syntax error. */
5688 if (decl1 == 0)
5689 return 0;
5691 decl_attributes (&decl1, attributes, 0);
5693 if (DECL_DECLARED_INLINE_P (decl1)
5694 && DECL_UNINLINABLE (decl1)
5695 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5696 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5698 announce_function (decl1);
5700 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5702 error ("return type is an incomplete type");
5703 /* Make it return void instead. */
5704 TREE_TYPE (decl1)
5705 = build_function_type (void_type_node,
5706 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5709 if (warn_about_return_type)
5710 pedwarn_c99 ("return type defaults to %<int%>");
5712 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5713 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5714 DECL_INITIAL (decl1) = error_mark_node;
5716 /* If this definition isn't a prototype and we had a prototype declaration
5717 before, copy the arg type info from that prototype. */
5718 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5719 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5720 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5721 TREE_TYPE (TREE_TYPE (old_decl)))
5722 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5724 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5725 TREE_TYPE (decl1));
5726 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5729 /* Optionally warn of old-fashioned def with no previous prototype. */
5730 if (warn_strict_prototypes
5731 && old_decl != error_mark_node
5732 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5733 && C_DECL_ISNT_PROTOTYPE (old_decl))
5734 warning (0, "function declaration isn%'t a prototype");
5735 /* Optionally warn of any global def with no previous prototype. */
5736 else if (warn_missing_prototypes
5737 && old_decl != error_mark_node
5738 && TREE_PUBLIC (decl1)
5739 && !MAIN_NAME_P (DECL_NAME (decl1))
5740 && C_DECL_ISNT_PROTOTYPE (old_decl))
5741 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5742 /* Optionally warn of any def with no previous prototype
5743 if the function has already been used. */
5744 else if (warn_missing_prototypes
5745 && old_decl != 0
5746 && old_decl != error_mark_node
5747 && TREE_USED (old_decl)
5748 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5749 warning (0, "%J%qD was used with no prototype before its definition",
5750 decl1, decl1);
5751 /* Optionally warn of any global def with no previous declaration. */
5752 else if (warn_missing_declarations
5753 && TREE_PUBLIC (decl1)
5754 && old_decl == 0
5755 && !MAIN_NAME_P (DECL_NAME (decl1)))
5756 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5757 /* Optionally warn of any def with no previous declaration
5758 if the function has already been used. */
5759 else if (warn_missing_declarations
5760 && old_decl != 0
5761 && old_decl != error_mark_node
5762 && TREE_USED (old_decl)
5763 && C_DECL_IMPLICIT (old_decl))
5764 warning (0, "%J%qD was used with no declaration before its definition",
5765 decl1, decl1);
5767 /* This is a definition, not a reference.
5768 So normally clear DECL_EXTERNAL.
5769 However, `extern inline' acts like a declaration
5770 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5771 DECL_EXTERNAL (decl1) = current_extern_inline;
5773 /* This function exists in static storage.
5774 (This does not mean `static' in the C sense!) */
5775 TREE_STATIC (decl1) = 1;
5777 /* A nested function is not global. */
5778 if (current_function_decl != 0)
5779 TREE_PUBLIC (decl1) = 0;
5781 /* This is the earliest point at which we might know the assembler
5782 name of the function. Thus, if it's set before this, die horribly. */
5783 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5785 /* If #pragma weak was used, mark the decl weak now. */
5786 if (current_scope == file_scope)
5787 maybe_apply_pragma_weak (decl1);
5789 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5790 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5792 tree args;
5793 int argct = 0;
5795 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5796 != integer_type_node)
5797 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5799 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5800 args = TREE_CHAIN (args))
5802 tree type = args ? TREE_VALUE (args) : 0;
5804 if (type == void_type_node)
5805 break;
5807 ++argct;
5808 switch (argct)
5810 case 1:
5811 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5812 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5813 decl1, decl1);
5814 break;
5816 case 2:
5817 if (TREE_CODE (type) != POINTER_TYPE
5818 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5819 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5820 != char_type_node))
5821 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5822 decl1, decl1);
5823 break;
5825 case 3:
5826 if (TREE_CODE (type) != POINTER_TYPE
5827 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5828 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5829 != char_type_node))
5830 pedwarn ("%Jthird argument of %qD should probably be "
5831 "%<char **%>", decl1, decl1);
5832 break;
5836 /* It is intentional that this message does not mention the third
5837 argument because it's only mentioned in an appendix of the
5838 standard. */
5839 if (argct > 0 && (argct < 2 || argct > 3))
5840 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5842 if (!TREE_PUBLIC (decl1))
5843 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5846 /* Record the decl so that the function name is defined.
5847 If we already have a decl for this name, and it is a FUNCTION_DECL,
5848 use the old decl. */
5850 current_function_decl = pushdecl (decl1);
5852 push_scope ();
5853 declare_parm_level ();
5855 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5856 /* Promote the value to int before returning it. */
5857 if (c_promoting_integer_type_p (restype))
5859 /* It retains unsignedness if not really getting wider. */
5860 if (TYPE_UNSIGNED (restype)
5861 && (TYPE_PRECISION (restype)
5862 == TYPE_PRECISION (integer_type_node)))
5863 restype = unsigned_type_node;
5864 else
5865 restype = integer_type_node;
5868 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5869 DECL_ARTIFICIAL (resdecl) = 1;
5870 DECL_IGNORED_P (resdecl) = 1;
5871 DECL_RESULT (current_function_decl) = resdecl;
5873 start_fname_decls ();
5875 return 1;
5878 /* Subroutine of store_parm_decls which handles new-style function
5879 definitions (prototype format). The parms already have decls, so we
5880 need only record them as in effect and complain if any redundant
5881 old-style parm decls were written. */
5882 static void
5883 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
5885 tree decl;
5887 if (current_scope->bindings)
5889 error ("%Jold-style parameter declarations in prototyped "
5890 "function definition", fndecl);
5892 /* Get rid of the old-style declarations. */
5893 pop_scope ();
5894 push_scope ();
5896 /* Don't issue this warning for nested functions, and don't issue this
5897 warning if we got here because ARG_INFO_TYPES was error_mark_node
5898 (this happens when a function definition has just an ellipsis in
5899 its parameter list). */
5900 else if (warn_traditional && !in_system_header && !current_function_scope
5901 && arg_info->types != error_mark_node)
5902 warning (0, "%Jtraditional C rejects ISO C style function definitions",
5903 fndecl);
5905 /* Now make all the parameter declarations visible in the function body.
5906 We can bypass most of the grunt work of pushdecl. */
5907 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
5909 DECL_CONTEXT (decl) = current_function_decl;
5910 if (DECL_NAME (decl))
5911 bind (DECL_NAME (decl), decl, current_scope,
5912 /*invisible=*/false, /*nested=*/false);
5913 else
5914 error ("%Jparameter name omitted", decl);
5917 /* Record the parameter list in the function declaration. */
5918 DECL_ARGUMENTS (fndecl) = arg_info->parms;
5920 /* Now make all the ancillary declarations visible, likewise. */
5921 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
5923 DECL_CONTEXT (decl) = current_function_decl;
5924 if (DECL_NAME (decl))
5925 bind (DECL_NAME (decl), decl, current_scope,
5926 /*invisible=*/false, /*nested=*/false);
5929 /* And all the tag declarations. */
5930 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
5931 if (TREE_PURPOSE (decl))
5932 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5933 /*invisible=*/false, /*nested=*/false);
5936 /* Subroutine of store_parm_decls which handles old-style function
5937 definitions (separate parameter list and declarations). */
5939 static void
5940 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
5942 struct c_binding *b;
5943 tree parm, decl, last;
5944 tree parmids = arg_info->parms;
5946 /* We use DECL_WEAK as a flag to show which parameters have been
5947 seen already, since it is not used on PARM_DECL. */
5948 #ifdef ENABLE_CHECKING
5949 for (b = current_scope->bindings; b; b = b->prev)
5950 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
5951 #endif
5953 if (warn_old_style_definition && !in_system_header)
5954 warning (0, "%Jold-style function definition", fndecl);
5956 /* Match each formal parameter name with its declaration. Save each
5957 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5958 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5960 if (TREE_VALUE (parm) == 0)
5962 error ("%Jparameter name missing from parameter list", fndecl);
5963 TREE_PURPOSE (parm) = 0;
5964 continue;
5967 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5968 if (b && B_IN_CURRENT_SCOPE (b))
5970 decl = b->decl;
5971 /* If we got something other than a PARM_DECL it is an error. */
5972 if (TREE_CODE (decl) != PARM_DECL)
5973 error ("%J%qD declared as a non-parameter", decl, decl);
5974 /* If the declaration is already marked, we have a duplicate
5975 name. Complain and ignore the duplicate. */
5976 else if (DECL_WEAK (decl))
5978 error ("%Jmultiple parameters named %qD", decl, decl);
5979 TREE_PURPOSE (parm) = 0;
5980 continue;
5982 /* If the declaration says "void", complain and turn it into
5983 an int. */
5984 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5986 error ("%Jparameter %qD declared with void type", decl, decl);
5987 TREE_TYPE (decl) = integer_type_node;
5988 DECL_ARG_TYPE (decl) = integer_type_node;
5989 layout_decl (decl, 0);
5992 /* If no declaration found, default to int. */
5993 else
5995 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5996 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5997 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5998 pushdecl (decl);
6000 if (flag_isoc99)
6001 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6002 else if (extra_warnings)
6003 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6006 TREE_PURPOSE (parm) = decl;
6007 DECL_WEAK (decl) = 1;
6010 /* Now examine the parms chain for incomplete declarations
6011 and declarations with no corresponding names. */
6013 for (b = current_scope->bindings; b; b = b->prev)
6015 parm = b->decl;
6016 if (TREE_CODE (parm) != PARM_DECL)
6017 continue;
6019 if (TREE_TYPE (parm) != error_mark_node
6020 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6022 error ("%Jparameter %qD has incomplete type", parm, parm);
6023 TREE_TYPE (parm) = error_mark_node;
6026 if (!DECL_WEAK (parm))
6028 error ("%Jdeclaration for parameter %qD but no such parameter",
6029 parm, parm);
6031 /* Pretend the parameter was not missing.
6032 This gets us to a standard state and minimizes
6033 further error messages. */
6034 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6038 /* Chain the declarations together in the order of the list of
6039 names. Store that chain in the function decl, replacing the
6040 list of names. Update the current scope to match. */
6041 DECL_ARGUMENTS (fndecl) = 0;
6043 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6044 if (TREE_PURPOSE (parm))
6045 break;
6046 if (parm && TREE_PURPOSE (parm))
6048 last = TREE_PURPOSE (parm);
6049 DECL_ARGUMENTS (fndecl) = last;
6050 DECL_WEAK (last) = 0;
6052 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6053 if (TREE_PURPOSE (parm))
6055 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6056 last = TREE_PURPOSE (parm);
6057 DECL_WEAK (last) = 0;
6059 TREE_CHAIN (last) = 0;
6062 /* If there was a previous prototype,
6063 set the DECL_ARG_TYPE of each argument according to
6064 the type previously specified, and report any mismatches. */
6066 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6068 tree type;
6069 for (parm = DECL_ARGUMENTS (fndecl),
6070 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6071 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6072 != void_type_node));
6073 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6075 if (parm == 0 || type == 0
6076 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6078 error ("number of arguments doesn%'t match prototype");
6079 error ("%Hprototype declaration",
6080 &current_function_prototype_locus);
6081 break;
6083 /* Type for passing arg must be consistent with that
6084 declared for the arg. ISO C says we take the unqualified
6085 type for parameters declared with qualified type. */
6086 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6087 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6089 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6090 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6092 /* Adjust argument to match prototype. E.g. a previous
6093 `int foo(float);' prototype causes
6094 `int foo(x) float x; {...}' to be treated like
6095 `int foo(float x) {...}'. This is particularly
6096 useful for argument types like uid_t. */
6097 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6099 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6100 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6101 && TYPE_PRECISION (TREE_TYPE (parm))
6102 < TYPE_PRECISION (integer_type_node))
6103 DECL_ARG_TYPE (parm) = integer_type_node;
6105 if (pedantic)
6107 pedwarn ("promoted argument %qD "
6108 "doesn%'t match prototype", parm);
6109 pedwarn ("%Hprototype declaration",
6110 &current_function_prototype_locus);
6113 else
6115 error ("argument %qD doesn%'t match prototype", parm);
6116 error ("%Hprototype declaration",
6117 &current_function_prototype_locus);
6121 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6124 /* Otherwise, create a prototype that would match. */
6126 else
6128 tree actual = 0, last = 0, type;
6130 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6132 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6133 if (last)
6134 TREE_CHAIN (last) = type;
6135 else
6136 actual = type;
6137 last = type;
6139 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6140 if (last)
6141 TREE_CHAIN (last) = type;
6142 else
6143 actual = type;
6145 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6146 of the type of this function, but we need to avoid having this
6147 affect the types of other similarly-typed functions, so we must
6148 first force the generation of an identical (but separate) type
6149 node for the relevant function type. The new node we create
6150 will be a variant of the main variant of the original function
6151 type. */
6153 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6155 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6159 /* Store parameter declarations passed in ARG_INFO into the current
6160 function declaration. */
6162 void
6163 store_parm_decls_from (struct c_arg_info *arg_info)
6165 current_function_arg_info = arg_info;
6166 store_parm_decls ();
6169 /* Store the parameter declarations into the current function declaration.
6170 This is called after parsing the parameter declarations, before
6171 digesting the body of the function.
6173 For an old-style definition, construct a prototype out of the old-style
6174 parameter declarations and inject it into the function's type. */
6176 void
6177 store_parm_decls (void)
6179 tree fndecl = current_function_decl;
6180 bool proto;
6182 /* The argument information block for FNDECL. */
6183 struct c_arg_info *arg_info = current_function_arg_info;
6184 current_function_arg_info = 0;
6186 /* True if this definition is written with a prototype. Note:
6187 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6188 list in a function definition as equivalent to (void) -- an
6189 empty argument list specifies the function has no parameters,
6190 but only (void) sets up a prototype for future calls. */
6191 proto = arg_info->types != 0;
6193 if (proto)
6194 store_parm_decls_newstyle (fndecl, arg_info);
6195 else
6196 store_parm_decls_oldstyle (fndecl, arg_info);
6198 /* The next call to push_scope will be a function body. */
6200 next_is_function_body = true;
6202 /* Write a record describing this function definition to the prototypes
6203 file (if requested). */
6205 gen_aux_info_record (fndecl, 1, 0, proto);
6207 /* Initialize the RTL code for the function. */
6208 allocate_struct_function (fndecl);
6210 /* Begin the statement tree for this function. */
6211 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6213 /* ??? Insert the contents of the pending sizes list into the function
6214 to be evaluated. The only reason left to have this is
6215 void foo(int n, int array[n++])
6216 because we throw away the array type in favor of a pointer type, and
6217 thus won't naturally see the SAVE_EXPR containing the increment. All
6218 other pending sizes would be handled by gimplify_parameters. */
6220 tree t;
6221 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6222 add_stmt (TREE_VALUE (t));
6225 /* Even though we're inside a function body, we still don't want to
6226 call expand_expr to calculate the size of a variable-sized array.
6227 We haven't necessarily assigned RTL to all variables yet, so it's
6228 not safe to try to expand expressions involving them. */
6229 cfun->x_dont_save_pending_sizes_p = 1;
6232 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6233 functions. */
6235 static void
6236 c_warn_unused_result_recursively (tree fndecl)
6238 struct cgraph_node *cgn;
6240 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6241 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6243 /* Finalize all nested functions now. */
6244 cgn = cgraph_node (fndecl);
6245 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6246 c_warn_unused_result_recursively (cgn->decl);
6249 /* Finish up a function declaration and compile that function
6250 all the way to assembler language output. The free the storage
6251 for the function definition.
6253 This is called after parsing the body of the function definition. */
6255 void
6256 finish_function (void)
6258 tree fndecl = current_function_decl;
6260 label_context_stack_se = label_context_stack_se->next;
6261 label_context_stack_vm = label_context_stack_vm->next;
6263 if (TREE_CODE (fndecl) == FUNCTION_DECL
6264 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6266 tree args = DECL_ARGUMENTS (fndecl);
6267 for (; args; args = TREE_CHAIN (args))
6269 tree type = TREE_TYPE (args);
6270 if (INTEGRAL_TYPE_P (type)
6271 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6272 DECL_ARG_TYPE (args) = integer_type_node;
6276 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6277 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6279 /* Must mark the RESULT_DECL as being in this function. */
6281 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6282 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6284 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6286 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6287 != integer_type_node)
6289 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6290 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6291 if (!warn_main)
6292 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6294 else
6296 if (flag_isoc99)
6298 tree stmt = c_finish_return (integer_zero_node);
6299 #ifdef USE_MAPPED_LOCATION
6300 /* Hack. We don't want the middle-end to warn that this return
6301 is unreachable, so we mark its location as special. Using
6302 UNKNOWN_LOCATION has the problem that it gets clobbered in
6303 annotate_one_with_locus. A cleaner solution might be to
6304 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6306 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6307 #else
6308 /* Hack. We don't want the middle-end to warn that this
6309 return is unreachable, so put the statement on the
6310 special line 0. */
6311 annotate_with_file_line (stmt, input_filename, 0);
6312 #endif
6317 /* Tie off the statement tree for this function. */
6318 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6320 finish_fname_decls ();
6322 /* Complain if there's just no return statement. */
6323 if (warn_return_type
6324 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6325 && !current_function_returns_value && !current_function_returns_null
6326 /* Don't complain if we are no-return. */
6327 && !current_function_returns_abnormally
6328 /* Don't warn for main(). */
6329 && !MAIN_NAME_P (DECL_NAME (fndecl))
6330 /* Or if they didn't actually specify a return type. */
6331 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6332 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6333 inline function, as we might never be compiled separately. */
6334 && DECL_INLINE (fndecl))
6335 warning (0, "no return statement in function returning non-void");
6337 /* With just -Wextra, complain only if function returns both with
6338 and without a value. */
6339 if (extra_warnings
6340 && current_function_returns_value
6341 && current_function_returns_null)
6342 warning (0, "this function may return with or without a value");
6344 /* Store the end of the function, so that we get good line number
6345 info for the epilogue. */
6346 cfun->function_end_locus = input_location;
6348 /* If we don't have ctors/dtors sections, and this is a static
6349 constructor or destructor, it must be recorded now. */
6350 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6351 && !targetm.have_ctors_dtors)
6352 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6353 if (DECL_STATIC_DESTRUCTOR (fndecl)
6354 && !targetm.have_ctors_dtors)
6355 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6357 /* Finalize the ELF visibility for the function. */
6358 c_determine_visibility (fndecl);
6360 /* Genericize before inlining. Delay genericizing nested functions
6361 until their parent function is genericized. Since finalizing
6362 requires GENERIC, delay that as well. */
6364 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6365 && !undef_nested_function)
6367 if (!decl_function_context (fndecl))
6369 c_genericize (fndecl);
6370 c_warn_unused_result_recursively (fndecl);
6372 /* ??? Objc emits functions after finalizing the compilation unit.
6373 This should be cleaned up later and this conditional removed. */
6374 if (cgraph_global_info_ready)
6376 c_expand_body (fndecl);
6377 return;
6380 cgraph_finalize_function (fndecl, false);
6382 else
6384 /* Register this function with cgraph just far enough to get it
6385 added to our parent's nested function list. Handy, since the
6386 C front end doesn't have such a list. */
6387 (void) cgraph_node (fndecl);
6391 if (!decl_function_context (fndecl))
6392 undef_nested_function = false;
6394 /* We're leaving the context of this function, so zap cfun.
6395 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6396 tree_rest_of_compilation. */
6397 cfun = NULL;
6398 current_function_decl = NULL;
6401 /* Generate the RTL for the body of FNDECL. */
6403 void
6404 c_expand_body (tree fndecl)
6407 if (!DECL_INITIAL (fndecl)
6408 || DECL_INITIAL (fndecl) == error_mark_node)
6409 return;
6411 tree_rest_of_compilation (fndecl);
6413 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6414 && targetm.have_ctors_dtors)
6415 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6416 DEFAULT_INIT_PRIORITY);
6417 if (DECL_STATIC_DESTRUCTOR (fndecl)
6418 && targetm.have_ctors_dtors)
6419 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6420 DEFAULT_INIT_PRIORITY);
6423 /* Check the declarations given in a for-loop for satisfying the C99
6424 constraints. */
6425 void
6426 check_for_loop_decls (void)
6428 struct c_binding *b;
6430 if (!flag_isoc99)
6432 /* If we get here, declarations have been used in a for loop without
6433 the C99 for loop scope. This doesn't make much sense, so don't
6434 allow it. */
6435 error ("%<for%> loop initial declaration used outside C99 mode");
6436 return;
6438 /* C99 subclause 6.8.5 paragraph 3:
6440 [#3] The declaration part of a for statement shall only
6441 declare identifiers for objects having storage class auto or
6442 register.
6444 It isn't clear whether, in this sentence, "identifiers" binds to
6445 "shall only declare" or to "objects" - that is, whether all identifiers
6446 declared must be identifiers for objects, or whether the restriction
6447 only applies to those that are. (A question on this in comp.std.c
6448 in November 2000 received no answer.) We implement the strictest
6449 interpretation, to avoid creating an extension which later causes
6450 problems. */
6452 for (b = current_scope->bindings; b; b = b->prev)
6454 tree id = b->id;
6455 tree decl = b->decl;
6457 if (!id)
6458 continue;
6460 switch (TREE_CODE (decl))
6462 case VAR_DECL:
6463 if (TREE_STATIC (decl))
6464 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6465 "initial declaration", decl, decl);
6466 else if (DECL_EXTERNAL (decl))
6467 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6468 "initial declaration", decl, decl);
6469 break;
6471 case RECORD_TYPE:
6472 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6473 id);
6474 break;
6475 case UNION_TYPE:
6476 error ("%<union %E%> declared in %<for%> loop initial declaration",
6477 id);
6478 break;
6479 case ENUMERAL_TYPE:
6480 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6481 id);
6482 break;
6483 default:
6484 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6485 "initial declaration", decl, decl);
6490 /* Save and reinitialize the variables
6491 used during compilation of a C function. */
6493 void
6494 c_push_function_context (struct function *f)
6496 struct language_function *p;
6497 p = GGC_NEW (struct language_function);
6498 f->language = p;
6500 p->base.x_stmt_tree = c_stmt_tree;
6501 p->x_break_label = c_break_label;
6502 p->x_cont_label = c_cont_label;
6503 p->x_switch_stack = c_switch_stack;
6504 p->arg_info = current_function_arg_info;
6505 p->returns_value = current_function_returns_value;
6506 p->returns_null = current_function_returns_null;
6507 p->returns_abnormally = current_function_returns_abnormally;
6508 p->warn_about_return_type = warn_about_return_type;
6509 p->extern_inline = current_extern_inline;
6512 /* Restore the variables used during compilation of a C function. */
6514 void
6515 c_pop_function_context (struct function *f)
6517 struct language_function *p = f->language;
6519 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6520 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6522 /* Stop pointing to the local nodes about to be freed. */
6523 /* But DECL_INITIAL must remain nonzero so we know this
6524 was an actual function definition. */
6525 DECL_INITIAL (current_function_decl) = error_mark_node;
6526 DECL_ARGUMENTS (current_function_decl) = 0;
6529 c_stmt_tree = p->base.x_stmt_tree;
6530 c_break_label = p->x_break_label;
6531 c_cont_label = p->x_cont_label;
6532 c_switch_stack = p->x_switch_stack;
6533 current_function_arg_info = p->arg_info;
6534 current_function_returns_value = p->returns_value;
6535 current_function_returns_null = p->returns_null;
6536 current_function_returns_abnormally = p->returns_abnormally;
6537 warn_about_return_type = p->warn_about_return_type;
6538 current_extern_inline = p->extern_inline;
6540 f->language = NULL;
6543 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6545 void
6546 c_dup_lang_specific_decl (tree decl)
6548 struct lang_decl *ld;
6550 if (!DECL_LANG_SPECIFIC (decl))
6551 return;
6553 ld = GGC_NEW (struct lang_decl);
6554 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6555 DECL_LANG_SPECIFIC (decl) = ld;
6558 /* The functions below are required for functionality of doing
6559 function at once processing in the C front end. Currently these
6560 functions are not called from anywhere in the C front end, but as
6561 these changes continue, that will change. */
6563 /* Returns nonzero if the current statement is a full expression,
6564 i.e. temporaries created during that statement should be destroyed
6565 at the end of the statement. */
6568 stmts_are_full_exprs_p (void)
6570 return 0;
6573 /* Returns the stmt_tree (if any) to which statements are currently
6574 being added. If there is no active statement-tree, NULL is
6575 returned. */
6577 stmt_tree
6578 current_stmt_tree (void)
6580 return &c_stmt_tree;
6583 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6584 C. */
6587 anon_aggr_type_p (tree ARG_UNUSED (node))
6589 return 0;
6592 /* Return the global value of T as a symbol. */
6594 tree
6595 identifier_global_value (tree t)
6597 struct c_binding *b;
6599 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6600 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6601 return b->decl;
6603 return 0;
6606 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6607 otherwise the name is found in ridpointers from RID_INDEX. */
6609 void
6610 record_builtin_type (enum rid rid_index, const char *name, tree type)
6612 tree id, decl;
6613 if (name == 0)
6614 id = ridpointers[(int) rid_index];
6615 else
6616 id = get_identifier (name);
6617 decl = build_decl (TYPE_DECL, id, type);
6618 pushdecl (decl);
6619 if (debug_hooks->type_decl)
6620 debug_hooks->type_decl (decl, false);
6623 /* Build the void_list_node (void_type_node having been created). */
6624 tree
6625 build_void_list_node (void)
6627 tree t = build_tree_list (NULL_TREE, void_type_node);
6628 return t;
6631 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6633 struct c_parm *
6634 build_c_parm (struct c_declspecs *specs, tree attrs,
6635 struct c_declarator *declarator)
6637 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6638 ret->specs = specs;
6639 ret->attrs = attrs;
6640 ret->declarator = declarator;
6641 return ret;
6644 /* Return a declarator with nested attributes. TARGET is the inner
6645 declarator to which these attributes apply. ATTRS are the
6646 attributes. */
6648 struct c_declarator *
6649 build_attrs_declarator (tree attrs, struct c_declarator *target)
6651 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6652 ret->kind = cdk_attrs;
6653 ret->declarator = target;
6654 ret->u.attrs = attrs;
6655 return ret;
6658 /* Return a declarator for a function with arguments specified by ARGS
6659 and return type specified by TARGET. */
6661 struct c_declarator *
6662 build_function_declarator (struct c_arg_info *args,
6663 struct c_declarator *target)
6665 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6666 ret->kind = cdk_function;
6667 ret->declarator = target;
6668 ret->u.arg_info = args;
6669 return ret;
6672 /* Return a declarator for the identifier IDENT (which may be
6673 NULL_TREE for an abstract declarator). */
6675 struct c_declarator *
6676 build_id_declarator (tree ident)
6678 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6679 ret->kind = cdk_id;
6680 ret->declarator = 0;
6681 ret->u.id = ident;
6682 /* Default value - may get reset to a more precise location. */
6683 ret->id_loc = input_location;
6684 return ret;
6687 /* Return something to represent absolute declarators containing a *.
6688 TARGET is the absolute declarator that the * contains.
6689 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6690 to apply to the pointer type. */
6692 struct c_declarator *
6693 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6694 struct c_declarator *target)
6696 tree attrs;
6697 int quals = 0;
6698 struct c_declarator *itarget = target;
6699 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6700 if (type_quals_attrs)
6702 attrs = type_quals_attrs->attrs;
6703 quals = quals_from_declspecs (type_quals_attrs);
6704 if (attrs != NULL_TREE)
6705 itarget = build_attrs_declarator (attrs, target);
6707 ret->kind = cdk_pointer;
6708 ret->declarator = itarget;
6709 ret->u.pointer_quals = quals;
6710 return ret;
6713 /* Return a pointer to a structure for an empty list of declaration
6714 specifiers. */
6716 struct c_declspecs *
6717 build_null_declspecs (void)
6719 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6720 ret->type = 0;
6721 ret->decl_attr = 0;
6722 ret->attrs = 0;
6723 ret->typespec_word = cts_none;
6724 ret->storage_class = csc_none;
6725 ret->declspecs_seen_p = false;
6726 ret->type_seen_p = false;
6727 ret->non_sc_seen_p = false;
6728 ret->typedef_p = false;
6729 ret->tag_defined_p = false;
6730 ret->explicit_signed_p = false;
6731 ret->deprecated_p = false;
6732 ret->default_int_p = false;
6733 ret->long_p = false;
6734 ret->long_long_p = false;
6735 ret->short_p = false;
6736 ret->signed_p = false;
6737 ret->unsigned_p = false;
6738 ret->complex_p = false;
6739 ret->inline_p = false;
6740 ret->thread_p = false;
6741 ret->const_p = false;
6742 ret->volatile_p = false;
6743 ret->restrict_p = false;
6744 return ret;
6747 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6748 returning SPECS. */
6750 struct c_declspecs *
6751 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6753 enum rid i;
6754 bool dupe = false;
6755 specs->non_sc_seen_p = true;
6756 specs->declspecs_seen_p = true;
6757 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6758 && C_IS_RESERVED_WORD (qual));
6759 i = C_RID_CODE (qual);
6760 switch (i)
6762 case RID_CONST:
6763 dupe = specs->const_p;
6764 specs->const_p = true;
6765 break;
6766 case RID_VOLATILE:
6767 dupe = specs->volatile_p;
6768 specs->volatile_p = true;
6769 break;
6770 case RID_RESTRICT:
6771 dupe = specs->restrict_p;
6772 specs->restrict_p = true;
6773 break;
6774 default:
6775 gcc_unreachable ();
6777 if (dupe && pedantic && !flag_isoc99)
6778 pedwarn ("duplicate %qE", qual);
6779 return specs;
6782 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6783 returning SPECS. */
6785 struct c_declspecs *
6786 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6788 tree type = spec.spec;
6789 specs->non_sc_seen_p = true;
6790 specs->declspecs_seen_p = true;
6791 specs->type_seen_p = true;
6792 if (TREE_DEPRECATED (type))
6793 specs->deprecated_p = true;
6795 /* Handle type specifier keywords. */
6796 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6798 enum rid i = C_RID_CODE (type);
6799 if (specs->type)
6801 error ("two or more data types in declaration specifiers");
6802 return specs;
6804 if ((int) i <= (int) RID_LAST_MODIFIER)
6806 /* "long", "short", "signed", "unsigned" or "_Complex". */
6807 bool dupe = false;
6808 switch (i)
6810 case RID_LONG:
6811 if (specs->long_long_p)
6813 error ("%<long long long%> is too long for GCC");
6814 break;
6816 if (specs->long_p)
6818 if (specs->typespec_word == cts_double)
6820 error ("both %<long long%> and %<double%> in "
6821 "declaration specifiers");
6822 break;
6824 if (pedantic && !flag_isoc99 && !in_system_header
6825 && warn_long_long)
6826 pedwarn ("ISO C90 does not support %<long long%>");
6827 specs->long_long_p = 1;
6828 break;
6830 if (specs->short_p)
6831 error ("both %<long%> and %<short%> in "
6832 "declaration specifiers");
6833 else if (specs->typespec_word == cts_void)
6834 error ("both %<long%> and %<void%> in "
6835 "declaration specifiers");
6836 else if (specs->typespec_word == cts_bool)
6837 error ("both %<long%> and %<_Bool%> in "
6838 "declaration specifiers");
6839 else if (specs->typespec_word == cts_char)
6840 error ("both %<long%> and %<char%> in "
6841 "declaration specifiers");
6842 else if (specs->typespec_word == cts_float)
6843 error ("both %<long%> and %<float%> in "
6844 "declaration specifiers");
6845 else
6846 specs->long_p = true;
6847 break;
6848 case RID_SHORT:
6849 dupe = specs->short_p;
6850 if (specs->long_p)
6851 error ("both %<long%> and %<short%> in "
6852 "declaration specifiers");
6853 else if (specs->typespec_word == cts_void)
6854 error ("both %<short%> and %<void%> in "
6855 "declaration specifiers");
6856 else if (specs->typespec_word == cts_bool)
6857 error ("both %<short%> and %<_Bool%> in "
6858 "declaration specifiers");
6859 else if (specs->typespec_word == cts_char)
6860 error ("both %<short%> and %<char%> in "
6861 "declaration specifiers");
6862 else if (specs->typespec_word == cts_float)
6863 error ("both %<short%> and %<float%> in "
6864 "declaration specifiers");
6865 else if (specs->typespec_word == cts_double)
6866 error ("both %<short%> and %<double%> in "
6867 "declaration specifiers");
6868 else
6869 specs->short_p = true;
6870 break;
6871 case RID_SIGNED:
6872 dupe = specs->signed_p;
6873 if (specs->unsigned_p)
6874 error ("both %<signed%> and %<unsigned%> in "
6875 "declaration specifiers");
6876 else if (specs->typespec_word == cts_void)
6877 error ("both %<signed%> and %<void%> in "
6878 "declaration specifiers");
6879 else if (specs->typespec_word == cts_bool)
6880 error ("both %<signed%> and %<_Bool%> in "
6881 "declaration specifiers");
6882 else if (specs->typespec_word == cts_float)
6883 error ("both %<signed%> and %<float%> in "
6884 "declaration specifiers");
6885 else if (specs->typespec_word == cts_double)
6886 error ("both %<signed%> and %<double%> in "
6887 "declaration specifiers");
6888 else
6889 specs->signed_p = true;
6890 break;
6891 case RID_UNSIGNED:
6892 dupe = specs->unsigned_p;
6893 if (specs->signed_p)
6894 error ("both %<signed%> and %<unsigned%> in "
6895 "declaration specifiers");
6896 else if (specs->typespec_word == cts_void)
6897 error ("both %<unsigned%> and %<void%> in "
6898 "declaration specifiers");
6899 else if (specs->typespec_word == cts_bool)
6900 error ("both %<unsigned%> and %<_Bool%> in "
6901 "declaration specifiers");
6902 else if (specs->typespec_word == cts_float)
6903 error ("both %<unsigned%> and %<float%> in "
6904 "declaration specifiers");
6905 else if (specs->typespec_word == cts_double)
6906 error ("both %<unsigned%> and %<double%> in "
6907 "declaration specifiers");
6908 else
6909 specs->unsigned_p = true;
6910 break;
6911 case RID_COMPLEX:
6912 dupe = specs->complex_p;
6913 if (pedantic && !flag_isoc99 && !in_system_header)
6914 pedwarn ("ISO C90 does not support complex types");
6915 if (specs->typespec_word == cts_void)
6916 error ("both %<complex%> and %<void%> in "
6917 "declaration specifiers");
6918 else if (specs->typespec_word == cts_bool)
6919 error ("both %<complex%> and %<_Bool%> in "
6920 "declaration specifiers");
6921 else
6922 specs->complex_p = true;
6923 break;
6924 default:
6925 gcc_unreachable ();
6928 if (dupe)
6929 error ("duplicate %qE", type);
6931 return specs;
6933 else
6935 /* "void", "_Bool", "char", "int", "float" or "double". */
6936 if (specs->typespec_word != cts_none)
6938 error ("two or more data types in declaration specifiers");
6939 return specs;
6941 switch (i)
6943 case RID_VOID:
6944 if (specs->long_p)
6945 error ("both %<long%> and %<void%> in "
6946 "declaration specifiers");
6947 else if (specs->short_p)
6948 error ("both %<short%> and %<void%> in "
6949 "declaration specifiers");
6950 else if (specs->signed_p)
6951 error ("both %<signed%> and %<void%> in "
6952 "declaration specifiers");
6953 else if (specs->unsigned_p)
6954 error ("both %<unsigned%> and %<void%> in "
6955 "declaration specifiers");
6956 else if (specs->complex_p)
6957 error ("both %<complex%> and %<void%> in "
6958 "declaration specifiers");
6959 else
6960 specs->typespec_word = cts_void;
6961 return specs;
6962 case RID_BOOL:
6963 if (specs->long_p)
6964 error ("both %<long%> and %<_Bool%> in "
6965 "declaration specifiers");
6966 else if (specs->short_p)
6967 error ("both %<short%> and %<_Bool%> in "
6968 "declaration specifiers");
6969 else if (specs->signed_p)
6970 error ("both %<signed%> and %<_Bool%> in "
6971 "declaration specifiers");
6972 else if (specs->unsigned_p)
6973 error ("both %<unsigned%> and %<_Bool%> in "
6974 "declaration specifiers");
6975 else if (specs->complex_p)
6976 error ("both %<complex%> and %<_Bool%> in "
6977 "declaration specifiers");
6978 else
6979 specs->typespec_word = cts_bool;
6980 return specs;
6981 case RID_CHAR:
6982 if (specs->long_p)
6983 error ("both %<long%> and %<char%> in "
6984 "declaration specifiers");
6985 else if (specs->short_p)
6986 error ("both %<short%> and %<char%> in "
6987 "declaration specifiers");
6988 else
6989 specs->typespec_word = cts_char;
6990 return specs;
6991 case RID_INT:
6992 specs->typespec_word = cts_int;
6993 return specs;
6994 case RID_FLOAT:
6995 if (specs->long_p)
6996 error ("both %<long%> and %<float%> in "
6997 "declaration specifiers");
6998 else if (specs->short_p)
6999 error ("both %<short%> and %<float%> in "
7000 "declaration specifiers");
7001 else if (specs->signed_p)
7002 error ("both %<signed%> and %<float%> in "
7003 "declaration specifiers");
7004 else if (specs->unsigned_p)
7005 error ("both %<unsigned%> and %<float%> in "
7006 "declaration specifiers");
7007 else
7008 specs->typespec_word = cts_float;
7009 return specs;
7010 case RID_DOUBLE:
7011 if (specs->long_long_p)
7012 error ("both %<long long%> and %<double%> in "
7013 "declaration specifiers");
7014 else if (specs->short_p)
7015 error ("both %<short%> and %<double%> in "
7016 "declaration specifiers");
7017 else if (specs->signed_p)
7018 error ("both %<signed%> and %<double%> in "
7019 "declaration specifiers");
7020 else if (specs->unsigned_p)
7021 error ("both %<unsigned%> and %<double%> in "
7022 "declaration specifiers");
7023 else
7024 specs->typespec_word = cts_double;
7025 return specs;
7026 default:
7027 /* ObjC reserved word "id", handled below. */
7028 break;
7033 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7034 form of ObjC type, cases such as "int" and "long" being handled
7035 above), a TYPE (struct, union, enum and typeof specifiers) or an
7036 ERROR_MARK. In none of these cases may there have previously
7037 been any type specifiers. */
7038 if (specs->type || specs->typespec_word != cts_none
7039 || specs->long_p || specs->short_p || specs->signed_p
7040 || specs->unsigned_p || specs->complex_p)
7041 error ("two or more data types in declaration specifiers");
7042 else if (TREE_CODE (type) == TYPE_DECL)
7044 if (TREE_TYPE (type) == error_mark_node)
7045 ; /* Allow the type to default to int to avoid cascading errors. */
7046 else
7048 specs->type = TREE_TYPE (type);
7049 specs->decl_attr = DECL_ATTRIBUTES (type);
7050 specs->typedef_p = true;
7051 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7054 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7056 tree t = lookup_name (type);
7057 if (!t || TREE_CODE (t) != TYPE_DECL)
7058 error ("%qE fails to be a typedef or built in type", type);
7059 else if (TREE_TYPE (t) == error_mark_node)
7061 else
7062 specs->type = TREE_TYPE (t);
7064 else if (TREE_CODE (type) != ERROR_MARK)
7066 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7067 specs->tag_defined_p = true;
7068 if (spec.kind == ctsk_typeof)
7069 specs->typedef_p = true;
7070 specs->type = type;
7073 return specs;
7076 /* Add the storage class specifier or function specifier SCSPEC to the
7077 declaration specifiers SPECS, returning SPECS. */
7079 struct c_declspecs *
7080 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7082 enum rid i;
7083 enum c_storage_class n = csc_none;
7084 bool dupe = false;
7085 specs->declspecs_seen_p = true;
7086 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7087 && C_IS_RESERVED_WORD (scspec));
7088 i = C_RID_CODE (scspec);
7089 if (extra_warnings && specs->non_sc_seen_p)
7090 warning (0, "%qE is not at beginning of declaration", scspec);
7091 switch (i)
7093 case RID_INLINE:
7094 /* C99 permits duplicate inline. Although of doubtful utility,
7095 it seems simplest to permit it in gnu89 mode as well, as
7096 there is also little utility in maintaining this as a
7097 difference between gnu89 and C99 inline. */
7098 dupe = false;
7099 specs->inline_p = true;
7100 break;
7101 case RID_THREAD:
7102 dupe = specs->thread_p;
7103 if (specs->storage_class == csc_auto)
7104 error ("%<__thread%> used with %<auto%>");
7105 else if (specs->storage_class == csc_register)
7106 error ("%<__thread%> used with %<register%>");
7107 else if (specs->storage_class == csc_typedef)
7108 error ("%<__thread%> used with %<typedef%>");
7109 else
7110 specs->thread_p = true;
7111 break;
7112 case RID_AUTO:
7113 n = csc_auto;
7114 break;
7115 case RID_EXTERN:
7116 n = csc_extern;
7117 /* Diagnose "__thread extern". */
7118 if (specs->thread_p)
7119 error ("%<__thread%> before %<extern%>");
7120 break;
7121 case RID_REGISTER:
7122 n = csc_register;
7123 break;
7124 case RID_STATIC:
7125 n = csc_static;
7126 /* Diagnose "__thread static". */
7127 if (specs->thread_p)
7128 error ("%<__thread%> before %<static%>");
7129 break;
7130 case RID_TYPEDEF:
7131 n = csc_typedef;
7132 break;
7133 default:
7134 gcc_unreachable ();
7136 if (n != csc_none && n == specs->storage_class)
7137 dupe = true;
7138 if (dupe)
7139 error ("duplicate %qE", scspec);
7140 if (n != csc_none)
7142 if (specs->storage_class != csc_none && n != specs->storage_class)
7144 error ("multiple storage classes in declaration specifiers");
7146 else
7148 specs->storage_class = n;
7149 if (n != csc_extern && n != csc_static && specs->thread_p)
7151 error ("%<__thread%> used with %qE", scspec);
7152 specs->thread_p = false;
7156 return specs;
7159 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7160 returning SPECS. */
7162 struct c_declspecs *
7163 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7165 specs->attrs = chainon (attrs, specs->attrs);
7166 specs->declspecs_seen_p = true;
7167 return specs;
7170 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7171 specifiers with any other type specifier to determine the resulting
7172 type. This is where ISO C checks on complex types are made, since
7173 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7174 double". */
7176 struct c_declspecs *
7177 finish_declspecs (struct c_declspecs *specs)
7179 /* If a type was specified as a whole, we have no modifiers and are
7180 done. */
7181 if (specs->type != NULL_TREE)
7183 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7184 && !specs->signed_p && !specs->unsigned_p
7185 && !specs->complex_p);
7186 return specs;
7189 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7190 has been specified, treat it as "int" unless "_Complex" is
7191 present and there are no other specifiers. If we just have
7192 "_Complex", it is equivalent to "_Complex double", but e.g.
7193 "_Complex short" is equivalent to "_Complex short int". */
7194 if (specs->typespec_word == cts_none)
7196 if (specs->long_p || specs->short_p
7197 || specs->signed_p || specs->unsigned_p)
7199 specs->typespec_word = cts_int;
7201 else if (specs->complex_p)
7203 specs->typespec_word = cts_double;
7204 if (pedantic)
7205 pedwarn ("ISO C does not support plain %<complex%> meaning "
7206 "%<double complex%>");
7208 else
7210 specs->typespec_word = cts_int;
7211 specs->default_int_p = true;
7212 /* We don't diagnose this here because grokdeclarator will
7213 give more specific diagnostics according to whether it is
7214 a function definition. */
7218 /* If "signed" was specified, record this to distinguish "int" and
7219 "signed int" in the case of a bit-field with
7220 -funsigned-bitfields. */
7221 specs->explicit_signed_p = specs->signed_p;
7223 /* Now compute the actual type. */
7224 switch (specs->typespec_word)
7226 case cts_void:
7227 gcc_assert (!specs->long_p && !specs->short_p
7228 && !specs->signed_p && !specs->unsigned_p
7229 && !specs->complex_p);
7230 specs->type = void_type_node;
7231 break;
7232 case cts_bool:
7233 gcc_assert (!specs->long_p && !specs->short_p
7234 && !specs->signed_p && !specs->unsigned_p
7235 && !specs->complex_p);
7236 specs->type = boolean_type_node;
7237 break;
7238 case cts_char:
7239 gcc_assert (!specs->long_p && !specs->short_p);
7240 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7241 if (specs->signed_p)
7242 specs->type = signed_char_type_node;
7243 else if (specs->unsigned_p)
7244 specs->type = unsigned_char_type_node;
7245 else
7246 specs->type = char_type_node;
7247 if (specs->complex_p)
7249 if (pedantic)
7250 pedwarn ("ISO C does not support complex integer types");
7251 specs->type = build_complex_type (specs->type);
7253 break;
7254 case cts_int:
7255 gcc_assert (!(specs->long_p && specs->short_p));
7256 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7257 if (specs->long_long_p)
7258 specs->type = (specs->unsigned_p
7259 ? long_long_unsigned_type_node
7260 : long_long_integer_type_node);
7261 else if (specs->long_p)
7262 specs->type = (specs->unsigned_p
7263 ? long_unsigned_type_node
7264 : long_integer_type_node);
7265 else if (specs->short_p)
7266 specs->type = (specs->unsigned_p
7267 ? short_unsigned_type_node
7268 : short_integer_type_node);
7269 else
7270 specs->type = (specs->unsigned_p
7271 ? unsigned_type_node
7272 : integer_type_node);
7273 if (specs->complex_p)
7275 if (pedantic)
7276 pedwarn ("ISO C does not support complex integer types");
7277 specs->type = build_complex_type (specs->type);
7279 break;
7280 case cts_float:
7281 gcc_assert (!specs->long_p && !specs->short_p
7282 && !specs->signed_p && !specs->unsigned_p);
7283 specs->type = (specs->complex_p
7284 ? complex_float_type_node
7285 : float_type_node);
7286 break;
7287 case cts_double:
7288 gcc_assert (!specs->long_long_p && !specs->short_p
7289 && !specs->signed_p && !specs->unsigned_p);
7290 if (specs->long_p)
7292 specs->type = (specs->complex_p
7293 ? complex_long_double_type_node
7294 : long_double_type_node);
7296 else
7298 specs->type = (specs->complex_p
7299 ? complex_double_type_node
7300 : double_type_node);
7302 break;
7303 default:
7304 gcc_unreachable ();
7307 return specs;
7310 /* Synthesize a function which calls all the global ctors or global
7311 dtors in this file. This is only used for targets which do not
7312 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7313 static void
7314 build_cdtor (int method_type, tree cdtors)
7316 tree body = 0;
7318 if (!cdtors)
7319 return;
7321 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7322 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7323 &body);
7325 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7328 /* Perform final processing on one file scope's declarations (or the
7329 external scope's declarations), GLOBALS. */
7330 static void
7331 c_write_global_declarations_1 (tree globals)
7333 size_t len = list_length (globals);
7334 tree *vec = XNEWVEC (tree, len);
7335 size_t i;
7336 tree decl;
7338 /* Process the decls in the order they were written. */
7339 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7341 vec[i] = decl;
7342 /* Check for used but undefined static functions using the C
7343 standard's definition of "used", and set TREE_NO_WARNING so
7344 that check_global_declarations doesn't repeat the check. */
7345 if (TREE_CODE (decl) == FUNCTION_DECL
7346 && DECL_INITIAL (decl) == 0
7347 && DECL_EXTERNAL (decl)
7348 && !TREE_PUBLIC (decl)
7349 && C_DECL_USED (decl))
7351 pedwarn ("%J%qF used but never defined", decl, decl);
7352 TREE_NO_WARNING (decl) = 1;
7356 wrapup_global_declarations (vec, len);
7357 check_global_declarations (vec, len);
7359 free (vec);
7362 void
7363 c_write_global_declarations (void)
7365 tree ext_block, t;
7367 /* We don't want to do this if generating a PCH. */
7368 if (pch_file)
7369 return;
7371 /* Don't waste time on further processing if -fsyntax-only or we've
7372 encountered errors. */
7373 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7374 return;
7376 /* Close the external scope. */
7377 ext_block = pop_scope ();
7378 external_scope = 0;
7379 gcc_assert (!current_scope);
7381 /* Process all file scopes in this compilation, and the external_scope,
7382 through wrapup_global_declarations and check_global_declarations. */
7383 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7384 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7385 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7387 /* Generate functions to call static constructors and destructors
7388 for targets that do not support .ctors/.dtors sections. These
7389 functions have magic names which are detected by collect2. */
7390 build_cdtor ('I', static_ctors); static_ctors = 0;
7391 build_cdtor ('D', static_dtors); static_dtors = 0;
7393 /* We're done parsing; proceed to optimize and emit assembly.
7394 FIXME: shouldn't be the front end's responsibility to call this. */
7395 cgraph_optimize ();
7398 #include "gt-c-decl.h"