gengtype.h (options::info): Change type to const char *.
[official-gcc.git] / gcc / c-decl.c
blob27860b4bd29916171d89ec01e7bdaee3e1ffedfe
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "libfuncs.h"
60 #include "except.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 enum decl_context
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
74 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* True means we've initialized exception handling. */
80 bool c_eh_initialized_p;
82 /* While defining an enum type, this is 1 plus the last enumerator
83 constant value. Note that will do not have to save this or `enum_overflow'
84 around nested function definition since such a definition could only
85 occur in an enum value expression and we don't use these variables in
86 that case. */
88 static tree enum_next_value;
90 /* Nonzero means that there was overflow computing enum_next_value. */
92 static int enum_overflow;
94 /* These #defines are for clarity in working with the information block
95 returned by get_parm_info. */
96 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
97 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
98 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
99 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
101 /* The file and line that the prototype came from if this is an
102 old-style definition; used for diagnostics in
103 store_parm_decls_oldstyle. */
105 static location_t current_function_prototype_locus;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* State saving variables. */
112 tree c_break_label;
113 tree c_cont_label;
115 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
116 included in this invocation. Note that the current translation
117 unit is not included in this list. */
119 static GTY(()) tree all_translation_units;
121 /* A list of decls to be made automatically visible in each file scope. */
122 static GTY(()) tree visible_builtins;
124 /* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement that specifies a return value is seen. */
127 int current_function_returns_value;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement with no argument is seen. */
132 int current_function_returns_null;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a call to a noreturn function is seen. */
137 int current_function_returns_abnormally;
139 /* Set to nonzero by `grokdeclarator' for a function
140 whose return type is defaulted, if warnings for this are desired. */
142 static int warn_about_return_type;
144 /* Nonzero when starting a function declared `extern inline'. */
146 static int current_extern_inline;
148 /* True means global_bindings_p should return false even if the scope stack
149 says we are in file scope. */
150 bool c_override_global_bindings_to_false;
153 /* Each c_binding structure describes one binding of an identifier to
154 a decl. All the decls in a scope - irrespective of namespace - are
155 chained together by the ->prev field, which (as the name implies)
156 runs in reverse order. All the decls in a given namespace bound to
157 a given identifier are chained by the ->shadowed field, which runs
158 from inner to outer scopes.
160 The ->decl field usually points to a DECL node, but there are two
161 exceptions. In the namespace of type tags, the bound entity is a
162 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
163 identifier is encountered, it is bound to error_mark_node to
164 suppress further errors about that identifier in the current
165 function.
167 The depth field is copied from the scope structure that holds this
168 decl. It is used to preserve the proper ordering of the ->shadowed
169 field (see bind()) and also for a handful of special-case checks.
170 Finally, the invisible bit is true for a decl which should be
171 ignored for purposes of normal name lookup, and the nested bit is
172 true for a decl that's been bound a second time in an inner scope;
173 in all such cases, the binding in the outer scope will have its
174 invisible bit true. */
176 struct c_binding GTY((chain_next ("%h.prev")))
178 tree decl; /* the decl bound */
179 tree id; /* the identifier it's bound to */
180 struct c_binding *prev; /* the previous decl in this scope */
181 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
182 unsigned int depth : 28; /* depth of this scope */
183 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
184 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
185 /* two free bits */
187 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
188 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
189 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
190 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
192 #define I_SYMBOL_BINDING(node) \
193 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
194 #define I_SYMBOL_DECL(node) \
195 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
197 #define I_TAG_BINDING(node) \
198 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
199 #define I_TAG_DECL(node) \
200 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
202 #define I_LABEL_BINDING(node) \
203 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
204 #define I_LABEL_DECL(node) \
205 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
207 /* Each C symbol points to three linked lists of c_binding structures.
208 These describe the values of the identifier in the three different
209 namespaces defined by the language. */
211 struct lang_identifier GTY(())
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The resulting tree type. */
225 union lang_tree_node
226 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
227 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)")))
229 union tree_node GTY ((tag ("0"),
230 desc ("tree_node_structure (&%h)")))
231 generic;
232 struct lang_identifier GTY ((tag ("1"))) identifier;
235 /* Each c_scope structure describes the complete contents of one
236 scope. Four scopes are distinguished specially: the innermost or
237 current scope, the innermost function scope, the file scope (always
238 the second to outermost) and the outermost or external scope.
240 Most declarations are recorded in the current scope.
242 All normal label declarations are recorded in the innermost
243 function scope, as are bindings of undeclared identifiers to
244 error_mark_node. (GCC permits nested functions as an extension,
245 hence the 'innermost' qualifier.) Explicitly declared labels
246 (using the __label__ extension) appear in the current scope.
248 Being in the file scope (current_scope == file_scope) causes
249 special behavior in several places below. Also, under some
250 conditions the Objective-C front end records declarations in the
251 file scope even though that isn't the current scope.
253 All declarations with external linkage are recorded in the external
254 scope, even if they aren't visible there; this models the fact that
255 such declarations are visible to the entire program, and (with a
256 bit of cleverness, see pushdecl) allows diagnosis of some violations
257 of C99 6.2.2p7 and 6.2.7p2:
259 If, within the same translation unit, the same identifier appears
260 with both internal and external linkage, the behavior is
261 undefined.
263 All declarations that refer to the same object or function shall
264 have compatible type; otherwise, the behavior is undefined.
266 Initially only the built-in declarations, which describe compiler
267 intrinsic functions plus a subset of the standard library, are in
268 this scope.
270 The order of the blocks list matters, and it is frequently appended
271 to. To avoid having to walk all the way to the end of the list on
272 each insertion, or reverse the list later, we maintain a pointer to
273 the last list entry. (FIXME: It should be feasible to use a reversed
274 list here.)
276 The bindings list is strictly in reverse order of declarations;
277 pop_scope relies on this. */
280 struct c_scope GTY((chain_next ("%h.outer")))
282 /* The scope containing this one. */
283 struct c_scope *outer;
285 /* The next outermost function scope. */
286 struct c_scope *outer_function;
288 /* All bindings in this scope. */
289 struct c_binding *bindings;
291 /* For each scope (except the global one), a chain of BLOCK nodes
292 for all the scopes that were entered and exited one level down. */
293 tree blocks;
294 tree blocks_last;
296 /* The depth of this scope. Used to keep the ->shadowed chain of
297 bindings sorted innermost to outermost. */
298 unsigned int depth : 28;
300 /* True if we are currently filling this scope with parameter
301 declarations. */
302 BOOL_BITFIELD parm_flag : 1;
304 /* True if we already complained about forward parameter decls
305 in this scope. This prevents double warnings on
306 foo (int a; int b; ...) */
307 BOOL_BITFIELD warned_forward_parm_decls : 1;
309 /* True if this is the outermost block scope of a function body.
310 This scope contains the parameters, the local variables declared
311 in the outermost block, and all the labels (except those in
312 nested functions, or declared at block scope with __label__). */
313 BOOL_BITFIELD function_body : 1;
315 /* True means make a BLOCK for this scope no matter what. */
316 BOOL_BITFIELD keep : 1;
319 /* The scope currently in effect. */
321 static GTY(()) struct c_scope *current_scope;
323 /* The innermost function scope. Ordinary (not explicitly declared)
324 labels, bindings to error_mark_node, and the lazily-created
325 bindings of __func__ and its friends get this scope. */
327 static GTY(()) struct c_scope *current_function_scope;
329 /* The C file scope. This is reset for each input translation unit. */
331 static GTY(()) struct c_scope *file_scope;
333 /* The outermost scope. This is used for all declarations with
334 external linkage, and only these, hence the name. */
336 static GTY(()) struct c_scope *external_scope;
338 /* A chain of c_scope structures awaiting reuse. */
340 static GTY((deletable)) struct c_scope *scope_freelist;
342 /* A chain of c_binding structures awaiting reuse. */
344 static GTY((deletable)) struct c_binding *binding_freelist;
346 /* Append VAR to LIST in scope SCOPE. */
347 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
348 struct c_scope *s_ = (scope); \
349 tree d_ = (decl); \
350 if (s_->list##_last) \
351 TREE_CHAIN (s_->list##_last) = d_; \
352 else \
353 s_->list = d_; \
354 s_->list##_last = d_; \
355 } while (0)
357 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
358 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
359 struct c_scope *t_ = (tscope); \
360 struct c_scope *f_ = (fscope); \
361 if (t_->to##_last) \
362 TREE_CHAIN (t_->to##_last) = f_->from; \
363 else \
364 t_->to = f_->from; \
365 t_->to##_last = f_->from##_last; \
366 } while (0)
368 /* True means unconditionally make a BLOCK for the next scope pushed. */
370 static bool keep_next_level_flag;
372 /* True means the next call to push_scope will be the outermost scope
373 of a function body, so do not push a new scope, merely cease
374 expecting parameter decls. */
376 static bool next_is_function_body;
378 /* Functions called automatically at the beginning and end of execution. */
380 static GTY(()) tree static_ctors;
381 static GTY(()) tree static_dtors;
383 /* Forward declarations. */
384 static tree lookup_name_in_scope (tree, struct c_scope *);
385 static tree c_make_fname_decl (tree, int);
386 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
387 static tree grokparms (tree, int);
388 static void layout_array_type (tree);
390 /* States indicating how grokdeclarator() should handle declspecs marked
391 with __attribute__((deprecated)). An object declared as
392 __attribute__((deprecated)) suppresses warnings of uses of other
393 deprecated items. */
395 enum deprecated_states {
396 DEPRECATED_NORMAL,
397 DEPRECATED_SUPPRESS
400 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
402 void
403 c_print_identifier (FILE *file, tree node, int indent)
405 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
406 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
407 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
408 if (C_IS_RESERVED_WORD (node))
410 tree rid = ridpointers[C_RID_CODE (node)];
411 indent_to (file, indent + 4);
412 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
413 (void *) rid, IDENTIFIER_POINTER (rid));
417 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
418 which may be any of several kinds of DECL or TYPE or error_mark_node,
419 in the scope SCOPE. */
420 static void
421 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
423 struct c_binding *b, **here;
425 if (binding_freelist)
427 b = binding_freelist;
428 binding_freelist = b->prev;
430 else
431 b = GGC_NEW (struct c_binding);
433 b->shadowed = 0;
434 b->decl = decl;
435 b->id = name;
436 b->depth = scope->depth;
437 b->invisible = invisible;
438 b->nested = nested;
440 b->prev = scope->bindings;
441 scope->bindings = b;
443 if (!name)
444 return;
446 switch (TREE_CODE (decl))
448 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
449 case ENUMERAL_TYPE:
450 case UNION_TYPE:
451 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
452 case VAR_DECL:
453 case FUNCTION_DECL:
454 case TYPE_DECL:
455 case CONST_DECL:
456 case PARM_DECL:
457 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
459 default:
460 abort ();
463 /* Locate the appropriate place in the chain of shadowed decls
464 to insert this binding. Normally, scope == current_scope and
465 this does nothing. */
466 while (*here && (*here)->depth > scope->depth)
467 here = &(*here)->shadowed;
469 b->shadowed = *here;
470 *here = b;
473 /* Clear the binding structure B, stick it on the binding_freelist,
474 and return the former value of b->prev. This is used by pop_scope
475 and get_parm_info to iterate destructively over all the bindings
476 from a given scope. */
477 static struct c_binding *
478 free_binding_and_advance (struct c_binding *b)
480 struct c_binding *prev = b->prev;
482 memset (b, 0, sizeof (struct c_binding));
483 b->prev = binding_freelist;
484 binding_freelist = b;
486 return prev;
490 /* Hook called at end of compilation to assume 1 elt
491 for a file-scope tentative array defn that wasn't complete before. */
493 void
494 c_finish_incomplete_decl (tree decl)
496 if (TREE_CODE (decl) == VAR_DECL)
498 tree type = TREE_TYPE (decl);
499 if (type != error_mark_node
500 && TREE_CODE (type) == ARRAY_TYPE
501 && ! DECL_EXTERNAL (decl)
502 && TYPE_DOMAIN (type) == 0)
504 warning ("%Jarray '%D' assumed to have one element", decl, decl);
506 complete_array_type (type, NULL_TREE, 1);
508 layout_decl (decl, 0);
513 /* The Objective-C front-end often needs to determine the current scope. */
515 void *
516 get_current_scope (void)
518 return current_scope;
521 /* The following function is used only by Objective-C. It needs to live here
522 because it accesses the innards of c_scope. */
524 void
525 objc_mark_locals_volatile (void *enclosing_blk)
527 struct c_scope *scope;
528 struct c_binding *b;
530 for (scope = current_scope;
531 scope && scope != enclosing_blk;
532 scope = scope->outer)
534 for (b = scope->bindings; b; b = b->prev)
536 if (TREE_CODE (b->decl) == VAR_DECL
537 || TREE_CODE (b->decl) == PARM_DECL)
539 C_DECL_REGISTER (b->decl) = 0;
540 DECL_REGISTER (b->decl) = 0;
541 TREE_THIS_VOLATILE (b->decl) = 1;
545 /* Do not climb up past the current function. */
546 if (scope->function_body)
547 break;
551 /* Nonzero if we are currently in file scope. */
554 global_bindings_p (void)
556 return current_scope == file_scope && !c_override_global_bindings_to_false;
559 void
560 keep_next_level (void)
562 keep_next_level_flag = true;
565 /* Identify this scope as currently being filled with parameters. */
567 void
568 declare_parm_level (void)
570 current_scope->parm_flag = true;
573 void
574 push_scope (void)
576 if (next_is_function_body)
578 /* This is the transition from the parameters to the top level
579 of the function body. These are the same scope
580 (C99 6.2.1p4,6) so we do not push another scope structure.
581 next_is_function_body is set only by store_parm_decls, which
582 in turn is called when and only when we are about to
583 encounter the opening curly brace for the function body.
585 The outermost block of a function always gets a BLOCK node,
586 because the debugging output routines expect that each
587 function has at least one BLOCK. */
588 current_scope->parm_flag = false;
589 current_scope->function_body = true;
590 current_scope->keep = true;
591 current_scope->outer_function = current_function_scope;
592 current_function_scope = current_scope;
594 keep_next_level_flag = false;
595 next_is_function_body = false;
597 else
599 struct c_scope *scope;
600 if (scope_freelist)
602 scope = scope_freelist;
603 scope_freelist = scope->outer;
605 else
606 scope = GGC_CNEW (struct c_scope);
608 scope->keep = keep_next_level_flag;
609 scope->outer = current_scope;
610 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
612 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
613 possible. */
614 if (current_scope && scope->depth == 0)
616 scope->depth--;
617 sorry ("GCC supports only %u nested scopes\n", scope->depth);
620 current_scope = scope;
621 keep_next_level_flag = false;
625 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
627 static void
628 set_type_context (tree type, tree context)
630 for (type = TYPE_MAIN_VARIANT (type); type;
631 type = TYPE_NEXT_VARIANT (type))
632 TYPE_CONTEXT (type) = context;
635 /* Exit a scope. Restore the state of the identifier-decl mappings
636 that were in effect when this scope was entered. Return a BLOCK
637 node containing all the DECLs in this scope that are of interest
638 to debug info generation. */
640 tree
641 pop_scope (void)
643 struct c_scope *scope = current_scope;
644 tree block, context, p;
645 struct c_binding *b;
647 bool functionbody = scope->function_body;
648 bool keep = functionbody || scope->keep || scope->bindings;
650 /* If appropriate, create a BLOCK to record the decls for the life
651 of this function. */
652 block = 0;
653 if (keep)
655 block = make_node (BLOCK);
656 BLOCK_SUBBLOCKS (block) = scope->blocks;
657 TREE_USED (block) = 1;
659 /* In each subblock, record that this is its superior. */
660 for (p = scope->blocks; p; p = TREE_CHAIN (p))
661 BLOCK_SUPERCONTEXT (p) = block;
663 BLOCK_VARS (block) = 0;
666 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
667 scope must be set so that they point to the appropriate
668 construct, i.e. either to the current FUNCTION_DECL node, or
669 else to the BLOCK node we just constructed.
671 Note that for tagged types whose scope is just the formal
672 parameter list for some function type specification, we can't
673 properly set their TYPE_CONTEXTs here, because we don't have a
674 pointer to the appropriate FUNCTION_TYPE node readily available
675 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
676 type nodes get set in `grokdeclarator' as soon as we have created
677 the FUNCTION_TYPE node which will represent the "scope" for these
678 "parameter list local" tagged types. */
679 if (scope->function_body)
680 context = current_function_decl;
681 else if (scope == file_scope)
683 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
684 TREE_CHAIN (file_decl) = all_translation_units;
685 all_translation_units = file_decl;
686 context = file_decl;
688 else
689 context = block;
691 /* Clear all bindings in this scope. */
692 for (b = scope->bindings; b; b = free_binding_and_advance (b))
694 p = b->decl;
695 switch (TREE_CODE (p))
697 case LABEL_DECL:
698 /* Warnings for unused labels, errors for undefined labels. */
699 if (TREE_USED (p) && !DECL_INITIAL (p))
701 error ("%Jlabel `%D' used but not defined", p, p);
702 DECL_INITIAL (p) = error_mark_node;
704 else if (!TREE_USED (p) && warn_unused_label)
706 if (DECL_INITIAL (p))
707 warning ("%Jlabel `%D' defined but not used", p, p);
708 else
709 warning ("%Jlabel `%D' declared but not defined", p, p);
711 /* Labels go in BLOCK_VARS. */
712 TREE_CHAIN (p) = BLOCK_VARS (block);
713 BLOCK_VARS (block) = p;
715 #ifdef ENABLE_CHECKING
716 if (I_LABEL_BINDING (b->id) != b) abort ();
717 #endif
718 I_LABEL_BINDING (b->id) = b->shadowed;
719 break;
721 case ENUMERAL_TYPE:
722 case UNION_TYPE:
723 case RECORD_TYPE:
724 set_type_context (p, context);
726 /* Types may not have tag-names, in which case the type
727 appears in the bindings list with b->id NULL. */
728 if (b->id)
730 #ifdef ENABLE_CHECKING
731 if (I_TAG_BINDING (b->id) != b) abort ();
732 #endif
733 I_TAG_BINDING (b->id) = b->shadowed;
735 break;
737 case FUNCTION_DECL:
738 /* Propagate TREE_ADDRESSABLE from nested functions to their
739 containing functions. */
740 if (! TREE_ASM_WRITTEN (p)
741 && DECL_INITIAL (p) != 0
742 && TREE_ADDRESSABLE (p)
743 && DECL_ABSTRACT_ORIGIN (p) != 0
744 && DECL_ABSTRACT_ORIGIN (p) != p)
745 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
746 goto common_symbol;
748 case VAR_DECL:
749 /* Warnings for unused variables. */
750 if (warn_unused_variable
751 && !TREE_USED (p)
752 && !DECL_IN_SYSTEM_HEADER (p)
753 && DECL_NAME (p)
754 && !DECL_ARTIFICIAL (p)
755 && (scope != file_scope
756 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
757 && !TREE_THIS_VOLATILE (p)))
758 && scope != external_scope)
759 warning ("%Junused variable `%D'", p, p);
761 /* Fall through. */
762 case TYPE_DECL:
763 case CONST_DECL:
764 common_symbol:
765 /* All of these go in BLOCK_VARS, but only if this is the
766 binding in the home scope. */
767 if (!b->nested)
769 TREE_CHAIN (p) = BLOCK_VARS (block);
770 BLOCK_VARS (block) = p;
772 /* If this is the file scope, and we are processing more
773 than one translation unit in this compilation, set
774 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
775 This makes same_translation_unit_p work, and causes
776 static declarations to be given disambiguating suffixes. */
777 if (scope == file_scope && num_in_fnames > 1)
779 DECL_CONTEXT (p) = context;
780 if (TREE_CODE (p) == TYPE_DECL)
781 set_type_context (TREE_TYPE (p), context);
784 /* Fall through. */
785 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
786 already been put there by store_parm_decls. Unused-
787 parameter warnings are handled by function.c.
788 error_mark_node obviously does not go in BLOCK_VARS and
789 does not get unused-variable warnings. */
790 case PARM_DECL:
791 case ERROR_MARK:
792 /* It is possible for a decl not to have a name. We get
793 here with b->id NULL in this case. */
794 if (b->id)
796 #ifdef ENABLE_CHECKING
797 if (I_SYMBOL_BINDING (b->id) != b) abort ();
798 #endif
799 I_SYMBOL_BINDING (b->id) = b->shadowed;
801 break;
803 default:
804 abort ();
809 /* Dispose of the block that we just made inside some higher level. */
810 if ((scope->function_body || scope == file_scope) && context)
812 DECL_INITIAL (context) = block;
813 BLOCK_SUPERCONTEXT (block) = context;
815 else if (scope->outer)
817 if (block)
818 SCOPE_LIST_APPEND (scope->outer, blocks, block);
819 /* If we did not make a block for the scope just exited, any
820 blocks made for inner scopes must be carried forward so they
821 will later become subblocks of something else. */
822 else if (scope->blocks)
823 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
826 /* Pop the current scope, and free the structure for reuse. */
827 current_scope = scope->outer;
828 if (scope->function_body)
829 current_function_scope = scope->outer_function;
831 memset (scope, 0, sizeof (struct c_scope));
832 scope->outer = scope_freelist;
833 scope_freelist = scope;
835 return block;
838 void
839 push_file_scope (void)
841 tree decl;
843 if (file_scope)
844 return;
846 push_scope ();
847 file_scope = current_scope;
849 start_fname_decls ();
851 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
852 bind (DECL_NAME (decl), decl, file_scope,
853 /*invisible=*/false, /*nested=*/true);
856 void
857 pop_file_scope (void)
859 /* In case there were missing closebraces, get us back to the global
860 binding level. */
861 while (current_scope != file_scope)
862 pop_scope ();
864 /* __FUNCTION__ is defined at file scope (""). This
865 call may not be necessary as my tests indicate it
866 still works without it. */
867 finish_fname_decls ();
869 /* This is the point to write out a PCH if we're doing that.
870 In that case we do not want to do anything else. */
871 if (pch_file)
873 c_common_write_pch ();
874 return;
877 /* Pop off the file scope and close this translation unit. */
878 pop_scope ();
879 file_scope = 0;
880 cgraph_finalize_compilation_unit ();
883 /* Insert BLOCK at the end of the list of subblocks of the current
884 scope. This is used when a BIND_EXPR is expanded, to handle the
885 BLOCK node inside the BIND_EXPR. */
887 void
888 insert_block (tree block)
890 TREE_USED (block) = 1;
891 SCOPE_LIST_APPEND (current_scope, blocks, block);
894 /* Push a definition or a declaration of struct, union or enum tag "name".
895 "type" should be the type node.
896 We assume that the tag "name" is not already defined.
898 Note that the definition may really be just a forward reference.
899 In that case, the TYPE_SIZE will be zero. */
901 static void
902 pushtag (tree name, tree type)
904 /* Record the identifier as the type's name if it has none. */
905 if (name && !TYPE_NAME (type))
906 TYPE_NAME (type) = name;
907 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
909 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
910 tagged type we just added to the current scope. This fake
911 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
912 to output a representation of a tagged type, and it also gives
913 us a convenient place to record the "scope start" address for the
914 tagged type. */
916 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
918 /* An approximation for now, so we can tell this is a function-scope tag.
919 This will be updated in pop_scope. */
920 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
923 /* Subroutine of compare_decls. Allow harmless mismatches in return
924 and argument types provided that the type modes match. This function
925 return a unified type given a suitable match, and 0 otherwise. */
927 static tree
928 match_builtin_function_types (tree newtype, tree oldtype)
930 tree newrettype, oldrettype;
931 tree newargs, oldargs;
932 tree trytype, tryargs;
934 /* Accept the return type of the new declaration if same modes. */
935 oldrettype = TREE_TYPE (oldtype);
936 newrettype = TREE_TYPE (newtype);
938 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
939 return 0;
941 oldargs = TYPE_ARG_TYPES (oldtype);
942 newargs = TYPE_ARG_TYPES (newtype);
943 tryargs = newargs;
945 while (oldargs || newargs)
947 if (! oldargs
948 || ! newargs
949 || ! TREE_VALUE (oldargs)
950 || ! TREE_VALUE (newargs)
951 || TYPE_MODE (TREE_VALUE (oldargs))
952 != TYPE_MODE (TREE_VALUE (newargs)))
953 return 0;
955 oldargs = TREE_CHAIN (oldargs);
956 newargs = TREE_CHAIN (newargs);
959 trytype = build_function_type (newrettype, tryargs);
960 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
963 /* Subroutine of diagnose_mismatched_decls. Check for function type
964 mismatch involving an empty arglist vs a nonempty one and give clearer
965 diagnostics. */
966 static void
967 diagnose_arglist_conflict (tree newdecl, tree olddecl,
968 tree newtype, tree oldtype)
970 tree t;
972 if (TREE_CODE (olddecl) != FUNCTION_DECL
973 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
974 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
976 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
977 return;
979 t = TYPE_ARG_TYPES (oldtype);
980 if (t == 0)
981 t = TYPE_ARG_TYPES (newtype);
982 for (; t; t = TREE_CHAIN (t))
984 tree type = TREE_VALUE (t);
986 if (TREE_CHAIN (t) == 0
987 && TYPE_MAIN_VARIANT (type) != void_type_node)
989 inform ("a parameter list with an ellipsis can't match "
990 "an empty parameter name list declaration");
991 break;
994 if (c_type_promotes_to (type) != type)
996 inform ("an argument type that has a default promotion can't match "
997 "an empty parameter name list declaration");
998 break;
1003 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1004 old-style function definition, NEWDECL is a prototype declaration.
1005 Diagnose inconsistencies in the argument list. Returns TRUE if
1006 the prototype is compatible, FALSE if not. */
1007 static bool
1008 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1010 tree newargs, oldargs;
1011 int i;
1013 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1014 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1016 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1017 newargs = TYPE_ARG_TYPES (newtype);
1018 i = 1;
1020 for (;;)
1022 tree oldargtype = TREE_VALUE (oldargs);
1023 tree newargtype = TREE_VALUE (newargs);
1025 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1026 break;
1028 /* Reaching the end of just one list means the two decls don't
1029 agree on the number of arguments. */
1030 if (END_OF_ARGLIST (oldargtype))
1032 error ("%Jprototype for '%D' declares more arguments "
1033 "than previous old-style definition", newdecl, newdecl);
1034 return false;
1036 else if (END_OF_ARGLIST (newargtype))
1038 error ("%Jprototype for '%D' declares fewer arguments "
1039 "than previous old-style definition", newdecl, newdecl);
1040 return false;
1043 /* Type for passing arg must be consistent with that declared
1044 for the arg. */
1045 else if (! comptypes (oldargtype, newargtype))
1047 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1048 newdecl, newdecl, i);
1049 return false;
1052 oldargs = TREE_CHAIN (oldargs);
1053 newargs = TREE_CHAIN (newargs);
1054 i++;
1057 /* If we get here, no errors were found, but do issue a warning
1058 for this poor-style construct. */
1059 warning ("%Jprototype for '%D' follows non-prototype definition",
1060 newdecl, newdecl);
1061 return true;
1062 #undef END_OF_ARGLIST
1065 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1066 first in a pair of mismatched declarations, using the diagnostic
1067 function DIAG. */
1068 static void
1069 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1071 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1073 else if (DECL_INITIAL (decl))
1074 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1075 else if (C_DECL_IMPLICIT (decl))
1076 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1077 else
1078 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1081 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1082 Returns true if the caller should proceed to merge the two, false
1083 if OLDDECL should simply be discarded. As a side effect, issues
1084 all necessary diagnostics for invalid or poor-style combinations.
1085 If it returns true, writes the types of NEWDECL and OLDDECL to
1086 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1087 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1089 static bool
1090 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1091 tree *newtypep, tree *oldtypep)
1093 tree newtype, oldtype;
1094 bool pedwarned = false;
1095 bool warned = false;
1097 /* If we have error_mark_node for either decl or type, just discard
1098 the previous decl - we're in an error cascade already. */
1099 if (olddecl == error_mark_node || newdecl == error_mark_node)
1100 return false;
1101 *oldtypep = oldtype = TREE_TYPE (olddecl);
1102 *newtypep = newtype = TREE_TYPE (newdecl);
1103 if (oldtype == error_mark_node || newtype == error_mark_node)
1104 return false;
1106 /* Two different categories of symbol altogether. This is an error
1107 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1108 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1110 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1111 && DECL_BUILT_IN (olddecl)
1112 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1114 error ("%J'%D' redeclared as different kind of symbol",
1115 newdecl, newdecl);
1116 locate_old_decl (olddecl, error);
1118 else if (TREE_PUBLIC (newdecl))
1119 warning ("%Jbuilt-in function '%D' declared as non-function",
1120 newdecl, newdecl);
1121 else if (warn_shadow)
1122 warning ("%Jdeclaration of '%D' shadows a built-in function",
1123 newdecl, newdecl);
1124 return false;
1127 if (!comptypes (oldtype, newtype))
1129 if (TREE_CODE (olddecl) == FUNCTION_DECL
1130 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1132 /* Accept harmless mismatch in function types.
1133 This is for the ffs and fprintf builtins. */
1134 tree trytype = match_builtin_function_types (newtype, oldtype);
1136 if (trytype && comptypes (newtype, trytype))
1137 *oldtypep = oldtype = trytype;
1138 else
1140 /* If types don't match for a built-in, throw away the
1141 built-in. No point in calling locate_old_decl here, it
1142 won't print anything. */
1143 warning ("%Jconflicting types for built-in function '%D'",
1144 newdecl, newdecl);
1145 return false;
1148 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1149 && DECL_IS_BUILTIN (olddecl))
1151 /* A conflicting function declaration for a predeclared
1152 function that isn't actually built in. Objective C uses
1153 these. The new declaration silently overrides everything
1154 but the volatility (i.e. noreturn) indication. See also
1155 below. FIXME: Make Objective C use normal builtins. */
1156 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1157 return false;
1159 /* Permit void foo (...) to match int foo (...) if the latter is
1160 the definition and implicit int was used. See
1161 c-torture/compile/920625-2.c. */
1162 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1163 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1164 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1165 && C_FUNCTION_IMPLICIT_INT (newdecl))
1167 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1168 /* Make sure we keep void as the return type. */
1169 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1170 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1171 pedwarned = true;
1173 else
1175 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1176 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1177 else
1178 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1179 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1180 locate_old_decl (olddecl, error);
1181 return false;
1185 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1186 but silently ignore the redeclaration if either is in a system
1187 header. (Conflicting redeclarations were handled above.) */
1188 if (TREE_CODE (newdecl) == TYPE_DECL)
1190 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1191 return true; /* Allow OLDDECL to continue in use. */
1193 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1194 locate_old_decl (olddecl, error);
1195 return false;
1198 /* Function declarations can either be 'static' or 'extern' (no
1199 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1200 can never conflict with each other on account of linkage (6.2.2p4).
1201 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1202 two definitions if one is 'extern inline' and one is not. The non-
1203 extern-inline definition supersedes the extern-inline definition. */
1204 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1206 /* If you declare a built-in function name as static, or
1207 define the built-in with an old-style definition (so we
1208 can't validate the argument list) the built-in definition is
1209 overridden, but optionally warn this was a bad choice of name. */
1210 if (DECL_BUILT_IN (olddecl)
1211 && !C_DECL_DECLARED_BUILTIN (olddecl)
1212 && (!TREE_PUBLIC (newdecl)
1213 || (DECL_INITIAL (newdecl)
1214 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1216 if (warn_shadow)
1217 warning ("%Jdeclaration of '%D' shadows a built-in function",
1218 newdecl, newdecl);
1219 /* Discard the old built-in function. */
1220 return false;
1223 if (DECL_INITIAL (newdecl))
1225 if (DECL_INITIAL (olddecl)
1226 && !(DECL_DECLARED_INLINE_P (olddecl)
1227 && DECL_EXTERNAL (olddecl)
1228 && !(DECL_DECLARED_INLINE_P (newdecl)
1229 && DECL_EXTERNAL (newdecl)
1230 && same_translation_unit_p (olddecl, newdecl))))
1232 error ("%Jredefinition of '%D'", newdecl, newdecl);
1233 locate_old_decl (olddecl, error);
1234 return false;
1237 /* If we have a prototype after an old-style function definition,
1238 the argument types must be checked specially. */
1239 else if (DECL_INITIAL (olddecl)
1240 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1241 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1242 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1244 locate_old_decl (olddecl, error);
1245 return false;
1247 /* A non-static declaration (even an "extern") followed by a
1248 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1249 The same is true for a static forward declaration at block
1250 scope followed by a non-static declaration/definition at file
1251 scope. Static followed by non-static at the same scope is
1252 not undefined behavior, and is the most convenient way to get
1253 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1254 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1255 we do diagnose it if -Wtraditional. */
1256 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1258 /* Two exceptions to the rule. If olddecl is an extern
1259 inline, or a predeclared function that isn't actually
1260 built in, newdecl silently overrides olddecl. The latter
1261 occur only in Objective C; see also above. (FIXME: Make
1262 Objective C use normal builtins.) */
1263 if (!DECL_IS_BUILTIN (olddecl)
1264 && !(DECL_EXTERNAL (olddecl)
1265 && DECL_DECLARED_INLINE_P (olddecl)))
1267 error ("%Jstatic declaration of '%D' follows "
1268 "non-static declaration", newdecl, newdecl);
1269 locate_old_decl (olddecl, error);
1271 return false;
1273 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1275 if (DECL_CONTEXT (olddecl))
1277 error ("%Jnon-static declaration of '%D' follows "
1278 "static declaration", newdecl, newdecl);
1279 locate_old_decl (olddecl, error);
1280 return false;
1282 else if (warn_traditional)
1284 warning ("%Jnon-static declaration of '%D' follows "
1285 "static declaration", newdecl, newdecl);
1286 warned = true;
1290 else if (TREE_CODE (newdecl) == VAR_DECL)
1292 /* Only variables can be thread-local, and all declarations must
1293 agree on this property. */
1294 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1296 if (DECL_THREAD_LOCAL (newdecl))
1297 error ("%Jthread-local declaration of '%D' follows "
1298 "non-thread-local declaration", newdecl, newdecl);
1299 else
1300 error ("%Jnon-thread-local declaration of '%D' follows "
1301 "thread-local declaration", newdecl, newdecl);
1303 locate_old_decl (olddecl, error);
1304 return false;
1307 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1308 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1310 error ("%Jredefinition of '%D'", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1312 return false;
1315 /* Objects declared at file scope: if the first declaration had
1316 external linkage (even if it was an external reference) the
1317 second must have external linkage as well, or the behavior is
1318 undefined. If the first declaration had internal linkage, then
1319 the second must too, or else be an external reference (in which
1320 case the composite declaration still has internal linkage).
1321 As for function declarations, we warn about the static-then-
1322 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1323 if (DECL_FILE_SCOPE_P (newdecl)
1324 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1326 if (DECL_EXTERNAL (newdecl))
1328 if (!DECL_FILE_SCOPE_P (olddecl))
1330 error ("%Jextern declaration of %qD follows "
1331 "declaration with no linkage", newdecl, newdecl);
1332 locate_old_decl (olddecl, error);
1333 return false;
1335 else if (warn_traditional)
1337 warning ("%Jnon-static declaration of '%D' follows "
1338 "static declaration", newdecl, newdecl);
1339 warned = true;
1342 else
1344 if (TREE_PUBLIC (newdecl))
1345 error ("%Jnon-static declaration of '%D' follows "
1346 "static declaration", newdecl, newdecl);
1347 else
1348 error ("%Jstatic declaration of '%D' follows "
1349 "non-static declaration", newdecl, newdecl);
1351 locate_old_decl (olddecl, error);
1352 return false;
1355 /* Two objects with the same name declared at the same block
1356 scope must both be external references (6.7p3). */
1357 else if (!DECL_FILE_SCOPE_P (newdecl))
1359 if (DECL_EXTERNAL (newdecl))
1360 abort ();
1361 else if (DECL_EXTERNAL (olddecl))
1362 error ("%Jdeclaration of '%D' with no linkage follows "
1363 "extern declaration", newdecl, newdecl);
1364 else
1365 error ("%Jredeclaration of '%D' with no linkage",
1366 newdecl, newdecl);
1368 locate_old_decl (olddecl, error);
1369 return false;
1373 /* warnings */
1374 /* All decls must agree on a visibility. */
1375 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1376 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1378 warning ("%Jredeclaration of '%D' with different visibility "
1379 "(old visibility preserved)", newdecl, newdecl);
1380 warned = true;
1383 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1385 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1386 if (DECL_DECLARED_INLINE_P (newdecl)
1387 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1389 warning ("%Jinline declaration of '%D' follows "
1390 "declaration with attribute noinline", newdecl, newdecl);
1391 warned = true;
1393 else if (DECL_DECLARED_INLINE_P (olddecl)
1394 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1396 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1397 "inline declaration ", newdecl, newdecl);
1398 warned = true;
1401 /* Inline declaration after use or definition.
1402 ??? Should we still warn about this now we have unit-at-a-time
1403 mode and can get it right?
1404 Definitely don't complain if the decls are in different translation
1405 units. */
1406 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1407 && same_translation_unit_p (olddecl, newdecl))
1409 if (TREE_USED (olddecl))
1411 warning ("%J'%D' declared inline after being called",
1412 olddecl, olddecl);
1413 warned = true;
1415 else if (DECL_INITIAL (olddecl))
1417 warning ("%J'%D' declared inline after its definition",
1418 olddecl, olddecl);
1419 warned = true;
1423 else /* PARM_DECL, VAR_DECL */
1425 /* Redeclaration of a parameter is a constraint violation (this is
1426 not explicitly stated, but follows from C99 6.7p3 [no more than
1427 one declaration of the same identifier with no linkage in the
1428 same scope, except type tags] and 6.2.2p6 [parameters have no
1429 linkage]). We must check for a forward parameter declaration,
1430 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1431 an extension, the mandatory diagnostic for which is handled by
1432 mark_forward_parm_decls. */
1434 if (TREE_CODE (newdecl) == PARM_DECL
1435 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1437 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1438 locate_old_decl (olddecl, error);
1439 return false;
1443 /* Optional warning for completely redundant decls. */
1444 if (!warned && !pedwarned
1445 && warn_redundant_decls
1446 /* Don't warn about a function declaration followed by a
1447 definition. */
1448 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1449 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1450 /* Don't warn about redundant redeclarations of builtins. */
1451 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1452 && !DECL_BUILT_IN (newdecl)
1453 && DECL_BUILT_IN (olddecl)
1454 && !C_DECL_DECLARED_BUILTIN (olddecl))
1455 /* Don't warn about an extern followed by a definition. */
1456 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1457 /* Don't warn about forward parameter decls. */
1458 && !(TREE_CODE (newdecl) == PARM_DECL
1459 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1461 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1462 warned = true;
1465 /* Report location of previous decl/defn in a consistent manner. */
1466 if (warned || pedwarned)
1467 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1469 return true;
1472 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1473 consistent with OLDDECL, but carries new information. Merge the
1474 new information into OLDDECL. This function issues no
1475 diagnostics. */
1477 static void
1478 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1480 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1481 && DECL_INITIAL (newdecl) != 0);
1483 /* For real parm decl following a forward decl, rechain the old decl
1484 in its new location and clear TREE_ASM_WRITTEN (it's not a
1485 forward decl anymore). */
1486 if (TREE_CODE (newdecl) == PARM_DECL
1487 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1489 struct c_binding *b, **here;
1491 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1492 if ((*here)->decl == olddecl)
1493 goto found;
1494 abort ();
1496 found:
1497 b = *here;
1498 *here = b->prev;
1499 b->prev = current_scope->bindings;
1500 current_scope->bindings = b;
1502 TREE_ASM_WRITTEN (olddecl) = 0;
1505 DECL_ATTRIBUTES (newdecl)
1506 = targetm.merge_decl_attributes (olddecl, newdecl);
1508 /* Merge the data types specified in the two decls. */
1509 TREE_TYPE (newdecl)
1510 = TREE_TYPE (olddecl)
1511 = composite_type (newtype, oldtype);
1513 /* Lay the type out, unless already done. */
1514 if (oldtype != TREE_TYPE (newdecl))
1516 if (TREE_TYPE (newdecl) != error_mark_node)
1517 layout_type (TREE_TYPE (newdecl));
1518 if (TREE_CODE (newdecl) != FUNCTION_DECL
1519 && TREE_CODE (newdecl) != TYPE_DECL
1520 && TREE_CODE (newdecl) != CONST_DECL)
1521 layout_decl (newdecl, 0);
1523 else
1525 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1526 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1527 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1528 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1529 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1530 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1532 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1533 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1537 /* Keep the old rtl since we can safely use it. */
1538 COPY_DECL_RTL (olddecl, newdecl);
1540 /* Merge the type qualifiers. */
1541 if (TREE_READONLY (newdecl))
1542 TREE_READONLY (olddecl) = 1;
1544 if (TREE_THIS_VOLATILE (newdecl))
1546 TREE_THIS_VOLATILE (olddecl) = 1;
1547 if (TREE_CODE (newdecl) == VAR_DECL)
1548 make_var_volatile (newdecl);
1551 /* Keep source location of definition rather than declaration. */
1552 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1553 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1555 /* Merge the unused-warning information. */
1556 if (DECL_IN_SYSTEM_HEADER (olddecl))
1557 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1558 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1559 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1561 /* Merge the initialization information. */
1562 if (DECL_INITIAL (newdecl) == 0)
1563 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1565 /* Merge the section attribute.
1566 We want to issue an error if the sections conflict but that must be
1567 done later in decl_attributes since we are called before attributes
1568 are assigned. */
1569 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1570 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1572 /* Copy the assembler name.
1573 Currently, it can only be defined in the prototype. */
1574 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1576 /* Use visibility of whichever declaration had it specified */
1577 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1579 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1580 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1583 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1585 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1586 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1587 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1588 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1589 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1590 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1591 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1592 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1593 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1596 /* Merge the storage class information. */
1597 merge_weak (newdecl, olddecl);
1599 /* For functions, static overrides non-static. */
1600 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1602 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1603 /* This is since we don't automatically
1604 copy the attributes of NEWDECL into OLDDECL. */
1605 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1606 /* If this clears `static', clear it in the identifier too. */
1607 if (! TREE_PUBLIC (olddecl))
1608 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1610 if (DECL_EXTERNAL (newdecl))
1612 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1613 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1615 /* An extern decl does not override previous storage class. */
1616 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1617 if (! DECL_EXTERNAL (newdecl))
1619 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1620 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1623 else
1625 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1626 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1629 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1631 /* If we're redefining a function previously defined as extern
1632 inline, make sure we emit debug info for the inline before we
1633 throw it away, in case it was inlined into a function that hasn't
1634 been written out yet. */
1635 if (new_is_definition && DECL_INITIAL (olddecl))
1637 if (TREE_USED (olddecl)
1638 /* In unit-at-a-time mode we never inline re-defined extern
1639 inline functions. */
1640 && !flag_unit_at_a_time
1641 && cgraph_function_possibly_inlined_p (olddecl))
1642 (*debug_hooks->outlining_inline_function) (olddecl);
1644 /* The new defn must not be inline. */
1645 DECL_INLINE (newdecl) = 0;
1646 DECL_UNINLINABLE (newdecl) = 1;
1648 else
1650 /* If either decl says `inline', this fn is inline,
1651 unless its definition was passed already. */
1652 if (DECL_DECLARED_INLINE_P (newdecl)
1653 || DECL_DECLARED_INLINE_P (olddecl))
1654 DECL_DECLARED_INLINE_P (newdecl) = 1;
1656 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1657 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1660 if (DECL_BUILT_IN (olddecl))
1662 /* If redeclaring a builtin function, it stays built in.
1663 But it gets tagged as having been declared. */
1664 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1665 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1666 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1669 /* Also preserve various other info from the definition. */
1670 if (! new_is_definition)
1672 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1673 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1674 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1675 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1676 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1678 /* Set DECL_INLINE on the declaration if we've got a body
1679 from which to instantiate. */
1680 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1682 DECL_INLINE (newdecl) = 1;
1683 DECL_ABSTRACT_ORIGIN (newdecl)
1684 = DECL_ABSTRACT_ORIGIN (olddecl);
1687 else
1689 /* If a previous declaration said inline, mark the
1690 definition as inlinable. */
1691 if (DECL_DECLARED_INLINE_P (newdecl)
1692 && ! DECL_UNINLINABLE (newdecl))
1693 DECL_INLINE (newdecl) = 1;
1697 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1698 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1700 unsigned olddecl_uid = DECL_UID (olddecl);
1701 tree olddecl_context = DECL_CONTEXT (olddecl);
1703 memcpy ((char *) olddecl + sizeof (struct tree_common),
1704 (char *) newdecl + sizeof (struct tree_common),
1705 sizeof (struct tree_decl) - sizeof (struct tree_common));
1706 DECL_UID (olddecl) = olddecl_uid;
1707 DECL_CONTEXT (olddecl) = olddecl_context;
1710 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1711 so that encode_section_info has a chance to look at the new decl
1712 flags and attributes. */
1713 if (DECL_RTL_SET_P (olddecl)
1714 && (TREE_CODE (olddecl) == FUNCTION_DECL
1715 || (TREE_CODE (olddecl) == VAR_DECL
1716 && TREE_STATIC (olddecl))))
1717 make_decl_rtl (olddecl, NULL);
1720 /* Handle when a new declaration NEWDECL has the same name as an old
1721 one OLDDECL in the same binding contour. Prints an error message
1722 if appropriate.
1724 If safely possible, alter OLDDECL to look like NEWDECL, and return
1725 true. Otherwise, return false. */
1727 static bool
1728 duplicate_decls (tree newdecl, tree olddecl)
1730 tree newtype = NULL, oldtype = NULL;
1732 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1733 return false;
1735 merge_decls (newdecl, olddecl, newtype, oldtype);
1736 return true;
1740 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1741 static void
1742 warn_if_shadowing (tree new_decl)
1744 struct c_binding *b;
1746 /* Shadow warnings wanted? */
1747 if (!warn_shadow
1748 /* No shadow warnings for internally generated vars. */
1749 || DECL_IS_BUILTIN (new_decl)
1750 /* No shadow warnings for vars made for inlining. */
1751 || DECL_FROM_INLINE (new_decl)
1752 /* Don't warn about the parm names in function declarator
1753 within a function declarator. It would be nice to avoid
1754 warning in any function declarator in a declaration, as
1755 opposed to a definition, but there is no way to tell
1756 it's not a definition at this point. */
1757 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1758 return;
1760 /* Is anything being shadowed? Invisible decls do not count. */
1761 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1762 if (b->decl && b->decl != new_decl && !b->invisible)
1764 tree old_decl = b->decl;
1766 if (TREE_CODE (old_decl) == PARM_DECL)
1767 warning ("%Jdeclaration of '%D' shadows a parameter",
1768 new_decl, new_decl);
1769 else if (DECL_FILE_SCOPE_P (old_decl))
1770 warning ("%Jdeclaration of '%D' shadows a global declaration",
1771 new_decl, new_decl);
1772 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1773 && DECL_BUILT_IN (old_decl))
1774 warning ("%Jdeclaration of '%D' shadows a built-in function",
1775 new_decl, new_decl);
1776 else
1777 warning ("%Jdeclaration of '%D' shadows a previous local",
1778 new_decl, new_decl);
1780 if (TREE_CODE (old_decl) != FUNCTION_DECL
1781 || ! DECL_BUILT_IN (old_decl))
1782 warning ("%Jshadowed declaration is here", old_decl);
1784 break;
1789 /* Subroutine of pushdecl.
1791 X is a TYPE_DECL for a typedef statement. Create a brand new
1792 ..._TYPE node (which will be just a variant of the existing
1793 ..._TYPE node with identical properties) and then install X
1794 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1796 The whole point here is to end up with a situation where each
1797 and every ..._TYPE node the compiler creates will be uniquely
1798 associated with AT MOST one node representing a typedef name.
1799 This way, even though the compiler substitutes corresponding
1800 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1801 early on, later parts of the compiler can always do the reverse
1802 translation and get back the corresponding typedef name. For
1803 example, given:
1805 typedef struct S MY_TYPE;
1806 MY_TYPE object;
1808 Later parts of the compiler might only know that `object' was of
1809 type `struct S' if it were not for code just below. With this
1810 code however, later parts of the compiler see something like:
1812 struct S' == struct S
1813 typedef struct S' MY_TYPE;
1814 struct S' object;
1816 And they can then deduce (from the node for type struct S') that
1817 the original object declaration was:
1819 MY_TYPE object;
1821 Being able to do this is important for proper support of protoize,
1822 and also for generating precise symbolic debugging information
1823 which takes full account of the programmer's (typedef) vocabulary.
1825 Obviously, we don't want to generate a duplicate ..._TYPE node if
1826 the TYPE_DECL node that we are now processing really represents a
1827 standard built-in type.
1829 Since all standard types are effectively declared at line zero
1830 in the source file, we can easily check to see if we are working
1831 on a standard type by checking the current value of lineno. */
1833 static void
1834 clone_underlying_type (tree x)
1836 if (DECL_IS_BUILTIN (x))
1838 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1839 TYPE_NAME (TREE_TYPE (x)) = x;
1841 else if (TREE_TYPE (x) != error_mark_node
1842 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1844 tree tt = TREE_TYPE (x);
1845 DECL_ORIGINAL_TYPE (x) = tt;
1846 tt = build_type_copy (tt);
1847 TYPE_NAME (tt) = x;
1848 TREE_USED (tt) = TREE_USED (x);
1849 TREE_TYPE (x) = tt;
1853 /* Record a decl-node X as belonging to the current lexical scope.
1854 Check for errors (such as an incompatible declaration for the same
1855 name already seen in the same scope).
1857 Returns either X or an old decl for the same name.
1858 If an old decl is returned, it may have been smashed
1859 to agree with what X says. */
1861 tree
1862 pushdecl (tree x)
1864 tree name = DECL_NAME (x);
1865 struct c_scope *scope = current_scope;
1866 struct c_binding *b;
1867 bool nested = false;
1869 /* Functions need the lang_decl data. */
1870 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1871 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1873 /* Must set DECL_CONTEXT for everything not at file scope or
1874 DECL_FILE_SCOPE_P won't work. Local externs don't count
1875 unless they have initializers (which generate code). */
1876 if (current_function_decl
1877 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1878 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1879 DECL_CONTEXT (x) = current_function_decl;
1881 /* Anonymous decls are just inserted in the scope. */
1882 if (!name)
1884 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1885 return x;
1888 /* First, see if there is another declaration with the same name in
1889 the current scope. If there is, duplicate_decls may do all the
1890 work for us. If duplicate_decls returns false, that indicates
1891 two incompatible decls in the same scope; we are to silently
1892 replace the old one (duplicate_decls has issued all appropriate
1893 diagnostics). In particular, we should not consider possible
1894 duplicates in the external scope, or shadowing. */
1895 b = I_SYMBOL_BINDING (name);
1896 if (b && B_IN_SCOPE (b, scope))
1898 if (duplicate_decls (x, b->decl))
1899 return b->decl;
1900 else
1901 goto skip_external_and_shadow_checks;
1904 /* All declarations with external linkage, and all external
1905 references, go in the external scope, no matter what scope is
1906 current. However, the binding in that scope is ignored for
1907 purposes of normal name lookup. A separate binding structure is
1908 created in the requested scope; this governs the normal
1909 visibility of the symbol.
1911 The binding in the externals scope is used exclusively for
1912 detecting duplicate declarations of the same object, no matter
1913 what scope they are in; this is what we do here. (C99 6.2.7p2:
1914 All declarations that refer to the same object or function shall
1915 have compatible type; otherwise, the behavior is undefined.) */
1916 if (DECL_EXTERNAL (x) || scope == file_scope)
1918 if (warn_nested_externs
1919 && scope != file_scope
1920 && !DECL_IN_SYSTEM_HEADER (x))
1921 warning ("nested extern declaration of '%D'", x);
1923 while (b && !B_IN_EXTERNAL_SCOPE (b))
1924 b = b->shadowed;
1926 /* The point of the same_translation_unit_p check here is,
1927 we want to detect a duplicate decl for a construct like
1928 foo() { extern bar(); } ... static bar(); but not if
1929 they are in different translation units. In any case,
1930 the static does not go in the externals scope. */
1931 if (b
1932 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
1933 && duplicate_decls (x, b->decl))
1935 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1936 return b->decl;
1938 else if (TREE_PUBLIC (x))
1940 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1941 nested = true;
1944 /* Similarly, a declaration of a function with static linkage at
1945 block scope must be checked against any existing declaration
1946 of that function at file scope. */
1947 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1948 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1950 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1951 warning ("nested static declaration of '%D'", x);
1953 while (b && !B_IN_FILE_SCOPE (b))
1954 b = b->shadowed;
1956 if (b && same_translation_unit_p (x, b->decl)
1957 && duplicate_decls (x, b->decl))
1959 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1960 return b->decl;
1962 else
1964 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1965 nested = true;
1969 warn_if_shadowing (x);
1971 skip_external_and_shadow_checks:
1972 if (TREE_CODE (x) == TYPE_DECL)
1973 clone_underlying_type (x);
1975 bind (name, x, scope, /*invisible=*/false, nested);
1977 /* If x's type is incomplete because it's based on a
1978 structure or union which has not yet been fully declared,
1979 attach it to that structure or union type, so we can go
1980 back and complete the variable declaration later, if the
1981 structure or union gets fully declared.
1983 If the input is erroneous, we can have error_mark in the type
1984 slot (e.g. "f(void a, ...)") - that doesn't count as an
1985 incomplete type. */
1986 if (TREE_TYPE (x) != error_mark_node
1987 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1989 tree element = TREE_TYPE (x);
1991 while (TREE_CODE (element) == ARRAY_TYPE)
1992 element = TREE_TYPE (element);
1993 element = TYPE_MAIN_VARIANT (element);
1995 if ((TREE_CODE (element) == RECORD_TYPE
1996 || TREE_CODE (element) == UNION_TYPE)
1997 && (TREE_CODE (x) != TYPE_DECL
1998 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1999 && !COMPLETE_TYPE_P (element))
2000 C_TYPE_INCOMPLETE_VARS (element)
2001 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2003 return x;
2006 /* Record X as belonging to file scope.
2007 This is used only internally by the Objective-C front end,
2008 and is limited to its needs. duplicate_decls is not called;
2009 if there is any preexisting decl for this identifier, it is an ICE. */
2011 tree
2012 pushdecl_top_level (tree x)
2014 tree name;
2015 bool nested = false;
2017 if (TREE_CODE (x) != VAR_DECL)
2018 abort ();
2020 name = DECL_NAME (x);
2022 if (I_SYMBOL_BINDING (name))
2023 abort ();
2025 if (TREE_PUBLIC (x))
2027 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2028 nested = true;
2030 if (file_scope)
2031 bind (name, x, file_scope, /*invisible=*/false, nested);
2033 return x;
2036 static void
2037 implicit_decl_warning (tree id, tree olddecl)
2039 void (*diag) (const char *, ...);
2040 switch (mesg_implicit_function_declaration)
2042 case 0: return;
2043 case 1: diag = warning; break;
2044 case 2: diag = error; break;
2045 default: abort ();
2048 diag (N_("implicit declaration of function '%E'"), id);
2049 if (olddecl)
2050 locate_old_decl (olddecl, diag);
2053 /* Generate an implicit declaration for identifier FUNCTIONID as a
2054 function of type int (). */
2056 tree
2057 implicitly_declare (tree functionid)
2059 tree decl = lookup_name_in_scope (functionid, external_scope);
2061 if (decl)
2063 /* FIXME: Objective-C has weird not-really-builtin functions
2064 which are supposed to be visible automatically. They wind up
2065 in the external scope because they're pushed before the file
2066 scope gets created. Catch this here and rebind them into the
2067 file scope. */
2068 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2070 bind (functionid, decl, file_scope,
2071 /*invisible=*/false, /*nested=*/true);
2072 return decl;
2074 else
2076 /* Implicit declaration of a function already declared
2077 (somehow) in a different scope, or as a built-in.
2078 If this is the first time this has happened, warn;
2079 then recycle the old declaration. */
2080 if (!C_DECL_IMPLICIT (decl))
2082 implicit_decl_warning (functionid, decl);
2083 C_DECL_IMPLICIT (decl) = 1;
2085 bind (functionid, decl, current_scope,
2086 /*invisible=*/false, /*nested=*/true);
2087 return decl;
2091 /* Not seen before. */
2092 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2093 DECL_EXTERNAL (decl) = 1;
2094 TREE_PUBLIC (decl) = 1;
2095 C_DECL_IMPLICIT (decl) = 1;
2096 implicit_decl_warning (functionid, 0);
2098 /* C89 says implicit declarations are in the innermost block.
2099 So we record the decl in the standard fashion. */
2100 decl = pushdecl (decl);
2102 /* No need to call objc_check_decl here - it's a function type. */
2103 rest_of_decl_compilation (decl, NULL, 0, 0);
2105 /* Write a record describing this implicit function declaration
2106 to the prototypes file (if requested). */
2107 gen_aux_info_record (decl, 0, 1, 0);
2109 /* Possibly apply some default attributes to this implicit declaration. */
2110 decl_attributes (&decl, NULL_TREE, 0);
2112 return decl;
2115 /* Issue an error message for a reference to an undeclared variable
2116 ID, including a reference to a builtin outside of function-call
2117 context. Establish a binding of the identifier to error_mark_node
2118 in an appropriate scope, which will suppress further errors for the
2119 same identifier. */
2120 void
2121 undeclared_variable (tree id)
2123 static bool already = false;
2124 struct c_scope *scope;
2126 if (current_function_decl == 0)
2128 error ("'%E' undeclared here (not in a function)", id);
2129 scope = current_scope;
2131 else
2133 error ("'%E' undeclared (first use in this function)", id);
2135 if (! already)
2137 error ("(Each undeclared identifier is reported only once");
2138 error ("for each function it appears in.)");
2139 already = true;
2142 /* If we are parsing old-style parameter decls, current_function_decl
2143 will be nonnull but current_function_scope will be null. */
2144 scope = current_function_scope ? current_function_scope : current_scope;
2146 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2149 /* Subroutine of lookup_label, declare_label, define_label: construct a
2150 LABEL_DECL with all the proper frills. */
2152 static tree
2153 make_label (tree name, location_t location)
2155 tree label = build_decl (LABEL_DECL, name, void_type_node);
2157 DECL_CONTEXT (label) = current_function_decl;
2158 DECL_MODE (label) = VOIDmode;
2159 DECL_SOURCE_LOCATION (label) = location;
2161 return label;
2164 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2165 Create one if none exists so far for the current function.
2166 This is called when a label is used in a goto expression or
2167 has its address taken. */
2169 tree
2170 lookup_label (tree name)
2172 tree label;
2174 if (current_function_decl == 0)
2176 error ("label %s referenced outside of any function",
2177 IDENTIFIER_POINTER (name));
2178 return 0;
2181 /* Use a label already defined or ref'd with this name, but not if
2182 it is inherited from a containing function and wasn't declared
2183 using __label__. */
2184 label = I_LABEL_DECL (name);
2185 if (label && (DECL_CONTEXT (label) == current_function_decl
2186 || C_DECLARED_LABEL_FLAG (label)))
2188 /* If the label has only been declared, update its apparent
2189 location to point here, for better diagnostics if it
2190 turns out not to have been defined. */
2191 if (!TREE_USED (label))
2192 DECL_SOURCE_LOCATION (label) = input_location;
2193 return label;
2196 /* No label binding for that identifier; make one. */
2197 label = make_label (name, input_location);
2199 /* Ordinary labels go in the current function scope. */
2200 bind (name, label, current_function_scope,
2201 /*invisible=*/false, /*nested=*/false);
2202 return label;
2205 /* Make a label named NAME in the current function, shadowing silently
2206 any that may be inherited from containing functions or containing
2207 scopes. This is called for __label__ declarations. */
2209 tree
2210 declare_label (tree name)
2212 struct c_binding *b = I_LABEL_BINDING (name);
2213 tree label;
2215 /* Check to make sure that the label hasn't already been declared
2216 at this scope */
2217 if (b && B_IN_CURRENT_SCOPE (b))
2219 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2220 locate_old_decl (b->decl, error);
2222 /* Just use the previous declaration. */
2223 return b->decl;
2226 label = make_label (name, input_location);
2227 C_DECLARED_LABEL_FLAG (label) = 1;
2229 /* Declared labels go in the current scope. */
2230 bind (name, label, current_scope,
2231 /*invisible=*/false, /*nested=*/false);
2232 return label;
2235 /* Define a label, specifying the location in the source file.
2236 Return the LABEL_DECL node for the label, if the definition is valid.
2237 Otherwise return 0. */
2239 tree
2240 define_label (location_t location, tree name)
2242 /* Find any preexisting label with this name. It is an error
2243 if that label has already been defined in this function, or
2244 if there is a containing function with a declared label with
2245 the same name. */
2246 tree label = I_LABEL_DECL (name);
2248 if (label
2249 && ((DECL_CONTEXT (label) == current_function_decl
2250 && DECL_INITIAL (label) != 0)
2251 || (DECL_CONTEXT (label) != current_function_decl
2252 && C_DECLARED_LABEL_FLAG (label))))
2254 error ("%Hduplicate label `%D'", &location, label);
2255 locate_old_decl (label, error);
2256 return 0;
2258 else if (label && DECL_CONTEXT (label) == current_function_decl)
2260 /* The label has been used or declared already in this function,
2261 but not defined. Update its location to point to this
2262 definition. */
2263 DECL_SOURCE_LOCATION (label) = location;
2265 else
2267 /* No label binding for that identifier; make one. */
2268 label = make_label (name, location);
2270 /* Ordinary labels go in the current function scope. */
2271 bind (name, label, current_function_scope,
2272 /*invisible=*/false, /*nested=*/false);
2275 if (warn_traditional && !in_system_header && lookup_name (name))
2276 warning ("%Htraditional C lacks a separate namespace for labels, "
2277 "identifier `%s' conflicts", &location,
2278 IDENTIFIER_POINTER (name));
2280 /* Mark label as having been defined. */
2281 DECL_INITIAL (label) = error_mark_node;
2282 return label;
2285 /* Given NAME, an IDENTIFIER_NODE,
2286 return the structure (or union or enum) definition for that name.
2287 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2288 CODE says which kind of type the caller wants;
2289 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2290 If the wrong kind of type is found, an error is reported. */
2292 static tree
2293 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2295 struct c_binding *b = I_TAG_BINDING (name);
2296 int thislevel = 0;
2298 if (!b || !b->decl)
2299 return 0;
2301 /* We only care about whether it's in this level if
2302 thislevel_only was set or it might be a type clash. */
2303 if (thislevel_only || TREE_CODE (b->decl) != code)
2305 /* For our purposes, a tag in the external scope is the same as
2306 a tag in the file scope. (Primarily relevant to Objective-C
2307 and its builtin structure tags, which get pushed before the
2308 file scope is created.) */
2309 if (B_IN_CURRENT_SCOPE (b)
2310 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2311 thislevel = 1;
2314 if (thislevel_only && !thislevel)
2315 return 0;
2317 if (TREE_CODE (b->decl) != code)
2319 /* Definition isn't the kind we were looking for. */
2320 pending_invalid_xref = name;
2321 pending_invalid_xref_location = input_location;
2323 /* If in the same binding level as a declaration as a tag
2324 of a different type, this must not be allowed to
2325 shadow that tag, so give the error immediately.
2326 (For example, "struct foo; union foo;" is invalid.) */
2327 if (thislevel)
2328 pending_xref_error ();
2330 return b->decl;
2333 /* Print an error message now
2334 for a recent invalid struct, union or enum cross reference.
2335 We don't print them immediately because they are not invalid
2336 when used in the `struct foo;' construct for shadowing. */
2338 void
2339 pending_xref_error (void)
2341 if (pending_invalid_xref != 0)
2342 error ("%H`%s' defined as wrong kind of tag",
2343 &pending_invalid_xref_location,
2344 IDENTIFIER_POINTER (pending_invalid_xref));
2345 pending_invalid_xref = 0;
2349 /* Look up NAME in the current scope and its superiors
2350 in the namespace of variables, functions and typedefs.
2351 Return a ..._DECL node of some kind representing its definition,
2352 or return 0 if it is undefined. */
2354 tree
2355 lookup_name (tree name)
2357 struct c_binding *b = I_SYMBOL_BINDING (name);
2358 if (b && !b->invisible)
2359 return b->decl;
2360 return 0;
2363 /* Similar to `lookup_name' but look only at the indicated scope. */
2365 static tree
2366 lookup_name_in_scope (tree name, struct c_scope *scope)
2368 struct c_binding *b;
2370 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2371 if (B_IN_SCOPE (b, scope))
2372 return b->decl;
2373 return 0;
2376 /* Create the predefined scalar types of C,
2377 and some nodes representing standard constants (0, 1, (void *) 0).
2378 Initialize the global scope.
2379 Make definitions for built-in primitive functions. */
2381 void
2382 c_init_decl_processing (void)
2384 tree endlink;
2385 tree ptr_ftype_void, ptr_ftype_ptr;
2386 location_t save_loc = input_location;
2388 /* Adds some ggc roots, and reserved words for c-parse.in. */
2389 c_parse_init ();
2391 current_function_decl = 0;
2393 /* Make the externals scope. */
2394 push_scope ();
2395 external_scope = current_scope;
2397 /* Declarations from c_common_nodes_and_builtins must not be associated
2398 with this input file, lest we get differences between using and not
2399 using preprocessed headers. */
2400 #ifdef USE_MAPPED_LOCATION
2401 input_location = BUILTINS_LOCATION;
2402 #else
2403 input_location.file = "<built-in>";
2404 input_location.line = 0;
2405 #endif
2407 build_common_tree_nodes (flag_signed_char);
2409 c_common_nodes_and_builtins ();
2411 /* In C, comparisons and TRUTH_* expressions have type int. */
2412 truthvalue_type_node = integer_type_node;
2413 truthvalue_true_node = integer_one_node;
2414 truthvalue_false_node = integer_zero_node;
2416 /* Even in C99, which has a real boolean type. */
2417 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2418 boolean_type_node));
2420 endlink = void_list_node;
2421 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2422 ptr_ftype_ptr
2423 = build_function_type (ptr_type_node,
2424 tree_cons (NULL_TREE, ptr_type_node, endlink));
2426 input_location = save_loc;
2428 pedantic_lvalues = true;
2430 make_fname_decl = c_make_fname_decl;
2431 start_fname_decls ();
2434 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2435 decl, NAME is the initialization string and TYPE_DEP indicates whether
2436 NAME depended on the type of the function. As we don't yet implement
2437 delayed emission of static data, we mark the decl as emitted
2438 so it is not placed in the output. Anything using it must therefore pull
2439 out the STRING_CST initializer directly. FIXME. */
2441 static tree
2442 c_make_fname_decl (tree id, int type_dep)
2444 const char *name = fname_as_string (type_dep);
2445 tree decl, type, init;
2446 size_t length = strlen (name);
2448 type = build_array_type
2449 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2450 build_index_type (size_int (length)));
2452 decl = build_decl (VAR_DECL, id, type);
2454 TREE_STATIC (decl) = 1;
2455 TREE_READONLY (decl) = 1;
2456 DECL_ARTIFICIAL (decl) = 1;
2458 init = build_string (length + 1, name);
2459 free ((char *) name);
2460 TREE_TYPE (init) = type;
2461 DECL_INITIAL (decl) = init;
2463 TREE_USED (decl) = 1;
2465 if (current_function_decl)
2467 DECL_CONTEXT (decl) = current_function_decl;
2468 bind (id, decl, current_function_scope,
2469 /*invisible=*/false, /*nested=*/false);
2472 finish_decl (decl, init, NULL_TREE);
2474 return decl;
2477 /* Return a definition for a builtin function named NAME and whose data type
2478 is TYPE. TYPE should be a function type with argument types.
2479 FUNCTION_CODE tells later passes how to compile calls to this function.
2480 See tree.h for its possible values.
2482 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2483 the name to be called if we can't opencode the function. If
2484 ATTRS is nonzero, use that for the function's attribute list. */
2486 tree
2487 builtin_function (const char *name, tree type, int function_code,
2488 enum built_in_class cl, const char *library_name,
2489 tree attrs)
2491 tree id = get_identifier (name);
2492 tree decl = build_decl (FUNCTION_DECL, id, type);
2493 TREE_PUBLIC (decl) = 1;
2494 DECL_EXTERNAL (decl) = 1;
2495 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2496 DECL_BUILT_IN_CLASS (decl) = cl;
2497 DECL_FUNCTION_CODE (decl) = function_code;
2498 if (library_name)
2499 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2501 /* Should never be called on a symbol with a preexisting meaning. */
2502 if (I_SYMBOL_BINDING (id))
2503 abort ();
2505 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2507 /* Builtins in the implementation namespace are made visible without
2508 needing to be explicitly declared. See push_file_scope. */
2509 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2511 TREE_CHAIN (decl) = visible_builtins;
2512 visible_builtins = decl;
2515 /* Possibly apply some default attributes to this built-in function. */
2516 if (attrs)
2517 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2518 else
2519 decl_attributes (&decl, NULL_TREE, 0);
2521 return decl;
2524 /* Called when a declaration is seen that contains no names to declare.
2525 If its type is a reference to a structure, union or enum inherited
2526 from a containing scope, shadow that tag name for the current scope
2527 with a forward reference.
2528 If its type defines a new named structure or union
2529 or defines an enum, it is valid but we need not do anything here.
2530 Otherwise, it is an error. */
2532 void
2533 shadow_tag (tree declspecs)
2535 shadow_tag_warned (declspecs, 0);
2538 void
2539 shadow_tag_warned (tree declspecs, int warned)
2542 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2543 no pedwarn. */
2545 int found_tag = 0;
2546 tree link;
2547 tree specs, attrs;
2549 pending_invalid_xref = 0;
2551 /* Remove the attributes from declspecs, since they will confuse the
2552 following code. */
2553 split_specs_attrs (declspecs, &specs, &attrs);
2555 for (link = specs; link; link = TREE_CHAIN (link))
2557 tree value = TREE_VALUE (link);
2558 enum tree_code code = TREE_CODE (value);
2560 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2561 /* Used to test also that TYPE_SIZE (value) != 0.
2562 That caused warning for `struct foo;' at top level in the file. */
2564 tree name = TYPE_NAME (value);
2565 tree t;
2567 found_tag++;
2569 if (name == 0)
2571 if (warned != 1 && code != ENUMERAL_TYPE)
2572 /* Empty unnamed enum OK */
2574 pedwarn ("unnamed struct/union that defines no instances");
2575 warned = 1;
2578 else
2580 t = lookup_tag (code, name, 1);
2582 if (t == 0)
2584 t = make_node (code);
2585 pushtag (name, t);
2589 else
2591 if (!warned && ! in_system_header)
2593 warning ("useless keyword or type name in empty declaration");
2594 warned = 2;
2599 if (found_tag > 1)
2600 error ("two types specified in one empty declaration");
2602 if (warned != 1)
2604 if (found_tag == 0)
2605 pedwarn ("empty declaration");
2609 /* Construct an array declarator. EXPR is the expression inside [], or
2610 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2611 to the pointer to which a parameter array is converted). STATIC_P is
2612 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2613 is nonzero is the array is [*], a VLA of unspecified length which is
2614 nevertheless a complete type (not currently implemented by GCC),
2615 zero otherwise. The declarator is constructed as an ARRAY_REF
2616 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2617 left of the [] (filled by in set_array_declarator_type) and operand 1
2618 is the expression inside; whose TREE_TYPE is the type qualifiers and
2619 which has TREE_STATIC set if "static" is used. */
2621 tree
2622 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2624 tree decl;
2625 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2626 TREE_TYPE (decl) = quals;
2627 TREE_STATIC (decl) = (static_p ? 1 : 0);
2628 if (pedantic && !flag_isoc99)
2630 if (static_p || quals != NULL_TREE)
2631 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2632 if (vla_unspec_p)
2633 pedwarn ("ISO C90 does not support `[*]' array declarators");
2635 if (vla_unspec_p)
2636 warning ("GCC does not yet properly implement `[*]' array declarators");
2637 return decl;
2640 /* Set the type of an array declarator. DECL is the declarator, as
2641 constructed by build_array_declarator; TYPE is what appears on the left
2642 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2643 abstract declarator, zero otherwise; this is used to reject static and
2644 type qualifiers in abstract declarators, where they are not in the
2645 C99 grammar. */
2647 tree
2648 set_array_declarator_type (tree decl, tree type, int abstract_p)
2650 TREE_OPERAND (decl, 0) = type;
2651 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2652 error ("static or type qualifiers in abstract declarator");
2653 return decl;
2656 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2658 tree
2659 groktypename (tree type_name)
2661 tree specs, attrs;
2663 if (TREE_CODE (type_name) != TREE_LIST)
2664 return type_name;
2666 split_specs_attrs (TREE_PURPOSE (type_name), &specs, &attrs);
2668 type_name = grokdeclarator (TREE_VALUE (type_name), specs, TYPENAME, 0,
2669 NULL);
2671 /* Apply attributes. */
2672 decl_attributes (&type_name, attrs, 0);
2674 return type_name;
2677 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2679 tree
2680 groktypename_in_parm_context (tree type_name)
2682 if (TREE_CODE (type_name) != TREE_LIST)
2683 return type_name;
2684 return grokdeclarator (TREE_VALUE (type_name),
2685 TREE_PURPOSE (type_name),
2686 PARM, 0, NULL);
2689 /* Decode a declarator in an ordinary declaration or data definition.
2690 This is called as soon as the type information and variable name
2691 have been parsed, before parsing the initializer if any.
2692 Here we create the ..._DECL node, fill in its type,
2693 and put it on the list of decls for the current context.
2694 The ..._DECL node is returned as the value.
2696 Exception: for arrays where the length is not specified,
2697 the type is left null, to be filled in by `finish_decl'.
2699 Function definitions do not come here; they go to start_function
2700 instead. However, external and forward declarations of functions
2701 do go through here. Structure field declarations are done by
2702 grokfield and not through here. */
2704 tree
2705 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2707 tree decl;
2708 tree tem;
2710 /* An object declared as __attribute__((deprecated)) suppresses
2711 warnings of uses of other deprecated items. */
2712 if (lookup_attribute ("deprecated", attributes))
2713 deprecated_state = DEPRECATED_SUPPRESS;
2715 decl = grokdeclarator (declarator, declspecs,
2716 NORMAL, initialized, NULL);
2718 deprecated_state = DEPRECATED_NORMAL;
2720 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2721 && MAIN_NAME_P (DECL_NAME (decl)))
2722 warning ("%J'%D' is usually a function", decl, decl);
2724 if (initialized)
2725 /* Is it valid for this decl to have an initializer at all?
2726 If not, set INITIALIZED to zero, which will indirectly
2727 tell 'finish_decl' to ignore the initializer once it is parsed. */
2728 switch (TREE_CODE (decl))
2730 case TYPE_DECL:
2731 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2732 initialized = 0;
2733 break;
2735 case FUNCTION_DECL:
2736 error ("function '%D' is initialized like a variable", decl);
2737 initialized = 0;
2738 break;
2740 case PARM_DECL:
2741 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2742 error ("parameter '%D' is initialized", decl);
2743 initialized = 0;
2744 break;
2746 default:
2747 /* Don't allow initializations for incomplete types except for
2748 arrays which might be completed by the initialization. */
2750 /* This can happen if the array size is an undefined macro.
2751 We already gave a warning, so we don't need another one. */
2752 if (TREE_TYPE (decl) == error_mark_node)
2753 initialized = 0;
2754 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2756 /* A complete type is ok if size is fixed. */
2758 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2759 || C_DECL_VARIABLE_SIZE (decl))
2761 error ("variable-sized object may not be initialized");
2762 initialized = 0;
2765 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2767 error ("variable '%D' has initializer but incomplete type", decl);
2768 initialized = 0;
2770 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2772 error ("elements of array '%D' have incomplete type", decl);
2773 initialized = 0;
2777 if (initialized)
2779 if (current_scope == file_scope)
2780 TREE_STATIC (decl) = 1;
2782 /* Tell 'pushdecl' this is an initialized decl
2783 even though we don't yet have the initializer expression.
2784 Also tell 'finish_decl' it may store the real initializer. */
2785 DECL_INITIAL (decl) = error_mark_node;
2788 /* If this is a function declaration, write a record describing it to the
2789 prototypes file (if requested). */
2791 if (TREE_CODE (decl) == FUNCTION_DECL)
2792 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2794 /* ANSI specifies that a tentative definition which is not merged with
2795 a non-tentative definition behaves exactly like a definition with an
2796 initializer equal to zero. (Section 3.7.2)
2798 -fno-common gives strict ANSI behavior, though this tends to break
2799 a large body of code that grew up without this rule.
2801 Thread-local variables are never common, since there's no entrenched
2802 body of code to break, and it allows more efficient variable references
2803 in the presence of dynamic linking. */
2805 if (TREE_CODE (decl) == VAR_DECL
2806 && !initialized
2807 && TREE_PUBLIC (decl)
2808 && !DECL_THREAD_LOCAL (decl)
2809 && !flag_no_common)
2810 DECL_COMMON (decl) = 1;
2812 /* Set attributes here so if duplicate decl, will have proper attributes. */
2813 decl_attributes (&decl, attributes, 0);
2815 if (TREE_CODE (decl) == FUNCTION_DECL
2816 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2818 tree ce = declarator;
2820 if (TREE_CODE (ce) == INDIRECT_REF)
2821 ce = TREE_OPERAND (declarator, 0);
2822 if (TREE_CODE (ce) == CALL_EXPR)
2824 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2825 for (; args; args = TREE_CHAIN (args))
2827 tree type = TREE_TYPE (args);
2828 if (type && INTEGRAL_TYPE_P (type)
2829 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2830 DECL_ARG_TYPE (args) = integer_type_node;
2835 if (TREE_CODE (decl) == FUNCTION_DECL
2836 && DECL_DECLARED_INLINE_P (decl)
2837 && DECL_UNINLINABLE (decl)
2838 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2839 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2841 /* Add this decl to the current scope.
2842 TEM may equal DECL or it may be a previous decl of the same name. */
2843 tem = pushdecl (decl);
2845 if (initialized)
2846 DECL_EXTERNAL (tem) = 0;
2848 return tem;
2851 /* Finish processing of a declaration;
2852 install its initial value.
2853 If the length of an array type is not known before,
2854 it must be determined now, from the initial value, or it is an error. */
2856 void
2857 finish_decl (tree decl, tree init, tree asmspec_tree)
2859 tree type = TREE_TYPE (decl);
2860 int was_incomplete = (DECL_SIZE (decl) == 0);
2861 const char *asmspec = 0;
2863 /* If a name was specified, get the string. */
2864 if (current_scope == file_scope)
2865 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2866 if (asmspec_tree)
2867 asmspec = TREE_STRING_POINTER (asmspec_tree);
2869 /* If `start_decl' didn't like having an initialization, ignore it now. */
2870 if (init != 0 && DECL_INITIAL (decl) == 0)
2871 init = 0;
2873 /* Don't crash if parm is initialized. */
2874 if (TREE_CODE (decl) == PARM_DECL)
2875 init = 0;
2877 if (init)
2878 store_init_value (decl, init);
2880 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2881 || TREE_CODE (decl) == FUNCTION_DECL
2882 || TREE_CODE (decl) == FIELD_DECL))
2883 objc_check_decl (decl);
2885 /* Deduce size of array from initialization, if not already known. */
2886 if (TREE_CODE (type) == ARRAY_TYPE
2887 && TYPE_DOMAIN (type) == 0
2888 && TREE_CODE (decl) != TYPE_DECL)
2890 int do_default
2891 = (TREE_STATIC (decl)
2892 /* Even if pedantic, an external linkage array
2893 may have incomplete type at first. */
2894 ? pedantic && !TREE_PUBLIC (decl)
2895 : !DECL_EXTERNAL (decl));
2896 int failure
2897 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2899 /* Get the completed type made by complete_array_type. */
2900 type = TREE_TYPE (decl);
2902 if (failure == 1)
2903 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2905 else if (failure == 2)
2907 if (do_default)
2908 error ("%Jarray size missing in '%D'", decl, decl);
2909 /* If a `static' var's size isn't known,
2910 make it extern as well as static, so it does not get
2911 allocated.
2912 If it is not `static', then do not mark extern;
2913 finish_incomplete_decl will give it a default size
2914 and it will get allocated. */
2915 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2916 DECL_EXTERNAL (decl) = 1;
2919 /* TYPE_MAX_VALUE is always one less than the number of elements
2920 in the array, because we start counting at zero. Therefore,
2921 warn only if the value is less than zero. */
2922 else if (pedantic && TYPE_DOMAIN (type) != 0
2923 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2924 error ("%Jzero or negative size array '%D'", decl, decl);
2926 layout_decl (decl, 0);
2929 if (TREE_CODE (decl) == VAR_DECL)
2931 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2932 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2933 layout_decl (decl, 0);
2935 if (DECL_SIZE (decl) == 0
2936 /* Don't give an error if we already gave one earlier. */
2937 && TREE_TYPE (decl) != error_mark_node
2938 && (TREE_STATIC (decl)
2940 /* A static variable with an incomplete type
2941 is an error if it is initialized.
2942 Also if it is not file scope.
2943 Otherwise, let it through, but if it is not `extern'
2944 then it may cause an error message later. */
2945 (DECL_INITIAL (decl) != 0
2946 || !DECL_FILE_SCOPE_P (decl))
2948 /* An automatic variable with an incomplete type
2949 is an error. */
2950 !DECL_EXTERNAL (decl)))
2952 error ("%Jstorage size of '%D' isn't known", decl, decl);
2953 TREE_TYPE (decl) = error_mark_node;
2956 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2957 && DECL_SIZE (decl) != 0)
2959 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2960 constant_expression_warning (DECL_SIZE (decl));
2961 else
2962 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2965 if (TREE_USED (type))
2966 TREE_USED (decl) = 1;
2969 /* If this is a function and an assembler name is specified, reset DECL_RTL
2970 so we can give it its new name. Also, update built_in_decls if it
2971 was a normal built-in. */
2972 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2974 /* ASMSPEC is given, and not the name of a register. Mark the
2975 name with a star so assemble_name won't munge it. */
2976 char *starred = (char *) alloca (strlen (asmspec) + 2);
2977 starred[0] = '*';
2978 strcpy (starred + 1, asmspec);
2980 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2982 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2983 SET_DECL_RTL (builtin, NULL_RTX);
2984 change_decl_assembler_name (builtin, get_identifier (starred));
2985 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2986 init_block_move_fn (starred);
2987 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2988 init_block_clear_fn (starred);
2990 SET_DECL_RTL (decl, NULL_RTX);
2991 change_decl_assembler_name (decl, get_identifier (starred));
2994 /* If #pragma weak was used, mark the decl weak now. */
2995 if (current_scope == file_scope)
2996 maybe_apply_pragma_weak (decl);
2998 /* Output the assembler code and/or RTL code for variables and functions,
2999 unless the type is an undefined structure or union.
3000 If not, it will get done when the type is completed. */
3002 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3004 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3005 if (c_dialect_objc ())
3006 objc_check_decl (decl);
3008 if (DECL_FILE_SCOPE_P (decl))
3010 if (DECL_INITIAL (decl) == NULL_TREE
3011 || DECL_INITIAL (decl) == error_mark_node)
3012 /* Don't output anything
3013 when a tentative file-scope definition is seen.
3014 But at end of compilation, do output code for them. */
3015 DECL_DEFER_OUTPUT (decl) = 1;
3016 rest_of_decl_compilation (decl, asmspec, true, 0);
3018 else
3020 /* This is a local variable. If there is an ASMSPEC, the
3021 user has requested that we handle it specially. */
3022 if (asmspec)
3024 /* In conjunction with an ASMSPEC, the `register'
3025 keyword indicates that we should place the variable
3026 in a particular register. */
3027 if (C_DECL_REGISTER (decl))
3029 DECL_HARD_REGISTER (decl) = 1;
3030 /* This cannot be done for a structure with volatile
3031 fields, on which DECL_REGISTER will have been
3032 reset. */
3033 if (!DECL_REGISTER (decl))
3034 error ("cannot put object with volatile field into register");
3037 /* If this is not a static variable, issue a warning.
3038 It doesn't make any sense to give an ASMSPEC for an
3039 ordinary, non-register local variable. Historically,
3040 GCC has accepted -- but ignored -- the ASMSPEC in
3041 this case. */
3042 if (TREE_CODE (decl) == VAR_DECL
3043 && !C_DECL_REGISTER (decl)
3044 && !TREE_STATIC (decl))
3045 warning ("%Jignoring asm-specifier for non-static local "
3046 "variable '%D'", decl, decl);
3047 else
3048 change_decl_assembler_name (decl, get_identifier (asmspec));
3051 if (TREE_CODE (decl) != FUNCTION_DECL)
3052 add_stmt (build_stmt (DECL_EXPR, decl));
3055 if (!DECL_FILE_SCOPE_P (decl))
3057 /* Recompute the RTL of a local array now
3058 if it used to be an incomplete type. */
3059 if (was_incomplete
3060 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3062 /* If we used it already as memory, it must stay in memory. */
3063 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3064 /* If it's still incomplete now, no init will save it. */
3065 if (DECL_SIZE (decl) == 0)
3066 DECL_INITIAL (decl) = 0;
3071 /* If this was marked 'used', be sure it will be output. */
3072 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3073 mark_decl_referenced (decl);
3075 if (TREE_CODE (decl) == TYPE_DECL)
3077 if (!DECL_FILE_SCOPE_P (decl)
3078 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3079 add_stmt (build_stmt (DECL_EXPR, decl));
3081 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3084 /* At the end of a declaration, throw away any variable type sizes
3085 of types defined inside that declaration. There is no use
3086 computing them in the following function definition. */
3087 if (current_scope == file_scope)
3088 get_pending_sizes ();
3090 /* Install a cleanup (aka destructor) if one was given. */
3091 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3093 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3094 if (attr)
3096 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3097 tree cleanup_decl = lookup_name (cleanup_id);
3098 tree cleanup;
3100 /* Build "cleanup(&decl)" for the destructor. */
3101 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3102 cleanup = build_tree_list (NULL_TREE, cleanup);
3103 cleanup = build_function_call (cleanup_decl, cleanup);
3105 /* Don't warn about decl unused; the cleanup uses it. */
3106 TREE_USED (decl) = 1;
3107 TREE_USED (cleanup_decl) = 1;
3109 /* Initialize EH, if we've been told to do so. */
3110 if (flag_exceptions && !c_eh_initialized_p)
3112 c_eh_initialized_p = true;
3113 eh_personality_libfunc
3114 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3115 ? "__gcc_personality_sj0"
3116 : "__gcc_personality_v0");
3117 using_eh_for_cleanups ();
3120 push_cleanup (decl, cleanup, false);
3125 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3126 and push that on the current scope. */
3128 void
3129 push_parm_decl (tree parm)
3131 tree decl;
3133 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3134 TREE_PURPOSE (TREE_PURPOSE (parm)),
3135 PARM, 0, NULL);
3136 decl_attributes (&decl, TREE_VALUE (parm), 0);
3138 decl = pushdecl (decl);
3140 finish_decl (decl, NULL_TREE, NULL_TREE);
3143 /* Mark all the parameter declarations to date as forward decls.
3144 Also diagnose use of this extension. */
3146 void
3147 mark_forward_parm_decls (void)
3149 struct c_binding *b;
3151 if (pedantic && !current_scope->warned_forward_parm_decls)
3153 pedwarn ("ISO C forbids forward parameter declarations");
3154 current_scope->warned_forward_parm_decls = true;
3157 for (b = current_scope->bindings; b; b = b->prev)
3158 if (TREE_CODE (b->decl) == PARM_DECL)
3159 TREE_ASM_WRITTEN (b->decl) = 1;
3162 static GTY(()) int compound_literal_number;
3164 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3165 literal, which may be an incomplete array type completed by the
3166 initializer; INIT is a CONSTRUCTOR that initializes the compound
3167 literal. */
3169 tree
3170 build_compound_literal (tree type, tree init)
3172 /* We do not use start_decl here because we have a type, not a declarator;
3173 and do not use finish_decl because the decl should be stored inside
3174 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3175 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3176 tree complit;
3177 tree stmt;
3178 DECL_EXTERNAL (decl) = 0;
3179 TREE_PUBLIC (decl) = 0;
3180 TREE_STATIC (decl) = (current_scope == file_scope);
3181 DECL_CONTEXT (decl) = current_function_decl;
3182 TREE_USED (decl) = 1;
3183 TREE_TYPE (decl) = type;
3184 TREE_READONLY (decl) = TYPE_READONLY (type);
3185 store_init_value (decl, init);
3187 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3189 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3190 if (failure)
3191 abort ();
3194 type = TREE_TYPE (decl);
3195 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3196 return error_mark_node;
3198 stmt = build_stmt (DECL_EXPR, decl);
3199 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3200 TREE_SIDE_EFFECTS (complit) = 1;
3202 layout_decl (decl, 0);
3204 if (TREE_STATIC (decl))
3206 /* This decl needs a name for the assembler output. We also need
3207 a unique suffix to be added to the name. */
3208 char *name;
3210 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3211 compound_literal_number);
3212 compound_literal_number++;
3213 DECL_NAME (decl) = get_identifier (name);
3214 DECL_DEFER_OUTPUT (decl) = 1;
3215 DECL_COMDAT (decl) = 1;
3216 DECL_ARTIFICIAL (decl) = 1;
3217 pushdecl (decl);
3218 rest_of_decl_compilation (decl, NULL, 1, 0);
3221 return complit;
3224 /* Make TYPE a complete type based on INITIAL_VALUE.
3225 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3226 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3229 complete_array_type (tree type, tree initial_value, int do_default)
3231 tree maxindex = NULL_TREE;
3232 int value = 0;
3234 if (initial_value)
3236 /* Note MAXINDEX is really the maximum index,
3237 one less than the size. */
3238 if (TREE_CODE (initial_value) == STRING_CST)
3240 int eltsize
3241 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3242 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3243 / eltsize) - 1, 0);
3245 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3247 tree elts = CONSTRUCTOR_ELTS (initial_value);
3248 maxindex = build_int_2 (-1, -1);
3249 for (; elts; elts = TREE_CHAIN (elts))
3251 if (TREE_PURPOSE (elts))
3252 maxindex = TREE_PURPOSE (elts);
3253 else
3254 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3255 maxindex, integer_one_node));
3258 else
3260 /* Make an error message unless that happened already. */
3261 if (initial_value != error_mark_node)
3262 value = 1;
3264 /* Prevent further error messages. */
3265 maxindex = build_int_2 (0, 0);
3269 if (!maxindex)
3271 if (do_default)
3272 maxindex = build_int_2 (0, 0);
3273 value = 2;
3276 if (maxindex)
3278 TYPE_DOMAIN (type) = build_index_type (maxindex);
3279 if (!TREE_TYPE (maxindex))
3280 abort ();
3283 /* Lay out the type now that we can get the real answer. */
3285 layout_type (type);
3287 return value;
3290 /* Determine whether TYPE is a structure with a flexible array member,
3291 or a union containing such a structure (possibly recursively). */
3293 static bool
3294 flexible_array_type_p (tree type)
3296 tree x;
3297 switch (TREE_CODE (type))
3299 case RECORD_TYPE:
3300 x = TYPE_FIELDS (type);
3301 if (x == NULL_TREE)
3302 return false;
3303 while (TREE_CHAIN (x) != NULL_TREE)
3304 x = TREE_CHAIN (x);
3305 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3306 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3307 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3308 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3309 return true;
3310 return false;
3311 case UNION_TYPE:
3312 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3314 if (flexible_array_type_p (TREE_TYPE (x)))
3315 return true;
3317 return false;
3318 default:
3319 return false;
3323 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3324 replacing with appropriate values if they are invalid. */
3325 static void
3326 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3328 tree type_mv;
3329 unsigned int max_width;
3330 unsigned HOST_WIDE_INT w;
3331 const char *name = orig_name ? orig_name: _("<anonymous>");
3333 /* Necessary? */
3334 STRIP_NOPS (*width);
3336 /* Detect and ignore out of range field width and process valid
3337 field widths. */
3338 if (TREE_CODE (*width) != INTEGER_CST)
3340 error ("bit-field `%s' width not an integer constant", name);
3341 *width = integer_one_node;
3343 else
3345 constant_expression_warning (*width);
3346 if (tree_int_cst_sgn (*width) < 0)
3348 error ("negative width in bit-field `%s'", name);
3349 *width = integer_one_node;
3351 else if (integer_zerop (*width) && orig_name)
3353 error ("zero width for bit-field `%s'", name);
3354 *width = integer_one_node;
3358 /* Detect invalid bit-field type. */
3359 if (TREE_CODE (*type) != INTEGER_TYPE
3360 && TREE_CODE (*type) != BOOLEAN_TYPE
3361 && TREE_CODE (*type) != ENUMERAL_TYPE)
3363 error ("bit-field `%s' has invalid type", name);
3364 *type = unsigned_type_node;
3367 type_mv = TYPE_MAIN_VARIANT (*type);
3368 if (pedantic
3369 && type_mv != integer_type_node
3370 && type_mv != unsigned_type_node
3371 && type_mv != boolean_type_node)
3372 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3374 if (type_mv == boolean_type_node)
3375 max_width = CHAR_TYPE_SIZE;
3376 else
3377 max_width = TYPE_PRECISION (*type);
3379 if (0 < compare_tree_int (*width, max_width))
3381 error ("width of `%s' exceeds its type", name);
3382 w = max_width;
3383 *width = build_int_2 (w, 0);
3385 else
3386 w = tree_low_cst (*width, 1);
3388 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3390 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3391 if (!lt
3392 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3393 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3394 warning ("`%s' is narrower than values of its type", name);
3398 /* Given declspecs and a declarator,
3399 determine the name and type of the object declared
3400 and construct a ..._DECL node for it.
3401 (In one case we can return a ..._TYPE node instead.
3402 For invalid input we sometimes return 0.)
3404 DECLSPECS is a chain of tree_list nodes whose value fields
3405 are the storage classes and type specifiers.
3407 DECL_CONTEXT says which syntactic context this declaration is in:
3408 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3409 FUNCDEF for a function definition. Like NORMAL but a few different
3410 error messages in each case. Return value may be zero meaning
3411 this definition is too screwy to try to parse.
3412 PARM for a parameter declaration (either within a function prototype
3413 or before a function body). Make a PARM_DECL, or return void_type_node.
3414 TYPENAME if for a typename (in a cast or sizeof).
3415 Don't make a DECL node; just return the ..._TYPE node.
3416 FIELD for a struct or union field; make a FIELD_DECL.
3417 INITIALIZED is 1 if the decl has an initializer.
3418 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3419 representing the width of the bit-field.
3421 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3422 It may also be so in the PARM case, for a prototype where the
3423 argument type is specified but not the name.
3425 This function is where the complicated C meanings of `static'
3426 and `extern' are interpreted. */
3428 static tree
3429 grokdeclarator (tree declarator, tree declspecs,
3430 enum decl_context decl_context, int initialized, tree *width)
3432 int specbits = 0;
3433 tree spec;
3434 tree type = NULL_TREE;
3435 int longlong = 0;
3436 int constp;
3437 int restrictp;
3438 int volatilep;
3439 int type_quals = TYPE_UNQUALIFIED;
3440 int inlinep;
3441 int explicit_int = 0;
3442 int explicit_char = 0;
3443 int defaulted_int = 0;
3444 tree typedef_decl = 0;
3445 const char *name, *orig_name;
3446 tree typedef_type = 0;
3447 int funcdef_flag = 0;
3448 enum tree_code innermost_code = ERROR_MARK;
3449 int size_varies = 0;
3450 tree decl_attr = NULL_TREE;
3451 tree array_ptr_quals = NULL_TREE;
3452 int array_parm_static = 0;
3453 tree returned_attrs = NULL_TREE;
3454 bool bitfield = width != NULL;
3455 tree element_type;
3456 tree arg_info = NULL_TREE;
3458 if (decl_context == FUNCDEF)
3459 funcdef_flag = 1, decl_context = NORMAL;
3461 /* Look inside a declarator for the name being declared
3462 and get it as a string, for an error message. */
3464 tree decl = declarator;
3465 name = 0;
3467 while (decl)
3468 switch (TREE_CODE (decl))
3470 case ARRAY_REF:
3471 case INDIRECT_REF:
3472 case CALL_EXPR:
3473 innermost_code = TREE_CODE (decl);
3474 decl = TREE_OPERAND (decl, 0);
3475 break;
3477 case TREE_LIST:
3478 decl = TREE_VALUE (decl);
3479 break;
3481 case IDENTIFIER_NODE:
3482 name = IDENTIFIER_POINTER (decl);
3483 decl = 0;
3484 break;
3486 default:
3487 abort ();
3489 orig_name = name;
3490 if (name == 0)
3491 name = "type name";
3494 /* A function definition's declarator must have the form of
3495 a function declarator. */
3497 if (funcdef_flag && innermost_code != CALL_EXPR)
3498 return 0;
3500 /* If this looks like a function definition, make it one,
3501 even if it occurs where parms are expected.
3502 Then store_parm_decls will reject it and not use it as a parm. */
3503 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3504 decl_context = PARM;
3506 /* Look through the decl specs and record which ones appear.
3507 Some typespecs are defined as built-in typenames.
3508 Others, the ones that are modifiers of other types,
3509 are represented by bits in SPECBITS: set the bits for
3510 the modifiers that appear. Storage class keywords are also in SPECBITS.
3512 If there is a typedef name or a type, store the type in TYPE.
3513 This includes builtin typedefs such as `int'.
3515 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3516 and did not come from a user typedef.
3518 Set LONGLONG if `long' is mentioned twice. */
3520 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3522 tree id = TREE_VALUE (spec);
3524 /* If the entire declaration is itself tagged as deprecated then
3525 suppress reports of deprecated items. */
3526 if (id && TREE_DEPRECATED (id))
3528 if (deprecated_state != DEPRECATED_SUPPRESS)
3529 warn_deprecated_use (id);
3532 if (id == ridpointers[(int) RID_INT])
3533 explicit_int = 1;
3534 if (id == ridpointers[(int) RID_CHAR])
3535 explicit_char = 1;
3537 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3539 enum rid i = C_RID_CODE (id);
3540 if ((int) i <= (int) RID_LAST_MODIFIER)
3542 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3544 if (longlong)
3545 error ("`long long long' is too long for GCC");
3546 else
3548 if (pedantic && !flag_isoc99 && ! in_system_header
3549 && warn_long_long)
3550 pedwarn ("ISO C90 does not support `long long'");
3551 longlong = 1;
3554 else if (specbits & (1 << (int) i))
3556 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3558 if (pedantic && !flag_isoc99)
3559 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3561 else
3562 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3565 /* Diagnose "__thread extern". Recall that this list
3566 is in the reverse order seen in the text. */
3567 if (i == RID_THREAD
3568 && (specbits & (1 << (int) RID_EXTERN
3569 | 1 << (int) RID_STATIC)))
3571 if (specbits & 1 << (int) RID_EXTERN)
3572 error ("`__thread' before `extern'");
3573 else
3574 error ("`__thread' before `static'");
3577 specbits |= 1 << (int) i;
3578 goto found;
3581 if (type)
3582 error ("two or more data types in declaration of `%s'", name);
3583 /* Actual typedefs come to us as TYPE_DECL nodes. */
3584 else if (TREE_CODE (id) == TYPE_DECL)
3586 if (TREE_TYPE (id) == error_mark_node)
3587 ; /* Allow the type to default to int to avoid cascading errors. */
3588 else
3590 type = TREE_TYPE (id);
3591 decl_attr = DECL_ATTRIBUTES (id);
3592 typedef_decl = id;
3595 /* Built-in types come as identifiers. */
3596 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3598 tree t = lookup_name (id);
3599 if (!t || TREE_CODE (t) != TYPE_DECL)
3600 error ("`%s' fails to be a typedef or built in type",
3601 IDENTIFIER_POINTER (id));
3602 else if (TREE_TYPE (t) == error_mark_node)
3604 else
3606 type = TREE_TYPE (t);
3607 typedef_decl = t;
3610 else if (TREE_CODE (id) != ERROR_MARK)
3611 type = id;
3613 found:
3617 typedef_type = type;
3618 if (type)
3619 size_varies = C_TYPE_VARIABLE_SIZE (type);
3621 /* No type at all: default to `int', and set DEFAULTED_INT
3622 because it was not a user-defined typedef. */
3624 if (type == 0)
3626 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3627 | (1 << (int) RID_SIGNED)
3628 | (1 << (int) RID_UNSIGNED)
3629 | (1 << (int) RID_COMPLEX))))
3630 /* Don't warn about typedef foo = bar. */
3631 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3632 && ! in_system_header)
3634 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3635 and this is a function, or if -Wimplicit; prefer the former
3636 warning since it is more explicit. */
3637 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3638 && funcdef_flag)
3639 warn_about_return_type = 1;
3640 else if (warn_implicit_int || flag_isoc99)
3641 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3642 name);
3645 defaulted_int = 1;
3646 type = integer_type_node;
3649 /* Now process the modifiers that were specified
3650 and check for invalid combinations. */
3652 /* Long double is a special combination. */
3654 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3655 && TYPE_MAIN_VARIANT (type) == double_type_node)
3657 specbits &= ~(1 << (int) RID_LONG);
3658 type = long_double_type_node;
3661 /* Check all other uses of type modifiers. */
3663 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3664 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3666 int ok = 0;
3668 if ((specbits & 1 << (int) RID_LONG)
3669 && (specbits & 1 << (int) RID_SHORT))
3670 error ("both long and short specified for `%s'", name);
3671 else if (((specbits & 1 << (int) RID_LONG)
3672 || (specbits & 1 << (int) RID_SHORT))
3673 && explicit_char)
3674 error ("long or short specified with char for `%s'", name);
3675 else if (((specbits & 1 << (int) RID_LONG)
3676 || (specbits & 1 << (int) RID_SHORT))
3677 && TREE_CODE (type) == REAL_TYPE)
3679 static int already = 0;
3681 error ("long or short specified with floating type for `%s'", name);
3682 if (! already && ! pedantic)
3684 error ("the only valid combination is `long double'");
3685 already = 1;
3688 else if ((specbits & 1 << (int) RID_SIGNED)
3689 && (specbits & 1 << (int) RID_UNSIGNED))
3690 error ("both signed and unsigned specified for `%s'", name);
3691 else if (TREE_CODE (type) != INTEGER_TYPE)
3692 error ("long, short, signed or unsigned invalid for `%s'", name);
3693 else
3695 ok = 1;
3696 if (!explicit_int && !defaulted_int && !explicit_char)
3698 error ("long, short, signed or unsigned used invalidly for `%s'",
3699 name);
3700 ok = 0;
3704 /* Discard the type modifiers if they are invalid. */
3705 if (! ok)
3707 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3708 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3709 longlong = 0;
3713 if ((specbits & (1 << (int) RID_COMPLEX))
3714 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3716 error ("complex invalid for `%s'", name);
3717 specbits &= ~(1 << (int) RID_COMPLEX);
3720 /* Decide whether an integer type is signed or not.
3721 Optionally treat bit-fields as signed by default. */
3722 if (specbits & 1 << (int) RID_UNSIGNED
3723 || (bitfield && ! flag_signed_bitfields
3724 && (explicit_int || defaulted_int || explicit_char
3725 /* A typedef for plain `int' without `signed'
3726 can be controlled just like plain `int'. */
3727 || ! (typedef_decl != 0
3728 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3729 && TREE_CODE (type) != ENUMERAL_TYPE
3730 && !(specbits & 1 << (int) RID_SIGNED)))
3732 if (longlong)
3733 type = long_long_unsigned_type_node;
3734 else if (specbits & 1 << (int) RID_LONG)
3735 type = long_unsigned_type_node;
3736 else if (specbits & 1 << (int) RID_SHORT)
3737 type = short_unsigned_type_node;
3738 else if (type == char_type_node)
3739 type = unsigned_char_type_node;
3740 else if (typedef_decl)
3741 type = c_common_unsigned_type (type);
3742 else
3743 type = unsigned_type_node;
3745 else if ((specbits & 1 << (int) RID_SIGNED)
3746 && type == char_type_node)
3747 type = signed_char_type_node;
3748 else if (longlong)
3749 type = long_long_integer_type_node;
3750 else if (specbits & 1 << (int) RID_LONG)
3751 type = long_integer_type_node;
3752 else if (specbits & 1 << (int) RID_SHORT)
3753 type = short_integer_type_node;
3755 if (specbits & 1 << (int) RID_COMPLEX)
3757 if (pedantic && !flag_isoc99)
3758 pedwarn ("ISO C90 does not support complex types");
3759 /* If we just have "complex", it is equivalent to
3760 "complex double", but if any modifiers at all are specified it is
3761 the complex form of TYPE. E.g, "complex short" is
3762 "complex short int". */
3764 if (defaulted_int && ! longlong
3765 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3766 | (1 << (int) RID_SIGNED)
3767 | (1 << (int) RID_UNSIGNED))))
3769 if (pedantic)
3770 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3771 type = complex_double_type_node;
3773 else if (type == integer_type_node)
3775 if (pedantic)
3776 pedwarn ("ISO C does not support complex integer types");
3777 type = complex_integer_type_node;
3779 else if (type == float_type_node)
3780 type = complex_float_type_node;
3781 else if (type == double_type_node)
3782 type = complex_double_type_node;
3783 else if (type == long_double_type_node)
3784 type = complex_long_double_type_node;
3785 else
3787 if (pedantic)
3788 pedwarn ("ISO C does not support complex integer types");
3789 type = build_complex_type (type);
3793 /* Check the type and width of a bit-field. */
3794 if (bitfield)
3795 check_bitfield_type_and_width (&type, width, orig_name);
3797 /* Figure out the type qualifiers for the declaration. There are
3798 two ways a declaration can become qualified. One is something
3799 like `const int i' where the `const' is explicit. Another is
3800 something like `typedef const int CI; CI i' where the type of the
3801 declaration contains the `const'. A third possibility is that
3802 there is a type qualifier on the element type of a typedefed
3803 array type, in which case we should extract that qualifier so
3804 that c_apply_type_quals_to_decls receives the full list of
3805 qualifiers to work with (C90 is not entirely clear about whether
3806 duplicate qualifiers should be diagnosed in this case, but it
3807 seems most appropriate to do so). */
3808 element_type = strip_array_types (type);
3809 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3810 restrictp
3811 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3812 volatilep
3813 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3814 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3815 if (pedantic && !flag_isoc99)
3817 if (constp > 1)
3818 pedwarn ("duplicate `const'");
3819 if (restrictp > 1)
3820 pedwarn ("duplicate `restrict'");
3821 if (volatilep > 1)
3822 pedwarn ("duplicate `volatile'");
3824 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3825 type = TYPE_MAIN_VARIANT (type);
3826 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3827 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3828 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3830 /* Warn if two storage classes are given. Default to `auto'. */
3833 int nclasses = 0;
3835 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3836 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3837 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3838 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3839 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3841 /* "static __thread" and "extern __thread" are allowed. */
3842 if ((specbits & (1 << (int) RID_THREAD
3843 | 1 << (int) RID_STATIC
3844 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3845 nclasses++;
3847 /* Warn about storage classes that are invalid for certain
3848 kinds of declarations (parameters, typenames, etc.). */
3850 if (nclasses > 1)
3851 error ("multiple storage classes in declaration of `%s'", name);
3852 else if (funcdef_flag
3853 && (specbits
3854 & ((1 << (int) RID_REGISTER)
3855 | (1 << (int) RID_AUTO)
3856 | (1 << (int) RID_TYPEDEF)
3857 | (1 << (int) RID_THREAD))))
3859 if (specbits & 1 << (int) RID_AUTO
3860 && (pedantic || current_scope == file_scope))
3861 pedwarn ("function definition declared `auto'");
3862 if (specbits & 1 << (int) RID_REGISTER)
3863 error ("function definition declared `register'");
3864 if (specbits & 1 << (int) RID_TYPEDEF)
3865 error ("function definition declared `typedef'");
3866 if (specbits & 1 << (int) RID_THREAD)
3867 error ("function definition declared `__thread'");
3868 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3869 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3871 else if (decl_context != NORMAL && nclasses > 0)
3873 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3875 else
3877 switch (decl_context)
3879 case FIELD:
3880 error ("storage class specified for structure field `%s'",
3881 name);
3882 break;
3883 case PARM:
3884 error ("storage class specified for parameter `%s'", name);
3885 break;
3886 default:
3887 error ("storage class specified for typename");
3888 break;
3890 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3891 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3892 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3895 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3897 /* `extern' with initialization is invalid if not at file scope. */
3898 if (current_scope == file_scope)
3899 warning ("`%s' initialized and declared `extern'", name);
3900 else
3901 error ("`%s' has both `extern' and initializer", name);
3903 else if (current_scope == file_scope)
3905 if (specbits & 1 << (int) RID_AUTO)
3906 error ("file-scope declaration of `%s' specifies `auto'", name);
3908 else
3910 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3911 error ("nested function `%s' declared `extern'", name);
3912 else if ((specbits & (1 << (int) RID_THREAD
3913 | 1 << (int) RID_EXTERN
3914 | 1 << (int) RID_STATIC))
3915 == (1 << (int) RID_THREAD))
3917 error ("function-scope `%s' implicitly auto and declared `__thread'",
3918 name);
3919 specbits &= ~(1 << (int) RID_THREAD);
3924 /* Now figure out the structure of the declarator proper.
3925 Descend through it, creating more complex types, until we reach
3926 the declared identifier (or NULL_TREE, in an absolute declarator). */
3928 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3930 if (type == error_mark_node)
3932 declarator = TREE_OPERAND (declarator, 0);
3933 continue;
3936 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3937 an INDIRECT_REF (for *...),
3938 a CALL_EXPR (for ...(...)),
3939 a TREE_LIST (for nested attributes),
3940 an identifier (for the name being declared)
3941 or a null pointer (for the place in an absolute declarator
3942 where the name was omitted).
3943 For the last two cases, we have just exited the loop.
3945 At this point, TYPE is the type of elements of an array,
3946 or for a function to return, or for a pointer to point to.
3947 After this sequence of ifs, TYPE is the type of the
3948 array or function or pointer, and DECLARATOR has had its
3949 outermost layer removed. */
3951 if (array_ptr_quals != NULL_TREE || array_parm_static)
3953 /* Only the innermost declarator (making a parameter be of
3954 array type which is converted to pointer type)
3955 may have static or type qualifiers. */
3956 error ("static or type qualifiers in non-parameter array declarator");
3957 array_ptr_quals = NULL_TREE;
3958 array_parm_static = 0;
3961 if (TREE_CODE (declarator) == TREE_LIST)
3963 /* We encode a declarator with embedded attributes using
3964 a TREE_LIST. */
3965 tree attrs = TREE_PURPOSE (declarator);
3966 tree inner_decl;
3967 int attr_flags = 0;
3968 declarator = TREE_VALUE (declarator);
3969 inner_decl = declarator;
3970 while (inner_decl != NULL_TREE
3971 && TREE_CODE (inner_decl) == TREE_LIST)
3972 inner_decl = TREE_VALUE (inner_decl);
3973 if (inner_decl == NULL_TREE
3974 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3975 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3976 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3977 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3978 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3979 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3980 returned_attrs = decl_attributes (&type,
3981 chainon (returned_attrs, attrs),
3982 attr_flags);
3984 else if (TREE_CODE (declarator) == ARRAY_REF)
3986 tree itype = NULL_TREE;
3987 tree size = TREE_OPERAND (declarator, 1);
3988 /* The index is a signed object `sizetype' bits wide. */
3989 tree index_type = c_common_signed_type (sizetype);
3991 array_ptr_quals = TREE_TYPE (declarator);
3992 array_parm_static = TREE_STATIC (declarator);
3994 declarator = TREE_OPERAND (declarator, 0);
3996 /* Check for some types that there cannot be arrays of. */
3998 if (VOID_TYPE_P (type))
4000 error ("declaration of `%s' as array of voids", name);
4001 type = error_mark_node;
4004 if (TREE_CODE (type) == FUNCTION_TYPE)
4006 error ("declaration of `%s' as array of functions", name);
4007 type = error_mark_node;
4010 if (pedantic && !in_system_header && flexible_array_type_p (type))
4011 pedwarn ("invalid use of structure with flexible array member");
4013 if (size == error_mark_node)
4014 type = error_mark_node;
4016 if (type == error_mark_node)
4017 continue;
4019 /* If size was specified, set ITYPE to a range-type for that size.
4020 Otherwise, ITYPE remains null. finish_decl may figure it out
4021 from an initial value. */
4023 if (size)
4025 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4026 STRIP_TYPE_NOPS (size);
4028 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4030 error ("size of array `%s' has non-integer type", name);
4031 size = integer_one_node;
4034 if (pedantic && integer_zerop (size))
4035 pedwarn ("ISO C forbids zero-size array `%s'", name);
4037 if (TREE_CODE (size) == INTEGER_CST)
4039 constant_expression_warning (size);
4040 if (tree_int_cst_sgn (size) < 0)
4042 error ("size of array `%s' is negative", name);
4043 size = integer_one_node;
4046 else
4048 /* Make sure the array size remains visibly nonconstant
4049 even if it is (eg) a const variable with known value. */
4050 size_varies = 1;
4052 if (!flag_isoc99 && pedantic)
4054 if (TREE_CONSTANT (size))
4055 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4056 name);
4057 else
4058 pedwarn ("ISO C90 forbids variable-size array `%s'",
4059 name);
4063 if (integer_zerop (size))
4065 /* A zero-length array cannot be represented with an
4066 unsigned index type, which is what we'll get with
4067 build_index_type. Create an open-ended range instead. */
4068 itype = build_range_type (sizetype, size, NULL_TREE);
4070 else
4072 /* Compute the maximum valid index, that is, size - 1.
4073 Do the calculation in index_type, so that if it is
4074 a variable the computations will be done in the
4075 proper mode. */
4076 itype = fold (build (MINUS_EXPR, index_type,
4077 convert (index_type, size),
4078 convert (index_type, size_one_node)));
4080 /* If that overflowed, the array is too big.
4081 ??? While a size of INT_MAX+1 technically shouldn't
4082 cause an overflow (because we subtract 1), the overflow
4083 is recorded during the conversion to index_type, before
4084 the subtraction. Handling this case seems like an
4085 unnecessary complication. */
4086 if (TREE_OVERFLOW (itype))
4088 error ("size of array `%s' is too large", name);
4089 type = error_mark_node;
4090 continue;
4093 if (size_varies)
4094 itype = variable_size (itype);
4095 itype = build_index_type (itype);
4098 else if (decl_context == FIELD)
4100 if (pedantic && !flag_isoc99 && !in_system_header)
4101 pedwarn ("ISO C90 does not support flexible array members");
4103 /* ISO C99 Flexible array members are effectively identical
4104 to GCC's zero-length array extension. */
4105 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4108 /* If pedantic, complain about arrays of incomplete types. */
4110 if (pedantic && !COMPLETE_TYPE_P (type))
4111 pedwarn ("array type has incomplete element type");
4113 /* Build the array type itself, then merge any constancy or
4114 volatility into the target type. We must do it in this order
4115 to ensure that the TYPE_MAIN_VARIANT field of the array type
4116 is set correctly. */
4118 type = build_array_type (type, itype);
4119 if (type_quals)
4120 type = c_build_qualified_type (type, type_quals);
4122 if (size_varies)
4123 C_TYPE_VARIABLE_SIZE (type) = 1;
4125 /* The GCC extension for zero-length arrays differs from
4126 ISO flexible array members in that sizeof yields zero. */
4127 if (size && integer_zerop (size))
4129 layout_type (type);
4130 TYPE_SIZE (type) = bitsize_zero_node;
4131 TYPE_SIZE_UNIT (type) = size_zero_node;
4133 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4134 /* We can never complete an array type which is the target of a
4135 pointer, so go ahead and lay it out. */
4136 layout_type (type);
4138 if (decl_context != PARM
4139 && (array_ptr_quals != NULL_TREE || array_parm_static))
4141 error ("static or type qualifiers in non-parameter array declarator");
4142 array_ptr_quals = NULL_TREE;
4143 array_parm_static = 0;
4146 else if (TREE_CODE (declarator) == CALL_EXPR)
4148 /* Say it's a definition only for the CALL_EXPR closest to
4149 the identifier. */
4150 bool really_funcdef = (funcdef_flag
4151 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4152 == IDENTIFIER_NODE));
4153 tree arg_types;
4155 /* Declaring a function type.
4156 Make sure we have a valid type for the function to return. */
4157 if (type == error_mark_node)
4158 continue;
4160 size_varies = 0;
4162 /* Warn about some types functions can't return. */
4164 if (TREE_CODE (type) == FUNCTION_TYPE)
4166 error ("`%s' declared as function returning a function", name);
4167 type = integer_type_node;
4169 if (TREE_CODE (type) == ARRAY_TYPE)
4171 error ("`%s' declared as function returning an array", name);
4172 type = integer_type_node;
4175 /* Construct the function type and go to the next
4176 inner layer of declarator. */
4177 arg_info = TREE_OPERAND (declarator, 1);
4178 arg_types = grokparms (arg_info, really_funcdef);
4180 /* Type qualifiers before the return type of the function
4181 qualify the return type, not the function type. */
4182 if (type_quals)
4184 /* Type qualifiers on a function return type are
4185 normally permitted by the standard but have no
4186 effect, so give a warning at -Wreturn-type.
4187 Qualifiers on a void return type are banned on
4188 function definitions in ISO C; GCC used to used them
4189 for noreturn functions. */
4190 if (VOID_TYPE_P (type) && really_funcdef)
4191 pedwarn ("function definition has qualified void return type");
4192 else if (warn_return_type)
4193 warning ("type qualifiers ignored on function return type");
4195 type = c_build_qualified_type (type, type_quals);
4197 type_quals = TYPE_UNQUALIFIED;
4199 type = build_function_type (type, arg_types);
4200 declarator = TREE_OPERAND (declarator, 0);
4202 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4203 the formal parameter list of this FUNCTION_TYPE to point to
4204 the FUNCTION_TYPE node itself. */
4207 tree link;
4209 for (link = ARG_INFO_TAGS (arg_info);
4210 link;
4211 link = TREE_CHAIN (link))
4212 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4215 else if (TREE_CODE (declarator) == INDIRECT_REF)
4217 /* Merge any constancy or volatility into the target type
4218 for the pointer. */
4220 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4221 && type_quals)
4222 pedwarn ("ISO C forbids qualified function types");
4223 if (type_quals)
4224 type = c_build_qualified_type (type, type_quals);
4225 type_quals = TYPE_UNQUALIFIED;
4226 size_varies = 0;
4228 type = build_pointer_type (type);
4230 /* Process a list of type modifier keywords
4231 (such as const or volatile) that were given inside the `*'. */
4233 if (TREE_TYPE (declarator))
4235 tree typemodlist;
4236 int erred = 0;
4238 constp = 0;
4239 volatilep = 0;
4240 restrictp = 0;
4241 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4242 typemodlist = TREE_CHAIN (typemodlist))
4244 tree qualifier = TREE_VALUE (typemodlist);
4246 if (C_IS_RESERVED_WORD (qualifier))
4248 if (C_RID_CODE (qualifier) == RID_CONST)
4249 constp++;
4250 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4251 volatilep++;
4252 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4253 restrictp++;
4254 else
4255 erred++;
4257 else
4258 erred++;
4261 if (erred)
4262 error ("invalid type modifier within pointer declarator");
4263 if (pedantic && !flag_isoc99)
4265 if (constp > 1)
4266 pedwarn ("duplicate `const'");
4267 if (volatilep > 1)
4268 pedwarn ("duplicate `volatile'");
4269 if (restrictp > 1)
4270 pedwarn ("duplicate `restrict'");
4273 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4274 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4275 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4278 declarator = TREE_OPERAND (declarator, 0);
4280 else
4281 abort ();
4285 /* Now TYPE has the actual type. */
4287 /* Did array size calculations overflow? */
4289 if (TREE_CODE (type) == ARRAY_TYPE
4290 && COMPLETE_TYPE_P (type)
4291 && TREE_OVERFLOW (TYPE_SIZE (type)))
4293 error ("size of array `%s' is too large", name);
4294 /* If we proceed with the array type as it is, we'll eventually
4295 crash in tree_low_cst(). */
4296 type = error_mark_node;
4299 /* If this is declaring a typedef name, return a TYPE_DECL. */
4301 if (specbits & (1 << (int) RID_TYPEDEF))
4303 tree decl;
4304 /* Note that the grammar rejects storage classes
4305 in typenames, fields or parameters */
4306 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4307 && type_quals)
4308 pedwarn ("ISO C forbids qualified function types");
4309 if (type_quals)
4310 type = c_build_qualified_type (type, type_quals);
4311 decl = build_decl (TYPE_DECL, declarator, type);
4312 if ((specbits & (1 << (int) RID_SIGNED))
4313 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4314 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4315 decl_attributes (&decl, returned_attrs, 0);
4316 return decl;
4319 /* Detect the case of an array type of unspecified size
4320 which came, as such, direct from a typedef name.
4321 We must copy the type, so that each identifier gets
4322 a distinct type, so that each identifier's size can be
4323 controlled separately by its own initializer. */
4325 if (type != 0 && typedef_type != 0
4326 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4327 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4329 type = build_array_type (TREE_TYPE (type), 0);
4330 if (size_varies)
4331 C_TYPE_VARIABLE_SIZE (type) = 1;
4334 /* If this is a type name (such as, in a cast or sizeof),
4335 compute the type and return it now. */
4337 if (decl_context == TYPENAME)
4339 /* Note that the grammar rejects storage classes
4340 in typenames, fields or parameters */
4341 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4342 && type_quals)
4343 pedwarn ("ISO C forbids const or volatile function types");
4344 if (type_quals)
4345 type = c_build_qualified_type (type, type_quals);
4346 decl_attributes (&type, returned_attrs, 0);
4347 return type;
4350 /* Aside from typedefs and type names (handle above),
4351 `void' at top level (not within pointer)
4352 is allowed only in public variables.
4353 We don't complain about parms either, but that is because
4354 a better error message can be made later. */
4356 if (VOID_TYPE_P (type) && decl_context != PARM
4357 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4358 && ((specbits & (1 << (int) RID_EXTERN))
4359 || (current_scope == file_scope
4360 && !(specbits
4361 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4363 error ("variable or field `%s' declared void", name);
4364 type = integer_type_node;
4367 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4368 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4371 tree decl;
4373 if (decl_context == PARM)
4375 tree type_as_written;
4376 tree promoted_type;
4378 /* A parameter declared as an array of T is really a pointer to T.
4379 One declared as a function is really a pointer to a function. */
4381 if (TREE_CODE (type) == ARRAY_TYPE)
4383 /* Transfer const-ness of array into that of type pointed to. */
4384 type = TREE_TYPE (type);
4385 if (type_quals)
4386 type = c_build_qualified_type (type, type_quals);
4387 type = build_pointer_type (type);
4388 type_quals = TYPE_UNQUALIFIED;
4389 if (array_ptr_quals)
4391 tree new_ptr_quals, new_ptr_attrs;
4392 int erred = 0;
4393 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4394 /* We don't yet implement attributes in this context. */
4395 if (new_ptr_attrs != NULL_TREE)
4396 warning ("attributes in parameter array declarator ignored");
4398 constp = 0;
4399 volatilep = 0;
4400 restrictp = 0;
4401 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4403 tree qualifier = TREE_VALUE (new_ptr_quals);
4405 if (C_IS_RESERVED_WORD (qualifier))
4407 if (C_RID_CODE (qualifier) == RID_CONST)
4408 constp++;
4409 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4410 volatilep++;
4411 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4412 restrictp++;
4413 else
4414 erred++;
4416 else
4417 erred++;
4420 if (erred)
4421 error ("invalid type modifier within array declarator");
4423 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4424 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4425 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4427 size_varies = 0;
4429 else if (TREE_CODE (type) == FUNCTION_TYPE)
4431 if (pedantic && type_quals)
4432 pedwarn ("ISO C forbids qualified function types");
4433 if (type_quals)
4434 type = c_build_qualified_type (type, type_quals);
4435 type = build_pointer_type (type);
4436 type_quals = TYPE_UNQUALIFIED;
4438 else if (type_quals)
4439 type = c_build_qualified_type (type, type_quals);
4441 type_as_written = type;
4443 decl = build_decl (PARM_DECL, declarator, type);
4444 if (size_varies)
4445 C_DECL_VARIABLE_SIZE (decl) = 1;
4447 /* Compute the type actually passed in the parmlist,
4448 for the case where there is no prototype.
4449 (For example, shorts and chars are passed as ints.)
4450 When there is a prototype, this is overridden later. */
4452 if (type == error_mark_node)
4453 promoted_type = type;
4454 else
4455 promoted_type = c_type_promotes_to (type);
4457 DECL_ARG_TYPE (decl) = promoted_type;
4458 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4460 else if (decl_context == FIELD)
4462 /* Structure field. It may not be a function. */
4464 if (TREE_CODE (type) == FUNCTION_TYPE)
4466 error ("field `%s' declared as a function", name);
4467 type = build_pointer_type (type);
4469 else if (TREE_CODE (type) != ERROR_MARK
4470 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4472 error ("field `%s' has incomplete type", name);
4473 type = error_mark_node;
4475 /* Move type qualifiers down to element of an array. */
4476 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4477 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4478 type_quals),
4479 TYPE_DOMAIN (type));
4480 decl = build_decl (FIELD_DECL, declarator, type);
4481 DECL_NONADDRESSABLE_P (decl) = bitfield;
4483 if (size_varies)
4484 C_DECL_VARIABLE_SIZE (decl) = 1;
4486 else if (TREE_CODE (type) == FUNCTION_TYPE)
4488 if (specbits & (1 << (int) RID_AUTO)
4489 && (pedantic || current_scope == file_scope))
4490 pedwarn ("invalid storage class for function `%s'", name);
4491 if (specbits & (1 << (int) RID_REGISTER))
4492 error ("invalid storage class for function `%s'", name);
4493 if (specbits & (1 << (int) RID_THREAD))
4494 error ("invalid storage class for function `%s'", name);
4495 /* Function declaration not at file scope.
4496 Storage classes other than `extern' are not allowed
4497 and `extern' makes no difference. */
4498 if (current_scope != file_scope
4499 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4500 && pedantic)
4501 pedwarn ("invalid storage class for function `%s'", name);
4503 decl = build_decl (FUNCTION_DECL, declarator, type);
4504 decl = build_decl_attribute_variant (decl, decl_attr);
4506 DECL_LANG_SPECIFIC (decl)
4507 = GGC_CNEW (struct lang_decl);
4509 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4510 pedwarn ("ISO C forbids qualified function types");
4512 /* GNU C interprets a volatile-qualified function type to indicate
4513 that the function does not return. */
4514 if ((type_quals & TYPE_QUAL_VOLATILE)
4515 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4516 warning ("`noreturn' function returns non-void value");
4518 /* Every function declaration is an external reference
4519 (DECL_EXTERNAL) except for those which are not at file
4520 scope and are explicitly declared "auto". This is
4521 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4522 GCC to signify a forward declaration of a nested function. */
4523 if ((specbits & (1 << RID_AUTO)) && current_scope != file_scope)
4524 DECL_EXTERNAL (decl) = 0;
4525 else
4526 DECL_EXTERNAL (decl) = 1;
4528 /* Record absence of global scope for `static' or `auto'. */
4529 TREE_PUBLIC (decl)
4530 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4532 /* For a function definition, record the argument information
4533 block in DECL_ARGUMENTS where store_parm_decls will look
4534 for it. */
4535 if (funcdef_flag)
4536 DECL_ARGUMENTS (decl) = arg_info;
4538 if (defaulted_int)
4539 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4541 /* Record presence of `inline', if it is reasonable. */
4542 if (MAIN_NAME_P (declarator))
4544 if (inlinep)
4545 warning ("cannot inline function `main'");
4547 else if (inlinep)
4549 /* Record that the function is declared `inline'. */
4550 DECL_DECLARED_INLINE_P (decl) = 1;
4552 /* Do not mark bare declarations as DECL_INLINE. Doing so
4553 in the presence of multiple declarations can result in
4554 the abstract origin pointing between the declarations,
4555 which will confuse dwarf2out. */
4556 if (initialized)
4558 DECL_INLINE (decl) = 1;
4559 if (specbits & (1 << (int) RID_EXTERN))
4560 current_extern_inline = 1;
4563 /* If -finline-functions, assume it can be inlined. This does
4564 two things: let the function be deferred until it is actually
4565 needed, and let dwarf2 know that the function is inlinable. */
4566 else if (flag_inline_trees == 2 && initialized)
4567 DECL_INLINE (decl) = 1;
4569 else
4571 /* It's a variable. */
4572 /* An uninitialized decl with `extern' is a reference. */
4573 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4575 /* Move type qualifiers down to element of an array. */
4576 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4578 int saved_align = TYPE_ALIGN(type);
4579 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4580 type_quals),
4581 TYPE_DOMAIN (type));
4582 TYPE_ALIGN (type) = saved_align;
4584 else if (type_quals)
4585 type = c_build_qualified_type (type, type_quals);
4587 /* C99 6.2.2p7: It is invalid (compile-time undefined
4588 behavior) to create an 'extern' declaration for a
4589 variable if there is a global declaration that is
4590 'static' and the global declaration is not visible.
4591 (If the static declaration _is_ currently visible,
4592 the 'extern' declaration is taken to refer to that decl.) */
4593 if (extern_ref && current_scope != file_scope)
4595 tree global_decl = identifier_global_value (declarator);
4596 tree visible_decl = lookup_name (declarator);
4598 if (global_decl
4599 && global_decl != visible_decl
4600 && TREE_CODE (global_decl) == VAR_DECL
4601 && !TREE_PUBLIC (global_decl))
4602 error ("variable previously declared 'static' redeclared "
4603 "'extern'");
4606 decl = build_decl (VAR_DECL, declarator, type);
4607 if (size_varies)
4608 C_DECL_VARIABLE_SIZE (decl) = 1;
4610 if (inlinep)
4611 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4613 /* At file scope, an initialized extern declaration may follow
4614 a static declaration. In that case, DECL_EXTERNAL will be
4615 reset later in start_decl. */
4616 DECL_EXTERNAL (decl) = !!(specbits & (1 << (int) RID_EXTERN));
4618 /* At file scope, the presence of a `static' or `register' storage
4619 class specifier, or the absence of all storage class specifiers
4620 makes this declaration a definition (perhaps tentative). Also,
4621 the absence of both `static' and `register' makes it public. */
4622 if (current_scope == file_scope)
4624 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4625 | (1 << (int) RID_REGISTER)));
4626 TREE_STATIC (decl) = !extern_ref;
4628 /* Not at file scope, only `static' makes a static definition. */
4629 else
4631 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4632 TREE_PUBLIC (decl) = extern_ref;
4635 if (specbits & 1 << (int) RID_THREAD)
4637 if (targetm.have_tls)
4638 DECL_THREAD_LOCAL (decl) = 1;
4639 else
4640 /* A mere warning is sure to result in improper semantics
4641 at runtime. Don't bother to allow this to compile. */
4642 error ("thread-local storage not supported for this target");
4646 /* Record `register' declaration for warnings on &
4647 and in case doing stupid register allocation. */
4649 if (specbits & (1 << (int) RID_REGISTER))
4651 C_DECL_REGISTER (decl) = 1;
4652 DECL_REGISTER (decl) = 1;
4655 /* Record constancy and volatility. */
4656 c_apply_type_quals_to_decl (type_quals, decl);
4658 /* If a type has volatile components, it should be stored in memory.
4659 Otherwise, the fact that those components are volatile
4660 will be ignored, and would even crash the compiler. */
4661 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4663 /* It is not an error for a structure with volatile fields to
4664 be declared register, but reset DECL_REGISTER since it
4665 cannot actually go in a register. */
4666 int was_reg = C_DECL_REGISTER (decl);
4667 C_DECL_REGISTER (decl) = 0;
4668 DECL_REGISTER (decl) = 0;
4669 c_mark_addressable (decl);
4670 C_DECL_REGISTER (decl) = was_reg;
4673 #ifdef ENABLE_CHECKING
4674 /* This is the earliest point at which we might know the assembler
4675 name of a variable. Thus, if it's known before this, die horribly. */
4676 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4677 abort ();
4678 #endif
4680 decl_attributes (&decl, returned_attrs, 0);
4682 return decl;
4686 /* Decode the parameter-list info for a function type or function definition.
4687 The argument is the value returned by `get_parm_info' (or made in parse.y
4688 if there is an identifier list instead of a parameter decl list).
4689 These two functions are separate because when a function returns
4690 or receives functions then each is called multiple times but the order
4691 of calls is different. The last call to `grokparms' is always the one
4692 that contains the formal parameter names of a function definition.
4694 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4696 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4697 a mere declaration. A nonempty identifier-list gets an error message
4698 when FUNCDEF_FLAG is zero. */
4700 static tree
4701 grokparms (tree arg_info, int funcdef_flag)
4703 tree arg_types = ARG_INFO_TYPES (arg_info);
4705 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4706 && !in_system_header)
4707 warning ("function declaration isn't a prototype");
4709 if (arg_types == error_mark_node)
4710 return 0; /* don't set TYPE_ARG_TYPES in this case */
4712 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4714 if (! funcdef_flag)
4715 pedwarn ("parameter names (without types) in function declaration");
4717 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4718 ARG_INFO_TYPES (arg_info) = 0;
4719 return 0;
4721 else
4723 tree parm, type, typelt;
4724 unsigned int parmno;
4726 /* If the arg types are incomplete in a declaration, they must
4727 include undefined tags. These tags can never be defined in
4728 the scope of the declaration, so the types can never be
4729 completed, and no call can be compiled successfully. */
4731 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4732 parm;
4733 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4735 type = TREE_VALUE (typelt);
4736 if (type == error_mark_node)
4737 continue;
4739 if (!COMPLETE_TYPE_P (type))
4741 if (funcdef_flag)
4743 if (DECL_NAME (parm))
4744 error ("%Jparameter %u ('%D') has incomplete type",
4745 parm, parmno, parm);
4746 else
4747 error ("%Jparameter %u has incomplete type",
4748 parm, parmno);
4750 TREE_VALUE (typelt) = error_mark_node;
4751 TREE_TYPE (parm) = error_mark_node;
4753 else
4755 if (DECL_NAME (parm))
4756 warning ("%Jparameter %u ('%D') has incomplete type",
4757 parm, parmno, parm);
4758 else
4759 warning ("%Jparameter %u has incomplete type",
4760 parm, parmno);
4764 return arg_types;
4768 /* Take apart the current scope and return a tree_list node with info
4769 on a parameter list just parsed. This tree_list node should be
4770 examined using the ARG_INFO_* macros, defined above:
4772 ARG_INFO_PARMS: a list of parameter decls.
4773 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4774 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4775 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4776 constants) defined with the parameters.
4778 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4780 ELLIPSIS being true means the argument list ended in '...' so don't
4781 append a sentinel (void_list_node) to the end of the type-list. */
4783 tree
4784 get_parm_info (bool ellipsis)
4786 struct c_binding *b = current_scope->bindings;
4787 tree arg_info = make_node (TREE_LIST);
4788 tree parms = 0;
4789 tree tags = 0;
4790 tree types = 0;
4791 tree others = 0;
4793 static bool explained_incomplete_types = false;
4794 bool gave_void_only_once_err = false;
4796 /* The bindings in this scope must not get put into a block.
4797 We will take care of deleting the binding nodes. */
4798 current_scope->bindings = 0;
4800 /* This function is only called if there was *something* on the
4801 parameter list. */
4802 #ifdef ENABLE_CHECKING
4803 if (b == 0)
4804 abort ();
4805 #endif
4807 /* A parameter list consisting solely of 'void' indicates that the
4808 function takes no arguments. But if the 'void' is qualified
4809 (by 'const' or 'volatile'), or has a storage class specifier
4810 ('register'), then the behavior is undefined; issue an error.
4811 Typedefs for 'void' are OK (see DR#157). */
4812 if (b->prev == 0 /* one binding */
4813 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4814 && !DECL_NAME (b->decl) /* anonymous */
4815 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4817 if (TREE_THIS_VOLATILE (b->decl)
4818 || TREE_READONLY (b->decl)
4819 || C_DECL_REGISTER (b->decl))
4820 error ("'void' as only parameter may not be qualified");
4822 /* There cannot be an ellipsis. */
4823 if (ellipsis)
4824 error ("'void' must be the only parameter");
4826 ARG_INFO_TYPES (arg_info) = void_list_node;
4827 return arg_info;
4830 if (!ellipsis)
4831 types = void_list_node;
4833 /* Break up the bindings list into parms, tags, types, and others;
4834 apply sanity checks; purge the name-to-decl bindings. */
4835 while (b)
4837 tree decl = b->decl;
4838 tree type = TREE_TYPE (decl);
4839 const char *keyword;
4841 switch (TREE_CODE (decl))
4843 case PARM_DECL:
4844 if (b->id)
4846 #ifdef ENABLE_CHECKING
4847 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4848 #endif
4849 I_SYMBOL_BINDING (b->id) = b->shadowed;
4852 /* Check for forward decls that never got their actual decl. */
4853 if (TREE_ASM_WRITTEN (decl))
4854 error ("%Jparameter '%D' has just a forward declaration",
4855 decl, decl);
4856 /* Check for (..., void, ...) and issue an error. */
4857 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4859 if (!gave_void_only_once_err)
4861 error ("'void' must be the only parameter");
4862 gave_void_only_once_err = true;
4865 else
4867 /* Valid parameter, add it to the list. */
4868 TREE_CHAIN (decl) = parms;
4869 parms = decl;
4871 /* Since there is a prototype, args are passed in their
4872 declared types. The back end may override this later. */
4873 DECL_ARG_TYPE (decl) = type;
4874 types = tree_cons (0, type, types);
4876 break;
4878 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4879 case UNION_TYPE: keyword = "union"; goto tag;
4880 case RECORD_TYPE: keyword = "struct"; goto tag;
4881 tag:
4882 /* Types may not have tag-names, in which case the type
4883 appears in the bindings list with b->id NULL. */
4884 if (b->id)
4886 #ifdef ENABLE_CHECKING
4887 if (I_TAG_BINDING (b->id) != b) abort ();
4888 #endif
4889 I_TAG_BINDING (b->id) = b->shadowed;
4892 /* Warn about any struct, union or enum tags defined in a
4893 parameter list. The scope of such types is limited to
4894 the parameter list, which is rarely if ever desirable
4895 (it's impossible to call such a function with type-
4896 correct arguments). An anonymous union parm type is
4897 meaningful as a GNU extension, so don't warn for that. */
4898 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4900 if (b->id)
4901 /* The %s will be one of 'struct', 'union', or 'enum'. */
4902 warning ("'%s %E' declared inside parameter list",
4903 keyword, b->id);
4904 else
4905 /* The %s will be one of 'struct', 'union', or 'enum'. */
4906 warning ("anonymous %s declared inside parameter list",
4907 keyword);
4909 if (! explained_incomplete_types)
4911 warning ("its scope is only this definition or declaration,"
4912 " which is probably not what you want");
4913 explained_incomplete_types = true;
4917 tags = tree_cons (b->id, decl, tags);
4918 break;
4920 case CONST_DECL:
4921 case TYPE_DECL:
4922 /* CONST_DECLs appear here when we have an embedded enum,
4923 and TYPE_DECLs appear here when we have an embedded struct
4924 or union. No warnings for this - we already warned about the
4925 type itself. */
4926 TREE_CHAIN (decl) = others;
4927 others = decl;
4928 /* fall through */
4930 case ERROR_MARK:
4931 /* error_mark_node appears here when we have an undeclared
4932 variable. Just throw it away. */
4933 if (b->id)
4935 #ifdef ENABLE_CHECKING
4936 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4937 #endif
4938 I_SYMBOL_BINDING (b->id) = b->shadowed;
4940 break;
4942 /* Other things that might be encountered. */
4943 case LABEL_DECL:
4944 case FUNCTION_DECL:
4945 case VAR_DECL:
4946 default:
4947 abort ();
4950 b = free_binding_and_advance (b);
4953 ARG_INFO_PARMS (arg_info) = parms;
4954 ARG_INFO_TAGS (arg_info) = tags;
4955 ARG_INFO_TYPES (arg_info) = types;
4956 ARG_INFO_OTHERS (arg_info) = others;
4957 return arg_info;
4960 /* Get the struct, enum or union (CODE says which) with tag NAME.
4961 Define the tag as a forward-reference if it is not defined. */
4963 tree
4964 xref_tag (enum tree_code code, tree name)
4966 /* If a cross reference is requested, look up the type
4967 already defined for this tag and return it. */
4969 tree ref = lookup_tag (code, name, 0);
4970 /* If this is the right type of tag, return what we found.
4971 (This reference will be shadowed by shadow_tag later if appropriate.)
4972 If this is the wrong type of tag, do not return it. If it was the
4973 wrong type in the same scope, we will have had an error
4974 message already; if in a different scope and declaring
4975 a name, pending_xref_error will give an error message; but if in a
4976 different scope and not declaring a name, this tag should
4977 shadow the previous declaration of a different type of tag, and
4978 this would not work properly if we return the reference found.
4979 (For example, with "struct foo" in an outer scope, "union foo;"
4980 must shadow that tag with a new one of union type.) */
4981 if (ref && TREE_CODE (ref) == code)
4982 return ref;
4984 /* If no such tag is yet defined, create a forward-reference node
4985 and record it as the "definition".
4986 When a real declaration of this type is found,
4987 the forward-reference will be altered into a real type. */
4989 ref = make_node (code);
4990 if (code == ENUMERAL_TYPE)
4992 /* Give the type a default layout like unsigned int
4993 to avoid crashing if it does not get defined. */
4994 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4995 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4996 TYPE_USER_ALIGN (ref) = 0;
4997 TYPE_UNSIGNED (ref) = 1;
4998 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4999 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5000 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5003 pushtag (name, ref);
5005 return ref;
5008 /* Make sure that the tag NAME is defined *in the current scope*
5009 at least as a forward reference.
5010 CODE says which kind of tag NAME ought to be. */
5012 tree
5013 start_struct (enum tree_code code, tree name)
5015 /* If there is already a tag defined at this scope
5016 (as a forward reference), just return it. */
5018 tree ref = 0;
5020 if (name != 0)
5021 ref = lookup_tag (code, name, 1);
5022 if (ref && TREE_CODE (ref) == code)
5024 if (TYPE_FIELDS (ref))
5026 if (code == UNION_TYPE)
5027 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5028 else
5029 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5032 else
5034 /* Otherwise create a forward-reference just so the tag is in scope. */
5036 ref = make_node (code);
5037 pushtag (name, ref);
5040 C_TYPE_BEING_DEFINED (ref) = 1;
5041 TYPE_PACKED (ref) = flag_pack_struct;
5042 return ref;
5045 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5046 of a structure component, returning a FIELD_DECL node.
5047 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5049 This is done during the parsing of the struct declaration.
5050 The FIELD_DECL nodes are chained together and the lot of them
5051 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5053 tree
5054 grokfield (tree declarator, tree declspecs, tree width)
5056 tree value;
5058 if (declarator == NULL_TREE && width == NULL_TREE)
5060 /* This is an unnamed decl.
5062 If we have something of the form "union { list } ;" then this
5063 is the anonymous union extension. Similarly for struct.
5065 If this is something of the form "struct foo;", then
5066 If MS extensions are enabled, this is handled as an
5067 anonymous struct.
5068 Otherwise this is a forward declaration of a structure tag.
5070 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5071 If MS extensions are enabled and foo names a structure, then
5072 again this is an anonymous struct.
5073 Otherwise this is an error.
5075 Oh what a horrid tangled web we weave. I wonder if MS consciously
5076 took this from Plan 9 or if it was an accident of implementation
5077 that took root before someone noticed the bug... */
5079 tree type = TREE_VALUE (declspecs);
5081 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5082 type = TREE_TYPE (type);
5083 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5085 if (flag_ms_extensions)
5086 ; /* ok */
5087 else if (flag_iso)
5088 goto warn_unnamed_field;
5089 else if (TYPE_NAME (type) == NULL)
5090 ; /* ok */
5091 else
5092 goto warn_unnamed_field;
5094 else
5096 warn_unnamed_field:
5097 warning ("declaration does not declare anything");
5098 return NULL_TREE;
5102 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5103 width ? &width : NULL);
5105 finish_decl (value, NULL_TREE, NULL_TREE);
5106 DECL_INITIAL (value) = width;
5108 return value;
5111 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5112 the list such that this does not present a problem later. */
5114 static void
5115 detect_field_duplicates (tree fieldlist)
5117 tree x, y;
5118 int timeout = 10;
5120 /* First, see if there are more than "a few" fields.
5121 This is trivially true if there are zero or one fields. */
5122 if (!fieldlist)
5123 return;
5124 x = TREE_CHAIN (fieldlist);
5125 if (!x)
5126 return;
5127 do {
5128 timeout--;
5129 x = TREE_CHAIN (x);
5130 } while (timeout > 0 && x);
5132 /* If there were "few" fields, avoid the overhead of allocating
5133 a hash table. Instead just do the nested traversal thing. */
5134 if (timeout > 0)
5136 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5137 if (DECL_NAME (x))
5139 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5140 if (DECL_NAME (y) == DECL_NAME (x))
5142 error ("%Jduplicate member '%D'", x, x);
5143 DECL_NAME (x) = NULL_TREE;
5147 else
5149 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5150 void **slot;
5152 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5153 if ((y = DECL_NAME (x)) != 0)
5155 slot = htab_find_slot (htab, y, INSERT);
5156 if (*slot)
5158 error ("%Jduplicate member '%D'", x, x);
5159 DECL_NAME (x) = NULL_TREE;
5161 *slot = y;
5164 htab_delete (htab);
5168 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5169 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5170 ATTRIBUTES are attributes to be applied to the structure. */
5172 tree
5173 finish_struct (tree t, tree fieldlist, tree attributes)
5175 tree x;
5176 bool toplevel = file_scope == current_scope;
5177 int saw_named_field;
5179 /* If this type was previously laid out as a forward reference,
5180 make sure we lay it out again. */
5182 TYPE_SIZE (t) = 0;
5184 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5186 if (pedantic)
5188 for (x = fieldlist; x; x = TREE_CHAIN (x))
5189 if (DECL_NAME (x) != 0)
5190 break;
5192 if (x == 0)
5193 pedwarn ("%s has no %s",
5194 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5195 fieldlist ? _("named members") : _("members"));
5198 /* Install struct as DECL_CONTEXT of each field decl.
5199 Also process specified field sizes, found in the DECL_INITIAL,
5200 storing 0 there after the type has been changed to precision equal
5201 to its width, rather than the precision of the specified standard
5202 type. (Correct layout requires the original type to have been preserved
5203 until now.) */
5205 saw_named_field = 0;
5206 for (x = fieldlist; x; x = TREE_CHAIN (x))
5208 DECL_CONTEXT (x) = t;
5209 DECL_PACKED (x) |= TYPE_PACKED (t);
5211 /* If any field is const, the structure type is pseudo-const. */
5212 if (TREE_READONLY (x))
5213 C_TYPE_FIELDS_READONLY (t) = 1;
5214 else
5216 /* A field that is pseudo-const makes the structure likewise. */
5217 tree t1 = TREE_TYPE (x);
5218 while (TREE_CODE (t1) == ARRAY_TYPE)
5219 t1 = TREE_TYPE (t1);
5220 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5221 && C_TYPE_FIELDS_READONLY (t1))
5222 C_TYPE_FIELDS_READONLY (t) = 1;
5225 /* Any field that is volatile means variables of this type must be
5226 treated in some ways as volatile. */
5227 if (TREE_THIS_VOLATILE (x))
5228 C_TYPE_FIELDS_VOLATILE (t) = 1;
5230 /* Any field of nominal variable size implies structure is too. */
5231 if (C_DECL_VARIABLE_SIZE (x))
5232 C_TYPE_VARIABLE_SIZE (t) = 1;
5234 /* Detect invalid nested redefinition. */
5235 if (TREE_TYPE (x) == t)
5236 error ("nested redefinition of `%s'",
5237 IDENTIFIER_POINTER (TYPE_NAME (t)));
5239 if (DECL_INITIAL (x))
5241 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5242 DECL_SIZE (x) = bitsize_int (width);
5243 DECL_BIT_FIELD (x) = 1;
5244 SET_DECL_C_BIT_FIELD (x);
5247 /* Detect flexible array member in an invalid context. */
5248 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5249 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5250 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5251 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5253 if (TREE_CODE (t) == UNION_TYPE)
5255 error ("%Jflexible array member in union", x);
5256 TREE_TYPE (x) = error_mark_node;
5258 else if (TREE_CHAIN (x) != NULL_TREE)
5260 error ("%Jflexible array member not at end of struct", x);
5261 TREE_TYPE (x) = error_mark_node;
5263 else if (! saw_named_field)
5265 error ("%Jflexible array member in otherwise empty struct", x);
5266 TREE_TYPE (x) = error_mark_node;
5270 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5271 && flexible_array_type_p (TREE_TYPE (x)))
5272 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5274 if (DECL_NAME (x))
5275 saw_named_field = 1;
5278 detect_field_duplicates (fieldlist);
5280 /* Now we have the nearly final fieldlist. Record it,
5281 then lay out the structure or union (including the fields). */
5283 TYPE_FIELDS (t) = fieldlist;
5285 layout_type (t);
5287 /* Give bit-fields their proper types. */
5289 tree *fieldlistp = &fieldlist;
5290 while (*fieldlistp)
5291 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5292 && TREE_TYPE (*fieldlistp) != error_mark_node)
5294 unsigned HOST_WIDE_INT width
5295 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5296 tree type = TREE_TYPE (*fieldlistp);
5297 if (width != TYPE_PRECISION (type))
5298 TREE_TYPE (*fieldlistp)
5299 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5300 DECL_INITIAL (*fieldlistp) = 0;
5302 else
5303 fieldlistp = &TREE_CHAIN (*fieldlistp);
5306 /* Now we have the truly final field list.
5307 Store it in this type and in the variants. */
5309 TYPE_FIELDS (t) = fieldlist;
5311 /* If there are lots of fields, sort so we can look through them fast.
5312 We arbitrarily consider 16 or more elts to be "a lot". */
5315 int len = 0;
5317 for (x = fieldlist; x; x = TREE_CHAIN (x))
5319 if (len > 15 || DECL_NAME (x) == NULL)
5320 break;
5321 len += 1;
5324 if (len > 15)
5326 tree *field_array;
5327 struct lang_type *space;
5328 struct sorted_fields_type *space2;
5330 len += list_length (x);
5332 /* Use the same allocation policy here that make_node uses, to
5333 ensure that this lives as long as the rest of the struct decl.
5334 All decls in an inline function need to be saved. */
5336 space = GGC_CNEW (struct lang_type);
5337 space2 = GGC_NEWVAR (struct sorted_fields_type,
5338 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5340 len = 0;
5341 space->s = space2;
5342 field_array = &space2->elts[0];
5343 for (x = fieldlist; x; x = TREE_CHAIN (x))
5345 field_array[len++] = x;
5347 /* If there is anonymous struct or union, break out of the loop. */
5348 if (DECL_NAME (x) == NULL)
5349 break;
5351 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5352 if (x == NULL)
5354 TYPE_LANG_SPECIFIC (t) = space;
5355 TYPE_LANG_SPECIFIC (t)->s->len = len;
5356 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5357 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5362 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5364 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5365 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5366 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5367 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5370 /* If this was supposed to be a transparent union, but we can't
5371 make it one, warn and turn off the flag. */
5372 if (TREE_CODE (t) == UNION_TYPE
5373 && TYPE_TRANSPARENT_UNION (t)
5374 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5376 TYPE_TRANSPARENT_UNION (t) = 0;
5377 warning ("union cannot be made transparent");
5380 /* If this structure or union completes the type of any previous
5381 variable declaration, lay it out and output its rtl. */
5382 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5384 x = TREE_CHAIN (x))
5386 tree decl = TREE_VALUE (x);
5387 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5388 layout_array_type (TREE_TYPE (decl));
5389 if (TREE_CODE (decl) != TYPE_DECL)
5391 layout_decl (decl, 0);
5392 if (c_dialect_objc ())
5393 objc_check_decl (decl);
5394 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5395 if (! toplevel)
5396 expand_decl (decl);
5399 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5401 /* Finish debugging output for this type. */
5402 rest_of_type_compilation (t, toplevel);
5404 return t;
5407 /* Lay out the type T, and its element type, and so on. */
5409 static void
5410 layout_array_type (tree t)
5412 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5413 layout_array_type (TREE_TYPE (t));
5414 layout_type (t);
5417 /* Begin compiling the definition of an enumeration type.
5418 NAME is its name (or null if anonymous).
5419 Returns the type object, as yet incomplete.
5420 Also records info about it so that build_enumerator
5421 may be used to declare the individual values as they are read. */
5423 tree
5424 start_enum (tree name)
5426 tree enumtype = 0;
5428 /* If this is the real definition for a previous forward reference,
5429 fill in the contents in the same object that used to be the
5430 forward reference. */
5432 if (name != 0)
5433 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5435 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5437 enumtype = make_node (ENUMERAL_TYPE);
5438 pushtag (name, enumtype);
5441 C_TYPE_BEING_DEFINED (enumtype) = 1;
5443 if (TYPE_VALUES (enumtype) != 0)
5445 /* This enum is a named one that has been declared already. */
5446 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5448 /* Completely replace its old definition.
5449 The old enumerators remain defined, however. */
5450 TYPE_VALUES (enumtype) = 0;
5453 enum_next_value = integer_zero_node;
5454 enum_overflow = 0;
5456 if (flag_short_enums)
5457 TYPE_PACKED (enumtype) = 1;
5459 return enumtype;
5462 /* After processing and defining all the values of an enumeration type,
5463 install their decls in the enumeration type and finish it off.
5464 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5465 and ATTRIBUTES are the specified attributes.
5466 Returns ENUMTYPE. */
5468 tree
5469 finish_enum (tree enumtype, tree values, tree attributes)
5471 tree pair, tem;
5472 tree minnode = 0, maxnode = 0;
5473 int precision, unsign;
5474 bool toplevel = (file_scope == current_scope);
5475 struct lang_type *lt;
5477 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5479 /* Calculate the maximum value of any enumerator in this type. */
5481 if (values == error_mark_node)
5482 minnode = maxnode = integer_zero_node;
5483 else
5485 minnode = maxnode = TREE_VALUE (values);
5486 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5488 tree value = TREE_VALUE (pair);
5489 if (tree_int_cst_lt (maxnode, value))
5490 maxnode = value;
5491 if (tree_int_cst_lt (value, minnode))
5492 minnode = value;
5496 /* Construct the final type of this enumeration. It is the same
5497 as one of the integral types - the narrowest one that fits, except
5498 that normally we only go as narrow as int - and signed iff any of
5499 the values are negative. */
5500 unsign = (tree_int_cst_sgn (minnode) >= 0);
5501 precision = MAX (min_precision (minnode, unsign),
5502 min_precision (maxnode, unsign));
5503 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5505 tem = c_common_type_for_size (precision, unsign);
5506 if (tem == NULL)
5508 warning ("enumeration values exceed range of largest integer");
5509 tem = long_long_integer_type_node;
5512 else
5513 tem = unsign ? unsigned_type_node : integer_type_node;
5515 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5516 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5517 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5518 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5519 TYPE_SIZE (enumtype) = 0;
5520 layout_type (enumtype);
5522 if (values != error_mark_node)
5524 /* Change the type of the enumerators to be the enum type. We
5525 need to do this irrespective of the size of the enum, for
5526 proper type checking. Replace the DECL_INITIALs of the
5527 enumerators, and the value slots of the list, with copies
5528 that have the enum type; they cannot be modified in place
5529 because they may be shared (e.g. integer_zero_node) Finally,
5530 change the purpose slots to point to the names of the decls. */
5531 for (pair = values; pair; pair = TREE_CHAIN (pair))
5533 tree enu = TREE_PURPOSE (pair);
5534 tree ini = DECL_INITIAL (enu);
5536 TREE_TYPE (enu) = enumtype;
5538 /* The ISO C Standard mandates enumerators to have type int,
5539 even though the underlying type of an enum type is
5540 unspecified. Here we convert any enumerators that fit in
5541 an int to type int, to avoid promotions to unsigned types
5542 when comparing integers with enumerators that fit in the
5543 int range. When -pedantic is given, build_enumerator()
5544 would have already taken care of those that don't fit. */
5545 if (int_fits_type_p (ini, integer_type_node))
5546 tem = integer_type_node;
5547 else
5548 tem = enumtype;
5549 ini = convert (tem, ini);
5551 DECL_INITIAL (enu) = ini;
5552 TREE_PURPOSE (pair) = DECL_NAME (enu);
5553 TREE_VALUE (pair) = ini;
5556 TYPE_VALUES (enumtype) = values;
5559 /* Record the min/max values so that we can warn about bit-field
5560 enumerations that are too small for the values. */
5561 lt = GGC_CNEW (struct lang_type);
5562 lt->enum_min = minnode;
5563 lt->enum_max = maxnode;
5564 TYPE_LANG_SPECIFIC (enumtype) = lt;
5566 /* Fix up all variant types of this enum type. */
5567 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5569 if (tem == enumtype)
5570 continue;
5571 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5572 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5573 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5574 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5575 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5576 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5577 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5578 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5579 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5580 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5581 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5584 /* Finish debugging output for this type. */
5585 rest_of_type_compilation (enumtype, toplevel);
5587 return enumtype;
5590 /* Build and install a CONST_DECL for one value of the
5591 current enumeration type (one that was begun with start_enum).
5592 Return a tree-list containing the CONST_DECL and its value.
5593 Assignment of sequential values by default is handled here. */
5595 tree
5596 build_enumerator (tree name, tree value)
5598 tree decl, type;
5600 /* Validate and default VALUE. */
5602 /* Remove no-op casts from the value. */
5603 if (value)
5604 STRIP_TYPE_NOPS (value);
5606 if (value != 0)
5608 /* Don't issue more errors for error_mark_node (i.e. an
5609 undeclared identifier) - just ignore the value expression. */
5610 if (value == error_mark_node)
5611 value = 0;
5612 else if (TREE_CODE (value) != INTEGER_CST)
5614 error ("enumerator value for '%E' is not an integer constant", name);
5615 value = 0;
5617 else
5619 value = default_conversion (value);
5620 constant_expression_warning (value);
5624 /* Default based on previous value. */
5625 /* It should no longer be possible to have NON_LVALUE_EXPR
5626 in the default. */
5627 if (value == 0)
5629 value = enum_next_value;
5630 if (enum_overflow)
5631 error ("overflow in enumeration values");
5634 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5636 pedwarn ("ISO C restricts enumerator values to range of `int'");
5637 /* XXX This causes -pedantic to change the meaning of the program.
5638 Remove? -zw 2004-03-15 */
5639 value = convert (integer_type_node, value);
5642 /* Set basis for default for next value. */
5643 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5644 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5646 /* Now create a declaration for the enum value name. */
5648 type = TREE_TYPE (value);
5649 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5650 TYPE_PRECISION (integer_type_node)),
5651 (TYPE_PRECISION (type)
5652 >= TYPE_PRECISION (integer_type_node)
5653 && TYPE_UNSIGNED (type)));
5655 decl = build_decl (CONST_DECL, name, type);
5656 DECL_INITIAL (decl) = convert (type, value);
5657 pushdecl (decl);
5659 return tree_cons (decl, value, NULL_TREE);
5663 /* Create the FUNCTION_DECL for a function definition.
5664 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5665 the declaration; they describe the function's name and the type it returns,
5666 but twisted together in a fashion that parallels the syntax of C.
5668 This function creates a binding context for the function body
5669 as well as setting up the FUNCTION_DECL in current_function_decl.
5671 Returns 1 on success. If the DECLARATOR is not suitable for a function
5672 (it defines a datum instead), we return 0, which tells
5673 yyparse to report a parse error. */
5676 start_function (tree declspecs, tree declarator, tree attributes)
5678 tree decl1, old_decl;
5679 tree restype, resdecl;
5681 current_function_returns_value = 0; /* Assume, until we see it does. */
5682 current_function_returns_null = 0;
5683 current_function_returns_abnormally = 0;
5684 warn_about_return_type = 0;
5685 current_extern_inline = 0;
5686 c_switch_stack = NULL;
5688 /* Indicate no valid break/continue context by setting these variables
5689 to some non-null, non-label value. We'll notice and emit the proper
5690 error message in c_finish_bc_stmt. */
5691 c_break_label = c_cont_label = size_zero_node;
5693 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5695 /* If the declarator is not suitable for a function definition,
5696 cause a syntax error. */
5697 if (decl1 == 0)
5698 return 0;
5700 decl_attributes (&decl1, attributes, 0);
5702 if (DECL_DECLARED_INLINE_P (decl1)
5703 && DECL_UNINLINABLE (decl1)
5704 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5705 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5707 announce_function (decl1);
5709 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5711 error ("return type is an incomplete type");
5712 /* Make it return void instead. */
5713 TREE_TYPE (decl1)
5714 = build_function_type (void_type_node,
5715 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5718 if (warn_about_return_type)
5719 pedwarn_c99 ("return type defaults to `int'");
5721 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5722 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5723 DECL_INITIAL (decl1) = error_mark_node;
5725 /* If this definition isn't a prototype and we had a prototype declaration
5726 before, copy the arg type info from that prototype.
5727 But not if what we had before was a builtin function. */
5728 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5729 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5730 && !DECL_BUILT_IN (old_decl)
5731 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5732 TREE_TYPE (TREE_TYPE (old_decl)))
5733 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5735 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5736 TREE_TYPE (decl1));
5737 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5740 /* Optionally warn of old-fashioned def with no previous prototype. */
5741 if (warn_strict_prototypes
5742 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5743 && C_DECL_ISNT_PROTOTYPE (old_decl))
5744 warning ("function declaration isn't a prototype");
5745 /* Optionally warn of any global def with no previous prototype. */
5746 else if (warn_missing_prototypes
5747 && TREE_PUBLIC (decl1)
5748 && ! MAIN_NAME_P (DECL_NAME (decl1))
5749 && C_DECL_ISNT_PROTOTYPE (old_decl))
5750 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5751 /* Optionally warn of any def with no previous prototype
5752 if the function has already been used. */
5753 else if (warn_missing_prototypes
5754 && old_decl != 0 && TREE_USED (old_decl)
5755 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5756 warning ("%J'%D' was used with no prototype before its definition",
5757 decl1, decl1);
5758 /* Optionally warn of any global def with no previous declaration. */
5759 else if (warn_missing_declarations
5760 && TREE_PUBLIC (decl1)
5761 && old_decl == 0
5762 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5763 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5764 /* Optionally warn of any def with no previous declaration
5765 if the function has already been used. */
5766 else if (warn_missing_declarations
5767 && old_decl != 0 && TREE_USED (old_decl)
5768 && C_DECL_IMPLICIT (old_decl))
5769 warning ("%J`%D' was used with no declaration before its definition",
5770 decl1, decl1);
5772 /* This is a definition, not a reference.
5773 So normally clear DECL_EXTERNAL.
5774 However, `extern inline' acts like a declaration
5775 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5776 DECL_EXTERNAL (decl1) = current_extern_inline;
5778 /* This function exists in static storage.
5779 (This does not mean `static' in the C sense!) */
5780 TREE_STATIC (decl1) = 1;
5782 /* A nested function is not global. */
5783 if (current_function_decl != 0)
5784 TREE_PUBLIC (decl1) = 0;
5786 #ifdef ENABLE_CHECKING
5787 /* This is the earliest point at which we might know the assembler
5788 name of the function. Thus, if it's set before this, die horribly. */
5789 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5790 abort ();
5791 #endif
5793 /* If #pragma weak was used, mark the decl weak now. */
5794 if (current_scope == file_scope)
5795 maybe_apply_pragma_weak (decl1);
5797 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5798 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5800 tree args;
5801 int argct = 0;
5803 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5804 != integer_type_node)
5805 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5807 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5808 args = TREE_CHAIN (args))
5810 tree type = args ? TREE_VALUE (args) : 0;
5812 if (type == void_type_node)
5813 break;
5815 ++argct;
5816 switch (argct)
5818 case 1:
5819 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5820 pedwarn ("%Jfirst argument of '%D' should be `int'",
5821 decl1, decl1);
5822 break;
5824 case 2:
5825 if (TREE_CODE (type) != POINTER_TYPE
5826 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5827 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5828 != char_type_node))
5829 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5830 decl1, decl1);
5831 break;
5833 case 3:
5834 if (TREE_CODE (type) != POINTER_TYPE
5835 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5836 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5837 != char_type_node))
5838 pedwarn ("%Jthird argument of '%D' should probably be "
5839 "'char **'", decl1, decl1);
5840 break;
5844 /* It is intentional that this message does not mention the third
5845 argument because it's only mentioned in an appendix of the
5846 standard. */
5847 if (argct > 0 && (argct < 2 || argct > 3))
5848 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5850 if (! TREE_PUBLIC (decl1))
5851 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5854 /* Record the decl so that the function name is defined.
5855 If we already have a decl for this name, and it is a FUNCTION_DECL,
5856 use the old decl. */
5858 current_function_decl = pushdecl (decl1);
5860 push_scope ();
5861 declare_parm_level ();
5863 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5864 /* Promote the value to int before returning it. */
5865 if (c_promoting_integer_type_p (restype))
5867 /* It retains unsignedness if not really getting wider. */
5868 if (TYPE_UNSIGNED (restype)
5869 && (TYPE_PRECISION (restype)
5870 == TYPE_PRECISION (integer_type_node)))
5871 restype = unsigned_type_node;
5872 else
5873 restype = integer_type_node;
5876 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5877 DECL_ARTIFICIAL (resdecl) = 1;
5878 DECL_IGNORED_P (resdecl) = 1;
5879 DECL_RESULT (current_function_decl) = resdecl;
5881 start_fname_decls ();
5883 return 1;
5886 /* Subroutine of store_parm_decls which handles new-style function
5887 definitions (prototype format). The parms already have decls, so we
5888 need only record them as in effect and complain if any redundant
5889 old-style parm decls were written. */
5890 static void
5891 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5893 tree decl;
5894 tree parms = ARG_INFO_PARMS (arg_info);
5895 tree tags = ARG_INFO_TAGS (arg_info);
5896 tree others = ARG_INFO_OTHERS (arg_info);
5898 if (current_scope->bindings)
5900 error ("%Jold-style parameter declarations in prototyped "
5901 "function definition", fndecl);
5903 /* Get rid of the old-style declarations. */
5904 pop_scope ();
5905 push_scope ();
5907 /* Don't issue this warning for nested functions, and don't issue this
5908 warning if we got here because ARG_INFO_TYPES was error_mark_node
5909 (this happens when a function definition has just an ellipsis in
5910 its parameter list). */
5911 else if (warn_traditional && !in_system_header && !current_function_scope
5912 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5913 warning ("%Jtraditional C rejects ISO C style function definitions",
5914 fndecl);
5916 /* Now make all the parameter declarations visible in the function body.
5917 We can bypass most of the grunt work of pushdecl. */
5918 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5920 DECL_CONTEXT (decl) = current_function_decl;
5921 if (DECL_NAME (decl))
5922 bind (DECL_NAME (decl), decl, current_scope,
5923 /*invisible=*/false, /*nested=*/false);
5924 else
5925 error ("%Jparameter name omitted", decl);
5928 /* Record the parameter list in the function declaration. */
5929 DECL_ARGUMENTS (fndecl) = parms;
5931 /* Now make all the ancillary declarations visible, likewise. */
5932 for (decl = others; decl; decl = TREE_CHAIN (decl))
5934 DECL_CONTEXT (decl) = current_function_decl;
5935 if (DECL_NAME (decl))
5936 bind (DECL_NAME (decl), decl, current_scope,
5937 /*invisible=*/false, /*nested=*/false);
5940 /* And all the tag declarations. */
5941 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5942 if (TREE_PURPOSE (decl))
5943 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5944 /*invisible=*/false, /*nested=*/false);
5947 /* Subroutine of store_parm_decls which handles old-style function
5948 definitions (separate parameter list and declarations). */
5950 static void
5951 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5953 struct c_binding *b;
5954 tree parm, decl, last;
5955 tree parmids = ARG_INFO_PARMS (arg_info);
5957 /* We use DECL_WEAK as a flag to show which parameters have been
5958 seen already, since it is not used on PARM_DECL. */
5959 #ifdef ENABLE_CHECKING
5960 for (b = current_scope->bindings; b; b = b->prev)
5961 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5962 abort ();
5963 #endif
5965 if (warn_old_style_definition && !in_system_header)
5966 warning ("%Jold-style function definition", fndecl);
5968 /* Match each formal parameter name with its declaration. Save each
5969 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5970 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5972 if (TREE_VALUE (parm) == 0)
5974 error ("%Jparameter name missing from parameter list", fndecl);
5975 TREE_PURPOSE (parm) = 0;
5976 continue;
5979 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5980 if (b && B_IN_CURRENT_SCOPE (b))
5982 decl = b->decl;
5983 /* If we got something other than a PARM_DECL it is an error. */
5984 if (TREE_CODE (decl) != PARM_DECL)
5985 error ("%J'%D' declared as a non-parameter", decl, decl);
5986 /* If the declaration is already marked, we have a duplicate
5987 name. Complain and ignore the duplicate. */
5988 else if (DECL_WEAK (decl))
5990 error ("%Jmultiple parameters named '%D'", decl, decl);
5991 TREE_PURPOSE (parm) = 0;
5992 continue;
5994 /* If the declaration says "void", complain and turn it into
5995 an int. */
5996 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5998 error ("%Jparameter '%D' declared with void type", decl, decl);
5999 TREE_TYPE (decl) = integer_type_node;
6000 DECL_ARG_TYPE (decl) = integer_type_node;
6001 layout_decl (decl, 0);
6004 /* If no declaration found, default to int. */
6005 else
6007 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6008 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6009 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6010 pushdecl (decl);
6012 if (flag_isoc99)
6013 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
6014 else if (extra_warnings)
6015 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
6018 TREE_PURPOSE (parm) = decl;
6019 DECL_WEAK (decl) = 1;
6022 /* Now examine the parms chain for incomplete declarations
6023 and declarations with no corresponding names. */
6025 for (b = current_scope->bindings; b; b = b->prev)
6027 parm = b->decl;
6028 if (TREE_CODE (parm) != PARM_DECL)
6029 continue;
6031 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6033 error ("%Jparameter '%D' has incomplete type", parm, parm);
6034 TREE_TYPE (parm) = error_mark_node;
6037 if (! DECL_WEAK (parm))
6039 error ("%Jdeclaration for parameter '%D' but no such parameter",
6040 parm, parm);
6042 /* Pretend the parameter was not missing.
6043 This gets us to a standard state and minimizes
6044 further error messages. */
6045 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6049 /* Chain the declarations together in the order of the list of
6050 names. Store that chain in the function decl, replacing the
6051 list of names. Update the current scope to match. */
6052 DECL_ARGUMENTS (fndecl) = 0;
6054 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6055 if (TREE_PURPOSE (parm))
6056 break;
6057 if (parm && TREE_PURPOSE (parm))
6059 last = TREE_PURPOSE (parm);
6060 DECL_ARGUMENTS (fndecl) = last;
6061 DECL_WEAK (last) = 0;
6063 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6064 if (TREE_PURPOSE (parm))
6066 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6067 last = TREE_PURPOSE (parm);
6068 DECL_WEAK (last) = 0;
6070 TREE_CHAIN (last) = 0;
6073 /* If there was a previous prototype,
6074 set the DECL_ARG_TYPE of each argument according to
6075 the type previously specified, and report any mismatches. */
6077 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6079 tree type;
6080 for (parm = DECL_ARGUMENTS (fndecl),
6081 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6082 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6083 != void_type_node));
6084 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6086 if (parm == 0 || type == 0
6087 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6089 error ("number of arguments doesn't match prototype");
6090 error ("%Hprototype declaration",
6091 &current_function_prototype_locus);
6092 break;
6094 /* Type for passing arg must be consistent with that
6095 declared for the arg. ISO C says we take the unqualified
6096 type for parameters declared with qualified type. */
6097 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6098 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6100 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6101 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6103 /* Adjust argument to match prototype. E.g. a previous
6104 `int foo(float);' prototype causes
6105 `int foo(x) float x; {...}' to be treated like
6106 `int foo(float x) {...}'. This is particularly
6107 useful for argument types like uid_t. */
6108 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6110 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6111 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6112 && TYPE_PRECISION (TREE_TYPE (parm))
6113 < TYPE_PRECISION (integer_type_node))
6114 DECL_ARG_TYPE (parm) = integer_type_node;
6116 if (pedantic)
6118 pedwarn ("promoted argument '%D' "
6119 "doesn't match prototype", parm);
6120 pedwarn ("%Hprototype declaration",
6121 &current_function_prototype_locus);
6124 else
6126 error ("argument '%D' doesn't match prototype", parm);
6127 error ("%Hprototype declaration",
6128 &current_function_prototype_locus);
6132 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6135 /* Otherwise, create a prototype that would match. */
6137 else
6139 tree actual = 0, last = 0, type;
6141 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6143 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6144 if (last)
6145 TREE_CHAIN (last) = type;
6146 else
6147 actual = type;
6148 last = type;
6150 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6151 if (last)
6152 TREE_CHAIN (last) = type;
6153 else
6154 actual = type;
6156 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6157 of the type of this function, but we need to avoid having this
6158 affect the types of other similarly-typed functions, so we must
6159 first force the generation of an identical (but separate) type
6160 node for the relevant function type. The new node we create
6161 will be a variant of the main variant of the original function
6162 type. */
6164 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6166 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6170 /* Store the parameter declarations into the current function declaration.
6171 This is called after parsing the parameter declarations, before
6172 digesting the body of the function.
6174 For an old-style definition, construct a prototype out of the old-style
6175 parameter declarations and inject it into the function's type. */
6177 void
6178 store_parm_decls (void)
6180 tree fndecl = current_function_decl;
6182 /* The argument information block for FNDECL. */
6183 tree arg_info = DECL_ARGUMENTS (fndecl);
6185 /* True if this definition is written with a prototype. Note:
6186 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6187 list in a function definition as equivalent to (void) -- an
6188 empty argument list specifies the function has no parameters,
6189 but only (void) sets up a prototype for future calls. */
6190 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6192 if (proto)
6193 store_parm_decls_newstyle (fndecl, arg_info);
6194 else
6195 store_parm_decls_oldstyle (fndecl, arg_info);
6197 /* The next call to push_scope will be a function body. */
6199 next_is_function_body = true;
6201 /* Write a record describing this function definition to the prototypes
6202 file (if requested). */
6204 gen_aux_info_record (fndecl, 1, 0, proto);
6206 /* Initialize the RTL code for the function. */
6207 allocate_struct_function (fndecl);
6209 /* Begin the statement tree for this function. */
6210 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6212 /* ??? Insert the contents of the pending sizes list into the function
6213 to be evaluated. This just changes mis-behaviour until assign_parms
6214 phase ordering problems are resolved. */
6216 tree t;
6217 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6218 add_stmt (TREE_VALUE (t));
6221 /* Even though we're inside a function body, we still don't want to
6222 call expand_expr to calculate the size of a variable-sized array.
6223 We haven't necessarily assigned RTL to all variables yet, so it's
6224 not safe to try to expand expressions involving them. */
6225 cfun->x_dont_save_pending_sizes_p = 1;
6228 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6230 static void
6231 c_finalize (tree fndecl)
6233 struct cgraph_node *cgn;
6235 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6236 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6238 /* ??? Objc emits functions after finalizing the compilation unit.
6239 This should be cleaned up later and this conditional removed. */
6240 if (cgraph_global_info_ready)
6242 c_expand_body (fndecl);
6243 return;
6246 /* Finalize all nested functions now. */
6247 cgn = cgraph_node (fndecl);
6248 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6249 c_finalize (cgn->decl);
6251 cgraph_finalize_function (fndecl, false);
6254 /* Finish up a function declaration and compile that function
6255 all the way to assembler language output. The free the storage
6256 for the function definition.
6258 This is called after parsing the body of the function definition. */
6260 void
6261 finish_function (void)
6263 tree fndecl = current_function_decl;
6265 if (TREE_CODE (fndecl) == FUNCTION_DECL
6266 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6268 tree args = DECL_ARGUMENTS (fndecl);
6269 for (; args; args = TREE_CHAIN (args))
6271 tree type = TREE_TYPE (args);
6272 if (INTEGRAL_TYPE_P (type)
6273 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6274 DECL_ARG_TYPE (args) = integer_type_node;
6278 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6279 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6281 /* Must mark the RESULT_DECL as being in this function. */
6283 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6284 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6286 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6288 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6289 != integer_type_node)
6291 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6292 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6293 if (!warn_main)
6294 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6296 else
6298 if (flag_isoc99)
6299 c_finish_return (integer_zero_node);
6303 /* Tie off the statement tree for this function. */
6304 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6306 finish_fname_decls ();
6308 /* Complain if there's just no return statement. */
6309 if (warn_return_type
6310 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6311 && !current_function_returns_value && !current_function_returns_null
6312 /* Don't complain if we abort. */
6313 && !current_function_returns_abnormally
6314 /* Don't warn for main(). */
6315 && !MAIN_NAME_P (DECL_NAME (fndecl))
6316 /* Or if they didn't actually specify a return type. */
6317 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6318 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6319 inline function, as we might never be compiled separately. */
6320 && DECL_INLINE (fndecl))
6321 warning ("no return statement in function returning non-void");
6323 /* With just -Wextra, complain only if function returns both with
6324 and without a value. */
6325 if (extra_warnings
6326 && current_function_returns_value
6327 && current_function_returns_null)
6328 warning ("this function may return with or without a value");
6330 /* Store the end of the function, so that we get good line number
6331 info for the epilogue. */
6332 cfun->function_end_locus = input_location;
6334 /* If we don't have ctors/dtors sections, and this is a static
6335 constructor or destructor, it must be recorded now. */
6336 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6337 && !targetm.have_ctors_dtors)
6338 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6339 if (DECL_STATIC_DESTRUCTOR (fndecl)
6340 && !targetm.have_ctors_dtors)
6341 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6343 /* Genericize before inlining. Delay genericizing nested functions
6344 until their parent function is genericized. Since finalizing
6345 requires GENERIC, delay that as well. */
6347 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6349 if (!decl_function_context (fndecl))
6351 c_genericize (fndecl);
6352 lower_nested_functions (fndecl);
6353 c_finalize (fndecl);
6355 else
6357 /* Register this function with cgraph just far enough to get it
6358 added to our parent's nested function list. Handy, since the
6359 C front end doesn't have such a list. */
6360 (void) cgraph_node (fndecl);
6364 /* We're leaving the context of this function, so zap cfun.
6365 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6366 tree_rest_of_compilation. */
6367 cfun = NULL;
6368 current_function_decl = NULL;
6371 /* Generate the RTL for the body of FNDECL. */
6373 void
6374 c_expand_body (tree fndecl)
6377 if (!DECL_INITIAL (fndecl)
6378 || DECL_INITIAL (fndecl) == error_mark_node)
6379 return;
6381 tree_rest_of_compilation (fndecl, false);
6383 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6384 && targetm.have_ctors_dtors)
6385 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6386 DEFAULT_INIT_PRIORITY);
6387 if (DECL_STATIC_DESTRUCTOR (fndecl)
6388 && targetm.have_ctors_dtors)
6389 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6390 DEFAULT_INIT_PRIORITY);
6393 /* Check the declarations given in a for-loop for satisfying the C99
6394 constraints. */
6395 void
6396 check_for_loop_decls (void)
6398 struct c_binding *b;
6400 if (!flag_isoc99)
6402 /* If we get here, declarations have been used in a for loop without
6403 the C99 for loop scope. This doesn't make much sense, so don't
6404 allow it. */
6405 error ("'for' loop initial declaration used outside C99 mode");
6406 return;
6408 /* C99 subclause 6.8.5 paragraph 3:
6410 [#3] The declaration part of a for statement shall only
6411 declare identifiers for objects having storage class auto or
6412 register.
6414 It isn't clear whether, in this sentence, "identifiers" binds to
6415 "shall only declare" or to "objects" - that is, whether all identifiers
6416 declared must be identifiers for objects, or whether the restriction
6417 only applies to those that are. (A question on this in comp.std.c
6418 in November 2000 received no answer.) We implement the strictest
6419 interpretation, to avoid creating an extension which later causes
6420 problems. */
6422 for (b = current_scope->bindings; b; b = b->prev)
6424 tree id = b->id;
6425 tree decl = b->decl;
6427 if (!id)
6428 continue;
6430 switch (TREE_CODE (decl))
6432 case VAR_DECL:
6433 if (TREE_STATIC (decl))
6434 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6435 "initial declaration", decl, decl);
6436 else if (DECL_EXTERNAL (decl))
6437 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6438 "initial declaration", decl, decl);
6439 break;
6441 case RECORD_TYPE:
6442 error ("'struct %E' declared in 'for' loop initial declaration", id);
6443 break;
6444 case UNION_TYPE:
6445 error ("'union %E' declared in 'for' loop initial declaration", id);
6446 break;
6447 case ENUMERAL_TYPE:
6448 error ("'enum %E' declared in 'for' loop initial declaration", id);
6449 break;
6450 default:
6451 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6452 "initial declaration", decl, decl);
6457 /* Save and reinitialize the variables
6458 used during compilation of a C function. */
6460 void
6461 c_push_function_context (struct function *f)
6463 struct language_function *p;
6464 p = GGC_NEW (struct language_function);
6465 f->language = p;
6467 p->base.x_stmt_tree = c_stmt_tree;
6468 p->x_break_label = c_break_label;
6469 p->x_cont_label = c_cont_label;
6470 p->x_switch_stack = c_switch_stack;
6471 p->returns_value = current_function_returns_value;
6472 p->returns_null = current_function_returns_null;
6473 p->returns_abnormally = current_function_returns_abnormally;
6474 p->warn_about_return_type = warn_about_return_type;
6475 p->extern_inline = current_extern_inline;
6478 /* Restore the variables used during compilation of a C function. */
6480 void
6481 c_pop_function_context (struct function *f)
6483 struct language_function *p = f->language;
6485 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6486 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6488 /* Stop pointing to the local nodes about to be freed. */
6489 /* But DECL_INITIAL must remain nonzero so we know this
6490 was an actual function definition. */
6491 DECL_INITIAL (current_function_decl) = error_mark_node;
6492 DECL_ARGUMENTS (current_function_decl) = 0;
6495 c_stmt_tree = p->base.x_stmt_tree;
6496 c_break_label = p->x_break_label;
6497 c_cont_label = p->x_cont_label;
6498 c_switch_stack = p->x_switch_stack;
6499 current_function_returns_value = p->returns_value;
6500 current_function_returns_null = p->returns_null;
6501 current_function_returns_abnormally = p->returns_abnormally;
6502 warn_about_return_type = p->warn_about_return_type;
6503 current_extern_inline = p->extern_inline;
6505 f->language = NULL;
6508 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6510 void
6511 c_dup_lang_specific_decl (tree decl)
6513 struct lang_decl *ld;
6515 if (!DECL_LANG_SPECIFIC (decl))
6516 return;
6518 ld = GGC_NEW (struct lang_decl);
6519 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6520 DECL_LANG_SPECIFIC (decl) = ld;
6523 /* The functions below are required for functionality of doing
6524 function at once processing in the C front end. Currently these
6525 functions are not called from anywhere in the C front end, but as
6526 these changes continue, that will change. */
6528 /* Returns nonzero if the current statement is a full expression,
6529 i.e. temporaries created during that statement should be destroyed
6530 at the end of the statement. */
6533 stmts_are_full_exprs_p (void)
6535 return 0;
6538 /* Returns the stmt_tree (if any) to which statements are currently
6539 being added. If there is no active statement-tree, NULL is
6540 returned. */
6542 stmt_tree
6543 current_stmt_tree (void)
6545 return &c_stmt_tree;
6548 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6549 C. */
6552 anon_aggr_type_p (tree ARG_UNUSED (node))
6554 return 0;
6557 /* Dummy function in place of callback used by C++. */
6559 void
6560 extract_interface_info (void)
6564 /* Return the global value of T as a symbol. */
6566 tree
6567 identifier_global_value (tree t)
6569 struct c_binding *b;
6571 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6572 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6573 return b->decl;
6575 return 0;
6578 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6579 otherwise the name is found in ridpointers from RID_INDEX. */
6581 void
6582 record_builtin_type (enum rid rid_index, const char *name, tree type)
6584 tree id;
6585 if (name == 0)
6586 id = ridpointers[(int) rid_index];
6587 else
6588 id = get_identifier (name);
6589 pushdecl (build_decl (TYPE_DECL, id, type));
6592 /* Build the void_list_node (void_type_node having been created). */
6593 tree
6594 build_void_list_node (void)
6596 tree t = build_tree_list (NULL_TREE, void_type_node);
6597 return t;
6600 /* Return something to represent absolute declarators containing a *.
6601 TARGET is the absolute declarator that the * contains.
6602 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6603 to apply to the pointer type, represented as identifiers, possible mixed
6604 with attributes.
6606 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6607 if attributes are present) and whose type is the modifier list. */
6609 tree
6610 make_pointer_declarator (tree type_quals_attrs, tree target)
6612 tree quals, attrs;
6613 tree itarget = target;
6614 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6615 if (attrs != NULL_TREE)
6616 itarget = tree_cons (attrs, target, NULL_TREE);
6617 return build1 (INDIRECT_REF, quals, itarget);
6620 /* Synthesize a function which calls all the global ctors or global
6621 dtors in this file. This is only used for targets which do not
6622 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6623 static void
6624 build_cdtor (int method_type, tree cdtors)
6626 tree body = 0;
6628 if (!cdtors)
6629 return;
6631 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6632 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6633 &body);
6635 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
6638 /* Perform final processing on one file scope's declarations (or the
6639 external scope's declarations), GLOBALS. */
6640 static void
6641 c_write_global_declarations_1 (tree globals)
6643 size_t len = list_length (globals);
6644 tree *vec = XNEWVEC (tree, len);
6645 size_t i;
6646 tree decl;
6648 /* Process the decls in the order they were written. */
6649 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6650 vec[i] = decl;
6652 wrapup_global_declarations (vec, len);
6653 check_global_declarations (vec, len);
6655 free (vec);
6658 void
6659 c_write_global_declarations (void)
6661 tree ext_block, t;
6663 /* We don't want to do this if generating a PCH. */
6664 if (pch_file)
6665 return;
6667 /* Don't waste time on further processing if -fsyntax-only or we've
6668 encountered errors. */
6669 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
6670 return;
6672 /* Close the external scope. */
6673 ext_block = pop_scope ();
6674 external_scope = 0;
6675 if (current_scope)
6676 abort ();
6678 /* Process all file scopes in this compilation, and the external_scope,
6679 through wrapup_global_declarations and check_global_declarations. */
6680 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6681 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6682 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6684 /* Generate functions to call static constructors and destructors
6685 for targets that do not support .ctors/.dtors sections. These
6686 functions have magic names which are detected by collect2. */
6687 build_cdtor ('I', static_ctors); static_ctors = 0;
6688 build_cdtor ('D', static_dtors); static_dtors = 0;
6690 /* We're done parsing; proceed to optimize and emit assembly.
6691 FIXME: shouldn't be the front end's responsibility to call this. */
6692 cgraph_optimize ();
6694 /* Presently this has to happen after cgraph_optimize.
6695 FIXME: shouldn't be the front end's responsibility to call this. */
6696 if (flag_mudflap)
6697 mudflap_finish_file ();
6700 #include "gt-c-decl.h"