* config.gcc: Add sh-*-symbianelf target.
[official-gcc.git] / gcc / c-decl.c
blob6ff2dff081f29b4eecb7bb56f995d104ea70c4e9
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_alloc (sizeof (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_alloc_cleared (sizeof (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. Keep this in sync with
750 stmt.c:warn_about_unused_variables, which we cannot use
751 since it expects a different data structure. */
752 if (warn_unused_variable
753 && !TREE_USED (p)
754 && !DECL_IN_SYSTEM_HEADER (p)
755 && DECL_NAME (p)
756 && !DECL_ARTIFICIAL (p)
757 && (scope != file_scope
758 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
759 && !TREE_THIS_VOLATILE (p)))
760 && scope != external_scope)
761 warning ("%Junused variable `%D'", p, p);
763 /* Fall through. */
764 case TYPE_DECL:
765 case CONST_DECL:
766 common_symbol:
767 /* All of these go in BLOCK_VARS, but only if this is the
768 binding in the home scope. */
769 if (!b->nested)
771 TREE_CHAIN (p) = BLOCK_VARS (block);
772 BLOCK_VARS (block) = p;
774 /* If this is the file scope, and we are processing more
775 than one translation unit in this compilation, set
776 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
777 This makes same_translation_unit_p work, and causes
778 static declarations to be given disambiguating suffixes. */
779 if (scope == file_scope && num_in_fnames > 1)
781 DECL_CONTEXT (p) = context;
782 if (TREE_CODE (p) == TYPE_DECL)
783 set_type_context (TREE_TYPE (p), context);
786 /* Fall through. */
787 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
788 already been put there by store_parm_decls. Unused-
789 parameter warnings are handled by function.c.
790 error_mark_node obviously does not go in BLOCK_VARS and
791 does not get unused-variable warnings. */
792 case PARM_DECL:
793 case ERROR_MARK:
794 /* It is possible for a decl not to have a name. We get
795 here with b->id NULL in this case. */
796 if (b->id)
798 #ifdef ENABLE_CHECKING
799 if (I_SYMBOL_BINDING (b->id) != b) abort ();
800 #endif
801 I_SYMBOL_BINDING (b->id) = b->shadowed;
803 break;
805 default:
806 abort ();
811 /* Dispose of the block that we just made inside some higher level. */
812 if ((scope->function_body || scope == file_scope) && context)
814 DECL_INITIAL (context) = block;
815 BLOCK_SUPERCONTEXT (block) = context;
817 else if (scope->outer)
819 if (block)
820 SCOPE_LIST_APPEND (scope->outer, blocks, block);
821 /* If we did not make a block for the scope just exited, any
822 blocks made for inner scopes must be carried forward so they
823 will later become subblocks of something else. */
824 else if (scope->blocks)
825 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
828 /* Pop the current scope, and free the structure for reuse. */
829 current_scope = scope->outer;
830 if (scope->function_body)
831 current_function_scope = scope->outer_function;
833 memset (scope, 0, sizeof (struct c_scope));
834 scope->outer = scope_freelist;
835 scope_freelist = scope;
837 return block;
840 void
841 push_file_scope (void)
843 tree decl;
845 if (file_scope)
846 return;
848 push_scope ();
849 file_scope = current_scope;
851 start_fname_decls ();
853 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
854 bind (DECL_NAME (decl), decl, file_scope,
855 /*invisible=*/false, /*nested=*/true);
858 void
859 pop_file_scope (void)
861 /* In case there were missing closebraces, get us back to the global
862 binding level. */
863 while (current_scope != file_scope)
864 pop_scope ();
866 /* __FUNCTION__ is defined at file scope (""). This
867 call may not be necessary as my tests indicate it
868 still works without it. */
869 finish_fname_decls ();
871 /* This is the point to write out a PCH if we're doing that.
872 In that case we do not want to do anything else. */
873 if (pch_file)
875 c_common_write_pch ();
876 return;
879 /* Pop off the file scope and close this translation unit. */
880 pop_scope ();
881 file_scope = 0;
882 cgraph_finalize_compilation_unit ();
885 /* Insert BLOCK at the end of the list of subblocks of the current
886 scope. This is used when a BIND_EXPR is expanded, to handle the
887 BLOCK node inside the BIND_EXPR. */
889 void
890 insert_block (tree block)
892 TREE_USED (block) = 1;
893 SCOPE_LIST_APPEND (current_scope, blocks, block);
896 /* Push a definition or a declaration of struct, union or enum tag "name".
897 "type" should be the type node.
898 We assume that the tag "name" is not already defined.
900 Note that the definition may really be just a forward reference.
901 In that case, the TYPE_SIZE will be zero. */
903 static void
904 pushtag (tree name, tree type)
906 /* Record the identifier as the type's name if it has none. */
907 if (name && !TYPE_NAME (type))
908 TYPE_NAME (type) = name;
909 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
911 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
912 tagged type we just added to the current scope. This fake
913 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
914 to output a representation of a tagged type, and it also gives
915 us a convenient place to record the "scope start" address for the
916 tagged type. */
918 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
920 /* An approximation for now, so we can tell this is a function-scope tag.
921 This will be updated in pop_scope. */
922 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
925 /* Subroutine of compare_decls. Allow harmless mismatches in return
926 and argument types provided that the type modes match. This function
927 return a unified type given a suitable match, and 0 otherwise. */
929 static tree
930 match_builtin_function_types (tree newtype, tree oldtype)
932 tree newrettype, oldrettype;
933 tree newargs, oldargs;
934 tree trytype, tryargs;
936 /* Accept the return type of the new declaration if same modes. */
937 oldrettype = TREE_TYPE (oldtype);
938 newrettype = TREE_TYPE (newtype);
940 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
941 return 0;
943 oldargs = TYPE_ARG_TYPES (oldtype);
944 newargs = TYPE_ARG_TYPES (newtype);
945 tryargs = newargs;
947 while (oldargs || newargs)
949 if (! oldargs
950 || ! newargs
951 || ! TREE_VALUE (oldargs)
952 || ! TREE_VALUE (newargs)
953 || TYPE_MODE (TREE_VALUE (oldargs))
954 != TYPE_MODE (TREE_VALUE (newargs)))
955 return 0;
957 oldargs = TREE_CHAIN (oldargs);
958 newargs = TREE_CHAIN (newargs);
961 trytype = build_function_type (newrettype, tryargs);
962 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
965 /* Subroutine of diagnose_mismatched_decls. Check for function type
966 mismatch involving an empty arglist vs a nonempty one and give clearer
967 diagnostics. */
968 static void
969 diagnose_arglist_conflict (tree newdecl, tree olddecl,
970 tree newtype, tree oldtype)
972 tree t;
974 if (TREE_CODE (olddecl) != FUNCTION_DECL
975 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
976 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
978 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
979 return;
981 t = TYPE_ARG_TYPES (oldtype);
982 if (t == 0)
983 t = TYPE_ARG_TYPES (newtype);
984 for (; t; t = TREE_CHAIN (t))
986 tree type = TREE_VALUE (t);
988 if (TREE_CHAIN (t) == 0
989 && TYPE_MAIN_VARIANT (type) != void_type_node)
991 inform ("a parameter list with an ellipsis can't match "
992 "an empty parameter name list declaration");
993 break;
996 if (c_type_promotes_to (type) != type)
998 inform ("an argument type that has a default promotion can't match "
999 "an empty parameter name list declaration");
1000 break;
1005 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1006 old-style function definition, NEWDECL is a prototype declaration.
1007 Diagnose inconsistencies in the argument list. Returns TRUE if
1008 the prototype is compatible, FALSE if not. */
1009 static bool
1010 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1012 tree newargs, oldargs;
1013 int i;
1015 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1016 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1018 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1019 newargs = TYPE_ARG_TYPES (newtype);
1020 i = 1;
1022 for (;;)
1024 tree oldargtype = TREE_VALUE (oldargs);
1025 tree newargtype = TREE_VALUE (newargs);
1027 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1028 break;
1030 /* Reaching the end of just one list means the two decls don't
1031 agree on the number of arguments. */
1032 if (END_OF_ARGLIST (oldargtype))
1034 error ("%Jprototype for '%D' declares more arguments "
1035 "than previous old-style definition", newdecl, newdecl);
1036 return false;
1038 else if (END_OF_ARGLIST (newargtype))
1040 error ("%Jprototype for '%D' declares fewer arguments "
1041 "than previous old-style definition", newdecl, newdecl);
1042 return false;
1045 /* Type for passing arg must be consistent with that declared
1046 for the arg. */
1047 else if (! comptypes (oldargtype, newargtype))
1049 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1050 newdecl, newdecl, i);
1051 return false;
1054 oldargs = TREE_CHAIN (oldargs);
1055 newargs = TREE_CHAIN (newargs);
1056 i++;
1059 /* If we get here, no errors were found, but do issue a warning
1060 for this poor-style construct. */
1061 warning ("%Jprototype for '%D' follows non-prototype definition",
1062 newdecl, newdecl);
1063 return true;
1064 #undef END_OF_ARGLIST
1067 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1068 first in a pair of mismatched declarations, using the diagnostic
1069 function DIAG. */
1070 static void
1071 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1073 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1075 else if (DECL_INITIAL (decl))
1076 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1077 else if (C_DECL_IMPLICIT (decl))
1078 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1079 else
1080 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1083 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1084 Returns true if the caller should proceed to merge the two, false
1085 if OLDDECL should simply be discarded. As a side effect, issues
1086 all necessary diagnostics for invalid or poor-style combinations.
1087 If it returns true, writes the types of NEWDECL and OLDDECL to
1088 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1089 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1091 static bool
1092 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1093 tree *newtypep, tree *oldtypep)
1095 tree newtype, oldtype;
1096 bool pedwarned = false;
1097 bool warned = false;
1099 /* If we have error_mark_node for either decl or type, just discard
1100 the previous decl - we're in an error cascade already. */
1101 if (olddecl == error_mark_node || newdecl == error_mark_node)
1102 return false;
1103 *oldtypep = oldtype = TREE_TYPE (olddecl);
1104 *newtypep = newtype = TREE_TYPE (newdecl);
1105 if (oldtype == error_mark_node || newtype == error_mark_node)
1106 return false;
1108 /* Two different categories of symbol altogether. This is an error
1109 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1110 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1112 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1113 && DECL_BUILT_IN (olddecl)
1114 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1116 error ("%J'%D' redeclared as different kind of symbol",
1117 newdecl, newdecl);
1118 locate_old_decl (olddecl, error);
1120 else if (TREE_PUBLIC (newdecl))
1121 warning ("%Jbuilt-in function '%D' declared as non-function",
1122 newdecl, newdecl);
1123 else if (warn_shadow)
1124 warning ("%Jdeclaration of '%D' shadows a built-in function",
1125 newdecl, newdecl);
1126 return false;
1129 if (!comptypes (oldtype, newtype))
1131 if (TREE_CODE (olddecl) == FUNCTION_DECL
1132 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1134 /* Accept harmless mismatch in function types.
1135 This is for the ffs and fprintf builtins. */
1136 tree trytype = match_builtin_function_types (newtype, oldtype);
1138 if (trytype && comptypes (newtype, trytype))
1139 *oldtypep = oldtype = trytype;
1140 else
1142 /* If types don't match for a built-in, throw away the
1143 built-in. No point in calling locate_old_decl here, it
1144 won't print anything. */
1145 warning ("%Jconflicting types for built-in function '%D'",
1146 newdecl, newdecl);
1147 return false;
1150 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1151 && DECL_IS_BUILTIN (olddecl))
1153 /* A conflicting function declaration for a predeclared
1154 function that isn't actually built in. Objective C uses
1155 these. The new declaration silently overrides everything
1156 but the volatility (i.e. noreturn) indication. See also
1157 below. FIXME: Make Objective C use normal builtins. */
1158 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1159 return false;
1161 /* Permit void foo (...) to match int foo (...) if the latter is
1162 the definition and implicit int was used. See
1163 c-torture/compile/920625-2.c. */
1164 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1165 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1166 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1167 && C_FUNCTION_IMPLICIT_INT (newdecl))
1169 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1170 /* Make sure we keep void as the return type. */
1171 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1172 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1173 pedwarned = true;
1175 else
1177 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1178 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1179 else
1180 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1181 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1182 locate_old_decl (olddecl, error);
1183 return false;
1187 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1188 but silently ignore the redeclaration if either is in a system
1189 header. (Conflicting redeclarations were handled above.) */
1190 if (TREE_CODE (newdecl) == TYPE_DECL)
1192 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1193 return true; /* Allow OLDDECL to continue in use. */
1195 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1196 locate_old_decl (olddecl, error);
1197 return false;
1200 /* Function declarations can either be 'static' or 'extern' (no
1201 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1202 can never conflict with each other on account of linkage (6.2.2p4).
1203 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1204 two definitions if one is 'extern inline' and one is not. The non-
1205 extern-inline definition supersedes the extern-inline definition. */
1206 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1208 /* If you declare a built-in function name as static, or
1209 define the built-in with an old-style definition (so we
1210 can't validate the argument list) the built-in definition is
1211 overridden, but optionally warn this was a bad choice of name. */
1212 if (DECL_BUILT_IN (olddecl)
1213 && !C_DECL_DECLARED_BUILTIN (olddecl)
1214 && (!TREE_PUBLIC (newdecl)
1215 || (DECL_INITIAL (newdecl)
1216 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1218 if (warn_shadow)
1219 warning ("%Jdeclaration of '%D' shadows a built-in function",
1220 newdecl, newdecl);
1221 /* Discard the old built-in function. */
1222 return false;
1225 if (DECL_INITIAL (newdecl))
1227 if (DECL_INITIAL (olddecl)
1228 && !(DECL_DECLARED_INLINE_P (olddecl)
1229 && DECL_EXTERNAL (olddecl)
1230 && !(DECL_DECLARED_INLINE_P (newdecl)
1231 && DECL_EXTERNAL (newdecl)
1232 && same_translation_unit_p (olddecl, newdecl))))
1234 error ("%Jredefinition of '%D'", newdecl, newdecl);
1235 locate_old_decl (olddecl, error);
1236 return false;
1239 /* If we have a prototype after an old-style function definition,
1240 the argument types must be checked specially. */
1241 else if (DECL_INITIAL (olddecl)
1242 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1243 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1244 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1246 locate_old_decl (olddecl, error);
1247 return false;
1249 /* A non-static declaration (even an "extern") followed by a
1250 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1251 The same is true for a static forward declaration at block
1252 scope followed by a non-static declaration/definition at file
1253 scope. Static followed by non-static at the same scope is
1254 not undefined behavior, and is the most convenient way to get
1255 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1256 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1257 we do diagnose it if -Wtraditional. */
1258 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1260 /* Two exceptions to the rule. If olddecl is an extern
1261 inline, or a predeclared function that isn't actually
1262 built in, newdecl silently overrides olddecl. The latter
1263 occur only in Objective C; see also above. (FIXME: Make
1264 Objective C use normal builtins.) */
1265 if (!DECL_IS_BUILTIN (olddecl)
1266 && !(DECL_EXTERNAL (olddecl)
1267 && DECL_DECLARED_INLINE_P (olddecl)))
1269 error ("%Jstatic declaration of '%D' follows "
1270 "non-static declaration", newdecl, newdecl);
1271 locate_old_decl (olddecl, error);
1273 return false;
1275 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1277 if (DECL_CONTEXT (olddecl))
1279 error ("%Jnon-static declaration of '%D' follows "
1280 "static declaration", newdecl, newdecl);
1281 locate_old_decl (olddecl, error);
1282 return false;
1284 else if (warn_traditional)
1286 warning ("%Jnon-static declaration of '%D' follows "
1287 "static declaration", newdecl, newdecl);
1288 warned = true;
1292 else if (TREE_CODE (newdecl) == VAR_DECL)
1294 /* Only variables can be thread-local, and all declarations must
1295 agree on this property. */
1296 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1298 if (DECL_THREAD_LOCAL (newdecl))
1299 error ("%Jthread-local declaration of '%D' follows "
1300 "non-thread-local declaration", newdecl, newdecl);
1301 else
1302 error ("%Jnon-thread-local declaration of '%D' follows "
1303 "thread-local declaration", newdecl, newdecl);
1305 locate_old_decl (olddecl, error);
1306 return false;
1309 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1310 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1312 error ("%Jredefinition of '%D'", newdecl, newdecl);
1313 locate_old_decl (olddecl, error);
1314 return false;
1317 /* Objects declared at file scope: if the first declaration had
1318 external linkage (even if it was an external reference) the
1319 second must have external linkage as well, or the behavior is
1320 undefined. If the first declaration had internal linkage, then
1321 the second must too, or else be an external reference (in which
1322 case the composite declaration still has internal linkage).
1323 As for function declarations, we warn about the static-then-
1324 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1325 if (DECL_FILE_SCOPE_P (newdecl)
1326 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1328 if (DECL_EXTERNAL (newdecl))
1330 if (warn_traditional)
1332 warning ("%Jnon-static declaration of '%D' follows "
1333 "static declaration", newdecl, newdecl);
1334 warned = true;
1337 else
1339 if (TREE_PUBLIC (newdecl))
1340 error ("%Jnon-static declaration of '%D' follows "
1341 "static declaration", newdecl, newdecl);
1342 else
1343 error ("%Jstatic declaration of '%D' follows "
1344 "non-static declaration", newdecl, newdecl);
1346 locate_old_decl (olddecl, error);
1347 return false;
1350 /* Two objects with the same name declared at the same block
1351 scope must both be external references (6.7p3). */
1352 else if (!DECL_FILE_SCOPE_P (newdecl)
1353 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1354 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1356 if (DECL_EXTERNAL (newdecl))
1357 error ("%Jextern declaration of '%D' follows "
1358 "declaration with no linkage", newdecl, newdecl);
1359 else if (DECL_EXTERNAL (olddecl))
1360 error ("%Jdeclaration of '%D' with no linkage follows "
1361 "extern declaration", newdecl, newdecl);
1362 else
1363 error ("%Jredeclaration of '%D' with no linkage",
1364 newdecl, newdecl);
1366 locate_old_decl (olddecl, error);
1367 return false;
1371 /* warnings */
1372 /* All decls must agree on a non-default visibility. */
1373 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1374 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1375 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1377 warning ("%Jredeclaration of '%D' with different visibility "
1378 "(old visibility preserved)", newdecl, newdecl);
1379 warned = true;
1382 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1384 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1385 if (DECL_DECLARED_INLINE_P (newdecl)
1386 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1388 warning ("%Jinline declaration of '%D' follows "
1389 "declaration with attribute noinline", newdecl, newdecl);
1390 warned = true;
1392 else if (DECL_DECLARED_INLINE_P (olddecl)
1393 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1395 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1396 "inline declaration ", newdecl, newdecl);
1397 warned = true;
1400 /* Inline declaration after use or definition.
1401 ??? Should we still warn about this now we have unit-at-a-time
1402 mode and can get it right?
1403 Definitely don't complain if the decls are in different translation
1404 units. */
1405 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1406 && same_translation_unit_p (olddecl, newdecl))
1408 if (TREE_USED (olddecl))
1410 warning ("%J'%D' declared inline after being called",
1411 olddecl, olddecl);
1412 warned = true;
1414 else if (DECL_INITIAL (olddecl))
1416 warning ("%J'%D' declared inline after its definition",
1417 olddecl, olddecl);
1418 warned = true;
1422 else /* PARM_DECL, VAR_DECL */
1424 /* Redeclaration of a parameter is a constraint violation (this is
1425 not explicitly stated, but follows from C99 6.7p3 [no more than
1426 one declaration of the same identifier with no linkage in the
1427 same scope, except type tags] and 6.2.2p6 [parameters have no
1428 linkage]). We must check for a forward parameter declaration,
1429 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1430 an extension, the mandatory diagnostic for which is handled by
1431 mark_forward_parm_decls. */
1433 if (TREE_CODE (newdecl) == PARM_DECL
1434 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1436 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1438 return false;
1442 /* Optional warning for completely redundant decls. */
1443 if (!warned && !pedwarned
1444 && warn_redundant_decls
1445 /* Don't warn about a function declaration followed by a
1446 definition. */
1447 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1448 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1449 /* Don't warn about an extern followed by a definition. */
1450 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1451 /* Don't warn about forward parameter decls. */
1452 && !(TREE_CODE (newdecl) == PARM_DECL
1453 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1455 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1456 warned = true;
1459 /* Report location of previous decl/defn in a consistent manner. */
1460 if (warned || pedwarned)
1461 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1463 return true;
1466 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1467 consistent with OLDDECL, but carries new information. Merge the
1468 new information into OLDDECL. This function issues no
1469 diagnostics. */
1471 static void
1472 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1474 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1475 && DECL_INITIAL (newdecl) != 0);
1477 /* For real parm decl following a forward decl, rechain the old decl
1478 in its new location and clear TREE_ASM_WRITTEN (it's not a
1479 forward decl anymore). */
1480 if (TREE_CODE (newdecl) == PARM_DECL
1481 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1483 struct c_binding *b, **here;
1485 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1486 if ((*here)->decl == olddecl)
1487 goto found;
1488 abort ();
1490 found:
1491 b = *here;
1492 *here = b->prev;
1493 b->prev = current_scope->bindings;
1494 current_scope->bindings = b;
1496 TREE_ASM_WRITTEN (olddecl) = 0;
1499 DECL_ATTRIBUTES (newdecl)
1500 = targetm.merge_decl_attributes (olddecl, newdecl);
1502 /* Merge the data types specified in the two decls. */
1503 TREE_TYPE (newdecl)
1504 = TREE_TYPE (olddecl)
1505 = composite_type (newtype, oldtype);
1507 /* Lay the type out, unless already done. */
1508 if (oldtype != TREE_TYPE (newdecl))
1510 if (TREE_TYPE (newdecl) != error_mark_node)
1511 layout_type (TREE_TYPE (newdecl));
1512 if (TREE_CODE (newdecl) != FUNCTION_DECL
1513 && TREE_CODE (newdecl) != TYPE_DECL
1514 && TREE_CODE (newdecl) != CONST_DECL)
1515 layout_decl (newdecl, 0);
1517 else
1519 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1520 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1521 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1522 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1523 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1524 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1526 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1527 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1531 /* Keep the old rtl since we can safely use it. */
1532 COPY_DECL_RTL (olddecl, newdecl);
1534 /* Merge the type qualifiers. */
1535 if (TREE_READONLY (newdecl))
1536 TREE_READONLY (olddecl) = 1;
1538 if (TREE_THIS_VOLATILE (newdecl))
1540 TREE_THIS_VOLATILE (olddecl) = 1;
1541 if (TREE_CODE (newdecl) == VAR_DECL)
1542 make_var_volatile (newdecl);
1545 /* Keep source location of definition rather than declaration. */
1546 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1547 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1549 /* Merge the unused-warning information. */
1550 if (DECL_IN_SYSTEM_HEADER (olddecl))
1551 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1552 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1553 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1555 /* Merge the initialization information. */
1556 if (DECL_INITIAL (newdecl) == 0)
1557 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1559 /* Merge the section attribute.
1560 We want to issue an error if the sections conflict but that must be
1561 done later in decl_attributes since we are called before attributes
1562 are assigned. */
1563 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1564 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1566 /* Copy the assembler name.
1567 Currently, it can only be defined in the prototype. */
1568 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1570 /* If either declaration has a nondefault visibility, use it. */
1571 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1572 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1574 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1576 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1577 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1578 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1579 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1580 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1581 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1582 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1583 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1584 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1587 /* Merge the storage class information. */
1588 merge_weak (newdecl, olddecl);
1590 /* For functions, static overrides non-static. */
1591 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1593 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1594 /* This is since we don't automatically
1595 copy the attributes of NEWDECL into OLDDECL. */
1596 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1597 /* If this clears `static', clear it in the identifier too. */
1598 if (! TREE_PUBLIC (olddecl))
1599 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1601 if (DECL_EXTERNAL (newdecl))
1603 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1604 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1606 /* An extern decl does not override previous storage class. */
1607 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1608 if (! DECL_EXTERNAL (newdecl))
1610 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1611 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1614 else
1616 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1617 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1620 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1622 /* If we're redefining a function previously defined as extern
1623 inline, make sure we emit debug info for the inline before we
1624 throw it away, in case it was inlined into a function that hasn't
1625 been written out yet. */
1626 if (new_is_definition && DECL_INITIAL (olddecl))
1628 if (TREE_USED (olddecl)
1629 /* In unit-at-a-time mode we never inline re-defined extern
1630 inline functions. */
1631 && !flag_unit_at_a_time
1632 && cgraph_function_possibly_inlined_p (olddecl))
1633 (*debug_hooks->outlining_inline_function) (olddecl);
1635 /* The new defn must not be inline. */
1636 DECL_INLINE (newdecl) = 0;
1637 DECL_UNINLINABLE (newdecl) = 1;
1639 else
1641 /* If either decl says `inline', this fn is inline,
1642 unless its definition was passed already. */
1643 if (DECL_DECLARED_INLINE_P (newdecl)
1644 || DECL_DECLARED_INLINE_P (olddecl))
1645 DECL_DECLARED_INLINE_P (newdecl) = 1;
1647 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1648 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1651 if (DECL_BUILT_IN (olddecl))
1653 /* If redeclaring a builtin function, it stays built in.
1654 But it gets tagged as having been declared. */
1655 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1656 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1657 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1660 /* Also preserve various other info from the definition. */
1661 if (! new_is_definition)
1663 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1664 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1665 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1666 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1667 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1669 /* Set DECL_INLINE on the declaration if we've got a body
1670 from which to instantiate. */
1671 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1673 DECL_INLINE (newdecl) = 1;
1674 DECL_ABSTRACT_ORIGIN (newdecl)
1675 = DECL_ABSTRACT_ORIGIN (olddecl);
1678 else
1680 /* If a previous declaration said inline, mark the
1681 definition as inlinable. */
1682 if (DECL_DECLARED_INLINE_P (newdecl)
1683 && ! DECL_UNINLINABLE (newdecl))
1684 DECL_INLINE (newdecl) = 1;
1688 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1689 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1691 unsigned olddecl_uid = DECL_UID (olddecl);
1692 tree olddecl_context = DECL_CONTEXT (olddecl);
1694 memcpy ((char *) olddecl + sizeof (struct tree_common),
1695 (char *) newdecl + sizeof (struct tree_common),
1696 sizeof (struct tree_decl) - sizeof (struct tree_common));
1697 DECL_UID (olddecl) = olddecl_uid;
1698 DECL_CONTEXT (olddecl) = olddecl_context;
1701 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1702 so that encode_section_info has a chance to look at the new decl
1703 flags and attributes. */
1704 if (DECL_RTL_SET_P (olddecl)
1705 && (TREE_CODE (olddecl) == FUNCTION_DECL
1706 || (TREE_CODE (olddecl) == VAR_DECL
1707 && TREE_STATIC (olddecl))))
1708 make_decl_rtl (olddecl, NULL);
1711 /* Handle when a new declaration NEWDECL has the same name as an old
1712 one OLDDECL in the same binding contour. Prints an error message
1713 if appropriate.
1715 If safely possible, alter OLDDECL to look like NEWDECL, and return
1716 true. Otherwise, return false. */
1718 static bool
1719 duplicate_decls (tree newdecl, tree olddecl)
1721 tree newtype = NULL, oldtype = NULL;
1723 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1724 return false;
1726 merge_decls (newdecl, olddecl, newtype, oldtype);
1727 return true;
1731 /* Check whether decl-node NEW shadows an existing declaration. */
1732 static void
1733 warn_if_shadowing (tree new)
1735 struct c_binding *b;
1737 /* Shadow warnings wanted? */
1738 if (!warn_shadow
1739 /* No shadow warnings for internally generated vars. */
1740 || DECL_IS_BUILTIN (new)
1741 /* No shadow warnings for vars made for inlining. */
1742 || DECL_FROM_INLINE (new)
1743 /* Don't warn about the parm names in function declarator
1744 within a function declarator. It would be nice to avoid
1745 warning in any function declarator in a declaration, as
1746 opposed to a definition, but there is no way to tell
1747 it's not a definition at this point. */
1748 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1749 return;
1751 /* Is anything being shadowed? Invisible decls do not count. */
1752 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1753 if (b->decl && b->decl != new && !b->invisible)
1755 tree old = b->decl;
1757 if (TREE_CODE (old) == PARM_DECL)
1758 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1759 else if (DECL_FILE_SCOPE_P (old))
1760 warning ("%Jdeclaration of '%D' shadows a global declaration",
1761 new, new);
1762 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1763 warning ("%Jdeclaration of '%D' shadows a built-in function",
1764 new, new);
1765 else
1766 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1768 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1769 warning ("%Jshadowed declaration is here", old);
1771 break;
1776 /* Subroutine of pushdecl.
1778 X is a TYPE_DECL for a typedef statement. Create a brand new
1779 ..._TYPE node (which will be just a variant of the existing
1780 ..._TYPE node with identical properties) and then install X
1781 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1783 The whole point here is to end up with a situation where each
1784 and every ..._TYPE node the compiler creates will be uniquely
1785 associated with AT MOST one node representing a typedef name.
1786 This way, even though the compiler substitutes corresponding
1787 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1788 early on, later parts of the compiler can always do the reverse
1789 translation and get back the corresponding typedef name. For
1790 example, given:
1792 typedef struct S MY_TYPE;
1793 MY_TYPE object;
1795 Later parts of the compiler might only know that `object' was of
1796 type `struct S' if it were not for code just below. With this
1797 code however, later parts of the compiler see something like:
1799 struct S' == struct S
1800 typedef struct S' MY_TYPE;
1801 struct S' object;
1803 And they can then deduce (from the node for type struct S') that
1804 the original object declaration was:
1806 MY_TYPE object;
1808 Being able to do this is important for proper support of protoize,
1809 and also for generating precise symbolic debugging information
1810 which takes full account of the programmer's (typedef) vocabulary.
1812 Obviously, we don't want to generate a duplicate ..._TYPE node if
1813 the TYPE_DECL node that we are now processing really represents a
1814 standard built-in type.
1816 Since all standard types are effectively declared at line zero
1817 in the source file, we can easily check to see if we are working
1818 on a standard type by checking the current value of lineno. */
1820 static void
1821 clone_underlying_type (tree x)
1823 if (DECL_IS_BUILTIN (x))
1825 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1826 TYPE_NAME (TREE_TYPE (x)) = x;
1828 else if (TREE_TYPE (x) != error_mark_node
1829 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1831 tree tt = TREE_TYPE (x);
1832 DECL_ORIGINAL_TYPE (x) = tt;
1833 tt = build_type_copy (tt);
1834 TYPE_NAME (tt) = x;
1835 TREE_USED (tt) = TREE_USED (x);
1836 TREE_TYPE (x) = tt;
1840 /* Record a decl-node X as belonging to the current lexical scope.
1841 Check for errors (such as an incompatible declaration for the same
1842 name already seen in the same scope).
1844 Returns either X or an old decl for the same name.
1845 If an old decl is returned, it may have been smashed
1846 to agree with what X says. */
1848 tree
1849 pushdecl (tree x)
1851 tree name = DECL_NAME (x);
1852 struct c_scope *scope = current_scope;
1853 struct c_binding *b;
1854 bool nested = false;
1856 /* Functions need the lang_decl data. */
1857 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1858 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1860 /* Must set DECL_CONTEXT for everything not at file scope or
1861 DECL_FILE_SCOPE_P won't work. Local externs don't count
1862 unless they have initializers (which generate code). */
1863 if (current_function_decl
1864 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1865 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1866 DECL_CONTEXT (x) = current_function_decl;
1868 /* Anonymous decls are just inserted in the scope. */
1869 if (!name)
1871 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1872 return x;
1875 /* First, see if there is another declaration with the same name in
1876 the current scope. If there is, duplicate_decls may do all the
1877 work for us. If duplicate_decls returns false, that indicates
1878 two incompatible decls in the same scope; we are to silently
1879 replace the old one (duplicate_decls has issued all appropriate
1880 diagnostics). In particular, we should not consider possible
1881 duplicates in the external scope, or shadowing. */
1882 b = I_SYMBOL_BINDING (name);
1883 if (b && B_IN_SCOPE (b, scope))
1885 if (duplicate_decls (x, b->decl))
1886 return b->decl;
1887 else
1888 goto skip_external_and_shadow_checks;
1891 /* All declarations with external linkage, and all external
1892 references, go in the external scope, no matter what scope is
1893 current. However, the binding in that scope is ignored for
1894 purposes of normal name lookup. A separate binding structure is
1895 created in the requested scope; this governs the normal
1896 visibility of the symbol.
1898 The binding in the externals scope is used exclusively for
1899 detecting duplicate declarations of the same object, no matter
1900 what scope they are in; this is what we do here. (C99 6.2.7p2:
1901 All declarations that refer to the same object or function shall
1902 have compatible type; otherwise, the behavior is undefined.) */
1903 if (DECL_EXTERNAL (x) || scope == file_scope)
1905 if (warn_nested_externs
1906 && scope != file_scope
1907 && !DECL_IN_SYSTEM_HEADER (x))
1908 warning ("nested extern declaration of '%D'", x);
1910 while (b && !B_IN_EXTERNAL_SCOPE (b))
1911 b = b->shadowed;
1913 /* The point of the same_translation_unit_p check here is,
1914 we want to detect a duplicate decl for a construct like
1915 foo() { extern bar(); } ... static bar(); but not if
1916 they are in different translation units. In any case,
1917 the static does not go in the externals scope. */
1918 if (b
1919 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
1920 && duplicate_decls (x, b->decl))
1922 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1923 return b->decl;
1925 else if (TREE_PUBLIC (x))
1927 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1928 nested = true;
1931 /* Similarly, a declaration of a function with static linkage at
1932 block scope must be checked against any existing declaration
1933 of that function at file scope. */
1934 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1935 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1937 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1938 warning ("nested static declaration of '%D'", x);
1940 while (b && !B_IN_FILE_SCOPE (b))
1941 b = b->shadowed;
1943 if (b && same_translation_unit_p (x, b->decl)
1944 && duplicate_decls (x, b->decl))
1946 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1947 return b->decl;
1949 else
1951 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1952 nested = true;
1956 warn_if_shadowing (x);
1958 skip_external_and_shadow_checks:
1959 if (TREE_CODE (x) == TYPE_DECL)
1960 clone_underlying_type (x);
1962 bind (name, x, scope, /*invisible=*/false, nested);
1964 /* If x's type is incomplete because it's based on a
1965 structure or union which has not yet been fully declared,
1966 attach it to that structure or union type, so we can go
1967 back and complete the variable declaration later, if the
1968 structure or union gets fully declared.
1970 If the input is erroneous, we can have error_mark in the type
1971 slot (e.g. "f(void a, ...)") - that doesn't count as an
1972 incomplete type. */
1973 if (TREE_TYPE (x) != error_mark_node
1974 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1976 tree element = TREE_TYPE (x);
1978 while (TREE_CODE (element) == ARRAY_TYPE)
1979 element = TREE_TYPE (element);
1980 element = TYPE_MAIN_VARIANT (element);
1982 if ((TREE_CODE (element) == RECORD_TYPE
1983 || TREE_CODE (element) == UNION_TYPE)
1984 && (TREE_CODE (x) != TYPE_DECL
1985 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1986 && !COMPLETE_TYPE_P (element))
1987 C_TYPE_INCOMPLETE_VARS (element)
1988 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1990 return x;
1993 /* Record X as belonging to file scope.
1994 This is used only internally by the Objective-C front end,
1995 and is limited to its needs. duplicate_decls is not called;
1996 if there is any preexisting decl for this identifier, it is an ICE. */
1998 tree
1999 pushdecl_top_level (tree x)
2001 tree name;
2002 bool nested = false;
2004 if (TREE_CODE (x) != VAR_DECL)
2005 abort ();
2007 name = DECL_NAME (x);
2009 if (I_SYMBOL_BINDING (name))
2010 abort ();
2012 if (TREE_PUBLIC (x))
2014 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2015 nested = true;
2017 if (file_scope)
2018 bind (name, x, file_scope, /*invisible=*/false, nested);
2020 return x;
2023 static void
2024 implicit_decl_warning (tree id, tree olddecl)
2026 void (*diag) (const char *, ...);
2027 switch (mesg_implicit_function_declaration)
2029 case 0: return;
2030 case 1: diag = warning; break;
2031 case 2: diag = error; break;
2032 default: abort ();
2035 diag (N_("implicit declaration of function '%E'"), id);
2036 if (olddecl)
2037 locate_old_decl (olddecl, diag);
2040 /* Generate an implicit declaration for identifier FUNCTIONID as a
2041 function of type int (). */
2043 tree
2044 implicitly_declare (tree functionid)
2046 tree decl = lookup_name_in_scope (functionid, external_scope);
2048 if (decl)
2050 /* FIXME: Objective-C has weird not-really-builtin functions
2051 which are supposed to be visible automatically. They wind up
2052 in the external scope because they're pushed before the file
2053 scope gets created. Catch this here and rebind them into the
2054 file scope. */
2055 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2057 bind (functionid, decl, file_scope,
2058 /*invisible=*/false, /*nested=*/true);
2059 return decl;
2061 else
2063 /* Implicit declaration of a function already declared
2064 (somehow) in a different scope, or as a built-in.
2065 If this is the first time this has happened, warn;
2066 then recycle the old declaration. */
2067 if (!C_DECL_IMPLICIT (decl))
2069 implicit_decl_warning (functionid, decl);
2070 C_DECL_IMPLICIT (decl) = 1;
2072 bind (functionid, decl, current_scope,
2073 /*invisible=*/false, /*nested=*/true);
2074 return decl;
2078 /* Not seen before. */
2079 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2080 DECL_EXTERNAL (decl) = 1;
2081 TREE_PUBLIC (decl) = 1;
2082 C_DECL_IMPLICIT (decl) = 1;
2083 implicit_decl_warning (functionid, 0);
2085 /* C89 says implicit declarations are in the innermost block.
2086 So we record the decl in the standard fashion. */
2087 decl = pushdecl (decl);
2089 /* No need to call objc_check_decl here - it's a function type. */
2090 rest_of_decl_compilation (decl, NULL, 0, 0);
2092 /* Write a record describing this implicit function declaration
2093 to the prototypes file (if requested). */
2094 gen_aux_info_record (decl, 0, 1, 0);
2096 /* Possibly apply some default attributes to this implicit declaration. */
2097 decl_attributes (&decl, NULL_TREE, 0);
2099 return decl;
2102 /* Issue an error message for a reference to an undeclared variable
2103 ID, including a reference to a builtin outside of function-call
2104 context. Establish a binding of the identifier to error_mark_node
2105 in an appropriate scope, which will suppress further errors for the
2106 same identifier. */
2107 void
2108 undeclared_variable (tree id)
2110 static bool already = false;
2111 struct c_scope *scope;
2113 if (current_function_decl == 0)
2115 error ("'%E' undeclared here (not in a function)", id);
2116 scope = current_scope;
2118 else
2120 error ("'%E' undeclared (first use in this function)", id);
2122 if (! already)
2124 error ("(Each undeclared identifier is reported only once");
2125 error ("for each function it appears in.)");
2126 already = true;
2129 /* If we are parsing old-style parameter decls, current_function_decl
2130 will be nonnull but current_function_scope will be null. */
2131 scope = current_function_scope ? current_function_scope : current_scope;
2133 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2136 /* Subroutine of lookup_label, declare_label, define_label: construct a
2137 LABEL_DECL with all the proper frills. */
2139 static tree
2140 make_label (tree name, location_t location)
2142 tree label = build_decl (LABEL_DECL, name, void_type_node);
2144 DECL_CONTEXT (label) = current_function_decl;
2145 DECL_MODE (label) = VOIDmode;
2146 DECL_SOURCE_LOCATION (label) = location;
2148 return label;
2151 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2152 Create one if none exists so far for the current function.
2153 This is called when a label is used in a goto expression or
2154 has its address taken. */
2156 tree
2157 lookup_label (tree name)
2159 tree label;
2161 if (current_function_decl == 0)
2163 error ("label %s referenced outside of any function",
2164 IDENTIFIER_POINTER (name));
2165 return 0;
2168 /* Use a label already defined or ref'd with this name, but not if
2169 it is inherited from a containing function and wasn't declared
2170 using __label__. */
2171 label = I_LABEL_DECL (name);
2172 if (label && (DECL_CONTEXT (label) == current_function_decl
2173 || C_DECLARED_LABEL_FLAG (label)))
2175 /* If the label has only been declared, update its apparent
2176 location to point here, for better diagnostics if it
2177 turns out not to have been defined. */
2178 if (!TREE_USED (label))
2179 DECL_SOURCE_LOCATION (label) = input_location;
2180 return label;
2183 /* No label binding for that identifier; make one. */
2184 label = make_label (name, input_location);
2186 /* Ordinary labels go in the current function scope. */
2187 bind (name, label, current_function_scope,
2188 /*invisible=*/false, /*nested=*/false);
2189 return label;
2192 /* Make a label named NAME in the current function, shadowing silently
2193 any that may be inherited from containing functions or containing
2194 scopes. This is called for __label__ declarations. */
2196 tree
2197 declare_label (tree name)
2199 struct c_binding *b = I_LABEL_BINDING (name);
2200 tree label;
2202 /* Check to make sure that the label hasn't already been declared
2203 at this scope */
2204 if (b && B_IN_CURRENT_SCOPE (b))
2206 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2207 locate_old_decl (b->decl, error);
2209 /* Just use the previous declaration. */
2210 return b->decl;
2213 label = make_label (name, input_location);
2214 C_DECLARED_LABEL_FLAG (label) = 1;
2216 /* Declared labels go in the current scope. */
2217 bind (name, label, current_scope,
2218 /*invisible=*/false, /*nested=*/false);
2219 return label;
2222 /* Define a label, specifying the location in the source file.
2223 Return the LABEL_DECL node for the label, if the definition is valid.
2224 Otherwise return 0. */
2226 tree
2227 define_label (location_t location, tree name)
2229 /* Find any preexisting label with this name. It is an error
2230 if that label has already been defined in this function, or
2231 if there is a containing function with a declared label with
2232 the same name. */
2233 tree label = I_LABEL_DECL (name);
2235 if (label
2236 && ((DECL_CONTEXT (label) == current_function_decl
2237 && DECL_INITIAL (label) != 0)
2238 || (DECL_CONTEXT (label) != current_function_decl
2239 && C_DECLARED_LABEL_FLAG (label))))
2241 error ("%Hduplicate label `%D'", &location, label);
2242 locate_old_decl (label, error);
2243 return 0;
2245 else if (label && DECL_CONTEXT (label) == current_function_decl)
2247 /* The label has been used or declared already in this function,
2248 but not defined. Update its location to point to this
2249 definition. */
2250 DECL_SOURCE_LOCATION (label) = location;
2252 else
2254 /* No label binding for that identifier; make one. */
2255 label = make_label (name, location);
2257 /* Ordinary labels go in the current function scope. */
2258 bind (name, label, current_function_scope,
2259 /*invisible=*/false, /*nested=*/false);
2262 if (warn_traditional && !in_system_header && lookup_name (name))
2263 warning ("%Htraditional C lacks a separate namespace for labels, "
2264 "identifier `%s' conflicts", &location,
2265 IDENTIFIER_POINTER (name));
2267 /* Mark label as having been defined. */
2268 DECL_INITIAL (label) = error_mark_node;
2269 return label;
2272 /* Given NAME, an IDENTIFIER_NODE,
2273 return the structure (or union or enum) definition for that name.
2274 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2275 CODE says which kind of type the caller wants;
2276 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2277 If the wrong kind of type is found, an error is reported. */
2279 static tree
2280 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2282 struct c_binding *b = I_TAG_BINDING (name);
2283 int thislevel = 0;
2285 if (!b || !b->decl)
2286 return 0;
2288 /* We only care about whether it's in this level if
2289 thislevel_only was set or it might be a type clash. */
2290 if (thislevel_only || TREE_CODE (b->decl) != code)
2292 /* For our purposes, a tag in the external scope is the same as
2293 a tag in the file scope. (Primarily relevant to Objective-C
2294 and its builtin structure tags, which get pushed before the
2295 file scope is created.) */
2296 if (B_IN_CURRENT_SCOPE (b)
2297 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2298 thislevel = 1;
2301 if (thislevel_only && !thislevel)
2302 return 0;
2304 if (TREE_CODE (b->decl) != code)
2306 /* Definition isn't the kind we were looking for. */
2307 pending_invalid_xref = name;
2308 pending_invalid_xref_location = input_location;
2310 /* If in the same binding level as a declaration as a tag
2311 of a different type, this must not be allowed to
2312 shadow that tag, so give the error immediately.
2313 (For example, "struct foo; union foo;" is invalid.) */
2314 if (thislevel)
2315 pending_xref_error ();
2317 return b->decl;
2320 /* Print an error message now
2321 for a recent invalid struct, union or enum cross reference.
2322 We don't print them immediately because they are not invalid
2323 when used in the `struct foo;' construct for shadowing. */
2325 void
2326 pending_xref_error (void)
2328 if (pending_invalid_xref != 0)
2329 error ("%H`%s' defined as wrong kind of tag",
2330 &pending_invalid_xref_location,
2331 IDENTIFIER_POINTER (pending_invalid_xref));
2332 pending_invalid_xref = 0;
2336 /* Look up NAME in the current scope and its superiors
2337 in the namespace of variables, functions and typedefs.
2338 Return a ..._DECL node of some kind representing its definition,
2339 or return 0 if it is undefined. */
2341 tree
2342 lookup_name (tree name)
2344 struct c_binding *b = I_SYMBOL_BINDING (name);
2345 if (b && !b->invisible)
2346 return b->decl;
2347 return 0;
2350 /* Similar to `lookup_name' but look only at the indicated scope. */
2352 static tree
2353 lookup_name_in_scope (tree name, struct c_scope *scope)
2355 struct c_binding *b;
2357 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2358 if (B_IN_SCOPE (b, scope))
2359 return b->decl;
2360 return 0;
2363 /* Create the predefined scalar types of C,
2364 and some nodes representing standard constants (0, 1, (void *) 0).
2365 Initialize the global scope.
2366 Make definitions for built-in primitive functions. */
2368 void
2369 c_init_decl_processing (void)
2371 tree endlink;
2372 tree ptr_ftype_void, ptr_ftype_ptr;
2373 location_t save_loc = input_location;
2375 /* Adds some ggc roots, and reserved words for c-parse.in. */
2376 c_parse_init ();
2378 current_function_decl = 0;
2380 /* Make the externals scope. */
2381 push_scope ();
2382 external_scope = current_scope;
2384 /* Declarations from c_common_nodes_and_builtins must not be associated
2385 with this input file, lest we get differences between using and not
2386 using preprocessed headers. */
2387 #ifdef USE_MAPPED_LOCATION
2388 input_location = BUILTINS_LOCATION;
2389 #else
2390 input_location.file = "<built-in>";
2391 input_location.line = 0;
2392 #endif
2394 build_common_tree_nodes (flag_signed_char);
2396 c_common_nodes_and_builtins ();
2398 /* In C, comparisons and TRUTH_* expressions have type int. */
2399 truthvalue_type_node = integer_type_node;
2400 truthvalue_true_node = integer_one_node;
2401 truthvalue_false_node = integer_zero_node;
2403 /* Even in C99, which has a real boolean type. */
2404 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2405 boolean_type_node));
2407 endlink = void_list_node;
2408 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2409 ptr_ftype_ptr
2410 = build_function_type (ptr_type_node,
2411 tree_cons (NULL_TREE, ptr_type_node, endlink));
2413 input_location = save_loc;
2415 pedantic_lvalues = true;
2417 make_fname_decl = c_make_fname_decl;
2418 start_fname_decls ();
2421 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2422 decl, NAME is the initialization string and TYPE_DEP indicates whether
2423 NAME depended on the type of the function. As we don't yet implement
2424 delayed emission of static data, we mark the decl as emitted
2425 so it is not placed in the output. Anything using it must therefore pull
2426 out the STRING_CST initializer directly. FIXME. */
2428 static tree
2429 c_make_fname_decl (tree id, int type_dep)
2431 const char *name = fname_as_string (type_dep);
2432 tree decl, type, init;
2433 size_t length = strlen (name);
2435 type = build_array_type
2436 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2437 build_index_type (size_int (length)));
2439 decl = build_decl (VAR_DECL, id, type);
2441 TREE_STATIC (decl) = 1;
2442 TREE_READONLY (decl) = 1;
2443 DECL_ARTIFICIAL (decl) = 1;
2445 init = build_string (length + 1, name);
2446 free ((char *) name);
2447 TREE_TYPE (init) = type;
2448 DECL_INITIAL (decl) = init;
2450 TREE_USED (decl) = 1;
2452 if (current_function_decl)
2454 DECL_CONTEXT (decl) = current_function_decl;
2455 bind (id, decl, current_function_scope,
2456 /*invisible=*/false, /*nested=*/false);
2459 finish_decl (decl, init, NULL_TREE);
2461 return decl;
2464 /* Return a definition for a builtin function named NAME and whose data type
2465 is TYPE. TYPE should be a function type with argument types.
2466 FUNCTION_CODE tells later passes how to compile calls to this function.
2467 See tree.h for its possible values.
2469 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2470 the name to be called if we can't opencode the function. If
2471 ATTRS is nonzero, use that for the function's attribute list. */
2473 tree
2474 builtin_function (const char *name, tree type, int function_code,
2475 enum built_in_class class, const char *library_name,
2476 tree attrs)
2478 tree id = get_identifier (name);
2479 tree decl = build_decl (FUNCTION_DECL, id, type);
2480 TREE_PUBLIC (decl) = 1;
2481 DECL_EXTERNAL (decl) = 1;
2482 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2483 DECL_BUILT_IN_CLASS (decl) = class;
2484 DECL_FUNCTION_CODE (decl) = function_code;
2485 if (library_name)
2486 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2488 /* Should never be called on a symbol with a preexisting meaning. */
2489 if (I_SYMBOL_BINDING (id))
2490 abort ();
2492 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2494 /* Builtins in the implementation namespace are made visible without
2495 needing to be explicitly declared. See push_file_scope. */
2496 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2498 TREE_CHAIN (decl) = visible_builtins;
2499 visible_builtins = decl;
2502 /* Possibly apply some default attributes to this built-in function. */
2503 if (attrs)
2504 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2505 else
2506 decl_attributes (&decl, NULL_TREE, 0);
2508 return decl;
2511 /* Called when a declaration is seen that contains no names to declare.
2512 If its type is a reference to a structure, union or enum inherited
2513 from a containing scope, shadow that tag name for the current scope
2514 with a forward reference.
2515 If its type defines a new named structure or union
2516 or defines an enum, it is valid but we need not do anything here.
2517 Otherwise, it is an error. */
2519 void
2520 shadow_tag (tree declspecs)
2522 shadow_tag_warned (declspecs, 0);
2525 void
2526 shadow_tag_warned (tree declspecs, int warned)
2529 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2530 no pedwarn. */
2532 int found_tag = 0;
2533 tree link;
2534 tree specs, attrs;
2536 pending_invalid_xref = 0;
2538 /* Remove the attributes from declspecs, since they will confuse the
2539 following code. */
2540 split_specs_attrs (declspecs, &specs, &attrs);
2542 for (link = specs; link; link = TREE_CHAIN (link))
2544 tree value = TREE_VALUE (link);
2545 enum tree_code code = TREE_CODE (value);
2547 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2548 /* Used to test also that TYPE_SIZE (value) != 0.
2549 That caused warning for `struct foo;' at top level in the file. */
2551 tree name = TYPE_NAME (value);
2552 tree t;
2554 found_tag++;
2556 if (name == 0)
2558 if (warned != 1 && code != ENUMERAL_TYPE)
2559 /* Empty unnamed enum OK */
2561 pedwarn ("unnamed struct/union that defines no instances");
2562 warned = 1;
2565 else
2567 t = lookup_tag (code, name, 1);
2569 if (t == 0)
2571 t = make_node (code);
2572 pushtag (name, t);
2576 else
2578 if (!warned && ! in_system_header)
2580 warning ("useless keyword or type name in empty declaration");
2581 warned = 2;
2586 if (found_tag > 1)
2587 error ("two types specified in one empty declaration");
2589 if (warned != 1)
2591 if (found_tag == 0)
2592 pedwarn ("empty declaration");
2596 /* Construct an array declarator. EXPR is the expression inside [], or
2597 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2598 to the pointer to which a parameter array is converted). STATIC_P is
2599 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2600 is nonzero is the array is [*], a VLA of unspecified length which is
2601 nevertheless a complete type (not currently implemented by GCC),
2602 zero otherwise. The declarator is constructed as an ARRAY_REF
2603 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2604 left of the [] (filled by in set_array_declarator_type) and operand 1
2605 is the expression inside; whose TREE_TYPE is the type qualifiers and
2606 which has TREE_STATIC set if "static" is used. */
2608 tree
2609 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2611 tree decl;
2612 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2613 TREE_TYPE (decl) = quals;
2614 TREE_STATIC (decl) = (static_p ? 1 : 0);
2615 if (pedantic && !flag_isoc99)
2617 if (static_p || quals != NULL_TREE)
2618 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2619 if (vla_unspec_p)
2620 pedwarn ("ISO C90 does not support `[*]' array declarators");
2622 if (vla_unspec_p)
2623 warning ("GCC does not yet properly implement `[*]' array declarators");
2624 return decl;
2627 /* Set the type of an array declarator. DECL is the declarator, as
2628 constructed by build_array_declarator; TYPE is what appears on the left
2629 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2630 abstract declarator, zero otherwise; this is used to reject static and
2631 type qualifiers in abstract declarators, where they are not in the
2632 C99 grammar. */
2634 tree
2635 set_array_declarator_type (tree decl, tree type, int abstract_p)
2637 TREE_OPERAND (decl, 0) = type;
2638 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2639 error ("static or type qualifiers in abstract declarator");
2640 return decl;
2643 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2645 tree
2646 groktypename (tree typename)
2648 tree specs, attrs;
2650 if (TREE_CODE (typename) != TREE_LIST)
2651 return typename;
2653 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2655 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2656 NULL);
2658 /* Apply attributes. */
2659 decl_attributes (&typename, attrs, 0);
2661 return typename;
2664 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2666 tree
2667 groktypename_in_parm_context (tree typename)
2669 if (TREE_CODE (typename) != TREE_LIST)
2670 return typename;
2671 return grokdeclarator (TREE_VALUE (typename),
2672 TREE_PURPOSE (typename),
2673 PARM, 0, NULL);
2676 /* Decode a declarator in an ordinary declaration or data definition.
2677 This is called as soon as the type information and variable name
2678 have been parsed, before parsing the initializer if any.
2679 Here we create the ..._DECL node, fill in its type,
2680 and put it on the list of decls for the current context.
2681 The ..._DECL node is returned as the value.
2683 Exception: for arrays where the length is not specified,
2684 the type is left null, to be filled in by `finish_decl'.
2686 Function definitions do not come here; they go to start_function
2687 instead. However, external and forward declarations of functions
2688 do go through here. Structure field declarations are done by
2689 grokfield and not through here. */
2691 tree
2692 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2694 tree decl;
2695 tree tem;
2697 /* An object declared as __attribute__((deprecated)) suppresses
2698 warnings of uses of other deprecated items. */
2699 if (lookup_attribute ("deprecated", attributes))
2700 deprecated_state = DEPRECATED_SUPPRESS;
2702 decl = grokdeclarator (declarator, declspecs,
2703 NORMAL, initialized, NULL);
2705 deprecated_state = DEPRECATED_NORMAL;
2707 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2708 && MAIN_NAME_P (DECL_NAME (decl)))
2709 warning ("%J'%D' is usually a function", decl, decl);
2711 if (initialized)
2712 /* Is it valid for this decl to have an initializer at all?
2713 If not, set INITIALIZED to zero, which will indirectly
2714 tell 'finish_decl' to ignore the initializer once it is parsed. */
2715 switch (TREE_CODE (decl))
2717 case TYPE_DECL:
2718 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2719 initialized = 0;
2720 break;
2722 case FUNCTION_DECL:
2723 error ("function '%D' is initialized like a variable", decl);
2724 initialized = 0;
2725 break;
2727 case PARM_DECL:
2728 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2729 error ("parameter '%D' is initialized", decl);
2730 initialized = 0;
2731 break;
2733 default:
2734 /* Don't allow initializations for incomplete types except for
2735 arrays which might be completed by the initialization. */
2737 /* This can happen if the array size is an undefined macro.
2738 We already gave a warning, so we don't need another one. */
2739 if (TREE_TYPE (decl) == error_mark_node)
2740 initialized = 0;
2741 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2743 /* A complete type is ok if size is fixed. */
2745 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2746 || C_DECL_VARIABLE_SIZE (decl))
2748 error ("variable-sized object may not be initialized");
2749 initialized = 0;
2752 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2754 error ("variable '%D' has initializer but incomplete type", decl);
2755 initialized = 0;
2757 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2759 error ("elements of array '%D' have incomplete type", decl);
2760 initialized = 0;
2764 if (initialized)
2766 DECL_EXTERNAL (decl) = 0;
2767 if (current_scope == file_scope)
2768 TREE_STATIC (decl) = 1;
2770 /* Tell 'pushdecl' this is an initialized decl
2771 even though we don't yet have the initializer expression.
2772 Also tell 'finish_decl' it may store the real initializer. */
2773 DECL_INITIAL (decl) = error_mark_node;
2776 /* If this is a function declaration, write a record describing it to the
2777 prototypes file (if requested). */
2779 if (TREE_CODE (decl) == FUNCTION_DECL)
2780 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2782 /* ANSI specifies that a tentative definition which is not merged with
2783 a non-tentative definition behaves exactly like a definition with an
2784 initializer equal to zero. (Section 3.7.2)
2786 -fno-common gives strict ANSI behavior, though this tends to break
2787 a large body of code that grew up without this rule.
2789 Thread-local variables are never common, since there's no entrenched
2790 body of code to break, and it allows more efficient variable references
2791 in the presence of dynamic linking. */
2793 if (TREE_CODE (decl) == VAR_DECL
2794 && !initialized
2795 && TREE_PUBLIC (decl)
2796 && !DECL_THREAD_LOCAL (decl)
2797 && !flag_no_common)
2798 DECL_COMMON (decl) = 1;
2800 /* Set attributes here so if duplicate decl, will have proper attributes. */
2801 decl_attributes (&decl, attributes, 0);
2803 if (TREE_CODE (decl) == FUNCTION_DECL
2804 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2806 tree ce = declarator;
2808 if (TREE_CODE (ce) == INDIRECT_REF)
2809 ce = TREE_OPERAND (declarator, 0);
2810 if (TREE_CODE (ce) == CALL_EXPR)
2812 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2813 for (; args; args = TREE_CHAIN (args))
2815 tree type = TREE_TYPE (args);
2816 if (type && INTEGRAL_TYPE_P (type)
2817 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2818 DECL_ARG_TYPE (args) = integer_type_node;
2823 if (TREE_CODE (decl) == FUNCTION_DECL
2824 && DECL_DECLARED_INLINE_P (decl)
2825 && DECL_UNINLINABLE (decl)
2826 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2827 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2829 /* Add this decl to the current scope.
2830 TEM may equal DECL or it may be a previous decl of the same name. */
2831 tem = pushdecl (decl);
2833 return tem;
2836 /* Finish processing of a declaration;
2837 install its initial value.
2838 If the length of an array type is not known before,
2839 it must be determined now, from the initial value, or it is an error. */
2841 void
2842 finish_decl (tree decl, tree init, tree asmspec_tree)
2844 tree type = TREE_TYPE (decl);
2845 int was_incomplete = (DECL_SIZE (decl) == 0);
2846 const char *asmspec = 0;
2848 /* If a name was specified, get the string. */
2849 if (current_scope == file_scope)
2850 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2851 if (asmspec_tree)
2852 asmspec = TREE_STRING_POINTER (asmspec_tree);
2854 /* If `start_decl' didn't like having an initialization, ignore it now. */
2855 if (init != 0 && DECL_INITIAL (decl) == 0)
2856 init = 0;
2858 /* Don't crash if parm is initialized. */
2859 if (TREE_CODE (decl) == PARM_DECL)
2860 init = 0;
2862 if (init)
2863 store_init_value (decl, init);
2865 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2866 || TREE_CODE (decl) == FUNCTION_DECL
2867 || TREE_CODE (decl) == FIELD_DECL))
2868 objc_check_decl (decl);
2870 /* Deduce size of array from initialization, if not already known. */
2871 if (TREE_CODE (type) == ARRAY_TYPE
2872 && TYPE_DOMAIN (type) == 0
2873 && TREE_CODE (decl) != TYPE_DECL)
2875 int do_default
2876 = (TREE_STATIC (decl)
2877 /* Even if pedantic, an external linkage array
2878 may have incomplete type at first. */
2879 ? pedantic && !TREE_PUBLIC (decl)
2880 : !DECL_EXTERNAL (decl));
2881 int failure
2882 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2884 /* Get the completed type made by complete_array_type. */
2885 type = TREE_TYPE (decl);
2887 if (failure == 1)
2888 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2890 else if (failure == 2)
2892 if (do_default)
2893 error ("%Jarray size missing in '%D'", decl, decl);
2894 /* If a `static' var's size isn't known,
2895 make it extern as well as static, so it does not get
2896 allocated.
2897 If it is not `static', then do not mark extern;
2898 finish_incomplete_decl will give it a default size
2899 and it will get allocated. */
2900 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2901 DECL_EXTERNAL (decl) = 1;
2904 /* TYPE_MAX_VALUE is always one less than the number of elements
2905 in the array, because we start counting at zero. Therefore,
2906 warn only if the value is less than zero. */
2907 else if (pedantic && TYPE_DOMAIN (type) != 0
2908 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2909 error ("%Jzero or negative size array '%D'", decl, decl);
2911 layout_decl (decl, 0);
2914 if (TREE_CODE (decl) == VAR_DECL)
2916 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2917 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2918 layout_decl (decl, 0);
2920 if (DECL_SIZE (decl) == 0
2921 /* Don't give an error if we already gave one earlier. */
2922 && TREE_TYPE (decl) != error_mark_node
2923 && (TREE_STATIC (decl)
2925 /* A static variable with an incomplete type
2926 is an error if it is initialized.
2927 Also if it is not file scope.
2928 Otherwise, let it through, but if it is not `extern'
2929 then it may cause an error message later. */
2930 (DECL_INITIAL (decl) != 0
2931 || !DECL_FILE_SCOPE_P (decl))
2933 /* An automatic variable with an incomplete type
2934 is an error. */
2935 !DECL_EXTERNAL (decl)))
2937 error ("%Jstorage size of '%D' isn't known", decl, decl);
2938 TREE_TYPE (decl) = error_mark_node;
2941 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2942 && DECL_SIZE (decl) != 0)
2944 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2945 constant_expression_warning (DECL_SIZE (decl));
2946 else
2947 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2950 if (TREE_USED (type))
2951 TREE_USED (decl) = 1;
2954 /* If this is a function and an assembler name is specified, reset DECL_RTL
2955 so we can give it its new name. Also, update built_in_decls if it
2956 was a normal built-in. */
2957 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2959 /* ASMSPEC is given, and not the name of a register. Mark the
2960 name with a star so assemble_name won't munge it. */
2961 char *starred = alloca (strlen (asmspec) + 2);
2962 starred[0] = '*';
2963 strcpy (starred + 1, asmspec);
2965 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2967 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2968 SET_DECL_RTL (builtin, NULL_RTX);
2969 change_decl_assembler_name (builtin, get_identifier (starred));
2970 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2971 init_block_move_fn (starred);
2972 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2973 init_block_clear_fn (starred);
2975 SET_DECL_RTL (decl, NULL_RTX);
2976 change_decl_assembler_name (decl, get_identifier (starred));
2979 /* If #pragma weak was used, mark the decl weak now. */
2980 if (current_scope == file_scope)
2981 maybe_apply_pragma_weak (decl);
2983 /* Output the assembler code and/or RTL code for variables and functions,
2984 unless the type is an undefined structure or union.
2985 If not, it will get done when the type is completed. */
2987 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2989 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2990 if (c_dialect_objc ())
2991 objc_check_decl (decl);
2993 if (DECL_FILE_SCOPE_P (decl))
2995 if (DECL_INITIAL (decl) == NULL_TREE
2996 || DECL_INITIAL (decl) == error_mark_node)
2997 /* Don't output anything
2998 when a tentative file-scope definition is seen.
2999 But at end of compilation, do output code for them. */
3000 DECL_DEFER_OUTPUT (decl) = 1;
3001 rest_of_decl_compilation (decl, asmspec, true, 0);
3003 else
3005 /* This is a local variable. If there is an ASMSPEC, the
3006 user has requested that we handle it specially. */
3007 if (asmspec)
3009 /* In conjunction with an ASMSPEC, the `register'
3010 keyword indicates that we should place the variable
3011 in a particular register. */
3012 if (C_DECL_REGISTER (decl))
3014 DECL_HARD_REGISTER (decl) = 1;
3015 /* This cannot be done for a structure with volatile
3016 fields, on which DECL_REGISTER will have been
3017 reset. */
3018 if (!DECL_REGISTER (decl))
3019 error ("cannot put object with volatile field into register");
3022 /* If this is not a static variable, issue a warning.
3023 It doesn't make any sense to give an ASMSPEC for an
3024 ordinary, non-register local variable. Historically,
3025 GCC has accepted -- but ignored -- the ASMSPEC in
3026 this case. */
3027 if (TREE_CODE (decl) == VAR_DECL
3028 && !C_DECL_REGISTER (decl)
3029 && !TREE_STATIC (decl))
3030 warning ("%Jignoring asm-specifier for non-static local "
3031 "variable '%D'", decl, decl);
3032 else
3033 change_decl_assembler_name (decl, get_identifier (asmspec));
3036 if (TREE_CODE (decl) != FUNCTION_DECL)
3037 add_stmt (build_stmt (DECL_EXPR, decl));
3040 if (!DECL_FILE_SCOPE_P (decl))
3042 /* Recompute the RTL of a local array now
3043 if it used to be an incomplete type. */
3044 if (was_incomplete
3045 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3047 /* If we used it already as memory, it must stay in memory. */
3048 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3049 /* If it's still incomplete now, no init will save it. */
3050 if (DECL_SIZE (decl) == 0)
3051 DECL_INITIAL (decl) = 0;
3056 /* If this was marked 'used', be sure it will be output. */
3057 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3058 mark_decl_referenced (decl);
3060 if (TREE_CODE (decl) == TYPE_DECL)
3062 if (!DECL_FILE_SCOPE_P (decl)
3063 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3064 add_stmt (build_stmt (DECL_EXPR, decl));
3066 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3069 /* At the end of a declaration, throw away any variable type sizes
3070 of types defined inside that declaration. There is no use
3071 computing them in the following function definition. */
3072 if (current_scope == file_scope)
3073 get_pending_sizes ();
3075 /* Install a cleanup (aka destructor) if one was given. */
3076 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3078 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3079 if (attr)
3081 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3082 tree cleanup_decl = lookup_name (cleanup_id);
3083 tree cleanup;
3085 /* Build "cleanup(&decl)" for the destructor. */
3086 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3087 cleanup = build_tree_list (NULL_TREE, cleanup);
3088 cleanup = build_function_call (cleanup_decl, cleanup);
3090 /* Don't warn about decl unused; the cleanup uses it. */
3091 TREE_USED (decl) = 1;
3092 TREE_USED (cleanup_decl) = 1;
3094 /* Initialize EH, if we've been told to do so. */
3095 if (flag_exceptions && !c_eh_initialized_p)
3097 c_eh_initialized_p = true;
3098 eh_personality_libfunc
3099 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3100 ? "__gcc_personality_sj0"
3101 : "__gcc_personality_v0");
3102 using_eh_for_cleanups ();
3105 push_cleanup (decl, cleanup, false);
3110 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3111 and push that on the current scope. */
3113 void
3114 push_parm_decl (tree parm)
3116 tree decl;
3118 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3119 TREE_PURPOSE (TREE_PURPOSE (parm)),
3120 PARM, 0, NULL);
3121 decl_attributes (&decl, TREE_VALUE (parm), 0);
3123 decl = pushdecl (decl);
3125 finish_decl (decl, NULL_TREE, NULL_TREE);
3128 /* Mark all the parameter declarations to date as forward decls.
3129 Also diagnose use of this extension. */
3131 void
3132 mark_forward_parm_decls (void)
3134 struct c_binding *b;
3136 if (pedantic && !current_scope->warned_forward_parm_decls)
3138 pedwarn ("ISO C forbids forward parameter declarations");
3139 current_scope->warned_forward_parm_decls = true;
3142 for (b = current_scope->bindings; b; b = b->prev)
3143 if (TREE_CODE (b->decl) == PARM_DECL)
3144 TREE_ASM_WRITTEN (b->decl) = 1;
3147 static GTY(()) int compound_literal_number;
3149 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3150 literal, which may be an incomplete array type completed by the
3151 initializer; INIT is a CONSTRUCTOR that initializes the compound
3152 literal. */
3154 tree
3155 build_compound_literal (tree type, tree init)
3157 /* We do not use start_decl here because we have a type, not a declarator;
3158 and do not use finish_decl because the decl should be stored inside
3159 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3160 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3161 tree complit;
3162 tree stmt;
3163 DECL_EXTERNAL (decl) = 0;
3164 TREE_PUBLIC (decl) = 0;
3165 TREE_STATIC (decl) = (current_scope == file_scope);
3166 DECL_CONTEXT (decl) = current_function_decl;
3167 TREE_USED (decl) = 1;
3168 TREE_TYPE (decl) = type;
3169 TREE_READONLY (decl) = TYPE_READONLY (type);
3170 store_init_value (decl, init);
3172 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3174 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3175 if (failure)
3176 abort ();
3179 type = TREE_TYPE (decl);
3180 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3181 return error_mark_node;
3183 stmt = build_stmt (DECL_EXPR, decl);
3184 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3185 TREE_SIDE_EFFECTS (complit) = 1;
3187 layout_decl (decl, 0);
3189 if (TREE_STATIC (decl))
3191 /* This decl needs a name for the assembler output. We also need
3192 a unique suffix to be added to the name. */
3193 char *name;
3195 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3196 compound_literal_number);
3197 compound_literal_number++;
3198 DECL_NAME (decl) = get_identifier (name);
3199 DECL_DEFER_OUTPUT (decl) = 1;
3200 DECL_COMDAT (decl) = 1;
3201 DECL_ARTIFICIAL (decl) = 1;
3202 pushdecl (decl);
3203 rest_of_decl_compilation (decl, NULL, 1, 0);
3206 return complit;
3209 /* Make TYPE a complete type based on INITIAL_VALUE.
3210 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3211 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3214 complete_array_type (tree type, tree initial_value, int do_default)
3216 tree maxindex = NULL_TREE;
3217 int value = 0;
3219 if (initial_value)
3221 /* Note MAXINDEX is really the maximum index,
3222 one less than the size. */
3223 if (TREE_CODE (initial_value) == STRING_CST)
3225 int eltsize
3226 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3227 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3228 / eltsize) - 1, 0);
3230 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3232 tree elts = CONSTRUCTOR_ELTS (initial_value);
3233 maxindex = build_int_2 (-1, -1);
3234 for (; elts; elts = TREE_CHAIN (elts))
3236 if (TREE_PURPOSE (elts))
3237 maxindex = TREE_PURPOSE (elts);
3238 else
3239 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3240 maxindex, integer_one_node));
3242 maxindex = copy_node (maxindex);
3244 else
3246 /* Make an error message unless that happened already. */
3247 if (initial_value != error_mark_node)
3248 value = 1;
3250 /* Prevent further error messages. */
3251 maxindex = build_int_2 (0, 0);
3255 if (!maxindex)
3257 if (do_default)
3258 maxindex = build_int_2 (0, 0);
3259 value = 2;
3262 if (maxindex)
3264 TYPE_DOMAIN (type) = build_index_type (maxindex);
3265 if (!TREE_TYPE (maxindex))
3266 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3269 /* Lay out the type now that we can get the real answer. */
3271 layout_type (type);
3273 return value;
3276 /* Determine whether TYPE is a structure with a flexible array member,
3277 or a union containing such a structure (possibly recursively). */
3279 static bool
3280 flexible_array_type_p (tree type)
3282 tree x;
3283 switch (TREE_CODE (type))
3285 case RECORD_TYPE:
3286 x = TYPE_FIELDS (type);
3287 if (x == NULL_TREE)
3288 return false;
3289 while (TREE_CHAIN (x) != NULL_TREE)
3290 x = TREE_CHAIN (x);
3291 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3292 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3293 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3294 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3295 return true;
3296 return false;
3297 case UNION_TYPE:
3298 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3300 if (flexible_array_type_p (TREE_TYPE (x)))
3301 return true;
3303 return false;
3304 default:
3305 return false;
3309 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3310 replacing with appropriate values if they are invalid. */
3311 static void
3312 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3314 tree type_mv;
3315 unsigned int max_width;
3316 unsigned HOST_WIDE_INT w;
3317 const char *name = orig_name ? orig_name: _("<anonymous>");
3319 /* Necessary? */
3320 STRIP_NOPS (*width);
3322 /* Detect and ignore out of range field width and process valid
3323 field widths. */
3324 if (TREE_CODE (*width) != INTEGER_CST)
3326 error ("bit-field `%s' width not an integer constant", name);
3327 *width = integer_one_node;
3329 else
3331 constant_expression_warning (*width);
3332 if (tree_int_cst_sgn (*width) < 0)
3334 error ("negative width in bit-field `%s'", name);
3335 *width = integer_one_node;
3337 else if (integer_zerop (*width) && orig_name)
3339 error ("zero width for bit-field `%s'", name);
3340 *width = integer_one_node;
3344 /* Detect invalid bit-field type. */
3345 if (TREE_CODE (*type) != INTEGER_TYPE
3346 && TREE_CODE (*type) != BOOLEAN_TYPE
3347 && TREE_CODE (*type) != ENUMERAL_TYPE)
3349 error ("bit-field `%s' has invalid type", name);
3350 *type = unsigned_type_node;
3353 type_mv = TYPE_MAIN_VARIANT (*type);
3354 if (pedantic
3355 && type_mv != integer_type_node
3356 && type_mv != unsigned_type_node
3357 && type_mv != boolean_type_node)
3358 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3360 if (type_mv == boolean_type_node)
3361 max_width = CHAR_TYPE_SIZE;
3362 else
3363 max_width = TYPE_PRECISION (*type);
3365 if (0 < compare_tree_int (*width, max_width))
3367 error ("width of `%s' exceeds its type", name);
3368 w = max_width;
3369 *width = build_int_2 (w, 0);
3371 else
3372 w = tree_low_cst (*width, 1);
3374 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3376 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3377 if (!lt
3378 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3379 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3380 warning ("`%s' is narrower than values of its type", name);
3384 /* Given declspecs and a declarator,
3385 determine the name and type of the object declared
3386 and construct a ..._DECL node for it.
3387 (In one case we can return a ..._TYPE node instead.
3388 For invalid input we sometimes return 0.)
3390 DECLSPECS is a chain of tree_list nodes whose value fields
3391 are the storage classes and type specifiers.
3393 DECL_CONTEXT says which syntactic context this declaration is in:
3394 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3395 FUNCDEF for a function definition. Like NORMAL but a few different
3396 error messages in each case. Return value may be zero meaning
3397 this definition is too screwy to try to parse.
3398 PARM for a parameter declaration (either within a function prototype
3399 or before a function body). Make a PARM_DECL, or return void_type_node.
3400 TYPENAME if for a typename (in a cast or sizeof).
3401 Don't make a DECL node; just return the ..._TYPE node.
3402 FIELD for a struct or union field; make a FIELD_DECL.
3403 INITIALIZED is 1 if the decl has an initializer.
3404 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3405 representing the width of the bit-field.
3407 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3408 It may also be so in the PARM case, for a prototype where the
3409 argument type is specified but not the name.
3411 This function is where the complicated C meanings of `static'
3412 and `extern' are interpreted. */
3414 static tree
3415 grokdeclarator (tree declarator, tree declspecs,
3416 enum decl_context decl_context, int initialized, tree *width)
3418 int specbits = 0;
3419 tree spec;
3420 tree type = NULL_TREE;
3421 int longlong = 0;
3422 int constp;
3423 int restrictp;
3424 int volatilep;
3425 int type_quals = TYPE_UNQUALIFIED;
3426 int inlinep;
3427 int explicit_int = 0;
3428 int explicit_char = 0;
3429 int defaulted_int = 0;
3430 tree typedef_decl = 0;
3431 const char *name, *orig_name;
3432 tree typedef_type = 0;
3433 int funcdef_flag = 0;
3434 enum tree_code innermost_code = ERROR_MARK;
3435 int size_varies = 0;
3436 tree decl_attr = NULL_TREE;
3437 tree array_ptr_quals = NULL_TREE;
3438 int array_parm_static = 0;
3439 tree returned_attrs = NULL_TREE;
3440 bool bitfield = width != NULL;
3441 tree element_type;
3442 tree arg_info = NULL_TREE;
3444 if (decl_context == FUNCDEF)
3445 funcdef_flag = 1, decl_context = NORMAL;
3447 /* Look inside a declarator for the name being declared
3448 and get it as a string, for an error message. */
3450 tree decl = declarator;
3451 name = 0;
3453 while (decl)
3454 switch (TREE_CODE (decl))
3456 case ARRAY_REF:
3457 case INDIRECT_REF:
3458 case CALL_EXPR:
3459 innermost_code = TREE_CODE (decl);
3460 decl = TREE_OPERAND (decl, 0);
3461 break;
3463 case TREE_LIST:
3464 decl = TREE_VALUE (decl);
3465 break;
3467 case IDENTIFIER_NODE:
3468 name = IDENTIFIER_POINTER (decl);
3469 decl = 0;
3470 break;
3472 default:
3473 abort ();
3475 orig_name = name;
3476 if (name == 0)
3477 name = "type name";
3480 /* A function definition's declarator must have the form of
3481 a function declarator. */
3483 if (funcdef_flag && innermost_code != CALL_EXPR)
3484 return 0;
3486 /* If this looks like a function definition, make it one,
3487 even if it occurs where parms are expected.
3488 Then store_parm_decls will reject it and not use it as a parm. */
3489 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3490 decl_context = PARM;
3492 /* Look through the decl specs and record which ones appear.
3493 Some typespecs are defined as built-in typenames.
3494 Others, the ones that are modifiers of other types,
3495 are represented by bits in SPECBITS: set the bits for
3496 the modifiers that appear. Storage class keywords are also in SPECBITS.
3498 If there is a typedef name or a type, store the type in TYPE.
3499 This includes builtin typedefs such as `int'.
3501 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3502 and did not come from a user typedef.
3504 Set LONGLONG if `long' is mentioned twice. */
3506 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3508 tree id = TREE_VALUE (spec);
3510 /* If the entire declaration is itself tagged as deprecated then
3511 suppress reports of deprecated items. */
3512 if (id && TREE_DEPRECATED (id))
3514 if (deprecated_state != DEPRECATED_SUPPRESS)
3515 warn_deprecated_use (id);
3518 if (id == ridpointers[(int) RID_INT])
3519 explicit_int = 1;
3520 if (id == ridpointers[(int) RID_CHAR])
3521 explicit_char = 1;
3523 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3525 enum rid i = C_RID_CODE (id);
3526 if ((int) i <= (int) RID_LAST_MODIFIER)
3528 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3530 if (longlong)
3531 error ("`long long long' is too long for GCC");
3532 else
3534 if (pedantic && !flag_isoc99 && ! in_system_header
3535 && warn_long_long)
3536 pedwarn ("ISO C90 does not support `long long'");
3537 longlong = 1;
3540 else if (specbits & (1 << (int) i))
3542 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3544 if (pedantic && !flag_isoc99)
3545 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3547 else
3548 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3551 /* Diagnose "__thread extern". Recall that this list
3552 is in the reverse order seen in the text. */
3553 if (i == RID_THREAD
3554 && (specbits & (1 << (int) RID_EXTERN
3555 | 1 << (int) RID_STATIC)))
3557 if (specbits & 1 << (int) RID_EXTERN)
3558 error ("`__thread' before `extern'");
3559 else
3560 error ("`__thread' before `static'");
3563 specbits |= 1 << (int) i;
3564 goto found;
3567 if (type)
3568 error ("two or more data types in declaration of `%s'", name);
3569 /* Actual typedefs come to us as TYPE_DECL nodes. */
3570 else if (TREE_CODE (id) == TYPE_DECL)
3572 if (TREE_TYPE (id) == error_mark_node)
3573 ; /* Allow the type to default to int to avoid cascading errors. */
3574 else
3576 type = TREE_TYPE (id);
3577 decl_attr = DECL_ATTRIBUTES (id);
3578 typedef_decl = id;
3581 /* Built-in types come as identifiers. */
3582 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3584 tree t = lookup_name (id);
3585 if (!t || TREE_CODE (t) != TYPE_DECL)
3586 error ("`%s' fails to be a typedef or built in type",
3587 IDENTIFIER_POINTER (id));
3588 else if (TREE_TYPE (t) == error_mark_node)
3590 else
3592 type = TREE_TYPE (t);
3593 typedef_decl = t;
3596 else if (TREE_CODE (id) != ERROR_MARK)
3597 type = id;
3599 found:
3603 typedef_type = type;
3604 if (type)
3605 size_varies = C_TYPE_VARIABLE_SIZE (type);
3607 /* No type at all: default to `int', and set DEFAULTED_INT
3608 because it was not a user-defined typedef. */
3610 if (type == 0)
3612 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3613 | (1 << (int) RID_SIGNED)
3614 | (1 << (int) RID_UNSIGNED)
3615 | (1 << (int) RID_COMPLEX))))
3616 /* Don't warn about typedef foo = bar. */
3617 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3618 && ! in_system_header)
3620 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3621 and this is a function, or if -Wimplicit; prefer the former
3622 warning since it is more explicit. */
3623 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3624 && funcdef_flag)
3625 warn_about_return_type = 1;
3626 else if (warn_implicit_int || flag_isoc99)
3627 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3628 name);
3631 defaulted_int = 1;
3632 type = integer_type_node;
3635 /* Now process the modifiers that were specified
3636 and check for invalid combinations. */
3638 /* Long double is a special combination. */
3640 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3641 && TYPE_MAIN_VARIANT (type) == double_type_node)
3643 specbits &= ~(1 << (int) RID_LONG);
3644 type = long_double_type_node;
3647 /* Check all other uses of type modifiers. */
3649 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3650 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3652 int ok = 0;
3654 if ((specbits & 1 << (int) RID_LONG)
3655 && (specbits & 1 << (int) RID_SHORT))
3656 error ("both long and short specified for `%s'", name);
3657 else if (((specbits & 1 << (int) RID_LONG)
3658 || (specbits & 1 << (int) RID_SHORT))
3659 && explicit_char)
3660 error ("long or short specified with char for `%s'", name);
3661 else if (((specbits & 1 << (int) RID_LONG)
3662 || (specbits & 1 << (int) RID_SHORT))
3663 && TREE_CODE (type) == REAL_TYPE)
3665 static int already = 0;
3667 error ("long or short specified with floating type for `%s'", name);
3668 if (! already && ! pedantic)
3670 error ("the only valid combination is `long double'");
3671 already = 1;
3674 else if ((specbits & 1 << (int) RID_SIGNED)
3675 && (specbits & 1 << (int) RID_UNSIGNED))
3676 error ("both signed and unsigned specified for `%s'", name);
3677 else if (TREE_CODE (type) != INTEGER_TYPE)
3678 error ("long, short, signed or unsigned invalid for `%s'", name);
3679 else
3681 ok = 1;
3682 if (!explicit_int && !defaulted_int && !explicit_char)
3684 error ("long, short, signed or unsigned used invalidly for `%s'",
3685 name);
3686 ok = 0;
3690 /* Discard the type modifiers if they are invalid. */
3691 if (! ok)
3693 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3694 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3695 longlong = 0;
3699 if ((specbits & (1 << (int) RID_COMPLEX))
3700 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3702 error ("complex invalid for `%s'", name);
3703 specbits &= ~(1 << (int) RID_COMPLEX);
3706 /* Decide whether an integer type is signed or not.
3707 Optionally treat bit-fields as signed by default. */
3708 if (specbits & 1 << (int) RID_UNSIGNED
3709 || (bitfield && ! flag_signed_bitfields
3710 && (explicit_int || defaulted_int || explicit_char
3711 /* A typedef for plain `int' without `signed'
3712 can be controlled just like plain `int'. */
3713 || ! (typedef_decl != 0
3714 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3715 && TREE_CODE (type) != ENUMERAL_TYPE
3716 && !(specbits & 1 << (int) RID_SIGNED)))
3718 if (longlong)
3719 type = long_long_unsigned_type_node;
3720 else if (specbits & 1 << (int) RID_LONG)
3721 type = long_unsigned_type_node;
3722 else if (specbits & 1 << (int) RID_SHORT)
3723 type = short_unsigned_type_node;
3724 else if (type == char_type_node)
3725 type = unsigned_char_type_node;
3726 else if (typedef_decl)
3727 type = c_common_unsigned_type (type);
3728 else
3729 type = unsigned_type_node;
3731 else if ((specbits & 1 << (int) RID_SIGNED)
3732 && type == char_type_node)
3733 type = signed_char_type_node;
3734 else if (longlong)
3735 type = long_long_integer_type_node;
3736 else if (specbits & 1 << (int) RID_LONG)
3737 type = long_integer_type_node;
3738 else if (specbits & 1 << (int) RID_SHORT)
3739 type = short_integer_type_node;
3741 if (specbits & 1 << (int) RID_COMPLEX)
3743 if (pedantic && !flag_isoc99)
3744 pedwarn ("ISO C90 does not support complex types");
3745 /* If we just have "complex", it is equivalent to
3746 "complex double", but if any modifiers at all are specified it is
3747 the complex form of TYPE. E.g, "complex short" is
3748 "complex short int". */
3750 if (defaulted_int && ! longlong
3751 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3752 | (1 << (int) RID_SIGNED)
3753 | (1 << (int) RID_UNSIGNED))))
3755 if (pedantic)
3756 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3757 type = complex_double_type_node;
3759 else if (type == integer_type_node)
3761 if (pedantic)
3762 pedwarn ("ISO C does not support complex integer types");
3763 type = complex_integer_type_node;
3765 else if (type == float_type_node)
3766 type = complex_float_type_node;
3767 else if (type == double_type_node)
3768 type = complex_double_type_node;
3769 else if (type == long_double_type_node)
3770 type = complex_long_double_type_node;
3771 else
3773 if (pedantic)
3774 pedwarn ("ISO C does not support complex integer types");
3775 type = build_complex_type (type);
3779 /* Check the type and width of a bit-field. */
3780 if (bitfield)
3781 check_bitfield_type_and_width (&type, width, orig_name);
3783 /* Figure out the type qualifiers for the declaration. There are
3784 two ways a declaration can become qualified. One is something
3785 like `const int i' where the `const' is explicit. Another is
3786 something like `typedef const int CI; CI i' where the type of the
3787 declaration contains the `const'. A third possibility is that
3788 there is a type qualifier on the element type of a typedefed
3789 array type, in which case we should extract that qualifier so
3790 that c_apply_type_quals_to_decls receives the full list of
3791 qualifiers to work with (C90 is not entirely clear about whether
3792 duplicate qualifiers should be diagnosed in this case, but it
3793 seems most appropriate to do so). */
3794 element_type = strip_array_types (type);
3795 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3796 restrictp
3797 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3798 volatilep
3799 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3800 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3801 if (pedantic && !flag_isoc99)
3803 if (constp > 1)
3804 pedwarn ("duplicate `const'");
3805 if (restrictp > 1)
3806 pedwarn ("duplicate `restrict'");
3807 if (volatilep > 1)
3808 pedwarn ("duplicate `volatile'");
3810 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3811 type = TYPE_MAIN_VARIANT (type);
3812 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3813 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3814 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3816 /* Warn if two storage classes are given. Default to `auto'. */
3819 int nclasses = 0;
3821 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3822 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3823 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3824 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3825 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3827 /* "static __thread" and "extern __thread" are allowed. */
3828 if ((specbits & (1 << (int) RID_THREAD
3829 | 1 << (int) RID_STATIC
3830 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3831 nclasses++;
3833 /* Warn about storage classes that are invalid for certain
3834 kinds of declarations (parameters, typenames, etc.). */
3836 if (nclasses > 1)
3837 error ("multiple storage classes in declaration of `%s'", name);
3838 else if (funcdef_flag
3839 && (specbits
3840 & ((1 << (int) RID_REGISTER)
3841 | (1 << (int) RID_AUTO)
3842 | (1 << (int) RID_TYPEDEF)
3843 | (1 << (int) RID_THREAD))))
3845 if (specbits & 1 << (int) RID_AUTO
3846 && (pedantic || current_scope == file_scope))
3847 pedwarn ("function definition declared `auto'");
3848 if (specbits & 1 << (int) RID_REGISTER)
3849 error ("function definition declared `register'");
3850 if (specbits & 1 << (int) RID_TYPEDEF)
3851 error ("function definition declared `typedef'");
3852 if (specbits & 1 << (int) RID_THREAD)
3853 error ("function definition declared `__thread'");
3854 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3855 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3857 else if (decl_context != NORMAL && nclasses > 0)
3859 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3861 else
3863 switch (decl_context)
3865 case FIELD:
3866 error ("storage class specified for structure field `%s'",
3867 name);
3868 break;
3869 case PARM:
3870 error ("storage class specified for parameter `%s'", name);
3871 break;
3872 default:
3873 error ("storage class specified for typename");
3874 break;
3876 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3877 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3878 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3881 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3883 /* `extern' with initialization is invalid if not at file scope. */
3884 if (current_scope == file_scope)
3885 warning ("`%s' initialized and declared `extern'", name);
3886 else
3887 error ("`%s' has both `extern' and initializer", name);
3889 else if (current_scope == file_scope)
3891 if (specbits & 1 << (int) RID_AUTO)
3892 error ("file-scope declaration of `%s' specifies `auto'", name);
3894 else
3896 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3897 error ("nested function `%s' declared `extern'", name);
3898 else if ((specbits & (1 << (int) RID_THREAD
3899 | 1 << (int) RID_EXTERN
3900 | 1 << (int) RID_STATIC))
3901 == (1 << (int) RID_THREAD))
3903 error ("function-scope `%s' implicitly auto and declared `__thread'",
3904 name);
3905 specbits &= ~(1 << (int) RID_THREAD);
3910 /* Now figure out the structure of the declarator proper.
3911 Descend through it, creating more complex types, until we reach
3912 the declared identifier (or NULL_TREE, in an absolute declarator). */
3914 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3916 if (type == error_mark_node)
3918 declarator = TREE_OPERAND (declarator, 0);
3919 continue;
3922 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3923 an INDIRECT_REF (for *...),
3924 a CALL_EXPR (for ...(...)),
3925 a TREE_LIST (for nested attributes),
3926 an identifier (for the name being declared)
3927 or a null pointer (for the place in an absolute declarator
3928 where the name was omitted).
3929 For the last two cases, we have just exited the loop.
3931 At this point, TYPE is the type of elements of an array,
3932 or for a function to return, or for a pointer to point to.
3933 After this sequence of ifs, TYPE is the type of the
3934 array or function or pointer, and DECLARATOR has had its
3935 outermost layer removed. */
3937 if (array_ptr_quals != NULL_TREE || array_parm_static)
3939 /* Only the innermost declarator (making a parameter be of
3940 array type which is converted to pointer type)
3941 may have static or type qualifiers. */
3942 error ("static or type qualifiers in non-parameter array declarator");
3943 array_ptr_quals = NULL_TREE;
3944 array_parm_static = 0;
3947 if (TREE_CODE (declarator) == TREE_LIST)
3949 /* We encode a declarator with embedded attributes using
3950 a TREE_LIST. */
3951 tree attrs = TREE_PURPOSE (declarator);
3952 tree inner_decl;
3953 int attr_flags = 0;
3954 declarator = TREE_VALUE (declarator);
3955 inner_decl = declarator;
3956 while (inner_decl != NULL_TREE
3957 && TREE_CODE (inner_decl) == TREE_LIST)
3958 inner_decl = TREE_VALUE (inner_decl);
3959 if (inner_decl == NULL_TREE
3960 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3961 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3962 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3963 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3964 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3965 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3966 returned_attrs = decl_attributes (&type,
3967 chainon (returned_attrs, attrs),
3968 attr_flags);
3970 else if (TREE_CODE (declarator) == ARRAY_REF)
3972 tree itype = NULL_TREE;
3973 tree size = TREE_OPERAND (declarator, 1);
3974 /* The index is a signed object `sizetype' bits wide. */
3975 tree index_type = c_common_signed_type (sizetype);
3977 array_ptr_quals = TREE_TYPE (declarator);
3978 array_parm_static = TREE_STATIC (declarator);
3980 declarator = TREE_OPERAND (declarator, 0);
3982 /* Check for some types that there cannot be arrays of. */
3984 if (VOID_TYPE_P (type))
3986 error ("declaration of `%s' as array of voids", name);
3987 type = error_mark_node;
3990 if (TREE_CODE (type) == FUNCTION_TYPE)
3992 error ("declaration of `%s' as array of functions", name);
3993 type = error_mark_node;
3996 if (pedantic && !in_system_header && flexible_array_type_p (type))
3997 pedwarn ("invalid use of structure with flexible array member");
3999 if (size == error_mark_node)
4000 type = error_mark_node;
4002 if (type == error_mark_node)
4003 continue;
4005 /* If size was specified, set ITYPE to a range-type for that size.
4006 Otherwise, ITYPE remains null. finish_decl may figure it out
4007 from an initial value. */
4009 if (size)
4011 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4012 STRIP_TYPE_NOPS (size);
4014 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4016 error ("size of array `%s' has non-integer type", name);
4017 size = integer_one_node;
4020 if (pedantic && integer_zerop (size))
4021 pedwarn ("ISO C forbids zero-size array `%s'", name);
4023 if (TREE_CODE (size) == INTEGER_CST)
4025 constant_expression_warning (size);
4026 if (tree_int_cst_sgn (size) < 0)
4028 error ("size of array `%s' is negative", name);
4029 size = integer_one_node;
4032 else
4034 /* Make sure the array size remains visibly nonconstant
4035 even if it is (eg) a const variable with known value. */
4036 size_varies = 1;
4038 if (!flag_isoc99 && pedantic)
4040 if (TREE_CONSTANT (size))
4041 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4042 name);
4043 else
4044 pedwarn ("ISO C90 forbids variable-size array `%s'",
4045 name);
4049 if (integer_zerop (size))
4051 /* A zero-length array cannot be represented with an
4052 unsigned index type, which is what we'll get with
4053 build_index_type. Create an open-ended range instead. */
4054 itype = build_range_type (sizetype, size, NULL_TREE);
4056 else
4058 /* Compute the maximum valid index, that is, size - 1.
4059 Do the calculation in index_type, so that if it is
4060 a variable the computations will be done in the
4061 proper mode. */
4062 itype = fold (build (MINUS_EXPR, index_type,
4063 convert (index_type, size),
4064 convert (index_type, size_one_node)));
4066 /* If that overflowed, the array is too big.
4067 ??? While a size of INT_MAX+1 technically shouldn't
4068 cause an overflow (because we subtract 1), the overflow
4069 is recorded during the conversion to index_type, before
4070 the subtraction. Handling this case seems like an
4071 unnecessary complication. */
4072 if (TREE_OVERFLOW (itype))
4074 error ("size of array `%s' is too large", name);
4075 type = error_mark_node;
4076 continue;
4079 if (size_varies)
4080 itype = variable_size (itype);
4081 itype = build_index_type (itype);
4084 else if (decl_context == FIELD)
4086 if (pedantic && !flag_isoc99 && !in_system_header)
4087 pedwarn ("ISO C90 does not support flexible array members");
4089 /* ISO C99 Flexible array members are effectively identical
4090 to GCC's zero-length array extension. */
4091 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4094 /* If pedantic, complain about arrays of incomplete types. */
4096 if (pedantic && !COMPLETE_TYPE_P (type))
4097 pedwarn ("array type has incomplete element type");
4099 /* Build the array type itself, then merge any constancy or
4100 volatility into the target type. We must do it in this order
4101 to ensure that the TYPE_MAIN_VARIANT field of the array type
4102 is set correctly. */
4104 type = build_array_type (type, itype);
4105 if (type_quals)
4106 type = c_build_qualified_type (type, type_quals);
4108 if (size_varies)
4109 C_TYPE_VARIABLE_SIZE (type) = 1;
4111 /* The GCC extension for zero-length arrays differs from
4112 ISO flexible array members in that sizeof yields zero. */
4113 if (size && integer_zerop (size))
4115 layout_type (type);
4116 TYPE_SIZE (type) = bitsize_zero_node;
4117 TYPE_SIZE_UNIT (type) = size_zero_node;
4119 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4120 /* We can never complete an array type which is the target of a
4121 pointer, so go ahead and lay it out. */
4122 layout_type (type);
4124 if (decl_context != PARM
4125 && (array_ptr_quals != NULL_TREE || array_parm_static))
4127 error ("static or type qualifiers in non-parameter array declarator");
4128 array_ptr_quals = NULL_TREE;
4129 array_parm_static = 0;
4132 else if (TREE_CODE (declarator) == CALL_EXPR)
4134 /* Say it's a definition only for the CALL_EXPR closest to
4135 the identifier. */
4136 bool really_funcdef = (funcdef_flag
4137 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4138 == IDENTIFIER_NODE));
4139 tree arg_types;
4141 /* Declaring a function type.
4142 Make sure we have a valid type for the function to return. */
4143 if (type == error_mark_node)
4144 continue;
4146 size_varies = 0;
4148 /* Warn about some types functions can't return. */
4150 if (TREE_CODE (type) == FUNCTION_TYPE)
4152 error ("`%s' declared as function returning a function", name);
4153 type = integer_type_node;
4155 if (TREE_CODE (type) == ARRAY_TYPE)
4157 error ("`%s' declared as function returning an array", name);
4158 type = integer_type_node;
4161 /* Construct the function type and go to the next
4162 inner layer of declarator. */
4163 arg_info = TREE_OPERAND (declarator, 1);
4164 arg_types = grokparms (arg_info, really_funcdef);
4166 /* Type qualifiers before the return type of the function
4167 qualify the return type, not the function type. */
4168 if (type_quals)
4170 /* Type qualifiers on a function return type are normally
4171 permitted by the standard but have no effect, so give a
4172 warning at -Wextra. Qualifiers on a void return type have
4173 meaning as a GNU extension, and are banned on function
4174 definitions in ISO C. FIXME: strictly we shouldn't
4175 pedwarn for qualified void return types except on function
4176 definitions, but not doing so could lead to the undesirable
4177 state of a "volatile void" function return type not being
4178 warned about, and a use of the function being compiled
4179 with GNU semantics, with no diagnostics under -pedantic. */
4180 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4181 pedwarn ("ISO C forbids qualified void function return type");
4182 else if (extra_warnings
4183 && !(VOID_TYPE_P (type)
4184 && type_quals == TYPE_QUAL_VOLATILE))
4185 warning ("type qualifiers ignored on function return type");
4187 type = c_build_qualified_type (type, type_quals);
4189 type_quals = TYPE_UNQUALIFIED;
4191 type = build_function_type (type, arg_types);
4192 declarator = TREE_OPERAND (declarator, 0);
4194 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4195 the formal parameter list of this FUNCTION_TYPE to point to
4196 the FUNCTION_TYPE node itself. */
4199 tree link;
4201 for (link = ARG_INFO_TAGS (arg_info);
4202 link;
4203 link = TREE_CHAIN (link))
4204 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4207 else if (TREE_CODE (declarator) == INDIRECT_REF)
4209 /* Merge any constancy or volatility into the target type
4210 for the pointer. */
4212 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4213 && type_quals)
4214 pedwarn ("ISO C forbids qualified function types");
4215 if (type_quals)
4216 type = c_build_qualified_type (type, type_quals);
4217 type_quals = TYPE_UNQUALIFIED;
4218 size_varies = 0;
4220 type = build_pointer_type (type);
4222 /* Process a list of type modifier keywords
4223 (such as const or volatile) that were given inside the `*'. */
4225 if (TREE_TYPE (declarator))
4227 tree typemodlist;
4228 int erred = 0;
4230 constp = 0;
4231 volatilep = 0;
4232 restrictp = 0;
4233 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4234 typemodlist = TREE_CHAIN (typemodlist))
4236 tree qualifier = TREE_VALUE (typemodlist);
4238 if (C_IS_RESERVED_WORD (qualifier))
4240 if (C_RID_CODE (qualifier) == RID_CONST)
4241 constp++;
4242 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4243 volatilep++;
4244 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4245 restrictp++;
4246 else
4247 erred++;
4249 else
4250 erred++;
4253 if (erred)
4254 error ("invalid type modifier within pointer declarator");
4255 if (pedantic && !flag_isoc99)
4257 if (constp > 1)
4258 pedwarn ("duplicate `const'");
4259 if (volatilep > 1)
4260 pedwarn ("duplicate `volatile'");
4261 if (restrictp > 1)
4262 pedwarn ("duplicate `restrict'");
4265 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4266 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4267 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4270 declarator = TREE_OPERAND (declarator, 0);
4272 else
4273 abort ();
4277 /* Now TYPE has the actual type. */
4279 /* Did array size calculations overflow? */
4281 if (TREE_CODE (type) == ARRAY_TYPE
4282 && COMPLETE_TYPE_P (type)
4283 && TREE_OVERFLOW (TYPE_SIZE (type)))
4285 error ("size of array `%s' is too large", name);
4286 /* If we proceed with the array type as it is, we'll eventually
4287 crash in tree_low_cst(). */
4288 type = error_mark_node;
4291 /* If this is declaring a typedef name, return a TYPE_DECL. */
4293 if (specbits & (1 << (int) RID_TYPEDEF))
4295 tree decl;
4296 /* Note that the grammar rejects storage classes
4297 in typenames, fields or parameters */
4298 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4299 && type_quals)
4300 pedwarn ("ISO C forbids qualified function types");
4301 if (type_quals)
4302 type = c_build_qualified_type (type, type_quals);
4303 decl = build_decl (TYPE_DECL, declarator, type);
4304 if ((specbits & (1 << (int) RID_SIGNED))
4305 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4306 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4307 decl_attributes (&decl, returned_attrs, 0);
4308 return decl;
4311 /* Detect the case of an array type of unspecified size
4312 which came, as such, direct from a typedef name.
4313 We must copy the type, so that each identifier gets
4314 a distinct type, so that each identifier's size can be
4315 controlled separately by its own initializer. */
4317 if (type != 0 && typedef_type != 0
4318 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4319 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4321 type = build_array_type (TREE_TYPE (type), 0);
4322 if (size_varies)
4323 C_TYPE_VARIABLE_SIZE (type) = 1;
4326 /* If this is a type name (such as, in a cast or sizeof),
4327 compute the type and return it now. */
4329 if (decl_context == TYPENAME)
4331 /* Note that the grammar rejects storage classes
4332 in typenames, fields or parameters */
4333 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4334 && type_quals)
4335 pedwarn ("ISO C forbids const or volatile function types");
4336 if (type_quals)
4337 type = c_build_qualified_type (type, type_quals);
4338 decl_attributes (&type, returned_attrs, 0);
4339 return type;
4342 /* Aside from typedefs and type names (handle above),
4343 `void' at top level (not within pointer)
4344 is allowed only in public variables.
4345 We don't complain about parms either, but that is because
4346 a better error message can be made later. */
4348 if (VOID_TYPE_P (type) && decl_context != PARM
4349 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4350 && ((specbits & (1 << (int) RID_EXTERN))
4351 || (current_scope == file_scope
4352 && !(specbits
4353 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4355 error ("variable or field `%s' declared void", name);
4356 type = integer_type_node;
4359 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4360 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4363 tree decl;
4365 if (decl_context == PARM)
4367 tree type_as_written;
4368 tree promoted_type;
4370 /* A parameter declared as an array of T is really a pointer to T.
4371 One declared as a function is really a pointer to a function. */
4373 if (TREE_CODE (type) == ARRAY_TYPE)
4375 /* Transfer const-ness of array into that of type pointed to. */
4376 type = TREE_TYPE (type);
4377 if (type_quals)
4378 type = c_build_qualified_type (type, type_quals);
4379 type = build_pointer_type (type);
4380 type_quals = TYPE_UNQUALIFIED;
4381 if (array_ptr_quals)
4383 tree new_ptr_quals, new_ptr_attrs;
4384 int erred = 0;
4385 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4386 /* We don't yet implement attributes in this context. */
4387 if (new_ptr_attrs != NULL_TREE)
4388 warning ("attributes in parameter array declarator ignored");
4390 constp = 0;
4391 volatilep = 0;
4392 restrictp = 0;
4393 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4395 tree qualifier = TREE_VALUE (new_ptr_quals);
4397 if (C_IS_RESERVED_WORD (qualifier))
4399 if (C_RID_CODE (qualifier) == RID_CONST)
4400 constp++;
4401 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4402 volatilep++;
4403 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4404 restrictp++;
4405 else
4406 erred++;
4408 else
4409 erred++;
4412 if (erred)
4413 error ("invalid type modifier within array declarator");
4415 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4416 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4417 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4419 size_varies = 0;
4421 else if (TREE_CODE (type) == FUNCTION_TYPE)
4423 if (pedantic && type_quals)
4424 pedwarn ("ISO C forbids qualified function types");
4425 if (type_quals)
4426 type = c_build_qualified_type (type, type_quals);
4427 type = build_pointer_type (type);
4428 type_quals = TYPE_UNQUALIFIED;
4430 else if (type_quals)
4431 type = c_build_qualified_type (type, type_quals);
4433 type_as_written = type;
4435 decl = build_decl (PARM_DECL, declarator, type);
4436 if (size_varies)
4437 C_DECL_VARIABLE_SIZE (decl) = 1;
4439 /* Compute the type actually passed in the parmlist,
4440 for the case where there is no prototype.
4441 (For example, shorts and chars are passed as ints.)
4442 When there is a prototype, this is overridden later. */
4444 if (type == error_mark_node)
4445 promoted_type = type;
4446 else
4447 promoted_type = c_type_promotes_to (type);
4449 DECL_ARG_TYPE (decl) = promoted_type;
4450 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4452 else if (decl_context == FIELD)
4454 /* Structure field. It may not be a function. */
4456 if (TREE_CODE (type) == FUNCTION_TYPE)
4458 error ("field `%s' declared as a function", name);
4459 type = build_pointer_type (type);
4461 else if (TREE_CODE (type) != ERROR_MARK
4462 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4464 error ("field `%s' has incomplete type", name);
4465 type = error_mark_node;
4467 /* Move type qualifiers down to element of an array. */
4468 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4469 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4470 type_quals),
4471 TYPE_DOMAIN (type));
4472 decl = build_decl (FIELD_DECL, declarator, type);
4473 DECL_NONADDRESSABLE_P (decl) = bitfield;
4475 if (size_varies)
4476 C_DECL_VARIABLE_SIZE (decl) = 1;
4478 else if (TREE_CODE (type) == FUNCTION_TYPE)
4480 if (specbits & (1 << (int) RID_AUTO)
4481 && (pedantic || current_scope == file_scope))
4482 pedwarn ("invalid storage class for function `%s'", name);
4483 if (specbits & (1 << (int) RID_REGISTER))
4484 error ("invalid storage class for function `%s'", name);
4485 if (specbits & (1 << (int) RID_THREAD))
4486 error ("invalid storage class for function `%s'", name);
4487 /* Function declaration not at file scope.
4488 Storage classes other than `extern' are not allowed
4489 and `extern' makes no difference. */
4490 if (current_scope != file_scope
4491 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4492 && pedantic)
4493 pedwarn ("invalid storage class for function `%s'", name);
4495 decl = build_decl (FUNCTION_DECL, declarator, type);
4496 decl = build_decl_attribute_variant (decl, decl_attr);
4498 DECL_LANG_SPECIFIC (decl)
4499 = ggc_alloc_cleared (sizeof (struct lang_decl));
4501 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4502 pedwarn ("ISO C forbids qualified function types");
4504 /* GNU C interprets a `volatile void' return type to indicate
4505 that the function does not return. */
4506 if ((type_quals & TYPE_QUAL_VOLATILE)
4507 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4508 warning ("`noreturn' function returns non-void value");
4510 /* Every function declaration is an external reference
4511 (DECL_EXTERNAL) except for those which are not at file
4512 scope and are explicitly declared "auto". This is
4513 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4514 GCC to signify a forward declaration of a nested function. */
4515 if ((specbits & (1 << RID_AUTO)) && current_scope != file_scope)
4516 DECL_EXTERNAL (decl) = 0;
4517 else
4518 DECL_EXTERNAL (decl) = 1;
4520 /* Record absence of global scope for `static' or `auto'. */
4521 TREE_PUBLIC (decl)
4522 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4524 /* For a function definition, record the argument information
4525 block in DECL_ARGUMENTS where store_parm_decls will look
4526 for it. */
4527 if (funcdef_flag)
4528 DECL_ARGUMENTS (decl) = arg_info;
4530 if (defaulted_int)
4531 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4533 /* Record presence of `inline', if it is reasonable. */
4534 if (MAIN_NAME_P (declarator))
4536 if (inlinep)
4537 warning ("cannot inline function `main'");
4539 else if (inlinep)
4541 /* Record that the function is declared `inline'. */
4542 DECL_DECLARED_INLINE_P (decl) = 1;
4544 /* Do not mark bare declarations as DECL_INLINE. Doing so
4545 in the presence of multiple declarations can result in
4546 the abstract origin pointing between the declarations,
4547 which will confuse dwarf2out. */
4548 if (initialized)
4550 DECL_INLINE (decl) = 1;
4551 if (specbits & (1 << (int) RID_EXTERN))
4552 current_extern_inline = 1;
4555 /* If -finline-functions, assume it can be inlined. This does
4556 two things: let the function be deferred until it is actually
4557 needed, and let dwarf2 know that the function is inlinable. */
4558 else if (flag_inline_trees == 2 && initialized)
4559 DECL_INLINE (decl) = 1;
4561 else
4563 /* It's a variable. */
4564 /* An uninitialized decl with `extern' is a reference. */
4565 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4567 /* Move type qualifiers down to element of an array. */
4568 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4570 int saved_align = TYPE_ALIGN(type);
4571 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4572 type_quals),
4573 TYPE_DOMAIN (type));
4574 TYPE_ALIGN (type) = saved_align;
4576 else if (type_quals)
4577 type = c_build_qualified_type (type, type_quals);
4579 /* C99 6.2.2p7: It is invalid (compile-time undefined
4580 behavior) to create an 'extern' declaration for a
4581 variable if there is a global declaration that is
4582 'static' and the global declaration is not visible.
4583 (If the static declaration _is_ currently visible,
4584 the 'extern' declaration is taken to refer to that decl.) */
4585 if (extern_ref && current_scope != file_scope)
4587 tree global_decl = identifier_global_value (declarator);
4588 tree visible_decl = lookup_name (declarator);
4590 if (global_decl
4591 && global_decl != visible_decl
4592 && TREE_CODE (global_decl) == VAR_DECL
4593 && !TREE_PUBLIC (global_decl))
4594 error ("variable previously declared 'static' redeclared "
4595 "'extern'");
4598 decl = build_decl (VAR_DECL, declarator, type);
4599 if (size_varies)
4600 C_DECL_VARIABLE_SIZE (decl) = 1;
4602 if (inlinep)
4603 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4605 DECL_EXTERNAL (decl) = extern_ref;
4607 /* At file scope, the presence of a `static' or `register' storage
4608 class specifier, or the absence of all storage class specifiers
4609 makes this declaration a definition (perhaps tentative). Also,
4610 the absence of both `static' and `register' makes it public. */
4611 if (current_scope == file_scope)
4613 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4614 | (1 << (int) RID_REGISTER)));
4615 TREE_STATIC (decl) = !extern_ref;
4617 /* Not at file scope, only `static' makes a static definition. */
4618 else
4620 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4621 TREE_PUBLIC (decl) = extern_ref;
4624 if (specbits & 1 << (int) RID_THREAD)
4626 if (targetm.have_tls)
4627 DECL_THREAD_LOCAL (decl) = 1;
4628 else
4629 /* A mere warning is sure to result in improper semantics
4630 at runtime. Don't bother to allow this to compile. */
4631 error ("thread-local storage not supported for this target");
4635 /* Record `register' declaration for warnings on &
4636 and in case doing stupid register allocation. */
4638 if (specbits & (1 << (int) RID_REGISTER))
4640 C_DECL_REGISTER (decl) = 1;
4641 DECL_REGISTER (decl) = 1;
4644 /* Record constancy and volatility. */
4645 c_apply_type_quals_to_decl (type_quals, decl);
4647 /* If a type has volatile components, it should be stored in memory.
4648 Otherwise, the fact that those components are volatile
4649 will be ignored, and would even crash the compiler. */
4650 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4652 /* It is not an error for a structure with volatile fields to
4653 be declared register, but reset DECL_REGISTER since it
4654 cannot actually go in a register. */
4655 int was_reg = C_DECL_REGISTER (decl);
4656 C_DECL_REGISTER (decl) = 0;
4657 DECL_REGISTER (decl) = 0;
4658 c_mark_addressable (decl);
4659 C_DECL_REGISTER (decl) = was_reg;
4662 #ifdef ENABLE_CHECKING
4663 /* This is the earliest point at which we might know the assembler
4664 name of a variable. Thus, if it's known before this, die horribly. */
4665 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4666 abort ();
4667 #endif
4669 decl_attributes (&decl, returned_attrs, 0);
4671 return decl;
4675 /* Decode the parameter-list info for a function type or function definition.
4676 The argument is the value returned by `get_parm_info' (or made in parse.y
4677 if there is an identifier list instead of a parameter decl list).
4678 These two functions are separate because when a function returns
4679 or receives functions then each is called multiple times but the order
4680 of calls is different. The last call to `grokparms' is always the one
4681 that contains the formal parameter names of a function definition.
4683 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4685 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4686 a mere declaration. A nonempty identifier-list gets an error message
4687 when FUNCDEF_FLAG is zero. */
4689 static tree
4690 grokparms (tree arg_info, int funcdef_flag)
4692 tree arg_types = ARG_INFO_TYPES (arg_info);
4694 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4695 && !in_system_header)
4696 warning ("function declaration isn't a prototype");
4698 if (arg_types == error_mark_node)
4699 return 0; /* don't set TYPE_ARG_TYPES in this case */
4701 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4703 if (! funcdef_flag)
4704 pedwarn ("parameter names (without types) in function declaration");
4706 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4707 ARG_INFO_TYPES (arg_info) = 0;
4708 return 0;
4710 else
4712 tree parm, type, typelt;
4713 unsigned int parmno;
4715 /* If the arg types are incomplete in a declaration, they must
4716 include undefined tags. These tags can never be defined in
4717 the scope of the declaration, so the types can never be
4718 completed, and no call can be compiled successfully. */
4720 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4721 parm;
4722 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4724 type = TREE_VALUE (typelt);
4725 if (type == error_mark_node)
4726 continue;
4728 if (!COMPLETE_TYPE_P (type))
4730 if (funcdef_flag)
4732 if (DECL_NAME (parm))
4733 error ("%Jparameter %u ('%D') has incomplete type",
4734 parm, parmno, parm);
4735 else
4736 error ("%Jparameter %u has incomplete type",
4737 parm, parmno);
4739 TREE_VALUE (typelt) = error_mark_node;
4740 TREE_TYPE (parm) = error_mark_node;
4742 else
4744 if (DECL_NAME (parm))
4745 warning ("%Jparameter %u ('%D') has incomplete type",
4746 parm, parmno, parm);
4747 else
4748 warning ("%Jparameter %u has incomplete type",
4749 parm, parmno);
4753 return arg_types;
4757 /* Take apart the current scope and return a tree_list node with info
4758 on a parameter list just parsed. This tree_list node should be
4759 examined using the ARG_INFO_* macros, defined above:
4761 ARG_INFO_PARMS: a list of parameter decls.
4762 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4763 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4764 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4765 constants) defined with the parameters.
4767 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4769 ELLIPSIS being true means the argument list ended in '...' so don't
4770 append a sentinel (void_list_node) to the end of the type-list. */
4772 tree
4773 get_parm_info (bool ellipsis)
4775 struct c_binding *b = current_scope->bindings;
4776 tree arg_info = make_node (TREE_LIST);
4777 tree parms = 0;
4778 tree tags = 0;
4779 tree types = 0;
4780 tree others = 0;
4782 static bool explained_incomplete_types = false;
4783 bool gave_void_only_once_err = false;
4785 /* The bindings in this scope must not get put into a block.
4786 We will take care of deleting the binding nodes. */
4787 current_scope->bindings = 0;
4789 /* This function is only called if there was *something* on the
4790 parameter list. */
4791 #ifdef ENABLE_CHECKING
4792 if (b == 0)
4793 abort ();
4794 #endif
4796 /* A parameter list consisting solely of 'void' indicates that the
4797 function takes no arguments. But if the 'void' is qualified
4798 (by 'const' or 'volatile'), or has a storage class specifier
4799 ('register'), then the behavior is undefined; issue an error.
4800 Typedefs for 'void' are OK (see DR#157). */
4801 if (b->prev == 0 /* one binding */
4802 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4803 && !DECL_NAME (b->decl) /* anonymous */
4804 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4806 if (TREE_THIS_VOLATILE (b->decl)
4807 || TREE_READONLY (b->decl)
4808 || C_DECL_REGISTER (b->decl))
4809 error ("'void' as only parameter may not be qualified");
4811 /* There cannot be an ellipsis. */
4812 if (ellipsis)
4813 error ("'void' must be the only parameter");
4815 ARG_INFO_TYPES (arg_info) = void_list_node;
4816 return arg_info;
4819 if (!ellipsis)
4820 types = void_list_node;
4822 /* Break up the bindings list into parms, tags, types, and others;
4823 apply sanity checks; purge the name-to-decl bindings. */
4824 while (b)
4826 tree decl = b->decl;
4827 tree type = TREE_TYPE (decl);
4828 const char *keyword;
4830 switch (TREE_CODE (decl))
4832 case PARM_DECL:
4833 if (b->id)
4835 #ifdef ENABLE_CHECKING
4836 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4837 #endif
4838 I_SYMBOL_BINDING (b->id) = b->shadowed;
4841 /* Check for forward decls that never got their actual decl. */
4842 if (TREE_ASM_WRITTEN (decl))
4843 error ("%Jparameter '%D' has just a forward declaration",
4844 decl, decl);
4845 /* Check for (..., void, ...) and issue an error. */
4846 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4848 if (!gave_void_only_once_err)
4850 error ("'void' must be the only parameter");
4851 gave_void_only_once_err = true;
4854 else
4856 /* Valid parameter, add it to the list. */
4857 TREE_CHAIN (decl) = parms;
4858 parms = decl;
4860 /* Since there is a prototype, args are passed in their
4861 declared types. The back end may override this later. */
4862 DECL_ARG_TYPE (decl) = type;
4863 types = tree_cons (0, type, types);
4865 break;
4867 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4868 case UNION_TYPE: keyword = "union"; goto tag;
4869 case RECORD_TYPE: keyword = "struct"; goto tag;
4870 tag:
4871 /* Types may not have tag-names, in which case the type
4872 appears in the bindings list with b->id NULL. */
4873 if (b->id)
4875 #ifdef ENABLE_CHECKING
4876 if (I_TAG_BINDING (b->id) != b) abort ();
4877 #endif
4878 I_TAG_BINDING (b->id) = b->shadowed;
4881 /* Warn about any struct, union or enum tags defined in a
4882 parameter list. The scope of such types is limited to
4883 the parameter list, which is rarely if ever desirable
4884 (it's impossible to call such a function with type-
4885 correct arguments). An anonymous union parm type is
4886 meaningful as a GNU extension, so don't warn for that. */
4887 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4889 if (b->id)
4890 /* The %s will be one of 'struct', 'union', or 'enum'. */
4891 warning ("'%s %E' declared inside parameter list",
4892 keyword, b->id);
4893 else
4894 /* The %s will be one of 'struct', 'union', or 'enum'. */
4895 warning ("anonymous %s declared inside parameter list",
4896 keyword);
4898 if (! explained_incomplete_types)
4900 warning ("its scope is only this definition or declaration,"
4901 " which is probably not what you want");
4902 explained_incomplete_types = true;
4906 tags = tree_cons (b->id, decl, tags);
4907 break;
4909 case CONST_DECL:
4910 case TYPE_DECL:
4911 /* CONST_DECLs appear here when we have an embedded enum,
4912 and TYPE_DECLs appear here when we have an embedded struct
4913 or union. No warnings for this - we already warned about the
4914 type itself. */
4915 TREE_CHAIN (decl) = others;
4916 others = decl;
4917 /* fall through */
4919 case ERROR_MARK:
4920 /* error_mark_node appears here when we have an undeclared
4921 variable. Just throw it away. */
4922 if (b->id)
4924 #ifdef ENABLE_CHECKING
4925 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4926 #endif
4927 I_SYMBOL_BINDING (b->id) = b->shadowed;
4929 break;
4931 /* Other things that might be encountered. */
4932 case LABEL_DECL:
4933 case FUNCTION_DECL:
4934 case VAR_DECL:
4935 default:
4936 abort ();
4939 b = free_binding_and_advance (b);
4942 ARG_INFO_PARMS (arg_info) = parms;
4943 ARG_INFO_TAGS (arg_info) = tags;
4944 ARG_INFO_TYPES (arg_info) = types;
4945 ARG_INFO_OTHERS (arg_info) = others;
4946 return arg_info;
4949 /* Get the struct, enum or union (CODE says which) with tag NAME.
4950 Define the tag as a forward-reference if it is not defined. */
4952 tree
4953 xref_tag (enum tree_code code, tree name)
4955 /* If a cross reference is requested, look up the type
4956 already defined for this tag and return it. */
4958 tree ref = lookup_tag (code, name, 0);
4959 /* If this is the right type of tag, return what we found.
4960 (This reference will be shadowed by shadow_tag later if appropriate.)
4961 If this is the wrong type of tag, do not return it. If it was the
4962 wrong type in the same scope, we will have had an error
4963 message already; if in a different scope and declaring
4964 a name, pending_xref_error will give an error message; but if in a
4965 different scope and not declaring a name, this tag should
4966 shadow the previous declaration of a different type of tag, and
4967 this would not work properly if we return the reference found.
4968 (For example, with "struct foo" in an outer scope, "union foo;"
4969 must shadow that tag with a new one of union type.) */
4970 if (ref && TREE_CODE (ref) == code)
4971 return ref;
4973 /* If no such tag is yet defined, create a forward-reference node
4974 and record it as the "definition".
4975 When a real declaration of this type is found,
4976 the forward-reference will be altered into a real type. */
4978 ref = make_node (code);
4979 if (code == ENUMERAL_TYPE)
4981 /* Give the type a default layout like unsigned int
4982 to avoid crashing if it does not get defined. */
4983 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4984 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4985 TYPE_USER_ALIGN (ref) = 0;
4986 TYPE_UNSIGNED (ref) = 1;
4987 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4988 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4989 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4992 pushtag (name, ref);
4994 return ref;
4997 /* Make sure that the tag NAME is defined *in the current scope*
4998 at least as a forward reference.
4999 CODE says which kind of tag NAME ought to be. */
5001 tree
5002 start_struct (enum tree_code code, tree name)
5004 /* If there is already a tag defined at this scope
5005 (as a forward reference), just return it. */
5007 tree ref = 0;
5009 if (name != 0)
5010 ref = lookup_tag (code, name, 1);
5011 if (ref && TREE_CODE (ref) == code)
5013 if (TYPE_FIELDS (ref))
5015 if (code == UNION_TYPE)
5016 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5017 else
5018 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5021 else
5023 /* Otherwise create a forward-reference just so the tag is in scope. */
5025 ref = make_node (code);
5026 pushtag (name, ref);
5029 C_TYPE_BEING_DEFINED (ref) = 1;
5030 TYPE_PACKED (ref) = flag_pack_struct;
5031 return ref;
5034 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5035 of a structure component, returning a FIELD_DECL node.
5036 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5038 This is done during the parsing of the struct declaration.
5039 The FIELD_DECL nodes are chained together and the lot of them
5040 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5042 tree
5043 grokfield (tree declarator, tree declspecs, tree width)
5045 tree value;
5047 if (declarator == NULL_TREE && width == NULL_TREE)
5049 /* This is an unnamed decl.
5051 If we have something of the form "union { list } ;" then this
5052 is the anonymous union extension. Similarly for struct.
5054 If this is something of the form "struct foo;", then
5055 If MS extensions are enabled, this is handled as an
5056 anonymous struct.
5057 Otherwise this is a forward declaration of a structure tag.
5059 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5060 If MS extensions are enabled and foo names a structure, then
5061 again this is an anonymous struct.
5062 Otherwise this is an error.
5064 Oh what a horrid tangled web we weave. I wonder if MS consciously
5065 took this from Plan 9 or if it was an accident of implementation
5066 that took root before someone noticed the bug... */
5068 tree type = TREE_VALUE (declspecs);
5070 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5071 type = TREE_TYPE (type);
5072 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5074 if (flag_ms_extensions)
5075 ; /* ok */
5076 else if (flag_iso)
5077 goto warn_unnamed_field;
5078 else if (TYPE_NAME (type) == NULL)
5079 ; /* ok */
5080 else
5081 goto warn_unnamed_field;
5083 else
5085 warn_unnamed_field:
5086 warning ("declaration does not declare anything");
5087 return NULL_TREE;
5091 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5092 width ? &width : NULL);
5094 finish_decl (value, NULL_TREE, NULL_TREE);
5095 DECL_INITIAL (value) = width;
5097 return value;
5100 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5101 the list such that this does not present a problem later. */
5103 static void
5104 detect_field_duplicates (tree fieldlist)
5106 tree x, y;
5107 int timeout = 10;
5109 /* First, see if there are more than "a few" fields.
5110 This is trivially true if there are zero or one fields. */
5111 if (!fieldlist)
5112 return;
5113 x = TREE_CHAIN (fieldlist);
5114 if (!x)
5115 return;
5116 do {
5117 timeout--;
5118 x = TREE_CHAIN (x);
5119 } while (timeout > 0 && x);
5121 /* If there were "few" fields, avoid the overhead of allocating
5122 a hash table. Instead just do the nested traversal thing. */
5123 if (timeout > 0)
5125 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5126 if (DECL_NAME (x))
5128 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5129 if (DECL_NAME (y) == DECL_NAME (x))
5131 error ("%Jduplicate member '%D'", x, x);
5132 DECL_NAME (x) = NULL_TREE;
5136 else
5138 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5139 void **slot;
5141 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5142 if ((y = DECL_NAME (x)) != 0)
5144 slot = htab_find_slot (htab, y, INSERT);
5145 if (*slot)
5147 error ("%Jduplicate member '%D'", x, x);
5148 DECL_NAME (x) = NULL_TREE;
5150 *slot = y;
5153 htab_delete (htab);
5157 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5158 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5159 ATTRIBUTES are attributes to be applied to the structure. */
5161 tree
5162 finish_struct (tree t, tree fieldlist, tree attributes)
5164 tree x;
5165 bool toplevel = file_scope == current_scope;
5166 int saw_named_field;
5168 /* If this type was previously laid out as a forward reference,
5169 make sure we lay it out again. */
5171 TYPE_SIZE (t) = 0;
5173 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5175 if (pedantic)
5177 for (x = fieldlist; x; x = TREE_CHAIN (x))
5178 if (DECL_NAME (x) != 0)
5179 break;
5181 if (x == 0)
5182 pedwarn ("%s has no %s",
5183 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5184 fieldlist ? _("named members") : _("members"));
5187 /* Install struct as DECL_CONTEXT of each field decl.
5188 Also process specified field sizes, found in the DECL_INITIAL,
5189 storing 0 there after the type has been changed to precision equal
5190 to its width, rather than the precision of the specified standard
5191 type. (Correct layout requires the original type to have been preserved
5192 until now.) */
5194 saw_named_field = 0;
5195 for (x = fieldlist; x; x = TREE_CHAIN (x))
5197 DECL_CONTEXT (x) = t;
5198 DECL_PACKED (x) |= TYPE_PACKED (t);
5200 /* If any field is const, the structure type is pseudo-const. */
5201 if (TREE_READONLY (x))
5202 C_TYPE_FIELDS_READONLY (t) = 1;
5203 else
5205 /* A field that is pseudo-const makes the structure likewise. */
5206 tree t1 = TREE_TYPE (x);
5207 while (TREE_CODE (t1) == ARRAY_TYPE)
5208 t1 = TREE_TYPE (t1);
5209 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5210 && C_TYPE_FIELDS_READONLY (t1))
5211 C_TYPE_FIELDS_READONLY (t) = 1;
5214 /* Any field that is volatile means variables of this type must be
5215 treated in some ways as volatile. */
5216 if (TREE_THIS_VOLATILE (x))
5217 C_TYPE_FIELDS_VOLATILE (t) = 1;
5219 /* Any field of nominal variable size implies structure is too. */
5220 if (C_DECL_VARIABLE_SIZE (x))
5221 C_TYPE_VARIABLE_SIZE (t) = 1;
5223 /* Detect invalid nested redefinition. */
5224 if (TREE_TYPE (x) == t)
5225 error ("nested redefinition of `%s'",
5226 IDENTIFIER_POINTER (TYPE_NAME (t)));
5228 if (DECL_INITIAL (x))
5230 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5231 DECL_SIZE (x) = bitsize_int (width);
5232 DECL_BIT_FIELD (x) = 1;
5233 SET_DECL_C_BIT_FIELD (x);
5236 /* Detect flexible array member in an invalid context. */
5237 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5238 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5239 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5240 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5242 if (TREE_CODE (t) == UNION_TYPE)
5244 error ("%Jflexible array member in union", x);
5245 TREE_TYPE (x) = error_mark_node;
5247 else if (TREE_CHAIN (x) != NULL_TREE)
5249 error ("%Jflexible array member not at end of struct", x);
5250 TREE_TYPE (x) = error_mark_node;
5252 else if (! saw_named_field)
5254 error ("%Jflexible array member in otherwise empty struct", x);
5255 TREE_TYPE (x) = error_mark_node;
5259 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5260 && flexible_array_type_p (TREE_TYPE (x)))
5261 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5263 if (DECL_NAME (x))
5264 saw_named_field = 1;
5267 detect_field_duplicates (fieldlist);
5269 /* Now we have the nearly final fieldlist. Record it,
5270 then lay out the structure or union (including the fields). */
5272 TYPE_FIELDS (t) = fieldlist;
5274 layout_type (t);
5276 /* Give bit-fields their proper types. */
5278 tree *fieldlistp = &fieldlist;
5279 while (*fieldlistp)
5280 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5281 && TREE_TYPE (*fieldlistp) != error_mark_node)
5283 unsigned HOST_WIDE_INT width
5284 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5285 tree type = TREE_TYPE (*fieldlistp);
5286 if (width != TYPE_PRECISION (type))
5287 TREE_TYPE (*fieldlistp)
5288 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5289 DECL_INITIAL (*fieldlistp) = 0;
5291 else
5292 fieldlistp = &TREE_CHAIN (*fieldlistp);
5295 /* Now we have the truly final field list.
5296 Store it in this type and in the variants. */
5298 TYPE_FIELDS (t) = fieldlist;
5300 /* If there are lots of fields, sort so we can look through them fast.
5301 We arbitrarily consider 16 or more elts to be "a lot". */
5304 int len = 0;
5306 for (x = fieldlist; x; x = TREE_CHAIN (x))
5308 if (len > 15 || DECL_NAME (x) == NULL)
5309 break;
5310 len += 1;
5313 if (len > 15)
5315 tree *field_array;
5316 struct lang_type *space;
5317 struct sorted_fields_type *space2;
5319 len += list_length (x);
5321 /* Use the same allocation policy here that make_node uses, to
5322 ensure that this lives as long as the rest of the struct decl.
5323 All decls in an inline function need to be saved. */
5325 space = ggc_alloc_cleared (sizeof (struct lang_type));
5326 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5328 len = 0;
5329 space->s = space2;
5330 field_array = &space2->elts[0];
5331 for (x = fieldlist; x; x = TREE_CHAIN (x))
5333 field_array[len++] = x;
5335 /* If there is anonymous struct or union, break out of the loop. */
5336 if (DECL_NAME (x) == NULL)
5337 break;
5339 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5340 if (x == NULL)
5342 TYPE_LANG_SPECIFIC (t) = space;
5343 TYPE_LANG_SPECIFIC (t)->s->len = len;
5344 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5345 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5350 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5352 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5353 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5354 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5355 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5358 /* If this was supposed to be a transparent union, but we can't
5359 make it one, warn and turn off the flag. */
5360 if (TREE_CODE (t) == UNION_TYPE
5361 && TYPE_TRANSPARENT_UNION (t)
5362 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5364 TYPE_TRANSPARENT_UNION (t) = 0;
5365 warning ("union cannot be made transparent");
5368 /* If this structure or union completes the type of any previous
5369 variable declaration, lay it out and output its rtl. */
5370 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5372 x = TREE_CHAIN (x))
5374 tree decl = TREE_VALUE (x);
5375 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5376 layout_array_type (TREE_TYPE (decl));
5377 if (TREE_CODE (decl) != TYPE_DECL)
5379 layout_decl (decl, 0);
5380 if (c_dialect_objc ())
5381 objc_check_decl (decl);
5382 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5383 if (! toplevel)
5384 expand_decl (decl);
5387 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5389 /* Finish debugging output for this type. */
5390 rest_of_type_compilation (t, toplevel);
5392 return t;
5395 /* Lay out the type T, and its element type, and so on. */
5397 static void
5398 layout_array_type (tree t)
5400 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5401 layout_array_type (TREE_TYPE (t));
5402 layout_type (t);
5405 /* Begin compiling the definition of an enumeration type.
5406 NAME is its name (or null if anonymous).
5407 Returns the type object, as yet incomplete.
5408 Also records info about it so that build_enumerator
5409 may be used to declare the individual values as they are read. */
5411 tree
5412 start_enum (tree name)
5414 tree enumtype = 0;
5416 /* If this is the real definition for a previous forward reference,
5417 fill in the contents in the same object that used to be the
5418 forward reference. */
5420 if (name != 0)
5421 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5423 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5425 enumtype = make_node (ENUMERAL_TYPE);
5426 pushtag (name, enumtype);
5429 C_TYPE_BEING_DEFINED (enumtype) = 1;
5431 if (TYPE_VALUES (enumtype) != 0)
5433 /* This enum is a named one that has been declared already. */
5434 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5436 /* Completely replace its old definition.
5437 The old enumerators remain defined, however. */
5438 TYPE_VALUES (enumtype) = 0;
5441 enum_next_value = integer_zero_node;
5442 enum_overflow = 0;
5444 if (flag_short_enums)
5445 TYPE_PACKED (enumtype) = 1;
5447 return enumtype;
5450 /* After processing and defining all the values of an enumeration type,
5451 install their decls in the enumeration type and finish it off.
5452 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5453 and ATTRIBUTES are the specified attributes.
5454 Returns ENUMTYPE. */
5456 tree
5457 finish_enum (tree enumtype, tree values, tree attributes)
5459 tree pair, tem;
5460 tree minnode = 0, maxnode = 0;
5461 int precision, unsign;
5462 bool toplevel = (file_scope == current_scope);
5463 struct lang_type *lt;
5465 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5467 /* Calculate the maximum value of any enumerator in this type. */
5469 if (values == error_mark_node)
5470 minnode = maxnode = integer_zero_node;
5471 else
5473 minnode = maxnode = TREE_VALUE (values);
5474 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5476 tree value = TREE_VALUE (pair);
5477 if (tree_int_cst_lt (maxnode, value))
5478 maxnode = value;
5479 if (tree_int_cst_lt (value, minnode))
5480 minnode = value;
5484 /* Construct the final type of this enumeration. It is the same
5485 as one of the integral types - the narrowest one that fits, except
5486 that normally we only go as narrow as int - and signed iff any of
5487 the values are negative. */
5488 unsign = (tree_int_cst_sgn (minnode) >= 0);
5489 precision = MAX (min_precision (minnode, unsign),
5490 min_precision (maxnode, unsign));
5491 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5493 tem = c_common_type_for_size (precision, unsign);
5494 if (tem == NULL)
5496 warning ("enumeration values exceed range of largest integer");
5497 tem = long_long_integer_type_node;
5500 else
5501 tem = unsign ? unsigned_type_node : integer_type_node;
5503 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5504 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5505 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5506 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5507 TYPE_SIZE (enumtype) = 0;
5508 layout_type (enumtype);
5510 if (values != error_mark_node)
5512 /* Change the type of the enumerators to be the enum type. We
5513 need to do this irrespective of the size of the enum, for
5514 proper type checking. Replace the DECL_INITIALs of the
5515 enumerators, and the value slots of the list, with copies
5516 that have the enum type; they cannot be modified in place
5517 because they may be shared (e.g. integer_zero_node) Finally,
5518 change the purpose slots to point to the names of the decls. */
5519 for (pair = values; pair; pair = TREE_CHAIN (pair))
5521 tree enu = TREE_PURPOSE (pair);
5522 tree ini = DECL_INITIAL (enu);
5524 TREE_TYPE (enu) = enumtype;
5526 /* The ISO C Standard mandates enumerators to have type int,
5527 even though the underlying type of an enum type is
5528 unspecified. Here we convert any enumerators that fit in
5529 an int to type int, to avoid promotions to unsigned types
5530 when comparing integers with enumerators that fit in the
5531 int range. When -pedantic is given, build_enumerator()
5532 would have already taken care of those that don't fit. */
5533 if (int_fits_type_p (ini, integer_type_node))
5534 tem = integer_type_node;
5535 else
5536 tem = enumtype;
5537 ini = convert (tem, ini);
5539 DECL_INITIAL (enu) = ini;
5540 TREE_PURPOSE (pair) = DECL_NAME (enu);
5541 TREE_VALUE (pair) = ini;
5544 TYPE_VALUES (enumtype) = values;
5547 /* Record the min/max values so that we can warn about bit-field
5548 enumerations that are too small for the values. */
5549 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5550 lt->enum_min = minnode;
5551 lt->enum_max = maxnode;
5552 TYPE_LANG_SPECIFIC (enumtype) = lt;
5554 /* Fix up all variant types of this enum type. */
5555 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5557 if (tem == enumtype)
5558 continue;
5559 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5560 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5561 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5562 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5563 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5564 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5565 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5566 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5567 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5568 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5569 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5572 /* Finish debugging output for this type. */
5573 rest_of_type_compilation (enumtype, toplevel);
5575 return enumtype;
5578 /* Build and install a CONST_DECL for one value of the
5579 current enumeration type (one that was begun with start_enum).
5580 Return a tree-list containing the CONST_DECL and its value.
5581 Assignment of sequential values by default is handled here. */
5583 tree
5584 build_enumerator (tree name, tree value)
5586 tree decl, type;
5588 /* Validate and default VALUE. */
5590 /* Remove no-op casts from the value. */
5591 if (value)
5592 STRIP_TYPE_NOPS (value);
5594 if (value != 0)
5596 /* Don't issue more errors for error_mark_node (i.e. an
5597 undeclared identifier) - just ignore the value expression. */
5598 if (value == error_mark_node)
5599 value = 0;
5600 else if (TREE_CODE (value) != INTEGER_CST)
5602 error ("enumerator value for '%E' is not an integer constant", name);
5603 value = 0;
5605 else
5607 value = default_conversion (value);
5608 constant_expression_warning (value);
5612 /* Default based on previous value. */
5613 /* It should no longer be possible to have NON_LVALUE_EXPR
5614 in the default. */
5615 if (value == 0)
5617 value = enum_next_value;
5618 if (enum_overflow)
5619 error ("overflow in enumeration values");
5622 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5624 pedwarn ("ISO C restricts enumerator values to range of `int'");
5625 /* XXX This causes -pedantic to change the meaning of the program.
5626 Remove? -zw 2004-03-15 */
5627 value = convert (integer_type_node, value);
5630 /* Set basis for default for next value. */
5631 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5632 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5634 /* Now create a declaration for the enum value name. */
5636 type = TREE_TYPE (value);
5637 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5638 TYPE_PRECISION (integer_type_node)),
5639 (TYPE_PRECISION (type)
5640 >= TYPE_PRECISION (integer_type_node)
5641 && TYPE_UNSIGNED (type)));
5643 decl = build_decl (CONST_DECL, name, type);
5644 DECL_INITIAL (decl) = convert (type, value);
5645 pushdecl (decl);
5647 return tree_cons (decl, value, NULL_TREE);
5651 /* Create the FUNCTION_DECL for a function definition.
5652 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5653 the declaration; they describe the function's name and the type it returns,
5654 but twisted together in a fashion that parallels the syntax of C.
5656 This function creates a binding context for the function body
5657 as well as setting up the FUNCTION_DECL in current_function_decl.
5659 Returns 1 on success. If the DECLARATOR is not suitable for a function
5660 (it defines a datum instead), we return 0, which tells
5661 yyparse to report a parse error. */
5664 start_function (tree declspecs, tree declarator, tree attributes)
5666 tree decl1, old_decl;
5667 tree restype;
5669 current_function_returns_value = 0; /* Assume, until we see it does. */
5670 current_function_returns_null = 0;
5671 current_function_returns_abnormally = 0;
5672 warn_about_return_type = 0;
5673 current_extern_inline = 0;
5674 c_switch_stack = NULL;
5676 /* Indicate no valid break/continue context by setting these variables
5677 to some non-null, non-label value. We'll notice and emit the proper
5678 error message in c_finish_bc_stmt. */
5679 c_break_label = c_cont_label = size_zero_node;
5681 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5683 /* If the declarator is not suitable for a function definition,
5684 cause a syntax error. */
5685 if (decl1 == 0)
5686 return 0;
5688 decl_attributes (&decl1, attributes, 0);
5690 if (DECL_DECLARED_INLINE_P (decl1)
5691 && DECL_UNINLINABLE (decl1)
5692 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5693 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5695 announce_function (decl1);
5697 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5699 error ("return type is an incomplete type");
5700 /* Make it return void instead. */
5701 TREE_TYPE (decl1)
5702 = build_function_type (void_type_node,
5703 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5706 if (warn_about_return_type)
5707 pedwarn_c99 ("return type defaults to `int'");
5709 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5710 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5711 DECL_INITIAL (decl1) = error_mark_node;
5713 /* If this definition isn't a prototype and we had a prototype declaration
5714 before, copy the arg type info from that prototype.
5715 But not if what we had before was a builtin function. */
5716 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5717 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5718 && !DECL_BUILT_IN (old_decl)
5719 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5720 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5721 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5723 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5724 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5727 /* Optionally warn of old-fashioned def with no previous prototype. */
5728 if (warn_strict_prototypes
5729 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5730 && C_DECL_ISNT_PROTOTYPE (old_decl))
5731 warning ("function declaration isn't a prototype");
5732 /* Optionally warn of any global def with no previous prototype. */
5733 else if (warn_missing_prototypes
5734 && TREE_PUBLIC (decl1)
5735 && ! MAIN_NAME_P (DECL_NAME (decl1))
5736 && C_DECL_ISNT_PROTOTYPE (old_decl))
5737 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5738 /* Optionally warn of any def with no previous prototype
5739 if the function has already been used. */
5740 else if (warn_missing_prototypes
5741 && old_decl != 0 && TREE_USED (old_decl)
5742 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5743 warning ("%J'%D' was used with no prototype before its definition",
5744 decl1, decl1);
5745 /* Optionally warn of any global def with no previous declaration. */
5746 else if (warn_missing_declarations
5747 && TREE_PUBLIC (decl1)
5748 && old_decl == 0
5749 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5750 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5751 /* Optionally warn of any def with no previous declaration
5752 if the function has already been used. */
5753 else if (warn_missing_declarations
5754 && old_decl != 0 && TREE_USED (old_decl)
5755 && C_DECL_IMPLICIT (old_decl))
5756 warning ("%J`%D' was used with no declaration before its definition",
5757 decl1, decl1);
5759 /* This is a definition, not a reference.
5760 So normally clear DECL_EXTERNAL.
5761 However, `extern inline' acts like a declaration
5762 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5763 DECL_EXTERNAL (decl1) = current_extern_inline;
5765 /* This function exists in static storage.
5766 (This does not mean `static' in the C sense!) */
5767 TREE_STATIC (decl1) = 1;
5769 /* A nested function is not global. */
5770 if (current_function_decl != 0)
5771 TREE_PUBLIC (decl1) = 0;
5773 #ifdef ENABLE_CHECKING
5774 /* This is the earliest point at which we might know the assembler
5775 name of the function. Thus, if it's set before this, die horribly. */
5776 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5777 abort ();
5778 #endif
5780 /* If #pragma weak was used, mark the decl weak now. */
5781 if (current_scope == file_scope)
5782 maybe_apply_pragma_weak (decl1);
5784 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5785 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5787 tree args;
5788 int argct = 0;
5790 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5791 != integer_type_node)
5792 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5794 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5795 args = TREE_CHAIN (args))
5797 tree type = args ? TREE_VALUE (args) : 0;
5799 if (type == void_type_node)
5800 break;
5802 ++argct;
5803 switch (argct)
5805 case 1:
5806 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5807 pedwarn ("%Jfirst argument of '%D' should be `int'",
5808 decl1, decl1);
5809 break;
5811 case 2:
5812 if (TREE_CODE (type) != POINTER_TYPE
5813 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5814 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5815 != char_type_node))
5816 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5817 decl1, decl1);
5818 break;
5820 case 3:
5821 if (TREE_CODE (type) != POINTER_TYPE
5822 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5823 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5824 != char_type_node))
5825 pedwarn ("%Jthird argument of '%D' should probably be "
5826 "'char **'", decl1, decl1);
5827 break;
5831 /* It is intentional that this message does not mention the third
5832 argument because it's only mentioned in an appendix of the
5833 standard. */
5834 if (argct > 0 && (argct < 2 || argct > 3))
5835 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5837 if (! TREE_PUBLIC (decl1))
5838 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5841 /* Record the decl so that the function name is defined.
5842 If we already have a decl for this name, and it is a FUNCTION_DECL,
5843 use the old decl. */
5845 current_function_decl = pushdecl (decl1);
5847 push_scope ();
5848 declare_parm_level ();
5850 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5851 /* Promote the value to int before returning it. */
5852 if (c_promoting_integer_type_p (restype))
5854 /* It retains unsignedness if not really getting wider. */
5855 if (TYPE_UNSIGNED (restype)
5856 && (TYPE_PRECISION (restype)
5857 == TYPE_PRECISION (integer_type_node)))
5858 restype = unsigned_type_node;
5859 else
5860 restype = integer_type_node;
5862 DECL_RESULT (current_function_decl)
5863 = build_decl (RESULT_DECL, NULL_TREE, restype);
5865 start_fname_decls ();
5867 return 1;
5870 /* Subroutine of store_parm_decls which handles new-style function
5871 definitions (prototype format). The parms already have decls, so we
5872 need only record them as in effect and complain if any redundant
5873 old-style parm decls were written. */
5874 static void
5875 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5877 tree decl;
5878 tree parms = ARG_INFO_PARMS (arg_info);
5879 tree tags = ARG_INFO_TAGS (arg_info);
5880 tree others = ARG_INFO_OTHERS (arg_info);
5882 if (current_scope->bindings)
5884 error ("%Jold-style parameter declarations in prototyped "
5885 "function definition", fndecl);
5887 /* Get rid of the old-style declarations. */
5888 pop_scope ();
5889 push_scope ();
5891 /* Don't issue this warning for nested functions, and don't issue this
5892 warning if we got here because ARG_INFO_TYPES was error_mark_node
5893 (this happens when a function definition has just an ellipsis in
5894 its parameter list). */
5895 else if (warn_traditional && !in_system_header && !current_function_scope
5896 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5897 warning ("%Jtraditional C rejects ISO C style function definitions",
5898 fndecl);
5900 /* Now make all the parameter declarations visible in the function body.
5901 We can bypass most of the grunt work of pushdecl. */
5902 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5904 DECL_CONTEXT (decl) = current_function_decl;
5905 if (DECL_NAME (decl))
5906 bind (DECL_NAME (decl), decl, current_scope,
5907 /*invisible=*/false, /*nested=*/false);
5908 else
5909 error ("%Jparameter name omitted", decl);
5912 /* Record the parameter list in the function declaration. */
5913 DECL_ARGUMENTS (fndecl) = parms;
5915 /* Now make all the ancillary declarations visible, likewise. */
5916 for (decl = others; decl; decl = TREE_CHAIN (decl))
5918 DECL_CONTEXT (decl) = current_function_decl;
5919 if (DECL_NAME (decl))
5920 bind (DECL_NAME (decl), decl, current_scope,
5921 /*invisible=*/false, /*nested=*/false);
5924 /* And all the tag declarations. */
5925 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5926 if (TREE_PURPOSE (decl))
5927 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5928 /*invisible=*/false, /*nested=*/false);
5931 /* Subroutine of store_parm_decls which handles old-style function
5932 definitions (separate parameter list and declarations). */
5934 static void
5935 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5937 struct c_binding *b;
5938 tree parm, decl, last;
5939 tree parmids = ARG_INFO_PARMS (arg_info);
5941 /* We use DECL_WEAK as a flag to show which parameters have been
5942 seen already, since it is not used on PARM_DECL. */
5943 #ifdef ENABLE_CHECKING
5944 for (b = current_scope->bindings; b; b = b->prev)
5945 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5946 abort ();
5947 #endif
5949 if (warn_old_style_definition && !in_system_header)
5950 warning ("%Jold-style function definition", fndecl);
5952 /* Match each formal parameter name with its declaration. Save each
5953 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5954 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5956 if (TREE_VALUE (parm) == 0)
5958 error ("%Jparameter name missing from parameter list", fndecl);
5959 TREE_PURPOSE (parm) = 0;
5960 continue;
5963 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5964 if (b && B_IN_CURRENT_SCOPE (b))
5966 decl = b->decl;
5967 /* If we got something other than a PARM_DECL it is an error. */
5968 if (TREE_CODE (decl) != PARM_DECL)
5969 error ("%J'%D' declared as a non-parameter", decl, decl);
5970 /* If the declaration is already marked, we have a duplicate
5971 name. Complain and ignore the duplicate. */
5972 else if (DECL_WEAK (decl))
5974 error ("%Jmultiple parameters named '%D'", decl, decl);
5975 TREE_PURPOSE (parm) = 0;
5976 continue;
5978 /* If the declaration says "void", complain and turn it into
5979 an int. */
5980 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5982 error ("%Jparameter '%D' declared with void type", decl, decl);
5983 TREE_TYPE (decl) = integer_type_node;
5984 DECL_ARG_TYPE (decl) = integer_type_node;
5985 layout_decl (decl, 0);
5988 /* If no declaration found, default to int. */
5989 else
5991 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5992 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5993 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5994 pushdecl (decl);
5996 if (flag_isoc99)
5997 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5998 else if (extra_warnings)
5999 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
6002 TREE_PURPOSE (parm) = decl;
6003 DECL_WEAK (decl) = 1;
6006 /* Now examine the parms chain for incomplete declarations
6007 and declarations with no corresponding names. */
6009 for (b = current_scope->bindings; b; b = b->prev)
6011 parm = b->decl;
6012 if (TREE_CODE (parm) != PARM_DECL)
6013 continue;
6015 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6017 error ("%Jparameter '%D' has incomplete type", parm, parm);
6018 TREE_TYPE (parm) = error_mark_node;
6021 if (! DECL_WEAK (parm))
6023 error ("%Jdeclaration for parameter '%D' but no such parameter",
6024 parm, parm);
6026 /* Pretend the parameter was not missing.
6027 This gets us to a standard state and minimizes
6028 further error messages. */
6029 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6033 /* Chain the declarations together in the order of the list of
6034 names. Store that chain in the function decl, replacing the
6035 list of names. Update the current scope to match. */
6036 DECL_ARGUMENTS (fndecl) = 0;
6038 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6039 if (TREE_PURPOSE (parm))
6040 break;
6041 if (parm && TREE_PURPOSE (parm))
6043 last = TREE_PURPOSE (parm);
6044 DECL_ARGUMENTS (fndecl) = last;
6045 DECL_WEAK (last) = 0;
6047 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6048 if (TREE_PURPOSE (parm))
6050 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6051 last = TREE_PURPOSE (parm);
6052 DECL_WEAK (last) = 0;
6054 TREE_CHAIN (last) = 0;
6057 /* If there was a previous prototype,
6058 set the DECL_ARG_TYPE of each argument according to
6059 the type previously specified, and report any mismatches. */
6061 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6063 tree type;
6064 for (parm = DECL_ARGUMENTS (fndecl),
6065 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6066 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6067 != void_type_node));
6068 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6070 if (parm == 0 || type == 0
6071 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6073 error ("number of arguments doesn't match prototype");
6074 error ("%Hprototype declaration",
6075 &current_function_prototype_locus);
6076 break;
6078 /* Type for passing arg must be consistent with that
6079 declared for the arg. ISO C says we take the unqualified
6080 type for parameters declared with qualified type. */
6081 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6082 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6084 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6085 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6087 /* Adjust argument to match prototype. E.g. a previous
6088 `int foo(float);' prototype causes
6089 `int foo(x) float x; {...}' to be treated like
6090 `int foo(float x) {...}'. This is particularly
6091 useful for argument types like uid_t. */
6092 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6094 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6095 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6096 && TYPE_PRECISION (TREE_TYPE (parm))
6097 < TYPE_PRECISION (integer_type_node))
6098 DECL_ARG_TYPE (parm) = integer_type_node;
6100 if (pedantic)
6102 pedwarn ("promoted argument '%D' "
6103 "doesn't match prototype", parm);
6104 pedwarn ("%Hprototype declaration",
6105 &current_function_prototype_locus);
6108 else
6110 error ("argument '%D' doesn't match prototype", parm);
6111 error ("%Hprototype declaration",
6112 &current_function_prototype_locus);
6116 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6119 /* Otherwise, create a prototype that would match. */
6121 else
6123 tree actual = 0, last = 0, type;
6125 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6127 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6128 if (last)
6129 TREE_CHAIN (last) = type;
6130 else
6131 actual = type;
6132 last = type;
6134 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6135 if (last)
6136 TREE_CHAIN (last) = type;
6137 else
6138 actual = type;
6140 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6141 of the type of this function, but we need to avoid having this
6142 affect the types of other similarly-typed functions, so we must
6143 first force the generation of an identical (but separate) type
6144 node for the relevant function type. The new node we create
6145 will be a variant of the main variant of the original function
6146 type. */
6148 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6150 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6154 /* Store the parameter declarations into the current function declaration.
6155 This is called after parsing the parameter declarations, before
6156 digesting the body of the function.
6158 For an old-style definition, construct a prototype out of the old-style
6159 parameter declarations and inject it into the function's type. */
6161 void
6162 store_parm_decls (void)
6164 tree fndecl = current_function_decl;
6166 /* The argument information block for FNDECL. */
6167 tree arg_info = DECL_ARGUMENTS (fndecl);
6169 /* True if this definition is written with a prototype. Note:
6170 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6171 list in a function definition as equivalent to (void) -- an
6172 empty argument list specifies the function has no parameters,
6173 but only (void) sets up a prototype for future calls. */
6174 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6176 if (proto)
6177 store_parm_decls_newstyle (fndecl, arg_info);
6178 else
6179 store_parm_decls_oldstyle (fndecl, arg_info);
6181 /* The next call to push_scope will be a function body. */
6183 next_is_function_body = true;
6185 /* Write a record describing this function definition to the prototypes
6186 file (if requested). */
6188 gen_aux_info_record (fndecl, 1, 0, proto);
6190 /* Initialize the RTL code for the function. */
6191 allocate_struct_function (fndecl);
6193 /* Begin the statement tree for this function. */
6194 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6196 /* ??? Insert the contents of the pending sizes list into the function
6197 to be evaluated. This just changes mis-behaviour until assign_parms
6198 phase ordering problems are resolved. */
6200 tree t;
6201 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6202 add_stmt (TREE_VALUE (t));
6205 /* Even though we're inside a function body, we still don't want to
6206 call expand_expr to calculate the size of a variable-sized array.
6207 We haven't necessarily assigned RTL to all variables yet, so it's
6208 not safe to try to expand expressions involving them. */
6209 cfun->x_dont_save_pending_sizes_p = 1;
6212 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6214 static void
6215 c_finalize (tree fndecl)
6217 struct cgraph_node *cgn;
6219 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6220 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6222 /* ??? Objc emits functions after finalizing the compilation unit.
6223 This should be cleaned up later and this conditional removed. */
6224 if (cgraph_global_info_ready)
6226 c_expand_body (fndecl);
6227 return;
6230 /* Finalize all nested functions now. */
6231 cgn = cgraph_node (fndecl);
6232 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6233 c_finalize (cgn->decl);
6235 cgraph_finalize_function (fndecl, false);
6238 /* Finish up a function declaration and compile that function
6239 all the way to assembler language output. The free the storage
6240 for the function definition.
6242 This is called after parsing the body of the function definition. */
6244 void
6245 finish_function (void)
6247 tree fndecl = current_function_decl;
6249 if (TREE_CODE (fndecl) == FUNCTION_DECL
6250 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6252 tree args = DECL_ARGUMENTS (fndecl);
6253 for (; args; args = TREE_CHAIN (args))
6255 tree type = TREE_TYPE (args);
6256 if (INTEGRAL_TYPE_P (type)
6257 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6258 DECL_ARG_TYPE (args) = integer_type_node;
6262 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6263 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6265 /* Must mark the RESULT_DECL as being in this function. */
6267 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6268 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6270 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6272 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6273 != integer_type_node)
6275 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6276 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6277 if (!warn_main)
6278 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6280 else
6282 if (flag_isoc99)
6283 c_finish_return (integer_zero_node);
6287 /* Tie off the statement tree for this function. */
6288 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6290 finish_fname_decls ();
6292 /* Complain if there's just no return statement. */
6293 if (warn_return_type
6294 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6295 && !current_function_returns_value && !current_function_returns_null
6296 /* Don't complain if we abort. */
6297 && !current_function_returns_abnormally
6298 /* Don't warn for main(). */
6299 && !MAIN_NAME_P (DECL_NAME (fndecl))
6300 /* Or if they didn't actually specify a return type. */
6301 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6302 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6303 inline function, as we might never be compiled separately. */
6304 && DECL_INLINE (fndecl))
6305 warning ("no return statement in function returning non-void");
6307 /* With just -Wextra, complain only if function returns both with
6308 and without a value. */
6309 if (extra_warnings
6310 && current_function_returns_value
6311 && current_function_returns_null)
6312 warning ("this function may return with or without a value");
6314 /* Store the end of the function, so that we get good line number
6315 info for the epilogue. */
6316 cfun->function_end_locus = input_location;
6318 /* If we don't have ctors/dtors sections, and this is a static
6319 constructor or destructor, it must be recorded now. */
6320 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6321 && !targetm.have_ctors_dtors)
6322 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6323 if (DECL_STATIC_DESTRUCTOR (fndecl)
6324 && !targetm.have_ctors_dtors)
6325 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6327 /* Genericize before inlining. Delay genericizing nested functions
6328 until their parent function is genericized. Since finalizing
6329 requires GENERIC, delay that as well. */
6331 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6333 if (!decl_function_context (fndecl))
6335 c_genericize (fndecl);
6336 lower_nested_functions (fndecl);
6337 c_finalize (fndecl);
6339 else
6341 /* Register this function with cgraph just far enough to get it
6342 added to our parent's nested function list. Handy, since the
6343 C front end doesn't have such a list. */
6344 (void) cgraph_node (fndecl);
6348 /* We're leaving the context of this function, so zap cfun.
6349 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6350 tree_rest_of_compilation. */
6351 cfun = NULL;
6352 current_function_decl = NULL;
6355 /* Generate the RTL for the body of FNDECL. */
6357 void
6358 c_expand_body (tree fndecl)
6361 if (!DECL_INITIAL (fndecl)
6362 || DECL_INITIAL (fndecl) == error_mark_node)
6363 return;
6365 tree_rest_of_compilation (fndecl, false);
6367 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6368 && targetm.have_ctors_dtors)
6369 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6370 DEFAULT_INIT_PRIORITY);
6371 if (DECL_STATIC_DESTRUCTOR (fndecl)
6372 && targetm.have_ctors_dtors)
6373 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6374 DEFAULT_INIT_PRIORITY);
6377 /* Check the declarations given in a for-loop for satisfying the C99
6378 constraints. */
6379 void
6380 check_for_loop_decls (void)
6382 struct c_binding *b;
6384 if (!flag_isoc99)
6386 /* If we get here, declarations have been used in a for loop without
6387 the C99 for loop scope. This doesn't make much sense, so don't
6388 allow it. */
6389 error ("'for' loop initial declaration used outside C99 mode");
6390 return;
6392 /* C99 subclause 6.8.5 paragraph 3:
6394 [#3] The declaration part of a for statement shall only
6395 declare identifiers for objects having storage class auto or
6396 register.
6398 It isn't clear whether, in this sentence, "identifiers" binds to
6399 "shall only declare" or to "objects" - that is, whether all identifiers
6400 declared must be identifiers for objects, or whether the restriction
6401 only applies to those that are. (A question on this in comp.std.c
6402 in November 2000 received no answer.) We implement the strictest
6403 interpretation, to avoid creating an extension which later causes
6404 problems. */
6406 for (b = current_scope->bindings; b; b = b->prev)
6408 tree id = b->id;
6409 tree decl = b->decl;
6411 if (!id)
6412 continue;
6414 switch (TREE_CODE (decl))
6416 case VAR_DECL:
6417 if (TREE_STATIC (decl))
6418 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6419 "initial declaration", decl, decl);
6420 else if (DECL_EXTERNAL (decl))
6421 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6422 "initial declaration", decl, decl);
6423 break;
6425 case RECORD_TYPE:
6426 error ("'struct %E' declared in 'for' loop initial declaration", id);
6427 break;
6428 case UNION_TYPE:
6429 error ("'union %E' declared in 'for' loop initial declaration", id);
6430 break;
6431 case ENUMERAL_TYPE:
6432 error ("'enum %E' declared in 'for' loop initial declaration", id);
6433 break;
6434 default:
6435 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6436 "initial declaration", decl, decl);
6441 /* Save and reinitialize the variables
6442 used during compilation of a C function. */
6444 void
6445 c_push_function_context (struct function *f)
6447 struct language_function *p;
6448 p = ggc_alloc (sizeof (struct language_function));
6449 f->language = p;
6451 p->base.x_stmt_tree = c_stmt_tree;
6452 p->x_break_label = c_break_label;
6453 p->x_cont_label = c_cont_label;
6454 p->x_switch_stack = c_switch_stack;
6455 p->returns_value = current_function_returns_value;
6456 p->returns_null = current_function_returns_null;
6457 p->returns_abnormally = current_function_returns_abnormally;
6458 p->warn_about_return_type = warn_about_return_type;
6459 p->extern_inline = current_extern_inline;
6462 /* Restore the variables used during compilation of a C function. */
6464 void
6465 c_pop_function_context (struct function *f)
6467 struct language_function *p = f->language;
6469 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6470 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6472 /* Stop pointing to the local nodes about to be freed. */
6473 /* But DECL_INITIAL must remain nonzero so we know this
6474 was an actual function definition. */
6475 DECL_INITIAL (current_function_decl) = error_mark_node;
6476 DECL_ARGUMENTS (current_function_decl) = 0;
6479 c_stmt_tree = p->base.x_stmt_tree;
6480 c_break_label = p->x_break_label;
6481 c_cont_label = p->x_cont_label;
6482 c_switch_stack = p->x_switch_stack;
6483 current_function_returns_value = p->returns_value;
6484 current_function_returns_null = p->returns_null;
6485 current_function_returns_abnormally = p->returns_abnormally;
6486 warn_about_return_type = p->warn_about_return_type;
6487 current_extern_inline = p->extern_inline;
6489 f->language = NULL;
6492 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6494 void
6495 c_dup_lang_specific_decl (tree decl)
6497 struct lang_decl *ld;
6499 if (!DECL_LANG_SPECIFIC (decl))
6500 return;
6502 ld = ggc_alloc (sizeof (struct lang_decl));
6503 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6504 DECL_LANG_SPECIFIC (decl) = ld;
6507 /* The functions below are required for functionality of doing
6508 function at once processing in the C front end. Currently these
6509 functions are not called from anywhere in the C front end, but as
6510 these changes continue, that will change. */
6512 /* Returns nonzero if the current statement is a full expression,
6513 i.e. temporaries created during that statement should be destroyed
6514 at the end of the statement. */
6517 stmts_are_full_exprs_p (void)
6519 return 0;
6522 /* Returns the stmt_tree (if any) to which statements are currently
6523 being added. If there is no active statement-tree, NULL is
6524 returned. */
6526 stmt_tree
6527 current_stmt_tree (void)
6529 return &c_stmt_tree;
6532 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6533 C. */
6536 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6538 return 0;
6541 /* Dummy function in place of callback used by C++. */
6543 void
6544 extract_interface_info (void)
6548 /* Return the global value of T as a symbol. */
6550 tree
6551 identifier_global_value (tree t)
6553 struct c_binding *b;
6555 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6556 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6557 return b->decl;
6559 return 0;
6562 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6563 otherwise the name is found in ridpointers from RID_INDEX. */
6565 void
6566 record_builtin_type (enum rid rid_index, const char *name, tree type)
6568 tree id;
6569 if (name == 0)
6570 id = ridpointers[(int) rid_index];
6571 else
6572 id = get_identifier (name);
6573 pushdecl (build_decl (TYPE_DECL, id, type));
6576 /* Build the void_list_node (void_type_node having been created). */
6577 tree
6578 build_void_list_node (void)
6580 tree t = build_tree_list (NULL_TREE, void_type_node);
6581 return t;
6584 /* Return something to represent absolute declarators containing a *.
6585 TARGET is the absolute declarator that the * contains.
6586 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6587 to apply to the pointer type, represented as identifiers, possible mixed
6588 with attributes.
6590 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6591 if attributes are present) and whose type is the modifier list. */
6593 tree
6594 make_pointer_declarator (tree type_quals_attrs, tree target)
6596 tree quals, attrs;
6597 tree itarget = target;
6598 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6599 if (attrs != NULL_TREE)
6600 itarget = tree_cons (attrs, target, NULL_TREE);
6601 return build1 (INDIRECT_REF, quals, itarget);
6604 /* Synthesize a function which calls all the global ctors or global
6605 dtors in this file. This is only used for targets which do not
6606 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6607 static void
6608 build_cdtor (int method_type, tree cdtors)
6610 tree body = 0;
6612 if (!cdtors)
6613 return;
6615 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6616 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6617 &body);
6619 cgraph_build_static_cdtor (method_type, body);
6622 /* Perform final processing on one file scope's declarations (or the
6623 external scope's declarations), GLOBALS. */
6624 static void
6625 c_write_global_declarations_1 (tree globals)
6627 size_t len = list_length (globals);
6628 tree *vec = xmalloc (sizeof (tree) * len);
6629 size_t i;
6630 tree decl;
6632 /* Process the decls in the order they were written. */
6633 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6634 vec[i] = decl;
6636 wrapup_global_declarations (vec, len);
6637 check_global_declarations (vec, len);
6639 free (vec);
6642 void
6643 c_write_global_declarations (void)
6645 tree ext_block, t;
6647 /* We don't want to do this if generating a PCH. */
6648 if (pch_file)
6649 return;
6651 /* Don't waste time on further processing if -fsyntax-only or we've
6652 encountered errors. */
6653 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
6654 return;
6656 /* Close the external scope. */
6657 ext_block = pop_scope ();
6658 external_scope = 0;
6659 if (current_scope)
6660 abort ();
6662 /* Process all file scopes in this compilation, and the external_scope,
6663 through wrapup_global_declarations and check_global_declarations. */
6664 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6665 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6666 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6668 /* Generate functions to call static constructors and destructors
6669 for targets that do not support .ctors/.dtors sections. These
6670 functions have magic names which are detected by collect2. */
6671 build_cdtor ('I', static_ctors); static_ctors = 0;
6672 build_cdtor ('D', static_dtors); static_dtors = 0;
6674 /* We're done parsing; proceed to optimize and emit assembly.
6675 FIXME: shouldn't be the front end's responsibility to call this. */
6676 cgraph_optimize ();
6678 /* Presently this has to happen after cgraph_optimize.
6679 FIXME: shouldn't be the front end's responsibility to call this. */
6680 if (flag_mudflap)
6681 mudflap_finish_file ();
6684 #include "gt-c-decl.h"