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 argument information structure for the function currently being
110 static GTY(()) tree current_function_arg_info;
112 /* The current statement tree. */
114 static GTY(()) struct stmt_tree_s c_stmt_tree;
116 /* State saving variables. */
120 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
121 included in this invocation. Note that the current translation
122 unit is not included in this list. */
124 static GTY(()) tree all_translation_units;
126 /* A list of decls to be made automatically visible in each file scope. */
127 static GTY(()) tree visible_builtins;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement that specifies a return value is seen. */
132 int current_function_returns_value;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a return statement with no argument is seen. */
137 int current_function_returns_null;
139 /* Set to 0 at beginning of a function definition, set to 1 if
140 a call to a noreturn function is seen. */
142 int current_function_returns_abnormally;
144 /* Set to nonzero by `grokdeclarator' for a function
145 whose return type is defaulted, if warnings for this are desired. */
147 static int warn_about_return_type;
149 /* Nonzero when starting a function declared `extern inline'. */
151 static int current_extern_inline;
153 /* True means global_bindings_p should return false even if the scope stack
154 says we are in file scope. */
155 bool c_override_global_bindings_to_false;
158 /* Each c_binding structure describes one binding of an identifier to
159 a decl. All the decls in a scope - irrespective of namespace - are
160 chained together by the ->prev field, which (as the name implies)
161 runs in reverse order. All the decls in a given namespace bound to
162 a given identifier are chained by the ->shadowed field, which runs
163 from inner to outer scopes.
165 The ->decl field usually points to a DECL node, but there are two
166 exceptions. In the namespace of type tags, the bound entity is a
167 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
168 identifier is encountered, it is bound to error_mark_node to
169 suppress further errors about that identifier in the current
172 The ->type field stores the type of the declaration in this scope;
173 if NULL, the type is the type of the ->decl field. This is only of
174 relevance for objects with external or internal linkage which may
175 be redeclared in inner scopes, forming composite types that only
176 persist for the duration of those scopes. In the external scope,
177 this stores the composite of all the types declared for this
178 object, visible or not. The ->inner_comp field (used only at file
179 scope) stores whether an incomplete array type at file scope was
180 completed at an inner scope to an array size other than 1.
182 The depth field is copied from the scope structure that holds this
183 decl. It is used to preserve the proper ordering of the ->shadowed
184 field (see bind()) and also for a handful of special-case checks.
185 Finally, the invisible bit is true for a decl which should be
186 ignored for purposes of normal name lookup, and the nested bit is
187 true for a decl that's been bound a second time in an inner scope;
188 in all such cases, the binding in the outer scope will have its
189 invisible bit true. */
191 struct c_binding GTY((chain_next ("%h.prev")))
193 tree decl; /* the decl bound */
194 tree type; /* the type in this scope */
195 tree id; /* the identifier it's bound to */
196 struct c_binding *prev; /* the previous decl in this scope */
197 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
198 unsigned int depth : 28; /* depth of this scope */
199 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
200 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
201 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
204 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
205 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
206 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
207 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
209 #define I_SYMBOL_BINDING(node) \
210 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
211 #define I_SYMBOL_DECL(node) \
212 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
214 #define I_TAG_BINDING(node) \
215 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
216 #define I_TAG_DECL(node) \
217 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
219 #define I_LABEL_BINDING(node) \
220 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
221 #define I_LABEL_DECL(node) \
222 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
224 /* Each C symbol points to three linked lists of c_binding structures.
225 These describe the values of the identifier in the three different
226 namespaces defined by the language. */
228 struct lang_identifier GTY(())
230 struct c_common_identifier common_id;
231 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
232 struct c_binding *tag_binding; /* struct/union/enum tags */
233 struct c_binding *label_binding; /* labels */
236 /* Validate c-lang.c's assumptions. */
237 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
238 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
240 /* The resulting tree type. */
243 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
244 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)")))
246 union tree_node GTY ((tag ("0"),
247 desc ("tree_node_structure (&%h)")))
249 struct lang_identifier GTY ((tag ("1"))) identifier;
252 /* Each c_scope structure describes the complete contents of one
253 scope. Four scopes are distinguished specially: the innermost or
254 current scope, the innermost function scope, the file scope (always
255 the second to outermost) and the outermost or external scope.
257 Most declarations are recorded in the current scope.
259 All normal label declarations are recorded in the innermost
260 function scope, as are bindings of undeclared identifiers to
261 error_mark_node. (GCC permits nested functions as an extension,
262 hence the 'innermost' qualifier.) Explicitly declared labels
263 (using the __label__ extension) appear in the current scope.
265 Being in the file scope (current_scope == file_scope) causes
266 special behavior in several places below. Also, under some
267 conditions the Objective-C front end records declarations in the
268 file scope even though that isn't the current scope.
270 All declarations with external linkage are recorded in the external
271 scope, even if they aren't visible there; this models the fact that
272 such declarations are visible to the entire program, and (with a
273 bit of cleverness, see pushdecl) allows diagnosis of some violations
274 of C99 6.2.2p7 and 6.2.7p2:
276 If, within the same translation unit, the same identifier appears
277 with both internal and external linkage, the behavior is
280 All declarations that refer to the same object or function shall
281 have compatible type; otherwise, the behavior is undefined.
283 Initially only the built-in declarations, which describe compiler
284 intrinsic functions plus a subset of the standard library, are in
287 The order of the blocks list matters, and it is frequently appended
288 to. To avoid having to walk all the way to the end of the list on
289 each insertion, or reverse the list later, we maintain a pointer to
290 the last list entry. (FIXME: It should be feasible to use a reversed
293 The bindings list is strictly in reverse order of declarations;
294 pop_scope relies on this. */
297 struct c_scope GTY((chain_next ("%h.outer")))
299 /* The scope containing this one. */
300 struct c_scope *outer;
302 /* The next outermost function scope. */
303 struct c_scope *outer_function;
305 /* All bindings in this scope. */
306 struct c_binding *bindings;
308 /* For each scope (except the global one), a chain of BLOCK nodes
309 for all the scopes that were entered and exited one level down. */
313 /* The depth of this scope. Used to keep the ->shadowed chain of
314 bindings sorted innermost to outermost. */
315 unsigned int depth : 28;
317 /* True if we are currently filling this scope with parameter
319 BOOL_BITFIELD parm_flag : 1;
321 /* True if we already complained about forward parameter decls
322 in this scope. This prevents double warnings on
323 foo (int a; int b; ...) */
324 BOOL_BITFIELD warned_forward_parm_decls : 1;
326 /* True if this is the outermost block scope of a function body.
327 This scope contains the parameters, the local variables declared
328 in the outermost block, and all the labels (except those in
329 nested functions, or declared at block scope with __label__). */
330 BOOL_BITFIELD function_body : 1;
332 /* True means make a BLOCK for this scope no matter what. */
333 BOOL_BITFIELD keep : 1;
336 /* The scope currently in effect. */
338 static GTY(()) struct c_scope *current_scope;
340 /* The innermost function scope. Ordinary (not explicitly declared)
341 labels, bindings to error_mark_node, and the lazily-created
342 bindings of __func__ and its friends get this scope. */
344 static GTY(()) struct c_scope *current_function_scope;
346 /* The C file scope. This is reset for each input translation unit. */
348 static GTY(()) struct c_scope *file_scope;
350 /* The outermost scope. This is used for all declarations with
351 external linkage, and only these, hence the name. */
353 static GTY(()) struct c_scope *external_scope;
355 /* A chain of c_scope structures awaiting reuse. */
357 static GTY((deletable)) struct c_scope *scope_freelist;
359 /* A chain of c_binding structures awaiting reuse. */
361 static GTY((deletable)) struct c_binding *binding_freelist;
363 /* Append VAR to LIST in scope SCOPE. */
364 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
365 struct c_scope *s_ = (scope); \
367 if (s_->list##_last) \
368 TREE_CHAIN (s_->list##_last) = d_; \
371 s_->list##_last = d_; \
374 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
375 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
376 struct c_scope *t_ = (tscope); \
377 struct c_scope *f_ = (fscope); \
379 TREE_CHAIN (t_->to##_last) = f_->from; \
382 t_->to##_last = f_->from##_last; \
385 /* True means unconditionally make a BLOCK for the next scope pushed. */
387 static bool keep_next_level_flag;
389 /* True means the next call to push_scope will be the outermost scope
390 of a function body, so do not push a new scope, merely cease
391 expecting parameter decls. */
393 static bool next_is_function_body;
395 /* Functions called automatically at the beginning and end of execution. */
397 static GTY(()) tree static_ctors;
398 static GTY(()) tree static_dtors;
400 /* Forward declarations. */
401 static tree lookup_name_in_scope (tree, struct c_scope *);
402 static tree c_make_fname_decl (tree, int);
403 static tree grokdeclarator (tree, tree, enum decl_context, bool, tree *);
404 static tree grokparms (tree, bool);
405 static void layout_array_type (tree);
407 /* States indicating how grokdeclarator() should handle declspecs marked
408 with __attribute__((deprecated)). An object declared as
409 __attribute__((deprecated)) suppresses warnings of uses of other
412 enum deprecated_states {
417 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
420 c_print_identifier (FILE *file, tree node, int indent)
422 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
423 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
424 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
425 if (C_IS_RESERVED_WORD (node))
427 tree rid = ridpointers[C_RID_CODE (node)];
428 indent_to (file, indent + 4);
429 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
430 (void *) rid, IDENTIFIER_POINTER (rid));
434 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
435 which may be any of several kinds of DECL or TYPE or error_mark_node,
436 in the scope SCOPE. */
438 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
440 struct c_binding *b, **here;
442 if (binding_freelist)
444 b = binding_freelist;
445 binding_freelist = b->prev;
448 b = GGC_NEW (struct c_binding);
453 b->depth = scope->depth;
454 b->invisible = invisible;
460 b->prev = scope->bindings;
466 switch (TREE_CODE (decl))
468 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
471 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
477 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
483 /* Locate the appropriate place in the chain of shadowed decls
484 to insert this binding. Normally, scope == current_scope and
485 this does nothing. */
486 while (*here && (*here)->depth > scope->depth)
487 here = &(*here)->shadowed;
493 /* Clear the binding structure B, stick it on the binding_freelist,
494 and return the former value of b->prev. This is used by pop_scope
495 and get_parm_info to iterate destructively over all the bindings
496 from a given scope. */
497 static struct c_binding *
498 free_binding_and_advance (struct c_binding *b)
500 struct c_binding *prev = b->prev;
502 memset (b, 0, sizeof (struct c_binding));
503 b->prev = binding_freelist;
504 binding_freelist = b;
510 /* Hook called at end of compilation to assume 1 elt
511 for a file-scope tentative array defn that wasn't complete before. */
514 c_finish_incomplete_decl (tree decl)
516 if (TREE_CODE (decl) == VAR_DECL)
518 tree type = TREE_TYPE (decl);
519 if (type != error_mark_node
520 && TREE_CODE (type) == ARRAY_TYPE
521 && ! DECL_EXTERNAL (decl)
522 && TYPE_DOMAIN (type) == 0)
524 warning ("%Jarray '%D' assumed to have one element", decl, decl);
526 complete_array_type (type, NULL_TREE, 1);
528 layout_decl (decl, 0);
533 /* The Objective-C front-end often needs to determine the current scope. */
536 objc_get_current_scope (void)
538 return current_scope;
541 /* The following function is used only by Objective-C. It needs to live here
542 because it accesses the innards of c_scope. */
545 objc_mark_locals_volatile (void *enclosing_blk)
547 struct c_scope *scope;
550 for (scope = current_scope;
551 scope && scope != enclosing_blk;
552 scope = scope->outer)
554 for (b = scope->bindings; b; b = b->prev)
556 if (TREE_CODE (b->decl) == VAR_DECL
557 || TREE_CODE (b->decl) == PARM_DECL)
559 C_DECL_REGISTER (b->decl) = 0;
560 DECL_REGISTER (b->decl) = 0;
561 TREE_THIS_VOLATILE (b->decl) = 1;
565 /* Do not climb up past the current function. */
566 if (scope->function_body)
571 /* Nonzero if we are currently in file scope. */
574 global_bindings_p (void)
576 return current_scope == file_scope && !c_override_global_bindings_to_false;
580 keep_next_level (void)
582 keep_next_level_flag = true;
585 /* Identify this scope as currently being filled with parameters. */
588 declare_parm_level (void)
590 current_scope->parm_flag = true;
596 if (next_is_function_body)
598 /* This is the transition from the parameters to the top level
599 of the function body. These are the same scope
600 (C99 6.2.1p4,6) so we do not push another scope structure.
601 next_is_function_body is set only by store_parm_decls, which
602 in turn is called when and only when we are about to
603 encounter the opening curly brace for the function body.
605 The outermost block of a function always gets a BLOCK node,
606 because the debugging output routines expect that each
607 function has at least one BLOCK. */
608 current_scope->parm_flag = false;
609 current_scope->function_body = true;
610 current_scope->keep = true;
611 current_scope->outer_function = current_function_scope;
612 current_function_scope = current_scope;
614 keep_next_level_flag = false;
615 next_is_function_body = false;
619 struct c_scope *scope;
622 scope = scope_freelist;
623 scope_freelist = scope->outer;
626 scope = GGC_CNEW (struct c_scope);
628 scope->keep = keep_next_level_flag;
629 scope->outer = current_scope;
630 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
632 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
634 if (current_scope && scope->depth == 0)
637 sorry ("GCC supports only %u nested scopes\n", scope->depth);
640 current_scope = scope;
641 keep_next_level_flag = false;
645 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
648 set_type_context (tree type, tree context)
650 for (type = TYPE_MAIN_VARIANT (type); type;
651 type = TYPE_NEXT_VARIANT (type))
652 TYPE_CONTEXT (type) = context;
655 /* Exit a scope. Restore the state of the identifier-decl mappings
656 that were in effect when this scope was entered. Return a BLOCK
657 node containing all the DECLs in this scope that are of interest
658 to debug info generation. */
663 struct c_scope *scope = current_scope;
664 tree block, context, p;
667 bool functionbody = scope->function_body;
668 bool keep = functionbody || scope->keep || scope->bindings;
670 /* If appropriate, create a BLOCK to record the decls for the life
675 block = make_node (BLOCK);
676 BLOCK_SUBBLOCKS (block) = scope->blocks;
677 TREE_USED (block) = 1;
679 /* In each subblock, record that this is its superior. */
680 for (p = scope->blocks; p; p = TREE_CHAIN (p))
681 BLOCK_SUPERCONTEXT (p) = block;
683 BLOCK_VARS (block) = 0;
686 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
687 scope must be set so that they point to the appropriate
688 construct, i.e. either to the current FUNCTION_DECL node, or
689 else to the BLOCK node we just constructed.
691 Note that for tagged types whose scope is just the formal
692 parameter list for some function type specification, we can't
693 properly set their TYPE_CONTEXTs here, because we don't have a
694 pointer to the appropriate FUNCTION_TYPE node readily available
695 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
696 type nodes get set in `grokdeclarator' as soon as we have created
697 the FUNCTION_TYPE node which will represent the "scope" for these
698 "parameter list local" tagged types. */
699 if (scope->function_body)
700 context = current_function_decl;
701 else if (scope == file_scope)
703 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
704 TREE_CHAIN (file_decl) = all_translation_units;
705 all_translation_units = file_decl;
711 /* Clear all bindings in this scope. */
712 for (b = scope->bindings; b; b = free_binding_and_advance (b))
715 switch (TREE_CODE (p))
718 /* Warnings for unused labels, errors for undefined labels. */
719 if (TREE_USED (p) && !DECL_INITIAL (p))
721 error ("%Jlabel `%D' used but not defined", p, p);
722 DECL_INITIAL (p) = error_mark_node;
724 else if (!TREE_USED (p) && warn_unused_label)
726 if (DECL_INITIAL (p))
727 warning ("%Jlabel `%D' defined but not used", p, p);
729 warning ("%Jlabel `%D' declared but not defined", p, p);
731 /* Labels go in BLOCK_VARS. */
732 TREE_CHAIN (p) = BLOCK_VARS (block);
733 BLOCK_VARS (block) = p;
735 #ifdef ENABLE_CHECKING
736 if (I_LABEL_BINDING (b->id) != b) abort ();
738 I_LABEL_BINDING (b->id) = b->shadowed;
744 set_type_context (p, context);
746 /* Types may not have tag-names, in which case the type
747 appears in the bindings list with b->id NULL. */
750 #ifdef ENABLE_CHECKING
751 if (I_TAG_BINDING (b->id) != b) abort ();
753 I_TAG_BINDING (b->id) = b->shadowed;
758 /* Propagate TREE_ADDRESSABLE from nested functions to their
759 containing functions. */
760 if (! TREE_ASM_WRITTEN (p)
761 && DECL_INITIAL (p) != 0
762 && TREE_ADDRESSABLE (p)
763 && DECL_ABSTRACT_ORIGIN (p) != 0
764 && DECL_ABSTRACT_ORIGIN (p) != p)
765 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
769 /* Warnings for unused variables. */
770 if (warn_unused_variable
772 && !DECL_IN_SYSTEM_HEADER (p)
774 && !DECL_ARTIFICIAL (p)
775 && (scope != file_scope
776 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
777 && !TREE_THIS_VOLATILE (p)))
778 && scope != external_scope)
779 warning ("%Junused variable `%D'", p, p);
783 error ("%Jtype of array %qD completed incompatibly with"
784 " implicit initialization", p, p);
791 /* All of these go in BLOCK_VARS, but only if this is the
792 binding in the home scope. */
795 TREE_CHAIN (p) = BLOCK_VARS (block);
796 BLOCK_VARS (block) = p;
798 /* If this is the file scope, and we are processing more
799 than one translation unit in this compilation, set
800 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
801 This makes same_translation_unit_p work, and causes
802 static declarations to be given disambiguating suffixes. */
803 if (scope == file_scope && num_in_fnames > 1)
805 DECL_CONTEXT (p) = context;
806 if (TREE_CODE (p) == TYPE_DECL)
807 set_type_context (TREE_TYPE (p), context);
811 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
812 already been put there by store_parm_decls. Unused-
813 parameter warnings are handled by function.c.
814 error_mark_node obviously does not go in BLOCK_VARS and
815 does not get unused-variable warnings. */
818 /* It is possible for a decl not to have a name. We get
819 here with b->id NULL in this case. */
822 #ifdef ENABLE_CHECKING
823 if (I_SYMBOL_BINDING (b->id) != b) abort ();
825 I_SYMBOL_BINDING (b->id) = b->shadowed;
826 if (b->shadowed && b->shadowed->type)
827 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
837 /* Dispose of the block that we just made inside some higher level. */
838 if ((scope->function_body || scope == file_scope) && context)
840 DECL_INITIAL (context) = block;
841 BLOCK_SUPERCONTEXT (block) = context;
843 else if (scope->outer)
846 SCOPE_LIST_APPEND (scope->outer, blocks, block);
847 /* If we did not make a block for the scope just exited, any
848 blocks made for inner scopes must be carried forward so they
849 will later become subblocks of something else. */
850 else if (scope->blocks)
851 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
854 /* Pop the current scope, and free the structure for reuse. */
855 current_scope = scope->outer;
856 if (scope->function_body)
857 current_function_scope = scope->outer_function;
859 memset (scope, 0, sizeof (struct c_scope));
860 scope->outer = scope_freelist;
861 scope_freelist = scope;
867 push_file_scope (void)
875 file_scope = current_scope;
877 start_fname_decls ();
879 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
880 bind (DECL_NAME (decl), decl, file_scope,
881 /*invisible=*/false, /*nested=*/true);
885 pop_file_scope (void)
887 /* In case there were missing closebraces, get us back to the global
889 while (current_scope != file_scope)
892 /* __FUNCTION__ is defined at file scope (""). This
893 call may not be necessary as my tests indicate it
894 still works without it. */
895 finish_fname_decls ();
897 /* This is the point to write out a PCH if we're doing that.
898 In that case we do not want to do anything else. */
901 c_common_write_pch ();
905 /* Pop off the file scope and close this translation unit. */
908 cgraph_finalize_compilation_unit ();
911 /* Insert BLOCK at the end of the list of subblocks of the current
912 scope. This is used when a BIND_EXPR is expanded, to handle the
913 BLOCK node inside the BIND_EXPR. */
916 insert_block (tree block)
918 TREE_USED (block) = 1;
919 SCOPE_LIST_APPEND (current_scope, blocks, block);
922 /* Push a definition or a declaration of struct, union or enum tag "name".
923 "type" should be the type node.
924 We assume that the tag "name" is not already defined.
926 Note that the definition may really be just a forward reference.
927 In that case, the TYPE_SIZE will be zero. */
930 pushtag (tree name, tree type)
932 /* Record the identifier as the type's name if it has none. */
933 if (name && !TYPE_NAME (type))
934 TYPE_NAME (type) = name;
935 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
937 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
938 tagged type we just added to the current scope. This fake
939 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
940 to output a representation of a tagged type, and it also gives
941 us a convenient place to record the "scope start" address for the
944 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
946 /* An approximation for now, so we can tell this is a function-scope tag.
947 This will be updated in pop_scope. */
948 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
951 /* Subroutine of compare_decls. Allow harmless mismatches in return
952 and argument types provided that the type modes match. This function
953 return a unified type given a suitable match, and 0 otherwise. */
956 match_builtin_function_types (tree newtype, tree oldtype)
958 tree newrettype, oldrettype;
959 tree newargs, oldargs;
960 tree trytype, tryargs;
962 /* Accept the return type of the new declaration if same modes. */
963 oldrettype = TREE_TYPE (oldtype);
964 newrettype = TREE_TYPE (newtype);
966 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
969 oldargs = TYPE_ARG_TYPES (oldtype);
970 newargs = TYPE_ARG_TYPES (newtype);
973 while (oldargs || newargs)
977 || ! TREE_VALUE (oldargs)
978 || ! TREE_VALUE (newargs)
979 || TYPE_MODE (TREE_VALUE (oldargs))
980 != TYPE_MODE (TREE_VALUE (newargs)))
983 oldargs = TREE_CHAIN (oldargs);
984 newargs = TREE_CHAIN (newargs);
987 trytype = build_function_type (newrettype, tryargs);
988 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
991 /* Subroutine of diagnose_mismatched_decls. Check for function type
992 mismatch involving an empty arglist vs a nonempty one and give clearer
995 diagnose_arglist_conflict (tree newdecl, tree olddecl,
996 tree newtype, tree oldtype)
1000 if (TREE_CODE (olddecl) != FUNCTION_DECL
1001 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1002 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1004 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1007 t = TYPE_ARG_TYPES (oldtype);
1009 t = TYPE_ARG_TYPES (newtype);
1010 for (; t; t = TREE_CHAIN (t))
1012 tree type = TREE_VALUE (t);
1014 if (TREE_CHAIN (t) == 0
1015 && TYPE_MAIN_VARIANT (type) != void_type_node)
1017 inform ("a parameter list with an ellipsis can't match "
1018 "an empty parameter name list declaration");
1022 if (c_type_promotes_to (type) != type)
1024 inform ("an argument type that has a default promotion can't match "
1025 "an empty parameter name list declaration");
1031 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1032 old-style function definition, NEWDECL is a prototype declaration.
1033 Diagnose inconsistencies in the argument list. Returns TRUE if
1034 the prototype is compatible, FALSE if not. */
1036 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1038 tree newargs, oldargs;
1041 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1042 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1044 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1045 newargs = TYPE_ARG_TYPES (newtype);
1050 tree oldargtype = TREE_VALUE (oldargs);
1051 tree newargtype = TREE_VALUE (newargs);
1053 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1056 /* Reaching the end of just one list means the two decls don't
1057 agree on the number of arguments. */
1058 if (END_OF_ARGLIST (oldargtype))
1060 error ("%Jprototype for '%D' declares more arguments "
1061 "than previous old-style definition", newdecl, newdecl);
1064 else if (END_OF_ARGLIST (newargtype))
1066 error ("%Jprototype for '%D' declares fewer arguments "
1067 "than previous old-style definition", newdecl, newdecl);
1071 /* Type for passing arg must be consistent with that declared
1073 else if (! comptypes (oldargtype, newargtype))
1075 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1076 newdecl, newdecl, i);
1080 oldargs = TREE_CHAIN (oldargs);
1081 newargs = TREE_CHAIN (newargs);
1085 /* If we get here, no errors were found, but do issue a warning
1086 for this poor-style construct. */
1087 warning ("%Jprototype for '%D' follows non-prototype definition",
1090 #undef END_OF_ARGLIST
1093 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1094 first in a pair of mismatched declarations, using the diagnostic
1097 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1099 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1101 else if (DECL_INITIAL (decl))
1102 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1103 else if (C_DECL_IMPLICIT (decl))
1104 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1106 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1109 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1110 Returns true if the caller should proceed to merge the two, false
1111 if OLDDECL should simply be discarded. As a side effect, issues
1112 all necessary diagnostics for invalid or poor-style combinations.
1113 If it returns true, writes the types of NEWDECL and OLDDECL to
1114 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1115 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1118 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1119 tree *newtypep, tree *oldtypep)
1121 tree newtype, oldtype;
1122 bool pedwarned = false;
1123 bool warned = false;
1125 /* If we have error_mark_node for either decl or type, just discard
1126 the previous decl - we're in an error cascade already. */
1127 if (olddecl == error_mark_node || newdecl == error_mark_node)
1129 *oldtypep = oldtype = TREE_TYPE (olddecl);
1130 *newtypep = newtype = TREE_TYPE (newdecl);
1131 if (oldtype == error_mark_node || newtype == error_mark_node)
1134 /* Two different categories of symbol altogether. This is an error
1135 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1136 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1138 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1139 && DECL_BUILT_IN (olddecl)
1140 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1142 error ("%J'%D' redeclared as different kind of symbol",
1144 locate_old_decl (olddecl, error);
1146 else if (TREE_PUBLIC (newdecl))
1147 warning ("%Jbuilt-in function '%D' declared as non-function",
1149 else if (warn_shadow)
1150 warning ("%Jdeclaration of '%D' shadows a built-in function",
1155 if (!comptypes (oldtype, newtype))
1157 if (TREE_CODE (olddecl) == FUNCTION_DECL
1158 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1160 /* Accept harmless mismatch in function types.
1161 This is for the ffs and fprintf builtins. */
1162 tree trytype = match_builtin_function_types (newtype, oldtype);
1164 if (trytype && comptypes (newtype, trytype))
1165 *oldtypep = oldtype = trytype;
1168 /* If types don't match for a built-in, throw away the
1169 built-in. No point in calling locate_old_decl here, it
1170 won't print anything. */
1171 warning ("%Jconflicting types for built-in function '%D'",
1176 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1177 && DECL_IS_BUILTIN (olddecl))
1179 /* A conflicting function declaration for a predeclared
1180 function that isn't actually built in. Objective C uses
1181 these. The new declaration silently overrides everything
1182 but the volatility (i.e. noreturn) indication. See also
1183 below. FIXME: Make Objective C use normal builtins. */
1184 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1187 /* Permit void foo (...) to match int foo (...) if the latter is
1188 the definition and implicit int was used. See
1189 c-torture/compile/920625-2.c. */
1190 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1191 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1192 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1193 && C_FUNCTION_IMPLICIT_INT (newdecl))
1195 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1196 /* Make sure we keep void as the return type. */
1197 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1198 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1203 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1204 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1206 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1207 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1208 locate_old_decl (olddecl, error);
1213 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1214 but silently ignore the redeclaration if either is in a system
1215 header. (Conflicting redeclarations were handled above.) */
1216 if (TREE_CODE (newdecl) == TYPE_DECL)
1218 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1219 return true; /* Allow OLDDECL to continue in use. */
1221 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1222 locate_old_decl (olddecl, error);
1226 /* Function declarations can either be 'static' or 'extern' (no
1227 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1228 can never conflict with each other on account of linkage (6.2.2p4).
1229 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1230 two definitions if one is 'extern inline' and one is not. The non-
1231 extern-inline definition supersedes the extern-inline definition. */
1232 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1234 /* If you declare a built-in function name as static, or
1235 define the built-in with an old-style definition (so we
1236 can't validate the argument list) the built-in definition is
1237 overridden, but optionally warn this was a bad choice of name. */
1238 if (DECL_BUILT_IN (olddecl)
1239 && !C_DECL_DECLARED_BUILTIN (olddecl)
1240 && (!TREE_PUBLIC (newdecl)
1241 || (DECL_INITIAL (newdecl)
1242 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1245 warning ("%Jdeclaration of '%D' shadows a built-in function",
1247 /* Discard the old built-in function. */
1251 if (DECL_INITIAL (newdecl))
1253 if (DECL_INITIAL (olddecl)
1254 && !(DECL_DECLARED_INLINE_P (olddecl)
1255 && DECL_EXTERNAL (olddecl)
1256 && !(DECL_DECLARED_INLINE_P (newdecl)
1257 && DECL_EXTERNAL (newdecl)
1258 && same_translation_unit_p (olddecl, newdecl))))
1260 error ("%Jredefinition of '%D'", newdecl, newdecl);
1261 locate_old_decl (olddecl, error);
1265 /* If we have a prototype after an old-style function definition,
1266 the argument types must be checked specially. */
1267 else if (DECL_INITIAL (olddecl)
1268 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1269 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1270 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1272 locate_old_decl (olddecl, error);
1275 /* A non-static declaration (even an "extern") followed by a
1276 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1277 The same is true for a static forward declaration at block
1278 scope followed by a non-static declaration/definition at file
1279 scope. Static followed by non-static at the same scope is
1280 not undefined behavior, and is the most convenient way to get
1281 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1282 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1283 we do diagnose it if -Wtraditional. */
1284 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1286 /* Two exceptions to the rule. If olddecl is an extern
1287 inline, or a predeclared function that isn't actually
1288 built in, newdecl silently overrides olddecl. The latter
1289 occur only in Objective C; see also above. (FIXME: Make
1290 Objective C use normal builtins.) */
1291 if (!DECL_IS_BUILTIN (olddecl)
1292 && !(DECL_EXTERNAL (olddecl)
1293 && DECL_DECLARED_INLINE_P (olddecl)))
1295 error ("%Jstatic declaration of '%D' follows "
1296 "non-static declaration", newdecl, newdecl);
1297 locate_old_decl (olddecl, error);
1301 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1303 if (DECL_CONTEXT (olddecl))
1305 error ("%Jnon-static declaration of '%D' follows "
1306 "static declaration", newdecl, newdecl);
1307 locate_old_decl (olddecl, error);
1310 else if (warn_traditional)
1312 warning ("%Jnon-static declaration of '%D' follows "
1313 "static declaration", newdecl, newdecl);
1318 else if (TREE_CODE (newdecl) == VAR_DECL)
1320 /* Only variables can be thread-local, and all declarations must
1321 agree on this property. */
1322 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1324 if (DECL_THREAD_LOCAL (newdecl))
1325 error ("%Jthread-local declaration of '%D' follows "
1326 "non-thread-local declaration", newdecl, newdecl);
1328 error ("%Jnon-thread-local declaration of '%D' follows "
1329 "thread-local declaration", newdecl, newdecl);
1331 locate_old_decl (olddecl, error);
1335 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1336 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1338 error ("%Jredefinition of '%D'", newdecl, newdecl);
1339 locate_old_decl (olddecl, error);
1343 /* Objects declared at file scope: if the first declaration had
1344 external linkage (even if it was an external reference) the
1345 second must have external linkage as well, or the behavior is
1346 undefined. If the first declaration had internal linkage, then
1347 the second must too, or else be an external reference (in which
1348 case the composite declaration still has internal linkage).
1349 As for function declarations, we warn about the static-then-
1350 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1351 if (DECL_FILE_SCOPE_P (newdecl)
1352 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1354 if (DECL_EXTERNAL (newdecl))
1356 if (!DECL_FILE_SCOPE_P (olddecl))
1358 error ("%Jextern declaration of %qD follows "
1359 "declaration with no linkage", newdecl, newdecl);
1360 locate_old_decl (olddecl, error);
1363 else if (warn_traditional)
1365 warning ("%Jnon-static declaration of '%D' follows "
1366 "static declaration", newdecl, newdecl);
1372 if (TREE_PUBLIC (newdecl))
1373 error ("%Jnon-static declaration of '%D' follows "
1374 "static declaration", newdecl, newdecl);
1376 error ("%Jstatic declaration of '%D' follows "
1377 "non-static declaration", newdecl, newdecl);
1379 locate_old_decl (olddecl, error);
1383 /* Two objects with the same name declared at the same block
1384 scope must both be external references (6.7p3). */
1385 else if (!DECL_FILE_SCOPE_P (newdecl))
1387 if (DECL_EXTERNAL (newdecl))
1389 /* Extern with initializer at block scope, which will
1390 already have received an error. */
1392 else if (DECL_EXTERNAL (olddecl))
1394 error ("%Jdeclaration of '%D' with no linkage follows "
1395 "extern declaration", newdecl, newdecl);
1396 locate_old_decl (olddecl, error);
1400 error ("%Jredeclaration of '%D' with no linkage",
1402 locate_old_decl (olddecl, error);
1410 /* All decls must agree on a visibility. */
1411 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1412 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1414 warning ("%Jredeclaration of '%D' with different visibility "
1415 "(old visibility preserved)", newdecl, newdecl);
1419 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1421 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1422 if (DECL_DECLARED_INLINE_P (newdecl)
1423 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1425 warning ("%Jinline declaration of '%D' follows "
1426 "declaration with attribute noinline", newdecl, newdecl);
1429 else if (DECL_DECLARED_INLINE_P (olddecl)
1430 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1432 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1433 "inline declaration ", newdecl, newdecl);
1437 /* Inline declaration after use or definition.
1438 ??? Should we still warn about this now we have unit-at-a-time
1439 mode and can get it right?
1440 Definitely don't complain if the decls are in different translation
1442 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1443 && same_translation_unit_p (olddecl, newdecl))
1445 if (TREE_USED (olddecl))
1447 warning ("%J'%D' declared inline after being called",
1451 else if (DECL_INITIAL (olddecl))
1453 warning ("%J'%D' declared inline after its definition",
1459 else /* PARM_DECL, VAR_DECL */
1461 /* Redeclaration of a parameter is a constraint violation (this is
1462 not explicitly stated, but follows from C99 6.7p3 [no more than
1463 one declaration of the same identifier with no linkage in the
1464 same scope, except type tags] and 6.2.2p6 [parameters have no
1465 linkage]). We must check for a forward parameter declaration,
1466 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1467 an extension, the mandatory diagnostic for which is handled by
1468 mark_forward_parm_decls. */
1470 if (TREE_CODE (newdecl) == PARM_DECL
1471 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1473 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1474 locate_old_decl (olddecl, error);
1479 /* Optional warning for completely redundant decls. */
1480 if (!warned && !pedwarned
1481 && warn_redundant_decls
1482 /* Don't warn about a function declaration followed by a
1484 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1485 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1486 /* Don't warn about redundant redeclarations of builtins. */
1487 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1488 && !DECL_BUILT_IN (newdecl)
1489 && DECL_BUILT_IN (olddecl)
1490 && !C_DECL_DECLARED_BUILTIN (olddecl))
1491 /* Don't warn about an extern followed by a definition. */
1492 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1493 /* Don't warn about forward parameter decls. */
1494 && !(TREE_CODE (newdecl) == PARM_DECL
1495 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1497 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1501 /* Report location of previous decl/defn in a consistent manner. */
1502 if (warned || pedwarned)
1503 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1508 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1509 consistent with OLDDECL, but carries new information. Merge the
1510 new information into OLDDECL. This function issues no
1514 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1516 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1517 && DECL_INITIAL (newdecl) != 0);
1519 /* For real parm decl following a forward decl, rechain the old decl
1520 in its new location and clear TREE_ASM_WRITTEN (it's not a
1521 forward decl anymore). */
1522 if (TREE_CODE (newdecl) == PARM_DECL
1523 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1525 struct c_binding *b, **here;
1527 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1528 if ((*here)->decl == olddecl)
1535 b->prev = current_scope->bindings;
1536 current_scope->bindings = b;
1538 TREE_ASM_WRITTEN (olddecl) = 0;
1541 DECL_ATTRIBUTES (newdecl)
1542 = targetm.merge_decl_attributes (olddecl, newdecl);
1544 /* Merge the data types specified in the two decls. */
1546 = TREE_TYPE (olddecl)
1547 = composite_type (newtype, oldtype);
1549 /* Lay the type out, unless already done. */
1550 if (oldtype != TREE_TYPE (newdecl))
1552 if (TREE_TYPE (newdecl) != error_mark_node)
1553 layout_type (TREE_TYPE (newdecl));
1554 if (TREE_CODE (newdecl) != FUNCTION_DECL
1555 && TREE_CODE (newdecl) != TYPE_DECL
1556 && TREE_CODE (newdecl) != CONST_DECL)
1557 layout_decl (newdecl, 0);
1561 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1562 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1563 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1564 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1565 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1566 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1568 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1569 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1573 /* Keep the old rtl since we can safely use it. */
1574 COPY_DECL_RTL (olddecl, newdecl);
1576 /* Merge the type qualifiers. */
1577 if (TREE_READONLY (newdecl))
1578 TREE_READONLY (olddecl) = 1;
1580 if (TREE_THIS_VOLATILE (newdecl))
1582 TREE_THIS_VOLATILE (olddecl) = 1;
1583 if (TREE_CODE (newdecl) == VAR_DECL)
1584 make_var_volatile (newdecl);
1587 /* Keep source location of definition rather than declaration. */
1588 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1589 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1591 /* Merge the unused-warning information. */
1592 if (DECL_IN_SYSTEM_HEADER (olddecl))
1593 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1594 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1595 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1597 /* Merge the initialization information. */
1598 if (DECL_INITIAL (newdecl) == 0)
1599 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1601 /* Merge the section attribute.
1602 We want to issue an error if the sections conflict but that must be
1603 done later in decl_attributes since we are called before attributes
1605 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1606 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1608 /* Copy the assembler name.
1609 Currently, it can only be defined in the prototype. */
1610 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1612 /* Use visibility of whichever declaration had it specified */
1613 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1615 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1616 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1619 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1621 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1622 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1623 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1624 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1625 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1626 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1627 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1628 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1629 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1632 /* Merge the storage class information. */
1633 merge_weak (newdecl, olddecl);
1635 /* For functions, static overrides non-static. */
1636 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1638 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1639 /* This is since we don't automatically
1640 copy the attributes of NEWDECL into OLDDECL. */
1641 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1642 /* If this clears `static', clear it in the identifier too. */
1643 if (! TREE_PUBLIC (olddecl))
1644 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1646 if (DECL_EXTERNAL (newdecl))
1648 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1649 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1651 /* An extern decl does not override previous storage class. */
1652 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1653 if (! DECL_EXTERNAL (newdecl))
1655 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1656 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1661 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1662 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1665 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1667 /* If we're redefining a function previously defined as extern
1668 inline, make sure we emit debug info for the inline before we
1669 throw it away, in case it was inlined into a function that hasn't
1670 been written out yet. */
1671 if (new_is_definition && DECL_INITIAL (olddecl))
1673 if (TREE_USED (olddecl)
1674 /* In unit-at-a-time mode we never inline re-defined extern
1675 inline functions. */
1676 && !flag_unit_at_a_time
1677 && cgraph_function_possibly_inlined_p (olddecl))
1678 (*debug_hooks->outlining_inline_function) (olddecl);
1680 /* The new defn must not be inline. */
1681 DECL_INLINE (newdecl) = 0;
1682 DECL_UNINLINABLE (newdecl) = 1;
1686 /* If either decl says `inline', this fn is inline,
1687 unless its definition was passed already. */
1688 if (DECL_DECLARED_INLINE_P (newdecl)
1689 || DECL_DECLARED_INLINE_P (olddecl))
1690 DECL_DECLARED_INLINE_P (newdecl) = 1;
1692 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1693 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1696 if (DECL_BUILT_IN (olddecl))
1698 /* If redeclaring a builtin function, it stays built in.
1699 But it gets tagged as having been declared. */
1700 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1701 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1702 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1705 /* Also preserve various other info from the definition. */
1706 if (! new_is_definition)
1708 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1709 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1710 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1711 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1712 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1714 /* Set DECL_INLINE on the declaration if we've got a body
1715 from which to instantiate. */
1716 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1718 DECL_INLINE (newdecl) = 1;
1719 DECL_ABSTRACT_ORIGIN (newdecl)
1720 = DECL_ABSTRACT_ORIGIN (olddecl);
1725 /* If a previous declaration said inline, mark the
1726 definition as inlinable. */
1727 if (DECL_DECLARED_INLINE_P (newdecl)
1728 && ! DECL_UNINLINABLE (newdecl))
1729 DECL_INLINE (newdecl) = 1;
1733 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1734 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1736 unsigned olddecl_uid = DECL_UID (olddecl);
1737 tree olddecl_context = DECL_CONTEXT (olddecl);
1739 memcpy ((char *) olddecl + sizeof (struct tree_common),
1740 (char *) newdecl + sizeof (struct tree_common),
1741 sizeof (struct tree_decl) - sizeof (struct tree_common));
1742 DECL_UID (olddecl) = olddecl_uid;
1743 DECL_CONTEXT (olddecl) = olddecl_context;
1746 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1747 so that encode_section_info has a chance to look at the new decl
1748 flags and attributes. */
1749 if (DECL_RTL_SET_P (olddecl)
1750 && (TREE_CODE (olddecl) == FUNCTION_DECL
1751 || (TREE_CODE (olddecl) == VAR_DECL
1752 && TREE_STATIC (olddecl))))
1753 make_decl_rtl (olddecl);
1756 /* Handle when a new declaration NEWDECL has the same name as an old
1757 one OLDDECL in the same binding contour. Prints an error message
1760 If safely possible, alter OLDDECL to look like NEWDECL, and return
1761 true. Otherwise, return false. */
1764 duplicate_decls (tree newdecl, tree olddecl)
1766 tree newtype = NULL, oldtype = NULL;
1768 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1771 merge_decls (newdecl, olddecl, newtype, oldtype);
1776 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1778 warn_if_shadowing (tree new_decl)
1780 struct c_binding *b;
1782 /* Shadow warnings wanted? */
1784 /* No shadow warnings for internally generated vars. */
1785 || DECL_IS_BUILTIN (new_decl)
1786 /* No shadow warnings for vars made for inlining. */
1787 || DECL_FROM_INLINE (new_decl)
1788 /* Don't warn about the parm names in function declarator
1789 within a function declarator. It would be nice to avoid
1790 warning in any function declarator in a declaration, as
1791 opposed to a definition, but there is no way to tell
1792 it's not a definition at this point. */
1793 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1796 /* Is anything being shadowed? Invisible decls do not count. */
1797 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1798 if (b->decl && b->decl != new_decl && !b->invisible)
1800 tree old_decl = b->decl;
1802 if (TREE_CODE (old_decl) == PARM_DECL)
1803 warning ("%Jdeclaration of '%D' shadows a parameter",
1804 new_decl, new_decl);
1805 else if (DECL_FILE_SCOPE_P (old_decl))
1806 warning ("%Jdeclaration of '%D' shadows a global declaration",
1807 new_decl, new_decl);
1808 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1809 && DECL_BUILT_IN (old_decl))
1810 warning ("%Jdeclaration of '%D' shadows a built-in function",
1811 new_decl, new_decl);
1813 warning ("%Jdeclaration of '%D' shadows a previous local",
1814 new_decl, new_decl);
1816 if (TREE_CODE (old_decl) != FUNCTION_DECL
1817 || ! DECL_BUILT_IN (old_decl))
1818 warning ("%Jshadowed declaration is here", old_decl);
1825 /* Subroutine of pushdecl.
1827 X is a TYPE_DECL for a typedef statement. Create a brand new
1828 ..._TYPE node (which will be just a variant of the existing
1829 ..._TYPE node with identical properties) and then install X
1830 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1832 The whole point here is to end up with a situation where each
1833 and every ..._TYPE node the compiler creates will be uniquely
1834 associated with AT MOST one node representing a typedef name.
1835 This way, even though the compiler substitutes corresponding
1836 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1837 early on, later parts of the compiler can always do the reverse
1838 translation and get back the corresponding typedef name. For
1841 typedef struct S MY_TYPE;
1844 Later parts of the compiler might only know that `object' was of
1845 type `struct S' if it were not for code just below. With this
1846 code however, later parts of the compiler see something like:
1848 struct S' == struct S
1849 typedef struct S' MY_TYPE;
1852 And they can then deduce (from the node for type struct S') that
1853 the original object declaration was:
1857 Being able to do this is important for proper support of protoize,
1858 and also for generating precise symbolic debugging information
1859 which takes full account of the programmer's (typedef) vocabulary.
1861 Obviously, we don't want to generate a duplicate ..._TYPE node if
1862 the TYPE_DECL node that we are now processing really represents a
1863 standard built-in type.
1865 Since all standard types are effectively declared at line zero
1866 in the source file, we can easily check to see if we are working
1867 on a standard type by checking the current value of lineno. */
1870 clone_underlying_type (tree x)
1872 if (DECL_IS_BUILTIN (x))
1874 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1875 TYPE_NAME (TREE_TYPE (x)) = x;
1877 else if (TREE_TYPE (x) != error_mark_node
1878 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1880 tree tt = TREE_TYPE (x);
1881 DECL_ORIGINAL_TYPE (x) = tt;
1882 tt = build_variant_type_copy (tt);
1884 TREE_USED (tt) = TREE_USED (x);
1889 /* Record a decl-node X as belonging to the current lexical scope.
1890 Check for errors (such as an incompatible declaration for the same
1891 name already seen in the same scope).
1893 Returns either X or an old decl for the same name.
1894 If an old decl is returned, it may have been smashed
1895 to agree with what X says. */
1900 tree name = DECL_NAME (x);
1901 struct c_scope *scope = current_scope;
1902 struct c_binding *b;
1903 bool nested = false;
1905 /* Functions need the lang_decl data. */
1906 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1907 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1909 /* Must set DECL_CONTEXT for everything not at file scope or
1910 DECL_FILE_SCOPE_P won't work. Local externs don't count
1911 unless they have initializers (which generate code). */
1912 if (current_function_decl
1913 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1914 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1915 DECL_CONTEXT (x) = current_function_decl;
1917 /* Anonymous decls are just inserted in the scope. */
1920 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1924 /* First, see if there is another declaration with the same name in
1925 the current scope. If there is, duplicate_decls may do all the
1926 work for us. If duplicate_decls returns false, that indicates
1927 two incompatible decls in the same scope; we are to silently
1928 replace the old one (duplicate_decls has issued all appropriate
1929 diagnostics). In particular, we should not consider possible
1930 duplicates in the external scope, or shadowing. */
1931 b = I_SYMBOL_BINDING (name);
1932 if (b && B_IN_SCOPE (b, scope))
1934 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
1935 && COMPLETE_TYPE_P (TREE_TYPE (x)))
1936 b->inner_comp = false;
1937 if (duplicate_decls (x, b->decl))
1940 goto skip_external_and_shadow_checks;
1943 /* All declarations with external linkage, and all external
1944 references, go in the external scope, no matter what scope is
1945 current. However, the binding in that scope is ignored for
1946 purposes of normal name lookup. A separate binding structure is
1947 created in the requested scope; this governs the normal
1948 visibility of the symbol.
1950 The binding in the externals scope is used exclusively for
1951 detecting duplicate declarations of the same object, no matter
1952 what scope they are in; this is what we do here. (C99 6.2.7p2:
1953 All declarations that refer to the same object or function shall
1954 have compatible type; otherwise, the behavior is undefined.) */
1955 if (DECL_EXTERNAL (x) || scope == file_scope)
1957 tree type = TREE_TYPE (x);
1960 bool type_saved = false;
1961 if (b && !B_IN_EXTERNAL_SCOPE (b)
1962 && (TREE_CODE (b->decl) == FUNCTION_DECL
1963 || TREE_CODE (b->decl) == VAR_DECL)
1964 && DECL_FILE_SCOPE_P (b->decl))
1967 vistype = TREE_TYPE (visdecl);
1969 if (warn_nested_externs
1970 && scope != file_scope
1971 && !DECL_IN_SYSTEM_HEADER (x))
1972 warning ("nested extern declaration of '%D'", x);
1974 while (b && !B_IN_EXTERNAL_SCOPE (b))
1976 /* If this decl might be modified, save its type. This is
1977 done here rather than when the decl is first bound
1978 because the type may change after first binding, through
1979 being completed or through attributes being added. If we
1980 encounter multiple such decls, only the first should have
1981 its type saved; the others will already have had their
1982 proper types saved and the types will not have changed as
1983 their scopes will not have been re-entered. */
1984 if (DECL_FILE_SCOPE_P (b->decl) && !type_saved)
1986 b->type = TREE_TYPE (b->decl);
1989 if (B_IN_FILE_SCOPE (b)
1990 && TREE_CODE (b->decl) == VAR_DECL
1991 && TREE_STATIC (b->decl)
1992 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
1993 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
1994 && TREE_CODE (type) == ARRAY_TYPE
1995 && TYPE_DOMAIN (type)
1996 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
1997 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1999 /* Array type completed in inner scope, which should be
2000 diagnosed if the completion does not have size 1 and
2001 it does not get completed in the file scope. */
2002 b->inner_comp = true;
2007 /* If a matching external declaration has been found, set its
2008 type to the composite of all the types of that declaration.
2009 After the consistency checks, it will be reset to the
2010 composite of the visible types only. */
2011 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2013 TREE_TYPE (b->decl) = b->type;
2015 /* The point of the same_translation_unit_p check here is,
2016 we want to detect a duplicate decl for a construct like
2017 foo() { extern bar(); } ... static bar(); but not if
2018 they are in different translation units. In any case,
2019 the static does not go in the externals scope. */
2021 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2022 && duplicate_decls (x, b->decl))
2025 thistype = (vistype ? composite_type (vistype, type) : type);
2026 b->type = TREE_TYPE (b->decl);
2027 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2029 = build_type_attribute_variant (thistype,
2030 TYPE_ATTRIBUTES (b->type));
2031 TREE_TYPE (b->decl) = thistype;
2032 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2035 else if (TREE_PUBLIC (x))
2037 if (visdecl && !b && duplicate_decls (x, visdecl))
2039 /* An external declaration at block scope referring to a
2040 visible entity with internal linkage. The composite
2041 type will already be correct for this scope, so we
2042 just need to fall through to make the declaration in
2048 bind (name, x, external_scope, /*invisible=*/true,
2054 /* Similarly, a declaration of a function with static linkage at
2055 block scope must be checked against any existing declaration
2056 of that function at file scope. */
2057 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
2058 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
2060 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
2061 warning ("nested static declaration of '%D'", x);
2063 while (b && !B_IN_FILE_SCOPE (b))
2066 if (b && same_translation_unit_p (x, b->decl)
2067 && duplicate_decls (x, b->decl))
2069 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2074 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
2079 warn_if_shadowing (x);
2081 skip_external_and_shadow_checks:
2082 if (TREE_CODE (x) == TYPE_DECL)
2083 clone_underlying_type (x);
2085 bind (name, x, scope, /*invisible=*/false, nested);
2087 /* If x's type is incomplete because it's based on a
2088 structure or union which has not yet been fully declared,
2089 attach it to that structure or union type, so we can go
2090 back and complete the variable declaration later, if the
2091 structure or union gets fully declared.
2093 If the input is erroneous, we can have error_mark in the type
2094 slot (e.g. "f(void a, ...)") - that doesn't count as an
2096 if (TREE_TYPE (x) != error_mark_node
2097 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2099 tree element = TREE_TYPE (x);
2101 while (TREE_CODE (element) == ARRAY_TYPE)
2102 element = TREE_TYPE (element);
2103 element = TYPE_MAIN_VARIANT (element);
2105 if ((TREE_CODE (element) == RECORD_TYPE
2106 || TREE_CODE (element) == UNION_TYPE)
2107 && (TREE_CODE (x) != TYPE_DECL
2108 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2109 && !COMPLETE_TYPE_P (element))
2110 C_TYPE_INCOMPLETE_VARS (element)
2111 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2116 /* Record X as belonging to file scope.
2117 This is used only internally by the Objective-C front end,
2118 and is limited to its needs. duplicate_decls is not called;
2119 if there is any preexisting decl for this identifier, it is an ICE. */
2122 pushdecl_top_level (tree x)
2125 bool nested = false;
2127 if (TREE_CODE (x) != VAR_DECL)
2130 name = DECL_NAME (x);
2132 if (I_SYMBOL_BINDING (name))
2135 if (TREE_PUBLIC (x))
2137 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2141 bind (name, x, file_scope, /*invisible=*/false, nested);
2147 implicit_decl_warning (tree id, tree olddecl)
2149 void (*diag) (const char *, ...);
2150 switch (mesg_implicit_function_declaration)
2153 case 1: diag = warning; break;
2154 case 2: diag = error; break;
2158 diag (N_("implicit declaration of function '%E'"), id);
2160 locate_old_decl (olddecl, diag);
2163 /* Generate an implicit declaration for identifier FUNCTIONID as a
2164 function of type int (). */
2167 implicitly_declare (tree functionid)
2169 struct c_binding *b;
2171 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2173 if (B_IN_SCOPE (b, external_scope))
2182 /* FIXME: Objective-C has weird not-really-builtin functions
2183 which are supposed to be visible automatically. They wind up
2184 in the external scope because they're pushed before the file
2185 scope gets created. Catch this here and rebind them into the
2187 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2189 bind (functionid, decl, file_scope,
2190 /*invisible=*/false, /*nested=*/true);
2195 tree newtype = default_function_type;
2197 TREE_TYPE (decl) = b->type;
2198 /* Implicit declaration of a function already declared
2199 (somehow) in a different scope, or as a built-in.
2200 If this is the first time this has happened, warn;
2201 then recycle the old declaration but with the new type. */
2202 if (!C_DECL_IMPLICIT (decl))
2204 implicit_decl_warning (functionid, decl);
2205 C_DECL_IMPLICIT (decl) = 1;
2207 if (DECL_BUILT_IN (decl))
2209 newtype = build_type_attribute_variant (newtype,
2211 (TREE_TYPE (decl)));
2212 if (!comptypes (newtype, TREE_TYPE (decl)))
2214 warning ("incompatible implicit declaration of built-in"
2215 " function %qD", decl);
2216 newtype = TREE_TYPE (decl);
2221 if (!comptypes (newtype, TREE_TYPE (decl)))
2223 error ("incompatible implicit declaration of function %qD",
2225 locate_old_decl (decl, error);
2228 b->type = TREE_TYPE (decl);
2229 TREE_TYPE (decl) = newtype;
2230 bind (functionid, decl, current_scope,
2231 /*invisible=*/false, /*nested=*/true);
2236 /* Not seen before. */
2237 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2238 DECL_EXTERNAL (decl) = 1;
2239 TREE_PUBLIC (decl) = 1;
2240 C_DECL_IMPLICIT (decl) = 1;
2241 implicit_decl_warning (functionid, 0);
2243 /* C89 says implicit declarations are in the innermost block.
2244 So we record the decl in the standard fashion. */
2245 decl = pushdecl (decl);
2247 /* No need to call objc_check_decl here - it's a function type. */
2248 rest_of_decl_compilation (decl, 0, 0);
2250 /* Write a record describing this implicit function declaration
2251 to the prototypes file (if requested). */
2252 gen_aux_info_record (decl, 0, 1, 0);
2254 /* Possibly apply some default attributes to this implicit declaration. */
2255 decl_attributes (&decl, NULL_TREE, 0);
2260 /* Issue an error message for a reference to an undeclared variable
2261 ID, including a reference to a builtin outside of function-call
2262 context. Establish a binding of the identifier to error_mark_node
2263 in an appropriate scope, which will suppress further errors for the
2266 undeclared_variable (tree id)
2268 static bool already = false;
2269 struct c_scope *scope;
2271 if (current_function_decl == 0)
2273 error ("'%E' undeclared here (not in a function)", id);
2274 scope = current_scope;
2278 error ("'%E' undeclared (first use in this function)", id);
2282 error ("(Each undeclared identifier is reported only once");
2283 error ("for each function it appears in.)");
2287 /* If we are parsing old-style parameter decls, current_function_decl
2288 will be nonnull but current_function_scope will be null. */
2289 scope = current_function_scope ? current_function_scope : current_scope;
2291 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2294 /* Subroutine of lookup_label, declare_label, define_label: construct a
2295 LABEL_DECL with all the proper frills. */
2298 make_label (tree name, location_t location)
2300 tree label = build_decl (LABEL_DECL, name, void_type_node);
2302 DECL_CONTEXT (label) = current_function_decl;
2303 DECL_MODE (label) = VOIDmode;
2304 DECL_SOURCE_LOCATION (label) = location;
2309 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2310 Create one if none exists so far for the current function.
2311 This is called when a label is used in a goto expression or
2312 has its address taken. */
2315 lookup_label (tree name)
2319 if (current_function_decl == 0)
2321 error ("label %s referenced outside of any function",
2322 IDENTIFIER_POINTER (name));
2326 /* Use a label already defined or ref'd with this name, but not if
2327 it is inherited from a containing function and wasn't declared
2329 label = I_LABEL_DECL (name);
2330 if (label && (DECL_CONTEXT (label) == current_function_decl
2331 || C_DECLARED_LABEL_FLAG (label)))
2333 /* If the label has only been declared, update its apparent
2334 location to point here, for better diagnostics if it
2335 turns out not to have been defined. */
2336 if (!TREE_USED (label))
2337 DECL_SOURCE_LOCATION (label) = input_location;
2341 /* No label binding for that identifier; make one. */
2342 label = make_label (name, input_location);
2344 /* Ordinary labels go in the current function scope. */
2345 bind (name, label, current_function_scope,
2346 /*invisible=*/false, /*nested=*/false);
2350 /* Make a label named NAME in the current function, shadowing silently
2351 any that may be inherited from containing functions or containing
2352 scopes. This is called for __label__ declarations. */
2355 declare_label (tree name)
2357 struct c_binding *b = I_LABEL_BINDING (name);
2360 /* Check to make sure that the label hasn't already been declared
2362 if (b && B_IN_CURRENT_SCOPE (b))
2364 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2365 locate_old_decl (b->decl, error);
2367 /* Just use the previous declaration. */
2371 label = make_label (name, input_location);
2372 C_DECLARED_LABEL_FLAG (label) = 1;
2374 /* Declared labels go in the current scope. */
2375 bind (name, label, current_scope,
2376 /*invisible=*/false, /*nested=*/false);
2380 /* Define a label, specifying the location in the source file.
2381 Return the LABEL_DECL node for the label, if the definition is valid.
2382 Otherwise return 0. */
2385 define_label (location_t location, tree name)
2387 /* Find any preexisting label with this name. It is an error
2388 if that label has already been defined in this function, or
2389 if there is a containing function with a declared label with
2391 tree label = I_LABEL_DECL (name);
2394 && ((DECL_CONTEXT (label) == current_function_decl
2395 && DECL_INITIAL (label) != 0)
2396 || (DECL_CONTEXT (label) != current_function_decl
2397 && C_DECLARED_LABEL_FLAG (label))))
2399 error ("%Hduplicate label `%D'", &location, label);
2400 locate_old_decl (label, error);
2403 else if (label && DECL_CONTEXT (label) == current_function_decl)
2405 /* The label has been used or declared already in this function,
2406 but not defined. Update its location to point to this
2408 DECL_SOURCE_LOCATION (label) = location;
2412 /* No label binding for that identifier; make one. */
2413 label = make_label (name, location);
2415 /* Ordinary labels go in the current function scope. */
2416 bind (name, label, current_function_scope,
2417 /*invisible=*/false, /*nested=*/false);
2420 if (warn_traditional && !in_system_header && lookup_name (name))
2421 warning ("%Htraditional C lacks a separate namespace for labels, "
2422 "identifier `%s' conflicts", &location,
2423 IDENTIFIER_POINTER (name));
2425 /* Mark label as having been defined. */
2426 DECL_INITIAL (label) = error_mark_node;
2430 /* Given NAME, an IDENTIFIER_NODE,
2431 return the structure (or union or enum) definition for that name.
2432 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2433 CODE says which kind of type the caller wants;
2434 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2435 If the wrong kind of type is found, an error is reported. */
2438 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2440 struct c_binding *b = I_TAG_BINDING (name);
2446 /* We only care about whether it's in this level if
2447 thislevel_only was set or it might be a type clash. */
2448 if (thislevel_only || TREE_CODE (b->decl) != code)
2450 /* For our purposes, a tag in the external scope is the same as
2451 a tag in the file scope. (Primarily relevant to Objective-C
2452 and its builtin structure tags, which get pushed before the
2453 file scope is created.) */
2454 if (B_IN_CURRENT_SCOPE (b)
2455 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2459 if (thislevel_only && !thislevel)
2462 if (TREE_CODE (b->decl) != code)
2464 /* Definition isn't the kind we were looking for. */
2465 pending_invalid_xref = name;
2466 pending_invalid_xref_location = input_location;
2468 /* If in the same binding level as a declaration as a tag
2469 of a different type, this must not be allowed to
2470 shadow that tag, so give the error immediately.
2471 (For example, "struct foo; union foo;" is invalid.) */
2473 pending_xref_error ();
2478 /* Print an error message now
2479 for a recent invalid struct, union or enum cross reference.
2480 We don't print them immediately because they are not invalid
2481 when used in the `struct foo;' construct for shadowing. */
2484 pending_xref_error (void)
2486 if (pending_invalid_xref != 0)
2487 error ("%H`%s' defined as wrong kind of tag",
2488 &pending_invalid_xref_location,
2489 IDENTIFIER_POINTER (pending_invalid_xref));
2490 pending_invalid_xref = 0;
2494 /* Look up NAME in the current scope and its superiors
2495 in the namespace of variables, functions and typedefs.
2496 Return a ..._DECL node of some kind representing its definition,
2497 or return 0 if it is undefined. */
2500 lookup_name (tree name)
2502 struct c_binding *b = I_SYMBOL_BINDING (name);
2503 if (b && !b->invisible)
2508 /* Similar to `lookup_name' but look only at the indicated scope. */
2511 lookup_name_in_scope (tree name, struct c_scope *scope)
2513 struct c_binding *b;
2515 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2516 if (B_IN_SCOPE (b, scope))
2521 /* Create the predefined scalar types of C,
2522 and some nodes representing standard constants (0, 1, (void *) 0).
2523 Initialize the global scope.
2524 Make definitions for built-in primitive functions. */
2527 c_init_decl_processing (void)
2530 tree ptr_ftype_void, ptr_ftype_ptr;
2531 location_t save_loc = input_location;
2533 /* Adds some ggc roots, and reserved words for c-parse.in. */
2536 current_function_decl = 0;
2538 /* Make the externals scope. */
2540 external_scope = current_scope;
2542 /* Declarations from c_common_nodes_and_builtins must not be associated
2543 with this input file, lest we get differences between using and not
2544 using preprocessed headers. */
2545 #ifdef USE_MAPPED_LOCATION
2546 input_location = BUILTINS_LOCATION;
2548 input_location.file = "<built-in>";
2549 input_location.line = 0;
2552 build_common_tree_nodes (flag_signed_char, false);
2554 c_common_nodes_and_builtins ();
2556 /* In C, comparisons and TRUTH_* expressions have type int. */
2557 truthvalue_type_node = integer_type_node;
2558 truthvalue_true_node = integer_one_node;
2559 truthvalue_false_node = integer_zero_node;
2561 /* Even in C99, which has a real boolean type. */
2562 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2563 boolean_type_node));
2565 endlink = void_list_node;
2566 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2568 = build_function_type (ptr_type_node,
2569 tree_cons (NULL_TREE, ptr_type_node, endlink));
2571 input_location = save_loc;
2573 pedantic_lvalues = true;
2575 make_fname_decl = c_make_fname_decl;
2576 start_fname_decls ();
2579 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2580 decl, NAME is the initialization string and TYPE_DEP indicates whether
2581 NAME depended on the type of the function. As we don't yet implement
2582 delayed emission of static data, we mark the decl as emitted
2583 so it is not placed in the output. Anything using it must therefore pull
2584 out the STRING_CST initializer directly. FIXME. */
2587 c_make_fname_decl (tree id, int type_dep)
2589 const char *name = fname_as_string (type_dep);
2590 tree decl, type, init;
2591 size_t length = strlen (name);
2593 type = build_array_type
2594 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2595 build_index_type (size_int (length)));
2597 decl = build_decl (VAR_DECL, id, type);
2599 TREE_STATIC (decl) = 1;
2600 TREE_READONLY (decl) = 1;
2601 DECL_ARTIFICIAL (decl) = 1;
2603 init = build_string (length + 1, name);
2604 free ((char *) name);
2605 TREE_TYPE (init) = type;
2606 DECL_INITIAL (decl) = init;
2608 TREE_USED (decl) = 1;
2610 if (current_function_decl)
2612 DECL_CONTEXT (decl) = current_function_decl;
2613 bind (id, decl, current_function_scope,
2614 /*invisible=*/false, /*nested=*/false);
2617 finish_decl (decl, init, NULL_TREE);
2622 /* Return a definition for a builtin function named NAME and whose data type
2623 is TYPE. TYPE should be a function type with argument types.
2624 FUNCTION_CODE tells later passes how to compile calls to this function.
2625 See tree.h for its possible values.
2627 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2628 the name to be called if we can't opencode the function. If
2629 ATTRS is nonzero, use that for the function's attribute list. */
2632 builtin_function (const char *name, tree type, int function_code,
2633 enum built_in_class cl, const char *library_name,
2636 tree id = get_identifier (name);
2637 tree decl = build_decl (FUNCTION_DECL, id, type);
2638 TREE_PUBLIC (decl) = 1;
2639 DECL_EXTERNAL (decl) = 1;
2640 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2641 DECL_BUILT_IN_CLASS (decl) = cl;
2642 DECL_FUNCTION_CODE (decl) = function_code;
2644 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2646 /* Should never be called on a symbol with a preexisting meaning. */
2647 if (I_SYMBOL_BINDING (id))
2650 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2652 /* Builtins in the implementation namespace are made visible without
2653 needing to be explicitly declared. See push_file_scope. */
2654 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2656 TREE_CHAIN (decl) = visible_builtins;
2657 visible_builtins = decl;
2660 /* Possibly apply some default attributes to this built-in function. */
2662 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2664 decl_attributes (&decl, NULL_TREE, 0);
2669 /* Called when a declaration is seen that contains no names to declare.
2670 If its type is a reference to a structure, union or enum inherited
2671 from a containing scope, shadow that tag name for the current scope
2672 with a forward reference.
2673 If its type defines a new named structure or union
2674 or defines an enum, it is valid but we need not do anything here.
2675 Otherwise, it is an error. */
2678 shadow_tag (tree declspecs)
2680 shadow_tag_warned (declspecs, 0);
2683 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2686 shadow_tag_warned (tree declspecs, int warned)
2692 pending_invalid_xref = 0;
2694 /* Remove the attributes from declspecs, since they will confuse the
2696 split_specs_attrs (declspecs, &specs, &attrs);
2698 for (link = specs; link; link = TREE_CHAIN (link))
2700 tree value = TREE_VALUE (link);
2701 enum tree_code code = TREE_CODE (value);
2703 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2704 /* Used to test also that TYPE_SIZE (value) != 0.
2705 That caused warning for `struct foo;' at top level in the file. */
2707 tree name = TYPE_NAME (value);
2714 if (warned != 1 && code != ENUMERAL_TYPE)
2715 /* Empty unnamed enum OK */
2717 pedwarn ("unnamed struct/union that defines no instances");
2723 t = lookup_tag (code, name, 1);
2727 t = make_node (code);
2734 if (!warned && ! in_system_header)
2736 warning ("useless keyword or type name in empty declaration");
2743 error ("two types specified in one empty declaration");
2748 pedwarn ("empty declaration");
2752 /* Construct an array declarator. EXPR is the expression inside [], or
2753 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2754 to the pointer to which a parameter array is converted). STATIC_P is
2755 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2756 is true if the array is [*], a VLA of unspecified length which is
2757 nevertheless a complete type (not currently implemented by GCC),
2758 false otherwise. The declarator is constructed as an ARRAY_REF
2759 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2760 left of the [] (filled by in set_array_declarator_inner) and operand 1
2761 is the expression inside; whose TREE_TYPE is the type qualifiers and
2762 which has TREE_STATIC set if "static" is used. */
2765 build_array_declarator (tree expr, tree quals, bool static_p,
2769 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2770 TREE_TYPE (decl) = quals;
2771 TREE_STATIC (decl) = (static_p ? 1 : 0);
2772 if (pedantic && !flag_isoc99)
2774 if (static_p || quals != NULL_TREE)
2775 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2777 pedwarn ("ISO C90 does not support `[*]' array declarators");
2780 warning ("GCC does not yet properly implement `[*]' array declarators");
2784 /* Set the type of an array declarator. DECL is the declarator, as
2785 constructed by build_array_declarator; TYPE is what appears on the left
2786 of the [] and goes in operand 0. ABSTRACT_P is true if it is an
2787 abstract declarator, false otherwise; this is used to reject static and
2788 type qualifiers in abstract declarators, where they are not in the
2792 set_array_declarator_inner (tree decl, tree type, bool abstract_p)
2794 TREE_OPERAND (decl, 0) = type;
2795 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2796 error ("static or type qualifiers in abstract declarator");
2800 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
2801 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
2803 The head of the declspec list is stored in DECLSPECS.
2804 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
2806 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
2807 the list elements. We drop the containing TREE_LIST nodes and link the
2808 resulting attributes together the way decl_attributes expects them. */
2811 split_specs_attrs (tree specs_attrs, tree *declspecs, tree *prefix_attributes)
2813 tree t, s, a, next, specs, attrs;
2815 /* This can happen after an __extension__ in pedantic mode. */
2816 if (specs_attrs != NULL_TREE
2817 && TREE_CODE (specs_attrs) == INTEGER_CST)
2819 *declspecs = NULL_TREE;
2820 *prefix_attributes = NULL_TREE;
2824 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
2825 if (specs_attrs != NULL_TREE
2826 && TREE_CODE (specs_attrs) != TREE_LIST)
2828 *declspecs = specs_attrs;
2829 *prefix_attributes = NULL_TREE;
2833 /* Remember to keep the lists in the same order, element-wise. */
2835 specs = s = NULL_TREE;
2836 attrs = a = NULL_TREE;
2837 for (t = specs_attrs; t; t = next)
2839 next = TREE_CHAIN (t);
2840 /* Declspecs have a non-NULL TREE_VALUE. */
2841 if (TREE_VALUE (t) != NULL_TREE)
2843 if (specs == NULL_TREE)
2851 /* The TREE_PURPOSE may also be empty in the case of
2852 __attribute__(()). */
2853 else if (TREE_PURPOSE (t) != NULL_TREE)
2855 if (attrs == NULL_TREE)
2856 attrs = a = TREE_PURPOSE (t);
2859 TREE_CHAIN (a) = TREE_PURPOSE (t);
2860 a = TREE_PURPOSE (t);
2862 /* More attrs can be linked here, move A to the end. */
2863 while (TREE_CHAIN (a) != NULL_TREE)
2868 /* Terminate the lists. */
2870 TREE_CHAIN (s) = NULL_TREE;
2872 TREE_CHAIN (a) = NULL_TREE;
2876 *prefix_attributes = attrs;
2879 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2882 groktypename (tree type_name)
2886 if (TREE_CODE (type_name) != TREE_LIST)
2889 split_specs_attrs (TREE_PURPOSE (type_name), &specs, &attrs);
2891 type_name = grokdeclarator (TREE_VALUE (type_name), specs, TYPENAME, false,
2894 /* Apply attributes. */
2895 decl_attributes (&type_name, attrs, 0);
2900 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2903 groktypename_in_parm_context (tree type_name)
2905 if (TREE_CODE (type_name) != TREE_LIST)
2907 return grokdeclarator (TREE_VALUE (type_name),
2908 TREE_PURPOSE (type_name),
2912 /* Decode a declarator in an ordinary declaration or data definition.
2913 This is called as soon as the type information and variable name
2914 have been parsed, before parsing the initializer if any.
2915 Here we create the ..._DECL node, fill in its type,
2916 and put it on the list of decls for the current context.
2917 The ..._DECL node is returned as the value.
2919 Exception: for arrays where the length is not specified,
2920 the type is left null, to be filled in by `finish_decl'.
2922 Function definitions do not come here; they go to start_function
2923 instead. However, external and forward declarations of functions
2924 do go through here. Structure field declarations are done by
2925 grokfield and not through here. */
2928 start_decl (tree declarator, tree declspecs, bool initialized, tree attributes)
2933 /* An object declared as __attribute__((deprecated)) suppresses
2934 warnings of uses of other deprecated items. */
2935 if (lookup_attribute ("deprecated", attributes))
2936 deprecated_state = DEPRECATED_SUPPRESS;
2938 decl = grokdeclarator (declarator, declspecs,
2939 NORMAL, initialized, NULL);
2941 deprecated_state = DEPRECATED_NORMAL;
2943 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2944 && MAIN_NAME_P (DECL_NAME (decl)))
2945 warning ("%J'%D' is usually a function", decl, decl);
2948 /* Is it valid for this decl to have an initializer at all?
2949 If not, set INITIALIZED to zero, which will indirectly
2950 tell 'finish_decl' to ignore the initializer once it is parsed. */
2951 switch (TREE_CODE (decl))
2954 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2959 error ("function '%D' is initialized like a variable", decl);
2964 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2965 error ("parameter '%D' is initialized", decl);
2970 /* Don't allow initializations for incomplete types except for
2971 arrays which might be completed by the initialization. */
2973 /* This can happen if the array size is an undefined macro.
2974 We already gave a warning, so we don't need another one. */
2975 if (TREE_TYPE (decl) == error_mark_node)
2977 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2979 /* A complete type is ok if size is fixed. */
2981 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2982 || C_DECL_VARIABLE_SIZE (decl))
2984 error ("variable-sized object may not be initialized");
2988 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2990 error ("variable '%D' has initializer but incomplete type", decl);
2993 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2995 error ("elements of array '%D' have incomplete type", decl);
3002 if (current_scope == file_scope)
3003 TREE_STATIC (decl) = 1;
3005 /* Tell 'pushdecl' this is an initialized decl
3006 even though we don't yet have the initializer expression.
3007 Also tell 'finish_decl' it may store the real initializer. */
3008 DECL_INITIAL (decl) = error_mark_node;
3011 /* If this is a function declaration, write a record describing it to the
3012 prototypes file (if requested). */
3014 if (TREE_CODE (decl) == FUNCTION_DECL)
3015 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3017 /* ANSI specifies that a tentative definition which is not merged with
3018 a non-tentative definition behaves exactly like a definition with an
3019 initializer equal to zero. (Section 3.7.2)
3021 -fno-common gives strict ANSI behavior, though this tends to break
3022 a large body of code that grew up without this rule.
3024 Thread-local variables are never common, since there's no entrenched
3025 body of code to break, and it allows more efficient variable references
3026 in the presence of dynamic linking. */
3028 if (TREE_CODE (decl) == VAR_DECL
3030 && TREE_PUBLIC (decl)
3031 && !DECL_THREAD_LOCAL (decl)
3033 DECL_COMMON (decl) = 1;
3035 /* Set attributes here so if duplicate decl, will have proper attributes. */
3036 decl_attributes (&decl, attributes, 0);
3038 if (TREE_CODE (decl) == FUNCTION_DECL
3039 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3041 tree ce = declarator;
3043 if (TREE_CODE (ce) == INDIRECT_REF)
3044 ce = TREE_OPERAND (declarator, 0);
3045 if (TREE_CODE (ce) == CALL_EXPR)
3047 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
3048 for (; args; args = TREE_CHAIN (args))
3050 tree type = TREE_TYPE (args);
3051 if (type && INTEGRAL_TYPE_P (type)
3052 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3053 DECL_ARG_TYPE (args) = integer_type_node;
3058 if (TREE_CODE (decl) == FUNCTION_DECL
3059 && DECL_DECLARED_INLINE_P (decl)
3060 && DECL_UNINLINABLE (decl)
3061 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3062 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
3064 /* Add this decl to the current scope.
3065 TEM may equal DECL or it may be a previous decl of the same name. */
3066 tem = pushdecl (decl);
3069 DECL_EXTERNAL (tem) = 0;
3074 /* Finish processing of a declaration;
3075 install its initial value.
3076 If the length of an array type is not known before,
3077 it must be determined now, from the initial value, or it is an error. */
3080 finish_decl (tree decl, tree init, tree asmspec_tree)
3082 tree type = TREE_TYPE (decl);
3083 int was_incomplete = (DECL_SIZE (decl) == 0);
3084 const char *asmspec = 0;
3086 /* If a name was specified, get the string. */
3087 if (current_scope == file_scope)
3088 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3090 asmspec = TREE_STRING_POINTER (asmspec_tree);
3092 /* If `start_decl' didn't like having an initialization, ignore it now. */
3093 if (init != 0 && DECL_INITIAL (decl) == 0)
3096 /* Don't crash if parm is initialized. */
3097 if (TREE_CODE (decl) == PARM_DECL)
3101 store_init_value (decl, init);
3103 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3104 || TREE_CODE (decl) == FUNCTION_DECL
3105 || TREE_CODE (decl) == FIELD_DECL))
3106 objc_check_decl (decl);
3108 /* Deduce size of array from initialization, if not already known. */
3109 if (TREE_CODE (type) == ARRAY_TYPE
3110 && TYPE_DOMAIN (type) == 0
3111 && TREE_CODE (decl) != TYPE_DECL)
3114 = (TREE_STATIC (decl)
3115 /* Even if pedantic, an external linkage array
3116 may have incomplete type at first. */
3117 ? pedantic && !TREE_PUBLIC (decl)
3118 : !DECL_EXTERNAL (decl));
3120 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3122 /* Get the completed type made by complete_array_type. */
3123 type = TREE_TYPE (decl);
3126 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
3128 else if (failure == 2)
3131 error ("%Jarray size missing in '%D'", decl, decl);
3132 /* If a `static' var's size isn't known,
3133 make it extern as well as static, so it does not get
3135 If it is not `static', then do not mark extern;
3136 finish_incomplete_decl will give it a default size
3137 and it will get allocated. */
3138 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3139 DECL_EXTERNAL (decl) = 1;
3142 /* TYPE_MAX_VALUE is always one less than the number of elements
3143 in the array, because we start counting at zero. Therefore,
3144 warn only if the value is less than zero. */
3145 else if (pedantic && TYPE_DOMAIN (type) != 0
3146 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3147 error ("%Jzero or negative size array '%D'", decl, decl);
3149 layout_decl (decl, 0);
3152 if (TREE_CODE (decl) == VAR_DECL)
3154 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3155 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3156 layout_decl (decl, 0);
3158 if (DECL_SIZE (decl) == 0
3159 /* Don't give an error if we already gave one earlier. */
3160 && TREE_TYPE (decl) != error_mark_node
3161 && (TREE_STATIC (decl)
3162 /* A static variable with an incomplete type
3163 is an error if it is initialized.
3164 Also if it is not file scope.
3165 Otherwise, let it through, but if it is not `extern'
3166 then it may cause an error message later. */
3167 ? (DECL_INITIAL (decl) != 0
3168 || !DECL_FILE_SCOPE_P (decl))
3169 /* An automatic variable with an incomplete type
3171 : !DECL_EXTERNAL (decl)))
3173 error ("%Jstorage size of '%D' isn't known", decl, decl);
3174 TREE_TYPE (decl) = error_mark_node;
3177 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3178 && DECL_SIZE (decl) != 0)
3180 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3181 constant_expression_warning (DECL_SIZE (decl));
3183 error ("%Jstorage size of '%D' isn't constant", decl, decl);
3186 if (TREE_USED (type))
3187 TREE_USED (decl) = 1;
3190 /* If this is a function and an assembler name is specified, reset DECL_RTL
3191 so we can give it its new name. Also, update built_in_decls if it
3192 was a normal built-in. */
3193 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3195 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3197 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
3198 set_user_assembler_name (builtin, asmspec);
3199 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
3200 init_block_move_fn (asmspec);
3201 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
3202 init_block_clear_fn (asmspec);
3204 set_user_assembler_name (decl, asmspec);
3207 /* If #pragma weak was used, mark the decl weak now. */
3208 if (current_scope == file_scope)
3209 maybe_apply_pragma_weak (decl);
3211 /* If this is a variable definition, determine its ELF visibility. */
3212 if (TREE_CODE (decl) == VAR_DECL
3213 && TREE_STATIC (decl)
3214 && !DECL_EXTERNAL (decl))
3215 c_determine_visibility (decl);
3217 /* Output the assembler code and/or RTL code for variables and functions,
3218 unless the type is an undefined structure or union.
3219 If not, it will get done when the type is completed. */
3221 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3223 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3224 if (c_dialect_objc ())
3225 objc_check_decl (decl);
3229 /* If this is not a static variable, issue a warning.
3230 It doesn't make any sense to give an ASMSPEC for an
3231 ordinary, non-register local variable. Historically,
3232 GCC has accepted -- but ignored -- the ASMSPEC in
3234 if (! DECL_FILE_SCOPE_P (decl)
3235 && TREE_CODE (decl) == VAR_DECL
3236 && !C_DECL_REGISTER (decl)
3237 && !TREE_STATIC (decl))
3238 warning ("%Jignoring asm-specifier for non-static local "
3239 "variable '%D'", decl, decl);
3240 else if (C_DECL_REGISTER (decl))
3241 change_decl_assembler_name (decl, get_identifier (asmspec));
3243 set_user_assembler_name (decl, asmspec);
3246 if (DECL_FILE_SCOPE_P (decl))
3248 if (DECL_INITIAL (decl) == NULL_TREE
3249 || DECL_INITIAL (decl) == error_mark_node)
3250 /* Don't output anything
3251 when a tentative file-scope definition is seen.
3252 But at end of compilation, do output code for them. */
3253 DECL_DEFER_OUTPUT (decl) = 1;
3254 rest_of_decl_compilation (decl, true, 0);
3258 /* In conjunction with an ASMSPEC, the `register'
3259 keyword indicates that we should place the variable
3260 in a particular register. */
3261 if (asmspec && C_DECL_REGISTER (decl))
3263 DECL_HARD_REGISTER (decl) = 1;
3264 /* This cannot be done for a structure with volatile
3265 fields, on which DECL_REGISTER will have been
3267 if (!DECL_REGISTER (decl))
3268 error ("cannot put object with volatile field into register");
3271 if (TREE_CODE (decl) != FUNCTION_DECL)
3273 /* If we're building a variable sized type, and we might be
3274 reachable other than via the top of the current binding
3275 level, then create a new BIND_EXPR so that we deallocate
3276 the object at the right time. */
3277 /* Note that DECL_SIZE can be null due to errors. */
3278 if (DECL_SIZE (decl)
3279 && !TREE_CONSTANT (DECL_SIZE (decl))
3280 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3283 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3284 TREE_SIDE_EFFECTS (bind) = 1;
3286 BIND_EXPR_BODY (bind) = push_stmt_list ();
3288 add_stmt (build_stmt (DECL_EXPR, decl));
3293 if (!DECL_FILE_SCOPE_P (decl))
3295 /* Recompute the RTL of a local array now
3296 if it used to be an incomplete type. */
3298 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3300 /* If we used it already as memory, it must stay in memory. */
3301 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3302 /* If it's still incomplete now, no init will save it. */
3303 if (DECL_SIZE (decl) == 0)
3304 DECL_INITIAL (decl) = 0;
3309 /* If this was marked 'used', be sure it will be output. */
3310 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3311 mark_decl_referenced (decl);
3313 if (TREE_CODE (decl) == TYPE_DECL)
3315 if (!DECL_FILE_SCOPE_P (decl)
3316 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3317 add_stmt (build_stmt (DECL_EXPR, decl));
3319 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3322 /* At the end of a declaration, throw away any variable type sizes
3323 of types defined inside that declaration. There is no use
3324 computing them in the following function definition. */
3325 if (current_scope == file_scope)
3326 get_pending_sizes ();
3328 /* Install a cleanup (aka destructor) if one was given. */
3329 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3331 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3334 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3335 tree cleanup_decl = lookup_name (cleanup_id);
3338 /* Build "cleanup(&decl)" for the destructor. */
3339 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3340 cleanup = build_tree_list (NULL_TREE, cleanup);
3341 cleanup = build_function_call (cleanup_decl, cleanup);
3343 /* Don't warn about decl unused; the cleanup uses it. */
3344 TREE_USED (decl) = 1;
3345 TREE_USED (cleanup_decl) = 1;
3347 /* Initialize EH, if we've been told to do so. */
3348 if (flag_exceptions && !c_eh_initialized_p)
3350 c_eh_initialized_p = true;
3351 eh_personality_libfunc
3352 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3353 ? "__gcc_personality_sj0"
3354 : "__gcc_personality_v0");
3355 using_eh_for_cleanups ();
3358 push_cleanup (decl, cleanup, false);
3363 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3366 grokparm (tree parm)
3368 tree decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3369 TREE_PURPOSE (TREE_PURPOSE (parm)),
3372 decl_attributes (&decl, TREE_VALUE (parm), 0);
3377 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3378 and push that on the current scope. */
3381 push_parm_decl (tree parm)
3385 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3386 TREE_PURPOSE (TREE_PURPOSE (parm)),
3388 decl_attributes (&decl, TREE_VALUE (parm), 0);
3390 decl = pushdecl (decl);
3392 finish_decl (decl, NULL_TREE, NULL_TREE);
3395 /* Mark all the parameter declarations to date as forward decls.
3396 Also diagnose use of this extension. */
3399 mark_forward_parm_decls (void)
3401 struct c_binding *b;
3403 if (pedantic && !current_scope->warned_forward_parm_decls)
3405 pedwarn ("ISO C forbids forward parameter declarations");
3406 current_scope->warned_forward_parm_decls = true;
3409 for (b = current_scope->bindings; b; b = b->prev)
3410 if (TREE_CODE (b->decl) == PARM_DECL)
3411 TREE_ASM_WRITTEN (b->decl) = 1;
3414 static GTY(()) int compound_literal_number;
3416 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3417 literal, which may be an incomplete array type completed by the
3418 initializer; INIT is a CONSTRUCTOR that initializes the compound
3422 build_compound_literal (tree type, tree init)
3424 /* We do not use start_decl here because we have a type, not a declarator;
3425 and do not use finish_decl because the decl should be stored inside
3426 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3427 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3430 DECL_EXTERNAL (decl) = 0;
3431 TREE_PUBLIC (decl) = 0;
3432 TREE_STATIC (decl) = (current_scope == file_scope);
3433 DECL_CONTEXT (decl) = current_function_decl;
3434 TREE_USED (decl) = 1;
3435 TREE_TYPE (decl) = type;
3436 TREE_READONLY (decl) = TYPE_READONLY (type);
3437 store_init_value (decl, init);
3439 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3441 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3446 type = TREE_TYPE (decl);
3447 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3448 return error_mark_node;
3450 stmt = build_stmt (DECL_EXPR, decl);
3451 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3452 TREE_SIDE_EFFECTS (complit) = 1;
3454 layout_decl (decl, 0);
3456 if (TREE_STATIC (decl))
3458 /* This decl needs a name for the assembler output. We also need
3459 a unique suffix to be added to the name. */
3462 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3463 compound_literal_number);
3464 compound_literal_number++;
3465 DECL_NAME (decl) = get_identifier (name);
3466 DECL_DEFER_OUTPUT (decl) = 1;
3467 DECL_COMDAT (decl) = 1;
3468 DECL_ARTIFICIAL (decl) = 1;
3470 rest_of_decl_compilation (decl, 1, 0);
3476 /* Make TYPE a complete type based on INITIAL_VALUE.
3477 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3478 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3481 complete_array_type (tree type, tree initial_value, int do_default)
3483 tree maxindex = NULL_TREE;
3488 /* Note MAXINDEX is really the maximum index,
3489 one less than the size. */
3490 if (TREE_CODE (initial_value) == STRING_CST)
3493 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3494 maxindex = build_int_cst (NULL_TREE,
3495 (TREE_STRING_LENGTH (initial_value)
3498 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3500 tree elts = CONSTRUCTOR_ELTS (initial_value);
3501 maxindex = build_int_cst (NULL_TREE, -1);
3502 for (; elts; elts = TREE_CHAIN (elts))
3504 if (TREE_PURPOSE (elts))
3505 maxindex = TREE_PURPOSE (elts);
3507 maxindex = fold (build2 (PLUS_EXPR, integer_type_node,
3508 maxindex, integer_one_node));
3513 /* Make an error message unless that happened already. */
3514 if (initial_value != error_mark_node)
3517 /* Prevent further error messages. */
3518 maxindex = build_int_cst (NULL_TREE, 0);
3525 maxindex = build_int_cst (NULL_TREE, 0);
3531 TYPE_DOMAIN (type) = build_index_type (maxindex);
3532 if (!TREE_TYPE (maxindex))
3536 /* Lay out the type now that we can get the real answer. */
3543 /* Determine whether TYPE is a structure with a flexible array member,
3544 or a union containing such a structure (possibly recursively). */
3547 flexible_array_type_p (tree type)
3550 switch (TREE_CODE (type))
3553 x = TYPE_FIELDS (type);
3556 while (TREE_CHAIN (x) != NULL_TREE)
3558 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3559 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3560 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3561 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3565 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3567 if (flexible_array_type_p (TREE_TYPE (x)))
3576 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3577 replacing with appropriate values if they are invalid. */
3579 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3582 unsigned int max_width;
3583 unsigned HOST_WIDE_INT w;
3584 const char *name = orig_name ? orig_name: _("<anonymous>");
3587 STRIP_NOPS (*width);
3589 /* Detect and ignore out of range field width and process valid
3591 if (TREE_CODE (*width) != INTEGER_CST)
3593 error ("bit-field `%s' width not an integer constant", name);
3594 *width = integer_one_node;
3598 constant_expression_warning (*width);
3599 if (tree_int_cst_sgn (*width) < 0)
3601 error ("negative width in bit-field `%s'", name);
3602 *width = integer_one_node;
3604 else if (integer_zerop (*width) && orig_name)
3606 error ("zero width for bit-field `%s'", name);
3607 *width = integer_one_node;
3611 /* Detect invalid bit-field type. */
3612 if (TREE_CODE (*type) != INTEGER_TYPE
3613 && TREE_CODE (*type) != BOOLEAN_TYPE
3614 && TREE_CODE (*type) != ENUMERAL_TYPE)
3616 error ("bit-field `%s' has invalid type", name);
3617 *type = unsigned_type_node;
3620 type_mv = TYPE_MAIN_VARIANT (*type);
3622 && type_mv != integer_type_node
3623 && type_mv != unsigned_type_node
3624 && type_mv != boolean_type_node)
3625 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3627 if (type_mv == boolean_type_node)
3628 max_width = CHAR_TYPE_SIZE;
3630 max_width = TYPE_PRECISION (*type);
3632 if (0 < compare_tree_int (*width, max_width))
3634 error ("width of `%s' exceeds its type", name);
3636 *width = build_int_cst (NULL_TREE, w);
3639 w = tree_low_cst (*width, 1);
3641 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3643 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3645 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3646 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3647 warning ("`%s' is narrower than values of its type", name);
3651 /* Given declspecs and a declarator,
3652 determine the name and type of the object declared
3653 and construct a ..._DECL node for it.
3654 (In one case we can return a ..._TYPE node instead.
3655 For invalid input we sometimes return 0.)
3657 DECLSPECS is a chain of tree_list nodes whose value fields
3658 are the storage classes and type specifiers.
3660 DECL_CONTEXT says which syntactic context this declaration is in:
3661 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3662 FUNCDEF for a function definition. Like NORMAL but a few different
3663 error messages in each case. Return value may be zero meaning
3664 this definition is too screwy to try to parse.
3665 PARM for a parameter declaration (either within a function prototype
3666 or before a function body). Make a PARM_DECL, or return void_type_node.
3667 TYPENAME if for a typename (in a cast or sizeof).
3668 Don't make a DECL node; just return the ..._TYPE node.
3669 FIELD for a struct or union field; make a FIELD_DECL.
3670 INITIALIZED is true if the decl has an initializer.
3671 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3672 representing the width of the bit-field.
3674 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3675 It may also be so in the PARM case, for a prototype where the
3676 argument type is specified but not the name.
3678 This function is where the complicated C meanings of `static'
3679 and `extern' are interpreted. */
3682 grokdeclarator (tree declarator, tree declspecs,
3683 enum decl_context decl_context, bool initialized, tree *width)
3687 tree type = NULL_TREE;
3692 int type_quals = TYPE_UNQUALIFIED;
3694 int explicit_int = 0;
3695 int explicit_char = 0;
3696 int defaulted_int = 0;
3697 tree typedef_decl = 0;
3698 const char *name, *orig_name;
3699 tree typedef_type = 0;
3700 int funcdef_flag = 0;
3701 enum tree_code innermost_code = ERROR_MARK;
3702 int size_varies = 0;
3703 tree decl_attr = NULL_TREE;
3704 tree array_ptr_quals = NULL_TREE;
3705 int array_parm_static = 0;
3706 tree returned_attrs = NULL_TREE;
3707 bool bitfield = width != NULL;
3709 tree arg_info = NULL_TREE;
3711 if (decl_context == FUNCDEF)
3712 funcdef_flag = 1, decl_context = NORMAL;
3714 /* Look inside a declarator for the name being declared
3715 and get it as a string, for an error message. */
3717 tree decl = declarator;
3721 switch (TREE_CODE (decl))
3726 innermost_code = TREE_CODE (decl);
3727 decl = TREE_OPERAND (decl, 0);
3731 decl = TREE_VALUE (decl);
3734 case IDENTIFIER_NODE:
3735 name = IDENTIFIER_POINTER (decl);
3747 /* A function definition's declarator must have the form of
3748 a function declarator. */
3750 if (funcdef_flag && innermost_code != CALL_EXPR)
3753 /* If this looks like a function definition, make it one,
3754 even if it occurs where parms are expected.
3755 Then store_parm_decls will reject it and not use it as a parm. */
3756 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3757 decl_context = PARM;
3759 /* Look through the decl specs and record which ones appear.
3760 Some typespecs are defined as built-in typenames.
3761 Others, the ones that are modifiers of other types,
3762 are represented by bits in SPECBITS: set the bits for
3763 the modifiers that appear. Storage class keywords are also in SPECBITS.
3765 If there is a typedef name or a type, store the type in TYPE.
3766 This includes builtin typedefs such as `int'.
3768 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3769 and did not come from a user typedef.
3771 Set LONGLONG if `long' is mentioned twice. */
3773 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3775 tree id = TREE_VALUE (spec);
3777 /* If the entire declaration is itself tagged as deprecated then
3778 suppress reports of deprecated items. */
3779 if (id && TREE_DEPRECATED (id))
3781 if (deprecated_state != DEPRECATED_SUPPRESS)
3782 warn_deprecated_use (id);
3785 if (id == ridpointers[(int) RID_INT])
3787 if (id == ridpointers[(int) RID_CHAR])
3790 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3792 enum rid i = C_RID_CODE (id);
3793 if ((int) i <= (int) RID_LAST_MODIFIER)
3795 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3798 error ("`long long long' is too long for GCC");
3801 if (pedantic && !flag_isoc99 && ! in_system_header
3803 pedwarn ("ISO C90 does not support `long long'");
3807 else if (specbits & (1 << (int) i))
3809 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3811 if (pedantic && !flag_isoc99)
3812 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3815 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3818 /* Diagnose "__thread extern". Recall that this list
3819 is in the reverse order seen in the text. */
3821 && (specbits & (1 << (int) RID_EXTERN
3822 | 1 << (int) RID_STATIC)))
3824 if (specbits & 1 << (int) RID_EXTERN)
3825 error ("`__thread' before `extern'");
3827 error ("`__thread' before `static'");
3830 specbits |= 1 << (int) i;
3835 error ("two or more data types in declaration of `%s'", name);
3836 /* Actual typedefs come to us as TYPE_DECL nodes. */
3837 else if (TREE_CODE (id) == TYPE_DECL)
3839 if (TREE_TYPE (id) == error_mark_node)
3840 ; /* Allow the type to default to int to avoid cascading errors. */
3843 type = TREE_TYPE (id);
3844 decl_attr = DECL_ATTRIBUTES (id);
3848 /* Built-in types come as identifiers. */
3849 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3851 tree t = lookup_name (id);
3852 if (!t || TREE_CODE (t) != TYPE_DECL)
3853 error ("`%s' fails to be a typedef or built in type",
3854 IDENTIFIER_POINTER (id));
3855 else if (TREE_TYPE (t) == error_mark_node)
3859 type = TREE_TYPE (t);
3863 else if (TREE_CODE (id) != ERROR_MARK)
3870 typedef_type = type;
3872 size_varies = C_TYPE_VARIABLE_SIZE (type);
3874 /* No type at all: default to `int', and set DEFAULTED_INT
3875 because it was not a user-defined typedef. */
3879 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3880 | (1 << (int) RID_SIGNED)
3881 | (1 << (int) RID_UNSIGNED)
3882 | (1 << (int) RID_COMPLEX))))
3883 /* Don't warn about typedef foo = bar. */
3884 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3885 && ! in_system_header)
3887 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3888 and this is a function, or if -Wimplicit; prefer the former
3889 warning since it is more explicit. */
3890 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3892 warn_about_return_type = 1;
3893 else if (warn_implicit_int || flag_isoc99)
3894 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3899 type = integer_type_node;
3902 /* Now process the modifiers that were specified
3903 and check for invalid combinations. */
3905 /* Long double is a special combination. */
3907 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3908 && TYPE_MAIN_VARIANT (type) == double_type_node)
3910 specbits &= ~(1 << (int) RID_LONG);
3911 type = long_double_type_node;
3914 /* Check all other uses of type modifiers. */
3916 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3917 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3921 if ((specbits & 1 << (int) RID_LONG)
3922 && (specbits & 1 << (int) RID_SHORT))
3923 error ("both long and short specified for `%s'", name);
3924 else if (((specbits & 1 << (int) RID_LONG)
3925 || (specbits & 1 << (int) RID_SHORT))
3927 error ("long or short specified with char for `%s'", name);
3928 else if (((specbits & 1 << (int) RID_LONG)
3929 || (specbits & 1 << (int) RID_SHORT))
3930 && TREE_CODE (type) == REAL_TYPE)
3932 static int already = 0;
3934 error ("long or short specified with floating type for `%s'", name);
3935 if (! already && ! pedantic)
3937 error ("the only valid combination is `long double'");
3941 else if ((specbits & 1 << (int) RID_SIGNED)
3942 && (specbits & 1 << (int) RID_UNSIGNED))
3943 error ("both signed and unsigned specified for `%s'", name);
3944 else if (TREE_CODE (type) != INTEGER_TYPE)
3945 error ("long, short, signed or unsigned invalid for `%s'", name);
3949 if (!explicit_int && !defaulted_int && !explicit_char)
3951 error ("long, short, signed or unsigned used invalidly for `%s'",
3957 /* Discard the type modifiers if they are invalid. */
3960 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3961 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3966 if ((specbits & (1 << (int) RID_COMPLEX))
3967 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3969 error ("complex invalid for `%s'", name);
3970 specbits &= ~(1 << (int) RID_COMPLEX);
3973 /* Decide whether an integer type is signed or not.
3974 Optionally treat bit-fields as signed by default. */
3975 if (specbits & 1 << (int) RID_UNSIGNED
3976 || (bitfield && ! flag_signed_bitfields
3977 && (explicit_int || defaulted_int || explicit_char
3978 /* A typedef for plain `int' without `signed'
3979 can be controlled just like plain `int'. */
3980 || ! (typedef_decl != 0
3981 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3982 && TREE_CODE (type) != ENUMERAL_TYPE
3983 && !(specbits & 1 << (int) RID_SIGNED)))
3986 type = long_long_unsigned_type_node;
3987 else if (specbits & 1 << (int) RID_LONG)
3988 type = long_unsigned_type_node;
3989 else if (specbits & 1 << (int) RID_SHORT)
3990 type = short_unsigned_type_node;
3991 else if (type == char_type_node)
3992 type = unsigned_char_type_node;
3993 else if (typedef_decl)
3994 type = c_common_unsigned_type (type);
3996 type = unsigned_type_node;
3998 else if ((specbits & 1 << (int) RID_SIGNED)
3999 && type == char_type_node)
4000 type = signed_char_type_node;
4002 type = long_long_integer_type_node;
4003 else if (specbits & 1 << (int) RID_LONG)
4004 type = long_integer_type_node;
4005 else if (specbits & 1 << (int) RID_SHORT)
4006 type = short_integer_type_node;
4008 if (specbits & 1 << (int) RID_COMPLEX)
4010 if (pedantic && !flag_isoc99)
4011 pedwarn ("ISO C90 does not support complex types");
4012 /* If we just have "complex", it is equivalent to
4013 "complex double", but if any modifiers at all are specified it is
4014 the complex form of TYPE. E.g, "complex short" is
4015 "complex short int". */
4017 if (defaulted_int && ! longlong
4018 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
4019 | (1 << (int) RID_SIGNED)
4020 | (1 << (int) RID_UNSIGNED))))
4023 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
4024 type = complex_double_type_node;
4026 else if (type == integer_type_node)
4029 pedwarn ("ISO C does not support complex integer types");
4030 type = complex_integer_type_node;
4032 else if (type == float_type_node)
4033 type = complex_float_type_node;
4034 else if (type == double_type_node)
4035 type = complex_double_type_node;
4036 else if (type == long_double_type_node)
4037 type = complex_long_double_type_node;
4041 pedwarn ("ISO C does not support complex integer types");
4042 type = build_complex_type (type);
4046 /* Check the type and width of a bit-field. */
4048 check_bitfield_type_and_width (&type, width, orig_name);
4050 /* Figure out the type qualifiers for the declaration. There are
4051 two ways a declaration can become qualified. One is something
4052 like `const int i' where the `const' is explicit. Another is
4053 something like `typedef const int CI; CI i' where the type of the
4054 declaration contains the `const'. A third possibility is that
4055 there is a type qualifier on the element type of a typedefed
4056 array type, in which case we should extract that qualifier so
4057 that c_apply_type_quals_to_decls receives the full list of
4058 qualifiers to work with (C90 is not entirely clear about whether
4059 duplicate qualifiers should be diagnosed in this case, but it
4060 seems most appropriate to do so). */
4061 element_type = strip_array_types (type);
4062 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
4064 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
4066 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
4067 inlinep = !! (specbits & (1 << (int) RID_INLINE));
4068 if (pedantic && !flag_isoc99)
4071 pedwarn ("duplicate `const'");
4073 pedwarn ("duplicate `restrict'");
4075 pedwarn ("duplicate `volatile'");
4077 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
4078 type = TYPE_MAIN_VARIANT (type);
4079 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4080 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4081 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4083 /* Warn if two storage classes are given. Default to `auto'. */
4088 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4089 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4090 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4091 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4092 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
4094 /* "static __thread" and "extern __thread" are allowed. */
4095 if ((specbits & (1 << (int) RID_THREAD
4096 | 1 << (int) RID_STATIC
4097 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
4100 /* Warn about storage classes that are invalid for certain
4101 kinds of declarations (parameters, typenames, etc.). */
4104 error ("multiple storage classes in declaration of `%s'", name);
4105 else if (funcdef_flag
4107 & ((1 << (int) RID_REGISTER)
4108 | (1 << (int) RID_AUTO)
4109 | (1 << (int) RID_TYPEDEF)
4110 | (1 << (int) RID_THREAD))))
4112 if (specbits & 1 << (int) RID_AUTO
4113 && (pedantic || current_scope == file_scope))
4114 pedwarn ("function definition declared `auto'");
4115 if (specbits & 1 << (int) RID_REGISTER)
4116 error ("function definition declared `register'");
4117 if (specbits & 1 << (int) RID_TYPEDEF)
4118 error ("function definition declared `typedef'");
4119 if (specbits & 1 << (int) RID_THREAD)
4120 error ("function definition declared `__thread'");
4121 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4122 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
4124 else if (decl_context != NORMAL && nclasses > 0)
4126 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4130 switch (decl_context)
4133 error ("storage class specified for structure field `%s'",
4137 error ("storage class specified for parameter `%s'", name);
4140 error ("storage class specified for typename");
4143 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4144 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4145 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
4148 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4150 /* `extern' with initialization is invalid if not at file scope. */
4151 if (current_scope == file_scope)
4152 warning ("`%s' initialized and declared `extern'", name);
4154 error ("`%s' has both `extern' and initializer", name);
4156 else if (current_scope == file_scope)
4158 if (specbits & 1 << (int) RID_AUTO)
4159 error ("file-scope declaration of `%s' specifies `auto'", name);
4163 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
4164 error ("nested function `%s' declared `extern'", name);
4165 else if ((specbits & (1 << (int) RID_THREAD
4166 | 1 << (int) RID_EXTERN
4167 | 1 << (int) RID_STATIC))
4168 == (1 << (int) RID_THREAD))
4170 error ("function-scope `%s' implicitly auto and declared `__thread'",
4172 specbits &= ~(1 << (int) RID_THREAD);
4177 /* Now figure out the structure of the declarator proper.
4178 Descend through it, creating more complex types, until we reach
4179 the declared identifier (or NULL_TREE, in an absolute declarator). */
4181 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4183 if (type == error_mark_node)
4185 declarator = TREE_OPERAND (declarator, 0);
4189 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4190 an INDIRECT_REF (for *...),
4191 a CALL_EXPR (for ...(...)),
4192 a TREE_LIST (for nested attributes),
4193 an identifier (for the name being declared)
4194 or a null pointer (for the place in an absolute declarator
4195 where the name was omitted).
4196 For the last two cases, we have just exited the loop.
4198 At this point, TYPE is the type of elements of an array,
4199 or for a function to return, or for a pointer to point to.
4200 After this sequence of ifs, TYPE is the type of the
4201 array or function or pointer, and DECLARATOR has had its
4202 outermost layer removed. */
4204 if (array_ptr_quals != NULL_TREE || array_parm_static)
4206 /* Only the innermost declarator (making a parameter be of
4207 array type which is converted to pointer type)
4208 may have static or type qualifiers. */
4209 error ("static or type qualifiers in non-parameter array declarator");
4210 array_ptr_quals = NULL_TREE;
4211 array_parm_static = 0;
4214 if (TREE_CODE (declarator) == TREE_LIST)
4216 /* We encode a declarator with embedded attributes using
4218 tree attrs = TREE_PURPOSE (declarator);
4221 declarator = TREE_VALUE (declarator);
4222 inner_decl = declarator;
4223 while (inner_decl != NULL_TREE
4224 && TREE_CODE (inner_decl) == TREE_LIST)
4225 inner_decl = TREE_VALUE (inner_decl);
4226 if (inner_decl == NULL_TREE
4227 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
4228 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4229 else if (TREE_CODE (inner_decl) == CALL_EXPR)
4230 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4231 else if (TREE_CODE (inner_decl) == ARRAY_REF)
4232 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4233 returned_attrs = decl_attributes (&type,
4234 chainon (returned_attrs, attrs),
4237 else if (TREE_CODE (declarator) == ARRAY_REF)
4239 tree itype = NULL_TREE;
4240 tree size = TREE_OPERAND (declarator, 1);
4241 /* The index is a signed object `sizetype' bits wide. */
4242 tree index_type = c_common_signed_type (sizetype);
4244 array_ptr_quals = TREE_TYPE (declarator);
4245 array_parm_static = TREE_STATIC (declarator);
4247 declarator = TREE_OPERAND (declarator, 0);
4249 /* Check for some types that there cannot be arrays of. */
4251 if (VOID_TYPE_P (type))
4253 error ("declaration of `%s' as array of voids", name);
4254 type = error_mark_node;
4257 if (TREE_CODE (type) == FUNCTION_TYPE)
4259 error ("declaration of `%s' as array of functions", name);
4260 type = error_mark_node;
4263 if (pedantic && !in_system_header && flexible_array_type_p (type))
4264 pedwarn ("invalid use of structure with flexible array member");
4266 if (size == error_mark_node)
4267 type = error_mark_node;
4269 if (type == error_mark_node)
4272 /* If size was specified, set ITYPE to a range-type for that size.
4273 Otherwise, ITYPE remains null. finish_decl may figure it out
4274 from an initial value. */
4278 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4279 STRIP_TYPE_NOPS (size);
4281 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4283 error ("size of array `%s' has non-integer type", name);
4284 size = integer_one_node;
4287 if (pedantic && integer_zerop (size))
4288 pedwarn ("ISO C forbids zero-size array `%s'", name);
4290 if (TREE_CODE (size) == INTEGER_CST)
4292 constant_expression_warning (size);
4293 if (tree_int_cst_sgn (size) < 0)
4295 error ("size of array `%s' is negative", name);
4296 size = integer_one_node;
4301 /* Make sure the array size remains visibly nonconstant
4302 even if it is (eg) a const variable with known value. */
4305 if (!flag_isoc99 && pedantic)
4307 if (TREE_CONSTANT (size))
4308 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4311 pedwarn ("ISO C90 forbids variable-size array `%s'",
4316 if (integer_zerop (size))
4318 /* A zero-length array cannot be represented with an
4319 unsigned index type, which is what we'll get with
4320 build_index_type. Create an open-ended range instead. */
4321 itype = build_range_type (sizetype, size, NULL_TREE);
4325 /* Compute the maximum valid index, that is, size - 1.
4326 Do the calculation in index_type, so that if it is
4327 a variable the computations will be done in the
4329 itype = fold (build2 (MINUS_EXPR, index_type,
4330 convert (index_type, size),
4331 convert (index_type, size_one_node)));
4333 /* If that overflowed, the array is too big.
4334 ??? While a size of INT_MAX+1 technically shouldn't
4335 cause an overflow (because we subtract 1), the overflow
4336 is recorded during the conversion to index_type, before
4337 the subtraction. Handling this case seems like an
4338 unnecessary complication. */
4339 if (TREE_OVERFLOW (itype))
4341 error ("size of array `%s' is too large", name);
4342 type = error_mark_node;
4347 itype = variable_size (itype);
4348 itype = build_index_type (itype);
4351 else if (decl_context == FIELD)
4353 if (pedantic && !flag_isoc99 && !in_system_header)
4354 pedwarn ("ISO C90 does not support flexible array members");
4356 /* ISO C99 Flexible array members are effectively identical
4357 to GCC's zero-length array extension. */
4358 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4361 /* If pedantic, complain about arrays of incomplete types. */
4363 if (pedantic && !COMPLETE_TYPE_P (type))
4364 pedwarn ("array type has incomplete element type");
4366 /* Build the array type itself, then merge any constancy or
4367 volatility into the target type. We must do it in this order
4368 to ensure that the TYPE_MAIN_VARIANT field of the array type
4369 is set correctly. */
4371 type = build_array_type (type, itype);
4373 type = c_build_qualified_type (type, type_quals);
4376 C_TYPE_VARIABLE_SIZE (type) = 1;
4378 /* The GCC extension for zero-length arrays differs from
4379 ISO flexible array members in that sizeof yields zero. */
4380 if (size && integer_zerop (size))
4383 TYPE_SIZE (type) = bitsize_zero_node;
4384 TYPE_SIZE_UNIT (type) = size_zero_node;
4386 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4387 /* We can never complete an array type which is the target of a
4388 pointer, so go ahead and lay it out. */
4391 if (decl_context != PARM
4392 && (array_ptr_quals != NULL_TREE || array_parm_static))
4394 error ("static or type qualifiers in non-parameter array declarator");
4395 array_ptr_quals = NULL_TREE;
4396 array_parm_static = 0;
4399 else if (TREE_CODE (declarator) == CALL_EXPR)
4401 /* Say it's a definition only for the declarator closest to
4402 the identifier, apart possibly from some attributes. */
4403 bool really_funcdef = false;
4407 tree t = TREE_OPERAND (declarator, 0);
4408 while (TREE_CODE (t) == TREE_LIST)
4410 really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE);
4413 /* Declaring a function type.
4414 Make sure we have a valid type for the function to return. */
4415 if (type == error_mark_node)
4420 /* Warn about some types functions can't return. */
4422 if (TREE_CODE (type) == FUNCTION_TYPE)
4424 error ("`%s' declared as function returning a function", name);
4425 type = integer_type_node;
4427 if (TREE_CODE (type) == ARRAY_TYPE)
4429 error ("`%s' declared as function returning an array", name);
4430 type = integer_type_node;
4433 /* Construct the function type and go to the next
4434 inner layer of declarator. */
4435 arg_info = TREE_OPERAND (declarator, 1);
4436 arg_types = grokparms (arg_info, really_funcdef);
4438 /* Type qualifiers before the return type of the function
4439 qualify the return type, not the function type. */
4442 /* Type qualifiers on a function return type are
4443 normally permitted by the standard but have no
4444 effect, so give a warning at -Wreturn-type.
4445 Qualifiers on a void return type are banned on
4446 function definitions in ISO C; GCC used to used them
4447 for noreturn functions. */
4448 if (VOID_TYPE_P (type) && really_funcdef)
4449 pedwarn ("function definition has qualified void return type");
4450 else if (warn_return_type)
4451 warning ("type qualifiers ignored on function return type");
4453 type = c_build_qualified_type (type, type_quals);
4455 type_quals = TYPE_UNQUALIFIED;
4457 type = build_function_type (type, arg_types);
4458 declarator = TREE_OPERAND (declarator, 0);
4460 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4461 the formal parameter list of this FUNCTION_TYPE to point to
4462 the FUNCTION_TYPE node itself. */
4467 for (link = ARG_INFO_TAGS (arg_info);
4469 link = TREE_CHAIN (link))
4470 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4473 else if (TREE_CODE (declarator) == INDIRECT_REF)
4475 /* Merge any constancy or volatility into the target type
4478 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4480 pedwarn ("ISO C forbids qualified function types");
4482 type = c_build_qualified_type (type, type_quals);
4483 type_quals = TYPE_UNQUALIFIED;
4486 type = build_pointer_type (type);
4488 /* Process a list of type modifier keywords
4489 (such as const or volatile) that were given inside the `*'. */
4491 if (TREE_TYPE (declarator))
4499 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4500 typemodlist = TREE_CHAIN (typemodlist))
4502 tree qualifier = TREE_VALUE (typemodlist);
4504 if (C_IS_RESERVED_WORD (qualifier))
4506 if (C_RID_CODE (qualifier) == RID_CONST)
4508 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4510 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4520 error ("invalid type modifier within pointer declarator");
4521 if (pedantic && !flag_isoc99)
4524 pedwarn ("duplicate `const'");
4526 pedwarn ("duplicate `volatile'");
4528 pedwarn ("duplicate `restrict'");
4531 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4532 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4533 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4536 declarator = TREE_OPERAND (declarator, 0);
4543 /* Now TYPE has the actual type. */
4545 /* Did array size calculations overflow? */
4547 if (TREE_CODE (type) == ARRAY_TYPE
4548 && COMPLETE_TYPE_P (type)
4549 && TREE_OVERFLOW (TYPE_SIZE (type)))
4551 error ("size of array `%s' is too large", name);
4552 /* If we proceed with the array type as it is, we'll eventually
4553 crash in tree_low_cst(). */
4554 type = error_mark_node;
4557 /* If this is declaring a typedef name, return a TYPE_DECL. */
4559 if (specbits & (1 << (int) RID_TYPEDEF))
4562 /* Note that the grammar rejects storage classes
4563 in typenames, fields or parameters */
4564 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4566 pedwarn ("ISO C forbids qualified function types");
4568 type = c_build_qualified_type (type, type_quals);
4569 decl = build_decl (TYPE_DECL, declarator, type);
4570 if ((specbits & (1 << (int) RID_SIGNED))
4571 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4572 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4573 decl_attributes (&decl, returned_attrs, 0);
4577 /* Detect the case of an array type of unspecified size
4578 which came, as such, direct from a typedef name.
4579 We must copy the type, so that each identifier gets
4580 a distinct type, so that each identifier's size can be
4581 controlled separately by its own initializer. */
4583 if (type != 0 && typedef_type != 0
4584 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4585 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4587 type = build_array_type (TREE_TYPE (type), 0);
4589 C_TYPE_VARIABLE_SIZE (type) = 1;
4592 /* If this is a type name (such as, in a cast or sizeof),
4593 compute the type and return it now. */
4595 if (decl_context == TYPENAME)
4597 /* Note that the grammar rejects storage classes
4598 in typenames, fields or parameters */
4599 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4601 pedwarn ("ISO C forbids const or volatile function types");
4603 type = c_build_qualified_type (type, type_quals);
4604 decl_attributes (&type, returned_attrs, 0);
4608 /* Aside from typedefs and type names (handle above),
4609 `void' at top level (not within pointer)
4610 is allowed only in public variables.
4611 We don't complain about parms either, but that is because
4612 a better error message can be made later. */
4614 if (VOID_TYPE_P (type) && decl_context != PARM
4615 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4616 && ((specbits & (1 << (int) RID_EXTERN))
4617 || (current_scope == file_scope
4619 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4621 error ("variable or field `%s' declared void", name);
4622 type = integer_type_node;
4625 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4626 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4631 if (decl_context == PARM)
4633 tree type_as_written;
4636 /* A parameter declared as an array of T is really a pointer to T.
4637 One declared as a function is really a pointer to a function. */
4639 if (TREE_CODE (type) == ARRAY_TYPE)
4641 /* Transfer const-ness of array into that of type pointed to. */
4642 type = TREE_TYPE (type);
4644 type = c_build_qualified_type (type, type_quals);
4645 type = build_pointer_type (type);
4646 type_quals = TYPE_UNQUALIFIED;
4647 if (array_ptr_quals)
4649 tree new_ptr_quals, new_ptr_attrs;
4651 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4652 /* We don't yet implement attributes in this context. */
4653 if (new_ptr_attrs != NULL_TREE)
4654 warning ("attributes in parameter array declarator ignored");
4659 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4661 tree qualifier = TREE_VALUE (new_ptr_quals);
4663 if (C_IS_RESERVED_WORD (qualifier))
4665 if (C_RID_CODE (qualifier) == RID_CONST)
4667 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4669 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4679 error ("invalid type modifier within array declarator");
4681 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4682 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4683 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4687 else if (TREE_CODE (type) == FUNCTION_TYPE)
4689 if (pedantic && type_quals)
4690 pedwarn ("ISO C forbids qualified function types");
4692 type = c_build_qualified_type (type, type_quals);
4693 type = build_pointer_type (type);
4694 type_quals = TYPE_UNQUALIFIED;
4696 else if (type_quals)
4697 type = c_build_qualified_type (type, type_quals);
4699 type_as_written = type;
4701 decl = build_decl (PARM_DECL, declarator, type);
4703 C_DECL_VARIABLE_SIZE (decl) = 1;
4705 /* Compute the type actually passed in the parmlist,
4706 for the case where there is no prototype.
4707 (For example, shorts and chars are passed as ints.)
4708 When there is a prototype, this is overridden later. */
4710 if (type == error_mark_node)
4711 promoted_type = type;
4713 promoted_type = c_type_promotes_to (type);
4715 DECL_ARG_TYPE (decl) = promoted_type;
4716 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4718 else if (decl_context == FIELD)
4720 /* Structure field. It may not be a function. */
4722 if (TREE_CODE (type) == FUNCTION_TYPE)
4724 error ("field `%s' declared as a function", name);
4725 type = build_pointer_type (type);
4727 else if (TREE_CODE (type) != ERROR_MARK
4728 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4730 error ("field `%s' has incomplete type", name);
4731 type = error_mark_node;
4733 /* Move type qualifiers down to element of an array. */
4734 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4735 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4737 TYPE_DOMAIN (type));
4738 decl = build_decl (FIELD_DECL, declarator, type);
4739 DECL_NONADDRESSABLE_P (decl) = bitfield;
4742 C_DECL_VARIABLE_SIZE (decl) = 1;
4744 else if (TREE_CODE (type) == FUNCTION_TYPE)
4746 if (specbits & (1 << (int) RID_REGISTER)
4747 || specbits & (1 << (int) RID_THREAD))
4748 error ("invalid storage class for function `%s'", name);
4749 else if (current_scope != file_scope)
4751 /* Function declaration not at file scope. Storage
4752 classes other than `extern' are not allowed, C99
4753 6.7.1p5, and `extern' makes no difference. However,
4754 GCC allows 'auto', perhaps with 'inline', to support
4755 nested functions. */
4756 if (specbits & (1 << (int) RID_AUTO))
4759 pedwarn ("invalid storage class for function `%s'", name);
4761 if (specbits & (1 << (int) RID_STATIC))
4762 error ("invalid storage class for function `%s'", name);
4765 decl = build_decl (FUNCTION_DECL, declarator, type);
4766 decl = build_decl_attribute_variant (decl, decl_attr);
4768 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4770 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4771 pedwarn ("ISO C forbids qualified function types");
4773 /* GNU C interprets a volatile-qualified function type to indicate
4774 that the function does not return. */
4775 if ((type_quals & TYPE_QUAL_VOLATILE)
4776 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4777 warning ("`noreturn' function returns non-void value");
4779 /* Every function declaration is an external reference
4780 (DECL_EXTERNAL) except for those which are not at file
4781 scope and are explicitly declared "auto". This is
4782 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4783 GCC to signify a forward declaration of a nested function. */
4784 if ((specbits & (1 << RID_AUTO)) && current_scope != file_scope)
4785 DECL_EXTERNAL (decl) = 0;
4787 DECL_EXTERNAL (decl) = 1;
4789 /* Record absence of global scope for `static' or `auto'. */
4791 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4793 /* For a function definition, record the argument information
4794 block where store_parm_decls will look for it. */
4796 current_function_arg_info = arg_info;
4799 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4801 /* Record presence of `inline', if it is reasonable. */
4802 if (MAIN_NAME_P (declarator))
4805 warning ("cannot inline function `main'");
4809 /* Record that the function is declared `inline'. */
4810 DECL_DECLARED_INLINE_P (decl) = 1;
4812 /* Do not mark bare declarations as DECL_INLINE. Doing so
4813 in the presence of multiple declarations can result in
4814 the abstract origin pointing between the declarations,
4815 which will confuse dwarf2out. */
4818 DECL_INLINE (decl) = 1;
4819 if (specbits & (1 << (int) RID_EXTERN))
4820 current_extern_inline = 1;
4823 /* If -finline-functions, assume it can be inlined. This does
4824 two things: let the function be deferred until it is actually
4825 needed, and let dwarf2 know that the function is inlinable. */
4826 else if (flag_inline_trees == 2 && initialized)
4827 DECL_INLINE (decl) = 1;
4831 /* It's a variable. */
4832 /* An uninitialized decl with `extern' is a reference. */
4833 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4835 /* Move type qualifiers down to element of an array. */
4836 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4838 int saved_align = TYPE_ALIGN(type);
4839 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4841 TYPE_DOMAIN (type));
4842 TYPE_ALIGN (type) = saved_align;
4844 else if (type_quals)
4845 type = c_build_qualified_type (type, type_quals);
4847 /* C99 6.2.2p7: It is invalid (compile-time undefined
4848 behavior) to create an 'extern' declaration for a
4849 variable if there is a global declaration that is
4850 'static' and the global declaration is not visible.
4851 (If the static declaration _is_ currently visible,
4852 the 'extern' declaration is taken to refer to that decl.) */
4853 if (extern_ref && current_scope != file_scope)
4855 tree global_decl = identifier_global_value (declarator);
4856 tree visible_decl = lookup_name (declarator);
4859 && global_decl != visible_decl
4860 && TREE_CODE (global_decl) == VAR_DECL
4861 && !TREE_PUBLIC (global_decl))
4862 error ("variable previously declared 'static' redeclared "
4866 decl = build_decl (VAR_DECL, declarator, type);
4868 C_DECL_VARIABLE_SIZE (decl) = 1;
4871 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4873 /* At file scope, an initialized extern declaration may follow
4874 a static declaration. In that case, DECL_EXTERNAL will be
4875 reset later in start_decl. */
4876 DECL_EXTERNAL (decl) = !!(specbits & (1 << (int) RID_EXTERN));
4878 /* At file scope, the presence of a `static' or `register' storage
4879 class specifier, or the absence of all storage class specifiers
4880 makes this declaration a definition (perhaps tentative). Also,
4881 the absence of both `static' and `register' makes it public. */
4882 if (current_scope == file_scope)
4884 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4885 | (1 << (int) RID_REGISTER)));
4886 TREE_STATIC (decl) = !extern_ref;
4888 /* Not at file scope, only `static' makes a static definition. */
4891 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4892 TREE_PUBLIC (decl) = extern_ref;
4895 if (specbits & 1 << (int) RID_THREAD)
4897 if (targetm.have_tls)
4898 DECL_THREAD_LOCAL (decl) = 1;
4900 /* A mere warning is sure to result in improper semantics
4901 at runtime. Don't bother to allow this to compile. */
4902 error ("thread-local storage not supported for this target");
4906 /* Record `register' declaration for warnings on &
4907 and in case doing stupid register allocation. */
4909 if (specbits & (1 << (int) RID_REGISTER))
4911 C_DECL_REGISTER (decl) = 1;
4912 DECL_REGISTER (decl) = 1;
4915 /* Record constancy and volatility. */
4916 c_apply_type_quals_to_decl (type_quals, decl);
4918 /* If a type has volatile components, it should be stored in memory.
4919 Otherwise, the fact that those components are volatile
4920 will be ignored, and would even crash the compiler. */
4921 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4923 /* It is not an error for a structure with volatile fields to
4924 be declared register, but reset DECL_REGISTER since it
4925 cannot actually go in a register. */
4926 int was_reg = C_DECL_REGISTER (decl);
4927 C_DECL_REGISTER (decl) = 0;
4928 DECL_REGISTER (decl) = 0;
4929 c_mark_addressable (decl);
4930 C_DECL_REGISTER (decl) = was_reg;
4933 #ifdef ENABLE_CHECKING
4934 /* This is the earliest point at which we might know the assembler
4935 name of a variable. Thus, if it's known before this, die horribly. */
4936 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4940 decl_attributes (&decl, returned_attrs, 0);
4946 /* Decode the parameter-list info for a function type or function definition.
4947 The argument is the value returned by `get_parm_info' (or made in parse.y
4948 if there is an identifier list instead of a parameter decl list).
4949 These two functions are separate because when a function returns
4950 or receives functions then each is called multiple times but the order
4951 of calls is different. The last call to `grokparms' is always the one
4952 that contains the formal parameter names of a function definition.
4954 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4956 FUNCDEF_FLAG is true for a function definition, false for
4957 a mere declaration. A nonempty identifier-list gets an error message
4958 when FUNCDEF_FLAG is false. */
4961 grokparms (tree arg_info, bool funcdef_flag)
4963 tree arg_types = ARG_INFO_TYPES (arg_info);
4965 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4966 && !in_system_header)
4967 warning ("function declaration isn't a prototype");
4969 if (arg_types == error_mark_node)
4970 return 0; /* don't set TYPE_ARG_TYPES in this case */
4972 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4975 pedwarn ("parameter names (without types) in function declaration");
4977 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4978 ARG_INFO_TYPES (arg_info) = 0;
4983 tree parm, type, typelt;
4984 unsigned int parmno;
4986 /* If the arg types are incomplete in a declaration, they must
4987 include undefined tags. These tags can never be defined in
4988 the scope of the declaration, so the types can never be
4989 completed, and no call can be compiled successfully. */
4991 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4993 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4995 type = TREE_VALUE (typelt);
4996 if (type == error_mark_node)
4999 if (!COMPLETE_TYPE_P (type))
5003 if (DECL_NAME (parm))
5004 error ("%Jparameter %u ('%D') has incomplete type",
5005 parm, parmno, parm);
5007 error ("%Jparameter %u has incomplete type",
5010 TREE_VALUE (typelt) = error_mark_node;
5011 TREE_TYPE (parm) = error_mark_node;
5015 if (DECL_NAME (parm))
5016 warning ("%Jparameter %u ('%D') has incomplete type",
5017 parm, parmno, parm);
5019 warning ("%Jparameter %u has incomplete type",
5028 /* Take apart the current scope and return a tree_list node with info
5029 on a parameter list just parsed. This tree_list node should be
5030 examined using the ARG_INFO_* macros, defined above:
5032 ARG_INFO_PARMS: a list of parameter decls.
5033 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
5034 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
5035 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
5036 constants) defined with the parameters.
5038 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
5040 ELLIPSIS being true means the argument list ended in '...' so don't
5041 append a sentinel (void_list_node) to the end of the type-list. */
5044 get_parm_info (bool ellipsis)
5046 struct c_binding *b = current_scope->bindings;
5047 tree arg_info = make_node (TREE_LIST);
5053 static bool explained_incomplete_types = false;
5054 bool gave_void_only_once_err = false;
5056 /* The bindings in this scope must not get put into a block.
5057 We will take care of deleting the binding nodes. */
5058 current_scope->bindings = 0;
5060 /* This function is only called if there was *something* on the
5062 #ifdef ENABLE_CHECKING
5067 /* A parameter list consisting solely of 'void' indicates that the
5068 function takes no arguments. But if the 'void' is qualified
5069 (by 'const' or 'volatile'), or has a storage class specifier
5070 ('register'), then the behavior is undefined; issue an error.
5071 Typedefs for 'void' are OK (see DR#157). */
5072 if (b->prev == 0 /* one binding */
5073 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5074 && !DECL_NAME (b->decl) /* anonymous */
5075 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5077 if (TREE_THIS_VOLATILE (b->decl)
5078 || TREE_READONLY (b->decl)
5079 || C_DECL_REGISTER (b->decl))
5080 error ("'void' as only parameter may not be qualified");
5082 /* There cannot be an ellipsis. */
5084 error ("'void' must be the only parameter");
5086 ARG_INFO_TYPES (arg_info) = void_list_node;
5091 types = void_list_node;
5093 /* Break up the bindings list into parms, tags, types, and others;
5094 apply sanity checks; purge the name-to-decl bindings. */
5097 tree decl = b->decl;
5098 tree type = TREE_TYPE (decl);
5099 const char *keyword;
5101 switch (TREE_CODE (decl))
5106 #ifdef ENABLE_CHECKING
5107 if (I_SYMBOL_BINDING (b->id) != b) abort ();
5109 I_SYMBOL_BINDING (b->id) = b->shadowed;
5112 /* Check for forward decls that never got their actual decl. */
5113 if (TREE_ASM_WRITTEN (decl))
5114 error ("%Jparameter '%D' has just a forward declaration",
5116 /* Check for (..., void, ...) and issue an error. */
5117 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5119 if (!gave_void_only_once_err)
5121 error ("'void' must be the only parameter");
5122 gave_void_only_once_err = true;
5127 /* Valid parameter, add it to the list. */
5128 TREE_CHAIN (decl) = parms;
5131 /* Since there is a prototype, args are passed in their
5132 declared types. The back end may override this later. */
5133 DECL_ARG_TYPE (decl) = type;
5134 types = tree_cons (0, type, types);
5138 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5139 case UNION_TYPE: keyword = "union"; goto tag;
5140 case RECORD_TYPE: keyword = "struct"; goto tag;
5142 /* Types may not have tag-names, in which case the type
5143 appears in the bindings list with b->id NULL. */
5146 #ifdef ENABLE_CHECKING
5147 if (I_TAG_BINDING (b->id) != b) abort ();
5149 I_TAG_BINDING (b->id) = b->shadowed;
5152 /* Warn about any struct, union or enum tags defined in a
5153 parameter list. The scope of such types is limited to
5154 the parameter list, which is rarely if ever desirable
5155 (it's impossible to call such a function with type-
5156 correct arguments). An anonymous union parm type is
5157 meaningful as a GNU extension, so don't warn for that. */
5158 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5161 /* The %s will be one of 'struct', 'union', or 'enum'. */
5162 warning ("'%s %E' declared inside parameter list",
5165 /* The %s will be one of 'struct', 'union', or 'enum'. */
5166 warning ("anonymous %s declared inside parameter list",
5169 if (! explained_incomplete_types)
5171 warning ("its scope is only this definition or declaration,"
5172 " which is probably not what you want");
5173 explained_incomplete_types = true;
5177 tags = tree_cons (b->id, decl, tags);
5182 /* CONST_DECLs appear here when we have an embedded enum,
5183 and TYPE_DECLs appear here when we have an embedded struct
5184 or union. No warnings for this - we already warned about the
5186 TREE_CHAIN (decl) = others;
5191 /* error_mark_node appears here when we have an undeclared
5192 variable. Just throw it away. */
5195 #ifdef ENABLE_CHECKING
5196 if (I_SYMBOL_BINDING (b->id) != b) abort ();
5198 I_SYMBOL_BINDING (b->id) = b->shadowed;
5202 /* Other things that might be encountered. */
5210 b = free_binding_and_advance (b);
5213 ARG_INFO_PARMS (arg_info) = parms;
5214 ARG_INFO_TAGS (arg_info) = tags;
5215 ARG_INFO_TYPES (arg_info) = types;
5216 ARG_INFO_OTHERS (arg_info) = others;
5220 /* Get the struct, enum or union (CODE says which) with tag NAME.
5221 Define the tag as a forward-reference if it is not defined. */
5224 xref_tag (enum tree_code code, tree name)
5226 /* If a cross reference is requested, look up the type
5227 already defined for this tag and return it. */
5229 tree ref = lookup_tag (code, name, 0);
5230 /* If this is the right type of tag, return what we found.
5231 (This reference will be shadowed by shadow_tag later if appropriate.)
5232 If this is the wrong type of tag, do not return it. If it was the
5233 wrong type in the same scope, we will have had an error
5234 message already; if in a different scope and declaring
5235 a name, pending_xref_error will give an error message; but if in a
5236 different scope and not declaring a name, this tag should
5237 shadow the previous declaration of a different type of tag, and
5238 this would not work properly if we return the reference found.
5239 (For example, with "struct foo" in an outer scope, "union foo;"
5240 must shadow that tag with a new one of union type.) */
5241 if (ref && TREE_CODE (ref) == code)
5244 /* If no such tag is yet defined, create a forward-reference node
5245 and record it as the "definition".
5246 When a real declaration of this type is found,
5247 the forward-reference will be altered into a real type. */
5249 ref = make_node (code);
5250 if (code == ENUMERAL_TYPE)
5252 /* Give the type a default layout like unsigned int
5253 to avoid crashing if it does not get defined. */
5254 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5255 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5256 TYPE_USER_ALIGN (ref) = 0;
5257 TYPE_UNSIGNED (ref) = 1;
5258 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5259 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5260 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5263 pushtag (name, ref);
5268 /* Make sure that the tag NAME is defined *in the current scope*
5269 at least as a forward reference.
5270 CODE says which kind of tag NAME ought to be. */
5273 start_struct (enum tree_code code, tree name)
5275 /* If there is already a tag defined at this scope
5276 (as a forward reference), just return it. */
5281 ref = lookup_tag (code, name, 1);
5282 if (ref && TREE_CODE (ref) == code)
5284 if (TYPE_FIELDS (ref))
5286 if (code == UNION_TYPE)
5287 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5289 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5294 /* Otherwise create a forward-reference just so the tag is in scope. */
5296 ref = make_node (code);
5297 pushtag (name, ref);
5300 C_TYPE_BEING_DEFINED (ref) = 1;
5301 TYPE_PACKED (ref) = flag_pack_struct;
5305 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5306 of a structure component, returning a FIELD_DECL node.
5307 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5309 This is done during the parsing of the struct declaration.
5310 The FIELD_DECL nodes are chained together and the lot of them
5311 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5314 grokfield (tree declarator, tree declspecs, tree width)
5318 if (declarator == NULL_TREE && width == NULL_TREE)
5320 /* This is an unnamed decl.
5322 If we have something of the form "union { list } ;" then this
5323 is the anonymous union extension. Similarly for struct.
5325 If this is something of the form "struct foo;", then
5326 If MS extensions are enabled, this is handled as an
5328 Otherwise this is a forward declaration of a structure tag.
5330 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5331 If MS extensions are enabled and foo names a structure, then
5332 again this is an anonymous struct.
5333 Otherwise this is an error.
5335 Oh what a horrid tangled web we weave. I wonder if MS consciously
5336 took this from Plan 9 or if it was an accident of implementation
5337 that took root before someone noticed the bug... */
5339 tree type = TREE_VALUE (declspecs);
5341 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5342 type = TREE_TYPE (type);
5343 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5345 if (flag_ms_extensions)
5348 goto warn_unnamed_field;
5349 else if (TYPE_NAME (type) == NULL)
5352 goto warn_unnamed_field;
5357 warning ("declaration does not declare anything");
5362 value = grokdeclarator (declarator, declspecs, FIELD, false,
5363 width ? &width : NULL);
5365 finish_decl (value, NULL_TREE, NULL_TREE);
5366 DECL_INITIAL (value) = width;
5371 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5372 the list such that this does not present a problem later. */
5375 detect_field_duplicates (tree fieldlist)
5380 /* First, see if there are more than "a few" fields.
5381 This is trivially true if there are zero or one fields. */
5384 x = TREE_CHAIN (fieldlist);
5390 } while (timeout > 0 && x);
5392 /* If there were "few" fields, avoid the overhead of allocating
5393 a hash table. Instead just do the nested traversal thing. */
5396 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5399 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5400 if (DECL_NAME (y) == DECL_NAME (x))
5402 error ("%Jduplicate member '%D'", x, x);
5403 DECL_NAME (x) = NULL_TREE;
5409 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5412 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5413 if ((y = DECL_NAME (x)) != 0)
5415 slot = htab_find_slot (htab, y, INSERT);
5418 error ("%Jduplicate member '%D'", x, x);
5419 DECL_NAME (x) = NULL_TREE;
5428 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5429 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5430 ATTRIBUTES are attributes to be applied to the structure. */
5433 finish_struct (tree t, tree fieldlist, tree attributes)
5436 bool toplevel = file_scope == current_scope;
5437 int saw_named_field;
5439 /* If this type was previously laid out as a forward reference,
5440 make sure we lay it out again. */
5444 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5448 for (x = fieldlist; x; x = TREE_CHAIN (x))
5449 if (DECL_NAME (x) != 0)
5453 pedwarn ("%s has no %s",
5454 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5455 fieldlist ? _("named members") : _("members"));
5458 /* Install struct as DECL_CONTEXT of each field decl.
5459 Also process specified field sizes, found in the DECL_INITIAL,
5460 storing 0 there after the type has been changed to precision equal
5461 to its width, rather than the precision of the specified standard
5462 type. (Correct layout requires the original type to have been preserved
5465 saw_named_field = 0;
5466 for (x = fieldlist; x; x = TREE_CHAIN (x))
5468 DECL_CONTEXT (x) = t;
5469 DECL_PACKED (x) |= TYPE_PACKED (t);
5471 /* If any field is const, the structure type is pseudo-const. */
5472 if (TREE_READONLY (x))
5473 C_TYPE_FIELDS_READONLY (t) = 1;
5476 /* A field that is pseudo-const makes the structure likewise. */
5477 tree t1 = TREE_TYPE (x);
5478 while (TREE_CODE (t1) == ARRAY_TYPE)
5479 t1 = TREE_TYPE (t1);
5480 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5481 && C_TYPE_FIELDS_READONLY (t1))
5482 C_TYPE_FIELDS_READONLY (t) = 1;
5485 /* Any field that is volatile means variables of this type must be
5486 treated in some ways as volatile. */
5487 if (TREE_THIS_VOLATILE (x))
5488 C_TYPE_FIELDS_VOLATILE (t) = 1;
5490 /* Any field of nominal variable size implies structure is too. */
5491 if (C_DECL_VARIABLE_SIZE (x))
5492 C_TYPE_VARIABLE_SIZE (t) = 1;
5494 /* Detect invalid nested redefinition. */
5495 if (TREE_TYPE (x) == t)
5496 error ("nested redefinition of `%s'",
5497 IDENTIFIER_POINTER (TYPE_NAME (t)));
5499 if (DECL_INITIAL (x))
5501 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5502 DECL_SIZE (x) = bitsize_int (width);
5503 DECL_BIT_FIELD (x) = 1;
5504 SET_DECL_C_BIT_FIELD (x);
5507 /* Detect flexible array member in an invalid context. */
5508 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5509 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5510 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5511 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5513 if (TREE_CODE (t) == UNION_TYPE)
5515 error ("%Jflexible array member in union", x);
5516 TREE_TYPE (x) = error_mark_node;
5518 else if (TREE_CHAIN (x) != NULL_TREE)
5520 error ("%Jflexible array member not at end of struct", x);
5521 TREE_TYPE (x) = error_mark_node;
5523 else if (! saw_named_field)
5525 error ("%Jflexible array member in otherwise empty struct", x);
5526 TREE_TYPE (x) = error_mark_node;
5530 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5531 && flexible_array_type_p (TREE_TYPE (x)))
5532 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5535 saw_named_field = 1;
5538 detect_field_duplicates (fieldlist);
5540 /* Now we have the nearly final fieldlist. Record it,
5541 then lay out the structure or union (including the fields). */
5543 TYPE_FIELDS (t) = fieldlist;
5547 /* Give bit-fields their proper types. */
5549 tree *fieldlistp = &fieldlist;
5551 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5552 && TREE_TYPE (*fieldlistp) != error_mark_node)
5554 unsigned HOST_WIDE_INT width
5555 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5556 tree type = TREE_TYPE (*fieldlistp);
5557 if (width != TYPE_PRECISION (type))
5558 TREE_TYPE (*fieldlistp)
5559 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5560 DECL_INITIAL (*fieldlistp) = 0;
5563 fieldlistp = &TREE_CHAIN (*fieldlistp);
5566 /* Now we have the truly final field list.
5567 Store it in this type and in the variants. */
5569 TYPE_FIELDS (t) = fieldlist;
5571 /* If there are lots of fields, sort so we can look through them fast.
5572 We arbitrarily consider 16 or more elts to be "a lot". */
5577 for (x = fieldlist; x; x = TREE_CHAIN (x))
5579 if (len > 15 || DECL_NAME (x) == NULL)
5587 struct lang_type *space;
5588 struct sorted_fields_type *space2;
5590 len += list_length (x);
5592 /* Use the same allocation policy here that make_node uses, to
5593 ensure that this lives as long as the rest of the struct decl.
5594 All decls in an inline function need to be saved. */
5596 space = GGC_CNEW (struct lang_type);
5597 space2 = GGC_NEWVAR (struct sorted_fields_type,
5598 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5602 field_array = &space2->elts[0];
5603 for (x = fieldlist; x; x = TREE_CHAIN (x))
5605 field_array[len++] = x;
5607 /* If there is anonymous struct or union, break out of the loop. */
5608 if (DECL_NAME (x) == NULL)
5611 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5614 TYPE_LANG_SPECIFIC (t) = space;
5615 TYPE_LANG_SPECIFIC (t)->s->len = len;
5616 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5617 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5622 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5624 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5625 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5626 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5627 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5630 /* If this was supposed to be a transparent union, but we can't
5631 make it one, warn and turn off the flag. */
5632 if (TREE_CODE (t) == UNION_TYPE
5633 && TYPE_TRANSPARENT_UNION (t)
5634 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5636 TYPE_TRANSPARENT_UNION (t) = 0;
5637 warning ("union cannot be made transparent");
5640 /* If this structure or union completes the type of any previous
5641 variable declaration, lay it out and output its rtl. */
5642 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5646 tree decl = TREE_VALUE (x);
5647 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5648 layout_array_type (TREE_TYPE (decl));
5649 if (TREE_CODE (decl) != TYPE_DECL)
5651 layout_decl (decl, 0);
5652 if (c_dialect_objc ())
5653 objc_check_decl (decl);
5654 rest_of_decl_compilation (decl, toplevel, 0);
5659 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5661 /* Finish debugging output for this type. */
5662 rest_of_type_compilation (t, toplevel);
5667 /* Lay out the type T, and its element type, and so on. */
5670 layout_array_type (tree t)
5672 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5673 layout_array_type (TREE_TYPE (t));
5677 /* Begin compiling the definition of an enumeration type.
5678 NAME is its name (or null if anonymous).
5679 Returns the type object, as yet incomplete.
5680 Also records info about it so that build_enumerator
5681 may be used to declare the individual values as they are read. */
5684 start_enum (tree name)
5688 /* If this is the real definition for a previous forward reference,
5689 fill in the contents in the same object that used to be the
5690 forward reference. */
5693 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5695 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5697 enumtype = make_node (ENUMERAL_TYPE);
5698 pushtag (name, enumtype);
5701 C_TYPE_BEING_DEFINED (enumtype) = 1;
5703 if (TYPE_VALUES (enumtype) != 0)
5705 /* This enum is a named one that has been declared already. */
5706 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5708 /* Completely replace its old definition.
5709 The old enumerators remain defined, however. */
5710 TYPE_VALUES (enumtype) = 0;
5713 enum_next_value = integer_zero_node;
5716 if (flag_short_enums)
5717 TYPE_PACKED (enumtype) = 1;
5722 /* After processing and defining all the values of an enumeration type,
5723 install their decls in the enumeration type and finish it off.
5724 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5725 and ATTRIBUTES are the specified attributes.
5726 Returns ENUMTYPE. */
5729 finish_enum (tree enumtype, tree values, tree attributes)
5732 tree minnode = 0, maxnode = 0;
5733 int precision, unsign;
5734 bool toplevel = (file_scope == current_scope);
5735 struct lang_type *lt;
5737 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5739 /* Calculate the maximum value of any enumerator in this type. */
5741 if (values == error_mark_node)
5742 minnode = maxnode = integer_zero_node;
5745 minnode = maxnode = TREE_VALUE (values);
5746 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5748 tree value = TREE_VALUE (pair);
5749 if (tree_int_cst_lt (maxnode, value))
5751 if (tree_int_cst_lt (value, minnode))
5756 /* Construct the final type of this enumeration. It is the same
5757 as one of the integral types - the narrowest one that fits, except
5758 that normally we only go as narrow as int - and signed iff any of
5759 the values are negative. */
5760 unsign = (tree_int_cst_sgn (minnode) >= 0);
5761 precision = MAX (min_precision (minnode, unsign),
5762 min_precision (maxnode, unsign));
5764 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5766 tem = c_common_type_for_size (precision, unsign);
5769 warning ("enumeration values exceed range of largest integer");
5770 tem = long_long_integer_type_node;
5774 tem = unsign ? unsigned_type_node : integer_type_node;
5776 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5777 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5778 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5779 TYPE_SIZE (enumtype) = 0;
5781 /* If the precision of the type was specific with an attribute and it
5782 was too small, give an error. Otherwise, use it. */
5783 if (TYPE_PRECISION (enumtype))
5785 if (precision > TYPE_PRECISION (enumtype))
5786 error ("specified mode too small for enumeral values");
5789 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5791 layout_type (enumtype);
5793 if (values != error_mark_node)
5795 /* Change the type of the enumerators to be the enum type. We
5796 need to do this irrespective of the size of the enum, for
5797 proper type checking. Replace the DECL_INITIALs of the
5798 enumerators, and the value slots of the list, with copies
5799 that have the enum type; they cannot be modified in place
5800 because they may be shared (e.g. integer_zero_node) Finally,
5801 change the purpose slots to point to the names of the decls. */
5802 for (pair = values; pair; pair = TREE_CHAIN (pair))
5804 tree enu = TREE_PURPOSE (pair);
5805 tree ini = DECL_INITIAL (enu);
5807 TREE_TYPE (enu) = enumtype;
5809 /* The ISO C Standard mandates enumerators to have type int,
5810 even though the underlying type of an enum type is
5811 unspecified. Here we convert any enumerators that fit in
5812 an int to type int, to avoid promotions to unsigned types
5813 when comparing integers with enumerators that fit in the
5814 int range. When -pedantic is given, build_enumerator()
5815 would have already taken care of those that don't fit. */
5816 if (int_fits_type_p (ini, integer_type_node))
5817 tem = integer_type_node;
5820 ini = convert (tem, ini);
5822 DECL_INITIAL (enu) = ini;
5823 TREE_PURPOSE (pair) = DECL_NAME (enu);
5824 TREE_VALUE (pair) = ini;
5827 TYPE_VALUES (enumtype) = values;
5830 /* Record the min/max values so that we can warn about bit-field
5831 enumerations that are too small for the values. */
5832 lt = GGC_CNEW (struct lang_type);
5833 lt->enum_min = minnode;
5834 lt->enum_max = maxnode;
5835 TYPE_LANG_SPECIFIC (enumtype) = lt;
5837 /* Fix up all variant types of this enum type. */
5838 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5840 if (tem == enumtype)
5842 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5843 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5844 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5845 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5846 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5847 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5848 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5849 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5850 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5851 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5852 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5855 /* Finish debugging output for this type. */
5856 rest_of_type_compilation (enumtype, toplevel);
5861 /* Build and install a CONST_DECL for one value of the
5862 current enumeration type (one that was begun with start_enum).
5863 Return a tree-list containing the CONST_DECL and its value.
5864 Assignment of sequential values by default is handled here. */
5867 build_enumerator (tree name, tree value)
5871 /* Validate and default VALUE. */
5873 /* Remove no-op casts from the value. */
5875 STRIP_TYPE_NOPS (value);
5879 /* Don't issue more errors for error_mark_node (i.e. an
5880 undeclared identifier) - just ignore the value expression. */
5881 if (value == error_mark_node)
5883 else if (TREE_CODE (value) != INTEGER_CST)
5885 error ("enumerator value for '%E' is not an integer constant", name);
5890 value = default_conversion (value);
5891 constant_expression_warning (value);
5895 /* Default based on previous value. */
5896 /* It should no longer be possible to have NON_LVALUE_EXPR
5900 value = enum_next_value;
5902 error ("overflow in enumeration values");
5905 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5907 pedwarn ("ISO C restricts enumerator values to range of `int'");
5908 /* XXX This causes -pedantic to change the meaning of the program.
5909 Remove? -zw 2004-03-15 */
5910 value = convert (integer_type_node, value);
5913 /* Set basis for default for next value. */
5914 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5915 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5917 /* Now create a declaration for the enum value name. */
5919 type = TREE_TYPE (value);
5920 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5921 TYPE_PRECISION (integer_type_node)),
5922 (TYPE_PRECISION (type)
5923 >= TYPE_PRECISION (integer_type_node)
5924 && TYPE_UNSIGNED (type)));
5926 decl = build_decl (CONST_DECL, name, type);
5927 DECL_INITIAL (decl) = convert (type, value);
5930 return tree_cons (decl, value, NULL_TREE);
5934 /* Create the FUNCTION_DECL for a function definition.
5935 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5936 the declaration; they describe the function's name and the type it returns,
5937 but twisted together in a fashion that parallels the syntax of C.
5939 This function creates a binding context for the function body
5940 as well as setting up the FUNCTION_DECL in current_function_decl.
5942 Returns 1 on success. If the DECLARATOR is not suitable for a function
5943 (it defines a datum instead), we return 0, which tells
5944 yyparse to report a parse error. */
5947 start_function (tree declspecs, tree declarator, tree attributes)
5949 tree decl1, old_decl;
5950 tree restype, resdecl;
5952 current_function_returns_value = 0; /* Assume, until we see it does. */
5953 current_function_returns_null = 0;
5954 current_function_returns_abnormally = 0;
5955 warn_about_return_type = 0;
5956 current_extern_inline = 0;
5957 c_switch_stack = NULL;
5959 /* Indicate no valid break/continue context by setting these variables
5960 to some non-null, non-label value. We'll notice and emit the proper
5961 error message in c_finish_bc_stmt. */
5962 c_break_label = c_cont_label = size_zero_node;
5964 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5966 /* If the declarator is not suitable for a function definition,
5967 cause a syntax error. */
5971 decl_attributes (&decl1, attributes, 0);
5973 if (DECL_DECLARED_INLINE_P (decl1)
5974 && DECL_UNINLINABLE (decl1)
5975 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5976 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5978 announce_function (decl1);
5980 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5982 error ("return type is an incomplete type");
5983 /* Make it return void instead. */
5985 = build_function_type (void_type_node,
5986 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5989 if (warn_about_return_type)
5990 pedwarn_c99 ("return type defaults to `int'");
5992 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5993 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5994 DECL_INITIAL (decl1) = error_mark_node;
5996 /* If this definition isn't a prototype and we had a prototype declaration
5997 before, copy the arg type info from that prototype.
5998 But not if what we had before was a builtin function. */
5999 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6000 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6001 && !DECL_BUILT_IN (old_decl)
6002 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6003 TREE_TYPE (TREE_TYPE (old_decl)))
6004 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6006 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6008 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6011 /* Optionally warn of old-fashioned def with no previous prototype. */
6012 if (warn_strict_prototypes
6013 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6014 && C_DECL_ISNT_PROTOTYPE (old_decl))
6015 warning ("function declaration isn't a prototype");
6016 /* Optionally warn of any global def with no previous prototype. */
6017 else if (warn_missing_prototypes
6018 && TREE_PUBLIC (decl1)
6019 && ! MAIN_NAME_P (DECL_NAME (decl1))
6020 && C_DECL_ISNT_PROTOTYPE (old_decl))
6021 warning ("%Jno previous prototype for '%D'", decl1, decl1);
6022 /* Optionally warn of any def with no previous prototype
6023 if the function has already been used. */
6024 else if (warn_missing_prototypes
6025 && old_decl != 0 && TREE_USED (old_decl)
6026 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6027 warning ("%J'%D' was used with no prototype before its definition",
6029 /* Optionally warn of any global def with no previous declaration. */
6030 else if (warn_missing_declarations
6031 && TREE_PUBLIC (decl1)
6033 && ! MAIN_NAME_P (DECL_NAME (decl1)))
6034 warning ("%Jno previous declaration for '%D'", decl1, decl1);
6035 /* Optionally warn of any def with no previous declaration
6036 if the function has already been used. */
6037 else if (warn_missing_declarations
6038 && old_decl != 0 && TREE_USED (old_decl)
6039 && C_DECL_IMPLICIT (old_decl))
6040 warning ("%J`%D' was used with no declaration before its definition",
6043 /* This is a definition, not a reference.
6044 So normally clear DECL_EXTERNAL.
6045 However, `extern inline' acts like a declaration
6046 except for defining how to inline. So set DECL_EXTERNAL in that case. */
6047 DECL_EXTERNAL (decl1) = current_extern_inline;
6049 /* This function exists in static storage.
6050 (This does not mean `static' in the C sense!) */
6051 TREE_STATIC (decl1) = 1;
6053 /* A nested function is not global. */
6054 if (current_function_decl != 0)
6055 TREE_PUBLIC (decl1) = 0;
6057 #ifdef ENABLE_CHECKING
6058 /* This is the earliest point at which we might know the assembler
6059 name of the function. Thus, if it's set before this, die horribly. */
6060 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
6064 /* If #pragma weak was used, mark the decl weak now. */
6065 if (current_scope == file_scope)
6066 maybe_apply_pragma_weak (decl1);
6068 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6069 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6074 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6075 != integer_type_node)
6076 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
6078 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6079 args = TREE_CHAIN (args))
6081 tree type = args ? TREE_VALUE (args) : 0;
6083 if (type == void_type_node)
6090 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6091 pedwarn ("%Jfirst argument of '%D' should be `int'",
6096 if (TREE_CODE (type) != POINTER_TYPE
6097 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6098 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6100 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
6105 if (TREE_CODE (type) != POINTER_TYPE
6106 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6107 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6109 pedwarn ("%Jthird argument of '%D' should probably be "
6110 "'char **'", decl1, decl1);
6115 /* It is intentional that this message does not mention the third
6116 argument because it's only mentioned in an appendix of the
6118 if (argct > 0 && (argct < 2 || argct > 3))
6119 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
6121 if (! TREE_PUBLIC (decl1))
6122 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
6125 /* Record the decl so that the function name is defined.
6126 If we already have a decl for this name, and it is a FUNCTION_DECL,
6127 use the old decl. */
6129 current_function_decl = pushdecl (decl1);
6132 declare_parm_level ();
6134 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6135 /* Promote the value to int before returning it. */
6136 if (c_promoting_integer_type_p (restype))
6138 /* It retains unsignedness if not really getting wider. */
6139 if (TYPE_UNSIGNED (restype)
6140 && (TYPE_PRECISION (restype)
6141 == TYPE_PRECISION (integer_type_node)))
6142 restype = unsigned_type_node;
6144 restype = integer_type_node;
6147 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6148 DECL_ARTIFICIAL (resdecl) = 1;
6149 DECL_IGNORED_P (resdecl) = 1;
6150 DECL_RESULT (current_function_decl) = resdecl;
6152 start_fname_decls ();
6157 /* Subroutine of store_parm_decls which handles new-style function
6158 definitions (prototype format). The parms already have decls, so we
6159 need only record them as in effect and complain if any redundant
6160 old-style parm decls were written. */
6162 store_parm_decls_newstyle (tree fndecl, tree arg_info)
6165 tree parms = ARG_INFO_PARMS (arg_info);
6166 tree tags = ARG_INFO_TAGS (arg_info);
6167 tree others = ARG_INFO_OTHERS (arg_info);
6169 if (current_scope->bindings)
6171 error ("%Jold-style parameter declarations in prototyped "
6172 "function definition", fndecl);
6174 /* Get rid of the old-style declarations. */
6178 /* Don't issue this warning for nested functions, and don't issue this
6179 warning if we got here because ARG_INFO_TYPES was error_mark_node
6180 (this happens when a function definition has just an ellipsis in
6181 its parameter list). */
6182 else if (warn_traditional && !in_system_header && !current_function_scope
6183 && ARG_INFO_TYPES (arg_info) != error_mark_node)
6184 warning ("%Jtraditional C rejects ISO C style function definitions",
6187 /* Now make all the parameter declarations visible in the function body.
6188 We can bypass most of the grunt work of pushdecl. */
6189 for (decl = parms; decl; decl = TREE_CHAIN (decl))
6191 DECL_CONTEXT (decl) = current_function_decl;
6192 if (DECL_NAME (decl))
6193 bind (DECL_NAME (decl), decl, current_scope,
6194 /*invisible=*/false, /*nested=*/false);
6196 error ("%Jparameter name omitted", decl);
6199 /* Record the parameter list in the function declaration. */
6200 DECL_ARGUMENTS (fndecl) = parms;
6202 /* Now make all the ancillary declarations visible, likewise. */
6203 for (decl = others; decl; decl = TREE_CHAIN (decl))
6205 DECL_CONTEXT (decl) = current_function_decl;
6206 if (DECL_NAME (decl))
6207 bind (DECL_NAME (decl), decl, current_scope,
6208 /*invisible=*/false, /*nested=*/false);
6211 /* And all the tag declarations. */
6212 for (decl = tags; decl; decl = TREE_CHAIN (decl))
6213 if (TREE_PURPOSE (decl))
6214 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6215 /*invisible=*/false, /*nested=*/false);
6218 /* Subroutine of store_parm_decls which handles old-style function
6219 definitions (separate parameter list and declarations). */
6222 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
6224 struct c_binding *b;
6225 tree parm, decl, last;
6226 tree parmids = ARG_INFO_PARMS (arg_info);
6228 /* We use DECL_WEAK as a flag to show which parameters have been
6229 seen already, since it is not used on PARM_DECL. */
6230 #ifdef ENABLE_CHECKING
6231 for (b = current_scope->bindings; b; b = b->prev)
6232 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
6236 if (warn_old_style_definition && !in_system_header)
6237 warning ("%Jold-style function definition", fndecl);
6239 /* Match each formal parameter name with its declaration. Save each
6240 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6241 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6243 if (TREE_VALUE (parm) == 0)
6245 error ("%Jparameter name missing from parameter list", fndecl);
6246 TREE_PURPOSE (parm) = 0;
6250 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6251 if (b && B_IN_CURRENT_SCOPE (b))
6254 /* If we got something other than a PARM_DECL it is an error. */
6255 if (TREE_CODE (decl) != PARM_DECL)
6256 error ("%J'%D' declared as a non-parameter", decl, decl);
6257 /* If the declaration is already marked, we have a duplicate
6258 name. Complain and ignore the duplicate. */
6259 else if (DECL_WEAK (decl))
6261 error ("%Jmultiple parameters named '%D'", decl, decl);
6262 TREE_PURPOSE (parm) = 0;
6265 /* If the declaration says "void", complain and turn it into
6267 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6269 error ("%Jparameter '%D' declared with void type", decl, decl);
6270 TREE_TYPE (decl) = integer_type_node;
6271 DECL_ARG_TYPE (decl) = integer_type_node;
6272 layout_decl (decl, 0);
6275 /* If no declaration found, default to int. */
6278 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6279 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6280 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6284 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
6285 else if (extra_warnings)
6286 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
6289 TREE_PURPOSE (parm) = decl;
6290 DECL_WEAK (decl) = 1;
6293 /* Now examine the parms chain for incomplete declarations
6294 and declarations with no corresponding names. */
6296 for (b = current_scope->bindings; b; b = b->prev)
6299 if (TREE_CODE (parm) != PARM_DECL)
6302 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6304 error ("%Jparameter '%D' has incomplete type", parm, parm);
6305 TREE_TYPE (parm) = error_mark_node;
6308 if (! DECL_WEAK (parm))
6310 error ("%Jdeclaration for parameter '%D' but no such parameter",
6313 /* Pretend the parameter was not missing.
6314 This gets us to a standard state and minimizes
6315 further error messages. */
6316 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6320 /* Chain the declarations together in the order of the list of
6321 names. Store that chain in the function decl, replacing the
6322 list of names. Update the current scope to match. */
6323 DECL_ARGUMENTS (fndecl) = 0;
6325 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6326 if (TREE_PURPOSE (parm))
6328 if (parm && TREE_PURPOSE (parm))
6330 last = TREE_PURPOSE (parm);
6331 DECL_ARGUMENTS (fndecl) = last;
6332 DECL_WEAK (last) = 0;
6334 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6335 if (TREE_PURPOSE (parm))
6337 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6338 last = TREE_PURPOSE (parm);
6339 DECL_WEAK (last) = 0;
6341 TREE_CHAIN (last) = 0;
6344 /* If there was a previous prototype,
6345 set the DECL_ARG_TYPE of each argument according to
6346 the type previously specified, and report any mismatches. */
6348 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6351 for (parm = DECL_ARGUMENTS (fndecl),
6352 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6353 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6354 != void_type_node));
6355 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6357 if (parm == 0 || type == 0
6358 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6360 error ("number of arguments doesn't match prototype");
6361 error ("%Hprototype declaration",
6362 ¤t_function_prototype_locus);
6365 /* Type for passing arg must be consistent with that
6366 declared for the arg. ISO C says we take the unqualified
6367 type for parameters declared with qualified type. */
6368 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6369 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6371 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6372 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6374 /* Adjust argument to match prototype. E.g. a previous
6375 `int foo(float);' prototype causes
6376 `int foo(x) float x; {...}' to be treated like
6377 `int foo(float x) {...}'. This is particularly
6378 useful for argument types like uid_t. */
6379 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6381 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6382 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6383 && TYPE_PRECISION (TREE_TYPE (parm))
6384 < TYPE_PRECISION (integer_type_node))
6385 DECL_ARG_TYPE (parm) = integer_type_node;
6389 pedwarn ("promoted argument '%D' "
6390 "doesn't match prototype", parm);
6391 pedwarn ("%Hprototype declaration",
6392 ¤t_function_prototype_locus);
6397 error ("argument '%D' doesn't match prototype", parm);
6398 error ("%Hprototype declaration",
6399 ¤t_function_prototype_locus);
6403 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6406 /* Otherwise, create a prototype that would match. */
6410 tree actual = 0, last = 0, type;
6412 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6414 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6416 TREE_CHAIN (last) = type;
6421 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6423 TREE_CHAIN (last) = type;
6427 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6428 of the type of this function, but we need to avoid having this
6429 affect the types of other similarly-typed functions, so we must
6430 first force the generation of an identical (but separate) type
6431 node for the relevant function type. The new node we create
6432 will be a variant of the main variant of the original function
6435 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6437 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6441 /* Store the parameter declarations into the current function declaration.
6442 This is called after parsing the parameter declarations, before
6443 digesting the body of the function.
6445 For an old-style definition, construct a prototype out of the old-style
6446 parameter declarations and inject it into the function's type. */
6449 store_parm_decls (void)
6451 tree fndecl = current_function_decl;
6454 /* The argument information block for FNDECL. */
6455 tree arg_info = current_function_arg_info;
6456 current_function_arg_info = 0;
6458 /* True if this definition is written with a prototype. Note:
6459 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6460 list in a function definition as equivalent to (void) -- an
6461 empty argument list specifies the function has no parameters,
6462 but only (void) sets up a prototype for future calls. */
6463 proto = ARG_INFO_TYPES (arg_info) != 0;
6466 store_parm_decls_newstyle (fndecl, arg_info);
6468 store_parm_decls_oldstyle (fndecl, arg_info);
6470 /* The next call to push_scope will be a function body. */
6472 next_is_function_body = true;
6474 /* Write a record describing this function definition to the prototypes
6475 file (if requested). */
6477 gen_aux_info_record (fndecl, 1, 0, proto);
6479 /* Initialize the RTL code for the function. */
6480 allocate_struct_function (fndecl);
6482 /* Begin the statement tree for this function. */
6483 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6485 /* ??? Insert the contents of the pending sizes list into the function
6486 to be evaluated. This just changes mis-behaviour until assign_parms
6487 phase ordering problems are resolved. */
6490 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6491 add_stmt (TREE_VALUE (t));
6494 /* Even though we're inside a function body, we still don't want to
6495 call expand_expr to calculate the size of a variable-sized array.
6496 We haven't necessarily assigned RTL to all variables yet, so it's
6497 not safe to try to expand expressions involving them. */
6498 cfun->x_dont_save_pending_sizes_p = 1;
6501 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6504 c_finalize (tree fndecl)
6506 struct cgraph_node *cgn;
6508 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6509 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6511 /* ??? Objc emits functions after finalizing the compilation unit.
6512 This should be cleaned up later and this conditional removed. */
6513 if (cgraph_global_info_ready)
6515 c_expand_body (fndecl);
6519 /* Finalize all nested functions now. */
6520 cgn = cgraph_node (fndecl);
6521 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6522 c_finalize (cgn->decl);
6524 cgraph_finalize_function (fndecl, false);
6527 /* Finish up a function declaration and compile that function
6528 all the way to assembler language output. The free the storage
6529 for the function definition.
6531 This is called after parsing the body of the function definition. */
6534 finish_function (void)
6536 tree fndecl = current_function_decl;
6538 if (TREE_CODE (fndecl) == FUNCTION_DECL
6539 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6541 tree args = DECL_ARGUMENTS (fndecl);
6542 for (; args; args = TREE_CHAIN (args))
6544 tree type = TREE_TYPE (args);
6545 if (INTEGRAL_TYPE_P (type)
6546 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6547 DECL_ARG_TYPE (args) = integer_type_node;
6551 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6552 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6554 /* Must mark the RESULT_DECL as being in this function. */
6556 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6557 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6559 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6561 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6562 != integer_type_node)
6564 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6565 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6567 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6572 c_finish_return (integer_zero_node);
6576 /* Tie off the statement tree for this function. */
6577 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6579 finish_fname_decls ();
6581 /* Complain if there's just no return statement. */
6582 if (warn_return_type
6583 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6584 && !current_function_returns_value && !current_function_returns_null
6585 /* Don't complain if we abort. */
6586 && !current_function_returns_abnormally
6587 /* Don't warn for main(). */
6588 && !MAIN_NAME_P (DECL_NAME (fndecl))
6589 /* Or if they didn't actually specify a return type. */
6590 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6591 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6592 inline function, as we might never be compiled separately. */
6593 && DECL_INLINE (fndecl))
6594 warning ("no return statement in function returning non-void");
6596 /* With just -Wextra, complain only if function returns both with
6597 and without a value. */
6599 && current_function_returns_value
6600 && current_function_returns_null)
6601 warning ("this function may return with or without a value");
6603 /* Store the end of the function, so that we get good line number
6604 info for the epilogue. */
6605 cfun->function_end_locus = input_location;
6607 /* If we don't have ctors/dtors sections, and this is a static
6608 constructor or destructor, it must be recorded now. */
6609 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6610 && !targetm.have_ctors_dtors)
6611 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6612 if (DECL_STATIC_DESTRUCTOR (fndecl)
6613 && !targetm.have_ctors_dtors)
6614 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6616 /* Finalize the ELF visibility for the function. */
6617 c_determine_visibility (fndecl);
6619 /* Genericize before inlining. Delay genericizing nested functions
6620 until their parent function is genericized. Since finalizing
6621 requires GENERIC, delay that as well. */
6623 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6625 if (!decl_function_context (fndecl))
6627 c_genericize (fndecl);
6628 lower_nested_functions (fndecl);
6629 c_finalize (fndecl);
6633 /* Register this function with cgraph just far enough to get it
6634 added to our parent's nested function list. Handy, since the
6635 C front end doesn't have such a list. */
6636 (void) cgraph_node (fndecl);
6640 /* We're leaving the context of this function, so zap cfun.
6641 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6642 tree_rest_of_compilation. */
6644 current_function_decl = NULL;
6647 /* Generate the RTL for the body of FNDECL. */
6650 c_expand_body (tree fndecl)
6653 if (!DECL_INITIAL (fndecl)
6654 || DECL_INITIAL (fndecl) == error_mark_node)
6657 tree_rest_of_compilation (fndecl, false);
6659 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6660 && targetm.have_ctors_dtors)
6661 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6662 DEFAULT_INIT_PRIORITY);
6663 if (DECL_STATIC_DESTRUCTOR (fndecl)
6664 && targetm.have_ctors_dtors)
6665 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6666 DEFAULT_INIT_PRIORITY);
6669 /* Check the declarations given in a for-loop for satisfying the C99
6672 check_for_loop_decls (void)
6674 struct c_binding *b;
6678 /* If we get here, declarations have been used in a for loop without
6679 the C99 for loop scope. This doesn't make much sense, so don't
6681 error ("'for' loop initial declaration used outside C99 mode");
6684 /* C99 subclause 6.8.5 paragraph 3:
6686 [#3] The declaration part of a for statement shall only
6687 declare identifiers for objects having storage class auto or
6690 It isn't clear whether, in this sentence, "identifiers" binds to
6691 "shall only declare" or to "objects" - that is, whether all identifiers
6692 declared must be identifiers for objects, or whether the restriction
6693 only applies to those that are. (A question on this in comp.std.c
6694 in November 2000 received no answer.) We implement the strictest
6695 interpretation, to avoid creating an extension which later causes
6698 for (b = current_scope->bindings; b; b = b->prev)
6701 tree decl = b->decl;
6706 switch (TREE_CODE (decl))
6709 if (TREE_STATIC (decl))
6710 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6711 "initial declaration", decl, decl);
6712 else if (DECL_EXTERNAL (decl))
6713 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6714 "initial declaration", decl, decl);
6718 error ("'struct %E' declared in 'for' loop initial declaration", id);
6721 error ("'union %E' declared in 'for' loop initial declaration", id);
6724 error ("'enum %E' declared in 'for' loop initial declaration", id);
6727 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6728 "initial declaration", decl, decl);
6733 /* Save and reinitialize the variables
6734 used during compilation of a C function. */
6737 c_push_function_context (struct function *f)
6739 struct language_function *p;
6740 p = GGC_NEW (struct language_function);
6743 p->base.x_stmt_tree = c_stmt_tree;
6744 p->x_break_label = c_break_label;
6745 p->x_cont_label = c_cont_label;
6746 p->x_switch_stack = c_switch_stack;
6747 p->arg_info = current_function_arg_info;
6748 p->returns_value = current_function_returns_value;
6749 p->returns_null = current_function_returns_null;
6750 p->returns_abnormally = current_function_returns_abnormally;
6751 p->warn_about_return_type = warn_about_return_type;
6752 p->extern_inline = current_extern_inline;
6755 /* Restore the variables used during compilation of a C function. */
6758 c_pop_function_context (struct function *f)
6760 struct language_function *p = f->language;
6762 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6763 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6765 /* Stop pointing to the local nodes about to be freed. */
6766 /* But DECL_INITIAL must remain nonzero so we know this
6767 was an actual function definition. */
6768 DECL_INITIAL (current_function_decl) = error_mark_node;
6769 DECL_ARGUMENTS (current_function_decl) = 0;
6772 c_stmt_tree = p->base.x_stmt_tree;
6773 c_break_label = p->x_break_label;
6774 c_cont_label = p->x_cont_label;
6775 c_switch_stack = p->x_switch_stack;
6776 current_function_arg_info = p->arg_info;
6777 current_function_returns_value = p->returns_value;
6778 current_function_returns_null = p->returns_null;
6779 current_function_returns_abnormally = p->returns_abnormally;
6780 warn_about_return_type = p->warn_about_return_type;
6781 current_extern_inline = p->extern_inline;
6786 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6789 c_dup_lang_specific_decl (tree decl)
6791 struct lang_decl *ld;
6793 if (!DECL_LANG_SPECIFIC (decl))
6796 ld = GGC_NEW (struct lang_decl);
6797 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6798 DECL_LANG_SPECIFIC (decl) = ld;
6801 /* The functions below are required for functionality of doing
6802 function at once processing in the C front end. Currently these
6803 functions are not called from anywhere in the C front end, but as
6804 these changes continue, that will change. */
6806 /* Returns nonzero if the current statement is a full expression,
6807 i.e. temporaries created during that statement should be destroyed
6808 at the end of the statement. */
6811 stmts_are_full_exprs_p (void)
6816 /* Returns the stmt_tree (if any) to which statements are currently
6817 being added. If there is no active statement-tree, NULL is
6821 current_stmt_tree (void)
6823 return &c_stmt_tree;
6826 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6830 anon_aggr_type_p (tree ARG_UNUSED (node))
6835 /* Dummy function in place of callback used by C++. */
6838 extract_interface_info (void)
6842 /* Return the global value of T as a symbol. */
6845 identifier_global_value (tree t)
6847 struct c_binding *b;
6849 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6850 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6856 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6857 otherwise the name is found in ridpointers from RID_INDEX. */
6860 record_builtin_type (enum rid rid_index, const char *name, tree type)
6864 id = ridpointers[(int) rid_index];
6866 id = get_identifier (name);
6867 decl = build_decl (TYPE_DECL, id, type);
6869 if (debug_hooks->type_decl)
6870 debug_hooks->type_decl (decl, false);
6873 /* Build the void_list_node (void_type_node having been created). */
6875 build_void_list_node (void)
6877 tree t = build_tree_list (NULL_TREE, void_type_node);
6881 /* Return a structure for a parameter with the given SPECS, ATTRS and
6885 build_c_parm (tree specs, tree attrs, tree declarator)
6887 return build_tree_list (build_tree_list (specs, declarator), attrs);
6890 /* Return a declarator with nested attributes. TARGET is the inner
6891 declarator to which these attributes apply. ATTRS are the
6895 build_attrs_declarator (tree attrs, tree target)
6897 return tree_cons (attrs, target, NULL_TREE);
6900 /* Return a declarator for a function with arguments specified by ARGS
6901 and return type specified by TARGET. */
6904 build_function_declarator (tree args, tree target)
6906 return build_nt (CALL_EXPR, target, args, NULL_TREE);
6909 /* Return something to represent absolute declarators containing a *.
6910 TARGET is the absolute declarator that the * contains.
6911 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6912 to apply to the pointer type, represented as identifiers, possible mixed
6915 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6916 if attributes are present) and whose type is the modifier list. */
6919 make_pointer_declarator (tree type_quals_attrs, tree target)
6922 tree itarget = target;
6923 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6924 if (attrs != NULL_TREE)
6925 itarget = build_attrs_declarator (attrs, target);
6926 return build1 (INDIRECT_REF, quals, itarget);
6929 /* Synthesize a function which calls all the global ctors or global
6930 dtors in this file. This is only used for targets which do not
6931 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6933 build_cdtor (int method_type, tree cdtors)
6940 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6941 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6944 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
6947 /* Perform final processing on one file scope's declarations (or the
6948 external scope's declarations), GLOBALS. */
6950 c_write_global_declarations_1 (tree globals)
6952 size_t len = list_length (globals);
6953 tree *vec = XNEWVEC (tree, len);
6957 /* Process the decls in the order they were written. */
6958 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6961 wrapup_global_declarations (vec, len);
6962 check_global_declarations (vec, len);
6968 c_write_global_declarations (void)
6972 /* We don't want to do this if generating a PCH. */
6976 /* Don't waste time on further processing if -fsyntax-only or we've
6977 encountered errors. */
6978 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
6981 /* Close the external scope. */
6982 ext_block = pop_scope ();
6987 /* Process all file scopes in this compilation, and the external_scope,
6988 through wrapup_global_declarations and check_global_declarations. */
6989 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6990 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6991 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6993 /* Generate functions to call static constructors and destructors
6994 for targets that do not support .ctors/.dtors sections. These
6995 functions have magic names which are detected by collect2. */
6996 build_cdtor ('I', static_ctors); static_ctors = 0;
6997 build_cdtor ('D', static_dtors); static_dtors = 0;
6999 /* We're done parsing; proceed to optimize and emit assembly.
7000 FIXME: shouldn't be the front end's responsibility to call this. */
7003 /* Presently this has to happen after cgraph_optimize.
7004 FIXME: shouldn't be the front end's responsibility to call this. */
7006 mudflap_finish_file ();
7009 #include "gt-c-decl.h"