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 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1142 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1144 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1145 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1146 locate_old_decl (olddecl, error);
1151 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1152 but silently ignore the redeclaration if either is in a system
1153 header. (Conflicting redeclarations were handled above.) */
1154 if (TREE_CODE (newdecl) == TYPE_DECL)
1156 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1157 return true; /* Allow OLDDECL to continue in use. */
1159 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1160 locate_old_decl (olddecl, error);
1164 /* Function declarations can either be 'static' or 'extern' (no
1165 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1166 can never conflict with each other on account of linkage (6.2.2p4).
1167 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1168 two definitions if one is 'extern inline' and one is not. The non-
1169 extern-inline definition supersedes the extern-inline definition. */
1170 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1172 /* If you declare a built-in function name as static, or
1173 define the built-in with an old-style definition (so we
1174 can't validate the argument list) the built-in definition is
1175 overridden, but optionally warn this was a bad choice of name. */
1176 if (DECL_BUILT_IN (olddecl)
1177 && !C_DECL_DECLARED_BUILTIN (olddecl)
1178 && (!TREE_PUBLIC (newdecl)
1179 || (DECL_INITIAL (newdecl)
1180 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1183 warning ("%Jdeclaration of '%D' shadows a built-in function",
1185 /* Discard the old built-in function. */
1189 if (DECL_INITIAL (newdecl))
1191 if (DECL_INITIAL (olddecl)
1192 && !(DECL_DECLARED_INLINE_P (olddecl)
1193 && DECL_EXTERNAL (olddecl)
1194 && !(DECL_DECLARED_INLINE_P (newdecl)
1195 && DECL_EXTERNAL (newdecl))))
1197 error ("%Jredefinition of '%D'", newdecl, newdecl);
1198 locate_old_decl (olddecl, error);
1202 /* If we have a prototype after an old-style function definition,
1203 the argument types must be checked specially. */
1204 else if (DECL_INITIAL (olddecl)
1205 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1206 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1207 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1209 locate_old_decl (olddecl, error);
1212 /* Mismatched non-static and static is considered poor style.
1213 We only diagnose static then non-static if -Wtraditional,
1214 because it is the most convenient way to get some effects
1215 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1216 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1217 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1219 /* A static function declaration for a predeclared function
1220 that isn't actually built in, silently overrides the
1221 default. Objective C uses these. See also above.
1222 FIXME: Make Objective C use normal builtins. */
1223 if (TREE_CODE (olddecl) == FUNCTION_DECL
1224 && DECL_SOURCE_LINE (olddecl) == 0)
1228 warning ("%Jstatic declaration of '%D' follows "
1229 "non-static declaration", newdecl, newdecl);
1233 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1234 && warn_traditional)
1236 warning ("%Jnon-static declaration of '%D' follows "
1237 "static declaration", newdecl, newdecl);
1241 else if (TREE_CODE (newdecl) == VAR_DECL)
1243 /* Only variables can be thread-local, and all declarations must
1244 agree on this property. */
1245 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1247 if (DECL_THREAD_LOCAL (newdecl))
1248 error ("%Jthread-local declaration of '%D' follows "
1249 "non-thread-local declaration", newdecl, newdecl);
1251 error ("%Jnon-thread-local declaration of '%D' follows "
1252 "thread-local declaration", newdecl, newdecl);
1254 locate_old_decl (olddecl, error);
1258 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1259 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1261 error ("%Jredefinition of '%D'", newdecl, newdecl);
1262 locate_old_decl (olddecl, error);
1266 /* Objects declared at file scope: if at least one is 'extern',
1267 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1268 if (DECL_FILE_SCOPE_P (newdecl))
1270 if (!DECL_EXTERNAL (newdecl)
1271 && !DECL_EXTERNAL (olddecl)
1272 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1274 if (TREE_PUBLIC (newdecl))
1275 error ("%Jnon-static declaration of '%D' follows "
1276 "static declaration", newdecl, newdecl);
1278 error ("%Jstatic declaration of '%D' follows "
1279 "non-static declaration", newdecl, newdecl);
1281 locate_old_decl (olddecl, error);
1285 /* Two objects with the same name declared at the same block
1286 scope must both be external references (6.7p3). */
1287 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1288 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1290 if (DECL_EXTERNAL (newdecl))
1291 error ("%Jextern declaration of '%D' follows "
1292 "declaration with no linkage", newdecl, newdecl);
1293 else if (DECL_EXTERNAL (olddecl))
1294 error ("%Jdeclaration of '%D' with no linkage follows "
1295 "extern declaration", newdecl, newdecl);
1297 error ("%Jredeclaration of '%D' with no linkage",
1300 locate_old_decl (olddecl, error);
1306 /* All decls must agree on a non-default visibility. */
1307 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1308 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1309 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1311 warning ("%Jredeclaration of '%D' with different visibility "
1312 "(old visibility preserved)", newdecl, newdecl);
1316 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1318 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1319 if (DECL_DECLARED_INLINE_P (newdecl)
1320 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1322 warning ("%Jinline declaration of '%D' follows "
1323 "declaration with attribute noinline", newdecl, newdecl);
1326 else if (DECL_DECLARED_INLINE_P (olddecl)
1327 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1329 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1330 "inline declaration ", newdecl, newdecl);
1334 /* Inline declaration after use or definition.
1335 ??? Should we still warn about this now we have unit-at-a-time
1336 mode and can get it right? */
1337 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1339 if (TREE_USED (olddecl))
1341 warning ("%J'%D' declared inline after being called",
1345 else if (DECL_INITIAL (olddecl))
1347 warning ("%J'%D' declared inline after its definition",
1353 else /* PARM_DECL, VAR_DECL */
1355 /* Redeclaration of a parameter is a constraint violation (this is
1356 not explicitly stated, but follows from C99 6.7p3 [no more than
1357 one declaration of the same identifier with no linkage in the
1358 same scope, except type tags] and 6.2.2p6 [parameters have no
1359 linkage]). We must check for a forward parameter declaration,
1360 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1361 an extension, the mandatory diagnostic for which is handled by
1362 mark_forward_parm_decls. */
1364 if (TREE_CODE (newdecl) == PARM_DECL
1365 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1367 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1368 locate_old_decl (olddecl, error);
1373 /* Optional warning for completely redundant decls. */
1374 if (!warned && !pedwarned
1375 && warn_redundant_decls
1376 /* Don't warn about a function declaration followed by a
1378 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1379 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1380 /* Don't warn about an extern followed by a definition. */
1381 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1382 /* Don't warn about forward parameter decls. */
1383 && !(TREE_CODE (newdecl) == PARM_DECL
1384 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1386 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1390 /* Report location of previous decl/defn in a consistent manner. */
1391 if (warned || pedwarned)
1392 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1397 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1398 consistent with OLDDECL, but carries new information. Merge the
1399 new information into OLDDECL. This function issues no
1403 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1405 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1406 && DECL_INITIAL (newdecl) != 0);
1408 /* For real parm decl following a forward decl, rechain the old decl
1409 in its new location and clear TREE_ASM_WRITTEN (it's not a
1410 forward decl anymore). */
1411 if (TREE_CODE (newdecl) == PARM_DECL
1412 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1414 struct c_binding *b, **here;
1416 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1417 if ((*here)->decl == olddecl)
1424 b->prev = current_scope->bindings;
1425 current_scope->bindings = b;
1427 TREE_ASM_WRITTEN (olddecl) = 0;
1430 DECL_ATTRIBUTES (newdecl)
1431 = targetm.merge_decl_attributes (olddecl, newdecl);
1433 /* Merge the data types specified in the two decls. */
1435 = TREE_TYPE (olddecl)
1436 = composite_type (newtype, oldtype);
1438 /* Lay the type out, unless already done. */
1439 if (oldtype != TREE_TYPE (newdecl))
1441 if (TREE_TYPE (newdecl) != error_mark_node)
1442 layout_type (TREE_TYPE (newdecl));
1443 if (TREE_CODE (newdecl) != FUNCTION_DECL
1444 && TREE_CODE (newdecl) != TYPE_DECL
1445 && TREE_CODE (newdecl) != CONST_DECL)
1446 layout_decl (newdecl, 0);
1450 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1451 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1452 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1453 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1454 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1455 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1457 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1458 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1462 /* Keep the old rtl since we can safely use it. */
1463 COPY_DECL_RTL (olddecl, newdecl);
1465 /* Merge the type qualifiers. */
1466 if (TREE_READONLY (newdecl))
1467 TREE_READONLY (olddecl) = 1;
1469 if (TREE_THIS_VOLATILE (newdecl))
1471 TREE_THIS_VOLATILE (olddecl) = 1;
1472 if (TREE_CODE (newdecl) == VAR_DECL)
1473 make_var_volatile (newdecl);
1476 /* Keep source location of definition rather than declaration. */
1477 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1478 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1480 /* Merge the unused-warning information. */
1481 if (DECL_IN_SYSTEM_HEADER (olddecl))
1482 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1483 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1484 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1486 /* Merge the initialization information. */
1487 if (DECL_INITIAL (newdecl) == 0)
1488 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1490 /* Merge the section attribute.
1491 We want to issue an error if the sections conflict but that must be
1492 done later in decl_attributes since we are called before attributes
1494 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1495 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1497 /* Copy the assembler name.
1498 Currently, it can only be defined in the prototype. */
1499 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1501 /* If either declaration has a nondefault visibility, use it. */
1502 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1503 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1505 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1507 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1508 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1509 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1510 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1511 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1512 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1513 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1514 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1515 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1518 /* Merge the storage class information. */
1519 merge_weak (newdecl, olddecl);
1521 /* For functions, static overrides non-static. */
1522 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1524 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1525 /* This is since we don't automatically
1526 copy the attributes of NEWDECL into OLDDECL. */
1527 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1528 /* If this clears `static', clear it in the identifier too. */
1529 if (! TREE_PUBLIC (olddecl))
1530 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1532 if (DECL_EXTERNAL (newdecl))
1534 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1535 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1537 /* An extern decl does not override previous storage class. */
1538 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1539 if (! DECL_EXTERNAL (newdecl))
1541 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1542 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1547 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1548 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1551 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1553 /* If we're redefining a function previously defined as extern
1554 inline, make sure we emit debug info for the inline before we
1555 throw it away, in case it was inlined into a function that hasn't
1556 been written out yet. */
1557 if (new_is_definition && DECL_INITIAL (olddecl))
1559 if (TREE_USED (olddecl)
1560 /* In unit-at-a-time mode we never inline re-defined extern
1561 inline functions. */
1562 && !flag_unit_at_a_time
1563 && cgraph_function_possibly_inlined_p (olddecl))
1564 (*debug_hooks->outlining_inline_function) (olddecl);
1566 /* The new defn must not be inline. */
1567 DECL_INLINE (newdecl) = 0;
1568 DECL_UNINLINABLE (newdecl) = 1;
1572 /* If either decl says `inline', this fn is inline,
1573 unless its definition was passed already. */
1574 if (DECL_DECLARED_INLINE_P (newdecl)
1575 || DECL_DECLARED_INLINE_P (olddecl))
1576 DECL_DECLARED_INLINE_P (newdecl) = 1;
1578 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1579 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1582 if (DECL_BUILT_IN (olddecl))
1584 /* If redeclaring a builtin function, it stays built in.
1585 But it gets tagged as having been declared. */
1586 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1587 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1588 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1591 /* Also preserve various other info from the definition. */
1592 if (! new_is_definition)
1594 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1595 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1596 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1597 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1598 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1600 /* Set DECL_INLINE on the declaration if we've got a body
1601 from which to instantiate. */
1602 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1604 DECL_INLINE (newdecl) = 1;
1605 DECL_ABSTRACT_ORIGIN (newdecl)
1606 = DECL_ABSTRACT_ORIGIN (olddecl);
1611 /* If a previous declaration said inline, mark the
1612 definition as inlinable. */
1613 if (DECL_DECLARED_INLINE_P (newdecl)
1614 && ! DECL_UNINLINABLE (newdecl))
1615 DECL_INLINE (newdecl) = 1;
1619 /* This bit must not get wiped out. */
1620 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1622 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1623 But preserve OLDDECL's DECL_UID. */
1625 unsigned olddecl_uid = DECL_UID (olddecl);
1627 memcpy ((char *) olddecl + sizeof (struct tree_common),
1628 (char *) newdecl + sizeof (struct tree_common),
1629 sizeof (struct tree_decl) - sizeof (struct tree_common));
1630 DECL_UID (olddecl) = olddecl_uid;
1633 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1634 so that encode_section_info has a chance to look at the new decl
1635 flags and attributes. */
1636 if (DECL_RTL_SET_P (olddecl)
1637 && (TREE_CODE (olddecl) == FUNCTION_DECL
1638 || (TREE_CODE (olddecl) == VAR_DECL
1639 && TREE_STATIC (olddecl))))
1640 make_decl_rtl (olddecl, NULL);
1643 /* Handle when a new declaration NEWDECL has the same name as an old
1644 one OLDDECL in the same binding contour. Prints an error message
1647 If safely possible, alter OLDDECL to look like NEWDECL, and return
1648 true. Otherwise, return false. */
1651 duplicate_decls (tree newdecl, tree olddecl)
1653 tree newtype = NULL, oldtype = NULL;
1655 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1658 merge_decls (newdecl, olddecl, newtype, oldtype);
1663 /* Check whether decl-node NEW shadows an existing declaration. */
1665 warn_if_shadowing (tree new)
1667 struct c_binding *b;
1669 /* Shadow warnings wanted? */
1671 /* No shadow warnings for internally generated vars. */
1672 || DECL_SOURCE_LINE (new) == 0
1673 /* No shadow warnings for vars made for inlining. */
1674 || DECL_FROM_INLINE (new)
1675 /* Don't warn about the parm names in function declarator
1676 within a function declarator. It would be nice to avoid
1677 warning in any function declarator in a declaration, as
1678 opposed to a definition, but there is no way to tell
1679 it's not a definition at this point. */
1680 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1683 /* Is anything being shadowed? Do not be confused by a second binding
1684 to the same decl in the externals scope. */
1685 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1686 if (b->decl && b->decl != new && b->contour != external_scope)
1690 if (TREE_CODE (old) == PARM_DECL)
1691 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1692 else if (DECL_FILE_SCOPE_P (old))
1693 warning ("%Jdeclaration of '%D' shadows a global declaration",
1695 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1696 warning ("%Jdeclaration of '%D' shadows a built-in function",
1699 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1701 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1702 warning ("%Jshadowed declaration is here", old);
1709 /* Subroutine of pushdecl.
1711 X is a TYPE_DECL for a typedef statement. Create a brand new
1712 ..._TYPE node (which will be just a variant of the existing
1713 ..._TYPE node with identical properties) and then install X
1714 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1716 The whole point here is to end up with a situation where each
1717 and every ..._TYPE node the compiler creates will be uniquely
1718 associated with AT MOST one node representing a typedef name.
1719 This way, even though the compiler substitutes corresponding
1720 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1721 early on, later parts of the compiler can always do the reverse
1722 translation and get back the corresponding typedef name. For
1725 typedef struct S MY_TYPE;
1728 Later parts of the compiler might only know that `object' was of
1729 type `struct S' if it were not for code just below. With this
1730 code however, later parts of the compiler see something like:
1732 struct S' == struct S
1733 typedef struct S' MY_TYPE;
1736 And they can then deduce (from the node for type struct S') that
1737 the original object declaration was:
1741 Being able to do this is important for proper support of protoize,
1742 and also for generating precise symbolic debugging information
1743 which takes full account of the programmer's (typedef) vocabulary.
1745 Obviously, we don't want to generate a duplicate ..._TYPE node if
1746 the TYPE_DECL node that we are now processing really represents a
1747 standard built-in type.
1749 Since all standard types are effectively declared at line zero
1750 in the source file, we can easily check to see if we are working
1751 on a standard type by checking the current value of lineno. */
1754 clone_underlying_type (tree x)
1756 if (DECL_SOURCE_LINE (x) == 0)
1758 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1759 TYPE_NAME (TREE_TYPE (x)) = x;
1761 else if (TREE_TYPE (x) != error_mark_node
1762 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1764 tree tt = TREE_TYPE (x);
1765 DECL_ORIGINAL_TYPE (x) = tt;
1766 tt = build_type_copy (tt);
1768 TREE_USED (tt) = TREE_USED (x);
1773 /* Record a decl-node X as belonging to the current lexical scope.
1774 Check for errors (such as an incompatible declaration for the same
1775 name already seen in the same scope).
1777 Returns either X or an old decl for the same name.
1778 If an old decl is returned, it may have been smashed
1779 to agree with what X says. */
1784 tree name = DECL_NAME (x);
1785 struct c_scope *scope = current_scope;
1786 struct c_binding *b;
1788 /* Functions need the lang_decl data. */
1789 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1790 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1792 /* Must set DECL_CONTEXT for everything not at file scope or
1793 DECL_FILE_SCOPE_P won't work. Local externs don't count
1794 unless they have initializers (which generate code). */
1795 if (current_function_decl
1796 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1797 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1798 DECL_CONTEXT (x) = current_function_decl;
1800 /* Anonymous decls are just inserted in the scope. */
1803 bind (name, x, scope);
1807 /* First, see if there is another declaration with the same name in
1808 the current scope. If there is, duplicate_decls may do all the
1809 work for us. If duplicate_decls returns false, that indicates
1810 two incompatible decls in the same scope; we are to silently
1811 replace the old one (duplicate_decls has issued all appropriate
1812 diagnostics). In particular, we should not consider possible
1813 duplicates in the external scope, or shadowing. */
1814 b = I_SYMBOL_BINDING (name);
1815 if (b && b->contour == scope)
1817 if (duplicate_decls (x, b->decl))
1820 goto skip_external_and_shadow_checks;
1823 /* All declarations with external linkage, and all external
1824 references, go in the external scope, no matter what scope is
1825 current. However, the binding in that scope is ignored for
1826 purposes of normal name lookup. A separate binding structure is
1827 created in the requested scope; this governs the normal
1828 visibility of the symbol.
1830 The binding in the externals scope is used exclusively for
1831 detecting duplicate declarations of the same object, no matter
1832 what scope they are in; this is what we do here. (C99 6.2.7p2:
1833 All declarations that refer to the same object or function shall
1834 have compatible type; otherwise, the behavior is undefined.) */
1835 if (DECL_EXTERNAL (x) || scope == file_scope)
1837 if (warn_nested_externs
1838 && scope != file_scope
1839 && !DECL_IN_SYSTEM_HEADER (x))
1840 warning ("nested extern declaration of `%s'",
1841 IDENTIFIER_POINTER (name));
1843 while (b && b->contour != external_scope)
1846 /* The point of the same_translation_unit_p check here is,
1847 we want to detect a duplicate decl for a construct like
1848 foo() { extern bar(); } ... static bar(); but not if
1849 they are in different translation units. In any case,
1850 the static does not go in the externals scope. */
1852 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1853 || same_translation_unit_p (x, b->decl))
1854 && duplicate_decls (x, b->decl))
1856 bind (name, b->decl, scope);
1859 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1861 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1862 bind (name, x, external_scope);
1866 warn_if_shadowing (x);
1868 skip_external_and_shadow_checks:
1869 if (TREE_CODE (x) == TYPE_DECL)
1870 clone_underlying_type (x);
1872 bind (name, x, scope);
1874 /* If x's type is incomplete because it's based on a
1875 structure or union which has not yet been fully declared,
1876 attach it to that structure or union type, so we can go
1877 back and complete the variable declaration later, if the
1878 structure or union gets fully declared.
1880 If the input is erroneous, we can have error_mark in the type
1881 slot (e.g. "f(void a, ...)") - that doesn't count as an
1883 if (TREE_TYPE (x) != error_mark_node
1884 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1886 tree element = TREE_TYPE (x);
1888 while (TREE_CODE (element) == ARRAY_TYPE)
1889 element = TREE_TYPE (element);
1890 element = TYPE_MAIN_VARIANT (element);
1892 if ((TREE_CODE (element) == RECORD_TYPE
1893 || TREE_CODE (element) == UNION_TYPE)
1894 && (TREE_CODE (x) != TYPE_DECL
1895 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1896 && !COMPLETE_TYPE_P (element))
1897 C_TYPE_INCOMPLETE_VARS (element)
1898 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1903 /* Record X as belonging to file scope.
1904 This is used only internally by the Objective-C front end,
1905 and is limited to its needs. duplicate_decls is not called;
1906 if there is any preexisting decl for this identifier, it is an ICE. */
1909 pushdecl_top_level (tree x)
1913 if (TREE_CODE (x) != VAR_DECL)
1916 name = DECL_NAME (x);
1918 if (I_SYMBOL_BINDING (name))
1921 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1923 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1924 bind (name, x, external_scope);
1927 bind (name, x, file_scope);
1933 implicit_decl_warning (tree id, tree olddecl)
1935 void (*diag) (const char *, ...);
1936 switch (mesg_implicit_function_declaration)
1939 case 1: diag = warning; break;
1940 case 2: diag = error; break;
1944 diag (N_("implicit declaration of function '%E'"), id);
1946 locate_old_decl (olddecl, diag);
1949 /* Generate an implicit declaration for identifier FUNCTIONID as a
1950 function of type int (). */
1953 implicitly_declare (tree functionid)
1955 tree decl = lookup_name_in_scope (functionid, external_scope);
1959 /* FIXME: Objective-C has weird not-really-builtin functions
1960 which are supposed to be visible automatically. They wind up
1961 in the external scope because they're pushed before the file
1962 scope gets created. Catch this here and rebind them into the
1964 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1966 bind (functionid, decl, file_scope);
1971 /* Implicit declaration of a function already declared
1972 (somehow) in a different scope, or as a built-in.
1973 If this is the first time this has happened, warn;
1974 then recycle the old declaration. */
1975 if (!C_DECL_IMPLICIT (decl))
1977 implicit_decl_warning (functionid, decl);
1978 C_DECL_IMPLICIT (decl) = 1;
1980 bind (functionid, decl, current_scope);
1985 /* Not seen before. */
1986 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1987 DECL_EXTERNAL (decl) = 1;
1988 TREE_PUBLIC (decl) = 1;
1989 C_DECL_IMPLICIT (decl) = 1;
1990 implicit_decl_warning (functionid, 0);
1992 /* C89 says implicit declarations are in the innermost block.
1993 So we record the decl in the standard fashion. */
1994 decl = pushdecl (decl);
1996 /* No need to call objc_check_decl here - it's a function type. */
1997 rest_of_decl_compilation (decl, NULL, 0, 0);
1999 /* Write a record describing this implicit function declaration
2000 to the prototypes file (if requested). */
2001 gen_aux_info_record (decl, 0, 1, 0);
2003 /* Possibly apply some default attributes to this implicit declaration. */
2004 decl_attributes (&decl, NULL_TREE, 0);
2009 /* Issue an error message for a reference to an undeclared variable
2010 ID, including a reference to a builtin outside of function-call
2011 context. Establish a binding of the identifier to error_mark_node
2012 in an appropriate scope, which will suppress further errors for the
2015 undeclared_variable (tree id)
2017 static bool already = false;
2018 struct c_scope *scope;
2020 if (current_function_decl == 0)
2022 error ("'%E' undeclared here (not in a function)", id);
2023 scope = current_scope;
2027 error ("'%E' undeclared (first use in this function)", id);
2031 error ("(Each undeclared identifier is reported only once");
2032 error ("for each function it appears in.)");
2036 /* If we are parsing old-style parameter decls, current_function_decl
2037 will be nonnull but current_function_scope will be null. */
2038 scope = current_function_scope ? current_function_scope : current_scope;
2040 bind (id, error_mark_node, scope);
2043 /* Subroutine of lookup_label, declare_label, define_label: construct a
2044 LABEL_DECL with all the proper frills. */
2047 make_label (tree name, location_t location)
2049 tree label = build_decl (LABEL_DECL, name, void_type_node);
2051 DECL_CONTEXT (label) = current_function_decl;
2052 DECL_MODE (label) = VOIDmode;
2053 DECL_SOURCE_LOCATION (label) = location;
2058 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2059 Create one if none exists so far for the current function.
2060 This is called when a label is used in a goto expression or
2061 has its address taken. */
2064 lookup_label (tree name)
2068 if (current_function_decl == 0)
2070 error ("label %s referenced outside of any function",
2071 IDENTIFIER_POINTER (name));
2075 /* Use a label already defined or ref'd with this name, but not if
2076 it is inherited from a containing function and wasn't declared
2078 label = I_LABEL_DECL (name);
2079 if (label && (DECL_CONTEXT (label) == current_function_decl
2080 || C_DECLARED_LABEL_FLAG (label)))
2082 /* If the label has only been declared, update its apparent
2083 location to point here, for better diagnostics if it
2084 turns out not to have been defined. */
2085 if (!TREE_USED (label))
2086 DECL_SOURCE_LOCATION (label) = input_location;
2090 /* No label binding for that identifier; make one. */
2091 label = make_label (name, input_location);
2093 /* Ordinary labels go in the current function scope. */
2094 bind (name, label, current_function_scope);
2098 /* Make a label named NAME in the current function, shadowing silently
2099 any that may be inherited from containing functions or containing
2100 scopes. This is called for __label__ declarations. */
2103 declare_label (tree name)
2105 struct c_binding *b = I_LABEL_BINDING (name);
2108 /* Check to make sure that the label hasn't already been declared
2110 if (b && b->contour == current_scope)
2112 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2113 locate_old_decl (b->decl, error);
2115 /* Just use the previous declaration. */
2119 label = make_label (name, input_location);
2120 C_DECLARED_LABEL_FLAG (label) = 1;
2122 /* Declared labels go in the current scope. */
2123 bind (name, label, current_scope);
2127 /* Define a label, specifying the location in the source file.
2128 Return the LABEL_DECL node for the label, if the definition is valid.
2129 Otherwise return 0. */
2132 define_label (location_t location, tree name)
2134 /* Find any preexisting label with this name. It is an error
2135 if that label has already been defined in this function, or
2136 if there is a containing function with a declared label with
2138 tree label = I_LABEL_DECL (name);
2141 && ((DECL_CONTEXT (label) == current_function_decl
2142 && DECL_INITIAL (label) != 0)
2143 || (DECL_CONTEXT (label) != current_function_decl
2144 && C_DECLARED_LABEL_FLAG (label))))
2146 error ("%Hduplicate label `%D'", &location, label);
2147 locate_old_decl (label, error);
2150 else if (label && DECL_CONTEXT (label) == current_function_decl)
2152 /* The label has been used or declared already in this function,
2153 but not defined. Update its location to point to this
2155 DECL_SOURCE_LOCATION (label) = location;
2159 /* No label binding for that identifier; make one. */
2160 label = make_label (name, location);
2162 /* Ordinary labels go in the current function scope. */
2163 bind (name, label, current_function_scope);
2166 if (warn_traditional && !in_system_header && lookup_name (name))
2167 warning ("%Htraditional C lacks a separate namespace for labels, "
2168 "identifier `%s' conflicts", &location,
2169 IDENTIFIER_POINTER (name));
2171 /* Mark label as having been defined. */
2172 DECL_INITIAL (label) = error_mark_node;
2176 /* Given NAME, an IDENTIFIER_NODE,
2177 return the structure (or union or enum) definition for that name.
2178 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2179 CODE says which kind of type the caller wants;
2180 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2181 If the wrong kind of type is found, an error is reported. */
2184 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2186 struct c_binding *b = I_TAG_BINDING (name);
2192 /* We only care about whether it's in this level if
2193 thislevel_only was set or it might be a type clash. */
2194 if (thislevel_only || TREE_CODE (b->decl) != code)
2196 /* For our purposes, a tag in the external scope is the same as
2197 a tag in the file scope. (Primarily relevant to Objective-C
2198 and its builtin structure tags, which get pushed before the
2199 file scope is created.) */
2200 if (b->contour == current_scope
2201 || (current_scope == file_scope && b->contour == external_scope))
2205 if (thislevel_only && !thislevel)
2208 if (TREE_CODE (b->decl) != code)
2210 /* Definition isn't the kind we were looking for. */
2211 pending_invalid_xref = name;
2212 pending_invalid_xref_location = input_location;
2214 /* If in the same binding level as a declaration as a tag
2215 of a different type, this must not be allowed to
2216 shadow that tag, so give the error immediately.
2217 (For example, "struct foo; union foo;" is invalid.) */
2219 pending_xref_error ();
2224 /* Print an error message now
2225 for a recent invalid struct, union or enum cross reference.
2226 We don't print them immediately because they are not invalid
2227 when used in the `struct foo;' construct for shadowing. */
2230 pending_xref_error (void)
2232 if (pending_invalid_xref != 0)
2233 error ("%H`%s' defined as wrong kind of tag",
2234 &pending_invalid_xref_location,
2235 IDENTIFIER_POINTER (pending_invalid_xref));
2236 pending_invalid_xref = 0;
2240 /* Look up NAME in the current scope and its superiors
2241 in the namespace of variables, functions and typedefs.
2242 Return a ..._DECL node of some kind representing its definition,
2243 or return 0 if it is undefined. */
2246 lookup_name (tree name)
2248 struct c_binding *b = I_SYMBOL_BINDING (name);
2249 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2254 /* Similar to `lookup_name' but look only at the indicated scope. */
2257 lookup_name_in_scope (tree name, struct c_scope *scope)
2259 struct c_binding *b;
2261 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2262 if (b->contour == scope)
2267 /* Create the predefined scalar types of C,
2268 and some nodes representing standard constants (0, 1, (void *) 0).
2269 Initialize the global scope.
2270 Make definitions for built-in primitive functions. */
2273 c_init_decl_processing (void)
2276 tree ptr_ftype_void, ptr_ftype_ptr;
2277 location_t save_loc = input_location;
2279 /* Adds some ggc roots, and reserved words for c-parse.in. */
2282 current_function_decl = 0;
2284 /* Make the externals scope. */
2286 external_scope = current_scope;
2288 /* Declarations from c_common_nodes_and_builtins must not be associated
2289 with this input file, lest we get differences between using and not
2290 using preprocessed headers. */
2291 input_location.file = "<internal>";
2292 input_location.line = 0;
2294 build_common_tree_nodes (flag_signed_char);
2296 c_common_nodes_and_builtins ();
2298 /* In C, comparisons and TRUTH_* expressions have type int. */
2299 truthvalue_type_node = integer_type_node;
2300 truthvalue_true_node = integer_one_node;
2301 truthvalue_false_node = integer_zero_node;
2303 /* Even in C99, which has a real boolean type. */
2304 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2305 boolean_type_node));
2307 endlink = void_list_node;
2308 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2310 = build_function_type (ptr_type_node,
2311 tree_cons (NULL_TREE, ptr_type_node, endlink));
2313 input_location = save_loc;
2315 pedantic_lvalues = true;
2317 make_fname_decl = c_make_fname_decl;
2318 start_fname_decls ();
2321 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2322 decl, NAME is the initialization string and TYPE_DEP indicates whether
2323 NAME depended on the type of the function. As we don't yet implement
2324 delayed emission of static data, we mark the decl as emitted
2325 so it is not placed in the output. Anything using it must therefore pull
2326 out the STRING_CST initializer directly. FIXME. */
2329 c_make_fname_decl (tree id, int type_dep)
2331 const char *name = fname_as_string (type_dep);
2332 tree decl, type, init;
2333 size_t length = strlen (name);
2335 type = build_array_type
2336 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2337 build_index_type (size_int (length)));
2339 decl = build_decl (VAR_DECL, id, type);
2341 TREE_STATIC (decl) = 1;
2342 TREE_READONLY (decl) = 1;
2343 DECL_ARTIFICIAL (decl) = 1;
2345 init = build_string (length + 1, name);
2346 free ((char *) name);
2347 TREE_TYPE (init) = type;
2348 DECL_INITIAL (decl) = init;
2350 TREE_USED (decl) = 1;
2352 if (current_function_decl)
2354 DECL_CONTEXT (decl) = current_function_decl;
2355 bind (id, decl, current_function_scope);
2358 finish_decl (decl, init, NULL_TREE);
2363 /* Return a definition for a builtin function named NAME and whose data type
2364 is TYPE. TYPE should be a function type with argument types.
2365 FUNCTION_CODE tells later passes how to compile calls to this function.
2366 See tree.h for its possible values.
2368 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2369 the name to be called if we can't opencode the function. If
2370 ATTRS is nonzero, use that for the function's attribute list. */
2373 builtin_function (const char *name, tree type, int function_code,
2374 enum built_in_class class, const char *library_name,
2377 tree id = get_identifier (name);
2378 tree decl = build_decl (FUNCTION_DECL, id, type);
2379 TREE_PUBLIC (decl) = 1;
2380 DECL_EXTERNAL (decl) = 1;
2381 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2382 DECL_BUILT_IN_CLASS (decl) = class;
2383 DECL_FUNCTION_CODE (decl) = function_code;
2385 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2386 make_decl_rtl (decl, NULL);
2388 /* Should never be called on a symbol with a preexisting meaning. */
2389 if (I_SYMBOL_BINDING (id))
2392 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2393 bind (id, decl, external_scope);
2395 /* Builtins in the implementation namespace are made visible without
2396 needing to be explicitly declared. See push_file_scope. */
2397 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2399 TREE_CHAIN (decl) = visible_builtins;
2400 visible_builtins = decl;
2403 /* Possibly apply some default attributes to this built-in function. */
2405 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2407 decl_attributes (&decl, NULL_TREE, 0);
2412 /* Called when a declaration is seen that contains no names to declare.
2413 If its type is a reference to a structure, union or enum inherited
2414 from a containing scope, shadow that tag name for the current scope
2415 with a forward reference.
2416 If its type defines a new named structure or union
2417 or defines an enum, it is valid but we need not do anything here.
2418 Otherwise, it is an error. */
2421 shadow_tag (tree declspecs)
2423 shadow_tag_warned (declspecs, 0);
2427 shadow_tag_warned (tree declspecs, int warned)
2430 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2437 pending_invalid_xref = 0;
2439 /* Remove the attributes from declspecs, since they will confuse the
2441 split_specs_attrs (declspecs, &specs, &attrs);
2443 for (link = specs; link; link = TREE_CHAIN (link))
2445 tree value = TREE_VALUE (link);
2446 enum tree_code code = TREE_CODE (value);
2448 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2449 /* Used to test also that TYPE_SIZE (value) != 0.
2450 That caused warning for `struct foo;' at top level in the file. */
2452 tree name = TYPE_NAME (value);
2459 if (warned != 1 && code != ENUMERAL_TYPE)
2460 /* Empty unnamed enum OK */
2462 pedwarn ("unnamed struct/union that defines no instances");
2468 t = lookup_tag (code, name, 1);
2472 t = make_node (code);
2479 if (!warned && ! in_system_header)
2481 warning ("useless keyword or type name in empty declaration");
2488 error ("two types specified in one empty declaration");
2493 pedwarn ("empty declaration");
2497 /* Construct an array declarator. EXPR is the expression inside [], or
2498 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2499 to the pointer to which a parameter array is converted). STATIC_P is
2500 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2501 is nonzero is the array is [*], a VLA of unspecified length which is
2502 nevertheless a complete type (not currently implemented by GCC),
2503 zero otherwise. The declarator is constructed as an ARRAY_REF
2504 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2505 left of the [] (filled by in set_array_declarator_type) and operand 1
2506 is the expression inside; whose TREE_TYPE is the type qualifiers and
2507 which has TREE_STATIC set if "static" is used. */
2510 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2513 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2514 TREE_TYPE (decl) = quals;
2515 TREE_STATIC (decl) = (static_p ? 1 : 0);
2516 if (pedantic && !flag_isoc99)
2518 if (static_p || quals != NULL_TREE)
2519 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2521 pedwarn ("ISO C90 does not support `[*]' array declarators");
2524 warning ("GCC does not yet properly implement `[*]' array declarators");
2528 /* Set the type of an array declarator. DECL is the declarator, as
2529 constructed by build_array_declarator; TYPE is what appears on the left
2530 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2531 abstract declarator, zero otherwise; this is used to reject static and
2532 type qualifiers in abstract declarators, where they are not in the
2536 set_array_declarator_type (tree decl, tree type, int abstract_p)
2538 TREE_OPERAND (decl, 0) = type;
2539 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2540 error ("static or type qualifiers in abstract declarator");
2544 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2547 groktypename (tree typename)
2551 if (TREE_CODE (typename) != TREE_LIST)
2554 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2556 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2559 /* Apply attributes. */
2560 decl_attributes (&typename, attrs, 0);
2565 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2568 groktypename_in_parm_context (tree typename)
2570 if (TREE_CODE (typename) != TREE_LIST)
2572 return grokdeclarator (TREE_VALUE (typename),
2573 TREE_PURPOSE (typename),
2577 /* Decode a declarator in an ordinary declaration or data definition.
2578 This is called as soon as the type information and variable name
2579 have been parsed, before parsing the initializer if any.
2580 Here we create the ..._DECL node, fill in its type,
2581 and put it on the list of decls for the current context.
2582 The ..._DECL node is returned as the value.
2584 Exception: for arrays where the length is not specified,
2585 the type is left null, to be filled in by `finish_decl'.
2587 Function definitions do not come here; they go to start_function
2588 instead. However, external and forward declarations of functions
2589 do go through here. Structure field declarations are done by
2590 grokfield and not through here. */
2593 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2598 /* An object declared as __attribute__((deprecated)) suppresses
2599 warnings of uses of other deprecated items. */
2600 if (lookup_attribute ("deprecated", attributes))
2601 deprecated_state = DEPRECATED_SUPPRESS;
2603 decl = grokdeclarator (declarator, declspecs,
2604 NORMAL, initialized, NULL);
2606 deprecated_state = DEPRECATED_NORMAL;
2608 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2609 && MAIN_NAME_P (DECL_NAME (decl)))
2610 warning ("%J'%D' is usually a function", decl, decl);
2613 /* Is it valid for this decl to have an initializer at all?
2614 If not, set INITIALIZED to zero, which will indirectly
2615 tell 'finish_decl' to ignore the initializer once it is parsed. */
2616 switch (TREE_CODE (decl))
2619 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2624 error ("function '%D' is initialized like a variable", decl);
2629 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2630 error ("parameter '%D' is initialized", decl);
2635 /* Don't allow initializations for incomplete types except for
2636 arrays which might be completed by the initialization. */
2638 /* This can happen if the array size is an undefined macro.
2639 We already gave a warning, so we don't need another one. */
2640 if (TREE_TYPE (decl) == error_mark_node)
2642 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2644 /* A complete type is ok if size is fixed. */
2646 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2647 || C_DECL_VARIABLE_SIZE (decl))
2649 error ("variable-sized object may not be initialized");
2653 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2655 error ("variable '%D' has initializer but incomplete type", decl);
2658 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2660 error ("elements of array '%D' have incomplete type", decl);
2667 DECL_EXTERNAL (decl) = 0;
2668 if (current_scope == file_scope)
2669 TREE_STATIC (decl) = 1;
2671 /* Tell 'pushdecl' this is an initialized decl
2672 even though we don't yet have the initializer expression.
2673 Also tell 'finish_decl' it may store the real initializer. */
2674 DECL_INITIAL (decl) = error_mark_node;
2677 /* If this is a function declaration, write a record describing it to the
2678 prototypes file (if requested). */
2680 if (TREE_CODE (decl) == FUNCTION_DECL)
2681 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2683 /* ANSI specifies that a tentative definition which is not merged with
2684 a non-tentative definition behaves exactly like a definition with an
2685 initializer equal to zero. (Section 3.7.2)
2687 -fno-common gives strict ANSI behavior, though this tends to break
2688 a large body of code that grew up without this rule.
2690 Thread-local variables are never common, since there's no entrenched
2691 body of code to break, and it allows more efficient variable references
2692 in the presence of dynamic linking. */
2694 if (TREE_CODE (decl) == VAR_DECL
2696 && TREE_PUBLIC (decl)
2697 && !DECL_THREAD_LOCAL (decl)
2699 DECL_COMMON (decl) = 1;
2701 /* Set attributes here so if duplicate decl, will have proper attributes. */
2702 decl_attributes (&decl, attributes, 0);
2704 if (TREE_CODE (decl) == FUNCTION_DECL
2705 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2707 tree ce = declarator;
2709 if (TREE_CODE (ce) == INDIRECT_REF)
2710 ce = TREE_OPERAND (declarator, 0);
2711 if (TREE_CODE (ce) == CALL_EXPR)
2713 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2714 for (; args; args = TREE_CHAIN (args))
2716 tree type = TREE_TYPE (args);
2717 if (INTEGRAL_TYPE_P (type)
2718 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2719 DECL_ARG_TYPE (args) = integer_type_node;
2724 if (TREE_CODE (decl) == FUNCTION_DECL
2725 && DECL_DECLARED_INLINE_P (decl)
2726 && DECL_UNINLINABLE (decl)
2727 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2728 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2730 /* Add this decl to the current scope.
2731 TEM may equal DECL or it may be a previous decl of the same name. */
2732 tem = pushdecl (decl);
2737 /* Finish processing of a declaration;
2738 install its initial value.
2739 If the length of an array type is not known before,
2740 it must be determined now, from the initial value, or it is an error. */
2743 finish_decl (tree decl, tree init, tree asmspec_tree)
2745 tree type = TREE_TYPE (decl);
2746 int was_incomplete = (DECL_SIZE (decl) == 0);
2747 const char *asmspec = 0;
2749 /* If a name was specified, get the string. */
2750 if (current_scope == file_scope)
2751 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2753 asmspec = TREE_STRING_POINTER (asmspec_tree);
2755 /* If `start_decl' didn't like having an initialization, ignore it now. */
2756 if (init != 0 && DECL_INITIAL (decl) == 0)
2759 /* Don't crash if parm is initialized. */
2760 if (TREE_CODE (decl) == PARM_DECL)
2764 store_init_value (decl, init);
2766 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2767 || TREE_CODE (decl) == FUNCTION_DECL
2768 || TREE_CODE (decl) == FIELD_DECL))
2769 objc_check_decl (decl);
2771 /* Deduce size of array from initialization, if not already known. */
2772 if (TREE_CODE (type) == ARRAY_TYPE
2773 && TYPE_DOMAIN (type) == 0
2774 && TREE_CODE (decl) != TYPE_DECL)
2777 = (TREE_STATIC (decl)
2778 /* Even if pedantic, an external linkage array
2779 may have incomplete type at first. */
2780 ? pedantic && !TREE_PUBLIC (decl)
2781 : !DECL_EXTERNAL (decl));
2783 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2785 /* Get the completed type made by complete_array_type. */
2786 type = TREE_TYPE (decl);
2789 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2791 else if (failure == 2)
2794 error ("%Jarray size missing in '%D'", decl, decl);
2795 /* If a `static' var's size isn't known,
2796 make it extern as well as static, so it does not get
2798 If it is not `static', then do not mark extern;
2799 finish_incomplete_decl will give it a default size
2800 and it will get allocated. */
2801 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2802 DECL_EXTERNAL (decl) = 1;
2805 /* TYPE_MAX_VALUE is always one less than the number of elements
2806 in the array, because we start counting at zero. Therefore,
2807 warn only if the value is less than zero. */
2808 else if (pedantic && TYPE_DOMAIN (type) != 0
2809 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2810 error ("%Jzero or negative size array '%D'", decl, decl);
2812 layout_decl (decl, 0);
2815 if (TREE_CODE (decl) == VAR_DECL)
2817 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2818 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2819 layout_decl (decl, 0);
2821 if (DECL_SIZE (decl) == 0
2822 /* Don't give an error if we already gave one earlier. */
2823 && TREE_TYPE (decl) != error_mark_node
2824 && (TREE_STATIC (decl)
2826 /* A static variable with an incomplete type
2827 is an error if it is initialized.
2828 Also if it is not file scope.
2829 Otherwise, let it through, but if it is not `extern'
2830 then it may cause an error message later. */
2831 (DECL_INITIAL (decl) != 0
2832 || !DECL_FILE_SCOPE_P (decl))
2834 /* An automatic variable with an incomplete type
2836 !DECL_EXTERNAL (decl)))
2838 error ("%Jstorage size of '%D' isn't known", decl, decl);
2839 TREE_TYPE (decl) = error_mark_node;
2842 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2843 && DECL_SIZE (decl) != 0)
2845 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2846 constant_expression_warning (DECL_SIZE (decl));
2848 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2851 if (TREE_USED (type))
2852 TREE_USED (decl) = 1;
2855 /* If this is a function and an assembler name is specified, reset DECL_RTL
2856 so we can give it its new name. Also, update built_in_decls if it
2857 was a normal built-in. */
2858 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2860 /* ASMSPEC is given, and not the name of a register. Mark the
2861 name with a star so assemble_name won't munge it. */
2862 char *starred = alloca (strlen (asmspec) + 2);
2864 strcpy (starred + 1, asmspec);
2866 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2868 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2869 SET_DECL_RTL (builtin, NULL_RTX);
2870 change_decl_assembler_name (builtin, get_identifier (starred));
2871 #ifdef TARGET_MEM_FUNCTIONS
2872 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2873 init_block_move_fn (starred);
2874 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2875 init_block_clear_fn (starred);
2877 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2878 init_block_move_fn (starred);
2879 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2880 init_block_clear_fn (starred);
2883 SET_DECL_RTL (decl, NULL_RTX);
2884 change_decl_assembler_name (decl, get_identifier (starred));
2887 /* If #pragma weak was used, mark the decl weak now. */
2888 if (current_scope == file_scope)
2889 maybe_apply_pragma_weak (decl);
2891 /* Output the assembler code and/or RTL code for variables and functions,
2892 unless the type is an undefined structure or union.
2893 If not, it will get done when the type is completed. */
2895 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2897 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2898 if (c_dialect_objc ())
2899 objc_check_decl (decl);
2901 if (DECL_FILE_SCOPE_P (decl))
2903 if (DECL_INITIAL (decl) == NULL_TREE
2904 || DECL_INITIAL (decl) == error_mark_node)
2905 /* Don't output anything
2906 when a tentative file-scope definition is seen.
2907 But at end of compilation, do output code for them. */
2908 DECL_DEFER_OUTPUT (decl) = 1;
2909 rest_of_decl_compilation (decl, asmspec, true, 0);
2913 /* This is a local variable. If there is an ASMSPEC, the
2914 user has requested that we handle it specially. */
2917 /* In conjunction with an ASMSPEC, the `register'
2918 keyword indicates that we should place the variable
2919 in a particular register. */
2920 if (C_DECL_REGISTER (decl))
2922 DECL_HARD_REGISTER (decl) = 1;
2923 /* This cannot be done for a structure with volatile
2924 fields, on which DECL_REGISTER will have been
2926 if (!DECL_REGISTER (decl))
2927 error ("cannot put object with volatile field into register");
2930 /* If this is not a static variable, issue a warning.
2931 It doesn't make any sense to give an ASMSPEC for an
2932 ordinary, non-register local variable. Historically,
2933 GCC has accepted -- but ignored -- the ASMSPEC in
2935 if (TREE_CODE (decl) == VAR_DECL
2936 && !C_DECL_REGISTER (decl)
2937 && !TREE_STATIC (decl))
2938 warning ("%Jignoring asm-specifier for non-static local "
2939 "variable '%D'", decl, decl);
2941 change_decl_assembler_name (decl, get_identifier (asmspec));
2944 if (TREE_CODE (decl) != FUNCTION_DECL)
2945 add_decl_stmt (decl);
2948 if (!DECL_FILE_SCOPE_P (decl))
2950 /* Recompute the RTL of a local array now
2951 if it used to be an incomplete type. */
2953 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2955 /* If we used it already as memory, it must stay in memory. */
2956 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2957 /* If it's still incomplete now, no init will save it. */
2958 if (DECL_SIZE (decl) == 0)
2959 DECL_INITIAL (decl) = 0;
2964 /* If this was marked 'used', be sure it will be output. */
2965 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2966 mark_decl_referenced (decl);
2968 if (TREE_CODE (decl) == TYPE_DECL)
2970 if (!DECL_FILE_SCOPE_P (decl)
2971 && variably_modified_type_p (TREE_TYPE (decl)))
2972 add_decl_stmt (decl);
2974 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2977 /* At the end of a declaration, throw away any variable type sizes
2978 of types defined inside that declaration. There is no use
2979 computing them in the following function definition. */
2980 if (current_scope == file_scope)
2981 get_pending_sizes ();
2983 /* Install a cleanup (aka destructor) if one was given. */
2984 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2986 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2989 static bool eh_initialized_p;
2991 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2992 tree cleanup_decl = lookup_name (cleanup_id);
2995 /* Build "cleanup(&decl)" for the destructor. */
2996 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2997 cleanup = build_tree_list (NULL_TREE, cleanup);
2998 cleanup = build_function_call (cleanup_decl, cleanup);
3000 /* Don't warn about decl unused; the cleanup uses it. */
3001 TREE_USED (decl) = 1;
3003 /* Initialize EH, if we've been told to do so. */
3004 if (flag_exceptions && !eh_initialized_p)
3006 eh_initialized_p = true;
3007 eh_personality_libfunc
3008 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3009 ? "__gcc_personality_sj0"
3010 : "__gcc_personality_v0");
3011 using_eh_for_cleanups ();
3014 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3019 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3020 and push that on the current scope. */
3023 push_parm_decl (tree parm)
3027 /* Don't attempt to expand sizes while parsing this decl.
3028 (We can get here with i_s_e 1 somehow from Objective-C.) */
3029 int save_immediate_size_expand = immediate_size_expand;
3030 immediate_size_expand = 0;
3032 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3033 TREE_PURPOSE (TREE_PURPOSE (parm)),
3035 decl_attributes (&decl, TREE_VALUE (parm), 0);
3037 decl = pushdecl (decl);
3039 finish_decl (decl, NULL_TREE, NULL_TREE);
3041 immediate_size_expand = save_immediate_size_expand;
3044 /* Mark all the parameter declarations to date as forward decls.
3045 Also diagnose use of this extension. */
3048 mark_forward_parm_decls (void)
3050 struct c_binding *b;
3052 if (pedantic && !current_scope->warned_forward_parm_decls)
3054 pedwarn ("ISO C forbids forward parameter declarations");
3055 current_scope->warned_forward_parm_decls = true;
3058 for (b = current_scope->bindings; b; b = b->prev)
3059 if (TREE_CODE (b->decl) == PARM_DECL)
3060 TREE_ASM_WRITTEN (b->decl) = 1;
3063 static GTY(()) int compound_literal_number;
3065 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3066 literal, which may be an incomplete array type completed by the
3067 initializer; INIT is a CONSTRUCTOR that initializes the compound
3071 build_compound_literal (tree type, tree init)
3073 /* We do not use start_decl here because we have a type, not a declarator;
3074 and do not use finish_decl because the decl should be stored inside
3075 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3076 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3079 DECL_EXTERNAL (decl) = 0;
3080 TREE_PUBLIC (decl) = 0;
3081 TREE_STATIC (decl) = (current_scope == file_scope);
3082 DECL_CONTEXT (decl) = current_function_decl;
3083 TREE_USED (decl) = 1;
3084 TREE_TYPE (decl) = type;
3085 TREE_READONLY (decl) = TYPE_READONLY (type);
3086 store_init_value (decl, init);
3088 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3090 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3095 type = TREE_TYPE (decl);
3096 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3097 return error_mark_node;
3099 stmt = build_stmt (DECL_STMT, decl);
3100 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3101 TREE_SIDE_EFFECTS (complit) = 1;
3103 layout_decl (decl, 0);
3105 if (TREE_STATIC (decl))
3107 /* This decl needs a name for the assembler output. We also need
3108 a unique suffix to be added to the name. */
3111 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3112 compound_literal_number);
3113 compound_literal_number++;
3114 DECL_NAME (decl) = get_identifier (name);
3115 DECL_DEFER_OUTPUT (decl) = 1;
3116 DECL_COMDAT (decl) = 1;
3117 DECL_ARTIFICIAL (decl) = 1;
3119 rest_of_decl_compilation (decl, NULL, 1, 0);
3125 /* Make TYPE a complete type based on INITIAL_VALUE.
3126 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3127 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3130 complete_array_type (tree type, tree initial_value, int do_default)
3132 tree maxindex = NULL_TREE;
3137 /* Note MAXINDEX is really the maximum index,
3138 one less than the size. */
3139 if (TREE_CODE (initial_value) == STRING_CST)
3142 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3143 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3146 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3148 tree elts = CONSTRUCTOR_ELTS (initial_value);
3149 maxindex = build_int_2 (-1, -1);
3150 for (; elts; elts = TREE_CHAIN (elts))
3152 if (TREE_PURPOSE (elts))
3153 maxindex = TREE_PURPOSE (elts);
3155 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3156 maxindex, integer_one_node));
3158 maxindex = copy_node (maxindex);
3162 /* Make an error message unless that happened already. */
3163 if (initial_value != error_mark_node)
3166 /* Prevent further error messages. */
3167 maxindex = build_int_2 (0, 0);
3174 maxindex = build_int_2 (0, 0);
3180 TYPE_DOMAIN (type) = build_index_type (maxindex);
3181 if (!TREE_TYPE (maxindex))
3182 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3185 /* Lay out the type now that we can get the real answer. */
3192 /* Determine whether TYPE is a structure with a flexible array member,
3193 or a union containing such a structure (possibly recursively). */
3196 flexible_array_type_p (tree type)
3199 switch (TREE_CODE (type))
3202 x = TYPE_FIELDS (type);
3205 while (TREE_CHAIN (x) != NULL_TREE)
3207 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3208 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3209 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3210 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3214 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3216 if (flexible_array_type_p (TREE_TYPE (x)))
3225 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3226 replacing with appropriate values if they are invalid. */
3228 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3231 unsigned int max_width;
3232 unsigned HOST_WIDE_INT w;
3233 const char *name = orig_name ? orig_name: _("<anonymous>");
3236 STRIP_NOPS (*width);
3238 /* Detect and ignore out of range field width and process valid
3240 if (TREE_CODE (*width) != INTEGER_CST)
3242 error ("bit-field `%s' width not an integer constant", name);
3243 *width = integer_one_node;
3247 constant_expression_warning (*width);
3248 if (tree_int_cst_sgn (*width) < 0)
3250 error ("negative width in bit-field `%s'", name);
3251 *width = integer_one_node;
3253 else if (integer_zerop (*width) && orig_name)
3255 error ("zero width for bit-field `%s'", name);
3256 *width = integer_one_node;
3260 /* Detect invalid bit-field type. */
3261 if (TREE_CODE (*type) != INTEGER_TYPE
3262 && TREE_CODE (*type) != BOOLEAN_TYPE
3263 && TREE_CODE (*type) != ENUMERAL_TYPE)
3265 error ("bit-field `%s' has invalid type", name);
3266 *type = unsigned_type_node;
3269 type_mv = TYPE_MAIN_VARIANT (*type);
3271 && type_mv != integer_type_node
3272 && type_mv != unsigned_type_node
3273 && type_mv != boolean_type_node)
3274 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3276 if (type_mv == boolean_type_node)
3277 max_width = CHAR_TYPE_SIZE;
3279 max_width = TYPE_PRECISION (*type);
3281 if (0 < compare_tree_int (*width, max_width))
3283 error ("width of `%s' exceeds its type", name);
3285 *width = build_int_2 (w, 0);
3288 w = tree_low_cst (*width, 1);
3290 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3292 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3294 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3295 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3296 warning ("`%s' is narrower than values of its type", name);
3300 /* Given declspecs and a declarator,
3301 determine the name and type of the object declared
3302 and construct a ..._DECL node for it.
3303 (In one case we can return a ..._TYPE node instead.
3304 For invalid input we sometimes return 0.)
3306 DECLSPECS is a chain of tree_list nodes whose value fields
3307 are the storage classes and type specifiers.
3309 DECL_CONTEXT says which syntactic context this declaration is in:
3310 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3311 FUNCDEF for a function definition. Like NORMAL but a few different
3312 error messages in each case. Return value may be zero meaning
3313 this definition is too screwy to try to parse.
3314 PARM for a parameter declaration (either within a function prototype
3315 or before a function body). Make a PARM_DECL, or return void_type_node.
3316 TYPENAME if for a typename (in a cast or sizeof).
3317 Don't make a DECL node; just return the ..._TYPE node.
3318 FIELD for a struct or union field; make a FIELD_DECL.
3319 INITIALIZED is 1 if the decl has an initializer.
3320 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3321 representing the width of the bit-field.
3323 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3324 It may also be so in the PARM case, for a prototype where the
3325 argument type is specified but not the name.
3327 This function is where the complicated C meanings of `static'
3328 and `extern' are interpreted. */
3331 grokdeclarator (tree declarator, tree declspecs,
3332 enum decl_context decl_context, int initialized, tree *width)
3336 tree type = NULL_TREE;
3341 int type_quals = TYPE_UNQUALIFIED;
3343 int explicit_int = 0;
3344 int explicit_char = 0;
3345 int defaulted_int = 0;
3346 tree typedef_decl = 0;
3347 const char *name, *orig_name;
3348 tree typedef_type = 0;
3349 int funcdef_flag = 0;
3350 enum tree_code innermost_code = ERROR_MARK;
3351 int size_varies = 0;
3352 tree decl_attr = NULL_TREE;
3353 tree array_ptr_quals = NULL_TREE;
3354 int array_parm_static = 0;
3355 tree returned_attrs = NULL_TREE;
3356 bool bitfield = width != NULL;
3358 tree arg_info = NULL_TREE;
3360 if (decl_context == FUNCDEF)
3361 funcdef_flag = 1, decl_context = NORMAL;
3363 /* Look inside a declarator for the name being declared
3364 and get it as a string, for an error message. */
3366 tree decl = declarator;
3370 switch (TREE_CODE (decl))
3375 innermost_code = TREE_CODE (decl);
3376 decl = TREE_OPERAND (decl, 0);
3380 decl = TREE_VALUE (decl);
3383 case IDENTIFIER_NODE:
3384 name = IDENTIFIER_POINTER (decl);
3396 /* A function definition's declarator must have the form of
3397 a function declarator. */
3399 if (funcdef_flag && innermost_code != CALL_EXPR)
3402 /* If this looks like a function definition, make it one,
3403 even if it occurs where parms are expected.
3404 Then store_parm_decls will reject it and not use it as a parm. */
3405 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3406 decl_context = PARM;
3408 /* Look through the decl specs and record which ones appear.
3409 Some typespecs are defined as built-in typenames.
3410 Others, the ones that are modifiers of other types,
3411 are represented by bits in SPECBITS: set the bits for
3412 the modifiers that appear. Storage class keywords are also in SPECBITS.
3414 If there is a typedef name or a type, store the type in TYPE.
3415 This includes builtin typedefs such as `int'.
3417 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3418 and did not come from a user typedef.
3420 Set LONGLONG if `long' is mentioned twice. */
3422 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3424 tree id = TREE_VALUE (spec);
3426 /* If the entire declaration is itself tagged as deprecated then
3427 suppress reports of deprecated items. */
3428 if (id && TREE_DEPRECATED (id))
3430 if (deprecated_state != DEPRECATED_SUPPRESS)
3431 warn_deprecated_use (id);
3434 if (id == ridpointers[(int) RID_INT])
3436 if (id == ridpointers[(int) RID_CHAR])
3439 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3441 enum rid i = C_RID_CODE (id);
3442 if ((int) i <= (int) RID_LAST_MODIFIER)
3444 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3447 error ("`long long long' is too long for GCC");
3450 if (pedantic && !flag_isoc99 && ! in_system_header
3452 pedwarn ("ISO C90 does not support `long long'");
3456 else if (specbits & (1 << (int) i))
3458 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3460 if (pedantic && !flag_isoc99)
3461 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3464 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3467 /* Diagnose "__thread extern". Recall that this list
3468 is in the reverse order seen in the text. */
3470 && (specbits & (1 << (int) RID_EXTERN
3471 | 1 << (int) RID_STATIC)))
3473 if (specbits & 1 << (int) RID_EXTERN)
3474 error ("`__thread' before `extern'");
3476 error ("`__thread' before `static'");
3479 specbits |= 1 << (int) i;
3484 error ("two or more data types in declaration of `%s'", name);
3485 /* Actual typedefs come to us as TYPE_DECL nodes. */
3486 else if (TREE_CODE (id) == TYPE_DECL)
3488 if (TREE_TYPE (id) == error_mark_node)
3489 ; /* Allow the type to default to int to avoid cascading errors. */
3492 type = TREE_TYPE (id);
3493 decl_attr = DECL_ATTRIBUTES (id);
3497 /* Built-in types come as identifiers. */
3498 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3500 tree t = lookup_name (id);
3501 if (!t || TREE_CODE (t) != TYPE_DECL)
3502 error ("`%s' fails to be a typedef or built in type",
3503 IDENTIFIER_POINTER (id));
3504 else if (TREE_TYPE (t) == error_mark_node)
3508 type = TREE_TYPE (t);
3512 else if (TREE_CODE (id) != ERROR_MARK)
3519 typedef_type = type;
3521 size_varies = C_TYPE_VARIABLE_SIZE (type);
3523 /* No type at all: default to `int', and set DEFAULTED_INT
3524 because it was not a user-defined typedef. */
3528 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3529 | (1 << (int) RID_SIGNED)
3530 | (1 << (int) RID_UNSIGNED)
3531 | (1 << (int) RID_COMPLEX))))
3532 /* Don't warn about typedef foo = bar. */
3533 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3534 && ! in_system_header)
3536 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3537 and this is a function, or if -Wimplicit; prefer the former
3538 warning since it is more explicit. */
3539 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3541 warn_about_return_type = 1;
3542 else if (warn_implicit_int || flag_isoc99)
3543 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3548 type = integer_type_node;
3551 /* Now process the modifiers that were specified
3552 and check for invalid combinations. */
3554 /* Long double is a special combination. */
3556 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3557 && TYPE_MAIN_VARIANT (type) == double_type_node)
3559 specbits &= ~(1 << (int) RID_LONG);
3560 type = long_double_type_node;
3563 /* Check all other uses of type modifiers. */
3565 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3566 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3570 if ((specbits & 1 << (int) RID_LONG)
3571 && (specbits & 1 << (int) RID_SHORT))
3572 error ("both long and short specified for `%s'", name);
3573 else if (((specbits & 1 << (int) RID_LONG)
3574 || (specbits & 1 << (int) RID_SHORT))
3576 error ("long or short specified with char for `%s'", name);
3577 else if (((specbits & 1 << (int) RID_LONG)
3578 || (specbits & 1 << (int) RID_SHORT))
3579 && TREE_CODE (type) == REAL_TYPE)
3581 static int already = 0;
3583 error ("long or short specified with floating type for `%s'", name);
3584 if (! already && ! pedantic)
3586 error ("the only valid combination is `long double'");
3590 else if ((specbits & 1 << (int) RID_SIGNED)
3591 && (specbits & 1 << (int) RID_UNSIGNED))
3592 error ("both signed and unsigned specified for `%s'", name);
3593 else if (TREE_CODE (type) != INTEGER_TYPE)
3594 error ("long, short, signed or unsigned invalid for `%s'", name);
3598 if (!explicit_int && !defaulted_int && !explicit_char)
3600 error ("long, short, signed or unsigned used invalidly for `%s'",
3606 /* Discard the type modifiers if they are invalid. */
3609 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3610 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3615 if ((specbits & (1 << (int) RID_COMPLEX))
3616 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3618 error ("complex invalid for `%s'", name);
3619 specbits &= ~(1 << (int) RID_COMPLEX);
3622 /* Decide whether an integer type is signed or not.
3623 Optionally treat bit-fields as signed by default. */
3624 if (specbits & 1 << (int) RID_UNSIGNED
3625 || (bitfield && ! flag_signed_bitfields
3626 && (explicit_int || defaulted_int || explicit_char
3627 /* A typedef for plain `int' without `signed'
3628 can be controlled just like plain `int'. */
3629 || ! (typedef_decl != 0
3630 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3631 && TREE_CODE (type) != ENUMERAL_TYPE
3632 && !(specbits & 1 << (int) RID_SIGNED)))
3635 type = long_long_unsigned_type_node;
3636 else if (specbits & 1 << (int) RID_LONG)
3637 type = long_unsigned_type_node;
3638 else if (specbits & 1 << (int) RID_SHORT)
3639 type = short_unsigned_type_node;
3640 else if (type == char_type_node)
3641 type = unsigned_char_type_node;
3642 else if (typedef_decl)
3643 type = c_common_unsigned_type (type);
3645 type = unsigned_type_node;
3647 else if ((specbits & 1 << (int) RID_SIGNED)
3648 && type == char_type_node)
3649 type = signed_char_type_node;
3651 type = long_long_integer_type_node;
3652 else if (specbits & 1 << (int) RID_LONG)
3653 type = long_integer_type_node;
3654 else if (specbits & 1 << (int) RID_SHORT)
3655 type = short_integer_type_node;
3657 if (specbits & 1 << (int) RID_COMPLEX)
3659 if (pedantic && !flag_isoc99)
3660 pedwarn ("ISO C90 does not support complex types");
3661 /* If we just have "complex", it is equivalent to
3662 "complex double", but if any modifiers at all are specified it is
3663 the complex form of TYPE. E.g, "complex short" is
3664 "complex short int". */
3666 if (defaulted_int && ! longlong
3667 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3668 | (1 << (int) RID_SIGNED)
3669 | (1 << (int) RID_UNSIGNED))))
3672 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3673 type = complex_double_type_node;
3675 else if (type == integer_type_node)
3678 pedwarn ("ISO C does not support complex integer types");
3679 type = complex_integer_type_node;
3681 else if (type == float_type_node)
3682 type = complex_float_type_node;
3683 else if (type == double_type_node)
3684 type = complex_double_type_node;
3685 else if (type == long_double_type_node)
3686 type = complex_long_double_type_node;
3690 pedwarn ("ISO C does not support complex integer types");
3691 type = build_complex_type (type);
3695 /* Check the type and width of a bit-field. */
3697 check_bitfield_type_and_width (&type, width, orig_name);
3699 /* Figure out the type qualifiers for the declaration. There are
3700 two ways a declaration can become qualified. One is something
3701 like `const int i' where the `const' is explicit. Another is
3702 something like `typedef const int CI; CI i' where the type of the
3703 declaration contains the `const'. A third possibility is that
3704 there is a type qualifier on the element type of a typedefed
3705 array type, in which case we should extract that qualifier so
3706 that c_apply_type_quals_to_decls receives the full list of
3707 qualifiers to work with (C90 is not entirely clear about whether
3708 duplicate qualifiers should be diagnosed in this case, but it
3709 seems most appropriate to do so). */
3710 element_type = strip_array_types (type);
3711 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3713 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3715 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3716 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3717 if (pedantic && !flag_isoc99)
3720 pedwarn ("duplicate `const'");
3722 pedwarn ("duplicate `restrict'");
3724 pedwarn ("duplicate `volatile'");
3726 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3727 type = TYPE_MAIN_VARIANT (type);
3728 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3729 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3730 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3732 /* Warn if two storage classes are given. Default to `auto'. */
3737 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3738 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3739 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3740 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3741 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3743 /* "static __thread" and "extern __thread" are allowed. */
3744 if ((specbits & (1 << (int) RID_THREAD
3745 | 1 << (int) RID_STATIC
3746 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3749 /* Warn about storage classes that are invalid for certain
3750 kinds of declarations (parameters, typenames, etc.). */
3753 error ("multiple storage classes in declaration of `%s'", name);
3754 else if (funcdef_flag
3756 & ((1 << (int) RID_REGISTER)
3757 | (1 << (int) RID_AUTO)
3758 | (1 << (int) RID_TYPEDEF)
3759 | (1 << (int) RID_THREAD))))
3761 if (specbits & 1 << (int) RID_AUTO
3762 && (pedantic || current_scope == file_scope))
3763 pedwarn ("function definition declared `auto'");
3764 if (specbits & 1 << (int) RID_REGISTER)
3765 error ("function definition declared `register'");
3766 if (specbits & 1 << (int) RID_TYPEDEF)
3767 error ("function definition declared `typedef'");
3768 if (specbits & 1 << (int) RID_THREAD)
3769 error ("function definition declared `__thread'");
3770 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3771 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3773 else if (decl_context != NORMAL && nclasses > 0)
3775 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3779 switch (decl_context)
3782 error ("storage class specified for structure field `%s'",
3786 error ("storage class specified for parameter `%s'", name);
3789 error ("storage class specified for typename");
3792 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3793 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3794 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3797 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3799 /* `extern' with initialization is invalid if not at file scope. */
3800 if (current_scope == file_scope)
3801 warning ("`%s' initialized and declared `extern'", name);
3803 error ("`%s' has both `extern' and initializer", name);
3805 else if (current_scope == file_scope)
3807 if (specbits & 1 << (int) RID_AUTO)
3808 error ("file-scope declaration of `%s' specifies `auto'", name);
3812 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3813 error ("nested function `%s' declared `extern'", name);
3814 else if ((specbits & (1 << (int) RID_THREAD
3815 | 1 << (int) RID_EXTERN
3816 | 1 << (int) RID_STATIC))
3817 == (1 << (int) RID_THREAD))
3819 error ("function-scope `%s' implicitly auto and declared `__thread'",
3821 specbits &= ~(1 << (int) RID_THREAD);
3826 /* Now figure out the structure of the declarator proper.
3827 Descend through it, creating more complex types, until we reach
3828 the declared identifier (or NULL_TREE, in an absolute declarator). */
3830 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3832 if (type == error_mark_node)
3834 declarator = TREE_OPERAND (declarator, 0);
3838 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3839 an INDIRECT_REF (for *...),
3840 a CALL_EXPR (for ...(...)),
3841 a TREE_LIST (for nested attributes),
3842 an identifier (for the name being declared)
3843 or a null pointer (for the place in an absolute declarator
3844 where the name was omitted).
3845 For the last two cases, we have just exited the loop.
3847 At this point, TYPE is the type of elements of an array,
3848 or for a function to return, or for a pointer to point to.
3849 After this sequence of ifs, TYPE is the type of the
3850 array or function or pointer, and DECLARATOR has had its
3851 outermost layer removed. */
3853 if (array_ptr_quals != NULL_TREE || array_parm_static)
3855 /* Only the innermost declarator (making a parameter be of
3856 array type which is converted to pointer type)
3857 may have static or type qualifiers. */
3858 error ("static or type qualifiers in non-parameter array declarator");
3859 array_ptr_quals = NULL_TREE;
3860 array_parm_static = 0;
3863 if (TREE_CODE (declarator) == TREE_LIST)
3865 /* We encode a declarator with embedded attributes using
3867 tree attrs = TREE_PURPOSE (declarator);
3870 declarator = TREE_VALUE (declarator);
3871 inner_decl = declarator;
3872 while (inner_decl != NULL_TREE
3873 && TREE_CODE (inner_decl) == TREE_LIST)
3874 inner_decl = TREE_VALUE (inner_decl);
3875 if (inner_decl == NULL_TREE
3876 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3877 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3878 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3879 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3880 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3881 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3882 returned_attrs = decl_attributes (&type,
3883 chainon (returned_attrs, attrs),
3886 else if (TREE_CODE (declarator) == ARRAY_REF)
3888 tree itype = NULL_TREE;
3889 tree size = TREE_OPERAND (declarator, 1);
3890 /* The index is a signed object `sizetype' bits wide. */
3891 tree index_type = c_common_signed_type (sizetype);
3893 array_ptr_quals = TREE_TYPE (declarator);
3894 array_parm_static = TREE_STATIC (declarator);
3896 declarator = TREE_OPERAND (declarator, 0);
3898 /* Check for some types that there cannot be arrays of. */
3900 if (VOID_TYPE_P (type))
3902 error ("declaration of `%s' as array of voids", name);
3903 type = error_mark_node;
3906 if (TREE_CODE (type) == FUNCTION_TYPE)
3908 error ("declaration of `%s' as array of functions", name);
3909 type = error_mark_node;
3912 if (pedantic && !in_system_header && flexible_array_type_p (type))
3913 pedwarn ("invalid use of structure with flexible array member");
3915 if (size == error_mark_node)
3916 type = error_mark_node;
3918 if (type == error_mark_node)
3921 /* If size was specified, set ITYPE to a range-type for that size.
3922 Otherwise, ITYPE remains null. finish_decl may figure it out
3923 from an initial value. */
3927 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3928 STRIP_TYPE_NOPS (size);
3930 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3932 error ("size of array `%s' has non-integer type", name);
3933 size = integer_one_node;
3936 if (pedantic && integer_zerop (size))
3937 pedwarn ("ISO C forbids zero-size array `%s'", name);
3939 if (TREE_CODE (size) == INTEGER_CST)
3941 constant_expression_warning (size);
3942 if (tree_int_cst_sgn (size) < 0)
3944 error ("size of array `%s' is negative", name);
3945 size = integer_one_node;
3950 /* Make sure the array size remains visibly nonconstant
3951 even if it is (eg) a const variable with known value. */
3954 if (!flag_isoc99 && pedantic)
3956 if (TREE_CONSTANT (size))
3957 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3960 pedwarn ("ISO C90 forbids variable-size array `%s'",
3965 if (integer_zerop (size))
3967 /* A zero-length array cannot be represented with an
3968 unsigned index type, which is what we'll get with
3969 build_index_type. Create an open-ended range instead. */
3970 itype = build_range_type (sizetype, size, NULL_TREE);
3974 /* Compute the maximum valid index, that is, size - 1.
3975 Do the calculation in index_type, so that if it is
3976 a variable the computations will be done in the
3978 itype = fold (build (MINUS_EXPR, index_type,
3979 convert (index_type, size),
3980 convert (index_type, size_one_node)));
3982 /* If that overflowed, the array is too big.
3983 ??? While a size of INT_MAX+1 technically shouldn't
3984 cause an overflow (because we subtract 1), the overflow
3985 is recorded during the conversion to index_type, before
3986 the subtraction. Handling this case seems like an
3987 unnecessary complication. */
3988 if (TREE_OVERFLOW (itype))
3990 error ("size of array `%s' is too large", name);
3991 type = error_mark_node;
3997 /* We must be able to distinguish the
3998 SAVE_EXPR_CONTEXT for the variably-sized type
3999 so that we can set it correctly in
4000 set_save_expr_context. The convention is
4001 that all SAVE_EXPRs that need to be reset
4002 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4003 tree cfd = current_function_decl;
4004 if (decl_context == PARM)
4005 current_function_decl = NULL_TREE;
4006 itype = variable_size (itype);
4007 if (decl_context == PARM)
4008 current_function_decl = cfd;
4010 itype = build_index_type (itype);
4013 else if (decl_context == FIELD)
4015 if (pedantic && !flag_isoc99 && !in_system_header)
4016 pedwarn ("ISO C90 does not support flexible array members");
4018 /* ISO C99 Flexible array members are effectively identical
4019 to GCC's zero-length array extension. */
4020 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4023 /* If pedantic, complain about arrays of incomplete types. */
4025 if (pedantic && !COMPLETE_TYPE_P (type))
4026 pedwarn ("array type has incomplete element type");
4028 /* Build the array type itself, then merge any constancy or
4029 volatility into the target type. We must do it in this order
4030 to ensure that the TYPE_MAIN_VARIANT field of the array type
4031 is set correctly. */
4033 type = build_array_type (type, itype);
4035 type = c_build_qualified_type (type, type_quals);
4038 C_TYPE_VARIABLE_SIZE (type) = 1;
4040 /* The GCC extension for zero-length arrays differs from
4041 ISO flexible array members in that sizeof yields zero. */
4042 if (size && integer_zerop (size))
4045 TYPE_SIZE (type) = bitsize_zero_node;
4046 TYPE_SIZE_UNIT (type) = size_zero_node;
4048 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4049 /* We can never complete an array type which is the target of a
4050 pointer, so go ahead and lay it out. */
4053 if (decl_context != PARM
4054 && (array_ptr_quals != NULL_TREE || array_parm_static))
4056 error ("static or type qualifiers in non-parameter array declarator");
4057 array_ptr_quals = NULL_TREE;
4058 array_parm_static = 0;
4061 else if (TREE_CODE (declarator) == CALL_EXPR)
4063 /* Say it's a definition only for the CALL_EXPR closest to
4065 bool really_funcdef = (funcdef_flag
4066 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4067 == IDENTIFIER_NODE));
4070 /* Declaring a function type.
4071 Make sure we have a valid type for the function to return. */
4072 if (type == error_mark_node)
4077 /* Warn about some types functions can't return. */
4079 if (TREE_CODE (type) == FUNCTION_TYPE)
4081 error ("`%s' declared as function returning a function", name);
4082 type = integer_type_node;
4084 if (TREE_CODE (type) == ARRAY_TYPE)