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
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
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
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. */
31 #include "coretypes.h"
35 #include "tree-inline.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
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
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. */
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
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 */
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. */
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)")))
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
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
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
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. */
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
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); \
350 if (s_->list##_last) \
351 TREE_CHAIN (s_->list##_last) = d_; \
354 s_->list##_last = d_; \
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); \
362 TREE_CHAIN (t_->to##_last) = f_->from; \
365 t_->to##_last = f_->from##_last; \
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
395 enum deprecated_states {
400 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
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. */
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;
431 b = ggc_alloc (sizeof (struct c_binding));
436 b->depth = scope->depth;
437 b->invisible = invisible;
440 b->prev = scope->bindings;
446 switch (TREE_CODE (decl))
448 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
451 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
457 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
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;
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;
490 /* Hook called at end of compilation to assume 1 elt
491 for a file-scope tentative array defn that wasn't complete before. */
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. */
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. */
525 objc_mark_locals_volatile (void *enclosing_blk)
527 struct c_scope *scope;
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)
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;
560 keep_next_level (void)
562 keep_next_level_flag = true;
565 /* Identify this scope as currently being filled with parameters. */
568 declare_parm_level (void)
570 current_scope->parm_flag = true;
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;
599 struct c_scope *scope;
602 scope = scope_freelist;
603 scope_freelist = scope->outer;
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
614 if (current_scope && scope->depth == 0)
617 sorry ("GCC supports only %u nested scopes\n", scope->depth);
620 current_scope = scope;
621 keep_next_level_flag = false;
625 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
628 set_type_context (tree type, tree context)
630 for (type = TYPE_MAIN_VARIANT (type); type;
631 type = TYPE_NEXT_VARIANT (type))
632 TYPE_CONTEXT (type) = context;
635 /* Exit a scope. Restore the state of the identifier-decl mappings
636 that were in effect when this scope was entered. Return a BLOCK
637 node containing all the DECLs in this scope that are of interest
638 to debug info generation. */
643 struct c_scope *scope = current_scope;
644 tree block, context, p;
647 bool functionbody = scope->function_body;
648 bool keep = functionbody || scope->keep || scope->bindings;
650 /* If appropriate, create a BLOCK to record the decls for the life
655 block = make_node (BLOCK);
656 BLOCK_SUBBLOCKS (block) = scope->blocks;
657 TREE_USED (block) = 1;
659 /* In each subblock, record that this is its superior. */
660 for (p = scope->blocks; p; p = TREE_CHAIN (p))
661 BLOCK_SUPERCONTEXT (p) = block;
663 BLOCK_VARS (block) = 0;
666 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
667 scope must be set so that they point to the appropriate
668 construct, i.e. either to the current FUNCTION_DECL node, or
669 else to the BLOCK node we just constructed.
671 Note that for tagged types whose scope is just the formal
672 parameter list for some function type specification, we can't
673 properly set their TYPE_CONTEXTs here, because we don't have a
674 pointer to the appropriate FUNCTION_TYPE node readily available
675 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
676 type nodes get set in `grokdeclarator' as soon as we have created
677 the FUNCTION_TYPE node which will represent the "scope" for these
678 "parameter list local" tagged types. */
679 if (scope->function_body)
680 context = current_function_decl;
681 else if (scope == file_scope)
683 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
684 TREE_CHAIN (file_decl) = all_translation_units;
685 all_translation_units = file_decl;
691 /* Clear all bindings in this scope. */
692 for (b = scope->bindings; b; b = free_binding_and_advance (b))
695 switch (TREE_CODE (p))
698 /* Warnings for unused labels, errors for undefined labels. */
699 if (TREE_USED (p) && !DECL_INITIAL (p))
701 error ("%Jlabel `%D' used but not defined", p, p);
702 DECL_INITIAL (p) = error_mark_node;
704 else if (!TREE_USED (p) && warn_unused_label)
706 if (DECL_INITIAL (p))
707 warning ("%Jlabel `%D' defined but not used", p, p);
709 warning ("%Jlabel `%D' declared but not defined", p, p);
711 /* Labels go in BLOCK_VARS. */
712 TREE_CHAIN (p) = BLOCK_VARS (block);
713 BLOCK_VARS (block) = p;
715 #ifdef ENABLE_CHECKING
716 if (I_LABEL_BINDING (b->id) != b) abort ();
718 I_LABEL_BINDING (b->id) = b->shadowed;
724 set_type_context (p, context);
726 /* Types may not have tag-names, in which case the type
727 appears in the bindings list with b->id NULL. */
730 #ifdef ENABLE_CHECKING
731 if (I_TAG_BINDING (b->id) != b) abort ();
733 I_TAG_BINDING (b->id) = b->shadowed;
738 /* Propagate TREE_ADDRESSABLE from nested functions to their
739 containing functions. */
740 if (! TREE_ASM_WRITTEN (p)
741 && DECL_INITIAL (p) != 0
742 && TREE_ADDRESSABLE (p)
743 && DECL_ABSTRACT_ORIGIN (p) != 0
744 && DECL_ABSTRACT_ORIGIN (p) != p)
745 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
749 /* Warnings for unused variables. */
750 if (warn_unused_variable
752 && !DECL_IN_SYSTEM_HEADER (p)
754 && !DECL_ARTIFICIAL (p)
755 && (scope != file_scope
756 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
757 && !TREE_THIS_VOLATILE (p)))
758 && scope != external_scope)
759 warning ("%Junused variable `%D'", p, p);
765 /* All of these go in BLOCK_VARS, but only if this is the
766 binding in the home scope. */
769 TREE_CHAIN (p) = BLOCK_VARS (block);
770 BLOCK_VARS (block) = p;
772 /* If this is the file scope, and we are processing more
773 than one translation unit in this compilation, set
774 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
775 This makes same_translation_unit_p work, and causes
776 static declarations to be given disambiguating suffixes. */
777 if (scope == file_scope && num_in_fnames > 1)
779 DECL_CONTEXT (p) = context;
780 if (TREE_CODE (p) == TYPE_DECL)
781 set_type_context (TREE_TYPE (p), context);
785 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
786 already been put there by store_parm_decls. Unused-
787 parameter warnings are handled by function.c.
788 error_mark_node obviously does not go in BLOCK_VARS and
789 does not get unused-variable warnings. */
792 /* It is possible for a decl not to have a name. We get
793 here with b->id NULL in this case. */
796 #ifdef ENABLE_CHECKING
797 if (I_SYMBOL_BINDING (b->id) != b) abort ();
799 I_SYMBOL_BINDING (b->id) = b->shadowed;
809 /* Dispose of the block that we just made inside some higher level. */
810 if ((scope->function_body || scope == file_scope) && context)
812 DECL_INITIAL (context) = block;
813 BLOCK_SUPERCONTEXT (block) = context;
815 else if (scope->outer)
818 SCOPE_LIST_APPEND (scope->outer, blocks, block);
819 /* If we did not make a block for the scope just exited, any
820 blocks made for inner scopes must be carried forward so they
821 will later become subblocks of something else. */
822 else if (scope->blocks)
823 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
826 /* Pop the current scope, and free the structure for reuse. */
827 current_scope = scope->outer;
828 if (scope->function_body)
829 current_function_scope = scope->outer_function;
831 memset (scope, 0, sizeof (struct c_scope));
832 scope->outer = scope_freelist;
833 scope_freelist = scope;
839 push_file_scope (void)
847 file_scope = current_scope;
849 start_fname_decls ();
851 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
852 bind (DECL_NAME (decl), decl, file_scope,
853 /*invisible=*/false, /*nested=*/true);
857 pop_file_scope (void)
859 /* In case there were missing closebraces, get us back to the global
861 while (current_scope != file_scope)
864 /* __FUNCTION__ is defined at file scope (""). This
865 call may not be necessary as my tests indicate it
866 still works without it. */
867 finish_fname_decls ();
869 /* This is the point to write out a PCH if we're doing that.
870 In that case we do not want to do anything else. */
873 c_common_write_pch ();
877 /* Pop off the file scope and close this translation unit. */
880 cgraph_finalize_compilation_unit ();
883 /* Insert BLOCK at the end of the list of subblocks of the current
884 scope. This is used when a BIND_EXPR is expanded, to handle the
885 BLOCK node inside the BIND_EXPR. */
888 insert_block (tree block)
890 TREE_USED (block) = 1;
891 SCOPE_LIST_APPEND (current_scope, blocks, block);
894 /* Push a definition or a declaration of struct, union or enum tag "name".
895 "type" should be the type node.
896 We assume that the tag "name" is not already defined.
898 Note that the definition may really be just a forward reference.
899 In that case, the TYPE_SIZE will be zero. */
902 pushtag (tree name, tree type)
904 /* Record the identifier as the type's name if it has none. */
905 if (name && !TYPE_NAME (type))
906 TYPE_NAME (type) = name;
907 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
909 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
910 tagged type we just added to the current scope. This fake
911 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
912 to output a representation of a tagged type, and it also gives
913 us a convenient place to record the "scope start" address for the
916 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
918 /* An approximation for now, so we can tell this is a function-scope tag.
919 This will be updated in pop_scope. */
920 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
923 /* Subroutine of compare_decls. Allow harmless mismatches in return
924 and argument types provided that the type modes match. This function
925 return a unified type given a suitable match, and 0 otherwise. */
928 match_builtin_function_types (tree newtype, tree oldtype)
930 tree newrettype, oldrettype;
931 tree newargs, oldargs;
932 tree trytype, tryargs;
934 /* Accept the return type of the new declaration if same modes. */
935 oldrettype = TREE_TYPE (oldtype);
936 newrettype = TREE_TYPE (newtype);
938 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
941 oldargs = TYPE_ARG_TYPES (oldtype);
942 newargs = TYPE_ARG_TYPES (newtype);
945 while (oldargs || newargs)
949 || ! TREE_VALUE (oldargs)
950 || ! TREE_VALUE (newargs)
951 || TYPE_MODE (TREE_VALUE (oldargs))
952 != TYPE_MODE (TREE_VALUE (newargs)))
955 oldargs = TREE_CHAIN (oldargs);
956 newargs = TREE_CHAIN (newargs);
959 trytype = build_function_type (newrettype, tryargs);
960 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
963 /* Subroutine of diagnose_mismatched_decls. Check for function type
964 mismatch involving an empty arglist vs a nonempty one and give clearer
967 diagnose_arglist_conflict (tree newdecl, tree olddecl,
968 tree newtype, tree oldtype)
972 if (TREE_CODE (olddecl) != FUNCTION_DECL
973 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
974 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
976 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
979 t = TYPE_ARG_TYPES (oldtype);
981 t = TYPE_ARG_TYPES (newtype);
982 for (; t; t = TREE_CHAIN (t))
984 tree type = TREE_VALUE (t);
986 if (TREE_CHAIN (t) == 0
987 && TYPE_MAIN_VARIANT (type) != void_type_node)
989 inform ("a parameter list with an ellipsis can't match "
990 "an empty parameter name list declaration");
994 if (c_type_promotes_to (type) != type)
996 inform ("an argument type that has a default promotion can't match "
997 "an empty parameter name list declaration");
1003 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1004 old-style function definition, NEWDECL is a prototype declaration.
1005 Diagnose inconsistencies in the argument list. Returns TRUE if
1006 the prototype is compatible, FALSE if not. */
1008 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1010 tree newargs, oldargs;
1013 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1014 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1016 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1017 newargs = TYPE_ARG_TYPES (newtype);
1022 tree oldargtype = TREE_VALUE (oldargs);
1023 tree newargtype = TREE_VALUE (newargs);
1025 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1028 /* Reaching the end of just one list means the two decls don't
1029 agree on the number of arguments. */
1030 if (END_OF_ARGLIST (oldargtype))
1032 error ("%Jprototype for '%D' declares more arguments "
1033 "than previous old-style definition", newdecl, newdecl);
1036 else if (END_OF_ARGLIST (newargtype))
1038 error ("%Jprototype for '%D' declares fewer arguments "
1039 "than previous old-style definition", newdecl, newdecl);
1043 /* Type for passing arg must be consistent with that declared
1045 else if (! comptypes (oldargtype, newargtype))
1047 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1048 newdecl, newdecl, i);
1052 oldargs = TREE_CHAIN (oldargs);
1053 newargs = TREE_CHAIN (newargs);
1057 /* If we get here, no errors were found, but do issue a warning
1058 for this poor-style construct. */
1059 warning ("%Jprototype for '%D' follows non-prototype definition",
1062 #undef END_OF_ARGLIST
1065 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1066 first in a pair of mismatched declarations, using the diagnostic
1069 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1071 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1073 else if (DECL_INITIAL (decl))
1074 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1075 else if (C_DECL_IMPLICIT (decl))
1076 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1078 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1081 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1082 Returns true if the caller should proceed to merge the two, false
1083 if OLDDECL should simply be discarded. As a side effect, issues
1084 all necessary diagnostics for invalid or poor-style combinations.
1085 If it returns true, writes the types of NEWDECL and OLDDECL to
1086 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1087 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1090 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1091 tree *newtypep, tree *oldtypep)
1093 tree newtype, oldtype;
1094 bool pedwarned = false;
1095 bool warned = false;
1097 /* If we have error_mark_node for either decl or type, just discard
1098 the previous decl - we're in an error cascade already. */
1099 if (olddecl == error_mark_node || newdecl == error_mark_node)
1101 *oldtypep = oldtype = TREE_TYPE (olddecl);
1102 *newtypep = newtype = TREE_TYPE (newdecl);
1103 if (oldtype == error_mark_node || newtype == error_mark_node)
1106 /* Two different categories of symbol altogether. This is an error
1107 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1108 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1110 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1111 && DECL_BUILT_IN (olddecl)
1112 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1114 error ("%J'%D' redeclared as different kind of symbol",
1116 locate_old_decl (olddecl, error);
1118 else if (TREE_PUBLIC (newdecl))
1119 warning ("%Jbuilt-in function '%D' declared as non-function",
1121 else if (warn_shadow)
1122 warning ("%Jdeclaration of '%D' shadows a built-in function",
1127 if (!comptypes (oldtype, newtype))
1129 if (TREE_CODE (olddecl) == FUNCTION_DECL
1130 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1132 /* Accept harmless mismatch in function types.
1133 This is for the ffs and fprintf builtins. */
1134 tree trytype = match_builtin_function_types (newtype, oldtype);
1136 if (trytype && comptypes (newtype, trytype))
1137 *oldtypep = oldtype = trytype;
1140 /* If types don't match for a built-in, throw away the
1141 built-in. No point in calling locate_old_decl here, it
1142 won't print anything. */
1143 warning ("%Jconflicting types for built-in function '%D'",
1148 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1149 && DECL_IS_BUILTIN (olddecl))
1151 /* A conflicting function declaration for a predeclared
1152 function that isn't actually built in. Objective C uses
1153 these. The new declaration silently overrides everything
1154 but the volatility (i.e. noreturn) indication. See also
1155 below. FIXME: Make Objective C use normal builtins. */
1156 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1159 /* Permit void foo (...) to match int foo (...) if the latter is
1160 the definition and implicit int was used. See
1161 c-torture/compile/920625-2.c. */
1162 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1163 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1164 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1165 && C_FUNCTION_IMPLICIT_INT (newdecl))
1167 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1168 /* Make sure we keep void as the return type. */
1169 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1170 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1175 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1176 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1178 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1179 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1180 locate_old_decl (olddecl, error);
1185 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1186 but silently ignore the redeclaration if either is in a system
1187 header. (Conflicting redeclarations were handled above.) */
1188 if (TREE_CODE (newdecl) == TYPE_DECL)
1190 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1191 return true; /* Allow OLDDECL to continue in use. */
1193 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1194 locate_old_decl (olddecl, error);
1198 /* Function declarations can either be 'static' or 'extern' (no
1199 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1200 can never conflict with each other on account of linkage (6.2.2p4).
1201 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1202 two definitions if one is 'extern inline' and one is not. The non-
1203 extern-inline definition supersedes the extern-inline definition. */
1204 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1206 /* If you declare a built-in function name as static, or
1207 define the built-in with an old-style definition (so we
1208 can't validate the argument list) the built-in definition is
1209 overridden, but optionally warn this was a bad choice of name. */
1210 if (DECL_BUILT_IN (olddecl)
1211 && !C_DECL_DECLARED_BUILTIN (olddecl)
1212 && (!TREE_PUBLIC (newdecl)
1213 || (DECL_INITIAL (newdecl)
1214 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1217 warning ("%Jdeclaration of '%D' shadows a built-in function",
1219 /* Discard the old built-in function. */
1223 if (DECL_INITIAL (newdecl))
1225 if (DECL_INITIAL (olddecl)
1226 && !(DECL_DECLARED_INLINE_P (olddecl)
1227 && DECL_EXTERNAL (olddecl)
1228 && !(DECL_DECLARED_INLINE_P (newdecl)
1229 && DECL_EXTERNAL (newdecl)
1230 && same_translation_unit_p (olddecl, newdecl))))
1232 error ("%Jredefinition of '%D'", newdecl, newdecl);
1233 locate_old_decl (olddecl, error);
1237 /* If we have a prototype after an old-style function definition,
1238 the argument types must be checked specially. */
1239 else if (DECL_INITIAL (olddecl)
1240 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1241 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1242 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1244 locate_old_decl (olddecl, error);
1247 /* A non-static declaration (even an "extern") followed by a
1248 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1249 The same is true for a static forward declaration at block
1250 scope followed by a non-static declaration/definition at file
1251 scope. Static followed by non-static at the same scope is
1252 not undefined behavior, and is the most convenient way to get
1253 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1254 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1255 we do diagnose it if -Wtraditional. */
1256 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1258 /* Two exceptions to the rule. If olddecl is an extern
1259 inline, or a predeclared function that isn't actually
1260 built in, newdecl silently overrides olddecl. The latter
1261 occur only in Objective C; see also above. (FIXME: Make
1262 Objective C use normal builtins.) */
1263 if (!DECL_IS_BUILTIN (olddecl)
1264 && !(DECL_EXTERNAL (olddecl)
1265 && DECL_DECLARED_INLINE_P (olddecl)))
1267 error ("%Jstatic declaration of '%D' follows "
1268 "non-static declaration", newdecl, newdecl);
1269 locate_old_decl (olddecl, error);
1273 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1275 if (DECL_CONTEXT (olddecl))
1277 error ("%Jnon-static declaration of '%D' follows "
1278 "static declaration", newdecl, newdecl);
1279 locate_old_decl (olddecl, error);
1282 else if (warn_traditional)
1284 warning ("%Jnon-static declaration of '%D' follows "
1285 "static declaration", newdecl, newdecl);
1290 else if (TREE_CODE (newdecl) == VAR_DECL)
1292 /* Only variables can be thread-local, and all declarations must
1293 agree on this property. */
1294 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1296 if (DECL_THREAD_LOCAL (newdecl))
1297 error ("%Jthread-local declaration of '%D' follows "
1298 "non-thread-local declaration", newdecl, newdecl);
1300 error ("%Jnon-thread-local declaration of '%D' follows "
1301 "thread-local declaration", newdecl, newdecl);
1303 locate_old_decl (olddecl, error);
1307 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1308 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1310 error ("%Jredefinition of '%D'", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1315 /* Objects declared at file scope: if the first declaration had
1316 external linkage (even if it was an external reference) the
1317 second must have external linkage as well, or the behavior is
1318 undefined. If the first declaration had internal linkage, then
1319 the second must too, or else be an external reference (in which
1320 case the composite declaration still has internal linkage).
1321 As for function declarations, we warn about the static-then-
1322 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1323 if (DECL_FILE_SCOPE_P (newdecl)
1324 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1326 if (DECL_EXTERNAL (newdecl))
1328 if (warn_traditional)
1330 warning ("%Jnon-static declaration of '%D' follows "
1331 "static declaration", newdecl, newdecl);
1337 if (TREE_PUBLIC (newdecl))
1338 error ("%Jnon-static declaration of '%D' follows "
1339 "static declaration", newdecl, newdecl);
1341 error ("%Jstatic declaration of '%D' follows "
1342 "non-static declaration", newdecl, newdecl);
1344 locate_old_decl (olddecl, error);
1348 /* Two objects with the same name declared at the same block
1349 scope must both be external references (6.7p3). */
1350 else if (!DECL_FILE_SCOPE_P (newdecl)
1351 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1352 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1354 if (DECL_EXTERNAL (newdecl))
1355 error ("%Jextern declaration of '%D' follows "
1356 "declaration with no linkage", newdecl, newdecl);
1357 else if (DECL_EXTERNAL (olddecl))
1358 error ("%Jdeclaration of '%D' with no linkage follows "
1359 "extern declaration", newdecl, newdecl);
1361 error ("%Jredeclaration of '%D' with no linkage",
1364 locate_old_decl (olddecl, error);
1370 /* All decls must agree on a non-default visibility. */
1371 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1372 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1373 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1375 warning ("%Jredeclaration of '%D' with different visibility "
1376 "(old visibility preserved)", newdecl, newdecl);
1380 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1382 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1383 if (DECL_DECLARED_INLINE_P (newdecl)
1384 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1386 warning ("%Jinline declaration of '%D' follows "
1387 "declaration with attribute noinline", newdecl, newdecl);
1390 else if (DECL_DECLARED_INLINE_P (olddecl)
1391 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1393 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1394 "inline declaration ", newdecl, newdecl);
1398 /* Inline declaration after use or definition.
1399 ??? Should we still warn about this now we have unit-at-a-time
1400 mode and can get it right?
1401 Definitely don't complain if the decls are in different translation
1403 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1404 && same_translation_unit_p (olddecl, newdecl))
1406 if (TREE_USED (olddecl))
1408 warning ("%J'%D' declared inline after being called",
1412 else if (DECL_INITIAL (olddecl))
1414 warning ("%J'%D' declared inline after its definition",
1420 else /* PARM_DECL, VAR_DECL */
1422 /* Redeclaration of a parameter is a constraint violation (this is
1423 not explicitly stated, but follows from C99 6.7p3 [no more than
1424 one declaration of the same identifier with no linkage in the
1425 same scope, except type tags] and 6.2.2p6 [parameters have no
1426 linkage]). We must check for a forward parameter declaration,
1427 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1428 an extension, the mandatory diagnostic for which is handled by
1429 mark_forward_parm_decls. */
1431 if (TREE_CODE (newdecl) == PARM_DECL
1432 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1434 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1435 locate_old_decl (olddecl, error);
1440 /* Optional warning for completely redundant decls. */
1441 if (!warned && !pedwarned
1442 && warn_redundant_decls
1443 /* Don't warn about a function declaration followed by a
1445 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1446 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1447 /* Don't warn about an extern followed by a definition. */
1448 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1449 /* Don't warn about forward parameter decls. */
1450 && !(TREE_CODE (newdecl) == PARM_DECL
1451 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1453 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1457 /* Report location of previous decl/defn in a consistent manner. */
1458 if (warned || pedwarned)
1459 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1464 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1465 consistent with OLDDECL, but carries new information. Merge the
1466 new information into OLDDECL. This function issues no
1470 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1472 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1473 && DECL_INITIAL (newdecl) != 0);
1475 /* For real parm decl following a forward decl, rechain the old decl
1476 in its new location and clear TREE_ASM_WRITTEN (it's not a
1477 forward decl anymore). */
1478 if (TREE_CODE (newdecl) == PARM_DECL
1479 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1481 struct c_binding *b, **here;
1483 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1484 if ((*here)->decl == olddecl)
1491 b->prev = current_scope->bindings;
1492 current_scope->bindings = b;
1494 TREE_ASM_WRITTEN (olddecl) = 0;
1497 DECL_ATTRIBUTES (newdecl)
1498 = targetm.merge_decl_attributes (olddecl, newdecl);
1500 /* Merge the data types specified in the two decls. */
1502 = TREE_TYPE (olddecl)
1503 = composite_type (newtype, oldtype);
1505 /* Lay the type out, unless already done. */
1506 if (oldtype != TREE_TYPE (newdecl))
1508 if (TREE_TYPE (newdecl) != error_mark_node)
1509 layout_type (TREE_TYPE (newdecl));
1510 if (TREE_CODE (newdecl) != FUNCTION_DECL
1511 && TREE_CODE (newdecl) != TYPE_DECL
1512 && TREE_CODE (newdecl) != CONST_DECL)
1513 layout_decl (newdecl, 0);
1517 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1518 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1519 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1520 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1521 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1522 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1524 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1525 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1529 /* Keep the old rtl since we can safely use it. */
1530 COPY_DECL_RTL (olddecl, newdecl);
1532 /* Merge the type qualifiers. */
1533 if (TREE_READONLY (newdecl))
1534 TREE_READONLY (olddecl) = 1;
1536 if (TREE_THIS_VOLATILE (newdecl))
1538 TREE_THIS_VOLATILE (olddecl) = 1;
1539 if (TREE_CODE (newdecl) == VAR_DECL)
1540 make_var_volatile (newdecl);
1543 /* Keep source location of definition rather than declaration. */
1544 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1545 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1547 /* Merge the unused-warning information. */
1548 if (DECL_IN_SYSTEM_HEADER (olddecl))
1549 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1550 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1551 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1553 /* Merge the initialization information. */
1554 if (DECL_INITIAL (newdecl) == 0)
1555 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1557 /* Merge the section attribute.
1558 We want to issue an error if the sections conflict but that must be
1559 done later in decl_attributes since we are called before attributes
1561 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1562 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1564 /* Copy the assembler name.
1565 Currently, it can only be defined in the prototype. */
1566 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1568 /* If either declaration has a nondefault visibility, use it. */
1569 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1570 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1572 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1574 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1575 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1576 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1577 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1578 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1579 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1580 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1581 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1582 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1585 /* Merge the storage class information. */
1586 merge_weak (newdecl, olddecl);
1588 /* For functions, static overrides non-static. */
1589 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1591 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1592 /* This is since we don't automatically
1593 copy the attributes of NEWDECL into OLDDECL. */
1594 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1595 /* If this clears `static', clear it in the identifier too. */
1596 if (! TREE_PUBLIC (olddecl))
1597 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1599 if (DECL_EXTERNAL (newdecl))
1601 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1602 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1604 /* An extern decl does not override previous storage class. */
1605 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1606 if (! DECL_EXTERNAL (newdecl))
1608 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1609 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1614 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1615 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1618 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1620 /* If we're redefining a function previously defined as extern
1621 inline, make sure we emit debug info for the inline before we
1622 throw it away, in case it was inlined into a function that hasn't
1623 been written out yet. */
1624 if (new_is_definition && DECL_INITIAL (olddecl))
1626 if (TREE_USED (olddecl)
1627 /* In unit-at-a-time mode we never inline re-defined extern
1628 inline functions. */
1629 && !flag_unit_at_a_time
1630 && cgraph_function_possibly_inlined_p (olddecl))
1631 (*debug_hooks->outlining_inline_function) (olddecl);
1633 /* The new defn must not be inline. */
1634 DECL_INLINE (newdecl) = 0;
1635 DECL_UNINLINABLE (newdecl) = 1;
1639 /* If either decl says `inline', this fn is inline,
1640 unless its definition was passed already. */
1641 if (DECL_DECLARED_INLINE_P (newdecl)
1642 || DECL_DECLARED_INLINE_P (olddecl))
1643 DECL_DECLARED_INLINE_P (newdecl) = 1;
1645 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1646 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1649 if (DECL_BUILT_IN (olddecl))
1651 /* If redeclaring a builtin function, it stays built in.
1652 But it gets tagged as having been declared. */
1653 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1654 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1655 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1658 /* Also preserve various other info from the definition. */
1659 if (! new_is_definition)
1661 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1662 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1663 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1664 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1665 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1667 /* Set DECL_INLINE on the declaration if we've got a body
1668 from which to instantiate. */
1669 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1671 DECL_INLINE (newdecl) = 1;
1672 DECL_ABSTRACT_ORIGIN (newdecl)
1673 = DECL_ABSTRACT_ORIGIN (olddecl);
1678 /* If a previous declaration said inline, mark the
1679 definition as inlinable. */
1680 if (DECL_DECLARED_INLINE_P (newdecl)
1681 && ! DECL_UNINLINABLE (newdecl))
1682 DECL_INLINE (newdecl) = 1;
1686 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1687 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1689 unsigned olddecl_uid = DECL_UID (olddecl);
1690 tree olddecl_context = DECL_CONTEXT (olddecl);
1692 memcpy ((char *) olddecl + sizeof (struct tree_common),
1693 (char *) newdecl + sizeof (struct tree_common),
1694 sizeof (struct tree_decl) - sizeof (struct tree_common));
1695 DECL_UID (olddecl) = olddecl_uid;
1696 DECL_CONTEXT (olddecl) = olddecl_context;
1699 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1700 so that encode_section_info has a chance to look at the new decl
1701 flags and attributes. */
1702 if (DECL_RTL_SET_P (olddecl)
1703 && (TREE_CODE (olddecl) == FUNCTION_DECL
1704 || (TREE_CODE (olddecl) == VAR_DECL
1705 && TREE_STATIC (olddecl))))
1706 make_decl_rtl (olddecl, NULL);
1709 /* Handle when a new declaration NEWDECL has the same name as an old
1710 one OLDDECL in the same binding contour. Prints an error message
1713 If safely possible, alter OLDDECL to look like NEWDECL, and return
1714 true. Otherwise, return false. */
1717 duplicate_decls (tree newdecl, tree olddecl)
1719 tree newtype = NULL, oldtype = NULL;
1721 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1724 merge_decls (newdecl, olddecl, newtype, oldtype);
1729 /* Check whether decl-node NEW shadows an existing declaration. */
1731 warn_if_shadowing (tree new)
1733 struct c_binding *b;
1735 /* Shadow warnings wanted? */
1737 /* No shadow warnings for internally generated vars. */
1738 || DECL_IS_BUILTIN (new)
1739 /* No shadow warnings for vars made for inlining. */
1740 || DECL_FROM_INLINE (new)
1741 /* Don't warn about the parm names in function declarator
1742 within a function declarator. It would be nice to avoid
1743 warning in any function declarator in a declaration, as
1744 opposed to a definition, but there is no way to tell
1745 it's not a definition at this point. */
1746 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1749 /* Is anything being shadowed? Invisible decls do not count. */
1750 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1751 if (b->decl && b->decl != new && !b->invisible)
1755 if (TREE_CODE (old) == PARM_DECL)
1756 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1757 else if (DECL_FILE_SCOPE_P (old))
1758 warning ("%Jdeclaration of '%D' shadows a global declaration",
1760 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1761 warning ("%Jdeclaration of '%D' shadows a built-in function",
1764 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1766 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1767 warning ("%Jshadowed declaration is here", old);
1774 /* Subroutine of pushdecl.
1776 X is a TYPE_DECL for a typedef statement. Create a brand new
1777 ..._TYPE node (which will be just a variant of the existing
1778 ..._TYPE node with identical properties) and then install X
1779 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1781 The whole point here is to end up with a situation where each
1782 and every ..._TYPE node the compiler creates will be uniquely
1783 associated with AT MOST one node representing a typedef name.
1784 This way, even though the compiler substitutes corresponding
1785 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1786 early on, later parts of the compiler can always do the reverse
1787 translation and get back the corresponding typedef name. For
1790 typedef struct S MY_TYPE;
1793 Later parts of the compiler might only know that `object' was of
1794 type `struct S' if it were not for code just below. With this
1795 code however, later parts of the compiler see something like:
1797 struct S' == struct S
1798 typedef struct S' MY_TYPE;
1801 And they can then deduce (from the node for type struct S') that
1802 the original object declaration was:
1806 Being able to do this is important for proper support of protoize,
1807 and also for generating precise symbolic debugging information
1808 which takes full account of the programmer's (typedef) vocabulary.
1810 Obviously, we don't want to generate a duplicate ..._TYPE node if
1811 the TYPE_DECL node that we are now processing really represents a
1812 standard built-in type.
1814 Since all standard types are effectively declared at line zero
1815 in the source file, we can easily check to see if we are working
1816 on a standard type by checking the current value of lineno. */
1819 clone_underlying_type (tree x)
1821 if (DECL_IS_BUILTIN (x))
1823 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1824 TYPE_NAME (TREE_TYPE (x)) = x;
1826 else if (TREE_TYPE (x) != error_mark_node
1827 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1829 tree tt = TREE_TYPE (x);
1830 DECL_ORIGINAL_TYPE (x) = tt;
1831 tt = build_type_copy (tt);
1833 TREE_USED (tt) = TREE_USED (x);
1838 /* Record a decl-node X as belonging to the current lexical scope.
1839 Check for errors (such as an incompatible declaration for the same
1840 name already seen in the same scope).
1842 Returns either X or an old decl for the same name.
1843 If an old decl is returned, it may have been smashed
1844 to agree with what X says. */
1849 tree name = DECL_NAME (x);
1850 struct c_scope *scope = current_scope;
1851 struct c_binding *b;
1852 bool nested = false;
1854 /* Functions need the lang_decl data. */
1855 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1856 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1858 /* Must set DECL_CONTEXT for everything not at file scope or
1859 DECL_FILE_SCOPE_P won't work. Local externs don't count
1860 unless they have initializers (which generate code). */
1861 if (current_function_decl
1862 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1863 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1864 DECL_CONTEXT (x) = current_function_decl;
1866 /* Anonymous decls are just inserted in the scope. */
1869 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1873 /* First, see if there is another declaration with the same name in
1874 the current scope. If there is, duplicate_decls may do all the
1875 work for us. If duplicate_decls returns false, that indicates
1876 two incompatible decls in the same scope; we are to silently
1877 replace the old one (duplicate_decls has issued all appropriate
1878 diagnostics). In particular, we should not consider possible
1879 duplicates in the external scope, or shadowing. */
1880 b = I_SYMBOL_BINDING (name);
1881 if (b && B_IN_SCOPE (b, scope))
1883 if (duplicate_decls (x, b->decl))
1886 goto skip_external_and_shadow_checks;
1889 /* All declarations with external linkage, and all external
1890 references, go in the external scope, no matter what scope is
1891 current. However, the binding in that scope is ignored for
1892 purposes of normal name lookup. A separate binding structure is
1893 created in the requested scope; this governs the normal
1894 visibility of the symbol.
1896 The binding in the externals scope is used exclusively for
1897 detecting duplicate declarations of the same object, no matter
1898 what scope they are in; this is what we do here. (C99 6.2.7p2:
1899 All declarations that refer to the same object or function shall
1900 have compatible type; otherwise, the behavior is undefined.) */
1901 if (DECL_EXTERNAL (x) || scope == file_scope)
1903 if (warn_nested_externs
1904 && scope != file_scope
1905 && !DECL_IN_SYSTEM_HEADER (x))
1906 warning ("nested extern declaration of '%D'", x);
1908 while (b && !B_IN_EXTERNAL_SCOPE (b))
1911 /* The point of the same_translation_unit_p check here is,
1912 we want to detect a duplicate decl for a construct like
1913 foo() { extern bar(); } ... static bar(); but not if
1914 they are in different translation units. In any case,
1915 the static does not go in the externals scope. */
1917 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
1918 && duplicate_decls (x, b->decl))
1920 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1923 else if (TREE_PUBLIC (x))
1925 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1929 /* Similarly, a declaration of a function with static linkage at
1930 block scope must be checked against any existing declaration
1931 of that function at file scope. */
1932 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1933 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1935 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1936 warning ("nested static declaration of '%D'", x);
1938 while (b && !B_IN_FILE_SCOPE (b))
1941 if (b && same_translation_unit_p (x, b->decl)
1942 && duplicate_decls (x, b->decl))
1944 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1949 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1954 warn_if_shadowing (x);
1956 skip_external_and_shadow_checks:
1957 if (TREE_CODE (x) == TYPE_DECL)
1958 clone_underlying_type (x);
1960 bind (name, x, scope, /*invisible=*/false, nested);
1962 /* If x's type is incomplete because it's based on a
1963 structure or union which has not yet been fully declared,
1964 attach it to that structure or union type, so we can go
1965 back and complete the variable declaration later, if the
1966 structure or union gets fully declared.
1968 If the input is erroneous, we can have error_mark in the type
1969 slot (e.g. "f(void a, ...)") - that doesn't count as an
1971 if (TREE_TYPE (x) != error_mark_node
1972 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1974 tree element = TREE_TYPE (x);
1976 while (TREE_CODE (element) == ARRAY_TYPE)
1977 element = TREE_TYPE (element);
1978 element = TYPE_MAIN_VARIANT (element);
1980 if ((TREE_CODE (element) == RECORD_TYPE
1981 || TREE_CODE (element) == UNION_TYPE)
1982 && (TREE_CODE (x) != TYPE_DECL
1983 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1984 && !COMPLETE_TYPE_P (element))
1985 C_TYPE_INCOMPLETE_VARS (element)
1986 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1991 /* Record X as belonging to file scope.
1992 This is used only internally by the Objective-C front end,
1993 and is limited to its needs. duplicate_decls is not called;
1994 if there is any preexisting decl for this identifier, it is an ICE. */
1997 pushdecl_top_level (tree x)
2000 bool nested = false;
2002 if (TREE_CODE (x) != VAR_DECL)
2005 name = DECL_NAME (x);
2007 if (I_SYMBOL_BINDING (name))
2010 if (TREE_PUBLIC (x))
2012 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2016 bind (name, x, file_scope, /*invisible=*/false, nested);
2022 implicit_decl_warning (tree id, tree olddecl)
2024 void (*diag) (const char *, ...);
2025 switch (mesg_implicit_function_declaration)
2028 case 1: diag = warning; break;
2029 case 2: diag = error; break;
2033 diag (N_("implicit declaration of function '%E'"), id);
2035 locate_old_decl (olddecl, diag);
2038 /* Generate an implicit declaration for identifier FUNCTIONID as a
2039 function of type int (). */
2042 implicitly_declare (tree functionid)
2044 tree decl = lookup_name_in_scope (functionid, external_scope);
2048 /* FIXME: Objective-C has weird not-really-builtin functions
2049 which are supposed to be visible automatically. They wind up
2050 in the external scope because they're pushed before the file
2051 scope gets created. Catch this here and rebind them into the
2053 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2055 bind (functionid, decl, file_scope,
2056 /*invisible=*/false, /*nested=*/true);
2061 /* Implicit declaration of a function already declared
2062 (somehow) in a different scope, or as a built-in.
2063 If this is the first time this has happened, warn;
2064 then recycle the old declaration. */
2065 if (!C_DECL_IMPLICIT (decl))
2067 implicit_decl_warning (functionid, decl);
2068 C_DECL_IMPLICIT (decl) = 1;
2070 bind (functionid, decl, current_scope,
2071 /*invisible=*/false, /*nested=*/true);
2076 /* Not seen before. */
2077 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2078 DECL_EXTERNAL (decl) = 1;
2079 TREE_PUBLIC (decl) = 1;
2080 C_DECL_IMPLICIT (decl) = 1;
2081 implicit_decl_warning (functionid, 0);
2083 /* C89 says implicit declarations are in the innermost block.
2084 So we record the decl in the standard fashion. */
2085 decl = pushdecl (decl);
2087 /* No need to call objc_check_decl here - it's a function type. */
2088 rest_of_decl_compilation (decl, NULL, 0, 0);
2090 /* Write a record describing this implicit function declaration
2091 to the prototypes file (if requested). */
2092 gen_aux_info_record (decl, 0, 1, 0);
2094 /* Possibly apply some default attributes to this implicit declaration. */
2095 decl_attributes (&decl, NULL_TREE, 0);
2100 /* Issue an error message for a reference to an undeclared variable
2101 ID, including a reference to a builtin outside of function-call
2102 context. Establish a binding of the identifier to error_mark_node
2103 in an appropriate scope, which will suppress further errors for the
2106 undeclared_variable (tree id)
2108 static bool already = false;
2109 struct c_scope *scope;
2111 if (current_function_decl == 0)
2113 error ("'%E' undeclared here (not in a function)", id);
2114 scope = current_scope;
2118 error ("'%E' undeclared (first use in this function)", id);
2122 error ("(Each undeclared identifier is reported only once");
2123 error ("for each function it appears in.)");
2127 /* If we are parsing old-style parameter decls, current_function_decl
2128 will be nonnull but current_function_scope will be null. */
2129 scope = current_function_scope ? current_function_scope : current_scope;
2131 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2134 /* Subroutine of lookup_label, declare_label, define_label: construct a
2135 LABEL_DECL with all the proper frills. */
2138 make_label (tree name, location_t location)
2140 tree label = build_decl (LABEL_DECL, name, void_type_node);
2142 DECL_CONTEXT (label) = current_function_decl;
2143 DECL_MODE (label) = VOIDmode;
2144 DECL_SOURCE_LOCATION (label) = location;
2149 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2150 Create one if none exists so far for the current function.
2151 This is called when a label is used in a goto expression or
2152 has its address taken. */
2155 lookup_label (tree name)
2159 if (current_function_decl == 0)
2161 error ("label %s referenced outside of any function",
2162 IDENTIFIER_POINTER (name));
2166 /* Use a label already defined or ref'd with this name, but not if
2167 it is inherited from a containing function and wasn't declared
2169 label = I_LABEL_DECL (name);
2170 if (label && (DECL_CONTEXT (label) == current_function_decl
2171 || C_DECLARED_LABEL_FLAG (label)))
2173 /* If the label has only been declared, update its apparent
2174 location to point here, for better diagnostics if it
2175 turns out not to have been defined. */
2176 if (!TREE_USED (label))
2177 DECL_SOURCE_LOCATION (label) = input_location;
2181 /* No label binding for that identifier; make one. */
2182 label = make_label (name, input_location);
2184 /* Ordinary labels go in the current function scope. */
2185 bind (name, label, current_function_scope,
2186 /*invisible=*/false, /*nested=*/false);
2190 /* Make a label named NAME in the current function, shadowing silently
2191 any that may be inherited from containing functions or containing
2192 scopes. This is called for __label__ declarations. */
2195 declare_label (tree name)
2197 struct c_binding *b = I_LABEL_BINDING (name);
2200 /* Check to make sure that the label hasn't already been declared
2202 if (b && B_IN_CURRENT_SCOPE (b))
2204 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2205 locate_old_decl (b->decl, error);
2207 /* Just use the previous declaration. */
2211 label = make_label (name, input_location);
2212 C_DECLARED_LABEL_FLAG (label) = 1;
2214 /* Declared labels go in the current scope. */
2215 bind (name, label, current_scope,
2216 /*invisible=*/false, /*nested=*/false);
2220 /* Define a label, specifying the location in the source file.
2221 Return the LABEL_DECL node for the label, if the definition is valid.
2222 Otherwise return 0. */
2225 define_label (location_t location, tree name)
2227 /* Find any preexisting label with this name. It is an error
2228 if that label has already been defined in this function, or
2229 if there is a containing function with a declared label with
2231 tree label = I_LABEL_DECL (name);
2234 && ((DECL_CONTEXT (label) == current_function_decl
2235 && DECL_INITIAL (label) != 0)
2236 || (DECL_CONTEXT (label) != current_function_decl
2237 && C_DECLARED_LABEL_FLAG (label))))
2239 error ("%Hduplicate label `%D'", &location, label);
2240 locate_old_decl (label, error);
2243 else if (label && DECL_CONTEXT (label) == current_function_decl)
2245 /* The label has been used or declared already in this function,
2246 but not defined. Update its location to point to this
2248 DECL_SOURCE_LOCATION (label) = location;
2252 /* No label binding for that identifier; make one. */
2253 label = make_label (name, location);
2255 /* Ordinary labels go in the current function scope. */
2256 bind (name, label, current_function_scope,
2257 /*invisible=*/false, /*nested=*/false);
2260 if (warn_traditional && !in_system_header && lookup_name (name))
2261 warning ("%Htraditional C lacks a separate namespace for labels, "
2262 "identifier `%s' conflicts", &location,
2263 IDENTIFIER_POINTER (name));
2265 /* Mark label as having been defined. */
2266 DECL_INITIAL (label) = error_mark_node;
2270 /* Given NAME, an IDENTIFIER_NODE,
2271 return the structure (or union or enum) definition for that name.
2272 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2273 CODE says which kind of type the caller wants;
2274 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2275 If the wrong kind of type is found, an error is reported. */
2278 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2280 struct c_binding *b = I_TAG_BINDING (name);
2286 /* We only care about whether it's in this level if
2287 thislevel_only was set or it might be a type clash. */
2288 if (thislevel_only || TREE_CODE (b->decl) != code)
2290 /* For our purposes, a tag in the external scope is the same as
2291 a tag in the file scope. (Primarily relevant to Objective-C
2292 and its builtin structure tags, which get pushed before the
2293 file scope is created.) */
2294 if (B_IN_CURRENT_SCOPE (b)
2295 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2299 if (thislevel_only && !thislevel)
2302 if (TREE_CODE (b->decl) != code)
2304 /* Definition isn't the kind we were looking for. */
2305 pending_invalid_xref = name;
2306 pending_invalid_xref_location = input_location;
2308 /* If in the same binding level as a declaration as a tag
2309 of a different type, this must not be allowed to
2310 shadow that tag, so give the error immediately.
2311 (For example, "struct foo; union foo;" is invalid.) */
2313 pending_xref_error ();
2318 /* Print an error message now
2319 for a recent invalid struct, union or enum cross reference.
2320 We don't print them immediately because they are not invalid
2321 when used in the `struct foo;' construct for shadowing. */
2324 pending_xref_error (void)
2326 if (pending_invalid_xref != 0)
2327 error ("%H`%s' defined as wrong kind of tag",
2328 &pending_invalid_xref_location,
2329 IDENTIFIER_POINTER (pending_invalid_xref));
2330 pending_invalid_xref = 0;
2334 /* Look up NAME in the current scope and its superiors
2335 in the namespace of variables, functions and typedefs.
2336 Return a ..._DECL node of some kind representing its definition,
2337 or return 0 if it is undefined. */
2340 lookup_name (tree name)
2342 struct c_binding *b = I_SYMBOL_BINDING (name);
2343 if (b && !b->invisible)
2348 /* Similar to `lookup_name' but look only at the indicated scope. */
2351 lookup_name_in_scope (tree name, struct c_scope *scope)
2353 struct c_binding *b;
2355 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2356 if (B_IN_SCOPE (b, scope))
2361 /* Create the predefined scalar types of C,
2362 and some nodes representing standard constants (0, 1, (void *) 0).
2363 Initialize the global scope.
2364 Make definitions for built-in primitive functions. */
2367 c_init_decl_processing (void)
2370 tree ptr_ftype_void, ptr_ftype_ptr;
2371 location_t save_loc = input_location;
2373 /* Adds some ggc roots, and reserved words for c-parse.in. */
2376 current_function_decl = 0;
2378 /* Make the externals scope. */
2380 external_scope = current_scope;
2382 /* Declarations from c_common_nodes_and_builtins must not be associated
2383 with this input file, lest we get differences between using and not
2384 using preprocessed headers. */
2385 #ifdef USE_MAPPED_LOCATION
2386 input_location = BUILTINS_LOCATION;
2388 input_location.file = "<built-in>";
2389 input_location.line = 0;
2392 build_common_tree_nodes (flag_signed_char);
2394 c_common_nodes_and_builtins ();
2396 /* In C, comparisons and TRUTH_* expressions have type int. */
2397 truthvalue_type_node = integer_type_node;
2398 truthvalue_true_node = integer_one_node;
2399 truthvalue_false_node = integer_zero_node;
2401 /* Even in C99, which has a real boolean type. */
2402 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2403 boolean_type_node));
2405 endlink = void_list_node;
2406 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2408 = build_function_type (ptr_type_node,
2409 tree_cons (NULL_TREE, ptr_type_node, endlink));
2411 input_location = save_loc;
2413 pedantic_lvalues = true;
2415 make_fname_decl = c_make_fname_decl;
2416 start_fname_decls ();
2419 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2420 decl, NAME is the initialization string and TYPE_DEP indicates whether
2421 NAME depended on the type of the function. As we don't yet implement
2422 delayed emission of static data, we mark the decl as emitted
2423 so it is not placed in the output. Anything using it must therefore pull
2424 out the STRING_CST initializer directly. FIXME. */
2427 c_make_fname_decl (tree id, int type_dep)
2429 const char *name = fname_as_string (type_dep);
2430 tree decl, type, init;
2431 size_t length = strlen (name);
2433 type = build_array_type
2434 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2435 build_index_type (size_int (length)));
2437 decl = build_decl (VAR_DECL, id, type);
2439 TREE_STATIC (decl) = 1;
2440 TREE_READONLY (decl) = 1;
2441 DECL_ARTIFICIAL (decl) = 1;
2443 init = build_string (length + 1, name);
2444 free ((char *) name);
2445 TREE_TYPE (init) = type;
2446 DECL_INITIAL (decl) = init;
2448 TREE_USED (decl) = 1;
2450 if (current_function_decl)
2452 DECL_CONTEXT (decl) = current_function_decl;
2453 bind (id, decl, current_function_scope,
2454 /*invisible=*/false, /*nested=*/false);
2457 finish_decl (decl, init, NULL_TREE);
2462 /* Return a definition for a builtin function named NAME and whose data type
2463 is TYPE. TYPE should be a function type with argument types.
2464 FUNCTION_CODE tells later passes how to compile calls to this function.
2465 See tree.h for its possible values.
2467 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2468 the name to be called if we can't opencode the function. If
2469 ATTRS is nonzero, use that for the function's attribute list. */
2472 builtin_function (const char *name, tree type, int function_code,
2473 enum built_in_class class, const char *library_name,
2476 tree id = get_identifier (name);
2477 tree decl = build_decl (FUNCTION_DECL, id, type);
2478 TREE_PUBLIC (decl) = 1;
2479 DECL_EXTERNAL (decl) = 1;
2480 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2481 DECL_BUILT_IN_CLASS (decl) = class;
2482 DECL_FUNCTION_CODE (decl) = function_code;
2484 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2486 /* Should never be called on a symbol with a preexisting meaning. */
2487 if (I_SYMBOL_BINDING (id))
2490 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2492 /* Builtins in the implementation namespace are made visible without
2493 needing to be explicitly declared. See push_file_scope. */
2494 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2496 TREE_CHAIN (decl) = visible_builtins;
2497 visible_builtins = decl;
2500 /* Possibly apply some default attributes to this built-in function. */
2502 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2504 decl_attributes (&decl, NULL_TREE, 0);
2509 /* Called when a declaration is seen that contains no names to declare.
2510 If its type is a reference to a structure, union or enum inherited
2511 from a containing scope, shadow that tag name for the current scope
2512 with a forward reference.
2513 If its type defines a new named structure or union
2514 or defines an enum, it is valid but we need not do anything here.
2515 Otherwise, it is an error. */
2518 shadow_tag (tree declspecs)
2520 shadow_tag_warned (declspecs, 0);
2524 shadow_tag_warned (tree declspecs, int warned)
2527 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2534 pending_invalid_xref = 0;
2536 /* Remove the attributes from declspecs, since they will confuse the
2538 split_specs_attrs (declspecs, &specs, &attrs);
2540 for (link = specs; link; link = TREE_CHAIN (link))
2542 tree value = TREE_VALUE (link);
2543 enum tree_code code = TREE_CODE (value);
2545 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2546 /* Used to test also that TYPE_SIZE (value) != 0.
2547 That caused warning for `struct foo;' at top level in the file. */
2549 tree name = TYPE_NAME (value);
2556 if (warned != 1 && code != ENUMERAL_TYPE)
2557 /* Empty unnamed enum OK */
2559 pedwarn ("unnamed struct/union that defines no instances");
2565 t = lookup_tag (code, name, 1);
2569 t = make_node (code);
2576 if (!warned && ! in_system_header)
2578 warning ("useless keyword or type name in empty declaration");
2585 error ("two types specified in one empty declaration");
2590 pedwarn ("empty declaration");
2594 /* Construct an array declarator. EXPR is the expression inside [], or
2595 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2596 to the pointer to which a parameter array is converted). STATIC_P is
2597 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2598 is nonzero is the array is [*], a VLA of unspecified length which is
2599 nevertheless a complete type (not currently implemented by GCC),
2600 zero otherwise. The declarator is constructed as an ARRAY_REF
2601 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2602 left of the [] (filled by in set_array_declarator_type) and operand 1
2603 is the expression inside; whose TREE_TYPE is the type qualifiers and
2604 which has TREE_STATIC set if "static" is used. */
2607 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2610 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2611 TREE_TYPE (decl) = quals;
2612 TREE_STATIC (decl) = (static_p ? 1 : 0);
2613 if (pedantic && !flag_isoc99)
2615 if (static_p || quals != NULL_TREE)
2616 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2618 pedwarn ("ISO C90 does not support `[*]' array declarators");
2621 warning ("GCC does not yet properly implement `[*]' array declarators");
2625 /* Set the type of an array declarator. DECL is the declarator, as
2626 constructed by build_array_declarator; TYPE is what appears on the left
2627 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2628 abstract declarator, zero otherwise; this is used to reject static and
2629 type qualifiers in abstract declarators, where they are not in the
2633 set_array_declarator_type (tree decl, tree type, int abstract_p)
2635 TREE_OPERAND (decl, 0) = type;
2636 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2637 error ("static or type qualifiers in abstract declarator");
2641 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2644 groktypename (tree typename)
2648 if (TREE_CODE (typename) != TREE_LIST)
2651 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2653 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2656 /* Apply attributes. */
2657 decl_attributes (&typename, attrs, 0);
2662 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2665 groktypename_in_parm_context (tree typename)
2667 if (TREE_CODE (typename) != TREE_LIST)
2669 return grokdeclarator (TREE_VALUE (typename),
2670 TREE_PURPOSE (typename),
2674 /* Decode a declarator in an ordinary declaration or data definition.
2675 This is called as soon as the type information and variable name
2676 have been parsed, before parsing the initializer if any.
2677 Here we create the ..._DECL node, fill in its type,
2678 and put it on the list of decls for the current context.
2679 The ..._DECL node is returned as the value.
2681 Exception: for arrays where the length is not specified,
2682 the type is left null, to be filled in by `finish_decl'.
2684 Function definitions do not come here; they go to start_function
2685 instead. However, external and forward declarations of functions
2686 do go through here. Structure field declarations are done by
2687 grokfield and not through here. */
2690 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2695 /* An object declared as __attribute__((deprecated)) suppresses
2696 warnings of uses of other deprecated items. */
2697 if (lookup_attribute ("deprecated", attributes))
2698 deprecated_state = DEPRECATED_SUPPRESS;
2700 decl = grokdeclarator (declarator, declspecs,
2701 NORMAL, initialized, NULL);
2703 deprecated_state = DEPRECATED_NORMAL;
2705 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2706 && MAIN_NAME_P (DECL_NAME (decl)))
2707 warning ("%J'%D' is usually a function", decl, decl);
2710 /* Is it valid for this decl to have an initializer at all?
2711 If not, set INITIALIZED to zero, which will indirectly
2712 tell 'finish_decl' to ignore the initializer once it is parsed. */
2713 switch (TREE_CODE (decl))
2716 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2721 error ("function '%D' is initialized like a variable", decl);
2726 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2727 error ("parameter '%D' is initialized", decl);
2732 /* Don't allow initializations for incomplete types except for
2733 arrays which might be completed by the initialization. */
2735 /* This can happen if the array size is an undefined macro.
2736 We already gave a warning, so we don't need another one. */
2737 if (TREE_TYPE (decl) == error_mark_node)
2739 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2741 /* A complete type is ok if size is fixed. */
2743 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2744 || C_DECL_VARIABLE_SIZE (decl))
2746 error ("variable-sized object may not be initialized");
2750 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2752 error ("variable '%D' has initializer but incomplete type", decl);
2755 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2757 error ("elements of array '%D' have incomplete type", decl);
2764 DECL_EXTERNAL (decl) = 0;
2765 if (current_scope == file_scope)
2766 TREE_STATIC (decl) = 1;
2768 /* Tell 'pushdecl' this is an initialized decl
2769 even though we don't yet have the initializer expression.
2770 Also tell 'finish_decl' it may store the real initializer. */
2771 DECL_INITIAL (decl) = error_mark_node;
2774 /* If this is a function declaration, write a record describing it to the
2775 prototypes file (if requested). */
2777 if (TREE_CODE (decl) == FUNCTION_DECL)
2778 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2780 /* ANSI specifies that a tentative definition which is not merged with
2781 a non-tentative definition behaves exactly like a definition with an
2782 initializer equal to zero. (Section 3.7.2)
2784 -fno-common gives strict ANSI behavior, though this tends to break
2785 a large body of code that grew up without this rule.
2787 Thread-local variables are never common, since there's no entrenched
2788 body of code to break, and it allows more efficient variable references
2789 in the presence of dynamic linking. */
2791 if (TREE_CODE (decl) == VAR_DECL
2793 && TREE_PUBLIC (decl)
2794 && !DECL_THREAD_LOCAL (decl)
2796 DECL_COMMON (decl) = 1;
2798 /* Set attributes here so if duplicate decl, will have proper attributes. */
2799 decl_attributes (&decl, attributes, 0);
2801 if (TREE_CODE (decl) == FUNCTION_DECL
2802 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2804 tree ce = declarator;
2806 if (TREE_CODE (ce) == INDIRECT_REF)
2807 ce = TREE_OPERAND (declarator, 0);
2808 if (TREE_CODE (ce) == CALL_EXPR)
2810 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2811 for (; args; args = TREE_CHAIN (args))
2813 tree type = TREE_TYPE (args);
2814 if (type && INTEGRAL_TYPE_P (type)
2815 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2816 DECL_ARG_TYPE (args) = integer_type_node;
2821 if (TREE_CODE (decl) == FUNCTION_DECL
2822 && DECL_DECLARED_INLINE_P (decl)
2823 && DECL_UNINLINABLE (decl)
2824 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2825 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2827 /* Add this decl to the current scope.
2828 TEM may equal DECL or it may be a previous decl of the same name. */
2829 tem = pushdecl (decl);
2834 /* Finish processing of a declaration;
2835 install its initial value.
2836 If the length of an array type is not known before,
2837 it must be determined now, from the initial value, or it is an error. */
2840 finish_decl (tree decl, tree init, tree asmspec_tree)
2842 tree type = TREE_TYPE (decl);
2843 int was_incomplete = (DECL_SIZE (decl) == 0);
2844 const char *asmspec = 0;
2846 /* If a name was specified, get the string. */
2847 if (current_scope == file_scope)
2848 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2850 asmspec = TREE_STRING_POINTER (asmspec_tree);
2852 /* If `start_decl' didn't like having an initialization, ignore it now. */
2853 if (init != 0 && DECL_INITIAL (decl) == 0)
2856 /* Don't crash if parm is initialized. */
2857 if (TREE_CODE (decl) == PARM_DECL)
2861 store_init_value (decl, init);
2863 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2864 || TREE_CODE (decl) == FUNCTION_DECL
2865 || TREE_CODE (decl) == FIELD_DECL))
2866 objc_check_decl (decl);
2868 /* Deduce size of array from initialization, if not already known. */
2869 if (TREE_CODE (type) == ARRAY_TYPE
2870 && TYPE_DOMAIN (type) == 0
2871 && TREE_CODE (decl) != TYPE_DECL)
2874 = (TREE_STATIC (decl)
2875 /* Even if pedantic, an external linkage array
2876 may have incomplete type at first. */
2877 ? pedantic && !TREE_PUBLIC (decl)
2878 : !DECL_EXTERNAL (decl));
2880 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2882 /* Get the completed type made by complete_array_type. */
2883 type = TREE_TYPE (decl);
2886 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2888 else if (failure == 2)
2891 error ("%Jarray size missing in '%D'", decl, decl);
2892 /* If a `static' var's size isn't known,
2893 make it extern as well as static, so it does not get
2895 If it is not `static', then do not mark extern;
2896 finish_incomplete_decl will give it a default size
2897 and it will get allocated. */
2898 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2899 DECL_EXTERNAL (decl) = 1;
2902 /* TYPE_MAX_VALUE is always one less than the number of elements
2903 in the array, because we start counting at zero. Therefore,
2904 warn only if the value is less than zero. */
2905 else if (pedantic && TYPE_DOMAIN (type) != 0
2906 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2907 error ("%Jzero or negative size array '%D'", decl, decl);
2909 layout_decl (decl, 0);
2912 if (TREE_CODE (decl) == VAR_DECL)
2914 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2915 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2916 layout_decl (decl, 0);
2918 if (DECL_SIZE (decl) == 0
2919 /* Don't give an error if we already gave one earlier. */
2920 && TREE_TYPE (decl) != error_mark_node
2921 && (TREE_STATIC (decl)
2923 /* A static variable with an incomplete type
2924 is an error if it is initialized.
2925 Also if it is not file scope.
2926 Otherwise, let it through, but if it is not `extern'
2927 then it may cause an error message later. */
2928 (DECL_INITIAL (decl) != 0
2929 || !DECL_FILE_SCOPE_P (decl))
2931 /* An automatic variable with an incomplete type
2933 !DECL_EXTERNAL (decl)))
2935 error ("%Jstorage size of '%D' isn't known", decl, decl);
2936 TREE_TYPE (decl) = error_mark_node;
2939 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2940 && DECL_SIZE (decl) != 0)
2942 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2943 constant_expression_warning (DECL_SIZE (decl));
2945 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2948 if (TREE_USED (type))
2949 TREE_USED (decl) = 1;
2952 /* If this is a function and an assembler name is specified, reset DECL_RTL
2953 so we can give it its new name. Also, update built_in_decls if it
2954 was a normal built-in. */
2955 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2957 /* ASMSPEC is given, and not the name of a register. Mark the
2958 name with a star so assemble_name won't munge it. */
2959 char *starred = alloca (strlen (asmspec) + 2);
2961 strcpy (starred + 1, asmspec);
2963 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2965 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2966 SET_DECL_RTL (builtin, NULL_RTX);
2967 change_decl_assembler_name (builtin, get_identifier (starred));
2968 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2969 init_block_move_fn (starred);
2970 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2971 init_block_clear_fn (starred);
2973 SET_DECL_RTL (decl, NULL_RTX);
2974 change_decl_assembler_name (decl, get_identifier (starred));
2977 /* If #pragma weak was used, mark the decl weak now. */
2978 if (current_scope == file_scope)
2979 maybe_apply_pragma_weak (decl);
2981 /* Output the assembler code and/or RTL code for variables and functions,
2982 unless the type is an undefined structure or union.
2983 If not, it will get done when the type is completed. */
2985 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2987 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2988 if (c_dialect_objc ())
2989 objc_check_decl (decl);
2991 if (DECL_FILE_SCOPE_P (decl))
2993 if (DECL_INITIAL (decl) == NULL_TREE
2994 || DECL_INITIAL (decl) == error_mark_node)
2995 /* Don't output anything
2996 when a tentative file-scope definition is seen.
2997 But at end of compilation, do output code for them. */
2998 DECL_DEFER_OUTPUT (decl) = 1;
2999 rest_of_decl_compilation (decl, asmspec, true, 0);
3003 /* This is a local variable. If there is an ASMSPEC, the
3004 user has requested that we handle it specially. */
3007 /* In conjunction with an ASMSPEC, the `register'
3008 keyword indicates that we should place the variable
3009 in a particular register. */
3010 if (C_DECL_REGISTER (decl))
3012 DECL_HARD_REGISTER (decl) = 1;
3013 /* This cannot be done for a structure with volatile
3014 fields, on which DECL_REGISTER will have been
3016 if (!DECL_REGISTER (decl))
3017 error ("cannot put object with volatile field into register");
3020 /* If this is not a static variable, issue a warning.
3021 It doesn't make any sense to give an ASMSPEC for an
3022 ordinary, non-register local variable. Historically,
3023 GCC has accepted -- but ignored -- the ASMSPEC in
3025 if (TREE_CODE (decl) == VAR_DECL
3026 && !C_DECL_REGISTER (decl)
3027 && !TREE_STATIC (decl))
3028 warning ("%Jignoring asm-specifier for non-static local "
3029 "variable '%D'", decl, decl);
3031 change_decl_assembler_name (decl, get_identifier (asmspec));
3034 if (TREE_CODE (decl) != FUNCTION_DECL)
3035 add_stmt (build_stmt (DECL_EXPR, decl));
3038 if (!DECL_FILE_SCOPE_P (decl))
3040 /* Recompute the RTL of a local array now
3041 if it used to be an incomplete type. */
3043 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3045 /* If we used it already as memory, it must stay in memory. */
3046 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3047 /* If it's still incomplete now, no init will save it. */
3048 if (DECL_SIZE (decl) == 0)
3049 DECL_INITIAL (decl) = 0;
3054 /* If this was marked 'used', be sure it will be output. */
3055 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3056 mark_decl_referenced (decl);
3058 if (TREE_CODE (decl) == TYPE_DECL)
3060 if (!DECL_FILE_SCOPE_P (decl)
3061 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3062 add_stmt (build_stmt (DECL_EXPR, decl));
3064 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3067 /* At the end of a declaration, throw away any variable type sizes
3068 of types defined inside that declaration. There is no use
3069 computing them in the following function definition. */
3070 if (current_scope == file_scope)
3071 get_pending_sizes ();
3073 /* Install a cleanup (aka destructor) if one was given. */
3074 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3076 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3079 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3080 tree cleanup_decl = lookup_name (cleanup_id);
3083 /* Build "cleanup(&decl)" for the destructor. */
3084 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3085 cleanup = build_tree_list (NULL_TREE, cleanup);
3086 cleanup = build_function_call (cleanup_decl, cleanup);
3088 /* Don't warn about decl unused; the cleanup uses it. */
3089 TREE_USED (decl) = 1;
3090 TREE_USED (cleanup_decl) = 1;
3092 /* Initialize EH, if we've been told to do so. */
3093 if (flag_exceptions && !c_eh_initialized_p)
3095 c_eh_initialized_p = true;
3096 eh_personality_libfunc
3097 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3098 ? "__gcc_personality_sj0"
3099 : "__gcc_personality_v0");
3100 using_eh_for_cleanups ();
3103 push_cleanup (decl, cleanup, false);
3108 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3109 and push that on the current scope. */
3112 push_parm_decl (tree parm)
3116 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3117 TREE_PURPOSE (TREE_PURPOSE (parm)),
3119 decl_attributes (&decl, TREE_VALUE (parm), 0);
3121 decl = pushdecl (decl);
3123 finish_decl (decl, NULL_TREE, NULL_TREE);
3126 /* Mark all the parameter declarations to date as forward decls.
3127 Also diagnose use of this extension. */
3130 mark_forward_parm_decls (void)
3132 struct c_binding *b;
3134 if (pedantic && !current_scope->warned_forward_parm_decls)
3136 pedwarn ("ISO C forbids forward parameter declarations");
3137 current_scope->warned_forward_parm_decls = true;
3140 for (b = current_scope->bindings; b; b = b->prev)
3141 if (TREE_CODE (b->decl) == PARM_DECL)
3142 TREE_ASM_WRITTEN (b->decl) = 1;
3145 static GTY(()) int compound_literal_number;
3147 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3148 literal, which may be an incomplete array type completed by the
3149 initializer; INIT is a CONSTRUCTOR that initializes the compound
3153 build_compound_literal (tree type, tree init)
3155 /* We do not use start_decl here because we have a type, not a declarator;
3156 and do not use finish_decl because the decl should be stored inside
3157 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3158 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3161 DECL_EXTERNAL (decl) = 0;
3162 TREE_PUBLIC (decl) = 0;
3163 TREE_STATIC (decl) = (current_scope == file_scope);
3164 DECL_CONTEXT (decl) = current_function_decl;
3165 TREE_USED (decl) = 1;
3166 TREE_TYPE (decl) = type;
3167 TREE_READONLY (decl) = TYPE_READONLY (type);
3168 store_init_value (decl, init);
3170 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3172 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3177 type = TREE_TYPE (decl);
3178 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3179 return error_mark_node;
3181 stmt = build_stmt (DECL_EXPR, decl);
3182 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3183 TREE_SIDE_EFFECTS (complit) = 1;
3185 layout_decl (decl, 0);
3187 if (TREE_STATIC (decl))
3189 /* This decl needs a name for the assembler output. We also need
3190 a unique suffix to be added to the name. */
3193 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3194 compound_literal_number);
3195 compound_literal_number++;
3196 DECL_NAME (decl) = get_identifier (name);
3197 DECL_DEFER_OUTPUT (decl) = 1;
3198 DECL_COMDAT (decl) = 1;
3199 DECL_ARTIFICIAL (decl) = 1;
3201 rest_of_decl_compilation (decl, NULL, 1, 0);
3207 /* Make TYPE a complete type based on INITIAL_VALUE.
3208 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3209 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3212 complete_array_type (tree type, tree initial_value, int do_default)
3214 tree maxindex = NULL_TREE;
3219 /* Note MAXINDEX is really the maximum index,
3220 one less than the size. */
3221 if (TREE_CODE (initial_value) == STRING_CST)
3224 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3225 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3228 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3230 tree elts = CONSTRUCTOR_ELTS (initial_value);
3231 maxindex = build_int_2 (-1, -1);
3232 for (; elts; elts = TREE_CHAIN (elts))
3234 if (TREE_PURPOSE (elts))
3235 maxindex = TREE_PURPOSE (elts);
3237 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3238 maxindex, integer_one_node));
3240 maxindex = copy_node (maxindex);
3244 /* Make an error message unless that happened already. */
3245 if (initial_value != error_mark_node)
3248 /* Prevent further error messages. */
3249 maxindex = build_int_2 (0, 0);
3256 maxindex = build_int_2 (0, 0);
3262 TYPE_DOMAIN (type) = build_index_type (maxindex);
3263 if (!TREE_TYPE (maxindex))
3264 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3267 /* Lay out the type now that we can get the real answer. */
3274 /* Determine whether TYPE is a structure with a flexible array member,
3275 or a union containing such a structure (possibly recursively). */
3278 flexible_array_type_p (tree type)
3281 switch (TREE_CODE (type))
3284 x = TYPE_FIELDS (type);
3287 while (TREE_CHAIN (x) != NULL_TREE)
3289 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3290 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3291 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3292 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3296 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3298 if (flexible_array_type_p (TREE_TYPE (x)))
3307 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3308 replacing with appropriate values if they are invalid. */
3310 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3313 unsigned int max_width;
3314 unsigned HOST_WIDE_INT w;
3315 const char *name = orig_name ? orig_name: _("<anonymous>");
3318 STRIP_NOPS (*width);
3320 /* Detect and ignore out of range field width and process valid
3322 if (TREE_CODE (*width) != INTEGER_CST)
3324 error ("bit-field `%s' width not an integer constant", name);
3325 *width = integer_one_node;
3329 constant_expression_warning (*width);
3330 if (tree_int_cst_sgn (*width) < 0)
3332 error ("negative width in bit-field `%s'", name);
3333 *width = integer_one_node;
3335 else if (integer_zerop (*width) && orig_name)
3337 error ("zero width for bit-field `%s'", name);
3338 *width = integer_one_node;
3342 /* Detect invalid bit-field type. */
3343 if (TREE_CODE (*type) != INTEGER_TYPE
3344 && TREE_CODE (*type) != BOOLEAN_TYPE
3345 && TREE_CODE (*type) != ENUMERAL_TYPE)
3347 error ("bit-field `%s' has invalid type", name);
3348 *type = unsigned_type_node;
3351 type_mv = TYPE_MAIN_VARIANT (*type);
3353 && type_mv != integer_type_node
3354 && type_mv != unsigned_type_node
3355 && type_mv != boolean_type_node)
3356 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3358 if (type_mv == boolean_type_node)
3359 max_width = CHAR_TYPE_SIZE;
3361 max_width = TYPE_PRECISION (*type);
3363 if (0 < compare_tree_int (*width, max_width))
3365 error ("width of `%s' exceeds its type", name);
3367 *width = build_int_2 (w, 0);
3370 w = tree_low_cst (*width, 1);
3372 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3374 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3376 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3377 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3378 warning ("`%s' is narrower than values of its type", name);
3382 /* Given declspecs and a declarator,
3383 determine the name and type of the object declared
3384 and construct a ..._DECL node for it.
3385 (In one case we can return a ..._TYPE node instead.
3386 For invalid input we sometimes return 0.)
3388 DECLSPECS is a chain of tree_list nodes whose value fields
3389 are the storage classes and type specifiers.
3391 DECL_CONTEXT says which syntactic context this declaration is in:
3392 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3393 FUNCDEF for a function definition. Like NORMAL but a few different
3394 error messages in each case. Return value may be zero meaning
3395 this definition is too screwy to try to parse.
3396 PARM for a parameter declaration (either within a function prototype
3397 or before a function body). Make a PARM_DECL, or return void_type_node.
3398 TYPENAME if for a typename (in a cast or sizeof).
3399 Don't make a DECL node; just return the ..._TYPE node.
3400 FIELD for a struct or union field; make a FIELD_DECL.
3401 INITIALIZED is 1 if the decl has an initializer.
3402 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3403 representing the width of the bit-field.
3405 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3406 It may also be so in the PARM case, for a prototype where the
3407 argument type is specified but not the name.
3409 This function is where the complicated C meanings of `static'
3410 and `extern' are interpreted. */
3413 grokdeclarator (tree declarator, tree declspecs,
3414 enum decl_context decl_context, int initialized, tree *width)
3418 tree type = NULL_TREE;
3423 int type_quals = TYPE_UNQUALIFIED;
3425 int explicit_int = 0;
3426 int explicit_char = 0;
3427 int defaulted_int = 0;
3428 tree typedef_decl = 0;
3429 const char *name, *orig_name;
3430 tree typedef_type = 0;
3431 int funcdef_flag = 0;
3432 enum tree_code innermost_code = ERROR_MARK;
3433 int size_varies = 0;
3434 tree decl_attr = NULL_TREE;
3435 tree array_ptr_quals = NULL_TREE;
3436 int array_parm_static = 0;
3437 tree returned_attrs = NULL_TREE;
3438 bool bitfield = width != NULL;
3440 tree arg_info = NULL_TREE;
3442 if (decl_context == FUNCDEF)
3443 funcdef_flag = 1, decl_context = NORMAL;
3445 /* Look inside a declarator for the name being declared
3446 and get it as a string, for an error message. */
3448 tree decl = declarator;
3452 switch (TREE_CODE (decl))
3457 innermost_code = TREE_CODE (decl);
3458 decl = TREE_OPERAND (decl, 0);
3462 decl = TREE_VALUE (decl);
3465 case IDENTIFIER_NODE:
3466 name = IDENTIFIER_POINTER (decl);
3478 /* A function definition's declarator must have the form of
3479 a function declarator. */
3481 if (funcdef_flag && innermost_code != CALL_EXPR)
3484 /* If this looks like a function definition, make it one,
3485 even if it occurs where parms are expected.
3486 Then store_parm_decls will reject it and not use it as a parm. */
3487 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3488 decl_context = PARM;
3490 /* Look through the decl specs and record which ones appear.
3491 Some typespecs are defined as built-in typenames.
3492 Others, the ones that are modifiers of other types,
3493 are represented by bits in SPECBITS: set the bits for
3494 the modifiers that appear. Storage class keywords are also in SPECBITS.
3496 If there is a typedef name or a type, store the type in TYPE.
3497 This includes builtin typedefs such as `int'.
3499 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3500 and did not come from a user typedef.
3502 Set LONGLONG if `long' is mentioned twice. */
3504 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3506 tree id = TREE_VALUE (spec);
3508 /* If the entire declaration is itself tagged as deprecated then
3509 suppress reports of deprecated items. */
3510 if (id && TREE_DEPRECATED (id))
3512 if (deprecated_state != DEPRECATED_SUPPRESS)
3513 warn_deprecated_use (id);
3516 if (id == ridpointers[(int) RID_INT])
3518 if (id == ridpointers[(int) RID_CHAR])
3521 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3523 enum rid i = C_RID_CODE (id);
3524 if ((int) i <= (int) RID_LAST_MODIFIER)
3526 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3529 error ("`long long long' is too long for GCC");
3532 if (pedantic && !flag_isoc99 && ! in_system_header
3534 pedwarn ("ISO C90 does not support `long long'");
3538 else if (specbits & (1 << (int) i))
3540 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3542 if (pedantic && !flag_isoc99)
3543 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3546 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3549 /* Diagnose "__thread extern". Recall that this list
3550 is in the reverse order seen in the text. */
3552 && (specbits & (1 << (int) RID_EXTERN
3553 | 1 << (int) RID_STATIC)))
3555 if (specbits & 1 << (int) RID_EXTERN)
3556 error ("`__thread' before `extern'");
3558 error ("`__thread' before `static'");
3561 specbits |= 1 << (int) i;
3566 error ("two or more data types in declaration of `%s'", name);
3567 /* Actual typedefs come to us as TYPE_DECL nodes. */
3568 else if (TREE_CODE (id) == TYPE_DECL)
3570 if (TREE_TYPE (id) == error_mark_node)
3571 ; /* Allow the type to default to int to avoid cascading errors. */
3574 type = TREE_TYPE (id);
3575 decl_attr = DECL_ATTRIBUTES (id);
3579 /* Built-in types come as identifiers. */
3580 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3582 tree t = lookup_name (id);
3583 if (!t || TREE_CODE (t) != TYPE_DECL)
3584 error ("`%s' fails to be a typedef or built in type",
3585 IDENTIFIER_POINTER (id));
3586 else if (TREE_TYPE (t) == error_mark_node)
3590 type = TREE_TYPE (t);
3594 else if (TREE_CODE (id) != ERROR_MARK)
3601 typedef_type = type;
3603 size_varies = C_TYPE_VARIABLE_SIZE (type);
3605 /* No type at all: default to `int', and set DEFAULTED_INT
3606 because it was not a user-defined typedef. */
3610 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3611 | (1 << (int) RID_SIGNED)
3612 | (1 << (int) RID_UNSIGNED)
3613 | (1 << (int) RID_COMPLEX))))
3614 /* Don't warn about typedef foo = bar. */
3615 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3616 && ! in_system_header)
3618 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3619 and this is a function, or if -Wimplicit; prefer the former
3620 warning since it is more explicit. */
3621 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3623 warn_about_return_type = 1;
3624 else if (warn_implicit_int || flag_isoc99)
3625 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3630 type = integer_type_node;
3633 /* Now process the modifiers that were specified
3634 and check for invalid combinations. */
3636 /* Long double is a special combination. */
3638 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3639 && TYPE_MAIN_VARIANT (type) == double_type_node)
3641 specbits &= ~(1 << (int) RID_LONG);
3642 type = long_double_type_node;
3645 /* Check all other uses of type modifiers. */
3647 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3648 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3652 if ((specbits & 1 << (int) RID_LONG)
3653 && (specbits & 1 << (int) RID_SHORT))
3654 error ("both long and short specified for `%s'", name);
3655 else if (((specbits & 1 << (int) RID_LONG)
3656 || (specbits & 1 << (int) RID_SHORT))
3658 error ("long or short specified with char for `%s'", name);
3659 else if (((specbits & 1 << (int) RID_LONG)
3660 || (specbits & 1 << (int) RID_SHORT))
3661 && TREE_CODE (type) == REAL_TYPE)
3663 static int already = 0;
3665 error ("long or short specified with floating type for `%s'", name);
3666 if (! already && ! pedantic)
3668 error ("the only valid combination is `long double'");
3672 else if ((specbits & 1 << (int) RID_SIGNED)
3673 && (specbits & 1 << (int) RID_UNSIGNED))
3674 error ("both signed and unsigned specified for `%s'", name);
3675 else if (TREE_CODE (type) != INTEGER_TYPE)
3676 error ("long, short, signed or unsigned invalid for `%s'", name);
3680 if (!explicit_int && !defaulted_int && !explicit_char)
3682 error ("long, short, signed or unsigned used invalidly for `%s'",
3688 /* Discard the type modifiers if they are invalid. */
3691 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3692 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3697 if ((specbits & (1 << (int) RID_COMPLEX))
3698 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3700 error ("complex invalid for `%s'", name);
3701 specbits &= ~(1 << (int) RID_COMPLEX);
3704 /* Decide whether an integer type is signed or not.
3705 Optionally treat bit-fields as signed by default. */
3706 if (specbits & 1 << (int) RID_UNSIGNED
3707 || (bitfield && ! flag_signed_bitfields
3708 && (explicit_int || defaulted_int || explicit_char
3709 /* A typedef for plain `int' without `signed'
3710 can be controlled just like plain `int'. */
3711 || ! (typedef_decl != 0
3712 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3713 && TREE_CODE (type) != ENUMERAL_TYPE
3714 && !(specbits & 1 << (int) RID_SIGNED)))
3717 type = long_long_unsigned_type_node;
3718 else if (specbits & 1 << (int) RID_LONG)
3719 type = long_unsigned_type_node;
3720 else if (specbits & 1 << (int) RID_SHORT)
3721 type = short_unsigned_type_node;
3722 else if (type == char_type_node)
3723 type = unsigned_char_type_node;
3724 else if (typedef_decl)
3725 type = c_common_unsigned_type (type);
3727 type = unsigned_type_node;
3729 else if ((specbits & 1 << (int) RID_SIGNED)
3730 && type == char_type_node)
3731 type = signed_char_type_node;
3733 type = long_long_integer_type_node;
3734 else if (specbits & 1 << (int) RID_LONG)
3735 type = long_integer_type_node;
3736 else if (specbits & 1 << (int) RID_SHORT)
3737 type = short_integer_type_node;
3739 if (specbits & 1 << (int) RID_COMPLEX)
3741 if (pedantic && !flag_isoc99)
3742 pedwarn ("ISO C90 does not support complex types");
3743 /* If we just have "complex", it is equivalent to
3744 "complex double", but if any modifiers at all are specified it is
3745 the complex form of TYPE. E.g, "complex short" is
3746 "complex short int". */
3748 if (defaulted_int && ! longlong
3749 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3750 | (1 << (int) RID_SIGNED)
3751 | (1 << (int) RID_UNSIGNED))))
3754 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3755 type = complex_double_type_node;
3757 else if (type == integer_type_node)
3760 pedwarn ("ISO C does not support complex integer types");
3761 type = complex_integer_type_node;
3763 else if (type == float_type_node)
3764 type = complex_float_type_node;
3765 else if (type == double_type_node)
3766 type = complex_double_type_node;
3767 else if (type == long_double_type_node)
3768 type = complex_long_double_type_node;
3772 pedwarn ("ISO C does not support complex integer types");
3773 type = build_complex_type (type);
3777 /* Check the type and width of a bit-field. */
3779 check_bitfield_type_and_width (&type, width, orig_name);
3781 /* Figure out the type qualifiers for the declaration. There are
3782 two ways a declaration can become qualified. One is something
3783 like `const int i' where the `const' is explicit. Another is
3784 something like `typedef const int CI; CI i' where the type of the
3785 declaration contains the `const'. A third possibility is that
3786 there is a type qualifier on the element type of a typedefed
3787 array type, in which case we should extract that qualifier so
3788 that c_apply_type_quals_to_decls receives the full list of
3789 qualifiers to work with (C90 is not entirely clear about whether
3790 duplicate qualifiers should be diagnosed in this case, but it
3791 seems most appropriate to do so). */
3792 element_type = strip_array_types (type);
3793 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3795 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3797 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3798 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3799 if (pedantic && !flag_isoc99)
3802 pedwarn ("duplicate `const'");
3804 pedwarn ("duplicate `restrict'");
3806 pedwarn ("duplicate `volatile'");
3808 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3809 type = TYPE_MAIN_VARIANT (type);
3810 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3811 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3812 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3814 /* Warn if two storage classes are given. Default to `auto'. */
3819 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3820 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3821 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3822 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3823 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3825 /* "static __thread" and "extern __thread" are allowed. */
3826 if ((specbits & (1 << (int) RID_THREAD
3827 | 1 << (int) RID_STATIC
3828 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3831 /* Warn about storage classes that are invalid for certain
3832 kinds of declarations (parameters, typenames, etc.). */
3835 error ("multiple storage classes in declaration of `%s'", name);
3836 else if (funcdef_flag
3838 & ((1 << (int) RID_REGISTER)
3839 | (1 << (int) RID_AUTO)
3840 | (1 << (int) RID_TYPEDEF)
3841 | (1 << (int) RID_THREAD))))
3843 if (specbits & 1 << (int) RID_AUTO
3844 && (pedantic || current_scope == file_scope))
3845 pedwarn ("function definition declared `auto'");
3846 if (specbits & 1 << (int) RID_REGISTER)
3847 error ("function definition declared `register'");
3848 if (specbits & 1 << (int) RID_TYPEDEF)
3849 error ("function definition declared `typedef'");
3850 if (specbits & 1 << (int) RID_THREAD)
3851 error ("function definition declared `__thread'");
3852 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3853 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3855 else if (decl_context != NORMAL && nclasses > 0)
3857 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3861 switch (decl_context)
3864 error ("storage class specified for structure field `%s'",
3868 error ("storage class specified for parameter `%s'", name);
3871 error ("storage class specified for typename");
3874 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3875 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3876 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3879 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3881 /* `extern' with initialization is invalid if not at file scope. */
3882 if (current_scope == file_scope)
3883 warning ("`%s' initialized and declared `extern'", name);
3885 error ("`%s' has both `extern' and initializer", name);
3887 else if (current_scope == file_scope)
3889 if (specbits & 1 << (int) RID_AUTO)
3890 error ("file-scope declaration of `%s' specifies `auto'", name);
3894 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3895 error ("nested function `%s' declared `extern'", name);
3896 else if ((specbits & (1 << (int) RID_THREAD
3897 | 1 << (int) RID_EXTERN
3898 | 1 << (int) RID_STATIC))
3899 == (1 << (int) RID_THREAD))
3901 error ("function-scope `%s' implicitly auto and declared `__thread'",
3903 specbits &= ~(1 << (int) RID_THREAD);
3908 /* Now figure out the structure of the declarator proper.
3909 Descend through it, creating more complex types, until we reach
3910 the declared identifier (or NULL_TREE, in an absolute declarator). */
3912 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3914 if (type == error_mark_node)
3916 declarator = TREE_OPERAND (declarator, 0);
3920 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3921 an INDIRECT_REF (for *...),
3922 a CALL_EXPR (for ...(...)),
3923 a TREE_LIST (for nested attributes),
3924 an identifier (for the name being declared)
3925 or a null pointer (for the place in an absolute declarator
3926 where the name was omitted).
3927 For the last two cases, we have just exited the loop.
3929 At this point, TYPE is the type of elements of an array,
3930 or for a function to return, or for a pointer to point to.
3931 After this sequence of ifs, TYPE is the type of the
3932 array or function or pointer, and DECLARATOR has had its
3933 outermost layer removed. */
3935 if (array_ptr_quals != NULL_TREE || array_parm_static)
3937 /* Only the innermost declarator (making a parameter be of
3938 array type which is converted to pointer type)
3939 may have static or type qualifiers. */
3940 error ("static or type qualifiers in non-parameter array declarator");
3941 array_ptr_quals = NULL_TREE;
3942 array_parm_static = 0;
3945 if (TREE_CODE (declarator) == TREE_LIST)
3947 /* We encode a declarator with embedded attributes using
3949 tree attrs = TREE_PURPOSE (declarator);
3952 declarator = TREE_VALUE (declarator);
3953 inner_decl = declarator;
3954 while (inner_decl != NULL_TREE
3955 && TREE_CODE (inner_decl) == TREE_LIST)
3956 inner_decl = TREE_VALUE (inner_decl);
3957 if (inner_decl == NULL_TREE
3958 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3959 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3960 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3961 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3962 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3963 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3964 returned_attrs = decl_attributes (&type,
3965 chainon (returned_attrs, attrs),
3968 else if (TREE_CODE (declarator) == ARRAY_REF)
3970 tree itype = NULL_TREE;
3971 tree size = TREE_OPERAND (declarator, 1);
3972 /* The index is a signed object `sizetype' bits wide. */
3973 tree index_type = c_common_signed_type (sizetype);
3975 array_ptr_quals = TREE_TYPE (declarator);
3976 array_parm_static = TREE_STATIC (declarator);
3978 declarator = TREE_OPERAND (declarator, 0);
3980 /* Check for some types that there cannot be arrays of. */
3982 if (VOID_TYPE_P (type))
3984 error ("declaration of `%s' as array of voids", name);
3985 type = error_mark_node;
3988 if (TREE_CODE (type) == FUNCTION_TYPE)
3990 error ("declaration of `%s' as array of functions", name);
3991 type = error_mark_node;
3994 if (pedantic && !in_system_header && flexible_array_type_p (type))
3995 pedwarn ("invalid use of structure with flexible array member");
3997 if (size == error_mark_node)
3998 type = error_mark_node;
4000 if (type == error_mark_node)
4003 /* If size was specified, set ITYPE to a range-type for that size.
4004 Otherwise, ITYPE remains null. finish_decl may figure it out
4005 from an initial value. */
4009 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4010 STRIP_TYPE_NOPS (size);
4012 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4014 error ("size of array `%s' has non-integer type", name);
4015 size = integer_one_node;
4018 if (pedantic && integer_zerop (size))
4019 pedwarn ("ISO C forbids zero-size array `%s'", name);
4021 if (TREE_CODE (size) == INTEGER_CST)
4023 constant_expression_warning (size);
4024 if (tree_int_cst_sgn (size) < 0)
4026 error ("size of array `%s' is negative", name);
4027 size = integer_one_node;
4032 /* Make sure the array size remains visibly nonconstant
4033 even if it is (eg) a const variable with known value. */
4036 if (!flag_isoc99 && pedantic)
4038 if (TREE_CONSTANT (size))
4039 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4042 pedwarn ("ISO C90 forbids variable-size array `%s'",
4047 if (integer_zerop (size))
4049 /* A zero-length array cannot be represented with an
4050 unsigned index type, which is what we'll get with
4051 build_index_type. Create an open-ended range instead. */
4052 itype = build_range_type (sizetype, size, NULL_TREE);
4056 /* Compute the maximum valid index, that is, size - 1.
4057 Do the calculation in index_type, so that if it is
4058 a variable the computations will be done in the
4060 itype = fold (build (MINUS_EXPR, index_type,
4061 convert (index_type, size),
4062 convert (index_type, size_one_node)));
4064 /* If that overflowed, the array is too big.
4065 ??? While a size of INT_MAX+1 technically shouldn't
4066 cause an overflow (because we subtract 1), the overflow
4067 is recorded during the conversion to index_type, before
4068 the subtraction. Handling this case seems like an
4069 unnecessary complication. */
4070 if (TREE_OVERFLOW (itype))
4072 error ("size of array `%s' is too large", name);
4073 type = error_mark_node;
4078 itype = variable_size (itype);
4079 itype = build_index_type (itype);
4082 else if (decl_context == FIELD)
4084 if (pedantic && !flag_isoc99 && !in_system_header)
4085 pedwarn ("ISO C90 does not support flexible array members");
4087 /* ISO C99 Flexible array members are effectively identical
4088 to GCC's zero-length array extension. */
4089 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4092 /* If pedantic, complain about arrays of incomplete types. */
4094 if (pedantic && !COMPLETE_TYPE_P (type))
4095 pedwarn ("array type has incomplete element type");
4097 /* Build the array type itself, then merge any constancy or
4098 volatility into the target type. We must do it in this order
4099 to ensure that the TYPE_MAIN_VARIANT field of the array type
4100 is set correctly. */
4102 type = build_array_type (type, itype);
4104 type = c_build_qualified_type (type, type_quals);
4107 C_TYPE_VARIABLE_SIZE (type) = 1;
4109 /* The GCC extension for zero-length arrays differs from
4110 ISO flexible array members in that sizeof yields zero. */
4111 if (size && integer_zerop (size))
4114 TYPE_SIZE (type) = bitsize_zero_node;
4115 TYPE_SIZE_UNIT (type) = size_zero_node;
4117 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4118 /* We can never complete an array type which is the target of a
4119 pointer, so go ahead and lay it out. */
4122 if (decl_context != PARM
4123 && (array_ptr_quals != NULL_TREE || array_parm_static))
4125 error ("static or type qualifiers in non-parameter array declarator");
4126 array_ptr_quals = NULL_TREE;
4127 array_parm_static = 0;
4130 else if (TREE_CODE (declarator) == CALL_EXPR)
4132 /* Say it's a definition only for the CALL_EXPR closest to
4134 bool really_funcdef = (funcdef_flag
4135 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4136 == IDENTIFIER_NODE));
4139 /* Declaring a function type.
4140 Make sure we have a valid type for the function to return. */
4141 if (type == error_mark_node)
4146 /* Warn about some types functions can't return. */
4148 if (TREE_CODE (type) == FUNCTION_TYPE)
4150 error ("`%s' declared as function returning a function", name);
4151 type = integer_type_node;
4153 if (TREE_CODE (type) == ARRAY_TYPE)
4155 error ("`%s' declared as function returning an array", name);
4156 type = integer_type_node;
4159 /* Construct the function type and go to the next
4160 inner layer of declarator. */
4161 arg_info = TREE_OPERAND (declarator, 1);
4162 arg_types = grokparms (arg_info, really_funcdef);
4164 /* Type qualifiers before the return type of the function
4165 qualify the return type, not the function type. */
4168 /* Type qualifiers on a function return type are
4169 normally permitted by the standard but have no
4170 effect, so give a warning at -Wreturn-type.
4171 Qualifiers on a void return type are banned on
4172 function definitions in ISO C; GCC used to used them
4173 for noreturn functions. */
4174 if (VOID_TYPE_P (type) && really_funcdef)
4175 pedwarn ("function definition has qualified void return type");
4176 else if (warn_return_type)
4177 warning ("type qualifiers ignored on function return type");
4179 type = c_build_qualified_type (type, type_quals);
4181 type_quals = TYPE_UNQUALIFIED;
4183 type = build_function_type (type, arg_types);
4184 declarator = TREE_OPERAND (declarator, 0);
4186 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4187 the formal parameter list of this FUNCTION_TYPE to point to
4188 the FUNCTION_TYPE node itself. */
4193 for (link = ARG_INFO_TAGS (arg_info);
4195 link = TREE_CHAIN (link))
4196 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4199 else if (TREE_CODE (declarator) == INDIRECT_REF)
4201 /* Merge any constancy or volatility into the target type
4204 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4206 pedwarn ("ISO C forbids qualified function types");
4208 type = c_build_qualified_type (type, type_quals);
4209 type_quals = TYPE_UNQUALIFIED;
4212 type = build_pointer_type (type);
4214 /* Process a list of type modifier keywords
4215 (such as const or volatile) that were given inside the `*'. */
4217 if (TREE_TYPE (declarator))
4225 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4226 typemodlist = TREE_CHAIN (typemodlist))
4228 tree qualifier = TREE_VALUE (typemodlist);
4230 if (C_IS_RESERVED_WORD (qualifier))
4232 if (C_RID_CODE (qualifier) == RID_CONST)
4234 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4236 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4246 error ("invalid type modifier within pointer declarator");
4247 if (pedantic && !flag_isoc99)
4250 pedwarn ("duplicate `const'");
4252 pedwarn ("duplicate `volatile'");
4254 pedwarn ("duplicate `restrict'");
4257 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4258 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4259 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4262 declarator = TREE_OPERAND (declarator, 0);
4269 /* Now TYPE has the actual type. */
4271 /* Did array size calculations overflow? */
4273 if (TREE_CODE (type) == ARRAY_TYPE
4274 && COMPLETE_TYPE_P (type)
4275 && TREE_OVERFLOW (TYPE_SIZE (type)))
4277 error ("size of array `%s' is too large", name);
4278 /* If we proceed with the array type as it is, we'll eventually
4279 crash in tree_low_cst(). */
4280 type = error_mark_node;
4283 /* If this is declaring a typedef name, return a TYPE_DECL. */
4285 if (specbits & (1 << (int) RID_TYPEDEF))
4288 /* Note that the grammar rejects storage classes
4289 in typenames, fields or parameters */
4290 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4292 pedwarn ("ISO C forbids qualified function types");
4294 type = c_build_qualified_type (type, type_quals);
4295 decl = build_decl (TYPE_DECL, declarator, type);
4296 if ((specbits & (1 << (int) RID_SIGNED))
4297 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4298 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4299 decl_attributes (&decl, returned_attrs, 0);
4303 /* Detect the case of an array type of unspecified size
4304 which came, as such, direct from a typedef name.
4305 We must copy the type, so that each identifier gets
4306 a distinct type, so that each identifier's size can be
4307 controlled separately by its own initializer. */
4309 if (type != 0 && typedef_type != 0
4310 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4311 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4313 type = build_array_type (TREE_TYPE (type), 0);
4315 C_TYPE_VARIABLE_SIZE (type) = 1;
4318 /* If this is a type name (such as, in a cast or sizeof),
4319 compute the type and return it now. */
4321 if (decl_context == TYPENAME)
4323 /* Note that the grammar rejects storage classes
4324 in typenames, fields or parameters */
4325 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4327 pedwarn ("ISO C forbids const or volatile function types");
4329 type = c_build_qualified_type (type, type_quals);
4330 decl_attributes (&type, returned_attrs, 0);
4334 /* Aside from typedefs and type names (handle above),
4335 `void' at top level (not within pointer)
4336 is allowed only in public variables.
4337 We don't complain about parms either, but that is because
4338 a better error message can be made later. */
4340 if (VOID_TYPE_P (type) && decl_context != PARM
4341 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4342 && ((specbits & (1 << (int) RID_EXTERN))
4343 || (current_scope == file_scope
4345 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4347 error ("variable or field `%s' declared void", name);
4348 type = integer_type_node;
4351 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4352 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4357 if (decl_context == PARM)
4359 tree type_as_written;
4362 /* A parameter declared as an array of T is really a pointer to T.
4363 One declared as a function is really a pointer to a function. */
4365 if (TREE_CODE (type) == ARRAY_TYPE)
4367 /* Transfer const-ness of array into that of type pointed to. */
4368 type = TREE_TYPE (type);
4370 type = c_build_qualified_type (type, type_quals);
4371 type = build_pointer_type (type);
4372 type_quals = TYPE_UNQUALIFIED;
4373 if (array_ptr_quals)
4375 tree new_ptr_quals, new_ptr_attrs;
4377 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4378 /* We don't yet implement attributes in this context. */
4379 if (new_ptr_attrs != NULL_TREE)
4380 warning ("attributes in parameter array declarator ignored");
4385 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4387 tree qualifier = TREE_VALUE (new_ptr_quals);
4389 if (C_IS_RESERVED_WORD (qualifier))
4391 if (C_RID_CODE (qualifier) == RID_CONST)
4393 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4395 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4405 error ("invalid type modifier within array declarator");
4407 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4408 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4409 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4413 else if (TREE_CODE (type) == FUNCTION_TYPE)
4415 if (pedantic && type_quals)
4416 pedwarn ("ISO C forbids qualified function types");
4418 type = c_build_qualified_type (type, type_quals);
4419 type = build_pointer_type (type);
4420 type_quals = TYPE_UNQUALIFIED;
4422 else if (type_quals)
4423 type = c_build_qualified_type (type, type_quals);
4425 type_as_written = type;
4427 decl = build_decl (PARM_DECL, declarator, type);
4429 C_DECL_VARIABLE_SIZE (decl) = 1;
4431 /* Compute the type actually passed in the parmlist,
4432 for the case where there is no prototype.
4433 (For example, shorts and chars are passed as ints.)
4434 When there is a prototype, this is overridden later. */
4436 if (type == error_mark_node)
4437 promoted_type = type;
4439 promoted_type = c_type_promotes_to (type);
4441 DECL_ARG_TYPE (decl) = promoted_type;
4442 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4444 else if (decl_context == FIELD)
4446 /* Structure field. It may not be a function. */
4448 if (TREE_CODE (type) == FUNCTION_TYPE)
4450 error ("field `%s' declared as a function", name);
4451 type = build_pointer_type (type);
4453 else if (TREE_CODE (type) != ERROR_MARK
4454 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4456 error ("field `%s' has incomplete type", name);
4457 type = error_mark_node;
4459 /* Move type qualifiers down to element of an array. */
4460 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4461 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4463 TYPE_DOMAIN (type));
4464 decl = build_decl (FIELD_DECL, declarator, type);
4465 DECL_NONADDRESSABLE_P (decl) = bitfield;
4468 C_DECL_VARIABLE_SIZE (decl) = 1;
4470 else if (TREE_CODE (type) == FUNCTION_TYPE)
4472 if (specbits & (1 << (int) RID_AUTO)
4473 && (pedantic || current_scope == file_scope))
4474 pedwarn ("invalid storage class for function `%s'", name);
4475 if (specbits & (1 << (int) RID_REGISTER))
4476 error ("invalid storage class for function `%s'", name);
4477 if (specbits & (1 << (int) RID_THREAD))
4478 error ("invalid storage class for function `%s'", name);
4479 /* Function declaration not at file scope.
4480 Storage classes other than `extern' are not allowed
4481 and `extern' makes no difference. */
4482 if (current_scope != file_scope
4483 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4485 pedwarn ("invalid storage class for function `%s'", name);
4487 decl = build_decl (FUNCTION_DECL, declarator, type);
4488 decl = build_decl_attribute_variant (decl, decl_attr);
4490 DECL_LANG_SPECIFIC (decl)
4491 = ggc_alloc_cleared (sizeof (struct lang_decl));
4493 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4494 pedwarn ("ISO C forbids qualified function types");
4496 /* GNU C interprets a volatile-qualified function type to indicate
4497 that the function does not return. */
4498 if ((type_quals & TYPE_QUAL_VOLATILE)
4499 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4500 warning ("`noreturn' function returns non-void value");
4502 /* Every function declaration is an external reference
4503 (DECL_EXTERNAL) except for those which are not at file
4504 scope and are explicitly declared "auto". This is
4505 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4506 GCC to signify a forward declaration of a nested function. */
4507 if ((specbits & (1 << RID_AUTO)) && current_scope != file_scope)
4508 DECL_EXTERNAL (decl) = 0;
4510 DECL_EXTERNAL (decl) = 1;
4512 /* Record absence of global scope for `static' or `auto'. */
4514 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4516 /* For a function definition, record the argument information
4517 block in DECL_ARGUMENTS where store_parm_decls will look
4520 DECL_ARGUMENTS (decl) = arg_info;
4523 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4525 /* Record presence of `inline', if it is reasonable. */
4526 if (MAIN_NAME_P (declarator))
4529 warning ("cannot inline function `main'");
4533 /* Record that the function is declared `inline'. */
4534 DECL_DECLARED_INLINE_P (decl) = 1;
4536 /* Do not mark bare declarations as DECL_INLINE. Doing so
4537 in the presence of multiple declarations can result in
4538 the abstract origin pointing between the declarations,
4539 which will confuse dwarf2out. */
4542 DECL_INLINE (decl) = 1;
4543 if (specbits & (1 << (int) RID_EXTERN))
4544 current_extern_inline = 1;
4547 /* If -finline-functions, assume it can be inlined. This does
4548 two things: let the function be deferred until it is actually
4549 needed, and let dwarf2 know that the function is inlinable. */
4550 else if (flag_inline_trees == 2 && initialized)
4551 DECL_INLINE (decl) = 1;
4555 /* It's a variable. */
4556 /* An uninitialized decl with `extern' is a reference. */
4557 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4559 /* Move type qualifiers down to element of an array. */
4560 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4562 int saved_align = TYPE_ALIGN(type);
4563 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4565 TYPE_DOMAIN (type));
4566 TYPE_ALIGN (type) = saved_align;
4568 else if (type_quals)
4569 type = c_build_qualified_type (type, type_quals);
4571 /* C99 6.2.2p7: It is invalid (compile-time undefined
4572 behavior) to create an 'extern' declaration for a
4573 variable if there is a global declaration that is
4574 'static' and the global declaration is not visible.
4575 (If the static declaration _is_ currently visible,
4576 the 'extern' declaration is taken to refer to that decl.) */
4577 if (extern_ref && current_scope != file_scope)
4579 tree global_decl = identifier_global_value (declarator);
4580 tree visible_decl = lookup_name (declarator);
4583 && global_decl != visible_decl
4584 && TREE_CODE (global_decl) == VAR_DECL
4585 && !TREE_PUBLIC (global_decl))
4586 error ("variable previously declared 'static' redeclared "
4590 decl = build_decl (VAR_DECL, declarator, type);
4592 C_DECL_VARIABLE_SIZE (decl) = 1;
4595 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4597 DECL_EXTERNAL (decl) = extern_ref;
4599 /* At file scope, the presence of a `static' or `register' storage
4600 class specifier, or the absence of all storage class specifiers
4601 makes this declaration a definition (perhaps tentative). Also,
4602 the absence of both `static' and `register' makes it public. */
4603 if (current_scope == file_scope)
4605 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4606 | (1 << (int) RID_REGISTER)));
4607 TREE_STATIC (decl) = !extern_ref;
4609 /* Not at file scope, only `static' makes a static definition. */
4612 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4613 TREE_PUBLIC (decl) = extern_ref;
4616 if (specbits & 1 << (int) RID_THREAD)
4618 if (targetm.have_tls)
4619 DECL_THREAD_LOCAL (decl) = 1;
4621 /* A mere warning is sure to result in improper semantics
4622 at runtime. Don't bother to allow this to compile. */
4623 error ("thread-local storage not supported for this target");
4627 /* Record `register' declaration for warnings on &
4628 and in case doing stupid register allocation. */
4630 if (specbits & (1 << (int) RID_REGISTER))
4632 C_DECL_REGISTER (decl) = 1;
4633 DECL_REGISTER (decl) = 1;
4636 /* Record constancy and volatility. */
4637 c_apply_type_quals_to_decl (type_quals, decl);
4639 /* If a type has volatile components, it should be stored in memory.
4640 Otherwise, the fact that those components are volatile
4641 will be ignored, and would even crash the compiler. */
4642 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4644 /* It is not an error for a structure with volatile fields to
4645 be declared register, but reset DECL_REGISTER since it
4646 cannot actually go in a register. */
4647 int was_reg = C_DECL_REGISTER (decl);
4648 C_DECL_REGISTER (decl) = 0;
4649 DECL_REGISTER (decl) = 0;
4650 c_mark_addressable (decl);
4651 C_DECL_REGISTER (decl) = was_reg;
4654 #ifdef ENABLE_CHECKING
4655 /* This is the earliest point at which we might know the assembler
4656 name of a variable. Thus, if it's known before this, die horribly. */
4657 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4661 decl_attributes (&decl, returned_attrs, 0);
4667 /* Decode the parameter-list info for a function type or function definition.
4668 The argument is the value returned by `get_parm_info' (or made in parse.y
4669 if there is an identifier list instead of a parameter decl list).
4670 These two functions are separate because when a function returns
4671 or receives functions then each is called multiple times but the order
4672 of calls is different. The last call to `grokparms' is always the one
4673 that contains the formal parameter names of a function definition.
4675 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4677 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4678 a mere declaration. A nonempty identifier-list gets an error message
4679 when FUNCDEF_FLAG is zero. */
4682 grokparms (tree arg_info, int funcdef_flag)
4684 tree arg_types = ARG_INFO_TYPES (arg_info);
4686 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4687 && !in_system_header)
4688 warning ("function declaration isn't a prototype");
4690 if (arg_types == error_mark_node)
4691 return 0; /* don't set TYPE_ARG_TYPES in this case */
4693 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4696 pedwarn ("parameter names (without types) in function declaration");
4698 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4699 ARG_INFO_TYPES (arg_info) = 0;
4704 tree parm, type, typelt;
4705 unsigned int parmno;
4707 /* If the arg types are incomplete in a declaration, they must
4708 include undefined tags. These tags can never be defined in
4709 the scope of the declaration, so the types can never be
4710 completed, and no call can be compiled successfully. */
4712 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4714 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4716 type = TREE_VALUE (typelt);
4717 if (type == error_mark_node)
4720 if (!COMPLETE_TYPE_P (type))
4724 if (DECL_NAME (parm))
4725 error ("%Jparameter %u ('%D') has incomplete type",
4726 parm, parmno, parm);
4728 error ("%Jparameter %u has incomplete type",
4731 TREE_VALUE (typelt) = error_mark_node;
4732 TREE_TYPE (parm) = error_mark_node;
4736 if (DECL_NAME (parm))
4737 warning ("%Jparameter %u ('%D') has incomplete type",
4738 parm, parmno, parm);
4740 warning ("%Jparameter %u has incomplete type",
4749 /* Take apart the current scope and return a tree_list node with info
4750 on a parameter list just parsed. This tree_list node should be
4751 examined using the ARG_INFO_* macros, defined above:
4753 ARG_INFO_PARMS: a list of parameter decls.
4754 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4755 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4756 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4757 constants) defined with the parameters.
4759 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4761 ELLIPSIS being true means the argument list ended in '...' so don't
4762 append a sentinel (void_list_node) to the end of the type-list. */
4765 get_parm_info (bool ellipsis)
4767 struct c_binding *b = current_scope->bindings;
4768 tree arg_info = make_node (TREE_LIST);
4774 static bool explained_incomplete_types = false;
4775 bool gave_void_only_once_err = false;
4777 /* The bindings in this scope must not get put into a block.
4778 We will take care of deleting the binding nodes. */
4779 current_scope->bindings = 0;
4781 /* This function is only called if there was *something* on the
4783 #ifdef ENABLE_CHECKING
4788 /* A parameter list consisting solely of 'void' indicates that the
4789 function takes no arguments. But if the 'void' is qualified
4790 (by 'const' or 'volatile'), or has a storage class specifier
4791 ('register'), then the behavior is undefined; issue an error.
4792 Typedefs for 'void' are OK (see DR#157). */
4793 if (b->prev == 0 /* one binding */
4794 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4795 && !DECL_NAME (b->decl) /* anonymous */
4796 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4798 if (TREE_THIS_VOLATILE (b->decl)
4799 || TREE_READONLY (b->decl)
4800 || C_DECL_REGISTER (b->decl))
4801 error ("'void' as only parameter may not be qualified");
4803 /* There cannot be an ellipsis. */
4805 error ("'void' must be the only parameter");
4807 ARG_INFO_TYPES (arg_info) = void_list_node;
4812 types = void_list_node;
4814 /* Break up the bindings list into parms, tags, types, and others;
4815 apply sanity checks; purge the name-to-decl bindings. */
4818 tree decl = b->decl;
4819 tree type = TREE_TYPE (decl);
4820 const char *keyword;
4822 switch (TREE_CODE (decl))
4827 #ifdef ENABLE_CHECKING
4828 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4830 I_SYMBOL_BINDING (b->id) = b->shadowed;
4833 /* Check for forward decls that never got their actual decl. */
4834 if (TREE_ASM_WRITTEN (decl))
4835 error ("%Jparameter '%D' has just a forward declaration",
4837 /* Check for (..., void, ...) and issue an error. */
4838 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4840 if (!gave_void_only_once_err)
4842 error ("'void' must be the only parameter");
4843 gave_void_only_once_err = true;
4848 /* Valid parameter, add it to the list. */
4849 TREE_CHAIN (decl) = parms;
4852 /* Since there is a prototype, args are passed in their
4853 declared types. The back end may override this later. */
4854 DECL_ARG_TYPE (decl) = type;
4855 types = tree_cons (0, type, types);
4859 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4860 case UNION_TYPE: keyword = "union"; goto tag;
4861 case RECORD_TYPE: keyword = "struct"; goto tag;
4863 /* Types may not have tag-names, in which case the type
4864 appears in the bindings list with b->id NULL. */
4867 #ifdef ENABLE_CHECKING
4868 if (I_TAG_BINDING (b->id) != b) abort ();
4870 I_TAG_BINDING (b->id) = b->shadowed;
4873 /* Warn about any struct, union or enum tags defined in a
4874 parameter list. The scope of such types is limited to
4875 the parameter list, which is rarely if ever desirable
4876 (it's impossible to call such a function with type-
4877 correct arguments). An anonymous union parm type is
4878 meaningful as a GNU extension, so don't warn for that. */
4879 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4882 /* The %s will be one of 'struct', 'union', or 'enum'. */
4883 warning ("'%s %E' declared inside parameter list",
4886 /* The %s will be one of 'struct', 'union', or 'enum'. */
4887 warning ("anonymous %s declared inside parameter list",
4890 if (! explained_incomplete_types)
4892 warning ("its scope is only this definition or declaration,"
4893 " which is probably not what you want");
4894 explained_incomplete_types = true;
4898 tags = tree_cons (b->id, decl, tags);
4903 /* CONST_DECLs appear here when we have an embedded enum,
4904 and TYPE_DECLs appear here when we have an embedded struct
4905 or union. No warnings for this - we already warned about the
4907 TREE_CHAIN (decl) = others;
4912 /* error_mark_node appears here when we have an undeclared
4913 variable. Just throw it away. */
4916 #ifdef ENABLE_CHECKING
4917 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4919 I_SYMBOL_BINDING (b->id) = b->shadowed;
4923 /* Other things that might be encountered. */
4931 b = free_binding_and_advance (b);
4934 ARG_INFO_PARMS (arg_info) = parms;
4935 ARG_INFO_TAGS (arg_info) = tags;
4936 ARG_INFO_TYPES (arg_info) = types;
4937 ARG_INFO_OTHERS (arg_info) = others;
4941 /* Get the struct, enum or union (CODE says which) with tag NAME.
4942 Define the tag as a forward-reference if it is not defined. */
4945 xref_tag (enum tree_code code, tree name)
4947 /* If a cross reference is requested, look up the type
4948 already defined for this tag and return it. */
4950 tree ref = lookup_tag (code, name, 0);
4951 /* If this is the right type of tag, return what we found.
4952 (This reference will be shadowed by shadow_tag later if appropriate.)
4953 If this is the wrong type of tag, do not return it. If it was the
4954 wrong type in the same scope, we will have had an error
4955 message already; if in a different scope and declaring
4956 a name, pending_xref_error will give an error message; but if in a
4957 different scope and not declaring a name, this tag should
4958 shadow the previous declaration of a different type of tag, and
4959 this would not work properly if we return the reference found.
4960 (For example, with "struct foo" in an outer scope, "union foo;"
4961 must shadow that tag with a new one of union type.) */
4962 if (ref && TREE_CODE (ref) == code)
4965 /* If no such tag is yet defined, create a forward-reference node
4966 and record it as the "definition".
4967 When a real declaration of this type is found,
4968 the forward-reference will be altered into a real type. */
4970 ref = make_node (code);
4971 if (code == ENUMERAL_TYPE)
4973 /* Give the type a default layout like unsigned int
4974 to avoid crashing if it does not get defined. */
4975 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4976 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4977 TYPE_USER_ALIGN (ref) = 0;
4978 TYPE_UNSIGNED (ref) = 1;
4979 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4980 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4981 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4984 pushtag (name, ref);
4989 /* Make sure that the tag NAME is defined *in the current scope*
4990 at least as a forward reference.
4991 CODE says which kind of tag NAME ought to be. */
4994 start_struct (enum tree_code code, tree name)
4996 /* If there is already a tag defined at this scope
4997 (as a forward reference), just return it. */
5002 ref = lookup_tag (code, name, 1);
5003 if (ref && TREE_CODE (ref) == code)
5005 if (TYPE_FIELDS (ref))
5007 if (code == UNION_TYPE)
5008 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5010 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5015 /* Otherwise create a forward-reference just so the tag is in scope. */
5017 ref = make_node (code);
5018 pushtag (name, ref);
5021 C_TYPE_BEING_DEFINED (ref) = 1;
5022 TYPE_PACKED (ref) = flag_pack_struct;
5026 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5027 of a structure component, returning a FIELD_DECL node.
5028 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5030 This is done during the parsing of the struct declaration.
5031 The FIELD_DECL nodes are chained together and the lot of them
5032 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5035 grokfield (tree declarator, tree declspecs, tree width)
5039 if (declarator == NULL_TREE && width == NULL_TREE)
5041 /* This is an unnamed decl.
5043 If we have something of the form "union { list } ;" then this
5044 is the anonymous union extension. Similarly for struct.
5046 If this is something of the form "struct foo;", then
5047 If MS extensions are enabled, this is handled as an
5049 Otherwise this is a forward declaration of a structure tag.
5051 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5052 If MS extensions are enabled and foo names a structure, then
5053 again this is an anonymous struct.
5054 Otherwise this is an error.
5056 Oh what a horrid tangled web we weave. I wonder if MS consciously
5057 took this from Plan 9 or if it was an accident of implementation
5058 that took root before someone noticed the bug... */
5060 tree type = TREE_VALUE (declspecs);
5062 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5063 type = TREE_TYPE (type);
5064 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5066 if (flag_ms_extensions)
5069 goto warn_unnamed_field;
5070 else if (TYPE_NAME (type) == NULL)
5073 goto warn_unnamed_field;
5078 warning ("declaration does not declare anything");
5083 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5084 width ? &width : NULL);
5086 finish_decl (value, NULL_TREE, NULL_TREE);
5087 DECL_INITIAL (value) = width;
5092 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5093 the list such that this does not present a problem later. */
5096 detect_field_duplicates (tree fieldlist)
5101 /* First, see if there are more than "a few" fields.
5102 This is trivially true if there are zero or one fields. */
5105 x = TREE_CHAIN (fieldlist);
5111 } while (timeout > 0 && x);
5113 /* If there were "few" fields, avoid the overhead of allocating
5114 a hash table. Instead just do the nested traversal thing. */
5117 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5120 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5121 if (DECL_NAME (y) == DECL_NAME (x))
5123 error ("%Jduplicate member '%D'", x, x);
5124 DECL_NAME (x) = NULL_TREE;
5130 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5133 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5134 if ((y = DECL_NAME (x)) != 0)
5136 slot = htab_find_slot (htab, y, INSERT);
5139 error ("%Jduplicate member '%D'", x, x);
5140 DECL_NAME (x) = NULL_TREE;
5149 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5150 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5151 ATTRIBUTES are attributes to be applied to the structure. */
5154 finish_struct (tree t, tree fieldlist, tree attributes)
5157 bool toplevel = file_scope == current_scope;
5158 int saw_named_field;
5160 /* If this type was previously laid out as a forward reference,
5161 make sure we lay it out again. */
5165 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5169 for (x = fieldlist; x; x = TREE_CHAIN (x))
5170 if (DECL_NAME (x) != 0)
5174 pedwarn ("%s has no %s",
5175 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5176 fieldlist ? _("named members") : _("members"));
5179 /* Install struct as DECL_CONTEXT of each field decl.
5180 Also process specified field sizes, found in the DECL_INITIAL,
5181 storing 0 there after the type has been changed to precision equal
5182 to its width, rather than the precision of the specified standard
5183 type. (Correct layout requires the original type to have been preserved
5186 saw_named_field = 0;
5187 for (x = fieldlist; x; x = TREE_CHAIN (x))
5189 DECL_CONTEXT (x) = t;
5190 DECL_PACKED (x) |= TYPE_PACKED (t);
5192 /* If any field is const, the structure type is pseudo-const. */
5193 if (TREE_READONLY (x))
5194 C_TYPE_FIELDS_READONLY (t) = 1;
5197 /* A field that is pseudo-const makes the structure likewise. */
5198 tree t1 = TREE_TYPE (x);
5199 while (TREE_CODE (t1) == ARRAY_TYPE)
5200 t1 = TREE_TYPE (t1);
5201 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5202 && C_TYPE_FIELDS_READONLY (t1))
5203 C_TYPE_FIELDS_READONLY (t) = 1;
5206 /* Any field that is volatile means variables of this type must be
5207 treated in some ways as volatile. */
5208 if (TREE_THIS_VOLATILE (x))
5209 C_TYPE_FIELDS_VOLATILE (t) = 1;
5211 /* Any field of nominal variable size implies structure is too. */
5212 if (C_DECL_VARIABLE_SIZE (x))
5213 C_TYPE_VARIABLE_SIZE (t) = 1;
5215 /* Detect invalid nested redefinition. */
5216 if (TREE_TYPE (x) == t)
5217 error ("nested redefinition of `%s'",
5218 IDENTIFIER_POINTER (TYPE_NAME (t)));
5220 if (DECL_INITIAL (x))
5222 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5223 DECL_SIZE (x) = bitsize_int (width);
5224 DECL_BIT_FIELD (x) = 1;
5225 SET_DECL_C_BIT_FIELD (x);
5228 /* Detect flexible array member in an invalid context. */
5229 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5230 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5231 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5232 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5234 if (TREE_CODE (t) == UNION_TYPE)
5236 error ("%Jflexible array member in union", x);
5237 TREE_TYPE (x) = error_mark_node;
5239 else if (TREE_CHAIN (x) != NULL_TREE)
5241 error ("%Jflexible array member not at end of struct", x);
5242 TREE_TYPE (x) = error_mark_node;
5244 else if (! saw_named_field)
5246 error ("%Jflexible array member in otherwise empty struct", x);
5247 TREE_TYPE (x) = error_mark_node;
5251 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5252 && flexible_array_type_p (TREE_TYPE (x)))
5253 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5256 saw_named_field = 1;
5259 detect_field_duplicates (fieldlist);
5261 /* Now we have the nearly final fieldlist. Record it,
5262 then lay out the structure or union (including the fields). */
5264 TYPE_FIELDS (t) = fieldlist;
5268 /* Give bit-fields their proper types. */
5270 tree *fieldlistp = &fieldlist;
5272 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5273 && TREE_TYPE (*fieldlistp) != error_mark_node)
5275 unsigned HOST_WIDE_INT width
5276 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5277 tree type = TREE_TYPE (*fieldlistp);
5278 if (width != TYPE_PRECISION (type))
5279 TREE_TYPE (*fieldlistp)
5280 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5281 DECL_INITIAL (*fieldlistp) = 0;
5284 fieldlistp = &TREE_CHAIN (*fieldlistp);
5287 /* Now we have the truly final field list.
5288 Store it in this type and in the variants. */
5290 TYPE_FIELDS (t) = fieldlist;
5292 /* If there are lots of fields, sort so we can look through them fast.
5293 We arbitrarily consider 16 or more elts to be "a lot". */
5298 for (x = fieldlist; x; x = TREE_CHAIN (x))
5300 if (len > 15 || DECL_NAME (x) == NULL)
5308 struct lang_type *space;
5309 struct sorted_fields_type *space2;
5311 len += list_length (x);
5313 /* Use the same allocation policy here that make_node uses, to
5314 ensure that this lives as long as the rest of the struct decl.
5315 All decls in an inline function need to be saved. */
5317 space = ggc_alloc_cleared (sizeof (struct lang_type));
5318 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5322 field_array = &space2->elts[0];
5323 for (x = fieldlist; x; x = TREE_CHAIN (x))
5325 field_array[len++] = x;
5327 /* If there is anonymous struct or union, break out of the loop. */
5328 if (DECL_NAME (x) == NULL)
5331 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5334 TYPE_LANG_SPECIFIC (t) = space;
5335 TYPE_LANG_SPECIFIC (t)->s->len = len;
5336 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5337 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5342 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5344 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5345 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5346 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5347 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5350 /* If this was supposed to be a transparent union, but we can't
5351 make it one, warn and turn off the flag. */
5352 if (TREE_CODE (t) == UNION_TYPE
5353 && TYPE_TRANSPARENT_UNION (t)
5354 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5356 TYPE_TRANSPARENT_UNION (t) = 0;
5357 warning ("union cannot be made transparent");
5360 /* If this structure or union completes the type of any previous
5361 variable declaration, lay it out and output its rtl. */
5362 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5366 tree decl = TREE_VALUE (x);
5367 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5368 layout_array_type (TREE_TYPE (decl));
5369 if (TREE_CODE (decl) != TYPE_DECL)
5371 layout_decl (decl, 0);
5372 if (c_dialect_objc ())
5373 objc_check_decl (decl);
5374 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5379 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5381 /* Finish debugging output for this type. */
5382 rest_of_type_compilation (t, toplevel);
5387 /* Lay out the type T, and its element type, and so on. */
5390 layout_array_type (tree t)
5392 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5393 layout_array_type (TREE_TYPE (t));
5397 /* Begin compiling the definition of an enumeration type.
5398 NAME is its name (or null if anonymous).
5399 Returns the type object, as yet incomplete.
5400 Also records info about it so that build_enumerator
5401 may be used to declare the individual values as they are read. */
5404 start_enum (tree name)
5408 /* If this is the real definition for a previous forward reference,
5409 fill in the contents in the same object that used to be the
5410 forward reference. */
5413 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5415 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5417 enumtype = make_node (ENUMERAL_TYPE);
5418 pushtag (name, enumtype);
5421 C_TYPE_BEING_DEFINED (enumtype) = 1;
5423 if (TYPE_VALUES (enumtype) != 0)
5425 /* This enum is a named one that has been declared already. */
5426 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5428 /* Completely replace its old definition.
5429 The old enumerators remain defined, however. */
5430 TYPE_VALUES (enumtype) = 0;
5433 enum_next_value = integer_zero_node;
5436 if (flag_short_enums)
5437 TYPE_PACKED (enumtype) = 1;
5442 /* After processing and defining all the values of an enumeration type,
5443 install their decls in the enumeration type and finish it off.
5444 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5445 and ATTRIBUTES are the specified attributes.
5446 Returns ENUMTYPE. */
5449 finish_enum (tree enumtype, tree values, tree attributes)
5452 tree minnode = 0, maxnode = 0;
5453 int precision, unsign;
5454 bool toplevel = (file_scope == current_scope);
5455 struct lang_type *lt;
5457 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5459 /* Calculate the maximum value of any enumerator in this type. */
5461 if (values == error_mark_node)
5462 minnode = maxnode = integer_zero_node;
5465 minnode = maxnode = TREE_VALUE (values);
5466 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5468 tree value = TREE_VALUE (pair);
5469 if (tree_int_cst_lt (maxnode, value))
5471 if (tree_int_cst_lt (value, minnode))
5476 /* Construct the final type of this enumeration. It is the same
5477 as one of the integral types - the narrowest one that fits, except
5478 that normally we only go as narrow as int - and signed iff any of
5479 the values are negative. */
5480 unsign = (tree_int_cst_sgn (minnode) >= 0);
5481 precision = MAX (min_precision (minnode, unsign),
5482 min_precision (maxnode, unsign));
5483 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5485 tem = c_common_type_for_size (precision, unsign);
5488 warning ("enumeration values exceed range of largest integer");
5489 tem = long_long_integer_type_node;
5493 tem = unsign ? unsigned_type_node : integer_type_node;
5495 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5496 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5497 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5498 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5499 TYPE_SIZE (enumtype) = 0;
5500 layout_type (enumtype);
5502 if (values != error_mark_node)
5504 /* Change the type of the enumerators to be the enum type. We
5505 need to do this irrespective of the size of the enum, for
5506 proper type checking. Replace the DECL_INITIALs of the
5507 enumerators, and the value slots of the list, with copies
5508 that have the enum type; they cannot be modified in place
5509 because they may be shared (e.g. integer_zero_node) Finally,
5510 change the purpose slots to point to the names of the decls. */
5511 for (pair = values; pair; pair = TREE_CHAIN (pair))
5513 tree enu = TREE_PURPOSE (pair);
5514 tree ini = DECL_INITIAL (enu);
5516 TREE_TYPE (enu) = enumtype;
5518 /* The ISO C Standard mandates enumerators to have type int,
5519 even though the underlying type of an enum type is
5520 unspecified. Here we convert any enumerators that fit in
5521 an int to type int, to avoid promotions to unsigned types
5522 when comparing integers with enumerators that fit in the
5523 int range. When -pedantic is given, build_enumerator()
5524 would have already taken care of those that don't fit. */
5525 if (int_fits_type_p (ini, integer_type_node))
5526 tem = integer_type_node;
5529 ini = convert (tem, ini);
5531 DECL_INITIAL (enu) = ini;
5532 TREE_PURPOSE (pair) = DECL_NAME (enu);
5533 TREE_VALUE (pair) = ini;
5536 TYPE_VALUES (enumtype) = values;
5539 /* Record the min/max values so that we can warn about bit-field
5540 enumerations that are too small for the values. */
5541 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5542 lt->enum_min = minnode;
5543 lt->enum_max = maxnode;
5544 TYPE_LANG_SPECIFIC (enumtype) = lt;
5546 /* Fix up all variant types of this enum type. */
5547 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5549 if (tem == enumtype)
5551 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5552 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5553 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5554 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5555 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5556 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5557 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5558 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5559 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5560 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5561 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5564 /* Finish debugging output for this type. */
5565 rest_of_type_compilation (enumtype, toplevel);
5570 /* Build and install a CONST_DECL for one value of the
5571 current enumeration type (one that was begun with start_enum).
5572 Return a tree-list containing the CONST_DECL and its value.
5573 Assignment of sequential values by default is handled here. */
5576 build_enumerator (tree name, tree value)
5580 /* Validate and default VALUE. */
5582 /* Remove no-op casts from the value. */
5584 STRIP_TYPE_NOPS (value);
5588 /* Don't issue more errors for error_mark_node (i.e. an
5589 undeclared identifier) - just ignore the value expression. */
5590 if (value == error_mark_node)
5592 else if (TREE_CODE (value) != INTEGER_CST)
5594 error ("enumerator value for '%E' is not an integer constant", name);
5599 value = default_conversion (value);
5600 constant_expression_warning (value);
5604 /* Default based on previous value. */
5605 /* It should no longer be possible to have NON_LVALUE_EXPR
5609 value = enum_next_value;
5611 error ("overflow in enumeration values");
5614 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5616 pedwarn ("ISO C restricts enumerator values to range of `int'");
5617 /* XXX This causes -pedantic to change the meaning of the program.
5618 Remove? -zw 2004-03-15 */
5619 value = convert (integer_type_node, value);
5622 /* Set basis for default for next value. */
5623 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5624 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5626 /* Now create a declaration for the enum value name. */
5628 type = TREE_TYPE (value);
5629 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5630 TYPE_PRECISION (integer_type_node)),
5631 (TYPE_PRECISION (type)
5632 >= TYPE_PRECISION (integer_type_node)
5633 && TYPE_UNSIGNED (type)));
5635 decl = build_decl (CONST_DECL, name, type);
5636 DECL_INITIAL (decl) = convert (type, value);
5639 return tree_cons (decl, value, NULL_TREE);
5643 /* Create the FUNCTION_DECL for a function definition.
5644 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5645 the declaration; they describe the function's name and the type it returns,
5646 but twisted together in a fashion that parallels the syntax of C.
5648 This function creates a binding context for the function body
5649 as well as setting up the FUNCTION_DECL in current_function_decl.
5651 Returns 1 on success. If the DECLARATOR is not suitable for a function
5652 (it defines a datum instead), we return 0, which tells
5653 yyparse to report a parse error. */
5656 start_function (tree declspecs, tree declarator, tree attributes)
5658 tree decl1, old_decl;
5661 current_function_returns_value = 0; /* Assume, until we see it does. */
5662 current_function_returns_null = 0;
5663 current_function_returns_abnormally = 0;
5664 warn_about_return_type = 0;
5665 current_extern_inline = 0;
5666 c_switch_stack = NULL;
5668 /* Indicate no valid break/continue context by setting these variables
5669 to some non-null, non-label value. We'll notice and emit the proper
5670 error message in c_finish_bc_stmt. */
5671 c_break_label = c_cont_label = size_zero_node;
5673 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5675 /* If the declarator is not suitable for a function definition,
5676 cause a syntax error. */
5680 decl_attributes (&decl1, attributes, 0);
5682 if (DECL_DECLARED_INLINE_P (decl1)
5683 && DECL_UNINLINABLE (decl1)
5684 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5685 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5687 announce_function (decl1);
5689 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5691 error ("return type is an incomplete type");
5692 /* Make it return void instead. */
5694 = build_function_type (void_type_node,
5695 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5698 if (warn_about_return_type)
5699 pedwarn_c99 ("return type defaults to `int'");
5701 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5702 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5703 DECL_INITIAL (decl1) = error_mark_node;
5705 /* If this definition isn't a prototype and we had a prototype declaration
5706 before, copy the arg type info from that prototype.
5707 But not if what we had before was a builtin function. */
5708 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5709 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5710 && !DECL_BUILT_IN (old_decl)
5711 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5712 TREE_TYPE (TREE_TYPE (old_decl)))
5713 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5715 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5716 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5719 /* Optionally warn of old-fashioned def with no previous prototype. */
5720 if (warn_strict_prototypes
5721 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5722 && C_DECL_ISNT_PROTOTYPE (old_decl))
5723 warning ("function declaration isn't a prototype");
5724 /* Optionally warn of any global def with no previous prototype. */
5725 else if (warn_missing_prototypes
5726 && TREE_PUBLIC (decl1)
5727 && ! MAIN_NAME_P (DECL_NAME (decl1))
5728 && C_DECL_ISNT_PROTOTYPE (old_decl))
5729 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5730 /* Optionally warn of any def with no previous prototype
5731 if the function has already been used. */
5732 else if (warn_missing_prototypes
5733 && old_decl != 0 && TREE_USED (old_decl)
5734 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5735 warning ("%J'%D' was used with no prototype before its definition",
5737 /* Optionally warn of any global def with no previous declaration. */
5738 else if (warn_missing_declarations
5739 && TREE_PUBLIC (decl1)
5741 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5742 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5743 /* Optionally warn of any def with no previous declaration
5744 if the function has already been used. */
5745 else if (warn_missing_declarations
5746 && old_decl != 0 && TREE_USED (old_decl)
5747 && C_DECL_IMPLICIT (old_decl))
5748 warning ("%J`%D' was used with no declaration before its definition",
5751 /* This is a definition, not a reference.
5752 So normally clear DECL_EXTERNAL.
5753 However, `extern inline' acts like a declaration
5754 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5755 DECL_EXTERNAL (decl1) = current_extern_inline;
5757 /* This function exists in static storage.
5758 (This does not mean `static' in the C sense!) */
5759 TREE_STATIC (decl1) = 1;
5761 /* A nested function is not global. */
5762 if (current_function_decl != 0)
5763 TREE_PUBLIC (decl1) = 0;
5765 #ifdef ENABLE_CHECKING
5766 /* This is the earliest point at which we might know the assembler
5767 name of the function. Thus, if it's set before this, die horribly. */
5768 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5772 /* If #pragma weak was used, mark the decl weak now. */
5773 if (current_scope == file_scope)
5774 maybe_apply_pragma_weak (decl1);
5776 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5777 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5782 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5783 != integer_type_node)
5784 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5786 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5787 args = TREE_CHAIN (args))
5789 tree type = args ? TREE_VALUE (args) : 0;
5791 if (type == void_type_node)
5798 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5799 pedwarn ("%Jfirst argument of '%D' should be `int'",
5804 if (TREE_CODE (type) != POINTER_TYPE
5805 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5806 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5808 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5813 if (TREE_CODE (type) != POINTER_TYPE
5814 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5815 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5817 pedwarn ("%Jthird argument of '%D' should probably be "
5818 "'char **'", decl1, decl1);
5823 /* It is intentional that this message does not mention the third
5824 argument because it's only mentioned in an appendix of the
5826 if (argct > 0 && (argct < 2 || argct > 3))
5827 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5829 if (! TREE_PUBLIC (decl1))
5830 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5833 /* Record the decl so that the function name is defined.
5834 If we already have a decl for this name, and it is a FUNCTION_DECL,
5835 use the old decl. */
5837 current_function_decl = pushdecl (decl1);
5840 declare_parm_level ();
5842 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5843 /* Promote the value to int before returning it. */
5844 if (c_promoting_integer_type_p (restype))
5846 /* It retains unsignedness if not really getting wider. */
5847 if (TYPE_UNSIGNED (restype)
5848 && (TYPE_PRECISION (restype)
5849 == TYPE_PRECISION (integer_type_node)))
5850 restype = unsigned_type_node;
5852 restype = integer_type_node;
5854 DECL_RESULT (current_function_decl)
5855 = build_decl (RESULT_DECL, NULL_TREE, restype);
5857 start_fname_decls ();
5862 /* Subroutine of store_parm_decls which handles new-style function
5863 definitions (prototype format). The parms already have decls, so we
5864 need only record them as in effect and complain if any redundant
5865 old-style parm decls were written. */
5867 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5870 tree parms = ARG_INFO_PARMS (arg_info);
5871 tree tags = ARG_INFO_TAGS (arg_info);
5872 tree others = ARG_INFO_OTHERS (arg_info);
5874 if (current_scope->bindings)
5876 error ("%Jold-style parameter declarations in prototyped "
5877 "function definition", fndecl);
5879 /* Get rid of the old-style declarations. */
5883 /* Don't issue this warning for nested functions, and don't issue this
5884 warning if we got here because ARG_INFO_TYPES was error_mark_node
5885 (this happens when a function definition has just an ellipsis in
5886 its parameter list). */
5887 else if (warn_traditional && !in_system_header && !current_function_scope
5888 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5889 warning ("%Jtraditional C rejects ISO C style function definitions",
5892 /* Now make all the parameter declarations visible in the function body.
5893 We can bypass most of the grunt work of pushdecl. */
5894 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5896 DECL_CONTEXT (decl) = current_function_decl;
5897 if (DECL_NAME (decl))
5898 bind (DECL_NAME (decl), decl, current_scope,
5899 /*invisible=*/false, /*nested=*/false);
5901 error ("%Jparameter name omitted", decl);
5904 /* Record the parameter list in the function declaration. */
5905 DECL_ARGUMENTS (fndecl) = parms;
5907 /* Now make all the ancillary declarations visible, likewise. */
5908 for (decl = others; decl; decl = TREE_CHAIN (decl))
5910 DECL_CONTEXT (decl) = current_function_decl;
5911 if (DECL_NAME (decl))
5912 bind (DECL_NAME (decl), decl, current_scope,
5913 /*invisible=*/false, /*nested=*/false);
5916 /* And all the tag declarations. */
5917 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5918 if (TREE_PURPOSE (decl))
5919 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5920 /*invisible=*/false, /*nested=*/false);
5923 /* Subroutine of store_parm_decls which handles old-style function
5924 definitions (separate parameter list and declarations). */
5927 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5929 struct c_binding *b;
5930 tree parm, decl, last;
5931 tree parmids = ARG_INFO_PARMS (arg_info);
5933 /* We use DECL_WEAK as a flag to show which parameters have been
5934 seen already, since it is not used on PARM_DECL. */
5935 #ifdef ENABLE_CHECKING
5936 for (b = current_scope->bindings; b; b = b->prev)
5937 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5941 if (warn_old_style_definition && !in_system_header)
5942 warning ("%Jold-style function definition", fndecl);
5944 /* Match each formal parameter name with its declaration. Save each
5945 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5946 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5948 if (TREE_VALUE (parm) == 0)
5950 error ("%Jparameter name missing from parameter list", fndecl);
5951 TREE_PURPOSE (parm) = 0;
5955 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5956 if (b && B_IN_CURRENT_SCOPE (b))
5959 /* If we got something other than a PARM_DECL it is an error. */
5960 if (TREE_CODE (decl) != PARM_DECL)
5961 error ("%J'%D' declared as a non-parameter", decl, decl);
5962 /* If the declaration is already marked, we have a duplicate
5963 name. Complain and ignore the duplicate. */
5964 else if (DECL_WEAK (decl))
5966 error ("%Jmultiple parameters named '%D'", decl, decl);
5967 TREE_PURPOSE (parm) = 0;
5970 /* If the declaration says "void", complain and turn it into
5972 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5974 error ("%Jparameter '%D' declared with void type", decl, decl);
5975 TREE_TYPE (decl) = integer_type_node;
5976 DECL_ARG_TYPE (decl) = integer_type_node;
5977 layout_decl (decl, 0);
5980 /* If no declaration found, default to int. */
5983 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5984 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5985 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5989 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5990 else if (extra_warnings)
5991 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5994 TREE_PURPOSE (parm) = decl;
5995 DECL_WEAK (decl) = 1;
5998 /* Now examine the parms chain for incomplete declarations
5999 and declarations with no corresponding names. */
6001 for (b = current_scope->bindings; b; b = b->prev)
6004 if (TREE_CODE (parm) != PARM_DECL)
6007 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6009 error ("%Jparameter '%D' has incomplete type", parm, parm);
6010 TREE_TYPE (parm) = error_mark_node;
6013 if (! DECL_WEAK (parm))
6015 error ("%Jdeclaration for parameter '%D' but no such parameter",
6018 /* Pretend the parameter was not missing.
6019 This gets us to a standard state and minimizes
6020 further error messages. */
6021 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6025 /* Chain the declarations together in the order of the list of
6026 names. Store that chain in the function decl, replacing the
6027 list of names. Update the current scope to match. */
6028 DECL_ARGUMENTS (fndecl) = 0;
6030 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6031 if (TREE_PURPOSE (parm))
6033 if (parm && TREE_PURPOSE (parm))
6035 last = TREE_PURPOSE (parm);
6036 DECL_ARGUMENTS (fndecl) = last;
6037 DECL_WEAK (last) = 0;
6039 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6040 if (TREE_PURPOSE (parm))
6042 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6043 last = TREE_PURPOSE (parm);
6044 DECL_WEAK (last) = 0;
6046 TREE_CHAIN (last) = 0;
6049 /* If there was a previous prototype,
6050 set the DECL_ARG_TYPE of each argument according to
6051 the type previously specified, and report any mismatches. */
6053 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6056 for (parm = DECL_ARGUMENTS (fndecl),
6057 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6058 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6059 != void_type_node));
6060 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6062 if (parm == 0 || type == 0
6063 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6065 error ("number of arguments doesn't match prototype");
6066 error ("%Hprototype declaration",
6067 ¤t_function_prototype_locus);
6070 /* Type for passing arg must be consistent with that
6071 declared for the arg. ISO C says we take the unqualified
6072 type for parameters declared with qualified type. */
6073 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6074 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6076 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6077 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6079 /* Adjust argument to match prototype. E.g. a previous
6080 `int foo(float);' prototype causes
6081 `int foo(x) float x; {...}' to be treated like
6082 `int foo(float x) {...}'. This is particularly
6083 useful for argument types like uid_t. */
6084 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6086 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6087 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6088 && TYPE_PRECISION (TREE_TYPE (parm))
6089 < TYPE_PRECISION (integer_type_node))
6090 DECL_ARG_TYPE (parm) = integer_type_node;
6094 pedwarn ("promoted argument '%D' "
6095 "doesn't match prototype", parm);
6096 pedwarn ("%Hprototype declaration",
6097 ¤t_function_prototype_locus);
6102 error ("argument '%D' doesn't match prototype", parm);
6103 error ("%Hprototype declaration",
6104 ¤t_function_prototype_locus);
6108 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6111 /* Otherwise, create a prototype that would match. */
6115 tree actual = 0, last = 0, type;
6117 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6119 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6121 TREE_CHAIN (last) = type;
6126 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6128 TREE_CHAIN (last) = type;
6132 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6133 of the type of this function, but we need to avoid having this
6134 affect the types of other similarly-typed functions, so we must
6135 first force the generation of an identical (but separate) type
6136 node for the relevant function type. The new node we create
6137 will be a variant of the main variant of the original function
6140 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6142 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6146 /* Store the parameter declarations into the current function declaration.
6147 This is called after parsing the parameter declarations, before
6148 digesting the body of the function.
6150 For an old-style definition, construct a prototype out of the old-style
6151 parameter declarations and inject it into the function's type. */
6154 store_parm_decls (void)
6156 tree fndecl = current_function_decl;
6158 /* The argument information block for FNDECL. */
6159 tree arg_info = DECL_ARGUMENTS (fndecl);
6161 /* True if this definition is written with a prototype. Note:
6162 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6163 list in a function definition as equivalent to (void) -- an
6164 empty argument list specifies the function has no parameters,
6165 but only (void) sets up a prototype for future calls. */
6166 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6169 store_parm_decls_newstyle (fndecl, arg_info);
6171 store_parm_decls_oldstyle (fndecl, arg_info);
6173 /* The next call to push_scope will be a function body. */
6175 next_is_function_body = true;
6177 /* Write a record describing this function definition to the prototypes
6178 file (if requested). */
6180 gen_aux_info_record (fndecl, 1, 0, proto);
6182 /* Initialize the RTL code for the function. */
6183 allocate_struct_function (fndecl);
6185 /* Begin the statement tree for this function. */
6186 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6188 /* ??? Insert the contents of the pending sizes list into the function
6189 to be evaluated. This just changes mis-behaviour until assign_parms
6190 phase ordering problems are resolved. */
6193 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6194 add_stmt (TREE_VALUE (t));
6197 /* Even though we're inside a function body, we still don't want to
6198 call expand_expr to calculate the size of a variable-sized array.
6199 We haven't necessarily assigned RTL to all variables yet, so it's
6200 not safe to try to expand expressions involving them. */
6201 cfun->x_dont_save_pending_sizes_p = 1;
6204 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6207 c_finalize (tree fndecl)
6209 struct cgraph_node *cgn;
6211 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6212 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6214 /* ??? Objc emits functions after finalizing the compilation unit.
6215 This should be cleaned up later and this conditional removed. */
6216 if (cgraph_global_info_ready)
6218 c_expand_body (fndecl);
6222 /* Finalize all nested functions now. */
6223 cgn = cgraph_node (fndecl);
6224 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6225 c_finalize (cgn->decl);
6227 cgraph_finalize_function (fndecl, false);
6230 /* Finish up a function declaration and compile that function
6231 all the way to assembler language output. The free the storage
6232 for the function definition.
6234 This is called after parsing the body of the function definition. */
6237 finish_function (void)
6239 tree fndecl = current_function_decl;
6241 if (TREE_CODE (fndecl) == FUNCTION_DECL
6242 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6244 tree args = DECL_ARGUMENTS (fndecl);
6245 for (; args; args = TREE_CHAIN (args))
6247 tree type = TREE_TYPE (args);
6248 if (INTEGRAL_TYPE_P (type)
6249 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6250 DECL_ARG_TYPE (args) = integer_type_node;
6254 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6255 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6257 /* Must mark the RESULT_DECL as being in this function. */
6259 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6260 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6262 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6264 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6265 != integer_type_node)
6267 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6268 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6270 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6275 c_finish_return (integer_zero_node);
6279 /* Tie off the statement tree for this function. */
6280 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6282 finish_fname_decls ();
6284 /* Complain if there's just no return statement. */
6285 if (warn_return_type
6286 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6287 && !current_function_returns_value && !current_function_returns_null
6288 /* Don't complain if we abort. */
6289 && !current_function_returns_abnormally
6290 /* Don't warn for main(). */
6291 && !MAIN_NAME_P (DECL_NAME (fndecl))
6292 /* Or if they didn't actually specify a return type. */
6293 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6294 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6295 inline function, as we might never be compiled separately. */
6296 && DECL_INLINE (fndecl))
6297 warning ("no return statement in function returning non-void");
6299 /* With just -Wextra, complain only if function returns both with
6300 and without a value. */
6302 && current_function_returns_value
6303 && current_function_returns_null)
6304 warning ("this function may return with or without a value");
6306 /* Store the end of the function, so that we get good line number
6307 info for the epilogue. */
6308 cfun->function_end_locus = input_location;
6310 /* If we don't have ctors/dtors sections, and this is a static
6311 constructor or destructor, it must be recorded now. */
6312 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6313 && !targetm.have_ctors_dtors)
6314 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6315 if (DECL_STATIC_DESTRUCTOR (fndecl)
6316 && !targetm.have_ctors_dtors)
6317 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6319 /* Genericize before inlining. Delay genericizing nested functions
6320 until their parent function is genericized. Since finalizing
6321 requires GENERIC, delay that as well. */
6323 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6325 if (!decl_function_context (fndecl))
6327 c_genericize (fndecl);
6328 lower_nested_functions (fndecl);
6329 c_finalize (fndecl);
6333 /* Register this function with cgraph just far enough to get it
6334 added to our parent's nested function list. Handy, since the
6335 C front end doesn't have such a list. */
6336 (void) cgraph_node (fndecl);
6340 /* We're leaving the context of this function, so zap cfun.
6341 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6342 tree_rest_of_compilation. */
6344 current_function_decl = NULL;
6347 /* Generate the RTL for the body of FNDECL. */
6350 c_expand_body (tree fndecl)
6353 if (!DECL_INITIAL (fndecl)
6354 || DECL_INITIAL (fndecl) == error_mark_node)
6357 tree_rest_of_compilation (fndecl, false);
6359 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6360 && targetm.have_ctors_dtors)
6361 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6362 DEFAULT_INIT_PRIORITY);
6363 if (DECL_STATIC_DESTRUCTOR (fndecl)
6364 && targetm.have_ctors_dtors)
6365 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6366 DEFAULT_INIT_PRIORITY);
6369 /* Check the declarations given in a for-loop for satisfying the C99
6372 check_for_loop_decls (void)
6374 struct c_binding *b;
6378 /* If we get here, declarations have been used in a for loop without
6379 the C99 for loop scope. This doesn't make much sense, so don't
6381 error ("'for' loop initial declaration used outside C99 mode");
6384 /* C99 subclause 6.8.5 paragraph 3:
6386 [#3] The declaration part of a for statement shall only
6387 declare identifiers for objects having storage class auto or
6390 It isn't clear whether, in this sentence, "identifiers" binds to
6391 "shall only declare" or to "objects" - that is, whether all identifiers
6392 declared must be identifiers for objects, or whether the restriction
6393 only applies to those that are. (A question on this in comp.std.c
6394 in November 2000 received no answer.) We implement the strictest
6395 interpretation, to avoid creating an extension which later causes
6398 for (b = current_scope->bindings; b; b = b->prev)
6401 tree decl = b->decl;
6406 switch (TREE_CODE (decl))
6409 if (TREE_STATIC (decl))
6410 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6411 "initial declaration", decl, decl);
6412 else if (DECL_EXTERNAL (decl))
6413 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6414 "initial declaration", decl, decl);
6418 error ("'struct %E' declared in 'for' loop initial declaration", id);
6421 error ("'union %E' declared in 'for' loop initial declaration", id);
6424 error ("'enum %E' declared in 'for' loop initial declaration", id);
6427 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6428 "initial declaration", decl, decl);
6433 /* Save and reinitialize the variables
6434 used during compilation of a C function. */
6437 c_push_function_context (struct function *f)
6439 struct language_function *p;
6440 p = ggc_alloc (sizeof (struct language_function));
6443 p->base.x_stmt_tree = c_stmt_tree;
6444 p->x_break_label = c_break_label;
6445 p->x_cont_label = c_cont_label;
6446 p->x_switch_stack = c_switch_stack;
6447 p->returns_value = current_function_returns_value;
6448 p->returns_null = current_function_returns_null;
6449 p->returns_abnormally = current_function_returns_abnormally;
6450 p->warn_about_return_type = warn_about_return_type;
6451 p->extern_inline = current_extern_inline;
6454 /* Restore the variables used during compilation of a C function. */
6457 c_pop_function_context (struct function *f)
6459 struct language_function *p = f->language;
6461 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6462 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6464 /* Stop pointing to the local nodes about to be freed. */
6465 /* But DECL_INITIAL must remain nonzero so we know this
6466 was an actual function definition. */
6467 DECL_INITIAL (current_function_decl) = error_mark_node;
6468 DECL_ARGUMENTS (current_function_decl) = 0;
6471 c_stmt_tree = p->base.x_stmt_tree;
6472 c_break_label = p->x_break_label;
6473 c_cont_label = p->x_cont_label;
6474 c_switch_stack = p->x_switch_stack;
6475 current_function_returns_value = p->returns_value;
6476 current_function_returns_null = p->returns_null;
6477 current_function_returns_abnormally = p->returns_abnormally;
6478 warn_about_return_type = p->warn_about_return_type;
6479 current_extern_inline = p->extern_inline;
6484 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6487 c_dup_lang_specific_decl (tree decl)
6489 struct lang_decl *ld;
6491 if (!DECL_LANG_SPECIFIC (decl))
6494 ld = ggc_alloc (sizeof (struct lang_decl));
6495 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6496 DECL_LANG_SPECIFIC (decl) = ld;
6499 /* The functions below are required for functionality of doing
6500 function at once processing in the C front end. Currently these
6501 functions are not called from anywhere in the C front end, but as
6502 these changes continue, that will change. */
6504 /* Returns nonzero if the current statement is a full expression,
6505 i.e. temporaries created during that statement should be destroyed
6506 at the end of the statement. */
6509 stmts_are_full_exprs_p (void)
6514 /* Returns the stmt_tree (if any) to which statements are currently
6515 being added. If there is no active statement-tree, NULL is
6519 current_stmt_tree (void)
6521 return &c_stmt_tree;
6524 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6528 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6533 /* Dummy function in place of callback used by C++. */
6536 extract_interface_info (void)
6540 /* Return the global value of T as a symbol. */
6543 identifier_global_value (tree t)
6545 struct c_binding *b;
6547 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6548 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6554 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6555 otherwise the name is found in ridpointers from RID_INDEX. */
6558 record_builtin_type (enum rid rid_index, const char *name, tree type)
6562 id = ridpointers[(int) rid_index];
6564 id = get_identifier (name);
6565 pushdecl (build_decl (TYPE_DECL, id, type));
6568 /* Build the void_list_node (void_type_node having been created). */
6570 build_void_list_node (void)
6572 tree t = build_tree_list (NULL_TREE, void_type_node);
6576 /* Return something to represent absolute declarators containing a *.
6577 TARGET is the absolute declarator that the * contains.
6578 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6579 to apply to the pointer type, represented as identifiers, possible mixed
6582 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6583 if attributes are present) and whose type is the modifier list. */
6586 make_pointer_declarator (tree type_quals_attrs, tree target)
6589 tree itarget = target;
6590 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6591 if (attrs != NULL_TREE)
6592 itarget = tree_cons (attrs, target, NULL_TREE);
6593 return build1 (INDIRECT_REF, quals, itarget);
6596 /* Synthesize a function which calls all the global ctors or global
6597 dtors in this file. This is only used for targets which do not
6598 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6600 build_cdtor (int method_type, tree cdtors)
6607 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6608 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6611 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
6614 /* Perform final processing on one file scope's declarations (or the
6615 external scope's declarations), GLOBALS. */
6617 c_write_global_declarations_1 (tree globals)
6619 size_t len = list_length (globals);
6620 tree *vec = xmalloc (sizeof (tree) * len);
6624 /* Process the decls in the order they were written. */
6625 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6628 wrapup_global_declarations (vec, len);
6629 check_global_declarations (vec, len);
6635 c_write_global_declarations (void)
6639 /* We don't want to do this if generating a PCH. */
6643 /* Don't waste time on further processing if -fsyntax-only or we've
6644 encountered errors. */
6645 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
6648 /* Close the external scope. */
6649 ext_block = pop_scope ();
6654 /* Process all file scopes in this compilation, and the external_scope,
6655 through wrapup_global_declarations and check_global_declarations. */
6656 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6657 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6658 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6660 /* Generate functions to call static constructors and destructors
6661 for targets that do not support .ctors/.dtors sections. These
6662 functions have magic names which are detected by collect2. */
6663 build_cdtor ('I', static_ctors); static_ctors = 0;
6664 build_cdtor ('D', static_dtors); static_dtors = 0;
6666 /* We're done parsing; proceed to optimize and emit assembly.
6667 FIXME: shouldn't be the front end's responsibility to call this. */
6670 /* Presently this has to happen after cgraph_optimize.
6671 FIXME: shouldn't be the front end's responsibility to call this. */
6673 mudflap_finish_file ();
6676 #include "gt-c-decl.h"