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, 2005 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"
36 #include "tree-inline.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
62 #include "langhooks-def.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type;
156 /* Nonzero when starting a function declared `extern inline'. */
158 static int current_extern_inline;
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
163 static bool undef_nested_function;
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
203 struct c_binding GTY((chain_next ("%h.prev")))
205 tree decl; /* the decl bound */
206 tree type; /* the type in this scope */
207 tree id; /* the identifier it's bound to */
208 struct c_binding *prev; /* the previous decl in this scope */
209 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
210 unsigned int depth : 28; /* depth of this scope */
211 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
240 struct lang_identifier GTY(())
242 struct c_common_identifier common_id;
243 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244 struct c_binding *tag_binding; /* struct/union/enum tags */
245 struct c_binding *label_binding; /* labels */
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
252 /* The resulting tree type. */
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 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)")))
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
261 struct lang_identifier GTY ((tag ("1"))) identifier;
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
269 Most declarations are recorded in the current scope.
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
309 struct c_scope GTY((chain_next ("%h.outer")))
311 /* The scope containing this one. */
312 struct c_scope *outer;
314 /* The next outermost function scope. */
315 struct c_scope *outer_function;
317 /* All bindings in this scope. */
318 struct c_binding *bindings;
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth : 28;
329 /* True if we are currently filling this scope with parameter
331 BOOL_BITFIELD parm_flag : 1;
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls : 1;
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body : 1;
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep : 1;
348 /* The scope currently in effect. */
350 static GTY(()) struct c_scope *current_scope;
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
356 static GTY(()) struct c_scope *current_function_scope;
358 /* The C file scope. This is reset for each input translation unit. */
360 static GTY(()) struct c_scope *file_scope;
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
365 static GTY(()) struct c_scope *external_scope;
367 /* A chain of c_scope structures awaiting reuse. */
369 static GTY((deletable)) struct c_scope *scope_freelist;
371 /* A chain of c_binding structures awaiting reuse. */
373 static GTY((deletable)) struct c_binding *binding_freelist;
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
383 s_->list##_last = d_; \
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
394 t_->to##_last = f_->from##_last; \
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
399 static bool keep_next_level_flag;
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
405 static bool next_is_function_body;
407 /* Functions called automatically at the beginning and end of execution. */
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
412 /* Forward declarations. */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416 struct c_declspecs *,
417 enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
426 enum deprecated_states {
431 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
434 c_print_identifier (FILE *file, tree node, int indent)
436 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
437 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
438 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
439 if (C_IS_RESERVED_WORD (node))
441 tree rid = ridpointers[C_RID_CODE (node)];
442 indent_to (file, indent + 4);
443 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
444 (void *) rid, IDENTIFIER_POINTER (rid));
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
452 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
454 struct c_binding *b, **here;
456 if (binding_freelist)
458 b = binding_freelist;
459 binding_freelist = b->prev;
462 b = GGC_NEW (struct c_binding);
467 b->depth = scope->depth;
468 b->invisible = invisible;
474 b->prev = scope->bindings;
480 switch (TREE_CODE (decl))
482 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
485 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
491 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here && (*here)->depth > scope->depth)
501 here = &(*here)->shadowed;
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding *
512 free_binding_and_advance (struct c_binding *b)
514 struct c_binding *prev = b->prev;
516 memset (b, 0, sizeof (struct c_binding));
517 b->prev = binding_freelist;
518 binding_freelist = b;
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
528 c_finish_incomplete_decl (tree decl)
530 if (TREE_CODE (decl) == VAR_DECL)
532 tree type = TREE_TYPE (decl);
533 if (type != error_mark_node
534 && TREE_CODE (type) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl)
536 && TYPE_DOMAIN (type) == 0)
538 warning (0, "%Jarray %qD assumed to have one element", decl, decl);
540 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
542 layout_decl (decl, 0);
547 /* The Objective-C front-end often needs to determine the current scope. */
550 objc_get_current_scope (void)
552 return current_scope;
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
559 objc_mark_locals_volatile (void *enclosing_blk)
561 struct c_scope *scope;
564 for (scope = current_scope;
565 scope && scope != enclosing_blk;
566 scope = scope->outer)
568 for (b = scope->bindings; b; b = b->prev)
570 if (TREE_CODE (b->decl) == VAR_DECL
571 || TREE_CODE (b->decl) == PARM_DECL)
573 C_DECL_REGISTER (b->decl) = 0;
574 DECL_REGISTER (b->decl) = 0;
575 TREE_THIS_VOLATILE (b->decl) = 1;
579 /* Do not climb up past the current function. */
580 if (scope->function_body)
585 /* Nonzero if we are currently in file scope. */
588 global_bindings_p (void)
590 return current_scope == file_scope && !c_override_global_bindings_to_false;
594 keep_next_level (void)
596 keep_next_level_flag = true;
599 /* Identify this scope as currently being filled with parameters. */
602 declare_parm_level (void)
604 current_scope->parm_flag = true;
610 if (next_is_function_body)
612 /* This is the transition from the parameters to the top level
613 of the function body. These are the same scope
614 (C99 6.2.1p4,6) so we do not push another scope structure.
615 next_is_function_body is set only by store_parm_decls, which
616 in turn is called when and only when we are about to
617 encounter the opening curly brace for the function body.
619 The outermost block of a function always gets a BLOCK node,
620 because the debugging output routines expect that each
621 function has at least one BLOCK. */
622 current_scope->parm_flag = false;
623 current_scope->function_body = true;
624 current_scope->keep = true;
625 current_scope->outer_function = current_function_scope;
626 current_function_scope = current_scope;
628 keep_next_level_flag = false;
629 next_is_function_body = false;
633 struct c_scope *scope;
636 scope = scope_freelist;
637 scope_freelist = scope->outer;
640 scope = GGC_CNEW (struct c_scope);
642 scope->keep = keep_next_level_flag;
643 scope->outer = current_scope;
644 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
646 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
648 if (current_scope && scope->depth == 0)
651 sorry ("GCC supports only %u nested scopes", scope->depth);
654 current_scope = scope;
655 keep_next_level_flag = false;
659 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
662 set_type_context (tree type, tree context)
664 for (type = TYPE_MAIN_VARIANT (type); type;
665 type = TYPE_NEXT_VARIANT (type))
666 TYPE_CONTEXT (type) = context;
669 /* Exit a scope. Restore the state of the identifier-decl mappings
670 that were in effect when this scope was entered. Return a BLOCK
671 node containing all the DECLs in this scope that are of interest
672 to debug info generation. */
677 struct c_scope *scope = current_scope;
678 tree block, context, p;
681 bool functionbody = scope->function_body;
682 bool keep = functionbody || scope->keep || scope->bindings;
684 c_end_vm_scope (scope->depth);
686 /* If appropriate, create a BLOCK to record the decls for the life
691 block = make_node (BLOCK);
692 BLOCK_SUBBLOCKS (block) = scope->blocks;
693 TREE_USED (block) = 1;
695 /* In each subblock, record that this is its superior. */
696 for (p = scope->blocks; p; p = TREE_CHAIN (p))
697 BLOCK_SUPERCONTEXT (p) = block;
699 BLOCK_VARS (block) = 0;
702 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
703 scope must be set so that they point to the appropriate
704 construct, i.e. either to the current FUNCTION_DECL node, or
705 else to the BLOCK node we just constructed.
707 Note that for tagged types whose scope is just the formal
708 parameter list for some function type specification, we can't
709 properly set their TYPE_CONTEXTs here, because we don't have a
710 pointer to the appropriate FUNCTION_TYPE node readily available
711 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
712 type nodes get set in `grokdeclarator' as soon as we have created
713 the FUNCTION_TYPE node which will represent the "scope" for these
714 "parameter list local" tagged types. */
715 if (scope->function_body)
716 context = current_function_decl;
717 else if (scope == file_scope)
719 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
720 TREE_CHAIN (file_decl) = all_translation_units;
721 all_translation_units = file_decl;
727 /* Clear all bindings in this scope. */
728 for (b = scope->bindings; b; b = free_binding_and_advance (b))
731 switch (TREE_CODE (p))
734 /* Warnings for unused labels, errors for undefined labels. */
735 if (TREE_USED (p) && !DECL_INITIAL (p))
737 error ("%Jlabel %qD used but not defined", p, p);
738 DECL_INITIAL (p) = error_mark_node;
740 else if (!TREE_USED (p) && warn_unused_label)
742 if (DECL_INITIAL (p))
743 warning (0, "%Jlabel %qD defined but not used", p, p);
745 warning (0, "%Jlabel %qD declared but not defined", p, p);
747 /* Labels go in BLOCK_VARS. */
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
750 gcc_assert (I_LABEL_BINDING (b->id) == b);
751 I_LABEL_BINDING (b->id) = b->shadowed;
757 set_type_context (p, context);
759 /* Types may not have tag-names, in which case the type
760 appears in the bindings list with b->id NULL. */
763 gcc_assert (I_TAG_BINDING (b->id) == b);
764 I_TAG_BINDING (b->id) = b->shadowed;
769 /* Propagate TREE_ADDRESSABLE from nested functions to their
770 containing functions. */
771 if (!TREE_ASM_WRITTEN (p)
772 && DECL_INITIAL (p) != 0
773 && TREE_ADDRESSABLE (p)
774 && DECL_ABSTRACT_ORIGIN (p) != 0
775 && DECL_ABSTRACT_ORIGIN (p) != p)
776 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
777 if (!DECL_EXTERNAL (p)
778 && DECL_INITIAL (p) == 0)
780 error ("%Jnested function %qD declared but never defined", p, p);
781 undef_nested_function = true;
786 /* Warnings for unused variables. */
787 if (warn_unused_variable
789 && !DECL_IN_SYSTEM_HEADER (p)
791 && !DECL_ARTIFICIAL (p)
792 && scope != file_scope
793 && scope != external_scope)
794 warning (0, "%Junused variable %qD", p, p);
798 error ("%Jtype of array %qD completed incompatibly with"
799 " implicit initialization", p, p);
806 /* All of these go in BLOCK_VARS, but only if this is the
807 binding in the home scope. */
810 TREE_CHAIN (p) = BLOCK_VARS (block);
811 BLOCK_VARS (block) = p;
813 /* If this is the file scope, and we are processing more
814 than one translation unit in this compilation, set
815 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
816 This makes same_translation_unit_p work, and causes
817 static declarations to be given disambiguating suffixes. */
818 if (scope == file_scope && num_in_fnames > 1)
820 DECL_CONTEXT (p) = context;
821 if (TREE_CODE (p) == TYPE_DECL)
822 set_type_context (TREE_TYPE (p), context);
826 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
827 already been put there by store_parm_decls. Unused-
828 parameter warnings are handled by function.c.
829 error_mark_node obviously does not go in BLOCK_VARS and
830 does not get unused-variable warnings. */
833 /* It is possible for a decl not to have a name. We get
834 here with b->id NULL in this case. */
837 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
838 I_SYMBOL_BINDING (b->id) = b->shadowed;
839 if (b->shadowed && b->shadowed->type)
840 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
850 /* Dispose of the block that we just made inside some higher level. */
851 if ((scope->function_body || scope == file_scope) && context)
853 DECL_INITIAL (context) = block;
854 BLOCK_SUPERCONTEXT (block) = context;
856 else if (scope->outer)
859 SCOPE_LIST_APPEND (scope->outer, blocks, block);
860 /* If we did not make a block for the scope just exited, any
861 blocks made for inner scopes must be carried forward so they
862 will later become subblocks of something else. */
863 else if (scope->blocks)
864 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
867 /* Pop the current scope, and free the structure for reuse. */
868 current_scope = scope->outer;
869 if (scope->function_body)
870 current_function_scope = scope->outer_function;
872 memset (scope, 0, sizeof (struct c_scope));
873 scope->outer = scope_freelist;
874 scope_freelist = scope;
880 push_file_scope (void)
888 file_scope = current_scope;
890 start_fname_decls ();
892 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
893 bind (DECL_NAME (decl), decl, file_scope,
894 /*invisible=*/false, /*nested=*/true);
898 pop_file_scope (void)
900 /* In case there were missing closebraces, get us back to the global
902 while (current_scope != file_scope)
905 /* __FUNCTION__ is defined at file scope (""). This
906 call may not be necessary as my tests indicate it
907 still works without it. */
908 finish_fname_decls ();
910 /* This is the point to write out a PCH if we're doing that.
911 In that case we do not want to do anything else. */
914 c_common_write_pch ();
918 /* Pop off the file scope and close this translation unit. */
922 maybe_apply_pending_pragma_weaks ();
923 cgraph_finalize_compilation_unit ();
926 /* Insert BLOCK at the end of the list of subblocks of the current
927 scope. This is used when a BIND_EXPR is expanded, to handle the
928 BLOCK node inside the BIND_EXPR. */
931 insert_block (tree block)
933 TREE_USED (block) = 1;
934 SCOPE_LIST_APPEND (current_scope, blocks, block);
937 /* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
945 pushtag (tree name, tree type)
947 /* Record the identifier as the type's name if it has none. */
948 if (name && !TYPE_NAME (type))
949 TYPE_NAME (type) = name;
950 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
953 tagged type we just added to the current scope. This fake
954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
955 to output a representation of a tagged type, and it also gives
956 us a convenient place to record the "scope start" address for the
959 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
961 /* An approximation for now, so we can tell this is a function-scope tag.
962 This will be updated in pop_scope. */
963 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
966 /* Subroutine of compare_decls. Allow harmless mismatches in return
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
971 match_builtin_function_types (tree newtype, tree oldtype)
973 tree newrettype, oldrettype;
974 tree newargs, oldargs;
975 tree trytype, tryargs;
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype = TREE_TYPE (oldtype);
979 newrettype = TREE_TYPE (newtype);
981 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
984 oldargs = TYPE_ARG_TYPES (oldtype);
985 newargs = TYPE_ARG_TYPES (newtype);
988 while (oldargs || newargs)
992 || !TREE_VALUE (oldargs)
993 || !TREE_VALUE (newargs)
994 || TYPE_MODE (TREE_VALUE (oldargs))
995 != TYPE_MODE (TREE_VALUE (newargs)))
998 oldargs = TREE_CHAIN (oldargs);
999 newargs = TREE_CHAIN (newargs);
1002 trytype = build_function_type (newrettype, tryargs);
1003 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1006 /* Subroutine of diagnose_mismatched_decls. Check for function type
1007 mismatch involving an empty arglist vs a nonempty one and give clearer
1010 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1011 tree newtype, tree oldtype)
1015 if (TREE_CODE (olddecl) != FUNCTION_DECL
1016 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1017 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1019 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1022 t = TYPE_ARG_TYPES (oldtype);
1024 t = TYPE_ARG_TYPES (newtype);
1025 for (; t; t = TREE_CHAIN (t))
1027 tree type = TREE_VALUE (t);
1029 if (TREE_CHAIN (t) == 0
1030 && TYPE_MAIN_VARIANT (type) != void_type_node)
1032 inform ("a parameter list with an ellipsis can%'t match "
1033 "an empty parameter name list declaration");
1037 if (c_type_promotes_to (type) != type)
1039 inform ("an argument type that has a default promotion can%'t match "
1040 "an empty parameter name list declaration");
1046 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1051 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1053 tree newargs, oldargs;
1056 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1058 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1059 newargs = TYPE_ARG_TYPES (newtype);
1064 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1065 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1067 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1070 /* Reaching the end of just one list means the two decls don't
1071 agree on the number of arguments. */
1072 if (END_OF_ARGLIST (oldargtype))
1074 error ("%Jprototype for %qD declares more arguments "
1075 "than previous old-style definition", newdecl, newdecl);
1078 else if (END_OF_ARGLIST (newargtype))
1080 error ("%Jprototype for %qD declares fewer arguments "
1081 "than previous old-style definition", newdecl, newdecl);
1085 /* Type for passing arg must be consistent with that declared
1087 else if (!comptypes (oldargtype, newargtype))
1089 error ("%Jprototype for %qD declares argument %d"
1090 " with incompatible type",
1091 newdecl, newdecl, i);
1095 oldargs = TREE_CHAIN (oldargs);
1096 newargs = TREE_CHAIN (newargs);
1100 /* If we get here, no errors were found, but do issue a warning
1101 for this poor-style construct. */
1102 warning (0, "%Jprototype for %qD follows non-prototype definition",
1105 #undef END_OF_ARGLIST
1108 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1109 first in a pair of mismatched declarations, using the diagnostic
1112 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1114 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1116 else if (DECL_INITIAL (decl))
1117 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1118 else if (C_DECL_IMPLICIT (decl))
1119 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1121 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1124 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1125 Returns true if the caller should proceed to merge the two, false
1126 if OLDDECL should simply be discarded. As a side effect, issues
1127 all necessary diagnostics for invalid or poor-style combinations.
1128 If it returns true, writes the types of NEWDECL and OLDDECL to
1129 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1130 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1133 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1134 tree *newtypep, tree *oldtypep)
1136 tree newtype, oldtype;
1137 bool pedwarned = false;
1138 bool warned = false;
1141 /* If we have error_mark_node for either decl or type, just discard
1142 the previous decl - we're in an error cascade already. */
1143 if (olddecl == error_mark_node || newdecl == error_mark_node)
1145 *oldtypep = oldtype = TREE_TYPE (olddecl);
1146 *newtypep = newtype = TREE_TYPE (newdecl);
1147 if (oldtype == error_mark_node || newtype == error_mark_node)
1150 /* Two different categories of symbol altogether. This is an error
1151 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1152 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1154 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1155 && DECL_BUILT_IN (olddecl)
1156 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1158 error ("%J%qD redeclared as different kind of symbol",
1160 locate_old_decl (olddecl, error);
1162 else if (TREE_PUBLIC (newdecl))
1163 warning (0, "%Jbuilt-in function %qD declared as non-function",
1165 else if (warn_shadow)
1166 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1171 /* Enumerators have no linkage, so may only be declared once in a
1173 if (TREE_CODE (olddecl) == CONST_DECL)
1175 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1176 locate_old_decl (olddecl, error);
1180 if (!comptypes (oldtype, newtype))
1182 if (TREE_CODE (olddecl) == FUNCTION_DECL
1183 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1185 /* Accept harmless mismatch in function types.
1186 This is for the ffs and fprintf builtins. */
1187 tree trytype = match_builtin_function_types (newtype, oldtype);
1189 if (trytype && comptypes (newtype, trytype))
1190 *oldtypep = oldtype = trytype;
1193 /* If types don't match for a built-in, throw away the
1194 built-in. No point in calling locate_old_decl here, it
1195 won't print anything. */
1196 warning (0, "%Jconflicting types for built-in function %qD",
1201 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_IS_BUILTIN (olddecl))
1204 /* A conflicting function declaration for a predeclared
1205 function that isn't actually built in. Objective C uses
1206 these. The new declaration silently overrides everything
1207 but the volatility (i.e. noreturn) indication. See also
1208 below. FIXME: Make Objective C use normal builtins. */
1209 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1212 /* Permit void foo (...) to match int foo (...) if the latter is
1213 the definition and implicit int was used. See
1214 c-torture/compile/920625-2.c. */
1215 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1216 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1217 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1218 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1220 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1221 /* Make sure we keep void as the return type. */
1222 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1223 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1226 /* Permit void foo (...) to match an earlier call to foo (...) with
1227 no declared type (thus, implicitly int). */
1228 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1229 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1230 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1231 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1233 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1234 /* Make sure we keep void as the return type. */
1235 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1240 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1241 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1243 error ("%Jconflicting types for %qD", newdecl, newdecl);
1244 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1245 locate_old_decl (olddecl, error);
1250 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1251 but silently ignore the redeclaration if either is in a system
1252 header. (Conflicting redeclarations were handled above.) */
1253 if (TREE_CODE (newdecl) == TYPE_DECL)
1255 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1256 return true; /* Allow OLDDECL to continue in use. */
1258 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1263 /* Function declarations can either be 'static' or 'extern' (no
1264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1265 can never conflict with each other on account of linkage (6.2.2p4).
1266 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1267 two definitions if one is 'extern inline' and one is not. The non-
1268 extern-inline definition supersedes the extern-inline definition. */
1269 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1271 /* If you declare a built-in function name as static, or
1272 define the built-in with an old-style definition (so we
1273 can't validate the argument list) the built-in definition is
1274 overridden, but optionally warn this was a bad choice of name. */
1275 if (DECL_BUILT_IN (olddecl)
1276 && !C_DECL_DECLARED_BUILTIN (olddecl)
1277 && (!TREE_PUBLIC (newdecl)
1278 || (DECL_INITIAL (newdecl)
1279 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1282 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1284 /* Discard the old built-in function. */
1288 if (DECL_INITIAL (newdecl))
1290 if (DECL_INITIAL (olddecl))
1292 /* If both decls have extern inline and are in the same TU,
1293 reject the new decl. */
1294 if (DECL_DECLARED_INLINE_P (olddecl)
1295 && DECL_EXTERNAL (olddecl)
1296 && DECL_DECLARED_INLINE_P (newdecl)
1297 && DECL_EXTERNAL (newdecl)
1298 && same_translation_unit_p (newdecl, olddecl))
1300 error ("%Jredefinition of %qD", newdecl, newdecl);
1301 locate_old_decl (olddecl, error);
1304 /* If both decls have not extern inline, reject the new decl. */
1305 if (!DECL_DECLARED_INLINE_P (olddecl)
1306 && !DECL_EXTERNAL (olddecl)
1307 && !DECL_DECLARED_INLINE_P (newdecl)
1308 && !DECL_EXTERNAL (newdecl))
1310 error ("%Jredefinition of %qD", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1314 /* If the new decl is declared as extern inline, error if they are
1315 in the same TU, otherwise retain the old decl. */
1316 if (!DECL_DECLARED_INLINE_P (olddecl)
1317 && !DECL_EXTERNAL (olddecl)
1318 && DECL_DECLARED_INLINE_P (newdecl)
1319 && DECL_EXTERNAL (newdecl))
1321 if (same_translation_unit_p (newdecl, olddecl))
1323 error ("%Jredefinition of %qD", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl)
1335 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1337 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1339 locate_old_decl (olddecl, error);
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl)
1359 && !(DECL_EXTERNAL (olddecl)
1360 && DECL_DECLARED_INLINE_P (olddecl)))
1362 error ("%Jstatic declaration of %qD follows "
1363 "non-static declaration", newdecl, newdecl);
1364 locate_old_decl (olddecl, error);
1368 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1370 if (DECL_CONTEXT (olddecl))
1372 error ("%Jnon-static declaration of %qD follows "
1373 "static declaration", newdecl, newdecl);
1374 locate_old_decl (olddecl, error);
1377 else if (warn_traditional)
1379 warning (0, "%Jnon-static declaration of %qD follows "
1380 "static declaration", newdecl, newdecl);
1385 else if (TREE_CODE (newdecl) == VAR_DECL)
1387 /* Only variables can be thread-local, and all declarations must
1388 agree on this property. */
1389 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1391 if (DECL_THREAD_LOCAL (newdecl))
1392 error ("%Jthread-local declaration of %qD follows "
1393 "non-thread-local declaration", newdecl, newdecl);
1395 error ("%Jnon-thread-local declaration of %qD follows "
1396 "thread-local declaration", newdecl, newdecl);
1398 locate_old_decl (olddecl, error);
1402 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1403 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1405 error ("%Jredefinition of %qD", newdecl, newdecl);
1406 locate_old_decl (olddecl, error);
1410 /* Objects declared at file scope: if the first declaration had
1411 external linkage (even if it was an external reference) the
1412 second must have external linkage as well, or the behavior is
1413 undefined. If the first declaration had internal linkage, then
1414 the second must too, or else be an external reference (in which
1415 case the composite declaration still has internal linkage).
1416 As for function declarations, we warn about the static-then-
1417 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1418 if (DECL_FILE_SCOPE_P (newdecl)
1419 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1421 if (DECL_EXTERNAL (newdecl))
1423 if (!DECL_FILE_SCOPE_P (olddecl))
1425 error ("%Jextern declaration of %qD follows "
1426 "declaration with no linkage", newdecl, newdecl);
1427 locate_old_decl (olddecl, error);
1430 else if (warn_traditional)
1432 warning (0, "%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1439 if (TREE_PUBLIC (newdecl))
1440 error ("%Jnon-static declaration of %qD follows "
1441 "static declaration", newdecl, newdecl);
1443 error ("%Jstatic declaration of %qD follows "
1444 "non-static declaration", newdecl, newdecl);
1446 locate_old_decl (olddecl, error);
1450 /* Two objects with the same name declared at the same block
1451 scope must both be external references (6.7p3). */
1452 else if (!DECL_FILE_SCOPE_P (newdecl))
1454 if (DECL_EXTERNAL (newdecl))
1456 /* Extern with initializer at block scope, which will
1457 already have received an error. */
1459 else if (DECL_EXTERNAL (olddecl))
1461 error ("%Jdeclaration of %qD with no linkage follows "
1462 "extern declaration", newdecl, newdecl);
1463 locate_old_decl (olddecl, error);
1467 error ("%Jredeclaration of %qD with no linkage",
1469 locate_old_decl (olddecl, error);
1477 /* All decls must agree on a visibility. */
1478 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1479 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1481 warning (0, "%Jredeclaration of %qD with different visibility "
1482 "(old visibility preserved)", newdecl, newdecl);
1486 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1488 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1489 if (DECL_DECLARED_INLINE_P (newdecl)
1490 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1492 warning (0, "%Jinline declaration of %qD follows "
1493 "declaration with attribute noinline", newdecl, newdecl);
1496 else if (DECL_DECLARED_INLINE_P (olddecl)
1497 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1499 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1500 "inline declaration ", newdecl, newdecl);
1504 /* Inline declaration after use or definition.
1505 ??? Should we still warn about this now we have unit-at-a-time
1506 mode and can get it right?
1507 Definitely don't complain if the decls are in different translation
1509 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1510 && same_translation_unit_p (olddecl, newdecl))
1512 if (TREE_USED (olddecl))
1514 warning (0, "%J%qD declared inline after being called",
1518 else if (DECL_INITIAL (olddecl))
1520 warning (0, "%J%qD declared inline after its definition",
1526 else /* PARM_DECL, VAR_DECL */
1528 /* Redeclaration of a parameter is a constraint violation (this is
1529 not explicitly stated, but follows from C99 6.7p3 [no more than
1530 one declaration of the same identifier with no linkage in the
1531 same scope, except type tags] and 6.2.2p6 [parameters have no
1532 linkage]). We must check for a forward parameter declaration,
1533 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1534 an extension, the mandatory diagnostic for which is handled by
1535 mark_forward_parm_decls. */
1537 if (TREE_CODE (newdecl) == PARM_DECL
1538 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1540 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1541 locate_old_decl (olddecl, error);
1546 /* Optional warning for completely redundant decls. */
1547 if (!warned && !pedwarned
1548 && warn_redundant_decls
1549 /* Don't warn about a function declaration followed by a
1551 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1552 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1553 /* Don't warn about redundant redeclarations of builtins. */
1554 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1555 && !DECL_BUILT_IN (newdecl)
1556 && DECL_BUILT_IN (olddecl)
1557 && !C_DECL_DECLARED_BUILTIN (olddecl))
1558 /* Don't warn about an extern followed by a definition. */
1559 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1560 /* Don't warn about forward parameter decls. */
1561 && !(TREE_CODE (newdecl) == PARM_DECL
1562 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1564 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1568 /* Report location of previous decl/defn in a consistent manner. */
1569 if (warned || pedwarned)
1570 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1575 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1576 consistent with OLDDECL, but carries new information. Merge the
1577 new information into OLDDECL. This function issues no
1581 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1583 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1584 && DECL_INITIAL (newdecl) != 0);
1585 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1586 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1587 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1588 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1590 /* For real parm decl following a forward decl, rechain the old decl
1591 in its new location and clear TREE_ASM_WRITTEN (it's not a
1592 forward decl anymore). */
1593 if (TREE_CODE (newdecl) == PARM_DECL
1594 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1596 struct c_binding *b, **here;
1598 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1599 if ((*here)->decl == olddecl)
1606 b->prev = current_scope->bindings;
1607 current_scope->bindings = b;
1609 TREE_ASM_WRITTEN (olddecl) = 0;
1612 DECL_ATTRIBUTES (newdecl)
1613 = targetm.merge_decl_attributes (olddecl, newdecl);
1615 /* Merge the data types specified in the two decls. */
1617 = TREE_TYPE (olddecl)
1618 = composite_type (newtype, oldtype);
1620 /* Lay the type out, unless already done. */
1621 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1623 if (TREE_TYPE (newdecl) != error_mark_node)
1624 layout_type (TREE_TYPE (newdecl));
1625 if (TREE_CODE (newdecl) != FUNCTION_DECL
1626 && TREE_CODE (newdecl) != TYPE_DECL
1627 && TREE_CODE (newdecl) != CONST_DECL)
1628 layout_decl (newdecl, 0);
1632 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1633 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1634 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1635 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1636 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1637 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1639 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1640 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1644 /* Keep the old rtl since we can safely use it. */
1645 COPY_DECL_RTL (olddecl, newdecl);
1647 /* Merge the type qualifiers. */
1648 if (TREE_READONLY (newdecl))
1649 TREE_READONLY (olddecl) = 1;
1651 if (TREE_THIS_VOLATILE (newdecl))
1653 TREE_THIS_VOLATILE (olddecl) = 1;
1654 if (TREE_CODE (newdecl) == VAR_DECL)
1655 make_var_volatile (newdecl);
1658 /* Merge deprecatedness. */
1659 if (TREE_DEPRECATED (newdecl))
1660 TREE_DEPRECATED (olddecl) = 1;
1662 /* Keep source location of definition rather than declaration and of
1663 prototype rather than non-prototype unless that prototype is
1665 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1666 || (old_is_prototype && !new_is_prototype
1667 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1668 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1670 /* Merge the unused-warning information. */
1671 if (DECL_IN_SYSTEM_HEADER (olddecl))
1672 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1673 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1674 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1676 /* Merge the initialization information. */
1677 if (DECL_INITIAL (newdecl) == 0)
1678 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1680 /* Merge the section attribute.
1681 We want to issue an error if the sections conflict but that must be
1682 done later in decl_attributes since we are called before attributes
1684 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1685 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1687 /* Copy the assembler name.
1688 Currently, it can only be defined in the prototype. */
1689 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1691 /* Use visibility of whichever declaration had it specified */
1692 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1694 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1695 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1698 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1700 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1701 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1702 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1703 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1704 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1705 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1706 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1707 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1708 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1709 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1712 /* Merge the storage class information. */
1713 merge_weak (newdecl, olddecl);
1715 /* For functions, static overrides non-static. */
1716 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1718 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1719 /* This is since we don't automatically
1720 copy the attributes of NEWDECL into OLDDECL. */
1721 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1722 /* If this clears `static', clear it in the identifier too. */
1723 if (!TREE_PUBLIC (olddecl))
1724 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1726 if (DECL_EXTERNAL (newdecl))
1728 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1729 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1731 /* An extern decl does not override previous storage class. */
1732 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1733 if (!DECL_EXTERNAL (newdecl))
1735 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1736 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1741 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1742 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1745 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1747 /* If we're redefining a function previously defined as extern
1748 inline, make sure we emit debug info for the inline before we
1749 throw it away, in case it was inlined into a function that hasn't
1750 been written out yet. */
1751 if (new_is_definition && DECL_INITIAL (olddecl))
1753 if (TREE_USED (olddecl)
1754 /* In unit-at-a-time mode we never inline re-defined extern
1755 inline functions. */
1756 && !flag_unit_at_a_time
1757 && cgraph_function_possibly_inlined_p (olddecl))
1758 (*debug_hooks->outlining_inline_function) (olddecl);
1760 /* The new defn must not be inline. */
1761 DECL_INLINE (newdecl) = 0;
1762 DECL_UNINLINABLE (newdecl) = 1;
1766 /* If either decl says `inline', this fn is inline,
1767 unless its definition was passed already. */
1768 if (DECL_DECLARED_INLINE_P (newdecl)
1769 || DECL_DECLARED_INLINE_P (olddecl))
1770 DECL_DECLARED_INLINE_P (newdecl) = 1;
1772 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1773 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1776 if (DECL_BUILT_IN (olddecl))
1778 /* If redeclaring a builtin function, it stays built in.
1779 But it gets tagged as having been declared. */
1780 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1781 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1782 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1783 if (new_is_prototype)
1784 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1786 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1787 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1790 /* Also preserve various other info from the definition. */
1791 if (!new_is_definition)
1793 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1794 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1795 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1796 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1797 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1799 /* Set DECL_INLINE on the declaration if we've got a body
1800 from which to instantiate. */
1801 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1803 DECL_INLINE (newdecl) = 1;
1804 DECL_ABSTRACT_ORIGIN (newdecl)
1805 = DECL_ABSTRACT_ORIGIN (olddecl);
1810 /* If a previous declaration said inline, mark the
1811 definition as inlinable. */
1812 if (DECL_DECLARED_INLINE_P (newdecl)
1813 && !DECL_UNINLINABLE (newdecl))
1814 DECL_INLINE (newdecl) = 1;
1818 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1819 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1821 unsigned olddecl_uid = DECL_UID (olddecl);
1822 tree olddecl_context = DECL_CONTEXT (olddecl);
1824 memcpy ((char *) olddecl + sizeof (struct tree_common),
1825 (char *) newdecl + sizeof (struct tree_common),
1826 sizeof (struct tree_decl) - sizeof (struct tree_common));
1827 DECL_UID (olddecl) = olddecl_uid;
1828 DECL_CONTEXT (olddecl) = olddecl_context;
1831 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1832 so that encode_section_info has a chance to look at the new decl
1833 flags and attributes. */
1834 if (DECL_RTL_SET_P (olddecl)
1835 && (TREE_CODE (olddecl) == FUNCTION_DECL
1836 || (TREE_CODE (olddecl) == VAR_DECL
1837 && TREE_STATIC (olddecl))))
1838 make_decl_rtl (olddecl);
1841 /* Handle when a new declaration NEWDECL has the same name as an old
1842 one OLDDECL in the same binding contour. Prints an error message
1845 If safely possible, alter OLDDECL to look like NEWDECL, and return
1846 true. Otherwise, return false. */
1849 duplicate_decls (tree newdecl, tree olddecl)
1851 tree newtype = NULL, oldtype = NULL;
1853 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1856 merge_decls (newdecl, olddecl, newtype, oldtype);
1861 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1863 warn_if_shadowing (tree new_decl)
1865 struct c_binding *b;
1867 /* Shadow warnings wanted? */
1869 /* No shadow warnings for internally generated vars. */
1870 || DECL_IS_BUILTIN (new_decl)
1871 /* No shadow warnings for vars made for inlining. */
1872 || DECL_FROM_INLINE (new_decl)
1873 /* Don't warn about the parm names in function declarator
1874 within a function declarator. It would be nice to avoid
1875 warning in any function declarator in a declaration, as
1876 opposed to a definition, but there is no way to tell
1877 it's not a definition at this point. */
1878 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1881 /* Is anything being shadowed? Invisible decls do not count. */
1882 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1883 if (b->decl && b->decl != new_decl && !b->invisible)
1885 tree old_decl = b->decl;
1887 if (old_decl == error_mark_node)
1889 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1890 new_decl, new_decl);
1893 else if (TREE_CODE (old_decl) == PARM_DECL)
1894 warning (0, "%Jdeclaration of %qD shadows a parameter",
1895 new_decl, new_decl);
1896 else if (DECL_FILE_SCOPE_P (old_decl))
1897 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1898 new_decl, new_decl);
1899 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1900 && DECL_BUILT_IN (old_decl))
1902 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1903 new_decl, new_decl);
1907 warning (0, "%Jdeclaration of %qD shadows a previous local",
1908 new_decl, new_decl);
1910 warning (0, "%Jshadowed declaration is here", old_decl);
1917 /* Subroutine of pushdecl.
1919 X is a TYPE_DECL for a typedef statement. Create a brand new
1920 ..._TYPE node (which will be just a variant of the existing
1921 ..._TYPE node with identical properties) and then install X
1922 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1924 The whole point here is to end up with a situation where each
1925 and every ..._TYPE node the compiler creates will be uniquely
1926 associated with AT MOST one node representing a typedef name.
1927 This way, even though the compiler substitutes corresponding
1928 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1929 early on, later parts of the compiler can always do the reverse
1930 translation and get back the corresponding typedef name. For
1933 typedef struct S MY_TYPE;
1936 Later parts of the compiler might only know that `object' was of
1937 type `struct S' if it were not for code just below. With this
1938 code however, later parts of the compiler see something like:
1940 struct S' == struct S
1941 typedef struct S' MY_TYPE;
1944 And they can then deduce (from the node for type struct S') that
1945 the original object declaration was:
1949 Being able to do this is important for proper support of protoize,
1950 and also for generating precise symbolic debugging information
1951 which takes full account of the programmer's (typedef) vocabulary.
1953 Obviously, we don't want to generate a duplicate ..._TYPE node if
1954 the TYPE_DECL node that we are now processing really represents a
1955 standard built-in type.
1957 Since all standard types are effectively declared at line zero
1958 in the source file, we can easily check to see if we are working
1959 on a standard type by checking the current value of lineno. */
1962 clone_underlying_type (tree x)
1964 if (DECL_IS_BUILTIN (x))
1966 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1967 TYPE_NAME (TREE_TYPE (x)) = x;
1969 else if (TREE_TYPE (x) != error_mark_node
1970 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1972 tree tt = TREE_TYPE (x);
1973 DECL_ORIGINAL_TYPE (x) = tt;
1974 tt = build_variant_type_copy (tt);
1976 TREE_USED (tt) = TREE_USED (x);
1981 /* Record a decl-node X as belonging to the current lexical scope.
1982 Check for errors (such as an incompatible declaration for the same
1983 name already seen in the same scope).
1985 Returns either X or an old decl for the same name.
1986 If an old decl is returned, it may have been smashed
1987 to agree with what X says. */
1992 tree name = DECL_NAME (x);
1993 struct c_scope *scope = current_scope;
1994 struct c_binding *b;
1995 bool nested = false;
1997 /* Functions need the lang_decl data. */
1998 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1999 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2001 /* Must set DECL_CONTEXT for everything not at file scope or
2002 DECL_FILE_SCOPE_P won't work. Local externs don't count
2003 unless they have initializers (which generate code). */
2004 if (current_function_decl
2005 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2006 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2007 DECL_CONTEXT (x) = current_function_decl;
2009 /* If this is of variably modified type, prevent jumping into its
2011 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2012 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2013 c_begin_vm_scope (scope->depth);
2015 /* Anonymous decls are just inserted in the scope. */
2018 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2022 /* First, see if there is another declaration with the same name in
2023 the current scope. If there is, duplicate_decls may do all the
2024 work for us. If duplicate_decls returns false, that indicates
2025 two incompatible decls in the same scope; we are to silently
2026 replace the old one (duplicate_decls has issued all appropriate
2027 diagnostics). In particular, we should not consider possible
2028 duplicates in the external scope, or shadowing. */
2029 b = I_SYMBOL_BINDING (name);
2030 if (b && B_IN_SCOPE (b, scope))
2032 struct c_binding *b_ext, *b_use;
2033 tree type = TREE_TYPE (x);
2034 tree visdecl = b->decl;
2035 tree vistype = TREE_TYPE (visdecl);
2036 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2037 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2038 b->inner_comp = false;
2041 /* If this is an external linkage declaration, we should check
2042 for compatibility with the type in the external scope before
2043 setting the type at this scope based on the visible
2044 information only. */
2045 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2047 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2048 b_ext = b_ext->shadowed;
2053 TREE_TYPE (b_use->decl) = b_use->type;
2056 if (duplicate_decls (x, b_use->decl))
2060 /* Save the updated type in the external scope and
2061 restore the proper type for this scope. */
2063 if (comptypes (vistype, type))
2064 thistype = composite_type (vistype, type);
2066 thistype = TREE_TYPE (b_use->decl);
2067 b_use->type = TREE_TYPE (b_use->decl);
2068 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2069 && DECL_BUILT_IN (b_use->decl))
2071 = build_type_attribute_variant (thistype,
2074 TREE_TYPE (b_use->decl) = thistype;
2079 goto skip_external_and_shadow_checks;
2082 /* All declarations with external linkage, and all external
2083 references, go in the external scope, no matter what scope is
2084 current. However, the binding in that scope is ignored for
2085 purposes of normal name lookup. A separate binding structure is
2086 created in the requested scope; this governs the normal
2087 visibility of the symbol.
2089 The binding in the externals scope is used exclusively for
2090 detecting duplicate declarations of the same object, no matter
2091 what scope they are in; this is what we do here. (C99 6.2.7p2:
2092 All declarations that refer to the same object or function shall
2093 have compatible type; otherwise, the behavior is undefined.) */
2094 if (DECL_EXTERNAL (x) || scope == file_scope)
2096 tree type = TREE_TYPE (x);
2099 bool type_saved = false;
2100 if (b && !B_IN_EXTERNAL_SCOPE (b)
2101 && (TREE_CODE (b->decl) == FUNCTION_DECL
2102 || TREE_CODE (b->decl) == VAR_DECL)
2103 && DECL_FILE_SCOPE_P (b->decl))
2106 vistype = TREE_TYPE (visdecl);
2108 if (warn_nested_externs
2109 && scope != file_scope
2110 && !DECL_IN_SYSTEM_HEADER (x))
2111 warning (0, "nested extern declaration of %qD", x);
2113 while (b && !B_IN_EXTERNAL_SCOPE (b))
2115 /* If this decl might be modified, save its type. This is
2116 done here rather than when the decl is first bound
2117 because the type may change after first binding, through
2118 being completed or through attributes being added. If we
2119 encounter multiple such decls, only the first should have
2120 its type saved; the others will already have had their
2121 proper types saved and the types will not have changed as
2122 their scopes will not have been re-entered. */
2123 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2125 b->type = TREE_TYPE (b->decl);
2128 if (B_IN_FILE_SCOPE (b)
2129 && TREE_CODE (b->decl) == VAR_DECL
2130 && TREE_STATIC (b->decl)
2131 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2132 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2133 && TREE_CODE (type) == ARRAY_TYPE
2134 && TYPE_DOMAIN (type)
2135 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2136 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2138 /* Array type completed in inner scope, which should be
2139 diagnosed if the completion does not have size 1 and
2140 it does not get completed in the file scope. */
2141 b->inner_comp = true;
2146 /* If a matching external declaration has been found, set its
2147 type to the composite of all the types of that declaration.
2148 After the consistency checks, it will be reset to the
2149 composite of the visible types only. */
2150 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2152 TREE_TYPE (b->decl) = b->type;
2154 /* The point of the same_translation_unit_p check here is,
2155 we want to detect a duplicate decl for a construct like
2156 foo() { extern bar(); } ... static bar(); but not if
2157 they are in different translation units. In any case,
2158 the static does not go in the externals scope. */
2160 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2161 && duplicate_decls (x, b->decl))
2166 if (comptypes (vistype, type))
2167 thistype = composite_type (vistype, type);
2169 thistype = TREE_TYPE (b->decl);
2173 b->type = TREE_TYPE (b->decl);
2174 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2176 = build_type_attribute_variant (thistype,
2177 TYPE_ATTRIBUTES (b->type));
2178 TREE_TYPE (b->decl) = thistype;
2179 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2182 else if (TREE_PUBLIC (x))
2184 if (visdecl && !b && duplicate_decls (x, visdecl))
2186 /* An external declaration at block scope referring to a
2187 visible entity with internal linkage. The composite
2188 type will already be correct for this scope, so we
2189 just need to fall through to make the declaration in
2196 bind (name, x, external_scope, /*invisible=*/true,
2203 warn_if_shadowing (x);
2205 skip_external_and_shadow_checks:
2206 if (TREE_CODE (x) == TYPE_DECL)
2207 clone_underlying_type (x);
2209 bind (name, x, scope, /*invisible=*/false, nested);
2211 /* If x's type is incomplete because it's based on a
2212 structure or union which has not yet been fully declared,
2213 attach it to that structure or union type, so we can go
2214 back and complete the variable declaration later, if the
2215 structure or union gets fully declared.
2217 If the input is erroneous, we can have error_mark in the type
2218 slot (e.g. "f(void a, ...)") - that doesn't count as an
2220 if (TREE_TYPE (x) != error_mark_node
2221 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2223 tree element = TREE_TYPE (x);
2225 while (TREE_CODE (element) == ARRAY_TYPE)
2226 element = TREE_TYPE (element);
2227 element = TYPE_MAIN_VARIANT (element);
2229 if ((TREE_CODE (element) == RECORD_TYPE
2230 || TREE_CODE (element) == UNION_TYPE)
2231 && (TREE_CODE (x) != TYPE_DECL
2232 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2233 && !COMPLETE_TYPE_P (element))
2234 C_TYPE_INCOMPLETE_VARS (element)
2235 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2240 /* Record X as belonging to file scope.
2241 This is used only internally by the Objective-C front end,
2242 and is limited to its needs. duplicate_decls is not called;
2243 if there is any preexisting decl for this identifier, it is an ICE. */
2246 pushdecl_top_level (tree x)
2249 bool nested = false;
2250 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2252 name = DECL_NAME (x);
2254 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2256 if (TREE_PUBLIC (x))
2258 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2262 bind (name, x, file_scope, /*invisible=*/false, nested);
2268 implicit_decl_warning (tree id, tree olddecl)
2270 void (*diag) (const char *, ...);
2271 switch (mesg_implicit_function_declaration)
2274 case 1: diag = warning0; break;
2275 case 2: diag = error; break;
2276 default: gcc_unreachable ();
2279 diag (N_("implicit declaration of function %qE"), id);
2281 locate_old_decl (olddecl, diag);
2284 /* Generate an implicit declaration for identifier FUNCTIONID as a
2285 function of type int (). */
2288 implicitly_declare (tree functionid)
2290 struct c_binding *b;
2294 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2296 if (B_IN_SCOPE (b, external_scope))
2305 if (decl == error_mark_node)
2308 /* FIXME: Objective-C has weird not-really-builtin functions
2309 which are supposed to be visible automatically. They wind up
2310 in the external scope because they're pushed before the file
2311 scope gets created. Catch this here and rebind them into the
2313 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2315 bind (functionid, decl, file_scope,
2316 /*invisible=*/false, /*nested=*/true);
2321 tree newtype = default_function_type;
2323 TREE_TYPE (decl) = b->type;
2324 /* Implicit declaration of a function already declared
2325 (somehow) in a different scope, or as a built-in.
2326 If this is the first time this has happened, warn;
2327 then recycle the old declaration but with the new type. */
2328 if (!C_DECL_IMPLICIT (decl))
2330 implicit_decl_warning (functionid, decl);
2331 C_DECL_IMPLICIT (decl) = 1;
2333 if (DECL_BUILT_IN (decl))
2335 newtype = build_type_attribute_variant (newtype,
2337 (TREE_TYPE (decl)));
2338 if (!comptypes (newtype, TREE_TYPE (decl)))
2340 warning (0, "incompatible implicit declaration of built-in"
2341 " function %qD", decl);
2342 newtype = TREE_TYPE (decl);
2347 if (!comptypes (newtype, TREE_TYPE (decl)))
2349 error ("incompatible implicit declaration of function %qD",
2351 locate_old_decl (decl, error);
2354 b->type = TREE_TYPE (decl);
2355 TREE_TYPE (decl) = newtype;
2356 bind (functionid, decl, current_scope,
2357 /*invisible=*/false, /*nested=*/true);
2362 /* Not seen before. */
2363 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2364 DECL_EXTERNAL (decl) = 1;
2365 TREE_PUBLIC (decl) = 1;
2366 C_DECL_IMPLICIT (decl) = 1;
2367 implicit_decl_warning (functionid, 0);
2368 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2370 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2372 /* C89 says implicit declarations are in the innermost block.
2373 So we record the decl in the standard fashion. */
2374 decl = pushdecl (decl);
2376 /* No need to call objc_check_decl here - it's a function type. */
2377 rest_of_decl_compilation (decl, 0, 0);
2379 /* Write a record describing this implicit function declaration
2380 to the prototypes file (if requested). */
2381 gen_aux_info_record (decl, 0, 1, 0);
2383 /* Possibly apply some default attributes to this implicit declaration. */
2384 decl_attributes (&decl, NULL_TREE, 0);
2389 /* Issue an error message for a reference to an undeclared variable
2390 ID, including a reference to a builtin outside of function-call
2391 context. Establish a binding of the identifier to error_mark_node
2392 in an appropriate scope, which will suppress further errors for the
2393 same identifier. The error message should be given location LOC. */
2395 undeclared_variable (tree id, location_t loc)
2397 static bool already = false;
2398 struct c_scope *scope;
2400 if (current_function_decl == 0)
2402 error ("%H%qE undeclared here (not in a function)", &loc, id);
2403 scope = current_scope;
2407 error ("%H%qE undeclared (first use in this function)", &loc, id);
2411 error ("%H(Each undeclared identifier is reported only once", &loc);
2412 error ("%Hfor each function it appears in.)", &loc);
2416 /* If we are parsing old-style parameter decls, current_function_decl
2417 will be nonnull but current_function_scope will be null. */
2418 scope = current_function_scope ? current_function_scope : current_scope;
2420 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2423 /* Subroutine of lookup_label, declare_label, define_label: construct a
2424 LABEL_DECL with all the proper frills. */
2427 make_label (tree name, location_t location)
2429 tree label = build_decl (LABEL_DECL, name, void_type_node);
2431 DECL_CONTEXT (label) = current_function_decl;
2432 DECL_MODE (label) = VOIDmode;
2433 DECL_SOURCE_LOCATION (label) = location;
2438 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2439 Create one if none exists so far for the current function.
2440 This is called when a label is used in a goto expression or
2441 has its address taken. */
2444 lookup_label (tree name)
2448 if (current_function_decl == 0)
2450 error ("label %qE referenced outside of any function", name);
2454 /* Use a label already defined or ref'd with this name, but not if
2455 it is inherited from a containing function and wasn't declared
2457 label = I_LABEL_DECL (name);
2458 if (label && (DECL_CONTEXT (label) == current_function_decl
2459 || C_DECLARED_LABEL_FLAG (label)))
2461 /* If the label has only been declared, update its apparent
2462 location to point here, for better diagnostics if it
2463 turns out not to have been defined. */
2464 if (!TREE_USED (label))
2465 DECL_SOURCE_LOCATION (label) = input_location;
2469 /* No label binding for that identifier; make one. */
2470 label = make_label (name, input_location);
2472 /* Ordinary labels go in the current function scope. */
2473 bind (name, label, current_function_scope,
2474 /*invisible=*/false, /*nested=*/false);
2478 /* Make a label named NAME in the current function, shadowing silently
2479 any that may be inherited from containing functions or containing
2480 scopes. This is called for __label__ declarations. */
2483 declare_label (tree name)
2485 struct c_binding *b = I_LABEL_BINDING (name);
2488 /* Check to make sure that the label hasn't already been declared
2490 if (b && B_IN_CURRENT_SCOPE (b))
2492 error ("duplicate label declaration %qE", name);
2493 locate_old_decl (b->decl, error);
2495 /* Just use the previous declaration. */
2499 label = make_label (name, input_location);
2500 C_DECLARED_LABEL_FLAG (label) = 1;
2502 /* Declared labels go in the current scope. */
2503 bind (name, label, current_scope,
2504 /*invisible=*/false, /*nested=*/false);
2508 /* Define a label, specifying the location in the source file.
2509 Return the LABEL_DECL node for the label, if the definition is valid.
2510 Otherwise return 0. */
2513 define_label (location_t location, tree name)
2515 /* Find any preexisting label with this name. It is an error
2516 if that label has already been defined in this function, or
2517 if there is a containing function with a declared label with
2519 tree label = I_LABEL_DECL (name);
2520 struct c_label_list *nlist_se, *nlist_vm;
2523 && ((DECL_CONTEXT (label) == current_function_decl
2524 && DECL_INITIAL (label) != 0)
2525 || (DECL_CONTEXT (label) != current_function_decl
2526 && C_DECLARED_LABEL_FLAG (label))))
2528 error ("%Hduplicate label %qD", &location, label);
2529 locate_old_decl (label, error);
2532 else if (label && DECL_CONTEXT (label) == current_function_decl)
2534 /* The label has been used or declared already in this function,
2535 but not defined. Update its location to point to this
2537 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2538 error ("%Jjump into statement expression", label);
2539 if (C_DECL_UNDEFINABLE_VM (label))
2540 error ("%Jjump into scope of identifier with variably modified type",
2542 DECL_SOURCE_LOCATION (label) = location;
2546 /* No label binding for that identifier; make one. */
2547 label = make_label (name, location);
2549 /* Ordinary labels go in the current function scope. */
2550 bind (name, label, current_function_scope,
2551 /*invisible=*/false, /*nested=*/false);
2554 if (warn_traditional && !in_system_header && lookup_name (name))
2555 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2556 "identifier %qE conflicts", &location, name);
2558 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2559 nlist_se->next = label_context_stack_se->labels_def;
2560 nlist_se->label = label;
2561 label_context_stack_se->labels_def = nlist_se;
2563 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2564 nlist_vm->next = label_context_stack_vm->labels_def;
2565 nlist_vm->label = label;
2566 label_context_stack_vm->labels_def = nlist_vm;
2568 /* Mark label as having been defined. */
2569 DECL_INITIAL (label) = error_mark_node;
2573 /* Given NAME, an IDENTIFIER_NODE,
2574 return the structure (or union or enum) definition for that name.
2575 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2576 CODE says which kind of type the caller wants;
2577 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2578 If the wrong kind of type is found, an error is reported. */
2581 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2583 struct c_binding *b = I_TAG_BINDING (name);
2589 /* We only care about whether it's in this level if
2590 thislevel_only was set or it might be a type clash. */
2591 if (thislevel_only || TREE_CODE (b->decl) != code)
2593 /* For our purposes, a tag in the external scope is the same as
2594 a tag in the file scope. (Primarily relevant to Objective-C
2595 and its builtin structure tags, which get pushed before the
2596 file scope is created.) */
2597 if (B_IN_CURRENT_SCOPE (b)
2598 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2602 if (thislevel_only && !thislevel)
2605 if (TREE_CODE (b->decl) != code)
2607 /* Definition isn't the kind we were looking for. */
2608 pending_invalid_xref = name;
2609 pending_invalid_xref_location = input_location;
2611 /* If in the same binding level as a declaration as a tag
2612 of a different type, this must not be allowed to
2613 shadow that tag, so give the error immediately.
2614 (For example, "struct foo; union foo;" is invalid.) */
2616 pending_xref_error ();
2621 /* Print an error message now
2622 for a recent invalid struct, union or enum cross reference.
2623 We don't print them immediately because they are not invalid
2624 when used in the `struct foo;' construct for shadowing. */
2627 pending_xref_error (void)
2629 if (pending_invalid_xref != 0)
2630 error ("%H%qE defined as wrong kind of tag",
2631 &pending_invalid_xref_location, pending_invalid_xref);
2632 pending_invalid_xref = 0;
2636 /* Look up NAME in the current scope and its superiors
2637 in the namespace of variables, functions and typedefs.
2638 Return a ..._DECL node of some kind representing its definition,
2639 or return 0 if it is undefined. */
2642 lookup_name (tree name)
2644 struct c_binding *b = I_SYMBOL_BINDING (name);
2645 if (b && !b->invisible)
2650 /* Similar to `lookup_name' but look only at the indicated scope. */
2653 lookup_name_in_scope (tree name, struct c_scope *scope)
2655 struct c_binding *b;
2657 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2658 if (B_IN_SCOPE (b, scope))
2663 /* Create the predefined scalar types of C,
2664 and some nodes representing standard constants (0, 1, (void *) 0).
2665 Initialize the global scope.
2666 Make definitions for built-in primitive functions. */
2669 c_init_decl_processing (void)
2671 location_t save_loc = input_location;
2673 /* Initialize reserved words for parser. */
2676 current_function_decl = 0;
2678 gcc_obstack_init (&parser_obstack);
2680 /* Make the externals scope. */
2682 external_scope = current_scope;
2684 /* Declarations from c_common_nodes_and_builtins must not be associated
2685 with this input file, lest we get differences between using and not
2686 using preprocessed headers. */
2687 #ifdef USE_MAPPED_LOCATION
2688 input_location = BUILTINS_LOCATION;
2690 input_location.file = "<built-in>";
2691 input_location.line = 0;
2694 build_common_tree_nodes (flag_signed_char, false);
2696 c_common_nodes_and_builtins ();
2698 /* In C, comparisons and TRUTH_* expressions have type int. */
2699 truthvalue_type_node = integer_type_node;
2700 truthvalue_true_node = integer_one_node;
2701 truthvalue_false_node = integer_zero_node;
2703 /* Even in C99, which has a real boolean type. */
2704 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2705 boolean_type_node));
2707 input_location = save_loc;
2709 pedantic_lvalues = true;
2711 make_fname_decl = c_make_fname_decl;
2712 start_fname_decls ();
2715 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2716 decl, NAME is the initialization string and TYPE_DEP indicates whether
2717 NAME depended on the type of the function. As we don't yet implement
2718 delayed emission of static data, we mark the decl as emitted
2719 so it is not placed in the output. Anything using it must therefore pull
2720 out the STRING_CST initializer directly. FIXME. */
2723 c_make_fname_decl (tree id, int type_dep)
2725 const char *name = fname_as_string (type_dep);
2726 tree decl, type, init;
2727 size_t length = strlen (name);
2729 type = build_array_type (char_type_node,
2730 build_index_type (size_int (length)));
2731 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2733 decl = build_decl (VAR_DECL, id, type);
2735 TREE_STATIC (decl) = 1;
2736 TREE_READONLY (decl) = 1;
2737 DECL_ARTIFICIAL (decl) = 1;
2739 init = build_string (length + 1, name);
2740 free ((char *) name);
2741 TREE_TYPE (init) = type;
2742 DECL_INITIAL (decl) = init;
2744 TREE_USED (decl) = 1;
2746 if (current_function_decl)
2748 DECL_CONTEXT (decl) = current_function_decl;
2749 bind (id, decl, current_function_scope,
2750 /*invisible=*/false, /*nested=*/false);
2753 finish_decl (decl, init, NULL_TREE);
2758 /* Return a definition for a builtin function named NAME and whose data type
2759 is TYPE. TYPE should be a function type with argument types.
2760 FUNCTION_CODE tells later passes how to compile calls to this function.
2761 See tree.h for its possible values.
2763 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2764 the name to be called if we can't opencode the function. If
2765 ATTRS is nonzero, use that for the function's attribute list. */
2768 builtin_function (const char *name, tree type, int function_code,
2769 enum built_in_class cl, const char *library_name,
2772 tree id = get_identifier (name);
2773 tree decl = build_decl (FUNCTION_DECL, id, type);
2774 TREE_PUBLIC (decl) = 1;
2775 DECL_EXTERNAL (decl) = 1;
2776 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2777 DECL_BUILT_IN_CLASS (decl) = cl;
2778 DECL_FUNCTION_CODE (decl) = function_code;
2779 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2781 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2783 /* Should never be called on a symbol with a preexisting meaning. */
2784 gcc_assert (!I_SYMBOL_BINDING (id));
2786 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2788 /* Builtins in the implementation namespace are made visible without
2789 needing to be explicitly declared. See push_file_scope. */
2790 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2792 TREE_CHAIN (decl) = visible_builtins;
2793 visible_builtins = decl;
2796 /* Possibly apply some default attributes to this built-in function. */
2798 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2800 decl_attributes (&decl, NULL_TREE, 0);
2805 /* Called when a declaration is seen that contains no names to declare.
2806 If its type is a reference to a structure, union or enum inherited
2807 from a containing scope, shadow that tag name for the current scope
2808 with a forward reference.
2809 If its type defines a new named structure or union
2810 or defines an enum, it is valid but we need not do anything here.
2811 Otherwise, it is an error. */
2814 shadow_tag (const struct c_declspecs *declspecs)
2816 shadow_tag_warned (declspecs, 0);
2819 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2822 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2824 bool found_tag = false;
2826 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2828 tree value = declspecs->type;
2829 enum tree_code code = TREE_CODE (value);
2831 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2832 /* Used to test also that TYPE_SIZE (value) != 0.
2833 That caused warning for `struct foo;' at top level in the file. */
2835 tree name = TYPE_NAME (value);
2842 if (warned != 1 && code != ENUMERAL_TYPE)
2843 /* Empty unnamed enum OK */
2845 pedwarn ("unnamed struct/union that defines no instances");
2849 else if (!declspecs->tag_defined_p
2850 && declspecs->storage_class != csc_none)
2853 pedwarn ("empty declaration with storage class specifier "
2854 "does not redeclare tag");
2856 pending_xref_error ();
2858 else if (!declspecs->tag_defined_p
2859 && (declspecs->const_p
2860 || declspecs->volatile_p
2861 || declspecs->restrict_p))
2864 pedwarn ("empty declaration with type qualifier "
2865 "does not redeclare tag");
2867 pending_xref_error ();
2871 pending_invalid_xref = 0;
2872 t = lookup_tag (code, name, 1);
2876 t = make_node (code);
2883 if (warned != 1 && !in_system_header)
2885 pedwarn ("useless type name in empty declaration");
2890 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2892 pedwarn ("useless type name in empty declaration");
2896 pending_invalid_xref = 0;
2898 if (declspecs->inline_p)
2900 error ("%<inline%> in empty declaration");
2904 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2906 error ("%<auto%> in file-scope empty declaration");
2910 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2912 error ("%<register%> in file-scope empty declaration");
2916 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2918 warning (0, "useless storage class specifier in empty declaration");
2922 if (!warned && !in_system_header && declspecs->thread_p)
2924 warning (0, "useless %<__thread%> in empty declaration");
2928 if (!warned && !in_system_header && (declspecs->const_p
2929 || declspecs->volatile_p
2930 || declspecs->restrict_p))
2932 warning (0, "useless type qualifier in empty declaration");
2939 pedwarn ("empty declaration");
2944 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2945 bits. SPECS represents declaration specifiers that the grammar
2946 only permits to contain type qualifiers and attributes. */
2949 quals_from_declspecs (const struct c_declspecs *specs)
2951 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2952 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2953 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2954 gcc_assert (!specs->type
2955 && !specs->decl_attr
2956 && specs->typespec_word == cts_none
2957 && specs->storage_class == csc_none
2958 && !specs->typedef_p
2959 && !specs->explicit_signed_p
2960 && !specs->deprecated_p
2962 && !specs->long_long_p
2965 && !specs->unsigned_p
2966 && !specs->complex_p
2968 && !specs->thread_p);
2972 /* Construct an array declarator. EXPR is the expression inside [], or
2973 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2974 to the pointer to which a parameter array is converted). STATIC_P is
2975 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2976 is true if the array is [*], a VLA of unspecified length which is
2977 nevertheless a complete type (not currently implemented by GCC),
2978 false otherwise. The field for the contained declarator is left to be
2979 filled in by set_array_declarator_inner. */
2981 struct c_declarator *
2982 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2985 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2986 struct c_declarator);
2987 declarator->kind = cdk_array;
2988 declarator->declarator = 0;
2989 declarator->u.array.dimen = expr;
2992 declarator->u.array.attrs = quals->attrs;
2993 declarator->u.array.quals = quals_from_declspecs (quals);
2997 declarator->u.array.attrs = NULL_TREE;
2998 declarator->u.array.quals = 0;
3000 declarator->u.array.static_p = static_p;
3001 declarator->u.array.vla_unspec_p = vla_unspec_p;
3002 if (pedantic && !flag_isoc99)
3004 if (static_p || quals != NULL)
3005 pedwarn ("ISO C90 does not support %<static%> or type "
3006 "qualifiers in parameter array declarators");
3008 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3011 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3015 /* Set the contained declarator of an array declarator. DECL is the
3016 declarator, as constructed by build_array_declarator; INNER is what
3017 appears on the left of the []. ABSTRACT_P is true if it is an
3018 abstract declarator, false otherwise; this is used to reject static
3019 and type qualifiers in abstract declarators, where they are not in
3020 the C99 grammar (subject to possible change in DR#289). */
3022 struct c_declarator *
3023 set_array_declarator_inner (struct c_declarator *decl,
3024 struct c_declarator *inner, bool abstract_p)
3026 decl->declarator = inner;
3027 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3028 || decl->u.array.attrs != NULL_TREE
3029 || decl->u.array.static_p))
3030 error ("static or type qualifiers in abstract declarator");
3034 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3037 groktypename (struct c_type_name *type_name)
3040 tree attrs = type_name->specs->attrs;
3042 type_name->specs->attrs = NULL_TREE;
3044 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3047 /* Apply attributes. */
3048 decl_attributes (&type, attrs, 0);
3053 /* Decode a declarator in an ordinary declaration or data definition.
3054 This is called as soon as the type information and variable name
3055 have been parsed, before parsing the initializer if any.
3056 Here we create the ..._DECL node, fill in its type,
3057 and put it on the list of decls for the current context.
3058 The ..._DECL node is returned as the value.
3060 Exception: for arrays where the length is not specified,
3061 the type is left null, to be filled in by `finish_decl'.
3063 Function definitions do not come here; they go to start_function
3064 instead. However, external and forward declarations of functions
3065 do go through here. Structure field declarations are done by
3066 grokfield and not through here. */
3069 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3070 bool initialized, tree attributes)
3075 /* An object declared as __attribute__((deprecated)) suppresses
3076 warnings of uses of other deprecated items. */
3077 if (lookup_attribute ("deprecated", attributes))
3078 deprecated_state = DEPRECATED_SUPPRESS;
3080 decl = grokdeclarator (declarator, declspecs,
3081 NORMAL, initialized, NULL);
3085 deprecated_state = DEPRECATED_NORMAL;
3087 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3088 && MAIN_NAME_P (DECL_NAME (decl)))
3089 warning (0, "%J%qD is usually a function", decl, decl);
3092 /* Is it valid for this decl to have an initializer at all?
3093 If not, set INITIALIZED to zero, which will indirectly
3094 tell 'finish_decl' to ignore the initializer once it is parsed. */
3095 switch (TREE_CODE (decl))
3098 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3103 error ("function %qD is initialized like a variable", decl);
3108 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3109 error ("parameter %qD is initialized", decl);
3114 /* Don't allow initializations for incomplete types except for
3115 arrays which might be completed by the initialization. */
3117 /* This can happen if the array size is an undefined macro.
3118 We already gave a warning, so we don't need another one. */
3119 if (TREE_TYPE (decl) == error_mark_node)
3121 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3123 /* A complete type is ok if size is fixed. */
3125 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3126 || C_DECL_VARIABLE_SIZE (decl))
3128 error ("variable-sized object may not be initialized");
3132 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3134 error ("variable %qD has initializer but incomplete type", decl);
3137 else if (C_DECL_VARIABLE_SIZE (decl))
3139 /* Although C99 is unclear about whether incomplete arrays
3140 of VLAs themselves count as VLAs, it does not make
3141 sense to permit them to be initialized given that
3142 ordinary VLAs may not be initialized. */
3143 error ("variable-sized object may not be initialized");
3150 if (current_scope == file_scope)
3151 TREE_STATIC (decl) = 1;
3153 /* Tell 'pushdecl' this is an initialized decl
3154 even though we don't yet have the initializer expression.
3155 Also tell 'finish_decl' it may store the real initializer. */
3156 DECL_INITIAL (decl) = error_mark_node;
3159 /* If this is a function declaration, write a record describing it to the
3160 prototypes file (if requested). */
3162 if (TREE_CODE (decl) == FUNCTION_DECL)
3163 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3165 /* ANSI specifies that a tentative definition which is not merged with
3166 a non-tentative definition behaves exactly like a definition with an
3167 initializer equal to zero. (Section 3.7.2)
3169 -fno-common gives strict ANSI behavior, though this tends to break
3170 a large body of code that grew up without this rule.
3172 Thread-local variables are never common, since there's no entrenched
3173 body of code to break, and it allows more efficient variable references
3174 in the presence of dynamic linking. */
3176 if (TREE_CODE (decl) == VAR_DECL
3178 && TREE_PUBLIC (decl)
3179 && !DECL_THREAD_LOCAL (decl)
3181 DECL_COMMON (decl) = 1;
3183 /* Set attributes here so if duplicate decl, will have proper attributes. */
3184 decl_attributes (&decl, attributes, 0);
3186 if (TREE_CODE (decl) == FUNCTION_DECL
3187 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3189 struct c_declarator *ce = declarator;
3191 if (ce->kind == cdk_pointer)
3192 ce = declarator->declarator;
3193 if (ce->kind == cdk_function)
3195 tree args = ce->u.arg_info->parms;
3196 for (; args; args = TREE_CHAIN (args))
3198 tree type = TREE_TYPE (args);
3199 if (type && INTEGRAL_TYPE_P (type)
3200 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3201 DECL_ARG_TYPE (args) = integer_type_node;
3206 if (TREE_CODE (decl) == FUNCTION_DECL
3207 && DECL_DECLARED_INLINE_P (decl)
3208 && DECL_UNINLINABLE (decl)
3209 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3210 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3212 /* Add this decl to the current scope.
3213 TEM may equal DECL or it may be a previous decl of the same name. */
3214 tem = pushdecl (decl);
3216 if (initialized && DECL_EXTERNAL (tem))
3218 DECL_EXTERNAL (tem) = 0;
3219 TREE_STATIC (tem) = 1;
3225 /* Finish processing of a declaration;
3226 install its initial value.
3227 If the length of an array type is not known before,
3228 it must be determined now, from the initial value, or it is an error. */
3231 finish_decl (tree decl, tree init, tree asmspec_tree)
3233 tree type = TREE_TYPE (decl);
3234 int was_incomplete = (DECL_SIZE (decl) == 0);
3235 const char *asmspec = 0;
3237 /* If a name was specified, get the string. */
3238 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3239 && DECL_FILE_SCOPE_P (decl))
3240 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3242 asmspec = TREE_STRING_POINTER (asmspec_tree);
3244 /* If `start_decl' didn't like having an initialization, ignore it now. */
3245 if (init != 0 && DECL_INITIAL (decl) == 0)
3248 /* Don't crash if parm is initialized. */
3249 if (TREE_CODE (decl) == PARM_DECL)
3253 store_init_value (decl, init);
3255 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3256 || TREE_CODE (decl) == FUNCTION_DECL
3257 || TREE_CODE (decl) == FIELD_DECL))
3258 objc_check_decl (decl);
3260 /* Deduce size of array from initialization, if not already known. */
3261 if (TREE_CODE (type) == ARRAY_TYPE
3262 && TYPE_DOMAIN (type) == 0
3263 && TREE_CODE (decl) != TYPE_DECL)
3266 = (TREE_STATIC (decl)
3267 /* Even if pedantic, an external linkage array
3268 may have incomplete type at first. */
3269 ? pedantic && !TREE_PUBLIC (decl)
3270 : !DECL_EXTERNAL (decl));
3272 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3275 /* Get the completed type made by complete_array_type. */
3276 type = TREE_TYPE (decl);
3281 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3286 error ("%Jarray size missing in %qD", decl, decl);
3287 /* If a `static' var's size isn't known,
3288 make it extern as well as static, so it does not get
3290 If it is not `static', then do not mark extern;
3291 finish_incomplete_decl will give it a default size
3292 and it will get allocated. */
3293 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3294 DECL_EXTERNAL (decl) = 1;
3298 error ("%Jzero or negative size array %qD", decl, decl);
3302 /* For global variables, update the copy of the type that
3303 exists in the binding. */
3304 if (TREE_PUBLIC (decl))
3306 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3307 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3308 b_ext = b_ext->shadowed;
3312 b_ext->type = composite_type (b_ext->type, type);
3323 if (DECL_INITIAL (decl))
3324 TREE_TYPE (DECL_INITIAL (decl)) = type;
3326 layout_decl (decl, 0);
3329 if (TREE_CODE (decl) == VAR_DECL)
3331 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3332 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3333 layout_decl (decl, 0);
3335 if (DECL_SIZE (decl) == 0
3336 /* Don't give an error if we already gave one earlier. */
3337 && TREE_TYPE (decl) != error_mark_node
3338 && (TREE_STATIC (decl)
3339 /* A static variable with an incomplete type
3340 is an error if it is initialized.
3341 Also if it is not file scope.
3342 Otherwise, let it through, but if it is not `extern'
3343 then it may cause an error message later. */
3344 ? (DECL_INITIAL (decl) != 0
3345 || !DECL_FILE_SCOPE_P (decl))
3346 /* An automatic variable with an incomplete type
3348 : !DECL_EXTERNAL (decl)))
3350 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3351 TREE_TYPE (decl) = error_mark_node;
3354 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3355 && DECL_SIZE (decl) != 0)
3357 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3358 constant_expression_warning (DECL_SIZE (decl));
3360 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3363 if (TREE_USED (type))
3364 TREE_USED (decl) = 1;
3367 /* If this is a function and an assembler name is specified, reset DECL_RTL
3368 so we can give it its new name. Also, update built_in_decls if it
3369 was a normal built-in. */
3370 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3372 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3373 set_builtin_user_assembler_name (decl, asmspec);
3374 set_user_assembler_name (decl, asmspec);
3377 /* If #pragma weak was used, mark the decl weak now. */
3378 maybe_apply_pragma_weak (decl);
3380 /* If this is a variable definition, determine its ELF visibility. */
3381 if (TREE_CODE (decl) == VAR_DECL
3382 && TREE_STATIC (decl)
3383 && !DECL_EXTERNAL (decl))
3384 c_determine_visibility (decl);
3386 /* Output the assembler code and/or RTL code for variables and functions,
3387 unless the type is an undefined structure or union.
3388 If not, it will get done when the type is completed. */
3390 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3392 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3393 if (c_dialect_objc ())
3394 objc_check_decl (decl);
3398 /* If this is not a static variable, issue a warning.
3399 It doesn't make any sense to give an ASMSPEC for an
3400 ordinary, non-register local variable. Historically,
3401 GCC has accepted -- but ignored -- the ASMSPEC in
3403 if (!DECL_FILE_SCOPE_P (decl)
3404 && TREE_CODE (decl) == VAR_DECL
3405 && !C_DECL_REGISTER (decl)
3406 && !TREE_STATIC (decl))
3407 warning (0, "%Jignoring asm-specifier for non-static local "
3408 "variable %qD", decl, decl);
3409 else if (C_DECL_REGISTER (decl))
3410 change_decl_assembler_name (decl, get_identifier (asmspec));
3412 set_user_assembler_name (decl, asmspec);
3415 if (DECL_FILE_SCOPE_P (decl))
3417 if (DECL_INITIAL (decl) == NULL_TREE
3418 || DECL_INITIAL (decl) == error_mark_node)
3419 /* Don't output anything
3420 when a tentative file-scope definition is seen.
3421 But at end of compilation, do output code for them. */
3422 DECL_DEFER_OUTPUT (decl) = 1;
3423 rest_of_decl_compilation (decl, true, 0);
3427 /* In conjunction with an ASMSPEC, the `register'
3428 keyword indicates that we should place the variable
3429 in a particular register. */
3430 if (asmspec && C_DECL_REGISTER (decl))
3432 DECL_HARD_REGISTER (decl) = 1;
3433 /* This cannot be done for a structure with volatile
3434 fields, on which DECL_REGISTER will have been
3436 if (!DECL_REGISTER (decl))
3437 error ("cannot put object with volatile field into register");
3440 if (TREE_CODE (decl) != FUNCTION_DECL)
3442 /* If we're building a variable sized type, and we might be
3443 reachable other than via the top of the current binding
3444 level, then create a new BIND_EXPR so that we deallocate
3445 the object at the right time. */
3446 /* Note that DECL_SIZE can be null due to errors. */
3447 if (DECL_SIZE (decl)
3448 && !TREE_CONSTANT (DECL_SIZE (decl))
3449 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3452 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3453 TREE_SIDE_EFFECTS (bind) = 1;
3455 BIND_EXPR_BODY (bind) = push_stmt_list ();
3457 add_stmt (build_stmt (DECL_EXPR, decl));
3462 if (!DECL_FILE_SCOPE_P (decl))
3464 /* Recompute the RTL of a local array now
3465 if it used to be an incomplete type. */
3467 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3469 /* If we used it already as memory, it must stay in memory. */
3470 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3471 /* If it's still incomplete now, no init will save it. */
3472 if (DECL_SIZE (decl) == 0)
3473 DECL_INITIAL (decl) = 0;
3478 /* If this was marked 'used', be sure it will be output. */
3479 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3480 mark_decl_referenced (decl);
3482 if (TREE_CODE (decl) == TYPE_DECL)
3484 if (!DECL_FILE_SCOPE_P (decl)
3485 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3486 add_stmt (build_stmt (DECL_EXPR, decl));
3488 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3491 /* At the end of a declaration, throw away any variable type sizes
3492 of types defined inside that declaration. There is no use
3493 computing them in the following function definition. */
3494 if (current_scope == file_scope)
3495 get_pending_sizes ();
3497 /* Install a cleanup (aka destructor) if one was given. */
3498 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3500 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3503 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3504 tree cleanup_decl = lookup_name (cleanup_id);
3507 /* Build "cleanup(&decl)" for the destructor. */
3508 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3509 cleanup = build_tree_list (NULL_TREE, cleanup);
3510 cleanup = build_function_call (cleanup_decl, cleanup);
3512 /* Don't warn about decl unused; the cleanup uses it. */
3513 TREE_USED (decl) = 1;
3514 TREE_USED (cleanup_decl) = 1;
3516 /* Initialize EH, if we've been told to do so. */
3517 if (flag_exceptions && !c_eh_initialized_p)
3519 c_eh_initialized_p = true;
3520 eh_personality_libfunc
3521 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3522 ? "__gcc_personality_sj0"
3523 : "__gcc_personality_v0");
3524 using_eh_for_cleanups ();
3527 push_cleanup (decl, cleanup, false);
3532 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3535 grokparm (const struct c_parm *parm)
3537 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3540 decl_attributes (&decl, parm->attrs, 0);
3545 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3546 and push that on the current scope. */
3549 push_parm_decl (const struct c_parm *parm)
3553 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3554 decl_attributes (&decl, parm->attrs, 0);
3556 decl = pushdecl (decl);
3558 finish_decl (decl, NULL_TREE, NULL_TREE);
3561 /* Mark all the parameter declarations to date as forward decls.
3562 Also diagnose use of this extension. */
3565 mark_forward_parm_decls (void)
3567 struct c_binding *b;
3569 if (pedantic && !current_scope->warned_forward_parm_decls)
3571 pedwarn ("ISO C forbids forward parameter declarations");
3572 current_scope->warned_forward_parm_decls = true;
3575 for (b = current_scope->bindings; b; b = b->prev)
3576 if (TREE_CODE (b->decl) == PARM_DECL)
3577 TREE_ASM_WRITTEN (b->decl) = 1;
3580 static GTY(()) int compound_literal_number;
3582 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3583 literal, which may be an incomplete array type completed by the
3584 initializer; INIT is a CONSTRUCTOR that initializes the compound
3588 build_compound_literal (tree type, tree init)
3590 /* We do not use start_decl here because we have a type, not a declarator;
3591 and do not use finish_decl because the decl should be stored inside
3592 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3597 if (type == error_mark_node)
3598 return error_mark_node;
3600 decl = build_decl (VAR_DECL, NULL_TREE, type);
3601 DECL_EXTERNAL (decl) = 0;
3602 TREE_PUBLIC (decl) = 0;
3603 TREE_STATIC (decl) = (current_scope == file_scope);
3604 DECL_CONTEXT (decl) = current_function_decl;
3605 TREE_USED (decl) = 1;
3606 TREE_TYPE (decl) = type;
3607 TREE_READONLY (decl) = TYPE_READONLY (type);
3608 store_init_value (decl, init);
3610 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3612 int failure = complete_array_type (&TREE_TYPE (decl),
3613 DECL_INITIAL (decl), true);
3614 gcc_assert (!failure);
3616 type = TREE_TYPE (decl);
3617 TREE_TYPE (DECL_INITIAL (decl)) = type;
3620 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3621 return error_mark_node;
3623 stmt = build_stmt (DECL_EXPR, decl);
3624 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3625 TREE_SIDE_EFFECTS (complit) = 1;
3627 layout_decl (decl, 0);
3629 if (TREE_STATIC (decl))
3631 /* This decl needs a name for the assembler output. We also need
3632 a unique suffix to be added to the name. */
3635 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3636 compound_literal_number);
3637 compound_literal_number++;
3638 DECL_NAME (decl) = get_identifier (name);
3639 DECL_DEFER_OUTPUT (decl) = 1;
3640 DECL_COMDAT (decl) = 1;
3641 DECL_ARTIFICIAL (decl) = 1;
3642 DECL_IGNORED_P (decl) = 1;
3644 rest_of_decl_compilation (decl, 1, 0);
3650 /* Determine whether TYPE is a structure with a flexible array member,
3651 or a union containing such a structure (possibly recursively). */
3654 flexible_array_type_p (tree type)
3657 switch (TREE_CODE (type))
3660 x = TYPE_FIELDS (type);
3663 while (TREE_CHAIN (x) != NULL_TREE)
3665 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3666 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3667 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3668 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3672 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3674 if (flexible_array_type_p (TREE_TYPE (x)))
3683 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3684 replacing with appropriate values if they are invalid. */
3686 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3689 unsigned int max_width;
3690 unsigned HOST_WIDE_INT w;
3691 const char *name = orig_name ? orig_name: _("<anonymous>");
3693 /* Detect and ignore out of range field width and process valid
3695 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3696 || TREE_CODE (*width) != INTEGER_CST)
3698 error ("bit-field %qs width not an integer constant", name);
3699 *width = integer_one_node;
3703 constant_expression_warning (*width);
3704 if (tree_int_cst_sgn (*width) < 0)
3706 error ("negative width in bit-field %qs", name);
3707 *width = integer_one_node;
3709 else if (integer_zerop (*width) && orig_name)
3711 error ("zero width for bit-field %qs", name);
3712 *width = integer_one_node;
3716 /* Detect invalid bit-field type. */
3717 if (TREE_CODE (*type) != INTEGER_TYPE
3718 && TREE_CODE (*type) != BOOLEAN_TYPE
3719 && TREE_CODE (*type) != ENUMERAL_TYPE)
3721 error ("bit-field %qs has invalid type", name);
3722 *type = unsigned_type_node;
3725 type_mv = TYPE_MAIN_VARIANT (*type);
3727 && type_mv != integer_type_node
3728 && type_mv != unsigned_type_node
3729 && type_mv != boolean_type_node)
3730 pedwarn ("type of bit-field %qs is a GCC extension", name);
3732 if (type_mv == boolean_type_node)
3733 max_width = CHAR_TYPE_SIZE;
3735 max_width = TYPE_PRECISION (*type);
3737 if (0 < compare_tree_int (*width, max_width))
3739 error ("width of %qs exceeds its type", name);
3741 *width = build_int_cst (NULL_TREE, w);
3744 w = tree_low_cst (*width, 1);
3746 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3748 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3750 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3751 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3752 warning (0, "%qs is narrower than values of its type", name);
3756 /* Given declspecs and a declarator,
3757 determine the name and type of the object declared
3758 and construct a ..._DECL node for it.
3759 (In one case we can return a ..._TYPE node instead.
3760 For invalid input we sometimes return 0.)
3762 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3764 DECL_CONTEXT says which syntactic context this declaration is in:
3765 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3766 FUNCDEF for a function definition. Like NORMAL but a few different
3767 error messages in each case. Return value may be zero meaning
3768 this definition is too screwy to try to parse.
3769 PARM for a parameter declaration (either within a function prototype
3770 or before a function body). Make a PARM_DECL, or return void_type_node.
3771 TYPENAME if for a typename (in a cast or sizeof).
3772 Don't make a DECL node; just return the ..._TYPE node.
3773 FIELD for a struct or union field; make a FIELD_DECL.
3774 INITIALIZED is true if the decl has an initializer.
3775 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3776 representing the width of the bit-field.
3778 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3779 It may also be so in the PARM case, for a prototype where the
3780 argument type is specified but not the name.
3782 This function is where the complicated C meanings of `static'
3783 and `extern' are interpreted. */
3786 grokdeclarator (const struct c_declarator *declarator,
3787 struct c_declspecs *declspecs,
3788 enum decl_context decl_context, bool initialized, tree *width)
3790 tree type = declspecs->type;
3791 bool threadp = declspecs->thread_p;
3792 enum c_storage_class storage_class = declspecs->storage_class;
3796 int type_quals = TYPE_UNQUALIFIED;
3797 const char *name, *orig_name;
3798 tree typedef_type = 0;
3799 int funcdef_flag = 0;
3800 bool funcdef_syntax = false;
3801 int size_varies = 0;
3802 tree decl_attr = declspecs->decl_attr;
3803 int array_ptr_quals = TYPE_UNQUALIFIED;
3804 tree array_ptr_attrs = NULL_TREE;
3805 int array_parm_static = 0;
3806 tree returned_attrs = NULL_TREE;
3807 bool bitfield = width != NULL;
3809 struct c_arg_info *arg_info = 0;
3811 if (decl_context == FUNCDEF)
3812 funcdef_flag = 1, decl_context = NORMAL;
3814 /* Look inside a declarator for the name being declared
3815 and get it as a string, for an error message. */
3817 const struct c_declarator *decl = declarator;
3826 funcdef_syntax = (decl->kind == cdk_function);
3827 decl = decl->declarator;
3831 decl = decl->declarator;
3836 name = IDENTIFIER_POINTER (decl->u.id);
3848 /* A function definition's declarator must have the form of
3849 a function declarator. */
3851 if (funcdef_flag && !funcdef_syntax)
3854 /* If this looks like a function definition, make it one,
3855 even if it occurs where parms are expected.
3856 Then store_parm_decls will reject it and not use it as a parm. */
3857 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3858 decl_context = PARM;
3860 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3861 warn_deprecated_use (declspecs->type);
3863 typedef_type = type;
3864 size_varies = C_TYPE_VARIABLE_SIZE (type);
3866 /* Diagnose defaulting to "int". */
3868 if (declspecs->default_int_p && !in_system_header)
3870 /* Issue a warning if this is an ISO C 99 program or if
3871 -Wreturn-type and this is a function, or if -Wimplicit;
3872 prefer the former warning since it is more explicit. */
3873 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3875 warn_about_return_type = 1;
3876 else if (warn_implicit_int || flag_isoc99)
3877 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3880 /* Adjust the type if a bit-field is being declared,
3881 -funsigned-bitfields applied and the type is not explicitly
3883 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3884 && TREE_CODE (type) == INTEGER_TYPE)
3885 type = c_common_unsigned_type (type);
3887 /* Figure out the type qualifiers for the declaration. There are
3888 two ways a declaration can become qualified. One is something
3889 like `const int i' where the `const' is explicit. Another is
3890 something like `typedef const int CI; CI i' where the type of the
3891 declaration contains the `const'. A third possibility is that
3892 there is a type qualifier on the element type of a typedefed
3893 array type, in which case we should extract that qualifier so
3894 that c_apply_type_quals_to_decls receives the full list of
3895 qualifiers to work with (C90 is not entirely clear about whether
3896 duplicate qualifiers should be diagnosed in this case, but it
3897 seems most appropriate to do so). */
3898 element_type = strip_array_types (type);
3899 constp = declspecs->const_p + TYPE_READONLY (element_type);
3900 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3901 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3902 if (pedantic && !flag_isoc99)
3905 pedwarn ("duplicate %<const%>");
3907 pedwarn ("duplicate %<restrict%>");
3909 pedwarn ("duplicate %<volatile%>");
3911 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3912 type = TYPE_MAIN_VARIANT (type);
3913 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3914 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3915 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3917 /* Warn about storage classes that are invalid for certain
3918 kinds of declarations (parameters, typenames, etc.). */
3922 || storage_class == csc_auto
3923 || storage_class == csc_register
3924 || storage_class == csc_typedef))
3926 if (storage_class == csc_auto
3927 && (pedantic || current_scope == file_scope))
3928 pedwarn ("function definition declared %<auto%>");
3929 if (storage_class == csc_register)
3930 error ("function definition declared %<register%>");
3931 if (storage_class == csc_typedef)
3932 error ("function definition declared %<typedef%>");
3934 error ("function definition declared %<__thread%>");
3936 if (storage_class == csc_auto
3937 || storage_class == csc_register
3938 || storage_class == csc_typedef)
3939 storage_class = csc_none;
3941 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3943 if (decl_context == PARM && storage_class == csc_register)
3947 switch (decl_context)
3950 error ("storage class specified for structure field %qs",
3954 error ("storage class specified for parameter %qs", name);
3957 error ("storage class specified for typename");
3960 storage_class = csc_none;
3964 else if (storage_class == csc_extern
3968 /* 'extern' with initialization is invalid if not at file scope. */
3969 if (current_scope == file_scope)
3970 warning (0, "%qs initialized and declared %<extern%>", name);
3972 error ("%qs has both %<extern%> and initializer", name);
3974 else if (current_scope == file_scope)
3976 if (storage_class == csc_auto)
3977 error ("file-scope declaration of %qs specifies %<auto%>", name);
3978 if (pedantic && storage_class == csc_register)
3979 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3983 if (storage_class == csc_extern && funcdef_flag)
3984 error ("nested function %qs declared %<extern%>", name);
3985 else if (threadp && storage_class == csc_none)
3987 error ("function-scope %qs implicitly auto and declared "
3994 /* Now figure out the structure of the declarator proper.
3995 Descend through it, creating more complex types, until we reach
3996 the declared identifier (or NULL_TREE, in an absolute declarator).
3997 At each stage we maintain an unqualified version of the type
3998 together with any qualifiers that should be applied to it with
3999 c_build_qualified_type; this way, array types including
4000 multidimensional array types are first built up in unqualified
4001 form and then the qualified form is created with
4002 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4004 while (declarator && declarator->kind != cdk_id)
4006 if (type == error_mark_node)
4008 declarator = declarator->declarator;
4012 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4013 a cdk_pointer (for *...),
4014 a cdk_function (for ...(...)),
4015 a cdk_attrs (for nested attributes),
4016 or a cdk_id (for the name being declared
4017 or the place in an absolute declarator
4018 where the name was omitted).
4019 For the last case, we have just exited the loop.
4021 At this point, TYPE is the type of elements of an array,
4022 or for a function to return, or for a pointer to point to.
4023 After this sequence of ifs, TYPE is the type of the
4024 array or function or pointer, and DECLARATOR has had its
4025 outermost layer removed. */
4027 if (array_ptr_quals != TYPE_UNQUALIFIED
4028 || array_ptr_attrs != NULL_TREE
4029 || array_parm_static)
4031 /* Only the innermost declarator (making a parameter be of
4032 array type which is converted to pointer type)
4033 may have static or type qualifiers. */
4034 error ("static or type qualifiers in non-parameter array declarator");
4035 array_ptr_quals = TYPE_UNQUALIFIED;
4036 array_ptr_attrs = NULL_TREE;
4037 array_parm_static = 0;
4040 switch (declarator->kind)
4044 /* A declarator with embedded attributes. */
4045 tree attrs = declarator->u.attrs;
4046 const struct c_declarator *inner_decl;
4048 declarator = declarator->declarator;
4049 inner_decl = declarator;
4050 while (inner_decl->kind == cdk_attrs)
4051 inner_decl = inner_decl->declarator;
4052 if (inner_decl->kind == cdk_id)
4053 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4054 else if (inner_decl->kind == cdk_function)
4055 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4056 else if (inner_decl->kind == cdk_array)
4057 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4058 returned_attrs = decl_attributes (&type,
4059 chainon (returned_attrs, attrs),
4065 tree itype = NULL_TREE;
4066 tree size = declarator->u.array.dimen;
4067 /* The index is a signed object `sizetype' bits wide. */
4068 tree index_type = c_common_signed_type (sizetype);
4070 array_ptr_quals = declarator->u.array.quals;
4071 array_ptr_attrs = declarator->u.array.attrs;
4072 array_parm_static = declarator->u.array.static_p;
4074 declarator = declarator->declarator;
4076 /* Check for some types that there cannot be arrays of. */
4078 if (VOID_TYPE_P (type))
4080 error ("declaration of %qs as array of voids", name);
4081 type = error_mark_node;
4084 if (TREE_CODE (type) == FUNCTION_TYPE)
4086 error ("declaration of %qs as array of functions", name);
4087 type = error_mark_node;
4090 if (pedantic && !in_system_header && flexible_array_type_p (type))
4091 pedwarn ("invalid use of structure with flexible array member");
4093 if (size == error_mark_node)
4094 type = error_mark_node;
4096 if (type == error_mark_node)
4099 /* If size was specified, set ITYPE to a range-type for
4100 that size. Otherwise, ITYPE remains null. finish_decl
4101 may figure it out from an initial value. */
4105 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4107 STRIP_TYPE_NOPS (size);
4109 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4111 error ("size of array %qs has non-integer type", name);
4112 size = integer_one_node;
4115 if (pedantic && integer_zerop (size))
4116 pedwarn ("ISO C forbids zero-size array %qs", name);
4118 if (TREE_CODE (size) == INTEGER_CST)
4120 constant_expression_warning (size);
4121 if (tree_int_cst_sgn (size) < 0)
4123 error ("size of array %qs is negative", name);
4124 size = integer_one_node;
4129 /* Make sure the array size remains visibly
4130 nonconstant even if it is (eg) a const variable
4131 with known value. */
4134 if (!flag_isoc99 && pedantic)
4136 if (TREE_CONSTANT (size))
4137 pedwarn ("ISO C90 forbids array %qs whose size "
4138 "can%'t be evaluated",
4141 pedwarn ("ISO C90 forbids variable-size array %qs",
4146 if (integer_zerop (size))
4148 /* A zero-length array cannot be represented with
4149 an unsigned index type, which is what we'll
4150 get with build_index_type. Create an
4151 open-ended range instead. */
4152 itype = build_range_type (sizetype, size, NULL_TREE);
4156 /* Arrange for the SAVE_EXPR on the inside of the
4157 MINUS_EXPR, which allows the -1 to get folded
4158 with the +1 that happens when building TYPE_SIZE. */
4160 size = variable_size (size);
4162 /* Compute the maximum valid index, that is, size
4163 - 1. Do the calculation in index_type, so that
4164 if it is a variable the computations will be
4165 done in the proper mode. */
4166 itype = fold (build2 (MINUS_EXPR, index_type,
4167 convert (index_type, size),
4168 convert (index_type,
4171 /* If that overflowed, the array is too big. ???
4172 While a size of INT_MAX+1 technically shouldn't
4173 cause an overflow (because we subtract 1), the
4174 overflow is recorded during the conversion to
4175 index_type, before the subtraction. Handling
4176 this case seems like an unnecessary
4178 if (TREE_OVERFLOW (itype))
4180 error ("size of array %qs is too large", name);
4181 type = error_mark_node;
4185 itype = build_index_type (itype);
4188 else if (decl_context == FIELD)
4190 if (pedantic && !flag_isoc99 && !in_system_header)
4191 pedwarn ("ISO C90 does not support flexible array members");
4193 /* ISO C99 Flexible array members are effectively
4194 identical to GCC's zero-length array extension. */
4195 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4198 /* Complain about arrays of incomplete types. */
4199 if (!COMPLETE_TYPE_P (type))
4201 error ("array type has incomplete element type");
4202 type = error_mark_node;
4205 type = build_array_type (type, itype);
4208 C_TYPE_VARIABLE_SIZE (type) = 1;
4210 /* The GCC extension for zero-length arrays differs from
4211 ISO flexible array members in that sizeof yields
4213 if (size && integer_zerop (size))
4215 TYPE_SIZE (type) = bitsize_zero_node;
4216 TYPE_SIZE_UNIT (type) = size_zero_node;
4219 if (decl_context != PARM
4220 && (array_ptr_quals != TYPE_UNQUALIFIED
4221 || array_ptr_attrs != NULL_TREE
4222 || array_parm_static))
4224 error ("static or type qualifiers in non-parameter array declarator");
4225 array_ptr_quals = TYPE_UNQUALIFIED;
4226 array_ptr_attrs = NULL_TREE;
4227 array_parm_static = 0;
4233 /* Say it's a definition only for the declarator closest
4234 to the identifier, apart possibly from some
4236 bool really_funcdef = false;
4240 const struct c_declarator *t = declarator->declarator;
4241 while (t->kind == cdk_attrs)
4243 really_funcdef = (t->kind == cdk_id);
4246 /* Declaring a function type. Make sure we have a valid
4247 type for the function to return. */
4248 if (type == error_mark_node)
4253 /* Warn about some types functions can't return. */
4254 if (TREE_CODE (type) == FUNCTION_TYPE)
4256 error ("%qs declared as function returning a function", name);
4257 type = integer_type_node;
4259 if (TREE_CODE (type) == ARRAY_TYPE)
4261 error ("%qs declared as function returning an array", name);
4262 type = integer_type_node;
4265 /* Construct the function type and go to the next
4266 inner layer of declarator. */
4267 arg_info = declarator->u.arg_info;
4268 arg_types = grokparms (arg_info, really_funcdef);
4270 /* Type qualifiers before the return type of the function
4271 qualify the return type, not the function type. */
4274 /* Type qualifiers on a function return type are
4275 normally permitted by the standard but have no
4276 effect, so give a warning at -Wreturn-type.
4277 Qualifiers on a void return type are banned on
4278 function definitions in ISO C; GCC used to used
4279 them for noreturn functions. */
4280 if (VOID_TYPE_P (type) && really_funcdef)
4281 pedwarn ("function definition has qualified void return type");
4282 else if (warn_return_type)
4283 warning (0, "type qualifiers ignored on function return type");
4285 type = c_build_qualified_type (type, type_quals);
4287 type_quals = TYPE_UNQUALIFIED;
4289 type = build_function_type (type, arg_types);
4290 declarator = declarator->declarator;
4292 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4293 the formal parameter list of this FUNCTION_TYPE to point to
4294 the FUNCTION_TYPE node itself. */
4298 for (link = arg_info->tags;
4300 link = TREE_CHAIN (link))
4301 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4307 /* Merge any constancy or volatility into the target type
4310 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4312 pedwarn ("ISO C forbids qualified function types");
4314 type = c_build_qualified_type (type, type_quals);
4317 type = build_pointer_type (type);
4319 /* Process type qualifiers (such as const or volatile)
4320 that were given inside the `*'. */
4321 type_quals = declarator->u.pointer_quals;
4323 declarator = declarator->declarator;
4331 /* Now TYPE has the actual type, apart from any qualifiers in
4334 /* Check the type and width of a bit-field. */
4336 check_bitfield_type_and_width (&type, width, orig_name);
4338 /* Did array size calculations overflow? */
4340 if (TREE_CODE (type) == ARRAY_TYPE
4341 && COMPLETE_TYPE_P (type)
4342 && TREE_OVERFLOW (TYPE_SIZE (type)))
4344 error ("size of array %qs is too large", name);
4345 /* If we proceed with the array type as it is, we'll eventually
4346 crash in tree_low_cst(). */
4347 type = error_mark_node;
4350 /* If this is declaring a typedef name, return a TYPE_DECL. */
4352 if (storage_class == csc_typedef)
4355 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4357 pedwarn ("ISO C forbids qualified function types");
4359 type = c_build_qualified_type (type, type_quals);
4360 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4361 if (declspecs->explicit_signed_p)
4362 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4363 decl_attributes (&decl, returned_attrs, 0);
4364 if (declspecs->inline_p)
4365 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4369 /* Detect the case of an array type of unspecified size
4370 which came, as such, direct from a typedef name.
4371 We must copy the type, so that each identifier gets
4372 a distinct type, so that each identifier's size can be
4373 controlled separately by its own initializer. */
4375 if (type != 0 && typedef_type != 0
4376 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4377 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4379 type = build_array_type (TREE_TYPE (type), 0);
4381 C_TYPE_VARIABLE_SIZE (type) = 1;
4384 /* If this is a type name (such as, in a cast or sizeof),
4385 compute the type and return it now. */
4387 if (decl_context == TYPENAME)
4389 /* Note that the grammar rejects storage classes in typenames
4391 gcc_assert (storage_class == csc_none && !threadp
4392 && !declspecs->inline_p);
4393 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4395 pedwarn ("ISO C forbids const or volatile function types");
4397 type = c_build_qualified_type (type, type_quals);
4398 decl_attributes (&type, returned_attrs, 0);
4402 /* Aside from typedefs and type names (handle above),
4403 `void' at top level (not within pointer)
4404 is allowed only in public variables.
4405 We don't complain about parms either, but that is because
4406 a better error message can be made later. */
4408 if (VOID_TYPE_P (type) && decl_context != PARM
4409 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4410 && (storage_class == csc_extern
4411 || (current_scope == file_scope
4412 && !(storage_class == csc_static
4413 || storage_class == csc_register)))))
4415 error ("variable or field %qs declared void", name);
4416 type = integer_type_node;
4419 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4420 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4425 if (decl_context == PARM)
4427 tree type_as_written;
4430 /* A parameter declared as an array of T is really a pointer to T.
4431 One declared as a function is really a pointer to a function. */
4433 if (TREE_CODE (type) == ARRAY_TYPE)
4435 /* Transfer const-ness of array into that of type pointed to. */
4436 type = TREE_TYPE (type);
4438 type = c_build_qualified_type (type, type_quals);
4439 type = build_pointer_type (type);
4440 type_quals = array_ptr_quals;
4442 /* We don't yet implement attributes in this context. */
4443 if (array_ptr_attrs != NULL_TREE)
4444 warning (0, "attributes in parameter array declarator ignored");
4448 else if (TREE_CODE (type) == FUNCTION_TYPE)
4450 if (pedantic && type_quals)
4451 pedwarn ("ISO C forbids qualified function types");
4453 type = c_build_qualified_type (type, type_quals);
4454 type = build_pointer_type (type);
4455 type_quals = TYPE_UNQUALIFIED;
4457 else if (type_quals)
4458 type = c_build_qualified_type (type, type_quals);
4460 type_as_written = type;
4462 decl = build_decl (PARM_DECL, declarator->u.id, type);
4464 C_DECL_VARIABLE_SIZE (decl) = 1;
4466 /* Compute the type actually passed in the parmlist,
4467 for the case where there is no prototype.
4468 (For example, shorts and chars are passed as ints.)
4469 When there is a prototype, this is overridden later. */
4471 if (type == error_mark_node)
4472 promoted_type = type;
4474 promoted_type = c_type_promotes_to (type);
4476 DECL_ARG_TYPE (decl) = promoted_type;
4477 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4478 if (declspecs->inline_p)
4479 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4481 else if (decl_context == FIELD)
4483 /* Note that the grammar rejects storage classes in typenames
4485 gcc_assert (storage_class == csc_none && !threadp
4486 && !declspecs->inline_p);
4488 /* Structure field. It may not be a function. */
4490 if (TREE_CODE (type) == FUNCTION_TYPE)
4492 error ("field %qs declared as a function", name);
4493 type = build_pointer_type (type);
4495 else if (TREE_CODE (type) != ERROR_MARK
4496 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4498 error ("field %qs has incomplete type", name);
4499 type = error_mark_node;
4501 type = c_build_qualified_type (type, type_quals);
4502 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4503 DECL_NONADDRESSABLE_P (decl) = bitfield;
4506 C_DECL_VARIABLE_SIZE (decl) = 1;
4508 else if (TREE_CODE (type) == FUNCTION_TYPE)
4510 if (storage_class == csc_register || threadp)
4512 error ("invalid storage class for function %qs", name);
4514 else if (current_scope != file_scope)
4516 /* Function declaration not at file scope. Storage
4517 classes other than `extern' are not allowed, C99
4518 6.7.1p5, and `extern' makes no difference. However,
4519 GCC allows 'auto', perhaps with 'inline', to support
4520 nested functions. */
4521 if (storage_class == csc_auto)
4524 pedwarn ("invalid storage class for function %qs", name);
4526 else if (storage_class == csc_static)
4528 error ("invalid storage class for function %qs", name);
4530 storage_class = declspecs->storage_class = csc_none;
4536 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4537 decl = build_decl_attribute_variant (decl, decl_attr);
4539 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4541 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4542 pedwarn ("ISO C forbids qualified function types");
4544 /* GNU C interprets a volatile-qualified function type to indicate
4545 that the function does not return. */
4546 if ((type_quals & TYPE_QUAL_VOLATILE)
4547 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4548 warning (0, "%<noreturn%> function returns non-void value");
4550 /* Every function declaration is an external reference
4551 (DECL_EXTERNAL) except for those which are not at file
4552 scope and are explicitly declared "auto". This is
4553 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4554 GCC to signify a forward declaration of a nested function. */
4555 if (storage_class == csc_auto && current_scope != file_scope)
4556 DECL_EXTERNAL (decl) = 0;
4558 DECL_EXTERNAL (decl) = 1;
4560 /* Record absence of global scope for `static' or `auto'. */
4562 = !(storage_class == csc_static || storage_class == csc_auto);
4564 /* For a function definition, record the argument information
4565 block where store_parm_decls will look for it. */
4567 current_function_arg_info = arg_info;
4569 if (declspecs->default_int_p)
4570 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4572 /* Record presence of `inline', if it is reasonable. */
4573 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4575 if (declspecs->inline_p)
4576 pedwarn ("cannot inline function %<main%>");
4578 else if (declspecs->inline_p)
4580 /* Record that the function is declared `inline'. */
4581 DECL_DECLARED_INLINE_P (decl) = 1;
4583 /* Do not mark bare declarations as DECL_INLINE. Doing so
4584 in the presence of multiple declarations can result in
4585 the abstract origin pointing between the declarations,
4586 which will confuse dwarf2out. */
4589 DECL_INLINE (decl) = 1;
4590 if (storage_class == csc_extern)
4591 current_extern_inline = 1;
4594 /* If -finline-functions, assume it can be inlined. This does
4595 two things: let the function be deferred until it is actually
4596 needed, and let dwarf2 know that the function is inlinable. */
4597 else if (flag_inline_trees == 2 && initialized)
4598 DECL_INLINE (decl) = 1;
4602 /* It's a variable. */
4603 /* An uninitialized decl with `extern' is a reference. */
4604 int extern_ref = !initialized && storage_class == csc_extern;
4606 type = c_build_qualified_type (type, type_quals);
4608 /* C99 6.2.2p7: It is invalid (compile-time undefined
4609 behavior) to create an 'extern' declaration for a
4610 variable if there is a global declaration that is
4611 'static' and the global declaration is not visible.
4612 (If the static declaration _is_ currently visible,
4613 the 'extern' declaration is taken to refer to that decl.) */
4614 if (extern_ref && current_scope != file_scope)
4616 tree global_decl = identifier_global_value (declarator->u.id);
4617 tree visible_decl = lookup_name (declarator->u.id);
4620 && global_decl != visible_decl
4621 && TREE_CODE (global_decl) == VAR_DECL
4622 && !TREE_PUBLIC (global_decl))
4623 error ("variable previously declared %<static%> redeclared "
4627 decl = build_decl (VAR_DECL, declarator->u.id, type);
4628 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4630 C_DECL_VARIABLE_SIZE (decl) = 1;
4632 if (declspecs->inline_p)
4633 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4635 /* At file scope, an initialized extern declaration may follow
4636 a static declaration. In that case, DECL_EXTERNAL will be
4637 reset later in start_decl. */
4638 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4640 /* At file scope, the presence of a `static' or `register' storage
4641 class specifier, or the absence of all storage class specifiers
4642 makes this declaration a definition (perhaps tentative). Also,
4643 the absence of both `static' and `register' makes it public. */
4644 if (current_scope == file_scope)
4646 TREE_PUBLIC (decl) = !(storage_class == csc_static
4647 || storage_class == csc_register);
4648 TREE_STATIC (decl) = !extern_ref;
4650 /* Not at file scope, only `static' makes a static definition. */
4653 TREE_STATIC (decl) = (storage_class == csc_static);
4654 TREE_PUBLIC (decl) = extern_ref;
4659 if (targetm.have_tls)
4660 DECL_THREAD_LOCAL (decl) = 1;
4662 /* A mere warning is sure to result in improper semantics
4663 at runtime. Don't bother to allow this to compile. */
4664 error ("thread-local storage not supported for this target");
4668 /* Record `register' declaration for warnings on &
4669 and in case doing stupid register allocation. */
4671 if (storage_class == csc_register)
4673 C_DECL_REGISTER (decl) = 1;
4674 DECL_REGISTER (decl) = 1;
4677 /* Record constancy and volatility. */
4678 c_apply_type_quals_to_decl (type_quals, decl);
4680 /* If a type has volatile components, it should be stored in memory.
4681 Otherwise, the fact that those components are volatile
4682 will be ignored, and would even crash the compiler. */
4683 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4685 /* It is not an error for a structure with volatile fields to
4686 be declared register, but reset DECL_REGISTER since it
4687 cannot actually go in a register. */
4688 int was_reg = C_DECL_REGISTER (decl);
4689 C_DECL_REGISTER (decl) = 0;
4690 DECL_REGISTER (decl) = 0;
4691 c_mark_addressable (decl);
4692 C_DECL_REGISTER (decl) = was_reg;
4695 /* This is the earliest point at which we might know the assembler
4696 name of a variable. Thus, if it's known before this, die horribly. */
4697 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4699 decl_attributes (&decl, returned_attrs, 0);
4705 /* Decode the parameter-list info for a function type or function definition.
4706 The argument is the value returned by `get_parm_info' (or made in parse.y
4707 if there is an identifier list instead of a parameter decl list).
4708 These two functions are separate because when a function returns
4709 or receives functions then each is called multiple times but the order
4710 of calls is different. The last call to `grokparms' is always the one
4711 that contains the formal parameter names of a function definition.
4713 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4715 FUNCDEF_FLAG is true for a function definition, false for
4716 a mere declaration. A nonempty identifier-list gets an error message
4717 when FUNCDEF_FLAG is false. */
4720 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4722 tree arg_types = arg_info->types;
4724 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4725 && !in_system_header)
4726 warning (0, "function declaration isn%'t a prototype");
4728 if (arg_types == error_mark_node)
4729 return 0; /* don't set TYPE_ARG_TYPES in this case */
4731 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4734 pedwarn ("parameter names (without types) in function declaration");
4736 arg_info->parms = arg_info->types;
4737 arg_info->types = 0;
4742 tree parm, type, typelt;
4743 unsigned int parmno;
4745 /* If there is a parameter of incomplete type in a definition,
4746 this is an error. In a declaration this is valid, and a
4747 struct or union type may be completed later, before any calls
4748 or definition of the function. In the case where the tag was
4749 first declared within the parameter list, a warning has
4750 already been given. If a parameter has void type, then
4751 however the function cannot be defined or called, so
4754 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4756 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4758 type = TREE_VALUE (typelt);
4759 if (type == error_mark_node)
4762 if (!COMPLETE_TYPE_P (type))
4766 if (DECL_NAME (parm))
4767 error ("%Jparameter %u (%qD) has incomplete type",
4768 parm, parmno, parm);
4770 error ("%Jparameter %u has incomplete type",
4773 TREE_VALUE (typelt) = error_mark_node;
4774 TREE_TYPE (parm) = error_mark_node;
4776 else if (VOID_TYPE_P (type))
4778 if (DECL_NAME (parm))
4779 warning (0, "%Jparameter %u (%qD) has void type",
4780 parm, parmno, parm);
4782 warning (0, "%Jparameter %u has void type",
4791 /* Take apart the current scope and return a c_arg_info structure with
4792 info on a parameter list just parsed.
4794 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4796 ELLIPSIS being true means the argument list ended in '...' so don't
4797 append a sentinel (void_list_node) to the end of the type-list. */
4800 get_parm_info (bool ellipsis)
4802 struct c_binding *b = current_scope->bindings;
4803 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4810 static bool explained_incomplete_types = false;
4811 bool gave_void_only_once_err = false;
4813 arg_info->parms = 0;
4815 arg_info->types = 0;
4816 arg_info->others = 0;
4818 /* The bindings in this scope must not get put into a block.
4819 We will take care of deleting the binding nodes. */
4820 current_scope->bindings = 0;
4822 /* This function is only called if there was *something* on the
4826 /* A parameter list consisting solely of 'void' indicates that the
4827 function takes no arguments. But if the 'void' is qualified
4828 (by 'const' or 'volatile'), or has a storage class specifier
4829 ('register'), then the behavior is undefined; issue an error.
4830 Typedefs for 'void' are OK (see DR#157). */
4831 if (b->prev == 0 /* one binding */
4832 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4833 && !DECL_NAME (b->decl) /* anonymous */
4834 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4836 if (TREE_THIS_VOLATILE (b->decl)
4837 || TREE_READONLY (b->decl)
4838 || C_DECL_REGISTER (b->decl))
4839 error ("%<void%> as only parameter may not be qualified");
4841 /* There cannot be an ellipsis. */
4843 error ("%<void%> must be the only parameter");
4845 arg_info->types = void_list_node;
4850 types = void_list_node;
4852 /* Break up the bindings list into parms, tags, types, and others;
4853 apply sanity checks; purge the name-to-decl bindings. */
4856 tree decl = b->decl;
4857 tree type = TREE_TYPE (decl);
4858 const char *keyword;
4860 switch (TREE_CODE (decl))
4865 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4866 I_SYMBOL_BINDING (b->id) = b->shadowed;
4869 /* Check for forward decls that never got their actual decl. */
4870 if (TREE_ASM_WRITTEN (decl))
4871 error ("%Jparameter %qD has just a forward declaration",
4873 /* Check for (..., void, ...) and issue an error. */
4874 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4876 if (!gave_void_only_once_err)
4878 error ("%<void%> must be the only parameter");
4879 gave_void_only_once_err = true;
4884 /* Valid parameter, add it to the list. */
4885 TREE_CHAIN (decl) = parms;
4888 /* Since there is a prototype, args are passed in their
4889 declared types. The back end may override this later. */
4890 DECL_ARG_TYPE (decl) = type;
4891 types = tree_cons (0, type, types);
4895 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4896 case UNION_TYPE: keyword = "union"; goto tag;
4897 case RECORD_TYPE: keyword = "struct"; goto tag;
4899 /* Types may not have tag-names, in which case the type
4900 appears in the bindings list with b->id NULL. */
4903 gcc_assert (I_TAG_BINDING (b->id) == b);
4904 I_TAG_BINDING (b->id) = b->shadowed;
4907 /* Warn about any struct, union or enum tags defined in a
4908 parameter list. The scope of such types is limited to
4909 the parameter list, which is rarely if ever desirable
4910 (it's impossible to call such a function with type-
4911 correct arguments). An anonymous union parm type is
4912 meaningful as a GNU extension, so don't warn for that. */
4913 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4916 /* The %s will be one of 'struct', 'union', or 'enum'. */
4917 warning (0, "%<%s %E%> declared inside parameter list",
4920 /* The %s will be one of 'struct', 'union', or 'enum'. */
4921 warning (0, "anonymous %s declared inside parameter list",
4924 if (!explained_incomplete_types)
4926 warning (0, "its scope is only this definition or declaration,"
4927 " which is probably not what you want");
4928 explained_incomplete_types = true;
4932 tags = tree_cons (b->id, decl, tags);
4938 /* CONST_DECLs appear here when we have an embedded enum,
4939 and TYPE_DECLs appear here when we have an embedded struct
4940 or union. No warnings for this - we already warned about the
4941 type itself. FUNCTION_DECLs appear when there is an implicit
4942 function declaration in the parameter list. */
4944 TREE_CHAIN (decl) = others;
4949 /* error_mark_node appears here when we have an undeclared
4950 variable. Just throw it away. */
4953 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4954 I_SYMBOL_BINDING (b->id) = b->shadowed;
4958 /* Other things that might be encountered. */
4965 b = free_binding_and_advance (b);
4968 arg_info->parms = parms;
4969 arg_info->tags = tags;
4970 arg_info->types = types;
4971 arg_info->others = others;
4975 /* Get the struct, enum or union (CODE says which) with tag NAME.
4976 Define the tag as a forward-reference if it is not defined.
4977 Return a c_typespec structure for the type specifier. */
4980 parser_xref_tag (enum tree_code code, tree name)
4982 struct c_typespec ret;
4983 /* If a cross reference is requested, look up the type
4984 already defined for this tag and return it. */
4986 tree ref = lookup_tag (code, name, 0);
4987 /* If this is the right type of tag, return what we found.
4988 (This reference will be shadowed by shadow_tag later if appropriate.)
4989 If this is the wrong type of tag, do not return it. If it was the
4990 wrong type in the same scope, we will have had an error
4991 message already; if in a different scope and declaring
4992 a name, pending_xref_error will give an error message; but if in a
4993 different scope and not declaring a name, this tag should
4994 shadow the previous declaration of a different type of tag, and
4995 this would not work properly if we return the reference found.
4996 (For example, with "struct foo" in an outer scope, "union foo;"
4997 must shadow that tag with a new one of union type.) */
4998 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4999 if (ref && TREE_CODE (ref) == code)
5005 /* If no such tag is yet defined, create a forward-reference node
5006 and record it as the "definition".
5007 When a real declaration of this type is found,
5008 the forward-reference will be altered into a real type. */
5010 ref = make_node (code);
5011 if (code == ENUMERAL_TYPE)
5013 /* Give the type a default layout like unsigned int
5014 to avoid crashing if it does not get defined. */
5015 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5016 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5017 TYPE_USER_ALIGN (ref) = 0;
5018 TYPE_UNSIGNED (ref) = 1;
5019 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5020 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5021 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5024 pushtag (name, ref);
5030 /* Get the struct, enum or union (CODE says which) with tag NAME.
5031 Define the tag as a forward-reference if it is not defined.
5032 Return a tree for the type. */
5035 xref_tag (enum tree_code code, tree name)
5037 return parser_xref_tag (code, name).spec;
5040 /* Make sure that the tag NAME is defined *in the current scope*
5041 at least as a forward reference.
5042 CODE says which kind of tag NAME ought to be. */
5045 start_struct (enum tree_code code, tree name)
5047 /* If there is already a tag defined at this scope
5048 (as a forward reference), just return it. */
5053 ref = lookup_tag (code, name, 1);
5054 if (ref && TREE_CODE (ref) == code)
5056 if (TYPE_SIZE (ref))
5058 if (code == UNION_TYPE)
5059 error ("redefinition of %<union %E%>", name);
5061 error ("redefinition of %<struct %E%>", name);
5063 else if (C_TYPE_BEING_DEFINED (ref))
5065 if (code == UNION_TYPE)
5066 error ("nested redefinition of %<union %E%>", name);
5068 error ("nested redefinition of %<struct %E%>", name);
5073 /* Otherwise create a forward-reference just so the tag is in scope. */
5075 ref = make_node (code);
5076 pushtag (name, ref);
5079 C_TYPE_BEING_DEFINED (ref) = 1;
5080 TYPE_PACKED (ref) = flag_pack_struct;
5084 /* Process the specs, declarator and width (NULL if omitted)
5085 of a structure component, returning a FIELD_DECL node.
5086 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5088 This is done during the parsing of the struct declaration.
5089 The FIELD_DECL nodes are chained together and the lot of them
5090 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5093 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5098 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5099 && width == NULL_TREE)
5101 /* This is an unnamed decl.
5103 If we have something of the form "union { list } ;" then this
5104 is the anonymous union extension. Similarly for struct.
5106 If this is something of the form "struct foo;", then
5107 If MS extensions are enabled, this is handled as an
5109 Otherwise this is a forward declaration of a structure tag.
5111 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5112 If MS extensions are enabled and foo names a structure, then
5113 again this is an anonymous struct.
5114 Otherwise this is an error.
5116 Oh what a horrid tangled web we weave. I wonder if MS consciously
5117 took this from Plan 9 or if it was an accident of implementation
5118 that took root before someone noticed the bug... */
5120 tree type = declspecs->type;
5121 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5122 || TREE_CODE (type) == UNION_TYPE);
5126 && (flag_ms_extensions || !declspecs->typedef_p))
5128 if (flag_ms_extensions)
5132 else if (TYPE_NAME (type) == NULL)
5139 pedwarn ("declaration does not declare anything");
5143 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5146 value = grokdeclarator (declarator, declspecs, FIELD, false,
5147 width ? &width : NULL);
5149 finish_decl (value, NULL_TREE, NULL_TREE);
5150 DECL_INITIAL (value) = width;
5155 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5156 the list such that this does not present a problem later. */
5159 detect_field_duplicates (tree fieldlist)
5164 /* First, see if there are more than "a few" fields.
5165 This is trivially true if there are zero or one fields. */
5168 x = TREE_CHAIN (fieldlist);
5174 } while (timeout > 0 && x);
5176 /* If there were "few" fields, avoid the overhead of allocating
5177 a hash table. Instead just do the nested traversal thing. */
5180 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5183 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5184 if (DECL_NAME (y) == DECL_NAME (x))
5186 error ("%Jduplicate member %qD", x, x);
5187 DECL_NAME (x) = NULL_TREE;
5193 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5196 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5197 if ((y = DECL_NAME (x)) != 0)
5199 slot = htab_find_slot (htab, y, INSERT);
5202 error ("%Jduplicate member %qD", x, x);
5203 DECL_NAME (x) = NULL_TREE;
5212 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5213 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5214 ATTRIBUTES are attributes to be applied to the structure. */
5217 finish_struct (tree t, tree fieldlist, tree attributes)
5220 bool toplevel = file_scope == current_scope;
5221 int saw_named_field;
5223 /* If this type was previously laid out as a forward reference,
5224 make sure we lay it out again. */
5228 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5232 for (x = fieldlist; x; x = TREE_CHAIN (x))
5233 if (DECL_NAME (x) != 0)
5238 if (TREE_CODE (t) == UNION_TYPE)
5241 pedwarn ("union has no named members");
5243 pedwarn ("union has no members");
5248 pedwarn ("struct has no named members");
5250 pedwarn ("struct has no members");
5255 /* Install struct as DECL_CONTEXT of each field decl.
5256 Also process specified field sizes, found in the DECL_INITIAL,
5257 storing 0 there after the type has been changed to precision equal
5258 to its width, rather than the precision of the specified standard
5259 type. (Correct layout requires the original type to have been preserved
5262 saw_named_field = 0;
5263 for (x = fieldlist; x; x = TREE_CHAIN (x))
5265 DECL_CONTEXT (x) = t;
5266 DECL_PACKED (x) |= TYPE_PACKED (t);
5268 /* If any field is const, the structure type is pseudo-const. */
5269 if (TREE_READONLY (x))
5270 C_TYPE_FIELDS_READONLY (t) = 1;
5273 /* A field that is pseudo-const makes the structure likewise. */
5274 tree t1 = TREE_TYPE (x);
5275 while (TREE_CODE (t1) == ARRAY_TYPE)
5276 t1 = TREE_TYPE (t1);
5277 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5278 && C_TYPE_FIELDS_READONLY (t1))
5279 C_TYPE_FIELDS_READONLY (t) = 1;
5282 /* Any field that is volatile means variables of this type must be
5283 treated in some ways as volatile. */
5284 if (TREE_THIS_VOLATILE (x))
5285 C_TYPE_FIELDS_VOLATILE (t) = 1;
5287 /* Any field of nominal variable size implies structure is too. */
5288 if (C_DECL_VARIABLE_SIZE (x))
5289 C_TYPE_VARIABLE_SIZE (t) = 1;
5291 if (DECL_INITIAL (x))
5293 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5294 DECL_SIZE (x) = bitsize_int (width);
5295 DECL_BIT_FIELD (x) = 1;
5296 SET_DECL_C_BIT_FIELD (x);
5299 /* Detect flexible array member in an invalid context. */
5300 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5301 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5302 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5303 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5305 if (TREE_CODE (t) == UNION_TYPE)
5307 error ("%Jflexible array member in union", x);
5308 TREE_TYPE (x) = error_mark_node;
5310 else if (TREE_CHAIN (x) != NULL_TREE)
5312 error ("%Jflexible array member not at end of struct", x);
5313 TREE_TYPE (x) = error_mark_node;
5315 else if (!saw_named_field)
5317 error ("%Jflexible array member in otherwise empty struct", x);
5318 TREE_TYPE (x) = error_mark_node;
5322 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5323 && flexible_array_type_p (TREE_TYPE (x)))
5324 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5327 saw_named_field = 1;
5330 detect_field_duplicates (fieldlist);
5332 /* Now we have the nearly final fieldlist. Record it,
5333 then lay out the structure or union (including the fields). */
5335 TYPE_FIELDS (t) = fieldlist;
5339 /* Give bit-fields their proper types. */
5341 tree *fieldlistp = &fieldlist;
5343 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5344 && TREE_TYPE (*fieldlistp) != error_mark_node)
5346 unsigned HOST_WIDE_INT width
5347 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5348 tree type = TREE_TYPE (*fieldlistp);
5349 if (width != TYPE_PRECISION (type))
5351 TREE_TYPE (*fieldlistp)
5352 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5353 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5355 DECL_INITIAL (*fieldlistp) = 0;
5358 fieldlistp = &TREE_CHAIN (*fieldlistp);
5361 /* Now we have the truly final field list.
5362 Store it in this type and in the variants. */
5364 TYPE_FIELDS (t) = fieldlist;
5366 /* If there are lots of fields, sort so we can look through them fast.
5367 We arbitrarily consider 16 or more elts to be "a lot". */
5372 for (x = fieldlist; x; x = TREE_CHAIN (x))
5374 if (len > 15 || DECL_NAME (x) == NULL)
5382 struct lang_type *space;
5383 struct sorted_fields_type *space2;
5385 len += list_length (x);
5387 /* Use the same allocation policy here that make_node uses, to
5388 ensure that this lives as long as the rest of the struct decl.
5389 All decls in an inline function need to be saved. */
5391 space = GGC_CNEW (struct lang_type);
5392 space2 = GGC_NEWVAR (struct sorted_fields_type,
5393 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5397 field_array = &space2->elts[0];
5398 for (x = fieldlist; x; x = TREE_CHAIN (x))
5400 field_array[len++] = x;
5402 /* If there is anonymous struct or union, break out of the loop. */
5403 if (DECL_NAME (x) == NULL)
5406 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5409 TYPE_LANG_SPECIFIC (t) = space;
5410 TYPE_LANG_SPECIFIC (t)->s->len = len;
5411 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5412 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5417 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5419 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5420 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5421 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5422 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5425 /* If this was supposed to be a transparent union, but we can't
5426 make it one, warn and turn off the flag. */
5427 if (TREE_CODE (t) == UNION_TYPE
5428 && TYPE_TRANSPARENT_UNION (t)
5429 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5431 TYPE_TRANSPARENT_UNION (t) = 0;
5432 warning (0, "union cannot be made transparent");
5435 /* If this structure or union completes the type of any previous
5436 variable declaration, lay it out and output its rtl. */
5437 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5441 tree decl = TREE_VALUE (x);
5442 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5443 layout_array_type (TREE_TYPE (decl));
5444 if (TREE_CODE (decl) != TYPE_DECL)
5446 layout_decl (decl, 0);
5447 if (c_dialect_objc ())
5448 objc_check_decl (decl);
5449 rest_of_decl_compilation (decl, toplevel, 0);
5454 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5456 /* Finish debugging output for this type. */
5457 rest_of_type_compilation (t, toplevel);
5459 /* If we're inside a function proper, i.e. not file-scope and not still
5460 parsing parameters, then arrange for the size of a variable sized type
5462 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5463 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5468 /* Lay out the type T, and its element type, and so on. */
5471 layout_array_type (tree t)
5473 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5474 layout_array_type (TREE_TYPE (t));
5478 /* Begin compiling the definition of an enumeration type.
5479 NAME is its name (or null if anonymous).
5480 Returns the type object, as yet incomplete.
5481 Also records info about it so that build_enumerator
5482 may be used to declare the individual values as they are read. */
5485 start_enum (tree name)
5489 /* If this is the real definition for a previous forward reference,
5490 fill in the contents in the same object that used to be the
5491 forward reference. */
5494 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5496 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5498 enumtype = make_node (ENUMERAL_TYPE);
5499 pushtag (name, enumtype);
5502 if (C_TYPE_BEING_DEFINED (enumtype))
5503 error ("nested redefinition of %<enum %E%>", name);
5505 C_TYPE_BEING_DEFINED (enumtype) = 1;
5507 if (TYPE_VALUES (enumtype) != 0)
5509 /* This enum is a named one that has been declared already. */
5510 error ("redeclaration of %<enum %E%>", name);
5512 /* Completely replace its old definition.
5513 The old enumerators remain defined, however. */
5514 TYPE_VALUES (enumtype) = 0;
5517 enum_next_value = integer_zero_node;
5520 if (flag_short_enums)
5521 TYPE_PACKED (enumtype) = 1;
5526 /* After processing and defining all the values of an enumeration type,
5527 install their decls in the enumeration type and finish it off.
5528 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5529 and ATTRIBUTES are the specified attributes.
5530 Returns ENUMTYPE. */
5533 finish_enum (tree enumtype, tree values, tree attributes)
5536 tree minnode = 0, maxnode = 0;
5537 int precision, unsign;
5538 bool toplevel = (file_scope == current_scope);
5539 struct lang_type *lt;
5541 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5543 /* Calculate the maximum value of any enumerator in this type. */
5545 if (values == error_mark_node)
5546 minnode = maxnode = integer_zero_node;
5549 minnode = maxnode = TREE_VALUE (values);
5550 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5552 tree value = TREE_VALUE (pair);
5553 if (tree_int_cst_lt (maxnode, value))
5555 if (tree_int_cst_lt (value, minnode))
5560 /* Construct the final type of this enumeration. It is the same
5561 as one of the integral types - the narrowest one that fits, except
5562 that normally we only go as narrow as int - and signed iff any of
5563 the values are negative. */
5564 unsign = (tree_int_cst_sgn (minnode) >= 0);
5565 precision = MAX (min_precision (minnode, unsign),
5566 min_precision (maxnode, unsign));
5568 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5570 tem = c_common_type_for_size (precision, unsign);
5573 warning (0, "enumeration values exceed range of largest integer");
5574 tem = long_long_integer_type_node;
5578 tem = unsign ? unsigned_type_node : integer_type_node;
5580 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5581 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5582 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5583 TYPE_SIZE (enumtype) = 0;
5585 /* If the precision of the type was specific with an attribute and it
5586 was too small, give an error. Otherwise, use it. */
5587 if (TYPE_PRECISION (enumtype))
5589 if (precision > TYPE_PRECISION (enumtype))
5590 error ("specified mode too small for enumeral values");
5593 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5595 layout_type (enumtype);
5597 if (values != error_mark_node)
5599 /* Change the type of the enumerators to be the enum type. We
5600 need to do this irrespective of the size of the enum, for
5601 proper type checking. Replace the DECL_INITIALs of the
5602 enumerators, and the value slots of the list, with copies
5603 that have the enum type; they cannot be modified in place
5604 because they may be shared (e.g. integer_zero_node) Finally,
5605 change the purpose slots to point to the names of the decls. */
5606 for (pair = values; pair; pair = TREE_CHAIN (pair))
5608 tree enu = TREE_PURPOSE (pair);
5609 tree ini = DECL_INITIAL (enu);
5611 TREE_TYPE (enu) = enumtype;
5613 /* The ISO C Standard mandates enumerators to have type int,
5614 even though the underlying type of an enum type is
5615 unspecified. Here we convert any enumerators that fit in
5616 an int to type int, to avoid promotions to unsigned types
5617 when comparing integers with enumerators that fit in the
5618 int range. When -pedantic is given, build_enumerator()
5619 would have already taken care of those that don't fit. */
5620 if (int_fits_type_p (ini, integer_type_node))
5621 tem = integer_type_node;
5624 ini = convert (tem, ini);
5626 DECL_INITIAL (enu) = ini;
5627 TREE_PURPOSE (pair) = DECL_NAME (enu);
5628 TREE_VALUE (pair) = ini;
5631 TYPE_VALUES (enumtype) = values;
5634 /* Record the min/max values so that we can warn about bit-field
5635 enumerations that are too small for the values. */
5636 lt = GGC_CNEW (struct lang_type);
5637 lt->enum_min = minnode;
5638 lt->enum_max = maxnode;
5639 TYPE_LANG_SPECIFIC (enumtype) = lt;
5641 /* Fix up all variant types of this enum type. */
5642 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5644 if (tem == enumtype)
5646 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5647 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5648 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5649 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5650 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5651 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5652 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5653 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5654 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5655 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5656 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5659 /* Finish debugging output for this type. */
5660 rest_of_type_compilation (enumtype, toplevel);
5665 /* Build and install a CONST_DECL for one value of the
5666 current enumeration type (one that was begun with start_enum).
5667 Return a tree-list containing the CONST_DECL and its value.
5668 Assignment of sequential values by default is handled here. */
5671 build_enumerator (tree name, tree value)
5675 /* Validate and default VALUE. */
5679 /* Don't issue more errors for error_mark_node (i.e. an
5680 undeclared identifier) - just ignore the value expression. */
5681 if (value == error_mark_node)
5683 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5684 || TREE_CODE (value) != INTEGER_CST)
5686 error ("enumerator value for %qE is not an integer constant", name);
5691 value = default_conversion (value);
5692 constant_expression_warning (value);
5696 /* Default based on previous value. */
5697 /* It should no longer be possible to have NON_LVALUE_EXPR
5701 value = enum_next_value;
5703 error ("overflow in enumeration values");
5706 if (pedantic && !int_fits_type_p (value, integer_type_node))
5708 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5709 /* XXX This causes -pedantic to change the meaning of the program.
5710 Remove? -zw 2004-03-15 */
5711 value = convert (integer_type_node, value);
5714 /* Set basis for default for next value. */
5715 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5716 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5718 /* Now create a declaration for the enum value name. */
5720 type = TREE_TYPE (value);
5721 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5722 TYPE_PRECISION (integer_type_node)),
5723 (TYPE_PRECISION (type)
5724 >= TYPE_PRECISION (integer_type_node)
5725 && TYPE_UNSIGNED (type)));
5727 decl = build_decl (CONST_DECL, name, type);
5728 DECL_INITIAL (decl) = convert (type, value);
5731 return tree_cons (decl, value, NULL_TREE);
5735 /* Create the FUNCTION_DECL for a function definition.
5736 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5737 the declaration; they describe the function's name and the type it returns,
5738 but twisted together in a fashion that parallels the syntax of C.
5740 This function creates a binding context for the function body
5741 as well as setting up the FUNCTION_DECL in current_function_decl.
5743 Returns 1 on success. If the DECLARATOR is not suitable for a function
5744 (it defines a datum instead), we return 0, which tells
5745 yyparse to report a parse error. */
5748 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5751 tree decl1, old_decl;
5752 tree restype, resdecl;
5753 struct c_label_context_se *nstack_se;
5754 struct c_label_context_vm *nstack_vm;
5756 current_function_returns_value = 0; /* Assume, until we see it does. */
5757 current_function_returns_null = 0;
5758 current_function_returns_abnormally = 0;
5759 warn_about_return_type = 0;
5760 current_extern_inline = 0;
5761 c_switch_stack = NULL;
5763 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5764 nstack_se->labels_def = NULL;
5765 nstack_se->labels_used = NULL;
5766 nstack_se->next = label_context_stack_se;
5767 label_context_stack_se = nstack_se;
5769 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5770 nstack_vm->labels_def = NULL;
5771 nstack_vm->labels_used = NULL;
5772 nstack_vm->scope = 0;
5773 nstack_vm->next = label_context_stack_vm;
5774 label_context_stack_vm = nstack_vm;
5776 /* Indicate no valid break/continue context by setting these variables
5777 to some non-null, non-label value. We'll notice and emit the proper
5778 error message in c_finish_bc_stmt. */
5779 c_break_label = c_cont_label = size_zero_node;
5781 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5783 /* If the declarator is not suitable for a function definition,
5784 cause a syntax error. */
5788 decl_attributes (&decl1, attributes, 0);
5790 if (DECL_DECLARED_INLINE_P (decl1)
5791 && DECL_UNINLINABLE (decl1)
5792 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5793 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5795 announce_function (decl1);
5797 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5799 error ("return type is an incomplete type");
5800 /* Make it return void instead. */
5802 = build_function_type (void_type_node,
5803 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5806 if (warn_about_return_type)
5807 pedwarn_c99 ("return type defaults to %<int%>");
5809 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5810 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5811 DECL_INITIAL (decl1) = error_mark_node;
5813 /* If this definition isn't a prototype and we had a prototype declaration
5814 before, copy the arg type info from that prototype. */
5815 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5816 current_function_prototype_locus = UNKNOWN_LOCATION;
5817 current_function_prototype_built_in = false;
5818 current_function_prototype_arg_types = NULL_TREE;
5819 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5821 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5822 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5823 TREE_TYPE (TREE_TYPE (old_decl))))
5825 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5827 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5828 current_function_prototype_built_in
5829 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5830 current_function_prototype_arg_types
5831 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5833 if (TREE_PUBLIC (decl1))
5835 /* If there is an external prototype declaration of this
5836 function, record its location but do not copy information
5837 to this decl. This may be an invisible declaration
5838 (built-in or in a scope which has finished) or simply
5839 have more refined argument types than any declaration
5841 struct c_binding *b;
5842 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5843 if (B_IN_SCOPE (b, external_scope))
5847 tree ext_decl, ext_type;
5849 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5850 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5851 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5852 TREE_TYPE (ext_type)))
5854 current_function_prototype_locus
5855 = DECL_SOURCE_LOCATION (ext_decl);
5856 current_function_prototype_built_in
5857 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5858 current_function_prototype_arg_types
5859 = TYPE_ARG_TYPES (ext_type);
5865 /* Optionally warn of old-fashioned def with no previous prototype. */
5866 if (warn_strict_prototypes
5867 && old_decl != error_mark_node
5868 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5869 && C_DECL_ISNT_PROTOTYPE (old_decl))
5870 warning (0, "function declaration isn%'t a prototype");
5871 /* Optionally warn of any global def with no previous prototype. */
5872 else if (warn_missing_prototypes
5873 && old_decl != error_mark_node
5874 && TREE_PUBLIC (decl1)
5875 && !MAIN_NAME_P (DECL_NAME (decl1))
5876 && C_DECL_ISNT_PROTOTYPE (old_decl))
5877 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5878 /* Optionally warn of any def with no previous prototype
5879 if the function has already been used. */
5880 else if (warn_missing_prototypes
5882 && old_decl != error_mark_node
5883 && TREE_USED (old_decl)
5884 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5885 warning (0, "%J%qD was used with no prototype before its definition",
5887 /* Optionally warn of any global def with no previous declaration. */
5888 else if (warn_missing_declarations
5889 && TREE_PUBLIC (decl1)
5891 && !MAIN_NAME_P (DECL_NAME (decl1)))
5892 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5893 /* Optionally warn of any def with no previous declaration
5894 if the function has already been used. */
5895 else if (warn_missing_declarations
5897 && old_decl != error_mark_node
5898 && TREE_USED (old_decl)
5899 && C_DECL_IMPLICIT (old_decl))
5900 warning (0, "%J%qD was used with no declaration before its definition",
5903 /* This is a definition, not a reference.
5904 So normally clear DECL_EXTERNAL.
5905 However, `extern inline' acts like a declaration
5906 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5907 DECL_EXTERNAL (decl1) = current_extern_inline;
5909 /* This function exists in static storage.
5910 (This does not mean `static' in the C sense!) */
5911 TREE_STATIC (decl1) = 1;
5913 /* A nested function is not global. */
5914 if (current_function_decl != 0)
5915 TREE_PUBLIC (decl1) = 0;
5917 /* This is the earliest point at which we might know the assembler
5918 name of the function. Thus, if it's set before this, die horribly. */
5919 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5921 /* If #pragma weak was used, mark the decl weak now. */
5922 if (current_scope == file_scope)
5923 maybe_apply_pragma_weak (decl1);
5925 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5926 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5931 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5932 != integer_type_node)
5933 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5935 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5936 args = TREE_CHAIN (args))
5938 tree type = args ? TREE_VALUE (args) : 0;
5940 if (type == void_type_node)
5947 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5948 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5953 if (TREE_CODE (type) != POINTER_TYPE
5954 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5955 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5957 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5962 if (TREE_CODE (type) != POINTER_TYPE
5963 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5964 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5966 pedwarn ("%Jthird argument of %qD should probably be "
5967 "%<char **%>", decl1, decl1);
5972 /* It is intentional that this message does not mention the third
5973 argument because it's only mentioned in an appendix of the
5975 if (argct > 0 && (argct < 2 || argct > 3))
5976 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5978 if (!TREE_PUBLIC (decl1))
5979 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5982 /* Record the decl so that the function name is defined.
5983 If we already have a decl for this name, and it is a FUNCTION_DECL,
5984 use the old decl. */
5986 current_function_decl = pushdecl (decl1);
5989 declare_parm_level ();
5991 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5992 /* Promote the value to int before returning it. */
5993 if (c_promoting_integer_type_p (restype))
5995 /* It retains unsignedness if not really getting wider. */
5996 if (TYPE_UNSIGNED (restype)
5997 && (TYPE_PRECISION (restype)
5998 == TYPE_PRECISION (integer_type_node)))
5999 restype = unsigned_type_node;
6001 restype = integer_type_node;
6004 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6005 DECL_ARTIFICIAL (resdecl) = 1;
6006 DECL_IGNORED_P (resdecl) = 1;
6007 DECL_RESULT (current_function_decl) = resdecl;
6009 start_fname_decls ();
6014 /* Subroutine of store_parm_decls which handles new-style function
6015 definitions (prototype format). The parms already have decls, so we
6016 need only record them as in effect and complain if any redundant
6017 old-style parm decls were written. */
6019 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6023 if (current_scope->bindings)
6025 error ("%Jold-style parameter declarations in prototyped "
6026 "function definition", fndecl);
6028 /* Get rid of the old-style declarations. */
6032 /* Don't issue this warning for nested functions, and don't issue this
6033 warning if we got here because ARG_INFO_TYPES was error_mark_node
6034 (this happens when a function definition has just an ellipsis in
6035 its parameter list). */
6036 else if (warn_traditional && !in_system_header && !current_function_scope
6037 && arg_info->types != error_mark_node)
6038 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6041 /* Now make all the parameter declarations visible in the function body.
6042 We can bypass most of the grunt work of pushdecl. */
6043 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6045 DECL_CONTEXT (decl) = current_function_decl;
6046 if (DECL_NAME (decl))
6047 bind (DECL_NAME (decl), decl, current_scope,
6048 /*invisible=*/false, /*nested=*/false);
6050 error ("%Jparameter name omitted", decl);
6053 /* Record the parameter list in the function declaration. */
6054 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6056 /* Now make all the ancillary declarations visible, likewise. */
6057 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6059 DECL_CONTEXT (decl) = current_function_decl;
6060 if (DECL_NAME (decl))
6061 bind (DECL_NAME (decl), decl, current_scope,
6062 /*invisible=*/false, /*nested=*/false);
6065 /* And all the tag declarations. */
6066 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6067 if (TREE_PURPOSE (decl))
6068 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6069 /*invisible=*/false, /*nested=*/false);
6072 /* Subroutine of store_parm_decls which handles old-style function
6073 definitions (separate parameter list and declarations). */
6076 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6078 struct c_binding *b;
6079 tree parm, decl, last;
6080 tree parmids = arg_info->parms;
6082 /* We use DECL_WEAK as a flag to show which parameters have been
6083 seen already, since it is not used on PARM_DECL. */
6084 #ifdef ENABLE_CHECKING
6085 for (b = current_scope->bindings; b; b = b->prev)
6086 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6089 if (!in_system_header)
6090 warning (OPT_Wold_style_definition, "%Jold-style function definition", fndecl);
6092 /* Match each formal parameter name with its declaration. Save each
6093 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6094 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6096 if (TREE_VALUE (parm) == 0)
6098 error ("%Jparameter name missing from parameter list", fndecl);
6099 TREE_PURPOSE (parm) = 0;
6103 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6104 if (b && B_IN_CURRENT_SCOPE (b))
6107 /* If we got something other than a PARM_DECL it is an error. */
6108 if (TREE_CODE (decl) != PARM_DECL)
6109 error ("%J%qD declared as a non-parameter", decl, decl);
6110 /* If the declaration is already marked, we have a duplicate
6111 name. Complain and ignore the duplicate. */
6112 else if (DECL_WEAK (decl))
6114 error ("%Jmultiple parameters named %qD", decl, decl);
6115 TREE_PURPOSE (parm) = 0;
6118 /* If the declaration says "void", complain and turn it into
6120 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6122 error ("%Jparameter %qD declared with void type", decl, decl);
6123 TREE_TYPE (decl) = integer_type_node;
6124 DECL_ARG_TYPE (decl) = integer_type_node;
6125 layout_decl (decl, 0);
6128 /* If no declaration found, default to int. */
6131 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6132 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6133 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6137 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6138 else if (extra_warnings)
6139 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6142 TREE_PURPOSE (parm) = decl;
6143 DECL_WEAK (decl) = 1;
6146 /* Now examine the parms chain for incomplete declarations
6147 and declarations with no corresponding names. */
6149 for (b = current_scope->bindings; b; b = b->prev)
6152 if (TREE_CODE (parm) != PARM_DECL)
6155 if (TREE_TYPE (parm) != error_mark_node
6156 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6158 error ("%Jparameter %qD has incomplete type", parm, parm);
6159 TREE_TYPE (parm) = error_mark_node;
6162 if (!DECL_WEAK (parm))
6164 error ("%Jdeclaration for parameter %qD but no such parameter",
6167 /* Pretend the parameter was not missing.
6168 This gets us to a standard state and minimizes
6169 further error messages. */
6170 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6174 /* Chain the declarations together in the order of the list of
6175 names. Store that chain in the function decl, replacing the
6176 list of names. Update the current scope to match. */
6177 DECL_ARGUMENTS (fndecl) = 0;
6179 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6180 if (TREE_PURPOSE (parm))
6182 if (parm && TREE_PURPOSE (parm))
6184 last = TREE_PURPOSE (parm);
6185 DECL_ARGUMENTS (fndecl) = last;
6186 DECL_WEAK (last) = 0;
6188 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6189 if (TREE_PURPOSE (parm))
6191 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6192 last = TREE_PURPOSE (parm);
6193 DECL_WEAK (last) = 0;
6195 TREE_CHAIN (last) = 0;
6198 /* If there was a previous prototype,
6199 set the DECL_ARG_TYPE of each argument according to
6200 the type previously specified, and report any mismatches. */
6202 if (current_function_prototype_arg_types)
6205 for (parm = DECL_ARGUMENTS (fndecl),
6206 type = current_function_prototype_arg_types;
6207 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6208 != void_type_node));
6209 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6211 if (parm == 0 || type == 0
6212 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6214 if (current_function_prototype_built_in)
6215 warning (0, "number of arguments doesn%'t match "
6216 "built-in prototype");
6219 error ("number of arguments doesn%'t match prototype");
6220 error ("%Hprototype declaration",
6221 ¤t_function_prototype_locus);
6225 /* Type for passing arg must be consistent with that
6226 declared for the arg. ISO C says we take the unqualified
6227 type for parameters declared with qualified type. */
6228 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6229 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6231 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6232 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6234 /* Adjust argument to match prototype. E.g. a previous
6235 `int foo(float);' prototype causes
6236 `int foo(x) float x; {...}' to be treated like
6237 `int foo(float x) {...}'. This is particularly
6238 useful for argument types like uid_t. */
6239 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6241 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6242 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6243 && TYPE_PRECISION (TREE_TYPE (parm))
6244 < TYPE_PRECISION (integer_type_node))
6245 DECL_ARG_TYPE (parm) = integer_type_node;
6249 /* ??? Is it possible to get here with a
6250 built-in prototype or will it always have
6251 been diagnosed as conflicting with an
6252 old-style definition and discarded? */
6253 if (current_function_prototype_built_in)
6254 warning (0, "promoted argument %qD "
6255 "doesn%'t match built-in prototype", parm);
6258 pedwarn ("promoted argument %qD "
6259 "doesn%'t match prototype", parm);
6260 pedwarn ("%Hprototype declaration",
6261 ¤t_function_prototype_locus);
6267 if (current_function_prototype_built_in)
6268 warning (0, "argument %qD doesn%'t match "
6269 "built-in prototype", parm);
6272 error ("argument %qD doesn%'t match prototype", parm);
6273 error ("%Hprototype declaration",
6274 ¤t_function_prototype_locus);
6279 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6282 /* Otherwise, create a prototype that would match. */
6286 tree actual = 0, last = 0, type;
6288 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6290 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6292 TREE_CHAIN (last) = type;
6297 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6299 TREE_CHAIN (last) = type;
6303 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6304 of the type of this function, but we need to avoid having this
6305 affect the types of other similarly-typed functions, so we must
6306 first force the generation of an identical (but separate) type
6307 node for the relevant function type. The new node we create
6308 will be a variant of the main variant of the original function
6311 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6313 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6317 /* Store parameter declarations passed in ARG_INFO into the current
6318 function declaration. */
6321 store_parm_decls_from (struct c_arg_info *arg_info)
6323 current_function_arg_info = arg_info;
6324 store_parm_decls ();
6327 /* Store the parameter declarations into the current function declaration.
6328 This is called after parsing the parameter declarations, before
6329 digesting the body of the function.
6331 For an old-style definition, construct a prototype out of the old-style
6332 parameter declarations and inject it into the function's type. */
6335 store_parm_decls (void)
6337 tree fndecl = current_function_decl;
6340 /* The argument information block for FNDECL. */
6341 struct c_arg_info *arg_info = current_function_arg_info;
6342 current_function_arg_info = 0;
6344 /* True if this definition is written with a prototype. Note:
6345 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6346 list in a function definition as equivalent to (void) -- an
6347 empty argument list specifies the function has no parameters,
6348 but only (void) sets up a prototype for future calls. */
6349 proto = arg_info->types != 0;
6352 store_parm_decls_newstyle (fndecl, arg_info);
6354 store_parm_decls_oldstyle (fndecl, arg_info);
6356 /* The next call to push_scope will be a function body. */
6358 next_is_function_body = true;
6360 /* Write a record describing this function definition to the prototypes
6361 file (if requested). */
6363 gen_aux_info_record (fndecl, 1, 0, proto);
6365 /* Initialize the RTL code for the function. */
6366 allocate_struct_function (fndecl);
6368 /* Begin the statement tree for this function. */
6369 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6371 /* ??? Insert the contents of the pending sizes list into the function
6372 to be evaluated. The only reason left to have this is
6373 void foo(int n, int array[n++])
6374 because we throw away the array type in favor of a pointer type, and
6375 thus won't naturally see the SAVE_EXPR containing the increment. All
6376 other pending sizes would be handled by gimplify_parameters. */
6379 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6380 add_stmt (TREE_VALUE (t));
6383 /* Even though we're inside a function body, we still don't want to
6384 call expand_expr to calculate the size of a variable-sized array.
6385 We haven't necessarily assigned RTL to all variables yet, so it's
6386 not safe to try to expand expressions involving them. */
6387 cfun->x_dont_save_pending_sizes_p = 1;
6390 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6394 c_warn_unused_result_recursively (tree fndecl)
6396 struct cgraph_node *cgn;
6398 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6399 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6401 /* Finalize all nested functions now. */
6402 cgn = cgraph_node (fndecl);
6403 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6404 c_warn_unused_result_recursively (cgn->decl);
6407 /* Finish up a function declaration and compile that function
6408 all the way to assembler language output. The free the storage
6409 for the function definition.
6411 This is called after parsing the body of the function definition. */
6414 finish_function (void)
6416 tree fndecl = current_function_decl;
6418 label_context_stack_se = label_context_stack_se->next;
6419 label_context_stack_vm = label_context_stack_vm->next;
6421 if (TREE_CODE (fndecl) == FUNCTION_DECL
6422 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6424 tree args = DECL_ARGUMENTS (fndecl);
6425 for (; args; args = TREE_CHAIN (args))
6427 tree type = TREE_TYPE (args);
6428 if (INTEGRAL_TYPE_P (type)
6429 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6430 DECL_ARG_TYPE (args) = integer_type_node;
6434 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6435 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6437 /* Must mark the RESULT_DECL as being in this function. */
6439 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6440 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6442 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6444 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6445 != integer_type_node)
6447 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6448 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6450 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6456 tree stmt = c_finish_return (integer_zero_node);
6457 #ifdef USE_MAPPED_LOCATION
6458 /* Hack. We don't want the middle-end to warn that this return
6459 is unreachable, so we mark its location as special. Using
6460 UNKNOWN_LOCATION has the problem that it gets clobbered in
6461 annotate_one_with_locus. A cleaner solution might be to
6462 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6464 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6466 /* Hack. We don't want the middle-end to warn that this
6467 return is unreachable, so put the statement on the
6469 annotate_with_file_line (stmt, input_filename, 0);
6475 /* Tie off the statement tree for this function. */
6476 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6478 finish_fname_decls ();
6480 /* Complain if there's just no return statement. */
6481 if (warn_return_type
6482 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6483 && !current_function_returns_value && !current_function_returns_null
6484 /* Don't complain if we are no-return. */
6485 && !current_function_returns_abnormally
6486 /* Don't warn for main(). */
6487 && !MAIN_NAME_P (DECL_NAME (fndecl))
6488 /* Or if they didn't actually specify a return type. */
6489 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6490 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6491 inline function, as we might never be compiled separately. */
6492 && DECL_INLINE (fndecl))
6493 warning (0, "no return statement in function returning non-void");
6495 /* With just -Wextra, complain only if function returns both with
6496 and without a value. */
6498 && current_function_returns_value
6499 && current_function_returns_null)
6500 warning (0, "this function may return with or without a value");
6502 /* Store the end of the function, so that we get good line number
6503 info for the epilogue. */
6504 cfun->function_end_locus = input_location;
6506 /* If we don't have ctors/dtors sections, and this is a static
6507 constructor or destructor, it must be recorded now. */
6508 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6509 && !targetm.have_ctors_dtors)
6510 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6511 if (DECL_STATIC_DESTRUCTOR (fndecl)
6512 && !targetm.have_ctors_dtors)
6513 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6515 /* Finalize the ELF visibility for the function. */
6516 c_determine_visibility (fndecl);
6518 /* Genericize before inlining. Delay genericizing nested functions
6519 until their parent function is genericized. Since finalizing
6520 requires GENERIC, delay that as well. */
6522 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6523 && !undef_nested_function)
6525 if (!decl_function_context (fndecl))
6527 c_genericize (fndecl);
6528 c_warn_unused_result_recursively (fndecl);
6530 /* ??? Objc emits functions after finalizing the compilation unit.
6531 This should be cleaned up later and this conditional removed. */
6532 if (cgraph_global_info_ready)
6534 c_expand_body (fndecl);
6538 cgraph_finalize_function (fndecl, false);
6542 /* Register this function with cgraph just far enough to get it
6543 added to our parent's nested function list. Handy, since the
6544 C front end doesn't have such a list. */
6545 (void) cgraph_node (fndecl);
6549 if (!decl_function_context (fndecl))
6550 undef_nested_function = false;
6552 /* We're leaving the context of this function, so zap cfun.
6553 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6554 tree_rest_of_compilation. */
6556 current_function_decl = NULL;
6559 /* Generate the RTL for the body of FNDECL. */
6562 c_expand_body (tree fndecl)
6565 if (!DECL_INITIAL (fndecl)
6566 || DECL_INITIAL (fndecl) == error_mark_node)
6569 tree_rest_of_compilation (fndecl);
6571 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6572 && targetm.have_ctors_dtors)
6573 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6574 DEFAULT_INIT_PRIORITY);
6575 if (DECL_STATIC_DESTRUCTOR (fndecl)
6576 && targetm.have_ctors_dtors)
6577 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6578 DEFAULT_INIT_PRIORITY);
6581 /* Check the declarations given in a for-loop for satisfying the C99
6584 check_for_loop_decls (void)
6586 struct c_binding *b;
6590 /* If we get here, declarations have been used in a for loop without
6591 the C99 for loop scope. This doesn't make much sense, so don't
6593 error ("%<for%> loop initial declaration used outside C99 mode");
6596 /* C99 subclause 6.8.5 paragraph 3:
6598 [#3] The declaration part of a for statement shall only
6599 declare identifiers for objects having storage class auto or
6602 It isn't clear whether, in this sentence, "identifiers" binds to
6603 "shall only declare" or to "objects" - that is, whether all identifiers
6604 declared must be identifiers for objects, or whether the restriction
6605 only applies to those that are. (A question on this in comp.std.c
6606 in November 2000 received no answer.) We implement the strictest
6607 interpretation, to avoid creating an extension which later causes
6610 for (b = current_scope->bindings; b; b = b->prev)
6613 tree decl = b->decl;
6618 switch (TREE_CODE (decl))
6621 if (TREE_STATIC (decl))
6622 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6623 "initial declaration", decl, decl);
6624 else if (DECL_EXTERNAL (decl))
6625 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6626 "initial declaration", decl, decl);
6630 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6634 error ("%<union %E%> declared in %<for%> loop initial declaration",
6638 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6642 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6643 "initial declaration", decl, decl);
6648 /* Save and reinitialize the variables
6649 used during compilation of a C function. */
6652 c_push_function_context (struct function *f)
6654 struct language_function *p;
6655 p = GGC_NEW (struct language_function);
6658 p->base.x_stmt_tree = c_stmt_tree;
6659 p->x_break_label = c_break_label;
6660 p->x_cont_label = c_cont_label;
6661 p->x_switch_stack = c_switch_stack;
6662 p->arg_info = current_function_arg_info;
6663 p->returns_value = current_function_returns_value;
6664 p->returns_null = current_function_returns_null;
6665 p->returns_abnormally = current_function_returns_abnormally;
6666 p->warn_about_return_type = warn_about_return_type;
6667 p->extern_inline = current_extern_inline;
6670 /* Restore the variables used during compilation of a C function. */
6673 c_pop_function_context (struct function *f)
6675 struct language_function *p = f->language;
6677 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6678 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6680 /* Stop pointing to the local nodes about to be freed. */
6681 /* But DECL_INITIAL must remain nonzero so we know this
6682 was an actual function definition. */
6683 DECL_INITIAL (current_function_decl) = error_mark_node;
6684 DECL_ARGUMENTS (current_function_decl) = 0;
6687 c_stmt_tree = p->base.x_stmt_tree;
6688 c_break_label = p->x_break_label;
6689 c_cont_label = p->x_cont_label;
6690 c_switch_stack = p->x_switch_stack;
6691 current_function_arg_info = p->arg_info;
6692 current_function_returns_value = p->returns_value;
6693 current_function_returns_null = p->returns_null;
6694 current_function_returns_abnormally = p->returns_abnormally;
6695 warn_about_return_type = p->warn_about_return_type;
6696 current_extern_inline = p->extern_inline;
6701 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6704 c_dup_lang_specific_decl (tree decl)
6706 struct lang_decl *ld;
6708 if (!DECL_LANG_SPECIFIC (decl))
6711 ld = GGC_NEW (struct lang_decl);
6712 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6713 DECL_LANG_SPECIFIC (decl) = ld;
6716 /* The functions below are required for functionality of doing
6717 function at once processing in the C front end. Currently these
6718 functions are not called from anywhere in the C front end, but as
6719 these changes continue, that will change. */
6721 /* Returns nonzero if the current statement is a full expression,
6722 i.e. temporaries created during that statement should be destroyed
6723 at the end of the statement. */
6726 stmts_are_full_exprs_p (void)
6731 /* Returns the stmt_tree (if any) to which statements are currently
6732 being added. If there is no active statement-tree, NULL is
6736 current_stmt_tree (void)
6738 return &c_stmt_tree;
6741 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6745 anon_aggr_type_p (tree ARG_UNUSED (node))
6750 /* Return the global value of T as a symbol. */
6753 identifier_global_value (tree t)
6755 struct c_binding *b;
6757 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6758 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6764 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6765 otherwise the name is found in ridpointers from RID_INDEX. */
6768 record_builtin_type (enum rid rid_index, const char *name, tree type)
6772 id = ridpointers[(int) rid_index];
6774 id = get_identifier (name);
6775 decl = build_decl (TYPE_DECL, id, type);
6777 if (debug_hooks->type_decl)
6778 debug_hooks->type_decl (decl, false);
6781 /* Build the void_list_node (void_type_node having been created). */
6783 build_void_list_node (void)
6785 tree t = build_tree_list (NULL_TREE, void_type_node);
6789 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6792 build_c_parm (struct c_declspecs *specs, tree attrs,
6793 struct c_declarator *declarator)
6795 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6798 ret->declarator = declarator;
6802 /* Return a declarator with nested attributes. TARGET is the inner
6803 declarator to which these attributes apply. ATTRS are the
6806 struct c_declarator *
6807 build_attrs_declarator (tree attrs, struct c_declarator *target)
6809 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6810 ret->kind = cdk_attrs;
6811 ret->declarator = target;
6812 ret->u.attrs = attrs;
6816 /* Return a declarator for a function with arguments specified by ARGS
6817 and return type specified by TARGET. */
6819 struct c_declarator *
6820 build_function_declarator (struct c_arg_info *args,
6821 struct c_declarator *target)
6823 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6824 ret->kind = cdk_function;
6825 ret->declarator = target;
6826 ret->u.arg_info = args;
6830 /* Return a declarator for the identifier IDENT (which may be
6831 NULL_TREE for an abstract declarator). */
6833 struct c_declarator *
6834 build_id_declarator (tree ident)
6836 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6838 ret->declarator = 0;
6840 /* Default value - may get reset to a more precise location. */
6841 ret->id_loc = input_location;
6845 /* Return something to represent absolute declarators containing a *.
6846 TARGET is the absolute declarator that the * contains.
6847 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6848 to apply to the pointer type. */
6850 struct c_declarator *
6851 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6852 struct c_declarator *target)
6856 struct c_declarator *itarget = target;
6857 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6858 if (type_quals_attrs)
6860 attrs = type_quals_attrs->attrs;
6861 quals = quals_from_declspecs (type_quals_attrs);
6862 if (attrs != NULL_TREE)
6863 itarget = build_attrs_declarator (attrs, target);
6865 ret->kind = cdk_pointer;
6866 ret->declarator = itarget;
6867 ret->u.pointer_quals = quals;
6871 /* Return a pointer to a structure for an empty list of declaration
6874 struct c_declspecs *
6875 build_null_declspecs (void)
6877 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6881 ret->typespec_word = cts_none;
6882 ret->storage_class = csc_none;
6883 ret->declspecs_seen_p = false;
6884 ret->type_seen_p = false;
6885 ret->non_sc_seen_p = false;
6886 ret->typedef_p = false;
6887 ret->tag_defined_p = false;
6888 ret->explicit_signed_p = false;
6889 ret->deprecated_p = false;
6890 ret->default_int_p = false;
6891 ret->long_p = false;
6892 ret->long_long_p = false;
6893 ret->short_p = false;
6894 ret->signed_p = false;
6895 ret->unsigned_p = false;
6896 ret->complex_p = false;
6897 ret->inline_p = false;
6898 ret->thread_p = false;
6899 ret->const_p = false;
6900 ret->volatile_p = false;
6901 ret->restrict_p = false;
6905 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6908 struct c_declspecs *
6909 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6913 specs->non_sc_seen_p = true;
6914 specs->declspecs_seen_p = true;
6915 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6916 && C_IS_RESERVED_WORD (qual));
6917 i = C_RID_CODE (qual);
6921 dupe = specs->const_p;
6922 specs->const_p = true;
6925 dupe = specs->volatile_p;
6926 specs->volatile_p = true;
6929 dupe = specs->restrict_p;
6930 specs->restrict_p = true;
6935 if (dupe && pedantic && !flag_isoc99)
6936 pedwarn ("duplicate %qE", qual);
6940 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6943 struct c_declspecs *
6944 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6946 tree type = spec.spec;
6947 specs->non_sc_seen_p = true;
6948 specs->declspecs_seen_p = true;
6949 specs->type_seen_p = true;
6950 if (TREE_DEPRECATED (type))
6951 specs->deprecated_p = true;
6953 /* Handle type specifier keywords. */
6954 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6956 enum rid i = C_RID_CODE (type);
6959 error ("two or more data types in declaration specifiers");
6962 if ((int) i <= (int) RID_LAST_MODIFIER)
6964 /* "long", "short", "signed", "unsigned" or "_Complex". */
6969 if (specs->long_long_p)
6971 error ("%<long long long%> is too long for GCC");
6976 if (specs->typespec_word == cts_double)
6978 error ("both %<long long%> and %<double%> in "
6979 "declaration specifiers");
6982 if (pedantic && !flag_isoc99 && !in_system_header
6984 pedwarn ("ISO C90 does not support %<long long%>");
6985 specs->long_long_p = 1;
6989 error ("both %<long%> and %<short%> in "
6990 "declaration specifiers");
6991 else if (specs->typespec_word == cts_void)
6992 error ("both %<long%> and %<void%> in "
6993 "declaration specifiers");
6994 else if (specs->typespec_word == cts_bool)
6995 error ("both %<long%> and %<_Bool%> in "
6996 "declaration specifiers");
6997 else if (specs->typespec_word == cts_char)
6998 error ("both %<long%> and %<char%> in "
6999 "declaration specifiers");
7000 else if (specs->typespec_word == cts_float)
7001 error ("both %<long%> and %<float%> in "
7002 "declaration specifiers");
7004 specs->long_p = true;
7007 dupe = specs->short_p;
7009 error ("both %<long%> and %<short%> in "
7010 "declaration specifiers");
7011 else if (specs->typespec_word == cts_void)
7012 error ("both %<short%> and %<void%> in "
7013 "declaration specifiers");
7014 else if (specs->typespec_word == cts_bool)
7015 error ("both %<short%> and %<_Bool%> in "
7016 "declaration specifiers");
7017 else if (specs->typespec_word == cts_char)
7018 error ("both %<short%> and %<char%> in "
7019 "declaration specifiers");
7020 else if (specs->typespec_word == cts_float)
7021 error ("both %<short%> and %<float%> in "
7022 "declaration specifiers");
7023 else if (specs->typespec_word == cts_double)
7024 error ("both %<short%> and %<double%> in "
7025 "declaration specifiers");
7027 specs->short_p = true;
7030 dupe = specs->signed_p;
7031 if (specs->unsigned_p)
7032 error ("both %<signed%> and %<unsigned%> in "
7033 "declaration specifiers");
7034 else if (specs->typespec_word == cts_void)
7035 error ("both %<signed%> and %<void%> in "
7036 "declaration specifiers");
7037 else if (specs->typespec_word == cts_bool)
7038 error ("both %<signed%> and %<_Bool%> in "
7039 "declaration specifiers");
7040 else if (specs->typespec_word == cts_float)
7041 error ("both %<signed%> and %<float%> in "
7042 "declaration specifiers");
7043 else if (specs->typespec_word == cts_double)
7044 error ("both %<signed%> and %<double%> in "
7045 "declaration specifiers");
7047 specs->signed_p = true;
7050 dupe = specs->unsigned_p;
7051 if (specs->signed_p)
7052 error ("both %<signed%> and %<unsigned%> in "
7053 "declaration specifiers");
7054 else if (specs->typespec_word == cts_void)
7055 error ("both %<unsigned%> and %<void%> in "
7056 "declaration specifiers");
7057 else if (specs->typespec_word == cts_bool)
7058 error ("both %<unsigned%> and %<_Bool%> in "
7059 "declaration specifiers");
7060 else if (specs->typespec_word == cts_float)
7061 error ("both %<unsigned%> and %<float%> in "
7062 "declaration specifiers");
7063 else if (specs->typespec_word == cts_double)
7064 error ("both %<unsigned%> and %<double%> in "
7065 "declaration specifiers");
7067 specs->unsigned_p = true;
7070 dupe = specs->complex_p;
7071 if (pedantic && !flag_isoc99 && !in_system_header)
7072 pedwarn ("ISO C90 does not support complex types");
7073 if (specs->typespec_word == cts_void)
7074 error ("both %<complex%> and %<void%> in "
7075 "declaration specifiers");
7076 else if (specs->typespec_word == cts_bool)
7077 error ("both %<complex%> and %<_Bool%> in "
7078 "declaration specifiers");
7080 specs->complex_p = true;
7087 error ("duplicate %qE", type);
7093 /* "void", "_Bool", "char", "int", "float" or "double". */
7094 if (specs->typespec_word != cts_none)
7096 error ("two or more data types in declaration specifiers");
7103 error ("both %<long%> and %<void%> in "
7104 "declaration specifiers");
7105 else if (specs->short_p)
7106 error ("both %<short%> and %<void%> in "
7107 "declaration specifiers");
7108 else if (specs->signed_p)
7109 error ("both %<signed%> and %<void%> in "
7110 "declaration specifiers");
7111 else if (specs->unsigned_p)
7112 error ("both %<unsigned%> and %<void%> in "
7113 "declaration specifiers");
7114 else if (specs->complex_p)
7115 error ("both %<complex%> and %<void%> in "
7116 "declaration specifiers");
7118 specs->typespec_word = cts_void;
7122 error ("both %<long%> and %<_Bool%> in "
7123 "declaration specifiers");
7124 else if (specs->short_p)
7125 error ("both %<short%> and %<_Bool%> in "
7126 "declaration specifiers");
7127 else if (specs->signed_p)
7128 error ("both %<signed%> and %<_Bool%> in "
7129 "declaration specifiers");
7130 else if (specs->unsigned_p)
7131 error ("both %<unsigned%> and %<_Bool%> in "
7132 "declaration specifiers");
7133 else if (specs->complex_p)
7134 error ("both %<complex%> and %<_Bool%> in "
7135 "declaration specifiers");
7137 specs->typespec_word = cts_bool;
7141 error ("both %<long%> and %<char%> in "
7142 "declaration specifiers");
7143 else if (specs->short_p)
7144 error ("both %<short%> and %<char%> in "
7145 "declaration specifiers");
7147 specs->typespec_word = cts_char;
7150 specs->typespec_word = cts_int;
7154 error ("both %<long%> and %<float%> in "
7155 "declaration specifiers");
7156 else if (specs->short_p)
7157 error ("both %<short%> and %<float%> in "
7158 "declaration specifiers");
7159 else if (specs->signed_p)
7160 error ("both %<signed%> and %<float%> in "
7161 "declaration specifiers");
7162 else if (specs->unsigned_p)
7163 error ("both %<unsigned%> and %<float%> in "
7164 "declaration specifiers");
7166 specs->typespec_word = cts_float;
7169 if (specs->long_long_p)
7170 error ("both %<long long%> and %<double%> in "
7171 "declaration specifiers");
7172 else if (specs->short_p)
7173 error ("both %<short%> and %<double%> in "
7174 "declaration specifiers");
7175 else if (specs->signed_p)
7176 error ("both %<signed%> and %<double%> in "
7177 "declaration specifiers");
7178 else if (specs->unsigned_p)
7179 error ("both %<unsigned%> and %<double%> in "
7180 "declaration specifiers");
7182 specs->typespec_word = cts_double;
7185 /* ObjC reserved word "id", handled below. */
7191 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7192 form of ObjC type, cases such as "int" and "long" being handled
7193 above), a TYPE (struct, union, enum and typeof specifiers) or an
7194 ERROR_MARK. In none of these cases may there have previously
7195 been any type specifiers. */
7196 if (specs->type || specs->typespec_word != cts_none
7197 || specs->long_p || specs->short_p || specs->signed_p
7198 || specs->unsigned_p || specs->complex_p)
7199 error ("two or more data types in declaration specifiers");
7200 else if (TREE_CODE (type) == TYPE_DECL)
7202 if (TREE_TYPE (type) == error_mark_node)
7203 ; /* Allow the type to default to int to avoid cascading errors. */
7206 specs->type = TREE_TYPE (type);
7207 specs->decl_attr = DECL_ATTRIBUTES (type);
7208 specs->typedef_p = true;
7209 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7212 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7214 tree t = lookup_name (type);
7215 if (!t || TREE_CODE (t) != TYPE_DECL)
7216 error ("%qE fails to be a typedef or built in type", type);
7217 else if (TREE_TYPE (t) == error_mark_node)
7220 specs->type = TREE_TYPE (t);
7222 else if (TREE_CODE (type) != ERROR_MARK)
7224 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7225 specs->tag_defined_p = true;
7226 if (spec.kind == ctsk_typeof)
7227 specs->typedef_p = true;
7234 /* Add the storage class specifier or function specifier SCSPEC to the
7235 declaration specifiers SPECS, returning SPECS. */
7237 struct c_declspecs *
7238 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7241 enum c_storage_class n = csc_none;
7243 specs->declspecs_seen_p = true;
7244 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7245 && C_IS_RESERVED_WORD (scspec));
7246 i = C_RID_CODE (scspec);
7247 if (extra_warnings && specs->non_sc_seen_p)
7248 warning (0, "%qE is not at beginning of declaration", scspec);
7252 /* C99 permits duplicate inline. Although of doubtful utility,
7253 it seems simplest to permit it in gnu89 mode as well, as
7254 there is also little utility in maintaining this as a
7255 difference between gnu89 and C99 inline. */
7257 specs->inline_p = true;
7260 dupe = specs->thread_p;
7261 if (specs->storage_class == csc_auto)
7262 error ("%<__thread%> used with %<auto%>");
7263 else if (specs->storage_class == csc_register)
7264 error ("%<__thread%> used with %<register%>");
7265 else if (specs->storage_class == csc_typedef)
7266 error ("%<__thread%> used with %<typedef%>");
7268 specs->thread_p = true;
7275 /* Diagnose "__thread extern". */
7276 if (specs->thread_p)
7277 error ("%<__thread%> before %<extern%>");
7284 /* Diagnose "__thread static". */
7285 if (specs->thread_p)
7286 error ("%<__thread%> before %<static%>");
7294 if (n != csc_none && n == specs->storage_class)
7297 error ("duplicate %qE", scspec);
7300 if (specs->storage_class != csc_none && n != specs->storage_class)
7302 error ("multiple storage classes in declaration specifiers");
7306 specs->storage_class = n;
7307 if (n != csc_extern && n != csc_static && specs->thread_p)
7309 error ("%<__thread%> used with %qE", scspec);
7310 specs->thread_p = false;
7317 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7320 struct c_declspecs *
7321 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7323 specs->attrs = chainon (attrs, specs->attrs);
7324 specs->declspecs_seen_p = true;
7328 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7329 specifiers with any other type specifier to determine the resulting
7330 type. This is where ISO C checks on complex types are made, since
7331 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7334 struct c_declspecs *
7335 finish_declspecs (struct c_declspecs *specs)
7337 /* If a type was specified as a whole, we have no modifiers and are
7339 if (specs->type != NULL_TREE)
7341 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7342 && !specs->signed_p && !specs->unsigned_p
7343 && !specs->complex_p);
7347 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7348 has been specified, treat it as "int" unless "_Complex" is
7349 present and there are no other specifiers. If we just have
7350 "_Complex", it is equivalent to "_Complex double", but e.g.
7351 "_Complex short" is equivalent to "_Complex short int". */
7352 if (specs->typespec_word == cts_none)
7354 if (specs->long_p || specs->short_p
7355 || specs->signed_p || specs->unsigned_p)
7357 specs->typespec_word = cts_int;
7359 else if (specs->complex_p)
7361 specs->typespec_word = cts_double;
7363 pedwarn ("ISO C does not support plain %<complex%> meaning "
7364 "%<double complex%>");
7368 specs->typespec_word = cts_int;
7369 specs->default_int_p = true;
7370 /* We don't diagnose this here because grokdeclarator will
7371 give more specific diagnostics according to whether it is
7372 a function definition. */
7376 /* If "signed" was specified, record this to distinguish "int" and
7377 "signed int" in the case of a bit-field with
7378 -funsigned-bitfields. */
7379 specs->explicit_signed_p = specs->signed_p;
7381 /* Now compute the actual type. */
7382 switch (specs->typespec_word)
7385 gcc_assert (!specs->long_p && !specs->short_p
7386 && !specs->signed_p && !specs->unsigned_p
7387 && !specs->complex_p);
7388 specs->type = void_type_node;
7391 gcc_assert (!specs->long_p && !specs->short_p
7392 && !specs->signed_p && !specs->unsigned_p
7393 && !specs->complex_p);
7394 specs->type = boolean_type_node;
7397 gcc_assert (!specs->long_p && !specs->short_p);
7398 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7399 if (specs->signed_p)
7400 specs->type = signed_char_type_node;
7401 else if (specs->unsigned_p)
7402 specs->type = unsigned_char_type_node;
7404 specs->type = char_type_node;
7405 if (specs->complex_p)
7408 pedwarn ("ISO C does not support complex integer types");
7409 specs->type = build_complex_type (specs->type);
7413 gcc_assert (!(specs->long_p && specs->short_p));
7414 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7415 if (specs->long_long_p)
7416 specs->type = (specs->unsigned_p
7417 ? long_long_unsigned_type_node
7418 : long_long_integer_type_node);
7419 else if (specs->long_p)
7420 specs->type = (specs->unsigned_p
7421 ? long_unsigned_type_node
7422 : long_integer_type_node);
7423 else if (specs->short_p)
7424 specs->type = (specs->unsigned_p
7425 ? short_unsigned_type_node
7426 : short_integer_type_node);
7428 specs->type = (specs->unsigned_p
7429 ? unsigned_type_node
7430 : integer_type_node);
7431 if (specs->complex_p)
7434 pedwarn ("ISO C does not support complex integer types");
7435 specs->type = build_complex_type (specs->type);
7439 gcc_assert (!specs->long_p && !specs->short_p
7440 && !specs->signed_p && !specs->unsigned_p);
7441 specs->type = (specs->complex_p
7442 ? complex_float_type_node
7446 gcc_assert (!specs->long_long_p && !specs->short_p
7447 && !specs->signed_p && !specs->unsigned_p);
7450 specs->type = (specs->complex_p
7451 ? complex_long_double_type_node
7452 : long_double_type_node);
7456 specs->type = (specs->complex_p
7457 ? complex_double_type_node
7458 : double_type_node);
7468 /* Synthesize a function which calls all the global ctors or global
7469 dtors in this file. This is only used for targets which do not
7470 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7472 build_cdtor (int method_type, tree cdtors)
7479 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7480 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7483 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7486 /* Perform final processing on one file scope's declarations (or the
7487 external scope's declarations), GLOBALS. */
7489 c_write_global_declarations_1 (tree globals)
7491 size_t len = list_length (globals);
7492 tree *vec = XNEWVEC (tree, len);
7496 /* Process the decls in the order they were written. */
7497 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7500 /* Check for used but undefined static functions using the C
7501 standard's definition of "used", and set TREE_NO_WARNING so
7502 that check_global_declarations doesn't repeat the check. */
7503 if (TREE_CODE (decl) == FUNCTION_DECL
7504 && DECL_INITIAL (decl) == 0
7505 && DECL_EXTERNAL (decl)
7506 && !TREE_PUBLIC (decl)
7507 && C_DECL_USED (decl))
7509 pedwarn ("%J%qF used but never defined", decl, decl);
7510 TREE_NO_WARNING (decl) = 1;
7514 wrapup_global_declarations (vec, len);
7515 check_global_declarations (vec, len);
7521 c_write_global_declarations (void)
7525 /* We don't want to do this if generating a PCH. */
7529 /* Don't waste time on further processing if -fsyntax-only or we've
7530 encountered errors. */
7531 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7534 /* Close the external scope. */
7535 ext_block = pop_scope ();
7537 gcc_assert (!current_scope);
7539 /* Process all file scopes in this compilation, and the external_scope,
7540 through wrapup_global_declarations and check_global_declarations. */
7541 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7542 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7543 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7545 /* Generate functions to call static constructors and destructors
7546 for targets that do not support .ctors/.dtors sections. These
7547 functions have magic names which are detected by collect2. */
7548 build_cdtor ('I', static_ctors); static_ctors = 0;
7549 build_cdtor ('D', static_dtors); static_dtors = 0;
7551 /* We're done parsing; proceed to optimize and emit assembly.
7552 FIXME: shouldn't be the front end's responsibility to call this. */
7556 #include "gt-c-decl.h"