* config/mn10300/mn10300-protos.h (mn10300_va_arg): Remove.
[official-gcc.git] / gcc / c-decl.c
blob8346c8a82e779b22f49266fbee771660140fadf9
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 /* Exit a scope. Restore the state of the identifier-decl mappings
626 that were in effect when this scope was entered. Return a BLOCK
627 node containing all the DECLs in this scope that are of interest
628 to debug info generation. */
630 tree
631 pop_scope (void)
633 struct c_scope *scope = current_scope;
634 tree block, context, p;
635 struct c_binding *b;
637 bool functionbody = scope->function_body;
638 bool keep = functionbody || scope->keep || scope->bindings;
640 /* If appropriate, create a BLOCK to record the decls for the life
641 of this function. */
642 block = 0;
643 if (keep)
645 block = make_node (BLOCK);
646 BLOCK_SUBBLOCKS (block) = scope->blocks;
647 TREE_USED (block) = 1;
649 /* In each subblock, record that this is its superior. */
650 for (p = scope->blocks; p; p = TREE_CHAIN (p))
651 BLOCK_SUPERCONTEXT (p) = block;
653 BLOCK_VARS (block) = 0;
656 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
657 scope must be set so that they point to the appropriate
658 construct, i.e. either to the current FUNCTION_DECL node, or
659 else to the BLOCK node we just constructed.
661 Note that for tagged types whose scope is just the formal
662 parameter list for some function type specification, we can't
663 properly set their TYPE_CONTEXTs here, because we don't have a
664 pointer to the appropriate FUNCTION_TYPE node readily available
665 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
666 type nodes get set in `grokdeclarator' as soon as we have created
667 the FUNCTION_TYPE node which will represent the "scope" for these
668 "parameter list local" tagged types. */
669 if (scope->function_body)
670 context = current_function_decl;
671 else if (scope == file_scope)
673 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
674 TREE_CHAIN (file_decl) = all_translation_units;
675 all_translation_units = file_decl;
676 context = file_decl;
678 else
679 context = block;
681 /* Clear all bindings in this scope. */
682 for (b = scope->bindings; b; b = free_binding_and_advance (b))
684 p = b->decl;
685 switch (TREE_CODE (p))
687 case LABEL_DECL:
688 /* Warnings for unused labels, errors for undefined labels. */
689 if (TREE_USED (p) && !DECL_INITIAL (p))
691 error ("%Jlabel `%D' used but not defined", p, p);
692 DECL_INITIAL (p) = error_mark_node;
694 else if (!TREE_USED (p) && warn_unused_label)
696 if (DECL_INITIAL (p))
697 warning ("%Jlabel `%D' defined but not used", p, p);
698 else
699 warning ("%Jlabel `%D' declared but not defined", p, p);
701 /* Labels go in BLOCK_VARS. */
702 TREE_CHAIN (p) = BLOCK_VARS (block);
703 BLOCK_VARS (block) = p;
705 #ifdef ENABLE_CHECKING
706 if (I_LABEL_BINDING (b->id) != b) abort ();
707 #endif
708 I_LABEL_BINDING (b->id) = b->shadowed;
709 break;
711 case ENUMERAL_TYPE:
712 case UNION_TYPE:
713 case RECORD_TYPE:
714 TYPE_CONTEXT (p) = context;
716 /* Types may not have tag-names, in which case the type
717 appears in the bindings list with b->id NULL. */
718 if (b->id)
720 #ifdef ENABLE_CHECKING
721 if (I_TAG_BINDING (b->id) != b) abort ();
722 #endif
723 I_TAG_BINDING (b->id) = b->shadowed;
725 break;
727 case FUNCTION_DECL:
728 /* Propagate TREE_ADDRESSABLE from nested functions to their
729 containing functions. */
730 if (! TREE_ASM_WRITTEN (p)
731 && DECL_INITIAL (p) != 0
732 && TREE_ADDRESSABLE (p)
733 && DECL_ABSTRACT_ORIGIN (p) != 0
734 && DECL_ABSTRACT_ORIGIN (p) != p)
735 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
736 goto common_symbol;
738 case VAR_DECL:
739 /* Warnings for unused variables. Keep this in sync with
740 stmt.c:warn_about_unused_variables, which we cannot use
741 since it expects a different data structure. */
742 if (warn_unused_variable
743 && !TREE_USED (p)
744 && !DECL_IN_SYSTEM_HEADER (p)
745 && DECL_NAME (p)
746 && !DECL_ARTIFICIAL (p)
747 && (scope != file_scope
748 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
749 && !TREE_THIS_VOLATILE (p)))
750 && scope != external_scope)
751 warning ("%Junused variable `%D'", p, p);
753 /* Fall through. */
754 case TYPE_DECL:
755 case CONST_DECL:
756 common_symbol:
757 /* All of these go in BLOCK_VARS, but only if this is the
758 binding in the home scope. */
759 if (!b->nested)
761 TREE_CHAIN (p) = BLOCK_VARS (block);
762 BLOCK_VARS (block) = p;
764 /* If this is the file scope, and we are processing more
765 than one translation unit in this compilation, set
766 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
767 This makes same_translation_unit_p work, and causes
768 static declarations to be given disambiguating suffixes. */
769 if (scope == file_scope && num_in_fnames > 1)
770 DECL_CONTEXT (p) = context;
772 /* Fall through. */
773 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
774 already been put there by store_parm_decls. Unused-
775 parameter warnings are handled by function.c.
776 error_mark_node obviously does not go in BLOCK_VARS and
777 does not get unused-variable warnings. */
778 case PARM_DECL:
779 case ERROR_MARK:
780 /* It is possible for a decl not to have a name. We get
781 here with b->id NULL in this case. */
782 if (b->id)
784 #ifdef ENABLE_CHECKING
785 if (I_SYMBOL_BINDING (b->id) != b) abort ();
786 #endif
787 I_SYMBOL_BINDING (b->id) = b->shadowed;
789 break;
791 default:
792 abort ();
797 /* Dispose of the block that we just made inside some higher level. */
798 if ((scope->function_body || scope == file_scope) && context)
800 DECL_INITIAL (context) = block;
801 BLOCK_SUPERCONTEXT (block) = context;
803 else if (scope->outer)
805 if (block)
806 SCOPE_LIST_APPEND (scope->outer, blocks, block);
807 /* If we did not make a block for the scope just exited, any
808 blocks made for inner scopes must be carried forward so they
809 will later become subblocks of something else. */
810 else if (scope->blocks)
811 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
814 /* Pop the current scope, and free the structure for reuse. */
815 current_scope = scope->outer;
816 if (scope->function_body)
817 current_function_scope = scope->outer_function;
819 memset (scope, 0, sizeof (struct c_scope));
820 scope->outer = scope_freelist;
821 scope_freelist = scope;
823 return block;
826 void
827 push_file_scope (void)
829 tree decl;
831 if (file_scope)
832 return;
834 push_scope ();
835 file_scope = current_scope;
837 start_fname_decls ();
839 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
840 bind (DECL_NAME (decl), decl, file_scope,
841 /*invisible=*/false, /*nested=*/true);
844 void
845 pop_file_scope (void)
847 /* In case there were missing closebraces, get us back to the global
848 binding level. */
849 while (current_scope != file_scope)
850 pop_scope ();
852 /* __FUNCTION__ is defined at file scope (""). This
853 call may not be necessary as my tests indicate it
854 still works without it. */
855 finish_fname_decls ();
857 /* This is the point to write out a PCH if we're doing that.
858 In that case we do not want to do anything else. */
859 if (pch_file)
861 c_common_write_pch ();
862 return;
865 /* Pop off the file scope and close this translation unit. */
866 pop_scope ();
867 file_scope = 0;
868 cpp_undef_all (parse_in);
869 cgraph_finalize_compilation_unit ();
872 /* Insert BLOCK at the end of the list of subblocks of the current
873 scope. This is used when a BIND_EXPR is expanded, to handle the
874 BLOCK node inside the BIND_EXPR. */
876 void
877 insert_block (tree block)
879 TREE_USED (block) = 1;
880 SCOPE_LIST_APPEND (current_scope, blocks, block);
883 /* Push a definition or a declaration of struct, union or enum tag "name".
884 "type" should be the type node.
885 We assume that the tag "name" is not already defined.
887 Note that the definition may really be just a forward reference.
888 In that case, the TYPE_SIZE will be zero. */
890 static void
891 pushtag (tree name, tree type)
893 /* Record the identifier as the type's name if it has none. */
894 if (name && !TYPE_NAME (type))
895 TYPE_NAME (type) = name;
896 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
898 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
899 tagged type we just added to the current scope. This fake
900 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
901 to output a representation of a tagged type, and it also gives
902 us a convenient place to record the "scope start" address for the
903 tagged type. */
905 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
907 /* An approximation for now, so we can tell this is a function-scope tag.
908 This will be updated in pop_scope. */
909 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
912 /* Subroutine of compare_decls. Allow harmless mismatches in return
913 and argument types provided that the type modes match. This function
914 return a unified type given a suitable match, and 0 otherwise. */
916 static tree
917 match_builtin_function_types (tree newtype, tree oldtype)
919 tree newrettype, oldrettype;
920 tree newargs, oldargs;
921 tree trytype, tryargs;
923 /* Accept the return type of the new declaration if same modes. */
924 oldrettype = TREE_TYPE (oldtype);
925 newrettype = TREE_TYPE (newtype);
927 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
928 return 0;
930 oldargs = TYPE_ARG_TYPES (oldtype);
931 newargs = TYPE_ARG_TYPES (newtype);
932 tryargs = newargs;
934 while (oldargs || newargs)
936 if (! oldargs
937 || ! newargs
938 || ! TREE_VALUE (oldargs)
939 || ! TREE_VALUE (newargs)
940 || TYPE_MODE (TREE_VALUE (oldargs))
941 != TYPE_MODE (TREE_VALUE (newargs)))
942 return 0;
944 oldargs = TREE_CHAIN (oldargs);
945 newargs = TREE_CHAIN (newargs);
948 trytype = build_function_type (newrettype, tryargs);
949 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
952 /* Subroutine of diagnose_mismatched_decls. Check for function type
953 mismatch involving an empty arglist vs a nonempty one and give clearer
954 diagnostics. */
955 static void
956 diagnose_arglist_conflict (tree newdecl, tree olddecl,
957 tree newtype, tree oldtype)
959 tree t;
961 if (TREE_CODE (olddecl) != FUNCTION_DECL
962 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
963 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
965 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
966 return;
968 t = TYPE_ARG_TYPES (oldtype);
969 if (t == 0)
970 t = TYPE_ARG_TYPES (newtype);
971 for (; t; t = TREE_CHAIN (t))
973 tree type = TREE_VALUE (t);
975 if (TREE_CHAIN (t) == 0
976 && TYPE_MAIN_VARIANT (type) != void_type_node)
978 inform ("a parameter list with an ellipsis can't match "
979 "an empty parameter name list declaration");
980 break;
983 if (c_type_promotes_to (type) != type)
985 inform ("an argument type that has a default promotion can't match "
986 "an empty parameter name list declaration");
987 break;
992 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
993 old-style function definition, NEWDECL is a prototype declaration.
994 Diagnose inconsistencies in the argument list. Returns TRUE if
995 the prototype is compatible, FALSE if not. */
996 static bool
997 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
999 tree newargs, oldargs;
1000 int i;
1002 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1003 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1005 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1006 newargs = TYPE_ARG_TYPES (newtype);
1007 i = 1;
1009 for (;;)
1011 tree oldargtype = TREE_VALUE (oldargs);
1012 tree newargtype = TREE_VALUE (newargs);
1014 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1015 break;
1017 /* Reaching the end of just one list means the two decls don't
1018 agree on the number of arguments. */
1019 if (END_OF_ARGLIST (oldargtype))
1021 error ("%Jprototype for '%D' declares more arguments "
1022 "than previous old-style definition", newdecl, newdecl);
1023 return false;
1025 else if (END_OF_ARGLIST (newargtype))
1027 error ("%Jprototype for '%D' declares fewer arguments "
1028 "than previous old-style definition", newdecl, newdecl);
1029 return false;
1032 /* Type for passing arg must be consistent with that declared
1033 for the arg. */
1034 else if (! comptypes (oldargtype, newargtype))
1036 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1037 newdecl, newdecl, i);
1038 return false;
1041 oldargs = TREE_CHAIN (oldargs);
1042 newargs = TREE_CHAIN (newargs);
1043 i++;
1046 /* If we get here, no errors were found, but do issue a warning
1047 for this poor-style construct. */
1048 warning ("%Jprototype for '%D' follows non-prototype definition",
1049 newdecl, newdecl);
1050 return true;
1051 #undef END_OF_ARGLIST
1054 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1055 first in a pair of mismatched declarations, using the diagnostic
1056 function DIAG. */
1057 static void
1058 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1060 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1062 else if (DECL_INITIAL (decl))
1063 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1064 else if (C_DECL_IMPLICIT (decl))
1065 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1066 else
1067 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1070 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1071 Returns true if the caller should proceed to merge the two, false
1072 if OLDDECL should simply be discarded. As a side effect, issues
1073 all necessary diagnostics for invalid or poor-style combinations.
1074 If it returns true, writes the types of NEWDECL and OLDDECL to
1075 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1076 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1078 static bool
1079 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1080 tree *newtypep, tree *oldtypep)
1082 tree newtype, oldtype;
1083 bool pedwarned = false;
1084 bool warned = false;
1086 /* If we have error_mark_node for either decl or type, just discard
1087 the previous decl - we're in an error cascade already. */
1088 if (olddecl == error_mark_node || newdecl == error_mark_node)
1089 return false;
1090 *oldtypep = oldtype = TREE_TYPE (olddecl);
1091 *newtypep = newtype = TREE_TYPE (newdecl);
1092 if (oldtype == error_mark_node || newtype == error_mark_node)
1093 return false;
1095 /* Two different categories of symbol altogether. This is an error
1096 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1097 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1099 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1100 && DECL_BUILT_IN (olddecl)
1101 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1103 error ("%J'%D' redeclared as different kind of symbol",
1104 newdecl, newdecl);
1105 locate_old_decl (olddecl, error);
1107 else if (TREE_PUBLIC (newdecl))
1108 warning ("%Jbuilt-in function '%D' declared as non-function",
1109 newdecl, newdecl);
1110 else if (warn_shadow)
1111 warning ("%Jdeclaration of '%D' shadows a built-in function",
1112 newdecl, newdecl);
1113 return false;
1116 if (!comptypes (oldtype, newtype))
1118 if (TREE_CODE (olddecl) == FUNCTION_DECL
1119 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1121 /* Accept harmless mismatch in function types.
1122 This is for the ffs and fprintf builtins. */
1123 tree trytype = match_builtin_function_types (newtype, oldtype);
1125 if (trytype && comptypes (newtype, trytype))
1126 *oldtypep = oldtype = trytype;
1127 else
1129 /* If types don't match for a built-in, throw away the
1130 built-in. No point in calling locate_old_decl here, it
1131 won't print anything. */
1132 warning ("%Jconflicting types for built-in function '%D'",
1133 newdecl, newdecl);
1134 return false;
1137 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1138 && DECL_IS_BUILTIN (olddecl))
1140 /* A conflicting function declaration for a predeclared
1141 function that isn't actually built in. Objective C uses
1142 these. The new declaration silently overrides everything
1143 but the volatility (i.e. noreturn) indication. See also
1144 below. FIXME: Make Objective C use normal builtins. */
1145 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1146 return false;
1148 /* Permit void foo (...) to match int foo (...) if the latter is
1149 the definition and implicit int was used. See
1150 c-torture/compile/920625-2.c. */
1151 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1152 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1153 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1154 && C_FUNCTION_IMPLICIT_INT (newdecl))
1156 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1157 /* Make sure we keep void as the return type. */
1158 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1159 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1160 pedwarned = true;
1162 else
1164 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1165 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1166 else
1167 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1168 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1169 locate_old_decl (olddecl, error);
1170 return false;
1174 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1175 but silently ignore the redeclaration if either is in a system
1176 header. (Conflicting redeclarations were handled above.) */
1177 if (TREE_CODE (newdecl) == TYPE_DECL)
1179 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1180 return true; /* Allow OLDDECL to continue in use. */
1182 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1183 locate_old_decl (olddecl, error);
1184 return false;
1187 /* Function declarations can either be 'static' or 'extern' (no
1188 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1189 can never conflict with each other on account of linkage (6.2.2p4).
1190 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1191 two definitions if one is 'extern inline' and one is not. The non-
1192 extern-inline definition supersedes the extern-inline definition. */
1193 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1195 /* If you declare a built-in function name as static, or
1196 define the built-in with an old-style definition (so we
1197 can't validate the argument list) the built-in definition is
1198 overridden, but optionally warn this was a bad choice of name. */
1199 if (DECL_BUILT_IN (olddecl)
1200 && !C_DECL_DECLARED_BUILTIN (olddecl)
1201 && (!TREE_PUBLIC (newdecl)
1202 || (DECL_INITIAL (newdecl)
1203 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1205 if (warn_shadow)
1206 warning ("%Jdeclaration of '%D' shadows a built-in function",
1207 newdecl, newdecl);
1208 /* Discard the old built-in function. */
1209 return false;
1212 if (DECL_INITIAL (newdecl))
1214 if (DECL_INITIAL (olddecl)
1215 && !(DECL_DECLARED_INLINE_P (olddecl)
1216 && DECL_EXTERNAL (olddecl)
1217 && !(DECL_DECLARED_INLINE_P (newdecl)
1218 && DECL_EXTERNAL (newdecl))))
1220 error ("%Jredefinition of '%D'", newdecl, newdecl);
1221 locate_old_decl (olddecl, error);
1222 return false;
1225 /* If we have a prototype after an old-style function definition,
1226 the argument types must be checked specially. */
1227 else if (DECL_INITIAL (olddecl)
1228 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1229 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1230 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1232 locate_old_decl (olddecl, error);
1233 return false;
1235 /* A non-static declaration (even an "extern") followed by a
1236 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1237 The same is true for a static forward declaration at block
1238 scope followed by a non-static declaration/definition at file
1239 scope. Static followed by non-static at the same scope is
1240 not undefined behavior, and is the most convenient way to get
1241 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1242 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1243 we do diagnose it if -Wtraditional. */
1244 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1246 /* Two exceptions to the rule. If olddecl is an extern
1247 inline, or a predeclared function that isn't actually
1248 built in, newdecl silently overrides olddecl. The latter
1249 occur only in Objective C; see also above. (FIXME: Make
1250 Objective C use normal builtins.) */
1251 if (!DECL_IS_BUILTIN (olddecl)
1252 && !(DECL_EXTERNAL (olddecl)
1253 && DECL_DECLARED_INLINE_P (olddecl)))
1255 error ("%Jstatic declaration of '%D' follows "
1256 "non-static declaration", newdecl, newdecl);
1257 locate_old_decl (olddecl, error);
1259 return false;
1261 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1263 if (DECL_CONTEXT (olddecl))
1265 error ("%Jnon-static declaration of '%D' follows "
1266 "static declaration", newdecl, newdecl);
1267 locate_old_decl (olddecl, error);
1268 return false;
1270 else if (warn_traditional)
1272 warning ("%Jnon-static declaration of '%D' follows "
1273 "static declaration", newdecl, newdecl);
1274 warned = true;
1278 else if (TREE_CODE (newdecl) == VAR_DECL)
1280 /* Only variables can be thread-local, and all declarations must
1281 agree on this property. */
1282 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1284 if (DECL_THREAD_LOCAL (newdecl))
1285 error ("%Jthread-local declaration of '%D' follows "
1286 "non-thread-local declaration", newdecl, newdecl);
1287 else
1288 error ("%Jnon-thread-local declaration of '%D' follows "
1289 "thread-local declaration", newdecl, newdecl);
1291 locate_old_decl (olddecl, error);
1292 return false;
1295 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1296 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1298 error ("%Jredefinition of '%D'", newdecl, newdecl);
1299 locate_old_decl (olddecl, error);
1300 return false;
1303 /* Objects declared at file scope: if the first declaration had
1304 external linkage (even if it was an external reference) the
1305 second must have external linkage as well, or the behavior is
1306 undefined. If the first declaration had internal linkage, then
1307 the second must too, or else be an external reference (in which
1308 case the composite declaration still has internal linkage).
1309 As for function declarations, we warn about the static-then-
1310 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1311 if (DECL_FILE_SCOPE_P (newdecl)
1312 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1314 if (DECL_EXTERNAL (newdecl))
1316 if (warn_traditional)
1318 warning ("%Jnon-static declaration of '%D' follows "
1319 "static declaration", newdecl, newdecl);
1320 warned = true;
1323 else
1325 if (TREE_PUBLIC (newdecl))
1326 error ("%Jnon-static declaration of '%D' follows "
1327 "static declaration", newdecl, newdecl);
1328 else
1329 error ("%Jstatic declaration of '%D' follows "
1330 "non-static declaration", newdecl, newdecl);
1332 locate_old_decl (olddecl, error);
1333 return false;
1336 /* Two objects with the same name declared at the same block
1337 scope must both be external references (6.7p3). */
1338 else if (!DECL_FILE_SCOPE_P (newdecl)
1339 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1340 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1342 if (DECL_EXTERNAL (newdecl))
1343 error ("%Jextern declaration of '%D' follows "
1344 "declaration with no linkage", newdecl, newdecl);
1345 else if (DECL_EXTERNAL (olddecl))
1346 error ("%Jdeclaration of '%D' with no linkage follows "
1347 "extern declaration", newdecl, newdecl);
1348 else
1349 error ("%Jredeclaration of '%D' with no linkage",
1350 newdecl, newdecl);
1352 locate_old_decl (olddecl, error);
1353 return false;
1357 /* warnings */
1358 /* All decls must agree on a non-default visibility. */
1359 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1360 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1361 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1363 warning ("%Jredeclaration of '%D' with different visibility "
1364 "(old visibility preserved)", newdecl, newdecl);
1365 warned = true;
1368 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1370 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1371 if (DECL_DECLARED_INLINE_P (newdecl)
1372 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1374 warning ("%Jinline declaration of '%D' follows "
1375 "declaration with attribute noinline", newdecl, newdecl);
1376 warned = true;
1378 else if (DECL_DECLARED_INLINE_P (olddecl)
1379 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1381 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1382 "inline declaration ", newdecl, newdecl);
1383 warned = true;
1386 /* Inline declaration after use or definition.
1387 ??? Should we still warn about this now we have unit-at-a-time
1388 mode and can get it right? */
1389 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1391 if (TREE_USED (olddecl))
1393 warning ("%J'%D' declared inline after being called",
1394 olddecl, olddecl);
1395 warned = true;
1397 else if (DECL_INITIAL (olddecl))
1399 warning ("%J'%D' declared inline after its definition",
1400 olddecl, olddecl);
1401 warned = true;
1405 else /* PARM_DECL, VAR_DECL */
1407 /* Redeclaration of a parameter is a constraint violation (this is
1408 not explicitly stated, but follows from C99 6.7p3 [no more than
1409 one declaration of the same identifier with no linkage in the
1410 same scope, except type tags] and 6.2.2p6 [parameters have no
1411 linkage]). We must check for a forward parameter declaration,
1412 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1413 an extension, the mandatory diagnostic for which is handled by
1414 mark_forward_parm_decls. */
1416 if (TREE_CODE (newdecl) == PARM_DECL
1417 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1419 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1420 locate_old_decl (olddecl, error);
1421 return false;
1425 /* Optional warning for completely redundant decls. */
1426 if (!warned && !pedwarned
1427 && warn_redundant_decls
1428 /* Don't warn about a function declaration followed by a
1429 definition. */
1430 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1431 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1432 /* Don't warn about an extern followed by a definition. */
1433 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1434 /* Don't warn about forward parameter decls. */
1435 && !(TREE_CODE (newdecl) == PARM_DECL
1436 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1438 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1439 warned = true;
1442 /* Report location of previous decl/defn in a consistent manner. */
1443 if (warned || pedwarned)
1444 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1446 return true;
1449 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1450 consistent with OLDDECL, but carries new information. Merge the
1451 new information into OLDDECL. This function issues no
1452 diagnostics. */
1454 static void
1455 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1457 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1458 && DECL_INITIAL (newdecl) != 0);
1460 /* For real parm decl following a forward decl, rechain the old decl
1461 in its new location and clear TREE_ASM_WRITTEN (it's not a
1462 forward decl anymore). */
1463 if (TREE_CODE (newdecl) == PARM_DECL
1464 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1466 struct c_binding *b, **here;
1468 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1469 if ((*here)->decl == olddecl)
1470 goto found;
1471 abort ();
1473 found:
1474 b = *here;
1475 *here = b->prev;
1476 b->prev = current_scope->bindings;
1477 current_scope->bindings = b;
1479 TREE_ASM_WRITTEN (olddecl) = 0;
1482 DECL_ATTRIBUTES (newdecl)
1483 = targetm.merge_decl_attributes (olddecl, newdecl);
1485 /* Merge the data types specified in the two decls. */
1486 TREE_TYPE (newdecl)
1487 = TREE_TYPE (olddecl)
1488 = composite_type (newtype, oldtype);
1490 /* Lay the type out, unless already done. */
1491 if (oldtype != TREE_TYPE (newdecl))
1493 if (TREE_TYPE (newdecl) != error_mark_node)
1494 layout_type (TREE_TYPE (newdecl));
1495 if (TREE_CODE (newdecl) != FUNCTION_DECL
1496 && TREE_CODE (newdecl) != TYPE_DECL
1497 && TREE_CODE (newdecl) != CONST_DECL)
1498 layout_decl (newdecl, 0);
1500 else
1502 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1503 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1504 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1505 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1506 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1507 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1509 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1510 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1514 /* Keep the old rtl since we can safely use it. */
1515 COPY_DECL_RTL (olddecl, newdecl);
1517 /* Merge the type qualifiers. */
1518 if (TREE_READONLY (newdecl))
1519 TREE_READONLY (olddecl) = 1;
1521 if (TREE_THIS_VOLATILE (newdecl))
1523 TREE_THIS_VOLATILE (olddecl) = 1;
1524 if (TREE_CODE (newdecl) == VAR_DECL)
1525 make_var_volatile (newdecl);
1528 /* Keep source location of definition rather than declaration. */
1529 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1530 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1532 /* Merge the unused-warning information. */
1533 if (DECL_IN_SYSTEM_HEADER (olddecl))
1534 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1535 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1536 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1538 /* Merge the initialization information. */
1539 if (DECL_INITIAL (newdecl) == 0)
1540 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1542 /* Merge the section attribute.
1543 We want to issue an error if the sections conflict but that must be
1544 done later in decl_attributes since we are called before attributes
1545 are assigned. */
1546 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1547 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1549 /* Copy the assembler name.
1550 Currently, it can only be defined in the prototype. */
1551 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1553 /* If either declaration has a nondefault visibility, use it. */
1554 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1555 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1557 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1559 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1560 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1561 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1562 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1563 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1564 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1565 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1566 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1567 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1570 /* Merge the storage class information. */
1571 merge_weak (newdecl, olddecl);
1573 /* For functions, static overrides non-static. */
1574 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1576 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1577 /* This is since we don't automatically
1578 copy the attributes of NEWDECL into OLDDECL. */
1579 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1580 /* If this clears `static', clear it in the identifier too. */
1581 if (! TREE_PUBLIC (olddecl))
1582 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1584 if (DECL_EXTERNAL (newdecl))
1586 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1587 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1589 /* An extern decl does not override previous storage class. */
1590 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1591 if (! DECL_EXTERNAL (newdecl))
1593 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1594 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1597 else
1599 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1600 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1603 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1605 /* If we're redefining a function previously defined as extern
1606 inline, make sure we emit debug info for the inline before we
1607 throw it away, in case it was inlined into a function that hasn't
1608 been written out yet. */
1609 if (new_is_definition && DECL_INITIAL (olddecl))
1611 if (TREE_USED (olddecl)
1612 /* In unit-at-a-time mode we never inline re-defined extern
1613 inline functions. */
1614 && !flag_unit_at_a_time
1615 && cgraph_function_possibly_inlined_p (olddecl))
1616 (*debug_hooks->outlining_inline_function) (olddecl);
1618 /* The new defn must not be inline. */
1619 DECL_INLINE (newdecl) = 0;
1620 DECL_UNINLINABLE (newdecl) = 1;
1622 else
1624 /* If either decl says `inline', this fn is inline,
1625 unless its definition was passed already. */
1626 if (DECL_DECLARED_INLINE_P (newdecl)
1627 || DECL_DECLARED_INLINE_P (olddecl))
1628 DECL_DECLARED_INLINE_P (newdecl) = 1;
1630 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1631 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1634 if (DECL_BUILT_IN (olddecl))
1636 /* If redeclaring a builtin function, it stays built in.
1637 But it gets tagged as having been declared. */
1638 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1639 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1640 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1643 /* Also preserve various other info from the definition. */
1644 if (! new_is_definition)
1646 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1647 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1648 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1649 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1650 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1652 /* Set DECL_INLINE on the declaration if we've got a body
1653 from which to instantiate. */
1654 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1656 DECL_INLINE (newdecl) = 1;
1657 DECL_ABSTRACT_ORIGIN (newdecl)
1658 = DECL_ABSTRACT_ORIGIN (olddecl);
1661 else
1663 /* If a previous declaration said inline, mark the
1664 definition as inlinable. */
1665 if (DECL_DECLARED_INLINE_P (newdecl)
1666 && ! DECL_UNINLINABLE (newdecl))
1667 DECL_INLINE (newdecl) = 1;
1671 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1672 But preserve OLDDECL's DECL_UID. */
1674 unsigned olddecl_uid = DECL_UID (olddecl);
1676 memcpy ((char *) olddecl + sizeof (struct tree_common),
1677 (char *) newdecl + sizeof (struct tree_common),
1678 sizeof (struct tree_decl) - sizeof (struct tree_common));
1679 DECL_UID (olddecl) = olddecl_uid;
1682 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1683 so that encode_section_info has a chance to look at the new decl
1684 flags and attributes. */
1685 if (DECL_RTL_SET_P (olddecl)
1686 && (TREE_CODE (olddecl) == FUNCTION_DECL
1687 || (TREE_CODE (olddecl) == VAR_DECL
1688 && TREE_STATIC (olddecl))))
1689 make_decl_rtl (olddecl, NULL);
1692 /* Handle when a new declaration NEWDECL has the same name as an old
1693 one OLDDECL in the same binding contour. Prints an error message
1694 if appropriate.
1696 If safely possible, alter OLDDECL to look like NEWDECL, and return
1697 true. Otherwise, return false. */
1699 static bool
1700 duplicate_decls (tree newdecl, tree olddecl)
1702 tree newtype = NULL, oldtype = NULL;
1704 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1705 return false;
1707 merge_decls (newdecl, olddecl, newtype, oldtype);
1708 return true;
1712 /* Check whether decl-node NEW shadows an existing declaration. */
1713 static void
1714 warn_if_shadowing (tree new)
1716 struct c_binding *b;
1718 /* Shadow warnings wanted? */
1719 if (!warn_shadow
1720 /* No shadow warnings for internally generated vars. */
1721 || DECL_IS_BUILTIN (new)
1722 /* No shadow warnings for vars made for inlining. */
1723 || DECL_FROM_INLINE (new)
1724 /* Don't warn about the parm names in function declarator
1725 within a function declarator. It would be nice to avoid
1726 warning in any function declarator in a declaration, as
1727 opposed to a definition, but there is no way to tell
1728 it's not a definition at this point. */
1729 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1730 return;
1732 /* Is anything being shadowed? Invisible decls do not count. */
1733 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1734 if (b->decl && b->decl != new && !b->invisible)
1736 tree old = b->decl;
1738 if (TREE_CODE (old) == PARM_DECL)
1739 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1740 else if (DECL_FILE_SCOPE_P (old))
1741 warning ("%Jdeclaration of '%D' shadows a global declaration",
1742 new, new);
1743 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1744 warning ("%Jdeclaration of '%D' shadows a built-in function",
1745 new, new);
1746 else
1747 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1749 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1750 warning ("%Jshadowed declaration is here", old);
1752 break;
1757 /* Subroutine of pushdecl.
1759 X is a TYPE_DECL for a typedef statement. Create a brand new
1760 ..._TYPE node (which will be just a variant of the existing
1761 ..._TYPE node with identical properties) and then install X
1762 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1764 The whole point here is to end up with a situation where each
1765 and every ..._TYPE node the compiler creates will be uniquely
1766 associated with AT MOST one node representing a typedef name.
1767 This way, even though the compiler substitutes corresponding
1768 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1769 early on, later parts of the compiler can always do the reverse
1770 translation and get back the corresponding typedef name. For
1771 example, given:
1773 typedef struct S MY_TYPE;
1774 MY_TYPE object;
1776 Later parts of the compiler might only know that `object' was of
1777 type `struct S' if it were not for code just below. With this
1778 code however, later parts of the compiler see something like:
1780 struct S' == struct S
1781 typedef struct S' MY_TYPE;
1782 struct S' object;
1784 And they can then deduce (from the node for type struct S') that
1785 the original object declaration was:
1787 MY_TYPE object;
1789 Being able to do this is important for proper support of protoize,
1790 and also for generating precise symbolic debugging information
1791 which takes full account of the programmer's (typedef) vocabulary.
1793 Obviously, we don't want to generate a duplicate ..._TYPE node if
1794 the TYPE_DECL node that we are now processing really represents a
1795 standard built-in type.
1797 Since all standard types are effectively declared at line zero
1798 in the source file, we can easily check to see if we are working
1799 on a standard type by checking the current value of lineno. */
1801 static void
1802 clone_underlying_type (tree x)
1804 if (DECL_IS_BUILTIN (x))
1806 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1807 TYPE_NAME (TREE_TYPE (x)) = x;
1809 else if (TREE_TYPE (x) != error_mark_node
1810 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1812 tree tt = TREE_TYPE (x);
1813 DECL_ORIGINAL_TYPE (x) = tt;
1814 tt = build_type_copy (tt);
1815 TYPE_NAME (tt) = x;
1816 TREE_USED (tt) = TREE_USED (x);
1817 TREE_TYPE (x) = tt;
1821 /* Record a decl-node X as belonging to the current lexical scope.
1822 Check for errors (such as an incompatible declaration for the same
1823 name already seen in the same scope).
1825 Returns either X or an old decl for the same name.
1826 If an old decl is returned, it may have been smashed
1827 to agree with what X says. */
1829 tree
1830 pushdecl (tree x)
1832 tree name = DECL_NAME (x);
1833 struct c_scope *scope = current_scope;
1834 struct c_binding *b;
1835 bool nested = false;
1837 /* Functions need the lang_decl data. */
1838 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1839 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1841 /* Must set DECL_CONTEXT for everything not at file scope or
1842 DECL_FILE_SCOPE_P won't work. Local externs don't count
1843 unless they have initializers (which generate code). */
1844 if (current_function_decl
1845 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1846 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1847 DECL_CONTEXT (x) = current_function_decl;
1849 /* Anonymous decls are just inserted in the scope. */
1850 if (!name)
1852 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1853 return x;
1856 /* First, see if there is another declaration with the same name in
1857 the current scope. If there is, duplicate_decls may do all the
1858 work for us. If duplicate_decls returns false, that indicates
1859 two incompatible decls in the same scope; we are to silently
1860 replace the old one (duplicate_decls has issued all appropriate
1861 diagnostics). In particular, we should not consider possible
1862 duplicates in the external scope, or shadowing. */
1863 b = I_SYMBOL_BINDING (name);
1864 if (b && B_IN_SCOPE (b, scope))
1866 if (duplicate_decls (x, b->decl))
1867 return b->decl;
1868 else
1869 goto skip_external_and_shadow_checks;
1872 /* All declarations with external linkage, and all external
1873 references, go in the external scope, no matter what scope is
1874 current. However, the binding in that scope is ignored for
1875 purposes of normal name lookup. A separate binding structure is
1876 created in the requested scope; this governs the normal
1877 visibility of the symbol.
1879 The binding in the externals scope is used exclusively for
1880 detecting duplicate declarations of the same object, no matter
1881 what scope they are in; this is what we do here. (C99 6.2.7p2:
1882 All declarations that refer to the same object or function shall
1883 have compatible type; otherwise, the behavior is undefined.) */
1884 if (DECL_EXTERNAL (x) || scope == file_scope)
1886 if (warn_nested_externs
1887 && scope != file_scope
1888 && !DECL_IN_SYSTEM_HEADER (x))
1889 warning ("nested extern declaration of '%D'", x);
1891 while (b && !B_IN_EXTERNAL_SCOPE (b))
1892 b = b->shadowed;
1894 /* The point of the same_translation_unit_p check here is,
1895 we want to detect a duplicate decl for a construct like
1896 foo() { extern bar(); } ... static bar(); but not if
1897 they are in different translation units. In any case,
1898 the static does not go in the externals scope. */
1899 if (b
1900 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1901 || same_translation_unit_p (x, b->decl))
1902 && duplicate_decls (x, b->decl))
1904 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1905 return b->decl;
1907 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1909 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1910 nested = true;
1913 /* Similarly, a declaration of a function with static linkage at
1914 block scope must be checked against any existing declaration
1915 of that function at file scope. */
1916 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1917 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1919 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1920 warning ("nested static declaration of '%D'", x);
1922 while (b && !B_IN_FILE_SCOPE (b))
1923 b = b->shadowed;
1925 if (b && same_translation_unit_p (x, b->decl)
1926 && duplicate_decls (x, b->decl))
1928 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1929 return b->decl;
1931 else
1933 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1934 nested = true;
1938 warn_if_shadowing (x);
1940 skip_external_and_shadow_checks:
1941 if (TREE_CODE (x) == TYPE_DECL)
1942 clone_underlying_type (x);
1944 bind (name, x, scope, /*invisible=*/false, nested);
1946 /* If x's type is incomplete because it's based on a
1947 structure or union which has not yet been fully declared,
1948 attach it to that structure or union type, so we can go
1949 back and complete the variable declaration later, if the
1950 structure or union gets fully declared.
1952 If the input is erroneous, we can have error_mark in the type
1953 slot (e.g. "f(void a, ...)") - that doesn't count as an
1954 incomplete type. */
1955 if (TREE_TYPE (x) != error_mark_node
1956 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1958 tree element = TREE_TYPE (x);
1960 while (TREE_CODE (element) == ARRAY_TYPE)
1961 element = TREE_TYPE (element);
1962 element = TYPE_MAIN_VARIANT (element);
1964 if ((TREE_CODE (element) == RECORD_TYPE
1965 || TREE_CODE (element) == UNION_TYPE)
1966 && (TREE_CODE (x) != TYPE_DECL
1967 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1968 && !COMPLETE_TYPE_P (element))
1969 C_TYPE_INCOMPLETE_VARS (element)
1970 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1972 return x;
1975 /* Record X as belonging to file scope.
1976 This is used only internally by the Objective-C front end,
1977 and is limited to its needs. duplicate_decls is not called;
1978 if there is any preexisting decl for this identifier, it is an ICE. */
1980 tree
1981 pushdecl_top_level (tree x)
1983 tree name;
1984 bool nested = false;
1986 if (TREE_CODE (x) != VAR_DECL)
1987 abort ();
1989 name = DECL_NAME (x);
1991 if (I_SYMBOL_BINDING (name))
1992 abort ();
1994 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1996 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1997 nested = true;
1999 if (file_scope)
2000 bind (name, x, file_scope, /*invisible=*/false, nested);
2002 return x;
2005 static void
2006 implicit_decl_warning (tree id, tree olddecl)
2008 void (*diag) (const char *, ...);
2009 switch (mesg_implicit_function_declaration)
2011 case 0: return;
2012 case 1: diag = warning; break;
2013 case 2: diag = error; break;
2014 default: abort ();
2017 diag (N_("implicit declaration of function '%E'"), id);
2018 if (olddecl)
2019 locate_old_decl (olddecl, diag);
2022 /* Generate an implicit declaration for identifier FUNCTIONID as a
2023 function of type int (). */
2025 tree
2026 implicitly_declare (tree functionid)
2028 tree decl = lookup_name_in_scope (functionid, external_scope);
2030 if (decl)
2032 /* FIXME: Objective-C has weird not-really-builtin functions
2033 which are supposed to be visible automatically. They wind up
2034 in the external scope because they're pushed before the file
2035 scope gets created. Catch this here and rebind them into the
2036 file scope. */
2037 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2039 bind (functionid, decl, file_scope,
2040 /*invisible=*/false, /*nested=*/true);
2041 return decl;
2043 else
2045 /* Implicit declaration of a function already declared
2046 (somehow) in a different scope, or as a built-in.
2047 If this is the first time this has happened, warn;
2048 then recycle the old declaration. */
2049 if (!C_DECL_IMPLICIT (decl))
2051 implicit_decl_warning (functionid, decl);
2052 C_DECL_IMPLICIT (decl) = 1;
2054 bind (functionid, decl, current_scope,
2055 /*invisible=*/false, /*nested=*/true);
2056 return decl;
2060 /* Not seen before. */
2061 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2062 DECL_EXTERNAL (decl) = 1;
2063 TREE_PUBLIC (decl) = 1;
2064 C_DECL_IMPLICIT (decl) = 1;
2065 implicit_decl_warning (functionid, 0);
2067 /* C89 says implicit declarations are in the innermost block.
2068 So we record the decl in the standard fashion. */
2069 decl = pushdecl (decl);
2071 /* No need to call objc_check_decl here - it's a function type. */
2072 rest_of_decl_compilation (decl, NULL, 0, 0);
2074 /* Write a record describing this implicit function declaration
2075 to the prototypes file (if requested). */
2076 gen_aux_info_record (decl, 0, 1, 0);
2078 /* Possibly apply some default attributes to this implicit declaration. */
2079 decl_attributes (&decl, NULL_TREE, 0);
2081 return decl;
2084 /* Issue an error message for a reference to an undeclared variable
2085 ID, including a reference to a builtin outside of function-call
2086 context. Establish a binding of the identifier to error_mark_node
2087 in an appropriate scope, which will suppress further errors for the
2088 same identifier. */
2089 void
2090 undeclared_variable (tree id)
2092 static bool already = false;
2093 struct c_scope *scope;
2095 if (current_function_decl == 0)
2097 error ("'%E' undeclared here (not in a function)", id);
2098 scope = current_scope;
2100 else
2102 error ("'%E' undeclared (first use in this function)", id);
2104 if (! already)
2106 error ("(Each undeclared identifier is reported only once");
2107 error ("for each function it appears in.)");
2108 already = true;
2111 /* If we are parsing old-style parameter decls, current_function_decl
2112 will be nonnull but current_function_scope will be null. */
2113 scope = current_function_scope ? current_function_scope : current_scope;
2115 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2118 /* Subroutine of lookup_label, declare_label, define_label: construct a
2119 LABEL_DECL with all the proper frills. */
2121 static tree
2122 make_label (tree name, location_t location)
2124 tree label = build_decl (LABEL_DECL, name, void_type_node);
2126 DECL_CONTEXT (label) = current_function_decl;
2127 DECL_MODE (label) = VOIDmode;
2128 DECL_SOURCE_LOCATION (label) = location;
2130 return label;
2133 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2134 Create one if none exists so far for the current function.
2135 This is called when a label is used in a goto expression or
2136 has its address taken. */
2138 tree
2139 lookup_label (tree name)
2141 tree label;
2143 if (current_function_decl == 0)
2145 error ("label %s referenced outside of any function",
2146 IDENTIFIER_POINTER (name));
2147 return 0;
2150 /* Use a label already defined or ref'd with this name, but not if
2151 it is inherited from a containing function and wasn't declared
2152 using __label__. */
2153 label = I_LABEL_DECL (name);
2154 if (label && (DECL_CONTEXT (label) == current_function_decl
2155 || C_DECLARED_LABEL_FLAG (label)))
2157 /* If the label has only been declared, update its apparent
2158 location to point here, for better diagnostics if it
2159 turns out not to have been defined. */
2160 if (!TREE_USED (label))
2161 DECL_SOURCE_LOCATION (label) = input_location;
2162 return label;
2165 /* No label binding for that identifier; make one. */
2166 label = make_label (name, input_location);
2168 /* Ordinary labels go in the current function scope. */
2169 bind (name, label, current_function_scope,
2170 /*invisible=*/false, /*nested=*/false);
2171 return label;
2174 /* Make a label named NAME in the current function, shadowing silently
2175 any that may be inherited from containing functions or containing
2176 scopes. This is called for __label__ declarations. */
2178 tree
2179 declare_label (tree name)
2181 struct c_binding *b = I_LABEL_BINDING (name);
2182 tree label;
2184 /* Check to make sure that the label hasn't already been declared
2185 at this scope */
2186 if (b && B_IN_CURRENT_SCOPE (b))
2188 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2189 locate_old_decl (b->decl, error);
2191 /* Just use the previous declaration. */
2192 return b->decl;
2195 label = make_label (name, input_location);
2196 C_DECLARED_LABEL_FLAG (label) = 1;
2198 /* Declared labels go in the current scope. */
2199 bind (name, label, current_scope,
2200 /*invisible=*/false, /*nested=*/false);
2201 return label;
2204 /* Define a label, specifying the location in the source file.
2205 Return the LABEL_DECL node for the label, if the definition is valid.
2206 Otherwise return 0. */
2208 tree
2209 define_label (location_t location, tree name)
2211 /* Find any preexisting label with this name. It is an error
2212 if that label has already been defined in this function, or
2213 if there is a containing function with a declared label with
2214 the same name. */
2215 tree label = I_LABEL_DECL (name);
2217 if (label
2218 && ((DECL_CONTEXT (label) == current_function_decl
2219 && DECL_INITIAL (label) != 0)
2220 || (DECL_CONTEXT (label) != current_function_decl
2221 && C_DECLARED_LABEL_FLAG (label))))
2223 error ("%Hduplicate label `%D'", &location, label);
2224 locate_old_decl (label, error);
2225 return 0;
2227 else if (label && DECL_CONTEXT (label) == current_function_decl)
2229 /* The label has been used or declared already in this function,
2230 but not defined. Update its location to point to this
2231 definition. */
2232 DECL_SOURCE_LOCATION (label) = location;
2234 else
2236 /* No label binding for that identifier; make one. */
2237 label = make_label (name, location);
2239 /* Ordinary labels go in the current function scope. */
2240 bind (name, label, current_function_scope,
2241 /*invisible=*/false, /*nested=*/false);
2244 if (warn_traditional && !in_system_header && lookup_name (name))
2245 warning ("%Htraditional C lacks a separate namespace for labels, "
2246 "identifier `%s' conflicts", &location,
2247 IDENTIFIER_POINTER (name));
2249 /* Mark label as having been defined. */
2250 DECL_INITIAL (label) = error_mark_node;
2251 return label;
2254 /* Given NAME, an IDENTIFIER_NODE,
2255 return the structure (or union or enum) definition for that name.
2256 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2257 CODE says which kind of type the caller wants;
2258 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2259 If the wrong kind of type is found, an error is reported. */
2261 static tree
2262 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2264 struct c_binding *b = I_TAG_BINDING (name);
2265 int thislevel = 0;
2267 if (!b || !b->decl)
2268 return 0;
2270 /* We only care about whether it's in this level if
2271 thislevel_only was set or it might be a type clash. */
2272 if (thislevel_only || TREE_CODE (b->decl) != code)
2274 /* For our purposes, a tag in the external scope is the same as
2275 a tag in the file scope. (Primarily relevant to Objective-C
2276 and its builtin structure tags, which get pushed before the
2277 file scope is created.) */
2278 if (B_IN_CURRENT_SCOPE (b)
2279 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2280 thislevel = 1;
2283 if (thislevel_only && !thislevel)
2284 return 0;
2286 if (TREE_CODE (b->decl) != code)
2288 /* Definition isn't the kind we were looking for. */
2289 pending_invalid_xref = name;
2290 pending_invalid_xref_location = input_location;
2292 /* If in the same binding level as a declaration as a tag
2293 of a different type, this must not be allowed to
2294 shadow that tag, so give the error immediately.
2295 (For example, "struct foo; union foo;" is invalid.) */
2296 if (thislevel)
2297 pending_xref_error ();
2299 return b->decl;
2302 /* Print an error message now
2303 for a recent invalid struct, union or enum cross reference.
2304 We don't print them immediately because they are not invalid
2305 when used in the `struct foo;' construct for shadowing. */
2307 void
2308 pending_xref_error (void)
2310 if (pending_invalid_xref != 0)
2311 error ("%H`%s' defined as wrong kind of tag",
2312 &pending_invalid_xref_location,
2313 IDENTIFIER_POINTER (pending_invalid_xref));
2314 pending_invalid_xref = 0;
2318 /* Look up NAME in the current scope and its superiors
2319 in the namespace of variables, functions and typedefs.
2320 Return a ..._DECL node of some kind representing its definition,
2321 or return 0 if it is undefined. */
2323 tree
2324 lookup_name (tree name)
2326 struct c_binding *b = I_SYMBOL_BINDING (name);
2327 if (b && !b->invisible)
2328 return b->decl;
2329 return 0;
2332 /* Similar to `lookup_name' but look only at the indicated scope. */
2334 static tree
2335 lookup_name_in_scope (tree name, struct c_scope *scope)
2337 struct c_binding *b;
2339 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2340 if (B_IN_SCOPE (b, scope))
2341 return b->decl;
2342 return 0;
2345 /* Create the predefined scalar types of C,
2346 and some nodes representing standard constants (0, 1, (void *) 0).
2347 Initialize the global scope.
2348 Make definitions for built-in primitive functions. */
2350 void
2351 c_init_decl_processing (void)
2353 tree endlink;
2354 tree ptr_ftype_void, ptr_ftype_ptr;
2355 location_t save_loc = input_location;
2357 /* Adds some ggc roots, and reserved words for c-parse.in. */
2358 c_parse_init ();
2360 current_function_decl = 0;
2362 /* Make the externals scope. */
2363 push_scope ();
2364 external_scope = current_scope;
2366 /* Declarations from c_common_nodes_and_builtins must not be associated
2367 with this input file, lest we get differences between using and not
2368 using preprocessed headers. */
2369 #ifdef USE_MAPPED_LOCATION
2370 input_location = BUILTINS_LOCATION;
2371 #else
2372 input_location.file = "<built-in>";
2373 input_location.line = 0;
2374 #endif
2376 build_common_tree_nodes (flag_signed_char);
2378 c_common_nodes_and_builtins ();
2380 /* In C, comparisons and TRUTH_* expressions have type int. */
2381 truthvalue_type_node = integer_type_node;
2382 truthvalue_true_node = integer_one_node;
2383 truthvalue_false_node = integer_zero_node;
2385 /* Even in C99, which has a real boolean type. */
2386 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2387 boolean_type_node));
2389 endlink = void_list_node;
2390 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2391 ptr_ftype_ptr
2392 = build_function_type (ptr_type_node,
2393 tree_cons (NULL_TREE, ptr_type_node, endlink));
2395 input_location = save_loc;
2397 pedantic_lvalues = true;
2399 make_fname_decl = c_make_fname_decl;
2400 start_fname_decls ();
2403 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2404 decl, NAME is the initialization string and TYPE_DEP indicates whether
2405 NAME depended on the type of the function. As we don't yet implement
2406 delayed emission of static data, we mark the decl as emitted
2407 so it is not placed in the output. Anything using it must therefore pull
2408 out the STRING_CST initializer directly. FIXME. */
2410 static tree
2411 c_make_fname_decl (tree id, int type_dep)
2413 const char *name = fname_as_string (type_dep);
2414 tree decl, type, init;
2415 size_t length = strlen (name);
2417 type = build_array_type
2418 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2419 build_index_type (size_int (length)));
2421 decl = build_decl (VAR_DECL, id, type);
2423 TREE_STATIC (decl) = 1;
2424 TREE_READONLY (decl) = 1;
2425 DECL_ARTIFICIAL (decl) = 1;
2427 init = build_string (length + 1, name);
2428 free ((char *) name);
2429 TREE_TYPE (init) = type;
2430 DECL_INITIAL (decl) = init;
2432 TREE_USED (decl) = 1;
2434 if (current_function_decl)
2436 DECL_CONTEXT (decl) = current_function_decl;
2437 bind (id, decl, current_function_scope,
2438 /*invisible=*/false, /*nested=*/false);
2441 finish_decl (decl, init, NULL_TREE);
2443 return decl;
2446 /* Return a definition for a builtin function named NAME and whose data type
2447 is TYPE. TYPE should be a function type with argument types.
2448 FUNCTION_CODE tells later passes how to compile calls to this function.
2449 See tree.h for its possible values.
2451 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2452 the name to be called if we can't opencode the function. If
2453 ATTRS is nonzero, use that for the function's attribute list. */
2455 tree
2456 builtin_function (const char *name, tree type, int function_code,
2457 enum built_in_class class, const char *library_name,
2458 tree attrs)
2460 tree id = get_identifier (name);
2461 tree decl = build_decl (FUNCTION_DECL, id, type);
2462 TREE_PUBLIC (decl) = 1;
2463 DECL_EXTERNAL (decl) = 1;
2464 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2465 DECL_BUILT_IN_CLASS (decl) = class;
2466 DECL_FUNCTION_CODE (decl) = function_code;
2467 if (library_name)
2468 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2470 /* Should never be called on a symbol with a preexisting meaning. */
2471 if (I_SYMBOL_BINDING (id))
2472 abort ();
2474 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2476 /* Builtins in the implementation namespace are made visible without
2477 needing to be explicitly declared. See push_file_scope. */
2478 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2480 TREE_CHAIN (decl) = visible_builtins;
2481 visible_builtins = decl;
2484 /* Possibly apply some default attributes to this built-in function. */
2485 if (attrs)
2486 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2487 else
2488 decl_attributes (&decl, NULL_TREE, 0);
2490 return decl;
2493 /* Called when a declaration is seen that contains no names to declare.
2494 If its type is a reference to a structure, union or enum inherited
2495 from a containing scope, shadow that tag name for the current scope
2496 with a forward reference.
2497 If its type defines a new named structure or union
2498 or defines an enum, it is valid but we need not do anything here.
2499 Otherwise, it is an error. */
2501 void
2502 shadow_tag (tree declspecs)
2504 shadow_tag_warned (declspecs, 0);
2507 void
2508 shadow_tag_warned (tree declspecs, int warned)
2511 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2512 no pedwarn. */
2514 int found_tag = 0;
2515 tree link;
2516 tree specs, attrs;
2518 pending_invalid_xref = 0;
2520 /* Remove the attributes from declspecs, since they will confuse the
2521 following code. */
2522 split_specs_attrs (declspecs, &specs, &attrs);
2524 for (link = specs; link; link = TREE_CHAIN (link))
2526 tree value = TREE_VALUE (link);
2527 enum tree_code code = TREE_CODE (value);
2529 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2530 /* Used to test also that TYPE_SIZE (value) != 0.
2531 That caused warning for `struct foo;' at top level in the file. */
2533 tree name = TYPE_NAME (value);
2534 tree t;
2536 found_tag++;
2538 if (name == 0)
2540 if (warned != 1 && code != ENUMERAL_TYPE)
2541 /* Empty unnamed enum OK */
2543 pedwarn ("unnamed struct/union that defines no instances");
2544 warned = 1;
2547 else
2549 t = lookup_tag (code, name, 1);
2551 if (t == 0)
2553 t = make_node (code);
2554 pushtag (name, t);
2558 else
2560 if (!warned && ! in_system_header)
2562 warning ("useless keyword or type name in empty declaration");
2563 warned = 2;
2568 if (found_tag > 1)
2569 error ("two types specified in one empty declaration");
2571 if (warned != 1)
2573 if (found_tag == 0)
2574 pedwarn ("empty declaration");
2578 /* Construct an array declarator. EXPR is the expression inside [], or
2579 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2580 to the pointer to which a parameter array is converted). STATIC_P is
2581 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2582 is nonzero is the array is [*], a VLA of unspecified length which is
2583 nevertheless a complete type (not currently implemented by GCC),
2584 zero otherwise. The declarator is constructed as an ARRAY_REF
2585 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2586 left of the [] (filled by in set_array_declarator_type) and operand 1
2587 is the expression inside; whose TREE_TYPE is the type qualifiers and
2588 which has TREE_STATIC set if "static" is used. */
2590 tree
2591 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2593 tree decl;
2594 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2595 TREE_TYPE (decl) = quals;
2596 TREE_STATIC (decl) = (static_p ? 1 : 0);
2597 if (pedantic && !flag_isoc99)
2599 if (static_p || quals != NULL_TREE)
2600 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2601 if (vla_unspec_p)
2602 pedwarn ("ISO C90 does not support `[*]' array declarators");
2604 if (vla_unspec_p)
2605 warning ("GCC does not yet properly implement `[*]' array declarators");
2606 return decl;
2609 /* Set the type of an array declarator. DECL is the declarator, as
2610 constructed by build_array_declarator; TYPE is what appears on the left
2611 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2612 abstract declarator, zero otherwise; this is used to reject static and
2613 type qualifiers in abstract declarators, where they are not in the
2614 C99 grammar. */
2616 tree
2617 set_array_declarator_type (tree decl, tree type, int abstract_p)
2619 TREE_OPERAND (decl, 0) = type;
2620 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2621 error ("static or type qualifiers in abstract declarator");
2622 return decl;
2625 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2627 tree
2628 groktypename (tree typename)
2630 tree specs, attrs;
2632 if (TREE_CODE (typename) != TREE_LIST)
2633 return typename;
2635 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2637 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2638 NULL);
2640 /* Apply attributes. */
2641 decl_attributes (&typename, attrs, 0);
2643 return typename;
2646 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2648 tree
2649 groktypename_in_parm_context (tree typename)
2651 if (TREE_CODE (typename) != TREE_LIST)
2652 return typename;
2653 return grokdeclarator (TREE_VALUE (typename),
2654 TREE_PURPOSE (typename),
2655 PARM, 0, NULL);
2658 /* Decode a declarator in an ordinary declaration or data definition.
2659 This is called as soon as the type information and variable name
2660 have been parsed, before parsing the initializer if any.
2661 Here we create the ..._DECL node, fill in its type,
2662 and put it on the list of decls for the current context.
2663 The ..._DECL node is returned as the value.
2665 Exception: for arrays where the length is not specified,
2666 the type is left null, to be filled in by `finish_decl'.
2668 Function definitions do not come here; they go to start_function
2669 instead. However, external and forward declarations of functions
2670 do go through here. Structure field declarations are done by
2671 grokfield and not through here. */
2673 tree
2674 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2676 tree decl;
2677 tree tem;
2679 /* An object declared as __attribute__((deprecated)) suppresses
2680 warnings of uses of other deprecated items. */
2681 if (lookup_attribute ("deprecated", attributes))
2682 deprecated_state = DEPRECATED_SUPPRESS;
2684 decl = grokdeclarator (declarator, declspecs,
2685 NORMAL, initialized, NULL);
2687 deprecated_state = DEPRECATED_NORMAL;
2689 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2690 && MAIN_NAME_P (DECL_NAME (decl)))
2691 warning ("%J'%D' is usually a function", decl, decl);
2693 if (initialized)
2694 /* Is it valid for this decl to have an initializer at all?
2695 If not, set INITIALIZED to zero, which will indirectly
2696 tell 'finish_decl' to ignore the initializer once it is parsed. */
2697 switch (TREE_CODE (decl))
2699 case TYPE_DECL:
2700 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2701 initialized = 0;
2702 break;
2704 case FUNCTION_DECL:
2705 error ("function '%D' is initialized like a variable", decl);
2706 initialized = 0;
2707 break;
2709 case PARM_DECL:
2710 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2711 error ("parameter '%D' is initialized", decl);
2712 initialized = 0;
2713 break;
2715 default:
2716 /* Don't allow initializations for incomplete types except for
2717 arrays which might be completed by the initialization. */
2719 /* This can happen if the array size is an undefined macro.
2720 We already gave a warning, so we don't need another one. */
2721 if (TREE_TYPE (decl) == error_mark_node)
2722 initialized = 0;
2723 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2725 /* A complete type is ok if size is fixed. */
2727 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2728 || C_DECL_VARIABLE_SIZE (decl))
2730 error ("variable-sized object may not be initialized");
2731 initialized = 0;
2734 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2736 error ("variable '%D' has initializer but incomplete type", decl);
2737 initialized = 0;
2739 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2741 error ("elements of array '%D' have incomplete type", decl);
2742 initialized = 0;
2746 if (initialized)
2748 DECL_EXTERNAL (decl) = 0;
2749 if (current_scope == file_scope)
2750 TREE_STATIC (decl) = 1;
2752 /* Tell 'pushdecl' this is an initialized decl
2753 even though we don't yet have the initializer expression.
2754 Also tell 'finish_decl' it may store the real initializer. */
2755 DECL_INITIAL (decl) = error_mark_node;
2758 /* If this is a function declaration, write a record describing it to the
2759 prototypes file (if requested). */
2761 if (TREE_CODE (decl) == FUNCTION_DECL)
2762 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2764 /* ANSI specifies that a tentative definition which is not merged with
2765 a non-tentative definition behaves exactly like a definition with an
2766 initializer equal to zero. (Section 3.7.2)
2768 -fno-common gives strict ANSI behavior, though this tends to break
2769 a large body of code that grew up without this rule.
2771 Thread-local variables are never common, since there's no entrenched
2772 body of code to break, and it allows more efficient variable references
2773 in the presence of dynamic linking. */
2775 if (TREE_CODE (decl) == VAR_DECL
2776 && !initialized
2777 && TREE_PUBLIC (decl)
2778 && !DECL_THREAD_LOCAL (decl)
2779 && !flag_no_common)
2780 DECL_COMMON (decl) = 1;
2782 /* Set attributes here so if duplicate decl, will have proper attributes. */
2783 decl_attributes (&decl, attributes, 0);
2785 if (TREE_CODE (decl) == FUNCTION_DECL
2786 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2788 tree ce = declarator;
2790 if (TREE_CODE (ce) == INDIRECT_REF)
2791 ce = TREE_OPERAND (declarator, 0);
2792 if (TREE_CODE (ce) == CALL_EXPR)
2794 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2795 for (; args; args = TREE_CHAIN (args))
2797 tree type = TREE_TYPE (args);
2798 if (type && INTEGRAL_TYPE_P (type)
2799 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2800 DECL_ARG_TYPE (args) = integer_type_node;
2805 if (TREE_CODE (decl) == FUNCTION_DECL
2806 && DECL_DECLARED_INLINE_P (decl)
2807 && DECL_UNINLINABLE (decl)
2808 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2809 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2811 /* Add this decl to the current scope.
2812 TEM may equal DECL or it may be a previous decl of the same name. */
2813 tem = pushdecl (decl);
2815 return tem;
2818 /* Finish processing of a declaration;
2819 install its initial value.
2820 If the length of an array type is not known before,
2821 it must be determined now, from the initial value, or it is an error. */
2823 void
2824 finish_decl (tree decl, tree init, tree asmspec_tree)
2826 tree type = TREE_TYPE (decl);
2827 int was_incomplete = (DECL_SIZE (decl) == 0);
2828 const char *asmspec = 0;
2830 /* If a name was specified, get the string. */
2831 if (current_scope == file_scope)
2832 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2833 if (asmspec_tree)
2834 asmspec = TREE_STRING_POINTER (asmspec_tree);
2836 /* If `start_decl' didn't like having an initialization, ignore it now. */
2837 if (init != 0 && DECL_INITIAL (decl) == 0)
2838 init = 0;
2840 /* Don't crash if parm is initialized. */
2841 if (TREE_CODE (decl) == PARM_DECL)
2842 init = 0;
2844 if (init)
2845 store_init_value (decl, init);
2847 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2848 || TREE_CODE (decl) == FUNCTION_DECL
2849 || TREE_CODE (decl) == FIELD_DECL))
2850 objc_check_decl (decl);
2852 /* Deduce size of array from initialization, if not already known. */
2853 if (TREE_CODE (type) == ARRAY_TYPE
2854 && TYPE_DOMAIN (type) == 0
2855 && TREE_CODE (decl) != TYPE_DECL)
2857 int do_default
2858 = (TREE_STATIC (decl)
2859 /* Even if pedantic, an external linkage array
2860 may have incomplete type at first. */
2861 ? pedantic && !TREE_PUBLIC (decl)
2862 : !DECL_EXTERNAL (decl));
2863 int failure
2864 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2866 /* Get the completed type made by complete_array_type. */
2867 type = TREE_TYPE (decl);
2869 if (failure == 1)
2870 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2872 else if (failure == 2)
2874 if (do_default)
2875 error ("%Jarray size missing in '%D'", decl, decl);
2876 /* If a `static' var's size isn't known,
2877 make it extern as well as static, so it does not get
2878 allocated.
2879 If it is not `static', then do not mark extern;
2880 finish_incomplete_decl will give it a default size
2881 and it will get allocated. */
2882 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2883 DECL_EXTERNAL (decl) = 1;
2886 /* TYPE_MAX_VALUE is always one less than the number of elements
2887 in the array, because we start counting at zero. Therefore,
2888 warn only if the value is less than zero. */
2889 else if (pedantic && TYPE_DOMAIN (type) != 0
2890 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2891 error ("%Jzero or negative size array '%D'", decl, decl);
2893 layout_decl (decl, 0);
2896 if (TREE_CODE (decl) == VAR_DECL)
2898 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2899 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2900 layout_decl (decl, 0);
2902 if (DECL_SIZE (decl) == 0
2903 /* Don't give an error if we already gave one earlier. */
2904 && TREE_TYPE (decl) != error_mark_node
2905 && (TREE_STATIC (decl)
2907 /* A static variable with an incomplete type
2908 is an error if it is initialized.
2909 Also if it is not file scope.
2910 Otherwise, let it through, but if it is not `extern'
2911 then it may cause an error message later. */
2912 (DECL_INITIAL (decl) != 0
2913 || !DECL_FILE_SCOPE_P (decl))
2915 /* An automatic variable with an incomplete type
2916 is an error. */
2917 !DECL_EXTERNAL (decl)))
2919 error ("%Jstorage size of '%D' isn't known", decl, decl);
2920 TREE_TYPE (decl) = error_mark_node;
2923 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2924 && DECL_SIZE (decl) != 0)
2926 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2927 constant_expression_warning (DECL_SIZE (decl));
2928 else
2929 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2932 if (TREE_USED (type))
2933 TREE_USED (decl) = 1;
2936 /* If this is a function and an assembler name is specified, reset DECL_RTL
2937 so we can give it its new name. Also, update built_in_decls if it
2938 was a normal built-in. */
2939 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2941 /* ASMSPEC is given, and not the name of a register. Mark the
2942 name with a star so assemble_name won't munge it. */
2943 char *starred = alloca (strlen (asmspec) + 2);
2944 starred[0] = '*';
2945 strcpy (starred + 1, asmspec);
2947 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2949 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2950 SET_DECL_RTL (builtin, NULL_RTX);
2951 change_decl_assembler_name (builtin, get_identifier (starred));
2952 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2953 init_block_move_fn (starred);
2954 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2955 init_block_clear_fn (starred);
2957 SET_DECL_RTL (decl, NULL_RTX);
2958 change_decl_assembler_name (decl, get_identifier (starred));
2961 /* If #pragma weak was used, mark the decl weak now. */
2962 if (current_scope == file_scope)
2963 maybe_apply_pragma_weak (decl);
2965 /* Output the assembler code and/or RTL code for variables and functions,
2966 unless the type is an undefined structure or union.
2967 If not, it will get done when the type is completed. */
2969 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2971 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2972 if (c_dialect_objc ())
2973 objc_check_decl (decl);
2975 if (DECL_FILE_SCOPE_P (decl))
2977 if (DECL_INITIAL (decl) == NULL_TREE
2978 || DECL_INITIAL (decl) == error_mark_node)
2979 /* Don't output anything
2980 when a tentative file-scope definition is seen.
2981 But at end of compilation, do output code for them. */
2982 DECL_DEFER_OUTPUT (decl) = 1;
2983 rest_of_decl_compilation (decl, asmspec, true, 0);
2985 else
2987 /* This is a local variable. If there is an ASMSPEC, the
2988 user has requested that we handle it specially. */
2989 if (asmspec)
2991 /* In conjunction with an ASMSPEC, the `register'
2992 keyword indicates that we should place the variable
2993 in a particular register. */
2994 if (C_DECL_REGISTER (decl))
2996 DECL_HARD_REGISTER (decl) = 1;
2997 /* This cannot be done for a structure with volatile
2998 fields, on which DECL_REGISTER will have been
2999 reset. */
3000 if (!DECL_REGISTER (decl))
3001 error ("cannot put object with volatile field into register");
3004 /* If this is not a static variable, issue a warning.
3005 It doesn't make any sense to give an ASMSPEC for an
3006 ordinary, non-register local variable. Historically,
3007 GCC has accepted -- but ignored -- the ASMSPEC in
3008 this case. */
3009 if (TREE_CODE (decl) == VAR_DECL
3010 && !C_DECL_REGISTER (decl)
3011 && !TREE_STATIC (decl))
3012 warning ("%Jignoring asm-specifier for non-static local "
3013 "variable '%D'", decl, decl);
3014 else
3015 change_decl_assembler_name (decl, get_identifier (asmspec));
3018 if (TREE_CODE (decl) != FUNCTION_DECL)
3019 add_stmt (build_stmt (DECL_EXPR, decl));
3022 if (!DECL_FILE_SCOPE_P (decl))
3024 /* Recompute the RTL of a local array now
3025 if it used to be an incomplete type. */
3026 if (was_incomplete
3027 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3029 /* If we used it already as memory, it must stay in memory. */
3030 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3031 /* If it's still incomplete now, no init will save it. */
3032 if (DECL_SIZE (decl) == 0)
3033 DECL_INITIAL (decl) = 0;
3038 /* If this was marked 'used', be sure it will be output. */
3039 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3040 mark_decl_referenced (decl);
3042 if (TREE_CODE (decl) == TYPE_DECL)
3044 if (!DECL_FILE_SCOPE_P (decl)
3045 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3046 add_stmt (build_stmt (DECL_EXPR, decl));
3048 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3051 /* At the end of a declaration, throw away any variable type sizes
3052 of types defined inside that declaration. There is no use
3053 computing them in the following function definition. */
3054 if (current_scope == file_scope)
3055 get_pending_sizes ();
3057 /* Install a cleanup (aka destructor) if one was given. */
3058 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3060 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3061 if (attr)
3063 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3064 tree cleanup_decl = lookup_name (cleanup_id);
3065 tree cleanup;
3067 /* Build "cleanup(&decl)" for the destructor. */
3068 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3069 cleanup = build_tree_list (NULL_TREE, cleanup);
3070 cleanup = build_function_call (cleanup_decl, cleanup);
3072 /* Don't warn about decl unused; the cleanup uses it. */
3073 TREE_USED (decl) = 1;
3074 TREE_USED (cleanup_decl) = 1;
3076 /* Initialize EH, if we've been told to do so. */
3077 if (flag_exceptions && !c_eh_initialized_p)
3079 c_eh_initialized_p = true;
3080 eh_personality_libfunc
3081 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3082 ? "__gcc_personality_sj0"
3083 : "__gcc_personality_v0");
3084 using_eh_for_cleanups ();
3087 push_cleanup (decl, cleanup, false);
3092 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3093 and push that on the current scope. */
3095 void
3096 push_parm_decl (tree parm)
3098 tree decl;
3100 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3101 TREE_PURPOSE (TREE_PURPOSE (parm)),
3102 PARM, 0, NULL);
3103 decl_attributes (&decl, TREE_VALUE (parm), 0);
3105 decl = pushdecl (decl);
3107 finish_decl (decl, NULL_TREE, NULL_TREE);
3110 /* Mark all the parameter declarations to date as forward decls.
3111 Also diagnose use of this extension. */
3113 void
3114 mark_forward_parm_decls (void)
3116 struct c_binding *b;
3118 if (pedantic && !current_scope->warned_forward_parm_decls)
3120 pedwarn ("ISO C forbids forward parameter declarations");
3121 current_scope->warned_forward_parm_decls = true;
3124 for (b = current_scope->bindings; b; b = b->prev)
3125 if (TREE_CODE (b->decl) == PARM_DECL)
3126 TREE_ASM_WRITTEN (b->decl) = 1;
3129 static GTY(()) int compound_literal_number;
3131 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3132 literal, which may be an incomplete array type completed by the
3133 initializer; INIT is a CONSTRUCTOR that initializes the compound
3134 literal. */
3136 tree
3137 build_compound_literal (tree type, tree init)
3139 /* We do not use start_decl here because we have a type, not a declarator;
3140 and do not use finish_decl because the decl should be stored inside
3141 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3142 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3143 tree complit;
3144 tree stmt;
3145 DECL_EXTERNAL (decl) = 0;
3146 TREE_PUBLIC (decl) = 0;
3147 TREE_STATIC (decl) = (current_scope == file_scope);
3148 DECL_CONTEXT (decl) = current_function_decl;
3149 TREE_USED (decl) = 1;
3150 TREE_TYPE (decl) = type;
3151 TREE_READONLY (decl) = TYPE_READONLY (type);
3152 store_init_value (decl, init);
3154 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3156 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3157 if (failure)
3158 abort ();
3161 type = TREE_TYPE (decl);
3162 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3163 return error_mark_node;
3165 stmt = build_stmt (DECL_EXPR, decl);
3166 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3167 TREE_SIDE_EFFECTS (complit) = 1;
3169 layout_decl (decl, 0);
3171 if (TREE_STATIC (decl))
3173 /* This decl needs a name for the assembler output. We also need
3174 a unique suffix to be added to the name. */
3175 char *name;
3177 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3178 compound_literal_number);
3179 compound_literal_number++;
3180 DECL_NAME (decl) = get_identifier (name);
3181 DECL_DEFER_OUTPUT (decl) = 1;
3182 DECL_COMDAT (decl) = 1;
3183 DECL_ARTIFICIAL (decl) = 1;
3184 pushdecl (decl);
3185 rest_of_decl_compilation (decl, NULL, 1, 0);
3188 return complit;
3191 /* Make TYPE a complete type based on INITIAL_VALUE.
3192 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3193 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3196 complete_array_type (tree type, tree initial_value, int do_default)
3198 tree maxindex = NULL_TREE;
3199 int value = 0;
3201 if (initial_value)
3203 /* Note MAXINDEX is really the maximum index,
3204 one less than the size. */
3205 if (TREE_CODE (initial_value) == STRING_CST)
3207 int eltsize
3208 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3209 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3210 / eltsize) - 1, 0);
3212 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3214 tree elts = CONSTRUCTOR_ELTS (initial_value);
3215 maxindex = build_int_2 (-1, -1);
3216 for (; elts; elts = TREE_CHAIN (elts))
3218 if (TREE_PURPOSE (elts))
3219 maxindex = TREE_PURPOSE (elts);
3220 else
3221 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3222 maxindex, integer_one_node));
3224 maxindex = copy_node (maxindex);
3226 else
3228 /* Make an error message unless that happened already. */
3229 if (initial_value != error_mark_node)
3230 value = 1;
3232 /* Prevent further error messages. */
3233 maxindex = build_int_2 (0, 0);
3237 if (!maxindex)
3239 if (do_default)
3240 maxindex = build_int_2 (0, 0);
3241 value = 2;
3244 if (maxindex)
3246 TYPE_DOMAIN (type) = build_index_type (maxindex);
3247 if (!TREE_TYPE (maxindex))
3248 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3251 /* Lay out the type now that we can get the real answer. */
3253 layout_type (type);
3255 return value;
3258 /* Determine whether TYPE is a structure with a flexible array member,
3259 or a union containing such a structure (possibly recursively). */
3261 static bool
3262 flexible_array_type_p (tree type)
3264 tree x;
3265 switch (TREE_CODE (type))
3267 case RECORD_TYPE:
3268 x = TYPE_FIELDS (type);
3269 if (x == NULL_TREE)
3270 return false;
3271 while (TREE_CHAIN (x) != NULL_TREE)
3272 x = TREE_CHAIN (x);
3273 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3274 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3275 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3276 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3277 return true;
3278 return false;
3279 case UNION_TYPE:
3280 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3282 if (flexible_array_type_p (TREE_TYPE (x)))
3283 return true;
3285 return false;
3286 default:
3287 return false;
3291 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3292 replacing with appropriate values if they are invalid. */
3293 static void
3294 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3296 tree type_mv;
3297 unsigned int max_width;
3298 unsigned HOST_WIDE_INT w;
3299 const char *name = orig_name ? orig_name: _("<anonymous>");
3301 /* Necessary? */
3302 STRIP_NOPS (*width);
3304 /* Detect and ignore out of range field width and process valid
3305 field widths. */
3306 if (TREE_CODE (*width) != INTEGER_CST)
3308 error ("bit-field `%s' width not an integer constant", name);
3309 *width = integer_one_node;
3311 else
3313 constant_expression_warning (*width);
3314 if (tree_int_cst_sgn (*width) < 0)
3316 error ("negative width in bit-field `%s'", name);
3317 *width = integer_one_node;
3319 else if (integer_zerop (*width) && orig_name)
3321 error ("zero width for bit-field `%s'", name);
3322 *width = integer_one_node;
3326 /* Detect invalid bit-field type. */
3327 if (TREE_CODE (*type) != INTEGER_TYPE
3328 && TREE_CODE (*type) != BOOLEAN_TYPE
3329 && TREE_CODE (*type) != ENUMERAL_TYPE)
3331 error ("bit-field `%s' has invalid type", name);
3332 *type = unsigned_type_node;
3335 type_mv = TYPE_MAIN_VARIANT (*type);
3336 if (pedantic
3337 && type_mv != integer_type_node
3338 && type_mv != unsigned_type_node
3339 && type_mv != boolean_type_node)
3340 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3342 if (type_mv == boolean_type_node)
3343 max_width = CHAR_TYPE_SIZE;
3344 else
3345 max_width = TYPE_PRECISION (*type);
3347 if (0 < compare_tree_int (*width, max_width))
3349 error ("width of `%s' exceeds its type", name);
3350 w = max_width;
3351 *width = build_int_2 (w, 0);
3353 else
3354 w = tree_low_cst (*width, 1);
3356 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3358 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3359 if (!lt
3360 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3361 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3362 warning ("`%s' is narrower than values of its type", name);
3366 /* Given declspecs and a declarator,
3367 determine the name and type of the object declared
3368 and construct a ..._DECL node for it.
3369 (In one case we can return a ..._TYPE node instead.
3370 For invalid input we sometimes return 0.)
3372 DECLSPECS is a chain of tree_list nodes whose value fields
3373 are the storage classes and type specifiers.
3375 DECL_CONTEXT says which syntactic context this declaration is in:
3376 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3377 FUNCDEF for a function definition. Like NORMAL but a few different
3378 error messages in each case. Return value may be zero meaning
3379 this definition is too screwy to try to parse.
3380 PARM for a parameter declaration (either within a function prototype
3381 or before a function body). Make a PARM_DECL, or return void_type_node.
3382 TYPENAME if for a typename (in a cast or sizeof).
3383 Don't make a DECL node; just return the ..._TYPE node.
3384 FIELD for a struct or union field; make a FIELD_DECL.
3385 INITIALIZED is 1 if the decl has an initializer.
3386 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3387 representing the width of the bit-field.
3389 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3390 It may also be so in the PARM case, for a prototype where the
3391 argument type is specified but not the name.
3393 This function is where the complicated C meanings of `static'
3394 and `extern' are interpreted. */
3396 static tree
3397 grokdeclarator (tree declarator, tree declspecs,
3398 enum decl_context decl_context, int initialized, tree *width)
3400 int specbits = 0;
3401 tree spec;
3402 tree type = NULL_TREE;
3403 int longlong = 0;
3404 int constp;
3405 int restrictp;
3406 int volatilep;
3407 int type_quals = TYPE_UNQUALIFIED;
3408 int inlinep;
3409 int explicit_int = 0;
3410 int explicit_char = 0;
3411 int defaulted_int = 0;
3412 tree typedef_decl = 0;
3413 const char *name, *orig_name;
3414 tree typedef_type = 0;
3415 int funcdef_flag = 0;
3416 enum tree_code innermost_code = ERROR_MARK;
3417 int size_varies = 0;
3418 tree decl_attr = NULL_TREE;
3419 tree array_ptr_quals = NULL_TREE;
3420 int array_parm_static = 0;
3421 tree returned_attrs = NULL_TREE;
3422 bool bitfield = width != NULL;
3423 tree element_type;
3424 tree arg_info = NULL_TREE;
3426 if (decl_context == FUNCDEF)
3427 funcdef_flag = 1, decl_context = NORMAL;
3429 /* Look inside a declarator for the name being declared
3430 and get it as a string, for an error message. */
3432 tree decl = declarator;
3433 name = 0;
3435 while (decl)
3436 switch (TREE_CODE (decl))
3438 case ARRAY_REF:
3439 case INDIRECT_REF:
3440 case CALL_EXPR:
3441 innermost_code = TREE_CODE (decl);
3442 decl = TREE_OPERAND (decl, 0);
3443 break;
3445 case TREE_LIST:
3446 decl = TREE_VALUE (decl);
3447 break;
3449 case IDENTIFIER_NODE:
3450 name = IDENTIFIER_POINTER (decl);
3451 decl = 0;
3452 break;
3454 default:
3455 abort ();
3457 orig_name = name;
3458 if (name == 0)
3459 name = "type name";
3462 /* A function definition's declarator must have the form of
3463 a function declarator. */
3465 if (funcdef_flag && innermost_code != CALL_EXPR)
3466 return 0;
3468 /* If this looks like a function definition, make it one,
3469 even if it occurs where parms are expected.
3470 Then store_parm_decls will reject it and not use it as a parm. */
3471 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3472 decl_context = PARM;
3474 /* Look through the decl specs and record which ones appear.
3475 Some typespecs are defined as built-in typenames.
3476 Others, the ones that are modifiers of other types,
3477 are represented by bits in SPECBITS: set the bits for
3478 the modifiers that appear. Storage class keywords are also in SPECBITS.
3480 If there is a typedef name or a type, store the type in TYPE.
3481 This includes builtin typedefs such as `int'.
3483 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3484 and did not come from a user typedef.
3486 Set LONGLONG if `long' is mentioned twice. */
3488 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3490 tree id = TREE_VALUE (spec);
3492 /* If the entire declaration is itself tagged as deprecated then
3493 suppress reports of deprecated items. */
3494 if (id && TREE_DEPRECATED (id))
3496 if (deprecated_state != DEPRECATED_SUPPRESS)
3497 warn_deprecated_use (id);
3500 if (id == ridpointers[(int) RID_INT])
3501 explicit_int = 1;
3502 if (id == ridpointers[(int) RID_CHAR])
3503 explicit_char = 1;
3505 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3507 enum rid i = C_RID_CODE (id);
3508 if ((int) i <= (int) RID_LAST_MODIFIER)
3510 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3512 if (longlong)
3513 error ("`long long long' is too long for GCC");
3514 else
3516 if (pedantic && !flag_isoc99 && ! in_system_header
3517 && warn_long_long)
3518 pedwarn ("ISO C90 does not support `long long'");
3519 longlong = 1;
3522 else if (specbits & (1 << (int) i))
3524 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3526 if (pedantic && !flag_isoc99)
3527 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3529 else
3530 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3533 /* Diagnose "__thread extern". Recall that this list
3534 is in the reverse order seen in the text. */
3535 if (i == RID_THREAD
3536 && (specbits & (1 << (int) RID_EXTERN
3537 | 1 << (int) RID_STATIC)))
3539 if (specbits & 1 << (int) RID_EXTERN)
3540 error ("`__thread' before `extern'");
3541 else
3542 error ("`__thread' before `static'");
3545 specbits |= 1 << (int) i;
3546 goto found;
3549 if (type)
3550 error ("two or more data types in declaration of `%s'", name);
3551 /* Actual typedefs come to us as TYPE_DECL nodes. */
3552 else if (TREE_CODE (id) == TYPE_DECL)
3554 if (TREE_TYPE (id) == error_mark_node)
3555 ; /* Allow the type to default to int to avoid cascading errors. */
3556 else
3558 type = TREE_TYPE (id);
3559 decl_attr = DECL_ATTRIBUTES (id);
3560 typedef_decl = id;
3563 /* Built-in types come as identifiers. */
3564 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3566 tree t = lookup_name (id);
3567 if (!t || TREE_CODE (t) != TYPE_DECL)
3568 error ("`%s' fails to be a typedef or built in type",
3569 IDENTIFIER_POINTER (id));
3570 else if (TREE_TYPE (t) == error_mark_node)
3572 else
3574 type = TREE_TYPE (t);
3575 typedef_decl = t;
3578 else if (TREE_CODE (id) != ERROR_MARK)
3579 type = id;
3581 found:
3585 typedef_type = type;
3586 if (type)
3587 size_varies = C_TYPE_VARIABLE_SIZE (type);
3589 /* No type at all: default to `int', and set DEFAULTED_INT
3590 because it was not a user-defined typedef. */
3592 if (type == 0)
3594 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3595 | (1 << (int) RID_SIGNED)
3596 | (1 << (int) RID_UNSIGNED)
3597 | (1 << (int) RID_COMPLEX))))
3598 /* Don't warn about typedef foo = bar. */
3599 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3600 && ! in_system_header)
3602 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3603 and this is a function, or if -Wimplicit; prefer the former
3604 warning since it is more explicit. */
3605 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3606 && funcdef_flag)
3607 warn_about_return_type = 1;
3608 else if (warn_implicit_int || flag_isoc99)
3609 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3610 name);
3613 defaulted_int = 1;
3614 type = integer_type_node;
3617 /* Now process the modifiers that were specified
3618 and check for invalid combinations. */
3620 /* Long double is a special combination. */
3622 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3623 && TYPE_MAIN_VARIANT (type) == double_type_node)
3625 specbits &= ~(1 << (int) RID_LONG);
3626 type = long_double_type_node;
3629 /* Check all other uses of type modifiers. */
3631 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3632 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3634 int ok = 0;
3636 if ((specbits & 1 << (int) RID_LONG)
3637 && (specbits & 1 << (int) RID_SHORT))
3638 error ("both long and short specified for `%s'", name);
3639 else if (((specbits & 1 << (int) RID_LONG)
3640 || (specbits & 1 << (int) RID_SHORT))
3641 && explicit_char)
3642 error ("long or short specified with char for `%s'", name);
3643 else if (((specbits & 1 << (int) RID_LONG)
3644 || (specbits & 1 << (int) RID_SHORT))
3645 && TREE_CODE (type) == REAL_TYPE)
3647 static int already = 0;
3649 error ("long or short specified with floating type for `%s'", name);
3650 if (! already && ! pedantic)
3652 error ("the only valid combination is `long double'");
3653 already = 1;
3656 else if ((specbits & 1 << (int) RID_SIGNED)
3657 && (specbits & 1 << (int) RID_UNSIGNED))
3658 error ("both signed and unsigned specified for `%s'", name);
3659 else if (TREE_CODE (type) != INTEGER_TYPE)
3660 error ("long, short, signed or unsigned invalid for `%s'", name);
3661 else
3663 ok = 1;
3664 if (!explicit_int && !defaulted_int && !explicit_char)
3666 error ("long, short, signed or unsigned used invalidly for `%s'",
3667 name);
3668 ok = 0;
3672 /* Discard the type modifiers if they are invalid. */
3673 if (! ok)
3675 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3676 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3677 longlong = 0;
3681 if ((specbits & (1 << (int) RID_COMPLEX))
3682 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3684 error ("complex invalid for `%s'", name);
3685 specbits &= ~(1 << (int) RID_COMPLEX);
3688 /* Decide whether an integer type is signed or not.
3689 Optionally treat bit-fields as signed by default. */
3690 if (specbits & 1 << (int) RID_UNSIGNED
3691 || (bitfield && ! flag_signed_bitfields
3692 && (explicit_int || defaulted_int || explicit_char
3693 /* A typedef for plain `int' without `signed'
3694 can be controlled just like plain `int'. */
3695 || ! (typedef_decl != 0
3696 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3697 && TREE_CODE (type) != ENUMERAL_TYPE
3698 && !(specbits & 1 << (int) RID_SIGNED)))
3700 if (longlong)
3701 type = long_long_unsigned_type_node;
3702 else if (specbits & 1 << (int) RID_LONG)
3703 type = long_unsigned_type_node;
3704 else if (specbits & 1 << (int) RID_SHORT)
3705 type = short_unsigned_type_node;
3706 else if (type == char_type_node)
3707 type = unsigned_char_type_node;
3708 else if (typedef_decl)
3709 type = c_common_unsigned_type (type);
3710 else
3711 type = unsigned_type_node;
3713 else if ((specbits & 1 << (int) RID_SIGNED)
3714 && type == char_type_node)
3715 type = signed_char_type_node;
3716 else if (longlong)
3717 type = long_long_integer_type_node;
3718 else if (specbits & 1 << (int) RID_LONG)
3719 type = long_integer_type_node;
3720 else if (specbits & 1 << (int) RID_SHORT)
3721 type = short_integer_type_node;
3723 if (specbits & 1 << (int) RID_COMPLEX)
3725 if (pedantic && !flag_isoc99)
3726 pedwarn ("ISO C90 does not support complex types");
3727 /* If we just have "complex", it is equivalent to
3728 "complex double", but if any modifiers at all are specified it is
3729 the complex form of TYPE. E.g, "complex short" is
3730 "complex short int". */
3732 if (defaulted_int && ! longlong
3733 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3734 | (1 << (int) RID_SIGNED)
3735 | (1 << (int) RID_UNSIGNED))))
3737 if (pedantic)
3738 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3739 type = complex_double_type_node;
3741 else if (type == integer_type_node)
3743 if (pedantic)
3744 pedwarn ("ISO C does not support complex integer types");
3745 type = complex_integer_type_node;
3747 else if (type == float_type_node)
3748 type = complex_float_type_node;
3749 else if (type == double_type_node)
3750 type = complex_double_type_node;
3751 else if (type == long_double_type_node)
3752 type = complex_long_double_type_node;
3753 else
3755 if (pedantic)
3756 pedwarn ("ISO C does not support complex integer types");
3757 type = build_complex_type (type);
3761 /* Check the type and width of a bit-field. */
3762 if (bitfield)
3763 check_bitfield_type_and_width (&type, width, orig_name);
3765 /* Figure out the type qualifiers for the declaration. There are
3766 two ways a declaration can become qualified. One is something
3767 like `const int i' where the `const' is explicit. Another is
3768 something like `typedef const int CI; CI i' where the type of the
3769 declaration contains the `const'. A third possibility is that
3770 there is a type qualifier on the element type of a typedefed
3771 array type, in which case we should extract that qualifier so
3772 that c_apply_type_quals_to_decls receives the full list of
3773 qualifiers to work with (C90 is not entirely clear about whether
3774 duplicate qualifiers should be diagnosed in this case, but it
3775 seems most appropriate to do so). */
3776 element_type = strip_array_types (type);
3777 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3778 restrictp
3779 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3780 volatilep
3781 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3782 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3783 if (pedantic && !flag_isoc99)
3785 if (constp > 1)
3786 pedwarn ("duplicate `const'");
3787 if (restrictp > 1)
3788 pedwarn ("duplicate `restrict'");
3789 if (volatilep > 1)
3790 pedwarn ("duplicate `volatile'");
3792 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3793 type = TYPE_MAIN_VARIANT (type);
3794 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3795 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3796 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3798 /* Warn if two storage classes are given. Default to `auto'. */
3801 int nclasses = 0;
3803 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3804 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3805 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3806 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3807 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3809 /* "static __thread" and "extern __thread" are allowed. */
3810 if ((specbits & (1 << (int) RID_THREAD
3811 | 1 << (int) RID_STATIC
3812 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3813 nclasses++;
3815 /* Warn about storage classes that are invalid for certain
3816 kinds of declarations (parameters, typenames, etc.). */
3818 if (nclasses > 1)
3819 error ("multiple storage classes in declaration of `%s'", name);
3820 else if (funcdef_flag
3821 && (specbits
3822 & ((1 << (int) RID_REGISTER)
3823 | (1 << (int) RID_AUTO)
3824 | (1 << (int) RID_TYPEDEF)
3825 | (1 << (int) RID_THREAD))))
3827 if (specbits & 1 << (int) RID_AUTO
3828 && (pedantic || current_scope == file_scope))
3829 pedwarn ("function definition declared `auto'");
3830 if (specbits & 1 << (int) RID_REGISTER)
3831 error ("function definition declared `register'");
3832 if (specbits & 1 << (int) RID_TYPEDEF)
3833 error ("function definition declared `typedef'");
3834 if (specbits & 1 << (int) RID_THREAD)
3835 error ("function definition declared `__thread'");
3836 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3837 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3839 else if (decl_context != NORMAL && nclasses > 0)
3841 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3843 else
3845 switch (decl_context)
3847 case FIELD:
3848 error ("storage class specified for structure field `%s'",
3849 name);
3850 break;
3851 case PARM:
3852 error ("storage class specified for parameter `%s'", name);
3853 break;
3854 default:
3855 error ("storage class specified for typename");
3856 break;
3858 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3859 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3860 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3863 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3865 /* `extern' with initialization is invalid if not at file scope. */
3866 if (current_scope == file_scope)
3867 warning ("`%s' initialized and declared `extern'", name);
3868 else
3869 error ("`%s' has both `extern' and initializer", name);
3871 else if (current_scope == file_scope)
3873 if (specbits & 1 << (int) RID_AUTO)
3874 error ("file-scope declaration of `%s' specifies `auto'", name);
3876 else
3878 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3879 error ("nested function `%s' declared `extern'", name);
3880 else if ((specbits & (1 << (int) RID_THREAD
3881 | 1 << (int) RID_EXTERN
3882 | 1 << (int) RID_STATIC))
3883 == (1 << (int) RID_THREAD))
3885 error ("function-scope `%s' implicitly auto and declared `__thread'",
3886 name);
3887 specbits &= ~(1 << (int) RID_THREAD);
3892 /* Now figure out the structure of the declarator proper.
3893 Descend through it, creating more complex types, until we reach
3894 the declared identifier (or NULL_TREE, in an absolute declarator). */
3896 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3898 if (type == error_mark_node)
3900 declarator = TREE_OPERAND (declarator, 0);
3901 continue;
3904 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3905 an INDIRECT_REF (for *...),
3906 a CALL_EXPR (for ...(...)),
3907 a TREE_LIST (for nested attributes),
3908 an identifier (for the name being declared)
3909 or a null pointer (for the place in an absolute declarator
3910 where the name was omitted).
3911 For the last two cases, we have just exited the loop.
3913 At this point, TYPE is the type of elements of an array,
3914 or for a function to return, or for a pointer to point to.
3915 After this sequence of ifs, TYPE is the type of the
3916 array or function or pointer, and DECLARATOR has had its
3917 outermost layer removed. */
3919 if (array_ptr_quals != NULL_TREE || array_parm_static)
3921 /* Only the innermost declarator (making a parameter be of
3922 array type which is converted to pointer type)
3923 may have static or type qualifiers. */
3924 error ("static or type qualifiers in non-parameter array declarator");
3925 array_ptr_quals = NULL_TREE;
3926 array_parm_static = 0;
3929 if (TREE_CODE (declarator) == TREE_LIST)
3931 /* We encode a declarator with embedded attributes using
3932 a TREE_LIST. */
3933 tree attrs = TREE_PURPOSE (declarator);
3934 tree inner_decl;
3935 int attr_flags = 0;
3936 declarator = TREE_VALUE (declarator);
3937 inner_decl = declarator;
3938 while (inner_decl != NULL_TREE
3939 && TREE_CODE (inner_decl) == TREE_LIST)
3940 inner_decl = TREE_VALUE (inner_decl);
3941 if (inner_decl == NULL_TREE
3942 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3943 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3944 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3945 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3946 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3947 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3948 returned_attrs = decl_attributes (&type,
3949 chainon (returned_attrs, attrs),
3950 attr_flags);
3952 else if (TREE_CODE (declarator) == ARRAY_REF)
3954 tree itype = NULL_TREE;
3955 tree size = TREE_OPERAND (declarator, 1);
3956 /* The index is a signed object `sizetype' bits wide. */
3957 tree index_type = c_common_signed_type (sizetype);
3959 array_ptr_quals = TREE_TYPE (declarator);
3960 array_parm_static = TREE_STATIC (declarator);
3962 declarator = TREE_OPERAND (declarator, 0);
3964 /* Check for some types that there cannot be arrays of. */
3966 if (VOID_TYPE_P (type))
3968 error ("declaration of `%s' as array of voids", name);
3969 type = error_mark_node;
3972 if (TREE_CODE (type) == FUNCTION_TYPE)
3974 error ("declaration of `%s' as array of functions", name);
3975 type = error_mark_node;
3978 if (pedantic && !in_system_header && flexible_array_type_p (type))
3979 pedwarn ("invalid use of structure with flexible array member");
3981 if (size == error_mark_node)
3982 type = error_mark_node;
3984 if (type == error_mark_node)
3985 continue;
3987 /* If size was specified, set ITYPE to a range-type for that size.
3988 Otherwise, ITYPE remains null. finish_decl may figure it out
3989 from an initial value. */
3991 if (size)
3993 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3994 STRIP_TYPE_NOPS (size);
3996 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3998 error ("size of array `%s' has non-integer type", name);
3999 size = integer_one_node;
4002 if (pedantic && integer_zerop (size))
4003 pedwarn ("ISO C forbids zero-size array `%s'", name);
4005 if (TREE_CODE (size) == INTEGER_CST)
4007 constant_expression_warning (size);
4008 if (tree_int_cst_sgn (size) < 0)
4010 error ("size of array `%s' is negative", name);
4011 size = integer_one_node;
4014 else
4016 /* Make sure the array size remains visibly nonconstant
4017 even if it is (eg) a const variable with known value. */
4018 size_varies = 1;
4020 if (!flag_isoc99 && pedantic)
4022 if (TREE_CONSTANT (size))
4023 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4024 name);
4025 else
4026 pedwarn ("ISO C90 forbids variable-size array `%s'",
4027 name);
4031 if (integer_zerop (size))
4033 /* A zero-length array cannot be represented with an
4034 unsigned index type, which is what we'll get with
4035 build_index_type. Create an open-ended range instead. */
4036 itype = build_range_type (sizetype, size, NULL_TREE);
4038 else
4040 /* Compute the maximum valid index, that is, size - 1.
4041 Do the calculation in index_type, so that if it is
4042 a variable the computations will be done in the
4043 proper mode. */
4044 itype = fold (build (MINUS_EXPR, index_type,
4045 convert (index_type, size),
4046 convert (index_type, size_one_node)));
4048 /* If that overflowed, the array is too big.
4049 ??? While a size of INT_MAX+1 technically shouldn't
4050 cause an overflow (because we subtract 1), the overflow
4051 is recorded during the conversion to index_type, before
4052 the subtraction. Handling this case seems like an
4053 unnecessary complication. */
4054 if (TREE_OVERFLOW (itype))
4056 error ("size of array `%s' is too large", name);
4057 type = error_mark_node;
4058 continue;
4061 if (size_varies)
4062 itype = variable_size (itype);
4063 itype = build_index_type (itype);
4066 else if (decl_context == FIELD)
4068 if (pedantic && !flag_isoc99 && !in_system_header)
4069 pedwarn ("ISO C90 does not support flexible array members");
4071 /* ISO C99 Flexible array members are effectively identical
4072 to GCC's zero-length array extension. */
4073 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4076 /* If pedantic, complain about arrays of incomplete types. */
4078 if (pedantic && !COMPLETE_TYPE_P (type))
4079 pedwarn ("array type has incomplete element type");
4081 /* Build the array type itself, then merge any constancy or
4082 volatility into the target type. We must do it in this order
4083 to ensure that the TYPE_MAIN_VARIANT field of the array type
4084 is set correctly. */
4086 type = build_array_type (type, itype);
4087 if (type_quals)
4088 type = c_build_qualified_type (type, type_quals);
4090 if (size_varies)
4091 C_TYPE_VARIABLE_SIZE (type) = 1;
4093 /* The GCC extension for zero-length arrays differs from
4094 ISO flexible array members in that sizeof yields zero. */
4095 if (size && integer_zerop (size))
4097 layout_type (type);
4098 TYPE_SIZE (type) = bitsize_zero_node;
4099 TYPE_SIZE_UNIT (type) = size_zero_node;
4101 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4102 /* We can never complete an array type which is the target of a
4103 pointer, so go ahead and lay it out. */
4104 layout_type (type);
4106 if (decl_context != PARM
4107 && (array_ptr_quals != NULL_TREE || array_parm_static))
4109 error ("static or type qualifiers in non-parameter array declarator");
4110 array_ptr_quals = NULL_TREE;
4111 array_parm_static = 0;
4114 else if (TREE_CODE (declarator) == CALL_EXPR)
4116 /* Say it's a definition only for the CALL_EXPR closest to
4117 the identifier. */
4118 bool really_funcdef = (funcdef_flag
4119 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4120 == IDENTIFIER_NODE));
4121 tree arg_types;
4123 /* Declaring a function type.
4124 Make sure we have a valid type for the function to return. */
4125 if (type == error_mark_node)
4126 continue;
4128 size_varies = 0;
4130 /* Warn about some types functions can't return. */
4132 if (TREE_CODE (type) == FUNCTION_TYPE)
4134 error ("`%s' declared as function returning a function", name);
4135 type = integer_type_node;
4137 if (TREE_CODE (type) == ARRAY_TYPE)
4139 error ("`%s' declared as function returning an array", name);
4140 type = integer_type_node;
4143 /* Construct the function type and go to the next
4144 inner layer of declarator. */
4145 arg_info = TREE_OPERAND (declarator, 1);
4146 arg_types = grokparms (arg_info, really_funcdef);
4148 /* Type qualifiers before the return type of the function
4149 qualify the return type, not the function type. */
4150 if (type_quals)
4152 /* Type qualifiers on a function return type are normally
4153 permitted by the standard but have no effect, so give a
4154 warning at -Wextra. Qualifiers on a void return type have
4155 meaning as a GNU extension, and are banned on function
4156 definitions in ISO C. FIXME: strictly we shouldn't
4157 pedwarn for qualified void return types except on function
4158 definitions, but not doing so could lead to the undesirable
4159 state of a "volatile void" function return type not being
4160 warned about, and a use of the function being compiled
4161 with GNU semantics, with no diagnostics under -pedantic. */
4162 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4163 pedwarn ("ISO C forbids qualified void function return type");
4164 else if (extra_warnings
4165 && !(VOID_TYPE_P (type)
4166 && type_quals == TYPE_QUAL_VOLATILE))
4167 warning ("type qualifiers ignored on function return type");
4169 type = c_build_qualified_type (type, type_quals);
4171 type_quals = TYPE_UNQUALIFIED;
4173 type = build_function_type (type, arg_types);
4174 declarator = TREE_OPERAND (declarator, 0);
4176 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4177 the formal parameter list of this FUNCTION_TYPE to point to
4178 the FUNCTION_TYPE node itself. */
4181 tree link;
4183 for (link = ARG_INFO_TAGS (arg_info);
4184 link;
4185 link = TREE_CHAIN (link))
4186 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4189 else if (TREE_CODE (declarator) == INDIRECT_REF)
4191 /* Merge any constancy or volatility into the target type
4192 for the pointer. */
4194 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4195 && type_quals)
4196 pedwarn ("ISO C forbids qualified function types");
4197 if (type_quals)
4198 type = c_build_qualified_type (type, type_quals);
4199 type_quals = TYPE_UNQUALIFIED;
4200 size_varies = 0;
4202 type = build_pointer_type (type);
4204 /* Process a list of type modifier keywords
4205 (such as const or volatile) that were given inside the `*'. */
4207 if (TREE_TYPE (declarator))
4209 tree typemodlist;
4210 int erred = 0;
4212 constp = 0;
4213 volatilep = 0;
4214 restrictp = 0;
4215 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4216 typemodlist = TREE_CHAIN (typemodlist))
4218 tree qualifier = TREE_VALUE (typemodlist);
4220 if (C_IS_RESERVED_WORD (qualifier))
4222 if (C_RID_CODE (qualifier) == RID_CONST)
4223 constp++;
4224 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4225 volatilep++;
4226 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4227 restrictp++;
4228 else
4229 erred++;
4231 else
4232 erred++;
4235 if (erred)
4236 error ("invalid type modifier within pointer declarator");
4237 if (pedantic && !flag_isoc99)
4239 if (constp > 1)
4240 pedwarn ("duplicate `const'");
4241 if (volatilep > 1)
4242 pedwarn ("duplicate `volatile'");
4243 if (restrictp > 1)
4244 pedwarn ("duplicate `restrict'");
4247 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4248 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4249 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4252 declarator = TREE_OPERAND (declarator, 0);
4254 else
4255 abort ();
4259 /* Now TYPE has the actual type. */
4261 /* Did array size calculations overflow? */
4263 if (TREE_CODE (type) == ARRAY_TYPE
4264 && COMPLETE_TYPE_P (type)
4265 && TREE_OVERFLOW (TYPE_SIZE (type)))
4267 error ("size of array `%s' is too large", name);
4268 /* If we proceed with the array type as it is, we'll eventually
4269 crash in tree_low_cst(). */
4270 type = error_mark_node;
4273 /* If this is declaring a typedef name, return a TYPE_DECL. */
4275 if (specbits & (1 << (int) RID_TYPEDEF))
4277 tree decl;
4278 /* Note that the grammar rejects storage classes
4279 in typenames, fields or parameters */
4280 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4281 && type_quals)
4282 pedwarn ("ISO C forbids qualified function types");
4283 if (type_quals)
4284 type = c_build_qualified_type (type, type_quals);
4285 decl = build_decl (TYPE_DECL, declarator, type);
4286 if ((specbits & (1 << (int) RID_SIGNED))
4287 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4288 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4289 decl_attributes (&decl, returned_attrs, 0);
4290 return decl;
4293 /* Detect the case of an array type of unspecified size
4294 which came, as such, direct from a typedef name.
4295 We must copy the type, so that each identifier gets
4296 a distinct type, so that each identifier's size can be
4297 controlled separately by its own initializer. */
4299 if (type != 0 && typedef_type != 0
4300 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4301 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4303 type = build_array_type (TREE_TYPE (type), 0);
4304 if (size_varies)
4305 C_TYPE_VARIABLE_SIZE (type) = 1;
4308 /* If this is a type name (such as, in a cast or sizeof),
4309 compute the type and return it now. */
4311 if (decl_context == TYPENAME)
4313 /* Note that the grammar rejects storage classes
4314 in typenames, fields or parameters */
4315 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4316 && type_quals)
4317 pedwarn ("ISO C forbids const or volatile function types");
4318 if (type_quals)
4319 type = c_build_qualified_type (type, type_quals);
4320 decl_attributes (&type, returned_attrs, 0);
4321 return type;
4324 /* Aside from typedefs and type names (handle above),
4325 `void' at top level (not within pointer)
4326 is allowed only in public variables.
4327 We don't complain about parms either, but that is because
4328 a better error message can be made later. */
4330 if (VOID_TYPE_P (type) && decl_context != PARM
4331 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4332 && ((specbits & (1 << (int) RID_EXTERN))
4333 || (current_scope == file_scope
4334 && !(specbits
4335 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4337 error ("variable or field `%s' declared void", name);
4338 type = integer_type_node;
4341 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4342 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4345 tree decl;
4347 if (decl_context == PARM)
4349 tree type_as_written;
4350 tree promoted_type;
4352 /* A parameter declared as an array of T is really a pointer to T.
4353 One declared as a function is really a pointer to a function. */
4355 if (TREE_CODE (type) == ARRAY_TYPE)
4357 /* Transfer const-ness of array into that of type pointed to. */
4358 type = TREE_TYPE (type);
4359 if (type_quals)
4360 type = c_build_qualified_type (type, type_quals);
4361 type = build_pointer_type (type);
4362 type_quals = TYPE_UNQUALIFIED;
4363 if (array_ptr_quals)
4365 tree new_ptr_quals, new_ptr_attrs;
4366 int erred = 0;
4367 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4368 /* We don't yet implement attributes in this context. */
4369 if (new_ptr_attrs != NULL_TREE)
4370 warning ("attributes in parameter array declarator ignored");
4372 constp = 0;
4373 volatilep = 0;
4374 restrictp = 0;
4375 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4377 tree qualifier = TREE_VALUE (new_ptr_quals);
4379 if (C_IS_RESERVED_WORD (qualifier))
4381 if (C_RID_CODE (qualifier) == RID_CONST)
4382 constp++;
4383 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4384 volatilep++;
4385 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4386 restrictp++;
4387 else
4388 erred++;
4390 else
4391 erred++;
4394 if (erred)
4395 error ("invalid type modifier within array declarator");
4397 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4398 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4399 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4401 size_varies = 0;
4403 else if (TREE_CODE (type) == FUNCTION_TYPE)
4405 if (pedantic && type_quals)
4406 pedwarn ("ISO C forbids qualified function types");
4407 if (type_quals)
4408 type = c_build_qualified_type (type, type_quals);
4409 type = build_pointer_type (type);
4410 type_quals = TYPE_UNQUALIFIED;
4412 else if (type_quals)
4413 type = c_build_qualified_type (type, type_quals);
4415 type_as_written = type;
4417 decl = build_decl (PARM_DECL, declarator, type);
4418 if (size_varies)
4419 C_DECL_VARIABLE_SIZE (decl) = 1;
4421 /* Compute the type actually passed in the parmlist,
4422 for the case where there is no prototype.
4423 (For example, shorts and chars are passed as ints.)
4424 When there is a prototype, this is overridden later. */
4426 if (type == error_mark_node)
4427 promoted_type = type;
4428 else
4429 promoted_type = c_type_promotes_to (type);
4431 DECL_ARG_TYPE (decl) = promoted_type;
4432 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4434 else if (decl_context == FIELD)
4436 /* Structure field. It may not be a function. */
4438 if (TREE_CODE (type) == FUNCTION_TYPE)
4440 error ("field `%s' declared as a function", name);
4441 type = build_pointer_type (type);
4443 else if (TREE_CODE (type) != ERROR_MARK
4444 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4446 error ("field `%s' has incomplete type", name);
4447 type = error_mark_node;
4449 /* Move type qualifiers down to element of an array. */
4450 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4451 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4452 type_quals),
4453 TYPE_DOMAIN (type));
4454 decl = build_decl (FIELD_DECL, declarator, type);
4455 DECL_NONADDRESSABLE_P (decl) = bitfield;
4457 if (size_varies)
4458 C_DECL_VARIABLE_SIZE (decl) = 1;
4460 else if (TREE_CODE (type) == FUNCTION_TYPE)
4462 /* Every function declaration is "external"
4463 except for those which are inside a function body
4464 in which `auto' is used.
4465 That is a case not specified by ANSI C,
4466 and we use it for forward declarations for nested functions. */
4467 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4468 || current_scope == file_scope);
4470 if (specbits & (1 << (int) RID_AUTO)
4471 && (pedantic || current_scope == file_scope))
4472 pedwarn ("invalid storage class for function `%s'", name);
4473 if (specbits & (1 << (int) RID_REGISTER))
4474 error ("invalid storage class for function `%s'", name);
4475 if (specbits & (1 << (int) RID_THREAD))
4476 error ("invalid storage class for function `%s'", name);
4477 /* Function declaration not at file scope.
4478 Storage classes other than `extern' are not allowed
4479 and `extern' makes no difference. */
4480 if (current_scope != file_scope
4481 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4482 && pedantic)
4483 pedwarn ("invalid storage class for function `%s'", name);
4485 decl = build_decl (FUNCTION_DECL, declarator, type);
4486 decl = build_decl_attribute_variant (decl, decl_attr);
4488 DECL_LANG_SPECIFIC (decl)
4489 = ggc_alloc_cleared (sizeof (struct lang_decl));
4491 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4492 pedwarn ("ISO C forbids qualified function types");
4494 /* GNU C interprets a `volatile void' return type to indicate
4495 that the function does not return. */
4496 if ((type_quals & TYPE_QUAL_VOLATILE)
4497 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4498 warning ("`noreturn' function returns non-void value");
4500 if (extern_ref)
4501 DECL_EXTERNAL (decl) = 1;
4502 /* Record absence of global scope for `static' or `auto'. */
4503 TREE_PUBLIC (decl)
4504 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4506 /* For a function definition, record the argument information
4507 block in DECL_ARGUMENTS where store_parm_decls will look
4508 for it. */
4509 if (funcdef_flag)
4510 DECL_ARGUMENTS (decl) = arg_info;
4512 if (defaulted_int)
4513 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4515 /* Record presence of `inline', if it is reasonable. */
4516 if (MAIN_NAME_P (declarator))
4518 if (inlinep)
4519 warning ("cannot inline function `main'");
4521 else if (inlinep)
4523 /* Record that the function is declared `inline'. */
4524 DECL_DECLARED_INLINE_P (decl) = 1;
4526 /* Do not mark bare declarations as DECL_INLINE. Doing so
4527 in the presence of multiple declarations can result in
4528 the abstract origin pointing between the declarations,
4529 which will confuse dwarf2out. */
4530 if (initialized)
4532 DECL_INLINE (decl) = 1;
4533 if (specbits & (1 << (int) RID_EXTERN))
4534 current_extern_inline = 1;
4537 /* If -finline-functions, assume it can be inlined. This does
4538 two things: let the function be deferred until it is actually
4539 needed, and let dwarf2 know that the function is inlinable. */
4540 else if (flag_inline_trees == 2 && initialized)
4541 DECL_INLINE (decl) = 1;
4543 else
4545 /* It's a variable. */
4546 /* An uninitialized decl with `extern' is a reference. */
4547 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4549 /* Move type qualifiers down to element of an array. */
4550 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4552 int saved_align = TYPE_ALIGN(type);
4553 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4554 type_quals),
4555 TYPE_DOMAIN (type));
4556 TYPE_ALIGN (type) = saved_align;
4558 else if (type_quals)
4559 type = c_build_qualified_type (type, type_quals);
4561 /* C99 6.2.2p7: It is invalid (compile-time undefined
4562 behavior) to create an 'extern' declaration for a
4563 variable if there is a global declaration that is
4564 'static' and the global declaration is not visible.
4565 (If the static declaration _is_ currently visible,
4566 the 'extern' declaration is taken to refer to that decl.) */
4567 if (extern_ref && current_scope != file_scope)
4569 tree global_decl = identifier_global_value (declarator);
4570 tree visible_decl = lookup_name (declarator);
4572 if (global_decl
4573 && global_decl != visible_decl
4574 && TREE_CODE (global_decl) == VAR_DECL
4575 && !TREE_PUBLIC (global_decl))
4576 error ("variable previously declared 'static' redeclared "
4577 "'extern'");
4580 decl = build_decl (VAR_DECL, declarator, type);
4581 if (size_varies)
4582 C_DECL_VARIABLE_SIZE (decl) = 1;
4584 if (inlinep)
4585 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4587 DECL_EXTERNAL (decl) = extern_ref;
4589 /* At file scope, the presence of a `static' or `register' storage
4590 class specifier, or the absence of all storage class specifiers
4591 makes this declaration a definition (perhaps tentative). Also,
4592 the absence of both `static' and `register' makes it public. */
4593 if (current_scope == file_scope)
4595 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4596 | (1 << (int) RID_REGISTER)));
4597 TREE_STATIC (decl) = !extern_ref;
4599 /* Not at file scope, only `static' makes a static definition. */
4600 else
4602 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4603 TREE_PUBLIC (decl) = extern_ref;
4606 if (specbits & 1 << (int) RID_THREAD)
4608 if (targetm.have_tls)
4609 DECL_THREAD_LOCAL (decl) = 1;
4610 else
4611 /* A mere warning is sure to result in improper semantics
4612 at runtime. Don't bother to allow this to compile. */
4613 error ("thread-local storage not supported for this target");
4617 /* Record `register' declaration for warnings on &
4618 and in case doing stupid register allocation. */
4620 if (specbits & (1 << (int) RID_REGISTER))
4622 C_DECL_REGISTER (decl) = 1;
4623 DECL_REGISTER (decl) = 1;
4626 /* Record constancy and volatility. */
4627 c_apply_type_quals_to_decl (type_quals, decl);
4629 /* If a type has volatile components, it should be stored in memory.
4630 Otherwise, the fact that those components are volatile
4631 will be ignored, and would even crash the compiler. */
4632 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4634 /* It is not an error for a structure with volatile fields to
4635 be declared register, but reset DECL_REGISTER since it
4636 cannot actually go in a register. */
4637 int was_reg = C_DECL_REGISTER (decl);
4638 C_DECL_REGISTER (decl) = 0;
4639 DECL_REGISTER (decl) = 0;
4640 c_mark_addressable (decl);
4641 C_DECL_REGISTER (decl) = was_reg;
4644 #ifdef ENABLE_CHECKING
4645 /* This is the earliest point at which we might know the assembler
4646 name of a variable. Thus, if it's known before this, die horribly. */
4647 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4648 abort ();
4649 #endif
4651 decl_attributes (&decl, returned_attrs, 0);
4653 return decl;
4657 /* Decode the parameter-list info for a function type or function definition.
4658 The argument is the value returned by `get_parm_info' (or made in parse.y
4659 if there is an identifier list instead of a parameter decl list).
4660 These two functions are separate because when a function returns
4661 or receives functions then each is called multiple times but the order
4662 of calls is different. The last call to `grokparms' is always the one
4663 that contains the formal parameter names of a function definition.
4665 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4667 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4668 a mere declaration. A nonempty identifier-list gets an error message
4669 when FUNCDEF_FLAG is zero. */
4671 static tree
4672 grokparms (tree arg_info, int funcdef_flag)
4674 tree arg_types = ARG_INFO_TYPES (arg_info);
4676 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4677 && !in_system_header)
4678 warning ("function declaration isn't a prototype");
4680 if (arg_types == error_mark_node)
4681 return 0; /* don't set TYPE_ARG_TYPES in this case */
4683 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4685 if (! funcdef_flag)
4686 pedwarn ("parameter names (without types) in function declaration");
4688 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4689 ARG_INFO_TYPES (arg_info) = 0;
4690 return 0;
4692 else
4694 tree parm, type, typelt;
4695 unsigned int parmno;
4697 /* If the arg types are incomplete in a declaration, they must
4698 include undefined tags. These tags can never be defined in
4699 the scope of the declaration, so the types can never be
4700 completed, and no call can be compiled successfully. */
4702 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4703 parm;
4704 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4706 type = TREE_VALUE (typelt);
4707 if (type == error_mark_node)
4708 continue;
4710 if (!COMPLETE_TYPE_P (type))
4712 if (funcdef_flag)
4714 if (DECL_NAME (parm))
4715 error ("%Jparameter %u ('%D') has incomplete type",
4716 parm, parmno, parm);
4717 else
4718 error ("%Jparameter %u has incomplete type",
4719 parm, parmno);
4721 TREE_VALUE (typelt) = error_mark_node;
4722 TREE_TYPE (parm) = error_mark_node;
4724 else
4726 if (DECL_NAME (parm))
4727 warning ("%Jparameter %u ('%D') has incomplete type",
4728 parm, parmno, parm);
4729 else
4730 warning ("%Jparameter %u has incomplete type",
4731 parm, parmno);
4735 return arg_types;
4739 /* Take apart the current scope and return a tree_list node with info
4740 on a parameter list just parsed. This tree_list node should be
4741 examined using the ARG_INFO_* macros, defined above:
4743 ARG_INFO_PARMS: a list of parameter decls.
4744 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4745 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4746 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4747 constants) defined with the parameters.
4749 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4751 ELLIPSIS being true means the argument list ended in '...' so don't
4752 append a sentinel (void_list_node) to the end of the type-list. */
4754 tree
4755 get_parm_info (bool ellipsis)
4757 struct c_binding *b = current_scope->bindings;
4758 tree arg_info = make_node (TREE_LIST);
4759 tree parms = 0;
4760 tree tags = 0;
4761 tree types = 0;
4762 tree others = 0;
4764 static bool explained_incomplete_types = false;
4765 bool gave_void_only_once_err = false;
4767 /* The bindings in this scope must not get put into a block.
4768 We will take care of deleting the binding nodes. */
4769 current_scope->bindings = 0;
4771 /* This function is only called if there was *something* on the
4772 parameter list. */
4773 #ifdef ENABLE_CHECKING
4774 if (b == 0)
4775 abort ();
4776 #endif
4778 /* A parameter list consisting solely of 'void' indicates that the
4779 function takes no arguments. But if the 'void' is qualified
4780 (by 'const' or 'volatile'), or has a storage class specifier
4781 ('register'), then the behavior is undefined; issue an error.
4782 Typedefs for 'void' are OK (see DR#157). */
4783 if (b->prev == 0 /* one binding */
4784 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4785 && !DECL_NAME (b->decl) /* anonymous */
4786 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4788 if (TREE_THIS_VOLATILE (b->decl)
4789 || TREE_READONLY (b->decl)
4790 || C_DECL_REGISTER (b->decl))
4791 error ("'void' as only parameter may not be qualified");
4793 /* There cannot be an ellipsis. */
4794 if (ellipsis)
4795 error ("'void' must be the only parameter");
4797 ARG_INFO_TYPES (arg_info) = void_list_node;
4798 return arg_info;
4801 if (!ellipsis)
4802 types = void_list_node;
4804 /* Break up the bindings list into parms, tags, types, and others;
4805 apply sanity checks; purge the name-to-decl bindings. */
4806 while (b)
4808 tree decl = b->decl;
4809 tree type = TREE_TYPE (decl);
4810 const char *keyword;
4812 switch (TREE_CODE (decl))
4814 case PARM_DECL:
4815 if (b->id)
4817 #ifdef ENABLE_CHECKING
4818 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4819 #endif
4820 I_SYMBOL_BINDING (b->id) = b->shadowed;
4823 /* Check for forward decls that never got their actual decl. */
4824 if (TREE_ASM_WRITTEN (decl))
4825 error ("%Jparameter '%D' has just a forward declaration",
4826 decl, decl);
4827 /* Check for (..., void, ...) and issue an error. */
4828 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4830 if (!gave_void_only_once_err)
4832 error ("'void' must be the only parameter");
4833 gave_void_only_once_err = true;
4836 else
4838 /* Valid parameter, add it to the list. */
4839 TREE_CHAIN (decl) = parms;
4840 parms = decl;
4842 /* Since there is a prototype, args are passed in their
4843 declared types. The back end may override this later. */
4844 DECL_ARG_TYPE (decl) = type;
4845 types = tree_cons (0, type, types);
4847 break;
4849 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4850 case UNION_TYPE: keyword = "union"; goto tag;
4851 case RECORD_TYPE: keyword = "struct"; goto tag;
4852 tag:
4853 /* Types may not have tag-names, in which case the type
4854 appears in the bindings list with b->id NULL. */
4855 if (b->id)
4857 #ifdef ENABLE_CHECKING
4858 if (I_TAG_BINDING (b->id) != b) abort ();
4859 #endif
4860 I_TAG_BINDING (b->id) = b->shadowed;
4863 /* Warn about any struct, union or enum tags defined in a
4864 parameter list. The scope of such types is limited to
4865 the parameter list, which is rarely if ever desirable
4866 (it's impossible to call such a function with type-
4867 correct arguments). An anonymous union parm type is
4868 meaningful as a GNU extension, so don't warn for that. */
4869 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4871 if (b->id)
4872 /* The %s will be one of 'struct', 'union', or 'enum'. */
4873 warning ("'%s %E' declared inside parameter list",
4874 keyword, b->id);
4875 else
4876 /* The %s will be one of 'struct', 'union', or 'enum'. */
4877 warning ("anonymous %s declared inside parameter list",
4878 keyword);
4880 if (! explained_incomplete_types)
4882 warning ("its scope is only this definition or declaration,"
4883 " which is probably not what you want");
4884 explained_incomplete_types = true;
4888 tags = tree_cons (b->id, decl, tags);
4889 break;
4891 case CONST_DECL:
4892 case TYPE_DECL:
4893 /* CONST_DECLs appear here when we have an embedded enum,
4894 and TYPE_DECLs appear here when we have an embedded struct
4895 or union. No warnings for this - we already warned about the
4896 type itself. */
4897 TREE_CHAIN (decl) = others;
4898 others = decl;
4899 /* fall through */
4901 case ERROR_MARK:
4902 /* error_mark_node appears here when we have an undeclared
4903 variable. Just throw it away. */
4904 if (b->id)
4906 #ifdef ENABLE_CHECKING
4907 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4908 #endif
4909 I_SYMBOL_BINDING (b->id) = b->shadowed;
4911 break;
4913 /* Other things that might be encountered. */
4914 case LABEL_DECL:
4915 case FUNCTION_DECL:
4916 case VAR_DECL:
4917 default:
4918 abort ();
4921 b = free_binding_and_advance (b);
4924 ARG_INFO_PARMS (arg_info) = parms;
4925 ARG_INFO_TAGS (arg_info) = tags;
4926 ARG_INFO_TYPES (arg_info) = types;
4927 ARG_INFO_OTHERS (arg_info) = others;
4928 return arg_info;
4931 /* Get the struct, enum or union (CODE says which) with tag NAME.
4932 Define the tag as a forward-reference if it is not defined. */
4934 tree
4935 xref_tag (enum tree_code code, tree name)
4937 /* If a cross reference is requested, look up the type
4938 already defined for this tag and return it. */
4940 tree ref = lookup_tag (code, name, 0);
4941 /* If this is the right type of tag, return what we found.
4942 (This reference will be shadowed by shadow_tag later if appropriate.)
4943 If this is the wrong type of tag, do not return it. If it was the
4944 wrong type in the same scope, we will have had an error
4945 message already; if in a different scope and declaring
4946 a name, pending_xref_error will give an error message; but if in a
4947 different scope and not declaring a name, this tag should
4948 shadow the previous declaration of a different type of tag, and
4949 this would not work properly if we return the reference found.
4950 (For example, with "struct foo" in an outer scope, "union foo;"
4951 must shadow that tag with a new one of union type.) */
4952 if (ref && TREE_CODE (ref) == code)
4953 return ref;
4955 /* If no such tag is yet defined, create a forward-reference node
4956 and record it as the "definition".
4957 When a real declaration of this type is found,
4958 the forward-reference will be altered into a real type. */
4960 ref = make_node (code);
4961 if (code == ENUMERAL_TYPE)
4963 /* Give the type a default layout like unsigned int
4964 to avoid crashing if it does not get defined. */
4965 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4966 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4967 TYPE_USER_ALIGN (ref) = 0;
4968 TYPE_UNSIGNED (ref) = 1;
4969 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4970 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4971 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4974 pushtag (name, ref);
4976 return ref;
4979 /* Make sure that the tag NAME is defined *in the current scope*
4980 at least as a forward reference.
4981 CODE says which kind of tag NAME ought to be. */
4983 tree
4984 start_struct (enum tree_code code, tree name)
4986 /* If there is already a tag defined at this scope
4987 (as a forward reference), just return it. */
4989 tree ref = 0;
4991 if (name != 0)
4992 ref = lookup_tag (code, name, 1);
4993 if (ref && TREE_CODE (ref) == code)
4995 if (TYPE_FIELDS (ref))
4997 if (code == UNION_TYPE)
4998 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4999 else
5000 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5003 else
5005 /* Otherwise create a forward-reference just so the tag is in scope. */
5007 ref = make_node (code);
5008 pushtag (name, ref);
5011 C_TYPE_BEING_DEFINED (ref) = 1;
5012 TYPE_PACKED (ref) = flag_pack_struct;
5013 return ref;
5016 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5017 of a structure component, returning a FIELD_DECL node.
5018 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5020 This is done during the parsing of the struct declaration.
5021 The FIELD_DECL nodes are chained together and the lot of them
5022 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5024 tree
5025 grokfield (tree declarator, tree declspecs, tree width)
5027 tree value;
5029 if (declarator == NULL_TREE && width == NULL_TREE)
5031 /* This is an unnamed decl.
5033 If we have something of the form "union { list } ;" then this
5034 is the anonymous union extension. Similarly for struct.
5036 If this is something of the form "struct foo;", then
5037 If MS extensions are enabled, this is handled as an
5038 anonymous struct.
5039 Otherwise this is a forward declaration of a structure tag.
5041 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5042 If MS extensions are enabled and foo names a structure, then
5043 again this is an anonymous struct.
5044 Otherwise this is an error.
5046 Oh what a horrid tangled web we weave. I wonder if MS consciously
5047 took this from Plan 9 or if it was an accident of implementation
5048 that took root before someone noticed the bug... */
5050 tree type = TREE_VALUE (declspecs);
5052 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5053 type = TREE_TYPE (type);
5054 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5056 if (flag_ms_extensions)
5057 ; /* ok */
5058 else if (flag_iso)
5059 goto warn_unnamed_field;
5060 else if (TYPE_NAME (type) == NULL)
5061 ; /* ok */
5062 else
5063 goto warn_unnamed_field;
5065 else
5067 warn_unnamed_field:
5068 warning ("declaration does not declare anything");
5069 return NULL_TREE;
5073 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5074 width ? &width : NULL);
5076 finish_decl (value, NULL_TREE, NULL_TREE);
5077 DECL_INITIAL (value) = width;
5079 return value;
5082 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5083 the list such that this does not present a problem later. */
5085 static void
5086 detect_field_duplicates (tree fieldlist)
5088 tree x, y;
5089 int timeout = 10;
5091 /* First, see if there are more than "a few" fields.
5092 This is trivially true if there are zero or one fields. */
5093 if (!fieldlist)
5094 return;
5095 x = TREE_CHAIN (fieldlist);
5096 if (!x)
5097 return;
5098 do {
5099 timeout--;
5100 x = TREE_CHAIN (x);
5101 } while (timeout > 0 && x);
5103 /* If there were "few" fields, avoid the overhead of allocating
5104 a hash table. Instead just do the nested traversal thing. */
5105 if (timeout > 0)
5107 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5108 if (DECL_NAME (x))
5110 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5111 if (DECL_NAME (y) == DECL_NAME (x))
5113 error ("%Jduplicate member '%D'", x, x);
5114 DECL_NAME (x) = NULL_TREE;
5118 else
5120 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5121 void **slot;
5123 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5124 if ((y = DECL_NAME (x)) != 0)
5126 slot = htab_find_slot (htab, y, INSERT);
5127 if (*slot)
5129 error ("%Jduplicate member '%D'", x, x);
5130 DECL_NAME (x) = NULL_TREE;
5132 *slot = y;
5135 htab_delete (htab);
5139 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5140 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5141 ATTRIBUTES are attributes to be applied to the structure. */
5143 tree
5144 finish_struct (tree t, tree fieldlist, tree attributes)
5146 tree x;
5147 bool toplevel = file_scope == current_scope;
5148 int saw_named_field;
5150 /* If this type was previously laid out as a forward reference,
5151 make sure we lay it out again. */
5153 TYPE_SIZE (t) = 0;
5155 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5157 if (pedantic)
5159 for (x = fieldlist; x; x = TREE_CHAIN (x))
5160 if (DECL_NAME (x) != 0)
5161 break;
5163 if (x == 0)
5164 pedwarn ("%s has no %s",
5165 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5166 fieldlist ? _("named members") : _("members"));
5169 /* Install struct as DECL_CONTEXT of each field decl.
5170 Also process specified field sizes, found in the DECL_INITIAL,
5171 storing 0 there after the type has been changed to precision equal
5172 to its width, rather than the precision of the specified standard
5173 type. (Correct layout requires the original type to have been preserved
5174 until now.) */
5176 saw_named_field = 0;
5177 for (x = fieldlist; x; x = TREE_CHAIN (x))
5179 DECL_CONTEXT (x) = t;
5180 DECL_PACKED (x) |= TYPE_PACKED (t);
5182 /* If any field is const, the structure type is pseudo-const. */
5183 if (TREE_READONLY (x))
5184 C_TYPE_FIELDS_READONLY (t) = 1;
5185 else
5187 /* A field that is pseudo-const makes the structure likewise. */
5188 tree t1 = TREE_TYPE (x);
5189 while (TREE_CODE (t1) == ARRAY_TYPE)
5190 t1 = TREE_TYPE (t1);
5191 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5192 && C_TYPE_FIELDS_READONLY (t1))
5193 C_TYPE_FIELDS_READONLY (t) = 1;
5196 /* Any field that is volatile means variables of this type must be
5197 treated in some ways as volatile. */
5198 if (TREE_THIS_VOLATILE (x))
5199 C_TYPE_FIELDS_VOLATILE (t) = 1;
5201 /* Any field of nominal variable size implies structure is too. */
5202 if (C_DECL_VARIABLE_SIZE (x))
5203 C_TYPE_VARIABLE_SIZE (t) = 1;
5205 /* Detect invalid nested redefinition. */
5206 if (TREE_TYPE (x) == t)
5207 error ("nested redefinition of `%s'",
5208 IDENTIFIER_POINTER (TYPE_NAME (t)));
5210 if (DECL_INITIAL (x))
5212 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5213 DECL_SIZE (x) = bitsize_int (width);
5214 DECL_BIT_FIELD (x) = 1;
5215 SET_DECL_C_BIT_FIELD (x);
5218 /* Detect flexible array member in an invalid context. */
5219 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5220 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5221 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5222 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5224 if (TREE_CODE (t) == UNION_TYPE)
5226 error ("%Jflexible array member in union", x);
5227 TREE_TYPE (x) = error_mark_node;
5229 else if (TREE_CHAIN (x) != NULL_TREE)
5231 error ("%Jflexible array member not at end of struct", x);
5232 TREE_TYPE (x) = error_mark_node;
5234 else if (! saw_named_field)
5236 error ("%Jflexible array member in otherwise empty struct", x);
5237 TREE_TYPE (x) = error_mark_node;
5241 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5242 && flexible_array_type_p (TREE_TYPE (x)))
5243 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5245 if (DECL_NAME (x))
5246 saw_named_field = 1;
5249 detect_field_duplicates (fieldlist);
5251 /* Now we have the nearly final fieldlist. Record it,
5252 then lay out the structure or union (including the fields). */
5254 TYPE_FIELDS (t) = fieldlist;
5256 layout_type (t);
5258 /* Give bit-fields their proper types. */
5260 tree *fieldlistp = &fieldlist;
5261 while (*fieldlistp)
5262 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5263 && TREE_TYPE (*fieldlistp) != error_mark_node)
5265 unsigned HOST_WIDE_INT width
5266 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5267 tree type = TREE_TYPE (*fieldlistp);
5268 if (width != TYPE_PRECISION (type))
5269 TREE_TYPE (*fieldlistp)
5270 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5271 DECL_INITIAL (*fieldlistp) = 0;
5273 else
5274 fieldlistp = &TREE_CHAIN (*fieldlistp);
5277 /* Now we have the truly final field list.
5278 Store it in this type and in the variants. */
5280 TYPE_FIELDS (t) = fieldlist;
5282 /* If there are lots of fields, sort so we can look through them fast.
5283 We arbitrarily consider 16 or more elts to be "a lot". */
5286 int len = 0;
5288 for (x = fieldlist; x; x = TREE_CHAIN (x))
5290 if (len > 15 || DECL_NAME (x) == NULL)
5291 break;
5292 len += 1;
5295 if (len > 15)
5297 tree *field_array;
5298 struct lang_type *space;
5299 struct sorted_fields_type *space2;
5301 len += list_length (x);
5303 /* Use the same allocation policy here that make_node uses, to
5304 ensure that this lives as long as the rest of the struct decl.
5305 All decls in an inline function need to be saved. */
5307 space = ggc_alloc_cleared (sizeof (struct lang_type));
5308 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5310 len = 0;
5311 space->s = space2;
5312 field_array = &space2->elts[0];
5313 for (x = fieldlist; x; x = TREE_CHAIN (x))
5315 field_array[len++] = x;
5317 /* If there is anonymous struct or union, break out of the loop. */
5318 if (DECL_NAME (x) == NULL)
5319 break;
5321 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5322 if (x == NULL)
5324 TYPE_LANG_SPECIFIC (t) = space;
5325 TYPE_LANG_SPECIFIC (t)->s->len = len;
5326 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5327 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5332 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5334 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5335 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5336 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5337 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5340 /* If this was supposed to be a transparent union, but we can't
5341 make it one, warn and turn off the flag. */
5342 if (TREE_CODE (t) == UNION_TYPE
5343 && TYPE_TRANSPARENT_UNION (t)
5344 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5346 TYPE_TRANSPARENT_UNION (t) = 0;
5347 warning ("union cannot be made transparent");
5350 /* If this structure or union completes the type of any previous
5351 variable declaration, lay it out and output its rtl. */
5352 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5354 x = TREE_CHAIN (x))
5356 tree decl = TREE_VALUE (x);
5357 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5358 layout_array_type (TREE_TYPE (decl));
5359 if (TREE_CODE (decl) != TYPE_DECL)
5361 layout_decl (decl, 0);
5362 if (c_dialect_objc ())
5363 objc_check_decl (decl);
5364 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5365 if (! toplevel)
5366 expand_decl (decl);
5369 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5371 /* Finish debugging output for this type. */
5372 rest_of_type_compilation (t, toplevel);
5374 return t;
5377 /* Lay out the type T, and its element type, and so on. */
5379 static void
5380 layout_array_type (tree t)
5382 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5383 layout_array_type (TREE_TYPE (t));
5384 layout_type (t);
5387 /* Begin compiling the definition of an enumeration type.
5388 NAME is its name (or null if anonymous).
5389 Returns the type object, as yet incomplete.
5390 Also records info about it so that build_enumerator
5391 may be used to declare the individual values as they are read. */
5393 tree
5394 start_enum (tree name)
5396 tree enumtype = 0;
5398 /* If this is the real definition for a previous forward reference,
5399 fill in the contents in the same object that used to be the
5400 forward reference. */
5402 if (name != 0)
5403 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5405 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5407 enumtype = make_node (ENUMERAL_TYPE);
5408 pushtag (name, enumtype);
5411 C_TYPE_BEING_DEFINED (enumtype) = 1;
5413 if (TYPE_VALUES (enumtype) != 0)
5415 /* This enum is a named one that has been declared already. */
5416 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5418 /* Completely replace its old definition.
5419 The old enumerators remain defined, however. */
5420 TYPE_VALUES (enumtype) = 0;
5423 enum_next_value = integer_zero_node;
5424 enum_overflow = 0;
5426 if (flag_short_enums)
5427 TYPE_PACKED (enumtype) = 1;
5429 return enumtype;
5432 /* After processing and defining all the values of an enumeration type,
5433 install their decls in the enumeration type and finish it off.
5434 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5435 and ATTRIBUTES are the specified attributes.
5436 Returns ENUMTYPE. */
5438 tree
5439 finish_enum (tree enumtype, tree values, tree attributes)
5441 tree pair, tem;
5442 tree minnode = 0, maxnode = 0;
5443 int precision, unsign;
5444 bool toplevel = (file_scope == current_scope);
5445 struct lang_type *lt;
5447 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5449 /* Calculate the maximum value of any enumerator in this type. */
5451 if (values == error_mark_node)
5452 minnode = maxnode = integer_zero_node;
5453 else
5455 minnode = maxnode = TREE_VALUE (values);
5456 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5458 tree value = TREE_VALUE (pair);
5459 if (tree_int_cst_lt (maxnode, value))
5460 maxnode = value;
5461 if (tree_int_cst_lt (value, minnode))
5462 minnode = value;
5466 /* Construct the final type of this enumeration. It is the same
5467 as one of the integral types - the narrowest one that fits, except
5468 that normally we only go as narrow as int - and signed iff any of
5469 the values are negative. */
5470 unsign = (tree_int_cst_sgn (minnode) >= 0);
5471 precision = MAX (min_precision (minnode, unsign),
5472 min_precision (maxnode, unsign));
5473 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5475 tem = c_common_type_for_size (precision, unsign);
5476 if (tem == NULL)
5478 warning ("enumeration values exceed range of largest integer");
5479 tem = long_long_integer_type_node;
5482 else
5483 tem = unsign ? unsigned_type_node : integer_type_node;
5485 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5486 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5487 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5488 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5489 TYPE_SIZE (enumtype) = 0;
5490 layout_type (enumtype);
5492 if (values != error_mark_node)
5494 /* Change the type of the enumerators to be the enum type. We
5495 need to do this irrespective of the size of the enum, for
5496 proper type checking. Replace the DECL_INITIALs of the
5497 enumerators, and the value slots of the list, with copies
5498 that have the enum type; they cannot be modified in place
5499 because they may be shared (e.g. integer_zero_node) Finally,
5500 change the purpose slots to point to the names of the decls. */
5501 for (pair = values; pair; pair = TREE_CHAIN (pair))
5503 tree enu = TREE_PURPOSE (pair);
5504 tree ini = DECL_INITIAL (enu);
5506 TREE_TYPE (enu) = enumtype;
5508 /* The ISO C Standard mandates enumerators to have type int,
5509 even though the underlying type of an enum type is
5510 unspecified. Here we convert any enumerators that fit in
5511 an int to type int, to avoid promotions to unsigned types
5512 when comparing integers with enumerators that fit in the
5513 int range. When -pedantic is given, build_enumerator()
5514 would have already taken care of those that don't fit. */
5515 if (int_fits_type_p (ini, integer_type_node))
5516 tem = integer_type_node;
5517 else
5518 tem = enumtype;
5519 ini = convert (tem, ini);
5521 DECL_INITIAL (enu) = ini;
5522 TREE_PURPOSE (pair) = DECL_NAME (enu);
5523 TREE_VALUE (pair) = ini;
5526 TYPE_VALUES (enumtype) = values;
5529 /* Record the min/max values so that we can warn about bit-field
5530 enumerations that are too small for the values. */
5531 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5532 lt->enum_min = minnode;
5533 lt->enum_max = maxnode;
5534 TYPE_LANG_SPECIFIC (enumtype) = lt;
5536 /* Fix up all variant types of this enum type. */
5537 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5539 if (tem == enumtype)
5540 continue;
5541 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5542 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5543 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5544 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5545 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5546 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5547 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5548 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5549 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5550 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5551 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5554 /* Finish debugging output for this type. */
5555 rest_of_type_compilation (enumtype, toplevel);
5557 return enumtype;
5560 /* Build and install a CONST_DECL for one value of the
5561 current enumeration type (one that was begun with start_enum).
5562 Return a tree-list containing the CONST_DECL and its value.
5563 Assignment of sequential values by default is handled here. */
5565 tree
5566 build_enumerator (tree name, tree value)
5568 tree decl, type;
5570 /* Validate and default VALUE. */
5572 /* Remove no-op casts from the value. */
5573 if (value)
5574 STRIP_TYPE_NOPS (value);
5576 if (value != 0)
5578 /* Don't issue more errors for error_mark_node (i.e. an
5579 undeclared identifier) - just ignore the value expression. */
5580 if (value == error_mark_node)
5581 value = 0;
5582 else if (TREE_CODE (value) != INTEGER_CST)
5584 error ("enumerator value for '%E' is not an integer constant", name);
5585 value = 0;
5587 else
5589 value = default_conversion (value);
5590 constant_expression_warning (value);
5594 /* Default based on previous value. */
5595 /* It should no longer be possible to have NON_LVALUE_EXPR
5596 in the default. */
5597 if (value == 0)
5599 value = enum_next_value;
5600 if (enum_overflow)
5601 error ("overflow in enumeration values");
5604 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5606 pedwarn ("ISO C restricts enumerator values to range of `int'");
5607 /* XXX This causes -pedantic to change the meaning of the program.
5608 Remove? -zw 2004-03-15 */
5609 value = convert (integer_type_node, value);
5612 /* Set basis for default for next value. */
5613 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5614 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5616 /* Now create a declaration for the enum value name. */
5618 type = TREE_TYPE (value);
5619 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5620 TYPE_PRECISION (integer_type_node)),
5621 (TYPE_PRECISION (type)
5622 >= TYPE_PRECISION (integer_type_node)
5623 && TYPE_UNSIGNED (type)));
5625 decl = build_decl (CONST_DECL, name, type);
5626 DECL_INITIAL (decl) = convert (type, value);
5627 pushdecl (decl);
5629 return tree_cons (decl, value, NULL_TREE);
5633 /* Create the FUNCTION_DECL for a function definition.
5634 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5635 the declaration; they describe the function's name and the type it returns,
5636 but twisted together in a fashion that parallels the syntax of C.
5638 This function creates a binding context for the function body
5639 as well as setting up the FUNCTION_DECL in current_function_decl.
5641 Returns 1 on success. If the DECLARATOR is not suitable for a function
5642 (it defines a datum instead), we return 0, which tells
5643 yyparse to report a parse error. */
5646 start_function (tree declspecs, tree declarator, tree attributes)
5648 tree decl1, old_decl;
5649 tree restype;
5651 current_function_returns_value = 0; /* Assume, until we see it does. */
5652 current_function_returns_null = 0;
5653 current_function_returns_abnormally = 0;
5654 warn_about_return_type = 0;
5655 current_extern_inline = 0;
5656 c_switch_stack = NULL;
5658 /* Indicate no valid break/continue context by setting these variables
5659 to some non-null, non-label value. We'll notice and emit the proper
5660 error message in c_finish_bc_stmt. */
5661 c_break_label = c_cont_label = size_zero_node;
5663 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5665 /* If the declarator is not suitable for a function definition,
5666 cause a syntax error. */
5667 if (decl1 == 0)
5668 return 0;
5670 decl_attributes (&decl1, attributes, 0);
5672 if (DECL_DECLARED_INLINE_P (decl1)
5673 && DECL_UNINLINABLE (decl1)
5674 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5675 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5677 announce_function (decl1);
5679 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5681 error ("return type is an incomplete type");
5682 /* Make it return void instead. */
5683 TREE_TYPE (decl1)
5684 = build_function_type (void_type_node,
5685 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5688 if (warn_about_return_type)
5689 pedwarn_c99 ("return type defaults to `int'");
5691 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5692 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5693 DECL_INITIAL (decl1) = error_mark_node;
5695 /* If this definition isn't a prototype and we had a prototype declaration
5696 before, copy the arg type info from that prototype.
5697 But not if what we had before was a builtin function. */
5698 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5699 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5700 && !DECL_BUILT_IN (old_decl)
5701 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5702 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5703 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5705 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5706 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5709 /* Optionally warn of old-fashioned def with no previous prototype. */
5710 if (warn_strict_prototypes
5711 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5712 && C_DECL_ISNT_PROTOTYPE (old_decl))
5713 warning ("function declaration isn't a prototype");
5714 /* Optionally warn of any global def with no previous prototype. */
5715 else if (warn_missing_prototypes
5716 && TREE_PUBLIC (decl1)
5717 && ! MAIN_NAME_P (DECL_NAME (decl1))
5718 && C_DECL_ISNT_PROTOTYPE (old_decl))
5719 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5720 /* Optionally warn of any def with no previous prototype
5721 if the function has already been used. */
5722 else if (warn_missing_prototypes
5723 && old_decl != 0 && TREE_USED (old_decl)
5724 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5725 warning ("%J'%D' was used with no prototype before its definition",
5726 decl1, decl1);
5727 /* Optionally warn of any global def with no previous declaration. */
5728 else if (warn_missing_declarations
5729 && TREE_PUBLIC (decl1)
5730 && old_decl == 0
5731 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5732 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5733 /* Optionally warn of any def with no previous declaration
5734 if the function has already been used. */
5735 else if (warn_missing_declarations
5736 && old_decl != 0 && TREE_USED (old_decl)
5737 && C_DECL_IMPLICIT (old_decl))
5738 warning ("%J`%D' was used with no declaration before its definition",
5739 decl1, decl1);
5741 /* This is a definition, not a reference.
5742 So normally clear DECL_EXTERNAL.
5743 However, `extern inline' acts like a declaration
5744 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5745 DECL_EXTERNAL (decl1) = current_extern_inline;
5747 /* This function exists in static storage.
5748 (This does not mean `static' in the C sense!) */
5749 TREE_STATIC (decl1) = 1;
5751 /* A nested function is not global. */
5752 if (current_function_decl != 0)
5753 TREE_PUBLIC (decl1) = 0;
5755 #ifdef ENABLE_CHECKING
5756 /* This is the earliest point at which we might know the assembler
5757 name of the function. Thus, if it's set before this, die horribly. */
5758 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5759 abort ();
5760 #endif
5762 /* If #pragma weak was used, mark the decl weak now. */
5763 if (current_scope == file_scope)
5764 maybe_apply_pragma_weak (decl1);
5766 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5767 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5769 tree args;
5770 int argct = 0;
5772 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5773 != integer_type_node)
5774 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5776 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5777 args = TREE_CHAIN (args))
5779 tree type = args ? TREE_VALUE (args) : 0;
5781 if (type == void_type_node)
5782 break;
5784 ++argct;
5785 switch (argct)
5787 case 1:
5788 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5789 pedwarn ("%Jfirst argument of '%D' should be `int'",
5790 decl1, decl1);
5791 break;
5793 case 2:
5794 if (TREE_CODE (type) != POINTER_TYPE
5795 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5796 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5797 != char_type_node))
5798 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5799 decl1, decl1);
5800 break;
5802 case 3:
5803 if (TREE_CODE (type) != POINTER_TYPE
5804 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5805 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5806 != char_type_node))
5807 pedwarn ("%Jthird argument of '%D' should probably be "
5808 "'char **'", decl1, decl1);
5809 break;
5813 /* It is intentional that this message does not mention the third
5814 argument because it's only mentioned in an appendix of the
5815 standard. */
5816 if (argct > 0 && (argct < 2 || argct > 3))
5817 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5819 if (! TREE_PUBLIC (decl1))
5820 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5823 /* Record the decl so that the function name is defined.
5824 If we already have a decl for this name, and it is a FUNCTION_DECL,
5825 use the old decl. */
5827 current_function_decl = pushdecl (decl1);
5829 push_scope ();
5830 declare_parm_level ();
5832 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5833 /* Promote the value to int before returning it. */
5834 if (c_promoting_integer_type_p (restype))
5836 /* It retains unsignedness if not really getting wider. */
5837 if (TYPE_UNSIGNED (restype)
5838 && (TYPE_PRECISION (restype)
5839 == TYPE_PRECISION (integer_type_node)))
5840 restype = unsigned_type_node;
5841 else
5842 restype = integer_type_node;
5844 DECL_RESULT (current_function_decl)
5845 = build_decl (RESULT_DECL, NULL_TREE, restype);
5847 start_fname_decls ();
5849 return 1;
5852 /* Subroutine of store_parm_decls which handles new-style function
5853 definitions (prototype format). The parms already have decls, so we
5854 need only record them as in effect and complain if any redundant
5855 old-style parm decls were written. */
5856 static void
5857 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5859 tree decl;
5860 tree parms = ARG_INFO_PARMS (arg_info);
5861 tree tags = ARG_INFO_TAGS (arg_info);
5862 tree others = ARG_INFO_OTHERS (arg_info);
5864 if (current_scope->bindings)
5866 error ("%Jold-style parameter declarations in prototyped "
5867 "function definition", fndecl);
5869 /* Get rid of the old-style declarations. */
5870 pop_scope ();
5871 push_scope ();
5873 /* Don't issue this warning for nested functions, and don't issue this
5874 warning if we got here because ARG_INFO_TYPES was error_mark_node
5875 (this happens when a function definition has just an ellipsis in
5876 its parameter list). */
5877 else if (warn_traditional && !in_system_header && !current_function_scope
5878 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5879 warning ("%Jtraditional C rejects ISO C style function definitions",
5880 fndecl);
5882 /* Now make all the parameter declarations visible in the function body.
5883 We can bypass most of the grunt work of pushdecl. */
5884 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5886 DECL_CONTEXT (decl) = current_function_decl;
5887 if (DECL_NAME (decl))
5888 bind (DECL_NAME (decl), decl, current_scope,
5889 /*invisible=*/false, /*nested=*/false);
5890 else
5891 error ("%Jparameter name omitted", decl);
5894 /* Record the parameter list in the function declaration. */
5895 DECL_ARGUMENTS (fndecl) = parms;
5897 /* Now make all the ancillary declarations visible, likewise. */
5898 for (decl = others; decl; decl = TREE_CHAIN (decl))
5900 DECL_CONTEXT (decl) = current_function_decl;
5901 if (DECL_NAME (decl))
5902 bind (DECL_NAME (decl), decl, current_scope,
5903 /*invisible=*/false, /*nested=*/false);
5906 /* And all the tag declarations. */
5907 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5908 if (TREE_PURPOSE (decl))
5909 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5910 /*invisible=*/false, /*nested=*/false);
5913 /* Subroutine of store_parm_decls which handles old-style function
5914 definitions (separate parameter list and declarations). */
5916 static void
5917 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5919 struct c_binding *b;
5920 tree parm, decl, last;
5921 tree parmids = ARG_INFO_PARMS (arg_info);
5923 /* We use DECL_WEAK as a flag to show which parameters have been
5924 seen already, since it is not used on PARM_DECL. */
5925 #ifdef ENABLE_CHECKING
5926 for (b = current_scope->bindings; b; b = b->prev)
5927 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5928 abort ();
5929 #endif
5931 if (warn_old_style_definition && !in_system_header)
5932 warning ("%Jold-style function definition", fndecl);
5934 /* Match each formal parameter name with its declaration. Save each
5935 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5936 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5938 if (TREE_VALUE (parm) == 0)
5940 error ("%Jparameter name missing from parameter list", fndecl);
5941 TREE_PURPOSE (parm) = 0;
5942 continue;
5945 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5946 if (b && B_IN_CURRENT_SCOPE (b))
5948 decl = b->decl;
5949 /* If we got something other than a PARM_DECL it is an error. */
5950 if (TREE_CODE (decl) != PARM_DECL)
5951 error ("%J'%D' declared as a non-parameter", decl, decl);
5952 /* If the declaration is already marked, we have a duplicate
5953 name. Complain and ignore the duplicate. */
5954 else if (DECL_WEAK (decl))
5956 error ("%Jmultiple parameters named '%D'", decl, decl);
5957 TREE_PURPOSE (parm) = 0;
5958 continue;
5960 /* If the declaration says "void", complain and turn it into
5961 an int. */
5962 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5964 error ("%Jparameter '%D' declared with void type", decl, decl);
5965 TREE_TYPE (decl) = integer_type_node;
5966 DECL_ARG_TYPE (decl) = integer_type_node;
5967 layout_decl (decl, 0);
5970 /* If no declaration found, default to int. */
5971 else
5973 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5974 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5975 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5976 pushdecl (decl);
5978 if (flag_isoc99)
5979 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5980 else if (extra_warnings)
5981 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5984 TREE_PURPOSE (parm) = decl;
5985 DECL_WEAK (decl) = 1;
5988 /* Now examine the parms chain for incomplete declarations
5989 and declarations with no corresponding names. */
5991 for (b = current_scope->bindings; b; b = b->prev)
5993 parm = b->decl;
5994 if (TREE_CODE (parm) != PARM_DECL)
5995 continue;
5997 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5999 error ("%Jparameter '%D' has incomplete type", parm, parm);
6000 TREE_TYPE (parm) = error_mark_node;
6003 if (! DECL_WEAK (parm))
6005 error ("%Jdeclaration for parameter '%D' but no such parameter",
6006 parm, parm);
6008 /* Pretend the parameter was not missing.
6009 This gets us to a standard state and minimizes
6010 further error messages. */
6011 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6015 /* Chain the declarations together in the order of the list of
6016 names. Store that chain in the function decl, replacing the
6017 list of names. Update the current scope to match. */
6018 DECL_ARGUMENTS (fndecl) = 0;
6020 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6021 if (TREE_PURPOSE (parm))
6022 break;
6023 if (parm && TREE_PURPOSE (parm))
6025 last = TREE_PURPOSE (parm);
6026 DECL_ARGUMENTS (fndecl) = last;
6027 DECL_WEAK (last) = 0;
6029 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6030 if (TREE_PURPOSE (parm))
6032 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6033 last = TREE_PURPOSE (parm);
6034 DECL_WEAK (last) = 0;
6036 TREE_CHAIN (last) = 0;
6039 /* If there was a previous prototype,
6040 set the DECL_ARG_TYPE of each argument according to
6041 the type previously specified, and report any mismatches. */
6043 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6045 tree type;
6046 for (parm = DECL_ARGUMENTS (fndecl),
6047 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6048 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6049 != void_type_node));
6050 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6052 if (parm == 0 || type == 0
6053 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6055 error ("number of arguments doesn't match prototype");
6056 error ("%Hprototype declaration",
6057 &current_function_prototype_locus);
6058 break;
6060 /* Type for passing arg must be consistent with that
6061 declared for the arg. ISO C says we take the unqualified
6062 type for parameters declared with qualified type. */
6063 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6064 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6066 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6067 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6069 /* Adjust argument to match prototype. E.g. a previous
6070 `int foo(float);' prototype causes
6071 `int foo(x) float x; {...}' to be treated like
6072 `int foo(float x) {...}'. This is particularly
6073 useful for argument types like uid_t. */
6074 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6076 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6077 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6078 && TYPE_PRECISION (TREE_TYPE (parm))
6079 < TYPE_PRECISION (integer_type_node))
6080 DECL_ARG_TYPE (parm) = integer_type_node;
6082 if (pedantic)
6084 pedwarn ("promoted argument '%D' "
6085 "doesn't match prototype", parm);
6086 pedwarn ("%Hprototype declaration",
6087 &current_function_prototype_locus);
6090 else
6092 error ("argument '%D' doesn't match prototype", parm);
6093 error ("%Hprototype declaration",
6094 &current_function_prototype_locus);
6098 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6101 /* Otherwise, create a prototype that would match. */
6103 else
6105 tree actual = 0, last = 0, type;
6107 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6109 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6110 if (last)
6111 TREE_CHAIN (last) = type;
6112 else
6113 actual = type;
6114 last = type;
6116 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6117 if (last)
6118 TREE_CHAIN (last) = type;
6119 else
6120 actual = type;
6122 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6123 of the type of this function, but we need to avoid having this
6124 affect the types of other similarly-typed functions, so we must
6125 first force the generation of an identical (but separate) type
6126 node for the relevant function type. The new node we create
6127 will be a variant of the main variant of the original function
6128 type. */
6130 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6132 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6136 /* Store the parameter declarations into the current function declaration.
6137 This is called after parsing the parameter declarations, before
6138 digesting the body of the function.
6140 For an old-style definition, construct a prototype out of the old-style
6141 parameter declarations and inject it into the function's type. */
6143 void
6144 store_parm_decls (void)
6146 tree fndecl = current_function_decl;
6148 /* The argument information block for FNDECL. */
6149 tree arg_info = DECL_ARGUMENTS (fndecl);
6151 /* True if this definition is written with a prototype. Note:
6152 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6153 list in a function definition as equivalent to (void) -- an
6154 empty argument list specifies the function has no parameters,
6155 but only (void) sets up a prototype for future calls. */
6156 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6158 if (proto)
6159 store_parm_decls_newstyle (fndecl, arg_info);
6160 else
6161 store_parm_decls_oldstyle (fndecl, arg_info);
6163 /* The next call to push_scope will be a function body. */
6165 next_is_function_body = true;
6167 /* Write a record describing this function definition to the prototypes
6168 file (if requested). */
6170 gen_aux_info_record (fndecl, 1, 0, proto);
6172 /* Initialize the RTL code for the function. */
6173 allocate_struct_function (fndecl);
6175 /* Begin the statement tree for this function. */
6176 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6178 /* ??? Insert the contents of the pending sizes list into the function
6179 to be evaluated. This just changes mis-behaviour until assign_parms
6180 phase ordering problems are resolved. */
6182 tree t;
6183 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6184 add_stmt (TREE_VALUE (t));
6187 /* Even though we're inside a function body, we still don't want to
6188 call expand_expr to calculate the size of a variable-sized array.
6189 We haven't necessarily assigned RTL to all variables yet, so it's
6190 not safe to try to expand expressions involving them. */
6191 cfun->x_dont_save_pending_sizes_p = 1;
6194 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6196 static void
6197 c_finalize (tree fndecl)
6199 struct cgraph_node *cgn;
6201 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6202 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6204 /* ??? Objc emits functions after finalizing the compilation unit.
6205 This should be cleaned up later and this conditional removed. */
6206 if (cgraph_global_info_ready)
6208 c_expand_body (fndecl);
6209 return;
6212 /* Finalize all nested functions now. */
6213 cgn = cgraph_node (fndecl);
6214 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6215 c_finalize (cgn->decl);
6217 cgraph_finalize_function (fndecl, false);
6220 /* Finish up a function declaration and compile that function
6221 all the way to assembler language output. The free the storage
6222 for the function definition.
6224 This is called after parsing the body of the function definition. */
6226 void
6227 finish_function (void)
6229 tree fndecl = current_function_decl;
6231 if (TREE_CODE (fndecl) == FUNCTION_DECL
6232 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6234 tree args = DECL_ARGUMENTS (fndecl);
6235 for (; args; args = TREE_CHAIN (args))
6237 tree type = TREE_TYPE (args);
6238 if (INTEGRAL_TYPE_P (type)
6239 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6240 DECL_ARG_TYPE (args) = integer_type_node;
6244 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6245 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6247 /* Must mark the RESULT_DECL as being in this function. */
6249 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6250 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6252 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6254 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6255 != integer_type_node)
6257 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6258 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6259 if (!warn_main)
6260 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6262 else
6264 if (flag_isoc99)
6265 c_finish_return (integer_zero_node);
6269 /* Tie off the statement tree for this function. */
6270 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6272 finish_fname_decls ();
6274 /* Complain if there's just no return statement. */
6275 if (warn_return_type
6276 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6277 && !current_function_returns_value && !current_function_returns_null
6278 /* Don't complain if we abort. */
6279 && !current_function_returns_abnormally
6280 /* Don't warn for main(). */
6281 && !MAIN_NAME_P (DECL_NAME (fndecl))
6282 /* Or if they didn't actually specify a return type. */
6283 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6284 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6285 inline function, as we might never be compiled separately. */
6286 && DECL_INLINE (fndecl))
6287 warning ("no return statement in function returning non-void");
6289 /* With just -Wextra, complain only if function returns both with
6290 and without a value. */
6291 if (extra_warnings
6292 && current_function_returns_value
6293 && current_function_returns_null)
6294 warning ("this function may return with or without a value");
6296 /* Store the end of the function, so that we get good line number
6297 info for the epilogue. */
6298 cfun->function_end_locus = input_location;
6300 /* If we don't have ctors/dtors sections, and this is a static
6301 constructor or destructor, it must be recorded now. */
6302 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6303 && !targetm.have_ctors_dtors)
6304 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6305 if (DECL_STATIC_DESTRUCTOR (fndecl)
6306 && !targetm.have_ctors_dtors)
6307 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6309 /* Genericize before inlining. Delay genericizing nested functions
6310 until their parent function is genericized. Since finalizing
6311 requires GENERIC, delay that as well. */
6313 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6315 if (!decl_function_context (fndecl))
6317 c_genericize (fndecl);
6318 lower_nested_functions (fndecl);
6319 c_finalize (fndecl);
6321 else
6323 /* Register this function with cgraph just far enough to get it
6324 added to our parent's nested function list. Handy, since the
6325 C front end doesn't have such a list. */
6326 (void) cgraph_node (fndecl);
6330 /* We're leaving the context of this function, so zap cfun.
6331 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6332 tree_rest_of_compilation. */
6333 cfun = NULL;
6334 current_function_decl = NULL;
6337 /* Generate the RTL for the body of FNDECL. */
6339 void
6340 c_expand_body (tree fndecl)
6343 if (!DECL_INITIAL (fndecl)
6344 || DECL_INITIAL (fndecl) == error_mark_node)
6345 return;
6347 tree_rest_of_compilation (fndecl, false);
6349 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6350 && targetm.have_ctors_dtors)
6351 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6352 DEFAULT_INIT_PRIORITY);
6353 if (DECL_STATIC_DESTRUCTOR (fndecl)
6354 && targetm.have_ctors_dtors)
6355 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6356 DEFAULT_INIT_PRIORITY);
6359 /* Check the declarations given in a for-loop for satisfying the C99
6360 constraints. */
6361 void
6362 check_for_loop_decls (void)
6364 struct c_binding *b;
6366 if (!flag_isoc99)
6368 /* If we get here, declarations have been used in a for loop without
6369 the C99 for loop scope. This doesn't make much sense, so don't
6370 allow it. */
6371 error ("'for' loop initial declaration used outside C99 mode");
6372 return;
6374 /* C99 subclause 6.8.5 paragraph 3:
6376 [#3] The declaration part of a for statement shall only
6377 declare identifiers for objects having storage class auto or
6378 register.
6380 It isn't clear whether, in this sentence, "identifiers" binds to
6381 "shall only declare" or to "objects" - that is, whether all identifiers
6382 declared must be identifiers for objects, or whether the restriction
6383 only applies to those that are. (A question on this in comp.std.c
6384 in November 2000 received no answer.) We implement the strictest
6385 interpretation, to avoid creating an extension which later causes
6386 problems. */
6388 for (b = current_scope->bindings; b; b = b->prev)
6390 tree id = b->id;
6391 tree decl = b->decl;
6393 if (!id)
6394 continue;
6396 switch (TREE_CODE (decl))
6398 case VAR_DECL:
6399 if (TREE_STATIC (decl))
6400 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6401 "initial declaration", decl, decl);
6402 else if (DECL_EXTERNAL (decl))
6403 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6404 "initial declaration", decl, decl);
6405 break;
6407 case RECORD_TYPE:
6408 error ("'struct %E' declared in 'for' loop initial declaration", id);
6409 break;
6410 case UNION_TYPE:
6411 error ("'union %E' declared in 'for' loop initial declaration", id);
6412 break;
6413 case ENUMERAL_TYPE:
6414 error ("'enum %E' declared in 'for' loop initial declaration", id);
6415 break;
6416 default:
6417 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6418 "initial declaration", decl, decl);
6423 /* Save and reinitialize the variables
6424 used during compilation of a C function. */
6426 void
6427 c_push_function_context (struct function *f)
6429 struct language_function *p;
6430 p = ggc_alloc (sizeof (struct language_function));
6431 f->language = p;
6433 p->base.x_stmt_tree = c_stmt_tree;
6434 p->x_break_label = c_break_label;
6435 p->x_cont_label = c_cont_label;
6436 p->x_switch_stack = c_switch_stack;
6437 p->returns_value = current_function_returns_value;
6438 p->returns_null = current_function_returns_null;
6439 p->returns_abnormally = current_function_returns_abnormally;
6440 p->warn_about_return_type = warn_about_return_type;
6441 p->extern_inline = current_extern_inline;
6444 /* Restore the variables used during compilation of a C function. */
6446 void
6447 c_pop_function_context (struct function *f)
6449 struct language_function *p = f->language;
6451 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6452 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6454 /* Stop pointing to the local nodes about to be freed. */
6455 /* But DECL_INITIAL must remain nonzero so we know this
6456 was an actual function definition. */
6457 DECL_INITIAL (current_function_decl) = error_mark_node;
6458 DECL_ARGUMENTS (current_function_decl) = 0;
6461 c_stmt_tree = p->base.x_stmt_tree;
6462 c_break_label = p->x_break_label;
6463 c_cont_label = p->x_cont_label;
6464 c_switch_stack = p->x_switch_stack;
6465 current_function_returns_value = p->returns_value;
6466 current_function_returns_null = p->returns_null;
6467 current_function_returns_abnormally = p->returns_abnormally;
6468 warn_about_return_type = p->warn_about_return_type;
6469 current_extern_inline = p->extern_inline;
6471 f->language = NULL;
6474 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6476 void
6477 c_dup_lang_specific_decl (tree decl)
6479 struct lang_decl *ld;
6481 if (!DECL_LANG_SPECIFIC (decl))
6482 return;
6484 ld = ggc_alloc (sizeof (struct lang_decl));
6485 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6486 DECL_LANG_SPECIFIC (decl) = ld;
6489 /* The functions below are required for functionality of doing
6490 function at once processing in the C front end. Currently these
6491 functions are not called from anywhere in the C front end, but as
6492 these changes continue, that will change. */
6494 /* Returns nonzero if the current statement is a full expression,
6495 i.e. temporaries created during that statement should be destroyed
6496 at the end of the statement. */
6499 stmts_are_full_exprs_p (void)
6501 return 0;
6504 /* Returns the stmt_tree (if any) to which statements are currently
6505 being added. If there is no active statement-tree, NULL is
6506 returned. */
6508 stmt_tree
6509 current_stmt_tree (void)
6511 return &c_stmt_tree;
6514 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6515 C. */
6518 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6520 return 0;
6523 /* Dummy function in place of callback used by C++. */
6525 void
6526 extract_interface_info (void)
6530 /* Return the global value of T as a symbol. */
6532 tree
6533 identifier_global_value (tree t)
6535 struct c_binding *b;
6537 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6538 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6539 return b->decl;
6541 return 0;
6544 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6545 otherwise the name is found in ridpointers from RID_INDEX. */
6547 void
6548 record_builtin_type (enum rid rid_index, const char *name, tree type)
6550 tree id;
6551 if (name == 0)
6552 id = ridpointers[(int) rid_index];
6553 else
6554 id = get_identifier (name);
6555 pushdecl (build_decl (TYPE_DECL, id, type));
6558 /* Build the void_list_node (void_type_node having been created). */
6559 tree
6560 build_void_list_node (void)
6562 tree t = build_tree_list (NULL_TREE, void_type_node);
6563 return t;
6566 /* Return something to represent absolute declarators containing a *.
6567 TARGET is the absolute declarator that the * contains.
6568 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6569 to apply to the pointer type, represented as identifiers, possible mixed
6570 with attributes.
6572 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6573 if attributes are present) and whose type is the modifier list. */
6575 tree
6576 make_pointer_declarator (tree type_quals_attrs, tree target)
6578 tree quals, attrs;
6579 tree itarget = target;
6580 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6581 if (attrs != NULL_TREE)
6582 itarget = tree_cons (attrs, target, NULL_TREE);
6583 return build1 (INDIRECT_REF, quals, itarget);
6586 /* Synthesize a function which calls all the global ctors or global
6587 dtors in this file. This is only used for targets which do not
6588 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6589 static void
6590 build_cdtor (int method_type, tree cdtors)
6592 tree body = 0;
6594 if (!cdtors)
6595 return;
6597 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6598 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6599 &body);
6601 cgraph_build_static_cdtor (method_type, body);
6604 /* Perform final processing on one file scope's declarations (or the
6605 external scope's declarations), GLOBALS. */
6606 static void
6607 c_write_global_declarations_1 (tree globals)
6609 size_t len = list_length (globals);
6610 tree *vec = xmalloc (sizeof (tree) * len);
6611 size_t i;
6612 tree decl;
6614 /* Process the decls in the order they were written. */
6615 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6616 vec[i] = decl;
6618 wrapup_global_declarations (vec, len);
6619 check_global_declarations (vec, len);
6621 free (vec);
6624 void
6625 c_write_global_declarations (void)
6627 tree ext_block, t;
6629 /* We don't want to do this if generating a PCH. */
6630 if (pch_file)
6631 return;
6633 /* Close the external scope. */
6634 ext_block = pop_scope ();
6635 external_scope = 0;
6636 if (current_scope)
6637 abort ();
6639 /* Process all file scopes in this compilation, and the external_scope,
6640 through wrapup_global_declarations and check_global_declarations. */
6641 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6642 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6643 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6645 /* Generate functions to call static constructors and destructors
6646 for targets that do not support .ctors/.dtors sections. These
6647 functions have magic names which are detected by collect2. */
6648 build_cdtor ('I', static_ctors); static_ctors = 0;
6649 build_cdtor ('D', static_dtors); static_dtors = 0;
6651 /* We're done parsing; proceed to optimize and emit assembly.
6652 FIXME: shouldn't be the front end's responsibility to call this. */
6653 cgraph_optimize ();
6655 /* Presently this has to happen after cgraph_optimize.
6656 FIXME: shouldn't be the front end's responsibility to call this. */
6657 if (flag_mudflap)
6658 mudflap_finish_file ();
6661 #include "gt-c-decl.h"