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",
1157 else if (warn_shadow)
1158 warning (0, "%Jdeclaration of %qD shadows a built-in function",
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)))))
1274 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1276 /* Discard the old built-in function. */
1280 if (DECL_INITIAL (newdecl))
1282 if (DECL_INITIAL (olddecl))
1284 /* If both decls have extern inline and are in the same TU,
1285 reject the new decl. */
1286 if (DECL_DECLARED_INLINE_P (olddecl)
1287 && DECL_EXTERNAL (olddecl)
1288 && DECL_DECLARED_INLINE_P (newdecl)
1289 && DECL_EXTERNAL (newdecl)
1290 && same_translation_unit_p (newdecl, olddecl))
1292 error ("%Jredefinition of %qD", newdecl, newdecl);
1293 locate_old_decl (olddecl, error);
1296 /* If both decls have not extern inline, reject the new decl. */
1297 if (!DECL_DECLARED_INLINE_P (olddecl)
1298 && !DECL_EXTERNAL (olddecl)
1299 && !DECL_DECLARED_INLINE_P (newdecl)
1300 && !DECL_EXTERNAL (newdecl))
1302 error ("%Jredefinition of %qD", newdecl, newdecl);
1303 locate_old_decl (olddecl, error);
1306 /* If the new decl is declared as extern inline, error if they are
1307 in the same TU, otherwise retain the old decl. */
1308 if (!DECL_DECLARED_INLINE_P (olddecl)
1309 && !DECL_EXTERNAL (olddecl)
1310 && DECL_DECLARED_INLINE_P (newdecl)
1311 && DECL_EXTERNAL (newdecl))
1313 if (same_translation_unit_p (newdecl, olddecl))
1315 error ("%Jredefinition of %qD", newdecl, newdecl);
1316 locate_old_decl (olddecl, error);
1324 /* If we have a prototype after an old-style function definition,
1325 the argument types must be checked specially. */
1326 else if (DECL_INITIAL (olddecl)
1327 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1328 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1329 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1331 locate_old_decl (olddecl, error);
1334 /* A non-static declaration (even an "extern") followed by a
1335 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1336 The same is true for a static forward declaration at block
1337 scope followed by a non-static declaration/definition at file
1338 scope. Static followed by non-static at the same scope is
1339 not undefined behavior, and is the most convenient way to get
1340 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1341 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1342 we do diagnose it if -Wtraditional. */
1343 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1345 /* Two exceptions to the rule. If olddecl is an extern
1346 inline, or a predeclared function that isn't actually
1347 built in, newdecl silently overrides olddecl. The latter
1348 occur only in Objective C; see also above. (FIXME: Make
1349 Objective C use normal builtins.) */
1350 if (!DECL_IS_BUILTIN (olddecl)
1351 && !(DECL_EXTERNAL (olddecl)
1352 && DECL_DECLARED_INLINE_P (olddecl)))
1354 error ("%Jstatic declaration of %qD follows "
1355 "non-static declaration", newdecl, newdecl);
1356 locate_old_decl (olddecl, error);
1360 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1362 if (DECL_CONTEXT (olddecl))
1364 error ("%Jnon-static declaration of %qD follows "
1365 "static declaration", newdecl, newdecl);
1366 locate_old_decl (olddecl, error);
1369 else if (warn_traditional)
1371 warning (0, "%Jnon-static declaration of %qD follows "
1372 "static declaration", newdecl, newdecl);
1377 else if (TREE_CODE (newdecl) == VAR_DECL)
1379 /* Only variables can be thread-local, and all declarations must
1380 agree on this property. */
1381 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1383 if (DECL_THREAD_LOCAL (newdecl))
1384 error ("%Jthread-local declaration of %qD follows "
1385 "non-thread-local declaration", newdecl, newdecl);
1387 error ("%Jnon-thread-local declaration of %qD follows "
1388 "thread-local declaration", newdecl, newdecl);
1390 locate_old_decl (olddecl, error);
1394 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1395 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1397 error ("%Jredefinition of %qD", newdecl, newdecl);
1398 locate_old_decl (olddecl, error);
1402 /* Objects declared at file scope: if the first declaration had
1403 external linkage (even if it was an external reference) the
1404 second must have external linkage as well, or the behavior is
1405 undefined. If the first declaration had internal linkage, then
1406 the second must too, or else be an external reference (in which
1407 case the composite declaration still has internal linkage).
1408 As for function declarations, we warn about the static-then-
1409 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1410 if (DECL_FILE_SCOPE_P (newdecl)
1411 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1413 if (DECL_EXTERNAL (newdecl))
1415 if (!DECL_FILE_SCOPE_P (olddecl))
1417 error ("%Jextern declaration of %qD follows "
1418 "declaration with no linkage", newdecl, newdecl);
1419 locate_old_decl (olddecl, error);
1422 else if (warn_traditional)
1424 warning (0, "%Jnon-static declaration of %qD follows "
1425 "static declaration", newdecl, newdecl);
1431 if (TREE_PUBLIC (newdecl))
1432 error ("%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1435 error ("%Jstatic declaration of %qD follows "
1436 "non-static declaration", newdecl, newdecl);
1438 locate_old_decl (olddecl, error);
1442 /* Two objects with the same name declared at the same block
1443 scope must both be external references (6.7p3). */
1444 else if (!DECL_FILE_SCOPE_P (newdecl))
1446 if (DECL_EXTERNAL (newdecl))
1448 /* Extern with initializer at block scope, which will
1449 already have received an error. */
1451 else if (DECL_EXTERNAL (olddecl))
1453 error ("%Jdeclaration of %qD with no linkage follows "
1454 "extern declaration", newdecl, newdecl);
1455 locate_old_decl (olddecl, error);
1459 error ("%Jredeclaration of %qD with no linkage",
1461 locate_old_decl (olddecl, error);
1469 /* All decls must agree on a visibility. */
1470 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1471 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1473 warning (0, "%Jredeclaration of %qD with different visibility "
1474 "(old visibility preserved)", newdecl, newdecl);
1478 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1480 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1481 if (DECL_DECLARED_INLINE_P (newdecl)
1482 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1484 warning (0, "%Jinline declaration of %qD follows "
1485 "declaration with attribute noinline", newdecl, newdecl);
1488 else if (DECL_DECLARED_INLINE_P (olddecl)
1489 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1491 warning (0, "%Jdeclaration of %qD with attribute noinline follows "
1492 "inline declaration ", newdecl, newdecl);
1496 /* Inline declaration after use or definition.
1497 ??? Should we still warn about this now we have unit-at-a-time
1498 mode and can get it right?
1499 Definitely don't complain if the decls are in different translation
1501 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1502 && same_translation_unit_p (olddecl, newdecl))
1504 if (TREE_USED (olddecl))
1506 warning (0, "%J%qD declared inline after being called",
1510 else if (DECL_INITIAL (olddecl))
1512 warning (0, "%J%qD declared inline after its definition",
1518 else /* PARM_DECL, VAR_DECL */
1520 /* Redeclaration of a parameter is a constraint violation (this is
1521 not explicitly stated, but follows from C99 6.7p3 [no more than
1522 one declaration of the same identifier with no linkage in the
1523 same scope, except type tags] and 6.2.2p6 [parameters have no
1524 linkage]). We must check for a forward parameter declaration,
1525 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1526 an extension, the mandatory diagnostic for which is handled by
1527 mark_forward_parm_decls. */
1529 if (TREE_CODE (newdecl) == PARM_DECL
1530 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1532 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1533 locate_old_decl (olddecl, error);
1538 /* Optional warning for completely redundant decls. */
1539 if (!warned && !pedwarned
1540 && warn_redundant_decls
1541 /* Don't warn about a function declaration followed by a
1543 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1544 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1545 /* Don't warn about redundant redeclarations of builtins. */
1546 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1547 && !DECL_BUILT_IN (newdecl)
1548 && DECL_BUILT_IN (olddecl)
1549 && !C_DECL_DECLARED_BUILTIN (olddecl))
1550 /* Don't warn about an extern followed by a definition. */
1551 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1552 /* Don't warn about forward parameter decls. */
1553 && !(TREE_CODE (newdecl) == PARM_DECL
1554 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1556 warning (0, "%Jredundant redeclaration of %qD", newdecl, newdecl);
1560 /* Report location of previous decl/defn in a consistent manner. */
1561 if (warned || pedwarned)
1562 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1567 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1568 consistent with OLDDECL, but carries new information. Merge the
1569 new information into OLDDECL. This function issues no
1573 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1575 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1576 && DECL_INITIAL (newdecl) != 0);
1577 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1578 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1579 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1580 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1582 /* For real parm decl following a forward decl, rechain the old decl
1583 in its new location and clear TREE_ASM_WRITTEN (it's not a
1584 forward decl anymore). */
1585 if (TREE_CODE (newdecl) == PARM_DECL
1586 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1588 struct c_binding *b, **here;
1590 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1591 if ((*here)->decl == olddecl)
1598 b->prev = current_scope->bindings;
1599 current_scope->bindings = b;
1601 TREE_ASM_WRITTEN (olddecl) = 0;
1604 DECL_ATTRIBUTES (newdecl)
1605 = targetm.merge_decl_attributes (olddecl, newdecl);
1607 /* Merge the data types specified in the two decls. */
1609 = TREE_TYPE (olddecl)
1610 = composite_type (newtype, oldtype);
1612 /* Lay the type out, unless already done. */
1613 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1615 if (TREE_TYPE (newdecl) != error_mark_node)
1616 layout_type (TREE_TYPE (newdecl));
1617 if (TREE_CODE (newdecl) != FUNCTION_DECL
1618 && TREE_CODE (newdecl) != TYPE_DECL
1619 && TREE_CODE (newdecl) != CONST_DECL)
1620 layout_decl (newdecl, 0);
1624 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1625 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1626 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1627 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1628 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1629 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1631 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1632 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1636 /* Keep the old rtl since we can safely use it. */
1637 COPY_DECL_RTL (olddecl, newdecl);
1639 /* Merge the type qualifiers. */
1640 if (TREE_READONLY (newdecl))
1641 TREE_READONLY (olddecl) = 1;
1643 if (TREE_THIS_VOLATILE (newdecl))
1645 TREE_THIS_VOLATILE (olddecl) = 1;
1646 if (TREE_CODE (newdecl) == VAR_DECL)
1647 make_var_volatile (newdecl);
1650 /* Merge deprecatedness. */
1651 if (TREE_DEPRECATED (newdecl))
1652 TREE_DEPRECATED (olddecl) = 1;
1654 /* Keep source location of definition rather than declaration and of
1655 prototype rather than non-prototype unless that prototype is
1657 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1658 || (old_is_prototype && !new_is_prototype
1659 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1660 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1662 /* Merge the unused-warning information. */
1663 if (DECL_IN_SYSTEM_HEADER (olddecl))
1664 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1665 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1666 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1668 /* Merge the initialization information. */
1669 if (DECL_INITIAL (newdecl) == 0)
1670 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1672 /* Merge the section attribute.
1673 We want to issue an error if the sections conflict but that must be
1674 done later in decl_attributes since we are called before attributes
1676 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1677 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1679 /* Copy the assembler name.
1680 Currently, it can only be defined in the prototype. */
1681 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1683 /* Use visibility of whichever declaration had it specified */
1684 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1686 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1687 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1690 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1692 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1693 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1694 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1695 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1696 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1697 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1698 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1699 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1700 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1701 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1704 /* Merge the storage class information. */
1705 merge_weak (newdecl, olddecl);
1707 /* For functions, static overrides non-static. */
1708 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1710 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1711 /* This is since we don't automatically
1712 copy the attributes of NEWDECL into OLDDECL. */
1713 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1714 /* If this clears `static', clear it in the identifier too. */
1715 if (!TREE_PUBLIC (olddecl))
1716 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1718 if (DECL_EXTERNAL (newdecl))
1720 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1721 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1723 /* An extern decl does not override previous storage class. */
1724 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1725 if (!DECL_EXTERNAL (newdecl))
1727 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1728 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1733 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1734 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1737 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739 /* If we're redefining a function previously defined as extern
1740 inline, make sure we emit debug info for the inline before we
1741 throw it away, in case it was inlined into a function that hasn't
1742 been written out yet. */
1743 if (new_is_definition && DECL_INITIAL (olddecl))
1745 if (TREE_USED (olddecl)
1746 /* In unit-at-a-time mode we never inline re-defined extern
1747 inline functions. */
1748 && !flag_unit_at_a_time
1749 && cgraph_function_possibly_inlined_p (olddecl))
1750 (*debug_hooks->outlining_inline_function) (olddecl);
1752 /* The new defn must not be inline. */
1753 DECL_INLINE (newdecl) = 0;
1754 DECL_UNINLINABLE (newdecl) = 1;
1758 /* If either decl says `inline', this fn is inline,
1759 unless its definition was passed already. */
1760 if (DECL_DECLARED_INLINE_P (newdecl)
1761 || DECL_DECLARED_INLINE_P (olddecl))
1762 DECL_DECLARED_INLINE_P (newdecl) = 1;
1764 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1765 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1768 if (DECL_BUILT_IN (olddecl))
1770 /* If redeclaring a builtin function, it stays built in.
1771 But it gets tagged as having been declared. */
1772 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1773 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1774 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1775 if (new_is_prototype)
1776 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1778 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1779 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1782 /* Also preserve various other info from the definition. */
1783 if (!new_is_definition)
1785 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1786 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1787 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1788 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1789 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1791 /* Set DECL_INLINE on the declaration if we've got a body
1792 from which to instantiate. */
1793 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1795 DECL_INLINE (newdecl) = 1;
1796 DECL_ABSTRACT_ORIGIN (newdecl)
1797 = DECL_ABSTRACT_ORIGIN (olddecl);
1802 /* If a previous declaration said inline, mark the
1803 definition as inlinable. */
1804 if (DECL_DECLARED_INLINE_P (newdecl)
1805 && !DECL_UNINLINABLE (newdecl))
1806 DECL_INLINE (newdecl) = 1;
1810 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1811 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1813 unsigned olddecl_uid = DECL_UID (olddecl);
1814 tree olddecl_context = DECL_CONTEXT (olddecl);
1816 memcpy ((char *) olddecl + sizeof (struct tree_common),
1817 (char *) newdecl + sizeof (struct tree_common),
1818 sizeof (struct tree_decl) - sizeof (struct tree_common));
1819 DECL_UID (olddecl) = olddecl_uid;
1820 DECL_CONTEXT (olddecl) = olddecl_context;
1823 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1824 so that encode_section_info has a chance to look at the new decl
1825 flags and attributes. */
1826 if (DECL_RTL_SET_P (olddecl)
1827 && (TREE_CODE (olddecl) == FUNCTION_DECL
1828 || (TREE_CODE (olddecl) == VAR_DECL
1829 && TREE_STATIC (olddecl))))
1830 make_decl_rtl (olddecl);
1833 /* Handle when a new declaration NEWDECL has the same name as an old
1834 one OLDDECL in the same binding contour. Prints an error message
1837 If safely possible, alter OLDDECL to look like NEWDECL, and return
1838 true. Otherwise, return false. */
1841 duplicate_decls (tree newdecl, tree olddecl)
1843 tree newtype = NULL, oldtype = NULL;
1845 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1848 merge_decls (newdecl, olddecl, newtype, oldtype);
1853 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1855 warn_if_shadowing (tree new_decl)
1857 struct c_binding *b;
1859 /* Shadow warnings wanted? */
1861 /* No shadow warnings for internally generated vars. */
1862 || DECL_IS_BUILTIN (new_decl)
1863 /* No shadow warnings for vars made for inlining. */
1864 || DECL_FROM_INLINE (new_decl)
1865 /* Don't warn about the parm names in function declarator
1866 within a function declarator. It would be nice to avoid
1867 warning in any function declarator in a declaration, as
1868 opposed to a definition, but there is no way to tell
1869 it's not a definition at this point. */
1870 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1873 /* Is anything being shadowed? Invisible decls do not count. */
1874 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1875 if (b->decl && b->decl != new_decl && !b->invisible)
1877 tree old_decl = b->decl;
1879 if (old_decl == error_mark_node)
1881 warning (0, "%Jdeclaration of %qD shadows previous non-variable",
1882 new_decl, new_decl);
1885 else if (TREE_CODE (old_decl) == PARM_DECL)
1886 warning (0, "%Jdeclaration of %qD shadows a parameter",
1887 new_decl, new_decl);
1888 else if (DECL_FILE_SCOPE_P (old_decl))
1889 warning (0, "%Jdeclaration of %qD shadows a global declaration",
1890 new_decl, new_decl);
1891 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1892 && DECL_BUILT_IN (old_decl))
1894 warning (0, "%Jdeclaration of %qD shadows a built-in function",
1895 new_decl, new_decl);
1899 warning (0, "%Jdeclaration of %qD shadows a previous local",
1900 new_decl, new_decl);
1902 warning (0, "%Jshadowed declaration is here", old_decl);
1909 /* Subroutine of pushdecl.
1911 X is a TYPE_DECL for a typedef statement. Create a brand new
1912 ..._TYPE node (which will be just a variant of the existing
1913 ..._TYPE node with identical properties) and then install X
1914 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1916 The whole point here is to end up with a situation where each
1917 and every ..._TYPE node the compiler creates will be uniquely
1918 associated with AT MOST one node representing a typedef name.
1919 This way, even though the compiler substitutes corresponding
1920 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1921 early on, later parts of the compiler can always do the reverse
1922 translation and get back the corresponding typedef name. For
1925 typedef struct S MY_TYPE;
1928 Later parts of the compiler might only know that `object' was of
1929 type `struct S' if it were not for code just below. With this
1930 code however, later parts of the compiler see something like:
1932 struct S' == struct S
1933 typedef struct S' MY_TYPE;
1936 And they can then deduce (from the node for type struct S') that
1937 the original object declaration was:
1941 Being able to do this is important for proper support of protoize,
1942 and also for generating precise symbolic debugging information
1943 which takes full account of the programmer's (typedef) vocabulary.
1945 Obviously, we don't want to generate a duplicate ..._TYPE node if
1946 the TYPE_DECL node that we are now processing really represents a
1947 standard built-in type.
1949 Since all standard types are effectively declared at line zero
1950 in the source file, we can easily check to see if we are working
1951 on a standard type by checking the current value of lineno. */
1954 clone_underlying_type (tree x)
1956 if (DECL_IS_BUILTIN (x))
1958 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1959 TYPE_NAME (TREE_TYPE (x)) = x;
1961 else if (TREE_TYPE (x) != error_mark_node
1962 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1964 tree tt = TREE_TYPE (x);
1965 DECL_ORIGINAL_TYPE (x) = tt;
1966 tt = build_variant_type_copy (tt);
1968 TREE_USED (tt) = TREE_USED (x);
1973 /* Record a decl-node X as belonging to the current lexical scope.
1974 Check for errors (such as an incompatible declaration for the same
1975 name already seen in the same scope).
1977 Returns either X or an old decl for the same name.
1978 If an old decl is returned, it may have been smashed
1979 to agree with what X says. */
1984 tree name = DECL_NAME (x);
1985 struct c_scope *scope = current_scope;
1986 struct c_binding *b;
1987 bool nested = false;
1989 /* Functions need the lang_decl data. */
1990 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
1991 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1993 /* Must set DECL_CONTEXT for everything not at file scope or
1994 DECL_FILE_SCOPE_P won't work. Local externs don't count
1995 unless they have initializers (which generate code). */
1996 if (current_function_decl
1997 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1998 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1999 DECL_CONTEXT (x) = current_function_decl;
2001 /* If this is of variably modified type, prevent jumping into its
2003 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2004 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2005 c_begin_vm_scope (scope->depth);
2007 /* Anonymous decls are just inserted in the scope. */
2010 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2014 /* First, see if there is another declaration with the same name in
2015 the current scope. If there is, duplicate_decls may do all the
2016 work for us. If duplicate_decls returns false, that indicates
2017 two incompatible decls in the same scope; we are to silently
2018 replace the old one (duplicate_decls has issued all appropriate
2019 diagnostics). In particular, we should not consider possible
2020 duplicates in the external scope, or shadowing. */
2021 b = I_SYMBOL_BINDING (name);
2022 if (b && B_IN_SCOPE (b, scope))
2024 struct c_binding *b_ext, *b_use;
2025 tree type = TREE_TYPE (x);
2026 tree visdecl = b->decl;
2027 tree vistype = TREE_TYPE (visdecl);
2028 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2029 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2030 b->inner_comp = false;
2033 /* If this is an external linkage declaration, we should check
2034 for compatibility with the type in the external scope before
2035 setting the type at this scope based on the visible
2036 information only. */
2037 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2039 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2040 b_ext = b_ext->shadowed;
2045 TREE_TYPE (b_use->decl) = b_use->type;
2048 if (duplicate_decls (x, b_use->decl))
2052 /* Save the updated type in the external scope and
2053 restore the proper type for this scope. */
2055 if (comptypes (vistype, type))
2056 thistype = composite_type (vistype, type);
2058 thistype = TREE_TYPE (b_use->decl);
2059 b_use->type = TREE_TYPE (b_use->decl);
2060 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2061 && DECL_BUILT_IN (b_use->decl))
2063 = build_type_attribute_variant (thistype,
2066 TREE_TYPE (b_use->decl) = thistype;
2071 goto skip_external_and_shadow_checks;
2074 /* All declarations with external linkage, and all external
2075 references, go in the external scope, no matter what scope is
2076 current. However, the binding in that scope is ignored for
2077 purposes of normal name lookup. A separate binding structure is
2078 created in the requested scope; this governs the normal
2079 visibility of the symbol.
2081 The binding in the externals scope is used exclusively for
2082 detecting duplicate declarations of the same object, no matter
2083 what scope they are in; this is what we do here. (C99 6.2.7p2:
2084 All declarations that refer to the same object or function shall
2085 have compatible type; otherwise, the behavior is undefined.) */
2086 if (DECL_EXTERNAL (x) || scope == file_scope)
2088 tree type = TREE_TYPE (x);
2091 bool type_saved = false;
2092 if (b && !B_IN_EXTERNAL_SCOPE (b)
2093 && (TREE_CODE (b->decl) == FUNCTION_DECL
2094 || TREE_CODE (b->decl) == VAR_DECL)
2095 && DECL_FILE_SCOPE_P (b->decl))
2098 vistype = TREE_TYPE (visdecl);
2100 if (warn_nested_externs
2101 && scope != file_scope
2102 && !DECL_IN_SYSTEM_HEADER (x))
2103 warning (0, "nested extern declaration of %qD", x);
2105 while (b && !B_IN_EXTERNAL_SCOPE (b))
2107 /* If this decl might be modified, save its type. This is
2108 done here rather than when the decl is first bound
2109 because the type may change after first binding, through
2110 being completed or through attributes being added. If we
2111 encounter multiple such decls, only the first should have
2112 its type saved; the others will already have had their
2113 proper types saved and the types will not have changed as
2114 their scopes will not have been re-entered. */
2115 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2117 b->type = TREE_TYPE (b->decl);
2120 if (B_IN_FILE_SCOPE (b)
2121 && TREE_CODE (b->decl) == VAR_DECL
2122 && TREE_STATIC (b->decl)
2123 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2124 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2125 && TREE_CODE (type) == ARRAY_TYPE
2126 && TYPE_DOMAIN (type)
2127 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2128 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2130 /* Array type completed in inner scope, which should be
2131 diagnosed if the completion does not have size 1 and
2132 it does not get completed in the file scope. */
2133 b->inner_comp = true;
2138 /* If a matching external declaration has been found, set its
2139 type to the composite of all the types of that declaration.
2140 After the consistency checks, it will be reset to the
2141 composite of the visible types only. */
2142 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2144 TREE_TYPE (b->decl) = b->type;
2146 /* The point of the same_translation_unit_p check here is,
2147 we want to detect a duplicate decl for a construct like
2148 foo() { extern bar(); } ... static bar(); but not if
2149 they are in different translation units. In any case,
2150 the static does not go in the externals scope. */
2152 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2153 && duplicate_decls (x, b->decl))
2158 if (comptypes (vistype, type))
2159 thistype = composite_type (vistype, type);
2161 thistype = TREE_TYPE (b->decl);
2165 b->type = TREE_TYPE (b->decl);
2166 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2168 = build_type_attribute_variant (thistype,
2169 TYPE_ATTRIBUTES (b->type));
2170 TREE_TYPE (b->decl) = thistype;
2171 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2174 else if (TREE_PUBLIC (x))
2176 if (visdecl && !b && duplicate_decls (x, visdecl))
2178 /* An external declaration at block scope referring to a
2179 visible entity with internal linkage. The composite
2180 type will already be correct for this scope, so we
2181 just need to fall through to make the declaration in
2188 bind (name, x, external_scope, /*invisible=*/true,
2195 warn_if_shadowing (x);
2197 skip_external_and_shadow_checks:
2198 if (TREE_CODE (x) == TYPE_DECL)
2199 clone_underlying_type (x);
2201 bind (name, x, scope, /*invisible=*/false, nested);
2203 /* If x's type is incomplete because it's based on a
2204 structure or union which has not yet been fully declared,
2205 attach it to that structure or union type, so we can go
2206 back and complete the variable declaration later, if the
2207 structure or union gets fully declared.
2209 If the input is erroneous, we can have error_mark in the type
2210 slot (e.g. "f(void a, ...)") - that doesn't count as an
2212 if (TREE_TYPE (x) != error_mark_node
2213 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2215 tree element = TREE_TYPE (x);
2217 while (TREE_CODE (element) == ARRAY_TYPE)
2218 element = TREE_TYPE (element);
2219 element = TYPE_MAIN_VARIANT (element);
2221 if ((TREE_CODE (element) == RECORD_TYPE
2222 || TREE_CODE (element) == UNION_TYPE)
2223 && (TREE_CODE (x) != TYPE_DECL
2224 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2225 && !COMPLETE_TYPE_P (element))
2226 C_TYPE_INCOMPLETE_VARS (element)
2227 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2232 /* Record X as belonging to file scope.
2233 This is used only internally by the Objective-C front end,
2234 and is limited to its needs. duplicate_decls is not called;
2235 if there is any preexisting decl for this identifier, it is an ICE. */
2238 pushdecl_top_level (tree x)
2241 bool nested = false;
2242 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2244 name = DECL_NAME (x);
2246 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2248 if (TREE_PUBLIC (x))
2250 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2254 bind (name, x, file_scope, /*invisible=*/false, nested);
2260 implicit_decl_warning (tree id, tree olddecl)
2262 void (*diag) (const char *, ...);
2263 switch (mesg_implicit_function_declaration)
2266 case 1: diag = warning0; break;
2267 case 2: diag = error; break;
2268 default: gcc_unreachable ();
2271 diag (N_("implicit declaration of function %qE"), id);
2273 locate_old_decl (olddecl, diag);
2276 /* Generate an implicit declaration for identifier FUNCTIONID as a
2277 function of type int (). */
2280 implicitly_declare (tree functionid)
2282 struct c_binding *b;
2286 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2288 if (B_IN_SCOPE (b, external_scope))
2297 if (decl == error_mark_node)
2300 /* FIXME: Objective-C has weird not-really-builtin functions
2301 which are supposed to be visible automatically. They wind up
2302 in the external scope because they're pushed before the file
2303 scope gets created. Catch this here and rebind them into the
2305 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2307 bind (functionid, decl, file_scope,
2308 /*invisible=*/false, /*nested=*/true);
2313 tree newtype = default_function_type;
2315 TREE_TYPE (decl) = b->type;
2316 /* Implicit declaration of a function already declared
2317 (somehow) in a different scope, or as a built-in.
2318 If this is the first time this has happened, warn;
2319 then recycle the old declaration but with the new type. */
2320 if (!C_DECL_IMPLICIT (decl))
2322 implicit_decl_warning (functionid, decl);
2323 C_DECL_IMPLICIT (decl) = 1;
2325 if (DECL_BUILT_IN (decl))
2327 newtype = build_type_attribute_variant (newtype,
2329 (TREE_TYPE (decl)));
2330 if (!comptypes (newtype, TREE_TYPE (decl)))
2332 warning (0, "incompatible implicit declaration of built-in"
2333 " function %qD", decl);
2334 newtype = TREE_TYPE (decl);
2339 if (!comptypes (newtype, TREE_TYPE (decl)))
2341 error ("incompatible implicit declaration of function %qD",
2343 locate_old_decl (decl, error);
2346 b->type = TREE_TYPE (decl);
2347 TREE_TYPE (decl) = newtype;
2348 bind (functionid, decl, current_scope,
2349 /*invisible=*/false, /*nested=*/true);
2354 /* Not seen before. */
2355 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2356 DECL_EXTERNAL (decl) = 1;
2357 TREE_PUBLIC (decl) = 1;
2358 C_DECL_IMPLICIT (decl) = 1;
2359 implicit_decl_warning (functionid, 0);
2360 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2362 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2364 /* C89 says implicit declarations are in the innermost block.
2365 So we record the decl in the standard fashion. */
2366 decl = pushdecl (decl);
2368 /* No need to call objc_check_decl here - it's a function type. */
2369 rest_of_decl_compilation (decl, 0, 0);
2371 /* Write a record describing this implicit function declaration
2372 to the prototypes file (if requested). */
2373 gen_aux_info_record (decl, 0, 1, 0);
2375 /* Possibly apply some default attributes to this implicit declaration. */
2376 decl_attributes (&decl, NULL_TREE, 0);
2381 /* Issue an error message for a reference to an undeclared variable
2382 ID, including a reference to a builtin outside of function-call
2383 context. Establish a binding of the identifier to error_mark_node
2384 in an appropriate scope, which will suppress further errors for the
2385 same identifier. The error message should be given location LOC. */
2387 undeclared_variable (tree id, location_t loc)
2389 static bool already = false;
2390 struct c_scope *scope;
2392 if (current_function_decl == 0)
2394 error ("%H%qE undeclared here (not in a function)", &loc, id);
2395 scope = current_scope;
2399 error ("%H%qE undeclared (first use in this function)", &loc, id);
2403 error ("%H(Each undeclared identifier is reported only once", &loc);
2404 error ("%Hfor each function it appears in.)", &loc);
2408 /* If we are parsing old-style parameter decls, current_function_decl
2409 will be nonnull but current_function_scope will be null. */
2410 scope = current_function_scope ? current_function_scope : current_scope;
2412 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2415 /* Subroutine of lookup_label, declare_label, define_label: construct a
2416 LABEL_DECL with all the proper frills. */
2419 make_label (tree name, location_t location)
2421 tree label = build_decl (LABEL_DECL, name, void_type_node);
2423 DECL_CONTEXT (label) = current_function_decl;
2424 DECL_MODE (label) = VOIDmode;
2425 DECL_SOURCE_LOCATION (label) = location;
2430 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2431 Create one if none exists so far for the current function.
2432 This is called when a label is used in a goto expression or
2433 has its address taken. */
2436 lookup_label (tree name)
2440 if (current_function_decl == 0)
2442 error ("label %qE referenced outside of any function", name);
2446 /* Use a label already defined or ref'd with this name, but not if
2447 it is inherited from a containing function and wasn't declared
2449 label = I_LABEL_DECL (name);
2450 if (label && (DECL_CONTEXT (label) == current_function_decl
2451 || C_DECLARED_LABEL_FLAG (label)))
2453 /* If the label has only been declared, update its apparent
2454 location to point here, for better diagnostics if it
2455 turns out not to have been defined. */
2456 if (!TREE_USED (label))
2457 DECL_SOURCE_LOCATION (label) = input_location;
2461 /* No label binding for that identifier; make one. */
2462 label = make_label (name, input_location);
2464 /* Ordinary labels go in the current function scope. */
2465 bind (name, label, current_function_scope,
2466 /*invisible=*/false, /*nested=*/false);
2470 /* Make a label named NAME in the current function, shadowing silently
2471 any that may be inherited from containing functions or containing
2472 scopes. This is called for __label__ declarations. */
2475 declare_label (tree name)
2477 struct c_binding *b = I_LABEL_BINDING (name);
2480 /* Check to make sure that the label hasn't already been declared
2482 if (b && B_IN_CURRENT_SCOPE (b))
2484 error ("duplicate label declaration %qE", name);
2485 locate_old_decl (b->decl, error);
2487 /* Just use the previous declaration. */
2491 label = make_label (name, input_location);
2492 C_DECLARED_LABEL_FLAG (label) = 1;
2494 /* Declared labels go in the current scope. */
2495 bind (name, label, current_scope,
2496 /*invisible=*/false, /*nested=*/false);
2500 /* Define a label, specifying the location in the source file.
2501 Return the LABEL_DECL node for the label, if the definition is valid.
2502 Otherwise return 0. */
2505 define_label (location_t location, tree name)
2507 /* Find any preexisting label with this name. It is an error
2508 if that label has already been defined in this function, or
2509 if there is a containing function with a declared label with
2511 tree label = I_LABEL_DECL (name);
2512 struct c_label_list *nlist_se, *nlist_vm;
2515 && ((DECL_CONTEXT (label) == current_function_decl
2516 && DECL_INITIAL (label) != 0)
2517 || (DECL_CONTEXT (label) != current_function_decl
2518 && C_DECLARED_LABEL_FLAG (label))))
2520 error ("%Hduplicate label %qD", &location, label);
2521 locate_old_decl (label, error);
2524 else if (label && DECL_CONTEXT (label) == current_function_decl)
2526 /* The label has been used or declared already in this function,
2527 but not defined. Update its location to point to this
2529 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2530 error ("%Jjump into statement expression", label);
2531 if (C_DECL_UNDEFINABLE_VM (label))
2532 error ("%Jjump into scope of identifier with variably modified type",
2534 DECL_SOURCE_LOCATION (label) = location;
2538 /* No label binding for that identifier; make one. */
2539 label = make_label (name, location);
2541 /* Ordinary labels go in the current function scope. */
2542 bind (name, label, current_function_scope,
2543 /*invisible=*/false, /*nested=*/false);
2546 if (warn_traditional && !in_system_header && lookup_name (name))
2547 warning (0, "%Htraditional C lacks a separate namespace for labels, "
2548 "identifier %qE conflicts", &location, name);
2550 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2551 nlist_se->next = label_context_stack_se->labels_def;
2552 nlist_se->label = label;
2553 label_context_stack_se->labels_def = nlist_se;
2555 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2556 nlist_vm->next = label_context_stack_vm->labels_def;
2557 nlist_vm->label = label;
2558 label_context_stack_vm->labels_def = nlist_vm;
2560 /* Mark label as having been defined. */
2561 DECL_INITIAL (label) = error_mark_node;
2565 /* Given NAME, an IDENTIFIER_NODE,
2566 return the structure (or union or enum) definition for that name.
2567 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2568 CODE says which kind of type the caller wants;
2569 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2570 If the wrong kind of type is found, an error is reported. */
2573 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2575 struct c_binding *b = I_TAG_BINDING (name);
2581 /* We only care about whether it's in this level if
2582 thislevel_only was set or it might be a type clash. */
2583 if (thislevel_only || TREE_CODE (b->decl) != code)
2585 /* For our purposes, a tag in the external scope is the same as
2586 a tag in the file scope. (Primarily relevant to Objective-C
2587 and its builtin structure tags, which get pushed before the
2588 file scope is created.) */
2589 if (B_IN_CURRENT_SCOPE (b)
2590 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2594 if (thislevel_only && !thislevel)
2597 if (TREE_CODE (b->decl) != code)
2599 /* Definition isn't the kind we were looking for. */
2600 pending_invalid_xref = name;
2601 pending_invalid_xref_location = input_location;
2603 /* If in the same binding level as a declaration as a tag
2604 of a different type, this must not be allowed to
2605 shadow that tag, so give the error immediately.
2606 (For example, "struct foo; union foo;" is invalid.) */
2608 pending_xref_error ();
2613 /* Print an error message now
2614 for a recent invalid struct, union or enum cross reference.
2615 We don't print them immediately because they are not invalid
2616 when used in the `struct foo;' construct for shadowing. */
2619 pending_xref_error (void)
2621 if (pending_invalid_xref != 0)
2622 error ("%H%qE defined as wrong kind of tag",
2623 &pending_invalid_xref_location, pending_invalid_xref);
2624 pending_invalid_xref = 0;
2628 /* Look up NAME in the current scope and its superiors
2629 in the namespace of variables, functions and typedefs.
2630 Return a ..._DECL node of some kind representing its definition,
2631 or return 0 if it is undefined. */
2634 lookup_name (tree name)
2636 struct c_binding *b = I_SYMBOL_BINDING (name);
2637 if (b && !b->invisible)
2642 /* Similar to `lookup_name' but look only at the indicated scope. */
2645 lookup_name_in_scope (tree name, struct c_scope *scope)
2647 struct c_binding *b;
2649 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2650 if (B_IN_SCOPE (b, scope))
2655 /* Create the predefined scalar types of C,
2656 and some nodes representing standard constants (0, 1, (void *) 0).
2657 Initialize the global scope.
2658 Make definitions for built-in primitive functions. */
2661 c_init_decl_processing (void)
2663 location_t save_loc = input_location;
2665 /* Initialize reserved words for parser. */
2668 current_function_decl = 0;
2670 gcc_obstack_init (&parser_obstack);
2672 /* Make the externals scope. */
2674 external_scope = current_scope;
2676 /* Declarations from c_common_nodes_and_builtins must not be associated
2677 with this input file, lest we get differences between using and not
2678 using preprocessed headers. */
2679 #ifdef USE_MAPPED_LOCATION
2680 input_location = BUILTINS_LOCATION;
2682 input_location.file = "<built-in>";
2683 input_location.line = 0;
2686 build_common_tree_nodes (flag_signed_char, false);
2688 c_common_nodes_and_builtins ();
2690 /* In C, comparisons and TRUTH_* expressions have type int. */
2691 truthvalue_type_node = integer_type_node;
2692 truthvalue_true_node = integer_one_node;
2693 truthvalue_false_node = integer_zero_node;
2695 /* Even in C99, which has a real boolean type. */
2696 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2697 boolean_type_node));
2699 input_location = save_loc;
2701 pedantic_lvalues = true;
2703 make_fname_decl = c_make_fname_decl;
2704 start_fname_decls ();
2707 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2708 decl, NAME is the initialization string and TYPE_DEP indicates whether
2709 NAME depended on the type of the function. As we don't yet implement
2710 delayed emission of static data, we mark the decl as emitted
2711 so it is not placed in the output. Anything using it must therefore pull
2712 out the STRING_CST initializer directly. FIXME. */
2715 c_make_fname_decl (tree id, int type_dep)
2717 const char *name = fname_as_string (type_dep);
2718 tree decl, type, init;
2719 size_t length = strlen (name);
2721 type = build_array_type (char_type_node,
2722 build_index_type (size_int (length)));
2723 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2725 decl = build_decl (VAR_DECL, id, type);
2727 TREE_STATIC (decl) = 1;
2728 TREE_READONLY (decl) = 1;
2729 DECL_ARTIFICIAL (decl) = 1;
2731 init = build_string (length + 1, name);
2732 free ((char *) name);
2733 TREE_TYPE (init) = type;
2734 DECL_INITIAL (decl) = init;
2736 TREE_USED (decl) = 1;
2738 if (current_function_decl)
2740 DECL_CONTEXT (decl) = current_function_decl;
2741 bind (id, decl, current_function_scope,
2742 /*invisible=*/false, /*nested=*/false);
2745 finish_decl (decl, init, NULL_TREE);
2750 /* Return a definition for a builtin function named NAME and whose data type
2751 is TYPE. TYPE should be a function type with argument types.
2752 FUNCTION_CODE tells later passes how to compile calls to this function.
2753 See tree.h for its possible values.
2755 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2756 the name to be called if we can't opencode the function. If
2757 ATTRS is nonzero, use that for the function's attribute list. */
2760 builtin_function (const char *name, tree type, int function_code,
2761 enum built_in_class cl, const char *library_name,
2764 tree id = get_identifier (name);
2765 tree decl = build_decl (FUNCTION_DECL, id, type);
2766 TREE_PUBLIC (decl) = 1;
2767 DECL_EXTERNAL (decl) = 1;
2768 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2769 DECL_BUILT_IN_CLASS (decl) = cl;
2770 DECL_FUNCTION_CODE (decl) = function_code;
2771 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2773 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2775 /* Should never be called on a symbol with a preexisting meaning. */
2776 gcc_assert (!I_SYMBOL_BINDING (id));
2778 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2780 /* Builtins in the implementation namespace are made visible without
2781 needing to be explicitly declared. See push_file_scope. */
2782 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2784 TREE_CHAIN (decl) = visible_builtins;
2785 visible_builtins = decl;
2788 /* Possibly apply some default attributes to this built-in function. */
2790 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2792 decl_attributes (&decl, NULL_TREE, 0);
2797 /* Called when a declaration is seen that contains no names to declare.
2798 If its type is a reference to a structure, union or enum inherited
2799 from a containing scope, shadow that tag name for the current scope
2800 with a forward reference.
2801 If its type defines a new named structure or union
2802 or defines an enum, it is valid but we need not do anything here.
2803 Otherwise, it is an error. */
2806 shadow_tag (const struct c_declspecs *declspecs)
2808 shadow_tag_warned (declspecs, 0);
2811 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2814 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2816 bool found_tag = false;
2818 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2820 tree value = declspecs->type;
2821 enum tree_code code = TREE_CODE (value);
2823 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2824 /* Used to test also that TYPE_SIZE (value) != 0.
2825 That caused warning for `struct foo;' at top level in the file. */
2827 tree name = TYPE_NAME (value);
2834 if (warned != 1 && code != ENUMERAL_TYPE)
2835 /* Empty unnamed enum OK */
2837 pedwarn ("unnamed struct/union that defines no instances");
2841 else if (!declspecs->tag_defined_p
2842 && declspecs->storage_class != csc_none)
2845 pedwarn ("empty declaration with storage class specifier "
2846 "does not redeclare tag");
2848 pending_xref_error ();
2850 else if (!declspecs->tag_defined_p
2851 && (declspecs->const_p
2852 || declspecs->volatile_p
2853 || declspecs->restrict_p))
2856 pedwarn ("empty declaration with type qualifier "
2857 "does not redeclare tag");
2859 pending_xref_error ();
2863 pending_invalid_xref = 0;
2864 t = lookup_tag (code, name, 1);
2868 t = make_node (code);
2875 if (warned != 1 && !in_system_header)
2877 pedwarn ("useless type name in empty declaration");
2882 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2884 pedwarn ("useless type name in empty declaration");
2888 pending_invalid_xref = 0;
2890 if (declspecs->inline_p)
2892 error ("%<inline%> in empty declaration");
2896 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2898 error ("%<auto%> in file-scope empty declaration");
2902 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2904 error ("%<register%> in file-scope empty declaration");
2908 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2910 warning (0, "useless storage class specifier in empty declaration");
2914 if (!warned && !in_system_header && declspecs->thread_p)
2916 warning (0, "useless %<__thread%> in empty declaration");
2920 if (!warned && !in_system_header && (declspecs->const_p
2921 || declspecs->volatile_p
2922 || declspecs->restrict_p))
2924 warning (0, "useless type qualifier in empty declaration");
2931 pedwarn ("empty declaration");
2936 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2937 bits. SPECS represents declaration specifiers that the grammar
2938 only permits to contain type qualifiers and attributes. */
2941 quals_from_declspecs (const struct c_declspecs *specs)
2943 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2944 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2945 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2946 gcc_assert (!specs->type
2947 && !specs->decl_attr
2948 && specs->typespec_word == cts_none
2949 && specs->storage_class == csc_none
2950 && !specs->typedef_p
2951 && !specs->explicit_signed_p
2952 && !specs->deprecated_p
2954 && !specs->long_long_p
2957 && !specs->unsigned_p
2958 && !specs->complex_p
2960 && !specs->thread_p);
2964 /* Construct an array declarator. EXPR is the expression inside [], or
2965 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2966 to the pointer to which a parameter array is converted). STATIC_P is
2967 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2968 is true if the array is [*], a VLA of unspecified length which is
2969 nevertheless a complete type (not currently implemented by GCC),
2970 false otherwise. The field for the contained declarator is left to be
2971 filled in by set_array_declarator_inner. */
2973 struct c_declarator *
2974 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2977 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2978 struct c_declarator);
2979 declarator->kind = cdk_array;
2980 declarator->declarator = 0;
2981 declarator->u.array.dimen = expr;
2984 declarator->u.array.attrs = quals->attrs;
2985 declarator->u.array.quals = quals_from_declspecs (quals);
2989 declarator->u.array.attrs = NULL_TREE;
2990 declarator->u.array.quals = 0;
2992 declarator->u.array.static_p = static_p;
2993 declarator->u.array.vla_unspec_p = vla_unspec_p;
2994 if (pedantic && !flag_isoc99)
2996 if (static_p || quals != NULL)
2997 pedwarn ("ISO C90 does not support %<static%> or type "
2998 "qualifiers in parameter array declarators");
3000 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3003 warning (0, "GCC does not yet properly implement %<[*]%> array declarators");
3007 /* Set the contained declarator of an array declarator. DECL is the
3008 declarator, as constructed by build_array_declarator; INNER is what
3009 appears on the left of the []. ABSTRACT_P is true if it is an
3010 abstract declarator, false otherwise; this is used to reject static
3011 and type qualifiers in abstract declarators, where they are not in
3012 the C99 grammar (subject to possible change in DR#289). */
3014 struct c_declarator *
3015 set_array_declarator_inner (struct c_declarator *decl,
3016 struct c_declarator *inner, bool abstract_p)
3018 decl->declarator = inner;
3019 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3020 || decl->u.array.attrs != NULL_TREE
3021 || decl->u.array.static_p))
3022 error ("static or type qualifiers in abstract declarator");
3026 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3029 groktypename (struct c_type_name *type_name)
3032 tree attrs = type_name->specs->attrs;
3034 type_name->specs->attrs = NULL_TREE;
3036 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3039 /* Apply attributes. */
3040 decl_attributes (&type, attrs, 0);
3045 /* Decode a declarator in an ordinary declaration or data definition.
3046 This is called as soon as the type information and variable name
3047 have been parsed, before parsing the initializer if any.
3048 Here we create the ..._DECL node, fill in its type,
3049 and put it on the list of decls for the current context.
3050 The ..._DECL node is returned as the value.
3052 Exception: for arrays where the length is not specified,
3053 the type is left null, to be filled in by `finish_decl'.
3055 Function definitions do not come here; they go to start_function
3056 instead. However, external and forward declarations of functions
3057 do go through here. Structure field declarations are done by
3058 grokfield and not through here. */
3061 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3062 bool initialized, tree attributes)
3067 /* An object declared as __attribute__((deprecated)) suppresses
3068 warnings of uses of other deprecated items. */
3069 if (lookup_attribute ("deprecated", attributes))
3070 deprecated_state = DEPRECATED_SUPPRESS;
3072 decl = grokdeclarator (declarator, declspecs,
3073 NORMAL, initialized, NULL);
3077 deprecated_state = DEPRECATED_NORMAL;
3079 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3080 && MAIN_NAME_P (DECL_NAME (decl)))
3081 warning (0, "%J%qD is usually a function", decl, decl);
3084 /* Is it valid for this decl to have an initializer at all?
3085 If not, set INITIALIZED to zero, which will indirectly
3086 tell 'finish_decl' to ignore the initializer once it is parsed. */
3087 switch (TREE_CODE (decl))
3090 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3095 error ("function %qD is initialized like a variable", decl);
3100 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3101 error ("parameter %qD is initialized", decl);
3106 /* Don't allow initializations for incomplete types except for
3107 arrays which might be completed by the initialization. */
3109 /* This can happen if the array size is an undefined macro.
3110 We already gave a warning, so we don't need another one. */
3111 if (TREE_TYPE (decl) == error_mark_node)
3113 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3115 /* A complete type is ok if size is fixed. */
3117 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3118 || C_DECL_VARIABLE_SIZE (decl))
3120 error ("variable-sized object may not be initialized");
3124 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3126 error ("variable %qD has initializer but incomplete type", decl);
3129 else if (C_DECL_VARIABLE_SIZE (decl))
3131 /* Although C99 is unclear about whether incomplete arrays
3132 of VLAs themselves count as VLAs, it does not make
3133 sense to permit them to be initialized given that
3134 ordinary VLAs may not be initialized. */
3135 error ("variable-sized object may not be initialized");
3142 if (current_scope == file_scope)
3143 TREE_STATIC (decl) = 1;
3145 /* Tell 'pushdecl' this is an initialized decl
3146 even though we don't yet have the initializer expression.
3147 Also tell 'finish_decl' it may store the real initializer. */
3148 DECL_INITIAL (decl) = error_mark_node;
3151 /* If this is a function declaration, write a record describing it to the
3152 prototypes file (if requested). */
3154 if (TREE_CODE (decl) == FUNCTION_DECL)
3155 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3157 /* ANSI specifies that a tentative definition which is not merged with
3158 a non-tentative definition behaves exactly like a definition with an
3159 initializer equal to zero. (Section 3.7.2)
3161 -fno-common gives strict ANSI behavior, though this tends to break
3162 a large body of code that grew up without this rule.
3164 Thread-local variables are never common, since there's no entrenched
3165 body of code to break, and it allows more efficient variable references
3166 in the presence of dynamic linking. */
3168 if (TREE_CODE (decl) == VAR_DECL
3170 && TREE_PUBLIC (decl)
3171 && !DECL_THREAD_LOCAL (decl)
3173 DECL_COMMON (decl) = 1;
3175 /* Set attributes here so if duplicate decl, will have proper attributes. */
3176 decl_attributes (&decl, attributes, 0);
3178 if (TREE_CODE (decl) == FUNCTION_DECL
3179 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3181 struct c_declarator *ce = declarator;
3183 if (ce->kind == cdk_pointer)
3184 ce = declarator->declarator;
3185 if (ce->kind == cdk_function)
3187 tree args = ce->u.arg_info->parms;
3188 for (; args; args = TREE_CHAIN (args))
3190 tree type = TREE_TYPE (args);
3191 if (type && INTEGRAL_TYPE_P (type)
3192 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3193 DECL_ARG_TYPE (args) = integer_type_node;
3198 if (TREE_CODE (decl) == FUNCTION_DECL
3199 && DECL_DECLARED_INLINE_P (decl)
3200 && DECL_UNINLINABLE (decl)
3201 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3202 warning (0, "%Jinline function %qD given attribute noinline", decl, decl);
3204 /* Add this decl to the current scope.
3205 TEM may equal DECL or it may be a previous decl of the same name. */
3206 tem = pushdecl (decl);
3208 if (initialized && DECL_EXTERNAL (tem))
3210 DECL_EXTERNAL (tem) = 0;
3211 TREE_STATIC (tem) = 1;
3217 /* Finish processing of a declaration;
3218 install its initial value.
3219 If the length of an array type is not known before,
3220 it must be determined now, from the initial value, or it is an error. */
3223 finish_decl (tree decl, tree init, tree asmspec_tree)
3225 tree type = TREE_TYPE (decl);
3226 int was_incomplete = (DECL_SIZE (decl) == 0);
3227 const char *asmspec = 0;
3229 /* If a name was specified, get the string. */
3230 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3231 && DECL_FILE_SCOPE_P (decl))
3232 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3234 asmspec = TREE_STRING_POINTER (asmspec_tree);
3236 /* If `start_decl' didn't like having an initialization, ignore it now. */
3237 if (init != 0 && DECL_INITIAL (decl) == 0)
3240 /* Don't crash if parm is initialized. */
3241 if (TREE_CODE (decl) == PARM_DECL)
3245 store_init_value (decl, init);
3247 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3248 || TREE_CODE (decl) == FUNCTION_DECL
3249 || TREE_CODE (decl) == FIELD_DECL))
3250 objc_check_decl (decl);
3252 /* Deduce size of array from initialization, if not already known. */
3253 if (TREE_CODE (type) == ARRAY_TYPE
3254 && TYPE_DOMAIN (type) == 0
3255 && TREE_CODE (decl) != TYPE_DECL)
3258 = (TREE_STATIC (decl)
3259 /* Even if pedantic, an external linkage array
3260 may have incomplete type at first. */
3261 ? pedantic && !TREE_PUBLIC (decl)
3262 : !DECL_EXTERNAL (decl));
3264 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3267 /* Get the completed type made by complete_array_type. */
3268 type = TREE_TYPE (decl);
3273 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3278 error ("%Jarray size missing in %qD", decl, decl);
3279 /* If a `static' var's size isn't known,
3280 make it extern as well as static, so it does not get
3282 If it is not `static', then do not mark extern;
3283 finish_incomplete_decl will give it a default size
3284 and it will get allocated. */
3285 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3286 DECL_EXTERNAL (decl) = 1;
3290 error ("%Jzero or negative size array %qD", decl, decl);
3294 /* For global variables, update the copy of the type that
3295 exists in the binding. */
3296 if (TREE_PUBLIC (decl))
3298 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3299 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3300 b_ext = b_ext->shadowed;
3304 b_ext->type = composite_type (b_ext->type, type);
3315 if (DECL_INITIAL (decl))
3316 TREE_TYPE (DECL_INITIAL (decl)) = type;
3318 layout_decl (decl, 0);
3321 if (TREE_CODE (decl) == VAR_DECL)
3323 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3324 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3325 layout_decl (decl, 0);
3327 if (DECL_SIZE (decl) == 0
3328 /* Don't give an error if we already gave one earlier. */
3329 && TREE_TYPE (decl) != error_mark_node
3330 && (TREE_STATIC (decl)
3331 /* A static variable with an incomplete type
3332 is an error if it is initialized.
3333 Also if it is not file scope.
3334 Otherwise, let it through, but if it is not `extern'
3335 then it may cause an error message later. */
3336 ? (DECL_INITIAL (decl) != 0
3337 || !DECL_FILE_SCOPE_P (decl))
3338 /* An automatic variable with an incomplete type
3340 : !DECL_EXTERNAL (decl)))
3342 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3343 TREE_TYPE (decl) = error_mark_node;
3346 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3347 && DECL_SIZE (decl) != 0)
3349 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3350 constant_expression_warning (DECL_SIZE (decl));
3352 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3355 if (TREE_USED (type))
3356 TREE_USED (decl) = 1;
3359 /* If this is a function and an assembler name is specified, reset DECL_RTL
3360 so we can give it its new name. Also, update built_in_decls if it
3361 was a normal built-in. */
3362 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3364 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3365 set_builtin_user_assembler_name (decl, asmspec);
3366 set_user_assembler_name (decl, asmspec);
3369 /* If #pragma weak was used, mark the decl weak now. */
3370 maybe_apply_pragma_weak (decl);
3372 /* If this is a variable definition, determine its ELF visibility. */
3373 if (TREE_CODE (decl) == VAR_DECL
3374 && TREE_STATIC (decl)
3375 && !DECL_EXTERNAL (decl))
3376 c_determine_visibility (decl);
3378 /* Output the assembler code and/or RTL code for variables and functions,
3379 unless the type is an undefined structure or union.
3380 If not, it will get done when the type is completed. */
3382 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3384 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3385 if (c_dialect_objc ())
3386 objc_check_decl (decl);
3390 /* If this is not a static variable, issue a warning.
3391 It doesn't make any sense to give an ASMSPEC for an
3392 ordinary, non-register local variable. Historically,
3393 GCC has accepted -- but ignored -- the ASMSPEC in
3395 if (!DECL_FILE_SCOPE_P (decl)
3396 && TREE_CODE (decl) == VAR_DECL
3397 && !C_DECL_REGISTER (decl)
3398 && !TREE_STATIC (decl))
3399 warning (0, "%Jignoring asm-specifier for non-static local "
3400 "variable %qD", decl, decl);
3401 else if (C_DECL_REGISTER (decl))
3402 change_decl_assembler_name (decl, get_identifier (asmspec));
3404 set_user_assembler_name (decl, asmspec);
3407 if (DECL_FILE_SCOPE_P (decl))
3409 if (DECL_INITIAL (decl) == NULL_TREE
3410 || DECL_INITIAL (decl) == error_mark_node)
3411 /* Don't output anything
3412 when a tentative file-scope definition is seen.
3413 But at end of compilation, do output code for them. */
3414 DECL_DEFER_OUTPUT (decl) = 1;
3415 rest_of_decl_compilation (decl, true, 0);
3419 /* In conjunction with an ASMSPEC, the `register'
3420 keyword indicates that we should place the variable
3421 in a particular register. */
3422 if (asmspec && C_DECL_REGISTER (decl))
3424 DECL_HARD_REGISTER (decl) = 1;
3425 /* This cannot be done for a structure with volatile
3426 fields, on which DECL_REGISTER will have been
3428 if (!DECL_REGISTER (decl))
3429 error ("cannot put object with volatile field into register");
3432 if (TREE_CODE (decl) != FUNCTION_DECL)
3434 /* If we're building a variable sized type, and we might be
3435 reachable other than via the top of the current binding
3436 level, then create a new BIND_EXPR so that we deallocate
3437 the object at the right time. */
3438 /* Note that DECL_SIZE can be null due to errors. */
3439 if (DECL_SIZE (decl)
3440 && !TREE_CONSTANT (DECL_SIZE (decl))
3441 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3444 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3445 TREE_SIDE_EFFECTS (bind) = 1;
3447 BIND_EXPR_BODY (bind) = push_stmt_list ();
3449 add_stmt (build_stmt (DECL_EXPR, decl));
3454 if (!DECL_FILE_SCOPE_P (decl))
3456 /* Recompute the RTL of a local array now
3457 if it used to be an incomplete type. */
3459 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3461 /* If we used it already as memory, it must stay in memory. */
3462 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3463 /* If it's still incomplete now, no init will save it. */
3464 if (DECL_SIZE (decl) == 0)
3465 DECL_INITIAL (decl) = 0;
3470 /* If this was marked 'used', be sure it will be output. */
3471 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3472 mark_decl_referenced (decl);
3474 if (TREE_CODE (decl) == TYPE_DECL)
3476 if (!DECL_FILE_SCOPE_P (decl)
3477 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3478 add_stmt (build_stmt (DECL_EXPR, decl));
3480 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3483 /* At the end of a declaration, throw away any variable type sizes
3484 of types defined inside that declaration. There is no use
3485 computing them in the following function definition. */
3486 if (current_scope == file_scope)
3487 get_pending_sizes ();
3489 /* Install a cleanup (aka destructor) if one was given. */
3490 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3492 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3495 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3496 tree cleanup_decl = lookup_name (cleanup_id);
3499 /* Build "cleanup(&decl)" for the destructor. */
3500 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3501 cleanup = build_tree_list (NULL_TREE, cleanup);
3502 cleanup = build_function_call (cleanup_decl, cleanup);
3504 /* Don't warn about decl unused; the cleanup uses it. */
3505 TREE_USED (decl) = 1;
3506 TREE_USED (cleanup_decl) = 1;
3508 /* Initialize EH, if we've been told to do so. */
3509 if (flag_exceptions && !c_eh_initialized_p)
3511 c_eh_initialized_p = true;
3512 eh_personality_libfunc
3513 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3514 ? "__gcc_personality_sj0"
3515 : "__gcc_personality_v0");
3516 using_eh_for_cleanups ();
3519 push_cleanup (decl, cleanup, false);
3524 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3527 grokparm (const struct c_parm *parm)
3529 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3532 decl_attributes (&decl, parm->attrs, 0);
3537 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3538 and push that on the current scope. */
3541 push_parm_decl (const struct c_parm *parm)
3545 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3546 decl_attributes (&decl, parm->attrs, 0);
3548 decl = pushdecl (decl);
3550 finish_decl (decl, NULL_TREE, NULL_TREE);
3553 /* Mark all the parameter declarations to date as forward decls.
3554 Also diagnose use of this extension. */
3557 mark_forward_parm_decls (void)
3559 struct c_binding *b;
3561 if (pedantic && !current_scope->warned_forward_parm_decls)
3563 pedwarn ("ISO C forbids forward parameter declarations");
3564 current_scope->warned_forward_parm_decls = true;
3567 for (b = current_scope->bindings; b; b = b->prev)
3568 if (TREE_CODE (b->decl) == PARM_DECL)
3569 TREE_ASM_WRITTEN (b->decl) = 1;
3572 static GTY(()) int compound_literal_number;
3574 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3575 literal, which may be an incomplete array type completed by the
3576 initializer; INIT is a CONSTRUCTOR that initializes the compound
3580 build_compound_literal (tree type, tree init)
3582 /* We do not use start_decl here because we have a type, not a declarator;
3583 and do not use finish_decl because the decl should be stored inside
3584 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3589 if (type == error_mark_node)
3590 return error_mark_node;
3592 decl = build_decl (VAR_DECL, NULL_TREE, type);
3593 DECL_EXTERNAL (decl) = 0;
3594 TREE_PUBLIC (decl) = 0;
3595 TREE_STATIC (decl) = (current_scope == file_scope);
3596 DECL_CONTEXT (decl) = current_function_decl;
3597 TREE_USED (decl) = 1;
3598 TREE_TYPE (decl) = type;
3599 TREE_READONLY (decl) = TYPE_READONLY (type);
3600 store_init_value (decl, init);
3602 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3604 int failure = complete_array_type (&TREE_TYPE (decl),
3605 DECL_INITIAL (decl), true);
3606 gcc_assert (!failure);
3608 type = TREE_TYPE (decl);
3609 TREE_TYPE (DECL_INITIAL (decl)) = type;
3612 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3613 return error_mark_node;
3615 stmt = build_stmt (DECL_EXPR, decl);
3616 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3617 TREE_SIDE_EFFECTS (complit) = 1;
3619 layout_decl (decl, 0);
3621 if (TREE_STATIC (decl))
3623 /* This decl needs a name for the assembler output. We also need
3624 a unique suffix to be added to the name. */
3627 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3628 compound_literal_number);
3629 compound_literal_number++;
3630 DECL_NAME (decl) = get_identifier (name);
3631 DECL_DEFER_OUTPUT (decl) = 1;
3632 DECL_COMDAT (decl) = 1;
3633 DECL_ARTIFICIAL (decl) = 1;
3634 DECL_IGNORED_P (decl) = 1;
3636 rest_of_decl_compilation (decl, 1, 0);
3642 /* Determine whether TYPE is a structure with a flexible array member,
3643 or a union containing such a structure (possibly recursively). */
3646 flexible_array_type_p (tree type)
3649 switch (TREE_CODE (type))
3652 x = TYPE_FIELDS (type);
3655 while (TREE_CHAIN (x) != NULL_TREE)
3657 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3658 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3659 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3660 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3664 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3666 if (flexible_array_type_p (TREE_TYPE (x)))
3675 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3676 replacing with appropriate values if they are invalid. */
3678 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3681 unsigned int max_width;
3682 unsigned HOST_WIDE_INT w;
3683 const char *name = orig_name ? orig_name: _("<anonymous>");
3685 /* Detect and ignore out of range field width and process valid
3687 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3688 || TREE_CODE (*width) != INTEGER_CST)
3690 error ("bit-field %qs width not an integer constant", name);
3691 *width = integer_one_node;
3695 constant_expression_warning (*width);
3696 if (tree_int_cst_sgn (*width) < 0)
3698 error ("negative width in bit-field %qs", name);
3699 *width = integer_one_node;
3701 else if (integer_zerop (*width) && orig_name)
3703 error ("zero width for bit-field %qs", name);
3704 *width = integer_one_node;
3708 /* Detect invalid bit-field type. */
3709 if (TREE_CODE (*type) != INTEGER_TYPE
3710 && TREE_CODE (*type) != BOOLEAN_TYPE
3711 && TREE_CODE (*type) != ENUMERAL_TYPE)
3713 error ("bit-field %qs has invalid type", name);
3714 *type = unsigned_type_node;
3717 type_mv = TYPE_MAIN_VARIANT (*type);
3719 && type_mv != integer_type_node
3720 && type_mv != unsigned_type_node
3721 && type_mv != boolean_type_node)
3722 pedwarn ("type of bit-field %qs is a GCC extension", name);
3724 if (type_mv == boolean_type_node)
3725 max_width = CHAR_TYPE_SIZE;
3727 max_width = TYPE_PRECISION (*type);
3729 if (0 < compare_tree_int (*width, max_width))
3731 error ("width of %qs exceeds its type", name);
3733 *width = build_int_cst (NULL_TREE, w);
3736 w = tree_low_cst (*width, 1);
3738 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3740 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3742 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3743 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3744 warning (0, "%qs is narrower than values of its type", name);
3748 /* Given declspecs and a declarator,
3749 determine the name and type of the object declared
3750 and construct a ..._DECL node for it.
3751 (In one case we can return a ..._TYPE node instead.
3752 For invalid input we sometimes return 0.)
3754 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3756 DECL_CONTEXT says which syntactic context this declaration is in:
3757 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3758 FUNCDEF for a function definition. Like NORMAL but a few different
3759 error messages in each case. Return value may be zero meaning
3760 this definition is too screwy to try to parse.
3761 PARM for a parameter declaration (either within a function prototype
3762 or before a function body). Make a PARM_DECL, or return void_type_node.
3763 TYPENAME if for a typename (in a cast or sizeof).
3764 Don't make a DECL node; just return the ..._TYPE node.
3765 FIELD for a struct or union field; make a FIELD_DECL.
3766 INITIALIZED is true if the decl has an initializer.
3767 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3768 representing the width of the bit-field.
3770 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3771 It may also be so in the PARM case, for a prototype where the
3772 argument type is specified but not the name.
3774 This function is where the complicated C meanings of `static'
3775 and `extern' are interpreted. */
3778 grokdeclarator (const struct c_declarator *declarator,
3779 struct c_declspecs *declspecs,
3780 enum decl_context decl_context, bool initialized, tree *width)
3782 tree type = declspecs->type;
3783 bool threadp = declspecs->thread_p;
3784 enum c_storage_class storage_class = declspecs->storage_class;
3788 int type_quals = TYPE_UNQUALIFIED;
3789 const char *name, *orig_name;
3790 tree typedef_type = 0;
3791 int funcdef_flag = 0;
3792 bool funcdef_syntax = false;
3793 int size_varies = 0;
3794 tree decl_attr = declspecs->decl_attr;
3795 int array_ptr_quals = TYPE_UNQUALIFIED;
3796 tree array_ptr_attrs = NULL_TREE;
3797 int array_parm_static = 0;
3798 tree returned_attrs = NULL_TREE;
3799 bool bitfield = width != NULL;
3801 struct c_arg_info *arg_info = 0;
3803 if (decl_context == FUNCDEF)
3804 funcdef_flag = 1, decl_context = NORMAL;
3806 /* Look inside a declarator for the name being declared
3807 and get it as a string, for an error message. */
3809 const struct c_declarator *decl = declarator;
3818 funcdef_syntax = (decl->kind == cdk_function);
3819 decl = decl->declarator;
3823 decl = decl->declarator;
3828 name = IDENTIFIER_POINTER (decl->u.id);
3840 /* A function definition's declarator must have the form of
3841 a function declarator. */
3843 if (funcdef_flag && !funcdef_syntax)
3846 /* If this looks like a function definition, make it one,
3847 even if it occurs where parms are expected.
3848 Then store_parm_decls will reject it and not use it as a parm. */
3849 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3850 decl_context = PARM;
3852 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3853 warn_deprecated_use (declspecs->type);
3855 typedef_type = type;
3856 size_varies = C_TYPE_VARIABLE_SIZE (type);
3858 /* Diagnose defaulting to "int". */
3860 if (declspecs->default_int_p && !in_system_header)
3862 /* Issue a warning if this is an ISO C 99 program or if
3863 -Wreturn-type and this is a function, or if -Wimplicit;
3864 prefer the former warning since it is more explicit. */
3865 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3867 warn_about_return_type = 1;
3868 else if (warn_implicit_int || flag_isoc99)
3869 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3872 /* Adjust the type if a bit-field is being declared,
3873 -funsigned-bitfields applied and the type is not explicitly
3875 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3876 && TREE_CODE (type) == INTEGER_TYPE)
3877 type = c_common_unsigned_type (type);
3879 /* Figure out the type qualifiers for the declaration. There are
3880 two ways a declaration can become qualified. One is something
3881 like `const int i' where the `const' is explicit. Another is
3882 something like `typedef const int CI; CI i' where the type of the
3883 declaration contains the `const'. A third possibility is that
3884 there is a type qualifier on the element type of a typedefed
3885 array type, in which case we should extract that qualifier so
3886 that c_apply_type_quals_to_decls receives the full list of
3887 qualifiers to work with (C90 is not entirely clear about whether
3888 duplicate qualifiers should be diagnosed in this case, but it
3889 seems most appropriate to do so). */
3890 element_type = strip_array_types (type);
3891 constp = declspecs->const_p + TYPE_READONLY (element_type);
3892 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3893 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3894 if (pedantic && !flag_isoc99)
3897 pedwarn ("duplicate %<const%>");
3899 pedwarn ("duplicate %<restrict%>");
3901 pedwarn ("duplicate %<volatile%>");
3903 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3904 type = TYPE_MAIN_VARIANT (type);
3905 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3906 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3907 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3909 /* Warn about storage classes that are invalid for certain
3910 kinds of declarations (parameters, typenames, etc.). */
3914 || storage_class == csc_auto
3915 || storage_class == csc_register
3916 || storage_class == csc_typedef))
3918 if (storage_class == csc_auto
3919 && (pedantic || current_scope == file_scope))
3920 pedwarn ("function definition declared %<auto%>");
3921 if (storage_class == csc_register)
3922 error ("function definition declared %<register%>");
3923 if (storage_class == csc_typedef)
3924 error ("function definition declared %<typedef%>");
3926 error ("function definition declared %<__thread%>");
3928 if (storage_class == csc_auto
3929 || storage_class == csc_register
3930 || storage_class == csc_typedef)
3931 storage_class = csc_none;
3933 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3935 if (decl_context == PARM && storage_class == csc_register)
3939 switch (decl_context)
3942 error ("storage class specified for structure field %qs",
3946 error ("storage class specified for parameter %qs", name);
3949 error ("storage class specified for typename");
3952 storage_class = csc_none;
3956 else if (storage_class == csc_extern
3960 /* 'extern' with initialization is invalid if not at file scope. */
3961 if (current_scope == file_scope)
3962 warning (0, "%qs initialized and declared %<extern%>", name);
3964 error ("%qs has both %<extern%> and initializer", name);
3966 else if (current_scope == file_scope)
3968 if (storage_class == csc_auto)
3969 error ("file-scope declaration of %qs specifies %<auto%>", name);
3970 if (pedantic && storage_class == csc_register)
3971 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
3975 if (storage_class == csc_extern && funcdef_flag)
3976 error ("nested function %qs declared %<extern%>", name);
3977 else if (threadp && storage_class == csc_none)
3979 error ("function-scope %qs implicitly auto and declared "
3986 /* Now figure out the structure of the declarator proper.
3987 Descend through it, creating more complex types, until we reach
3988 the declared identifier (or NULL_TREE, in an absolute declarator).
3989 At each stage we maintain an unqualified version of the type
3990 together with any qualifiers that should be applied to it with
3991 c_build_qualified_type; this way, array types including
3992 multidimensional array types are first built up in unqualified
3993 form and then the qualified form is created with
3994 TYPE_MAIN_VARIANT pointing to the unqualified form. */
3996 while (declarator && declarator->kind != cdk_id)
3998 if (type == error_mark_node)
4000 declarator = declarator->declarator;
4004 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4005 a cdk_pointer (for *...),
4006 a cdk_function (for ...(...)),
4007 a cdk_attrs (for nested attributes),
4008 or a cdk_id (for the name being declared
4009 or the place in an absolute declarator
4010 where the name was omitted).
4011 For the last case, we have just exited the loop.
4013 At this point, TYPE is the type of elements of an array,
4014 or for a function to return, or for a pointer to point to.
4015 After this sequence of ifs, TYPE is the type of the
4016 array or function or pointer, and DECLARATOR has had its
4017 outermost layer removed. */
4019 if (array_ptr_quals != TYPE_UNQUALIFIED
4020 || array_ptr_attrs != NULL_TREE
4021 || array_parm_static)
4023 /* Only the innermost declarator (making a parameter be of
4024 array type which is converted to pointer type)
4025 may have static or type qualifiers. */
4026 error ("static or type qualifiers in non-parameter array declarator");
4027 array_ptr_quals = TYPE_UNQUALIFIED;
4028 array_ptr_attrs = NULL_TREE;
4029 array_parm_static = 0;
4032 switch (declarator->kind)
4036 /* A declarator with embedded attributes. */
4037 tree attrs = declarator->u.attrs;
4038 const struct c_declarator *inner_decl;
4040 declarator = declarator->declarator;
4041 inner_decl = declarator;
4042 while (inner_decl->kind == cdk_attrs)
4043 inner_decl = inner_decl->declarator;
4044 if (inner_decl->kind == cdk_id)
4045 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4046 else if (inner_decl->kind == cdk_function)
4047 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4048 else if (inner_decl->kind == cdk_array)
4049 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4050 returned_attrs = decl_attributes (&type,
4051 chainon (returned_attrs, attrs),
4057 tree itype = NULL_TREE;
4058 tree size = declarator->u.array.dimen;
4059 /* The index is a signed object `sizetype' bits wide. */
4060 tree index_type = c_common_signed_type (sizetype);
4062 array_ptr_quals = declarator->u.array.quals;
4063 array_ptr_attrs = declarator->u.array.attrs;
4064 array_parm_static = declarator->u.array.static_p;
4066 declarator = declarator->declarator;
4068 /* Check for some types that there cannot be arrays of. */
4070 if (VOID_TYPE_P (type))
4072 error ("declaration of %qs as array of voids", name);
4073 type = error_mark_node;
4076 if (TREE_CODE (type) == FUNCTION_TYPE)
4078 error ("declaration of %qs as array of functions", name);
4079 type = error_mark_node;
4082 if (pedantic && !in_system_header && flexible_array_type_p (type))
4083 pedwarn ("invalid use of structure with flexible array member");
4085 if (size == error_mark_node)
4086 type = error_mark_node;
4088 if (type == error_mark_node)
4091 /* If size was specified, set ITYPE to a range-type for
4092 that size. Otherwise, ITYPE remains null. finish_decl
4093 may figure it out from an initial value. */
4097 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4099 STRIP_TYPE_NOPS (size);
4101 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4103 error ("size of array %qs has non-integer type", name);
4104 size = integer_one_node;
4107 if (pedantic && integer_zerop (size))
4108 pedwarn ("ISO C forbids zero-size array %qs", name);
4110 if (TREE_CODE (size) == INTEGER_CST)
4112 constant_expression_warning (size);
4113 if (tree_int_cst_sgn (size) < 0)
4115 error ("size of array %qs is negative", name);
4116 size = integer_one_node;
4121 /* Make sure the array size remains visibly
4122 nonconstant even if it is (eg) a const variable
4123 with known value. */
4126 if (!flag_isoc99 && pedantic)
4128 if (TREE_CONSTANT (size))
4129 pedwarn ("ISO C90 forbids array %qs whose size "
4130 "can%'t be evaluated",
4133 pedwarn ("ISO C90 forbids variable-size array %qs",
4138 if (integer_zerop (size))
4140 /* A zero-length array cannot be represented with
4141 an unsigned index type, which is what we'll
4142 get with build_index_type. Create an
4143 open-ended range instead. */
4144 itype = build_range_type (sizetype, size, NULL_TREE);
4148 /* Arrange for the SAVE_EXPR on the inside of the
4149 MINUS_EXPR, which allows the -1 to get folded
4150 with the +1 that happens when building TYPE_SIZE. */
4152 size = variable_size (size);
4154 /* Compute the maximum valid index, that is, size
4155 - 1. Do the calculation in index_type, so that
4156 if it is a variable the computations will be
4157 done in the proper mode. */
4158 itype = fold (build2 (MINUS_EXPR, index_type,
4159 convert (index_type, size),
4160 convert (index_type,
4163 /* If that overflowed, the array is too big. ???
4164 While a size of INT_MAX+1 technically shouldn't
4165 cause an overflow (because we subtract 1), the
4166 overflow is recorded during the conversion to
4167 index_type, before the subtraction. Handling
4168 this case seems like an unnecessary
4170 if (TREE_OVERFLOW (itype))
4172 error ("size of array %qs is too large", name);
4173 type = error_mark_node;
4177 itype = build_index_type (itype);
4180 else if (decl_context == FIELD)
4182 if (pedantic && !flag_isoc99 && !in_system_header)
4183 pedwarn ("ISO C90 does not support flexible array members");
4185 /* ISO C99 Flexible array members are effectively
4186 identical to GCC's zero-length array extension. */
4187 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4190 /* Complain about arrays of incomplete types. */
4191 if (!COMPLETE_TYPE_P (type))
4193 error ("array type has incomplete element type");
4194 type = error_mark_node;
4197 type = build_array_type (type, itype);
4200 C_TYPE_VARIABLE_SIZE (type) = 1;
4202 /* The GCC extension for zero-length arrays differs from
4203 ISO flexible array members in that sizeof yields
4205 if (size && integer_zerop (size))
4207 TYPE_SIZE (type) = bitsize_zero_node;
4208 TYPE_SIZE_UNIT (type) = size_zero_node;
4211 if (decl_context != PARM
4212 && (array_ptr_quals != TYPE_UNQUALIFIED
4213 || array_ptr_attrs != NULL_TREE
4214 || array_parm_static))
4216 error ("static or type qualifiers in non-parameter array declarator");
4217 array_ptr_quals = TYPE_UNQUALIFIED;
4218 array_ptr_attrs = NULL_TREE;
4219 array_parm_static = 0;
4225 /* Say it's a definition only for the declarator closest
4226 to the identifier, apart possibly from some
4228 bool really_funcdef = false;
4232 const struct c_declarator *t = declarator->declarator;
4233 while (t->kind == cdk_attrs)
4235 really_funcdef = (t->kind == cdk_id);
4238 /* Declaring a function type. Make sure we have a valid
4239 type for the function to return. */
4240 if (type == error_mark_node)
4245 /* Warn about some types functions can't return. */
4246 if (TREE_CODE (type) == FUNCTION_TYPE)
4248 error ("%qs declared as function returning a function", name);
4249 type = integer_type_node;
4251 if (TREE_CODE (type) == ARRAY_TYPE)
4253 error ("%qs declared as function returning an array", name);
4254 type = integer_type_node;
4257 /* Construct the function type and go to the next
4258 inner layer of declarator. */
4259 arg_info = declarator->u.arg_info;
4260 arg_types = grokparms (arg_info, really_funcdef);
4262 /* Type qualifiers before the return type of the function
4263 qualify the return type, not the function type. */
4266 /* Type qualifiers on a function return type are
4267 normally permitted by the standard but have no
4268 effect, so give a warning at -Wreturn-type.
4269 Qualifiers on a void return type are banned on
4270 function definitions in ISO C; GCC used to used
4271 them for noreturn functions. */
4272 if (VOID_TYPE_P (type) && really_funcdef)
4273 pedwarn ("function definition has qualified void return type");
4274 else if (warn_return_type)
4275 warning (0, "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", fndecl);
6084 /* Match each formal parameter name with its declaration. Save each
6085 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6086 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6088 if (TREE_VALUE (parm) == 0)
6090 error ("%Jparameter name missing from parameter list", fndecl);
6091 TREE_PURPOSE (parm) = 0;
6095 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6096 if (b && B_IN_CURRENT_SCOPE (b))
6099 /* If we got something other than a PARM_DECL it is an error. */
6100 if (TREE_CODE (decl) != PARM_DECL)
6101 error ("%J%qD declared as a non-parameter", decl, decl);
6102 /* If the declaration is already marked, we have a duplicate
6103 name. Complain and ignore the duplicate. */
6104 else if (DECL_WEAK (decl))
6106 error ("%Jmultiple parameters named %qD", decl, decl);
6107 TREE_PURPOSE (parm) = 0;
6110 /* If the declaration says "void", complain and turn it into
6112 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6114 error ("%Jparameter %qD declared with void type", decl, decl);
6115 TREE_TYPE (decl) = integer_type_node;
6116 DECL_ARG_TYPE (decl) = integer_type_node;
6117 layout_decl (decl, 0);
6120 /* If no declaration found, default to int. */
6123 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6124 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6125 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6129 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6130 else if (extra_warnings)
6131 warning (0, "%Jtype of %qD defaults to %<int%>", decl, decl);
6134 TREE_PURPOSE (parm) = decl;
6135 DECL_WEAK (decl) = 1;
6138 /* Now examine the parms chain for incomplete declarations
6139 and declarations with no corresponding names. */
6141 for (b = current_scope->bindings; b; b = b->prev)
6144 if (TREE_CODE (parm) != PARM_DECL)
6147 if (TREE_TYPE (parm) != error_mark_node
6148 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6150 error ("%Jparameter %qD has incomplete type", parm, parm);
6151 TREE_TYPE (parm) = error_mark_node;
6154 if (!DECL_WEAK (parm))
6156 error ("%Jdeclaration for parameter %qD but no such parameter",
6159 /* Pretend the parameter was not missing.
6160 This gets us to a standard state and minimizes
6161 further error messages. */
6162 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6166 /* Chain the declarations together in the order of the list of
6167 names. Store that chain in the function decl, replacing the
6168 list of names. Update the current scope to match. */
6169 DECL_ARGUMENTS (fndecl) = 0;
6171 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6172 if (TREE_PURPOSE (parm))
6174 if (parm && TREE_PURPOSE (parm))
6176 last = TREE_PURPOSE (parm);
6177 DECL_ARGUMENTS (fndecl) = last;
6178 DECL_WEAK (last) = 0;
6180 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6181 if (TREE_PURPOSE (parm))
6183 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6184 last = TREE_PURPOSE (parm);
6185 DECL_WEAK (last) = 0;
6187 TREE_CHAIN (last) = 0;
6190 /* If there was a previous prototype,
6191 set the DECL_ARG_TYPE of each argument according to
6192 the type previously specified, and report any mismatches. */
6194 if (current_function_prototype_arg_types)
6197 for (parm = DECL_ARGUMENTS (fndecl),
6198 type = current_function_prototype_arg_types;
6199 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6200 != void_type_node));
6201 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6203 if (parm == 0 || type == 0
6204 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6206 if (current_function_prototype_built_in)
6207 warning (0, "number of arguments doesn%'t match "
6208 "built-in prototype");
6211 error ("number of arguments doesn%'t match prototype");
6212 error ("%Hprototype declaration",
6213 ¤t_function_prototype_locus);
6217 /* Type for passing arg must be consistent with that
6218 declared for the arg. ISO C says we take the unqualified
6219 type for parameters declared with qualified type. */
6220 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6221 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6223 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6224 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6226 /* Adjust argument to match prototype. E.g. a previous
6227 `int foo(float);' prototype causes
6228 `int foo(x) float x; {...}' to be treated like
6229 `int foo(float x) {...}'. This is particularly
6230 useful for argument types like uid_t. */
6231 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6233 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6234 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6235 && TYPE_PRECISION (TREE_TYPE (parm))
6236 < TYPE_PRECISION (integer_type_node))
6237 DECL_ARG_TYPE (parm) = integer_type_node;
6241 /* ??? Is it possible to get here with a
6242 built-in prototype or will it always have
6243 been diagnosed as conflicting with an
6244 old-style definition and discarded? */
6245 if (current_function_prototype_built_in)
6246 warning (0, "promoted argument %qD "
6247 "doesn%'t match built-in prototype", parm);
6250 pedwarn ("promoted argument %qD "
6251 "doesn%'t match prototype", parm);
6252 pedwarn ("%Hprototype declaration",
6253 ¤t_function_prototype_locus);
6259 if (current_function_prototype_built_in)
6260 warning (0, "argument %qD doesn%'t match "
6261 "built-in prototype", parm);
6264 error ("argument %qD doesn%'t match prototype", parm);
6265 error ("%Hprototype declaration",
6266 ¤t_function_prototype_locus);
6271 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6274 /* Otherwise, create a prototype that would match. */
6278 tree actual = 0, last = 0, type;
6280 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6282 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6284 TREE_CHAIN (last) = type;
6289 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6291 TREE_CHAIN (last) = type;
6295 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6296 of the type of this function, but we need to avoid having this
6297 affect the types of other similarly-typed functions, so we must
6298 first force the generation of an identical (but separate) type
6299 node for the relevant function type. The new node we create
6300 will be a variant of the main variant of the original function
6303 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6305 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6309 /* Store parameter declarations passed in ARG_INFO into the current
6310 function declaration. */
6313 store_parm_decls_from (struct c_arg_info *arg_info)
6315 current_function_arg_info = arg_info;
6316 store_parm_decls ();
6319 /* Store the parameter declarations into the current function declaration.
6320 This is called after parsing the parameter declarations, before
6321 digesting the body of the function.
6323 For an old-style definition, construct a prototype out of the old-style
6324 parameter declarations and inject it into the function's type. */
6327 store_parm_decls (void)
6329 tree fndecl = current_function_decl;
6332 /* The argument information block for FNDECL. */
6333 struct c_arg_info *arg_info = current_function_arg_info;
6334 current_function_arg_info = 0;
6336 /* True if this definition is written with a prototype. Note:
6337 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6338 list in a function definition as equivalent to (void) -- an
6339 empty argument list specifies the function has no parameters,
6340 but only (void) sets up a prototype for future calls. */
6341 proto = arg_info->types != 0;
6344 store_parm_decls_newstyle (fndecl, arg_info);
6346 store_parm_decls_oldstyle (fndecl, arg_info);
6348 /* The next call to push_scope will be a function body. */
6350 next_is_function_body = true;
6352 /* Write a record describing this function definition to the prototypes
6353 file (if requested). */
6355 gen_aux_info_record (fndecl, 1, 0, proto);
6357 /* Initialize the RTL code for the function. */
6358 allocate_struct_function (fndecl);
6360 /* Begin the statement tree for this function. */
6361 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6363 /* ??? Insert the contents of the pending sizes list into the function
6364 to be evaluated. The only reason left to have this is
6365 void foo(int n, int array[n++])
6366 because we throw away the array type in favor of a pointer type, and
6367 thus won't naturally see the SAVE_EXPR containing the increment. All
6368 other pending sizes would be handled by gimplify_parameters. */
6371 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6372 add_stmt (TREE_VALUE (t));
6375 /* Even though we're inside a function body, we still don't want to
6376 call expand_expr to calculate the size of a variable-sized array.
6377 We haven't necessarily assigned RTL to all variables yet, so it's
6378 not safe to try to expand expressions involving them. */
6379 cfun->x_dont_save_pending_sizes_p = 1;
6382 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6386 c_warn_unused_result_recursively (tree fndecl)
6388 struct cgraph_node *cgn;
6390 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6391 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6393 /* Finalize all nested functions now. */
6394 cgn = cgraph_node (fndecl);
6395 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6396 c_warn_unused_result_recursively (cgn->decl);
6399 /* Finish up a function declaration and compile that function
6400 all the way to assembler language output. The free the storage
6401 for the function definition.
6403 This is called after parsing the body of the function definition. */
6406 finish_function (void)
6408 tree fndecl = current_function_decl;
6410 label_context_stack_se = label_context_stack_se->next;
6411 label_context_stack_vm = label_context_stack_vm->next;
6413 if (TREE_CODE (fndecl) == FUNCTION_DECL
6414 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6416 tree args = DECL_ARGUMENTS (fndecl);
6417 for (; args; args = TREE_CHAIN (args))
6419 tree type = TREE_TYPE (args);
6420 if (INTEGRAL_TYPE_P (type)
6421 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6422 DECL_ARG_TYPE (args) = integer_type_node;
6426 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6427 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6429 /* Must mark the RESULT_DECL as being in this function. */
6431 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6432 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6434 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6436 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6437 != integer_type_node)
6439 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6440 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6442 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6448 tree stmt = c_finish_return (integer_zero_node);
6449 #ifdef USE_MAPPED_LOCATION
6450 /* Hack. We don't want the middle-end to warn that this return
6451 is unreachable, so we mark its location as special. Using
6452 UNKNOWN_LOCATION has the problem that it gets clobbered in
6453 annotate_one_with_locus. A cleaner solution might be to
6454 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6456 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6458 /* Hack. We don't want the middle-end to warn that this
6459 return is unreachable, so put the statement on the
6461 annotate_with_file_line (stmt, input_filename, 0);
6467 /* Tie off the statement tree for this function. */
6468 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6470 finish_fname_decls ();
6472 /* Complain if there's just no return statement. */
6473 if (warn_return_type
6474 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6475 && !current_function_returns_value && !current_function_returns_null
6476 /* Don't complain if we are no-return. */
6477 && !current_function_returns_abnormally
6478 /* Don't warn for main(). */
6479 && !MAIN_NAME_P (DECL_NAME (fndecl))
6480 /* Or if they didn't actually specify a return type. */
6481 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6482 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6483 inline function, as we might never be compiled separately. */
6484 && DECL_INLINE (fndecl))
6486 warning (0, "no return statement in function returning non-void");
6487 TREE_NO_WARNING (fndecl) = 1;
6490 /* With just -Wextra, complain only if function returns both with
6491 and without a value. */
6493 && current_function_returns_value
6494 && current_function_returns_null)
6495 warning (0, "this function may return with or without a value");
6497 /* Store the end of the function, so that we get good line number
6498 info for the epilogue. */
6499 cfun->function_end_locus = input_location;
6501 /* If we don't have ctors/dtors sections, and this is a static
6502 constructor or destructor, it must be recorded now. */
6503 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6504 && !targetm.have_ctors_dtors)
6505 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6506 if (DECL_STATIC_DESTRUCTOR (fndecl)
6507 && !targetm.have_ctors_dtors)
6508 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6510 /* Finalize the ELF visibility for the function. */
6511 c_determine_visibility (fndecl);
6513 /* Genericize before inlining. Delay genericizing nested functions
6514 until their parent function is genericized. Since finalizing
6515 requires GENERIC, delay that as well. */
6517 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6518 && !undef_nested_function)
6520 if (!decl_function_context (fndecl))
6522 c_genericize (fndecl);
6523 c_warn_unused_result_recursively (fndecl);
6525 /* ??? Objc emits functions after finalizing the compilation unit.
6526 This should be cleaned up later and this conditional removed. */
6527 if (cgraph_global_info_ready)
6529 c_expand_body (fndecl);
6533 cgraph_finalize_function (fndecl, false);
6537 /* Register this function with cgraph just far enough to get it
6538 added to our parent's nested function list. Handy, since the
6539 C front end doesn't have such a list. */
6540 (void) cgraph_node (fndecl);
6544 if (!decl_function_context (fndecl))
6545 undef_nested_function = false;
6547 /* We're leaving the context of this function, so zap cfun.
6548 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6549 tree_rest_of_compilation. */
6551 current_function_decl = NULL;
6554 /* Generate the RTL for the body of FNDECL. */
6557 c_expand_body (tree fndecl)
6560 if (!DECL_INITIAL (fndecl)
6561 || DECL_INITIAL (fndecl) == error_mark_node)
6564 tree_rest_of_compilation (fndecl);
6566 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6567 && targetm.have_ctors_dtors)
6568 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6569 DEFAULT_INIT_PRIORITY);
6570 if (DECL_STATIC_DESTRUCTOR (fndecl)
6571 && targetm.have_ctors_dtors)
6572 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6573 DEFAULT_INIT_PRIORITY);
6576 /* Check the declarations given in a for-loop for satisfying the C99
6579 check_for_loop_decls (void)
6581 struct c_binding *b;
6585 /* If we get here, declarations have been used in a for loop without
6586 the C99 for loop scope. This doesn't make much sense, so don't
6588 error ("%<for%> loop initial declaration used outside C99 mode");
6591 /* C99 subclause 6.8.5 paragraph 3:
6593 [#3] The declaration part of a for statement shall only
6594 declare identifiers for objects having storage class auto or
6597 It isn't clear whether, in this sentence, "identifiers" binds to
6598 "shall only declare" or to "objects" - that is, whether all identifiers
6599 declared must be identifiers for objects, or whether the restriction
6600 only applies to those that are. (A question on this in comp.std.c
6601 in November 2000 received no answer.) We implement the strictest
6602 interpretation, to avoid creating an extension which later causes
6605 for (b = current_scope->bindings; b; b = b->prev)
6608 tree decl = b->decl;
6613 switch (TREE_CODE (decl))
6616 if (TREE_STATIC (decl))
6617 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6618 "initial declaration", decl, decl);
6619 else if (DECL_EXTERNAL (decl))
6620 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6621 "initial declaration", decl, decl);
6625 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6629 error ("%<union %E%> declared in %<for%> loop initial declaration",
6633 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6637 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6638 "initial declaration", decl, decl);
6643 /* Save and reinitialize the variables
6644 used during compilation of a C function. */
6647 c_push_function_context (struct function *f)
6649 struct language_function *p;
6650 p = GGC_NEW (struct language_function);
6653 p->base.x_stmt_tree = c_stmt_tree;
6654 p->x_break_label = c_break_label;
6655 p->x_cont_label = c_cont_label;
6656 p->x_switch_stack = c_switch_stack;
6657 p->arg_info = current_function_arg_info;
6658 p->returns_value = current_function_returns_value;
6659 p->returns_null = current_function_returns_null;
6660 p->returns_abnormally = current_function_returns_abnormally;
6661 p->warn_about_return_type = warn_about_return_type;
6662 p->extern_inline = current_extern_inline;
6665 /* Restore the variables used during compilation of a C function. */
6668 c_pop_function_context (struct function *f)
6670 struct language_function *p = f->language;
6672 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6673 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6675 /* Stop pointing to the local nodes about to be freed. */
6676 /* But DECL_INITIAL must remain nonzero so we know this
6677 was an actual function definition. */
6678 DECL_INITIAL (current_function_decl) = error_mark_node;
6679 DECL_ARGUMENTS (current_function_decl) = 0;
6682 c_stmt_tree = p->base.x_stmt_tree;
6683 c_break_label = p->x_break_label;
6684 c_cont_label = p->x_cont_label;
6685 c_switch_stack = p->x_switch_stack;
6686 current_function_arg_info = p->arg_info;
6687 current_function_returns_value = p->returns_value;
6688 current_function_returns_null = p->returns_null;
6689 current_function_returns_abnormally = p->returns_abnormally;
6690 warn_about_return_type = p->warn_about_return_type;
6691 current_extern_inline = p->extern_inline;
6696 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6699 c_dup_lang_specific_decl (tree decl)
6701 struct lang_decl *ld;
6703 if (!DECL_LANG_SPECIFIC (decl))
6706 ld = GGC_NEW (struct lang_decl);
6707 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6708 DECL_LANG_SPECIFIC (decl) = ld;
6711 /* The functions below are required for functionality of doing
6712 function at once processing in the C front end. Currently these
6713 functions are not called from anywhere in the C front end, but as
6714 these changes continue, that will change. */
6716 /* Returns nonzero if the current statement is a full expression,
6717 i.e. temporaries created during that statement should be destroyed
6718 at the end of the statement. */
6721 stmts_are_full_exprs_p (void)
6726 /* Returns the stmt_tree (if any) to which statements are currently
6727 being added. If there is no active statement-tree, NULL is
6731 current_stmt_tree (void)
6733 return &c_stmt_tree;
6736 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6740 anon_aggr_type_p (tree ARG_UNUSED (node))
6745 /* Return the global value of T as a symbol. */
6748 identifier_global_value (tree t)
6750 struct c_binding *b;
6752 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6753 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6759 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6760 otherwise the name is found in ridpointers from RID_INDEX. */
6763 record_builtin_type (enum rid rid_index, const char *name, tree type)
6767 id = ridpointers[(int) rid_index];
6769 id = get_identifier (name);
6770 decl = build_decl (TYPE_DECL, id, type);
6772 if (debug_hooks->type_decl)
6773 debug_hooks->type_decl (decl, false);
6776 /* Build the void_list_node (void_type_node having been created). */
6778 build_void_list_node (void)
6780 tree t = build_tree_list (NULL_TREE, void_type_node);
6784 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6787 build_c_parm (struct c_declspecs *specs, tree attrs,
6788 struct c_declarator *declarator)
6790 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6793 ret->declarator = declarator;
6797 /* Return a declarator with nested attributes. TARGET is the inner
6798 declarator to which these attributes apply. ATTRS are the
6801 struct c_declarator *
6802 build_attrs_declarator (tree attrs, struct c_declarator *target)
6804 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6805 ret->kind = cdk_attrs;
6806 ret->declarator = target;
6807 ret->u.attrs = attrs;
6811 /* Return a declarator for a function with arguments specified by ARGS
6812 and return type specified by TARGET. */
6814 struct c_declarator *
6815 build_function_declarator (struct c_arg_info *args,
6816 struct c_declarator *target)
6818 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6819 ret->kind = cdk_function;
6820 ret->declarator = target;
6821 ret->u.arg_info = args;
6825 /* Return a declarator for the identifier IDENT (which may be
6826 NULL_TREE for an abstract declarator). */
6828 struct c_declarator *
6829 build_id_declarator (tree ident)
6831 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6833 ret->declarator = 0;
6835 /* Default value - may get reset to a more precise location. */
6836 ret->id_loc = input_location;
6840 /* Return something to represent absolute declarators containing a *.
6841 TARGET is the absolute declarator that the * contains.
6842 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6843 to apply to the pointer type. */
6845 struct c_declarator *
6846 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6847 struct c_declarator *target)
6851 struct c_declarator *itarget = target;
6852 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6853 if (type_quals_attrs)
6855 attrs = type_quals_attrs->attrs;
6856 quals = quals_from_declspecs (type_quals_attrs);
6857 if (attrs != NULL_TREE)
6858 itarget = build_attrs_declarator (attrs, target);
6860 ret->kind = cdk_pointer;
6861 ret->declarator = itarget;
6862 ret->u.pointer_quals = quals;
6866 /* Return a pointer to a structure for an empty list of declaration
6869 struct c_declspecs *
6870 build_null_declspecs (void)
6872 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6876 ret->typespec_word = cts_none;
6877 ret->storage_class = csc_none;
6878 ret->declspecs_seen_p = false;
6879 ret->type_seen_p = false;
6880 ret->non_sc_seen_p = false;
6881 ret->typedef_p = false;
6882 ret->tag_defined_p = false;
6883 ret->explicit_signed_p = false;
6884 ret->deprecated_p = false;
6885 ret->default_int_p = false;
6886 ret->long_p = false;
6887 ret->long_long_p = false;
6888 ret->short_p = false;
6889 ret->signed_p = false;
6890 ret->unsigned_p = false;
6891 ret->complex_p = false;
6892 ret->inline_p = false;
6893 ret->thread_p = false;
6894 ret->const_p = false;
6895 ret->volatile_p = false;
6896 ret->restrict_p = false;
6900 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6903 struct c_declspecs *
6904 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6908 specs->non_sc_seen_p = true;
6909 specs->declspecs_seen_p = true;
6910 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6911 && C_IS_RESERVED_WORD (qual));
6912 i = C_RID_CODE (qual);
6916 dupe = specs->const_p;
6917 specs->const_p = true;
6920 dupe = specs->volatile_p;
6921 specs->volatile_p = true;
6924 dupe = specs->restrict_p;
6925 specs->restrict_p = true;
6930 if (dupe && pedantic && !flag_isoc99)
6931 pedwarn ("duplicate %qE", qual);
6935 /* Add the type specifier TYPE to the declaration specifiers SPECS,
6938 struct c_declspecs *
6939 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
6941 tree type = spec.spec;
6942 specs->non_sc_seen_p = true;
6943 specs->declspecs_seen_p = true;
6944 specs->type_seen_p = true;
6945 if (TREE_DEPRECATED (type))
6946 specs->deprecated_p = true;
6948 /* Handle type specifier keywords. */
6949 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
6951 enum rid i = C_RID_CODE (type);
6954 error ("two or more data types in declaration specifiers");
6957 if ((int) i <= (int) RID_LAST_MODIFIER)
6959 /* "long", "short", "signed", "unsigned" or "_Complex". */
6964 if (specs->long_long_p)
6966 error ("%<long long long%> is too long for GCC");
6971 if (specs->typespec_word == cts_double)
6973 error ("both %<long long%> and %<double%> in "
6974 "declaration specifiers");
6977 if (pedantic && !flag_isoc99 && !in_system_header
6979 pedwarn ("ISO C90 does not support %<long long%>");
6980 specs->long_long_p = 1;
6984 error ("both %<long%> and %<short%> in "
6985 "declaration specifiers");
6986 else if (specs->typespec_word == cts_void)
6987 error ("both %<long%> and %<void%> in "
6988 "declaration specifiers");
6989 else if (specs->typespec_word == cts_bool)
6990 error ("both %<long%> and %<_Bool%> in "
6991 "declaration specifiers");
6992 else if (specs->typespec_word == cts_char)
6993 error ("both %<long%> and %<char%> in "
6994 "declaration specifiers");
6995 else if (specs->typespec_word == cts_float)
6996 error ("both %<long%> and %<float%> in "
6997 "declaration specifiers");
6999 specs->long_p = true;
7002 dupe = specs->short_p;
7004 error ("both %<long%> and %<short%> in "
7005 "declaration specifiers");
7006 else if (specs->typespec_word == cts_void)
7007 error ("both %<short%> and %<void%> in "
7008 "declaration specifiers");
7009 else if (specs->typespec_word == cts_bool)
7010 error ("both %<short%> and %<_Bool%> in "
7011 "declaration specifiers");
7012 else if (specs->typespec_word == cts_char)
7013 error ("both %<short%> and %<char%> in "
7014 "declaration specifiers");
7015 else if (specs->typespec_word == cts_float)
7016 error ("both %<short%> and %<float%> in "
7017 "declaration specifiers");
7018 else if (specs->typespec_word == cts_double)
7019 error ("both %<short%> and %<double%> in "
7020 "declaration specifiers");
7022 specs->short_p = true;
7025 dupe = specs->signed_p;
7026 if (specs->unsigned_p)
7027 error ("both %<signed%> and %<unsigned%> in "
7028 "declaration specifiers");
7029 else if (specs->typespec_word == cts_void)
7030 error ("both %<signed%> and %<void%> in "
7031 "declaration specifiers");
7032 else if (specs->typespec_word == cts_bool)
7033 error ("both %<signed%> and %<_Bool%> in "
7034 "declaration specifiers");
7035 else if (specs->typespec_word == cts_float)
7036 error ("both %<signed%> and %<float%> in "
7037 "declaration specifiers");
7038 else if (specs->typespec_word == cts_double)
7039 error ("both %<signed%> and %<double%> in "
7040 "declaration specifiers");
7042 specs->signed_p = true;
7045 dupe = specs->unsigned_p;
7046 if (specs->signed_p)
7047 error ("both %<signed%> and %<unsigned%> in "
7048 "declaration specifiers");
7049 else if (specs->typespec_word == cts_void)
7050 error ("both %<unsigned%> and %<void%> in "
7051 "declaration specifiers");
7052 else if (specs->typespec_word == cts_bool)
7053 error ("both %<unsigned%> and %<_Bool%> in "
7054 "declaration specifiers");
7055 else if (specs->typespec_word == cts_float)
7056 error ("both %<unsigned%> and %<float%> in "
7057 "declaration specifiers");
7058 else if (specs->typespec_word == cts_double)
7059 error ("both %<unsigned%> and %<double%> in "
7060 "declaration specifiers");
7062 specs->unsigned_p = true;
7065 dupe = specs->complex_p;
7066 if (pedantic && !flag_isoc99 && !in_system_header)
7067 pedwarn ("ISO C90 does not support complex types");
7068 if (specs->typespec_word == cts_void)
7069 error ("both %<complex%> and %<void%> in "
7070 "declaration specifiers");
7071 else if (specs->typespec_word == cts_bool)
7072 error ("both %<complex%> and %<_Bool%> in "
7073 "declaration specifiers");
7075 specs->complex_p = true;
7082 error ("duplicate %qE", type);
7088 /* "void", "_Bool", "char", "int", "float" or "double". */
7089 if (specs->typespec_word != cts_none)
7091 error ("two or more data types in declaration specifiers");
7098 error ("both %<long%> and %<void%> in "
7099 "declaration specifiers");
7100 else if (specs->short_p)
7101 error ("both %<short%> and %<void%> in "
7102 "declaration specifiers");
7103 else if (specs->signed_p)
7104 error ("both %<signed%> and %<void%> in "
7105 "declaration specifiers");
7106 else if (specs->unsigned_p)
7107 error ("both %<unsigned%> and %<void%> in "
7108 "declaration specifiers");
7109 else if (specs->complex_p)
7110 error ("both %<complex%> and %<void%> in "
7111 "declaration specifiers");
7113 specs->typespec_word = cts_void;
7117 error ("both %<long%> and %<_Bool%> in "
7118 "declaration specifiers");
7119 else if (specs->short_p)
7120 error ("both %<short%> and %<_Bool%> in "
7121 "declaration specifiers");
7122 else if (specs->signed_p)
7123 error ("both %<signed%> and %<_Bool%> in "
7124 "declaration specifiers");
7125 else if (specs->unsigned_p)
7126 error ("both %<unsigned%> and %<_Bool%> in "
7127 "declaration specifiers");
7128 else if (specs->complex_p)
7129 error ("both %<complex%> and %<_Bool%> in "
7130 "declaration specifiers");
7132 specs->typespec_word = cts_bool;
7136 error ("both %<long%> and %<char%> in "
7137 "declaration specifiers");
7138 else if (specs->short_p)
7139 error ("both %<short%> and %<char%> in "
7140 "declaration specifiers");
7142 specs->typespec_word = cts_char;
7145 specs->typespec_word = cts_int;
7149 error ("both %<long%> and %<float%> in "
7150 "declaration specifiers");
7151 else if (specs->short_p)
7152 error ("both %<short%> and %<float%> in "
7153 "declaration specifiers");
7154 else if (specs->signed_p)
7155 error ("both %<signed%> and %<float%> in "
7156 "declaration specifiers");
7157 else if (specs->unsigned_p)
7158 error ("both %<unsigned%> and %<float%> in "
7159 "declaration specifiers");
7161 specs->typespec_word = cts_float;
7164 if (specs->long_long_p)
7165 error ("both %<long long%> and %<double%> in "
7166 "declaration specifiers");
7167 else if (specs->short_p)
7168 error ("both %<short%> and %<double%> in "
7169 "declaration specifiers");
7170 else if (specs->signed_p)
7171 error ("both %<signed%> and %<double%> in "
7172 "declaration specifiers");
7173 else if (specs->unsigned_p)
7174 error ("both %<unsigned%> and %<double%> in "
7175 "declaration specifiers");
7177 specs->typespec_word = cts_double;
7180 /* ObjC reserved word "id", handled below. */
7186 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7187 form of ObjC type, cases such as "int" and "long" being handled
7188 above), a TYPE (struct, union, enum and typeof specifiers) or an
7189 ERROR_MARK. In none of these cases may there have previously
7190 been any type specifiers. */
7191 if (specs->type || specs->typespec_word != cts_none
7192 || specs->long_p || specs->short_p || specs->signed_p
7193 || specs->unsigned_p || specs->complex_p)
7194 error ("two or more data types in declaration specifiers");
7195 else if (TREE_CODE (type) == TYPE_DECL)
7197 if (TREE_TYPE (type) == error_mark_node)
7198 ; /* Allow the type to default to int to avoid cascading errors. */
7201 specs->type = TREE_TYPE (type);
7202 specs->decl_attr = DECL_ATTRIBUTES (type);
7203 specs->typedef_p = true;
7204 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7207 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7209 tree t = lookup_name (type);
7210 if (!t || TREE_CODE (t) != TYPE_DECL)
7211 error ("%qE fails to be a typedef or built in type", type);
7212 else if (TREE_TYPE (t) == error_mark_node)
7215 specs->type = TREE_TYPE (t);
7217 else if (TREE_CODE (type) != ERROR_MARK)
7219 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7220 specs->tag_defined_p = true;
7221 if (spec.kind == ctsk_typeof)
7222 specs->typedef_p = true;
7229 /* Add the storage class specifier or function specifier SCSPEC to the
7230 declaration specifiers SPECS, returning SPECS. */
7232 struct c_declspecs *
7233 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7236 enum c_storage_class n = csc_none;
7238 specs->declspecs_seen_p = true;
7239 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7240 && C_IS_RESERVED_WORD (scspec));
7241 i = C_RID_CODE (scspec);
7242 if (extra_warnings && specs->non_sc_seen_p)
7243 warning (0, "%qE is not at beginning of declaration", scspec);
7247 /* C99 permits duplicate inline. Although of doubtful utility,
7248 it seems simplest to permit it in gnu89 mode as well, as
7249 there is also little utility in maintaining this as a
7250 difference between gnu89 and C99 inline. */
7252 specs->inline_p = true;
7255 dupe = specs->thread_p;
7256 if (specs->storage_class == csc_auto)
7257 error ("%<__thread%> used with %<auto%>");
7258 else if (specs->storage_class == csc_register)
7259 error ("%<__thread%> used with %<register%>");
7260 else if (specs->storage_class == csc_typedef)
7261 error ("%<__thread%> used with %<typedef%>");
7263 specs->thread_p = true;
7270 /* Diagnose "__thread extern". */
7271 if (specs->thread_p)
7272 error ("%<__thread%> before %<extern%>");
7279 /* Diagnose "__thread static". */
7280 if (specs->thread_p)
7281 error ("%<__thread%> before %<static%>");
7289 if (n != csc_none && n == specs->storage_class)
7292 error ("duplicate %qE", scspec);
7295 if (specs->storage_class != csc_none && n != specs->storage_class)
7297 error ("multiple storage classes in declaration specifiers");
7301 specs->storage_class = n;
7302 if (n != csc_extern && n != csc_static && specs->thread_p)
7304 error ("%<__thread%> used with %qE", scspec);
7305 specs->thread_p = false;
7312 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7315 struct c_declspecs *
7316 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7318 specs->attrs = chainon (attrs, specs->attrs);
7319 specs->declspecs_seen_p = true;
7323 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7324 specifiers with any other type specifier to determine the resulting
7325 type. This is where ISO C checks on complex types are made, since
7326 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7329 struct c_declspecs *
7330 finish_declspecs (struct c_declspecs *specs)
7332 /* If a type was specified as a whole, we have no modifiers and are
7334 if (specs->type != NULL_TREE)
7336 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7337 && !specs->signed_p && !specs->unsigned_p
7338 && !specs->complex_p);
7342 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7343 has been specified, treat it as "int" unless "_Complex" is
7344 present and there are no other specifiers. If we just have
7345 "_Complex", it is equivalent to "_Complex double", but e.g.
7346 "_Complex short" is equivalent to "_Complex short int". */
7347 if (specs->typespec_word == cts_none)
7349 if (specs->long_p || specs->short_p
7350 || specs->signed_p || specs->unsigned_p)
7352 specs->typespec_word = cts_int;
7354 else if (specs->complex_p)
7356 specs->typespec_word = cts_double;
7358 pedwarn ("ISO C does not support plain %<complex%> meaning "
7359 "%<double complex%>");
7363 specs->typespec_word = cts_int;
7364 specs->default_int_p = true;
7365 /* We don't diagnose this here because grokdeclarator will
7366 give more specific diagnostics according to whether it is
7367 a function definition. */
7371 /* If "signed" was specified, record this to distinguish "int" and
7372 "signed int" in the case of a bit-field with
7373 -funsigned-bitfields. */
7374 specs->explicit_signed_p = specs->signed_p;
7376 /* Now compute the actual type. */
7377 switch (specs->typespec_word)
7380 gcc_assert (!specs->long_p && !specs->short_p
7381 && !specs->signed_p && !specs->unsigned_p
7382 && !specs->complex_p);
7383 specs->type = void_type_node;
7386 gcc_assert (!specs->long_p && !specs->short_p
7387 && !specs->signed_p && !specs->unsigned_p
7388 && !specs->complex_p);
7389 specs->type = boolean_type_node;
7392 gcc_assert (!specs->long_p && !specs->short_p);
7393 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7394 if (specs->signed_p)
7395 specs->type = signed_char_type_node;
7396 else if (specs->unsigned_p)
7397 specs->type = unsigned_char_type_node;
7399 specs->type = char_type_node;
7400 if (specs->complex_p)
7403 pedwarn ("ISO C does not support complex integer types");
7404 specs->type = build_complex_type (specs->type);
7408 gcc_assert (!(specs->long_p && specs->short_p));
7409 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7410 if (specs->long_long_p)
7411 specs->type = (specs->unsigned_p
7412 ? long_long_unsigned_type_node
7413 : long_long_integer_type_node);
7414 else if (specs->long_p)
7415 specs->type = (specs->unsigned_p
7416 ? long_unsigned_type_node
7417 : long_integer_type_node);
7418 else if (specs->short_p)
7419 specs->type = (specs->unsigned_p
7420 ? short_unsigned_type_node
7421 : short_integer_type_node);
7423 specs->type = (specs->unsigned_p
7424 ? unsigned_type_node
7425 : integer_type_node);
7426 if (specs->complex_p)
7429 pedwarn ("ISO C does not support complex integer types");
7430 specs->type = build_complex_type (specs->type);
7434 gcc_assert (!specs->long_p && !specs->short_p
7435 && !specs->signed_p && !specs->unsigned_p);
7436 specs->type = (specs->complex_p
7437 ? complex_float_type_node
7441 gcc_assert (!specs->long_long_p && !specs->short_p
7442 && !specs->signed_p && !specs->unsigned_p);
7445 specs->type = (specs->complex_p
7446 ? complex_long_double_type_node
7447 : long_double_type_node);
7451 specs->type = (specs->complex_p
7452 ? complex_double_type_node
7453 : double_type_node);
7463 /* Synthesize a function which calls all the global ctors or global
7464 dtors in this file. This is only used for targets which do not
7465 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7467 build_cdtor (int method_type, tree cdtors)
7474 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7475 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7478 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7481 /* Perform final processing on one file scope's declarations (or the
7482 external scope's declarations), GLOBALS. */
7484 c_write_global_declarations_1 (tree globals)
7486 size_t len = list_length (globals);
7487 tree *vec = XNEWVEC (tree, len);
7491 /* Process the decls in the order they were written. */
7492 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
7495 /* Check for used but undefined static functions using the C
7496 standard's definition of "used", and set TREE_NO_WARNING so
7497 that check_global_declarations doesn't repeat the check. */
7498 if (TREE_CODE (decl) == FUNCTION_DECL
7499 && DECL_INITIAL (decl) == 0
7500 && DECL_EXTERNAL (decl)
7501 && !TREE_PUBLIC (decl)
7502 && C_DECL_USED (decl))
7504 pedwarn ("%J%qF used but never defined", decl, decl);
7505 TREE_NO_WARNING (decl) = 1;
7509 wrapup_global_declarations (vec, len);
7510 check_global_declarations (vec, len);
7516 c_write_global_declarations (void)
7520 /* We don't want to do this if generating a PCH. */
7524 /* Don't waste time on further processing if -fsyntax-only or we've
7525 encountered errors. */
7526 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7529 /* Close the external scope. */
7530 ext_block = pop_scope ();
7532 gcc_assert (!current_scope);
7534 /* Process all file scopes in this compilation, and the external_scope,
7535 through wrapup_global_declarations and check_global_declarations. */
7536 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7537 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7538 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7540 /* Generate functions to call static constructors and destructors
7541 for targets that do not support .ctors/.dtors sections. These
7542 functions have magic names which are detected by collect2. */
7543 build_cdtor ('I', static_ctors); static_ctors = 0;
7544 build_cdtor ('D', static_dtors); static_dtors = 0;
7546 /* We're done parsing; proceed to optimize and emit assembly.
7547 FIXME: shouldn't be the front end's responsibility to call this. */
7551 #include "gt-c-decl.h"