1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
35 #include "tree-inline.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
74 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* True means we've initialized exception handling. */
80 bool c_eh_initialized_p;
82 /* While defining an enum type, this is 1 plus the last enumerator
83 constant value. Note that will do not have to save this or `enum_overflow'
84 around nested function definition since such a definition could only
85 occur in an enum value expression and we don't use these variables in
88 static tree enum_next_value;
90 /* Nonzero means that there was overflow computing enum_next_value. */
92 static int enum_overflow;
94 /* These #defines are for clarity in working with the information block
95 returned by get_parm_info. */
96 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
97 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
98 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
99 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
101 /* The file and line that the prototype came from if this is an
102 old-style definition; used for diagnostics in
103 store_parm_decls_oldstyle. */
105 static location_t current_function_prototype_locus;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* State saving variables. */
115 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
116 included in this invocation. Note that the current translation
117 unit is not included in this list. */
119 static GTY(()) tree all_translation_units;
121 /* A list of decls to be made automatically visible in each file scope. */
122 static GTY(()) tree visible_builtins;
124 /* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement that specifies a return value is seen. */
127 int current_function_returns_value;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement with no argument is seen. */
132 int current_function_returns_null;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a call to a noreturn function is seen. */
137 int current_function_returns_abnormally;
139 /* Set to nonzero by `grokdeclarator' for a function
140 whose return type is defaulted, if warnings for this are desired. */
142 static int warn_about_return_type;
144 /* Nonzero when starting a function declared `extern inline'. */
146 static int current_extern_inline;
148 /* True means global_bindings_p should return false even if the scope stack
149 says we are in file scope. */
150 bool c_override_global_bindings_to_false;
153 /* Each c_binding structure describes one binding of an identifier to
154 a decl. All the decls in a scope - irrespective of namespace - are
155 chained together by the ->prev field, which (as the name implies)
156 runs in reverse order. All the decls in a given namespace bound to
157 a given identifier are chained by the ->shadowed field, which runs
158 from inner to outer scopes.
160 The ->decl field usually points to a DECL node, but there are two
161 exceptions. In the namespace of type tags, the bound entity is a
162 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
163 identifier is encountered, it is bound to error_mark_node to
164 suppress further errors about that identifier in the current
167 The depth field is copied from the scope structure that holds this
168 decl. It is used to preserve the proper ordering of the ->shadowed
169 field (see bind()) and also for a handful of special-case checks.
170 Finally, the invisible bit is true for a decl which should be
171 ignored for purposes of normal name lookup, and the nested bit is
172 true for a decl that's been bound a second time in an inner scope;
173 in all such cases, the binding in the outer scope will have its
174 invisible bit true. */
176 struct c_binding GTY((chain_next ("%h.prev")))
178 tree decl; /* the decl bound */
179 tree id; /* the identifier it's bound to */
180 struct c_binding *prev; /* the previous decl in this scope */
181 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
182 unsigned int depth : 28; /* depth of this scope */
183 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
184 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
187 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
188 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
189 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
190 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
192 #define I_SYMBOL_BINDING(node) \
193 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
194 #define I_SYMBOL_DECL(node) \
195 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
197 #define I_TAG_BINDING(node) \
198 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
199 #define I_TAG_DECL(node) \
200 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
202 #define I_LABEL_BINDING(node) \
203 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
204 #define I_LABEL_DECL(node) \
205 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
207 /* Each C symbol points to three linked lists of c_binding structures.
208 These describe the values of the identifier in the three different
209 namespaces defined by the language. */
211 struct lang_identifier GTY(())
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The resulting tree type. */
226 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
227 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *)TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *)TREE_CHAIN (&%h.generic)")))
229 union tree_node GTY ((tag ("0"),
230 desc ("tree_node_structure (&%h)")))
232 struct lang_identifier GTY ((tag ("1"))) identifier;
235 /* Each c_scope structure describes the complete contents of one
236 scope. Four scopes are distinguished specially: the innermost or
237 current scope, the innermost function scope, the file scope (always
238 the second to outermost) and the outermost or external scope.
240 Most declarations are recorded in the current scope.
242 All normal label declarations are recorded in the innermost
243 function scope, as are bindings of undeclared identifiers to
244 error_mark_node. (GCC permits nested functions as an extension,
245 hence the 'innermost' qualifier.) Explicitly declared labels
246 (using the __label__ extension) appear in the current scope.
248 Being in the file scope (current_scope == file_scope) causes
249 special behavior in several places below. Also, under some
250 conditions the Objective-C front end records declarations in the
251 file scope even though that isn't the current scope.
253 All declarations with external linkage are recorded in the external
254 scope, even if they aren't visible there; this models the fact that
255 such declarations are visible to the entire program, and (with a
256 bit of cleverness, see pushdecl) allows diagnosis of some violations
257 of C99 6.2.2p7 and 6.2.7p2:
259 If, within the same translation unit, the same identifier appears
260 with both internal and external linkage, the behavior is
263 All declarations that refer to the same object or function shall
264 have compatible type; otherwise, the behavior is undefined.
266 Initially only the built-in declarations, which describe compiler
267 intrinsic functions plus a subset of the standard library, are in
270 The order of the blocks list matters, and it is frequently appended
271 to. To avoid having to walk all the way to the end of the list on
272 each insertion, or reverse the list later, we maintain a pointer to
273 the last list entry. (FIXME: It should be feasible to use a reversed
276 The bindings list is strictly in reverse order of declarations;
277 pop_scope relies on this. */
280 struct c_scope GTY((chain_next ("%h.outer")))
282 /* The scope containing this one. */
283 struct c_scope *outer;
285 /* The next outermost function scope. */
286 struct c_scope *outer_function;
288 /* All bindings in this scope. */
289 struct c_binding *bindings;
291 /* For each scope (except the global one), a chain of BLOCK nodes
292 for all the scopes that were entered and exited one level down. */
296 /* The depth of this scope. Used to keep the ->shadowed chain of
297 bindings sorted innermost to outermost. */
298 unsigned int depth : 28;
300 /* True if we are currently filling this scope with parameter
302 BOOL_BITFIELD parm_flag : 1;
304 /* True if we already complained about forward parameter decls
305 in this scope. This prevents double warnings on
306 foo (int a; int b; ...) */
307 BOOL_BITFIELD warned_forward_parm_decls : 1;
309 /* True if this is the outermost block scope of a function body.
310 This scope contains the parameters, the local variables declared
311 in the outermost block, and all the labels (except those in
312 nested functions, or declared at block scope with __label__). */
313 BOOL_BITFIELD function_body : 1;
315 /* True means make a BLOCK for this scope no matter what. */
316 BOOL_BITFIELD keep : 1;
319 /* The scope currently in effect. */
321 static GTY(()) struct c_scope *current_scope;
323 /* The innermost function scope. Ordinary (not explicitly declared)
324 labels, bindings to error_mark_node, and the lazily-created
325 bindings of __func__ and its friends get this scope. */
327 static GTY(()) struct c_scope *current_function_scope;
329 /* The C file scope. This is reset for each input translation unit. */
331 static GTY(()) struct c_scope *file_scope;
333 /* The outermost scope. This is used for all declarations with
334 external linkage, and only these, hence the name. */
336 static GTY(()) struct c_scope *external_scope;
338 /* A chain of c_scope structures awaiting reuse. */
340 static GTY((deletable)) struct c_scope *scope_freelist;
342 /* A chain of c_binding structures awaiting reuse. */
344 static GTY((deletable)) struct c_binding *binding_freelist;
346 /* Append VAR to LIST in scope SCOPE. */
347 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
348 struct c_scope *s_ = (scope); \
350 if (s_->list##_last) \
351 TREE_CHAIN (s_->list##_last) = d_; \
354 s_->list##_last = d_; \
357 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
358 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
359 struct c_scope *t_ = (tscope); \
360 struct c_scope *f_ = (fscope); \
362 TREE_CHAIN (t_->to##_last) = f_->from; \
365 t_->to##_last = f_->from##_last; \
368 /* True means unconditionally make a BLOCK for the next scope pushed. */
370 static bool keep_next_level_flag;
372 /* True means the next call to push_scope will be the outermost scope
373 of a function body, so do not push a new scope, merely cease
374 expecting parameter decls. */
376 static bool next_is_function_body;
378 /* Functions called automatically at the beginning and end of execution. */
380 tree static_ctors, static_dtors;
382 /* Forward declarations. */
383 static tree lookup_name_in_scope (tree, struct c_scope *);
384 static tree c_make_fname_decl (tree, int);
385 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
386 static tree grokparms (tree, int);
387 static void layout_array_type (tree);
389 /* States indicating how grokdeclarator() should handle declspecs marked
390 with __attribute__((deprecated)). An object declared as
391 __attribute__((deprecated)) suppresses warnings of uses of other
394 enum deprecated_states {
399 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
402 c_print_identifier (FILE *file, tree node, int indent)
404 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
405 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
406 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
407 if (C_IS_RESERVED_WORD (node))
409 tree rid = ridpointers[C_RID_CODE (node)];
410 indent_to (file, indent + 4);
411 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
412 (void *) rid, IDENTIFIER_POINTER (rid));
416 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
417 which may be any of several kinds of DECL or TYPE or error_mark_node,
418 in the scope SCOPE. */
420 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
422 struct c_binding *b, **here;
424 if (binding_freelist)
426 b = binding_freelist;
427 binding_freelist = b->prev;
430 b = ggc_alloc (sizeof (struct c_binding));
435 b->depth = scope->depth;
436 b->invisible = invisible;
439 b->prev = scope->bindings;
445 switch (TREE_CODE (decl))
447 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
450 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
456 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
462 /* Locate the appropriate place in the chain of shadowed decls
463 to insert this binding. Normally, scope == current_scope and
464 this does nothing. */
465 while (*here && (*here)->depth > scope->depth)
466 here = &(*here)->shadowed;
472 /* Clear the binding structure B, stick it on the binding_freelist,
473 and return the former value of b->prev. This is used by pop_scope
474 and get_parm_info to iterate destructively over all the bindings
475 from a given scope. */
476 static struct c_binding *
477 free_binding_and_advance (struct c_binding *b)
479 struct c_binding *prev = b->prev;
481 memset (b, 0, sizeof (struct c_binding));
482 b->prev = binding_freelist;
483 binding_freelist = b;
489 /* Hook called at end of compilation to assume 1 elt
490 for a file-scope tentative array defn that wasn't complete before. */
493 c_finish_incomplete_decl (tree decl)
495 if (TREE_CODE (decl) == VAR_DECL)
497 tree type = TREE_TYPE (decl);
498 if (type != error_mark_node
499 && TREE_CODE (type) == ARRAY_TYPE
500 && ! DECL_EXTERNAL (decl)
501 && TYPE_DOMAIN (type) == 0)
503 warning ("%Jarray '%D' assumed to have one element", decl, decl);
505 complete_array_type (type, NULL_TREE, 1);
507 layout_decl (decl, 0);
512 /* The Objective-C front-end often needs to determine the current scope. */
515 get_current_scope (void)
517 return current_scope;
520 /* The following function is used only by Objective-C. It needs to live here
521 because it accesses the innards of c_scope. */
524 objc_mark_locals_volatile (void *enclosing_blk)
526 struct c_scope *scope;
529 for (scope = current_scope;
530 scope && scope != enclosing_blk;
531 scope = scope->outer)
533 for (b = scope->bindings; b; b = b->prev)
535 if (TREE_CODE (b->decl) == VAR_DECL
536 || TREE_CODE (b->decl) == PARM_DECL)
538 C_DECL_REGISTER (b->decl) = 0;
539 DECL_REGISTER (b->decl) = 0;
540 TREE_THIS_VOLATILE (b->decl) = 1;
544 /* Do not climb up past the current function. */
545 if (scope->function_body)
550 /* Nonzero if we are currently in file scope. */
553 global_bindings_p (void)
555 return current_scope == file_scope && !c_override_global_bindings_to_false;
559 keep_next_level (void)
561 keep_next_level_flag = true;
564 /* Identify this scope as currently being filled with parameters. */
567 declare_parm_level (void)
569 current_scope->parm_flag = true;
575 if (next_is_function_body)
577 /* This is the transition from the parameters to the top level
578 of the function body. These are the same scope
579 (C99 6.2.1p4,6) so we do not push another scope structure.
580 next_is_function_body is set only by store_parm_decls, which
581 in turn is called when and only when we are about to
582 encounter the opening curly brace for the function body.
584 The outermost block of a function always gets a BLOCK node,
585 because the debugging output routines expect that each
586 function has at least one BLOCK. */
587 current_scope->parm_flag = false;
588 current_scope->function_body = true;
589 current_scope->keep = true;
590 current_scope->outer_function = current_function_scope;
591 current_function_scope = current_scope;
593 keep_next_level_flag = false;
594 next_is_function_body = false;
598 struct c_scope *scope;
601 scope = scope_freelist;
602 scope_freelist = scope->outer;
605 scope = ggc_alloc_cleared (sizeof (struct c_scope));
607 scope->keep = keep_next_level_flag;
608 scope->outer = current_scope;
609 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
611 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
613 if (current_scope && scope->depth == 0)
616 sorry ("GCC supports only %u nested scopes\n", scope->depth);
619 current_scope = scope;
620 keep_next_level_flag = false;
624 /* Exit a scope. Restore the state of the identifier-decl mappings
625 that were in effect when this scope was entered. Return a BLOCK
626 node containing all the DECLs in this scope that are of interest
627 to debug info generation. */
632 struct c_scope *scope = current_scope;
633 tree block, context, p;
636 bool functionbody = scope->function_body;
637 bool keep = functionbody || scope->keep || scope->bindings;
639 /* If appropriate, create a BLOCK to record the decls for the life
644 block = make_node (BLOCK);
645 BLOCK_SUBBLOCKS (block) = scope->blocks;
646 TREE_USED (block) = 1;
648 /* In each subblock, record that this is its superior. */
649 for (p = scope->blocks; p; p = TREE_CHAIN (p))
650 BLOCK_SUPERCONTEXT (p) = block;
652 BLOCK_VARS (block) = 0;
655 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
656 scope must be set so that they point to the appropriate
657 construct, i.e. either to the current FUNCTION_DECL node, or
658 else to the BLOCK node we just constructed.
660 Note that for tagged types whose scope is just the formal
661 parameter list for some function type specification, we can't
662 properly set their TYPE_CONTEXTs here, because we don't have a
663 pointer to the appropriate FUNCTION_TYPE node readily available
664 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
665 type nodes get set in `grokdeclarator' as soon as we have created
666 the FUNCTION_TYPE node which will represent the "scope" for these
667 "parameter list local" tagged types. */
668 if (scope->function_body)
669 context = current_function_decl;
670 else if (scope == file_scope)
672 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
673 TREE_CHAIN (file_decl) = all_translation_units;
674 all_translation_units = file_decl;
680 /* Clear all bindings in this scope. */
681 for (b = scope->bindings; b; b = free_binding_and_advance (b))
684 switch (TREE_CODE (p))
687 /* Warnings for unused labels, errors for undefined labels. */
688 if (TREE_USED (p) && !DECL_INITIAL (p))
690 error ("%Jlabel `%D' used but not defined", p, p);
691 DECL_INITIAL (p) = error_mark_node;
693 else if (!TREE_USED (p) && warn_unused_label)
695 if (DECL_INITIAL (p))
696 warning ("%Jlabel `%D' defined but not used", p, p);
698 warning ("%Jlabel `%D' declared but not defined", p, p);
700 /* Labels go in BLOCK_VARS. */
701 TREE_CHAIN (p) = BLOCK_VARS (block);
702 BLOCK_VARS (block) = p;
704 #ifdef ENABLE_CHECKING
705 if (I_LABEL_BINDING (b->id) != b) abort ();
707 I_LABEL_BINDING (b->id) = b->shadowed;
713 TYPE_CONTEXT (p) = context;
715 /* Types may not have tag-names, in which case the type
716 appears in the bindings list with b->id NULL. */
719 #ifdef ENABLE_CHECKING
720 if (I_TAG_BINDING (b->id) != b) abort ();
722 I_TAG_BINDING (b->id) = b->shadowed;
727 /* Propagate TREE_ADDRESSABLE from nested functions to their
728 containing functions. */
729 if (! TREE_ASM_WRITTEN (p)
730 && DECL_INITIAL (p) != 0
731 && TREE_ADDRESSABLE (p)
732 && DECL_ABSTRACT_ORIGIN (p) != 0
733 && DECL_ABSTRACT_ORIGIN (p) != p)
734 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
738 /* Warnings for unused variables. Keep this in sync with
739 stmt.c:warn_about_unused_variables, which we cannot use
740 since it expects a different data structure. */
741 if (warn_unused_variable
743 && !DECL_IN_SYSTEM_HEADER (p)
745 && !DECL_ARTIFICIAL (p)
746 && (scope != file_scope
747 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
748 && !TREE_THIS_VOLATILE (p)))
749 && scope != external_scope)
750 warning ("%Junused variable `%D'", p, p);
756 /* All of these go in BLOCK_VARS, but only if this is the
757 binding in the home scope. */
760 TREE_CHAIN (p) = BLOCK_VARS (block);
761 BLOCK_VARS (block) = p;
763 /* If this is the file scope, must set DECL_CONTEXT on
764 these. Do so even for externals, so that
765 same_translation_unit_p works. */
766 if (scope == file_scope)
767 DECL_CONTEXT (p) = context;
770 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
771 already been put there by store_parm_decls. Unused-
772 parameter warnings are handled by function.c.
773 error_mark_node obviously does not go in BLOCK_VARS and
774 does not get unused-variable warnings. */
777 /* It is possible for a decl not to have a name. We get
778 here with b->id NULL in this case. */
781 #ifdef ENABLE_CHECKING
782 if (I_SYMBOL_BINDING (b->id) != b) abort ();
784 I_SYMBOL_BINDING (b->id) = b->shadowed;
794 /* Dispose of the block that we just made inside some higher level. */
795 if ((scope->function_body || scope == file_scope) && context)
797 DECL_INITIAL (context) = block;
798 BLOCK_SUPERCONTEXT (block) = context;
800 else if (scope->outer)
803 SCOPE_LIST_APPEND (scope->outer, blocks, block);
804 /* If we did not make a block for the scope just exited, any
805 blocks made for inner scopes must be carried forward so they
806 will later become subblocks of something else. */
807 else if (scope->blocks)
808 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
811 /* Pop the current scope, and free the structure for reuse. */
812 current_scope = scope->outer;
813 if (scope->function_body)
814 current_function_scope = scope->outer_function;
816 memset (scope, 0, sizeof (struct c_scope));
817 scope->outer = scope_freelist;
818 scope_freelist = scope;
824 push_file_scope (void)
832 file_scope = current_scope;
834 start_fname_decls ();
836 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
837 bind (DECL_NAME (decl), decl, file_scope,
838 /*invisible=*/false, /*nested=*/true);
842 pop_file_scope (void)
844 /* In case there were missing closebraces, get us back to the global
846 while (current_scope != file_scope)
849 /* __FUNCTION__ is defined at file scope (""). This
850 call may not be necessary as my tests indicate it
851 still works without it. */
852 finish_fname_decls ();
854 /* Kludge: don't actually pop the file scope if generating a
855 precompiled header, so that macros and local symbols are still
856 visible to the PCH generator. */
860 /* And pop off the file scope. */
864 cpp_undef_all (parse_in);
867 /* Insert BLOCK at the end of the list of subblocks of the current
868 scope. This is used when a BIND_EXPR is expanded, to handle the
869 BLOCK node inside the BIND_EXPR. */
872 insert_block (tree block)
874 TREE_USED (block) = 1;
875 SCOPE_LIST_APPEND (current_scope, blocks, block);
878 /* Push a definition or a declaration of struct, union or enum tag "name".
879 "type" should be the type node.
880 We assume that the tag "name" is not already defined.
882 Note that the definition may really be just a forward reference.
883 In that case, the TYPE_SIZE will be zero. */
886 pushtag (tree name, tree type)
888 /* Record the identifier as the type's name if it has none. */
889 if (name && !TYPE_NAME (type))
890 TYPE_NAME (type) = name;
891 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
893 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
894 tagged type we just added to the current scope. This fake
895 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
896 to output a representation of a tagged type, and it also gives
897 us a convenient place to record the "scope start" address for the
900 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
902 /* An approximation for now, so we can tell this is a function-scope tag.
903 This will be updated in pop_scope. */
904 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
907 /* Subroutine of compare_decls. Allow harmless mismatches in return
908 and argument types provided that the type modes match. This function
909 return a unified type given a suitable match, and 0 otherwise. */
912 match_builtin_function_types (tree newtype, tree oldtype)
914 tree newrettype, oldrettype;
915 tree newargs, oldargs;
916 tree trytype, tryargs;
918 /* Accept the return type of the new declaration if same modes. */
919 oldrettype = TREE_TYPE (oldtype);
920 newrettype = TREE_TYPE (newtype);
922 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
925 oldargs = TYPE_ARG_TYPES (oldtype);
926 newargs = TYPE_ARG_TYPES (newtype);
929 while (oldargs || newargs)
933 || ! TREE_VALUE (oldargs)
934 || ! TREE_VALUE (newargs)
935 || TYPE_MODE (TREE_VALUE (oldargs))
936 != TYPE_MODE (TREE_VALUE (newargs)))
939 oldargs = TREE_CHAIN (oldargs);
940 newargs = TREE_CHAIN (newargs);
943 trytype = build_function_type (newrettype, tryargs);
944 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
947 /* Subroutine of diagnose_mismatched_decls. Check for function type
948 mismatch involving an empty arglist vs a nonempty one and give clearer
951 diagnose_arglist_conflict (tree newdecl, tree olddecl,
952 tree newtype, tree oldtype)
956 if (TREE_CODE (olddecl) != FUNCTION_DECL
957 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
958 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
960 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
963 t = TYPE_ARG_TYPES (oldtype);
965 t = TYPE_ARG_TYPES (newtype);
966 for (; t; t = TREE_CHAIN (t))
968 tree type = TREE_VALUE (t);
970 if (TREE_CHAIN (t) == 0
971 && TYPE_MAIN_VARIANT (type) != void_type_node)
973 inform ("a parameter list with an ellipsis can't match "
974 "an empty parameter name list declaration");
978 if (c_type_promotes_to (type) != type)
980 inform ("an argument type that has a default promotion can't match "
981 "an empty parameter name list declaration");
987 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
988 old-style function definition, NEWDECL is a prototype declaration.
989 Diagnose inconsistencies in the argument list. Returns TRUE if
990 the prototype is compatible, FALSE if not. */
992 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
994 tree newargs, oldargs;
997 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
998 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1000 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1001 newargs = TYPE_ARG_TYPES (newtype);
1006 tree oldargtype = TREE_VALUE (oldargs);
1007 tree newargtype = TREE_VALUE (newargs);
1009 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1012 /* Reaching the end of just one list means the two decls don't
1013 agree on the number of arguments. */
1014 if (END_OF_ARGLIST (oldargtype))
1016 error ("%Jprototype for '%D' declares more arguments "
1017 "than previous old-style definition", newdecl, newdecl);
1020 else if (END_OF_ARGLIST (newargtype))
1022 error ("%Jprototype for '%D' declares fewer arguments "
1023 "than previous old-style definition", newdecl, newdecl);
1027 /* Type for passing arg must be consistent with that declared
1029 else if (! comptypes (oldargtype, newargtype))
1031 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1032 newdecl, newdecl, i);
1036 oldargs = TREE_CHAIN (oldargs);
1037 newargs = TREE_CHAIN (newargs);
1041 /* If we get here, no errors were found, but do issue a warning
1042 for this poor-style construct. */
1043 warning ("%Jprototype for '%D' follows non-prototype definition",
1046 #undef END_OF_ARGLIST
1049 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1050 first in a pair of mismatched declarations, using the diagnostic
1053 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1055 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1057 else if (DECL_INITIAL (decl))
1058 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1059 else if (C_DECL_IMPLICIT (decl))
1060 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1062 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1065 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1066 Returns true if the caller should proceed to merge the two, false
1067 if OLDDECL should simply be discarded. As a side effect, issues
1068 all necessary diagnostics for invalid or poor-style combinations.
1069 If it returns true, writes the types of NEWDECL and OLDDECL to
1070 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1071 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1074 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1075 tree *newtypep, tree *oldtypep)
1077 tree newtype, oldtype;
1078 bool pedwarned = false;
1079 bool warned = false;
1081 /* If we have error_mark_node for either decl or type, just discard
1082 the previous decl - we're in an error cascade already. */
1083 if (olddecl == error_mark_node || newdecl == error_mark_node)
1085 *oldtypep = oldtype = TREE_TYPE (olddecl);
1086 *newtypep = newtype = TREE_TYPE (newdecl);
1087 if (oldtype == error_mark_node || newtype == error_mark_node)
1090 /* Two different categories of symbol altogether. This is an error
1091 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1092 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1094 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1095 && DECL_BUILT_IN (olddecl)
1096 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1098 error ("%J'%D' redeclared as different kind of symbol",
1100 locate_old_decl (olddecl, error);
1102 else if (TREE_PUBLIC (newdecl))
1103 warning ("%Jbuilt-in function '%D' declared as non-function",
1105 else if (warn_shadow)
1106 warning ("%Jdeclaration of '%D' shadows a built-in function",
1111 if (!comptypes (oldtype, newtype))
1113 if (TREE_CODE (olddecl) == FUNCTION_DECL
1114 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1116 /* Accept harmless mismatch in function types.
1117 This is for the ffs and fprintf builtins. */
1118 tree trytype = match_builtin_function_types (newtype, oldtype);
1120 if (trytype && comptypes (newtype, trytype))
1121 *oldtypep = oldtype = trytype;
1124 /* If types don't match for a built-in, throw away the
1125 built-in. No point in calling locate_old_decl here, it
1126 won't print anything. */
1127 warning ("%Jconflicting types for built-in function '%D'",
1132 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1133 && DECL_IS_BUILTIN (olddecl))
1135 /* A conflicting function declaration for a predeclared
1136 function that isn't actually built in. Objective C uses
1137 these. The new declaration silently overrides everything
1138 but the volatility (i.e. noreturn) indication. See also
1139 below. FIXME: Make Objective C use normal builtins. */
1140 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1143 /* Permit void foo (...) to match int foo (...) if the latter is
1144 the definition and implicit int was used. See
1145 c-torture/compile/920625-2.c. */
1146 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1147 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1148 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1149 && C_FUNCTION_IMPLICIT_INT (newdecl))
1151 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1152 /* Make sure we keep void as the return type. */
1153 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1154 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1159 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1160 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1162 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1163 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1164 locate_old_decl (olddecl, error);
1169 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1170 but silently ignore the redeclaration if either is in a system
1171 header. (Conflicting redeclarations were handled above.) */
1172 if (TREE_CODE (newdecl) == TYPE_DECL)
1174 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1175 return true; /* Allow OLDDECL to continue in use. */
1177 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1178 locate_old_decl (olddecl, error);
1182 /* Function declarations can either be 'static' or 'extern' (no
1183 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1184 can never conflict with each other on account of linkage (6.2.2p4).
1185 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1186 two definitions if one is 'extern inline' and one is not. The non-
1187 extern-inline definition supersedes the extern-inline definition. */
1188 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1190 /* If you declare a built-in function name as static, or
1191 define the built-in with an old-style definition (so we
1192 can't validate the argument list) the built-in definition is
1193 overridden, but optionally warn this was a bad choice of name. */
1194 if (DECL_BUILT_IN (olddecl)
1195 && !C_DECL_DECLARED_BUILTIN (olddecl)
1196 && (!TREE_PUBLIC (newdecl)
1197 || (DECL_INITIAL (newdecl)
1198 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1201 warning ("%Jdeclaration of '%D' shadows a built-in function",
1203 /* Discard the old built-in function. */
1207 if (DECL_INITIAL (newdecl))
1209 if (DECL_INITIAL (olddecl)
1210 && !(DECL_DECLARED_INLINE_P (olddecl)
1211 && DECL_EXTERNAL (olddecl)
1212 && !(DECL_DECLARED_INLINE_P (newdecl)
1213 && DECL_EXTERNAL (newdecl))))
1215 error ("%Jredefinition of '%D'", newdecl, newdecl);
1216 locate_old_decl (olddecl, error);
1220 /* If we have a prototype after an old-style function definition,
1221 the argument types must be checked specially. */
1222 else if (DECL_INITIAL (olddecl)
1223 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1224 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1225 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1227 locate_old_decl (olddecl, error);
1230 /* A non-static declaration (even an "extern") followed by a
1231 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1232 The same is true for a static forward declaration at block
1233 scope followed by a non-static declaration/definition at file
1234 scope. Static followed by non-static at the same scope is
1235 not undefined behavior, and is the most convenient way to get
1236 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1237 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1238 we do diagnose it if -Wtraditional. */
1239 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1241 /* Two exceptions to the rule. If olddecl is an extern
1242 inline, or a predeclared function that isn't actually
1243 built in, newdecl silently overrides olddecl. The latter
1244 occur only in Objective C; see also above. (FIXME: Make
1245 Objective C use normal builtins.) */
1246 if (!DECL_IS_BUILTIN (olddecl)
1247 && !(DECL_EXTERNAL (olddecl)
1248 && DECL_DECLARED_INLINE_P (olddecl)))
1250 error ("%Jstatic declaration of '%D' follows "
1251 "non-static declaration", newdecl, newdecl);
1252 locate_old_decl (olddecl, error);
1256 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1258 if (DECL_CONTEXT (olddecl))
1260 error ("%Jnon-static declaration of '%D' follows "
1261 "static declaration", newdecl, newdecl);
1262 locate_old_decl (olddecl, error);
1265 else if (warn_traditional)
1267 warning ("%Jnon-static declaration of '%D' follows "
1268 "static declaration", newdecl, newdecl);
1273 else if (TREE_CODE (newdecl) == VAR_DECL)
1275 /* Only variables can be thread-local, and all declarations must
1276 agree on this property. */
1277 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1279 if (DECL_THREAD_LOCAL (newdecl))
1280 error ("%Jthread-local declaration of '%D' follows "
1281 "non-thread-local declaration", newdecl, newdecl);
1283 error ("%Jnon-thread-local declaration of '%D' follows "
1284 "thread-local declaration", newdecl, newdecl);
1286 locate_old_decl (olddecl, error);
1290 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1291 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1293 error ("%Jredefinition of '%D'", newdecl, newdecl);
1294 locate_old_decl (olddecl, error);
1298 /* Objects declared at file scope: if the first declaration had
1299 external linkage (even if it was an external reference) the
1300 second must have external linkage as well, or the behavior is
1301 undefined. If the first declaration had internal linkage, then
1302 the second must too, or else be an external reference (in which
1303 case the composite declaration still has internal linkage).
1304 As for function declarations, we warn about the static-then-
1305 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1306 if (DECL_FILE_SCOPE_P (newdecl)
1307 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1309 if (DECL_EXTERNAL (newdecl))
1311 if (warn_traditional)
1313 warning ("%Jnon-static declaration of '%D' follows "
1314 "static declaration", newdecl, newdecl);
1320 if (TREE_PUBLIC (newdecl))
1321 error ("%Jnon-static declaration of '%D' follows "
1322 "static declaration", newdecl, newdecl);
1324 error ("%Jstatic declaration of '%D' follows "
1325 "non-static declaration", newdecl, newdecl);
1327 locate_old_decl (olddecl, error);
1331 /* Two objects with the same name declared at the same block
1332 scope must both be external references (6.7p3). */
1333 else if (!DECL_FILE_SCOPE_P (newdecl)
1334 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1335 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1337 if (DECL_EXTERNAL (newdecl))
1338 error ("%Jextern declaration of '%D' follows "
1339 "declaration with no linkage", newdecl, newdecl);
1340 else if (DECL_EXTERNAL (olddecl))
1341 error ("%Jdeclaration of '%D' with no linkage follows "
1342 "extern declaration", newdecl, newdecl);
1344 error ("%Jredeclaration of '%D' with no linkage",
1347 locate_old_decl (olddecl, error);
1353 /* All decls must agree on a non-default visibility. */
1354 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1355 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1356 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1358 warning ("%Jredeclaration of '%D' with different visibility "
1359 "(old visibility preserved)", newdecl, newdecl);
1363 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1365 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1366 if (DECL_DECLARED_INLINE_P (newdecl)
1367 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1369 warning ("%Jinline declaration of '%D' follows "
1370 "declaration with attribute noinline", newdecl, newdecl);
1373 else if (DECL_DECLARED_INLINE_P (olddecl)
1374 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1376 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1377 "inline declaration ", newdecl, newdecl);
1381 /* Inline declaration after use or definition.
1382 ??? Should we still warn about this now we have unit-at-a-time
1383 mode and can get it right? */
1384 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1386 if (TREE_USED (olddecl))
1388 warning ("%J'%D' declared inline after being called",
1392 else if (DECL_INITIAL (olddecl))
1394 warning ("%J'%D' declared inline after its definition",
1400 else /* PARM_DECL, VAR_DECL */
1402 /* Redeclaration of a parameter is a constraint violation (this is
1403 not explicitly stated, but follows from C99 6.7p3 [no more than
1404 one declaration of the same identifier with no linkage in the
1405 same scope, except type tags] and 6.2.2p6 [parameters have no
1406 linkage]). We must check for a forward parameter declaration,
1407 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1408 an extension, the mandatory diagnostic for which is handled by
1409 mark_forward_parm_decls. */
1411 if (TREE_CODE (newdecl) == PARM_DECL
1412 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1414 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1415 locate_old_decl (olddecl, error);
1420 /* Optional warning for completely redundant decls. */
1421 if (!warned && !pedwarned
1422 && warn_redundant_decls
1423 /* Don't warn about a function declaration followed by a
1425 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1426 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1427 /* Don't warn about an extern followed by a definition. */
1428 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1429 /* Don't warn about forward parameter decls. */
1430 && !(TREE_CODE (newdecl) == PARM_DECL
1431 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1433 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1437 /* Report location of previous decl/defn in a consistent manner. */
1438 if (warned || pedwarned)
1439 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1444 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1445 consistent with OLDDECL, but carries new information. Merge the
1446 new information into OLDDECL. This function issues no
1450 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1452 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1453 && DECL_INITIAL (newdecl) != 0);
1455 /* For real parm decl following a forward decl, rechain the old decl
1456 in its new location and clear TREE_ASM_WRITTEN (it's not a
1457 forward decl anymore). */
1458 if (TREE_CODE (newdecl) == PARM_DECL
1459 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1461 struct c_binding *b, **here;
1463 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1464 if ((*here)->decl == olddecl)
1471 b->prev = current_scope->bindings;
1472 current_scope->bindings = b;
1474 TREE_ASM_WRITTEN (olddecl) = 0;
1477 DECL_ATTRIBUTES (newdecl)
1478 = targetm.merge_decl_attributes (olddecl, newdecl);
1480 /* Merge the data types specified in the two decls. */
1482 = TREE_TYPE (olddecl)
1483 = composite_type (newtype, oldtype);
1485 /* Lay the type out, unless already done. */
1486 if (oldtype != TREE_TYPE (newdecl))
1488 if (TREE_TYPE (newdecl) != error_mark_node)
1489 layout_type (TREE_TYPE (newdecl));
1490 if (TREE_CODE (newdecl) != FUNCTION_DECL
1491 && TREE_CODE (newdecl) != TYPE_DECL
1492 && TREE_CODE (newdecl) != CONST_DECL)
1493 layout_decl (newdecl, 0);
1497 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1498 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1499 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1500 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1501 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1502 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1504 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1505 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1509 /* Keep the old rtl since we can safely use it. */
1510 COPY_DECL_RTL (olddecl, newdecl);
1512 /* Merge the type qualifiers. */
1513 if (TREE_READONLY (newdecl))
1514 TREE_READONLY (olddecl) = 1;
1516 if (TREE_THIS_VOLATILE (newdecl))
1518 TREE_THIS_VOLATILE (olddecl) = 1;
1519 if (TREE_CODE (newdecl) == VAR_DECL)
1520 make_var_volatile (newdecl);
1523 /* Keep source location of definition rather than declaration. */
1524 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1525 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1527 /* Merge the unused-warning information. */
1528 if (DECL_IN_SYSTEM_HEADER (olddecl))
1529 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1530 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1531 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1533 /* Merge the initialization information. */
1534 if (DECL_INITIAL (newdecl) == 0)
1535 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1537 /* Merge the section attribute.
1538 We want to issue an error if the sections conflict but that must be
1539 done later in decl_attributes since we are called before attributes
1541 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1542 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1544 /* Copy the assembler name.
1545 Currently, it can only be defined in the prototype. */
1546 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1548 /* If either declaration has a nondefault visibility, use it. */
1549 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1550 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1552 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1554 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1555 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1556 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1557 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1558 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1559 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1560 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1561 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1562 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1565 /* Merge the storage class information. */
1566 merge_weak (newdecl, olddecl);
1568 /* For functions, static overrides non-static. */
1569 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1571 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1572 /* This is since we don't automatically
1573 copy the attributes of NEWDECL into OLDDECL. */
1574 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1575 /* If this clears `static', clear it in the identifier too. */
1576 if (! TREE_PUBLIC (olddecl))
1577 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1579 if (DECL_EXTERNAL (newdecl))
1581 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1582 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1584 /* An extern decl does not override previous storage class. */
1585 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1586 if (! DECL_EXTERNAL (newdecl))
1588 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1589 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1594 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1595 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1598 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1600 /* If we're redefining a function previously defined as extern
1601 inline, make sure we emit debug info for the inline before we
1602 throw it away, in case it was inlined into a function that hasn't
1603 been written out yet. */
1604 if (new_is_definition && DECL_INITIAL (olddecl))
1606 if (TREE_USED (olddecl)
1607 /* In unit-at-a-time mode we never inline re-defined extern
1608 inline functions. */
1609 && !flag_unit_at_a_time
1610 && cgraph_function_possibly_inlined_p (olddecl))
1611 (*debug_hooks->outlining_inline_function) (olddecl);
1613 /* The new defn must not be inline. */
1614 DECL_INLINE (newdecl) = 0;
1615 DECL_UNINLINABLE (newdecl) = 1;
1619 /* If either decl says `inline', this fn is inline,
1620 unless its definition was passed already. */
1621 if (DECL_DECLARED_INLINE_P (newdecl)
1622 || DECL_DECLARED_INLINE_P (olddecl))
1623 DECL_DECLARED_INLINE_P (newdecl) = 1;
1625 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1626 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1629 if (DECL_BUILT_IN (olddecl))
1631 /* If redeclaring a builtin function, it stays built in.
1632 But it gets tagged as having been declared. */
1633 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1634 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1635 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1638 /* Also preserve various other info from the definition. */
1639 if (! new_is_definition)
1641 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1642 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1643 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1644 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1645 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1647 /* Set DECL_INLINE on the declaration if we've got a body
1648 from which to instantiate. */
1649 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1651 DECL_INLINE (newdecl) = 1;
1652 DECL_ABSTRACT_ORIGIN (newdecl)
1653 = DECL_ABSTRACT_ORIGIN (olddecl);
1658 /* If a previous declaration said inline, mark the
1659 definition as inlinable. */
1660 if (DECL_DECLARED_INLINE_P (newdecl)
1661 && ! DECL_UNINLINABLE (newdecl))
1662 DECL_INLINE (newdecl) = 1;
1666 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1667 But preserve OLDDECL's DECL_UID. */
1669 unsigned olddecl_uid = DECL_UID (olddecl);
1671 memcpy ((char *) olddecl + sizeof (struct tree_common),
1672 (char *) newdecl + sizeof (struct tree_common),
1673 sizeof (struct tree_decl) - sizeof (struct tree_common));
1674 DECL_UID (olddecl) = olddecl_uid;
1677 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1678 so that encode_section_info has a chance to look at the new decl
1679 flags and attributes. */
1680 if (DECL_RTL_SET_P (olddecl)
1681 && (TREE_CODE (olddecl) == FUNCTION_DECL
1682 || (TREE_CODE (olddecl) == VAR_DECL
1683 && TREE_STATIC (olddecl))))
1684 make_decl_rtl (olddecl, NULL);
1687 /* Handle when a new declaration NEWDECL has the same name as an old
1688 one OLDDECL in the same binding contour. Prints an error message
1691 If safely possible, alter OLDDECL to look like NEWDECL, and return
1692 true. Otherwise, return false. */
1695 duplicate_decls (tree newdecl, tree olddecl)
1697 tree newtype = NULL, oldtype = NULL;
1699 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1702 merge_decls (newdecl, olddecl, newtype, oldtype);
1707 /* Check whether decl-node NEW shadows an existing declaration. */
1709 warn_if_shadowing (tree new)
1711 struct c_binding *b;
1713 /* Shadow warnings wanted? */
1715 /* No shadow warnings for internally generated vars. */
1716 || DECL_IS_BUILTIN (new)
1717 /* No shadow warnings for vars made for inlining. */
1718 || DECL_FROM_INLINE (new)
1719 /* Don't warn about the parm names in function declarator
1720 within a function declarator. It would be nice to avoid
1721 warning in any function declarator in a declaration, as
1722 opposed to a definition, but there is no way to tell
1723 it's not a definition at this point. */
1724 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1727 /* Is anything being shadowed? Invisible decls do not count. */
1728 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1729 if (b->decl && b->decl != new && !b->invisible)
1733 if (TREE_CODE (old) == PARM_DECL)
1734 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1735 else if (DECL_FILE_SCOPE_P (old))
1736 warning ("%Jdeclaration of '%D' shadows a global declaration",
1738 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1739 warning ("%Jdeclaration of '%D' shadows a built-in function",
1742 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1744 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1745 warning ("%Jshadowed declaration is here", old);
1752 /* Subroutine of pushdecl.
1754 X is a TYPE_DECL for a typedef statement. Create a brand new
1755 ..._TYPE node (which will be just a variant of the existing
1756 ..._TYPE node with identical properties) and then install X
1757 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1759 The whole point here is to end up with a situation where each
1760 and every ..._TYPE node the compiler creates will be uniquely
1761 associated with AT MOST one node representing a typedef name.
1762 This way, even though the compiler substitutes corresponding
1763 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1764 early on, later parts of the compiler can always do the reverse
1765 translation and get back the corresponding typedef name. For
1768 typedef struct S MY_TYPE;
1771 Later parts of the compiler might only know that `object' was of
1772 type `struct S' if it were not for code just below. With this
1773 code however, later parts of the compiler see something like:
1775 struct S' == struct S
1776 typedef struct S' MY_TYPE;
1779 And they can then deduce (from the node for type struct S') that
1780 the original object declaration was:
1784 Being able to do this is important for proper support of protoize,
1785 and also for generating precise symbolic debugging information
1786 which takes full account of the programmer's (typedef) vocabulary.
1788 Obviously, we don't want to generate a duplicate ..._TYPE node if
1789 the TYPE_DECL node that we are now processing really represents a
1790 standard built-in type.
1792 Since all standard types are effectively declared at line zero
1793 in the source file, we can easily check to see if we are working
1794 on a standard type by checking the current value of lineno. */
1797 clone_underlying_type (tree x)
1799 if (DECL_IS_BUILTIN (x))
1801 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1802 TYPE_NAME (TREE_TYPE (x)) = x;
1804 else if (TREE_TYPE (x) != error_mark_node
1805 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1807 tree tt = TREE_TYPE (x);
1808 DECL_ORIGINAL_TYPE (x) = tt;
1809 tt = build_type_copy (tt);
1811 TREE_USED (tt) = TREE_USED (x);
1816 /* Record a decl-node X as belonging to the current lexical scope.
1817 Check for errors (such as an incompatible declaration for the same
1818 name already seen in the same scope).
1820 Returns either X or an old decl for the same name.
1821 If an old decl is returned, it may have been smashed
1822 to agree with what X says. */
1827 tree name = DECL_NAME (x);
1828 struct c_scope *scope = current_scope;
1829 struct c_binding *b;
1830 bool nested = false;
1832 /* Functions need the lang_decl data. */
1833 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1834 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1836 /* Must set DECL_CONTEXT for everything not at file scope or
1837 DECL_FILE_SCOPE_P won't work. Local externs don't count
1838 unless they have initializers (which generate code). */
1839 if (current_function_decl
1840 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1841 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1842 DECL_CONTEXT (x) = current_function_decl;
1844 /* Anonymous decls are just inserted in the scope. */
1847 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1851 /* First, see if there is another declaration with the same name in
1852 the current scope. If there is, duplicate_decls may do all the
1853 work for us. If duplicate_decls returns false, that indicates
1854 two incompatible decls in the same scope; we are to silently
1855 replace the old one (duplicate_decls has issued all appropriate
1856 diagnostics). In particular, we should not consider possible
1857 duplicates in the external scope, or shadowing. */
1858 b = I_SYMBOL_BINDING (name);
1859 if (b && B_IN_SCOPE (b, scope))
1861 if (duplicate_decls (x, b->decl))
1864 goto skip_external_and_shadow_checks;
1867 /* All declarations with external linkage, and all external
1868 references, go in the external scope, no matter what scope is
1869 current. However, the binding in that scope is ignored for
1870 purposes of normal name lookup. A separate binding structure is
1871 created in the requested scope; this governs the normal
1872 visibility of the symbol.
1874 The binding in the externals scope is used exclusively for
1875 detecting duplicate declarations of the same object, no matter
1876 what scope they are in; this is what we do here. (C99 6.2.7p2:
1877 All declarations that refer to the same object or function shall
1878 have compatible type; otherwise, the behavior is undefined.) */
1879 if (DECL_EXTERNAL (x) || scope == file_scope)
1881 if (warn_nested_externs
1882 && scope != file_scope
1883 && !DECL_IN_SYSTEM_HEADER (x))
1884 warning ("nested extern declaration of '%D'", x);
1886 while (b && !B_IN_EXTERNAL_SCOPE (b))
1889 /* The point of the same_translation_unit_p check here is,
1890 we want to detect a duplicate decl for a construct like
1891 foo() { extern bar(); } ... static bar(); but not if
1892 they are in different translation units. In any case,
1893 the static does not go in the externals scope. */
1895 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1896 || same_translation_unit_p (x, b->decl))
1897 && duplicate_decls (x, b->decl))
1899 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1902 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1904 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1908 /* Similarly, a declaration of a function with static linkage at
1909 block scope must be checked against any existing declaration
1910 of that function at file scope. */
1911 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1912 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1914 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1915 warning ("nested static declaration of '%D'", x);
1917 while (b && !B_IN_FILE_SCOPE (b))
1920 if (b && same_translation_unit_p (x, b->decl)
1921 && duplicate_decls (x, b->decl))
1923 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1928 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1933 warn_if_shadowing (x);
1935 skip_external_and_shadow_checks:
1936 if (TREE_CODE (x) == TYPE_DECL)
1937 clone_underlying_type (x);
1939 bind (name, x, scope, /*invisible=*/false, nested);
1941 /* If x's type is incomplete because it's based on a
1942 structure or union which has not yet been fully declared,
1943 attach it to that structure or union type, so we can go
1944 back and complete the variable declaration later, if the
1945 structure or union gets fully declared.
1947 If the input is erroneous, we can have error_mark in the type
1948 slot (e.g. "f(void a, ...)") - that doesn't count as an
1950 if (TREE_TYPE (x) != error_mark_node
1951 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1953 tree element = TREE_TYPE (x);
1955 while (TREE_CODE (element) == ARRAY_TYPE)
1956 element = TREE_TYPE (element);
1957 element = TYPE_MAIN_VARIANT (element);
1959 if ((TREE_CODE (element) == RECORD_TYPE
1960 || TREE_CODE (element) == UNION_TYPE)
1961 && (TREE_CODE (x) != TYPE_DECL
1962 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1963 && !COMPLETE_TYPE_P (element))
1964 C_TYPE_INCOMPLETE_VARS (element)
1965 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1970 /* Record X as belonging to file scope.
1971 This is used only internally by the Objective-C front end,
1972 and is limited to its needs. duplicate_decls is not called;
1973 if there is any preexisting decl for this identifier, it is an ICE. */
1976 pushdecl_top_level (tree x)
1979 bool nested = false;
1981 if (TREE_CODE (x) != VAR_DECL)
1984 name = DECL_NAME (x);
1986 if (I_SYMBOL_BINDING (name))
1989 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1991 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1995 bind (name, x, file_scope, /*invisible=*/false, nested);
2001 implicit_decl_warning (tree id, tree olddecl)
2003 void (*diag) (const char *, ...);
2004 switch (mesg_implicit_function_declaration)
2007 case 1: diag = warning; break;
2008 case 2: diag = error; break;
2012 diag (N_("implicit declaration of function '%E'"), id);
2014 locate_old_decl (olddecl, diag);
2017 /* Generate an implicit declaration for identifier FUNCTIONID as a
2018 function of type int (). */
2021 implicitly_declare (tree functionid)
2023 tree decl = lookup_name_in_scope (functionid, external_scope);
2027 /* FIXME: Objective-C has weird not-really-builtin functions
2028 which are supposed to be visible automatically. They wind up
2029 in the external scope because they're pushed before the file
2030 scope gets created. Catch this here and rebind them into the
2032 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2034 bind (functionid, decl, file_scope,
2035 /*invisible=*/false, /*nested=*/true);
2040 /* Implicit declaration of a function already declared
2041 (somehow) in a different scope, or as a built-in.
2042 If this is the first time this has happened, warn;
2043 then recycle the old declaration. */
2044 if (!C_DECL_IMPLICIT (decl))
2046 implicit_decl_warning (functionid, decl);
2047 C_DECL_IMPLICIT (decl) = 1;
2049 bind (functionid, decl, current_scope,
2050 /*invisible=*/false, /*nested=*/true);
2055 /* Not seen before. */
2056 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2057 DECL_EXTERNAL (decl) = 1;
2058 TREE_PUBLIC (decl) = 1;
2059 C_DECL_IMPLICIT (decl) = 1;
2060 implicit_decl_warning (functionid, 0);
2062 /* C89 says implicit declarations are in the innermost block.
2063 So we record the decl in the standard fashion. */
2064 decl = pushdecl (decl);
2066 /* No need to call objc_check_decl here - it's a function type. */
2067 rest_of_decl_compilation (decl, NULL, 0, 0);
2069 /* Write a record describing this implicit function declaration
2070 to the prototypes file (if requested). */
2071 gen_aux_info_record (decl, 0, 1, 0);
2073 /* Possibly apply some default attributes to this implicit declaration. */
2074 decl_attributes (&decl, NULL_TREE, 0);
2079 /* Issue an error message for a reference to an undeclared variable
2080 ID, including a reference to a builtin outside of function-call
2081 context. Establish a binding of the identifier to error_mark_node
2082 in an appropriate scope, which will suppress further errors for the
2085 undeclared_variable (tree id)
2087 static bool already = false;
2088 struct c_scope *scope;
2090 if (current_function_decl == 0)
2092 error ("'%E' undeclared here (not in a function)", id);
2093 scope = current_scope;
2097 error ("'%E' undeclared (first use in this function)", id);
2101 error ("(Each undeclared identifier is reported only once");
2102 error ("for each function it appears in.)");
2106 /* If we are parsing old-style parameter decls, current_function_decl
2107 will be nonnull but current_function_scope will be null. */
2108 scope = current_function_scope ? current_function_scope : current_scope;
2110 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2113 /* Subroutine of lookup_label, declare_label, define_label: construct a
2114 LABEL_DECL with all the proper frills. */
2117 make_label (tree name, location_t location)
2119 tree label = build_decl (LABEL_DECL, name, void_type_node);
2121 DECL_CONTEXT (label) = current_function_decl;
2122 DECL_MODE (label) = VOIDmode;
2123 DECL_SOURCE_LOCATION (label) = location;
2128 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2129 Create one if none exists so far for the current function.
2130 This is called when a label is used in a goto expression or
2131 has its address taken. */
2134 lookup_label (tree name)
2138 if (current_function_decl == 0)
2140 error ("label %s referenced outside of any function",
2141 IDENTIFIER_POINTER (name));
2145 /* Use a label already defined or ref'd with this name, but not if
2146 it is inherited from a containing function and wasn't declared
2148 label = I_LABEL_DECL (name);
2149 if (label && (DECL_CONTEXT (label) == current_function_decl
2150 || C_DECLARED_LABEL_FLAG (label)))
2152 /* If the label has only been declared, update its apparent
2153 location to point here, for better diagnostics if it
2154 turns out not to have been defined. */
2155 if (!TREE_USED (label))
2156 DECL_SOURCE_LOCATION (label) = input_location;
2160 /* No label binding for that identifier; make one. */
2161 label = make_label (name, input_location);
2163 /* Ordinary labels go in the current function scope. */
2164 bind (name, label, current_function_scope,
2165 /*invisible=*/false, /*nested=*/false);
2169 /* Make a label named NAME in the current function, shadowing silently
2170 any that may be inherited from containing functions or containing
2171 scopes. This is called for __label__ declarations. */
2174 declare_label (tree name)
2176 struct c_binding *b = I_LABEL_BINDING (name);
2179 /* Check to make sure that the label hasn't already been declared
2181 if (b && B_IN_CURRENT_SCOPE (b))
2183 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2184 locate_old_decl (b->decl, error);
2186 /* Just use the previous declaration. */
2190 label = make_label (name, input_location);
2191 C_DECLARED_LABEL_FLAG (label) = 1;
2193 /* Declared labels go in the current scope. */
2194 bind (name, label, current_scope,
2195 /*invisible=*/false, /*nested=*/false);
2199 /* Define a label, specifying the location in the source file.
2200 Return the LABEL_DECL node for the label, if the definition is valid.
2201 Otherwise return 0. */
2204 define_label (location_t location, tree name)
2206 /* Find any preexisting label with this name. It is an error
2207 if that label has already been defined in this function, or
2208 if there is a containing function with a declared label with
2210 tree label = I_LABEL_DECL (name);
2213 && ((DECL_CONTEXT (label) == current_function_decl
2214 && DECL_INITIAL (label) != 0)
2215 || (DECL_CONTEXT (label) != current_function_decl
2216 && C_DECLARED_LABEL_FLAG (label))))
2218 error ("%Hduplicate label `%D'", &location, label);
2219 locate_old_decl (label, error);
2222 else if (label && DECL_CONTEXT (label) == current_function_decl)
2224 /* The label has been used or declared already in this function,
2225 but not defined. Update its location to point to this
2227 DECL_SOURCE_LOCATION (label) = location;
2231 /* No label binding for that identifier; make one. */
2232 label = make_label (name, location);
2234 /* Ordinary labels go in the current function scope. */
2235 bind (name, label, current_function_scope,
2236 /*invisible=*/false, /*nested=*/false);
2239 if (warn_traditional && !in_system_header && lookup_name (name))
2240 warning ("%Htraditional C lacks a separate namespace for labels, "
2241 "identifier `%s' conflicts", &location,
2242 IDENTIFIER_POINTER (name));
2244 /* Mark label as having been defined. */
2245 DECL_INITIAL (label) = error_mark_node;
2249 /* Given NAME, an IDENTIFIER_NODE,
2250 return the structure (or union or enum) definition for that name.
2251 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2252 CODE says which kind of type the caller wants;
2253 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2254 If the wrong kind of type is found, an error is reported. */
2257 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2259 struct c_binding *b = I_TAG_BINDING (name);
2265 /* We only care about whether it's in this level if
2266 thislevel_only was set or it might be a type clash. */
2267 if (thislevel_only || TREE_CODE (b->decl) != code)
2269 /* For our purposes, a tag in the external scope is the same as
2270 a tag in the file scope. (Primarily relevant to Objective-C
2271 and its builtin structure tags, which get pushed before the
2272 file scope is created.) */
2273 if (B_IN_CURRENT_SCOPE (b)
2274 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2278 if (thislevel_only && !thislevel)
2281 if (TREE_CODE (b->decl) != code)
2283 /* Definition isn't the kind we were looking for. */
2284 pending_invalid_xref = name;
2285 pending_invalid_xref_location = input_location;
2287 /* If in the same binding level as a declaration as a tag
2288 of a different type, this must not be allowed to
2289 shadow that tag, so give the error immediately.
2290 (For example, "struct foo; union foo;" is invalid.) */
2292 pending_xref_error ();
2297 /* Print an error message now
2298 for a recent invalid struct, union or enum cross reference.
2299 We don't print them immediately because they are not invalid
2300 when used in the `struct foo;' construct for shadowing. */
2303 pending_xref_error (void)
2305 if (pending_invalid_xref != 0)
2306 error ("%H`%s' defined as wrong kind of tag",
2307 &pending_invalid_xref_location,
2308 IDENTIFIER_POINTER (pending_invalid_xref));
2309 pending_invalid_xref = 0;
2313 /* Look up NAME in the current scope and its superiors
2314 in the namespace of variables, functions and typedefs.
2315 Return a ..._DECL node of some kind representing its definition,
2316 or return 0 if it is undefined. */
2319 lookup_name (tree name)
2321 struct c_binding *b = I_SYMBOL_BINDING (name);
2322 if (b && !b->invisible)
2327 /* Similar to `lookup_name' but look only at the indicated scope. */
2330 lookup_name_in_scope (tree name, struct c_scope *scope)
2332 struct c_binding *b;
2334 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2335 if (B_IN_SCOPE (b, scope))
2340 /* Create the predefined scalar types of C,
2341 and some nodes representing standard constants (0, 1, (void *) 0).
2342 Initialize the global scope.
2343 Make definitions for built-in primitive functions. */
2346 c_init_decl_processing (void)
2349 tree ptr_ftype_void, ptr_ftype_ptr;
2350 location_t save_loc = input_location;
2352 /* Adds some ggc roots, and reserved words for c-parse.in. */
2355 current_function_decl = 0;
2357 /* Make the externals scope. */
2359 external_scope = current_scope;
2361 /* Declarations from c_common_nodes_and_builtins must not be associated
2362 with this input file, lest we get differences between using and not
2363 using preprocessed headers. */
2364 #ifdef USE_MAPPED_LOCATION
2365 input_location = BUILTINS_LOCATION;
2367 input_location.file = "<built-in>";
2368 input_location.line = 0;
2371 build_common_tree_nodes (flag_signed_char);
2373 c_common_nodes_and_builtins ();
2375 /* In C, comparisons and TRUTH_* expressions have type int. */
2376 truthvalue_type_node = integer_type_node;
2377 truthvalue_true_node = integer_one_node;
2378 truthvalue_false_node = integer_zero_node;
2380 /* Even in C99, which has a real boolean type. */
2381 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2382 boolean_type_node));
2384 endlink = void_list_node;
2385 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2387 = build_function_type (ptr_type_node,
2388 tree_cons (NULL_TREE, ptr_type_node, endlink));
2390 input_location = save_loc;
2392 pedantic_lvalues = true;
2394 make_fname_decl = c_make_fname_decl;
2395 start_fname_decls ();
2398 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2399 decl, NAME is the initialization string and TYPE_DEP indicates whether
2400 NAME depended on the type of the function. As we don't yet implement
2401 delayed emission of static data, we mark the decl as emitted
2402 so it is not placed in the output. Anything using it must therefore pull
2403 out the STRING_CST initializer directly. FIXME. */
2406 c_make_fname_decl (tree id, int type_dep)
2408 const char *name = fname_as_string (type_dep);
2409 tree decl, type, init;
2410 size_t length = strlen (name);
2412 type = build_array_type
2413 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2414 build_index_type (size_int (length)));
2416 decl = build_decl (VAR_DECL, id, type);
2418 TREE_STATIC (decl) = 1;
2419 TREE_READONLY (decl) = 1;
2420 DECL_ARTIFICIAL (decl) = 1;
2422 init = build_string (length + 1, name);
2423 free ((char *) name);
2424 TREE_TYPE (init) = type;
2425 DECL_INITIAL (decl) = init;
2427 TREE_USED (decl) = 1;
2429 if (current_function_decl)
2431 DECL_CONTEXT (decl) = current_function_decl;
2432 bind (id, decl, current_function_scope,
2433 /*invisible=*/false, /*nested=*/false);
2436 finish_decl (decl, init, NULL_TREE);
2441 /* Return a definition for a builtin function named NAME and whose data type
2442 is TYPE. TYPE should be a function type with argument types.
2443 FUNCTION_CODE tells later passes how to compile calls to this function.
2444 See tree.h for its possible values.
2446 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2447 the name to be called if we can't opencode the function. If
2448 ATTRS is nonzero, use that for the function's attribute list. */
2451 builtin_function (const char *name, tree type, int function_code,
2452 enum built_in_class class, const char *library_name,
2455 tree id = get_identifier (name);
2456 tree decl = build_decl (FUNCTION_DECL, id, type);
2457 TREE_PUBLIC (decl) = 1;
2458 DECL_EXTERNAL (decl) = 1;
2459 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2460 DECL_BUILT_IN_CLASS (decl) = class;
2461 DECL_FUNCTION_CODE (decl) = function_code;
2463 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2465 /* Should never be called on a symbol with a preexisting meaning. */
2466 if (I_SYMBOL_BINDING (id))
2469 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2471 /* Builtins in the implementation namespace are made visible without
2472 needing to be explicitly declared. See push_file_scope. */
2473 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2475 TREE_CHAIN (decl) = visible_builtins;
2476 visible_builtins = decl;
2479 /* Possibly apply some default attributes to this built-in function. */
2481 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2483 decl_attributes (&decl, NULL_TREE, 0);
2488 /* Called when a declaration is seen that contains no names to declare.
2489 If its type is a reference to a structure, union or enum inherited
2490 from a containing scope, shadow that tag name for the current scope
2491 with a forward reference.
2492 If its type defines a new named structure or union
2493 or defines an enum, it is valid but we need not do anything here.
2494 Otherwise, it is an error. */
2497 shadow_tag (tree declspecs)
2499 shadow_tag_warned (declspecs, 0);
2503 shadow_tag_warned (tree declspecs, int warned)
2506 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2513 pending_invalid_xref = 0;
2515 /* Remove the attributes from declspecs, since they will confuse the
2517 split_specs_attrs (declspecs, &specs, &attrs);
2519 for (link = specs; link; link = TREE_CHAIN (link))
2521 tree value = TREE_VALUE (link);
2522 enum tree_code code = TREE_CODE (value);
2524 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2525 /* Used to test also that TYPE_SIZE (value) != 0.
2526 That caused warning for `struct foo;' at top level in the file. */
2528 tree name = TYPE_NAME (value);
2535 if (warned != 1 && code != ENUMERAL_TYPE)
2536 /* Empty unnamed enum OK */
2538 pedwarn ("unnamed struct/union that defines no instances");
2544 t = lookup_tag (code, name, 1);
2548 t = make_node (code);
2555 if (!warned && ! in_system_header)
2557 warning ("useless keyword or type name in empty declaration");
2564 error ("two types specified in one empty declaration");
2569 pedwarn ("empty declaration");
2573 /* Construct an array declarator. EXPR is the expression inside [], or
2574 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2575 to the pointer to which a parameter array is converted). STATIC_P is
2576 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2577 is nonzero is the array is [*], a VLA of unspecified length which is
2578 nevertheless a complete type (not currently implemented by GCC),
2579 zero otherwise. The declarator is constructed as an ARRAY_REF
2580 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2581 left of the [] (filled by in set_array_declarator_type) and operand 1
2582 is the expression inside; whose TREE_TYPE is the type qualifiers and
2583 which has TREE_STATIC set if "static" is used. */
2586 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2589 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2590 TREE_TYPE (decl) = quals;
2591 TREE_STATIC (decl) = (static_p ? 1 : 0);
2592 if (pedantic && !flag_isoc99)
2594 if (static_p || quals != NULL_TREE)
2595 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2597 pedwarn ("ISO C90 does not support `[*]' array declarators");
2600 warning ("GCC does not yet properly implement `[*]' array declarators");
2604 /* Set the type of an array declarator. DECL is the declarator, as
2605 constructed by build_array_declarator; TYPE is what appears on the left
2606 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2607 abstract declarator, zero otherwise; this is used to reject static and
2608 type qualifiers in abstract declarators, where they are not in the
2612 set_array_declarator_type (tree decl, tree type, int abstract_p)
2614 TREE_OPERAND (decl, 0) = type;
2615 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2616 error ("static or type qualifiers in abstract declarator");
2620 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2623 groktypename (tree typename)
2627 if (TREE_CODE (typename) != TREE_LIST)
2630 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2632 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2635 /* Apply attributes. */
2636 decl_attributes (&typename, attrs, 0);
2641 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2644 groktypename_in_parm_context (tree typename)
2646 if (TREE_CODE (typename) != TREE_LIST)
2648 return grokdeclarator (TREE_VALUE (typename),
2649 TREE_PURPOSE (typename),
2653 /* Decode a declarator in an ordinary declaration or data definition.
2654 This is called as soon as the type information and variable name
2655 have been parsed, before parsing the initializer if any.
2656 Here we create the ..._DECL node, fill in its type,
2657 and put it on the list of decls for the current context.
2658 The ..._DECL node is returned as the value.
2660 Exception: for arrays where the length is not specified,
2661 the type is left null, to be filled in by `finish_decl'.
2663 Function definitions do not come here; they go to start_function
2664 instead. However, external and forward declarations of functions
2665 do go through here. Structure field declarations are done by
2666 grokfield and not through here. */
2669 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2674 /* An object declared as __attribute__((deprecated)) suppresses
2675 warnings of uses of other deprecated items. */
2676 if (lookup_attribute ("deprecated", attributes))
2677 deprecated_state = DEPRECATED_SUPPRESS;
2679 decl = grokdeclarator (declarator, declspecs,
2680 NORMAL, initialized, NULL);
2682 deprecated_state = DEPRECATED_NORMAL;
2684 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2685 && MAIN_NAME_P (DECL_NAME (decl)))
2686 warning ("%J'%D' is usually a function", decl, decl);
2689 /* Is it valid for this decl to have an initializer at all?
2690 If not, set INITIALIZED to zero, which will indirectly
2691 tell 'finish_decl' to ignore the initializer once it is parsed. */
2692 switch (TREE_CODE (decl))
2695 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2700 error ("function '%D' is initialized like a variable", decl);
2705 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2706 error ("parameter '%D' is initialized", decl);
2711 /* Don't allow initializations for incomplete types except for
2712 arrays which might be completed by the initialization. */
2714 /* This can happen if the array size is an undefined macro.
2715 We already gave a warning, so we don't need another one. */
2716 if (TREE_TYPE (decl) == error_mark_node)
2718 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2720 /* A complete type is ok if size is fixed. */
2722 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2723 || C_DECL_VARIABLE_SIZE (decl))
2725 error ("variable-sized object may not be initialized");
2729 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2731 error ("variable '%D' has initializer but incomplete type", decl);
2734 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2736 error ("elements of array '%D' have incomplete type", decl);
2743 DECL_EXTERNAL (decl) = 0;
2744 if (current_scope == file_scope)
2745 TREE_STATIC (decl) = 1;
2747 /* Tell 'pushdecl' this is an initialized decl
2748 even though we don't yet have the initializer expression.
2749 Also tell 'finish_decl' it may store the real initializer. */
2750 DECL_INITIAL (decl) = error_mark_node;
2753 /* If this is a function declaration, write a record describing it to the
2754 prototypes file (if requested). */
2756 if (TREE_CODE (decl) == FUNCTION_DECL)
2757 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2759 /* ANSI specifies that a tentative definition which is not merged with
2760 a non-tentative definition behaves exactly like a definition with an
2761 initializer equal to zero. (Section 3.7.2)
2763 -fno-common gives strict ANSI behavior, though this tends to break
2764 a large body of code that grew up without this rule.
2766 Thread-local variables are never common, since there's no entrenched
2767 body of code to break, and it allows more efficient variable references
2768 in the presence of dynamic linking. */
2770 if (TREE_CODE (decl) == VAR_DECL
2772 && TREE_PUBLIC (decl)
2773 && !DECL_THREAD_LOCAL (decl)
2775 DECL_COMMON (decl) = 1;
2777 /* Set attributes here so if duplicate decl, will have proper attributes. */
2778 decl_attributes (&decl, attributes, 0);
2780 if (TREE_CODE (decl) == FUNCTION_DECL
2781 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2783 tree ce = declarator;
2785 if (TREE_CODE (ce) == INDIRECT_REF)
2786 ce = TREE_OPERAND (declarator, 0);
2787 if (TREE_CODE (ce) == CALL_EXPR)
2789 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2790 for (; args; args = TREE_CHAIN (args))
2792 tree type = TREE_TYPE (args);
2793 if (type && INTEGRAL_TYPE_P (type)
2794 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2795 DECL_ARG_TYPE (args) = integer_type_node;
2800 if (TREE_CODE (decl) == FUNCTION_DECL
2801 && DECL_DECLARED_INLINE_P (decl)
2802 && DECL_UNINLINABLE (decl)
2803 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2804 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2806 /* Add this decl to the current scope.
2807 TEM may equal DECL or it may be a previous decl of the same name. */
2808 tem = pushdecl (decl);
2813 /* Finish processing of a declaration;
2814 install its initial value.
2815 If the length of an array type is not known before,
2816 it must be determined now, from the initial value, or it is an error. */
2819 finish_decl (tree decl, tree init, tree asmspec_tree)
2821 tree type = TREE_TYPE (decl);
2822 int was_incomplete = (DECL_SIZE (decl) == 0);
2823 const char *asmspec = 0;
2825 /* If a name was specified, get the string. */
2826 if (current_scope == file_scope)
2827 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2829 asmspec = TREE_STRING_POINTER (asmspec_tree);
2831 /* If `start_decl' didn't like having an initialization, ignore it now. */
2832 if (init != 0 && DECL_INITIAL (decl) == 0)
2835 /* Don't crash if parm is initialized. */
2836 if (TREE_CODE (decl) == PARM_DECL)
2840 store_init_value (decl, init);
2842 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2843 || TREE_CODE (decl) == FUNCTION_DECL
2844 || TREE_CODE (decl) == FIELD_DECL))
2845 objc_check_decl (decl);
2847 /* Deduce size of array from initialization, if not already known. */
2848 if (TREE_CODE (type) == ARRAY_TYPE
2849 && TYPE_DOMAIN (type) == 0
2850 && TREE_CODE (decl) != TYPE_DECL)
2853 = (TREE_STATIC (decl)
2854 /* Even if pedantic, an external linkage array
2855 may have incomplete type at first. */
2856 ? pedantic && !TREE_PUBLIC (decl)
2857 : !DECL_EXTERNAL (decl));
2859 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2861 /* Get the completed type made by complete_array_type. */
2862 type = TREE_TYPE (decl);
2865 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2867 else if (failure == 2)
2870 error ("%Jarray size missing in '%D'", decl, decl);
2871 /* If a `static' var's size isn't known,
2872 make it extern as well as static, so it does not get
2874 If it is not `static', then do not mark extern;
2875 finish_incomplete_decl will give it a default size
2876 and it will get allocated. */
2877 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2878 DECL_EXTERNAL (decl) = 1;
2881 /* TYPE_MAX_VALUE is always one less than the number of elements
2882 in the array, because we start counting at zero. Therefore,
2883 warn only if the value is less than zero. */
2884 else if (pedantic && TYPE_DOMAIN (type) != 0
2885 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2886 error ("%Jzero or negative size array '%D'", decl, decl);
2888 layout_decl (decl, 0);
2891 if (TREE_CODE (decl) == VAR_DECL)
2893 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2894 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2895 layout_decl (decl, 0);
2897 if (DECL_SIZE (decl) == 0
2898 /* Don't give an error if we already gave one earlier. */
2899 && TREE_TYPE (decl) != error_mark_node
2900 && (TREE_STATIC (decl)
2902 /* A static variable with an incomplete type
2903 is an error if it is initialized.
2904 Also if it is not file scope.
2905 Otherwise, let it through, but if it is not `extern'
2906 then it may cause an error message later. */
2907 (DECL_INITIAL (decl) != 0
2908 || !DECL_FILE_SCOPE_P (decl))
2910 /* An automatic variable with an incomplete type
2912 !DECL_EXTERNAL (decl)))
2914 error ("%Jstorage size of '%D' isn't known", decl, decl);
2915 TREE_TYPE (decl) = error_mark_node;
2918 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2919 && DECL_SIZE (decl) != 0)
2921 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2922 constant_expression_warning (DECL_SIZE (decl));
2924 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2927 if (TREE_USED (type))
2928 TREE_USED (decl) = 1;
2931 /* If this is a function and an assembler name is specified, reset DECL_RTL
2932 so we can give it its new name. Also, update built_in_decls if it
2933 was a normal built-in. */
2934 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2936 /* ASMSPEC is given, and not the name of a register. Mark the
2937 name with a star so assemble_name won't munge it. */
2938 char *starred = alloca (strlen (asmspec) + 2);
2940 strcpy (starred + 1, asmspec);
2942 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2944 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2945 SET_DECL_RTL (builtin, NULL_RTX);
2946 change_decl_assembler_name (builtin, get_identifier (starred));
2947 #ifdef TARGET_MEM_FUNCTIONS
2948 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2949 init_block_move_fn (starred);
2950 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2951 init_block_clear_fn (starred);
2953 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2954 init_block_move_fn (starred);
2955 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2956 init_block_clear_fn (starred);
2959 SET_DECL_RTL (decl, NULL_RTX);
2960 change_decl_assembler_name (decl, get_identifier (starred));
2963 /* If #pragma weak was used, mark the decl weak now. */
2964 if (current_scope == file_scope)
2965 maybe_apply_pragma_weak (decl);
2967 /* Output the assembler code and/or RTL code for variables and functions,
2968 unless the type is an undefined structure or union.
2969 If not, it will get done when the type is completed. */
2971 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2973 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2974 if (c_dialect_objc ())
2975 objc_check_decl (decl);
2977 if (DECL_FILE_SCOPE_P (decl))
2979 if (DECL_INITIAL (decl) == NULL_TREE
2980 || DECL_INITIAL (decl) == error_mark_node)
2981 /* Don't output anything
2982 when a tentative file-scope definition is seen.
2983 But at end of compilation, do output code for them. */
2984 DECL_DEFER_OUTPUT (decl) = 1;
2985 rest_of_decl_compilation (decl, asmspec, true, 0);
2989 /* This is a local variable. If there is an ASMSPEC, the
2990 user has requested that we handle it specially. */
2993 /* In conjunction with an ASMSPEC, the `register'
2994 keyword indicates that we should place the variable
2995 in a particular register. */
2996 if (C_DECL_REGISTER (decl))
2998 DECL_HARD_REGISTER (decl) = 1;
2999 /* This cannot be done for a structure with volatile
3000 fields, on which DECL_REGISTER will have been
3002 if (!DECL_REGISTER (decl))
3003 error ("cannot put object with volatile field into register");
3006 /* If this is not a static variable, issue a warning.
3007 It doesn't make any sense to give an ASMSPEC for an
3008 ordinary, non-register local variable. Historically,
3009 GCC has accepted -- but ignored -- the ASMSPEC in
3011 if (TREE_CODE (decl) == VAR_DECL
3012 && !C_DECL_REGISTER (decl)
3013 && !TREE_STATIC (decl))
3014 warning ("%Jignoring asm-specifier for non-static local "
3015 "variable '%D'", decl, decl);
3017 change_decl_assembler_name (decl, get_identifier (asmspec));
3020 if (TREE_CODE (decl) != FUNCTION_DECL)
3021 add_stmt (build_stmt (DECL_EXPR, decl));
3024 if (!DECL_FILE_SCOPE_P (decl))
3026 /* Recompute the RTL of a local array now
3027 if it used to be an incomplete type. */
3029 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3031 /* If we used it already as memory, it must stay in memory. */
3032 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3033 /* If it's still incomplete now, no init will save it. */
3034 if (DECL_SIZE (decl) == 0)
3035 DECL_INITIAL (decl) = 0;
3040 /* If this was marked 'used', be sure it will be output. */
3041 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3042 mark_decl_referenced (decl);
3044 if (TREE_CODE (decl) == TYPE_DECL)
3046 if (!DECL_FILE_SCOPE_P (decl)
3047 && variably_modified_type_p (TREE_TYPE (decl)))
3048 add_stmt (build_stmt (DECL_EXPR, decl));
3050 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3053 /* At the end of a declaration, throw away any variable type sizes
3054 of types defined inside that declaration. There is no use
3055 computing them in the following function definition. */
3056 if (current_scope == file_scope)
3057 get_pending_sizes ();
3059 /* Install a cleanup (aka destructor) if one was given. */
3060 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3062 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3065 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3066 tree cleanup_decl = lookup_name (cleanup_id);
3069 /* Build "cleanup(&decl)" for the destructor. */
3070 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3071 cleanup = build_tree_list (NULL_TREE, cleanup);
3072 cleanup = build_function_call (cleanup_decl, cleanup);
3074 /* Don't warn about decl unused; the cleanup uses it. */
3075 TREE_USED (decl) = 1;
3076 TREE_USED (cleanup_decl) = 1;
3078 /* Initialize EH, if we've been told to do so. */
3079 if (flag_exceptions && !c_eh_initialized_p)
3081 c_eh_initialized_p = true;
3082 eh_personality_libfunc
3083 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3084 ? "__gcc_personality_sj0"
3085 : "__gcc_personality_v0");
3086 using_eh_for_cleanups ();
3089 push_cleanup (decl, cleanup, false);
3094 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3095 and push that on the current scope. */
3098 push_parm_decl (tree parm)
3102 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3103 TREE_PURPOSE (TREE_PURPOSE (parm)),
3105 decl_attributes (&decl, TREE_VALUE (parm), 0);
3107 decl = pushdecl (decl);
3109 finish_decl (decl, NULL_TREE, NULL_TREE);
3112 /* Mark all the parameter declarations to date as forward decls.
3113 Also diagnose use of this extension. */
3116 mark_forward_parm_decls (void)
3118 struct c_binding *b;
3120 if (pedantic && !current_scope->warned_forward_parm_decls)
3122 pedwarn ("ISO C forbids forward parameter declarations");
3123 current_scope->warned_forward_parm_decls = true;
3126 for (b = current_scope->bindings; b; b = b->prev)
3127 if (TREE_CODE (b->decl) == PARM_DECL)
3128 TREE_ASM_WRITTEN (b->decl) = 1;
3131 static GTY(()) int compound_literal_number;
3133 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3134 literal, which may be an incomplete array type completed by the
3135 initializer; INIT is a CONSTRUCTOR that initializes the compound
3139 build_compound_literal (tree type, tree init)
3141 /* We do not use start_decl here because we have a type, not a declarator;
3142 and do not use finish_decl because the decl should be stored inside
3143 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3144 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3147 DECL_EXTERNAL (decl) = 0;
3148 TREE_PUBLIC (decl) = 0;
3149 TREE_STATIC (decl) = (current_scope == file_scope);
3150 DECL_CONTEXT (decl) = current_function_decl;
3151 TREE_USED (decl) = 1;
3152 TREE_TYPE (decl) = type;
3153 TREE_READONLY (decl) = TYPE_READONLY (type);
3154 store_init_value (decl, init);
3156 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3158 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3163 type = TREE_TYPE (decl);
3164 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3165 return error_mark_node;
3167 stmt = build_stmt (DECL_EXPR, decl);
3168 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3169 TREE_SIDE_EFFECTS (complit) = 1;
3171 layout_decl (decl, 0);
3173 if (TREE_STATIC (decl))
3175 /* This decl needs a name for the assembler output. We also need
3176 a unique suffix to be added to the name. */
3179 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3180 compound_literal_number);
3181 compound_literal_number++;
3182 DECL_NAME (decl) = get_identifier (name);
3183 DECL_DEFER_OUTPUT (decl) = 1;
3184 DECL_COMDAT (decl) = 1;
3185 DECL_ARTIFICIAL (decl) = 1;
3187 rest_of_decl_compilation (decl, NULL, 1, 0);
3193 /* Make TYPE a complete type based on INITIAL_VALUE.
3194 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3195 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3198 complete_array_type (tree type, tree initial_value, int do_default)
3200 tree maxindex = NULL_TREE;
3205 /* Note MAXINDEX is really the maximum index,
3206 one less than the size. */
3207 if (TREE_CODE (initial_value) == STRING_CST)
3210 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3211 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3214 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3216 tree elts = CONSTRUCTOR_ELTS (initial_value);
3217 maxindex = build_int_2 (-1, -1);
3218 for (; elts; elts = TREE_CHAIN (elts))
3220 if (TREE_PURPOSE (elts))
3221 maxindex = TREE_PURPOSE (elts);
3223 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3224 maxindex, integer_one_node));
3226 maxindex = copy_node (maxindex);
3230 /* Make an error message unless that happened already. */
3231 if (initial_value != error_mark_node)
3234 /* Prevent further error messages. */
3235 maxindex = build_int_2 (0, 0);
3242 maxindex = build_int_2 (0, 0);
3248 TYPE_DOMAIN (type) = build_index_type (maxindex);
3249 if (!TREE_TYPE (maxindex))
3250 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3253 /* Lay out the type now that we can get the real answer. */
3260 /* Determine whether TYPE is a structure with a flexible array member,
3261 or a union containing such a structure (possibly recursively). */
3264 flexible_array_type_p (tree type)
3267 switch (TREE_CODE (type))
3270 x = TYPE_FIELDS (type);
3273 while (TREE_CHAIN (x) != NULL_TREE)
3275 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3276 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3277 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3278 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3282 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3284 if (flexible_array_type_p (TREE_TYPE (x)))
3293 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3294 replacing with appropriate values if they are invalid. */
3296 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3299 unsigned int max_width;
3300 unsigned HOST_WIDE_INT w;
3301 const char *name = orig_name ? orig_name: _("<anonymous>");
3304 STRIP_NOPS (*width);
3306 /* Detect and ignore out of range field width and process valid
3308 if (TREE_CODE (*width) != INTEGER_CST)
3310 error ("bit-field `%s' width not an integer constant", name);
3311 *width = integer_one_node;
3315 constant_expression_warning (*width);
3316 if (tree_int_cst_sgn (*width) < 0)
3318 error ("negative width in bit-field `%s'", name);
3319 *width = integer_one_node;
3321 else if (integer_zerop (*width) && orig_name)
3323 error ("zero width for bit-field `%s'", name);
3324 *width = integer_one_node;
3328 /* Detect invalid bit-field type. */
3329 if (TREE_CODE (*type) != INTEGER_TYPE
3330 && TREE_CODE (*type) != BOOLEAN_TYPE
3331 && TREE_CODE (*type) != ENUMERAL_TYPE)
3333 error ("bit-field `%s' has invalid type", name);
3334 *type = unsigned_type_node;
3337 type_mv = TYPE_MAIN_VARIANT (*type);
3339 && type_mv != integer_type_node
3340 && type_mv != unsigned_type_node
3341 && type_mv != boolean_type_node)
3342 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3344 if (type_mv == boolean_type_node)
3345 max_width = CHAR_TYPE_SIZE;
3347 max_width = TYPE_PRECISION (*type);
3349 if (0 < compare_tree_int (*width, max_width))
3351 error ("width of `%s' exceeds its type", name);
3353 *width = build_int_2 (w, 0);
3356 w = tree_low_cst (*width, 1);
3358 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3360 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3362 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3363 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3364 warning ("`%s' is narrower than values of its type", name);
3368 /* Given declspecs and a declarator,
3369 determine the name and type of the object declared
3370 and construct a ..._DECL node for it.
3371 (In one case we can return a ..._TYPE node instead.
3372 For invalid input we sometimes return 0.)
3374 DECLSPECS is a chain of tree_list nodes whose value fields
3375 are the storage classes and type specifiers.
3377 DECL_CONTEXT says which syntactic context this declaration is in:
3378 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3379 FUNCDEF for a function definition. Like NORMAL but a few different
3380 error messages in each case. Return value may be zero meaning
3381 this definition is too screwy to try to parse.
3382 PARM for a parameter declaration (either within a function prototype
3383 or before a function body). Make a PARM_DECL, or return void_type_node.
3384 TYPENAME if for a typename (in a cast or sizeof).
3385 Don't make a DECL node; just return the ..._TYPE node.
3386 FIELD for a struct or union field; make a FIELD_DECL.
3387 INITIALIZED is 1 if the decl has an initializer.
3388 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3389 representing the width of the bit-field.
3391 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3392 It may also be so in the PARM case, for a prototype where the
3393 argument type is specified but not the name.
3395 This function is where the complicated C meanings of `static'
3396 and `extern' are interpreted. */
3399 grokdeclarator (tree declarator, tree declspecs,
3400 enum decl_context decl_context, int initialized, tree *width)
3404 tree type = NULL_TREE;
3409 int type_quals = TYPE_UNQUALIFIED;
3411 int explicit_int = 0;
3412 int explicit_char = 0;
3413 int defaulted_int = 0;
3414 tree typedef_decl = 0;
3415 const char *name, *orig_name;
3416 tree typedef_type = 0;
3417 int funcdef_flag = 0;
3418 enum tree_code innermost_code = ERROR_MARK;
3419 int size_varies = 0;
3420 tree decl_attr = NULL_TREE;
3421 tree array_ptr_quals = NULL_TREE;
3422 int array_parm_static = 0;
3423 tree returned_attrs = NULL_TREE;
3424 bool bitfield = width != NULL;
3426 tree arg_info = NULL_TREE;
3428 if (decl_context == FUNCDEF)
3429 funcdef_flag = 1, decl_context = NORMAL;
3431 /* Look inside a declarator for the name being declared
3432 and get it as a string, for an error message. */
3434 tree decl = declarator;
3438 switch (TREE_CODE (decl))
3443 innermost_code = TREE_CODE (decl);
3444 decl = TREE_OPERAND (decl, 0);
3448 decl = TREE_VALUE (decl);
3451 case IDENTIFIER_NODE:
3452 name = IDENTIFIER_POINTER (decl);
3464 /* A function definition's declarator must have the form of
3465 a function declarator. */
3467 if (funcdef_flag && innermost_code != CALL_EXPR)
3470 /* If this looks like a function definition, make it one,
3471 even if it occurs where parms are expected.
3472 Then store_parm_decls will reject it and not use it as a parm. */
3473 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3474 decl_context = PARM;
3476 /* Look through the decl specs and record which ones appear.
3477 Some typespecs are defined as built-in typenames.
3478 Others, the ones that are modifiers of other types,
3479 are represented by bits in SPECBITS: set the bits for
3480 the modifiers that appear. Storage class keywords are also in SPECBITS.
3482 If there is a typedef name or a type, store the type in TYPE.
3483 This includes builtin typedefs such as `int'.
3485 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3486 and did not come from a user typedef.
3488 Set LONGLONG if `long' is mentioned twice. */
3490 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3492 tree id = TREE_VALUE (spec);
3494 /* If the entire declaration is itself tagged as deprecated then
3495 suppress reports of deprecated items. */
3496 if (id && TREE_DEPRECATED (id))
3498 if (deprecated_state != DEPRECATED_SUPPRESS)
3499 warn_deprecated_use (id);
3502 if (id == ridpointers[(int) RID_INT])
3504 if (id == ridpointers[(int) RID_CHAR])
3507 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3509 enum rid i = C_RID_CODE (id);
3510 if ((int) i <= (int) RID_LAST_MODIFIER)
3512 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3515 error ("`long long long' is too long for GCC");
3518 if (pedantic && !flag_isoc99 && ! in_system_header
3520 pedwarn ("ISO C90 does not support `long long'");
3524 else if (specbits & (1 << (int) i))
3526 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3528 if (pedantic && !flag_isoc99)
3529 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3532 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3535 /* Diagnose "__thread extern". Recall that this list
3536 is in the reverse order seen in the text. */
3538 && (specbits & (1 << (int) RID_EXTERN
3539 | 1 << (int) RID_STATIC)))
3541 if (specbits & 1 << (int) RID_EXTERN)
3542 error ("`__thread' before `extern'");
3544 error ("`__thread' before `static'");
3547 specbits |= 1 << (int) i;
3552 error ("two or more data types in declaration of `%s'", name);
3553 /* Actual typedefs come to us as TYPE_DECL nodes. */
3554 else if (TREE_CODE (id) == TYPE_DECL)
3556 if (TREE_TYPE (id) == error_mark_node)
3557 ; /* Allow the type to default to int to avoid cascading errors. */
3560 type = TREE_TYPE (id);
3561 decl_attr = DECL_ATTRIBUTES (id);
3565 /* Built-in types come as identifiers. */
3566 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3568 tree t = lookup_name (id);
3569 if (!t || TREE_CODE (t) != TYPE_DECL)
3570 error ("`%s' fails to be a typedef or built in type",
3571 IDENTIFIER_POINTER (id));
3572 else if (TREE_TYPE (t) == error_mark_node)
3576 type = TREE_TYPE (t);
3580 else if (TREE_CODE (id) != ERROR_MARK)
3587 typedef_type = type;
3589 size_varies = C_TYPE_VARIABLE_SIZE (type);
3591 /* No type at all: default to `int', and set DEFAULTED_INT
3592 because it was not a user-defined typedef. */
3596 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3597 | (1 << (int) RID_SIGNED)
3598 | (1 << (int) RID_UNSIGNED)
3599 | (1 << (int) RID_COMPLEX))))
3600 /* Don't warn about typedef foo = bar. */
3601 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3602 && ! in_system_header)
3604 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3605 and this is a function, or if -Wimplicit; prefer the former
3606 warning since it is more explicit. */
3607 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3609 warn_about_return_type = 1;
3610 else if (warn_implicit_int || flag_isoc99)
3611 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3616 type = integer_type_node;
3619 /* Now process the modifiers that were specified
3620 and check for invalid combinations. */
3622 /* Long double is a special combination. */
3624 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3625 && TYPE_MAIN_VARIANT (type) == double_type_node)
3627 specbits &= ~(1 << (int) RID_LONG);
3628 type = long_double_type_node;
3631 /* Check all other uses of type modifiers. */
3633 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3634 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3638 if ((specbits & 1 << (int) RID_LONG)
3639 && (specbits & 1 << (int) RID_SHORT))
3640 error ("both long and short specified for `%s'", name);
3641 else if (((specbits & 1 << (int) RID_LONG)
3642 || (specbits & 1 << (int) RID_SHORT))
3644 error ("long or short specified with char for `%s'", name);
3645 else if (((specbits & 1 << (int) RID_LONG)
3646 || (specbits & 1 << (int) RID_SHORT))
3647 && TREE_CODE (type) == REAL_TYPE)
3649 static int already = 0;
3651 error ("long or short specified with floating type for `%s'", name);
3652 if (! already && ! pedantic)
3654 error ("the only valid combination is `long double'");
3658 else if ((specbits & 1 << (int) RID_SIGNED)
3659 && (specbits & 1 << (int) RID_UNSIGNED))
3660 error ("both signed and unsigned specified for `%s'", name);
3661 else if (TREE_CODE (type) != INTEGER_TYPE)
3662 error ("long, short, signed or unsigned invalid for `%s'", name);
3666 if (!explicit_int && !defaulted_int && !explicit_char)
3668 error ("long, short, signed or unsigned used invalidly for `%s'",
3674 /* Discard the type modifiers if they are invalid. */
3677 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3678 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3683 if ((specbits & (1 << (int) RID_COMPLEX))
3684 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3686 error ("complex invalid for `%s'", name);
3687 specbits &= ~(1 << (int) RID_COMPLEX);
3690 /* Decide whether an integer type is signed or not.
3691 Optionally treat bit-fields as signed by default. */
3692 if (specbits & 1 << (int) RID_UNSIGNED
3693 || (bitfield && ! flag_signed_bitfields
3694 && (explicit_int || defaulted_int || explicit_char
3695 /* A typedef for plain `int' without `signed'
3696 can be controlled just like plain `int'. */
3697 || ! (typedef_decl != 0
3698 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3699 && TREE_CODE (type) != ENUMERAL_TYPE
3700 && !(specbits & 1 << (int) RID_SIGNED)))
3703 type = long_long_unsigned_type_node;
3704 else if (specbits & 1 << (int) RID_LONG)
3705 type = long_unsigned_type_node;
3706 else if (specbits & 1 << (int) RID_SHORT)
3707 type = short_unsigned_type_node;
3708 else if (type == char_type_node)
3709 type = unsigned_char_type_node;
3710 else if (typedef_decl)
3711 type = c_common_unsigned_type (type);
3713 type = unsigned_type_node;
3715 else if ((specbits & 1 << (int) RID_SIGNED)
3716 && type == char_type_node)
3717 type = signed_char_type_node;
3719 type = long_long_integer_type_node;
3720 else if (specbits & 1 << (int) RID_LONG)
3721 type = long_integer_type_node;
3722 else if (specbits & 1 << (int) RID_SHORT)
3723 type = short_integer_type_node;
3725 if (specbits & 1 << (int) RID_COMPLEX)
3727 if (pedantic && !flag_isoc99)
3728 pedwarn ("ISO C90 does not support complex types");
3729 /* If we just have "complex", it is equivalent to
3730 "complex double", but if any modifiers at all are specified it is
3731 the complex form of TYPE. E.g, "complex short" is
3732 "complex short int". */
3734 if (defaulted_int && ! longlong
3735 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3736 | (1 << (int) RID_SIGNED)
3737 | (1 << (int) RID_UNSIGNED))))
3740 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3741 type = complex_double_type_node;
3743 else if (type == integer_type_node)
3746 pedwarn ("ISO C does not support complex integer types");
3747 type = complex_integer_type_node;
3749 else if (type == float_type_node)
3750 type = complex_float_type_node;
3751 else if (type == double_type_node)
3752 type = complex_double_type_node;
3753 else if (type == long_double_type_node)
3754 type = complex_long_double_type_node;
3758 pedwarn ("ISO C does not support complex integer types");
3759 type = build_complex_type (type);
3763 /* Check the type and width of a bit-field. */
3765 check_bitfield_type_and_width (&type, width, orig_name);
3767 /* Figure out the type qualifiers for the declaration. There are
3768 two ways a declaration can become qualified. One is something
3769 like `const int i' where the `const' is explicit. Another is
3770 something like `typedef const int CI; CI i' where the type of the
3771 declaration contains the `const'. A third possibility is that
3772 there is a type qualifier on the element type of a typedefed
3773 array type, in which case we should extract that qualifier so
3774 that c_apply_type_quals_to_decls receives the full list of
3775 qualifiers to work with (C90 is not entirely clear about whether
3776 duplicate qualifiers should be diagnosed in this case, but it
3777 seems most appropriate to do so). */
3778 element_type = strip_array_types (type);
3779 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3781 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3783 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3784 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3785 if (pedantic && !flag_isoc99)
3788 pedwarn ("duplicate `const'");
3790 pedwarn ("duplicate `restrict'");
3792 pedwarn ("duplicate `volatile'");
3794 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3795 type = TYPE_MAIN_VARIANT (type);
3796 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3797 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3798 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3800 /* Warn if two storage classes are given. Default to `auto'. */
3805 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3806 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3807 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3808 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3809 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3811 /* "static __thread" and "extern __thread" are allowed. */
3812 if ((specbits & (1 << (int) RID_THREAD
3813 | 1 << (int) RID_STATIC
3814 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3817 /* Warn about storage classes that are invalid for certain
3818 kinds of declarations (parameters, typenames, etc.). */
3821 error ("multiple storage classes in declaration of `%s'", name);
3822 else if (funcdef_flag
3824 & ((1 << (int) RID_REGISTER)
3825 | (1 << (int) RID_AUTO)
3826 | (1 << (int) RID_TYPEDEF)
3827 | (1 << (int) RID_THREAD))))
3829 if (specbits & 1 << (int) RID_AUTO
3830 && (pedantic || current_scope == file_scope))
3831 pedwarn ("function definition declared `auto'");
3832 if (specbits & 1 << (int) RID_REGISTER)
3833 error ("function definition declared `register'");
3834 if (specbits & 1 << (int) RID_TYPEDEF)
3835 error ("function definition declared `typedef'");
3836 if (specbits & 1 << (int) RID_THREAD)
3837 error ("function definition declared `__thread'");
3838 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3839 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3841 else if (decl_context != NORMAL && nclasses > 0)
3843 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3847 switch (decl_context)
3850 error ("storage class specified for structure field `%s'",
3854 error ("storage class specified for parameter `%s'", name);
3857 error ("storage class specified for typename");
3860 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3861 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3862 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3865 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3867 /* `extern' with initialization is invalid if not at file scope. */
3868 if (current_scope == file_scope)
3869 warning ("`%s' initialized and declared `extern'", name);
3871 error ("`%s' has both `extern' and initializer", name);
3873 else if (current_scope == file_scope)
3875 if (specbits & 1 << (int) RID_AUTO)
3876 error ("file-scope declaration of `%s' specifies `auto'", name);
3880 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3881 error ("nested function `%s' declared `extern'", name);
3882 else if ((specbits & (1 << (int) RID_THREAD
3883 | 1 << (int) RID_EXTERN
3884 | 1 << (int) RID_STATIC))
3885 == (1 << (int) RID_THREAD))
3887 error ("function-scope `%s' implicitly auto and declared `__thread'",
3889 specbits &= ~(1 << (int) RID_THREAD);
3894 /* Now figure out the structure of the declarator proper.
3895 Descend through it, creating more complex types, until we reach
3896 the declared identifier (or NULL_TREE, in an absolute declarator). */
3898 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3900 if (type == error_mark_node)
3902 declarator = TREE_OPERAND (declarator, 0);
3906 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3907 an INDIRECT_REF (for *...),
3908 a CALL_EXPR (for ...(...)),
3909 a TREE_LIST (for nested attributes),
3910 an identifier (for the name being declared)
3911 or a null pointer (for the place in an absolute declarator
3912 where the name was omitted).
3913 For the last two cases, we have just exited the loop.
3915 At this point, TYPE is the type of elements of an array,
3916 or for a function to return, or for a pointer to point to.
3917 After this sequence of ifs, TYPE is the type of the
3918 array or function or pointer, and DECLARATOR has had its
3919 outermost layer removed. */
3921 if (array_ptr_quals != NULL_TREE || array_parm_static)
3923 /* Only the innermost declarator (making a parameter be of
3924 array type which is converted to pointer type)
3925 may have static or type qualifiers. */
3926 error ("static or type qualifiers in non-parameter array declarator");
3927 array_ptr_quals = NULL_TREE;
3928 array_parm_static = 0;
3931 if (TREE_CODE (declarator) == TREE_LIST)
3933 /* We encode a declarator with embedded attributes using
3935 tree attrs = TREE_PURPOSE (declarator);
3938 declarator = TREE_VALUE (declarator);
3939 inner_decl = declarator;
3940 while (inner_decl != NULL_TREE
3941 && TREE_CODE (inner_decl) == TREE_LIST)
3942 inner_decl = TREE_VALUE (inner_decl);
3943 if (inner_decl == NULL_TREE
3944 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3945 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3946 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3947 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3948 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3949 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3950 returned_attrs = decl_attributes (&type,
3951 chainon (returned_attrs, attrs),
3954 else if (TREE_CODE (declarator) == ARRAY_REF)
3956 tree itype = NULL_TREE;
3957 tree size = TREE_OPERAND (declarator, 1);
3958 /* The index is a signed object `sizetype' bits wide. */
3959 tree index_type = c_common_signed_type (sizetype);
3961 array_ptr_quals = TREE_TYPE (declarator);
3962 array_parm_static = TREE_STATIC (declarator);
3964 declarator = TREE_OPERAND (declarator, 0);
3966 /* Check for some types that there cannot be arrays of. */
3968 if (VOID_TYPE_P (type))
3970 error ("declaration of `%s' as array of voids", name);
3971 type = error_mark_node;
3974 if (TREE_CODE (type) == FUNCTION_TYPE)
3976 error ("declaration of `%s' as array of functions", name);
3977 type = error_mark_node;
3980 if (pedantic && !in_system_header && flexible_array_type_p (type))
3981 pedwarn ("invalid use of structure with flexible array member");
3983 if (size == error_mark_node)
3984 type = error_mark_node;
3986 if (type == error_mark_node)
3989 /* If size was specified, set ITYPE to a range-type for that size.
3990 Otherwise, ITYPE remains null. finish_decl may figure it out
3991 from an initial value. */
3995 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3996 STRIP_TYPE_NOPS (size);
3998 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4000 error ("size of array `%s' has non-integer type", name);
4001 size = integer_one_node;
4004 if (pedantic && integer_zerop (size))
4005 pedwarn ("ISO C forbids zero-size array `%s'", name);
4007 if (TREE_CODE (size) == INTEGER_CST)
4009 constant_expression_warning (size);
4010 if (tree_int_cst_sgn (size) < 0)
4012 error ("size of array `%s' is negative", name);
4013 size = integer_one_node;
4018 /* Make sure the array size remains visibly nonconstant
4019 even if it is (eg) a const variable with known value. */
4022 if (!flag_isoc99 && pedantic)
4024 if (TREE_CONSTANT (size))
4025 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4028 pedwarn ("ISO C90 forbids variable-size array `%s'",
4033 if (integer_zerop (size))
4035 /* A zero-length array cannot be represented with an
4036 unsigned index type, which is what we'll get with
4037 build_index_type. Create an open-ended range instead. */
4038 itype = build_range_type (sizetype, size, NULL_TREE);
4042 /* Compute the maximum valid index, that is, size - 1.
4043 Do the calculation in index_type, so that if it is
4044 a variable the computations will be done in the
4046 itype = fold (build (MINUS_EXPR, index_type,
4047 convert (index_type, size),
4048 convert (index_type, size_one_node)));
4050 /* If that overflowed, the array is too big.
4051 ??? While a size of INT_MAX+1 technically shouldn't
4052 cause an overflow (because we subtract 1), the overflow
4053 is recorded during the conversion to index_type, before
4054 the subtraction. Handling this case seems like an
4055 unnecessary complication. */
4056 if (TREE_OVERFLOW (itype))
4058 error ("size of array `%s' is too large", name);
4059 type = error_mark_node;
4064 itype = variable_size (itype);
4065 itype = build_index_type (itype);
4068 else if (decl_context == FIELD)
4070 if (pedantic && !flag_isoc99 && !in_system_header)
4071 pedwarn ("ISO C90 does not support flexible array members");
4073 /* ISO C99 Flexible array members are effectively identical
4074 to GCC's zero-length array extension. */
4075 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4078 /* If pedantic, complain about arrays of incomplete types. */
4080 if (pedantic && !COMPLETE_TYPE_P (type))
4081 pedwarn ("array type has incomplete element type");
4083 /* Build the array type itself, then merge any constancy or
4084 volatility into the target type. We must do it in this order
4085 to ensure that the TYPE_MAIN_VARIANT field of the array type
4086 is set correctly. */
4088 type = build_array_type (type, itype);
4090 type = c_build_qualified_type (type, type_quals);
4093 C_TYPE_VARIABLE_SIZE (type) = 1;
4095 /* The GCC extension for zero-length arrays differs from
4096 ISO flexible array members in that sizeof yields zero. */
4097 if (size && integer_zerop (size))
4100 TYPE_SIZE (type) = bitsize_zero_node;
4101 TYPE_SIZE_UNIT (type) = size_zero_node;
4103 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4104 /* We can never complete an array type which is the target of a
4105 pointer, so go ahead and lay it out. */
4108 if (decl_context != PARM
4109 && (array_ptr_quals != NULL_TREE || array_parm_static))
4111 error ("static or type qualifiers in non-parameter array declarator");
4112 array_ptr_quals = NULL_TREE;
4113 array_parm_static = 0;
4116 else if (TREE_CODE (declarator) == CALL_EXPR)
4118 /* Say it's a definition only for the CALL_EXPR closest to
4120 bool really_funcdef = (funcdef_flag
4121 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4122 == IDENTIFIER_NODE));
4125 /* Declaring a function type.
4126 Make sure we have a valid type for the function to return. */
4127 if (type == error_mark_node)
4132 /* Warn about some types functions can't return. */
4134 if (TREE_CODE (type) == FUNCTION_TYPE)
4136 error ("`%s' declared as function returning a function", name);
4137 type = integer_type_node;
4139 if (TREE_CODE (type) == ARRAY_TYPE)
4141 error ("`%s' declared as function returning an array", name);
4142 type = integer_type_node;
4145 /* Construct the function type and go to the next
4146 inner layer of declarator. */
4147 arg_info = TREE_OPERAND (declarator, 1);
4148 arg_types = grokparms (arg_info, really_funcdef);
4150 /* Type qualifiers before the return type of the function
4151 qualify the return type, not the function type. */
4154 /* Type qualifiers on a function return type are normally
4155 permitted by the standard but have no effect, so give a
4156 warning at -Wextra. Qualifiers on a void return type have
4157 meaning as a GNU extension, and are banned on function
4158 definitions in ISO C. FIXME: strictly we shouldn't
4159 pedwarn for qualified void return types except on function
4160 definitions, but not doing so could lead to the undesirable
4161 state of a "volatile void" function return type not being
4162 warned about, and a use of the function being compiled
4163 with GNU semantics, with no diagnostics under -pedantic. */
4164 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4165 pedwarn ("ISO C forbids qualified void function return type");
4166 else if (extra_warnings
4167 && !(VOID_TYPE_P (type)
4168 && type_quals == TYPE_QUAL_VOLATILE))
4169 warning ("type qualifiers ignored on function return type");
4171 type = c_build_qualified_type (type, type_quals);
4173 type_quals = TYPE_UNQUALIFIED;
4175 type = build_function_type (type, arg_types);
4176 declarator = TREE_OPERAND (declarator, 0);
4178 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4179 the formal parameter list of this FUNCTION_TYPE to point to
4180 the FUNCTION_TYPE node itself. */
4185 for (link = ARG_INFO_TAGS (arg_info);
4187 link = TREE_CHAIN (link))
4188 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4191 else if (TREE_CODE (declarator) == INDIRECT_REF)
4193 /* Merge any constancy or volatility into the target type
4196 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4198 pedwarn ("ISO C forbids qualified function types");
4200 type = c_build_qualified_type (type, type_quals);
4201 type_quals = TYPE_UNQUALIFIED;
4204 type = build_pointer_type (type);
4206 /* Process a list of type modifier keywords
4207 (such as const or volatile) that were given inside the `*'. */
4209 if (TREE_TYPE (declarator))
4217 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4218 typemodlist = TREE_CHAIN (typemodlist))
4220 tree qualifier = TREE_VALUE (typemodlist);
4222 if (C_IS_RESERVED_WORD (qualifier))
4224 if (C_RID_CODE (qualifier) == RID_CONST)
4226 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4228 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4238 error ("invalid type modifier within pointer declarator");
4239 if (pedantic && !flag_isoc99)
4242 pedwarn ("duplicate `const'");
4244 pedwarn ("duplicate `volatile'");
4246 pedwarn ("duplicate `restrict'");
4249 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4250 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4251 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4254 declarator = TREE_OPERAND (declarator, 0);
4261 /* Now TYPE has the actual type. */
4263 /* Did array size calculations overflow? */
4265 if (TREE_CODE (type) == ARRAY_TYPE
4266 && COMPLETE_TYPE_P (type)
4267 && TREE_OVERFLOW (TYPE_SIZE (type)))
4269 error ("size of array `%s' is too large", name);
4270 /* If we proceed with the array type as it is, we'll eventually
4271 crash in tree_low_cst(). */
4272 type = error_mark_node;
4275 /* If this is declaring a typedef name, return a TYPE_DECL. */
4277 if (specbits & (1 << (int) RID_TYPEDEF))
4280 /* Note that the grammar rejects storage classes
4281 in typenames, fields or parameters */
4282 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4284 pedwarn ("ISO C forbids qualified function types");
4286 type = c_build_qualified_type (type, type_quals);
4287 decl = build_decl (TYPE_DECL, declarator, type);
4288 if ((specbits & (1 << (int) RID_SIGNED))
4289 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4290 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4291 decl_attributes (&decl, returned_attrs, 0);
4295 /* Detect the case of an array type of unspecified size
4296 which came, as such, direct from a typedef name.
4297 We must copy the type, so that each identifier gets
4298 a distinct type, so that each identifier's size can be
4299 controlled separately by its own initializer. */
4301 if (type != 0 && typedef_type != 0
4302 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4303 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4305 type = build_array_type (TREE_TYPE (type), 0);
4307 C_TYPE_VARIABLE_SIZE (type) = 1;
4310 /* If this is a type name (such as, in a cast or sizeof),
4311 compute the type and return it now. */
4313 if (decl_context == TYPENAME)
4315 /* Note that the grammar rejects storage classes
4316 in typenames, fields or parameters */
4317 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4319 pedwarn ("ISO C forbids const or volatile function types");
4321 type = c_build_qualified_type (type, type_quals);
4322 decl_attributes (&type, returned_attrs, 0);
4326 /* Aside from typedefs and type names (handle above),
4327 `void' at top level (not within pointer)
4328 is allowed only in public variables.
4329 We don't complain about parms either, but that is because
4330 a better error message can be made later. */
4332 if (VOID_TYPE_P (type) && decl_context != PARM
4333 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4334 && ((specbits & (1 << (int) RID_EXTERN))
4335 || (current_scope == file_scope
4337 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4339 error ("variable or field `%s' declared void", name);
4340 type = integer_type_node;
4343 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4344 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4349 if (decl_context == PARM)
4351 tree type_as_written;
4354 /* A parameter declared as an array of T is really a pointer to T.
4355 One declared as a function is really a pointer to a function. */
4357 if (TREE_CODE (type) == ARRAY_TYPE)
4359 /* Transfer const-ness of array into that of type pointed to. */
4360 type = TREE_TYPE (type);
4362 type = c_build_qualified_type (type, type_quals);
4363 type = build_pointer_type (type);
4364 type_quals = TYPE_UNQUALIFIED;
4365 if (array_ptr_quals)
4367 tree new_ptr_quals, new_ptr_attrs;
4369 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4370 /* We don't yet implement attributes in this context. */
4371 if (new_ptr_attrs != NULL_TREE)
4372 warning ("attributes in parameter array declarator ignored");
4377 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4379 tree qualifier = TREE_VALUE (new_ptr_quals);
4381 if (C_IS_RESERVED_WORD (qualifier))
4383 if (C_RID_CODE (qualifier) == RID_CONST)
4385 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4387 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4397 error ("invalid type modifier within array declarator");
4399 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4400 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4401 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4405 else if (TREE_CODE (type) == FUNCTION_TYPE)
4407 if (pedantic && type_quals)
4408 pedwarn ("ISO C forbids qualified function types");
4410 type = c_build_qualified_type (type, type_quals);
4411 type = build_pointer_type (type);
4412 type_quals = TYPE_UNQUALIFIED;
4414 else if (type_quals)
4415 type = c_build_qualified_type (type, type_quals);
4417 type_as_written = type;
4419 decl = build_decl (PARM_DECL, declarator, type);
4421 C_DECL_VARIABLE_SIZE (decl) = 1;
4423 /* Compute the type actually passed in the parmlist,
4424 for the case where there is no prototype.
4425 (For example, shorts and chars are passed as ints.)
4426 When there is a prototype, this is overridden later. */
4428 if (type == error_mark_node)
4429 promoted_type = type;
4431 promoted_type = c_type_promotes_to (type);
4433 DECL_ARG_TYPE (decl) = promoted_type;
4434 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4436 else if (decl_context == FIELD)
4438 /* Structure field. It may not be a function. */
4440 if (TREE_CODE (type) == FUNCTION_TYPE)
4442 error ("field `%s' declared as a function", name);
4443 type = build_pointer_type (type);
4445 else if (TREE_CODE (type) != ERROR_MARK
4446 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4448 error ("field `%s' has incomplete type", name);
4449 type = error_mark_node;
4451 /* Move type qualifiers down to element of an array. */
4452 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4453 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4455 TYPE_DOMAIN (type));
4456 decl = build_decl (FIELD_DECL, declarator, type);
4457 DECL_NONADDRESSABLE_P (decl) = bitfield;
4460 C_DECL_VARIABLE_SIZE (decl) = 1;
4462 else if (TREE_CODE (type) == FUNCTION_TYPE)
4464 /* Every function declaration is "external"
4465 except for those which are inside a function body
4466 in which `auto' is used.
4467 That is a case not specified by ANSI C,
4468 and we use it for forward declarations for nested functions. */
4469 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4470 || current_scope == file_scope);
4472 if (specbits & (1 << (int) RID_AUTO)
4473 && (pedantic || current_scope == file_scope))
4474 pedwarn ("invalid storage class for function `%s'", name);
4475 if (specbits & (1 << (int) RID_REGISTER))
4476 error ("invalid storage class for function `%s'", name);
4477 if (specbits & (1 << (int) RID_THREAD))
4478 error ("invalid storage class for function `%s'", name);
4479 /* Function declaration not at file scope.
4480 Storage classes other than `extern' are not allowed
4481 and `extern' makes no difference. */
4482 if (current_scope != file_scope
4483 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4485 pedwarn ("invalid storage class for function `%s'", name);
4487 decl = build_decl (FUNCTION_DECL, declarator, type);
4488 decl = build_decl_attribute_variant (decl, decl_attr);
4490 DECL_LANG_SPECIFIC (decl)
4491 = ggc_alloc_cleared (sizeof (struct lang_decl));
4493 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4494 pedwarn ("ISO C forbids qualified function types");
4496 /* GNU C interprets a `volatile void' return type to indicate
4497 that the function does not return. */
4498 if ((type_quals & TYPE_QUAL_VOLATILE)
4499 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4500 warning ("`noreturn' function returns non-void value");
4503 DECL_EXTERNAL (decl) = 1;
4504 /* Record absence of global scope for `static' or `auto'. */
4506 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4508 /* For a function definition, record the argument information
4509 block in DECL_ARGUMENTS where store_parm_decls will look
4512 DECL_ARGUMENTS (decl) = arg_info;
4515 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4517 /* Record presence of `inline', if it is reasonable. */
4518 if (MAIN_NAME_P (declarator))
4521 warning ("cannot inline function `main'");
4525 /* Record that the function is declared `inline'. */
4526 DECL_DECLARED_INLINE_P (decl) = 1;
4528 /* Do not mark bare declarations as DECL_INLINE. Doing so
4529 in the presence of multiple declarations can result in
4530 the abstract origin pointing between the declarations,
4531 which will confuse dwarf2out. */
4534 DECL_INLINE (decl) = 1;
4535 if (specbits & (1 << (int) RID_EXTERN))
4536 current_extern_inline = 1;
4539 /* If -finline-functions, assume it can be inlined. This does
4540 two things: let the function be deferred until it is actually
4541 needed, and let dwarf2 know that the function is inlinable. */
4542 else if (flag_inline_trees == 2 && initialized)
4543 DECL_INLINE (decl) = 1;
4547 /* It's a variable. */
4548 /* An uninitialized decl with `extern' is a reference. */
4549 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4551 /* Move type qualifiers down to element of an array. */
4552 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4554 int saved_align = TYPE_ALIGN(type);
4555 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4557 TYPE_DOMAIN (type));
4558 TYPE_ALIGN (type) = saved_align;
4560 else if (type_quals)
4561 type = c_build_qualified_type (type, type_quals);
4563 /* C99 6.2.2p7: It is invalid (compile-time undefined
4564 behavior) to create an 'extern' declaration for a
4565 variable if there is a global declaration that is
4566 'static' and the global declaration is not visible.
4567 (If the static declaration _is_ currently visible,
4568 the 'extern' declaration is taken to refer to that decl.) */
4569 if (extern_ref && current_scope != file_scope)
4571 tree global_decl = identifier_global_value (declarator);
4572 tree visible_decl = lookup_name (declarator);
4575 && global_decl != visible_decl
4576 && TREE_CODE (global_decl) == VAR_DECL
4577 && !TREE_PUBLIC (global_decl))
4578 error ("variable previously declared 'static' redeclared "
4582 decl = build_decl (VAR_DECL, declarator, type);
4584 C_DECL_VARIABLE_SIZE (decl) = 1;
4587 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4589 DECL_EXTERNAL (decl) = extern_ref;
4591 /* At file scope, the presence of a `static' or `register' storage
4592 class specifier, or the absence of all storage class specifiers
4593 makes this declaration a definition (perhaps tentative). Also,
4594 the absence of both `static' and `register' makes it public. */
4595 if (current_scope == file_scope)
4597 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4598 | (1 << (int) RID_REGISTER)));
4599 TREE_STATIC (decl) = !extern_ref;
4601 /* Not at file scope, only `static' makes a static definition. */
4604 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4605 TREE_PUBLIC (decl) = extern_ref;
4608 if (specbits & 1 << (int) RID_THREAD)
4610 if (targetm.have_tls)
4611 DECL_THREAD_LOCAL (decl) = 1;
4613 /* A mere warning is sure to result in improper semantics
4614 at runtime. Don't bother to allow this to compile. */
4615 error ("thread-local storage not supported for this target");
4619 /* Record `register' declaration for warnings on &
4620 and in case doing stupid register allocation. */
4622 if (specbits & (1 << (int) RID_REGISTER))
4624 C_DECL_REGISTER (decl) = 1;
4625 DECL_REGISTER (decl) = 1;
4628 /* Record constancy and volatility. */
4629 c_apply_type_quals_to_decl (type_quals, decl);
4631 /* If a type has volatile components, it should be stored in memory.
4632 Otherwise, the fact that those components are volatile
4633 will be ignored, and would even crash the compiler. */
4634 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4636 /* It is not an error for a structure with volatile fields to
4637 be declared register, but reset DECL_REGISTER since it
4638 cannot actually go in a register. */
4639 int was_reg = C_DECL_REGISTER (decl);
4640 C_DECL_REGISTER (decl) = 0;
4641 DECL_REGISTER (decl) = 0;
4642 c_mark_addressable (decl);
4643 C_DECL_REGISTER (decl) = was_reg;
4646 #ifdef ENABLE_CHECKING
4647 /* This is the earliest point at which we might know the assembler
4648 name of a variable. Thus, if it's known before this, die horribly. */
4649 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4653 decl_attributes (&decl, returned_attrs, 0);
4659 /* Decode the parameter-list info for a function type or function definition.
4660 The argument is the value returned by `get_parm_info' (or made in parse.y
4661 if there is an identifier list instead of a parameter decl list).
4662 These two functions are separate because when a function returns
4663 or receives functions then each is called multiple times but the order
4664 of calls is different. The last call to `grokparms' is always the one
4665 that contains the formal parameter names of a function definition.
4667 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4669 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4670 a mere declaration. A nonempty identifier-list gets an error message
4671 when FUNCDEF_FLAG is zero. */
4674 grokparms (tree arg_info, int funcdef_flag)
4676 tree arg_types = ARG_INFO_TYPES (arg_info);
4678 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4679 && !in_system_header)
4680 warning ("function declaration isn't a prototype");
4682 if (arg_types == error_mark_node)
4683 return 0; /* don't set TYPE_ARG_TYPES in this case */
4685 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4688 pedwarn ("parameter names (without types) in function declaration");
4690 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4691 ARG_INFO_TYPES (arg_info) = 0;
4696 tree parm, type, typelt;
4697 unsigned int parmno;
4699 /* If the arg types are incomplete in a declaration, they must
4700 include undefined tags. These tags can never be defined in
4701 the scope of the declaration, so the types can never be
4702 completed, and no call can be compiled successfully. */
4704 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4706 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4708 type = TREE_VALUE (typelt);
4709 if (type == error_mark_node)
4712 if (!COMPLETE_TYPE_P (type))
4716 if (DECL_NAME (parm))
4717 error ("%Jparameter %u ('%D') has incomplete type",
4718 parm, parmno, parm);
4720 error ("%Jparameter %u has incomplete type",
4723 TREE_VALUE (typelt) = error_mark_node;
4724 TREE_TYPE (parm) = error_mark_node;
4728 if (DECL_NAME (parm))
4729 warning ("%Jparameter %u ('%D') has incomplete type",
4730 parm, parmno, parm);
4732 warning ("%Jparameter %u has incomplete type",
4741 /* Take apart the current scope and return a tree_list node with info
4742 on a parameter list just parsed. This tree_list node should be
4743 examined using the ARG_INFO_* macros, defined above:
4745 ARG_INFO_PARMS: a list of parameter decls.
4746 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4747 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4748 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4749 constants) defined with the parameters.
4751 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4753 ELLIPSIS being true means the argument list ended in '...' so don't
4754 append a sentinel (void_list_node) to the end of the type-list. */
4757 get_parm_info (bool ellipsis)
4759 struct c_binding *b = current_scope->bindings;
4760 tree arg_info = make_node (TREE_LIST);
4766 static bool explained_incomplete_types = false;
4767 bool gave_void_only_once_err = false;
4769 /* The bindings in this scope must not get put into a block.
4770 We will take care of deleting the binding nodes. */
4771 current_scope->bindings = 0;
4773 /* This function is only called if there was *something* on the
4775 #ifdef ENABLE_CHECKING
4780 /* A parameter list consisting solely of 'void' indicates that the
4781 function takes no arguments. But if the 'void' is qualified
4782 (by 'const' or 'volatile'), or has a storage class specifier
4783 ('register'), then the behavior is undefined; issue an error.
4784 Typedefs for 'void' are OK (see DR#157). */
4785 if (b->prev == 0 /* one binding */
4786 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4787 && !DECL_NAME (b->decl) /* anonymous */
4788 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4790 if (TREE_THIS_VOLATILE (b->decl)
4791 || TREE_READONLY (b->decl)
4792 || C_DECL_REGISTER (b->decl))
4793 error ("'void' as only parameter may not be qualified");
4795 /* There cannot be an ellipsis. */
4797 error ("'void' must be the only parameter");
4799 ARG_INFO_TYPES (arg_info) = void_list_node;
4804 types = void_list_node;
4806 /* Break up the bindings list into parms, tags, types, and others;
4807 apply sanity checks; purge the name-to-decl bindings. */
4810 tree decl = b->decl;
4811 tree type = TREE_TYPE (decl);
4812 const char *keyword;
4814 switch (TREE_CODE (decl))
4819 #ifdef ENABLE_CHECKING
4820 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4822 I_SYMBOL_BINDING (b->id) = b->shadowed;
4825 /* Check for forward decls that never got their actual decl. */
4826 if (TREE_ASM_WRITTEN (decl))
4827 error ("%Jparameter '%D' has just a forward declaration",
4829 /* Check for (..., void, ...) and issue an error. */
4830 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4832 if (!gave_void_only_once_err)
4834 error ("'void' must be the only parameter");
4835 gave_void_only_once_err = true;
4840 /* Valid parameter, add it to the list. */
4841 TREE_CHAIN (decl) = parms;
4844 /* Since there is a prototype, args are passed in their
4845 declared types. The back end may override this later. */
4846 DECL_ARG_TYPE (decl) = type;
4847 types = tree_cons (0, type, types);
4851 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4852 case UNION_TYPE: keyword = "union"; goto tag;
4853 case RECORD_TYPE: keyword = "struct"; goto tag;
4855 /* Types may not have tag-names, in which case the type
4856 appears in the bindings list with b->id NULL. */
4859 #ifdef ENABLE_CHECKING
4860 if (I_TAG_BINDING (b->id) != b) abort ();
4862 I_TAG_BINDING (b->id) = b->shadowed;
4865 /* Warn about any struct, union or enum tags defined in a
4866 parameter list. The scope of such types is limited to
4867 the parameter list, which is rarely if ever desirable
4868 (it's impossible to call such a function with type-
4869 correct arguments). An anonymous union parm type is
4870 meaningful as a GNU extension, so don't warn for that. */
4871 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4874 /* The %s will be one of 'struct', 'union', or 'enum'. */
4875 warning ("'%s %E' declared inside parameter list",
4878 /* The %s will be one of 'struct', 'union', or 'enum'. */
4879 warning ("anonymous %s declared inside parameter list",
4882 if (! explained_incomplete_types)
4884 warning ("its scope is only this definition or declaration,"
4885 " which is probably not what you want");
4886 explained_incomplete_types = true;
4890 tags = tree_cons (b->id, decl, tags);
4895 /* CONST_DECLs appear here when we have an embedded enum,
4896 and TYPE_DECLs appear here when we have an embedded struct
4897 or union. No warnings for this - we already warned about the
4899 TREE_CHAIN (decl) = others;
4904 /* error_mark_node appears here when we have an undeclared
4905 variable. Just throw it away. */
4908 #ifdef ENABLE_CHECKING
4909 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4911 I_SYMBOL_BINDING (b->id) = b->shadowed;
4915 /* Other things that might be encountered. */
4923 b = free_binding_and_advance (b);
4926 ARG_INFO_PARMS (arg_info) = parms;
4927 ARG_INFO_TAGS (arg_info) = tags;
4928 ARG_INFO_TYPES (arg_info) = types;
4929 ARG_INFO_OTHERS (arg_info) = others;
4933 /* Get the struct, enum or union (CODE says which) with tag NAME.
4934 Define the tag as a forward-reference if it is not defined. */
4937 xref_tag (enum tree_code code, tree name)
4939 /* If a cross reference is requested, look up the type
4940 already defined for this tag and return it. */
4942 tree ref = lookup_tag (code, name, 0);
4943 /* If this is the right type of tag, return what we found.
4944 (This reference will be shadowed by shadow_tag later if appropriate.)
4945 If this is the wrong type of tag, do not return it. If it was the
4946 wrong type in the same scope, we will have had an error
4947 message already; if in a different scope and declaring
4948 a name, pending_xref_error will give an error message; but if in a
4949 different scope and not declaring a name, this tag should
4950 shadow the previous declaration of a different type of tag, and
4951 this would not work properly if we return the reference found.
4952 (For example, with "struct foo" in an outer scope, "union foo;"
4953 must shadow that tag with a new one of union type.) */
4954 if (ref && TREE_CODE (ref) == code)
4957 /* If no such tag is yet defined, create a forward-reference node
4958 and record it as the "definition".
4959 When a real declaration of this type is found,
4960 the forward-reference will be altered into a real type. */
4962 ref = make_node (code);
4963 if (code == ENUMERAL_TYPE)
4965 /* Give the type a default layout like unsigned int
4966 to avoid crashing if it does not get defined. */
4967 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4968 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4969 TYPE_USER_ALIGN (ref) = 0;
4970 TYPE_UNSIGNED (ref) = 1;
4971 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4972 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4973 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4976 pushtag (name, ref);
4981 /* Make sure that the tag NAME is defined *in the current scope*
4982 at least as a forward reference.
4983 CODE says which kind of tag NAME ought to be. */
4986 start_struct (enum tree_code code, tree name)
4988 /* If there is already a tag defined at this scope
4989 (as a forward reference), just return it. */
4994 ref = lookup_tag (code, name, 1);
4995 if (ref && TREE_CODE (ref) == code)
4997 if (TYPE_FIELDS (ref))
4999 if (code == UNION_TYPE)
5000 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5002 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5007 /* Otherwise create a forward-reference just so the tag is in scope. */
5009 ref = make_node (code);
5010 pushtag (name, ref);
5013 C_TYPE_BEING_DEFINED (ref) = 1;
5014 TYPE_PACKED (ref) = flag_pack_struct;
5018 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5019 of a structure component, returning a FIELD_DECL node.
5020 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5022 This is done during the parsing of the struct declaration.
5023 The FIELD_DECL nodes are chained together and the lot of them
5024 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5027 grokfield (tree declarator, tree declspecs, tree width)
5031 if (declarator == NULL_TREE && width == NULL_TREE)
5033 /* This is an unnamed decl.
5035 If we have something of the form "union { list } ;" then this
5036 is the anonymous union extension. Similarly for struct.
5038 If this is something of the form "struct foo;", then
5039 If MS extensions are enabled, this is handled as an
5041 Otherwise this is a forward declaration of a structure tag.
5043 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5044 If MS extensions are enabled and foo names a structure, then
5045 again this is an anonymous struct.
5046 Otherwise this is an error.
5048 Oh what a horrid tangled web we weave. I wonder if MS consciously
5049 took this from Plan 9 or if it was an accident of implementation
5050 that took root before someone noticed the bug... */
5052 tree type = TREE_VALUE (declspecs);
5054 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5055 type = TREE_TYPE (type);
5056 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5058 if (flag_ms_extensions)
5061 goto warn_unnamed_field;
5062 else if (TYPE_NAME (type) == NULL)
5065 goto warn_unnamed_field;
5070 warning ("declaration does not declare anything");
5075 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5076 width ? &width : NULL);
5078 finish_decl (value, NULL_TREE, NULL_TREE);
5079 DECL_INITIAL (value) = width;
5084 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5085 the list such that this does not present a problem later. */
5088 detect_field_duplicates (tree fieldlist)
5093 /* First, see if there are more than "a few" fields.
5094 This is trivially true if there are zero or one fields. */
5097 x = TREE_CHAIN (fieldlist);
5103 } while (timeout > 0 && x);
5105 /* If there were "few" fields, avoid the overhead of allocating
5106 a hash table. Instead just do the nested traversal thing. */
5109 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5112 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5113 if (DECL_NAME (y) == DECL_NAME (x))
5115 error ("%Jduplicate member '%D'", x, x);
5116 DECL_NAME (x) = NULL_TREE;
5122 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5125 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5126 if ((y = DECL_NAME (x)) != 0)
5128 slot = htab_find_slot (htab, y, INSERT);
5131 error ("%Jduplicate member '%D'", x, x);
5132 DECL_NAME (x) = NULL_TREE;
5141 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5142 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5143 ATTRIBUTES are attributes to be applied to the structure. */
5146 finish_struct (tree t, tree fieldlist, tree attributes)
5149 bool toplevel = file_scope == current_scope;
5150 int saw_named_field;
5152 /* If this type was previously laid out as a forward reference,
5153 make sure we lay it out again. */
5157 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5161 for (x = fieldlist; x; x = TREE_CHAIN (x))
5162 if (DECL_NAME (x) != 0)
5166 pedwarn ("%s has no %s",
5167 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5168 fieldlist ? _("named members") : _("members"));
5171 /* Install struct as DECL_CONTEXT of each field decl.
5172 Also process specified field sizes,m which is found in the DECL_INITIAL.
5173 Store 0 there, except for ": 0" fields (so we can find them
5174 and delete them, below). */
5176 saw_named_field = 0;
5177 for (x = fieldlist; x; x = TREE_CHAIN (x))
5179 DECL_CONTEXT (x) = t;
5180 DECL_PACKED (x) |= TYPE_PACKED (t);
5182 /* If any field is const, the structure type is pseudo-const. */
5183 if (TREE_READONLY (x))
5184 C_TYPE_FIELDS_READONLY (t) = 1;
5187 /* A field that is pseudo-const makes the structure likewise. */
5188 tree t1 = TREE_TYPE (x);
5189 while (TREE_CODE (t1) == ARRAY_TYPE)
5190 t1 = TREE_TYPE (t1);
5191 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5192 && C_TYPE_FIELDS_READONLY (t1))
5193 C_TYPE_FIELDS_READONLY (t) = 1;
5196 /* Any field that is volatile means variables of this type must be
5197 treated in some ways as volatile. */
5198 if (TREE_THIS_VOLATILE (x))
5199 C_TYPE_FIELDS_VOLATILE (t) = 1;
5201 /* Any field of nominal variable size implies structure is too. */
5202 if (C_DECL_VARIABLE_SIZE (x))
5203 C_TYPE_VARIABLE_SIZE (t) = 1;
5205 /* Detect invalid nested redefinition. */
5206 if (TREE_TYPE (x) == t)
5207 error ("nested redefinition of `%s'",
5208 IDENTIFIER_POINTER (TYPE_NAME (t)));
5210 if (DECL_INITIAL (x))
5212 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5213 DECL_SIZE (x) = bitsize_int (width);
5214 DECL_BIT_FIELD (x) = 1;
5215 SET_DECL_C_BIT_FIELD (x);
5218 DECL_INITIAL (x) = 0;
5220 /* Detect flexible array member in an invalid context. */
5221 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5222 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5223 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5224 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5226 if (TREE_CODE (t) == UNION_TYPE)
5228 error ("%Jflexible array member in union", x);
5229 TREE_TYPE (x) = error_mark_node;
5231 else if (TREE_CHAIN (x) != NULL_TREE)
5233 error ("%Jflexible array member not at end of struct", x);
5234 TREE_TYPE (x) = error_mark_node;
5236 else if (! saw_named_field)
5238 error ("%Jflexible array member in otherwise empty struct", x);
5239 TREE_TYPE (x) = error_mark_node;
5243 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5244 && flexible_array_type_p (TREE_TYPE (x)))
5245 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5248 saw_named_field = 1;
5251 detect_field_duplicates (fieldlist);
5253 /* Now we have the nearly final fieldlist. Record it,
5254 then lay out the structure or union (including the fields). */
5256 TYPE_FIELDS (t) = fieldlist;
5260 /* Delete all zero-width bit-fields from the fieldlist. */
5262 tree *fieldlistp = &fieldlist;
5264 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5265 *fieldlistp = TREE_CHAIN (*fieldlistp);
5267 fieldlistp = &TREE_CHAIN (*fieldlistp);
5270 /* Now we have the truly final field list.
5271 Store it in this type and in the variants. */
5273 TYPE_FIELDS (t) = fieldlist;
5275 /* If there are lots of fields, sort so we can look through them fast.
5276 We arbitrarily consider 16 or more elts to be "a lot". */
5281 for (x = fieldlist; x; x = TREE_CHAIN (x))
5283 if (len > 15 || DECL_NAME (x) == NULL)
5291 struct lang_type *space;
5292 struct sorted_fields_type *space2;
5294 len += list_length (x);
5296 /* Use the same allocation policy here that make_node uses, to
5297 ensure that this lives as long as the rest of the struct decl.
5298 All decls in an inline function need to be saved. */
5300 space = ggc_alloc_cleared (sizeof (struct lang_type));
5301 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5305 field_array = &space2->elts[0];
5306 for (x = fieldlist; x; x = TREE_CHAIN (x))
5308 field_array[len++] = x;
5310 /* If there is anonymous struct or union, break out of the loop. */
5311 if (DECL_NAME (x) == NULL)
5314 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5317 TYPE_LANG_SPECIFIC (t) = space;
5318 TYPE_LANG_SPECIFIC (t)->s->len = len;
5319 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5320 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5325 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5327 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5328 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5329 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5330 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5333 /* If this was supposed to be a transparent union, but we can't
5334 make it one, warn and turn off the flag. */
5335 if (TREE_CODE (t) == UNION_TYPE
5336 && TYPE_TRANSPARENT_UNION (t)
5337 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5339 TYPE_TRANSPARENT_UNION (t) = 0;
5340 warning ("union cannot be made transparent");
5343 /* If this structure or union completes the type of any previous
5344 variable declaration, lay it out and output its rtl. */
5345 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5349 tree decl = TREE_VALUE (x);
5350 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5351 layout_array_type (TREE_TYPE (decl));
5352 if (TREE_CODE (decl) != TYPE_DECL)
5354 layout_decl (decl, 0);
5355 if (c_dialect_objc ())
5356 objc_check_decl (decl);
5357 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5362 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5364 /* Finish debugging output for this type. */
5365 rest_of_type_compilation (t, toplevel);
5370 /* Lay out the type T, and its element type, and so on. */
5373 layout_array_type (tree t)
5375 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5376 layout_array_type (TREE_TYPE (t));
5380 /* Begin compiling the definition of an enumeration type.
5381 NAME is its name (or null if anonymous).
5382 Returns the type object, as yet incomplete.
5383 Also records info about it so that build_enumerator
5384 may be used to declare the individual values as they are read. */
5387 start_enum (tree name)
5391 /* If this is the real definition for a previous forward reference,
5392 fill in the contents in the same object that used to be the
5393 forward reference. */
5396 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5398 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5400 enumtype = make_node (ENUMERAL_TYPE);
5401 pushtag (name, enumtype);
5404 C_TYPE_BEING_DEFINED (enumtype) = 1;
5406 if (TYPE_VALUES (enumtype) != 0)
5408 /* This enum is a named one that has been declared already. */
5409 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5411 /* Completely replace its old definition.
5412 The old enumerators remain defined, however. */
5413 TYPE_VALUES (enumtype) = 0;
5416 enum_next_value = integer_zero_node;
5419 if (flag_short_enums)
5420 TYPE_PACKED (enumtype) = 1;
5425 /* After processing and defining all the values of an enumeration type,
5426 install their decls in the enumeration type and finish it off.
5427 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5428 and ATTRIBUTES are the specified attributes.
5429 Returns ENUMTYPE. */
5432 finish_enum (tree enumtype, tree values, tree attributes)
5435 tree minnode = 0, maxnode = 0;
5436 int precision, unsign;
5437 bool toplevel = (file_scope == current_scope);
5438 struct lang_type *lt;
5440 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5442 /* Calculate the maximum value of any enumerator in this type. */
5444 if (values == error_mark_node)
5445 minnode = maxnode = integer_zero_node;
5448 minnode = maxnode = TREE_VALUE (values);
5449 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5451 tree value = TREE_VALUE (pair);
5452 if (tree_int_cst_lt (maxnode, value))
5454 if (tree_int_cst_lt (value, minnode))
5459 /* Construct the final type of this enumeration. It is the same
5460 as one of the integral types - the narrowest one that fits, except
5461 that normally we only go as narrow as int - and signed iff any of
5462 the values are negative. */
5463 unsign = (tree_int_cst_sgn (minnode) >= 0);
5464 precision = MAX (min_precision (minnode, unsign),
5465 min_precision (maxnode, unsign));
5466 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5468 tem = c_common_type_for_size (precision, unsign);
5471 warning ("enumeration values exceed range of largest integer");
5472 tem = long_long_integer_type_node;
5476 tem = unsign ? unsigned_type_node : integer_type_node;
5478 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5479 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5480 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5481 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5482 TYPE_SIZE (enumtype) = 0;
5483 layout_type (enumtype);
5485 if (values != error_mark_node)
5487 /* Change the type of the enumerators to be the enum type. We
5488 need to do this irrespective of the size of the enum, for
5489 proper type checking. Replace the DECL_INITIALs of the
5490 enumerators, and the value slots of the list, with copies
5491 that have the enum type; they cannot be modified in place
5492 because they may be shared (e.g. integer_zero_node) Finally,
5493 change the purpose slots to point to the names of the decls. */
5494 for (pair = values; pair; pair = TREE_CHAIN (pair))
5496 tree enu = TREE_PURPOSE (pair);
5497 tree ini = DECL_INITIAL (enu);
5499 TREE_TYPE (enu) = enumtype;
5501 /* The ISO C Standard mandates enumerators to have type int,
5502 even though the underlying type of an enum type is
5503 unspecified. Here we convert any enumerators that fit in
5504 an int to type int, to avoid promotions to unsigned types
5505 when comparing integers with enumerators that fit in the
5506 int range. When -pedantic is given, build_enumerator()
5507 would have already taken care of those that don't fit. */
5508 if (int_fits_type_p (ini, integer_type_node))
5509 tem = integer_type_node;
5512 ini = convert (tem, ini);
5514 DECL_INITIAL (enu) = ini;
5515 TREE_PURPOSE (pair) = DECL_NAME (enu);
5516 TREE_VALUE (pair) = ini;
5519 TYPE_VALUES (enumtype) = values;
5522 /* Record the min/max values so that we can warn about bit-field
5523 enumerations that are too small for the values. */
5524 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5525 lt->enum_min = minnode;
5526 lt->enum_max = maxnode;
5527 TYPE_LANG_SPECIFIC (enumtype) = lt;
5529 /* Fix up all variant types of this enum type. */
5530 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5532 if (tem == enumtype)
5534 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5535 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5536 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5537 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5538 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5539 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5540 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5541 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5542 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5543 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5544 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5547 /* Finish debugging output for this type. */
5548 rest_of_type_compilation (enumtype, toplevel);
5553 /* Build and install a CONST_DECL for one value of the
5554 current enumeration type (one that was begun with start_enum).
5555 Return a tree-list containing the CONST_DECL and its value.
5556 Assignment of sequential values by default is handled here. */
5559 build_enumerator (tree name, tree value)
5563 /* Validate and default VALUE. */
5565 /* Remove no-op casts from the value. */
5567 STRIP_TYPE_NOPS (value);
5571 /* Don't issue more errors for error_mark_node (i.e. an
5572 undeclared identifier) - just ignore the value expression. */
5573 if (value == error_mark_node)
5575 else if (TREE_CODE (value) != INTEGER_CST)
5577 error ("enumerator value for '%E' is not an integer constant", name);
5582 value = default_conversion (value);
5583 constant_expression_warning (value);
5587 /* Default based on previous value. */
5588 /* It should no longer be possible to have NON_LVALUE_EXPR
5592 value = enum_next_value;
5594 error ("overflow in enumeration values");
5597 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5599 pedwarn ("ISO C restricts enumerator values to range of `int'");
5600 /* XXX This causes -pedantic to change the meaning of the program.
5601 Remove? -zw 2004-03-15 */
5602 value = convert (integer_type_node, value);
5605 /* Set basis for default for next value. */
5606 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5607 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5609 /* Now create a declaration for the enum value name. */
5611 type = TREE_TYPE (value);
5612 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5613 TYPE_PRECISION (integer_type_node)),
5614 (TYPE_PRECISION (type)
5615 >= TYPE_PRECISION (integer_type_node)
5616 && TYPE_UNSIGNED (type)));
5618 decl = build_decl (CONST_DECL, name, type);
5619 DECL_INITIAL (decl) = convert (type, value);
5622 return tree_cons (decl, value, NULL_TREE);
5626 /* Create the FUNCTION_DECL for a function definition.
5627 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5628 the declaration; they describe the function's name and the type it returns,
5629 but twisted together in a fashion that parallels the syntax of C.
5631 This function creates a binding context for the function body
5632 as well as setting up the FUNCTION_DECL in current_function_decl.
5634 Returns 1 on success. If the DECLARATOR is not suitable for a function
5635 (it defines a datum instead), we return 0, which tells
5636 yyparse to report a parse error. */
5639 start_function (tree declspecs, tree declarator, tree attributes)
5641 tree decl1, old_decl;
5644 current_function_returns_value = 0; /* Assume, until we see it does. */
5645 current_function_returns_null = 0;
5646 current_function_returns_abnormally = 0;
5647 warn_about_return_type = 0;
5648 current_extern_inline = 0;
5649 c_switch_stack = NULL;
5651 /* Indicate no valid break/continue context by setting these variables
5652 to some non-null, non-label value. We'll notice and emit the proper
5653 error message in c_finish_bc_stmt. */
5654 c_break_label = c_cont_label = size_zero_node;
5656 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5658 /* If the declarator is not suitable for a function definition,
5659 cause a syntax error. */
5663 decl_attributes (&decl1, attributes, 0);
5665 if (DECL_DECLARED_INLINE_P (decl1)
5666 && DECL_UNINLINABLE (decl1)
5667 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5668 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5670 announce_function (decl1);
5672 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5674 error ("return type is an incomplete type");
5675 /* Make it return void instead. */
5677 = build_function_type (void_type_node,
5678 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5681 if (warn_about_return_type)
5682 pedwarn_c99 ("return type defaults to `int'");
5684 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5685 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5686 DECL_INITIAL (decl1) = error_mark_node;
5688 /* If this definition isn't a prototype and we had a prototype declaration
5689 before, copy the arg type info from that prototype.
5690 But not if what we had before was a builtin function. */
5691 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5692 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5693 && !DECL_BUILT_IN (old_decl)
5694 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5695 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5696 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5698 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5699 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5702 /* Optionally warn of old-fashioned def with no previous prototype. */
5703 if (warn_strict_prototypes
5704 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5705 && C_DECL_ISNT_PROTOTYPE (old_decl))
5706 warning ("function declaration isn't a prototype");
5707 /* Optionally warn of any global def with no previous prototype. */
5708 else if (warn_missing_prototypes
5709 && TREE_PUBLIC (decl1)
5710 && ! MAIN_NAME_P (DECL_NAME (decl1))
5711 && C_DECL_ISNT_PROTOTYPE (old_decl))
5712 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5713 /* Optionally warn of any def with no previous prototype
5714 if the function has already been used. */
5715 else if (warn_missing_prototypes
5716 && old_decl != 0 && TREE_USED (old_decl)
5717 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5718 warning ("%J'%D' was used with no prototype before its definition",
5720 /* Optionally warn of any global def with no previous declaration. */
5721 else if (warn_missing_declarations
5722 && TREE_PUBLIC (decl1)
5724 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5725 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5726 /* Optionally warn of any def with no previous declaration
5727 if the function has already been used. */
5728 else if (warn_missing_declarations
5729 && old_decl != 0 && TREE_USED (old_decl)
5730 && C_DECL_IMPLICIT (old_decl))
5731 warning ("%J`%D' was used with no declaration before its definition",
5734 /* This is a definition, not a reference.
5735 So normally clear DECL_EXTERNAL.
5736 However, `extern inline' acts like a declaration
5737 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5738 DECL_EXTERNAL (decl1) = current_extern_inline;
5740 /* This function exists in static storage.
5741 (This does not mean `static' in the C sense!) */
5742 TREE_STATIC (decl1) = 1;
5744 /* A nested function is not global. */
5745 if (current_function_decl != 0)
5746 TREE_PUBLIC (decl1) = 0;
5748 #ifdef ENABLE_CHECKING
5749 /* This is the earliest point at which we might know the assembler
5750 name of the function. Thus, if it's set before this, die horribly. */
5751 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5755 /* If #pragma weak was used, mark the decl weak now. */
5756 if (current_scope == file_scope)
5757 maybe_apply_pragma_weak (decl1);
5759 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5760 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5765 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5766 != integer_type_node)
5767 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5769 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5770 args = TREE_CHAIN (args))
5772 tree type = args ? TREE_VALUE (args) : 0;
5774 if (type == void_type_node)
5781 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5782 pedwarn ("%Jfirst argument of '%D' should be `int'",
5787 if (TREE_CODE (type) != POINTER_TYPE
5788 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5789 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5791 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5796 if (TREE_CODE (type) != POINTER_TYPE
5797 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5798 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5800 pedwarn ("%Jthird argument of '%D' should probably be "
5801 "'char **'", decl1, decl1);
5806 /* It is intentional that this message does not mention the third
5807 argument because it's only mentioned in an appendix of the
5809 if (argct > 0 && (argct < 2 || argct > 3))
5810 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5812 if (! TREE_PUBLIC (decl1))
5813 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5816 /* Record the decl so that the function name is defined.
5817 If we already have a decl for this name, and it is a FUNCTION_DECL,
5818 use the old decl. */
5820 current_function_decl = pushdecl (decl1);
5823 declare_parm_level ();
5825 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5826 /* Promote the value to int before returning it. */
5827 if (c_promoting_integer_type_p (restype))
5829 /* It retains unsignedness if not really getting wider. */
5830 if (TYPE_UNSIGNED (restype)
5831 && (TYPE_PRECISION (restype)
5832 == TYPE_PRECISION (integer_type_node)))
5833 restype = unsigned_type_node;
5835 restype = integer_type_node;
5837 DECL_RESULT (current_function_decl)
5838 = build_decl (RESULT_DECL, NULL_TREE, restype);
5840 start_fname_decls ();
5845 /* Subroutine of store_parm_decls which handles new-style function
5846 definitions (prototype format). The parms already have decls, so we
5847 need only record them as in effect and complain if any redundant
5848 old-style parm decls were written. */
5850 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5853 tree parms = ARG_INFO_PARMS (arg_info);
5854 tree tags = ARG_INFO_TAGS (arg_info);
5855 tree others = ARG_INFO_OTHERS (arg_info);
5857 if (current_scope->bindings)
5859 error ("%Jold-style parameter declarations in prototyped "
5860 "function definition", fndecl);
5862 /* Get rid of the old-style declarations. */
5866 /* Don't issue this warning for nested functions, and don't issue this
5867 warning if we got here because ARG_INFO_TYPES was error_mark_node
5868 (this happens when a function definition has just an ellipsis in
5869 its parameter list). */
5870 else if (warn_traditional && !in_system_header && !current_function_scope
5871 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5872 warning ("%Jtraditional C rejects ISO C style function definitions",
5875 /* Now make all the parameter declarations visible in the function body.
5876 We can bypass most of the grunt work of pushdecl. */
5877 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5879 DECL_CONTEXT (decl) = current_function_decl;
5880 if (DECL_NAME (decl))
5881 bind (DECL_NAME (decl), decl, current_scope,
5882 /*invisible=*/false, /*nested=*/false);
5884 error ("%Jparameter name omitted", decl);
5887 /* Record the parameter list in the function declaration. */
5888 DECL_ARGUMENTS (fndecl) = parms;
5890 /* Now make all the ancillary declarations visible, likewise. */
5891 for (decl = others; decl; decl = TREE_CHAIN (decl))
5893 DECL_CONTEXT (decl) = current_function_decl;
5894 if (DECL_NAME (decl))
5895 bind (DECL_NAME (decl), decl, current_scope,
5896 /*invisible=*/false, /*nested=*/false);
5899 /* And all the tag declarations. */
5900 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5901 if (TREE_PURPOSE (decl))
5902 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
5903 /*invisible=*/false, /*nested=*/false);
5906 /* Subroutine of store_parm_decls which handles old-style function
5907 definitions (separate parameter list and declarations). */
5910 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5912 struct c_binding *b;
5913 tree parm, decl, last;
5914 tree parmids = ARG_INFO_PARMS (arg_info);
5916 /* We use DECL_WEAK as a flag to show which parameters have been
5917 seen already, since it is not used on PARM_DECL. */
5918 #ifdef ENABLE_CHECKING
5919 for (b = current_scope->bindings; b; b = b->prev)
5920 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5924 if (warn_old_style_definition && !in_system_header)
5925 warning ("%Jold-style function definition", fndecl);
5927 /* Match each formal parameter name with its declaration. Save each
5928 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5929 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5931 if (TREE_VALUE (parm) == 0)
5933 error ("%Jparameter name missing from parameter list", fndecl);
5934 TREE_PURPOSE (parm) = 0;
5938 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5939 if (b && B_IN_CURRENT_SCOPE (b))
5942 /* If we got something other than a PARM_DECL it is an error. */
5943 if (TREE_CODE (decl) != PARM_DECL)
5944 error ("%J'%D' declared as a non-parameter", decl, decl);
5945 /* If the declaration is already marked, we have a duplicate
5946 name. Complain and ignore the duplicate. */
5947 else if (DECL_WEAK (decl))
5949 error ("%Jmultiple parameters named '%D'", decl, decl);
5950 TREE_PURPOSE (parm) = 0;
5953 /* If the declaration says "void", complain and turn it into
5955 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5957 error ("%Jparameter '%D' declared with void type", decl, decl);
5958 TREE_TYPE (decl) = integer_type_node;
5959 DECL_ARG_TYPE (decl) = integer_type_node;
5960 layout_decl (decl, 0);
5963 /* If no declaration found, default to int. */
5966 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5967 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5968 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5972 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5973 else if (extra_warnings)
5974 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5977 TREE_PURPOSE (parm) = decl;
5978 DECL_WEAK (decl) = 1;
5981 /* Now examine the parms chain for incomplete declarations
5982 and declarations with no corresponding names. */
5984 for (b = current_scope->bindings; b; b = b->prev)
5987 if (TREE_CODE (parm) != PARM_DECL)
5990 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5992 error ("%Jparameter '%D' has incomplete type", parm, parm);
5993 TREE_TYPE (parm) = error_mark_node;
5996 if (! DECL_WEAK (parm))
5998 error ("%Jdeclaration for parameter '%D' but no such parameter",
6001 /* Pretend the parameter was not missing.
6002 This gets us to a standard state and minimizes
6003 further error messages. */
6004 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6008 /* Chain the declarations together in the order of the list of
6009 names. Store that chain in the function decl, replacing the
6010 list of names. Update the current scope to match. */
6011 DECL_ARGUMENTS (fndecl) = 0;
6013 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6014 if (TREE_PURPOSE (parm))
6016 if (parm && TREE_PURPOSE (parm))
6018 last = TREE_PURPOSE (parm);
6019 DECL_ARGUMENTS (fndecl) = last;
6020 DECL_WEAK (last) = 0;
6022 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6023 if (TREE_PURPOSE (parm))
6025 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6026 last = TREE_PURPOSE (parm);
6027 DECL_WEAK (last) = 0;
6029 TREE_CHAIN (last) = 0;
6032 /* If there was a previous prototype,
6033 set the DECL_ARG_TYPE of each argument according to
6034 the type previously specified, and report any mismatches. */
6036 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6039 for (parm = DECL_ARGUMENTS (fndecl),
6040 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6041 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6042 != void_type_node));
6043 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6045 if (parm == 0 || type == 0
6046 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6048 error ("number of arguments doesn't match prototype");
6049 error ("%Hprototype declaration",
6050 ¤t_function_prototype_locus);
6053 /* Type for passing arg must be consistent with that
6054 declared for the arg. ISO C says we take the unqualified
6055 type for parameters declared with qualified type. */
6056 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6057 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6059 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6060 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6062 /* Adjust argument to match prototype. E.g. a previous
6063 `int foo(float);' prototype causes
6064 `int foo(x) float x; {...}' to be treated like
6065 `int foo(float x) {...}'. This is particularly
6066 useful for argument types like uid_t. */
6067 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6069 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6070 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6071 && TYPE_PRECISION (TREE_TYPE (parm))
6072 < TYPE_PRECISION (integer_type_node))
6073 DECL_ARG_TYPE (parm) = integer_type_node;
6077 pedwarn ("promoted argument '%D' "
6078 "doesn't match prototype", parm);
6079 pedwarn ("%Hprototype declaration",
6080 ¤t_function_prototype_locus);
6085 error ("argument '%D' doesn't match prototype", parm);
6086 error ("%Hprototype declaration",
6087 ¤t_function_prototype_locus);
6091 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6094 /* Otherwise, create a prototype that would match. */
6098 tree actual = 0, last = 0, type;
6100 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6102 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6104 TREE_CHAIN (last) = type;
6109 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6111 TREE_CHAIN (last) = type;
6115 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6116 of the type of this function, but we need to avoid having this
6117 affect the types of other similarly-typed functions, so we must
6118 first force the generation of an identical (but separate) type
6119 node for the relevant function type. The new node we create
6120 will be a variant of the main variant of the original function
6123 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6125 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6129 /* Store the parameter declarations into the current function declaration.
6130 This is called after parsing the parameter declarations, before
6131 digesting the body of the function.
6133 For an old-style definition, construct a prototype out of the old-style
6134 parameter declarations and inject it into the function's type. */
6137 store_parm_decls (void)
6139 tree fndecl = current_function_decl;
6141 /* The argument information block for FNDECL. */
6142 tree arg_info = DECL_ARGUMENTS (fndecl);
6144 /* True if this definition is written with a prototype. Note:
6145 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6146 list in a function definition as equivalent to (void) -- an
6147 empty argument list specifies the function has no parameters,
6148 but only (void) sets up a prototype for future calls. */
6149 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6152 store_parm_decls_newstyle (fndecl, arg_info);
6154 store_parm_decls_oldstyle (fndecl, arg_info);
6156 /* The next call to push_scope will be a function body. */
6158 next_is_function_body = true;
6160 /* Write a record describing this function definition to the prototypes
6161 file (if requested). */
6163 gen_aux_info_record (fndecl, 1, 0, proto);
6165 /* Initialize the RTL code for the function. */
6166 allocate_struct_function (fndecl);
6168 /* Begin the statement tree for this function. */
6169 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6171 /* ??? Insert the contents of the pending sizes list into the function
6172 to be evaluated. This just changes mis-behaviour until assign_parms
6173 phase ordering problems are resolved. */
6176 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6177 add_stmt (TREE_VALUE (t));
6180 /* Even though we're inside a function body, we still don't want to
6181 call expand_expr to calculate the size of a variable-sized array.
6182 We haven't necessarily assigned RTL to all variables yet, so it's
6183 not safe to try to expand expressions involving them. */
6184 cfun->x_dont_save_pending_sizes_p = 1;
6187 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6190 c_finalize (tree fndecl)
6192 struct cgraph_node *cgn;
6194 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6195 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6197 /* ??? Objc emits functions after finalizing the compilation unit.
6198 This should be cleaned up later and this conditional removed. */
6199 if (cgraph_global_info_ready)
6201 c_expand_body (fndecl);
6205 /* Finalize all nested functions now. */
6206 cgn = cgraph_node (fndecl);
6207 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6208 c_finalize (cgn->decl);
6210 cgraph_finalize_function (fndecl, false);
6213 /* Finish up a function declaration and compile that function
6214 all the way to assembler language output. The free the storage
6215 for the function definition.
6217 This is called after parsing the body of the function definition. */
6220 finish_function (void)
6222 tree fndecl = current_function_decl;
6224 if (TREE_CODE (fndecl) == FUNCTION_DECL
6225 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6227 tree args = DECL_ARGUMENTS (fndecl);
6228 for (; args; args = TREE_CHAIN (args))
6230 tree type = TREE_TYPE (args);
6231 if (INTEGRAL_TYPE_P (type)
6232 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6233 DECL_ARG_TYPE (args) = integer_type_node;
6237 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6238 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6240 /* Must mark the RESULT_DECL as being in this function. */
6242 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6243 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6245 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6247 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6248 != integer_type_node)
6250 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6251 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6253 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6258 c_finish_return (integer_zero_node);
6262 /* Tie off the statement tree for this function. */
6263 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6265 finish_fname_decls ();
6267 /* Complain if there's just no return statement. */
6268 if (warn_return_type
6269 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6270 && !current_function_returns_value && !current_function_returns_null
6271 /* Don't complain if we abort. */
6272 && !current_function_returns_abnormally
6273 /* Don't warn for main(). */
6274 && !MAIN_NAME_P (DECL_NAME (fndecl))
6275 /* Or if they didn't actually specify a return type. */
6276 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6277 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6278 inline function, as we might never be compiled separately. */
6279 && DECL_INLINE (fndecl))
6280 warning ("no return statement in function returning non-void");
6282 /* With just -Wextra, complain only if function returns both with
6283 and without a value. */
6285 && current_function_returns_value
6286 && current_function_returns_null)
6287 warning ("this function may return with or without a value");
6289 /* Store the end of the function, so that we get good line number
6290 info for the epilogue. */
6291 cfun->function_end_locus = input_location;
6293 /* If we don't have ctors/dtors sections, and this is a static
6294 constructor or destructor, it must be recorded now. */
6295 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6296 && !targetm.have_ctors_dtors)
6297 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6298 if (DECL_STATIC_DESTRUCTOR (fndecl)
6299 && !targetm.have_ctors_dtors)
6300 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6302 /* Genericize before inlining. Delay genericizing nested functions
6303 until their parent function is genericized. Since finalizing
6304 requires GENERIC, delay that as well. */
6306 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6308 if (!decl_function_context (fndecl))
6310 c_genericize (fndecl);
6311 lower_nested_functions (fndecl);
6312 c_finalize (fndecl);
6316 /* Register this function with cgraph just far enough to get it
6317 added to our parent's nested function list. Handy, since the
6318 C front end doesn't have such a list. */
6319 (void) cgraph_node (fndecl);
6323 /* We're leaving the context of this function, so zap cfun.
6324 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6325 tree_rest_of_compilation. */
6327 current_function_decl = NULL;
6330 /* Generate the RTL for the body of FNDECL. */
6333 c_expand_body (tree fndecl)
6336 if (!DECL_INITIAL (fndecl)
6337 || DECL_INITIAL (fndecl) == error_mark_node)
6340 tree_rest_of_compilation (fndecl, false);
6342 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6343 && targetm.have_ctors_dtors)
6344 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6345 DEFAULT_INIT_PRIORITY);
6346 if (DECL_STATIC_DESTRUCTOR (fndecl)
6347 && targetm.have_ctors_dtors)
6348 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6349 DEFAULT_INIT_PRIORITY);
6352 /* Check the declarations given in a for-loop for satisfying the C99
6355 check_for_loop_decls (void)
6357 struct c_binding *b;
6361 /* If we get here, declarations have been used in a for loop without
6362 the C99 for loop scope. This doesn't make much sense, so don't
6364 error ("'for' loop initial declaration used outside C99 mode");
6367 /* C99 subclause 6.8.5 paragraph 3:
6369 [#3] The declaration part of a for statement shall only
6370 declare identifiers for objects having storage class auto or
6373 It isn't clear whether, in this sentence, "identifiers" binds to
6374 "shall only declare" or to "objects" - that is, whether all identifiers
6375 declared must be identifiers for objects, or whether the restriction
6376 only applies to those that are. (A question on this in comp.std.c
6377 in November 2000 received no answer.) We implement the strictest
6378 interpretation, to avoid creating an extension which later causes
6381 for (b = current_scope->bindings; b; b = b->prev)
6384 tree decl = b->decl;
6389 switch (TREE_CODE (decl))
6392 if (TREE_STATIC (decl))
6393 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6394 "initial declaration", decl, decl);
6395 else if (DECL_EXTERNAL (decl))
6396 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6397 "initial declaration", decl, decl);
6401 error ("'struct %E' declared in 'for' loop initial declaration", id);
6404 error ("'union %E' declared in 'for' loop initial declaration", id);
6407 error ("'enum %E' declared in 'for' loop initial declaration", id);
6410 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6411 "initial declaration", decl, decl);
6416 /* Save and reinitialize the variables
6417 used during compilation of a C function. */
6420 c_push_function_context (struct function *f)
6422 struct language_function *p;
6423 p = ggc_alloc (sizeof (struct language_function));
6426 p->base.x_stmt_tree = c_stmt_tree;
6427 p->x_break_label = c_break_label;
6428 p->x_cont_label = c_cont_label;
6429 p->x_switch_stack = c_switch_stack;
6430 p->returns_value = current_function_returns_value;
6431 p->returns_null = current_function_returns_null;
6432 p->returns_abnormally = current_function_returns_abnormally;
6433 p->warn_about_return_type = warn_about_return_type;
6434 p->extern_inline = current_extern_inline;
6437 /* Restore the variables used during compilation of a C function. */
6440 c_pop_function_context (struct function *f)
6442 struct language_function *p = f->language;
6444 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6445 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6447 /* Stop pointing to the local nodes about to be freed. */
6448 /* But DECL_INITIAL must remain nonzero so we know this
6449 was an actual function definition. */
6450 DECL_INITIAL (current_function_decl) = error_mark_node;
6451 DECL_ARGUMENTS (current_function_decl) = 0;
6454 c_stmt_tree = p->base.x_stmt_tree;
6455 c_break_label = p->x_break_label;
6456 c_cont_label = p->x_cont_label;
6457 c_switch_stack = p->x_switch_stack;
6458 current_function_returns_value = p->returns_value;
6459 current_function_returns_null = p->returns_null;
6460 current_function_returns_abnormally = p->returns_abnormally;
6461 warn_about_return_type = p->warn_about_return_type;
6462 current_extern_inline = p->extern_inline;
6467 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6470 c_dup_lang_specific_decl (tree decl)
6472 struct lang_decl *ld;
6474 if (!DECL_LANG_SPECIFIC (decl))
6477 ld = ggc_alloc (sizeof (struct lang_decl));
6478 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6479 DECL_LANG_SPECIFIC (decl) = ld;
6482 /* The functions below are required for functionality of doing
6483 function at once processing in the C front end. Currently these
6484 functions are not called from anywhere in the C front end, but as
6485 these changes continue, that will change. */
6487 /* Returns nonzero if the current statement is a full expression,
6488 i.e. temporaries created during that statement should be destroyed
6489 at the end of the statement. */
6492 stmts_are_full_exprs_p (void)
6497 /* Returns the stmt_tree (if any) to which statements are currently
6498 being added. If there is no active statement-tree, NULL is
6502 current_stmt_tree (void)
6504 return &c_stmt_tree;
6507 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6511 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6516 /* Dummy function in place of callback used by C++. */
6519 extract_interface_info (void)
6523 /* Return the global value of T as a symbol. */
6526 identifier_global_value (tree t)
6528 struct c_binding *b;
6530 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6531 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6537 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6538 otherwise the name is found in ridpointers from RID_INDEX. */
6541 record_builtin_type (enum rid rid_index, const char *name, tree type)
6545 id = ridpointers[(int) rid_index];
6547 id = get_identifier (name);
6548 pushdecl (build_decl (TYPE_DECL, id, type));
6551 /* Build the void_list_node (void_type_node having been created). */
6553 build_void_list_node (void)
6555 tree t = build_tree_list (NULL_TREE, void_type_node);
6559 /* Return something to represent absolute declarators containing a *.
6560 TARGET is the absolute declarator that the * contains.
6561 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6562 to apply to the pointer type, represented as identifiers, possible mixed
6565 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6566 if attributes are present) and whose type is the modifier list. */
6569 make_pointer_declarator (tree type_quals_attrs, tree target)
6572 tree itarget = target;
6573 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6574 if (attrs != NULL_TREE)
6575 itarget = tree_cons (attrs, target, NULL_TREE);
6576 return build1 (INDIRECT_REF, quals, itarget);
6579 /* A wrapper around lhd_set_decl_assembler_name that gives static
6580 variables their C names if they are at file scope and only one
6581 translation unit is being compiled, for backwards compatibility
6582 with certain bizarre assembler hacks (like crtstuff.c). */
6585 c_static_assembler_name (tree decl)
6587 if (num_in_fnames == 1
6588 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6589 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6590 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6592 lhd_set_decl_assembler_name (decl);
6595 /* Perform final processing on file-scope data. */
6597 c_write_global_declarations_1 (tree globals)
6599 size_t len = list_length (globals);
6600 tree *vec = xmalloc (sizeof (tree) * len);
6604 /* Process the decls in the order they were written. */
6605 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6608 wrapup_global_declarations (vec, len);
6609 check_global_declarations (vec, len);
6615 c_write_global_declarations (void)
6619 /* We don't want to do this if generating a PCH. */
6623 /* Process all file scopes in this compilation. */
6624 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6625 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6627 /* Now do the same for the externals scope. */
6630 c_write_global_declarations_1 (BLOCK_VARS (t));
6633 #include "gt-c-decl.h"