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. */
75 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* While defining an enum type, this is 1 plus the last enumerator
80 constant value. Note that will do not have to save this or `enum_overflow'
81 around nested function definition since such a definition could only
82 occur in an enum value expression and we don't use these variables in
85 static tree enum_next_value;
87 /* Nonzero means that there was overflow computing enum_next_value. */
89 static int enum_overflow;
91 /* These #defines are for clarity in working with the information block
92 returned by get_parm_info. */
93 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
94 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
95 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
96 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
98 /* The file and line that the prototype came from if this is an
99 old-style definition; used for diagnostics in
100 store_parm_decls_oldstyle. */
102 static location_t current_function_prototype_locus;
104 /* The current statement tree. */
106 static GTY(()) struct stmt_tree_s c_stmt_tree;
108 /* The current scope statement stack. */
110 static GTY(()) tree c_scope_stmt_stack;
112 /* State saving variables. */
113 int c_in_iteration_stmt;
116 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
117 included in this invocation. Note that the current translation
118 unit is not included in this list. */
120 static GTY(()) tree all_translation_units;
122 /* A list of decls to be made automatically visible in each file scope. */
123 static GTY(()) tree visible_builtins;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement that specifies a return value is seen. */
128 int current_function_returns_value;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement with no argument is seen. */
133 int current_function_returns_null;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a call to a noreturn function is seen. */
138 int current_function_returns_abnormally;
140 /* Set to nonzero by `grokdeclarator' for a function
141 whose return type is defaulted, if warnings for this are desired. */
143 static int warn_about_return_type;
145 /* Nonzero when starting a function declared `extern inline'. */
147 static int current_extern_inline;
149 /* True means global_bindings_p should return false even if the scope stack
150 says we are in file scope. */
151 bool c_override_global_bindings_to_false;
154 /* Each c_binding structure describes one binding of an identifier to
155 a decl. All the decls in a scope - irrespective of namespace - are
156 chained together by the ->prev field, which (as the name implies)
157 runs in reverse order. All the decls in a given namespace bound to
158 a given identifier are chained by the ->shadowed field, which runs
159 from inner to outer scopes. Finally, the ->contour field points
160 back to the relevant scope structure; this is mainly used to make
161 decls in the externals scope invisible (see below).
163 The ->decl field usually points to a DECL node, but there are two
164 exceptions. In the namespace of type tags, the bound entity is a
165 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
166 identifier is encountered, it is bound to error_mark_node to
167 suppress further errors about that identifier in the current
170 struct c_binding GTY((chain_next ("%h.prev")))
172 tree decl; /* the decl bound */
173 tree id; /* the identifier it's bound to */
174 struct c_binding *prev; /* the previous decl in this scope */
175 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
176 struct c_scope *contour; /* the scope in which this decl is bound */
179 #define I_SYMBOL_BINDING(node) \
180 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
181 #define I_SYMBOL_DECL(node) \
182 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
184 #define I_TAG_BINDING(node) \
185 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
186 #define I_TAG_DECL(node) \
187 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
189 #define I_LABEL_BINDING(node) \
190 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
191 #define I_LABEL_DECL(node) \
192 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
194 /* Each C symbol points to three linked lists of c_binding structures.
195 These describe the values of the identifier in the three different
196 namespaces defined by the language. */
198 struct lang_identifier GTY(())
200 struct c_common_identifier common_id;
201 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
202 struct c_binding *tag_binding; /* struct/union/enum tags */
203 struct c_binding *label_binding; /* labels */
206 /* Validate c-lang.c's assumptions. */
207 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
208 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
210 /* The resulting tree type. */
213 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
214 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)")))
216 union tree_node GTY ((tag ("0"),
217 desc ("tree_node_structure (&%h)")))
219 struct lang_identifier GTY ((tag ("1"))) identifier;
222 /* Each c_scope structure describes the complete contents of one
223 scope. Four scopes are distinguished specially: the innermost or
224 current scope, the innermost function scope, the file scope (always
225 the second to outermost) and the outermost or external scope.
227 Most declarations are recorded in the current scope.
229 All normal label declarations are recorded in the innermost
230 function scope, as are bindings of undeclared identifiers to
231 error_mark_node. (GCC permits nested functions as an extension,
232 hence the 'innermost' qualifier.) Explicitly declared labels
233 (using the __label__ extension) appear in the current scope.
235 Being in the file scope (current_scope == file_scope) causes
236 special behavior in several places below. Also, under some
237 conditions the Objective-C front end records declarations in the
238 file scope even though that isn't the current scope.
240 All declarations with external linkage are recorded in the external
241 scope, even if they aren't visible there; this models the fact that
242 such declarations are visible to the entire program, and (with a
243 bit of cleverness, see pushdecl) allows diagnosis of some violations
244 of C99 6.2.2p7 and 6.2.7p2:
246 If, within the same translation unit, the same identifier appears
247 with both internal and external linkage, the behavior is
250 All declarations that refer to the same object or function shall
251 have compatible type; otherwise, the behavior is undefined.
253 Initially only the built-in declarations, which describe compiler
254 intrinsic functions plus a subset of the standard library, are in
257 The order of the blocks list matters, and it is frequently appended
258 to. To avoid having to walk all the way to the end of the list on
259 each insertion, or reverse the list later, we maintain a pointer to
260 the last list entry. (FIXME: It should be feasible to use a reversed
263 The bindings list is strictly in reverse order of declarations;
264 pop_scope relies on this. */
267 struct c_scope GTY((chain_next ("%h.outer")))
269 /* The scope containing this one. */
270 struct c_scope *outer;
272 /* The next outermost function scope. */
273 struct c_scope *outer_function;
275 /* All bindings in this scope. */
276 struct c_binding *bindings;
278 /* For each scope (except the global one), a chain of BLOCK nodes
279 for all the scopes that were entered and exited one level down. */
283 /* The depth of this scope. Used to keep the ->shadowed chain of
284 bindings sorted innermost to outermost. */
285 unsigned int depth : 28;
287 /* True if we are currently filling this scope with parameter
289 BOOL_BITFIELD parm_flag : 1;
291 /* True if we already complained about forward parameter decls
292 in this scope. This prevents double warnings on
293 foo (int a; int b; ...) */
294 BOOL_BITFIELD warned_forward_parm_decls : 1;
296 /* True if this is the outermost block scope of a function body.
297 This scope contains the parameters, the local variables declared
298 in the outermost block, and all the labels (except those in
299 nested functions, or declared at block scope with __label__). */
300 BOOL_BITFIELD function_body : 1;
302 /* True means make a BLOCK for this scope no matter what. */
303 BOOL_BITFIELD keep : 1;
306 /* The scope currently in effect. */
308 static GTY(()) struct c_scope *current_scope;
310 /* The innermost function scope. Ordinary (not explicitly declared)
311 labels, bindings to error_mark_node, and the lazily-created
312 bindings of __func__ and its friends get this scope. */
314 static GTY(()) struct c_scope *current_function_scope;
316 /* The C file scope. This is reset for each input translation unit. */
318 static GTY(()) struct c_scope *file_scope;
320 /* The outermost scope. This is used for all declarations with
321 external linkage, and only these, hence the name. */
323 static GTY(()) struct c_scope *external_scope;
325 /* A chain of c_scope structures awaiting reuse. */
327 static GTY((deletable)) struct c_scope *scope_freelist;
329 /* A chain of c_binding structures awaiting reuse. */
331 static GTY((deletable)) struct c_binding *binding_freelist;
333 /* Append VAR to LIST in scope SCOPE. */
334 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
335 struct c_scope *s_ = (scope); \
337 if (s_->list##_last) \
338 TREE_CHAIN (s_->list##_last) = d_; \
341 s_->list##_last = d_; \
344 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
345 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
346 struct c_scope *t_ = (tscope); \
347 struct c_scope *f_ = (fscope); \
349 TREE_CHAIN (t_->to##_last) = f_->from; \
352 t_->to##_last = f_->from##_last; \
355 /* True means unconditionally make a BLOCK for the next scope pushed. */
357 static bool keep_next_level_flag;
359 /* True means the next call to push_scope will be the outermost scope
360 of a function body, so do not push a new scope, merely cease
361 expecting parameter decls. */
363 static bool next_is_function_body;
365 /* Functions called automatically at the beginning and end of execution. */
367 tree static_ctors, static_dtors;
369 /* Forward declarations. */
370 static tree lookup_name_in_scope (tree, struct c_scope *);
371 static tree c_make_fname_decl (tree, int);
372 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
373 static tree grokparms (tree, int);
374 static void layout_array_type (tree);
376 /* States indicating how grokdeclarator() should handle declspecs marked
377 with __attribute__((deprecated)). An object declared as
378 __attribute__((deprecated)) suppresses warnings of uses of other
381 enum deprecated_states {
386 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
389 c_print_identifier (FILE *file, tree node, int indent)
391 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
392 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
393 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
394 if (C_IS_RESERVED_WORD (node))
396 tree rid = ridpointers[C_RID_CODE (node)];
397 indent_to (file, indent + 4);
398 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
399 (void *) rid, IDENTIFIER_POINTER (rid));
403 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
404 which may be any of several kinds of DECL or TYPE or error_mark_node,
405 in the scope SCOPE. */
407 bind (tree name, tree decl, struct c_scope *scope)
409 struct c_binding *b, **here;
411 if (binding_freelist)
413 b = binding_freelist;
414 binding_freelist = b->prev;
417 b = ggc_alloc (sizeof (struct c_binding));
424 b->prev = scope->bindings;
430 switch (TREE_CODE (decl))
432 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
435 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
441 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
447 /* Locate the appropriate place in the chain of shadowed decls
448 to insert this binding. Normally, scope == current_scope and
449 this does nothing. */
450 while (*here && (*here)->contour->depth > scope->depth)
451 here = &(*here)->shadowed;
457 /* Clear the binding structure B, stick it on the binding_freelist,
458 and return the former value of b->prev. This is used by pop_scope
459 and get_parm_info to iterate destructively over all the bindings
460 from a given scope. */
461 static struct c_binding *
462 free_binding_and_advance (struct c_binding *b)
464 struct c_binding *prev = b->prev;
470 b->prev = binding_freelist;
471 binding_freelist = b;
477 /* Hook called at end of compilation to assume 1 elt
478 for a file-scope tentative array defn that wasn't complete before. */
481 c_finish_incomplete_decl (tree decl)
483 if (TREE_CODE (decl) == VAR_DECL)
485 tree type = TREE_TYPE (decl);
486 if (type != error_mark_node
487 && TREE_CODE (type) == ARRAY_TYPE
488 && ! DECL_EXTERNAL (decl)
489 && TYPE_DOMAIN (type) == 0)
491 warning ("%Jarray '%D' assumed to have one element", decl, decl);
493 complete_array_type (type, NULL_TREE, 1);
495 layout_decl (decl, 0);
500 /* The Objective-C front-end often needs to determine the current scope. */
503 get_current_scope (void)
505 return current_scope;
508 /* The following function is used only by Objective-C. It needs to live here
509 because it accesses the innards of c_scope. */
512 objc_mark_locals_volatile (void *enclosing_blk)
514 struct c_scope *scope;
517 for (scope = current_scope;
518 scope && scope != enclosing_blk;
519 scope = scope->outer)
521 for (b = scope->bindings; b; b = b->prev)
523 if (TREE_CODE (b->decl) == VAR_DECL
524 || TREE_CODE (b->decl) == PARM_DECL)
526 C_DECL_REGISTER (b->decl) = 0;
527 DECL_REGISTER (b->decl) = 0;
528 TREE_THIS_VOLATILE (b->decl) = 1;
532 /* Do not climb up past the current function. */
533 if (scope->function_body)
538 /* Nonzero if we are currently in file scope. */
541 global_bindings_p (void)
543 return current_scope == file_scope && !c_override_global_bindings_to_false;
547 keep_next_level (void)
549 keep_next_level_flag = true;
552 /* Identify this scope as currently being filled with parameters. */
555 declare_parm_level (void)
557 current_scope->parm_flag = true;
563 if (next_is_function_body)
565 /* This is the transition from the parameters to the top level
566 of the function body. These are the same scope
567 (C99 6.2.1p4,6) so we do not push another scope structure.
568 next_is_function_body is set only by store_parm_decls, which
569 in turn is called when and only when we are about to
570 encounter the opening curly brace for the function body.
572 The outermost block of a function always gets a BLOCK node,
573 because the debugging output routines expect that each
574 function has at least one BLOCK. */
575 current_scope->parm_flag = false;
576 current_scope->function_body = true;
577 current_scope->keep = true;
578 current_scope->outer_function = current_function_scope;
579 current_function_scope = current_scope;
581 keep_next_level_flag = false;
582 next_is_function_body = false;
586 struct c_scope *scope;
589 scope = scope_freelist;
590 scope_freelist = scope->outer;
593 scope = ggc_alloc_cleared (sizeof (struct c_scope));
595 scope->keep = keep_next_level_flag;
596 scope->outer = current_scope;
597 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
599 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
601 if (current_scope && scope->depth == 0)
604 sorry ("GCC supports only %u nested scopes\n", scope->depth);
607 current_scope = scope;
608 keep_next_level_flag = false;
612 /* Exit a scope. Restore the state of the identifier-decl mappings
613 that were in effect when this scope was entered. Return a BLOCK
614 node containing all the DECLs in this scope that are of interest
615 to debug info generation. */
620 struct c_scope *scope = current_scope;
621 tree block, context, p;
624 bool functionbody = scope->function_body;
625 bool keep = functionbody || scope->keep || scope->bindings;
627 /* If appropriate, create a BLOCK to record the decls for the life
632 block = make_node (BLOCK);
633 BLOCK_SUBBLOCKS (block) = scope->blocks;
634 TREE_USED (block) = 1;
636 /* In each subblock, record that this is its superior. */
637 for (p = scope->blocks; p; p = TREE_CHAIN (p))
638 BLOCK_SUPERCONTEXT (p) = block;
640 BLOCK_VARS (block) = 0;
643 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
644 scope must be set so that they point to the appropriate
645 construct, i.e. either to the current FUNCTION_DECL node, or
646 else to the BLOCK node we just constructed.
648 Note that for tagged types whose scope is just the formal
649 parameter list for some function type specification, we can't
650 properly set their TYPE_CONTEXTs here, because we don't have a
651 pointer to the appropriate FUNCTION_TYPE node readily available
652 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
653 type nodes get set in `grokdeclarator' as soon as we have created
654 the FUNCTION_TYPE node which will represent the "scope" for these
655 "parameter list local" tagged types. */
656 if (scope->function_body)
657 context = current_function_decl;
658 else if (scope == file_scope)
660 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
661 TREE_CHAIN (file_decl) = all_translation_units;
662 all_translation_units = file_decl;
668 /* Clear all bindings in this scope. */
669 for (b = scope->bindings; b; b = free_binding_and_advance (b))
672 switch (TREE_CODE (p))
675 /* Warnings for unused labels, errors for undefined labels. */
676 if (TREE_USED (p) && !DECL_INITIAL (p))
678 error ("%Jlabel `%D' used but not defined", p, p);
679 DECL_INITIAL (p) = error_mark_node;
681 else if (!TREE_USED (p) && warn_unused_label)
683 if (DECL_INITIAL (p))
684 warning ("%Jlabel `%D' defined but not used", p, p);
686 warning ("%Jlabel `%D' declared but not defined", p, p);
688 /* Labels go in BLOCK_VARS. */
689 TREE_CHAIN (p) = BLOCK_VARS (block);
690 BLOCK_VARS (block) = p;
692 #ifdef ENABLE_CHECKING
693 if (I_LABEL_BINDING (b->id) != b) abort ();
695 I_LABEL_BINDING (b->id) = b->shadowed;
701 TYPE_CONTEXT (p) = context;
703 /* Types may not have tag-names, in which case the type
704 appears in the bindings list with b->id NULL. */
707 #ifdef ENABLE_CHECKING
708 if (I_TAG_BINDING (b->id) != b) abort ();
710 I_TAG_BINDING (b->id) = b->shadowed;
715 /* Propagate TREE_ADDRESSABLE from nested functions to their
716 containing functions. */
717 if (! TREE_ASM_WRITTEN (p)
718 && DECL_INITIAL (p) != 0
719 && TREE_ADDRESSABLE (p)
720 && DECL_ABSTRACT_ORIGIN (p) != 0
721 && DECL_ABSTRACT_ORIGIN (p) != p)
722 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
726 /* Warnings for unused variables. Keep this in sync with
727 stmt.c:warn_about_unused_variables, which we cannot use
728 since it expects a different data structure. */
729 if (warn_unused_variable
731 && !DECL_IN_SYSTEM_HEADER (p)
733 && !DECL_ARTIFICIAL (p)
734 && (scope != file_scope
735 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
736 && !TREE_THIS_VOLATILE (p)))
737 && scope != external_scope)
738 warning ("%Junused variable `%D'", p, p);
744 /* All of these go in BLOCK_VARS, but only if this is the
745 binding in the home scope. */
746 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
751 /* If this is the file scope, must set DECL_CONTEXT on these. */
752 if (!C_DECL_IN_EXTERNAL_SCOPE (p) && scope == file_scope)
753 DECL_CONTEXT (p) = context;
756 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
757 already been put there by store_parm_decls. Unused-
758 parameter warnings are handled by function.c.
759 error_mark_node obviously does not go in BLOCK_VARS and
760 does not get unused-variable warnings. */
763 /* It is possible for a decl not to have a name. We get
764 here with b->id NULL in this case. */
767 #ifdef ENABLE_CHECKING
768 if (I_SYMBOL_BINDING (b->id) != b) abort ();
770 I_SYMBOL_BINDING (b->id) = b->shadowed;
780 /* Dispose of the block that we just made inside some higher level. */
781 if ((scope->function_body || scope == file_scope) && context)
783 DECL_INITIAL (context) = block;
784 BLOCK_SUPERCONTEXT (block) = context;
786 else if (scope->outer)
789 SCOPE_LIST_APPEND (scope->outer, blocks, block);
790 /* If we did not make a block for the scope just exited, any
791 blocks made for inner scopes must be carried forward so they
792 will later become subblocks of something else. */
793 else if (scope->blocks)
794 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
797 /* Pop the current scope, and free the structure for reuse. */
798 current_scope = scope->outer;
799 if (scope->function_body)
800 current_function_scope = scope->outer_function;
802 memset (scope, 0, sizeof (struct c_scope));
803 scope->outer = scope_freelist;
804 scope_freelist = scope;
810 push_file_scope (void)
815 file_scope = current_scope;
817 start_fname_decls ();
819 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
820 bind (DECL_NAME (decl), decl, file_scope);
824 pop_file_scope (void)
826 /* In case there were missing closebraces, get us back to the global
828 while (current_scope != file_scope)
831 /* __FUNCTION__ is defined at file scope (""). This
832 call may not be necessary as my tests indicate it
833 still works without it. */
834 finish_fname_decls ();
836 /* Kludge: don't actually pop the file scope if generating a
837 precompiled header, so that macros and local symbols are still
838 visible to the PCH generator. */
842 /* And pop off the file scope. */
846 cpp_undef_all (parse_in);
849 /* Insert BLOCK at the end of the list of subblocks of the current
850 scope. This is used when a BIND_EXPR is expanded, to handle the
851 BLOCK node inside the BIND_EXPR. */
854 insert_block (tree block)
856 TREE_USED (block) = 1;
857 SCOPE_LIST_APPEND (current_scope, blocks, block);
860 /* Push a definition or a declaration of struct, union or enum tag "name".
861 "type" should be the type node.
862 We assume that the tag "name" is not already defined.
864 Note that the definition may really be just a forward reference.
865 In that case, the TYPE_SIZE will be zero. */
868 pushtag (tree name, tree type)
870 /* Record the identifier as the type's name if it has none. */
871 if (name && !TYPE_NAME (type))
872 TYPE_NAME (type) = name;
873 bind (name, type, current_scope);
875 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
876 tagged type we just added to the current scope. This fake
877 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
878 to output a representation of a tagged type, and it also gives
879 us a convenient place to record the "scope start" address for the
882 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
884 /* An approximation for now, so we can tell this is a function-scope tag.
885 This will be updated in pop_scope. */
886 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
889 /* Subroutine of compare_decls. Allow harmless mismatches in return
890 and argument types provided that the type modes match. This function
891 return a unified type given a suitable match, and 0 otherwise. */
894 match_builtin_function_types (tree newtype, tree oldtype)
896 tree newrettype, oldrettype;
897 tree newargs, oldargs;
898 tree trytype, tryargs;
900 /* Accept the return type of the new declaration if same modes. */
901 oldrettype = TREE_TYPE (oldtype);
902 newrettype = TREE_TYPE (newtype);
904 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
907 oldargs = TYPE_ARG_TYPES (oldtype);
908 newargs = TYPE_ARG_TYPES (newtype);
911 while (oldargs || newargs)
915 || ! TREE_VALUE (oldargs)
916 || ! TREE_VALUE (newargs)
917 || TYPE_MODE (TREE_VALUE (oldargs))
918 != TYPE_MODE (TREE_VALUE (newargs)))
921 oldargs = TREE_CHAIN (oldargs);
922 newargs = TREE_CHAIN (newargs);
925 trytype = build_function_type (newrettype, tryargs);
926 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
929 /* Subroutine of diagnose_mismatched_decls. Check for function type
930 mismatch involving an empty arglist vs a nonempty one and give clearer
933 diagnose_arglist_conflict (tree newdecl, tree olddecl,
934 tree newtype, tree oldtype)
938 if (TREE_CODE (olddecl) != FUNCTION_DECL
939 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
940 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
942 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
945 t = TYPE_ARG_TYPES (oldtype);
947 t = TYPE_ARG_TYPES (newtype);
948 for (; t; t = TREE_CHAIN (t))
950 tree type = TREE_VALUE (t);
952 if (TREE_CHAIN (t) == 0
953 && TYPE_MAIN_VARIANT (type) != void_type_node)
955 inform ("a parameter list with an ellipsis can't match "
956 "an empty parameter name list declaration");
960 if (c_type_promotes_to (type) != type)
962 inform ("an argument type that has a default promotion can't match "
963 "an empty parameter name list declaration");
969 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
970 old-style function definition, NEWDECL is a prototype declaration.
971 Diagnose inconsistencies in the argument list. Returns TRUE if
972 the prototype is compatible, FALSE if not. */
974 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
976 tree newargs, oldargs;
979 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
980 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
982 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
983 newargs = TYPE_ARG_TYPES (newtype);
988 tree oldargtype = TREE_VALUE (oldargs);
989 tree newargtype = TREE_VALUE (newargs);
991 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
994 /* Reaching the end of just one list means the two decls don't
995 agree on the number of arguments. */
996 if (END_OF_ARGLIST (oldargtype))
998 error ("%Jprototype for '%D' declares more arguments "
999 "than previous old-style definition", newdecl, newdecl);
1002 else if (END_OF_ARGLIST (newargtype))
1004 error ("%Jprototype for '%D' declares fewer arguments "
1005 "than previous old-style definition", newdecl, newdecl);
1009 /* Type for passing arg must be consistent with that declared
1011 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
1013 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1014 newdecl, newdecl, i);
1018 oldargs = TREE_CHAIN (oldargs);
1019 newargs = TREE_CHAIN (newargs);
1023 /* If we get here, no errors were found, but do issue a warning
1024 for this poor-style construct. */
1025 warning ("%Jprototype for '%D' follows non-prototype definition",
1028 #undef END_OF_ARGLIST
1031 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1032 first in a pair of mismatched declarations, using the diagnostic
1035 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1037 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1039 else if (DECL_INITIAL (decl))
1040 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1041 else if (C_DECL_IMPLICIT (decl))
1042 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1044 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1047 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1048 Returns true if the caller should proceed to merge the two, false
1049 if OLDDECL should simply be discarded. As a side effect, issues
1050 all necessary diagnostics for invalid or poor-style combinations.
1051 If it returns true, writes the types of NEWDECL and OLDDECL to
1052 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1053 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1056 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1057 tree *newtypep, tree *oldtypep)
1059 tree newtype, oldtype;
1060 bool pedwarned = false;
1061 bool warned = false;
1063 /* If we have error_mark_node for either decl or type, just discard
1064 the previous decl - we're in an error cascade already. */
1065 if (olddecl == error_mark_node || newdecl == error_mark_node)
1067 *oldtypep = oldtype = TREE_TYPE (olddecl);
1068 *newtypep = newtype = TREE_TYPE (newdecl);
1069 if (oldtype == error_mark_node || newtype == error_mark_node)
1072 /* Two different categories of symbol altogether. This is an error
1073 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1074 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1076 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1077 && DECL_BUILT_IN (olddecl)
1078 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1080 error ("%J'%D' redeclared as different kind of symbol",
1082 locate_old_decl (olddecl, error);
1084 else if (TREE_PUBLIC (newdecl))
1085 warning ("%Jbuilt-in function '%D' declared as non-function",
1087 else if (warn_shadow)
1088 warning ("%Jdeclaration of '%D' shadows a built-in function",
1093 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1095 if (TREE_CODE (olddecl) == FUNCTION_DECL
1096 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1098 /* Accept harmless mismatch in function types.
1099 This is for the ffs and fprintf builtins. */
1100 tree trytype = match_builtin_function_types (newtype, oldtype);
1102 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1103 *oldtypep = oldtype = trytype;
1106 /* If types don't match for a built-in, throw away the
1107 built-in. No point in calling locate_old_decl here, it
1108 won't print anything. */
1109 warning ("%Jconflicting types for built-in function '%D'",
1114 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1115 && DECL_SOURCE_LINE (olddecl) == 0)
1117 /* A conflicting function declaration for a predeclared
1118 function that isn't actually built in. Objective C uses
1119 these. The new declaration silently overrides everything
1120 but the volatility (i.e. noreturn) indication. See also
1121 below. FIXME: Make Objective C use normal builtins. */
1122 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1125 /* Permit void foo (...) to match int foo (...) if the latter is
1126 the definition and implicit int was used. See
1127 c-torture/compile/920625-2.c. */
1128 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1129 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1130 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1131 && C_FUNCTION_IMPLICIT_INT (newdecl))
1133 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1134 /* Make sure we keep void as the return type. */
1135 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1136 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1141 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1142 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1143 locate_old_decl (olddecl, error);
1148 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1149 but silently ignore the redeclaration if either is in a system
1150 header. (Conflicting redeclarations were handled above.) */
1151 if (TREE_CODE (newdecl) == TYPE_DECL)
1153 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1154 return true; /* Allow OLDDECL to continue in use. */
1156 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1157 locate_old_decl (olddecl, error);
1161 /* Function declarations can either be 'static' or 'extern' (no
1162 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1163 can never conflict with each other on account of linkage (6.2.2p4).
1164 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1165 two definitions if one is 'extern inline' and one is not. The non-
1166 extern-inline definition supersedes the extern-inline definition. */
1167 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1169 /* If you declare a built-in function name as static, or
1170 define the built-in with an old-style definition (so we
1171 can't validate the argument list) the built-in definition is
1172 overridden, but optionally warn this was a bad choice of name. */
1173 if (DECL_BUILT_IN (olddecl)
1174 && !C_DECL_DECLARED_BUILTIN (olddecl)
1175 && (!TREE_PUBLIC (newdecl)
1176 || (DECL_INITIAL (newdecl)
1177 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1180 warning ("%Jdeclaration of '%D' shadows a built-in function",
1182 /* Discard the old built-in function. */
1186 if (DECL_INITIAL (newdecl))
1188 if (DECL_INITIAL (olddecl)
1189 && !(DECL_DECLARED_INLINE_P (olddecl)
1190 && DECL_EXTERNAL (olddecl)
1191 && !(DECL_DECLARED_INLINE_P (newdecl)
1192 && DECL_EXTERNAL (newdecl))))
1194 error ("%Jredefinition of '%D'", newdecl, newdecl);
1195 locate_old_decl (olddecl, error);
1199 /* If we have a prototype after an old-style function definition,
1200 the argument types must be checked specially. */
1201 else if (DECL_INITIAL (olddecl)
1202 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1203 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1204 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1206 locate_old_decl (olddecl, error);
1209 /* Mismatched non-static and static is considered poor style.
1210 We only diagnose static then non-static if -Wtraditional,
1211 because it is the most convenient way to get some effects
1212 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1213 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1214 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1216 /* A static function declaration for a predeclared function
1217 that isn't actually built in, silently overrides the
1218 default. Objective C uses these. See also above.
1219 FIXME: Make Objective C use normal builtins. */
1220 if (TREE_CODE (olddecl) == FUNCTION_DECL
1221 && DECL_SOURCE_LINE (olddecl) == 0)
1225 warning ("%Jstatic declaration of '%D' follows "
1226 "non-static declaration", newdecl, newdecl);
1230 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1231 && warn_traditional)
1233 warning ("%Jnon-static declaration of '%D' follows "
1234 "static declaration", newdecl, newdecl);
1238 else if (TREE_CODE (newdecl) == VAR_DECL)
1240 /* Only variables can be thread-local, and all declarations must
1241 agree on this property. */
1242 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1244 if (DECL_THREAD_LOCAL (newdecl))
1245 error ("%Jthread-local declaration of '%D' follows "
1246 "non-thread-local declaration", newdecl, newdecl);
1248 error ("%Jnon-thread-local declaration of '%D' follows "
1249 "thread-local declaration", newdecl, newdecl);
1251 locate_old_decl (olddecl, error);
1255 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1256 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1258 error ("%Jredefinition of '%D'", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1263 /* Objects declared at file scope: if at least one is 'extern',
1264 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1265 if (DECL_FILE_SCOPE_P (newdecl))
1267 if (!DECL_EXTERNAL (newdecl)
1268 && !DECL_EXTERNAL (olddecl)
1269 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1271 if (TREE_PUBLIC (newdecl))
1272 error ("%Jnon-static declaration of '%D' follows "
1273 "static declaration", newdecl, newdecl);
1275 error ("%Jstatic declaration of '%D' follows "
1276 "non-static declaration", newdecl, newdecl);
1278 locate_old_decl (olddecl, error);
1282 /* Two objects with the same name declared at the same block
1283 scope must both be external references (6.7p3). */
1284 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1285 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1287 if (DECL_EXTERNAL (newdecl))
1288 error ("%Jextern declaration of '%D' follows "
1289 "declaration with no linkage", newdecl, newdecl);
1290 else if (DECL_EXTERNAL (olddecl))
1291 error ("%Jdeclaration of '%D' with no linkage follows "
1292 "extern declaration", newdecl, newdecl);
1294 error ("%Jredeclaration of '%D' with no linkage",
1297 locate_old_decl (olddecl, error);
1303 /* All decls must agree on a non-default visibility. */
1304 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1305 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1306 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1308 warning ("%Jredeclaration of '%D' with different visibility "
1309 "(old visibility preserved)", newdecl, newdecl);
1313 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1315 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1316 if (DECL_DECLARED_INLINE_P (newdecl)
1317 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1319 warning ("%Jinline declaration of '%D' follows "
1320 "declaration with attribute noinline", newdecl, newdecl);
1323 else if (DECL_DECLARED_INLINE_P (olddecl)
1324 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1326 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1327 "inline declaration ", newdecl, newdecl);
1331 /* Inline declaration after use or definition.
1332 ??? Should we still warn about this now we have unit-at-a-time
1333 mode and can get it right? */
1334 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1336 if (TREE_USED (olddecl))
1338 warning ("%J'%D' declared inline after being called",
1342 else if (DECL_INITIAL (olddecl))
1344 warning ("%J'%D' declared inline after its definition",
1350 else /* PARM_DECL, VAR_DECL */
1352 /* Redeclaration of a parameter is a constraint violation (this is
1353 not explicitly stated, but follows from C99 6.7p3 [no more than
1354 one declaration of the same identifier with no linkage in the
1355 same scope, except type tags] and 6.2.2p6 [parameters have no
1356 linkage]). We must check for a forward parameter declaration,
1357 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1358 an extension, the mandatory diagnostic for which is handled by
1359 mark_forward_parm_decls. */
1361 if (TREE_CODE (newdecl) == PARM_DECL
1362 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1364 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1365 locate_old_decl (olddecl, error);
1369 /* These bits are only type qualifiers when applied to objects. */
1370 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1372 if (TREE_THIS_VOLATILE (newdecl))
1373 pedwarn ("%Jvolatile declaration of '%D' follows "
1374 "non-volatile declaration", newdecl, newdecl);
1376 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1377 "volatile declaration", newdecl, newdecl);
1380 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1382 if (TREE_READONLY (newdecl))
1383 pedwarn ("%Jconst declaration of '%D' follows "
1384 "non-const declaration", newdecl, newdecl);
1386 pedwarn ("%Jnon-const declaration of '%D' follows "
1387 "const declaration", newdecl, newdecl);
1392 /* Optional warning for completely redundant decls. */
1393 if (!warned && !pedwarned
1394 && warn_redundant_decls
1395 /* Don't warn about a function declaration followed by a
1397 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1398 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1399 /* Don't warn about an extern followed by a definition. */
1400 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1401 /* Don't warn about forward parameter decls. */
1402 && !(TREE_CODE (newdecl) == PARM_DECL
1403 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1405 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1409 /* Report location of previous decl/defn in a consistent manner. */
1410 if (warned || pedwarned)
1411 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1416 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1417 consistent with OLDDECL, but carries new information. Merge the
1418 new information into OLDDECL. This function issues no
1422 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1424 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1425 && DECL_INITIAL (newdecl) != 0);
1427 /* For real parm decl following a forward decl, rechain the old decl
1428 in its new location and clear TREE_ASM_WRITTEN (it's not a
1429 forward decl anymore). */
1430 if (TREE_CODE (newdecl) == PARM_DECL
1431 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1433 struct c_binding *b, **here;
1435 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1436 if ((*here)->decl == olddecl)
1443 b->prev = current_scope->bindings;
1444 current_scope->bindings = b;
1446 TREE_ASM_WRITTEN (olddecl) = 0;
1449 DECL_ATTRIBUTES (newdecl)
1450 = targetm.merge_decl_attributes (olddecl, newdecl);
1452 /* Merge the data types specified in the two decls. */
1454 = TREE_TYPE (olddecl)
1455 = composite_type (newtype, oldtype);
1457 /* Lay the type out, unless already done. */
1458 if (oldtype != TREE_TYPE (newdecl))
1460 if (TREE_TYPE (newdecl) != error_mark_node)
1461 layout_type (TREE_TYPE (newdecl));
1462 if (TREE_CODE (newdecl) != FUNCTION_DECL
1463 && TREE_CODE (newdecl) != TYPE_DECL
1464 && TREE_CODE (newdecl) != CONST_DECL)
1465 layout_decl (newdecl, 0);
1469 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1470 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1471 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1472 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1473 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1474 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1476 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1477 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1481 /* Keep the old rtl since we can safely use it. */
1482 COPY_DECL_RTL (olddecl, newdecl);
1484 /* Merge the type qualifiers. */
1485 if (TREE_READONLY (newdecl))
1486 TREE_READONLY (olddecl) = 1;
1488 if (TREE_THIS_VOLATILE (newdecl))
1490 TREE_THIS_VOLATILE (olddecl) = 1;
1491 if (TREE_CODE (newdecl) == VAR_DECL)
1492 make_var_volatile (newdecl);
1495 /* Keep source location of definition rather than declaration. */
1496 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1497 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1499 /* Merge the unused-warning information. */
1500 if (DECL_IN_SYSTEM_HEADER (olddecl))
1501 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1502 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1503 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1505 /* Merge the initialization information. */
1506 if (DECL_INITIAL (newdecl) == 0)
1507 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1509 /* Merge the section attribute.
1510 We want to issue an error if the sections conflict but that must be
1511 done later in decl_attributes since we are called before attributes
1513 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1514 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1516 /* Copy the assembler name.
1517 Currently, it can only be defined in the prototype. */
1518 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1520 /* If either declaration has a nondefault visibility, use it. */
1521 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1522 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1524 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1526 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1527 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1528 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1529 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1530 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1531 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1532 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1533 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1534 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1537 /* Merge the storage class information. */
1538 merge_weak (newdecl, olddecl);
1540 /* For functions, static overrides non-static. */
1541 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1543 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1544 /* This is since we don't automatically
1545 copy the attributes of NEWDECL into OLDDECL. */
1546 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1547 /* If this clears `static', clear it in the identifier too. */
1548 if (! TREE_PUBLIC (olddecl))
1549 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1551 if (DECL_EXTERNAL (newdecl))
1553 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1554 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1556 /* An extern decl does not override previous storage class. */
1557 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1558 if (! DECL_EXTERNAL (newdecl))
1560 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1561 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1566 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1567 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1570 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1572 /* If we're redefining a function previously defined as extern
1573 inline, make sure we emit debug info for the inline before we
1574 throw it away, in case it was inlined into a function that hasn't
1575 been written out yet. */
1576 if (new_is_definition && DECL_INITIAL (olddecl))
1578 if (TREE_USED (olddecl)
1579 /* In unit-at-a-time mode we never inline re-defined extern
1580 inline functions. */
1581 && !flag_unit_at_a_time
1582 && cgraph_function_possibly_inlined_p (olddecl))
1583 (*debug_hooks->outlining_inline_function) (olddecl);
1585 /* The new defn must not be inline. */
1586 DECL_INLINE (newdecl) = 0;
1587 DECL_UNINLINABLE (newdecl) = 1;
1591 /* If either decl says `inline', this fn is inline,
1592 unless its definition was passed already. */
1593 if (DECL_DECLARED_INLINE_P (newdecl)
1594 || DECL_DECLARED_INLINE_P (olddecl))
1595 DECL_DECLARED_INLINE_P (newdecl) = 1;
1597 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1598 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1601 if (DECL_BUILT_IN (olddecl))
1603 /* If redeclaring a builtin function, it stays built in.
1604 But it gets tagged as having been declared. */
1605 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1606 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1607 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1610 /* Also preserve various other info from the definition. */
1611 if (! new_is_definition)
1613 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1614 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1615 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1616 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1617 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1619 /* Set DECL_INLINE on the declaration if we've got a body
1620 from which to instantiate. */
1621 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1623 DECL_INLINE (newdecl) = 1;
1624 DECL_ABSTRACT_ORIGIN (newdecl)
1625 = DECL_ABSTRACT_ORIGIN (olddecl);
1630 /* If a previous declaration said inline, mark the
1631 definition as inlinable. */
1632 if (DECL_DECLARED_INLINE_P (newdecl)
1633 && ! DECL_UNINLINABLE (newdecl))
1634 DECL_INLINE (newdecl) = 1;
1638 /* This bit must not get wiped out. */
1639 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1641 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1642 But preserve OLDDECL's DECL_UID. */
1644 unsigned olddecl_uid = DECL_UID (olddecl);
1646 memcpy ((char *) olddecl + sizeof (struct tree_common),
1647 (char *) newdecl + sizeof (struct tree_common),
1648 sizeof (struct tree_decl) - sizeof (struct tree_common));
1649 DECL_UID (olddecl) = olddecl_uid;
1652 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1653 so that encode_section_info has a chance to look at the new decl
1654 flags and attributes. */
1655 if (DECL_RTL_SET_P (olddecl)
1656 && (TREE_CODE (olddecl) == FUNCTION_DECL
1657 || (TREE_CODE (olddecl) == VAR_DECL
1658 && TREE_STATIC (olddecl))))
1659 make_decl_rtl (olddecl, NULL);
1662 /* Handle when a new declaration NEWDECL has the same name as an old
1663 one OLDDECL in the same binding contour. Prints an error message
1666 If safely possible, alter OLDDECL to look like NEWDECL, and return
1667 true. Otherwise, return false. */
1670 duplicate_decls (tree newdecl, tree olddecl)
1672 tree newtype = NULL, oldtype = NULL;
1674 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1677 merge_decls (newdecl, olddecl, newtype, oldtype);
1682 /* Check whether decl-node NEW shadows an existing declaration. */
1684 warn_if_shadowing (tree new)
1686 struct c_binding *b;
1688 /* Shadow warnings wanted? */
1690 /* No shadow warnings for internally generated vars. */
1691 || DECL_SOURCE_LINE (new) == 0
1692 /* No shadow warnings for vars made for inlining. */
1693 || DECL_FROM_INLINE (new)
1694 /* Don't warn about the parm names in function declarator
1695 within a function declarator. It would be nice to avoid
1696 warning in any function declarator in a declaration, as
1697 opposed to a definition, but there is no way to tell
1698 it's not a definition at this point. */
1699 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1702 /* Is anything being shadowed? Do not be confused by a second binding
1703 to the same decl in the externals scope. */
1704 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1705 if (b->decl && b->decl != new && b->contour != external_scope)
1709 if (TREE_CODE (old) == PARM_DECL)
1710 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1711 else if (DECL_FILE_SCOPE_P (old))
1712 warning ("%Jdeclaration of '%D' shadows a global declaration",
1714 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1715 warning ("%Jdeclaration of '%D' shadows a built-in function",
1718 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1720 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1721 warning ("%Jshadowed declaration is here", old);
1728 /* Subroutine of pushdecl.
1730 X is a TYPE_DECL for a typedef statement. Create a brand new
1731 ..._TYPE node (which will be just a variant of the existing
1732 ..._TYPE node with identical properties) and then install X
1733 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1735 The whole point here is to end up with a situation where each
1736 and every ..._TYPE node the compiler creates will be uniquely
1737 associated with AT MOST one node representing a typedef name.
1738 This way, even though the compiler substitutes corresponding
1739 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1740 early on, later parts of the compiler can always do the reverse
1741 translation and get back the corresponding typedef name. For
1744 typedef struct S MY_TYPE;
1747 Later parts of the compiler might only know that `object' was of
1748 type `struct S' if it were not for code just below. With this
1749 code however, later parts of the compiler see something like:
1751 struct S' == struct S
1752 typedef struct S' MY_TYPE;
1755 And they can then deduce (from the node for type struct S') that
1756 the original object declaration was:
1760 Being able to do this is important for proper support of protoize,
1761 and also for generating precise symbolic debugging information
1762 which takes full account of the programmer's (typedef) vocabulary.
1764 Obviously, we don't want to generate a duplicate ..._TYPE node if
1765 the TYPE_DECL node that we are now processing really represents a
1766 standard built-in type.
1768 Since all standard types are effectively declared at line zero
1769 in the source file, we can easily check to see if we are working
1770 on a standard type by checking the current value of lineno. */
1773 clone_underlying_type (tree x)
1775 if (DECL_SOURCE_LINE (x) == 0)
1777 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1778 TYPE_NAME (TREE_TYPE (x)) = x;
1780 else if (TREE_TYPE (x) != error_mark_node
1781 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1783 tree tt = TREE_TYPE (x);
1784 DECL_ORIGINAL_TYPE (x) = tt;
1785 tt = build_type_copy (tt);
1787 TREE_USED (tt) = TREE_USED (x);
1792 /* Record a decl-node X as belonging to the current lexical scope.
1793 Check for errors (such as an incompatible declaration for the same
1794 name already seen in the same scope).
1796 Returns either X or an old decl for the same name.
1797 If an old decl is returned, it may have been smashed
1798 to agree with what X says. */
1803 tree name = DECL_NAME (x);
1804 struct c_scope *scope = current_scope;
1805 struct c_binding *b;
1807 /* Functions need the lang_decl data. */
1808 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1809 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1811 /* Must set DECL_CONTEXT for everything not at file scope or
1812 DECL_FILE_SCOPE_P won't work. Local externs don't count
1813 unless they have initializers (which generate code). */
1814 if (current_function_decl
1815 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1816 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1817 DECL_CONTEXT (x) = current_function_decl;
1819 /* Anonymous decls are just inserted in the scope. */
1822 bind (name, x, scope);
1826 /* First, see if there is another declaration with the same name in
1827 the current scope. If there is, duplicate_decls may do all the
1828 work for us. If duplicate_decls returns false, that indicates
1829 two incompatible decls in the same scope; we are to silently
1830 replace the old one (duplicate_decls has issued all appropriate
1831 diagnostics). In particular, we should not consider possible
1832 duplicates in the external scope, or shadowing. */
1833 b = I_SYMBOL_BINDING (name);
1834 if (b && b->contour == scope)
1836 if (duplicate_decls (x, b->decl))
1839 goto skip_external_and_shadow_checks;
1842 /* All declarations with external linkage, and all external
1843 references, go in the external scope, no matter what scope is
1844 current. However, the binding in that scope is ignored for
1845 purposes of normal name lookup. A separate binding structure is
1846 created in the requested scope; this governs the normal
1847 visibility of the symbol.
1849 The binding in the externals scope is used exclusively for
1850 detecting duplicate declarations of the same object, no matter
1851 what scope they are in; this is what we do here. (C99 6.2.7p2:
1852 All declarations that refer to the same object or function shall
1853 have compatible type; otherwise, the behavior is undefined.) */
1854 if (DECL_EXTERNAL (x) || scope == file_scope)
1856 if (warn_nested_externs
1857 && scope != file_scope
1858 && !DECL_IN_SYSTEM_HEADER (x))
1859 warning ("nested extern declaration of `%s'",
1860 IDENTIFIER_POINTER (name));
1862 while (b && b->contour != external_scope)
1865 /* The point of the same_translation_unit_p check here is,
1866 we want to detect a duplicate decl for a construct like
1867 foo() { extern bar(); } ... static bar(); but not if
1868 they are in different translation units. In any case,
1869 the static does not go in the externals scope. */
1871 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1872 || same_translation_unit_p (x, b->decl))
1873 && duplicate_decls (x, b->decl))
1875 bind (name, b->decl, scope);
1878 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1880 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1881 bind (name, x, external_scope);
1885 warn_if_shadowing (x);
1887 skip_external_and_shadow_checks:
1888 if (TREE_CODE (x) == TYPE_DECL)
1889 clone_underlying_type (x);
1891 bind (name, x, scope);
1893 /* If x's type is incomplete because it's based on a
1894 structure or union which has not yet been fully declared,
1895 attach it to that structure or union type, so we can go
1896 back and complete the variable declaration later, if the
1897 structure or union gets fully declared.
1899 If the input is erroneous, we can have error_mark in the type
1900 slot (e.g. "f(void a, ...)") - that doesn't count as an
1902 if (TREE_TYPE (x) != error_mark_node
1903 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1905 tree element = TREE_TYPE (x);
1907 while (TREE_CODE (element) == ARRAY_TYPE)
1908 element = TREE_TYPE (element);
1909 element = TYPE_MAIN_VARIANT (element);
1911 if ((TREE_CODE (element) == RECORD_TYPE
1912 || TREE_CODE (element) == UNION_TYPE)
1913 && (TREE_CODE (x) != TYPE_DECL
1914 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1915 && !COMPLETE_TYPE_P (element))
1916 C_TYPE_INCOMPLETE_VARS (element)
1917 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1922 /* Record X as belonging to file scope.
1923 This is used only internally by the Objective-C front end,
1924 and is limited to its needs. duplicate_decls is not called;
1925 if there is any preexisting decl for this identifier, it is an ICE. */
1928 pushdecl_top_level (tree x)
1932 if (TREE_CODE (x) != VAR_DECL)
1935 name = DECL_NAME (x);
1937 if (I_SYMBOL_BINDING (name))
1940 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1942 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1943 bind (name, x, external_scope);
1946 bind (name, x, file_scope);
1952 implicit_decl_warning (tree id, tree olddecl)
1954 void (*diag) (const char *, ...);
1955 switch (mesg_implicit_function_declaration)
1958 case 1: diag = warning; break;
1959 case 2: diag = error; break;
1963 diag (N_("implicit declaration of function '%E'"), id);
1965 locate_old_decl (olddecl, diag);
1968 /* Generate an implicit declaration for identifier FUNCTIONID as a
1969 function of type int (). */
1972 implicitly_declare (tree functionid)
1974 tree decl = lookup_name_in_scope (functionid, external_scope);
1978 /* FIXME: Objective-C has weird not-really-builtin functions
1979 which are supposed to be visible automatically. They wind up
1980 in the external scope because they're pushed before the file
1981 scope gets created. Catch this here and rebind them into the
1983 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1985 bind (functionid, decl, file_scope);
1990 /* Implicit declaration of a function already declared
1991 (somehow) in a different scope, or as a built-in.
1992 If this is the first time this has happened, warn;
1993 then recycle the old declaration. */
1994 if (!C_DECL_IMPLICIT (decl))
1996 implicit_decl_warning (functionid, decl);
1997 C_DECL_IMPLICIT (decl) = 1;
1999 bind (functionid, decl, current_scope);
2004 /* Not seen before. */
2005 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2006 DECL_EXTERNAL (decl) = 1;
2007 TREE_PUBLIC (decl) = 1;
2008 C_DECL_IMPLICIT (decl) = 1;
2009 implicit_decl_warning (functionid, 0);
2011 /* C89 says implicit declarations are in the innermost block.
2012 So we record the decl in the standard fashion. */
2013 decl = pushdecl (decl);
2015 /* No need to call objc_check_decl here - it's a function type. */
2016 rest_of_decl_compilation (decl, NULL, 0, 0);
2018 /* Write a record describing this implicit function declaration
2019 to the prototypes file (if requested). */
2020 gen_aux_info_record (decl, 0, 1, 0);
2022 /* Possibly apply some default attributes to this implicit declaration. */
2023 decl_attributes (&decl, NULL_TREE, 0);
2028 /* Issue an error message for a reference to an undeclared variable
2029 ID, including a reference to a builtin outside of function-call
2030 context. Establish a binding of the identifier to error_mark_node
2031 in an appropriate scope, which will suppress further errors for the
2034 undeclared_variable (tree id)
2036 static bool already = false;
2037 struct c_scope *scope;
2039 if (current_function_decl == 0)
2041 error ("'%E' undeclared here (not in a function)", id);
2042 scope = current_scope;
2046 error ("'%E' undeclared (first use in this function)", id);
2050 error ("(Each undeclared identifier is reported only once");
2051 error ("for each function it appears in.)");
2055 /* If we are parsing old-style parameter decls, current_function_decl
2056 will be nonnull but current_function_scope will be null. */
2057 scope = current_function_scope ? current_function_scope : current_scope;
2059 bind (id, error_mark_node, scope);
2062 /* Subroutine of lookup_label, declare_label, define_label: construct a
2063 LABEL_DECL with all the proper frills. */
2066 make_label (tree name, location_t location)
2068 tree label = build_decl (LABEL_DECL, name, void_type_node);
2070 DECL_CONTEXT (label) = current_function_decl;
2071 DECL_MODE (label) = VOIDmode;
2072 DECL_SOURCE_LOCATION (label) = location;
2077 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2078 Create one if none exists so far for the current function.
2079 This is called when a label is used in a goto expression or
2080 has its address taken. */
2083 lookup_label (tree name)
2087 if (current_function_decl == 0)
2089 error ("label %s referenced outside of any function",
2090 IDENTIFIER_POINTER (name));
2094 /* Use a label already defined or ref'd with this name, but not if
2095 it is inherited from a containing function and wasn't declared
2097 label = I_LABEL_DECL (name);
2098 if (label && (DECL_CONTEXT (label) == current_function_decl
2099 || C_DECLARED_LABEL_FLAG (label)))
2101 /* If the label has only been declared, update its apparent
2102 location to point here, for better diagnostics if it
2103 turns out not to have been defined. */
2104 if (!TREE_USED (label))
2105 DECL_SOURCE_LOCATION (label) = input_location;
2109 /* No label binding for that identifier; make one. */
2110 label = make_label (name, input_location);
2112 /* Ordinary labels go in the current function scope. */
2113 bind (name, label, current_function_scope);
2117 /* Make a label named NAME in the current function, shadowing silently
2118 any that may be inherited from containing functions or containing
2119 scopes. This is called for __label__ declarations. */
2122 declare_label (tree name)
2124 struct c_binding *b = I_LABEL_BINDING (name);
2127 /* Check to make sure that the label hasn't already been declared
2129 if (b && b->contour == current_scope)
2131 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2132 locate_old_decl (b->decl, error);
2134 /* Just use the previous declaration. */
2138 label = make_label (name, input_location);
2139 C_DECLARED_LABEL_FLAG (label) = 1;
2141 /* Declared labels go in the current scope. */
2142 bind (name, label, current_scope);
2146 /* Define a label, specifying the location in the source file.
2147 Return the LABEL_DECL node for the label, if the definition is valid.
2148 Otherwise return 0. */
2151 define_label (location_t location, tree name)
2153 /* Find any preexisting label with this name. It is an error
2154 if that label has already been defined in this function, or
2155 if there is a containing function with a declared label with
2157 tree label = I_LABEL_DECL (name);
2160 && ((DECL_CONTEXT (label) == current_function_decl
2161 && DECL_INITIAL (label) != 0)
2162 || (DECL_CONTEXT (label) != current_function_decl
2163 && C_DECLARED_LABEL_FLAG (label))))
2165 error ("%Hduplicate label `%D'", &location, label);
2166 locate_old_decl (label, error);
2169 else if (label && DECL_CONTEXT (label) == current_function_decl)
2171 /* The label has been used or declared already in this function,
2172 but not defined. Update its location to point to this
2174 DECL_SOURCE_LOCATION (label) = location;
2178 /* No label binding for that identifier; make one. */
2179 label = make_label (name, location);
2181 /* Ordinary labels go in the current function scope. */
2182 bind (name, label, current_function_scope);
2185 if (warn_traditional && !in_system_header && lookup_name (name))
2186 warning ("%Htraditional C lacks a separate namespace for labels, "
2187 "identifier `%s' conflicts", &location,
2188 IDENTIFIER_POINTER (name));
2190 /* Mark label as having been defined. */
2191 DECL_INITIAL (label) = error_mark_node;
2195 /* Given NAME, an IDENTIFIER_NODE,
2196 return the structure (or union or enum) definition for that name.
2197 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2198 CODE says which kind of type the caller wants;
2199 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2200 If the wrong kind of type is found, an error is reported. */
2203 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2205 struct c_binding *b = I_TAG_BINDING (name);
2211 /* We only care about whether it's in this level if
2212 thislevel_only was set or it might be a type clash. */
2213 if (thislevel_only || TREE_CODE (b->decl) != code)
2215 /* For our purposes, a tag in the external scope is the same as
2216 a tag in the file scope. (Primarily relevant to Objective-C
2217 and its builtin structure tags, which get pushed before the
2218 file scope is created.) */
2219 if (b->contour == current_scope
2220 || (current_scope == file_scope && b->contour == external_scope))
2224 if (thislevel_only && !thislevel)
2227 if (TREE_CODE (b->decl) != code)
2229 /* Definition isn't the kind we were looking for. */
2230 pending_invalid_xref = name;
2231 pending_invalid_xref_location = input_location;
2233 /* If in the same binding level as a declaration as a tag
2234 of a different type, this must not be allowed to
2235 shadow that tag, so give the error immediately.
2236 (For example, "struct foo; union foo;" is invalid.) */
2238 pending_xref_error ();
2243 /* Print an error message now
2244 for a recent invalid struct, union or enum cross reference.
2245 We don't print them immediately because they are not invalid
2246 when used in the `struct foo;' construct for shadowing. */
2249 pending_xref_error (void)
2251 if (pending_invalid_xref != 0)
2252 error ("%H`%s' defined as wrong kind of tag",
2253 &pending_invalid_xref_location,
2254 IDENTIFIER_POINTER (pending_invalid_xref));
2255 pending_invalid_xref = 0;
2259 /* Look up NAME in the current scope and its superiors
2260 in the namespace of variables, functions and typedefs.
2261 Return a ..._DECL node of some kind representing its definition,
2262 or return 0 if it is undefined. */
2265 lookup_name (tree name)
2267 struct c_binding *b = I_SYMBOL_BINDING (name);
2268 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2273 /* Similar to `lookup_name' but look only at the indicated scope. */
2276 lookup_name_in_scope (tree name, struct c_scope *scope)
2278 struct c_binding *b;
2280 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2281 if (b->contour == scope)
2286 /* Create the predefined scalar types of C,
2287 and some nodes representing standard constants (0, 1, (void *) 0).
2288 Initialize the global scope.
2289 Make definitions for built-in primitive functions. */
2292 c_init_decl_processing (void)
2295 tree ptr_ftype_void, ptr_ftype_ptr;
2296 location_t save_loc = input_location;
2298 /* Adds some ggc roots, and reserved words for c-parse.in. */
2301 current_function_decl = 0;
2303 /* Make the externals scope. */
2305 external_scope = current_scope;
2307 /* Declarations from c_common_nodes_and_builtins must not be associated
2308 with this input file, lest we get differences between using and not
2309 using preprocessed headers. */
2310 input_location.file = "<internal>";
2311 input_location.line = 0;
2313 build_common_tree_nodes (flag_signed_char);
2315 c_common_nodes_and_builtins ();
2317 /* In C, comparisons and TRUTH_* expressions have type int. */
2318 truthvalue_type_node = integer_type_node;
2319 truthvalue_true_node = integer_one_node;
2320 truthvalue_false_node = integer_zero_node;
2322 /* Even in C99, which has a real boolean type. */
2323 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2324 boolean_type_node));
2326 endlink = void_list_node;
2327 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2329 = build_function_type (ptr_type_node,
2330 tree_cons (NULL_TREE, ptr_type_node, endlink));
2332 input_location = save_loc;
2334 pedantic_lvalues = true;
2336 make_fname_decl = c_make_fname_decl;
2337 start_fname_decls ();
2340 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2341 decl, NAME is the initialization string and TYPE_DEP indicates whether
2342 NAME depended on the type of the function. As we don't yet implement
2343 delayed emission of static data, we mark the decl as emitted
2344 so it is not placed in the output. Anything using it must therefore pull
2345 out the STRING_CST initializer directly. FIXME. */
2348 c_make_fname_decl (tree id, int type_dep)
2350 const char *name = fname_as_string (type_dep);
2351 tree decl, type, init;
2352 size_t length = strlen (name);
2354 type = build_array_type
2355 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2356 build_index_type (size_int (length)));
2358 decl = build_decl (VAR_DECL, id, type);
2360 TREE_STATIC (decl) = 1;
2361 TREE_READONLY (decl) = 1;
2362 DECL_ARTIFICIAL (decl) = 1;
2364 init = build_string (length + 1, name);
2365 free ((char *) name);
2366 TREE_TYPE (init) = type;
2367 DECL_INITIAL (decl) = init;
2369 TREE_USED (decl) = 1;
2371 if (current_function_decl)
2373 DECL_CONTEXT (decl) = current_function_decl;
2374 bind (id, decl, current_function_scope);
2377 finish_decl (decl, init, NULL_TREE);
2382 /* Return a definition for a builtin function named NAME and whose data type
2383 is TYPE. TYPE should be a function type with argument types.
2384 FUNCTION_CODE tells later passes how to compile calls to this function.
2385 See tree.h for its possible values.
2387 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2388 the name to be called if we can't opencode the function. If
2389 ATTRS is nonzero, use that for the function's attribute list. */
2392 builtin_function (const char *name, tree type, int function_code,
2393 enum built_in_class class, const char *library_name,
2396 tree id = get_identifier (name);
2397 tree decl = build_decl (FUNCTION_DECL, id, type);
2398 TREE_PUBLIC (decl) = 1;
2399 DECL_EXTERNAL (decl) = 1;
2400 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2401 DECL_BUILT_IN_CLASS (decl) = class;
2402 DECL_FUNCTION_CODE (decl) = function_code;
2404 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2405 make_decl_rtl (decl, NULL);
2407 /* Should never be called on a symbol with a preexisting meaning. */
2408 if (I_SYMBOL_BINDING (id))
2411 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2412 bind (id, decl, external_scope);
2414 /* Builtins in the implementation namespace are made visible without
2415 needing to be explicitly declared. See push_file_scope. */
2416 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2418 TREE_CHAIN (decl) = visible_builtins;
2419 visible_builtins = decl;
2422 /* Possibly apply some default attributes to this built-in function. */
2424 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2426 decl_attributes (&decl, NULL_TREE, 0);
2431 /* Called when a declaration is seen that contains no names to declare.
2432 If its type is a reference to a structure, union or enum inherited
2433 from a containing scope, shadow that tag name for the current scope
2434 with a forward reference.
2435 If its type defines a new named structure or union
2436 or defines an enum, it is valid but we need not do anything here.
2437 Otherwise, it is an error. */
2440 shadow_tag (tree declspecs)
2442 shadow_tag_warned (declspecs, 0);
2446 shadow_tag_warned (tree declspecs, int warned)
2449 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2456 pending_invalid_xref = 0;
2458 /* Remove the attributes from declspecs, since they will confuse the
2460 split_specs_attrs (declspecs, &specs, &attrs);
2462 for (link = specs; link; link = TREE_CHAIN (link))
2464 tree value = TREE_VALUE (link);
2465 enum tree_code code = TREE_CODE (value);
2467 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2468 /* Used to test also that TYPE_SIZE (value) != 0.
2469 That caused warning for `struct foo;' at top level in the file. */
2471 tree name = TYPE_NAME (value);
2478 if (warned != 1 && code != ENUMERAL_TYPE)
2479 /* Empty unnamed enum OK */
2481 pedwarn ("unnamed struct/union that defines no instances");
2487 t = lookup_tag (code, name, 1);
2491 t = make_node (code);
2498 if (!warned && ! in_system_header)
2500 warning ("useless keyword or type name in empty declaration");
2507 error ("two types specified in one empty declaration");
2512 pedwarn ("empty declaration");
2516 /* Construct an array declarator. EXPR is the expression inside [], or
2517 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2518 to the pointer to which a parameter array is converted). STATIC_P is
2519 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2520 is nonzero is the array is [*], a VLA of unspecified length which is
2521 nevertheless a complete type (not currently implemented by GCC),
2522 zero otherwise. The declarator is constructed as an ARRAY_REF
2523 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2524 left of the [] (filled by in set_array_declarator_type) and operand 1
2525 is the expression inside; whose TREE_TYPE is the type qualifiers and
2526 which has TREE_STATIC set if "static" is used. */
2529 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2532 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2533 TREE_TYPE (decl) = quals;
2534 TREE_STATIC (decl) = (static_p ? 1 : 0);
2535 if (pedantic && !flag_isoc99)
2537 if (static_p || quals != NULL_TREE)
2538 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2540 pedwarn ("ISO C90 does not support `[*]' array declarators");
2543 warning ("GCC does not yet properly implement `[*]' array declarators");
2547 /* Set the type of an array declarator. DECL is the declarator, as
2548 constructed by build_array_declarator; TYPE is what appears on the left
2549 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2550 abstract declarator, zero otherwise; this is used to reject static and
2551 type qualifiers in abstract declarators, where they are not in the
2555 set_array_declarator_type (tree decl, tree type, int abstract_p)
2557 TREE_OPERAND (decl, 0) = type;
2558 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2559 error ("static or type qualifiers in abstract declarator");
2563 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2566 groktypename (tree typename)
2570 if (TREE_CODE (typename) != TREE_LIST)
2573 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2575 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2578 /* Apply attributes. */
2579 decl_attributes (&typename, attrs, 0);
2584 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2587 groktypename_in_parm_context (tree typename)
2589 if (TREE_CODE (typename) != TREE_LIST)
2591 return grokdeclarator (TREE_VALUE (typename),
2592 TREE_PURPOSE (typename),
2596 /* Decode a declarator in an ordinary declaration or data definition.
2597 This is called as soon as the type information and variable name
2598 have been parsed, before parsing the initializer if any.
2599 Here we create the ..._DECL node, fill in its type,
2600 and put it on the list of decls for the current context.
2601 The ..._DECL node is returned as the value.
2603 Exception: for arrays where the length is not specified,
2604 the type is left null, to be filled in by `finish_decl'.
2606 Function definitions do not come here; they go to start_function
2607 instead. However, external and forward declarations of functions
2608 do go through here. Structure field declarations are done by
2609 grokfield and not through here. */
2612 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2617 /* An object declared as __attribute__((deprecated)) suppresses
2618 warnings of uses of other deprecated items. */
2619 if (lookup_attribute ("deprecated", attributes))
2620 deprecated_state = DEPRECATED_SUPPRESS;
2622 decl = grokdeclarator (declarator, declspecs,
2623 NORMAL, initialized, NULL);
2625 deprecated_state = DEPRECATED_NORMAL;
2627 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2628 && MAIN_NAME_P (DECL_NAME (decl)))
2629 warning ("%J'%D' is usually a function", decl, decl);
2632 /* Is it valid for this decl to have an initializer at all?
2633 If not, set INITIALIZED to zero, which will indirectly
2634 tell 'finish_decl' to ignore the initializer once it is parsed. */
2635 switch (TREE_CODE (decl))
2638 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2643 error ("function '%D' is initialized like a variable", decl);
2648 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2649 error ("parameter '%D' is initialized", decl);
2654 /* Don't allow initializations for incomplete types except for
2655 arrays which might be completed by the initialization. */
2657 /* This can happen if the array size is an undefined macro.
2658 We already gave a warning, so we don't need another one. */
2659 if (TREE_TYPE (decl) == error_mark_node)
2661 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2663 /* A complete type is ok if size is fixed. */
2665 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2666 || C_DECL_VARIABLE_SIZE (decl))
2668 error ("variable-sized object may not be initialized");
2672 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2674 error ("variable '%D' has initializer but incomplete type", decl);
2677 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2679 error ("elements of array '%D' have incomplete type", decl);
2686 DECL_EXTERNAL (decl) = 0;
2687 if (current_scope == file_scope)
2688 TREE_STATIC (decl) = 1;
2690 /* Tell 'pushdecl' this is an initialized decl
2691 even though we don't yet have the initializer expression.
2692 Also tell 'finish_decl' it may store the real initializer. */
2693 DECL_INITIAL (decl) = error_mark_node;
2696 /* If this is a function declaration, write a record describing it to the
2697 prototypes file (if requested). */
2699 if (TREE_CODE (decl) == FUNCTION_DECL)
2700 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2702 /* ANSI specifies that a tentative definition which is not merged with
2703 a non-tentative definition behaves exactly like a definition with an
2704 initializer equal to zero. (Section 3.7.2)
2706 -fno-common gives strict ANSI behavior, though this tends to break
2707 a large body of code that grew up without this rule.
2709 Thread-local variables are never common, since there's no entrenched
2710 body of code to break, and it allows more efficient variable references
2711 in the presence of dynamic linking. */
2713 if (TREE_CODE (decl) == VAR_DECL
2715 && TREE_PUBLIC (decl)
2716 && !DECL_THREAD_LOCAL (decl)
2718 DECL_COMMON (decl) = 1;
2720 /* Set attributes here so if duplicate decl, will have proper attributes. */
2721 decl_attributes (&decl, attributes, 0);
2723 if (TREE_CODE (decl) == FUNCTION_DECL
2724 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2726 tree ce = declarator;
2728 if (TREE_CODE (ce) == INDIRECT_REF)
2729 ce = TREE_OPERAND (declarator, 0);
2730 if (TREE_CODE (ce) == CALL_EXPR)
2732 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2733 for (; args; args = TREE_CHAIN (args))
2735 tree type = TREE_TYPE (args);
2736 if (INTEGRAL_TYPE_P (type)
2737 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2738 DECL_ARG_TYPE (args) = integer_type_node;
2743 if (TREE_CODE (decl) == FUNCTION_DECL
2744 && DECL_DECLARED_INLINE_P (decl)
2745 && DECL_UNINLINABLE (decl)
2746 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2747 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2749 /* Add this decl to the current scope.
2750 TEM may equal DECL or it may be a previous decl of the same name. */
2751 tem = pushdecl (decl);
2756 /* Finish processing of a declaration;
2757 install its initial value.
2758 If the length of an array type is not known before,
2759 it must be determined now, from the initial value, or it is an error. */
2762 finish_decl (tree decl, tree init, tree asmspec_tree)
2764 tree type = TREE_TYPE (decl);
2765 int was_incomplete = (DECL_SIZE (decl) == 0);
2766 const char *asmspec = 0;
2768 /* If a name was specified, get the string. */
2769 if (current_scope == file_scope)
2770 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2772 asmspec = TREE_STRING_POINTER (asmspec_tree);
2774 /* If `start_decl' didn't like having an initialization, ignore it now. */
2775 if (init != 0 && DECL_INITIAL (decl) == 0)
2778 /* Don't crash if parm is initialized. */
2779 if (TREE_CODE (decl) == PARM_DECL)
2783 store_init_value (decl, init);
2785 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2786 || TREE_CODE (decl) == FUNCTION_DECL
2787 || TREE_CODE (decl) == FIELD_DECL))
2788 objc_check_decl (decl);
2790 /* Deduce size of array from initialization, if not already known. */
2791 if (TREE_CODE (type) == ARRAY_TYPE
2792 && TYPE_DOMAIN (type) == 0
2793 && TREE_CODE (decl) != TYPE_DECL)
2796 = (TREE_STATIC (decl)
2797 /* Even if pedantic, an external linkage array
2798 may have incomplete type at first. */
2799 ? pedantic && !TREE_PUBLIC (decl)
2800 : !DECL_EXTERNAL (decl));
2802 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2804 /* Get the completed type made by complete_array_type. */
2805 type = TREE_TYPE (decl);
2808 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2810 else if (failure == 2)
2813 error ("%Jarray size missing in '%D'", decl, decl);
2814 /* If a `static' var's size isn't known,
2815 make it extern as well as static, so it does not get
2817 If it is not `static', then do not mark extern;
2818 finish_incomplete_decl will give it a default size
2819 and it will get allocated. */
2820 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2821 DECL_EXTERNAL (decl) = 1;
2824 /* TYPE_MAX_VALUE is always one less than the number of elements
2825 in the array, because we start counting at zero. Therefore,
2826 warn only if the value is less than zero. */
2827 else if (pedantic && TYPE_DOMAIN (type) != 0
2828 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2829 error ("%Jzero or negative size array '%D'", decl, decl);
2831 layout_decl (decl, 0);
2834 if (TREE_CODE (decl) == VAR_DECL)
2836 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2837 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2838 layout_decl (decl, 0);
2840 if (DECL_SIZE (decl) == 0
2841 /* Don't give an error if we already gave one earlier. */
2842 && TREE_TYPE (decl) != error_mark_node
2843 && (TREE_STATIC (decl)
2845 /* A static variable with an incomplete type
2846 is an error if it is initialized.
2847 Also if it is not file scope.
2848 Otherwise, let it through, but if it is not `extern'
2849 then it may cause an error message later. */
2850 (DECL_INITIAL (decl) != 0
2851 || !DECL_FILE_SCOPE_P (decl))
2853 /* An automatic variable with an incomplete type
2855 !DECL_EXTERNAL (decl)))
2857 error ("%Jstorage size of '%D' isn't known", decl, decl);
2858 TREE_TYPE (decl) = error_mark_node;
2861 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2862 && DECL_SIZE (decl) != 0)
2864 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2865 constant_expression_warning (DECL_SIZE (decl));
2867 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2870 if (TREE_USED (type))
2871 TREE_USED (decl) = 1;
2874 /* If this is a function and an assembler name is specified, reset DECL_RTL
2875 so we can give it its new name. Also, update built_in_decls if it
2876 was a normal built-in. */
2877 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2879 /* ASMSPEC is given, and not the name of a register. Mark the
2880 name with a star so assemble_name won't munge it. */
2881 char *starred = alloca (strlen (asmspec) + 2);
2883 strcpy (starred + 1, asmspec);
2885 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2887 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2888 SET_DECL_RTL (builtin, NULL_RTX);
2889 change_decl_assembler_name (builtin, get_identifier (starred));
2890 #ifdef TARGET_MEM_FUNCTIONS
2891 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2892 init_block_move_fn (starred);
2893 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2894 init_block_clear_fn (starred);
2896 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2897 init_block_move_fn (starred);
2898 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2899 init_block_clear_fn (starred);
2902 SET_DECL_RTL (decl, NULL_RTX);
2903 change_decl_assembler_name (decl, get_identifier (starred));
2906 /* If #pragma weak was used, mark the decl weak now. */
2907 if (current_scope == file_scope)
2908 maybe_apply_pragma_weak (decl);
2910 /* Output the assembler code and/or RTL code for variables and functions,
2911 unless the type is an undefined structure or union.
2912 If not, it will get done when the type is completed. */
2914 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2916 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2917 if (c_dialect_objc ())
2918 objc_check_decl (decl);
2920 if (DECL_FILE_SCOPE_P (decl))
2922 if (DECL_INITIAL (decl) == NULL_TREE
2923 || DECL_INITIAL (decl) == error_mark_node)
2924 /* Don't output anything
2925 when a tentative file-scope definition is seen.
2926 But at end of compilation, do output code for them. */
2927 DECL_DEFER_OUTPUT (decl) = 1;
2928 rest_of_decl_compilation (decl, asmspec, true, 0);
2932 /* This is a local variable. If there is an ASMSPEC, the
2933 user has requested that we handle it specially. */
2936 /* In conjunction with an ASMSPEC, the `register'
2937 keyword indicates that we should place the variable
2938 in a particular register. */
2939 if (C_DECL_REGISTER (decl))
2941 DECL_HARD_REGISTER (decl) = 1;
2942 /* This cannot be done for a structure with volatile
2943 fields, on which DECL_REGISTER will have been
2945 if (!DECL_REGISTER (decl))
2946 error ("cannot put object with volatile field into register");
2949 /* If this is not a static variable, issue a warning.
2950 It doesn't make any sense to give an ASMSPEC for an
2951 ordinary, non-register local variable. Historically,
2952 GCC has accepted -- but ignored -- the ASMSPEC in
2954 if (TREE_CODE (decl) == VAR_DECL
2955 && !C_DECL_REGISTER (decl)
2956 && !TREE_STATIC (decl))
2957 warning ("%Jignoring asm-specifier for non-static local "
2958 "variable '%D'", decl, decl);
2960 change_decl_assembler_name (decl, get_identifier (asmspec));
2963 if (TREE_CODE (decl) != FUNCTION_DECL)
2964 add_decl_stmt (decl);
2967 if (!DECL_FILE_SCOPE_P (decl))
2969 /* Recompute the RTL of a local array now
2970 if it used to be an incomplete type. */
2972 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2974 /* If we used it already as memory, it must stay in memory. */
2975 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2976 /* If it's still incomplete now, no init will save it. */
2977 if (DECL_SIZE (decl) == 0)
2978 DECL_INITIAL (decl) = 0;
2983 /* If this was marked 'used', be sure it will be output. */
2984 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2985 mark_decl_referenced (decl);
2987 if (TREE_CODE (decl) == TYPE_DECL)
2989 if (!DECL_FILE_SCOPE_P (decl)
2990 && variably_modified_type_p (TREE_TYPE (decl)))
2991 add_decl_stmt (decl);
2993 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2996 /* At the end of a declaration, throw away any variable type sizes
2997 of types defined inside that declaration. There is no use
2998 computing them in the following function definition. */
2999 if (current_scope == file_scope)
3000 get_pending_sizes ();
3002 /* Install a cleanup (aka destructor) if one was given. */
3003 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3005 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3008 static bool eh_initialized_p;
3010 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3011 tree cleanup_decl = lookup_name (cleanup_id);
3014 /* Build "cleanup(&decl)" for the destructor. */
3015 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3016 cleanup = build_tree_list (NULL_TREE, cleanup);
3017 cleanup = build_function_call (cleanup_decl, cleanup);
3019 /* Don't warn about decl unused; the cleanup uses it. */
3020 TREE_USED (decl) = 1;
3022 /* Initialize EH, if we've been told to do so. */
3023 if (flag_exceptions && !eh_initialized_p)
3025 eh_initialized_p = true;
3026 eh_personality_libfunc
3027 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3028 ? "__gcc_personality_sj0"
3029 : "__gcc_personality_v0");
3030 using_eh_for_cleanups ();
3033 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3038 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3039 and push that on the current scope. */
3042 push_parm_decl (tree parm)
3046 /* Don't attempt to expand sizes while parsing this decl.
3047 (We can get here with i_s_e 1 somehow from Objective-C.) */
3048 int save_immediate_size_expand = immediate_size_expand;
3049 immediate_size_expand = 0;
3051 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3052 TREE_PURPOSE (TREE_PURPOSE (parm)),
3054 decl_attributes (&decl, TREE_VALUE (parm), 0);
3056 decl = pushdecl (decl);
3058 finish_decl (decl, NULL_TREE, NULL_TREE);
3060 immediate_size_expand = save_immediate_size_expand;
3063 /* Mark all the parameter declarations to date as forward decls.
3064 Also diagnose use of this extension. */
3067 mark_forward_parm_decls (void)
3069 struct c_binding *b;
3071 if (pedantic && !current_scope->warned_forward_parm_decls)
3073 pedwarn ("ISO C forbids forward parameter declarations");
3074 current_scope->warned_forward_parm_decls = true;
3077 for (b = current_scope->bindings; b; b = b->prev)
3078 if (TREE_CODE (b->decl) == PARM_DECL)
3079 TREE_ASM_WRITTEN (b->decl) = 1;
3082 static GTY(()) int compound_literal_number;
3084 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3085 literal, which may be an incomplete array type completed by the
3086 initializer; INIT is a CONSTRUCTOR that initializes the compound
3090 build_compound_literal (tree type, tree init)
3092 /* We do not use start_decl here because we have a type, not a declarator;
3093 and do not use finish_decl because the decl should be stored inside
3094 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3095 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3098 DECL_EXTERNAL (decl) = 0;
3099 TREE_PUBLIC (decl) = 0;
3100 TREE_STATIC (decl) = (current_scope == file_scope);
3101 DECL_CONTEXT (decl) = current_function_decl;
3102 TREE_USED (decl) = 1;
3103 TREE_TYPE (decl) = type;
3104 TREE_READONLY (decl) = TYPE_READONLY (type);
3105 store_init_value (decl, init);
3107 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3109 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3114 type = TREE_TYPE (decl);
3115 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3116 return error_mark_node;
3118 stmt = build_stmt (DECL_STMT, decl);
3119 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3120 TREE_SIDE_EFFECTS (complit) = 1;
3122 layout_decl (decl, 0);
3124 if (TREE_STATIC (decl))
3126 /* This decl needs a name for the assembler output. We also need
3127 a unique suffix to be added to the name. */
3130 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3131 compound_literal_number);
3132 compound_literal_number++;
3133 DECL_NAME (decl) = get_identifier (name);
3134 DECL_DEFER_OUTPUT (decl) = 1;
3135 DECL_COMDAT (decl) = 1;
3136 DECL_ARTIFICIAL (decl) = 1;
3138 rest_of_decl_compilation (decl, NULL, 1, 0);
3144 /* Make TYPE a complete type based on INITIAL_VALUE.
3145 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3146 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3149 complete_array_type (tree type, tree initial_value, int do_default)
3151 tree maxindex = NULL_TREE;
3156 /* Note MAXINDEX is really the maximum index,
3157 one less than the size. */
3158 if (TREE_CODE (initial_value) == STRING_CST)
3161 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3162 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3165 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3167 tree elts = CONSTRUCTOR_ELTS (initial_value);
3168 maxindex = build_int_2 (-1, -1);
3169 for (; elts; elts = TREE_CHAIN (elts))
3171 if (TREE_PURPOSE (elts))
3172 maxindex = TREE_PURPOSE (elts);
3174 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3175 maxindex, integer_one_node));
3177 maxindex = copy_node (maxindex);
3181 /* Make an error message unless that happened already. */
3182 if (initial_value != error_mark_node)
3185 /* Prevent further error messages. */
3186 maxindex = build_int_2 (0, 0);
3193 maxindex = build_int_2 (0, 0);
3199 TYPE_DOMAIN (type) = build_index_type (maxindex);
3200 if (!TREE_TYPE (maxindex))
3201 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3204 /* Lay out the type now that we can get the real answer. */
3211 /* Determine whether TYPE is a structure with a flexible array member,
3212 or a union containing such a structure (possibly recursively). */
3215 flexible_array_type_p (tree type)
3218 switch (TREE_CODE (type))
3221 x = TYPE_FIELDS (type);
3224 while (TREE_CHAIN (x) != NULL_TREE)
3226 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3227 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3228 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3229 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3233 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3235 if (flexible_array_type_p (TREE_TYPE (x)))
3244 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3245 replacing with appropriate values if they are invalid. */
3247 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3250 unsigned int max_width;
3251 unsigned HOST_WIDE_INT w;
3252 const char *name = orig_name ? orig_name: _("<anonymous>");
3255 STRIP_NOPS (*width);
3257 /* Detect and ignore out of range field width and process valid
3259 if (TREE_CODE (*width) != INTEGER_CST)
3261 error ("bit-field `%s' width not an integer constant", name);
3262 *width = integer_one_node;
3266 constant_expression_warning (*width);
3267 if (tree_int_cst_sgn (*width) < 0)
3269 error ("negative width in bit-field `%s'", name);
3270 *width = integer_one_node;
3272 else if (integer_zerop (*width) && orig_name)
3274 error ("zero width for bit-field `%s'", name);
3275 *width = integer_one_node;
3279 /* Detect invalid bit-field type. */
3280 if (TREE_CODE (*type) != INTEGER_TYPE
3281 && TREE_CODE (*type) != BOOLEAN_TYPE
3282 && TREE_CODE (*type) != ENUMERAL_TYPE)
3284 error ("bit-field `%s' has invalid type", name);
3285 *type = unsigned_type_node;
3288 type_mv = TYPE_MAIN_VARIANT (*type);
3290 && type_mv != integer_type_node
3291 && type_mv != unsigned_type_node
3292 && type_mv != boolean_type_node)
3293 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3295 if (type_mv == boolean_type_node)
3296 max_width = CHAR_TYPE_SIZE;
3298 max_width = TYPE_PRECISION (*type);
3300 if (0 < compare_tree_int (*width, max_width))
3302 error ("width of `%s' exceeds its type", name);
3304 *width = build_int_2 (w, 0);
3307 w = tree_low_cst (*width, 1);
3309 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3311 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3313 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3314 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3315 warning ("`%s' is narrower than values of its type", name);
3319 /* Given declspecs and a declarator,
3320 determine the name and type of the object declared
3321 and construct a ..._DECL node for it.
3322 (In one case we can return a ..._TYPE node instead.
3323 For invalid input we sometimes return 0.)
3325 DECLSPECS is a chain of tree_list nodes whose value fields
3326 are the storage classes and type specifiers.
3328 DECL_CONTEXT says which syntactic context this declaration is in:
3329 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3330 FUNCDEF for a function definition. Like NORMAL but a few different
3331 error messages in each case. Return value may be zero meaning
3332 this definition is too screwy to try to parse.
3333 PARM for a parameter declaration (either within a function prototype
3334 or before a function body). Make a PARM_DECL, or return void_type_node.
3335 TYPENAME if for a typename (in a cast or sizeof).
3336 Don't make a DECL node; just return the ..._TYPE node.
3337 FIELD for a struct or union field; make a FIELD_DECL.
3338 INITIALIZED is 1 if the decl has an initializer.
3339 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3340 representing the width of the bit-field.
3342 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3343 It may also be so in the PARM case, for a prototype where the
3344 argument type is specified but not the name.
3346 This function is where the complicated C meanings of `static'
3347 and `extern' are interpreted. */
3350 grokdeclarator (tree declarator, tree declspecs,
3351 enum decl_context decl_context, int initialized, tree *width)
3355 tree type = NULL_TREE;
3360 int type_quals = TYPE_UNQUALIFIED;
3362 int explicit_int = 0;
3363 int explicit_char = 0;
3364 int defaulted_int = 0;
3365 tree typedef_decl = 0;
3366 const char *name, *orig_name;
3367 tree typedef_type = 0;
3368 int funcdef_flag = 0;
3369 enum tree_code innermost_code = ERROR_MARK;
3370 int size_varies = 0;
3371 tree decl_attr = NULL_TREE;
3372 tree array_ptr_quals = NULL_TREE;
3373 int array_parm_static = 0;
3374 tree returned_attrs = NULL_TREE;
3375 bool bitfield = width != NULL;
3377 tree arg_info = NULL_TREE;
3379 if (decl_context == FUNCDEF)
3380 funcdef_flag = 1, decl_context = NORMAL;
3382 /* Look inside a declarator for the name being declared
3383 and get it as a string, for an error message. */
3385 tree decl = declarator;
3389 switch (TREE_CODE (decl))
3394 innermost_code = TREE_CODE (decl);
3395 decl = TREE_OPERAND (decl, 0);
3399 decl = TREE_VALUE (decl);
3402 case IDENTIFIER_NODE:
3403 name = IDENTIFIER_POINTER (decl);
3415 /* A function definition's declarator must have the form of
3416 a function declarator. */
3418 if (funcdef_flag && innermost_code != CALL_EXPR)
3421 /* If this looks like a function definition, make it one,
3422 even if it occurs where parms are expected.
3423 Then store_parm_decls will reject it and not use it as a parm. */
3424 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3425 decl_context = PARM;
3427 /* Look through the decl specs and record which ones appear.
3428 Some typespecs are defined as built-in typenames.
3429 Others, the ones that are modifiers of other types,
3430 are represented by bits in SPECBITS: set the bits for
3431 the modifiers that appear. Storage class keywords are also in SPECBITS.
3433 If there is a typedef name or a type, store the type in TYPE.
3434 This includes builtin typedefs such as `int'.
3436 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3437 and did not come from a user typedef.
3439 Set LONGLONG if `long' is mentioned twice. */
3441 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3443 tree id = TREE_VALUE (spec);
3445 /* If the entire declaration is itself tagged as deprecated then
3446 suppress reports of deprecated items. */
3447 if (id && TREE_DEPRECATED (id))
3449 if (deprecated_state != DEPRECATED_SUPPRESS)
3450 warn_deprecated_use (id);
3453 if (id == ridpointers[(int) RID_INT])
3455 if (id == ridpointers[(int) RID_CHAR])
3458 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3460 enum rid i = C_RID_CODE (id);
3461 if ((int) i <= (int) RID_LAST_MODIFIER)
3463 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3466 error ("`long long long' is too long for GCC");
3469 if (pedantic && !flag_isoc99 && ! in_system_header
3471 pedwarn ("ISO C90 does not support `long long'");
3475 else if (specbits & (1 << (int) i))
3477 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3479 if (pedantic && !flag_isoc99)
3480 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3483 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3486 /* Diagnose "__thread extern". Recall that this list
3487 is in the reverse order seen in the text. */
3489 && (specbits & (1 << (int) RID_EXTERN
3490 | 1 << (int) RID_STATIC)))
3492 if (specbits & 1 << (int) RID_EXTERN)
3493 error ("`__thread' before `extern'");
3495 error ("`__thread' before `static'");
3498 specbits |= 1 << (int) i;
3503 error ("two or more data types in declaration of `%s'", name);
3504 /* Actual typedefs come to us as TYPE_DECL nodes. */
3505 else if (TREE_CODE (id) == TYPE_DECL)
3507 if (TREE_TYPE (id) == error_mark_node)
3508 ; /* Allow the type to default to int to avoid cascading errors. */
3511 type = TREE_TYPE (id);
3512 decl_attr = DECL_ATTRIBUTES (id);
3516 /* Built-in types come as identifiers. */
3517 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3519 tree t = lookup_name (id);
3520 if (!t || TREE_CODE (t) != TYPE_DECL)
3521 error ("`%s' fails to be a typedef or built in type",
3522 IDENTIFIER_POINTER (id));
3523 else if (TREE_TYPE (t) == error_mark_node)
3527 type = TREE_TYPE (t);
3531 else if (TREE_CODE (id) != ERROR_MARK)
3538 typedef_type = type;
3540 size_varies = C_TYPE_VARIABLE_SIZE (type);
3542 /* No type at all: default to `int', and set DEFAULTED_INT
3543 because it was not a user-defined typedef. */
3547 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3548 | (1 << (int) RID_SIGNED)
3549 | (1 << (int) RID_UNSIGNED)
3550 | (1 << (int) RID_COMPLEX))))
3551 /* Don't warn about typedef foo = bar. */
3552 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3553 && ! in_system_header)
3555 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3556 and this is a function, or if -Wimplicit; prefer the former
3557 warning since it is more explicit. */
3558 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3560 warn_about_return_type = 1;
3561 else if (warn_implicit_int || flag_isoc99)
3562 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3567 type = integer_type_node;
3570 /* Now process the modifiers that were specified
3571 and check for invalid combinations. */
3573 /* Long double is a special combination. */
3575 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3576 && TYPE_MAIN_VARIANT (type) == double_type_node)
3578 specbits &= ~(1 << (int) RID_LONG);
3579 type = long_double_type_node;
3582 /* Check all other uses of type modifiers. */
3584 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3585 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3589 if ((specbits & 1 << (int) RID_LONG)
3590 && (specbits & 1 << (int) RID_SHORT))
3591 error ("both long and short specified for `%s'", name);
3592 else if (((specbits & 1 << (int) RID_LONG)
3593 || (specbits & 1 << (int) RID_SHORT))
3595 error ("long or short specified with char for `%s'", name);
3596 else if (((specbits & 1 << (int) RID_LONG)
3597 || (specbits & 1 << (int) RID_SHORT))
3598 && TREE_CODE (type) == REAL_TYPE)
3600 static int already = 0;
3602 error ("long or short specified with floating type for `%s'", name);
3603 if (! already && ! pedantic)
3605 error ("the only valid combination is `long double'");
3609 else if ((specbits & 1 << (int) RID_SIGNED)
3610 && (specbits & 1 << (int) RID_UNSIGNED))
3611 error ("both signed and unsigned specified for `%s'", name);
3612 else if (TREE_CODE (type) != INTEGER_TYPE)
3613 error ("long, short, signed or unsigned invalid for `%s'", name);
3617 if (!explicit_int && !defaulted_int && !explicit_char)
3619 error ("long, short, signed or unsigned used invalidly for `%s'",
3625 /* Discard the type modifiers if they are invalid. */
3628 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3629 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3634 if ((specbits & (1 << (int) RID_COMPLEX))
3635 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3637 error ("complex invalid for `%s'", name);
3638 specbits &= ~(1 << (int) RID_COMPLEX);
3641 /* Decide whether an integer type is signed or not.
3642 Optionally treat bit-fields as signed by default. */
3643 if (specbits & 1 << (int) RID_UNSIGNED
3644 || (bitfield && ! flag_signed_bitfields
3645 && (explicit_int || defaulted_int || explicit_char
3646 /* A typedef for plain `int' without `signed'
3647 can be controlled just like plain `int'. */
3648 || ! (typedef_decl != 0
3649 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3650 && TREE_CODE (type) != ENUMERAL_TYPE
3651 && !(specbits & 1 << (int) RID_SIGNED)))
3654 type = long_long_unsigned_type_node;
3655 else if (specbits & 1 << (int) RID_LONG)
3656 type = long_unsigned_type_node;
3657 else if (specbits & 1 << (int) RID_SHORT)
3658 type = short_unsigned_type_node;
3659 else if (type == char_type_node)
3660 type = unsigned_char_type_node;
3661 else if (typedef_decl)
3662 type = c_common_unsigned_type (type);
3664 type = unsigned_type_node;
3666 else if ((specbits & 1 << (int) RID_SIGNED)
3667 && type == char_type_node)
3668 type = signed_char_type_node;
3670 type = long_long_integer_type_node;
3671 else if (specbits & 1 << (int) RID_LONG)
3672 type = long_integer_type_node;
3673 else if (specbits & 1 << (int) RID_SHORT)
3674 type = short_integer_type_node;
3676 if (specbits & 1 << (int) RID_COMPLEX)
3678 if (pedantic && !flag_isoc99)
3679 pedwarn ("ISO C90 does not support complex types");
3680 /* If we just have "complex", it is equivalent to
3681 "complex double", but if any modifiers at all are specified it is
3682 the complex form of TYPE. E.g, "complex short" is
3683 "complex short int". */
3685 if (defaulted_int && ! longlong
3686 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3687 | (1 << (int) RID_SIGNED)
3688 | (1 << (int) RID_UNSIGNED))))
3691 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3692 type = complex_double_type_node;
3694 else if (type == integer_type_node)
3697 pedwarn ("ISO C does not support complex integer types");
3698 type = complex_integer_type_node;
3700 else if (type == float_type_node)
3701 type = complex_float_type_node;
3702 else if (type == double_type_node)
3703 type = complex_double_type_node;
3704 else if (type == long_double_type_node)
3705 type = complex_long_double_type_node;
3709 pedwarn ("ISO C does not support complex integer types");
3710 type = build_complex_type (type);
3714 /* Check the type and width of a bit-field. */
3716 check_bitfield_type_and_width (&type, width, orig_name);
3718 /* Figure out the type qualifiers for the declaration. There are
3719 two ways a declaration can become qualified. One is something
3720 like `const int i' where the `const' is explicit. Another is
3721 something like `typedef const int CI; CI i' where the type of the
3722 declaration contains the `const'. A third possibility is that
3723 there is a type qualifier on the element type of a typedefed
3724 array type, in which case we should extract that qualifier so
3725 that c_apply_type_quals_to_decls receives the full list of
3726 qualifiers to work with (C90 is not entirely clear about whether
3727 duplicate qualifiers should be diagnosed in this case, but it
3728 seems most appropriate to do so). */
3729 element_type = strip_array_types (type);
3730 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3732 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3734 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3735 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3736 if (pedantic && !flag_isoc99)
3739 pedwarn ("duplicate `const'");
3741 pedwarn ("duplicate `restrict'");
3743 pedwarn ("duplicate `volatile'");
3745 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3746 type = TYPE_MAIN_VARIANT (type);
3747 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3748 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3749 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3751 /* Warn if two storage classes are given. Default to `auto'. */
3756 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3757 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3758 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3759 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3760 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3762 /* "static __thread" and "extern __thread" are allowed. */
3763 if ((specbits & (1 << (int) RID_THREAD
3764 | 1 << (int) RID_STATIC
3765 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3768 /* Warn about storage classes that are invalid for certain
3769 kinds of declarations (parameters, typenames, etc.). */
3772 error ("multiple storage classes in declaration of `%s'", name);
3773 else if (funcdef_flag
3775 & ((1 << (int) RID_REGISTER)
3776 | (1 << (int) RID_AUTO)
3777 | (1 << (int) RID_TYPEDEF)
3778 | (1 << (int) RID_THREAD))))
3780 if (specbits & 1 << (int) RID_AUTO
3781 && (pedantic || current_scope == file_scope))
3782 pedwarn ("function definition declared `auto'");
3783 if (specbits & 1 << (int) RID_REGISTER)
3784 error ("function definition declared `register'");
3785 if (specbits & 1 << (int) RID_TYPEDEF)
3786 error ("function definition declared `typedef'");
3787 if (specbits & 1 << (int) RID_THREAD)
3788 error ("function definition declared `__thread'");
3789 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3790 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3792 else if (decl_context != NORMAL && nclasses > 0)
3794 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3798 switch (decl_context)
3801 error ("storage class specified for structure field `%s'",
3805 error ("storage class specified for parameter `%s'", name);
3808 error ("storage class specified for typename");
3811 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3812 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3813 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3816 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3818 /* `extern' with initialization is invalid if not at file scope. */
3819 if (current_scope == file_scope)
3820 warning ("`%s' initialized and declared `extern'", name);
3822 error ("`%s' has both `extern' and initializer", name);
3824 else if (current_scope == file_scope)
3826 if (specbits & 1 << (int) RID_AUTO)
3827 error ("file-scope declaration of `%s' specifies `auto'", name);
3831 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3832 error ("nested function `%s' declared `extern'", name);
3833 else if ((specbits & (1 << (int) RID_THREAD
3834 | 1 << (int) RID_EXTERN
3835 | 1 << (int) RID_STATIC))
3836 == (1 << (int) RID_THREAD))
3838 error ("function-scope `%s' implicitly auto and declared `__thread'",
3840 specbits &= ~(1 << (int) RID_THREAD);
3845 /* Now figure out the structure of the declarator proper.
3846 Descend through it, creating more complex types, until we reach
3847 the declared identifier (or NULL_TREE, in an absolute declarator). */
3849 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3851 if (type == error_mark_node)
3853 declarator = TREE_OPERAND (declarator, 0);
3857 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3858 an INDIRECT_REF (for *...),
3859 a CALL_EXPR (for ...(...)),
3860 a TREE_LIST (for nested attributes),
3861 an identifier (for the name being declared)
3862 or a null pointer (for the place in an absolute declarator
3863 where the name was omitted).
3864 For the last two cases, we have just exited the loop.
3866 At this point, TYPE is the type of elements of an array,
3867 or for a function to return, or for a pointer to point to.
3868 After this sequence of ifs, TYPE is the type of the
3869 array or function or pointer, and DECLARATOR has had its
3870 outermost layer removed. */
3872 if (array_ptr_quals != NULL_TREE || array_parm_static)
3874 /* Only the innermost declarator (making a parameter be of
3875 array type which is converted to pointer type)
3876 may have static or type qualifiers. */
3877 error ("static or type qualifiers in non-parameter array declarator");
3878 array_ptr_quals = NULL_TREE;
3879 array_parm_static = 0;
3882 if (TREE_CODE (declarator) == TREE_LIST)
3884 /* We encode a declarator with embedded attributes using
3886 tree attrs = TREE_PURPOSE (declarator);
3889 declarator = TREE_VALUE (declarator);
3890 inner_decl = declarator;
3891 while (inner_decl != NULL_TREE
3892 && TREE_CODE (inner_decl) == TREE_LIST)
3893 inner_decl = TREE_VALUE (inner_decl);
3894 if (inner_decl == NULL_TREE
3895 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3896 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3897 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3898 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3899 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3900 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3901 returned_attrs = decl_attributes (&type,
3902 chainon (returned_attrs, attrs),
3905 else if (TREE_CODE (declarator) == ARRAY_REF)
3907 tree itype = NULL_TREE;
3908 tree size = TREE_OPERAND (declarator, 1);
3909 /* The index is a signed object `sizetype' bits wide. */
3910 tree index_type = c_common_signed_type (sizetype);
3912 array_ptr_quals = TREE_TYPE (declarator);
3913 array_parm_static = TREE_STATIC (declarator);
3915 declarator = TREE_OPERAND (declarator, 0);
3917 /* Check for some types that there cannot be arrays of. */
3919 if (VOID_TYPE_P (type))
3921 error ("declaration of `%s' as array of voids", name);
3922 type = error_mark_node;
3925 if (TREE_CODE (type) == FUNCTION_TYPE)
3927 error ("declaration of `%s' as array of functions", name);
3928 type = error_mark_node;
3931 if (pedantic && !in_system_header && flexible_array_type_p (type))
3932 pedwarn ("invalid use of structure with flexible array member");
3934 if (size == error_mark_node)
3935 type = error_mark_node;
3937 if (type == error_mark_node)
3940 /* If size was specified, set ITYPE to a range-type for that size.
3941 Otherwise, ITYPE remains null. finish_decl may figure it out
3942 from an initial value. */
3946 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3947 STRIP_TYPE_NOPS (size);
3949 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3951 error ("size of array `%s' has non-integer type", name);
3952 size = integer_one_node;
3955 if (pedantic && integer_zerop (size))
3956 pedwarn ("ISO C forbids zero-size array `%s'", name);
3958 if (TREE_CODE (size) == INTEGER_CST)
3960 constant_expression_warning (size);
3961 if (tree_int_cst_sgn (size) < 0)
3963 error ("size of array `%s' is negative", name);
3964 size = integer_one_node;
3969 /* Make sure the array size remains visibly nonconstant
3970 even if it is (eg) a const variable with known value. */
3973 if (!flag_isoc99 && pedantic)
3975 if (TREE_CONSTANT (size))
3976 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3979 pedwarn ("ISO C90 forbids variable-size array `%s'",
3984 if (integer_zerop (size))
3986 /* A zero-length array cannot be represented with an
3987 unsigned index type, which is what we'll get with
3988 build_index_type. Create an open-ended range instead. */
3989 itype = build_range_type (sizetype, size, NULL_TREE);
3993 /* Compute the maximum valid index, that is, size - 1.
3994 Do the calculation in index_type, so that if it is
3995 a variable the computations will be done in the
3997 itype = fold (build (MINUS_EXPR, index_type,
3998 convert (index_type, size),
3999 convert (index_type, size_one_node)));
4001 /* If that overflowed, the array is too big.
4002 ??? While a size of INT_MAX+1 technically shouldn't
4003 cause an overflow (because we subtract 1), the overflow
4004 is recorded during the conversion to index_type, before
4005 the subtraction. Handling this case seems like an
4006 unnecessary complication. */
4007 if (TREE_OVERFLOW (itype))
4009 error ("size of array `%s' is too large", name);
4010 type = error_mark_node;
4016 /* We must be able to distinguish the
4017 SAVE_EXPR_CONTEXT for the variably-sized type
4018 so that we can set it correctly in
4019 set_save_expr_context. The convention is
4020 that all SAVE_EXPRs that need to be reset
4021 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4022 tree cfd = current_function_decl;
4023 if (decl_context == PARM)
4024 current_function_decl = NULL_TREE;
4025 itype = variable_size (itype);
4026 if (decl_context == PARM)
4027 current_function_decl = cfd;
4029 itype = build_index_type (itype);
4032 else if (decl_context == FIELD)
4034 if (pedantic && !flag_isoc99 && !in_system_header)
4035 pedwarn ("ISO C90 does not support flexible array members");
4037 /* ISO C99 Flexible array members are effectively identical
4038 to GCC's zero-length array extension. */
4039 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4042 /* If pedantic, complain about arrays of incomplete types. */
4044 if (pedantic && !COMPLETE_TYPE_P (type))
4045 pedwarn ("array type has incomplete element type");
4047 /* Build the array type itself, then merge any constancy or
4048 volatility into the target type. We must do it in this order
4049 to ensure that the TYPE_MAIN_VARIANT field of the array type
4050 is set correctly. */
4052 type = build_array_type (type, itype);
4054 type = c_build_qualified_type (type, type_quals);
4057 C_TYPE_VARIABLE_SIZE (type) = 1;
4059 /* The GCC extension for zero-length arrays differs from
4060 ISO flexible array members in that sizeof yields zero. */
4061 if (size && integer_zerop (size))
4064 TYPE_SIZE (type) = bitsize_zero_node;
4065 TYPE_SIZE_UNIT (type) = size_zero_node;
4067 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4068 /* We can never complete an array type which is the target of a
4069 pointer, so go ahead and lay it out. */
4072 if (decl_context != PARM
4073 && (array_ptr_quals != NULL_TREE || array_parm_static))
4075 error ("static or type qualifiers in non-parameter array declarator");
4076 array_ptr_quals = NULL_TREE;
4077 array_parm_static = 0;
4080 else if (TREE_CODE (declarator) == CALL_EXPR)
4082 /* Say it's a definition only for the CALL_EXPR closest to
4084 bool really_funcdef = (funcdef_flag
4085 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4086 == IDENTIFIER_NODE));
4089 /* Declaring a function type.
4090 Make sure we have a valid type for the function to return. */
4091 if (type == error_mark_node)
4096 /* Warn about some types functions can't return. */
4098 if (TREE_CODE (type) == FUNCTION_TYPE)
4100 error ("`%s' declared as function returning a function", name);
4101 type = integer_type_node;
4103 if (TREE_CODE (type) == ARRAY_TYPE)
4105 error ("`%s' declared as function returning an array", name);
4106 type = integer_type_node;
4109 /* Construct the function type and go to the next
4110 inner layer of declarator. */
4111 arg_info = TREE_OPERAND (declarator, 1);
4112 arg_types = grokparms (arg_info, really_funcdef);
4114 /* Type qualifiers before the return type of the function
4115 qualify the return type, not the function type. */
4118 /* Type qualifiers on a function return type are normally
4119 permitted by the standard but have no effect, so give a
4120 warning at -Wextra. Qualifiers on a void return type have
4121 meaning as a GNU extension, and are banned on function
4122 definitions in ISO C. FIXME: strictly we shouldn't
4123 pedwarn for qualified void return types except on function
4124 definitions, but not doing so could lead to the undesirable
4125 state of a "volatile void" function return type not being
4126 warned about, and a use of the function being compiled
4127 with GNU semantics, with no diagnostics under -pedantic. */
4128 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4129 pedwarn ("ISO C forbids qualified void function return type");
4130 else if (extra_warnings
4131 && !(VOID_TYPE_P (type)
4132 && type_quals == TYPE_QUAL_VOLATILE))
4133 warning ("type qualifiers ignored on function return type");
4135 type = c_build_qualified_type (type, type_quals);
4137 type_quals = TYPE_UNQUALIFIED;
4139 type = build_function_type (type, arg_types);
4140 declarator = TREE_OPERAND (declarator, 0);
4142 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4143 the formal parameter list of this FUNCTION_TYPE to point to
4144 the FUNCTION_TYPE node itself. */
4149 for (link = ARG_INFO_TAGS (arg_info);
4151 link = TREE_CHAIN (link))
4152 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4155 else if (TREE_CODE (declarator) == INDIRECT_REF)
4157 /* Merge any constancy or volatility into the target type
4160 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4162 pedwarn ("ISO C forbids qualified function types");
4164 type = c_build_qualified_type (type, type_quals);
4165 type_quals = TYPE_UNQUALIFIED;
4168 type = build_pointer_type (type);
4170 /* Process a list of type modifier keywords
4171 (such as const or volatile) that were given inside the `*'. */
4173 if (TREE_TYPE (declarator))
4181 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4182 typemodlist = TREE_CHAIN (typemodlist))
4184 tree qualifier = TREE_VALUE (typemodlist);
4186 if (C_IS_RESERVED_WORD (qualifier))
4188 if (C_RID_CODE (qualifier) == RID_CONST)
4190 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4192 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4202 error ("invalid type modifier within pointer declarator");
4203 if (pedantic && !flag_isoc99)
4206 pedwarn ("duplicate `const'");
4208 pedwarn ("duplicate `volatile'");
4210 pedwarn ("duplicate `restrict'");
4213 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4214 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4215 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4218 declarator = TREE_OPERAND (declarator, 0);
4225 /* Now TYPE has the actual type. */
4227 /* Did array size calculations overflow? */
4229 if (TREE_CODE (type) == ARRAY_TYPE
4230 && COMPLETE_TYPE_P (type)
4231 && TREE_OVERFLOW (TYPE_SIZE (type)))
4233 error ("size of array `%s' is too large", name);
4234 /* If we proceed with the array type as it is, we'll eventually
4235 crash in tree_low_cst(). */
4236 type = error_mark_node;
4239 /* If this is declaring a typedef name, return a TYPE_DECL. */
4241 if (specbits & (1 << (int) RID_TYPEDEF))
4244 /* Note that the grammar rejects storage classes
4245 in typenames, fields or parameters */
4246 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4248 pedwarn ("ISO C forbids qualified function types");
4250 type = c_build_qualified_type (type, type_quals);
4251 decl = build_decl (TYPE_DECL, declarator, type);
4252 if ((specbits & (1 << (int) RID_SIGNED))
4253 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4254 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4255 decl_attributes (&decl, returned_attrs, 0);
4259 /* Detect the case of an array type of unspecified size
4260 which came, as such, direct from a typedef name.
4261 We must copy the type, so that each identifier gets
4262 a distinct type, so that each identifier's size can be
4263 controlled separately by its own initializer. */
4265 if (type != 0 && typedef_type != 0
4266 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4267 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4269 type = build_array_type (TREE_TYPE (type), 0);
4271 C_TYPE_VARIABLE_SIZE (type) = 1;
4274 /* If this is a type name (such as, in a cast or sizeof),
4275 compute the type and return it now. */
4277 if (decl_context == TYPENAME)
4279 /* Note that the grammar rejects storage classes
4280 in typenames, fields or parameters */
4281 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4283 pedwarn ("ISO C forbids const or volatile function types");
4285 type = c_build_qualified_type (type, type_quals);
4286 decl_attributes (&type, returned_attrs, 0);
4290 /* Aside from typedefs and type names (handle above),
4291 `void' at top level (not within pointer)
4292 is allowed only in public variables.
4293 We don't complain about parms either, but that is because
4294 a better error message can be made later. */
4296 if (VOID_TYPE_P (type) && decl_context != PARM
4297 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4298 && ((specbits & (1 << (int) RID_EXTERN))
4299 || (current_scope == file_scope
4301 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4303 error ("variable or field `%s' declared void", name);
4304 type = integer_type_node;
4307 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4308 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4313 if (decl_context == PARM)
4315 tree type_as_written;
4318 /* A parameter declared as an array of T is really a pointer to T.
4319 One declared as a function is really a pointer to a function. */
4321 if (TREE_CODE (type) == ARRAY_TYPE)
4323 /* Transfer const-ness of array into that of type pointed to. */
4324 type = TREE_TYPE (type);
4326 type = c_build_qualified_type (type, type_quals);
4327 type = build_pointer_type (type);
4328 type_quals = TYPE_UNQUALIFIED;
4329 if (array_ptr_quals)
4331 tree new_ptr_quals, new_ptr_attrs;
4333 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4334 /* We don't yet implement attributes in this context. */
4335 if (new_ptr_attrs != NULL_TREE)
4336 warning ("attributes in parameter array declarator ignored");
4341 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4343 tree qualifier = TREE_VALUE (new_ptr_quals);
4345 if (C_IS_RESERVED_WORD (qualifier))
4347 if (C_RID_CODE (qualifier) == RID_CONST)
4349 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4351 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4361 error ("invalid type modifier within array declarator");
4363 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4364 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4365 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4369 else if (TREE_CODE (type) == FUNCTION_TYPE)
4371 if (pedantic && type_quals)
4372 pedwarn ("ISO C forbids qualified function types");
4374 type = c_build_qualified_type (type, type_quals);
4375 type = build_pointer_type (type);
4376 type_quals = TYPE_UNQUALIFIED;
4378 else if (type_quals)
4379 type = c_build_qualified_type (type, type_quals);
4381 type_as_written = type;
4383 decl = build_decl (PARM_DECL, declarator, type);
4385 C_DECL_VARIABLE_SIZE (decl) = 1;
4387 /* Compute the type actually passed in the parmlist,
4388 for the case where there is no prototype.
4389 (For example, shorts and chars are passed as ints.)
4390 When there is a prototype, this is overridden later. */
4392 if (type == error_mark_node)
4393 promoted_type = type;
4395 promoted_type = c_type_promotes_to (type);
4397 DECL_ARG_TYPE (decl) = promoted_type;
4398 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4400 else if (decl_context == FIELD)
4402 /* Structure field. It may not be a function. */
4404 if (TREE_CODE (type) == FUNCTION_TYPE)
4406 error ("field `%s' declared as a function", name);
4407 type = build_pointer_type (type);
4409 else if (TREE_CODE (type) != ERROR_MARK
4410 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4412 error ("field `%s' has incomplete type", name);
4413 type = error_mark_node;
4415 /* Move type qualifiers down to element of an array. */
4416 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4417 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4419 TYPE_DOMAIN (type));
4420 decl = build_decl (FIELD_DECL, declarator, type);
4421 DECL_NONADDRESSABLE_P (decl) = bitfield;
4424 C_DECL_VARIABLE_SIZE (decl) = 1;
4426 else if (TREE_CODE (type) == FUNCTION_TYPE)
4428 /* Every function declaration is "external"
4429 except for those which are inside a function body
4430 in which `auto' is used.
4431 That is a case not specified by ANSI C,
4432 and we use it for forward declarations for nested functions. */
4433 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4434 || current_scope == file_scope);
4436 if (specbits & (1 << (int) RID_AUTO)
4437 && (pedantic || current_scope == file_scope))
4438 pedwarn ("invalid storage class for function `%s'", name);
4439 if (specbits & (1 << (int) RID_REGISTER))
4440 error ("invalid storage class for function `%s'", name);
4441 if (specbits & (1 << (int) RID_THREAD))
4442 error ("invalid storage class for function `%s'", name);
4443 /* Function declaration not at file scope.
4444 Storage classes other than `extern' are not allowed
4445 and `extern' makes no difference. */
4446 if (current_scope != file_scope
4447 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4449 pedwarn ("invalid storage class for function `%s'", name);
4451 decl = build_decl (FUNCTION_DECL, declarator, type);
4452 decl = build_decl_attribute_variant (decl, decl_attr);
4454 DECL_LANG_SPECIFIC (decl)
4455 = ggc_alloc_cleared (sizeof (struct lang_decl));
4457 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4458 pedwarn ("ISO C forbids qualified function types");
4460 /* GNU C interprets a `volatile void' return type to indicate
4461 that the function does not return. */
4462 if ((type_quals & TYPE_QUAL_VOLATILE)
4463 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4464 warning ("`noreturn' function returns non-void value");
4467 DECL_EXTERNAL (decl) = 1;
4468 /* Record absence of global scope for `static' or `auto'. */
4470 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4472 /* For a function definition, record the argument information
4473 block in DECL_ARGUMENTS where store_parm_decls will look
4476 DECL_ARGUMENTS (decl) = arg_info;
4479 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4481 /* Record presence of `inline', if it is reasonable. */
4482 if (MAIN_NAME_P (declarator))
4485 warning ("cannot inline function `main'");
4489 /* Record that the function is declared `inline'. */
4490 DECL_DECLARED_INLINE_P (decl) = 1;
4492 /* Do not mark bare declarations as DECL_INLINE. Doing so
4493 in the presence of multiple declarations can result in
4494 the abstract origin pointing between the declarations,
4495 which will confuse dwarf2out. */
4498 DECL_INLINE (decl) = 1;
4499 if (specbits & (1 << (int) RID_EXTERN))
4500 current_extern_inline = 1;
4503 /* If -finline-functions, assume it can be inlined. This does
4504 two things: let the function be deferred until it is actually
4505 needed, and let dwarf2 know that the function is inlinable. */
4506 else if (flag_inline_trees == 2 && initialized)
4507 DECL_INLINE (decl) = 1;
4511 /* It's a variable. */
4512 /* An uninitialized decl with `extern' is a reference. */
4513 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4515 /* Move type qualifiers down to element of an array. */
4516 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4518 int saved_align = TYPE_ALIGN(type);
4519 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4521 TYPE_DOMAIN (type));
4522 TYPE_ALIGN (type) = saved_align;
4524 else if (type_quals)
4525 type = c_build_qualified_type (type, type_quals);
4527 /* C99 6.2.2p7: It is invalid (compile-time undefined
4528 behavior) to create an 'extern' declaration for a
4529 variable if there is a global declaration that is
4530 'static' and the global declaration is not visible.
4531 (If the static declaration _is_ currently visible,
4532 the 'extern' declaration is taken to refer to that decl.) */
4533 if (extern_ref && current_scope != file_scope)
4535 tree global_decl = identifier_global_value (declarator);
4536 tree visible_decl = lookup_name (declarator);
4539 && global_decl != visible_decl
4540 && TREE_CODE (global_decl) == VAR_DECL
4541 && !TREE_PUBLIC (global_decl))
4542 error ("variable previously declared 'static' redeclared "
4546 decl = build_decl (VAR_DECL, declarator, type);
4548 C_DECL_VARIABLE_SIZE (decl) = 1;
4551 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4553 DECL_EXTERNAL (decl) = extern_ref;
4555 /* At file scope, the presence of a `static' or `register' storage
4556 class specifier, or the absence of all storage class specifiers
4557 makes this declaration a definition (perhaps tentative). Also,
4558 the absence of both `static' and `register' makes it public. */
4559 if (current_scope == file_scope)
4561 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4562 | (1 << (int) RID_REGISTER)));
4563 TREE_STATIC (decl) = !extern_ref;
4565 /* Not at file scope, only `static' makes a static definition. */
4568 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4569 TREE_PUBLIC (decl) = extern_ref;
4572 if (specbits & 1 << (int) RID_THREAD)
4574 if (targetm.have_tls)
4575 DECL_THREAD_LOCAL (decl) = 1;
4577 /* A mere warning is sure to result in improper semantics
4578 at runtime. Don't bother to allow this to compile. */
4579 error ("thread-local storage not supported for this target");
4583 /* Record `register' declaration for warnings on &
4584 and in case doing stupid register allocation. */
4586 if (specbits & (1 << (int) RID_REGISTER))
4588 C_DECL_REGISTER (decl) = 1;
4589 DECL_REGISTER (decl) = 1;
4592 /* Record constancy and volatility. */
4593 c_apply_type_quals_to_decl (type_quals, decl);
4595 /* If a type has volatile components, it should be stored in memory.
4596 Otherwise, the fact that those components are volatile
4597 will be ignored, and would even crash the compiler. */
4598 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4600 /* It is not an error for a structure with volatile fields to
4601 be declared register, but reset DECL_REGISTER since it
4602 cannot actually go in a register. */
4603 int was_reg = C_DECL_REGISTER (decl);
4604 C_DECL_REGISTER (decl) = 0;
4605 DECL_REGISTER (decl) = 0;
4606 c_mark_addressable (decl);
4607 C_DECL_REGISTER (decl) = was_reg;
4610 #ifdef ENABLE_CHECKING
4611 /* This is the earliest point at which we might know the assembler
4612 name of a variable. Thus, if it's known before this, die horribly. */
4613 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4617 decl_attributes (&decl, returned_attrs, 0);
4623 /* Decode the parameter-list info for a function type or function definition.
4624 The argument is the value returned by `get_parm_info' (or made in parse.y
4625 if there is an identifier list instead of a parameter decl list).
4626 These two functions are separate because when a function returns
4627 or receives functions then each is called multiple times but the order
4628 of calls is different. The last call to `grokparms' is always the one
4629 that contains the formal parameter names of a function definition.
4631 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4633 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4634 a mere declaration. A nonempty identifier-list gets an error message
4635 when FUNCDEF_FLAG is zero. */
4638 grokparms (tree arg_info, int funcdef_flag)
4640 tree arg_types = ARG_INFO_TYPES (arg_info);
4642 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4643 && !in_system_header)
4644 warning ("function declaration isn't a prototype");
4646 if (arg_types == error_mark_node)
4647 return 0; /* don't set TYPE_ARG_TYPES in this case */
4649 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4652 pedwarn ("parameter names (without types) in function declaration");
4654 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4655 ARG_INFO_TYPES (arg_info) = 0;
4660 tree parm, type, typelt;
4661 unsigned int parmno;
4663 /* If the arg types are incomplete in a declaration, they must
4664 include undefined tags. These tags can never be defined in
4665 the scope of the declaration, so the types can never be
4666 completed, and no call can be compiled successfully. */
4668 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4670 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4672 type = TREE_VALUE (typelt);
4673 if (type == error_mark_node)
4676 if (!COMPLETE_TYPE_P (type))
4680 if (DECL_NAME (parm))
4681 error ("%Jparameter %u ('%D') has incomplete type",
4682 parm, parmno, parm);
4684 error ("%Jparameter %u has incomplete type",
4687 TREE_VALUE (typelt) = error_mark_node;
4688 TREE_TYPE (parm) = error_mark_node;
4692 if (DECL_NAME (parm))
4693 warning ("%Jparameter %u ('%D') has incomplete type",
4694 parm, parmno, parm);
4696 warning ("%Jparameter %u has incomplete type",
4705 /* Take apart the current scope and return a tree_list node with info
4706 on a parameter list just parsed. This tree_list node should be
4707 examined using the ARG_INFO_* macros, defined above:
4709 ARG_INFO_PARMS: a list of parameter decls.
4710 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4711 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4712 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4713 constants) defined with the parameters.
4715 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4717 ELLIPSIS being true means the argument list ended in '...' so don't
4718 append a sentinel (void_list_node) to the end of the type-list. */
4721 get_parm_info (bool ellipsis)
4723 struct c_binding *b = current_scope->bindings;
4724 tree arg_info = make_node (TREE_LIST);
4730 static bool explained_incomplete_types = false;
4731 bool gave_void_only_once_err = false;
4733 /* The bindings in this scope must not get put into a block.
4734 We will take care of deleting the binding nodes. */
4735 current_scope->bindings = 0;
4737 /* This function is only called if there was *something* on the
4739 #ifdef ENABLE_CHECKING
4744 /* A parameter list consisting solely of 'void' indicates that the
4745 function takes no arguments. But if the 'void' is qualified
4746 (by 'const' or 'volatile'), or has a storage class specifier
4747 ('register'), then the behavior is undefined; issue an error.
4748 Typedefs for 'void' are OK (see DR#157). */
4749 if (b->prev == 0 /* one binding */
4750 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4751 && !DECL_NAME (b->decl) /* anonymous */
4752 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4754 if (TREE_THIS_VOLATILE (b->decl)
4755 || TREE_READONLY (b->decl)
4756 || C_DECL_REGISTER (b->decl))
4757 error ("'void' as only parameter may not be qualified");
4759 /* There cannot be an ellipsis. */
4761 error ("'void' must be the only parameter");
4763 ARG_INFO_TYPES (arg_info) = void_list_node;
4768 types = void_list_node;
4770 /* Break up the bindings list into parms, tags, types, and others;
4771 apply sanity checks; purge the name-to-decl bindings. */
4774 tree decl = b->decl;
4775 tree type = TREE_TYPE (decl);
4776 const char *keyword;
4778 switch (TREE_CODE (decl))
4783 #ifdef ENABLE_CHECKING
4784 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4786 I_SYMBOL_BINDING (b->id) = b->shadowed;
4789 /* Check for forward decls that never got their actual decl. */
4790 if (TREE_ASM_WRITTEN (decl))
4791 error ("%Jparameter '%D' has just a forward declaration",
4793 /* Check for (..., void, ...) and issue an error. */
4794 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4796 if (!gave_void_only_once_err)
4798 error ("'void' must be the only parameter");
4799 gave_void_only_once_err = true;
4804 /* Valid parameter, add it to the list. */
4805 TREE_CHAIN (decl) = parms;
4808 /* Since there is a prototype, args are passed in their
4809 declared types. The back end may override this later. */
4810 DECL_ARG_TYPE (decl) = type;
4811 types = tree_cons (0, type, types);
4815 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4816 case UNION_TYPE: keyword = "union"; goto tag;
4817 case RECORD_TYPE: keyword = "struct"; goto tag;
4819 /* Types may not have tag-names, in which case the type
4820 appears in the bindings list with b->id NULL. */
4823 #ifdef ENABLE_CHECKING
4824 if (I_TAG_BINDING (b->id) != b) abort ();
4826 I_TAG_BINDING (b->id) = b->shadowed;
4829 /* Warn about any struct, union or enum tags defined in a
4830 parameter list. The scope of such types is limited to
4831 the parameter list, which is rarely if ever desirable
4832 (it's impossible to call such a function with type-
4833 correct arguments). An anonymous union parm type is
4834 meaningful as a GNU extension, so don't warn for that. */
4835 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4838 /* The %s will be one of 'struct', 'union', or 'enum'. */
4839 warning ("'%s %E' declared inside parameter list",
4842 /* The %s will be one of 'struct', 'union', or 'enum'. */
4843 warning ("anonymous %s declared inside parameter list",
4846 if (! explained_incomplete_types)
4848 warning ("its scope is only this definition or declaration,"
4849 " which is probably not what you want");
4850 explained_incomplete_types = true;
4854 tags = tree_cons (b->id, decl, tags);
4859 /* CONST_DECLs appear here when we have an embedded enum,
4860 and TYPE_DECLs appear here when we have an embedded struct
4861 or union. No warnings for this - we already warned about the
4863 TREE_CHAIN (decl) = others;
4868 /* error_mark_node appears here when we have an undeclared
4869 variable. Just throw it away. */
4872 #ifdef ENABLE_CHECKING
4873 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4875 I_SYMBOL_BINDING (b->id) = b->shadowed;
4879 /* Other things that might be encountered. */
4887 b = free_binding_and_advance (b);
4890 ARG_INFO_PARMS (arg_info) = parms;
4891 ARG_INFO_TAGS (arg_info) = tags;
4892 ARG_INFO_TYPES (arg_info) = types;
4893 ARG_INFO_OTHERS (arg_info) = others;
4897 /* Get the struct, enum or union (CODE says which) with tag NAME.
4898 Define the tag as a forward-reference if it is not defined. */
4901 xref_tag (enum tree_code code, tree name)
4903 /* If a cross reference is requested, look up the type
4904 already defined for this tag and return it. */
4906 tree ref = lookup_tag (code, name, 0);
4907 /* If this is the right type of tag, return what we found.
4908 (This reference will be shadowed by shadow_tag later if appropriate.)
4909 If this is the wrong type of tag, do not return it. If it was the
4910 wrong type in the same scope, we will have had an error
4911 message already; if in a different scope and declaring
4912 a name, pending_xref_error will give an error message; but if in a
4913 different scope and not declaring a name, this tag should
4914 shadow the previous declaration of a different type of tag, and
4915 this would not work properly if we return the reference found.
4916 (For example, with "struct foo" in an outer scope, "union foo;"
4917 must shadow that tag with a new one of union type.) */
4918 if (ref && TREE_CODE (ref) == code)
4921 /* If no such tag is yet defined, create a forward-reference node
4922 and record it as the "definition".
4923 When a real declaration of this type is found,
4924 the forward-reference will be altered into a real type. */
4926 ref = make_node (code);
4927 if (code == ENUMERAL_TYPE)
4929 /* Give the type a default layout like unsigned int
4930 to avoid crashing if it does not get defined. */
4931 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4932 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4933 TYPE_USER_ALIGN (ref) = 0;
4934 TYPE_UNSIGNED (ref) = 1;
4935 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4936 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4937 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4940 pushtag (name, ref);
4945 /* Make sure that the tag NAME is defined *in the current scope*
4946 at least as a forward reference.
4947 CODE says which kind of tag NAME ought to be. */
4950 start_struct (enum tree_code code, tree name)
4952 /* If there is already a tag defined at this scope
4953 (as a forward reference), just return it. */
4958 ref = lookup_tag (code, name, 1);
4959 if (ref && TREE_CODE (ref) == code)
4961 if (TYPE_FIELDS (ref))
4963 if (code == UNION_TYPE)
4964 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4966 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4971 /* Otherwise create a forward-reference just so the tag is in scope. */
4973 ref = make_node (code);
4974 pushtag (name, ref);
4977 C_TYPE_BEING_DEFINED (ref) = 1;
4978 TYPE_PACKED (ref) = flag_pack_struct;
4982 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4983 of a structure component, returning a FIELD_DECL node.
4984 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4986 This is done during the parsing of the struct declaration.
4987 The FIELD_DECL nodes are chained together and the lot of them
4988 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4991 grokfield (tree declarator, tree declspecs, tree width)
4995 if (declarator == NULL_TREE && width == NULL_TREE)
4997 /* This is an unnamed decl.
4999 If we have something of the form "union { list } ;" then this
5000 is the anonymous union extension. Similarly for struct.
5002 If this is something of the form "struct foo;", then
5003 If MS extensions are enabled, this is handled as an
5005 Otherwise this is a forward declaration of a structure tag.
5007 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5008 If MS extensions are enabled and foo names a structure, then
5009 again this is an anonymous struct.
5010 Otherwise this is an error.
5012 Oh what a horrid tangled web we weave. I wonder if MS consciously
5013 took this from Plan 9 or if it was an accident of implementation
5014 that took root before someone noticed the bug... */
5016 tree type = TREE_VALUE (declspecs);
5018 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5019 type = TREE_TYPE (type);
5020 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5022 if (flag_ms_extensions)
5025 goto warn_unnamed_field;
5026 else if (TYPE_NAME (type) == NULL)
5029 goto warn_unnamed_field;
5034 warning ("declaration does not declare anything");
5039 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5040 width ? &width : NULL);
5042 finish_decl (value, NULL_TREE, NULL_TREE);
5043 DECL_INITIAL (value) = width;
5048 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5049 the list such that this does not present a problem later. */
5052 detect_field_duplicates (tree fieldlist)
5057 /* First, see if there are more than "a few" fields.
5058 This is trivially true if there are zero or one fields. */
5061 x = TREE_CHAIN (fieldlist);
5067 } while (timeout > 0 && x);
5069 /* If there were "few" fields, avoid the overhead of allocating
5070 a hash table. Instead just do the nested traversal thing. */
5073 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5076 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5077 if (DECL_NAME (y) == DECL_NAME (x))
5079 error ("%Jduplicate member '%D'", x, x);
5080 DECL_NAME (x) = NULL_TREE;
5086 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5089 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5090 if ((y = DECL_NAME (x)) != 0)
5092 slot = htab_find_slot (htab, y, INSERT);
5095 error ("%Jduplicate member '%D'", x, x);
5096 DECL_NAME (x) = NULL_TREE;
5105 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5106 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5107 ATTRIBUTES are attributes to be applied to the structure. */
5110 finish_struct (tree t, tree fieldlist, tree attributes)
5113 bool toplevel = file_scope == current_scope;
5114 int saw_named_field;
5116 /* If this type was previously laid out as a forward reference,
5117 make sure we lay it out again. */
5121 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5125 for (x = fieldlist; x; x = TREE_CHAIN (x))
5126 if (DECL_NAME (x) != 0)
5130 pedwarn ("%s has no %s",
5131 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5132 fieldlist ? _("named members") : _("members"));
5135 /* Install struct as DECL_CONTEXT of each field decl.
5136 Also process specified field sizes,m which is found in the DECL_INITIAL.
5137 Store 0 there, except for ": 0" fields (so we can find them
5138 and delete them, below). */
5140 saw_named_field = 0;
5141 for (x = fieldlist; x; x = TREE_CHAIN (x))
5143 DECL_CONTEXT (x) = t;
5144 DECL_PACKED (x) |= TYPE_PACKED (t);
5146 /* If any field is const, the structure type is pseudo-const. */
5147 if (TREE_READONLY (x))
5148 C_TYPE_FIELDS_READONLY (t) = 1;
5151 /* A field that is pseudo-const makes the structure likewise. */
5152 tree t1 = TREE_TYPE (x);
5153 while (TREE_CODE (t1) == ARRAY_TYPE)
5154 t1 = TREE_TYPE (t1);
5155 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5156 && C_TYPE_FIELDS_READONLY (t1))
5157 C_TYPE_FIELDS_READONLY (t) = 1;
5160 /* Any field that is volatile means variables of this type must be
5161 treated in some ways as volatile. */
5162 if (TREE_THIS_VOLATILE (x))
5163 C_TYPE_FIELDS_VOLATILE (t) = 1;
5165 /* Any field of nominal variable size implies structure is too. */
5166 if (C_DECL_VARIABLE_SIZE (x))
5167 C_TYPE_VARIABLE_SIZE (t) = 1;
5169 /* Detect invalid nested redefinition. */
5170 if (TREE_TYPE (x) == t)
5171 error ("nested redefinition of `%s'",
5172 IDENTIFIER_POINTER (TYPE_NAME (t)));
5174 if (DECL_INITIAL (x))
5176 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5177 DECL_SIZE (x) = bitsize_int (width);
5178 DECL_BIT_FIELD (x) = 1;
5179 SET_DECL_C_BIT_FIELD (x);
5182 DECL_INITIAL (x) = 0;
5184 /* Detect flexible array member in an invalid context. */
5185 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5186 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5187 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5188 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5190 if (TREE_CODE (t) == UNION_TYPE)
5192 error ("%Jflexible array member in union", x);
5193 TREE_TYPE (x) = error_mark_node;
5195 else if (TREE_CHAIN (x) != NULL_TREE)
5197 error ("%Jflexible array member not at end of struct", x);
5198 TREE_TYPE (x) = error_mark_node;
5200 else if (! saw_named_field)
5202 error ("%Jflexible array member in otherwise empty struct", x);
5203 TREE_TYPE (x) = error_mark_node;
5207 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5208 && flexible_array_type_p (TREE_TYPE (x)))
5209 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5212 saw_named_field = 1;
5215 detect_field_duplicates (fieldlist);
5217 /* Now we have the nearly final fieldlist. Record it,
5218 then lay out the structure or union (including the fields). */
5220 TYPE_FIELDS (t) = fieldlist;
5224 /* Delete all zero-width bit-fields from the fieldlist. */
5226 tree *fieldlistp = &fieldlist;
5228 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5229 *fieldlistp = TREE_CHAIN (*fieldlistp);
5231 fieldlistp = &TREE_CHAIN (*fieldlistp);
5234 /* Now we have the truly final field list.
5235 Store it in this type and in the variants. */
5237 TYPE_FIELDS (t) = fieldlist;
5239 /* If there are lots of fields, sort so we can look through them fast.
5240 We arbitrarily consider 16 or more elts to be "a lot". */
5245 for (x = fieldlist; x; x = TREE_CHAIN (x))
5247 if (len > 15 || DECL_NAME (x) == NULL)
5255 struct lang_type *space;
5256 struct sorted_fields_type *space2;
5258 len += list_length (x);
5260 /* Use the same allocation policy here that make_node uses, to
5261 ensure that this lives as long as the rest of the struct decl.
5262 All decls in an inline function need to be saved. */
5264 space = ggc_alloc_cleared (sizeof (struct lang_type));
5265 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5269 field_array = &space2->elts[0];
5270 for (x = fieldlist; x; x = TREE_CHAIN (x))
5272 field_array[len++] = x;
5274 /* If there is anonymous struct or union, break out of the loop. */
5275 if (DECL_NAME (x) == NULL)
5278 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5281 TYPE_LANG_SPECIFIC (t) = space;
5282 TYPE_LANG_SPECIFIC (t)->s->len = len;
5283 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5284 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5289 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5291 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5292 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5293 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5294 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5297 /* If this was supposed to be a transparent union, but we can't
5298 make it one, warn and turn off the flag. */
5299 if (TREE_CODE (t) == UNION_TYPE
5300 && TYPE_TRANSPARENT_UNION (t)
5301 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5303 TYPE_TRANSPARENT_UNION (t) = 0;
5304 warning ("union cannot be made transparent");
5307 /* If this structure or union completes the type of any previous
5308 variable declaration, lay it out and output its rtl. */
5309 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5313 tree decl = TREE_VALUE (x);
5314 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5315 layout_array_type (TREE_TYPE (decl));
5316 if (TREE_CODE (decl) != TYPE_DECL)
5318 layout_decl (decl, 0);
5319 if (c_dialect_objc ())
5320 objc_check_decl (decl);
5321 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5326 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5328 /* Finish debugging output for this type. */
5329 rest_of_type_compilation (t, toplevel);
5334 /* Lay out the type T, and its element type, and so on. */
5337 layout_array_type (tree t)
5339 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5340 layout_array_type (TREE_TYPE (t));
5344 /* Begin compiling the definition of an enumeration type.
5345 NAME is its name (or null if anonymous).
5346 Returns the type object, as yet incomplete.
5347 Also records info about it so that build_enumerator
5348 may be used to declare the individual values as they are read. */
5351 start_enum (tree name)
5355 /* If this is the real definition for a previous forward reference,
5356 fill in the contents in the same object that used to be the
5357 forward reference. */
5360 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5362 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5364 enumtype = make_node (ENUMERAL_TYPE);
5365 pushtag (name, enumtype);
5368 C_TYPE_BEING_DEFINED (enumtype) = 1;
5370 if (TYPE_VALUES (enumtype) != 0)
5372 /* This enum is a named one that has been declared already. */
5373 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5375 /* Completely replace its old definition.
5376 The old enumerators remain defined, however. */
5377 TYPE_VALUES (enumtype) = 0;
5380 enum_next_value = integer_zero_node;
5383 if (flag_short_enums)
5384 TYPE_PACKED (enumtype) = 1;
5389 /* After processing and defining all the values of an enumeration type,
5390 install their decls in the enumeration type and finish it off.
5391 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5392 and ATTRIBUTES are the specified attributes.
5393 Returns ENUMTYPE. */
5396 finish_enum (tree enumtype, tree values, tree attributes)
5399 tree minnode = 0, maxnode = 0;
5400 int precision, unsign;
5401 bool toplevel = (file_scope == current_scope);
5402 struct lang_type *lt;
5404 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5406 /* Calculate the maximum value of any enumerator in this type. */
5408 if (values == error_mark_node)
5409 minnode = maxnode = integer_zero_node;
5412 minnode = maxnode = TREE_VALUE (values);
5413 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5415 tree value = TREE_VALUE (pair);
5416 if (tree_int_cst_lt (maxnode, value))
5418 if (tree_int_cst_lt (value, minnode))
5423 /* Construct the final type of this enumeration. It is the same
5424 as one of the integral types - the narrowest one that fits, except
5425 that normally we only go as narrow as int - and signed iff any of
5426 the values are negative. */
5427 unsign = (tree_int_cst_sgn (minnode) >= 0);
5428 precision = MAX (min_precision (minnode, unsign),
5429 min_precision (maxnode, unsign));
5430 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5432 tem = c_common_type_for_size (precision, unsign);
5435 warning ("enumeration values exceed range of largest integer");
5436 tem = long_long_integer_type_node;
5440 tem = unsign ? unsigned_type_node : integer_type_node;
5442 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5443 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5444 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5445 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5446 TYPE_SIZE (enumtype) = 0;
5447 layout_type (enumtype);
5449 if (values != error_mark_node)
5451 /* Change the type of the enumerators to be the enum type. We
5452 need to do this irrespective of the size of the enum, for
5453 proper type checking. Replace the DECL_INITIALs of the
5454 enumerators, and the value slots of the list, with copies
5455 that have the enum type; they cannot be modified in place
5456 because they may be shared (e.g. integer_zero_node) Finally,
5457 change the purpose slots to point to the names of the decls. */
5458 for (pair = values; pair; pair = TREE_CHAIN (pair))
5460 tree enu = TREE_PURPOSE (pair);
5461 tree ini = DECL_INITIAL (enu);
5463 TREE_TYPE (enu) = enumtype;
5465 /* The ISO C Standard mandates enumerators to have type int,
5466 even though the underlying type of an enum type is
5467 unspecified. Here we convert any enumerators that fit in
5468 an int to type int, to avoid promotions to unsigned types
5469 when comparing integers with enumerators that fit in the
5470 int range. When -pedantic is given, build_enumerator()
5471 would have already taken care of those that don't fit. */
5472 if (int_fits_type_p (ini, integer_type_node))
5473 tem = integer_type_node;
5476 ini = convert (tem, ini);
5478 DECL_INITIAL (enu) = ini;
5479 TREE_PURPOSE (pair) = DECL_NAME (enu);
5480 TREE_VALUE (pair) = ini;
5483 TYPE_VALUES (enumtype) = values;
5486 /* Record the min/max values so that we can warn about bit-field
5487 enumerations that are too small for the values. */
5488 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5489 lt->enum_min = minnode;
5490 lt->enum_max = maxnode;
5491 TYPE_LANG_SPECIFIC (enumtype) = lt;
5493 /* Fix up all variant types of this enum type. */
5494 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5496 if (tem == enumtype)
5498 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5499 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5500 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5501 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5502 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5503 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5504 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5505 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5506 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5507 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5508 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5511 /* Finish debugging output for this type. */
5512 rest_of_type_compilation (enumtype, toplevel);
5517 /* Build and install a CONST_DECL for one value of the
5518 current enumeration type (one that was begun with start_enum).
5519 Return a tree-list containing the CONST_DECL and its value.
5520 Assignment of sequential values by default is handled here. */
5523 build_enumerator (tree name, tree value)
5527 /* Validate and default VALUE. */
5529 /* Remove no-op casts from the value. */
5531 STRIP_TYPE_NOPS (value);
5535 /* Don't issue more errors for error_mark_node (i.e. an
5536 undeclared identifier) - just ignore the value expression. */
5537 if (value == error_mark_node)
5539 else if (TREE_CODE (value) != INTEGER_CST)
5541 error ("enumerator value for '%E' is not an integer constant", name);
5546 value = default_conversion (value);
5547 constant_expression_warning (value);
5551 /* Default based on previous value. */
5552 /* It should no longer be possible to have NON_LVALUE_EXPR
5556 value = enum_next_value;
5558 error ("overflow in enumeration values");
5561 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5563 pedwarn ("ISO C restricts enumerator values to range of `int'");
5564 /* XXX This causes -pedantic to change the meaning of the program.
5565 Remove? -zw 2004-03-15 */
5566 value = convert (integer_type_node, value);
5569 /* Set basis for default for next value. */
5570 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5571 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5573 /* Now create a declaration for the enum value name. */
5575 type = TREE_TYPE (value);
5576 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5577 TYPE_PRECISION (integer_type_node)),
5578 (TYPE_PRECISION (type)
5579 >= TYPE_PRECISION (integer_type_node)
5580 && TYPE_UNSIGNED (type)));
5582 decl = build_decl (CONST_DECL, name, type);
5583 DECL_INITIAL (decl) = convert (type, value);
5586 return tree_cons (decl, value, NULL_TREE);
5590 /* Create the FUNCTION_DECL for a function definition.
5591 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5592 the declaration; they describe the function's name and the type it returns,
5593 but twisted together in a fashion that parallels the syntax of C.
5595 This function creates a binding context for the function body
5596 as well as setting up the FUNCTION_DECL in current_function_decl.
5598 Returns 1 on success. If the DECLARATOR is not suitable for a function
5599 (it defines a datum instead), we return 0, which tells
5600 yyparse to report a parse error. */
5603 start_function (tree declspecs, tree declarator, tree attributes)
5605 tree decl1, old_decl;
5607 int old_immediate_size_expand = immediate_size_expand;
5609 current_function_returns_value = 0; /* Assume, until we see it does. */
5610 current_function_returns_null = 0;
5611 current_function_returns_abnormally = 0;
5612 warn_about_return_type = 0;
5613 current_extern_inline = 0;
5614 c_in_iteration_stmt = 0;
5617 /* Don't expand any sizes in the return type of the function. */
5618 immediate_size_expand = 0;
5620 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5622 /* If the declarator is not suitable for a function definition,
5623 cause a syntax error. */
5626 immediate_size_expand = old_immediate_size_expand;
5630 decl_attributes (&decl1, attributes, 0);
5632 if (DECL_DECLARED_INLINE_P (decl1)
5633 && DECL_UNINLINABLE (decl1)
5634 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5635 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5637 announce_function (decl1);
5639 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5641 error ("return type is an incomplete type");
5642 /* Make it return void instead. */
5644 = build_function_type (void_type_node,
5645 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5648 if (warn_about_return_type)
5649 pedwarn_c99 ("return type defaults to `int'");
5651 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5652 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5653 DECL_INITIAL (decl1) = error_mark_node;
5655 /* If this definition isn't a prototype and we had a prototype declaration
5656 before, copy the arg type info from that prototype.
5657 But not if what we had before was a builtin function. */
5658 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5659 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5660 && !DECL_BUILT_IN (old_decl)
5661 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5662 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5663 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5665 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5666 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5669 /* Optionally warn of old-fashioned def with no previous prototype. */
5670 if (warn_strict_prototypes
5671 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5672 && C_DECL_ISNT_PROTOTYPE (old_decl))
5673 warning ("function declaration isn't a prototype");
5674 /* Optionally warn of any global def with no previous prototype. */
5675 else if (warn_missing_prototypes
5676 && TREE_PUBLIC (decl1)
5677 && ! MAIN_NAME_P (DECL_NAME (decl1))
5678 && C_DECL_ISNT_PROTOTYPE (old_decl))
5679 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5680 /* Optionally warn of any def with no previous prototype
5681 if the function has already been used. */
5682 else if (warn_missing_prototypes
5683 && old_decl != 0 && TREE_USED (old_decl)
5684 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5685 warning ("%J'%D' was used with no prototype before its definition",
5687 /* Optionally warn of any global def with no previous declaration. */
5688 else if (warn_missing_declarations
5689 && TREE_PUBLIC (decl1)
5691 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5692 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5693 /* Optionally warn of any def with no previous declaration
5694 if the function has already been used. */
5695 else if (warn_missing_declarations
5696 && old_decl != 0 && TREE_USED (old_decl)
5697 && C_DECL_IMPLICIT (old_decl))
5698 warning ("%J`%D' was used with no declaration before its definition",
5701 /* This is a definition, not a reference.
5702 So normally clear DECL_EXTERNAL.
5703 However, `extern inline' acts like a declaration
5704 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5705 DECL_EXTERNAL (decl1) = current_extern_inline;
5707 /* This function exists in static storage.
5708 (This does not mean `static' in the C sense!) */
5709 TREE_STATIC (decl1) = 1;
5711 /* A nested function is not global. */
5712 if (current_function_decl != 0)
5713 TREE_PUBLIC (decl1) = 0;
5715 #ifdef ENABLE_CHECKING
5716 /* This is the earliest point at which we might know the assembler
5717 name of the function. Thus, if it's set before this, die horribly. */
5718 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5722 /* If #pragma weak was used, mark the decl weak now. */
5723 if (current_scope == file_scope)
5724 maybe_apply_pragma_weak (decl1);
5726 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5727 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5732 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5733 != integer_type_node)
5734 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5736 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5737 args = TREE_CHAIN (args))
5739 tree type = args ? TREE_VALUE (args) : 0;
5741 if (type == void_type_node)
5748 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5749 pedwarn ("%Jfirst argument of '%D' should be `int'",
5754 if (TREE_CODE (type) != POINTER_TYPE
5755 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5756 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5758 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5763 if (TREE_CODE (type) != POINTER_TYPE
5764 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5765 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5767 pedwarn ("%Jthird argument of '%D' should probably be "
5768 "'char **'", decl1, decl1);
5773 /* It is intentional that this message does not mention the third
5774 argument because it's only mentioned in an appendix of the
5776 if (argct > 0 && (argct < 2 || argct > 3))
5777 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5779 if (! TREE_PUBLIC (decl1))
5780 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5783 /* Record the decl so that the function name is defined.
5784 If we already have a decl for this name, and it is a FUNCTION_DECL,
5785 use the old decl. */
5787 current_function_decl = pushdecl (decl1);
5790 declare_parm_level ();
5792 make_decl_rtl (current_function_decl, NULL);
5794 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5795 /* Promote the value to int before returning it. */
5796 if (c_promoting_integer_type_p (restype))
5798 /* It retains unsignedness if not really getting wider. */
5799 if (TYPE_UNSIGNED (restype)
5800 && (TYPE_PRECISION (restype)
5801 == TYPE_PRECISION (integer_type_node)))
5802 restype = unsigned_type_node;
5804 restype = integer_type_node;
5806 DECL_RESULT (current_function_decl)
5807 = build_decl (RESULT_DECL, NULL_TREE, restype);
5809 /* If this fcn was already referenced via a block-scope `extern' decl
5810 (or an implicit decl), propagate certain information about the usage. */
5811 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5812 TREE_ADDRESSABLE (current_function_decl) = 1;
5814 immediate_size_expand = old_immediate_size_expand;
5816 start_fname_decls ();
5821 /* Subroutine of store_parm_decls which handles new-style function
5822 definitions (prototype format). The parms already have decls, so we
5823 need only record them as in effect and complain if any redundant
5824 old-style parm decls were written. */
5826 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5829 tree parms = ARG_INFO_PARMS (arg_info);
5830 tree tags = ARG_INFO_TAGS (arg_info);
5831 tree others = ARG_INFO_OTHERS (arg_info);
5833 if (current_scope->bindings)
5835 error ("%Jold-style parameter declarations in prototyped "
5836 "function definition", fndecl);
5838 /* Get rid of the old-style declarations. */
5842 /* Don't issue this warning for nested functions, and don't issue this
5843 warning if we got here because ARG_INFO_TYPES was error_mark_node
5844 (this happens when a function definition has just an ellipsis in
5845 its parameter list). */
5846 else if (warn_traditional && !in_system_header && !current_function_scope
5847 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5848 warning ("%Jtraditional C rejects ISO C style function definitions",
5851 /* Now make all the parameter declarations visible in the function body.
5852 We can bypass most of the grunt work of pushdecl. */
5853 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5855 DECL_CONTEXT (decl) = current_function_decl;
5856 if (DECL_NAME (decl))
5857 bind (DECL_NAME (decl), decl, current_scope);
5859 error ("%Jparameter name omitted", decl);
5862 /* Record the parameter list in the function declaration. */
5863 DECL_ARGUMENTS (fndecl) = parms;
5865 /* Now make all the ancillary declarations visible, likewise. */
5866 for (decl = others; decl; decl = TREE_CHAIN (decl))
5868 DECL_CONTEXT (decl) = current_function_decl;
5869 if (DECL_NAME (decl))
5870 bind (DECL_NAME (decl), decl, current_scope);
5873 /* And all the tag declarations. */
5874 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5875 if (TREE_PURPOSE (decl))
5876 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope);
5879 /* Subroutine of store_parm_decls which handles old-style function
5880 definitions (separate parameter list and declarations). */
5883 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5885 struct c_binding *b;
5886 tree parm, decl, last;
5887 tree parmids = ARG_INFO_PARMS (arg_info);
5889 /* We use DECL_WEAK as a flag to show which parameters have been
5890 seen already, since it is not used on PARM_DECL. */
5891 #ifdef ENABLE_CHECKING
5892 for (b = current_scope->bindings; b; b = b->prev)
5893 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5897 if (warn_old_style_definition && !in_system_header)
5898 warning ("%Jold-style function definition", fndecl);
5900 /* Match each formal parameter name with its declaration. Save each
5901 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5902 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5904 if (TREE_VALUE (parm) == 0)
5906 error ("%Jparameter name missing from parameter list", fndecl);
5907 TREE_PURPOSE (parm) = 0;
5911 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5912 if (b && b->contour == current_scope)
5915 /* If we got something other than a PARM_DECL it is an error. */
5916 if (TREE_CODE (decl) != PARM_DECL)
5917 error ("%J'%D' declared as a non-parameter", decl, decl);
5918 /* If the declaration is already marked, we have a duplicate
5919 name. Complain and ignore the duplicate. */
5920 else if (DECL_WEAK (decl))
5922 error ("%Jmultiple parameters named '%D'", decl, decl);
5923 TREE_PURPOSE (parm) = 0;
5926 /* If the declaration says "void", complain and turn it into
5928 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5930 error ("%Jparameter '%D' declared with void type", decl, decl);
5931 TREE_TYPE (decl) = integer_type_node;
5932 DECL_ARG_TYPE (decl) = integer_type_node;
5933 layout_decl (decl, 0);
5936 /* If no declaration found, default to int. */
5939 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5940 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5941 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5945 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5946 else if (extra_warnings)
5947 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5950 TREE_PURPOSE (parm) = decl;
5951 DECL_WEAK (decl) = 1;
5954 /* Now examine the parms chain for incomplete declarations
5955 and declarations with no corresponding names. */
5957 for (b = current_scope->bindings; b; b = b->prev)
5960 if (TREE_CODE (parm) != PARM_DECL)
5963 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5965 error ("%Jparameter '%D' has incomplete type", parm, parm);
5966 TREE_TYPE (parm) = error_mark_node;
5969 if (! DECL_WEAK (parm))
5971 error ("%Jdeclaration for parameter '%D' but no such parameter",
5974 /* Pretend the parameter was not missing.
5975 This gets us to a standard state and minimizes
5976 further error messages. */
5977 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5981 /* Chain the declarations together in the order of the list of
5982 names. Store that chain in the function decl, replacing the
5983 list of names. Update the current scope to match. */
5984 DECL_ARGUMENTS (fndecl) = 0;
5986 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5987 if (TREE_PURPOSE (parm))
5989 if (parm && TREE_PURPOSE (parm))
5991 last = TREE_PURPOSE (parm);
5992 DECL_ARGUMENTS (fndecl) = last;
5993 DECL_WEAK (last) = 0;
5995 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5996 if (TREE_PURPOSE (parm))
5998 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5999 last = TREE_PURPOSE (parm);
6000 DECL_WEAK (last) = 0;
6002 TREE_CHAIN (last) = 0;
6005 /* If there was a previous prototype,
6006 set the DECL_ARG_TYPE of each argument according to
6007 the type previously specified, and report any mismatches. */
6009 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6012 for (parm = DECL_ARGUMENTS (fndecl),
6013 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6014 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6015 != void_type_node));
6016 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6018 if (parm == 0 || type == 0
6019 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6021 error ("number of arguments doesn't match prototype");
6022 error ("%Hprototype declaration",
6023 ¤t_function_prototype_locus);
6026 /* Type for passing arg must be consistent with that
6027 declared for the arg. ISO C says we take the unqualified
6028 type for parameters declared with qualified type. */
6029 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6030 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6033 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6034 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6036 /* Adjust argument to match prototype. E.g. a previous
6037 `int foo(float);' prototype causes
6038 `int foo(x) float x; {...}' to be treated like
6039 `int foo(float x) {...}'. This is particularly
6040 useful for argument types like uid_t. */
6041 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6043 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6044 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6045 && TYPE_PRECISION (TREE_TYPE (parm))
6046 < TYPE_PRECISION (integer_type_node))
6047 DECL_ARG_TYPE (parm) = integer_type_node;
6051 pedwarn ("promoted argument '%D' "
6052 "doesn't match prototype", parm);
6053 pedwarn ("%Hprototype declaration",
6054 ¤t_function_prototype_locus);
6059 error ("argument '%D' doesn't match prototype", parm);
6060 error ("%Hprototype declaration",
6061 ¤t_function_prototype_locus);
6065 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6068 /* Otherwise, create a prototype that would match. */
6072 tree actual = 0, last = 0, type;
6074 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6076 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6078 TREE_CHAIN (last) = type;
6083 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6085 TREE_CHAIN (last) = type;
6089 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6090 of the type of this function, but we need to avoid having this
6091 affect the types of other similarly-typed functions, so we must
6092 first force the generation of an identical (but separate) type
6093 node for the relevant function type. The new node we create
6094 will be a variant of the main variant of the original function
6097 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6099 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6103 /* A subroutine of store_parm_decls called via walk_tree. Mark all
6107 set_decl_nonlocal (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
6113 DECL_NONLOCAL (t) = 1;
6116 else if (TYPE_P (t))
6122 /* Store the parameter declarations into the current function declaration.
6123 This is called after parsing the parameter declarations, before
6124 digesting the body of the function.
6126 For an old-style definition, construct a prototype out of the old-style
6127 parameter declarations and inject it into the function's type. */
6130 store_parm_decls (void)
6132 tree fndecl = current_function_decl;
6134 /* The function containing FNDECL, if any. */
6135 tree context = decl_function_context (fndecl);
6137 /* The argument information block for FNDECL. */
6138 tree arg_info = DECL_ARGUMENTS (fndecl);
6140 /* True if this definition is written with a prototype. Note:
6141 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6142 list in a function definition as equivalent to (void) -- an
6143 empty argument list specifies the function has no parameters,
6144 but only (void) sets up a prototype for future calls. */
6145 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6148 store_parm_decls_newstyle (fndecl, arg_info);
6150 store_parm_decls_oldstyle (fndecl, arg_info);
6152 /* The next call to push_scope will be a function body. */
6154 next_is_function_body = true;
6156 /* Write a record describing this function definition to the prototypes
6157 file (if requested). */
6159 gen_aux_info_record (fndecl, 1, 0, proto);
6161 /* Initialize the RTL code for the function. */
6162 allocate_struct_function (fndecl);
6164 /* Begin the statement tree for this function. */
6165 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6167 /* If this is a nested function, save away the sizes of any
6168 variable-size types so that we can expand them when generating
6174 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6175 = nreverse (get_pending_sizes ());
6176 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6180 /* We will have a nonlocal use of whatever variables are
6181 buried inside here. */
6182 walk_tree (&TREE_OPERAND (TREE_VALUE (t), 0),
6183 set_decl_nonlocal, NULL, NULL);
6185 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6189 /* This function is being processed in whole-function mode. */
6190 cfun->x_whole_function_mode_p = 1;
6192 /* Even though we're inside a function body, we still don't want to
6193 call expand_expr to calculate the size of a variable-sized array.
6194 We haven't necessarily assigned RTL to all variables yet, so it's
6195 not safe to try to expand expressions involving them. */
6196 immediate_size_expand = 0;
6197 cfun->x_dont_save_pending_sizes_p = 1;
6200 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6203 c_finalize (tree fndecl)
6205 struct cgraph_node *cgn;
6207 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6208 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6210 /* ??? Objc emits functions after finalizing the compilation unit.
6211 This should be cleaned up later and this conditional removed. */
6212 if (cgraph_global_info_ready)
6214 c_expand_body (fndecl);
6218 /* Finalize all nested functions now. */
6219 cgn = cgraph_node (fndecl);
6220 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6221 c_finalize (cgn->decl);
6223 cgraph_finalize_function (fndecl, false);
6226 /* Finish up a function declaration and compile that function
6227 all the way to assembler language output. The free the storage
6228 for the function definition.
6230 This is called after parsing the body of the function definition. */
6233 finish_function (void)
6235 tree fndecl = current_function_decl;
6237 /* When a function declaration is totally empty, e.g.
6239 (the argument list is irrelevant) the compstmt rule will not
6240 bother calling push_scope/pop_scope, which means we get here with
6241 the scope stack out of sync. Detect this situation by noticing
6242 that current_scope is still as store_parm_decls left it, and do
6243 a dummy push/pop to get back to consistency.
6244 Note that the call to push_scope does not actually push another
6245 scope - see there for details. */
6247 if (current_scope->parm_flag && next_is_function_body)
6253 if (TREE_CODE (fndecl) == FUNCTION_DECL
6254 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6256 tree args = DECL_ARGUMENTS (fndecl);
6257 for (; args; args = TREE_CHAIN (args))
6259 tree type = TREE_TYPE (args);
6260 if (INTEGRAL_TYPE_P (type)
6261 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6262 DECL_ARG_TYPE (args) = integer_type_node;
6266 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6267 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6269 /* Must mark the RESULT_DECL as being in this function. */
6271 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6272 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6274 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6276 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6277 != integer_type_node)
6279 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6280 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6282 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6286 #ifdef DEFAULT_MAIN_RETURN
6287 /* Make it so that `main' always returns success by default. */
6288 DEFAULT_MAIN_RETURN;
6291 c_expand_return (integer_zero_node);
6296 finish_fname_decls ();
6298 /* Tie off the statement tree for this function. */
6299 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6301 /* Complain if there's just no return statement. */
6302 if (warn_return_type
6303 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6304 && !current_function_returns_value && !current_function_returns_null
6305 /* Don't complain if we abort. */
6306 && !current_function_returns_abnormally
6307 /* Don't warn for main(). */
6308 && !MAIN_NAME_P (DECL_NAME (fndecl))
6309 /* Or if they didn't actually specify a return type. */
6310 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6311 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6312 inline function, as we might never be compiled separately. */
6313 && DECL_INLINE (fndecl))
6314 warning ("no return statement in function returning non-void");
6316 /* With just -Wextra, complain only if function returns both with
6317 and without a value. */
6319 && current_function_returns_value
6320 && current_function_returns_null)
6321 warning ("this function may return with or without a value");
6323 /* Store the end of the function, so that we get good line number
6324 info for the epilogue. */
6325 cfun->function_end_locus = input_location;
6327 /* If we don't have ctors/dtors sections, and this is a static
6328 constructor or destructor, it must be recorded now. */
6329 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6330 && !targetm.have_ctors_dtors)
6331 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6332 if (DECL_STATIC_DESTRUCTOR (fndecl)
6333 && !targetm.have_ctors_dtors)
6334 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6336 /* Genericize before inlining. Delay genericizing nested functions
6337 until their parent function is genericized. Since finalizing
6338 requires GENERIC, delay that as well. */
6340 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6342 if (!decl_function_context (fndecl))
6344 c_genericize (fndecl);
6345 lower_nested_functions (fndecl);
6346 c_finalize (fndecl);
6350 /* Register this function with cgraph just far enough to get it
6351 added to our parent's nested function list. Handy, since the
6352 C front end doesn't have such a list. */
6353 (void) cgraph_node (fndecl);
6357 /* We're leaving the context of this function, so zap cfun.
6358 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6359 tree_rest_of_compilation. */
6361 current_function_decl = NULL;
6364 /* Generate the RTL for the body of FNDECL. */
6367 c_expand_body (tree fndecl)
6370 if (!DECL_INITIAL (fndecl)
6371 || DECL_INITIAL (fndecl) == error_mark_node)
6374 tree_rest_of_compilation (fndecl, false);
6376 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6377 && targetm.have_ctors_dtors)
6378 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6379 DEFAULT_INIT_PRIORITY);
6380 if (DECL_STATIC_DESTRUCTOR (fndecl)
6381 && targetm.have_ctors_dtors)
6382 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6383 DEFAULT_INIT_PRIORITY);
6386 /* Check the declarations given in a for-loop for satisfying the C99
6389 check_for_loop_decls (void)
6391 struct c_binding *b;
6395 /* If we get here, declarations have been used in a for loop without
6396 the C99 for loop scope. This doesn't make much sense, so don't
6398 error ("'for' loop initial declaration used outside C99 mode");
6401 /* C99 subclause 6.8.5 paragraph 3:
6403 [#3] The declaration part of a for statement shall only
6404 declare identifiers for objects having storage class auto or
6407 It isn't clear whether, in this sentence, "identifiers" binds to
6408 "shall only declare" or to "objects" - that is, whether all identifiers
6409 declared must be identifiers for objects, or whether the restriction
6410 only applies to those that are. (A question on this in comp.std.c
6411 in November 2000 received no answer.) We implement the strictest
6412 interpretation, to avoid creating an extension which later causes
6415 for (b = current_scope->bindings; b; b = b->prev)
6418 tree decl = b->decl;
6423 switch (TREE_CODE (decl))
6426 if (TREE_STATIC (decl))
6427 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6428 "initial declaration", decl, decl);
6429 else if (DECL_EXTERNAL (decl))
6430 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6431 "initial declaration", decl, decl);
6435 error ("'struct %E' declared in 'for' loop initial declaration", id);
6438 error ("'union %E' declared in 'for' loop initial declaration", id);
6441 error ("'enum %E' declared in 'for' loop initial declaration", id);
6444 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6445 "initial declaration", decl, decl);
6450 /* Save and reinitialize the variables
6451 used during compilation of a C function. */
6454 c_push_function_context (struct function *f)
6456 struct language_function *p;
6457 p = ggc_alloc (sizeof (struct language_function));
6460 p->base.x_stmt_tree = c_stmt_tree;
6461 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6462 p->x_in_iteration_stmt = c_in_iteration_stmt;
6463 p->x_in_case_stmt = c_in_case_stmt;
6464 p->returns_value = current_function_returns_value;
6465 p->returns_null = current_function_returns_null;
6466 p->returns_abnormally = current_function_returns_abnormally;
6467 p->warn_about_return_type = warn_about_return_type;
6468 p->extern_inline = current_extern_inline;
6471 /* Restore the variables used during compilation of a C function. */
6474 c_pop_function_context (struct function *f)
6476 struct language_function *p = f->language;
6478 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6479 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6481 /* Stop pointing to the local nodes about to be freed. */
6482 /* But DECL_INITIAL must remain nonzero so we know this
6483 was an actual function definition. */
6484 DECL_INITIAL (current_function_decl) = error_mark_node;
6485 DECL_ARGUMENTS (current_function_decl) = 0;
6488 c_stmt_tree = p->base.x_stmt_tree;
6489 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6490 c_in_iteration_stmt = p->x_in_iteration_stmt;
6491 c_in_case_stmt = p->x_in_case_stmt;
6492 current_function_returns_value = p->returns_value;
6493 current_function_returns_null = p->returns_null;
6494 current_function_returns_abnormally = p->returns_abnormally;
6495 warn_about_return_type = p->warn_about_return_type;
6496 current_extern_inline = p->extern_inline;
6501 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6504 c_dup_lang_specific_decl (tree decl)
6506 struct lang_decl *ld;
6508 if (!DECL_LANG_SPECIFIC (decl))
6511 ld = ggc_alloc (sizeof (struct lang_decl));
6512 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6513 DECL_LANG_SPECIFIC (decl) = ld;
6516 /* The functions below are required for functionality of doing
6517 function at once processing in the C front end. Currently these
6518 functions are not called from anywhere in the C front end, but as
6519 these changes continue, that will change. */
6521 /* Returns nonzero if the current statement is a full expression,
6522 i.e. temporaries created during that statement should be destroyed
6523 at the end of the statement. */
6526 stmts_are_full_exprs_p (void)
6531 /* Returns the stmt_tree (if any) to which statements are currently
6532 being added. If there is no active statement-tree, NULL is
6536 current_stmt_tree (void)
6538 return &c_stmt_tree;
6541 /* Returns the stack of SCOPE_STMTs for the current function. */
6544 current_scope_stmt_stack (void)
6546 return &c_scope_stmt_stack;
6549 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6553 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6558 /* Dummy function in place of callback used by C++. */
6561 extract_interface_info (void)
6565 /* Return a new COMPOUND_STMT, after adding it to the current
6569 c_begin_compound_stmt (void)
6573 /* Create the COMPOUND_STMT. */
6574 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6579 /* Return the global value of T as a symbol. */
6582 identifier_global_value (tree t)
6584 struct c_binding *b;
6586 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6587 if (b->contour == file_scope || b->contour == external_scope)
6593 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6594 otherwise the name is found in ridpointers from RID_INDEX. */
6597 record_builtin_type (enum rid rid_index, const char *name, tree type)
6601 id = ridpointers[(int) rid_index];
6603 id = get_identifier (name);
6604 pushdecl (build_decl (TYPE_DECL, id, type));
6607 /* Build the void_list_node (void_type_node having been created). */
6609 build_void_list_node (void)
6611 tree t = build_tree_list (NULL_TREE, void_type_node);
6615 /* Return something to represent absolute declarators containing a *.
6616 TARGET is the absolute declarator that the * contains.
6617 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6618 to apply to the pointer type, represented as identifiers, possible mixed
6621 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6622 if attributes are present) and whose type is the modifier list. */
6625 make_pointer_declarator (tree type_quals_attrs, tree target)
6628 tree itarget = target;
6629 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6630 if (attrs != NULL_TREE)
6631 itarget = tree_cons (attrs, target, NULL_TREE);
6632 return build1 (INDIRECT_REF, quals, itarget);
6635 /* A wrapper around lhd_set_decl_assembler_name that gives static
6636 variables their C names if they are at file scope and only one
6637 translation unit is being compiled, for backwards compatibility
6638 with certain bizarre assembler hacks (like crtstuff.c). */
6641 c_static_assembler_name (tree decl)
6643 if (num_in_fnames == 1
6644 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6645 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6646 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6648 lhd_set_decl_assembler_name (decl);
6651 /* Perform final processing on file-scope data. */
6653 c_write_global_declarations_1 (tree globals)
6655 size_t len = list_length (globals);
6656 tree *vec = xmalloc (sizeof (tree) * len);
6660 /* Process the decls in the order they were written. */
6661 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6664 wrapup_global_declarations (vec, len);
6665 check_global_declarations (vec, len);
6671 c_write_global_declarations (void)
6675 /* We don't want to do this if generating a PCH. */
6679 /* Process all file scopes in this compilation. */
6680 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6681 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6683 /* Now do the same for the externals scope. */
6686 c_write_global_declarations_1 (BLOCK_VARS (t));
6689 #include "gt-c-decl.h"