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)
569 objc_volatilize_decl (b->decl);
571 /* Do not climb up past the current function. */
572 if (scope->function_body)
577 /* Nonzero if we are currently in file scope. */
580 global_bindings_p (void)
582 return current_scope == file_scope && !c_override_global_bindings_to_false;
586 keep_next_level (void)
588 keep_next_level_flag = true;
591 /* Identify this scope as currently being filled with parameters. */
594 declare_parm_level (void)
596 current_scope->parm_flag = true;
602 if (next_is_function_body)
604 /* This is the transition from the parameters to the top level
605 of the function body. These are the same scope
606 (C99 6.2.1p4,6) so we do not push another scope structure.
607 next_is_function_body is set only by store_parm_decls, which
608 in turn is called when and only when we are about to
609 encounter the opening curly brace for the function body.
611 The outermost block of a function always gets a BLOCK node,
612 because the debugging output routines expect that each
613 function has at least one BLOCK. */
614 current_scope->parm_flag = false;
615 current_scope->function_body = true;
616 current_scope->keep = true;
617 current_scope->outer_function = current_function_scope;
618 current_function_scope = current_scope;
620 keep_next_level_flag = false;
621 next_is_function_body = false;
625 struct c_scope *scope;
628 scope = scope_freelist;
629 scope_freelist = scope->outer;
632 scope = GGC_CNEW (struct c_scope);
634 scope->keep = keep_next_level_flag;
635 scope->outer = current_scope;
636 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
638 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
640 if (current_scope && scope->depth == 0)
643 sorry ("GCC supports only %u nested scopes", scope->depth);
646 current_scope = scope;
647 keep_next_level_flag = false;
651 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
654 set_type_context (tree type, tree context)
656 for (type = TYPE_MAIN_VARIANT (type); type;
657 type = TYPE_NEXT_VARIANT (type))
658 TYPE_CONTEXT (type) = context;
661 /* Exit a scope. Restore the state of the identifier-decl mappings
662 that were in effect when this scope was entered. Return a BLOCK
663 node containing all the DECLs in this scope that are of interest
664 to debug info generation. */
669 struct c_scope *scope = current_scope;
670 tree block, context, p;
673 bool functionbody = scope->function_body;
674 bool keep = functionbody || scope->keep || scope->bindings;
676 c_end_vm_scope (scope->depth);
678 /* If appropriate, create a BLOCK to record the decls for the life
683 block = make_node (BLOCK);
684 BLOCK_SUBBLOCKS (block) = scope->blocks;
685 TREE_USED (block) = 1;
687 /* In each subblock, record that this is its superior. */
688 for (p = scope->blocks; p; p = TREE_CHAIN (p))
689 BLOCK_SUPERCONTEXT (p) = block;
691 BLOCK_VARS (block) = 0;
694 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
695 scope must be set so that they point to the appropriate
696 construct, i.e. either to the current FUNCTION_DECL node, or
697 else to the BLOCK node we just constructed.
699 Note that for tagged types whose scope is just the formal
700 parameter list for some function type specification, we can't
701 properly set their TYPE_CONTEXTs here, because we don't have a
702 pointer to the appropriate FUNCTION_TYPE node readily available
703 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
704 type nodes get set in `grokdeclarator' as soon as we have created
705 the FUNCTION_TYPE node which will represent the "scope" for these
706 "parameter list local" tagged types. */
707 if (scope->function_body)
708 context = current_function_decl;
709 else if (scope == file_scope)
711 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
712 TREE_CHAIN (file_decl) = all_translation_units;
713 all_translation_units = file_decl;
719 /* Clear all bindings in this scope. */
720 for (b = scope->bindings; b; b = free_binding_and_advance (b))
723 switch (TREE_CODE (p))
726 /* Warnings for unused labels, errors for undefined labels. */
727 if (TREE_USED (p) && !DECL_INITIAL (p))
729 error ("%Jlabel %qD used but not defined", p, p);
730 DECL_INITIAL (p) = error_mark_node;
732 else if (!TREE_USED (p) && warn_unused_label)
734 if (DECL_INITIAL (p))
735 warning (0, "%Jlabel %qD defined but not used", p, p);
737 warning (0, "%Jlabel %qD declared but not defined", p, p);
739 /* Labels go in BLOCK_VARS. */
740 TREE_CHAIN (p) = BLOCK_VARS (block);
741 BLOCK_VARS (block) = p;
742 gcc_assert (I_LABEL_BINDING (b->id) == b);
743 I_LABEL_BINDING (b->id) = b->shadowed;
749 set_type_context (p, context);
751 /* Types may not have tag-names, in which case the type
752 appears in the bindings list with b->id NULL. */
755 gcc_assert (I_TAG_BINDING (b->id) == b);
756 I_TAG_BINDING (b->id) = b->shadowed;
761 /* Propagate TREE_ADDRESSABLE from nested functions to their
762 containing functions. */
763 if (!TREE_ASM_WRITTEN (p)
764 && DECL_INITIAL (p) != 0
765 && TREE_ADDRESSABLE (p)
766 && DECL_ABSTRACT_ORIGIN (p) != 0
767 && DECL_ABSTRACT_ORIGIN (p) != p)
768 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
769 if (!DECL_EXTERNAL (p)
770 && DECL_INITIAL (p) == 0)
772 error ("%Jnested function %qD declared but never defined", p, p);
773 undef_nested_function = true;
778 /* Warnings for unused variables. */
779 if (warn_unused_variable
781 && !DECL_IN_SYSTEM_HEADER (p)
783 && !DECL_ARTIFICIAL (p)
784 && scope != file_scope
785 && scope != external_scope)
786 warning (0, "%Junused variable %qD", p, p);
790 error ("%Jtype of array %qD completed incompatibly with"
791 " implicit initialization", p, p);
798 /* All of these go in BLOCK_VARS, but only if this is the
799 binding in the home scope. */
802 TREE_CHAIN (p) = BLOCK_VARS (block);
803 BLOCK_VARS (block) = p;
805 /* If this is the file scope, and we are processing more
806 than one translation unit in this compilation, set
807 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
808 This makes same_translation_unit_p work, and causes
809 static declarations to be given disambiguating suffixes. */
810 if (scope == file_scope && num_in_fnames > 1)
812 DECL_CONTEXT (p) = context;
813 if (TREE_CODE (p) == TYPE_DECL)
814 set_type_context (TREE_TYPE (p), context);
818 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
819 already been put there by store_parm_decls. Unused-
820 parameter warnings are handled by function.c.
821 error_mark_node obviously does not go in BLOCK_VARS and
822 does not get unused-variable warnings. */
825 /* It is possible for a decl not to have a name. We get
826 here with b->id NULL in this case. */
829 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
830 I_SYMBOL_BINDING (b->id) = b->shadowed;
831 if (b->shadowed && b->shadowed->type)
832 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
842 /* Dispose of the block that we just made inside some higher level. */
843 if ((scope->function_body || scope == file_scope) && context)
845 DECL_INITIAL (context) = block;
846 BLOCK_SUPERCONTEXT (block) = context;
848 else if (scope->outer)
851 SCOPE_LIST_APPEND (scope->outer, blocks, block);
852 /* If we did not make a block for the scope just exited, any
853 blocks made for inner scopes must be carried forward so they
854 will later become subblocks of something else. */
855 else if (scope->blocks)
856 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
859 /* Pop the current scope, and free the structure for reuse. */
860 current_scope = scope->outer;
861 if (scope->function_body)
862 current_function_scope = scope->outer_function;
864 memset (scope, 0, sizeof (struct c_scope));
865 scope->outer = scope_freelist;
866 scope_freelist = scope;
872 push_file_scope (void)
880 file_scope = current_scope;
882 start_fname_decls ();
884 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
885 bind (DECL_NAME (decl), decl, file_scope,
886 /*invisible=*/false, /*nested=*/true);
890 pop_file_scope (void)
892 /* In case there were missing closebraces, get us back to the global
894 while (current_scope != file_scope)
897 /* __FUNCTION__ is defined at file scope (""). This
898 call may not be necessary as my tests indicate it
899 still works without it. */
900 finish_fname_decls ();
902 /* This is the point to write out a PCH if we're doing that.
903 In that case we do not want to do anything else. */
906 c_common_write_pch ();
910 /* Pop off the file scope and close this translation unit. */
914 maybe_apply_pending_pragma_weaks ();
915 cgraph_finalize_compilation_unit ();
918 /* Insert BLOCK at the end of the list of subblocks of the current
919 scope. This is used when a BIND_EXPR is expanded, to handle the
920 BLOCK node inside the BIND_EXPR. */
923 insert_block (tree block)
925 TREE_USED (block) = 1;
926 SCOPE_LIST_APPEND (current_scope, blocks, block);
929 /* Push a definition or a declaration of struct, union or enum tag "name".
930 "type" should be the type node.
931 We assume that the tag "name" is not already defined.
933 Note that the definition may really be just a forward reference.
934 In that case, the TYPE_SIZE will be zero. */
937 pushtag (tree name, tree type)
939 /* Record the identifier as the type's name if it has none. */
940 if (name && !TYPE_NAME (type))
941 TYPE_NAME (type) = name;
942 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
944 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
945 tagged type we just added to the current scope. This fake
946 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
947 to output a representation of a tagged type, and it also gives
948 us a convenient place to record the "scope start" address for the
951 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
953 /* An approximation for now, so we can tell this is a function-scope tag.
954 This will be updated in pop_scope. */
955 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
958 /* Subroutine of compare_decls. Allow harmless mismatches in return
959 and argument types provided that the type modes match. This function
960 return a unified type given a suitable match, and 0 otherwise. */
963 match_builtin_function_types (tree newtype, tree oldtype)
965 tree newrettype, oldrettype;
966 tree newargs, oldargs;
967 tree trytype, tryargs;
969 /* Accept the return type of the new declaration if same modes. */
970 oldrettype = TREE_TYPE (oldtype);
971 newrettype = TREE_TYPE (newtype);
973 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
976 oldargs = TYPE_ARG_TYPES (oldtype);
977 newargs = TYPE_ARG_TYPES (newtype);
980 while (oldargs || newargs)
984 || !TREE_VALUE (oldargs)
985 || !TREE_VALUE (newargs)
986 || TYPE_MODE (TREE_VALUE (oldargs))
987 != TYPE_MODE (TREE_VALUE (newargs)))
990 oldargs = TREE_CHAIN (oldargs);
991 newargs = TREE_CHAIN (newargs);
994 trytype = build_function_type (newrettype, tryargs);
995 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
998 /* Subroutine of diagnose_mismatched_decls. Check for function type
999 mismatch involving an empty arglist vs a nonempty one and give clearer
1002 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1003 tree newtype, tree oldtype)
1007 if (TREE_CODE (olddecl) != FUNCTION_DECL
1008 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1009 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1011 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1014 t = TYPE_ARG_TYPES (oldtype);
1016 t = TYPE_ARG_TYPES (newtype);
1017 for (; t; t = TREE_CHAIN (t))
1019 tree type = TREE_VALUE (t);
1021 if (TREE_CHAIN (t) == 0
1022 && TYPE_MAIN_VARIANT (type) != void_type_node)
1024 inform ("a parameter list with an ellipsis can%'t match "
1025 "an empty parameter name list declaration");
1029 if (c_type_promotes_to (type) != type)
1031 inform ("an argument type that has a default promotion can%'t match "
1032 "an empty parameter name list declaration");
1038 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1039 old-style function definition, NEWDECL is a prototype declaration.
1040 Diagnose inconsistencies in the argument list. Returns TRUE if
1041 the prototype is compatible, FALSE if not. */
1043 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1045 tree newargs, oldargs;
1048 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1050 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1051 newargs = TYPE_ARG_TYPES (newtype);
1056 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1057 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1059 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1062 /* Reaching the end of just one list means the two decls don't
1063 agree on the number of arguments. */
1064 if (END_OF_ARGLIST (oldargtype))
1066 error ("%Jprototype for %qD declares more arguments "
1067 "than previous old-style definition", newdecl, newdecl);
1070 else if (END_OF_ARGLIST (newargtype))
1072 error ("%Jprototype for %qD declares fewer arguments "
1073 "than previous old-style definition", newdecl, newdecl);
1077 /* Type for passing arg must be consistent with that declared
1079 else if (!comptypes (oldargtype, newargtype))
1081 error ("%Jprototype for %qD declares argument %d"
1082 " with incompatible type",
1083 newdecl, newdecl, i);
1087 oldargs = TREE_CHAIN (oldargs);
1088 newargs = TREE_CHAIN (newargs);
1092 /* If we get here, no errors were found, but do issue a warning
1093 for this poor-style construct. */
1094 warning (0, "%Jprototype for %qD follows non-prototype definition",
1097 #undef END_OF_ARGLIST
1100 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1101 first in a pair of mismatched declarations, using the diagnostic
1104 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1106 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1108 else if (DECL_INITIAL (decl))
1109 diag (N_("%Jprevious definition of %qD was here"), decl, decl);
1110 else if (C_DECL_IMPLICIT (decl))
1111 diag (N_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1113 diag (N_("%Jprevious declaration of %qD was here"), decl, decl);
1116 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1117 Returns true if the caller should proceed to merge the two, false
1118 if OLDDECL should simply be discarded. As a side effect, issues
1119 all necessary diagnostics for invalid or poor-style combinations.
1120 If it returns true, writes the types of NEWDECL and OLDDECL to
1121 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1122 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1125 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1126 tree *newtypep, tree *oldtypep)
1128 tree newtype, oldtype;
1129 bool pedwarned = false;
1130 bool warned = false;
1133 /* If we have error_mark_node for either decl or type, just discard
1134 the previous decl - we're in an error cascade already. */
1135 if (olddecl == error_mark_node || newdecl == error_mark_node)
1137 *oldtypep = oldtype = TREE_TYPE (olddecl);
1138 *newtypep = newtype = TREE_TYPE (newdecl);
1139 if (oldtype == error_mark_node || newtype == error_mark_node)
1142 /* Two different categories of symbol altogether. This is an error
1143 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1144 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1146 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1147 && DECL_BUILT_IN (olddecl)
1148 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1150 error ("%J%qD redeclared as different kind of symbol",
1152 locate_old_decl (olddecl, error);
1154 else if (TREE_PUBLIC (newdecl))
1155 warning (0, "%Jbuilt-in function %qD declared as non-function",
1158 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1159 "a built-in function", newdecl, newdecl);
1163 /* Enumerators have no linkage, so may only be declared once in a
1165 if (TREE_CODE (olddecl) == CONST_DECL)
1167 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1168 locate_old_decl (olddecl, error);
1172 if (!comptypes (oldtype, newtype))
1174 if (TREE_CODE (olddecl) == FUNCTION_DECL
1175 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1177 /* Accept harmless mismatch in function types.
1178 This is for the ffs and fprintf builtins. */
1179 tree trytype = match_builtin_function_types (newtype, oldtype);
1181 if (trytype && comptypes (newtype, trytype))
1182 *oldtypep = oldtype = trytype;
1185 /* If types don't match for a built-in, throw away the
1186 built-in. No point in calling locate_old_decl here, it
1187 won't print anything. */
1188 warning (0, "%Jconflicting types for built-in function %qD",
1193 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1194 && DECL_IS_BUILTIN (olddecl))
1196 /* A conflicting function declaration for a predeclared
1197 function that isn't actually built in. Objective C uses
1198 these. The new declaration silently overrides everything
1199 but the volatility (i.e. noreturn) indication. See also
1200 below. FIXME: Make Objective C use normal builtins. */
1201 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1204 /* Permit void foo (...) to match int foo (...) if the latter is
1205 the definition and implicit int was used. See
1206 c-torture/compile/920625-2.c. */
1207 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1208 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1209 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1210 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1212 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1213 /* Make sure we keep void as the return type. */
1214 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1215 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1218 /* Permit void foo (...) to match an earlier call to foo (...) with
1219 no declared type (thus, implicitly int). */
1220 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1221 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1222 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1223 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1225 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1226 /* Make sure we keep void as the return type. */
1227 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1232 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1233 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1235 error ("%Jconflicting types for %qD", newdecl, newdecl);
1236 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1237 locate_old_decl (olddecl, error);
1242 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1243 but silently ignore the redeclaration if either is in a system
1244 header. (Conflicting redeclarations were handled above.) */
1245 if (TREE_CODE (newdecl) == TYPE_DECL)
1247 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1248 return true; /* Allow OLDDECL to continue in use. */
1250 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1251 locate_old_decl (olddecl, error);
1255 /* Function declarations can either be 'static' or 'extern' (no
1256 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1257 can never conflict with each other on account of linkage (6.2.2p4).
1258 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1259 two definitions if one is 'extern inline' and one is not. The non-
1260 extern-inline definition supersedes the extern-inline definition. */
1261 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1263 /* If you declare a built-in function name as static, or
1264 define the built-in with an old-style definition (so we
1265 can't validate the argument list) the built-in definition is
1266 overridden, but optionally warn this was a bad choice of name. */
1267 if (DECL_BUILT_IN (olddecl)
1268 && !C_DECL_DECLARED_BUILTIN (olddecl)
1269 && (!TREE_PUBLIC (newdecl)
1270 || (DECL_INITIAL (newdecl)
1271 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1273 warning (OPT_Wshadow, "%Jdeclaration of %qD shadows "
1274 "a built-in function", newdecl, newdecl);
1275 /* Discard the old built-in function. */
1279 if (DECL_INITIAL (newdecl))
1281 if (DECL_INITIAL (olddecl))
1283 /* If both decls have extern inline and are in the same TU,
1284 reject the new decl. */
1285 if (DECL_DECLARED_INLINE_P (olddecl)
1286 && DECL_EXTERNAL (olddecl)
1287 && DECL_DECLARED_INLINE_P (newdecl)
1288 && DECL_EXTERNAL (newdecl)
1289 && same_translation_unit_p (newdecl, olddecl))
1291 error ("%Jredefinition of %qD", newdecl, newdecl);
1292 locate_old_decl (olddecl, error);
1295 /* If both decls have not extern inline, reject the new decl. */
1296 if (!DECL_DECLARED_INLINE_P (olddecl)
1297 && !DECL_EXTERNAL (olddecl)
1298 && !DECL_DECLARED_INLINE_P (newdecl)
1299 && !DECL_EXTERNAL (newdecl))
1301 error ("%Jredefinition of %qD", newdecl, newdecl);
1302 locate_old_decl (olddecl, error);
1305 /* If the new decl is declared as extern inline, error if they are
1306 in the same TU, otherwise retain the old decl. */
1307 if (!DECL_DECLARED_INLINE_P (olddecl)
1308 && !DECL_EXTERNAL (olddecl)
1309 && DECL_DECLARED_INLINE_P (newdecl)
1310 && DECL_EXTERNAL (newdecl))
1312 if (same_translation_unit_p (newdecl, olddecl))
1314 error ("%Jredefinition of %qD", newdecl, newdecl);
1315 locate_old_decl (olddecl, error);
1323 /* If we have a prototype after an old-style function definition,
1324 the argument types must be checked specially. */
1325 else if (DECL_INITIAL (olddecl)
1326 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1327 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1328 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1330 locate_old_decl (olddecl, error);
1333 /* A non-static declaration (even an "extern") followed by a
1334 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1335 The same is true for a static forward declaration at block
1336 scope followed by a non-static declaration/definition at file
1337 scope. Static followed by non-static at the same scope is
1338 not undefined behavior, and is the most convenient way to get
1339 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1340 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1341 we do diagnose it if -Wtraditional. */
1342 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1344 /* Two exceptions to the rule. If olddecl is an extern
1345 inline, or a predeclared function that isn't actually
1346 built in, newdecl silently overrides olddecl. The latter
1347 occur only in Objective C; see also above. (FIXME: Make
1348 Objective C use normal builtins.) */
1349 if (!DECL_IS_BUILTIN (olddecl)
1350 && !(DECL_EXTERNAL (olddecl)
1351 && DECL_DECLARED_INLINE_P (olddecl)))
1353 error ("%Jstatic declaration of %qD follows "
1354 "non-static declaration", newdecl, newdecl);
1355 locate_old_decl (olddecl, error);
1359 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1361 if (DECL_CONTEXT (olddecl))
1363 error ("%Jnon-static declaration of %qD follows "
1364 "static declaration", newdecl, newdecl);
1365 locate_old_decl (olddecl, error);
1368 else if (warn_traditional)
1370 warning (0, "%Jnon-static declaration of %qD follows "
1371 "static declaration", newdecl, newdecl);
1376 else if (TREE_CODE (newdecl) == VAR_DECL)
1378 /* Only variables can be thread-local, and all declarations must
1379 agree on this property. */
1380 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1382 if (DECL_THREAD_LOCAL (newdecl))
1383 error ("%Jthread-local declaration of %qD follows "
1384 "non-thread-local declaration", newdecl, newdecl);
1386 error ("%Jnon-thread-local declaration of %qD follows "
1387 "thread-local declaration", newdecl, newdecl);
1389 locate_old_decl (olddecl, error);
1393 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1394 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1396 error ("%Jredefinition of %qD", newdecl, newdecl);
1397 locate_old_decl (olddecl, error);
1401 /* Objects declared at file scope: if the first declaration had
1402 external linkage (even if it was an external reference) the
1403 second must have external linkage as well, or the behavior is
1404 undefined. If the first declaration had internal linkage, then
1405 the second must too, or else be an external reference (in which
1406 case the composite declaration still has internal linkage).
1407 As for function declarations, we warn about the static-then-
1408 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1409 if (DECL_FILE_SCOPE_P (newdecl)
1410 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1412 if (DECL_EXTERNAL (newdecl))
1414 if (!DECL_FILE_SCOPE_P (olddecl))
1416 error ("%Jextern declaration of %qD follows "
1417 "declaration with no linkage", newdecl, newdecl);
1418 locate_old_decl (olddecl, error);
1421 else if (warn_traditional)
1423 warning (0, "%Jnon-static declaration of %qD follows "
1424 "static declaration", newdecl, newdecl);
1430 if (TREE_PUBLIC (newdecl))
1431 error ("%Jnon-static declaration of %qD follows "
1432 "static declaration", newdecl, newdecl);
1434 error ("%Jstatic declaration of %qD follows "
1435 "non-static declaration", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1441 /* Two objects with the same name declared at the same block
1442 scope must both be external references (6.7p3). */
1443 else if (!DECL_FILE_SCOPE_P (newdecl))
1445 if (DECL_EXTERNAL (newdecl))
1447 /* Extern with initializer at block scope, which will
1448 already have received an error. */
1450 else if (DECL_EXTERNAL (olddecl))
1452 error ("%Jdeclaration of %qD with no linkage follows "
1453 "extern declaration", newdecl, newdecl);
1454 locate_old_decl (olddecl, error);
1458 error ("%Jredeclaration of %qD with no linkage",
1460 locate_old_decl (olddecl, error);
1468 /* All decls must agree on a visibility. */
1469 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1470 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1472 warning (0, "%Jredeclaration of %qD with different visibility "
1473 "(old visibility preserved)", newdecl, newdecl);
1477 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1479 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1480 if (DECL_DECLARED_INLINE_P (newdecl)
1481 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1483 warning (0, "%Jinline declaration of %qD follows "
1484 "declaration with attribute noinline", newdecl, newdecl);
1487 else if (DECL_DECLARED_INLINE_P (olddecl)
1488 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1490 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1491 "inline declaration ", newdecl, newdecl);
1495 /* Inline declaration after use or definition.
1496 ??? Should we still warn about this now we have unit-at-a-time
1497 mode and can get it right?
1498 Definitely don't complain if the decls are in different translation
1500 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1501 && same_translation_unit_p (olddecl, newdecl))
1503 if (TREE_USED (olddecl))
1505 warning (0, "%J%qD declared inline after being called",
1509 else if (DECL_INITIAL (olddecl))
1511 warning (0, "%J%qD declared inline after its definition",
1517 else /* PARM_DECL, VAR_DECL */
1519 /* Redeclaration of a parameter is a constraint violation (this is
1520 not explicitly stated, but follows from C99 6.7p3 [no more than
1521 one declaration of the same identifier with no linkage in the
1522 same scope, except type tags] and 6.2.2p6 [parameters have no
1523 linkage]). We must check for a forward parameter declaration,
1524 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1525 an extension, the mandatory diagnostic for which is handled by
1526 mark_forward_parm_decls. */
1528 if (TREE_CODE (newdecl) == PARM_DECL
1529 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1531 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1532 locate_old_decl (olddecl, error);
1537 /* Optional warning for completely redundant decls. */
1538 if (!warned && !pedwarned
1539 && warn_redundant_decls
1540 /* Don't warn about a function declaration followed by a
1542 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1543 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1544 /* Don't warn about redundant redeclarations of builtins. */
1545 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1546 && !DECL_BUILT_IN (newdecl)
1547 && DECL_BUILT_IN (olddecl)
1548 && !C_DECL_DECLARED_BUILTIN (olddecl))
1549 /* Don't warn about an extern followed by a definition. */
1550 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1551 /* Don't warn about forward parameter decls. */
1552 && !(TREE_CODE (newdecl) == PARM_DECL
1553 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1555 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1559 /* Report location of previous decl/defn in a consistent manner. */
1560 if (warned || pedwarned)
1561 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1566 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1567 consistent with OLDDECL, but carries new information. Merge the
1568 new information into OLDDECL. This function issues no
1572 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1574 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1575 && DECL_INITIAL (newdecl) != 0);
1576 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1577 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1578 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1579 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1581 /* For real parm decl following a forward decl, rechain the old decl
1582 in its new location and clear TREE_ASM_WRITTEN (it's not a
1583 forward decl anymore). */
1584 if (TREE_CODE (newdecl) == PARM_DECL
1585 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1587 struct c_binding *b, **here;
1589 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1590 if ((*here)->decl == olddecl)
1597 b->prev = current_scope->bindings;
1598 current_scope->bindings = b;
1600 TREE_ASM_WRITTEN (olddecl) = 0;
1603 DECL_ATTRIBUTES (newdecl)
1604 = targetm.merge_decl_attributes (olddecl, newdecl);
1606 /* Merge the data types specified in the two decls. */
1608 = TREE_TYPE (olddecl)
1609 = composite_type (newtype, oldtype);
1611 /* Lay the type out, unless already done. */
1612 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1614 if (TREE_TYPE (newdecl) != error_mark_node)
1615 layout_type (TREE_TYPE (newdecl));
1616 if (TREE_CODE (newdecl) != FUNCTION_DECL
1617 && TREE_CODE (newdecl) != TYPE_DECL
1618 && TREE_CODE (newdecl) != CONST_DECL)
1619 layout_decl (newdecl, 0);
1623 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1624 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1625 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1626 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1627 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1628 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1630 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1631 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1635 /* Keep the old rtl since we can safely use it. */
1636 COPY_DECL_RTL (olddecl, newdecl);
1638 /* Merge the type qualifiers. */
1639 if (TREE_READONLY (newdecl))
1640 TREE_READONLY (olddecl) = 1;
1642 if (TREE_THIS_VOLATILE (newdecl))
1644 TREE_THIS_VOLATILE (olddecl) = 1;
1645 if (TREE_CODE (newdecl) == VAR_DECL)
1646 make_var_volatile (newdecl);
1649 /* Merge deprecatedness. */
1650 if (TREE_DEPRECATED (newdecl))
1651 TREE_DEPRECATED (olddecl) = 1;
1653 /* Keep source location of definition rather than declaration and of
1654 prototype rather than non-prototype unless that prototype is
1656 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1657 || (old_is_prototype && !new_is_prototype
1658 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1659 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1661 /* Merge the unused-warning information. */
1662 if (DECL_IN_SYSTEM_HEADER (olddecl))
1663 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1664 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1665 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1667 /* Merge the initialization information. */
1668 if (DECL_INITIAL (newdecl) == 0)
1669 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1671 /* Merge the section attribute.
1672 We want to issue an error if the sections conflict but that must be
1673 done later in decl_attributes since we are called before attributes
1675 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1676 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1678 /* Copy the assembler name.
1679 Currently, it can only be defined in the prototype. */
1680 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1682 /* Use visibility of whichever declaration had it specified */
1683 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1685 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1686 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1689 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1691 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1692 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1693 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1694 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1695 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1696 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1697 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1698 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1699 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1700 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1703 /* Merge the storage class information. */
1704 merge_weak (newdecl, olddecl);
1706 /* For functions, static overrides non-static. */
1707 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1709 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1710 /* This is since we don't automatically
1711 copy the attributes of NEWDECL into OLDDECL. */
1712 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1713 /* If this clears `static', clear it in the identifier too. */
1714 if (!TREE_PUBLIC (olddecl))
1715 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1717 if (DECL_EXTERNAL (newdecl))
1719 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1720 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1722 /* An extern decl does not override previous storage class. */
1723 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1724 if (!DECL_EXTERNAL (newdecl))
1726 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1727 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1732 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1733 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1736 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1738 /* If we're redefining a function previously defined as extern
1739 inline, make sure we emit debug info for the inline before we
1740 throw it away, in case it was inlined into a function that hasn't
1741 been written out yet. */
1742 if (new_is_definition && DECL_INITIAL (olddecl))
1744 if (TREE_USED (olddecl)
1745 /* In unit-at-a-time mode we never inline re-defined extern
1746 inline functions. */
1747 && !flag_unit_at_a_time
1748 && cgraph_function_possibly_inlined_p (olddecl))
1749 (*debug_hooks->outlining_inline_function) (olddecl);
1751 /* The new defn must not be inline. */
1752 DECL_INLINE (newdecl) = 0;
1753 DECL_UNINLINABLE (newdecl) = 1;
1757 /* If either decl says `inline', this fn is inline,
1758 unless its definition was passed already. */
1759 if (DECL_DECLARED_INLINE_P (newdecl)
1760 || DECL_DECLARED_INLINE_P (olddecl))
1761 DECL_DECLARED_INLINE_P (newdecl) = 1;
1763 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1764 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1767 if (DECL_BUILT_IN (olddecl))
1769 /* If redeclaring a builtin function, it stays built in.
1770 But it gets tagged as having been declared. */
1771 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1772 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1773 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1774 if (new_is_prototype)
1775 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1777 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1778 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1781 /* Also preserve various other info from the definition. */
1782 if (!new_is_definition)
1784 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1785 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1786 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1787 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1788 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1790 /* Set DECL_INLINE on the declaration if we've got a body
1791 from which to instantiate. */
1792 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1794 DECL_INLINE (newdecl) = 1;
1795 DECL_ABSTRACT_ORIGIN (newdecl)
1796 = DECL_ABSTRACT_ORIGIN (olddecl);
1801 /* If a previous declaration said inline, mark the
1802 definition as inlinable. */
1803 if (DECL_DECLARED_INLINE_P (newdecl)
1804 && !DECL_UNINLINABLE (newdecl))
1805 DECL_INLINE (newdecl) = 1;
1809 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1810 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1812 unsigned olddecl_uid = DECL_UID (olddecl);
1813 tree olddecl_context = DECL_CONTEXT (olddecl);
1815 memcpy ((char *) olddecl + sizeof (struct tree_common),
1816 (char *) newdecl + sizeof (struct tree_common),
1817 sizeof (struct tree_decl) - sizeof (struct tree_common));
1818 DECL_UID (olddecl) = olddecl_uid;
1819 DECL_CONTEXT (olddecl) = olddecl_context;
1822 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1823 so that encode_section_info has a chance to look at the new decl
1824 flags and attributes. */
1825 if (DECL_RTL_SET_P (olddecl)
1826 && (TREE_CODE (olddecl) == FUNCTION_DECL
1827 || (TREE_CODE (olddecl) == VAR_DECL
1828 && TREE_STATIC (olddecl))))
1829 make_decl_rtl (olddecl);
1832 /* Handle when a new declaration NEWDECL has the same name as an old
1833 one OLDDECL in the same binding contour. Prints an error message
1836 If safely possible, alter OLDDECL to look like NEWDECL, and return
1837 true. Otherwise, return false. */
1840 duplicate_decls (tree newdecl, tree olddecl)
1842 tree newtype = NULL, oldtype = NULL;
1844 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1847 merge_decls (newdecl, olddecl, newtype, oldtype);
1852 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1854 warn_if_shadowing (tree new_decl)
1856 struct c_binding *b;
1858 /* Shadow warnings wanted? */
1860 /* No shadow warnings for internally generated vars. */
1861 || DECL_IS_BUILTIN (new_decl)
1862 /* No shadow warnings for vars made for inlining. */
1863 || DECL_FROM_INLINE (new_decl)
1864 /* Don't warn about the parm names in function declarator
1865 within a function declarator. It would be nice to avoid
1866 warning in any function declarator in a declaration, as
1867 opposed to a definition, but there is no way to tell
1868 it's not a definition at this point. */
1869 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1872 /* Is anything being shadowed? Invisible decls do not count. */
1873 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1874 if (b->decl && b->decl != new_decl && !b->invisible)
1876 tree old_decl = b->decl;
1878 if (old_decl == error_mark_node)
1880 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1881 new_decl, new_decl);
1884 else if (TREE_CODE (old_decl) == PARM_DECL)
1885 warning (0, "%Jdeclaration of %qD shadows a parameter",
1886 new_decl, new_decl);
1887 else if (DECL_FILE_SCOPE_P (old_decl))
1888 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1889 new_decl, new_decl);
1890 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1891 && DECL_BUILT_IN (old_decl))
1893 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1894 new_decl, new_decl);
1898 warning (0, "%Jdeclaration of %qD shadows a previous local",
1899 new_decl, new_decl);
1901 warning (0, "%Jshadowed declaration is here", old_decl);
1908 /* Subroutine of pushdecl.
1910 X is a TYPE_DECL for a typedef statement. Create a brand new
1911 ..._TYPE node (which will be just a variant of the existing
1912 ..._TYPE node with identical properties) and then install X
1913 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1915 The whole point here is to end up with a situation where each
1916 and every ..._TYPE node the compiler creates will be uniquely
1917 associated with AT MOST one node representing a typedef name.
1918 This way, even though the compiler substitutes corresponding
1919 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1920 early on, later parts of the compiler can always do the reverse
1921 translation and get back the corresponding typedef name. For
1924 typedef struct S MY_TYPE;
1927 Later parts of the compiler might only know that `object' was of
1928 type `struct S' if it were not for code just below. With this
1929 code however, later parts of the compiler see something like:
1931 struct S' == struct S
1932 typedef struct S' MY_TYPE;
1935 And they can then deduce (from the node for type struct S') that
1936 the original object declaration was:
1940 Being able to do this is important for proper support of protoize,
1941 and also for generating precise symbolic debugging information
1942 which takes full account of the programmer's (typedef) vocabulary.
1944 Obviously, we don't want to generate a duplicate ..._TYPE node if
1945 the TYPE_DECL node that we are now processing really represents a
1946 standard built-in type.
1948 Since all standard types are effectively declared at line zero
1949 in the source file, we can easily check to see if we are working
1950 on a standard type by checking the current value of lineno. */
1953 clone_underlying_type (tree x)
1955 if (DECL_IS_BUILTIN (x))
1957 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1958 TYPE_NAME (TREE_TYPE (x)) = x;
1960 else if (TREE_TYPE (x) != error_mark_node
1961 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1963 tree tt = TREE_TYPE (x);
1964 DECL_ORIGINAL_TYPE (x) = tt;
1965 tt = build_variant_type_copy (tt);
1967 TREE_USED (tt) = TREE_USED (x);
1972 /* Record a decl-node X as belonging to the current lexical scope.
1973 Check for errors (such as an incompatible declaration for the same
1974 name already seen in the same scope).
1976 Returns either X or an old decl for the same name.
1977 If an old decl is returned, it may have been smashed
1978 to agree with what X says. */
1983 tree name = DECL_NAME (x);
1984 struct c_scope *scope = current_scope;
1985 struct c_binding *b;
1986 bool nested = false;
1988 /* Functions need the lang_decl data. */
1989 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1990 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1992 /* Must set DECL_CONTEXT for everything not at file scope or
1993 DECL_FILE_SCOPE_P won't work. Local externs don't count
1994 unless they have initializers (which generate code). */
1995 if (current_function_decl
1996 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1997 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1998 DECL_CONTEXT (x) = current_function_decl;
2000 /* If this is of variably modified type, prevent jumping into its
2002 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2003 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2004 c_begin_vm_scope (scope->depth);
2006 /* Anonymous decls are just inserted in the scope. */
2009 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2013 /* First, see if there is another declaration with the same name in
2014 the current scope. If there is, duplicate_decls may do all the
2015 work for us. If duplicate_decls returns false, that indicates
2016 two incompatible decls in the same scope; we are to silently
2017 replace the old one (duplicate_decls has issued all appropriate
2018 diagnostics). In particular, we should not consider possible
2019 duplicates in the external scope, or shadowing. */
2020 b = I_SYMBOL_BINDING (name);
2021 if (b && B_IN_SCOPE (b, scope))
2023 struct c_binding *b_ext, *b_use;
2024 tree type = TREE_TYPE (x);
2025 tree visdecl = b->decl;
2026 tree vistype = TREE_TYPE (visdecl);
2027 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2028 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2029 b->inner_comp = false;
2032 /* If this is an external linkage declaration, we should check
2033 for compatibility with the type in the external scope before
2034 setting the type at this scope based on the visible
2035 information only. */
2036 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2038 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2039 b_ext = b_ext->shadowed;
2044 TREE_TYPE (b_use->decl) = b_use->type;
2047 if (duplicate_decls (x, b_use->decl))
2051 /* Save the updated type in the external scope and
2052 restore the proper type for this scope. */
2054 if (comptypes (vistype, type))
2055 thistype = composite_type (vistype, type);
2057 thistype = TREE_TYPE (b_use->decl);
2058 b_use->type = TREE_TYPE (b_use->decl);
2059 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2060 && DECL_BUILT_IN (b_use->decl))
2062 = build_type_attribute_variant (thistype,
2065 TREE_TYPE (b_use->decl) = thistype;
2070 goto skip_external_and_shadow_checks;
2073 /* All declarations with external linkage, and all external
2074 references, go in the external scope, no matter what scope is
2075 current. However, the binding in that scope is ignored for
2076 purposes of normal name lookup. A separate binding structure is
2077 created in the requested scope; this governs the normal
2078 visibility of the symbol.
2080 The binding in the externals scope is used exclusively for
2081 detecting duplicate declarations of the same object, no matter
2082 what scope they are in; this is what we do here. (C99 6.2.7p2:
2083 All declarations that refer to the same object or function shall
2084 have compatible type; otherwise, the behavior is undefined.) */
2085 if (DECL_EXTERNAL (x) || scope == file_scope)
2087 tree type = TREE_TYPE (x);
2090 bool type_saved = false;
2091 if (b && !B_IN_EXTERNAL_SCOPE (b)
2092 && (TREE_CODE (b->decl) == FUNCTION_DECL
2093 || TREE_CODE (b->decl) == VAR_DECL)
2094 && DECL_FILE_SCOPE_P (b->decl))
2097 vistype = TREE_TYPE (visdecl);
2099 if (warn_nested_externs
2100 && scope != file_scope
2101 && !DECL_IN_SYSTEM_HEADER (x))
2102 warning (0, "nested extern declaration of %qD", x);
2104 while (b && !B_IN_EXTERNAL_SCOPE (b))
2106 /* If this decl might be modified, save its type. This is
2107 done here rather than when the decl is first bound
2108 because the type may change after first binding, through
2109 being completed or through attributes being added. If we
2110 encounter multiple such decls, only the first should have
2111 its type saved; the others will already have had their
2112 proper types saved and the types will not have changed as
2113 their scopes will not have been re-entered. */
2114 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2116 b->type = TREE_TYPE (b->decl);
2119 if (B_IN_FILE_SCOPE (b)
2120 && TREE_CODE (b->decl) == VAR_DECL
2121 && TREE_STATIC (b->decl)
2122 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2123 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2124 && TREE_CODE (type) == ARRAY_TYPE
2125 && TYPE_DOMAIN (type)
2126 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2127 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2129 /* Array type completed in inner scope, which should be
2130 diagnosed if the completion does not have size 1 and
2131 it does not get completed in the file scope. */
2132 b->inner_comp = true;
2137 /* If a matching external declaration has been found, set its
2138 type to the composite of all the types of that declaration.
2139 After the consistency checks, it will be reset to the
2140 composite of the visible types only. */
2141 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2143 TREE_TYPE (b->decl) = b->type;
2145 /* The point of the same_translation_unit_p check here is,
2146 we want to detect a duplicate decl for a construct like
2147 foo() { extern bar(); } ... static bar(); but not if
2148 they are in different translation units. In any case,
2149 the static does not go in the externals scope. */
2151 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2152 && duplicate_decls (x, b->decl))
2157 if (comptypes (vistype, type))
2158 thistype = composite_type (vistype, type);
2160 thistype = TREE_TYPE (b->decl);
2164 b->type = TREE_TYPE (b->decl);
2165 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2167 = build_type_attribute_variant (thistype,
2168 TYPE_ATTRIBUTES (b->type));
2169 TREE_TYPE (b->decl) = thistype;
2170 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2173 else if (TREE_PUBLIC (x))
2175 if (visdecl && !b && duplicate_decls (x, visdecl))
2177 /* An external declaration at block scope referring to a
2178 visible entity with internal linkage. The composite
2179 type will already be correct for this scope, so we
2180 just need to fall through to make the declaration in
2187 bind (name, x, external_scope, /*invisible=*/true,
2194 warn_if_shadowing (x);
2196 skip_external_and_shadow_checks:
2197 if (TREE_CODE (x) == TYPE_DECL)
2198 clone_underlying_type (x);
2200 bind (name, x, scope, /*invisible=*/false, nested);
2202 /* If x's type is incomplete because it's based on a
2203 structure or union which has not yet been fully declared,
2204 attach it to that structure or union type, so we can go
2205 back and complete the variable declaration later, if the
2206 structure or union gets fully declared.
2208 If the input is erroneous, we can have error_mark in the type
2209 slot (e.g. "f(void a, ...)") - that doesn't count as an
2211 if (TREE_TYPE (x) != error_mark_node
2212 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2214 tree element = TREE_TYPE (x);
2216 while (TREE_CODE (element) == ARRAY_TYPE)
2217 element = TREE_TYPE (element);
2218 element = TYPE_MAIN_VARIANT (element);
2220 if ((TREE_CODE (element) == RECORD_TYPE
2221 || TREE_CODE (element) == UNION_TYPE)
2222 && (TREE_CODE (x) != TYPE_DECL
2223 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2224 && !COMPLETE_TYPE_P (element))
2225 C_TYPE_INCOMPLETE_VARS (element)
2226 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2231 /* Record X as belonging to file scope.
2232 This is used only internally by the Objective-C front end,
2233 and is limited to its needs. duplicate_decls is not called;
2234 if there is any preexisting decl for this identifier, it is an ICE. */
2237 pushdecl_top_level (tree x)
2240 bool nested = false;
2241 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2243 name = DECL_NAME (x);
2245 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2247 if (TREE_PUBLIC (x))
2249 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2253 bind (name, x, file_scope, /*invisible=*/false, nested);
2259 implicit_decl_warning (tree id, tree olddecl)
2261 void (*diag) (const char *, ...);
2262 switch (mesg_implicit_function_declaration)
2265 case 1: diag = warning0; break;
2266 case 2: diag = error; break;
2267 default: gcc_unreachable ();
2270 diag (N_("implicit declaration of function %qE"), id);
2272 locate_old_decl (olddecl, diag);
2275 /* Generate an implicit declaration for identifier FUNCTIONID as a
2276 function of type int (). */
2279 implicitly_declare (tree functionid)
2281 struct c_binding *b;
2285 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2287 if (B_IN_SCOPE (b, external_scope))
2296 if (decl == error_mark_node)
2299 /* FIXME: Objective-C has weird not-really-builtin functions
2300 which are supposed to be visible automatically. They wind up
2301 in the external scope because they're pushed before the file
2302 scope gets created. Catch this here and rebind them into the
2304 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2306 bind (functionid, decl, file_scope,
2307 /*invisible=*/false, /*nested=*/true);
2312 tree newtype = default_function_type;
2314 TREE_TYPE (decl) = b->type;
2315 /* Implicit declaration of a function already declared
2316 (somehow) in a different scope, or as a built-in.
2317 If this is the first time this has happened, warn;
2318 then recycle the old declaration but with the new type. */
2319 if (!C_DECL_IMPLICIT (decl))
2321 implicit_decl_warning (functionid, decl);
2322 C_DECL_IMPLICIT (decl) = 1;
2324 if (DECL_BUILT_IN (decl))
2326 newtype = build_type_attribute_variant (newtype,
2328 (TREE_TYPE (decl)));
2329 if (!comptypes (newtype, TREE_TYPE (decl)))
2331 warning (0, "incompatible implicit declaration of built-in"
2332 " function %qD", decl);
2333 newtype = TREE_TYPE (decl);
2338 if (!comptypes (newtype, TREE_TYPE (decl)))
2340 error ("incompatible implicit declaration of function %qD",
2342 locate_old_decl (decl, error);
2345 b->type = TREE_TYPE (decl);
2346 TREE_TYPE (decl) = newtype;
2347 bind (functionid, decl, current_scope,
2348 /*invisible=*/false, /*nested=*/true);
2353 /* Not seen before. */
2354 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2355 DECL_EXTERNAL (decl) = 1;
2356 TREE_PUBLIC (decl) = 1;
2357 C_DECL_IMPLICIT (decl) = 1;
2358 implicit_decl_warning (functionid, 0);
2359 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2361 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2363 /* C89 says implicit declarations are in the innermost block.
2364 So we record the decl in the standard fashion. */
2365 decl = pushdecl (decl);
2367 /* No need to call objc_check_decl here - it's a function type. */
2368 rest_of_decl_compilation (decl, 0, 0);
2370 /* Write a record describing this implicit function declaration
2371 to the prototypes file (if requested). */
2372 gen_aux_info_record (decl, 0, 1, 0);
2374 /* Possibly apply some default attributes to this implicit declaration. */
2375 decl_attributes (&decl, NULL_TREE, 0);
2380 /* Issue an error message for a reference to an undeclared variable
2381 ID, including a reference to a builtin outside of function-call
2382 context. Establish a binding of the identifier to error_mark_node
2383 in an appropriate scope, which will suppress further errors for the
2384 same identifier. The error message should be given location LOC. */
2386 undeclared_variable (tree id, location_t loc)
2388 static bool already = false;
2389 struct c_scope *scope;
2391 if (current_function_decl == 0)
2393 error ("%H%qE undeclared here (not in a function)", &loc, id);
2394 scope = current_scope;
2398 error ("%H%qE undeclared (first use in this function)", &loc, id);
2402 error ("%H(Each undeclared identifier is reported only once", &loc);
2403 error ("%Hfor each function it appears in.)", &loc);
2407 /* If we are parsing old-style parameter decls, current_function_decl
2408 will be nonnull but current_function_scope will be null. */
2409 scope = current_function_scope ? current_function_scope : current_scope;
2411 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2414 /* Subroutine of lookup_label, declare_label, define_label: construct a
2415 LABEL_DECL with all the proper frills. */
2418 make_label (tree name, location_t location)
2420 tree label = build_decl (LABEL_DECL, name, void_type_node);
2422 DECL_CONTEXT (label) = current_function_decl;
2423 DECL_MODE (label) = VOIDmode;
2424 DECL_SOURCE_LOCATION (label) = location;
2429 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2430 Create one if none exists so far for the current function.
2431 This is called when a label is used in a goto expression or
2432 has its address taken. */
2435 lookup_label (tree name)
2439 if (current_function_decl == 0)
2441 error ("label %qE referenced outside of any function", name);
2445 /* Use a label already defined or ref'd with this name, but not if
2446 it is inherited from a containing function and wasn't declared
2448 label = I_LABEL_DECL (name);
2449 if (label && (DECL_CONTEXT (label) == current_function_decl
2450 || C_DECLARED_LABEL_FLAG (label)))
2452 /* If the label has only been declared, update its apparent
2453 location to point here, for better diagnostics if it
2454 turns out not to have been defined. */
2455 if (!TREE_USED (label))
2456 DECL_SOURCE_LOCATION (label) = input_location;
2460 /* No label binding for that identifier; make one. */
2461 label = make_label (name, input_location);
2463 /* Ordinary labels go in the current function scope. */
2464 bind (name, label, current_function_scope,
2465 /*invisible=*/false, /*nested=*/false);
2469 /* Make a label named NAME in the current function, shadowing silently
2470 any that may be inherited from containing functions or containing
2471 scopes. This is called for __label__ declarations. */
2474 declare_label (tree name)
2476 struct c_binding *b = I_LABEL_BINDING (name);
2479 /* Check to make sure that the label hasn't already been declared
2481 if (b && B_IN_CURRENT_SCOPE (b))
2483 error ("duplicate label declaration %qE", name);
2484 locate_old_decl (b->decl, error);
2486 /* Just use the previous declaration. */
2490 label = make_label (name, input_location);
2491 C_DECLARED_LABEL_FLAG (label) = 1;
2493 /* Declared labels go in the current scope. */
2494 bind (name, label, current_scope,
2495 /*invisible=*/false, /*nested=*/false);
2499 /* Define a label, specifying the location in the source file.
2500 Return the LABEL_DECL node for the label, if the definition is valid.
2501 Otherwise return 0. */
2504 define_label (location_t location, tree name)
2506 /* Find any preexisting label with this name. It is an error
2507 if that label has already been defined in this function, or
2508 if there is a containing function with a declared label with
2510 tree label = I_LABEL_DECL (name);
2511 struct c_label_list *nlist_se, *nlist_vm;
2514 && ((DECL_CONTEXT (label) == current_function_decl
2515 && DECL_INITIAL (label) != 0)
2516 || (DECL_CONTEXT (label) != current_function_decl
2517 && C_DECLARED_LABEL_FLAG (label))))
2519 error ("%Hduplicate label %qD", &location, label);
2520 locate_old_decl (label, error);
2523 else if (label && DECL_CONTEXT (label) == current_function_decl)
2525 /* The label has been used or declared already in this function,
2526 but not defined. Update its location to point to this
2528 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2529 error ("%Jjump into statement expression", label);
2530 if (C_DECL_UNDEFINABLE_VM (label))
2531 error ("%Jjump into scope of identifier with variably modified type",
2533 DECL_SOURCE_LOCATION (label) = location;
2537 /* No label binding for that identifier; make one. */
2538 label = make_label (name, location);
2540 /* Ordinary labels go in the current function scope. */
2541 bind (name, label, current_function_scope,
2542 /*invisible=*/false, /*nested=*/false);
2545 if (!in_system_header && lookup_name (name))
2546 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2547 "for labels, identifier %qE conflicts", &location, name);
2549 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2550 nlist_se->next = label_context_stack_se->labels_def;
2551 nlist_se->label = label;
2552 label_context_stack_se->labels_def = nlist_se;
2554 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2555 nlist_vm->next = label_context_stack_vm->labels_def;
2556 nlist_vm->label = label;
2557 label_context_stack_vm->labels_def = nlist_vm;
2559 /* Mark label as having been defined. */
2560 DECL_INITIAL (label) = error_mark_node;
2564 /* Given NAME, an IDENTIFIER_NODE,
2565 return the structure (or union or enum) definition for that name.
2566 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2567 CODE says which kind of type the caller wants;
2568 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2569 If the wrong kind of type is found, an error is reported. */
2572 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2574 struct c_binding *b = I_TAG_BINDING (name);
2580 /* We only care about whether it's in this level if
2581 thislevel_only was set or it might be a type clash. */
2582 if (thislevel_only || TREE_CODE (b->decl) != code)
2584 /* For our purposes, a tag in the external scope is the same as
2585 a tag in the file scope. (Primarily relevant to Objective-C
2586 and its builtin structure tags, which get pushed before the
2587 file scope is created.) */
2588 if (B_IN_CURRENT_SCOPE (b)
2589 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2593 if (thislevel_only && !thislevel)
2596 if (TREE_CODE (b->decl) != code)
2598 /* Definition isn't the kind we were looking for. */
2599 pending_invalid_xref = name;
2600 pending_invalid_xref_location = input_location;
2602 /* If in the same binding level as a declaration as a tag
2603 of a different type, this must not be allowed to
2604 shadow that tag, so give the error immediately.
2605 (For example, "struct foo; union foo;" is invalid.) */
2607 pending_xref_error ();
2612 /* Print an error message now
2613 for a recent invalid struct, union or enum cross reference.
2614 We don't print them immediately because they are not invalid
2615 when used in the `struct foo;' construct for shadowing. */
2618 pending_xref_error (void)
2620 if (pending_invalid_xref != 0)
2621 error ("%H%qE defined as wrong kind of tag",
2622 &pending_invalid_xref_location, pending_invalid_xref);
2623 pending_invalid_xref = 0;
2627 /* Look up NAME in the current scope and its superiors
2628 in the namespace of variables, functions and typedefs.
2629 Return a ..._DECL node of some kind representing its definition,
2630 or return 0 if it is undefined. */
2633 lookup_name (tree name)
2635 struct c_binding *b = I_SYMBOL_BINDING (name);
2636 if (b && !b->invisible)
2641 /* Similar to `lookup_name' but look only at the indicated scope. */
2644 lookup_name_in_scope (tree name, struct c_scope *scope)
2646 struct c_binding *b;
2648 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2649 if (B_IN_SCOPE (b, scope))
2654 /* Create the predefined scalar types of C,
2655 and some nodes representing standard constants (0, 1, (void *) 0).
2656 Initialize the global scope.
2657 Make definitions for built-in primitive functions. */
2660 c_init_decl_processing (void)
2662 location_t save_loc = input_location;
2664 /* Initialize reserved words for parser. */
2667 current_function_decl = 0;
2669 gcc_obstack_init (&parser_obstack);
2671 /* Make the externals scope. */
2673 external_scope = current_scope;
2675 /* Declarations from c_common_nodes_and_builtins must not be associated
2676 with this input file, lest we get differences between using and not
2677 using preprocessed headers. */
2678 #ifdef USE_MAPPED_LOCATION
2679 input_location = BUILTINS_LOCATION;
2681 input_location.file = "<built-in>";
2682 input_location.line = 0;
2685 build_common_tree_nodes (flag_signed_char, false);
2687 c_common_nodes_and_builtins ();
2689 /* In C, comparisons and TRUTH_* expressions have type int. */
2690 truthvalue_type_node = integer_type_node;
2691 truthvalue_true_node = integer_one_node;
2692 truthvalue_false_node = integer_zero_node;
2694 /* Even in C99, which has a real boolean type. */
2695 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2696 boolean_type_node));
2698 input_location = save_loc;
2700 pedantic_lvalues = true;
2702 make_fname_decl = c_make_fname_decl;
2703 start_fname_decls ();
2706 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2707 decl, NAME is the initialization string and TYPE_DEP indicates whether
2708 NAME depended on the type of the function. As we don't yet implement
2709 delayed emission of static data, we mark the decl as emitted
2710 so it is not placed in the output. Anything using it must therefore pull
2711 out the STRING_CST initializer directly. FIXME. */
2714 c_make_fname_decl (tree id, int type_dep)
2716 const char *name = fname_as_string (type_dep);
2717 tree decl, type, init;
2718 size_t length = strlen (name);
2720 type = build_array_type (char_type_node,
2721 build_index_type (size_int (length)));
2722 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2724 decl = build_decl (VAR_DECL, id, type);
2726 TREE_STATIC (decl) = 1;
2727 TREE_READONLY (decl) = 1;
2728 DECL_ARTIFICIAL (decl) = 1;
2730 init = build_string (length + 1, name);
2731 free ((char *) name);
2732 TREE_TYPE (init) = type;
2733 DECL_INITIAL (decl) = init;
2735 TREE_USED (decl) = 1;
2737 if (current_function_decl)
2739 DECL_CONTEXT (decl) = current_function_decl;
2740 bind (id, decl, current_function_scope,
2741 /*invisible=*/false, /*nested=*/false);
2744 finish_decl (decl, init, NULL_TREE);
2749 /* Return a definition for a builtin function named NAME and whose data type
2750 is TYPE. TYPE should be a function type with argument types.
2751 FUNCTION_CODE tells later passes how to compile calls to this function.
2752 See tree.h for its possible values.
2754 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2755 the name to be called if we can't opencode the function. If
2756 ATTRS is nonzero, use that for the function's attribute list. */
2759 builtin_function (const char *name, tree type, int function_code,
2760 enum built_in_class cl, const char *library_name,
2763 tree id = get_identifier (name);
2764 tree decl = build_decl (FUNCTION_DECL, id, type);
2765 TREE_PUBLIC (decl) = 1;
2766 DECL_EXTERNAL (decl) = 1;
2767 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2768 DECL_BUILT_IN_CLASS (decl) = cl;
2769 DECL_FUNCTION_CODE (decl) = function_code;
2770 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2772 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2774 /* Should never be called on a symbol with a preexisting meaning. */
2775 gcc_assert (!I_SYMBOL_BINDING (id));
2777 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2779 /* Builtins in the implementation namespace are made visible without
2780 needing to be explicitly declared. See push_file_scope. */
2781 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2783 TREE_CHAIN (decl) = visible_builtins;
2784 visible_builtins = decl;
2787 /* Possibly apply some default attributes to this built-in function. */
2789 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2791 decl_attributes (&decl, NULL_TREE, 0);
2796 /* Called when a declaration is seen that contains no names to declare.
2797 If its type is a reference to a structure, union or enum inherited
2798 from a containing scope, shadow that tag name for the current scope
2799 with a forward reference.
2800 If its type defines a new named structure or union
2801 or defines an enum, it is valid but we need not do anything here.
2802 Otherwise, it is an error. */
2805 shadow_tag (const struct c_declspecs *declspecs)
2807 shadow_tag_warned (declspecs, 0);
2810 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2813 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2815 bool found_tag = false;
2817 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2819 tree value = declspecs->type;
2820 enum tree_code code = TREE_CODE (value);
2822 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2823 /* Used to test also that TYPE_SIZE (value) != 0.
2824 That caused warning for `struct foo;' at top level in the file. */
2826 tree name = TYPE_NAME (value);
2833 if (warned != 1 && code != ENUMERAL_TYPE)
2834 /* Empty unnamed enum OK */
2836 pedwarn ("unnamed struct/union that defines no instances");
2840 else if (!declspecs->tag_defined_p
2841 && declspecs->storage_class != csc_none)
2844 pedwarn ("empty declaration with storage class specifier "
2845 "does not redeclare tag");
2847 pending_xref_error ();
2849 else if (!declspecs->tag_defined_p
2850 && (declspecs->const_p
2851 || declspecs->volatile_p
2852 || declspecs->restrict_p))
2855 pedwarn ("empty declaration with type qualifier "
2856 "does not redeclare tag");
2858 pending_xref_error ();
2862 pending_invalid_xref = 0;
2863 t = lookup_tag (code, name, 1);
2867 t = make_node (code);
2874 if (warned != 1 && !in_system_header)
2876 pedwarn ("useless type name in empty declaration");
2881 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2883 pedwarn ("useless type name in empty declaration");
2887 pending_invalid_xref = 0;
2889 if (declspecs->inline_p)
2891 error ("%<inline%> in empty declaration");
2895 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2897 error ("%<auto%> in file-scope empty declaration");
2901 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2903 error ("%<register%> in file-scope empty declaration");
2907 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2909 warning (0, "useless storage class specifier in empty declaration");
2913 if (!warned && !in_system_header && declspecs->thread_p)
2915 warning (0, "useless %<__thread%> in empty declaration");
2919 if (!warned && !in_system_header && (declspecs->const_p
2920 || declspecs->volatile_p
2921 || declspecs->restrict_p))
2923 warning (0, "useless type qualifier in empty declaration");
2930 pedwarn ("empty declaration");
2935 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2936 bits. SPECS represents declaration specifiers that the grammar
2937 only permits to contain type qualifiers and attributes. */
2940 quals_from_declspecs (const struct c_declspecs *specs)
2942 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2943 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2944 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2945 gcc_assert (!specs->type
2946 && !specs->decl_attr
2947 && specs->typespec_word == cts_none
2948 && specs->storage_class == csc_none
2949 && !specs->typedef_p
2950 && !specs->explicit_signed_p
2951 && !specs->deprecated_p
2953 && !specs->long_long_p
2956 && !specs->unsigned_p
2957 && !specs->complex_p
2959 && !specs->thread_p);
2963 /* Construct an array declarator. EXPR is the expression inside [], or
2964 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2965 to the pointer to which a parameter array is converted). STATIC_P is
2966 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2967 is true if the array is [*], a VLA of unspecified length which is
2968 nevertheless a complete type (not currently implemented by GCC),
2969 false otherwise. The field for the contained declarator is left to be
2970 filled in by set_array_declarator_inner. */
2972 struct c_declarator *
2973 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2976 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2977 struct c_declarator);
2978 declarator->kind = cdk_array;
2979 declarator->declarator = 0;
2980 declarator->u.array.dimen = expr;
2983 declarator->u.array.attrs = quals->attrs;
2984 declarator->u.array.quals = quals_from_declspecs (quals);
2988 declarator->u.array.attrs = NULL_TREE;
2989 declarator->u.array.quals = 0;
2991 declarator->u.array.static_p = static_p;
2992 declarator->u.array.vla_unspec_p = vla_unspec_p;
2993 if (pedantic && !flag_isoc99)
2995 if (static_p || quals != NULL)
2996 pedwarn ("ISO C90 does not support %<static%> or type "
2997 "qualifiers in parameter array declarators");
2999 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3002 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3006 /* Set the contained declarator of an array declarator. DECL is the
3007 declarator, as constructed by build_array_declarator; INNER is what
3008 appears on the left of the []. ABSTRACT_P is true if it is an
3009 abstract declarator, false otherwise; this is used to reject static
3010 and type qualifiers in abstract declarators, where they are not in
3011 the C99 grammar (subject to possible change in DR#289). */
3013 struct c_declarator *
3014 set_array_declarator_inner (struct c_declarator *decl,
3015 struct c_declarator *inner, bool abstract_p)
3017 decl->declarator = inner;
3018 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3019 || decl->u.array.attrs != NULL_TREE
3020 || decl->u.array.static_p))
3021 error ("static or type qualifiers in abstract declarator");
3025 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3028 groktypename (struct c_type_name *type_name)
3031 tree attrs = type_name->specs->attrs;
3033 type_name->specs->attrs = NULL_TREE;
3035 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3038 /* Apply attributes. */
3039 decl_attributes (&type, attrs, 0);
3044 /* Decode a declarator in an ordinary declaration or data definition.
3045 This is called as soon as the type information and variable name
3046 have been parsed, before parsing the initializer if any.
3047 Here we create the ..._DECL node, fill in its type,
3048 and put it on the list of decls for the current context.
3049 The ..._DECL node is returned as the value.
3051 Exception: for arrays where the length is not specified,
3052 the type is left null, to be filled in by `finish_decl'.
3054 Function definitions do not come here; they go to start_function
3055 instead. However, external and forward declarations of functions
3056 do go through here. Structure field declarations are done by
3057 grokfield and not through here. */
3060 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3061 bool initialized, tree attributes)
3066 /* An object declared as __attribute__((deprecated)) suppresses
3067 warnings of uses of other deprecated items. */
3068 if (lookup_attribute ("deprecated", attributes))
3069 deprecated_state = DEPRECATED_SUPPRESS;
3071 decl = grokdeclarator (declarator, declspecs,
3072 NORMAL, initialized, NULL);
3076 deprecated_state = DEPRECATED_NORMAL;
3078 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3079 && MAIN_NAME_P (DECL_NAME (decl)))
3080 warning (0, "%J%qD is usually a function", decl, decl);
3083 /* Is it valid for this decl to have an initializer at all?
3084 If not, set INITIALIZED to zero, which will indirectly
3085 tell 'finish_decl' to ignore the initializer once it is parsed. */
3086 switch (TREE_CODE (decl))
3089 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3094 error ("function %qD is initialized like a variable", decl);
3099 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3100 error ("parameter %qD is initialized", decl);
3105 /* Don't allow initializations for incomplete types except for
3106 arrays which might be completed by the initialization. */
3108 /* This can happen if the array size is an undefined macro.
3109 We already gave a warning, so we don't need another one. */
3110 if (TREE_TYPE (decl) == error_mark_node)
3112 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3114 /* A complete type is ok if size is fixed. */
3116 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3117 || C_DECL_VARIABLE_SIZE (decl))
3119 error ("variable-sized object may not be initialized");
3123 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3125 error ("variable %qD has initializer but incomplete type", decl);
3128 else if (C_DECL_VARIABLE_SIZE (decl))
3130 /* Although C99 is unclear about whether incomplete arrays
3131 of VLAs themselves count as VLAs, it does not make
3132 sense to permit them to be initialized given that
3133 ordinary VLAs may not be initialized. */
3134 error ("variable-sized object may not be initialized");
3141 if (current_scope == file_scope)
3142 TREE_STATIC (decl) = 1;
3144 /* Tell 'pushdecl' this is an initialized decl
3145 even though we don't yet have the initializer expression.
3146 Also tell 'finish_decl' it may store the real initializer. */
3147 DECL_INITIAL (decl) = error_mark_node;
3150 /* If this is a function declaration, write a record describing it to the
3151 prototypes file (if requested). */
3153 if (TREE_CODE (decl) == FUNCTION_DECL)
3154 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3156 /* ANSI specifies that a tentative definition which is not merged with
3157 a non-tentative definition behaves exactly like a definition with an
3158 initializer equal to zero. (Section 3.7.2)
3160 -fno-common gives strict ANSI behavior, though this tends to break
3161 a large body of code that grew up without this rule.
3163 Thread-local variables are never common, since there's no entrenched
3164 body of code to break, and it allows more efficient variable references
3165 in the presence of dynamic linking. */
3167 if (TREE_CODE (decl) == VAR_DECL
3169 && TREE_PUBLIC (decl)
3170 && !DECL_THREAD_LOCAL (decl)
3172 DECL_COMMON (decl) = 1;
3174 /* Set attributes here so if duplicate decl, will have proper attributes. */
3175 decl_attributes (&decl, attributes, 0);
3177 if (TREE_CODE (decl) == FUNCTION_DECL
3178 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3180 struct c_declarator *ce = declarator;
3182 if (ce->kind == cdk_pointer)
3183 ce = declarator->declarator;
3184 if (ce->kind == cdk_function)
3186 tree args = ce->u.arg_info->parms;
3187 for (; args; args = TREE_CHAIN (args))
3189 tree type = TREE_TYPE (args);
3190 if (type && INTEGRAL_TYPE_P (type)
3191 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3192 DECL_ARG_TYPE (args) = integer_type_node;
3197 if (TREE_CODE (decl) == FUNCTION_DECL
3198 && DECL_DECLARED_INLINE_P (decl)
3199 && DECL_UNINLINABLE (decl)
3200 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3201 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3203 /* Add this decl to the current scope.
3204 TEM may equal DECL or it may be a previous decl of the same name. */
3205 tem = pushdecl (decl);
3207 if (initialized && DECL_EXTERNAL (tem))
3209 DECL_EXTERNAL (tem) = 0;
3210 TREE_STATIC (tem) = 1;
3216 /* Finish processing of a declaration;
3217 install its initial value.
3218 If the length of an array type is not known before,
3219 it must be determined now, from the initial value, or it is an error. */
3222 finish_decl (tree decl, tree init, tree asmspec_tree)
3224 tree type = TREE_TYPE (decl);
3225 int was_incomplete = (DECL_SIZE (decl) == 0);
3226 const char *asmspec = 0;
3228 /* If a name was specified, get the string. */
3229 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3230 && DECL_FILE_SCOPE_P (decl))
3231 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3233 asmspec = TREE_STRING_POINTER (asmspec_tree);
3235 /* If `start_decl' didn't like having an initialization, ignore it now. */
3236 if (init != 0 && DECL_INITIAL (decl) == 0)
3239 /* Don't crash if parm is initialized. */
3240 if (TREE_CODE (decl) == PARM_DECL)
3244 store_init_value (decl, init);
3246 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3247 || TREE_CODE (decl) == FUNCTION_DECL
3248 || TREE_CODE (decl) == FIELD_DECL))
3249 objc_check_decl (decl);
3251 /* Deduce size of array from initialization, if not already known. */
3252 if (TREE_CODE (type) == ARRAY_TYPE
3253 && TYPE_DOMAIN (type) == 0
3254 && TREE_CODE (decl) != TYPE_DECL)
3257 = (TREE_STATIC (decl)
3258 /* Even if pedantic, an external linkage array
3259 may have incomplete type at first. */
3260 ? pedantic && !TREE_PUBLIC (decl)
3261 : !DECL_EXTERNAL (decl));
3263 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3266 /* Get the completed type made by complete_array_type. */
3267 type = TREE_TYPE (decl);
3272 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3277 error ("%Jarray size missing in %qD", decl, decl);
3278 /* If a `static' var's size isn't known,
3279 make it extern as well as static, so it does not get
3281 If it is not `static', then do not mark extern;
3282 finish_incomplete_decl will give it a default size
3283 and it will get allocated. */
3284 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3285 DECL_EXTERNAL (decl) = 1;
3289 error ("%Jzero or negative size array %qD", decl, decl);
3293 /* For global variables, update the copy of the type that
3294 exists in the binding. */
3295 if (TREE_PUBLIC (decl))
3297 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3298 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3299 b_ext = b_ext->shadowed;
3303 b_ext->type = composite_type (b_ext->type, type);
3314 if (DECL_INITIAL (decl))
3315 TREE_TYPE (DECL_INITIAL (decl)) = type;
3317 layout_decl (decl, 0);
3320 if (TREE_CODE (decl) == VAR_DECL)
3322 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3323 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3324 layout_decl (decl, 0);
3326 if (DECL_SIZE (decl) == 0
3327 /* Don't give an error if we already gave one earlier. */
3328 && TREE_TYPE (decl) != error_mark_node
3329 && (TREE_STATIC (decl)
3330 /* A static variable with an incomplete type
3331 is an error if it is initialized.
3332 Also if it is not file scope.
3333 Otherwise, let it through, but if it is not `extern'
3334 then it may cause an error message later. */
3335 ? (DECL_INITIAL (decl) != 0
3336 || !DECL_FILE_SCOPE_P (decl))
3337 /* An automatic variable with an incomplete type
3339 : !DECL_EXTERNAL (decl)))
3341 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3342 TREE_TYPE (decl) = error_mark_node;
3345 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3346 && DECL_SIZE (decl) != 0)
3348 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3349 constant_expression_warning (DECL_SIZE (decl));
3351 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3354 if (TREE_USED (type))
3355 TREE_USED (decl) = 1;
3358 /* If this is a function and an assembler name is specified, reset DECL_RTL
3359 so we can give it its new name. Also, update built_in_decls if it
3360 was a normal built-in. */
3361 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3363 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3364 set_builtin_user_assembler_name (decl, asmspec);
3365 set_user_assembler_name (decl, asmspec);
3368 /* If #pragma weak was used, mark the decl weak now. */
3369 maybe_apply_pragma_weak (decl);
3371 /* If this is a variable definition, determine its ELF visibility. */
3372 if (TREE_CODE (decl) == VAR_DECL
3373 && TREE_STATIC (decl)
3374 && !DECL_EXTERNAL (decl))
3375 c_determine_visibility (decl);
3377 /* Output the assembler code and/or RTL code for variables and functions,
3378 unless the type is an undefined structure or union.
3379 If not, it will get done when the type is completed. */
3381 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3383 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3384 if (c_dialect_objc ())
3385 objc_check_decl (decl);
3389 /* If this is not a static variable, issue a warning.
3390 It doesn't make any sense to give an ASMSPEC for an
3391 ordinary, non-register local variable. Historically,
3392 GCC has accepted -- but ignored -- the ASMSPEC in
3394 if (!DECL_FILE_SCOPE_P (decl)
3395 && TREE_CODE (decl) == VAR_DECL
3396 && !C_DECL_REGISTER (decl)
3397 && !TREE_STATIC (decl))
3398 warning (0, "%Jignoring asm-specifier for non-static local "
3399 "variable %qD", decl, decl);
3400 else if (C_DECL_REGISTER (decl))
3401 change_decl_assembler_name (decl, get_identifier (asmspec));
3403 set_user_assembler_name (decl, asmspec);
3406 if (DECL_FILE_SCOPE_P (decl))
3408 if (DECL_INITIAL (decl) == NULL_TREE
3409 || DECL_INITIAL (decl) == error_mark_node)
3410 /* Don't output anything
3411 when a tentative file-scope definition is seen.
3412 But at end of compilation, do output code for them. */
3413 DECL_DEFER_OUTPUT (decl) = 1;
3414 rest_of_decl_compilation (decl, true, 0);
3418 /* In conjunction with an ASMSPEC, the `register'
3419 keyword indicates that we should place the variable
3420 in a particular register. */
3421 if (asmspec && C_DECL_REGISTER (decl))
3423 DECL_HARD_REGISTER (decl) = 1;
3424 /* This cannot be done for a structure with volatile
3425 fields, on which DECL_REGISTER will have been
3427 if (!DECL_REGISTER (decl))
3428 error ("cannot put object with volatile field into register");
3431 if (TREE_CODE (decl) != FUNCTION_DECL)
3433 /* If we're building a variable sized type, and we might be
3434 reachable other than via the top of the current binding
3435 level, then create a new BIND_EXPR so that we deallocate
3436 the object at the right time. */
3437 /* Note that DECL_SIZE can be null due to errors. */
3438 if (DECL_SIZE (decl)
3439 && !TREE_CONSTANT (DECL_SIZE (decl))
3440 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3443 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3444 TREE_SIDE_EFFECTS (bind) = 1;
3446 BIND_EXPR_BODY (bind) = push_stmt_list ();
3448 add_stmt (build_stmt (DECL_EXPR, decl));
3453 if (!DECL_FILE_SCOPE_P (decl))
3455 /* Recompute the RTL of a local array now
3456 if it used to be an incomplete type. */
3458 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3460 /* If we used it already as memory, it must stay in memory. */
3461 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3462 /* If it's still incomplete now, no init will save it. */
3463 if (DECL_SIZE (decl) == 0)
3464 DECL_INITIAL (decl) = 0;
3469 /* If this was marked 'used', be sure it will be output. */
3470 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3471 mark_decl_referenced (decl);
3473 if (TREE_CODE (decl) == TYPE_DECL)
3475 if (!DECL_FILE_SCOPE_P (decl)
3476 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3477 add_stmt (build_stmt (DECL_EXPR, decl));
3479 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3482 /* At the end of a declaration, throw away any variable type sizes
3483 of types defined inside that declaration. There is no use
3484 computing them in the following function definition. */
3485 if (current_scope == file_scope)
3486 get_pending_sizes ();
3488 /* Install a cleanup (aka destructor) if one was given. */
3489 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3491 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3494 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3495 tree cleanup_decl = lookup_name (cleanup_id);
3498 /* Build "cleanup(&decl)" for the destructor. */
3499 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3500 cleanup = build_tree_list (NULL_TREE, cleanup);
3501 cleanup = build_function_call (cleanup_decl, cleanup);
3503 /* Don't warn about decl unused; the cleanup uses it. */
3504 TREE_USED (decl) = 1;
3505 TREE_USED (cleanup_decl) = 1;
3507 /* Initialize EH, if we've been told to do so. */
3508 if (flag_exceptions && !c_eh_initialized_p)
3510 c_eh_initialized_p = true;
3511 eh_personality_libfunc
3512 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3513 ? "__gcc_personality_sj0"
3514 : "__gcc_personality_v0");
3515 using_eh_for_cleanups ();
3518 push_cleanup (decl, cleanup, false);
3523 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3526 grokparm (const struct c_parm *parm)
3528 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3531 decl_attributes (&decl, parm->attrs, 0);
3536 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3537 and push that on the current scope. */
3540 push_parm_decl (const struct c_parm *parm)
3544 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3545 decl_attributes (&decl, parm->attrs, 0);
3547 decl = pushdecl (decl);
3549 finish_decl (decl, NULL_TREE, NULL_TREE);
3552 /* Mark all the parameter declarations to date as forward decls.
3553 Also diagnose use of this extension. */
3556 mark_forward_parm_decls (void)
3558 struct c_binding *b;
3560 if (pedantic && !current_scope->warned_forward_parm_decls)
3562 pedwarn ("ISO C forbids forward parameter declarations");
3563 current_scope->warned_forward_parm_decls = true;
3566 for (b = current_scope->bindings; b; b = b->prev)
3567 if (TREE_CODE (b->decl) == PARM_DECL)
3568 TREE_ASM_WRITTEN (b->decl) = 1;
3571 static GTY(()) int compound_literal_number;
3573 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3574 literal, which may be an incomplete array type completed by the
3575 initializer; INIT is a CONSTRUCTOR that initializes the compound
3579 build_compound_literal (tree type, tree init)
3581 /* We do not use start_decl here because we have a type, not a declarator;
3582 and do not use finish_decl because the decl should be stored inside
3583 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3588 if (type == error_mark_node)
3589 return error_mark_node;
3591 decl = build_decl (VAR_DECL, NULL_TREE, type);
3592 DECL_EXTERNAL (decl) = 0;
3593 TREE_PUBLIC (decl) = 0;
3594 TREE_STATIC (decl) = (current_scope == file_scope);
3595 DECL_CONTEXT (decl) = current_function_decl;
3596 TREE_USED (decl) = 1;
3597 TREE_TYPE (decl) = type;
3598 TREE_READONLY (decl) = TYPE_READONLY (type);
3599 store_init_value (decl, init);
3601 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3603 int failure = complete_array_type (&TREE_TYPE (decl),
3604 DECL_INITIAL (decl), true);
3605 gcc_assert (!failure);
3607 type = TREE_TYPE (decl);
3608 TREE_TYPE (DECL_INITIAL (decl)) = type;
3611 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3612 return error_mark_node;
3614 stmt = build_stmt (DECL_EXPR, decl);
3615 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3616 TREE_SIDE_EFFECTS (complit) = 1;
3618 layout_decl (decl, 0);
3620 if (TREE_STATIC (decl))
3622 /* This decl needs a name for the assembler output. We also need
3623 a unique suffix to be added to the name. */
3626 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3627 compound_literal_number);
3628 compound_literal_number++;
3629 DECL_NAME (decl) = get_identifier (name);
3630 DECL_DEFER_OUTPUT (decl) = 1;
3631 DECL_COMDAT (decl) = 1;
3632 DECL_ARTIFICIAL (decl) = 1;
3633 DECL_IGNORED_P (decl) = 1;
3635 rest_of_decl_compilation (decl, 1, 0);
3641 /* Determine whether TYPE is a structure with a flexible array member,
3642 or a union containing such a structure (possibly recursively). */
3645 flexible_array_type_p (tree type)
3648 switch (TREE_CODE (type))
3651 x = TYPE_FIELDS (type);
3654 while (TREE_CHAIN (x) != NULL_TREE)
3656 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3657 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3658 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3659 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3663 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3665 if (flexible_array_type_p (TREE_TYPE (x)))
3674 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3675 replacing with appropriate values if they are invalid. */
3677 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3680 unsigned int max_width;
3681 unsigned HOST_WIDE_INT w;
3682 const char *name = orig_name ? orig_name: _("<anonymous>");
3684 /* Detect and ignore out of range field width and process valid
3686 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3687 || TREE_CODE (*width) != INTEGER_CST)
3689 error ("bit-field %qs width not an integer constant", name);
3690 *width = integer_one_node;
3694 constant_expression_warning (*width);
3695 if (tree_int_cst_sgn (*width) < 0)
3697 error ("negative width in bit-field %qs", name);
3698 *width = integer_one_node;
3700 else if (integer_zerop (*width) && orig_name)
3702 error ("zero width for bit-field %qs", name);
3703 *width = integer_one_node;
3707 /* Detect invalid bit-field type. */
3708 if (TREE_CODE (*type) != INTEGER_TYPE
3709 && TREE_CODE (*type) != BOOLEAN_TYPE
3710 && TREE_CODE (*type) != ENUMERAL_TYPE)
3712 error ("bit-field %qs has invalid type", name);
3713 *type = unsigned_type_node;
3716 type_mv = TYPE_MAIN_VARIANT (*type);
3718 && type_mv != integer_type_node
3719 && type_mv != unsigned_type_node
3720 && type_mv != boolean_type_node)
3721 pedwarn ("type of bit-field %qs is a GCC extension", name);
3723 if (type_mv == boolean_type_node)
3724 max_width = CHAR_TYPE_SIZE;
3726 max_width = TYPE_PRECISION (*type);
3728 if (0 < compare_tree_int (*width, max_width))
3730 error ("width of %qs exceeds its type", name);
3732 *width = build_int_cst (NULL_TREE, w);
3735 w = tree_low_cst (*width, 1);
3737 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3739 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3741 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3742 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3743 warning (0, "%qs is narrower than values of its type", name);
3747 /* Given declspecs and a declarator,
3748 determine the name and type of the object declared
3749 and construct a ..._DECL node for it.
3750 (In one case we can return a ..._TYPE node instead.
3751 For invalid input we sometimes return 0.)
3753 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3755 DECL_CONTEXT says which syntactic context this declaration is in:
3756 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3757 FUNCDEF for a function definition. Like NORMAL but a few different
3758 error messages in each case. Return value may be zero meaning
3759 this definition is too screwy to try to parse.
3760 PARM for a parameter declaration (either within a function prototype
3761 or before a function body). Make a PARM_DECL, or return void_type_node.
3762 TYPENAME if for a typename (in a cast or sizeof).
3763 Don't make a DECL node; just return the ..._TYPE node.
3764 FIELD for a struct or union field; make a FIELD_DECL.
3765 INITIALIZED is true if the decl has an initializer.
3766 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3767 representing the width of the bit-field.
3769 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3770 It may also be so in the PARM case, for a prototype where the
3771 argument type is specified but not the name.
3773 This function is where the complicated C meanings of `static'
3774 and `extern' are interpreted. */
3777 grokdeclarator (const struct c_declarator *declarator,
3778 struct c_declspecs *declspecs,
3779 enum decl_context decl_context, bool initialized, tree *width)
3781 tree type = declspecs->type;
3782 bool threadp = declspecs->thread_p;
3783 enum c_storage_class storage_class = declspecs->storage_class;
3787 int type_quals = TYPE_UNQUALIFIED;
3788 const char *name, *orig_name;
3789 tree typedef_type = 0;
3790 int funcdef_flag = 0;
3791 bool funcdef_syntax = false;
3792 int size_varies = 0;
3793 tree decl_attr = declspecs->decl_attr;
3794 int array_ptr_quals = TYPE_UNQUALIFIED;
3795 tree array_ptr_attrs = NULL_TREE;
3796 int array_parm_static = 0;
3797 tree returned_attrs = NULL_TREE;
3798 bool bitfield = width != NULL;
3800 struct c_arg_info *arg_info = 0;
3802 if (decl_context == FUNCDEF)
3803 funcdef_flag = 1, decl_context = NORMAL;
3805 /* Look inside a declarator for the name being declared
3806 and get it as a string, for an error message. */
3808 const struct c_declarator *decl = declarator;
3817 funcdef_syntax = (decl->kind == cdk_function);
3818 decl = decl->declarator;
3822 decl = decl->declarator;
3827 name = IDENTIFIER_POINTER (decl->u.id);
3839 /* A function definition's declarator must have the form of
3840 a function declarator. */
3842 if (funcdef_flag && !funcdef_syntax)
3845 /* If this looks like a function definition, make it one,
3846 even if it occurs where parms are expected.
3847 Then store_parm_decls will reject it and not use it as a parm. */
3848 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3849 decl_context = PARM;
3851 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3852 warn_deprecated_use (declspecs->type);
3854 typedef_type = type;
3855 size_varies = C_TYPE_VARIABLE_SIZE (type);
3857 /* Diagnose defaulting to "int". */
3859 if (declspecs->default_int_p && !in_system_header)
3861 /* Issue a warning if this is an ISO C 99 program or if
3862 -Wreturn-type and this is a function, or if -Wimplicit;
3863 prefer the former warning since it is more explicit. */
3864 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3866 warn_about_return_type = 1;
3867 else if (warn_implicit_int || flag_isoc99)
3868 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3871 /* Adjust the type if a bit-field is being declared,
3872 -funsigned-bitfields applied and the type is not explicitly
3874 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3875 && TREE_CODE (type) == INTEGER_TYPE)
3876 type = c_common_unsigned_type (type);
3878 /* Figure out the type qualifiers for the declaration. There are
3879 two ways a declaration can become qualified. One is something
3880 like `const int i' where the `const' is explicit. Another is
3881 something like `typedef const int CI; CI i' where the type of the
3882 declaration contains the `const'. A third possibility is that
3883 there is a type qualifier on the element type of a typedefed
3884 array type, in which case we should extract that qualifier so
3885 that c_apply_type_quals_to_decls receives the full list of
3886 qualifiers to work with (C90 is not entirely clear about whether
3887 duplicate qualifiers should be diagnosed in this case, but it
3888 seems most appropriate to do so). */
3889 element_type = strip_array_types (type);
3890 constp = declspecs->const_p + TYPE_READONLY (element_type);
3891 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3892 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3893 if (pedantic && !flag_isoc99)
3896 pedwarn ("duplicate %<const%>");
3898 pedwarn ("duplicate %<restrict%>");
3900 pedwarn ("duplicate %<volatile%>");
3902 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3903 type = TYPE_MAIN_VARIANT (type);
3904 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3905 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3906 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3908 /* Warn about storage classes that are invalid for certain
3909 kinds of declarations (parameters, typenames, etc.). */
3913 || storage_class == csc_auto
3914 || storage_class == csc_register
3915 || storage_class == csc_typedef))
3917 if (storage_class == csc_auto
3918 && (pedantic || current_scope == file_scope))
3919 pedwarn ("function definition declared %<auto%>");
3920 if (storage_class == csc_register)
3921 error ("function definition declared %<register%>");
3922 if (storage_class == csc_typedef)
3923 error ("function definition declared %<typedef%>");
3925 error ("function definition declared %<__thread%>");
3927 if (storage_class == csc_auto
3928 || storage_class == csc_register
3929 || storage_class == csc_typedef)
3930 storage_class = csc_none;
3932 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3934 if (decl_context == PARM && storage_class == csc_register)
3938 switch (decl_context)
3941 error ("storage class specified for structure field %qs",
3945 error ("storage class specified for parameter %qs", name);
3948 error ("storage class specified for typename");
3951 storage_class = csc_none;
3955 else if (storage_class == csc_extern
3959 /* 'extern' with initialization is invalid if not at file scope. */
3960 if (current_scope == file_scope)
3961 warning (0, "%qs initialized and declared %<extern%>", name);
3963 error ("%qs has both %<extern%> and initializer", name);
3965 else if (current_scope == file_scope)
3967 if (storage_class == csc_auto)
3968 error ("file-scope declaration of %qs specifies %<auto%>", name);
3969 if (pedantic && storage_class == csc_register)
3970 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3974 if (storage_class == csc_extern && funcdef_flag)
3975 error ("nested function %qs declared %<extern%>", name);
3976 else if (threadp && storage_class == csc_none)
3978 error ("function-scope %qs implicitly auto and declared "
3985 /* Now figure out the structure of the declarator proper.
3986 Descend through it, creating more complex types, until we reach
3987 the declared identifier (or NULL_TREE, in an absolute declarator).
3988 At each stage we maintain an unqualified version of the type
3989 together with any qualifiers that should be applied to it with
3990 c_build_qualified_type; this way, array types including
3991 multidimensional array types are first built up in unqualified
3992 form and then the qualified form is created with
3993 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3995 while (declarator && declarator->kind != cdk_id)
3997 if (type == error_mark_node)
3999 declarator = declarator->declarator;
4003 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4004 a cdk_pointer (for *...),
4005 a cdk_function (for ...(...)),
4006 a cdk_attrs (for nested attributes),
4007 or a cdk_id (for the name being declared
4008 or the place in an absolute declarator
4009 where the name was omitted).
4010 For the last case, we have just exited the loop.
4012 At this point, TYPE is the type of elements of an array,
4013 or for a function to return, or for a pointer to point to.
4014 After this sequence of ifs, TYPE is the type of the
4015 array or function or pointer, and DECLARATOR has had its
4016 outermost layer removed. */
4018 if (array_ptr_quals != TYPE_UNQUALIFIED
4019 || array_ptr_attrs != NULL_TREE
4020 || array_parm_static)
4022 /* Only the innermost declarator (making a parameter be of
4023 array type which is converted to pointer type)
4024 may have static or type qualifiers. */
4025 error ("static or type qualifiers in non-parameter array declarator");
4026 array_ptr_quals = TYPE_UNQUALIFIED;
4027 array_ptr_attrs = NULL_TREE;
4028 array_parm_static = 0;
4031 switch (declarator->kind)
4035 /* A declarator with embedded attributes. */
4036 tree attrs = declarator->u.attrs;
4037 const struct c_declarator *inner_decl;
4039 declarator = declarator->declarator;
4040 inner_decl = declarator;
4041 while (inner_decl->kind == cdk_attrs)
4042 inner_decl = inner_decl->declarator;
4043 if (inner_decl->kind == cdk_id)
4044 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4045 else if (inner_decl->kind == cdk_function)
4046 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4047 else if (inner_decl->kind == cdk_array)
4048 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4049 returned_attrs = decl_attributes (&type,
4050 chainon (returned_attrs, attrs),
4056 tree itype = NULL_TREE;
4057 tree size = declarator->u.array.dimen;
4058 /* The index is a signed object `sizetype' bits wide. */
4059 tree index_type = c_common_signed_type (sizetype);
4061 array_ptr_quals = declarator->u.array.quals;
4062 array_ptr_attrs = declarator->u.array.attrs;
4063 array_parm_static = declarator->u.array.static_p;
4065 declarator = declarator->declarator;
4067 /* Check for some types that there cannot be arrays of. */
4069 if (VOID_TYPE_P (type))
4071 error ("declaration of %qs as array of voids", name);
4072 type = error_mark_node;
4075 if (TREE_CODE (type) == FUNCTION_TYPE)
4077 error ("declaration of %qs as array of functions", name);
4078 type = error_mark_node;
4081 if (pedantic && !in_system_header && flexible_array_type_p (type))
4082 pedwarn ("invalid use of structure with flexible array member");
4084 if (size == error_mark_node)
4085 type = error_mark_node;
4087 if (type == error_mark_node)
4090 /* If size was specified, set ITYPE to a range-type for
4091 that size. Otherwise, ITYPE remains null. finish_decl
4092 may figure it out from an initial value. */
4096 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4098 STRIP_TYPE_NOPS (size);
4100 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4102 error ("size of array %qs has non-integer type", name);
4103 size = integer_one_node;
4106 if (pedantic && integer_zerop (size))
4107 pedwarn ("ISO C forbids zero-size array %qs", name);
4109 if (TREE_CODE (size) == INTEGER_CST)
4111 constant_expression_warning (size);
4112 if (tree_int_cst_sgn (size) < 0)
4114 error ("size of array %qs is negative", name);
4115 size = integer_one_node;
4120 /* Make sure the array size remains visibly
4121 nonconstant even if it is (eg) a const variable
4122 with known value. */
4125 if (!flag_isoc99 && pedantic)
4127 if (TREE_CONSTANT (size))
4128 pedwarn ("ISO C90 forbids array %qs whose size "
4129 "can%'t be evaluated",
4132 pedwarn ("ISO C90 forbids variable-size array %qs",
4137 if (integer_zerop (size))
4139 /* A zero-length array cannot be represented with
4140 an unsigned index type, which is what we'll
4141 get with build_index_type. Create an
4142 open-ended range instead. */
4143 itype = build_range_type (sizetype, size, NULL_TREE);
4147 /* Arrange for the SAVE_EXPR on the inside of the
4148 MINUS_EXPR, which allows the -1 to get folded
4149 with the +1 that happens when building TYPE_SIZE. */
4151 size = variable_size (size);
4153 /* Compute the maximum valid index, that is, size
4154 - 1. Do the calculation in index_type, so that
4155 if it is a variable the computations will be
4156 done in the proper mode. */
4157 itype = fold (build2 (MINUS_EXPR, index_type,
4158 convert (index_type, size),
4159 convert (index_type,
4162 /* If that overflowed, the array is too big. ???
4163 While a size of INT_MAX+1 technically shouldn't
4164 cause an overflow (because we subtract 1), the
4165 overflow is recorded during the conversion to
4166 index_type, before the subtraction. Handling
4167 this case seems like an unnecessary
4169 if (TREE_OVERFLOW (itype))
4171 error ("size of array %qs is too large", name);
4172 type = error_mark_node;
4176 itype = build_index_type (itype);
4179 else if (decl_context == FIELD)
4181 if (pedantic && !flag_isoc99 && !in_system_header)
4182 pedwarn ("ISO C90 does not support flexible array members");
4184 /* ISO C99 Flexible array members are effectively
4185 identical to GCC's zero-length array extension. */
4186 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4189 /* Complain about arrays of incomplete types. */
4190 if (!COMPLETE_TYPE_P (type))
4192 error ("array type has incomplete element type");
4193 type = error_mark_node;
4196 type = build_array_type (type, itype);
4199 C_TYPE_VARIABLE_SIZE (type) = 1;
4201 /* The GCC extension for zero-length arrays differs from
4202 ISO flexible array members in that sizeof yields
4204 if (size && integer_zerop (size))
4206 TYPE_SIZE (type) = bitsize_zero_node;
4207 TYPE_SIZE_UNIT (type) = size_zero_node;
4210 if (decl_context != PARM
4211 && (array_ptr_quals != TYPE_UNQUALIFIED
4212 || array_ptr_attrs != NULL_TREE
4213 || array_parm_static))
4215 error ("static or type qualifiers in non-parameter array declarator");
4216 array_ptr_quals = TYPE_UNQUALIFIED;
4217 array_ptr_attrs = NULL_TREE;
4218 array_parm_static = 0;
4224 /* Say it's a definition only for the declarator closest
4225 to the identifier, apart possibly from some
4227 bool really_funcdef = false;
4231 const struct c_declarator *t = declarator->declarator;
4232 while (t->kind == cdk_attrs)
4234 really_funcdef = (t->kind == cdk_id);
4237 /* Declaring a function type. Make sure we have a valid
4238 type for the function to return. */
4239 if (type == error_mark_node)
4244 /* Warn about some types functions can't return. */
4245 if (TREE_CODE (type) == FUNCTION_TYPE)
4247 error ("%qs declared as function returning a function", name);
4248 type = integer_type_node;
4250 if (TREE_CODE (type) == ARRAY_TYPE)
4252 error ("%qs declared as function returning an array", name);
4253 type = integer_type_node;
4256 /* Construct the function type and go to the next
4257 inner layer of declarator. */
4258 arg_info = declarator->u.arg_info;
4259 arg_types = grokparms (arg_info, really_funcdef);
4261 /* Type qualifiers before the return type of the function
4262 qualify the return type, not the function type. */
4265 /* Type qualifiers on a function return type are
4266 normally permitted by the standard but have no
4267 effect, so give a warning at -Wreturn-type.
4268 Qualifiers on a void return type are banned on
4269 function definitions in ISO C; GCC used to used
4270 them for noreturn functions. */
4271 if (VOID_TYPE_P (type) && really_funcdef)
4272 pedwarn ("function definition has qualified void return type");
4274 warning (OPT_Wreturn_type,
4275 "type qualifiers ignored on function return type");
4277 type = c_build_qualified_type (type, type_quals);
4279 type_quals = TYPE_UNQUALIFIED;
4281 type = build_function_type (type, arg_types);
4282 declarator = declarator->declarator;
4284 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4285 the formal parameter list of this FUNCTION_TYPE to point to
4286 the FUNCTION_TYPE node itself. */
4290 for (link = arg_info->tags;
4292 link = TREE_CHAIN (link))
4293 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4299 /* Merge any constancy or volatility into the target type
4302 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4304 pedwarn ("ISO C forbids qualified function types");
4306 type = c_build_qualified_type (type, type_quals);
4309 type = build_pointer_type (type);
4311 /* Process type qualifiers (such as const or volatile)
4312 that were given inside the `*'. */
4313 type_quals = declarator->u.pointer_quals;
4315 declarator = declarator->declarator;
4323 /* Now TYPE has the actual type, apart from any qualifiers in
4326 /* Check the type and width of a bit-field. */
4328 check_bitfield_type_and_width (&type, width, orig_name);
4330 /* Did array size calculations overflow? */
4332 if (TREE_CODE (type) == ARRAY_TYPE
4333 && COMPLETE_TYPE_P (type)
4334 && TREE_OVERFLOW (TYPE_SIZE (type)))
4336 error ("size of array %qs is too large", name);
4337 /* If we proceed with the array type as it is, we'll eventually
4338 crash in tree_low_cst(). */
4339 type = error_mark_node;
4342 /* If this is declaring a typedef name, return a TYPE_DECL. */
4344 if (storage_class == csc_typedef)
4347 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4349 pedwarn ("ISO C forbids qualified function types");
4351 type = c_build_qualified_type (type, type_quals);
4352 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4353 if (declspecs->explicit_signed_p)
4354 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4355 decl_attributes (&decl, returned_attrs, 0);
4356 if (declspecs->inline_p)
4357 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4361 /* Detect the case of an array type of unspecified size
4362 which came, as such, direct from a typedef name.
4363 We must copy the type, so that each identifier gets
4364 a distinct type, so that each identifier's size can be
4365 controlled separately by its own initializer. */
4367 if (type != 0 && typedef_type != 0
4368 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4369 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4371 type = build_array_type (TREE_TYPE (type), 0);
4373 C_TYPE_VARIABLE_SIZE (type) = 1;
4376 /* If this is a type name (such as, in a cast or sizeof),
4377 compute the type and return it now. */
4379 if (decl_context == TYPENAME)
4381 /* Note that the grammar rejects storage classes in typenames
4383 gcc_assert (storage_class == csc_none && !threadp
4384 && !declspecs->inline_p);
4385 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4387 pedwarn ("ISO C forbids const or volatile function types");
4389 type = c_build_qualified_type (type, type_quals);
4390 decl_attributes (&type, returned_attrs, 0);
4394 /* Aside from typedefs and type names (handle above),
4395 `void' at top level (not within pointer)
4396 is allowed only in public variables.
4397 We don't complain about parms either, but that is because
4398 a better error message can be made later. */
4400 if (VOID_TYPE_P (type) && decl_context != PARM
4401 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4402 && (storage_class == csc_extern
4403 || (current_scope == file_scope
4404 && !(storage_class == csc_static
4405 || storage_class == csc_register)))))
4407 error ("variable or field %qs declared void", name);
4408 type = integer_type_node;
4411 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4412 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4417 if (decl_context == PARM)
4419 tree type_as_written;
4422 /* A parameter declared as an array of T is really a pointer to T.
4423 One declared as a function is really a pointer to a function. */
4425 if (TREE_CODE (type) == ARRAY_TYPE)
4427 /* Transfer const-ness of array into that of type pointed to. */
4428 type = TREE_TYPE (type);
4430 type = c_build_qualified_type (type, type_quals);
4431 type = build_pointer_type (type);
4432 type_quals = array_ptr_quals;
4434 /* We don't yet implement attributes in this context. */
4435 if (array_ptr_attrs != NULL_TREE)
4436 warning (0, "attributes in parameter array declarator ignored");
4440 else if (TREE_CODE (type) == FUNCTION_TYPE)
4442 if (pedantic && type_quals)
4443 pedwarn ("ISO C forbids qualified function types");
4445 type = c_build_qualified_type (type, type_quals);
4446 type = build_pointer_type (type);
4447 type_quals = TYPE_UNQUALIFIED;
4449 else if (type_quals)
4450 type = c_build_qualified_type (type, type_quals);
4452 type_as_written = type;
4454 decl = build_decl (PARM_DECL, declarator->u.id, type);
4456 C_DECL_VARIABLE_SIZE (decl) = 1;
4458 /* Compute the type actually passed in the parmlist,
4459 for the case where there is no prototype.
4460 (For example, shorts and chars are passed as ints.)
4461 When there is a prototype, this is overridden later. */
4463 if (type == error_mark_node)
4464 promoted_type = type;
4466 promoted_type = c_type_promotes_to (type);
4468 DECL_ARG_TYPE (decl) = promoted_type;
4469 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4470 if (declspecs->inline_p)
4471 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4473 else if (decl_context == FIELD)
4475 /* Note that the grammar rejects storage classes in typenames
4477 gcc_assert (storage_class == csc_none && !threadp
4478 && !declspecs->inline_p);
4480 /* Structure field. It may not be a function. */
4482 if (TREE_CODE (type) == FUNCTION_TYPE)
4484 error ("field %qs declared as a function", name);
4485 type = build_pointer_type (type);
4487 else if (TREE_CODE (type) != ERROR_MARK
4488 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4490 error ("field %qs has incomplete type", name);
4491 type = error_mark_node;
4493 type = c_build_qualified_type (type, type_quals);
4494 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4495 DECL_NONADDRESSABLE_P (decl) = bitfield;
4498 C_DECL_VARIABLE_SIZE (decl) = 1;
4500 else if (TREE_CODE (type) == FUNCTION_TYPE)
4502 if (storage_class == csc_register || threadp)
4504 error ("invalid storage class for function %qs", name);
4506 else if (current_scope != file_scope)
4508 /* Function declaration not at file scope. Storage
4509 classes other than `extern' are not allowed, C99
4510 6.7.1p5, and `extern' makes no difference. However,
4511 GCC allows 'auto', perhaps with 'inline', to support
4512 nested functions. */
4513 if (storage_class == csc_auto)
4516 pedwarn ("invalid storage class for function %qs", name);
4518 else if (storage_class == csc_static)
4520 error ("invalid storage class for function %qs", name);
4522 storage_class = declspecs->storage_class = csc_none;
4528 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4529 decl = build_decl_attribute_variant (decl, decl_attr);
4531 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4533 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4534 pedwarn ("ISO C forbids qualified function types");
4536 /* GNU C interprets a volatile-qualified function type to indicate
4537 that the function does not return. */
4538 if ((type_quals & TYPE_QUAL_VOLATILE)
4539 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4540 warning (0, "%<noreturn%> function returns non-void value");
4542 /* Every function declaration is an external reference
4543 (DECL_EXTERNAL) except for those which are not at file
4544 scope and are explicitly declared "auto". This is
4545 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4546 GCC to signify a forward declaration of a nested function. */
4547 if (storage_class == csc_auto && current_scope != file_scope)
4548 DECL_EXTERNAL (decl) = 0;
4550 DECL_EXTERNAL (decl) = 1;
4552 /* Record absence of global scope for `static' or `auto'. */
4554 = !(storage_class == csc_static || storage_class == csc_auto);
4556 /* For a function definition, record the argument information
4557 block where store_parm_decls will look for it. */
4559 current_function_arg_info = arg_info;
4561 if (declspecs->default_int_p)
4562 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4564 /* Record presence of `inline', if it is reasonable. */
4565 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4567 if (declspecs->inline_p)
4568 pedwarn ("cannot inline function %<main%>");
4570 else if (declspecs->inline_p)
4572 /* Record that the function is declared `inline'. */
4573 DECL_DECLARED_INLINE_P (decl) = 1;
4575 /* Do not mark bare declarations as DECL_INLINE. Doing so
4576 in the presence of multiple declarations can result in
4577 the abstract origin pointing between the declarations,
4578 which will confuse dwarf2out. */
4581 DECL_INLINE (decl) = 1;
4582 if (storage_class == csc_extern)
4583 current_extern_inline = 1;
4586 /* If -finline-functions, assume it can be inlined. This does
4587 two things: let the function be deferred until it is actually
4588 needed, and let dwarf2 know that the function is inlinable. */
4589 else if (flag_inline_trees == 2 && initialized)
4590 DECL_INLINE (decl) = 1;
4594 /* It's a variable. */
4595 /* An uninitialized decl with `extern' is a reference. */
4596 int extern_ref = !initialized && storage_class == csc_extern;
4598 type = c_build_qualified_type (type, type_quals);
4600 /* C99 6.2.2p7: It is invalid (compile-time undefined
4601 behavior) to create an 'extern' declaration for a
4602 variable if there is a global declaration that is
4603 'static' and the global declaration is not visible.
4604 (If the static declaration _is_ currently visible,
4605 the 'extern' declaration is taken to refer to that decl.) */
4606 if (extern_ref && current_scope != file_scope)
4608 tree global_decl = identifier_global_value (declarator->u.id);
4609 tree visible_decl = lookup_name (declarator->u.id);
4612 && global_decl != visible_decl
4613 && TREE_CODE (global_decl) == VAR_DECL
4614 && !TREE_PUBLIC (global_decl))
4615 error ("variable previously declared %<static%> redeclared "
4619 decl = build_decl (VAR_DECL, declarator->u.id, type);
4620 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4622 C_DECL_VARIABLE_SIZE (decl) = 1;
4624 if (declspecs->inline_p)
4625 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4627 /* At file scope, an initialized extern declaration may follow
4628 a static declaration. In that case, DECL_EXTERNAL will be
4629 reset later in start_decl. */
4630 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4632 /* At file scope, the presence of a `static' or `register' storage
4633 class specifier, or the absence of all storage class specifiers
4634 makes this declaration a definition (perhaps tentative). Also,
4635 the absence of both `static' and `register' makes it public. */
4636 if (current_scope == file_scope)
4638 TREE_PUBLIC (decl) = !(storage_class == csc_static
4639 || storage_class == csc_register);
4640 TREE_STATIC (decl) = !extern_ref;
4642 /* Not at file scope, only `static' makes a static definition. */
4645 TREE_STATIC (decl) = (storage_class == csc_static);
4646 TREE_PUBLIC (decl) = extern_ref;
4651 if (targetm.have_tls)
4652 DECL_THREAD_LOCAL (decl) = 1;
4654 /* A mere warning is sure to result in improper semantics
4655 at runtime. Don't bother to allow this to compile. */
4656 error ("thread-local storage not supported for this target");
4660 /* Record `register' declaration for warnings on &
4661 and in case doing stupid register allocation. */
4663 if (storage_class == csc_register)
4665 C_DECL_REGISTER (decl) = 1;
4666 DECL_REGISTER (decl) = 1;
4669 /* Record constancy and volatility. */
4670 c_apply_type_quals_to_decl (type_quals, decl);
4672 /* If a type has volatile components, it should be stored in memory.
4673 Otherwise, the fact that those components are volatile
4674 will be ignored, and would even crash the compiler. */
4675 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4677 /* It is not an error for a structure with volatile fields to
4678 be declared register, but reset DECL_REGISTER since it
4679 cannot actually go in a register. */
4680 int was_reg = C_DECL_REGISTER (decl);
4681 C_DECL_REGISTER (decl) = 0;
4682 DECL_REGISTER (decl) = 0;
4683 c_mark_addressable (decl);
4684 C_DECL_REGISTER (decl) = was_reg;
4687 /* This is the earliest point at which we might know the assembler
4688 name of a variable. Thus, if it's known before this, die horribly. */
4689 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4691 decl_attributes (&decl, returned_attrs, 0);
4697 /* Decode the parameter-list info for a function type or function definition.
4698 The argument is the value returned by `get_parm_info' (or made in parse.y
4699 if there is an identifier list instead of a parameter decl list).
4700 These two functions are separate because when a function returns
4701 or receives functions then each is called multiple times but the order
4702 of calls is different. The last call to `grokparms' is always the one
4703 that contains the formal parameter names of a function definition.
4705 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4707 FUNCDEF_FLAG is true for a function definition, false for
4708 a mere declaration. A nonempty identifier-list gets an error message
4709 when FUNCDEF_FLAG is false. */
4712 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4714 tree arg_types = arg_info->types;
4716 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4717 && !in_system_header)
4718 warning (0, "function declaration isn%'t a prototype");
4720 if (arg_types == error_mark_node)
4721 return 0; /* don't set TYPE_ARG_TYPES in this case */
4723 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4726 pedwarn ("parameter names (without types) in function declaration");
4728 arg_info->parms = arg_info->types;
4729 arg_info->types = 0;
4734 tree parm, type, typelt;
4735 unsigned int parmno;
4737 /* If there is a parameter of incomplete type in a definition,
4738 this is an error. In a declaration this is valid, and a
4739 struct or union type may be completed later, before any calls
4740 or definition of the function. In the case where the tag was
4741 first declared within the parameter list, a warning has
4742 already been given. If a parameter has void type, then
4743 however the function cannot be defined or called, so
4746 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4748 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4750 type = TREE_VALUE (typelt);
4751 if (type == error_mark_node)
4754 if (!COMPLETE_TYPE_P (type))
4758 if (DECL_NAME (parm))
4759 error ("%Jparameter %u (%qD) has incomplete type",
4760 parm, parmno, parm);
4762 error ("%Jparameter %u has incomplete type",
4765 TREE_VALUE (typelt) = error_mark_node;
4766 TREE_TYPE (parm) = error_mark_node;
4768 else if (VOID_TYPE_P (type))
4770 if (DECL_NAME (parm))
4771 warning (0, "%Jparameter %u (%qD) has void type",
4772 parm, parmno, parm);
4774 warning (0, "%Jparameter %u has void type",
4783 /* Take apart the current scope and return a c_arg_info structure with
4784 info on a parameter list just parsed.
4786 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4788 ELLIPSIS being true means the argument list ended in '...' so don't
4789 append a sentinel (void_list_node) to the end of the type-list. */
4792 get_parm_info (bool ellipsis)
4794 struct c_binding *b = current_scope->bindings;
4795 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4802 static bool explained_incomplete_types = false;
4803 bool gave_void_only_once_err = false;
4805 arg_info->parms = 0;
4807 arg_info->types = 0;
4808 arg_info->others = 0;
4810 /* The bindings in this scope must not get put into a block.
4811 We will take care of deleting the binding nodes. */
4812 current_scope->bindings = 0;
4814 /* This function is only called if there was *something* on the
4818 /* A parameter list consisting solely of 'void' indicates that the
4819 function takes no arguments. But if the 'void' is qualified
4820 (by 'const' or 'volatile'), or has a storage class specifier
4821 ('register'), then the behavior is undefined; issue an error.
4822 Typedefs for 'void' are OK (see DR#157). */
4823 if (b->prev == 0 /* one binding */
4824 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4825 && !DECL_NAME (b->decl) /* anonymous */
4826 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4828 if (TREE_THIS_VOLATILE (b->decl)
4829 || TREE_READONLY (b->decl)
4830 || C_DECL_REGISTER (b->decl))
4831 error ("%<void%> as only parameter may not be qualified");
4833 /* There cannot be an ellipsis. */
4835 error ("%<void%> must be the only parameter");
4837 arg_info->types = void_list_node;
4842 types = void_list_node;
4844 /* Break up the bindings list into parms, tags, types, and others;
4845 apply sanity checks; purge the name-to-decl bindings. */
4848 tree decl = b->decl;
4849 tree type = TREE_TYPE (decl);
4850 const char *keyword;
4852 switch (TREE_CODE (decl))
4857 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4858 I_SYMBOL_BINDING (b->id) = b->shadowed;
4861 /* Check for forward decls that never got their actual decl. */
4862 if (TREE_ASM_WRITTEN (decl))
4863 error ("%Jparameter %qD has just a forward declaration",
4865 /* Check for (..., void, ...) and issue an error. */
4866 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4868 if (!gave_void_only_once_err)
4870 error ("%<void%> must be the only parameter");
4871 gave_void_only_once_err = true;
4876 /* Valid parameter, add it to the list. */
4877 TREE_CHAIN (decl) = parms;
4880 /* Since there is a prototype, args are passed in their
4881 declared types. The back end may override this later. */
4882 DECL_ARG_TYPE (decl) = type;
4883 types = tree_cons (0, type, types);
4887 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4888 case UNION_TYPE: keyword = "union"; goto tag;
4889 case RECORD_TYPE: keyword = "struct"; goto tag;
4891 /* Types may not have tag-names, in which case the type
4892 appears in the bindings list with b->id NULL. */
4895 gcc_assert (I_TAG_BINDING (b->id) == b);
4896 I_TAG_BINDING (b->id) = b->shadowed;
4899 /* Warn about any struct, union or enum tags defined in a
4900 parameter list. The scope of such types is limited to
4901 the parameter list, which is rarely if ever desirable
4902 (it's impossible to call such a function with type-
4903 correct arguments). An anonymous union parm type is
4904 meaningful as a GNU extension, so don't warn for that. */
4905 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4908 /* The %s will be one of 'struct', 'union', or 'enum'. */
4909 warning (0, "%<%s %E%> declared inside parameter list",
4912 /* The %s will be one of 'struct', 'union', or 'enum'. */
4913 warning (0, "anonymous %s declared inside parameter list",
4916 if (!explained_incomplete_types)
4918 warning (0, "its scope is only this definition or declaration,"
4919 " which is probably not what you want");
4920 explained_incomplete_types = true;
4924 tags = tree_cons (b->id, decl, tags);
4930 /* CONST_DECLs appear here when we have an embedded enum,
4931 and TYPE_DECLs appear here when we have an embedded struct
4932 or union. No warnings for this - we already warned about the
4933 type itself. FUNCTION_DECLs appear when there is an implicit
4934 function declaration in the parameter list. */
4936 TREE_CHAIN (decl) = others;
4941 /* error_mark_node appears here when we have an undeclared
4942 variable. Just throw it away. */
4945 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4946 I_SYMBOL_BINDING (b->id) = b->shadowed;
4950 /* Other things that might be encountered. */
4957 b = free_binding_and_advance (b);
4960 arg_info->parms = parms;
4961 arg_info->tags = tags;
4962 arg_info->types = types;
4963 arg_info->others = others;
4967 /* Get the struct, enum or union (CODE says which) with tag NAME.
4968 Define the tag as a forward-reference if it is not defined.
4969 Return a c_typespec structure for the type specifier. */
4972 parser_xref_tag (enum tree_code code, tree name)
4974 struct c_typespec ret;
4975 /* If a cross reference is requested, look up the type
4976 already defined for this tag and return it. */
4978 tree ref = lookup_tag (code, name, 0);
4979 /* If this is the right type of tag, return what we found.
4980 (This reference will be shadowed by shadow_tag later if appropriate.)
4981 If this is the wrong type of tag, do not return it. If it was the
4982 wrong type in the same scope, we will have had an error
4983 message already; if in a different scope and declaring
4984 a name, pending_xref_error will give an error message; but if in a
4985 different scope and not declaring a name, this tag should
4986 shadow the previous declaration of a different type of tag, and
4987 this would not work properly if we return the reference found.
4988 (For example, with "struct foo" in an outer scope, "union foo;"
4989 must shadow that tag with a new one of union type.) */
4990 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
4991 if (ref && TREE_CODE (ref) == code)
4997 /* If no such tag is yet defined, create a forward-reference node
4998 and record it as the "definition".
4999 When a real declaration of this type is found,
5000 the forward-reference will be altered into a real type. */
5002 ref = make_node (code);
5003 if (code == ENUMERAL_TYPE)
5005 /* Give the type a default layout like unsigned int
5006 to avoid crashing if it does not get defined. */
5007 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5008 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5009 TYPE_USER_ALIGN (ref) = 0;
5010 TYPE_UNSIGNED (ref) = 1;
5011 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5012 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5013 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5016 pushtag (name, ref);
5022 /* Get the struct, enum or union (CODE says which) with tag NAME.
5023 Define the tag as a forward-reference if it is not defined.
5024 Return a tree for the type. */
5027 xref_tag (enum tree_code code, tree name)
5029 return parser_xref_tag (code, name).spec;
5032 /* Make sure that the tag NAME is defined *in the current scope*
5033 at least as a forward reference.
5034 CODE says which kind of tag NAME ought to be. */
5037 start_struct (enum tree_code code, tree name)
5039 /* If there is already a tag defined at this scope
5040 (as a forward reference), just return it. */
5045 ref = lookup_tag (code, name, 1);
5046 if (ref && TREE_CODE (ref) == code)
5048 if (TYPE_SIZE (ref))
5050 if (code == UNION_TYPE)
5051 error ("redefinition of %<union %E%>", name);
5053 error ("redefinition of %<struct %E%>", name);
5055 else if (C_TYPE_BEING_DEFINED (ref))
5057 if (code == UNION_TYPE)
5058 error ("nested redefinition of %<union %E%>", name);
5060 error ("nested redefinition of %<struct %E%>", name);
5065 /* Otherwise create a forward-reference just so the tag is in scope. */
5067 ref = make_node (code);
5068 pushtag (name, ref);
5071 C_TYPE_BEING_DEFINED (ref) = 1;
5072 TYPE_PACKED (ref) = flag_pack_struct;
5076 /* Process the specs, declarator and width (NULL if omitted)
5077 of a structure component, returning a FIELD_DECL node.
5078 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5080 This is done during the parsing of the struct declaration.
5081 The FIELD_DECL nodes are chained together and the lot of them
5082 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5085 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5090 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5091 && width == NULL_TREE)
5093 /* This is an unnamed decl.
5095 If we have something of the form "union { list } ;" then this
5096 is the anonymous union extension. Similarly for struct.
5098 If this is something of the form "struct foo;", then
5099 If MS extensions are enabled, this is handled as an
5101 Otherwise this is a forward declaration of a structure tag.
5103 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5104 If MS extensions are enabled and foo names a structure, then
5105 again this is an anonymous struct.
5106 Otherwise this is an error.
5108 Oh what a horrid tangled web we weave. I wonder if MS consciously
5109 took this from Plan 9 or if it was an accident of implementation
5110 that took root before someone noticed the bug... */
5112 tree type = declspecs->type;
5113 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5114 || TREE_CODE (type) == UNION_TYPE);
5118 && (flag_ms_extensions || !declspecs->typedef_p))
5120 if (flag_ms_extensions)
5124 else if (TYPE_NAME (type) == NULL)
5131 pedwarn ("declaration does not declare anything");
5135 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5138 value = grokdeclarator (declarator, declspecs, FIELD, false,
5139 width ? &width : NULL);
5141 finish_decl (value, NULL_TREE, NULL_TREE);
5142 DECL_INITIAL (value) = width;
5147 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5148 the list such that this does not present a problem later. */
5151 detect_field_duplicates (tree fieldlist)
5156 /* First, see if there are more than "a few" fields.
5157 This is trivially true if there are zero or one fields. */
5160 x = TREE_CHAIN (fieldlist);
5166 } while (timeout > 0 && x);
5168 /* If there were "few" fields, avoid the overhead of allocating
5169 a hash table. Instead just do the nested traversal thing. */
5172 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5175 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5176 if (DECL_NAME (y) == DECL_NAME (x))
5178 error ("%Jduplicate member %qD", x, x);
5179 DECL_NAME (x) = NULL_TREE;
5185 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5188 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5189 if ((y = DECL_NAME (x)) != 0)
5191 slot = htab_find_slot (htab, y, INSERT);
5194 error ("%Jduplicate member %qD", x, x);
5195 DECL_NAME (x) = NULL_TREE;
5204 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5205 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5206 ATTRIBUTES are attributes to be applied to the structure. */
5209 finish_struct (tree t, tree fieldlist, tree attributes)
5212 bool toplevel = file_scope == current_scope;
5213 int saw_named_field;
5215 /* If this type was previously laid out as a forward reference,
5216 make sure we lay it out again. */
5220 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5224 for (x = fieldlist; x; x = TREE_CHAIN (x))
5225 if (DECL_NAME (x) != 0)
5230 if (TREE_CODE (t) == UNION_TYPE)
5233 pedwarn ("union has no named members");
5235 pedwarn ("union has no members");
5240 pedwarn ("struct has no named members");
5242 pedwarn ("struct has no members");
5247 /* Install struct as DECL_CONTEXT of each field decl.
5248 Also process specified field sizes, found in the DECL_INITIAL,
5249 storing 0 there after the type has been changed to precision equal
5250 to its width, rather than the precision of the specified standard
5251 type. (Correct layout requires the original type to have been preserved
5254 saw_named_field = 0;
5255 for (x = fieldlist; x; x = TREE_CHAIN (x))
5257 DECL_CONTEXT (x) = t;
5258 DECL_PACKED (x) |= TYPE_PACKED (t);
5260 /* If any field is const, the structure type is pseudo-const. */
5261 if (TREE_READONLY (x))
5262 C_TYPE_FIELDS_READONLY (t) = 1;
5265 /* A field that is pseudo-const makes the structure likewise. */
5266 tree t1 = TREE_TYPE (x);
5267 while (TREE_CODE (t1) == ARRAY_TYPE)
5268 t1 = TREE_TYPE (t1);
5269 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5270 && C_TYPE_FIELDS_READONLY (t1))
5271 C_TYPE_FIELDS_READONLY (t) = 1;
5274 /* Any field that is volatile means variables of this type must be
5275 treated in some ways as volatile. */
5276 if (TREE_THIS_VOLATILE (x))
5277 C_TYPE_FIELDS_VOLATILE (t) = 1;
5279 /* Any field of nominal variable size implies structure is too. */
5280 if (C_DECL_VARIABLE_SIZE (x))
5281 C_TYPE_VARIABLE_SIZE (t) = 1;
5283 if (DECL_INITIAL (x))
5285 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5286 DECL_SIZE (x) = bitsize_int (width);
5287 DECL_BIT_FIELD (x) = 1;
5288 SET_DECL_C_BIT_FIELD (x);
5291 /* Detect flexible array member in an invalid context. */
5292 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5293 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5294 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5295 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5297 if (TREE_CODE (t) == UNION_TYPE)
5299 error ("%Jflexible array member in union", x);
5300 TREE_TYPE (x) = error_mark_node;
5302 else if (TREE_CHAIN (x) != NULL_TREE)
5304 error ("%Jflexible array member not at end of struct", x);
5305 TREE_TYPE (x) = error_mark_node;
5307 else if (!saw_named_field)
5309 error ("%Jflexible array member in otherwise empty struct", x);
5310 TREE_TYPE (x) = error_mark_node;
5314 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5315 && flexible_array_type_p (TREE_TYPE (x)))
5316 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5319 saw_named_field = 1;
5322 detect_field_duplicates (fieldlist);
5324 /* Now we have the nearly final fieldlist. Record it,
5325 then lay out the structure or union (including the fields). */
5327 TYPE_FIELDS (t) = fieldlist;
5331 /* Give bit-fields their proper types. */
5333 tree *fieldlistp = &fieldlist;
5335 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5336 && TREE_TYPE (*fieldlistp) != error_mark_node)
5338 unsigned HOST_WIDE_INT width
5339 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5340 tree type = TREE_TYPE (*fieldlistp);
5341 if (width != TYPE_PRECISION (type))
5343 TREE_TYPE (*fieldlistp)
5344 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5345 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5347 DECL_INITIAL (*fieldlistp) = 0;
5350 fieldlistp = &TREE_CHAIN (*fieldlistp);
5353 /* Now we have the truly final field list.
5354 Store it in this type and in the variants. */
5356 TYPE_FIELDS (t) = fieldlist;
5358 /* If there are lots of fields, sort so we can look through them fast.
5359 We arbitrarily consider 16 or more elts to be "a lot". */
5364 for (x = fieldlist; x; x = TREE_CHAIN (x))
5366 if (len > 15 || DECL_NAME (x) == NULL)
5374 struct lang_type *space;
5375 struct sorted_fields_type *space2;
5377 len += list_length (x);
5379 /* Use the same allocation policy here that make_node uses, to
5380 ensure that this lives as long as the rest of the struct decl.
5381 All decls in an inline function need to be saved. */
5383 space = GGC_CNEW (struct lang_type);
5384 space2 = GGC_NEWVAR (struct sorted_fields_type,
5385 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5389 field_array = &space2->elts[0];
5390 for (x = fieldlist; x; x = TREE_CHAIN (x))
5392 field_array[len++] = x;
5394 /* If there is anonymous struct or union, break out of the loop. */
5395 if (DECL_NAME (x) == NULL)
5398 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5401 TYPE_LANG_SPECIFIC (t) = space;
5402 TYPE_LANG_SPECIFIC (t)->s->len = len;
5403 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5404 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5409 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5411 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5412 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5413 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5414 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5417 /* If this was supposed to be a transparent union, but we can't
5418 make it one, warn and turn off the flag. */
5419 if (TREE_CODE (t) == UNION_TYPE
5420 && TYPE_TRANSPARENT_UNION (t)
5421 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5423 TYPE_TRANSPARENT_UNION (t) = 0;
5424 warning (0, "union cannot be made transparent");
5427 /* If this structure or union completes the type of any previous
5428 variable declaration, lay it out and output its rtl. */
5429 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5433 tree decl = TREE_VALUE (x);
5434 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5435 layout_array_type (TREE_TYPE (decl));
5436 if (TREE_CODE (decl) != TYPE_DECL)
5438 layout_decl (decl, 0);
5439 if (c_dialect_objc ())
5440 objc_check_decl (decl);
5441 rest_of_decl_compilation (decl, toplevel, 0);
5446 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5448 /* Finish debugging output for this type. */
5449 rest_of_type_compilation (t, toplevel);
5451 /* If we're inside a function proper, i.e. not file-scope and not still
5452 parsing parameters, then arrange for the size of a variable sized type
5454 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5455 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5460 /* Lay out the type T, and its element type, and so on. */
5463 layout_array_type (tree t)
5465 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5466 layout_array_type (TREE_TYPE (t));
5470 /* Begin compiling the definition of an enumeration type.
5471 NAME is its name (or null if anonymous).
5472 Returns the type object, as yet incomplete.
5473 Also records info about it so that build_enumerator
5474 may be used to declare the individual values as they are read. */
5477 start_enum (tree name)
5481 /* If this is the real definition for a previous forward reference,
5482 fill in the contents in the same object that used to be the
5483 forward reference. */
5486 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5488 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5490 enumtype = make_node (ENUMERAL_TYPE);
5491 pushtag (name, enumtype);
5494 if (C_TYPE_BEING_DEFINED (enumtype))
5495 error ("nested redefinition of %<enum %E%>", name);
5497 C_TYPE_BEING_DEFINED (enumtype) = 1;
5499 if (TYPE_VALUES (enumtype) != 0)
5501 /* This enum is a named one that has been declared already. */
5502 error ("redeclaration of %<enum %E%>", name);
5504 /* Completely replace its old definition.
5505 The old enumerators remain defined, however. */
5506 TYPE_VALUES (enumtype) = 0;
5509 enum_next_value = integer_zero_node;
5512 if (flag_short_enums)
5513 TYPE_PACKED (enumtype) = 1;
5518 /* After processing and defining all the values of an enumeration type,
5519 install their decls in the enumeration type and finish it off.
5520 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5521 and ATTRIBUTES are the specified attributes.
5522 Returns ENUMTYPE. */
5525 finish_enum (tree enumtype, tree values, tree attributes)
5528 tree minnode = 0, maxnode = 0;
5529 int precision, unsign;
5530 bool toplevel = (file_scope == current_scope);
5531 struct lang_type *lt;
5533 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5535 /* Calculate the maximum value of any enumerator in this type. */
5537 if (values == error_mark_node)
5538 minnode = maxnode = integer_zero_node;
5541 minnode = maxnode = TREE_VALUE (values);
5542 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5544 tree value = TREE_VALUE (pair);
5545 if (tree_int_cst_lt (maxnode, value))
5547 if (tree_int_cst_lt (value, minnode))
5552 /* Construct the final type of this enumeration. It is the same
5553 as one of the integral types - the narrowest one that fits, except
5554 that normally we only go as narrow as int - and signed iff any of
5555 the values are negative. */
5556 unsign = (tree_int_cst_sgn (minnode) >= 0);
5557 precision = MAX (min_precision (minnode, unsign),
5558 min_precision (maxnode, unsign));
5560 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5562 tem = c_common_type_for_size (precision, unsign);
5565 warning (0, "enumeration values exceed range of largest integer");
5566 tem = long_long_integer_type_node;
5570 tem = unsign ? unsigned_type_node : integer_type_node;
5572 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5573 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5574 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5575 TYPE_SIZE (enumtype) = 0;
5577 /* If the precision of the type was specific with an attribute and it
5578 was too small, give an error. Otherwise, use it. */
5579 if (TYPE_PRECISION (enumtype))
5581 if (precision > TYPE_PRECISION (enumtype))
5582 error ("specified mode too small for enumeral values");
5585 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5587 layout_type (enumtype);
5589 if (values != error_mark_node)
5591 /* Change the type of the enumerators to be the enum type. We
5592 need to do this irrespective of the size of the enum, for
5593 proper type checking. Replace the DECL_INITIALs of the
5594 enumerators, and the value slots of the list, with copies
5595 that have the enum type; they cannot be modified in place
5596 because they may be shared (e.g. integer_zero_node) Finally,
5597 change the purpose slots to point to the names of the decls. */
5598 for (pair = values; pair; pair = TREE_CHAIN (pair))
5600 tree enu = TREE_PURPOSE (pair);
5601 tree ini = DECL_INITIAL (enu);
5603 TREE_TYPE (enu) = enumtype;
5605 /* The ISO C Standard mandates enumerators to have type int,
5606 even though the underlying type of an enum type is
5607 unspecified. Here we convert any enumerators that fit in
5608 an int to type int, to avoid promotions to unsigned types
5609 when comparing integers with enumerators that fit in the
5610 int range. When -pedantic is given, build_enumerator()
5611 would have already taken care of those that don't fit. */
5612 if (int_fits_type_p (ini, integer_type_node))
5613 tem = integer_type_node;
5616 ini = convert (tem, ini);
5618 DECL_INITIAL (enu) = ini;
5619 TREE_PURPOSE (pair) = DECL_NAME (enu);
5620 TREE_VALUE (pair) = ini;
5623 TYPE_VALUES (enumtype) = values;
5626 /* Record the min/max values so that we can warn about bit-field
5627 enumerations that are too small for the values. */
5628 lt = GGC_CNEW (struct lang_type);
5629 lt->enum_min = minnode;
5630 lt->enum_max = maxnode;
5631 TYPE_LANG_SPECIFIC (enumtype) = lt;
5633 /* Fix up all variant types of this enum type. */
5634 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5636 if (tem == enumtype)
5638 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5639 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5640 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5641 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5642 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5643 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5644 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5645 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5646 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5647 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5648 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5651 /* Finish debugging output for this type. */
5652 rest_of_type_compilation (enumtype, toplevel);
5657 /* Build and install a CONST_DECL for one value of the
5658 current enumeration type (one that was begun with start_enum).
5659 Return a tree-list containing the CONST_DECL and its value.
5660 Assignment of sequential values by default is handled here. */
5663 build_enumerator (tree name, tree value)
5667 /* Validate and default VALUE. */
5671 /* Don't issue more errors for error_mark_node (i.e. an
5672 undeclared identifier) - just ignore the value expression. */
5673 if (value == error_mark_node)
5675 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5676 || TREE_CODE (value) != INTEGER_CST)
5678 error ("enumerator value for %qE is not an integer constant", name);
5683 value = default_conversion (value);
5684 constant_expression_warning (value);
5688 /* Default based on previous value. */
5689 /* It should no longer be possible to have NON_LVALUE_EXPR
5693 value = enum_next_value;
5695 error ("overflow in enumeration values");
5698 if (pedantic && !int_fits_type_p (value, integer_type_node))
5700 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5701 /* XXX This causes -pedantic to change the meaning of the program.
5702 Remove? -zw 2004-03-15 */
5703 value = convert (integer_type_node, value);
5706 /* Set basis for default for next value. */
5707 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5708 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5710 /* Now create a declaration for the enum value name. */
5712 type = TREE_TYPE (value);
5713 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5714 TYPE_PRECISION (integer_type_node)),
5715 (TYPE_PRECISION (type)
5716 >= TYPE_PRECISION (integer_type_node)
5717 && TYPE_UNSIGNED (type)));
5719 decl = build_decl (CONST_DECL, name, type);
5720 DECL_INITIAL (decl) = convert (type, value);
5723 return tree_cons (decl, value, NULL_TREE);
5727 /* Create the FUNCTION_DECL for a function definition.
5728 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5729 the declaration; they describe the function's name and the type it returns,
5730 but twisted together in a fashion that parallels the syntax of C.
5732 This function creates a binding context for the function body
5733 as well as setting up the FUNCTION_DECL in current_function_decl.
5735 Returns 1 on success. If the DECLARATOR is not suitable for a function
5736 (it defines a datum instead), we return 0, which tells
5737 yyparse to report a parse error. */
5740 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5743 tree decl1, old_decl;
5744 tree restype, resdecl;
5745 struct c_label_context_se *nstack_se;
5746 struct c_label_context_vm *nstack_vm;
5748 current_function_returns_value = 0; /* Assume, until we see it does. */
5749 current_function_returns_null = 0;
5750 current_function_returns_abnormally = 0;
5751 warn_about_return_type = 0;
5752 current_extern_inline = 0;
5753 c_switch_stack = NULL;
5755 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5756 nstack_se->labels_def = NULL;
5757 nstack_se->labels_used = NULL;
5758 nstack_se->next = label_context_stack_se;
5759 label_context_stack_se = nstack_se;
5761 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5762 nstack_vm->labels_def = NULL;
5763 nstack_vm->labels_used = NULL;
5764 nstack_vm->scope = 0;
5765 nstack_vm->next = label_context_stack_vm;
5766 label_context_stack_vm = nstack_vm;
5768 /* Indicate no valid break/continue context by setting these variables
5769 to some non-null, non-label value. We'll notice and emit the proper
5770 error message in c_finish_bc_stmt. */
5771 c_break_label = c_cont_label = size_zero_node;
5773 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5775 /* If the declarator is not suitable for a function definition,
5776 cause a syntax error. */
5780 decl_attributes (&decl1, attributes, 0);
5782 if (DECL_DECLARED_INLINE_P (decl1)
5783 && DECL_UNINLINABLE (decl1)
5784 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5785 warning (0, "%Jinline function %qD given attribute noinline", decl1, decl1);
5787 announce_function (decl1);
5789 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5791 error ("return type is an incomplete type");
5792 /* Make it return void instead. */
5794 = build_function_type (void_type_node,
5795 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5798 if (warn_about_return_type)
5799 pedwarn_c99 ("return type defaults to %<int%>");
5801 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5802 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5803 DECL_INITIAL (decl1) = error_mark_node;
5805 /* If this definition isn't a prototype and we had a prototype declaration
5806 before, copy the arg type info from that prototype. */
5807 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5808 current_function_prototype_locus = UNKNOWN_LOCATION;
5809 current_function_prototype_built_in = false;
5810 current_function_prototype_arg_types = NULL_TREE;
5811 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5813 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5814 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5815 TREE_TYPE (TREE_TYPE (old_decl))))
5817 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5819 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5820 current_function_prototype_built_in
5821 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5822 current_function_prototype_arg_types
5823 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5825 if (TREE_PUBLIC (decl1))
5827 /* If there is an external prototype declaration of this
5828 function, record its location but do not copy information
5829 to this decl. This may be an invisible declaration
5830 (built-in or in a scope which has finished) or simply
5831 have more refined argument types than any declaration
5833 struct c_binding *b;
5834 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5835 if (B_IN_SCOPE (b, external_scope))
5839 tree ext_decl, ext_type;
5841 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5842 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5843 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5844 TREE_TYPE (ext_type)))
5846 current_function_prototype_locus
5847 = DECL_SOURCE_LOCATION (ext_decl);
5848 current_function_prototype_built_in
5849 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5850 current_function_prototype_arg_types
5851 = TYPE_ARG_TYPES (ext_type);
5857 /* Optionally warn of old-fashioned def with no previous prototype. */
5858 if (warn_strict_prototypes
5859 && old_decl != error_mark_node
5860 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5861 && C_DECL_ISNT_PROTOTYPE (old_decl))
5862 warning (0, "function declaration isn%'t a prototype");
5863 /* Optionally warn of any global def with no previous prototype. */
5864 else if (warn_missing_prototypes
5865 && old_decl != error_mark_node
5866 && TREE_PUBLIC (decl1)
5867 && !MAIN_NAME_P (DECL_NAME (decl1))
5868 && C_DECL_ISNT_PROTOTYPE (old_decl))
5869 warning (0, "%Jno previous prototype for %qD", decl1, decl1);
5870 /* Optionally warn of any def with no previous prototype
5871 if the function has already been used. */
5872 else if (warn_missing_prototypes
5874 && old_decl != error_mark_node
5875 && TREE_USED (old_decl)
5876 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5877 warning (0, "%J%qD was used with no prototype before its definition",
5879 /* Optionally warn of any global def with no previous declaration. */
5880 else if (warn_missing_declarations
5881 && TREE_PUBLIC (decl1)
5883 && !MAIN_NAME_P (DECL_NAME (decl1)))
5884 warning (0, "%Jno previous declaration for %qD", decl1, decl1);
5885 /* Optionally warn of any def with no previous declaration
5886 if the function has already been used. */
5887 else if (warn_missing_declarations
5889 && old_decl != error_mark_node
5890 && TREE_USED (old_decl)
5891 && C_DECL_IMPLICIT (old_decl))
5892 warning (0, "%J%qD was used with no declaration before its definition",
5895 /* This is a definition, not a reference.
5896 So normally clear DECL_EXTERNAL.
5897 However, `extern inline' acts like a declaration
5898 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5899 DECL_EXTERNAL (decl1) = current_extern_inline;
5901 /* This function exists in static storage.
5902 (This does not mean `static' in the C sense!) */
5903 TREE_STATIC (decl1) = 1;
5905 /* A nested function is not global. */
5906 if (current_function_decl != 0)
5907 TREE_PUBLIC (decl1) = 0;
5909 /* This is the earliest point at which we might know the assembler
5910 name of the function. Thus, if it's set before this, die horribly. */
5911 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5913 /* If #pragma weak was used, mark the decl weak now. */
5914 if (current_scope == file_scope)
5915 maybe_apply_pragma_weak (decl1);
5917 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5918 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5923 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5924 != integer_type_node)
5925 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
5927 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5928 args = TREE_CHAIN (args))
5930 tree type = args ? TREE_VALUE (args) : 0;
5932 if (type == void_type_node)
5939 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5940 pedwarn ("%Jfirst argument of %qD should be %<int%>",
5945 if (TREE_CODE (type) != POINTER_TYPE
5946 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5947 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5949 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
5954 if (TREE_CODE (type) != POINTER_TYPE
5955 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5956 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5958 pedwarn ("%Jthird argument of %qD should probably be "
5959 "%<char **%>", decl1, decl1);
5964 /* It is intentional that this message does not mention the third
5965 argument because it's only mentioned in an appendix of the
5967 if (argct > 0 && (argct < 2 || argct > 3))
5968 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
5970 if (!TREE_PUBLIC (decl1))
5971 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
5974 /* Record the decl so that the function name is defined.
5975 If we already have a decl for this name, and it is a FUNCTION_DECL,
5976 use the old decl. */
5978 current_function_decl = pushdecl (decl1);
5981 declare_parm_level ();
5983 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5984 /* Promote the value to int before returning it. */
5985 if (c_promoting_integer_type_p (restype))
5987 /* It retains unsignedness if not really getting wider. */
5988 if (TYPE_UNSIGNED (restype)
5989 && (TYPE_PRECISION (restype)
5990 == TYPE_PRECISION (integer_type_node)))
5991 restype = unsigned_type_node;
5993 restype = integer_type_node;
5996 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
5997 DECL_ARTIFICIAL (resdecl) = 1;
5998 DECL_IGNORED_P (resdecl) = 1;
5999 DECL_RESULT (current_function_decl) = resdecl;
6001 start_fname_decls ();
6006 /* Subroutine of store_parm_decls which handles new-style function
6007 definitions (prototype format). The parms already have decls, so we
6008 need only record them as in effect and complain if any redundant
6009 old-style parm decls were written. */
6011 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6015 if (current_scope->bindings)
6017 error ("%Jold-style parameter declarations in prototyped "
6018 "function definition", fndecl);
6020 /* Get rid of the old-style declarations. */
6024 /* Don't issue this warning for nested functions, and don't issue this
6025 warning if we got here because ARG_INFO_TYPES was error_mark_node
6026 (this happens when a function definition has just an ellipsis in
6027 its parameter list). */
6028 else if (warn_traditional && !in_system_header && !current_function_scope
6029 && arg_info->types != error_mark_node)
6030 warning (0, "%Jtraditional C rejects ISO C style function definitions",
6033 /* Now make all the parameter declarations visible in the function body.
6034 We can bypass most of the grunt work of pushdecl. */
6035 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6037 DECL_CONTEXT (decl) = current_function_decl;
6038 if (DECL_NAME (decl))
6039 bind (DECL_NAME (decl), decl, current_scope,
6040 /*invisible=*/false, /*nested=*/false);
6042 error ("%Jparameter name omitted", decl);
6045 /* Record the parameter list in the function declaration. */
6046 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6048 /* Now make all the ancillary declarations visible, likewise. */
6049 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6051 DECL_CONTEXT (decl) = current_function_decl;
6052 if (DECL_NAME (decl))
6053 bind (DECL_NAME (decl), decl, current_scope,
6054 /*invisible=*/false, /*nested=*/false);
6057 /* And all the tag declarations. */
6058 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6059 if (TREE_PURPOSE (decl))
6060 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6061 /*invisible=*/false, /*nested=*/false);
6064 /* Subroutine of store_parm_decls which handles old-style function
6065 definitions (separate parameter list and declarations). */
6068 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6070 struct c_binding *b;
6071 tree parm, decl, last;
6072 tree parmids = arg_info->parms;
6074 /* We use DECL_WEAK as a flag to show which parameters have been
6075 seen already, since it is not used on PARM_DECL. */
6076 #ifdef ENABLE_CHECKING
6077 for (b = current_scope->bindings; b; b = b->prev)
6078 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6081 if (!in_system_header)
6082 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6085 /* Match each formal parameter name with its declaration. Save each
6086 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6087 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6089 if (TREE_VALUE (parm) == 0)
6091 error ("%Jparameter name missing from parameter list", fndecl);
6092 TREE_PURPOSE (parm) = 0;
6096 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6097 if (b && B_IN_CURRENT_SCOPE (b))
6100 /* If we got something other than a PARM_DECL it is an error. */
6101 if (TREE_CODE (decl) != PARM_DECL)
6102 error ("%J%qD declared as a non-parameter", decl, decl);
6103 /* If the declaration is already marked, we have a duplicate
6104 name. Complain and ignore the duplicate. */
6105 else if (DECL_WEAK (decl))
6107 error ("%Jmultiple parameters named %qD", decl, decl);
6108 TREE_PURPOSE (parm) = 0;
6111 /* If the declaration says "void", complain and turn it into
6113 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6115 error ("%Jparameter %qD declared with void type", decl, decl);
6116 TREE_TYPE (decl) = integer_type_node;
6117 DECL_ARG_TYPE (decl) = integer_type_node;
6118 layout_decl (decl, 0);
6121 /* If no declaration found, default to int. */
6124 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6125 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6126 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6130 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6131 else if (extra_warnings)
6132 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6135 TREE_PURPOSE (parm) = decl;
6136 DECL_WEAK (decl) = 1;
6139 /* Now examine the parms chain for incomplete declarations
6140 and declarations with no corresponding names. */
6142 for (b = current_scope->bindings; b; b = b->prev)
6145 if (TREE_CODE (parm) != PARM_DECL)
6148 if (TREE_TYPE (parm) != error_mark_node
6149 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6151 error ("%Jparameter %qD has incomplete type", parm, parm);
6152 TREE_TYPE (parm) = error_mark_node;
6155 if (!DECL_WEAK (parm))
6157 error ("%Jdeclaration for parameter %qD but no such parameter",
6160 /* Pretend the parameter was not missing.
6161 This gets us to a standard state and minimizes
6162 further error messages. */
6163 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6167 /* Chain the declarations together in the order of the list of
6168 names. Store that chain in the function decl, replacing the
6169 list of names. Update the current scope to match. */
6170 DECL_ARGUMENTS (fndecl) = 0;
6172 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6173 if (TREE_PURPOSE (parm))
6175 if (parm && TREE_PURPOSE (parm))
6177 last = TREE_PURPOSE (parm);
6178 DECL_ARGUMENTS (fndecl) = last;
6179 DECL_WEAK (last) = 0;
6181 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6182 if (TREE_PURPOSE (parm))
6184 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6185 last = TREE_PURPOSE (parm);
6186 DECL_WEAK (last) = 0;
6188 TREE_CHAIN (last) = 0;
6191 /* If there was a previous prototype,
6192 set the DECL_ARG_TYPE of each argument according to
6193 the type previously specified, and report any mismatches. */
6195 if (current_function_prototype_arg_types)
6198 for (parm = DECL_ARGUMENTS (fndecl),
6199 type = current_function_prototype_arg_types;
6200 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6201 != void_type_node));
6202 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6204 if (parm == 0 || type == 0
6205 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6207 if (current_function_prototype_built_in)
6208 warning (0, "number of arguments doesn%'t match "
6209 "built-in prototype");
6212 error ("number of arguments doesn%'t match prototype");
6213 error ("%Hprototype declaration",
6214 ¤t_function_prototype_locus);
6218 /* Type for passing arg must be consistent with that
6219 declared for the arg. ISO C says we take the unqualified
6220 type for parameters declared with qualified type. */
6221 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6222 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6224 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6225 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6227 /* Adjust argument to match prototype. E.g. a previous
6228 `int foo(float);' prototype causes
6229 `int foo(x) float x; {...}' to be treated like
6230 `int foo(float x) {...}'. This is particularly
6231 useful for argument types like uid_t. */
6232 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6234 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6235 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6236 && TYPE_PRECISION (TREE_TYPE (parm))
6237 < TYPE_PRECISION (integer_type_node))
6238 DECL_ARG_TYPE (parm) = integer_type_node;
6242 /* ??? Is it possible to get here with a
6243 built-in prototype or will it always have
6244 been diagnosed as conflicting with an
6245 old-style definition and discarded? */
6246 if (current_function_prototype_built_in)
6247 warning (0, "promoted argument %qD "
6248 "doesn%'t match built-in prototype", parm);
6251 pedwarn ("promoted argument %qD "
6252 "doesn%'t match prototype", parm);
6253 pedwarn ("%Hprototype declaration",
6254 ¤t_function_prototype_locus);
6260 if (current_function_prototype_built_in)
6261 warning (0, "argument %qD doesn%'t match "
6262 "built-in prototype", parm);
6265 error ("argument %qD doesn%'t match prototype", parm);
6266 error ("%Hprototype declaration",
6267 ¤t_function_prototype_locus);
6272 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6275 /* Otherwise, create a prototype that would match. */
6279 tree actual = 0, last = 0, type;
6281 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6283 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6285 TREE_CHAIN (last) = type;
6290 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6292 TREE_CHAIN (last) = type;
6296 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6297 of the type of this function, but we need to avoid having this
6298 affect the types of other similarly-typed functions, so we must
6299 first force the generation of an identical (but separate) type
6300 node for the relevant function type. The new node we create
6301 will be a variant of the main variant of the original function
6304 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6306 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6310 /* Store parameter declarations passed in ARG_INFO into the current
6311 function declaration. */
6314 store_parm_decls_from (struct c_arg_info *arg_info)
6316 current_function_arg_info = arg_info;
6317 store_parm_decls ();
6320 /* Store the parameter declarations into the current function declaration.
6321 This is called after parsing the parameter declarations, before
6322 digesting the body of the function.
6324 For an old-style definition, construct a prototype out of the old-style
6325 parameter declarations and inject it into the function's type. */
6328 store_parm_decls (void)
6330 tree fndecl = current_function_decl;
6333 /* The argument information block for FNDECL. */
6334 struct c_arg_info *arg_info = current_function_arg_info;
6335 current_function_arg_info = 0;
6337 /* True if this definition is written with a prototype. Note:
6338 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6339 list in a function definition as equivalent to (void) -- an
6340 empty argument list specifies the function has no parameters,
6341 but only (void) sets up a prototype for future calls. */
6342 proto = arg_info->types != 0;
6345 store_parm_decls_newstyle (fndecl, arg_info);
6347 store_parm_decls_oldstyle (fndecl, arg_info);
6349 /* The next call to push_scope will be a function body. */
6351 next_is_function_body = true;
6353 /* Write a record describing this function definition to the prototypes
6354 file (if requested). */
6356 gen_aux_info_record (fndecl, 1, 0, proto);
6358 /* Initialize the RTL code for the function. */
6359 allocate_struct_function (fndecl);
6361 /* Begin the statement tree for this function. */
6362 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6364 /* ??? Insert the contents of the pending sizes list into the function
6365 to be evaluated. The only reason left to have this is
6366 void foo(int n, int array[n++])
6367 because we throw away the array type in favor of a pointer type, and
6368 thus won't naturally see the SAVE_EXPR containing the increment. All
6369 other pending sizes would be handled by gimplify_parameters. */
6372 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6373 add_stmt (TREE_VALUE (t));
6376 /* Even though we're inside a function body, we still don't want to
6377 call expand_expr to calculate the size of a variable-sized array.
6378 We haven't necessarily assigned RTL to all variables yet, so it's
6379 not safe to try to expand expressions involving them. */
6380 cfun->x_dont_save_pending_sizes_p = 1;
6383 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6387 c_warn_unused_result_recursively (tree fndecl)
6389 struct cgraph_node *cgn;
6391 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6392 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6394 /* Finalize all nested functions now. */
6395 cgn = cgraph_node (fndecl);
6396 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6397 c_warn_unused_result_recursively (cgn->decl);
6400 /* Finish up a function declaration and compile that function
6401 all the way to assembler language output. The free the storage
6402 for the function definition.
6404 This is called after parsing the body of the function definition. */
6407 finish_function (void)
6409 tree fndecl = current_function_decl;
6411 label_context_stack_se = label_context_stack_se->next;
6412 label_context_stack_vm = label_context_stack_vm->next;
6414 if (TREE_CODE (fndecl) == FUNCTION_DECL
6415 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6417 tree args = DECL_ARGUMENTS (fndecl);
6418 for (; args; args = TREE_CHAIN (args))
6420 tree type = TREE_TYPE (args);
6421 if (INTEGRAL_TYPE_P (type)
6422 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6423 DECL_ARG_TYPE (args) = integer_type_node;
6427 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6428 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6430 /* Must mark the RESULT_DECL as being in this function. */
6432 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6433 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6435 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6437 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6438 != integer_type_node)
6440 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6441 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6443 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6449 tree stmt = c_finish_return (integer_zero_node);
6450 #ifdef USE_MAPPED_LOCATION
6451 /* Hack. We don't want the middle-end to warn that this return
6452 is unreachable, so we mark its location as special. Using
6453 UNKNOWN_LOCATION has the problem that it gets clobbered in
6454 annotate_one_with_locus. A cleaner solution might be to
6455 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6457 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6459 /* Hack. We don't want the middle-end to warn that this
6460 return is unreachable, so put the statement on the
6462 annotate_with_file_line (stmt, input_filename, 0);
6468 /* Tie off the statement tree for this function. */
6469 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6471 finish_fname_decls ();
6473 /* Complain if there's just no return statement. */
6474 if (warn_return_type
6475 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6476 && !current_function_returns_value && !current_function_returns_null
6477 /* Don't complain if we are no-return. */
6478 && !current_function_returns_abnormally
6479 /* Don't warn for main(). */
6480 && !MAIN_NAME_P (DECL_NAME (fndecl))
6481 /* Or if they didn't actually specify a return type. */
6482 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6483 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6484 inline function, as we might never be compiled separately. */
6485 && DECL_INLINE (fndecl))
6487 warning (0, "no return statement in function returning non-void");
6488 TREE_NO_WARNING (fndecl) = 1;
6491 /* With just -Wextra, complain only if function returns both with
6492 and without a value. */
6494 && current_function_returns_value
6495 && current_function_returns_null)
6496 warning (0, "this function may return with or without a value");
6498 /* Store the end of the function, so that we get good line number
6499 info for the epilogue. */
6500 cfun->function_end_locus = input_location;
6502 /* If we don't have ctors/dtors sections, and this is a static
6503 constructor or destructor, it must be recorded now. */
6504 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6505 && !targetm.have_ctors_dtors)
6506 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6507 if (DECL_STATIC_DESTRUCTOR (fndecl)
6508 && !targetm.have_ctors_dtors)
6509 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6511 /* Finalize the ELF visibility for the function. */
6512 c_determine_visibility (fndecl);
6514 /* Genericize before inlining. Delay genericizing nested functions
6515 until their parent function is genericized. Since finalizing
6516 requires GENERIC, delay that as well. */
6518 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6519 && !undef_nested_function)
6521 if (!decl_function_context (fndecl))
6523 c_genericize (fndecl);
6524 c_warn_unused_result_recursively (fndecl);
6526 /* ??? Objc emits functions after finalizing the compilation unit.
6527 This should be cleaned up later and this conditional removed. */
6528 if (cgraph_global_info_ready)
6530 c_expand_body (fndecl);
6534 cgraph_finalize_function (fndecl, false);
6538 /* Register this function with cgraph just far enough to get it
6539 added to our parent's nested function list. Handy, since the
6540 C front end doesn't have such a list. */
6541 (void) cgraph_node (fndecl);
6545 if (!decl_function_context (fndecl))
6546 undef_nested_function = false;
6548 /* We're leaving the context of this function, so zap cfun.
6549 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6550 tree_rest_of_compilation. */
6552 current_function_decl = NULL;
6555 /* Generate the RTL for the body of FNDECL. */
6558 c_expand_body (tree fndecl)
6561 if (!DECL_INITIAL (fndecl)
6562 || DECL_INITIAL (fndecl) == error_mark_node)
6565 tree_rest_of_compilation (fndecl);
6567 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6568 && targetm.have_ctors_dtors)
6569 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6570 DEFAULT_INIT_PRIORITY);
6571 if (DECL_STATIC_DESTRUCTOR (fndecl)
6572 && targetm.have_ctors_dtors)
6573 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6574 DEFAULT_INIT_PRIORITY);
6577 /* Check the declarations given in a for-loop for satisfying the C99
6580 check_for_loop_decls (void)
6582 struct c_binding *b;
6586 /* If we get here, declarations have been used in a for loop without
6587 the C99 for loop scope. This doesn't make much sense, so don't
6589 error ("%<for%> loop initial declaration used outside C99 mode");
6592 /* C99 subclause 6.8.5 paragraph 3:
6594 [#3] The declaration part of a for statement shall only
6595 declare identifiers for objects having storage class auto or
6598 It isn't clear whether, in this sentence, "identifiers" binds to
6599 "shall only declare" or to "objects" - that is, whether all identifiers
6600 declared must be identifiers for objects, or whether the restriction
6601 only applies to those that are. (A question on this in comp.std.c
6602 in November 2000 received no answer.) We implement the strictest
6603 interpretation, to avoid creating an extension which later causes
6606 for (b = current_scope->bindings; b; b = b->prev)
6609 tree decl = b->decl;
6614 switch (TREE_CODE (decl))
6617 if (TREE_STATIC (decl))
6618 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6619 "initial declaration", decl, decl);
6620 else if (DECL_EXTERNAL (decl))
6621 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6622 "initial declaration", decl, decl);
6626 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6630 error ("%<union %E%> declared in %<for%> loop initial declaration",
6634 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6638 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6639 "initial declaration", decl, decl);
6644 /* Save and reinitialize the variables
6645 used during compilation of a C function. */
6648 c_push_function_context (struct function *f)
6650 struct language_function *p;
6651 p = GGC_NEW (struct language_function);
6654 p->base.x_stmt_tree = c_stmt_tree;
6655 p->x_break_label = c_break_label;
6656 p->x_cont_label = c_cont_label;
6657 p->x_switch_stack = c_switch_stack;
6658 p->arg_info = current_function_arg_info;
6659 p->returns_value = current_function_returns_value;
6660 p->returns_null = current_function_returns_null;
6661 p->returns_abnormally = current_function_returns_abnormally;
6662 p->warn_about_return_type = warn_about_return_type;
6663 p->extern_inline = current_extern_inline;
6666 /* Restore the variables used during compilation of a C function. */
6669 c_pop_function_context (struct function *f)
6671 struct language_function *p = f->language;
6673 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6674 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6676 /* Stop pointing to the local nodes about to be freed. */
6677 /* But DECL_INITIAL must remain nonzero so we know this
6678 was an actual function definition. */
6679 DECL_INITIAL (current_function_decl) = error_mark_node;
6680 DECL_ARGUMENTS (current_function_decl) = 0;
6683 c_stmt_tree = p->base.x_stmt_tree;
6684 c_break_label = p->x_break_label;
6685 c_cont_label = p->x_cont_label;
6686 c_switch_stack = p->x_switch_stack;
6687 current_function_arg_info = p->arg_info;
6688 current_function_returns_value = p->returns_value;
6689 current_function_returns_null = p->returns_null;
6690 current_function_returns_abnormally = p->returns_abnormally;
6691 warn_about_return_type = p->warn_about_return_type;
6692 current_extern_inline = p->extern_inline;
6697 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6700 c_dup_lang_specific_decl (tree decl)
6702 struct lang_decl *ld;
6704 if (!DECL_LANG_SPECIFIC (decl))
6707 ld = GGC_NEW (struct lang_decl);
6708 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6709 DECL_LANG_SPECIFIC (decl) = ld;
6712 /* The functions below are required for functionality of doing
6713 function at once processing in the C front end. Currently these
6714 functions are not called from anywhere in the C front end, but as
6715 these changes continue, that will change. */
6717 /* Returns nonzero if the current statement is a full expression,
6718 i.e. temporaries created during that statement should be destroyed
6719 at the end of the statement. */
6722 stmts_are_full_exprs_p (void)
6727 /* Returns the stmt_tree (if any) to which statements are currently
6728 being added. If there is no active statement-tree, NULL is
6732 current_stmt_tree (void)
6734 return &c_stmt_tree;
6737 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6741 anon_aggr_type_p (tree ARG_UNUSED (node))
6746 /* Return the global value of T as a symbol. */
6749 identifier_global_value (tree t)
6751 struct c_binding *b;
6753 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6754 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6760 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6761 otherwise the name is found in ridpointers from RID_INDEX. */
6764 record_builtin_type (enum rid rid_index, const char *name, tree type)
6768 id = ridpointers[(int) rid_index];
6770 id = get_identifier (name);
6771 decl = build_decl (TYPE_DECL, id, type);
6773 if (debug_hooks->type_decl)
6774 debug_hooks->type_decl (decl, false);
6777 /* Build the void_list_node (void_type_node having been created). */
6779 build_void_list_node (void)
6781 tree t = build_tree_list (NULL_TREE, void_type_node);
6785 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6788 build_c_parm (struct c_declspecs *specs, tree attrs,
6789 struct c_declarator *declarator)
6791 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6794 ret->declarator = declarator;
6798 /* Return a declarator with nested attributes. TARGET is the inner
6799 declarator to which these attributes apply. ATTRS are the
6802 struct c_declarator *
6803 build_attrs_declarator (tree attrs, struct c_declarator *target)
6805 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6806 ret->kind = cdk_attrs;
6807 ret->declarator = target;
6808 ret->u.attrs = attrs;
6812 /* Return a declarator for a function with arguments specified by ARGS
6813 and return type specified by TARGET. */
6815 struct c_declarator *
6816 build_function_declarator (struct c_arg_info *args,
6817 struct c_declarator *target)
6819 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6820 ret->kind = cdk_function;
6821 ret->declarator = target;
6822 ret->u.arg_info = args;
6826 /* Return a declarator for the identifier IDENT (which may be
6827 NULL_TREE for an abstract declarator). */
6829 struct c_declarator *
6830 build_id_declarator (tree ident)
6832 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6834 ret->declarator = 0;
6836 /* Default value - may get reset to a more precise location. */
6837 ret->id_loc = input_location;
6841 /* Return something to represent absolute declarators containing a *.
6842 TARGET is the absolute declarator that the * contains.
6843 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6844 to apply to the pointer type. */
6846 struct c_declarator *
6847 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6848 struct c_declarator *target)
6852 struct c_declarator *itarget = target;
6853 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6854 if (type_quals_attrs)
6856 attrs = type_quals_attrs->attrs;
6857 quals = quals_from_declspecs (type_quals_attrs);
6858 if (attrs != NULL_TREE)
6859 itarget = build_attrs_declarator (attrs, target);
6861 ret->kind = cdk_pointer;
6862 ret->declarator = itarget;
6863 ret->u.pointer_quals = quals;
6867 /* Return a pointer to a structure for an empty list of declaration
6870 struct c_declspecs *
6871 build_null_declspecs (void)
6873 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6877 ret->typespec_word = cts_none;
6878 ret->storage_class = csc_none;
6879 ret->declspecs_seen_p = false;
6880 ret->type_seen_p = false;
6881 ret->non_sc_seen_p = false;
6882 ret->typedef_p = false;
6883 ret->tag_defined_p = false;
6884 ret->explicit_signed_p = false;
6885 ret->deprecated_p = false;
6886 ret->default_int_p = false;
6887 ret->long_p = false;
6888 ret->long_long_p = false;
6889 ret->short_p = false;
6890 ret->signed_p = false;
6891 ret->unsigned_p = false;
6892 ret->complex_p = false;
6893 ret->inline_p = false;
6894 ret->thread_p = false;
6895 ret->const_p = false;
6896 ret->volatile_p = false;
6897 ret->restrict_p = false;
6901 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6904 struct c_declspecs *
6905 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6909 specs->non_sc_seen_p = true;
6910 specs->declspecs_seen_p = true;
6911 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6912 && C_IS_RESERVED_WORD (qual));
6913 i = C_RID_CODE (qual);
6917 dupe = specs->const_p;
6918 specs->const_p = true;
6921 dupe = specs->volatile_p;
6922 specs->volatile_p = true;
6925 dupe = specs->restrict_p;
6926 specs->restrict_p = true;
6931 if (dupe && pedantic && !flag_isoc99)
6932 pedwarn ("duplicate %qE", qual);
6936 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6939 struct c_declspecs *
6940 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6942 tree type = spec.spec;
6943 specs->non_sc_seen_p = true;
6944 specs->declspecs_seen_p = true;
6945 specs->type_seen_p = true;
6946 if (TREE_DEPRECATED (type))
6947 specs->deprecated_p = true;
6949 /* Handle type specifier keywords. */
6950 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6952 enum rid i = C_RID_CODE (type);
6955 error ("two or more data types in declaration specifiers");
6958 if ((int) i <= (int) RID_LAST_MODIFIER)
6960 /* "long", "short", "signed", "unsigned" or "_Complex". */
6965 if (specs->long_long_p)
6967 error ("%<long long long%> is too long for GCC");
6972 if (specs->typespec_word == cts_double)
6974 error ("both %<long long%> and %<double%> in "
6975 "declaration specifiers");
6978 if (pedantic && !flag_isoc99 && !in_system_header
6980 pedwarn ("ISO C90 does not support %<long long%>");
6981 specs->long_long_p = 1;
6985 error ("both %<long%> and %<short%> in "
6986 "declaration specifiers");
6987 else if (specs->typespec_word == cts_void)
6988 error ("both %<long%> and %<void%> in "
6989 "declaration specifiers");
6990 else if (specs->typespec_word == cts_bool)
6991 error ("both %<long%> and %<_Bool%> in "
6992 "declaration specifiers");
6993 else if (specs->typespec_word == cts_char)
6994 error ("both %<long%> and %<char%> in "
6995 "declaration specifiers");
6996 else if (specs->typespec_word == cts_float)
6997 error ("both %<long%> and %<float%> in "
6998 "declaration specifiers");
7000 specs->long_p = true;
7003 dupe = specs->short_p;
7005 error ("both %<long%> and %<short%> in "
7006 "declaration specifiers");
7007 else if (specs->typespec_word == cts_void)
7008 error ("both %<short%> and %<void%> in "
7009 "declaration specifiers");
7010 else if (specs->typespec_word == cts_bool)
7011 error ("both %<short%> and %<_Bool%> in "
7012 "declaration specifiers");
7013 else if (specs->typespec_word == cts_char)
7014 error ("both %<short%> and %<char%> in "
7015 "declaration specifiers");
7016 else if (specs->typespec_word == cts_float)
7017 error ("both %<short%> and %<float%> in "
7018 "declaration specifiers");
7019 else if (specs->typespec_word == cts_double)
7020 error ("both %<short%> and %<double%> in "
7021 "declaration specifiers");
7023 specs->short_p = true;
7026 dupe = specs->signed_p;
7027 if (specs->unsigned_p)
7028 error ("both %<signed%> and %<unsigned%> in "
7029 "declaration specifiers");
7030 else if (specs->typespec_word == cts_void)
7031 error ("both %<signed%> and %<void%> in "
7032 "declaration specifiers");
7033 else if (specs->typespec_word == cts_bool)
7034 error ("both %<signed%> and %<_Bool%> in "
7035 "declaration specifiers");
7036 else if (specs->typespec_word == cts_float)
7037 error ("both %<signed%> and %<float%> in "
7038 "declaration specifiers");
7039 else if (specs->typespec_word == cts_double)
7040 error ("both %<signed%> and %<double%> in "
7041 "declaration specifiers");
7043 specs->signed_p = true;
7046 dupe = specs->unsigned_p;
7047 if (specs->signed_p)
7048 error ("both %<signed%> and %<unsigned%> in "
7049 "declaration specifiers");
7050 else if (specs->typespec_word == cts_void)
7051 error ("both %<unsigned%> and %<void%> in "
7052 "declaration specifiers");
7053 else if (specs->typespec_word == cts_bool)
7054 error ("both %<unsigned%> and %<_Bool%> in "
7055 "declaration specifiers");
7056 else if (specs->typespec_word == cts_float)
7057 error ("both %<unsigned%> and %<float%> in "
7058 "declaration specifiers");
7059 else if (specs->typespec_word == cts_double)
7060 error ("both %<unsigned%> and %<double%> in "
7061 "declaration specifiers");
7063 specs->unsigned_p = true;
7066 dupe = specs->complex_p;
7067 if (pedantic && !flag_isoc99 && !in_system_header)
7068 pedwarn ("ISO C90 does not support complex types");
7069 if (specs->typespec_word == cts_void)
7070 error ("both %<complex%> and %<void%> in "
7071 "declaration specifiers");
7072 else if (specs->typespec_word == cts_bool)
7073 error ("both %<complex%> and %<_Bool%> in "
7074 "declaration specifiers");
7076 specs->complex_p = true;
7083 error ("duplicate %qE", type);
7089 /* "void", "_Bool", "char", "int", "float" or "double". */
7090 if (specs->typespec_word != cts_none)
7092 error ("two or more data types in declaration specifiers");
7099 error ("both %<long%> and %<void%> in "
7100 "declaration specifiers");
7101 else if (specs->short_p)
7102 error ("both %<short%> and %<void%> in "
7103 "declaration specifiers");
7104 else if (specs->signed_p)
7105 error ("both %<signed%> and %<void%> in "
7106 "declaration specifiers");
7107 else if (specs->unsigned_p)
7108 error ("both %<unsigned%> and %<void%> in "
7109 "declaration specifiers");
7110 else if (specs->complex_p)
7111 error ("both %<complex%> and %<void%> in "
7112 "declaration specifiers");
7114 specs->typespec_word = cts_void;
7118 error ("both %<long%> and %<_Bool%> in "
7119 "declaration specifiers");
7120 else if (specs->short_p)
7121 error ("both %<short%> and %<_Bool%> in "
7122 "declaration specifiers");
7123 else if (specs->signed_p)
7124 error ("both %<signed%> and %<_Bool%> in "
7125 "declaration specifiers");
7126 else if (specs->unsigned_p)
7127 error ("both %<unsigned%> and %<_Bool%> in "
7128 "declaration specifiers");
7129 else if (specs->complex_p)
7130 error ("both %<complex%> and %<_Bool%> in "
7131 "declaration specifiers");
7133 specs->typespec_word = cts_bool;
7137 error ("both %<long%> and %<char%> in "
7138 "declaration specifiers");
7139 else if (specs->short_p)
7140 error ("both %<short%> and %<char%> in "
7141 "declaration specifiers");
7143 specs->typespec_word = cts_char;
7146 specs->typespec_word = cts_int;
7150 error ("both %<long%> and %<float%> in "
7151 "declaration specifiers");
7152 else if (specs->short_p)
7153 error ("both %<short%> and %<float%> in "
7154 "declaration specifiers");
7155 else if (specs->signed_p)
7156 error ("both %<signed%> and %<float%> in "
7157 "declaration specifiers");
7158 else if (specs->unsigned_p)
7159 error ("both %<unsigned%> and %<float%> in "
7160 "declaration specifiers");
7162 specs->typespec_word = cts_float;
7165 if (specs->long_long_p)
7166 error ("both %<long long%> and %<double%> in "
7167 "declaration specifiers");
7168 else if (specs->short_p)
7169 error ("both %<short%> and %<double%> in "
7170 "declaration specifiers");
7171 else if (specs->signed_p)
7172 error ("both %<signed%> and %<double%> in "
7173 "declaration specifiers");
7174 else if (specs->unsigned_p)
7175 error ("both %<unsigned%> and %<double%> in "
7176 "declaration specifiers");
7178 specs->typespec_word = cts_double;
7181 /* ObjC reserved word "id", handled below. */
7187 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7188 form of ObjC type, cases such as "int" and "long" being handled
7189 above), a TYPE (struct, union, enum and typeof specifiers) or an
7190 ERROR_MARK. In none of these cases may there have previously
7191 been any type specifiers. */
7192 if (specs->type || specs->typespec_word != cts_none
7193 || specs->long_p || specs->short_p || specs->signed_p
7194 || specs->unsigned_p || specs->complex_p)
7195 error ("two or more data types in declaration specifiers");
7196 else if (TREE_CODE (type) == TYPE_DECL)
7198 if (TREE_TYPE (type) == error_mark_node)
7199 ; /* Allow the type to default to int to avoid cascading errors. */
7202 specs->type = TREE_TYPE (type);
7203 specs->decl_attr = DECL_ATTRIBUTES (type);
7204 specs->typedef_p = true;
7205 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7208 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7210 tree t = lookup_name (type);
7211 if (!t || TREE_CODE (t) != TYPE_DECL)
7212 error ("%qE fails to be a typedef or built in type", type);
7213 else if (TREE_TYPE (t) == error_mark_node)
7216 specs->type = TREE_TYPE (t);
7218 else if (TREE_CODE (type) != ERROR_MARK)
7220 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7221 specs->tag_defined_p = true;
7222 if (spec.kind == ctsk_typeof)
7223 specs->typedef_p = true;
7230 /* Add the storage class specifier or function specifier SCSPEC to the
7231 declaration specifiers SPECS, returning SPECS. */
7233 struct c_declspecs *
7234 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7237 enum c_storage_class n = csc_none;
7239 specs->declspecs_seen_p = true;
7240 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7241 && C_IS_RESERVED_WORD (scspec));
7242 i = C_RID_CODE (scspec);
7243 if (extra_warnings && specs->non_sc_seen_p)
7244 warning (0, "%qE is not at beginning of declaration", scspec);
7248 /* C99 permits duplicate inline. Although of doubtful utility,
7249 it seems simplest to permit it in gnu89 mode as well, as
7250 there is also little utility in maintaining this as a
7251 difference between gnu89 and C99 inline. */
7253 specs->inline_p = true;
7256 dupe = specs->thread_p;
7257 if (specs->storage_class == csc_auto)
7258 error ("%<__thread%> used with %<auto%>");
7259 else if (specs->storage_class == csc_register)
7260 error ("%<__thread%> used with %<register%>");
7261 else if (specs->storage_class == csc_typedef)
7262 error ("%<__thread%> used with %<typedef%>");
7264 specs->thread_p = true;
7271 /* Diagnose "__thread extern". */
7272 if (specs->thread_p)
7273 error ("%<__thread%> before %<extern%>");
7280 /* Diagnose "__thread static". */
7281 if (specs->thread_p)
7282 error ("%<__thread%> before %<static%>");
7290 if (n != csc_none && n == specs->storage_class)
7293 error ("duplicate %qE", scspec);
7296 if (specs->storage_class != csc_none && n != specs->storage_class)
7298 error ("multiple storage classes in declaration specifiers");
7302 specs->storage_class = n;
7303 if (n != csc_extern && n != csc_static && specs->thread_p)
7305 error ("%<__thread%> used with %qE", scspec);
7306 specs->thread_p = false;
7313 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7316 struct c_declspecs *
7317 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7319 specs->attrs = chainon (attrs, specs->attrs);
7320 specs->declspecs_seen_p = true;
7324 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7325 specifiers with any other type specifier to determine the resulting
7326 type. This is where ISO C checks on complex types are made, since
7327 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7330 struct c_declspecs *
7331 finish_declspecs (struct c_declspecs *specs)
7333 /* If a type was specified as a whole, we have no modifiers and are
7335 if (specs->type != NULL_TREE)
7337 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7338 && !specs->signed_p && !specs->unsigned_p
7339 && !specs->complex_p);
7343 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7344 has been specified, treat it as "int" unless "_Complex" is
7345 present and there are no other specifiers. If we just have
7346 "_Complex", it is equivalent to "_Complex double", but e.g.
7347 "_Complex short" is equivalent to "_Complex short int". */
7348 if (specs->typespec_word == cts_none)
7350 if (specs->long_p || specs->short_p
7351 || specs->signed_p || specs->unsigned_p)
7353 specs->typespec_word = cts_int;
7355 else if (specs->complex_p)
7357 specs->typespec_word = cts_double;
7359 pedwarn ("ISO C does not support plain %<complex%> meaning "
7360 "%<double complex%>");
7364 specs->typespec_word = cts_int;
7365 specs->default_int_p = true;
7366 /* We don't diagnose this here because grokdeclarator will
7367 give more specific diagnostics according to whether it is
7368 a function definition. */
7372 /* If "signed" was specified, record this to distinguish "int" and
7373 "signed int" in the case of a bit-field with
7374 -funsigned-bitfields. */
7375 specs->explicit_signed_p = specs->signed_p;
7377 /* Now compute the actual type. */
7378 switch (specs->typespec_word)
7381 gcc_assert (!specs->long_p && !specs->short_p
7382 && !specs->signed_p && !specs->unsigned_p
7383 && !specs->complex_p);
7384 specs->type = void_type_node;
7387 gcc_assert (!specs->long_p && !specs->short_p
7388 && !specs->signed_p && !specs->unsigned_p
7389 && !specs->complex_p);
7390 specs->type = boolean_type_node;
7393 gcc_assert (!specs->long_p && !specs->short_p);
7394 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7395 if (specs->signed_p)
7396 specs->type = signed_char_type_node;
7397 else if (specs->unsigned_p)
7398 specs->type = unsigned_char_type_node;
7400 specs->type = char_type_node;
7401 if (specs->complex_p)
7404 pedwarn ("ISO C does not support complex integer types");
7405 specs->type = build_complex_type (specs->type);
7409 gcc_assert (!(specs->long_p && specs->short_p));
7410 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7411 if (specs->long_long_p)
7412 specs->type = (specs->unsigned_p
7413 ? long_long_unsigned_type_node
7414 : long_long_integer_type_node);
7415 else if (specs->long_p)
7416 specs->type = (specs->unsigned_p
7417 ? long_unsigned_type_node
7418 : long_integer_type_node);
7419 else if (specs->short_p)
7420 specs->type = (specs->unsigned_p
7421 ? short_unsigned_type_node
7422 : short_integer_type_node);
7424 specs->type = (specs->unsigned_p
7425 ? unsigned_type_node
7426 : integer_type_node);
7427 if (specs->complex_p)
7430 pedwarn ("ISO C does not support complex integer types");
7431 specs->type = build_complex_type (specs->type);
7435 gcc_assert (!specs->long_p && !specs->short_p
7436 && !specs->signed_p && !specs->unsigned_p);
7437 specs->type = (specs->complex_p
7438 ? complex_float_type_node
7442 gcc_assert (!specs->long_long_p && !specs->short_p
7443 && !specs->signed_p && !specs->unsigned_p);
7446 specs->type = (specs->complex_p
7447 ? complex_long_double_type_node
7448 : long_double_type_node);
7452 specs->type = (specs->complex_p
7453 ? complex_double_type_node
7454 : double_type_node);
7464 /* Synthesize a function which calls all the global ctors or global
7465 dtors in this file. This is only used for targets which do not
7466 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7468 build_cdtor (int method_type, tree cdtors)
7475 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7476 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7479 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7482 /* Perform final processing on one file scope's declarations (or the
7483 external scope's declarations), GLOBALS. */
7485 c_write_global_declarations_1 (tree globals)
7487 size_t len = list_length (globals);
7488 tree *vec = XNEWVEC (tree, len);
7492 /* Process the decls in the order they were written. */
7493 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7496 /* Check for used but undefined static functions using the C
7497 standard's definition of "used", and set TREE_NO_WARNING so
7498 that check_global_declarations doesn't repeat the check. */
7499 if (TREE_CODE (decl) == FUNCTION_DECL
7500 && DECL_INITIAL (decl) == 0
7501 && DECL_EXTERNAL (decl)
7502 && !TREE_PUBLIC (decl)
7503 && C_DECL_USED (decl))
7505 pedwarn ("%J%qF used but never defined", decl, decl);
7506 TREE_NO_WARNING (decl) = 1;
7510 wrapup_global_declarations (vec, len);
7511 check_global_declarations (vec, len);
7517 c_write_global_declarations (void)
7521 /* We don't want to do this if generating a PCH. */
7525 /* Don't waste time on further processing if -fsyntax-only or we've
7526 encountered errors. */
7527 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7530 /* Close the external scope. */
7531 ext_block = pop_scope ();
7533 gcc_assert (!current_scope);
7535 /* Process all file scopes in this compilation, and the external_scope,
7536 through wrapup_global_declarations and check_global_declarations. */
7537 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7538 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7539 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7541 /* Generate functions to call static constructors and destructors
7542 for targets that do not support .ctors/.dtors sections. These
7543 functions have magic names which are detected by collect2. */
7544 build_cdtor ('I', static_ctors); static_ctors = 0;
7545 build_cdtor ('D', static_dtors); static_dtors = 0;
7547 /* We're done parsing; proceed to optimize and emit assembly.
7548 FIXME: shouldn't be the front end's responsibility to call this. */
7552 #include "gt-c-decl.h"