1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
31 #include "coretypes.h"
35 #include "tree-inline.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
74 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* True means we've initialized exception handling. */
80 bool c_eh_initialized_p;
82 /* While defining an enum type, this is 1 plus the last enumerator
83 constant value. Note that will do not have to save this or `enum_overflow'
84 around nested function definition since such a definition could only
85 occur in an enum value expression and we don't use these variables in
88 static tree enum_next_value;
90 /* Nonzero means that there was overflow computing enum_next_value. */
92 static int enum_overflow;
94 /* These #defines are for clarity in working with the information block
95 returned by get_parm_info. */
96 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
97 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
98 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
99 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
101 /* The file and line that the prototype came from if this is an
102 old-style definition; used for diagnostics in
103 store_parm_decls_oldstyle. */
105 static location_t current_function_prototype_locus;
107 /* The current statement tree. */
109 static GTY(()) struct stmt_tree_s c_stmt_tree;
111 /* State saving variables. */
115 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
116 included in this invocation. Note that the current translation
117 unit is not included in this list. */
119 static GTY(()) tree all_translation_units;
121 /* A list of decls to be made automatically visible in each file scope. */
122 static GTY(()) tree visible_builtins;
124 /* Set to 0 at beginning of a function definition, set to 1 if
125 a return statement that specifies a return value is seen. */
127 int current_function_returns_value;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement with no argument is seen. */
132 int current_function_returns_null;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a call to a noreturn function is seen. */
137 int current_function_returns_abnormally;
139 /* Set to nonzero by `grokdeclarator' for a function
140 whose return type is defaulted, if warnings for this are desired. */
142 static int warn_about_return_type;
144 /* Nonzero when starting a function declared `extern inline'. */
146 static int current_extern_inline;
148 /* True means global_bindings_p should return false even if the scope stack
149 says we are in file scope. */
150 bool c_override_global_bindings_to_false;
153 /* Each c_binding structure describes one binding of an identifier to
154 a decl. All the decls in a scope - irrespective of namespace - are
155 chained together by the ->prev field, which (as the name implies)
156 runs in reverse order. All the decls in a given namespace bound to
157 a given identifier are chained by the ->shadowed field, which runs
158 from inner to outer scopes.
160 The ->decl field usually points to a DECL node, but there are two
161 exceptions. In the namespace of type tags, the bound entity is a
162 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
163 identifier is encountered, it is bound to error_mark_node to
164 suppress further errors about that identifier in the current
167 The depth field is copied from the scope structure that holds this
168 decl. It is used to preserve the proper ordering of the ->shadowed
169 field (see bind()) and also for a handful of special-case checks.
170 Finally, the invisible bit is true for a decl which should be
171 ignored for purposes of normal name lookup, and the nested bit is
172 true for a decl that's been bound a second time in an inner scope;
173 in all such cases, the binding in the outer scope will have its
174 invisible bit true. */
176 struct c_binding GTY((chain_next ("%h.prev")))
178 tree decl; /* the decl bound */
179 tree id; /* the identifier it's bound to */
180 struct c_binding *prev; /* the previous decl in this scope */
181 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
182 unsigned int depth : 28; /* depth of this scope */
183 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
184 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
187 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
188 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
189 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
190 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
192 #define I_SYMBOL_BINDING(node) \
193 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
194 #define I_SYMBOL_DECL(node) \
195 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
197 #define I_TAG_BINDING(node) \
198 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
199 #define I_TAG_DECL(node) \
200 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
202 #define I_LABEL_BINDING(node) \
203 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
204 #define I_LABEL_DECL(node) \
205 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
207 /* Each C symbol points to three linked lists of c_binding structures.
208 These describe the values of the identifier in the three different
209 namespaces defined by the language. */
211 struct lang_identifier GTY(())
213 struct c_common_identifier common_id;
214 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
215 struct c_binding *tag_binding; /* struct/union/enum tags */
216 struct c_binding *label_binding; /* labels */
219 /* Validate c-lang.c's assumptions. */
220 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
221 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
223 /* The resulting tree type. */
226 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
227 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *)TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *)TREE_CHAIN (&%h.generic)")))
229 union tree_node GTY ((tag ("0"),
230 desc ("tree_node_structure (&%h)")))
232 struct lang_identifier GTY ((tag ("1"))) identifier;
235 /* Each c_scope structure describes the complete contents of one
236 scope. Four scopes are distinguished specially: the innermost or
237 current scope, the innermost function scope, the file scope (always
238 the second to outermost) and the outermost or external scope.
240 Most declarations are recorded in the current scope.
242 All normal label declarations are recorded in the innermost
243 function scope, as are bindings of undeclared identifiers to
244 error_mark_node. (GCC permits nested functions as an extension,
245 hence the 'innermost' qualifier.) Explicitly declared labels
246 (using the __label__ extension) appear in the current scope.
248 Being in the file scope (current_scope == file_scope) causes
249 special behavior in several places below. Also, under some
250 conditions the Objective-C front end records declarations in the
251 file scope even though that isn't the current scope.
253 All declarations with external linkage are recorded in the external
254 scope, even if they aren't visible there; this models the fact that
255 such declarations are visible to the entire program, and (with a
256 bit of cleverness, see pushdecl) allows diagnosis of some violations
257 of C99 6.2.2p7 and 6.2.7p2:
259 If, within the same translation unit, the same identifier appears
260 with both internal and external linkage, the behavior is
263 All declarations that refer to the same object or function shall
264 have compatible type; otherwise, the behavior is undefined.
266 Initially only the built-in declarations, which describe compiler
267 intrinsic functions plus a subset of the standard library, are in
270 The order of the blocks list matters, and it is frequently appended
271 to. To avoid having to walk all the way to the end of the list on
272 each insertion, or reverse the list later, we maintain a pointer to
273 the last list entry. (FIXME: It should be feasible to use a reversed
276 The bindings list is strictly in reverse order of declarations;
277 pop_scope relies on this. */
280 struct c_scope GTY((chain_next ("%h.outer")))
282 /* The scope containing this one. */
283 struct c_scope *outer;
285 /* The next outermost function scope. */
286 struct c_scope *outer_function;
288 /* All bindings in this scope. */
289 struct c_binding *bindings;
291 /* For each scope (except the global one), a chain of BLOCK nodes
292 for all the scopes that were entered and exited one level down. */
296 /* The depth of this scope. Used to keep the ->shadowed chain of
297 bindings sorted innermost to outermost. */
298 unsigned int depth : 28;
300 /* True if we are currently filling this scope with parameter
302 BOOL_BITFIELD parm_flag : 1;
304 /* True if we already complained about forward parameter decls
305 in this scope. This prevents double warnings on
306 foo (int a; int b; ...) */
307 BOOL_BITFIELD warned_forward_parm_decls : 1;
309 /* True if this is the outermost block scope of a function body.
310 This scope contains the parameters, the local variables declared
311 in the outermost block, and all the labels (except those in
312 nested functions, or declared at block scope with __label__). */
313 BOOL_BITFIELD function_body : 1;
315 /* True means make a BLOCK for this scope no matter what. */
316 BOOL_BITFIELD keep : 1;
319 /* The scope currently in effect. */
321 static GTY(()) struct c_scope *current_scope;
323 /* The innermost function scope. Ordinary (not explicitly declared)
324 labels, bindings to error_mark_node, and the lazily-created
325 bindings of __func__ and its friends get this scope. */
327 static GTY(()) struct c_scope *current_function_scope;
329 /* The C file scope. This is reset for each input translation unit. */
331 static GTY(()) struct c_scope *file_scope;
333 /* The outermost scope. This is used for all declarations with
334 external linkage, and only these, hence the name. */
336 static GTY(()) struct c_scope *external_scope;
338 /* A chain of c_scope structures awaiting reuse. */
340 static GTY((deletable)) struct c_scope *scope_freelist;
342 /* A chain of c_binding structures awaiting reuse. */
344 static GTY((deletable)) struct c_binding *binding_freelist;
346 /* Append VAR to LIST in scope SCOPE. */
347 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
348 struct c_scope *s_ = (scope); \
350 if (s_->list##_last) \
351 TREE_CHAIN (s_->list##_last) = d_; \
354 s_->list##_last = d_; \
357 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
358 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
359 struct c_scope *t_ = (tscope); \
360 struct c_scope *f_ = (fscope); \
362 TREE_CHAIN (t_->to##_last) = f_->from; \
365 t_->to##_last = f_->from##_last; \
368 /* True means unconditionally make a BLOCK for the next scope pushed. */
370 static bool keep_next_level_flag;
372 /* True means the next call to push_scope will be the outermost scope
373 of a function body, so do not push a new scope, merely cease
374 expecting parameter decls. */
376 static bool next_is_function_body;
378 /* Functions called automatically at the beginning and end of execution. */
380 static GTY(()) tree static_ctors;
381 static GTY(()) tree static_dtors;
383 /* Forward declarations. */
384 static tree lookup_name_in_scope (tree, struct c_scope *);
385 static tree c_make_fname_decl (tree, int);
386 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
387 static tree grokparms (tree, int);
388 static void layout_array_type (tree);
390 /* States indicating how grokdeclarator() should handle declspecs marked
391 with __attribute__((deprecated)). An object declared as
392 __attribute__((deprecated)) suppresses warnings of uses of other
395 enum deprecated_states {
400 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
403 c_print_identifier (FILE *file, tree node, int indent)
405 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
406 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
407 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
408 if (C_IS_RESERVED_WORD (node))
410 tree rid = ridpointers[C_RID_CODE (node)];
411 indent_to (file, indent + 4);
412 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
413 (void *) rid, IDENTIFIER_POINTER (rid));
417 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
418 which may be any of several kinds of DECL or TYPE or error_mark_node,
419 in the scope SCOPE. */
421 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
423 struct c_binding *b, **here;
425 if (binding_freelist)
427 b = binding_freelist;
428 binding_freelist = b->prev;
431 b = ggc_alloc (sizeof (struct c_binding));
436 b->depth = scope->depth;
437 b->invisible = invisible;
440 b->prev = scope->bindings;
446 switch (TREE_CODE (decl))
448 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
451 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
457 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
463 /* Locate the appropriate place in the chain of shadowed decls
464 to insert this binding. Normally, scope == current_scope and
465 this does nothing. */
466 while (*here && (*here)->depth > scope->depth)
467 here = &(*here)->shadowed;
473 /* Clear the binding structure B, stick it on the binding_freelist,
474 and return the former value of b->prev. This is used by pop_scope
475 and get_parm_info to iterate destructively over all the bindings
476 from a given scope. */
477 static struct c_binding *
478 free_binding_and_advance (struct c_binding *b)
480 struct c_binding *prev = b->prev;
482 memset (b, 0, sizeof (struct c_binding));
483 b->prev = binding_freelist;
484 binding_freelist = b;
490 /* Hook called at end of compilation to assume 1 elt
491 for a file-scope tentative array defn that wasn't complete before. */
494 c_finish_incomplete_decl (tree decl)
496 if (TREE_CODE (decl) == VAR_DECL)
498 tree type = TREE_TYPE (decl);
499 if (type != error_mark_node
500 && TREE_CODE (type) == ARRAY_TYPE
501 && ! DECL_EXTERNAL (decl)
502 && TYPE_DOMAIN (type) == 0)
504 warning ("%Jarray '%D' assumed to have one element", decl, decl);
506 complete_array_type (type, NULL_TREE, 1);
508 layout_decl (decl, 0);
513 /* The Objective-C front-end often needs to determine the current scope. */
516 get_current_scope (void)
518 return current_scope;
521 /* The following function is used only by Objective-C. It needs to live here
522 because it accesses the innards of c_scope. */
525 objc_mark_locals_volatile (void *enclosing_blk)
527 struct c_scope *scope;
530 for (scope = current_scope;
531 scope && scope != enclosing_blk;
532 scope = scope->outer)
534 for (b = scope->bindings; b; b = b->prev)
536 if (TREE_CODE (b->decl) == VAR_DECL
537 || TREE_CODE (b->decl) == PARM_DECL)
539 C_DECL_REGISTER (b->decl) = 0;
540 DECL_REGISTER (b->decl) = 0;
541 TREE_THIS_VOLATILE (b->decl) = 1;
545 /* Do not climb up past the current function. */
546 if (scope->function_body)
551 /* Nonzero if we are currently in file scope. */
554 global_bindings_p (void)
556 return current_scope == file_scope && !c_override_global_bindings_to_false;
560 keep_next_level (void)
562 keep_next_level_flag = true;
565 /* Identify this scope as currently being filled with parameters. */
568 declare_parm_level (void)
570 current_scope->parm_flag = true;
576 if (next_is_function_body)
578 /* This is the transition from the parameters to the top level
579 of the function body. These are the same scope
580 (C99 6.2.1p4,6) so we do not push another scope structure.
581 next_is_function_body is set only by store_parm_decls, which
582 in turn is called when and only when we are about to
583 encounter the opening curly brace for the function body.
585 The outermost block of a function always gets a BLOCK node,
586 because the debugging output routines expect that each
587 function has at least one BLOCK. */
588 current_scope->parm_flag = false;
589 current_scope->function_body = true;
590 current_scope->keep = true;
591 current_scope->outer_function = current_function_scope;
592 current_function_scope = current_scope;
594 keep_next_level_flag = false;
595 next_is_function_body = false;
599 struct c_scope *scope;
602 scope = scope_freelist;
603 scope_freelist = scope->outer;
606 scope = ggc_alloc_cleared (sizeof (struct c_scope));
608 scope->keep = keep_next_level_flag;
609 scope->outer = current_scope;
610 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
612 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
614 if (current_scope && scope->depth == 0)
617 sorry ("GCC supports only %u nested scopes\n", scope->depth);
620 current_scope = scope;
621 keep_next_level_flag = false;
625 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
628 set_type_context (tree type, tree context)
630 for (type = TYPE_MAIN_VARIANT (type); type;
631 type = TYPE_NEXT_VARIANT (type))
632 TYPE_CONTEXT (type) = context;
635 /* Exit a scope. Restore the state of the identifier-decl mappings
636 that were in effect when this scope was entered. Return a BLOCK
637 node containing all the DECLs in this scope that are of interest
638 to debug info generation. */
643 struct c_scope *scope = current_scope;
644 tree block, context, p;
647 bool functionbody = scope->function_body;
648 bool keep = functionbody || scope->keep || scope->bindings;
650 /* If appropriate, create a BLOCK to record the decls for the life
655 block = make_node (BLOCK);
656 BLOCK_SUBBLOCKS (block) = scope->blocks;
657 TREE_USED (block) = 1;
659 /* In each subblock, record that this is its superior. */
660 for (p = scope->blocks; p; p = TREE_CHAIN (p))
661 BLOCK_SUPERCONTEXT (p) = block;
663 BLOCK_VARS (block) = 0;
666 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
667 scope must be set so that they point to the appropriate
668 construct, i.e. either to the current FUNCTION_DECL node, or
669 else to the BLOCK node we just constructed.
671 Note that for tagged types whose scope is just the formal
672 parameter list for some function type specification, we can't
673 properly set their TYPE_CONTEXTs here, because we don't have a
674 pointer to the appropriate FUNCTION_TYPE node readily available
675 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
676 type nodes get set in `grokdeclarator' as soon as we have created
677 the FUNCTION_TYPE node which will represent the "scope" for these
678 "parameter list local" tagged types. */
679 if (scope->function_body)
680 context = current_function_decl;
681 else if (scope == file_scope)
683 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
684 TREE_CHAIN (file_decl) = all_translation_units;
685 all_translation_units = file_decl;
691 /* Clear all bindings in this scope. */
692 for (b = scope->bindings; b; b = free_binding_and_advance (b))
695 switch (TREE_CODE (p))
698 /* Warnings for unused labels, errors for undefined labels. */
699 if (TREE_USED (p) && !DECL_INITIAL (p))
701 error ("%Jlabel `%D' used but not defined", p, p);
702 DECL_INITIAL (p) = error_mark_node;
704 else if (!TREE_USED (p) && warn_unused_label)
706 if (DECL_INITIAL (p))
707 warning ("%Jlabel `%D' defined but not used", p, p);
709 warning ("%Jlabel `%D' declared but not defined", p, p);
711 /* Labels go in BLOCK_VARS. */
712 TREE_CHAIN (p) = BLOCK_VARS (block);
713 BLOCK_VARS (block) = p;
715 #ifdef ENABLE_CHECKING
716 if (I_LABEL_BINDING (b->id) != b) abort ();
718 I_LABEL_BINDING (b->id) = b->shadowed;
724 set_type_context (p, context);
726 /* Types may not have tag-names, in which case the type
727 appears in the bindings list with b->id NULL. */
730 #ifdef ENABLE_CHECKING
731 if (I_TAG_BINDING (b->id) != b) abort ();
733 I_TAG_BINDING (b->id) = b->shadowed;
738 /* Propagate TREE_ADDRESSABLE from nested functions to their
739 containing functions. */
740 if (! TREE_ASM_WRITTEN (p)
741 && DECL_INITIAL (p) != 0
742 && TREE_ADDRESSABLE (p)
743 && DECL_ABSTRACT_ORIGIN (p) != 0
744 && DECL_ABSTRACT_ORIGIN (p) != p)
745 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
749 /* Warnings for unused variables. Keep this in sync with
750 stmt.c:warn_about_unused_variables, which we cannot use
751 since it expects a different data structure. */
752 if (warn_unused_variable
754 && !DECL_IN_SYSTEM_HEADER (p)
756 && !DECL_ARTIFICIAL (p)
757 && (scope != file_scope
758 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
759 && !TREE_THIS_VOLATILE (p)))
760 && scope != external_scope)
761 warning ("%Junused variable `%D'", p, p);
767 /* All of these go in BLOCK_VARS, but only if this is the
768 binding in the home scope. */
771 TREE_CHAIN (p) = BLOCK_VARS (block);
772 BLOCK_VARS (block) = p;
774 /* If this is the file scope, and we are processing more
775 than one translation unit in this compilation, set
776 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
777 This makes same_translation_unit_p work, and causes
778 static declarations to be given disambiguating suffixes. */
779 if (scope == file_scope && num_in_fnames > 1)
781 DECL_CONTEXT (p) = context;
782 if (TREE_CODE (p) == TYPE_DECL)
783 set_type_context (TREE_TYPE (p), context);
787 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
788 already been put there by store_parm_decls. Unused-
789 parameter warnings are handled by function.c.
790 error_mark_node obviously does not go in BLOCK_VARS and
791 does not get unused-variable warnings. */
794 /* It is possible for a decl not to have a name. We get
795 here with b->id NULL in this case. */
798 #ifdef ENABLE_CHECKING
799 if (I_SYMBOL_BINDING (b->id) != b) abort ();
801 I_SYMBOL_BINDING (b->id) = b->shadowed;
811 /* Dispose of the block that we just made inside some higher level. */
812 if ((scope->function_body || scope == file_scope) && context)
814 DECL_INITIAL (context) = block;
815 BLOCK_SUPERCONTEXT (block) = context;
817 else if (scope->outer)
820 SCOPE_LIST_APPEND (scope->outer, blocks, block);
821 /* If we did not make a block for the scope just exited, any
822 blocks made for inner scopes must be carried forward so they
823 will later become subblocks of something else. */
824 else if (scope->blocks)
825 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
828 /* Pop the current scope, and free the structure for reuse. */
829 current_scope = scope->outer;
830 if (scope->function_body)
831 current_function_scope = scope->outer_function;
833 memset (scope, 0, sizeof (struct c_scope));
834 scope->outer = scope_freelist;
835 scope_freelist = scope;
841 push_file_scope (void)
849 file_scope = current_scope;
851 start_fname_decls ();
853 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
854 bind (DECL_NAME (decl), decl, file_scope,
855 /*invisible=*/false, /*nested=*/true);
859 pop_file_scope (void)
861 /* In case there were missing closebraces, get us back to the global
863 while (current_scope != file_scope)
866 /* __FUNCTION__ is defined at file scope (""). This
867 call may not be necessary as my tests indicate it
868 still works without it. */
869 finish_fname_decls ();
871 /* This is the point to write out a PCH if we're doing that.
872 In that case we do not want to do anything else. */
875 c_common_write_pch ();
879 /* Pop off the file scope and close this translation unit. */
882 cgraph_finalize_compilation_unit ();
885 /* Insert BLOCK at the end of the list of subblocks of the current
886 scope. This is used when a BIND_EXPR is expanded, to handle the
887 BLOCK node inside the BIND_EXPR. */
890 insert_block (tree block)
892 TREE_USED (block) = 1;
893 SCOPE_LIST_APPEND (current_scope, blocks, block);
896 /* Push a definition or a declaration of struct, union or enum tag "name".
897 "type" should be the type node.
898 We assume that the tag "name" is not already defined.
900 Note that the definition may really be just a forward reference.
901 In that case, the TYPE_SIZE will be zero. */
904 pushtag (tree name, tree type)
906 /* Record the identifier as the type's name if it has none. */
907 if (name && !TYPE_NAME (type))
908 TYPE_NAME (type) = name;
909 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
911 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
912 tagged type we just added to the current scope. This fake
913 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
914 to output a representation of a tagged type, and it also gives
915 us a convenient place to record the "scope start" address for the
918 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
920 /* An approximation for now, so we can tell this is a function-scope tag.
921 This will be updated in pop_scope. */
922 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
925 /* Subroutine of compare_decls. Allow harmless mismatches in return
926 and argument types provided that the type modes match. This function
927 return a unified type given a suitable match, and 0 otherwise. */
930 match_builtin_function_types (tree newtype, tree oldtype)
932 tree newrettype, oldrettype;
933 tree newargs, oldargs;
934 tree trytype, tryargs;
936 /* Accept the return type of the new declaration if same modes. */
937 oldrettype = TREE_TYPE (oldtype);
938 newrettype = TREE_TYPE (newtype);
940 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
943 oldargs = TYPE_ARG_TYPES (oldtype);
944 newargs = TYPE_ARG_TYPES (newtype);
947 while (oldargs || newargs)
951 || ! TREE_VALUE (oldargs)
952 || ! TREE_VALUE (newargs)
953 || TYPE_MODE (TREE_VALUE (oldargs))
954 != TYPE_MODE (TREE_VALUE (newargs)))
957 oldargs = TREE_CHAIN (oldargs);
958 newargs = TREE_CHAIN (newargs);
961 trytype = build_function_type (newrettype, tryargs);
962 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
965 /* Subroutine of diagnose_mismatched_decls. Check for function type
966 mismatch involving an empty arglist vs a nonempty one and give clearer
969 diagnose_arglist_conflict (tree newdecl, tree olddecl,
970 tree newtype, tree oldtype)
974 if (TREE_CODE (olddecl) != FUNCTION_DECL
975 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
976 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
978 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
981 t = TYPE_ARG_TYPES (oldtype);
983 t = TYPE_ARG_TYPES (newtype);
984 for (; t; t = TREE_CHAIN (t))
986 tree type = TREE_VALUE (t);
988 if (TREE_CHAIN (t) == 0
989 && TYPE_MAIN_VARIANT (type) != void_type_node)
991 inform ("a parameter list with an ellipsis can't match "
992 "an empty parameter name list declaration");
996 if (c_type_promotes_to (type) != type)
998 inform ("an argument type that has a default promotion can't match "
999 "an empty parameter name list declaration");
1005 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1006 old-style function definition, NEWDECL is a prototype declaration.
1007 Diagnose inconsistencies in the argument list. Returns TRUE if
1008 the prototype is compatible, FALSE if not. */
1010 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1012 tree newargs, oldargs;
1015 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1016 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1018 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1019 newargs = TYPE_ARG_TYPES (newtype);
1024 tree oldargtype = TREE_VALUE (oldargs);
1025 tree newargtype = TREE_VALUE (newargs);
1027 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1030 /* Reaching the end of just one list means the two decls don't
1031 agree on the number of arguments. */
1032 if (END_OF_ARGLIST (oldargtype))
1034 error ("%Jprototype for '%D' declares more arguments "
1035 "than previous old-style definition", newdecl, newdecl);
1038 else if (END_OF_ARGLIST (newargtype))
1040 error ("%Jprototype for '%D' declares fewer arguments "
1041 "than previous old-style definition", newdecl, newdecl);
1045 /* Type for passing arg must be consistent with that declared
1047 else if (! comptypes (oldargtype, newargtype))
1049 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1050 newdecl, newdecl, i);
1054 oldargs = TREE_CHAIN (oldargs);
1055 newargs = TREE_CHAIN (newargs);
1059 /* If we get here, no errors were found, but do issue a warning
1060 for this poor-style construct. */
1061 warning ("%Jprototype for '%D' follows non-prototype definition",
1064 #undef END_OF_ARGLIST
1067 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1068 first in a pair of mismatched declarations, using the diagnostic
1071 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1073 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1075 else if (DECL_INITIAL (decl))
1076 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1077 else if (C_DECL_IMPLICIT (decl))
1078 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1080 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1083 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1084 Returns true if the caller should proceed to merge the two, false
1085 if OLDDECL should simply be discarded. As a side effect, issues
1086 all necessary diagnostics for invalid or poor-style combinations.
1087 If it returns true, writes the types of NEWDECL and OLDDECL to
1088 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1089 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1092 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1093 tree *newtypep, tree *oldtypep)
1095 tree newtype, oldtype;
1096 bool pedwarned = false;
1097 bool warned = false;
1099 /* If we have error_mark_node for either decl or type, just discard
1100 the previous decl - we're in an error cascade already. */
1101 if (olddecl == error_mark_node || newdecl == error_mark_node)
1103 *oldtypep = oldtype = TREE_TYPE (olddecl);
1104 *newtypep = newtype = TREE_TYPE (newdecl);
1105 if (oldtype == error_mark_node || newtype == error_mark_node)
1108 /* Two different categories of symbol altogether. This is an error
1109 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1110 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1112 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1113 && DECL_BUILT_IN (olddecl)
1114 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1116 error ("%J'%D' redeclared as different kind of symbol",
1118 locate_old_decl (olddecl, error);
1120 else if (TREE_PUBLIC (newdecl))
1121 warning ("%Jbuilt-in function '%D' declared as non-function",
1123 else if (warn_shadow)
1124 warning ("%Jdeclaration of '%D' shadows a built-in function",
1129 if (!comptypes (oldtype, newtype))
1131 if (TREE_CODE (olddecl) == FUNCTION_DECL
1132 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1134 /* Accept harmless mismatch in function types.
1135 This is for the ffs and fprintf builtins. */
1136 tree trytype = match_builtin_function_types (newtype, oldtype);
1138 if (trytype && comptypes (newtype, trytype))
1139 *oldtypep = oldtype = trytype;
1142 /* If types don't match for a built-in, throw away the
1143 built-in. No point in calling locate_old_decl here, it
1144 won't print anything. */
1145 warning ("%Jconflicting types for built-in function '%D'",
1150 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1151 && DECL_IS_BUILTIN (olddecl))
1153 /* A conflicting function declaration for a predeclared
1154 function that isn't actually built in. Objective C uses
1155 these. The new declaration silently overrides everything
1156 but the volatility (i.e. noreturn) indication. See also
1157 below. FIXME: Make Objective C use normal builtins. */
1158 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1161 /* Permit void foo (...) to match int foo (...) if the latter is
1162 the definition and implicit int was used. See
1163 c-torture/compile/920625-2.c. */
1164 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1165 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1166 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1167 && C_FUNCTION_IMPLICIT_INT (newdecl))
1169 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1170 /* Make sure we keep void as the return type. */
1171 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1172 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1177 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1178 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1180 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1181 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1182 locate_old_decl (olddecl, error);
1187 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1188 but silently ignore the redeclaration if either is in a system
1189 header. (Conflicting redeclarations were handled above.) */
1190 if (TREE_CODE (newdecl) == TYPE_DECL)
1192 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1193 return true; /* Allow OLDDECL to continue in use. */
1195 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1196 locate_old_decl (olddecl, error);
1200 /* Function declarations can either be 'static' or 'extern' (no
1201 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1202 can never conflict with each other on account of linkage (6.2.2p4).
1203 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1204 two definitions if one is 'extern inline' and one is not. The non-
1205 extern-inline definition supersedes the extern-inline definition. */
1206 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1208 /* If you declare a built-in function name as static, or
1209 define the built-in with an old-style definition (so we
1210 can't validate the argument list) the built-in definition is
1211 overridden, but optionally warn this was a bad choice of name. */
1212 if (DECL_BUILT_IN (olddecl)
1213 && !C_DECL_DECLARED_BUILTIN (olddecl)
1214 && (!TREE_PUBLIC (newdecl)
1215 || (DECL_INITIAL (newdecl)
1216 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1219 warning ("%Jdeclaration of '%D' shadows a built-in function",
1221 /* Discard the old built-in function. */
1225 if (DECL_INITIAL (newdecl))
1227 if (DECL_INITIAL (olddecl)
1228 && !(DECL_DECLARED_INLINE_P (olddecl)
1229 && DECL_EXTERNAL (olddecl)
1230 && !(DECL_DECLARED_INLINE_P (newdecl)
1231 && DECL_EXTERNAL (newdecl)
1232 && same_translation_unit_p (olddecl, newdecl))))
1234 error ("%Jredefinition of '%D'", newdecl, newdecl);
1235 locate_old_decl (olddecl, error);
1239 /* If we have a prototype after an old-style function definition,
1240 the argument types must be checked specially. */
1241 else if (DECL_INITIAL (olddecl)
1242 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1243 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1244 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1246 locate_old_decl (olddecl, error);
1249 /* A non-static declaration (even an "extern") followed by a
1250 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1251 The same is true for a static forward declaration at block
1252 scope followed by a non-static declaration/definition at file
1253 scope. Static followed by non-static at the same scope is
1254 not undefined behavior, and is the most convenient way to get
1255 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1256 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1257 we do diagnose it if -Wtraditional. */
1258 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1260 /* Two exceptions to the rule. If olddecl is an extern
1261 inline, or a predeclared function that isn't actually
1262 built in, newdecl silently overrides olddecl. The latter
1263 occur only in Objective C; see also above. (FIXME: Make
1264 Objective C use normal builtins.) */
1265 if (!DECL_IS_BUILTIN (olddecl)
1266 && !(DECL_EXTERNAL (olddecl)
1267 && DECL_DECLARED_INLINE_P (olddecl)))
1269 error ("%Jstatic declaration of '%D' follows "
1270 "non-static declaration", newdecl, newdecl);
1271 locate_old_decl (olddecl, error);
1275 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1277 if (DECL_CONTEXT (olddecl))
1279 error ("%Jnon-static declaration of '%D' follows "
1280 "static declaration", newdecl, newdecl);
1281 locate_old_decl (olddecl, error);
1284 else if (warn_traditional)
1286 warning ("%Jnon-static declaration of '%D' follows "
1287 "static declaration", newdecl, newdecl);
1292 else if (TREE_CODE (newdecl) == VAR_DECL)
1294 /* Only variables can be thread-local, and all declarations must
1295 agree on this property. */
1296 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1298 if (DECL_THREAD_LOCAL (newdecl))
1299 error ("%Jthread-local declaration of '%D' follows "
1300 "non-thread-local declaration", newdecl, newdecl);
1302 error ("%Jnon-thread-local declaration of '%D' follows "
1303 "thread-local declaration", newdecl, newdecl);
1305 locate_old_decl (olddecl, error);
1309 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1310 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1312 error ("%Jredefinition of '%D'", newdecl, newdecl);
1313 locate_old_decl (olddecl, error);
1317 /* Objects declared at file scope: if the first declaration had
1318 external linkage (even if it was an external reference) the
1319 second must have external linkage as well, or the behavior is
1320 undefined. If the first declaration had internal linkage, then
1321 the second must too, or else be an external reference (in which
1322 case the composite declaration still has internal linkage).
1323 As for function declarations, we warn about the static-then-
1324 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1325 if (DECL_FILE_SCOPE_P (newdecl)
1326 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1328 if (DECL_EXTERNAL (newdecl))
1330 if (warn_traditional)
1332 warning ("%Jnon-static declaration of '%D' follows "
1333 "static declaration", newdecl, newdecl);
1339 if (TREE_PUBLIC (newdecl))
1340 error ("%Jnon-static declaration of '%D' follows "
1341 "static declaration", newdecl, newdecl);
1343 error ("%Jstatic declaration of '%D' follows "
1344 "non-static declaration", newdecl, newdecl);
1346 locate_old_decl (olddecl, error);
1350 /* Two objects with the same name declared at the same block
1351 scope must both be external references (6.7p3). */
1352 else if (!DECL_FILE_SCOPE_P (newdecl)
1353 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1354 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1356 if (DECL_EXTERNAL (newdecl))
1357 error ("%Jextern declaration of '%D' follows "
1358 "declaration with no linkage", newdecl, newdecl);
1359 else if (DECL_EXTERNAL (olddecl))
1360 error ("%Jdeclaration of '%D' with no linkage follows "
1361 "extern declaration", newdecl, newdecl);
1363 error ("%Jredeclaration of '%D' with no linkage",
1366 locate_old_decl (olddecl, error);
1372 /* All decls must agree on a non-default visibility. */
1373 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1374 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1375 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1377 warning ("%Jredeclaration of '%D' with different visibility "
1378 "(old visibility preserved)", newdecl, newdecl);
1382 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1384 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1385 if (DECL_DECLARED_INLINE_P (newdecl)
1386 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1388 warning ("%Jinline declaration of '%D' follows "
1389 "declaration with attribute noinline", newdecl, newdecl);
1392 else if (DECL_DECLARED_INLINE_P (olddecl)
1393 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1395 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1396 "inline declaration ", newdecl, newdecl);
1400 /* Inline declaration after use or definition.
1401 ??? Should we still warn about this now we have unit-at-a-time
1402 mode and can get it right?
1403 Definitely don't complain if the decls are in different translation
1405 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1406 && same_translation_unit_p (olddecl, newdecl))
1408 if (TREE_USED (olddecl))
1410 warning ("%J'%D' declared inline after being called",
1414 else if (DECL_INITIAL (olddecl))
1416 warning ("%J'%D' declared inline after its definition",
1422 else /* PARM_DECL, VAR_DECL */
1424 /* Redeclaration of a parameter is a constraint violation (this is
1425 not explicitly stated, but follows from C99 6.7p3 [no more than
1426 one declaration of the same identifier with no linkage in the
1427 same scope, except type tags] and 6.2.2p6 [parameters have no
1428 linkage]). We must check for a forward parameter declaration,
1429 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1430 an extension, the mandatory diagnostic for which is handled by
1431 mark_forward_parm_decls. */
1433 if (TREE_CODE (newdecl) == PARM_DECL
1434 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1436 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1437 locate_old_decl (olddecl, error);
1442 /* Optional warning for completely redundant decls. */
1443 if (!warned && !pedwarned
1444 && warn_redundant_decls
1445 /* Don't warn about a function declaration followed by a
1447 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1448 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1449 /* Don't warn about an extern followed by a definition. */
1450 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1451 /* Don't warn about forward parameter decls. */
1452 && !(TREE_CODE (newdecl) == PARM_DECL
1453 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1455 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1459 /* Report location of previous decl/defn in a consistent manner. */
1460 if (warned || pedwarned)
1461 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1466 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1467 consistent with OLDDECL, but carries new information. Merge the
1468 new information into OLDDECL. This function issues no
1472 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1474 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1475 && DECL_INITIAL (newdecl) != 0);
1477 /* For real parm decl following a forward decl, rechain the old decl
1478 in its new location and clear TREE_ASM_WRITTEN (it's not a
1479 forward decl anymore). */
1480 if (TREE_CODE (newdecl) == PARM_DECL
1481 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1483 struct c_binding *b, **here;
1485 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1486 if ((*here)->decl == olddecl)
1493 b->prev = current_scope->bindings;
1494 current_scope->bindings = b;
1496 TREE_ASM_WRITTEN (olddecl) = 0;
1499 DECL_ATTRIBUTES (newdecl)
1500 = targetm.merge_decl_attributes (olddecl, newdecl);
1502 /* Merge the data types specified in the two decls. */
1504 = TREE_TYPE (olddecl)
1505 = composite_type (newtype, oldtype);
1507 /* Lay the type out, unless already done. */
1508 if (oldtype != TREE_TYPE (newdecl))
1510 if (TREE_TYPE (newdecl) != error_mark_node)
1511 layout_type (TREE_TYPE (newdecl));
1512 if (TREE_CODE (newdecl) != FUNCTION_DECL
1513 && TREE_CODE (newdecl) != TYPE_DECL
1514 && TREE_CODE (newdecl) != CONST_DECL)
1515 layout_decl (newdecl, 0);
1519 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1520 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1521 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1522 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1523 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1524 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1526 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1527 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1531 /* Keep the old rtl since we can safely use it. */
1532 COPY_DECL_RTL (olddecl, newdecl);
1534 /* Merge the type qualifiers. */
1535 if (TREE_READONLY (newdecl))
1536 TREE_READONLY (olddecl) = 1;
1538 if (TREE_THIS_VOLATILE (newdecl))
1540 TREE_THIS_VOLATILE (olddecl) = 1;
1541 if (TREE_CODE (newdecl) == VAR_DECL)
1542 make_var_volatile (newdecl);
1545 /* Keep source location of definition rather than declaration. */
1546 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1547 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1549 /* Merge the unused-warning information. */
1550 if (DECL_IN_SYSTEM_HEADER (olddecl))
1551 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1552 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1553 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1555 /* Merge the initialization information. */
1556 if (DECL_INITIAL (newdecl) == 0)
1557 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1559 /* Merge the section attribute.
1560 We want to issue an error if the sections conflict but that must be
1561 done later in decl_attributes since we are called before attributes
1563 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1564 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1566 /* Copy the assembler name.
1567 Currently, it can only be defined in the prototype. */
1568 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1570 /* If either declaration has a nondefault visibility, use it. */
1571 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1572 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1574 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1576 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1577 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1578 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1579 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1580 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1581 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1582 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1583 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1584 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1587 /* Merge the storage class information. */
1588 merge_weak (newdecl, olddecl);
1590 /* For functions, static overrides non-static. */
1591 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1593 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1594 /* This is since we don't automatically
1595 copy the attributes of NEWDECL into OLDDECL. */
1596 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1597 /* If this clears `static', clear it in the identifier too. */
1598 if (! TREE_PUBLIC (olddecl))
1599 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1601 if (DECL_EXTERNAL (newdecl))
1603 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1604 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1606 /* An extern decl does not override previous storage class. */
1607 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1608 if (! DECL_EXTERNAL (newdecl))
1610 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1611 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1616 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1617 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1620 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1622 /* If we're redefining a function previously defined as extern
1623 inline, make sure we emit debug info for the inline before we
1624 throw it away, in case it was inlined into a function that hasn't
1625 been written out yet. */
1626 if (new_is_definition && DECL_INITIAL (olddecl))
1628 if (TREE_USED (olddecl)
1629 /* In unit-at-a-time mode we never inline re-defined extern
1630 inline functions. */
1631 && !flag_unit_at_a_time
1632 && cgraph_function_possibly_inlined_p (olddecl))
1633 (*debug_hooks->outlining_inline_function) (olddecl);
1635 /* The new defn must not be inline. */
1636 DECL_INLINE (newdecl) = 0;
1637 DECL_UNINLINABLE (newdecl) = 1;
1641 /* If either decl says `inline', this fn is inline,
1642 unless its definition was passed already. */
1643 if (DECL_DECLARED_INLINE_P (newdecl)
1644 || DECL_DECLARED_INLINE_P (olddecl))
1645 DECL_DECLARED_INLINE_P (newdecl) = 1;
1647 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1648 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1651 if (DECL_BUILT_IN (olddecl))
1653 /* If redeclaring a builtin function, it stays built in.
1654 But it gets tagged as having been declared. */
1655 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1656 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1657 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1660 /* Also preserve various other info from the definition. */
1661 if (! new_is_definition)
1663 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1664 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1665 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1666 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1667 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1669 /* Set DECL_INLINE on the declaration if we've got a body
1670 from which to instantiate. */
1671 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1673 DECL_INLINE (newdecl) = 1;
1674 DECL_ABSTRACT_ORIGIN (newdecl)
1675 = DECL_ABSTRACT_ORIGIN (olddecl);
1680 /* If a previous declaration said inline, mark the
1681 definition as inlinable. */
1682 if (DECL_DECLARED_INLINE_P (newdecl)
1683 && ! DECL_UNINLINABLE (newdecl))
1684 DECL_INLINE (newdecl) = 1;
1688 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1689 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1691 unsigned olddecl_uid = DECL_UID (olddecl);
1692 tree olddecl_context = DECL_CONTEXT (olddecl);
1694 memcpy ((char *) olddecl + sizeof (struct tree_common),
1695 (char *) newdecl + sizeof (struct tree_common),
1696 sizeof (struct tree_decl) - sizeof (struct tree_common));
1697 DECL_UID (olddecl) = olddecl_uid;
1698 DECL_CONTEXT (olddecl) = olddecl_context;
1701 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1702 so that encode_section_info has a chance to look at the new decl
1703 flags and attributes. */
1704 if (DECL_RTL_SET_P (olddecl)
1705 && (TREE_CODE (olddecl) == FUNCTION_DECL
1706 || (TREE_CODE (olddecl) == VAR_DECL
1707 && TREE_STATIC (olddecl))))
1708 make_decl_rtl (olddecl, NULL);
1711 /* Handle when a new declaration NEWDECL has the same name as an old
1712 one OLDDECL in the same binding contour. Prints an error message
1715 If safely possible, alter OLDDECL to look like NEWDECL, and return
1716 true. Otherwise, return false. */
1719 duplicate_decls (tree newdecl, tree olddecl)
1721 tree newtype = NULL, oldtype = NULL;
1723 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1726 merge_decls (newdecl, olddecl, newtype, oldtype);
1731 /* Check whether decl-node NEW shadows an existing declaration. */
1733 warn_if_shadowing (tree new)
1735 struct c_binding *b;
1737 /* Shadow warnings wanted? */
1739 /* No shadow warnings for internally generated vars. */
1740 || DECL_IS_BUILTIN (new)
1741 /* No shadow warnings for vars made for inlining. */
1742 || DECL_FROM_INLINE (new)
1743 /* Don't warn about the parm names in function declarator
1744 within a function declarator. It would be nice to avoid
1745 warning in any function declarator in a declaration, as
1746 opposed to a definition, but there is no way to tell
1747 it's not a definition at this point. */
1748 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1751 /* Is anything being shadowed? Invisible decls do not count. */
1752 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1753 if (b->decl && b->decl != new && !b->invisible)
1757 if (TREE_CODE (old) == PARM_DECL)
1758 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1759 else if (DECL_FILE_SCOPE_P (old))
1760 warning ("%Jdeclaration of '%D' shadows a global declaration",
1762 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1763 warning ("%Jdeclaration of '%D' shadows a built-in function",
1766 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1768 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1769 warning ("%Jshadowed declaration is here", old);
1776 /* Subroutine of pushdecl.
1778 X is a TYPE_DECL for a typedef statement. Create a brand new
1779 ..._TYPE node (which will be just a variant of the existing
1780 ..._TYPE node with identical properties) and then install X
1781 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1783 The whole point here is to end up with a situation where each
1784 and every ..._TYPE node the compiler creates will be uniquely
1785 associated with AT MOST one node representing a typedef name.
1786 This way, even though the compiler substitutes corresponding
1787 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1788 early on, later parts of the compiler can always do the reverse
1789 translation and get back the corresponding typedef name. For
1792 typedef struct S MY_TYPE;
1795 Later parts of the compiler might only know that `object' was of
1796 type `struct S' if it were not for code just below. With this
1797 code however, later parts of the compiler see something like:
1799 struct S' == struct S
1800 typedef struct S' MY_TYPE;
1803 And they can then deduce (from the node for type struct S') that
1804 the original object declaration was:
1808 Being able to do this is important for proper support of protoize,
1809 and also for generating precise symbolic debugging information
1810 which takes full account of the programmer's (typedef) vocabulary.
1812 Obviously, we don't want to generate a duplicate ..._TYPE node if
1813 the TYPE_DECL node that we are now processing really represents a
1814 standard built-in type.
1816 Since all standard types are effectively declared at line zero
1817 in the source file, we can easily check to see if we are working
1818 on a standard type by checking the current value of lineno. */
1821 clone_underlying_type (tree x)
1823 if (DECL_IS_BUILTIN (x))
1825 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1826 TYPE_NAME (TREE_TYPE (x)) = x;
1828 else if (TREE_TYPE (x) != error_mark_node
1829 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1831 tree tt = TREE_TYPE (x);
1832 DECL_ORIGINAL_TYPE (x) = tt;
1833 tt = build_type_copy (tt);
1835 TREE_USED (tt) = TREE_USED (x);
1840 /* Record a decl-node X as belonging to the current lexical scope.
1841 Check for errors (such as an incompatible declaration for the same
1842 name already seen in the same scope).
1844 Returns either X or an old decl for the same name.
1845 If an old decl is returned, it may have been smashed
1846 to agree with what X says. */
1851 tree name = DECL_NAME (x);
1852 struct c_scope *scope = current_scope;
1853 struct c_binding *b;
1854 bool nested = false;
1856 /* Functions need the lang_decl data. */
1857 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1858 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1860 /* Must set DECL_CONTEXT for everything not at file scope or
1861 DECL_FILE_SCOPE_P won't work. Local externs don't count
1862 unless they have initializers (which generate code). */
1863 if (current_function_decl
1864 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1865 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1866 DECL_CONTEXT (x) = current_function_decl;
1868 /* Anonymous decls are just inserted in the scope. */
1871 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1875 /* First, see if there is another declaration with the same name in
1876 the current scope. If there is, duplicate_decls may do all the
1877 work for us. If duplicate_decls returns false, that indicates
1878 two incompatible decls in the same scope; we are to silently
1879 replace the old one (duplicate_decls has issued all appropriate
1880 diagnostics). In particular, we should not consider possible
1881 duplicates in the external scope, or shadowing. */
1882 b = I_SYMBOL_BINDING (name);
1883 if (b && B_IN_SCOPE (b, scope))
1885 if (duplicate_decls (x, b->decl))
1888 goto skip_external_and_shadow_checks;
1891 /* All declarations with external linkage, and all external
1892 references, go in the external scope, no matter what scope is
1893 current. However, the binding in that scope is ignored for
1894 purposes of normal name lookup. A separate binding structure is
1895 created in the requested scope; this governs the normal
1896 visibility of the symbol.
1898 The binding in the externals scope is used exclusively for
1899 detecting duplicate declarations of the same object, no matter
1900 what scope they are in; this is what we do here. (C99 6.2.7p2:
1901 All declarations that refer to the same object or function shall
1902 have compatible type; otherwise, the behavior is undefined.) */
1903 if (DECL_EXTERNAL (x) || scope == file_scope)
1905 if (warn_nested_externs
1906 && scope != file_scope
1907 && !DECL_IN_SYSTEM_HEADER (x))
1908 warning ("nested extern declaration of '%D'", x);
1910 while (b && !B_IN_EXTERNAL_SCOPE (b))
1913 /* The point of the same_translation_unit_p check here is,
1914 we want to detect a duplicate decl for a construct like
1915 foo() { extern bar(); } ... static bar(); but not if
1916 they are in different translation units. In any case,
1917 the static does not go in the externals scope. */
1919 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
1920 && duplicate_decls (x, b->decl))
1922 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1925 else if (TREE_PUBLIC (x))
1927 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
1931 /* Similarly, a declaration of a function with static linkage at
1932 block scope must be checked against any existing declaration
1933 of that function at file scope. */
1934 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
1935 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
1937 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
1938 warning ("nested static declaration of '%D'", x);
1940 while (b && !B_IN_FILE_SCOPE (b))
1943 if (b && same_translation_unit_p (x, b->decl)
1944 && duplicate_decls (x, b->decl))
1946 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
1951 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
1956 warn_if_shadowing (x);
1958 skip_external_and_shadow_checks:
1959 if (TREE_CODE (x) == TYPE_DECL)
1960 clone_underlying_type (x);
1962 bind (name, x, scope, /*invisible=*/false, nested);
1964 /* If x's type is incomplete because it's based on a
1965 structure or union which has not yet been fully declared,
1966 attach it to that structure or union type, so we can go
1967 back and complete the variable declaration later, if the
1968 structure or union gets fully declared.
1970 If the input is erroneous, we can have error_mark in the type
1971 slot (e.g. "f(void a, ...)") - that doesn't count as an
1973 if (TREE_TYPE (x) != error_mark_node
1974 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1976 tree element = TREE_TYPE (x);
1978 while (TREE_CODE (element) == ARRAY_TYPE)
1979 element = TREE_TYPE (element);
1980 element = TYPE_MAIN_VARIANT (element);
1982 if ((TREE_CODE (element) == RECORD_TYPE
1983 || TREE_CODE (element) == UNION_TYPE)
1984 && (TREE_CODE (x) != TYPE_DECL
1985 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1986 && !COMPLETE_TYPE_P (element))
1987 C_TYPE_INCOMPLETE_VARS (element)
1988 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1993 /* Record X as belonging to file scope.
1994 This is used only internally by the Objective-C front end,
1995 and is limited to its needs. duplicate_decls is not called;
1996 if there is any preexisting decl for this identifier, it is an ICE. */
1999 pushdecl_top_level (tree x)
2002 bool nested = false;
2004 if (TREE_CODE (x) != VAR_DECL)
2007 name = DECL_NAME (x);
2009 if (I_SYMBOL_BINDING (name))
2012 if (TREE_PUBLIC (x))
2014 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2018 bind (name, x, file_scope, /*invisible=*/false, nested);
2024 implicit_decl_warning (tree id, tree olddecl)
2026 void (*diag) (const char *, ...);
2027 switch (mesg_implicit_function_declaration)
2030 case 1: diag = warning; break;
2031 case 2: diag = error; break;
2035 diag (N_("implicit declaration of function '%E'"), id);
2037 locate_old_decl (olddecl, diag);
2040 /* Generate an implicit declaration for identifier FUNCTIONID as a
2041 function of type int (). */
2044 implicitly_declare (tree functionid)
2046 tree decl = lookup_name_in_scope (functionid, external_scope);
2050 /* FIXME: Objective-C has weird not-really-builtin functions
2051 which are supposed to be visible automatically. They wind up
2052 in the external scope because they're pushed before the file
2053 scope gets created. Catch this here and rebind them into the
2055 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2057 bind (functionid, decl, file_scope,
2058 /*invisible=*/false, /*nested=*/true);
2063 /* Implicit declaration of a function already declared
2064 (somehow) in a different scope, or as a built-in.
2065 If this is the first time this has happened, warn;
2066 then recycle the old declaration. */
2067 if (!C_DECL_IMPLICIT (decl))
2069 implicit_decl_warning (functionid, decl);
2070 C_DECL_IMPLICIT (decl) = 1;
2072 bind (functionid, decl, current_scope,
2073 /*invisible=*/false, /*nested=*/true);
2078 /* Not seen before. */
2079 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2080 DECL_EXTERNAL (decl) = 1;
2081 TREE_PUBLIC (decl) = 1;
2082 C_DECL_IMPLICIT (decl) = 1;
2083 implicit_decl_warning (functionid, 0);
2085 /* C89 says implicit declarations are in the innermost block.
2086 So we record the decl in the standard fashion. */
2087 decl = pushdecl (decl);
2089 /* No need to call objc_check_decl here - it's a function type. */
2090 rest_of_decl_compilation (decl, NULL, 0, 0);
2092 /* Write a record describing this implicit function declaration
2093 to the prototypes file (if requested). */
2094 gen_aux_info_record (decl, 0, 1, 0);
2096 /* Possibly apply some default attributes to this implicit declaration. */
2097 decl_attributes (&decl, NULL_TREE, 0);
2102 /* Issue an error message for a reference to an undeclared variable
2103 ID, including a reference to a builtin outside of function-call
2104 context. Establish a binding of the identifier to error_mark_node
2105 in an appropriate scope, which will suppress further errors for the
2108 undeclared_variable (tree id)
2110 static bool already = false;
2111 struct c_scope *scope;
2113 if (current_function_decl == 0)
2115 error ("'%E' undeclared here (not in a function)", id);
2116 scope = current_scope;
2120 error ("'%E' undeclared (first use in this function)", id);
2124 error ("(Each undeclared identifier is reported only once");
2125 error ("for each function it appears in.)");
2129 /* If we are parsing old-style parameter decls, current_function_decl
2130 will be nonnull but current_function_scope will be null. */
2131 scope = current_function_scope ? current_function_scope : current_scope;
2133 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2136 /* Subroutine of lookup_label, declare_label, define_label: construct a
2137 LABEL_DECL with all the proper frills. */
2140 make_label (tree name, location_t location)
2142 tree label = build_decl (LABEL_DECL, name, void_type_node);
2144 DECL_CONTEXT (label) = current_function_decl;
2145 DECL_MODE (label) = VOIDmode;
2146 DECL_SOURCE_LOCATION (label) = location;
2151 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2152 Create one if none exists so far for the current function.
2153 This is called when a label is used in a goto expression or
2154 has its address taken. */
2157 lookup_label (tree name)
2161 if (current_function_decl == 0)
2163 error ("label %s referenced outside of any function",
2164 IDENTIFIER_POINTER (name));
2168 /* Use a label already defined or ref'd with this name, but not if
2169 it is inherited from a containing function and wasn't declared
2171 label = I_LABEL_DECL (name);
2172 if (label && (DECL_CONTEXT (label) == current_function_decl
2173 || C_DECLARED_LABEL_FLAG (label)))
2175 /* If the label has only been declared, update its apparent
2176 location to point here, for better diagnostics if it
2177 turns out not to have been defined. */
2178 if (!TREE_USED (label))
2179 DECL_SOURCE_LOCATION (label) = input_location;
2183 /* No label binding for that identifier; make one. */
2184 label = make_label (name, input_location);
2186 /* Ordinary labels go in the current function scope. */
2187 bind (name, label, current_function_scope,
2188 /*invisible=*/false, /*nested=*/false);
2192 /* Make a label named NAME in the current function, shadowing silently
2193 any that may be inherited from containing functions or containing
2194 scopes. This is called for __label__ declarations. */
2197 declare_label (tree name)
2199 struct c_binding *b = I_LABEL_BINDING (name);
2202 /* Check to make sure that the label hasn't already been declared
2204 if (b && B_IN_CURRENT_SCOPE (b))
2206 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2207 locate_old_decl (b->decl, error);
2209 /* Just use the previous declaration. */
2213 label = make_label (name, input_location);
2214 C_DECLARED_LABEL_FLAG (label) = 1;
2216 /* Declared labels go in the current scope. */
2217 bind (name, label, current_scope,
2218 /*invisible=*/false, /*nested=*/false);
2222 /* Define a label, specifying the location in the source file.
2223 Return the LABEL_DECL node for the label, if the definition is valid.
2224 Otherwise return 0. */
2227 define_label (location_t location, tree name)
2229 /* Find any preexisting label with this name. It is an error
2230 if that label has already been defined in this function, or
2231 if there is a containing function with a declared label with
2233 tree label = I_LABEL_DECL (name);
2236 && ((DECL_CONTEXT (label) == current_function_decl
2237 && DECL_INITIAL (label) != 0)
2238 || (DECL_CONTEXT (label) != current_function_decl
2239 && C_DECLARED_LABEL_FLAG (label))))
2241 error ("%Hduplicate label `%D'", &location, label);
2242 locate_old_decl (label, error);
2245 else if (label && DECL_CONTEXT (label) == current_function_decl)
2247 /* The label has been used or declared already in this function,
2248 but not defined. Update its location to point to this
2250 DECL_SOURCE_LOCATION (label) = location;
2254 /* No label binding for that identifier; make one. */
2255 label = make_label (name, location);
2257 /* Ordinary labels go in the current function scope. */
2258 bind (name, label, current_function_scope,
2259 /*invisible=*/false, /*nested=*/false);
2262 if (warn_traditional && !in_system_header && lookup_name (name))
2263 warning ("%Htraditional C lacks a separate namespace for labels, "
2264 "identifier `%s' conflicts", &location,
2265 IDENTIFIER_POINTER (name));
2267 /* Mark label as having been defined. */
2268 DECL_INITIAL (label) = error_mark_node;
2272 /* Given NAME, an IDENTIFIER_NODE,
2273 return the structure (or union or enum) definition for that name.
2274 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2275 CODE says which kind of type the caller wants;
2276 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2277 If the wrong kind of type is found, an error is reported. */
2280 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2282 struct c_binding *b = I_TAG_BINDING (name);
2288 /* We only care about whether it's in this level if
2289 thislevel_only was set or it might be a type clash. */
2290 if (thislevel_only || TREE_CODE (b->decl) != code)
2292 /* For our purposes, a tag in the external scope is the same as
2293 a tag in the file scope. (Primarily relevant to Objective-C
2294 and its builtin structure tags, which get pushed before the
2295 file scope is created.) */
2296 if (B_IN_CURRENT_SCOPE (b)
2297 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2301 if (thislevel_only && !thislevel)
2304 if (TREE_CODE (b->decl) != code)
2306 /* Definition isn't the kind we were looking for. */
2307 pending_invalid_xref = name;
2308 pending_invalid_xref_location = input_location;
2310 /* If in the same binding level as a declaration as a tag
2311 of a different type, this must not be allowed to
2312 shadow that tag, so give the error immediately.
2313 (For example, "struct foo; union foo;" is invalid.) */
2315 pending_xref_error ();
2320 /* Print an error message now
2321 for a recent invalid struct, union or enum cross reference.
2322 We don't print them immediately because they are not invalid
2323 when used in the `struct foo;' construct for shadowing. */
2326 pending_xref_error (void)
2328 if (pending_invalid_xref != 0)
2329 error ("%H`%s' defined as wrong kind of tag",
2330 &pending_invalid_xref_location,
2331 IDENTIFIER_POINTER (pending_invalid_xref));
2332 pending_invalid_xref = 0;
2336 /* Look up NAME in the current scope and its superiors
2337 in the namespace of variables, functions and typedefs.
2338 Return a ..._DECL node of some kind representing its definition,
2339 or return 0 if it is undefined. */
2342 lookup_name (tree name)
2344 struct c_binding *b = I_SYMBOL_BINDING (name);
2345 if (b && !b->invisible)
2350 /* Similar to `lookup_name' but look only at the indicated scope. */
2353 lookup_name_in_scope (tree name, struct c_scope *scope)
2355 struct c_binding *b;
2357 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2358 if (B_IN_SCOPE (b, scope))
2363 /* Create the predefined scalar types of C,
2364 and some nodes representing standard constants (0, 1, (void *) 0).
2365 Initialize the global scope.
2366 Make definitions for built-in primitive functions. */
2369 c_init_decl_processing (void)
2372 tree ptr_ftype_void, ptr_ftype_ptr;
2373 location_t save_loc = input_location;
2375 /* Adds some ggc roots, and reserved words for c-parse.in. */
2378 current_function_decl = 0;
2380 /* Make the externals scope. */
2382 external_scope = current_scope;
2384 /* Declarations from c_common_nodes_and_builtins must not be associated
2385 with this input file, lest we get differences between using and not
2386 using preprocessed headers. */
2387 #ifdef USE_MAPPED_LOCATION
2388 input_location = BUILTINS_LOCATION;
2390 input_location.file = "<built-in>";
2391 input_location.line = 0;
2394 build_common_tree_nodes (flag_signed_char);
2396 c_common_nodes_and_builtins ();
2398 /* In C, comparisons and TRUTH_* expressions have type int. */
2399 truthvalue_type_node = integer_type_node;
2400 truthvalue_true_node = integer_one_node;
2401 truthvalue_false_node = integer_zero_node;
2403 /* Even in C99, which has a real boolean type. */
2404 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2405 boolean_type_node));
2407 endlink = void_list_node;
2408 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2410 = build_function_type (ptr_type_node,
2411 tree_cons (NULL_TREE, ptr_type_node, endlink));
2413 input_location = save_loc;
2415 pedantic_lvalues = true;
2417 make_fname_decl = c_make_fname_decl;
2418 start_fname_decls ();
2421 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2422 decl, NAME is the initialization string and TYPE_DEP indicates whether
2423 NAME depended on the type of the function. As we don't yet implement
2424 delayed emission of static data, we mark the decl as emitted
2425 so it is not placed in the output. Anything using it must therefore pull
2426 out the STRING_CST initializer directly. FIXME. */
2429 c_make_fname_decl (tree id, int type_dep)
2431 const char *name = fname_as_string (type_dep);
2432 tree decl, type, init;
2433 size_t length = strlen (name);
2435 type = build_array_type
2436 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2437 build_index_type (size_int (length)));
2439 decl = build_decl (VAR_DECL, id, type);
2441 TREE_STATIC (decl) = 1;
2442 TREE_READONLY (decl) = 1;
2443 DECL_ARTIFICIAL (decl) = 1;
2445 init = build_string (length + 1, name);
2446 free ((char *) name);
2447 TREE_TYPE (init) = type;
2448 DECL_INITIAL (decl) = init;
2450 TREE_USED (decl) = 1;
2452 if (current_function_decl)
2454 DECL_CONTEXT (decl) = current_function_decl;
2455 bind (id, decl, current_function_scope,
2456 /*invisible=*/false, /*nested=*/false);
2459 finish_decl (decl, init, NULL_TREE);
2464 /* Return a definition for a builtin function named NAME and whose data type
2465 is TYPE. TYPE should be a function type with argument types.
2466 FUNCTION_CODE tells later passes how to compile calls to this function.
2467 See tree.h for its possible values.
2469 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2470 the name to be called if we can't opencode the function. If
2471 ATTRS is nonzero, use that for the function's attribute list. */
2474 builtin_function (const char *name, tree type, int function_code,
2475 enum built_in_class class, const char *library_name,
2478 tree id = get_identifier (name);
2479 tree decl = build_decl (FUNCTION_DECL, id, type);
2480 TREE_PUBLIC (decl) = 1;
2481 DECL_EXTERNAL (decl) = 1;
2482 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2483 DECL_BUILT_IN_CLASS (decl) = class;
2484 DECL_FUNCTION_CODE (decl) = function_code;
2486 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2488 /* Should never be called on a symbol with a preexisting meaning. */
2489 if (I_SYMBOL_BINDING (id))
2492 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2494 /* Builtins in the implementation namespace are made visible without
2495 needing to be explicitly declared. See push_file_scope. */
2496 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2498 TREE_CHAIN (decl) = visible_builtins;
2499 visible_builtins = decl;
2502 /* Possibly apply some default attributes to this built-in function. */
2504 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2506 decl_attributes (&decl, NULL_TREE, 0);
2511 /* Called when a declaration is seen that contains no names to declare.
2512 If its type is a reference to a structure, union or enum inherited
2513 from a containing scope, shadow that tag name for the current scope
2514 with a forward reference.
2515 If its type defines a new named structure or union
2516 or defines an enum, it is valid but we need not do anything here.
2517 Otherwise, it is an error. */
2520 shadow_tag (tree declspecs)
2522 shadow_tag_warned (declspecs, 0);
2526 shadow_tag_warned (tree declspecs, int warned)
2529 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2536 pending_invalid_xref = 0;
2538 /* Remove the attributes from declspecs, since they will confuse the
2540 split_specs_attrs (declspecs, &specs, &attrs);
2542 for (link = specs; link; link = TREE_CHAIN (link))
2544 tree value = TREE_VALUE (link);
2545 enum tree_code code = TREE_CODE (value);
2547 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2548 /* Used to test also that TYPE_SIZE (value) != 0.
2549 That caused warning for `struct foo;' at top level in the file. */
2551 tree name = TYPE_NAME (value);
2558 if (warned != 1 && code != ENUMERAL_TYPE)
2559 /* Empty unnamed enum OK */
2561 pedwarn ("unnamed struct/union that defines no instances");
2567 t = lookup_tag (code, name, 1);
2571 t = make_node (code);
2578 if (!warned && ! in_system_header)
2580 warning ("useless keyword or type name in empty declaration");
2587 error ("two types specified in one empty declaration");
2592 pedwarn ("empty declaration");
2596 /* Construct an array declarator. EXPR is the expression inside [], or
2597 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2598 to the pointer to which a parameter array is converted). STATIC_P is
2599 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2600 is nonzero is the array is [*], a VLA of unspecified length which is
2601 nevertheless a complete type (not currently implemented by GCC),
2602 zero otherwise. The declarator is constructed as an ARRAY_REF
2603 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2604 left of the [] (filled by in set_array_declarator_type) and operand 1
2605 is the expression inside; whose TREE_TYPE is the type qualifiers and
2606 which has TREE_STATIC set if "static" is used. */
2609 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2612 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2613 TREE_TYPE (decl) = quals;
2614 TREE_STATIC (decl) = (static_p ? 1 : 0);
2615 if (pedantic && !flag_isoc99)
2617 if (static_p || quals != NULL_TREE)
2618 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2620 pedwarn ("ISO C90 does not support `[*]' array declarators");
2623 warning ("GCC does not yet properly implement `[*]' array declarators");
2627 /* Set the type of an array declarator. DECL is the declarator, as
2628 constructed by build_array_declarator; TYPE is what appears on the left
2629 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2630 abstract declarator, zero otherwise; this is used to reject static and
2631 type qualifiers in abstract declarators, where they are not in the
2635 set_array_declarator_type (tree decl, tree type, int abstract_p)
2637 TREE_OPERAND (decl, 0) = type;
2638 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2639 error ("static or type qualifiers in abstract declarator");
2643 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2646 groktypename (tree typename)
2650 if (TREE_CODE (typename) != TREE_LIST)
2653 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2655 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2658 /* Apply attributes. */
2659 decl_attributes (&typename, attrs, 0);
2664 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2667 groktypename_in_parm_context (tree typename)
2669 if (TREE_CODE (typename) != TREE_LIST)
2671 return grokdeclarator (TREE_VALUE (typename),
2672 TREE_PURPOSE (typename),
2676 /* Decode a declarator in an ordinary declaration or data definition.
2677 This is called as soon as the type information and variable name
2678 have been parsed, before parsing the initializer if any.
2679 Here we create the ..._DECL node, fill in its type,
2680 and put it on the list of decls for the current context.
2681 The ..._DECL node is returned as the value.
2683 Exception: for arrays where the length is not specified,
2684 the type is left null, to be filled in by `finish_decl'.
2686 Function definitions do not come here; they go to start_function
2687 instead. However, external and forward declarations of functions
2688 do go through here. Structure field declarations are done by
2689 grokfield and not through here. */
2692 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2697 /* An object declared as __attribute__((deprecated)) suppresses
2698 warnings of uses of other deprecated items. */
2699 if (lookup_attribute ("deprecated", attributes))
2700 deprecated_state = DEPRECATED_SUPPRESS;
2702 decl = grokdeclarator (declarator, declspecs,
2703 NORMAL, initialized, NULL);
2705 deprecated_state = DEPRECATED_NORMAL;
2707 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2708 && MAIN_NAME_P (DECL_NAME (decl)))
2709 warning ("%J'%D' is usually a function", decl, decl);
2712 /* Is it valid for this decl to have an initializer at all?
2713 If not, set INITIALIZED to zero, which will indirectly
2714 tell 'finish_decl' to ignore the initializer once it is parsed. */
2715 switch (TREE_CODE (decl))
2718 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2723 error ("function '%D' is initialized like a variable", decl);
2728 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2729 error ("parameter '%D' is initialized", decl);
2734 /* Don't allow initializations for incomplete types except for
2735 arrays which might be completed by the initialization. */
2737 /* This can happen if the array size is an undefined macro.
2738 We already gave a warning, so we don't need another one. */
2739 if (TREE_TYPE (decl) == error_mark_node)
2741 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2743 /* A complete type is ok if size is fixed. */
2745 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2746 || C_DECL_VARIABLE_SIZE (decl))
2748 error ("variable-sized object may not be initialized");
2752 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2754 error ("variable '%D' has initializer but incomplete type", decl);
2757 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2759 error ("elements of array '%D' have incomplete type", decl);
2766 DECL_EXTERNAL (decl) = 0;
2767 if (current_scope == file_scope)
2768 TREE_STATIC (decl) = 1;
2770 /* Tell 'pushdecl' this is an initialized decl
2771 even though we don't yet have the initializer expression.
2772 Also tell 'finish_decl' it may store the real initializer. */
2773 DECL_INITIAL (decl) = error_mark_node;
2776 /* If this is a function declaration, write a record describing it to the
2777 prototypes file (if requested). */
2779 if (TREE_CODE (decl) == FUNCTION_DECL)
2780 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2782 /* ANSI specifies that a tentative definition which is not merged with
2783 a non-tentative definition behaves exactly like a definition with an
2784 initializer equal to zero. (Section 3.7.2)
2786 -fno-common gives strict ANSI behavior, though this tends to break
2787 a large body of code that grew up without this rule.
2789 Thread-local variables are never common, since there's no entrenched
2790 body of code to break, and it allows more efficient variable references
2791 in the presence of dynamic linking. */
2793 if (TREE_CODE (decl) == VAR_DECL
2795 && TREE_PUBLIC (decl)
2796 && !DECL_THREAD_LOCAL (decl)
2798 DECL_COMMON (decl) = 1;
2800 /* Set attributes here so if duplicate decl, will have proper attributes. */
2801 decl_attributes (&decl, attributes, 0);
2803 if (TREE_CODE (decl) == FUNCTION_DECL
2804 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2806 tree ce = declarator;
2808 if (TREE_CODE (ce) == INDIRECT_REF)
2809 ce = TREE_OPERAND (declarator, 0);
2810 if (TREE_CODE (ce) == CALL_EXPR)
2812 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2813 for (; args; args = TREE_CHAIN (args))
2815 tree type = TREE_TYPE (args);
2816 if (type && INTEGRAL_TYPE_P (type)
2817 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2818 DECL_ARG_TYPE (args) = integer_type_node;
2823 if (TREE_CODE (decl) == FUNCTION_DECL
2824 && DECL_DECLARED_INLINE_P (decl)
2825 && DECL_UNINLINABLE (decl)
2826 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2827 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2829 /* Add this decl to the current scope.
2830 TEM may equal DECL or it may be a previous decl of the same name. */
2831 tem = pushdecl (decl);
2836 /* Finish processing of a declaration;
2837 install its initial value.
2838 If the length of an array type is not known before,
2839 it must be determined now, from the initial value, or it is an error. */
2842 finish_decl (tree decl, tree init, tree asmspec_tree)
2844 tree type = TREE_TYPE (decl);
2845 int was_incomplete = (DECL_SIZE (decl) == 0);
2846 const char *asmspec = 0;
2848 /* If a name was specified, get the string. */
2849 if (current_scope == file_scope)
2850 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2852 asmspec = TREE_STRING_POINTER (asmspec_tree);
2854 /* If `start_decl' didn't like having an initialization, ignore it now. */
2855 if (init != 0 && DECL_INITIAL (decl) == 0)
2858 /* Don't crash if parm is initialized. */
2859 if (TREE_CODE (decl) == PARM_DECL)
2863 store_init_value (decl, init);
2865 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2866 || TREE_CODE (decl) == FUNCTION_DECL
2867 || TREE_CODE (decl) == FIELD_DECL))
2868 objc_check_decl (decl);
2870 /* Deduce size of array from initialization, if not already known. */
2871 if (TREE_CODE (type) == ARRAY_TYPE
2872 && TYPE_DOMAIN (type) == 0
2873 && TREE_CODE (decl) != TYPE_DECL)
2876 = (TREE_STATIC (decl)
2877 /* Even if pedantic, an external linkage array
2878 may have incomplete type at first. */
2879 ? pedantic && !TREE_PUBLIC (decl)
2880 : !DECL_EXTERNAL (decl));
2882 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2884 /* Get the completed type made by complete_array_type. */
2885 type = TREE_TYPE (decl);
2888 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2890 else if (failure == 2)
2893 error ("%Jarray size missing in '%D'", decl, decl);
2894 /* If a `static' var's size isn't known,
2895 make it extern as well as static, so it does not get
2897 If it is not `static', then do not mark extern;
2898 finish_incomplete_decl will give it a default size
2899 and it will get allocated. */
2900 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2901 DECL_EXTERNAL (decl) = 1;
2904 /* TYPE_MAX_VALUE is always one less than the number of elements
2905 in the array, because we start counting at zero. Therefore,
2906 warn only if the value is less than zero. */
2907 else if (pedantic && TYPE_DOMAIN (type) != 0
2908 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2909 error ("%Jzero or negative size array '%D'", decl, decl);
2911 layout_decl (decl, 0);
2914 if (TREE_CODE (decl) == VAR_DECL)
2916 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2917 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2918 layout_decl (decl, 0);
2920 if (DECL_SIZE (decl) == 0
2921 /* Don't give an error if we already gave one earlier. */
2922 && TREE_TYPE (decl) != error_mark_node
2923 && (TREE_STATIC (decl)
2925 /* A static variable with an incomplete type
2926 is an error if it is initialized.
2927 Also if it is not file scope.
2928 Otherwise, let it through, but if it is not `extern'
2929 then it may cause an error message later. */
2930 (DECL_INITIAL (decl) != 0
2931 || !DECL_FILE_SCOPE_P (decl))
2933 /* An automatic variable with an incomplete type
2935 !DECL_EXTERNAL (decl)))
2937 error ("%Jstorage size of '%D' isn't known", decl, decl);
2938 TREE_TYPE (decl) = error_mark_node;
2941 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2942 && DECL_SIZE (decl) != 0)
2944 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2945 constant_expression_warning (DECL_SIZE (decl));
2947 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2950 if (TREE_USED (type))
2951 TREE_USED (decl) = 1;
2954 /* If this is a function and an assembler name is specified, reset DECL_RTL
2955 so we can give it its new name. Also, update built_in_decls if it
2956 was a normal built-in. */
2957 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2959 /* ASMSPEC is given, and not the name of a register. Mark the
2960 name with a star so assemble_name won't munge it. */
2961 char *starred = alloca (strlen (asmspec) + 2);
2963 strcpy (starred + 1, asmspec);
2965 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2967 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2968 SET_DECL_RTL (builtin, NULL_RTX);
2969 change_decl_assembler_name (builtin, get_identifier (starred));
2970 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2971 init_block_move_fn (starred);
2972 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2973 init_block_clear_fn (starred);
2975 SET_DECL_RTL (decl, NULL_RTX);
2976 change_decl_assembler_name (decl, get_identifier (starred));
2979 /* If #pragma weak was used, mark the decl weak now. */
2980 if (current_scope == file_scope)
2981 maybe_apply_pragma_weak (decl);
2983 /* Output the assembler code and/or RTL code for variables and functions,
2984 unless the type is an undefined structure or union.
2985 If not, it will get done when the type is completed. */
2987 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2989 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2990 if (c_dialect_objc ())
2991 objc_check_decl (decl);
2993 if (DECL_FILE_SCOPE_P (decl))
2995 if (DECL_INITIAL (decl) == NULL_TREE
2996 || DECL_INITIAL (decl) == error_mark_node)
2997 /* Don't output anything
2998 when a tentative file-scope definition is seen.
2999 But at end of compilation, do output code for them. */
3000 DECL_DEFER_OUTPUT (decl) = 1;
3001 rest_of_decl_compilation (decl, asmspec, true, 0);
3005 /* This is a local variable. If there is an ASMSPEC, the
3006 user has requested that we handle it specially. */
3009 /* In conjunction with an ASMSPEC, the `register'
3010 keyword indicates that we should place the variable
3011 in a particular register. */
3012 if (C_DECL_REGISTER (decl))
3014 DECL_HARD_REGISTER (decl) = 1;
3015 /* This cannot be done for a structure with volatile
3016 fields, on which DECL_REGISTER will have been
3018 if (!DECL_REGISTER (decl))
3019 error ("cannot put object with volatile field into register");
3022 /* If this is not a static variable, issue a warning.
3023 It doesn't make any sense to give an ASMSPEC for an
3024 ordinary, non-register local variable. Historically,
3025 GCC has accepted -- but ignored -- the ASMSPEC in
3027 if (TREE_CODE (decl) == VAR_DECL
3028 && !C_DECL_REGISTER (decl)
3029 && !TREE_STATIC (decl))
3030 warning ("%Jignoring asm-specifier for non-static local "
3031 "variable '%D'", decl, decl);
3033 change_decl_assembler_name (decl, get_identifier (asmspec));
3036 if (TREE_CODE (decl) != FUNCTION_DECL)
3037 add_stmt (build_stmt (DECL_EXPR, decl));
3040 if (!DECL_FILE_SCOPE_P (decl))
3042 /* Recompute the RTL of a local array now
3043 if it used to be an incomplete type. */
3045 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3047 /* If we used it already as memory, it must stay in memory. */
3048 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3049 /* If it's still incomplete now, no init will save it. */
3050 if (DECL_SIZE (decl) == 0)
3051 DECL_INITIAL (decl) = 0;
3056 /* If this was marked 'used', be sure it will be output. */
3057 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3058 mark_decl_referenced (decl);
3060 if (TREE_CODE (decl) == TYPE_DECL)
3062 if (!DECL_FILE_SCOPE_P (decl)
3063 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3064 add_stmt (build_stmt (DECL_EXPR, decl));
3066 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
3069 /* At the end of a declaration, throw away any variable type sizes
3070 of types defined inside that declaration. There is no use
3071 computing them in the following function definition. */
3072 if (current_scope == file_scope)
3073 get_pending_sizes ();
3075 /* Install a cleanup (aka destructor) if one was given. */
3076 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3078 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3081 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3082 tree cleanup_decl = lookup_name (cleanup_id);
3085 /* Build "cleanup(&decl)" for the destructor. */
3086 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3087 cleanup = build_tree_list (NULL_TREE, cleanup);
3088 cleanup = build_function_call (cleanup_decl, cleanup);
3090 /* Don't warn about decl unused; the cleanup uses it. */
3091 TREE_USED (decl) = 1;
3092 TREE_USED (cleanup_decl) = 1;
3094 /* Initialize EH, if we've been told to do so. */
3095 if (flag_exceptions && !c_eh_initialized_p)
3097 c_eh_initialized_p = true;
3098 eh_personality_libfunc
3099 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3100 ? "__gcc_personality_sj0"
3101 : "__gcc_personality_v0");
3102 using_eh_for_cleanups ();
3105 push_cleanup (decl, cleanup, false);
3110 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3111 and push that on the current scope. */
3114 push_parm_decl (tree parm)
3118 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3119 TREE_PURPOSE (TREE_PURPOSE (parm)),
3121 decl_attributes (&decl, TREE_VALUE (parm), 0);
3123 decl = pushdecl (decl);
3125 finish_decl (decl, NULL_TREE, NULL_TREE);
3128 /* Mark all the parameter declarations to date as forward decls.
3129 Also diagnose use of this extension. */
3132 mark_forward_parm_decls (void)
3134 struct c_binding *b;
3136 if (pedantic && !current_scope->warned_forward_parm_decls)
3138 pedwarn ("ISO C forbids forward parameter declarations");
3139 current_scope->warned_forward_parm_decls = true;
3142 for (b = current_scope->bindings; b; b = b->prev)
3143 if (TREE_CODE (b->decl) == PARM_DECL)
3144 TREE_ASM_WRITTEN (b->decl) = 1;
3147 static GTY(()) int compound_literal_number;
3149 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3150 literal, which may be an incomplete array type completed by the
3151 initializer; INIT is a CONSTRUCTOR that initializes the compound
3155 build_compound_literal (tree type, tree init)
3157 /* We do not use start_decl here because we have a type, not a declarator;
3158 and do not use finish_decl because the decl should be stored inside
3159 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3160 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3163 DECL_EXTERNAL (decl) = 0;
3164 TREE_PUBLIC (decl) = 0;
3165 TREE_STATIC (decl) = (current_scope == file_scope);
3166 DECL_CONTEXT (decl) = current_function_decl;
3167 TREE_USED (decl) = 1;
3168 TREE_TYPE (decl) = type;
3169 TREE_READONLY (decl) = TYPE_READONLY (type);
3170 store_init_value (decl, init);
3172 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3174 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3179 type = TREE_TYPE (decl);
3180 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3181 return error_mark_node;
3183 stmt = build_stmt (DECL_EXPR, decl);
3184 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3185 TREE_SIDE_EFFECTS (complit) = 1;
3187 layout_decl (decl, 0);
3189 if (TREE_STATIC (decl))
3191 /* This decl needs a name for the assembler output. We also need
3192 a unique suffix to be added to the name. */
3195 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3196 compound_literal_number);
3197 compound_literal_number++;
3198 DECL_NAME (decl) = get_identifier (name);
3199 DECL_DEFER_OUTPUT (decl) = 1;
3200 DECL_COMDAT (decl) = 1;
3201 DECL_ARTIFICIAL (decl) = 1;
3203 rest_of_decl_compilation (decl, NULL, 1, 0);
3209 /* Make TYPE a complete type based on INITIAL_VALUE.
3210 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3211 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3214 complete_array_type (tree type, tree initial_value, int do_default)
3216 tree maxindex = NULL_TREE;
3221 /* Note MAXINDEX is really the maximum index,
3222 one less than the size. */
3223 if (TREE_CODE (initial_value) == STRING_CST)
3226 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3227 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3230 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3232 tree elts = CONSTRUCTOR_ELTS (initial_value);
3233 maxindex = build_int_2 (-1, -1);
3234 for (; elts; elts = TREE_CHAIN (elts))
3236 if (TREE_PURPOSE (elts))
3237 maxindex = TREE_PURPOSE (elts);
3239 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3240 maxindex, integer_one_node));
3242 maxindex = copy_node (maxindex);
3246 /* Make an error message unless that happened already. */
3247 if (initial_value != error_mark_node)
3250 /* Prevent further error messages. */
3251 maxindex = build_int_2 (0, 0);
3258 maxindex = build_int_2 (0, 0);
3264 TYPE_DOMAIN (type) = build_index_type (maxindex);
3265 if (!TREE_TYPE (maxindex))
3266 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3269 /* Lay out the type now that we can get the real answer. */
3276 /* Determine whether TYPE is a structure with a flexible array member,
3277 or a union containing such a structure (possibly recursively). */
3280 flexible_array_type_p (tree type)
3283 switch (TREE_CODE (type))
3286 x = TYPE_FIELDS (type);
3289 while (TREE_CHAIN (x) != NULL_TREE)
3291 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3292 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3293 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3294 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3298 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3300 if (flexible_array_type_p (TREE_TYPE (x)))
3309 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3310 replacing with appropriate values if they are invalid. */
3312 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3315 unsigned int max_width;
3316 unsigned HOST_WIDE_INT w;
3317 const char *name = orig_name ? orig_name: _("<anonymous>");
3320 STRIP_NOPS (*width);
3322 /* Detect and ignore out of range field width and process valid
3324 if (TREE_CODE (*width) != INTEGER_CST)
3326 error ("bit-field `%s' width not an integer constant", name);
3327 *width = integer_one_node;
3331 constant_expression_warning (*width);
3332 if (tree_int_cst_sgn (*width) < 0)
3334 error ("negative width in bit-field `%s'", name);
3335 *width = integer_one_node;
3337 else if (integer_zerop (*width) && orig_name)
3339 error ("zero width for bit-field `%s'", name);
3340 *width = integer_one_node;
3344 /* Detect invalid bit-field type. */
3345 if (TREE_CODE (*type) != INTEGER_TYPE
3346 && TREE_CODE (*type) != BOOLEAN_TYPE
3347 && TREE_CODE (*type) != ENUMERAL_TYPE)
3349 error ("bit-field `%s' has invalid type", name);
3350 *type = unsigned_type_node;
3353 type_mv = TYPE_MAIN_VARIANT (*type);
3355 && type_mv != integer_type_node
3356 && type_mv != unsigned_type_node
3357 && type_mv != boolean_type_node)
3358 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3360 if (type_mv == boolean_type_node)
3361 max_width = CHAR_TYPE_SIZE;
3363 max_width = TYPE_PRECISION (*type);
3365 if (0 < compare_tree_int (*width, max_width))
3367 error ("width of `%s' exceeds its type", name);
3369 *width = build_int_2 (w, 0);
3372 w = tree_low_cst (*width, 1);
3374 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3376 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3378 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3379 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3380 warning ("`%s' is narrower than values of its type", name);
3384 /* Given declspecs and a declarator,
3385 determine the name and type of the object declared
3386 and construct a ..._DECL node for it.
3387 (In one case we can return a ..._TYPE node instead.
3388 For invalid input we sometimes return 0.)
3390 DECLSPECS is a chain of tree_list nodes whose value fields
3391 are the storage classes and type specifiers.
3393 DECL_CONTEXT says which syntactic context this declaration is in:
3394 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3395 FUNCDEF for a function definition. Like NORMAL but a few different
3396 error messages in each case. Return value may be zero meaning
3397 this definition is too screwy to try to parse.
3398 PARM for a parameter declaration (either within a function prototype
3399 or before a function body). Make a PARM_DECL, or return void_type_node.
3400 TYPENAME if for a typename (in a cast or sizeof).
3401 Don't make a DECL node; just return the ..._TYPE node.
3402 FIELD for a struct or union field; make a FIELD_DECL.
3403 INITIALIZED is 1 if the decl has an initializer.
3404 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3405 representing the width of the bit-field.
3407 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3408 It may also be so in the PARM case, for a prototype where the
3409 argument type is specified but not the name.
3411 This function is where the complicated C meanings of `static'
3412 and `extern' are interpreted. */
3415 grokdeclarator (tree declarator, tree declspecs,
3416 enum decl_context decl_context, int initialized, tree *width)
3420 tree type = NULL_TREE;
3425 int type_quals = TYPE_UNQUALIFIED;
3427 int explicit_int = 0;
3428 int explicit_char = 0;
3429 int defaulted_int = 0;
3430 tree typedef_decl = 0;
3431 const char *name, *orig_name;
3432 tree typedef_type = 0;
3433 int funcdef_flag = 0;
3434 enum tree_code innermost_code = ERROR_MARK;
3435 int size_varies = 0;
3436 tree decl_attr = NULL_TREE;
3437 tree array_ptr_quals = NULL_TREE;
3438 int array_parm_static = 0;
3439 tree returned_attrs = NULL_TREE;
3440 bool bitfield = width != NULL;
3442 tree arg_info = NULL_TREE;
3444 if (decl_context == FUNCDEF)
3445 funcdef_flag = 1, decl_context = NORMAL;
3447 /* Look inside a declarator for the name being declared
3448 and get it as a string, for an error message. */
3450 tree decl = declarator;
3454 switch (TREE_CODE (decl))
3459 innermost_code = TREE_CODE (decl);
3460 decl = TREE_OPERAND (decl, 0);
3464 decl = TREE_VALUE (decl);
3467 case IDENTIFIER_NODE:
3468 name = IDENTIFIER_POINTER (decl);
3480 /* A function definition's declarator must have the form of
3481 a function declarator. */
3483 if (funcdef_flag && innermost_code != CALL_EXPR)
3486 /* If this looks like a function definition, make it one,
3487 even if it occurs where parms are expected.
3488 Then store_parm_decls will reject it and not use it as a parm. */
3489 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3490 decl_context = PARM;
3492 /* Look through the decl specs and record which ones appear.
3493 Some typespecs are defined as built-in typenames.
3494 Others, the ones that are modifiers of other types,
3495 are represented by bits in SPECBITS: set the bits for
3496 the modifiers that appear. Storage class keywords are also in SPECBITS.
3498 If there is a typedef name or a type, store the type in TYPE.
3499 This includes builtin typedefs such as `int'.
3501 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3502 and did not come from a user typedef.
3504 Set LONGLONG if `long' is mentioned twice. */
3506 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3508 tree id = TREE_VALUE (spec);
3510 /* If the entire declaration is itself tagged as deprecated then
3511 suppress reports of deprecated items. */
3512 if (id && TREE_DEPRECATED (id))
3514 if (deprecated_state != DEPRECATED_SUPPRESS)
3515 warn_deprecated_use (id);
3518 if (id == ridpointers[(int) RID_INT])
3520 if (id == ridpointers[(int) RID_CHAR])
3523 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3525 enum rid i = C_RID_CODE (id);
3526 if ((int) i <= (int) RID_LAST_MODIFIER)
3528 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3531 error ("`long long long' is too long for GCC");
3534 if (pedantic && !flag_isoc99 && ! in_system_header
3536 pedwarn ("ISO C90 does not support `long long'");
3540 else if (specbits & (1 << (int) i))
3542 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3544 if (pedantic && !flag_isoc99)
3545 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3548 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3551 /* Diagnose "__thread extern". Recall that this list
3552 is in the reverse order seen in the text. */
3554 && (specbits & (1 << (int) RID_EXTERN
3555 | 1 << (int) RID_STATIC)))
3557 if (specbits & 1 << (int) RID_EXTERN)
3558 error ("`__thread' before `extern'");
3560 error ("`__thread' before `static'");
3563 specbits |= 1 << (int) i;
3568 error ("two or more data types in declaration of `%s'", name);
3569 /* Actual typedefs come to us as TYPE_DECL nodes. */
3570 else if (TREE_CODE (id) == TYPE_DECL)
3572 if (TREE_TYPE (id) == error_mark_node)
3573 ; /* Allow the type to default to int to avoid cascading errors. */
3576 type = TREE_TYPE (id);
3577 decl_attr = DECL_ATTRIBUTES (id);
3581 /* Built-in types come as identifiers. */
3582 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3584 tree t = lookup_name (id);
3585 if (!t || TREE_CODE (t) != TYPE_DECL)
3586 error ("`%s' fails to be a typedef or built in type",
3587 IDENTIFIER_POINTER (id));
3588 else if (TREE_TYPE (t) == error_mark_node)
3592 type = TREE_TYPE (t);
3596 else if (TREE_CODE (id) != ERROR_MARK)
3603 typedef_type = type;
3605 size_varies = C_TYPE_VARIABLE_SIZE (type);
3607 /* No type at all: default to `int', and set DEFAULTED_INT
3608 because it was not a user-defined typedef. */
3612 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3613 | (1 << (int) RID_SIGNED)
3614 | (1 << (int) RID_UNSIGNED)
3615 | (1 << (int) RID_COMPLEX))))
3616 /* Don't warn about typedef foo = bar. */
3617 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3618 && ! in_system_header)
3620 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3621 and this is a function, or if -Wimplicit; prefer the former
3622 warning since it is more explicit. */
3623 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3625 warn_about_return_type = 1;
3626 else if (warn_implicit_int || flag_isoc99)
3627 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3632 type = integer_type_node;
3635 /* Now process the modifiers that were specified
3636 and check for invalid combinations. */
3638 /* Long double is a special combination. */
3640 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3641 && TYPE_MAIN_VARIANT (type) == double_type_node)
3643 specbits &= ~(1 << (int) RID_LONG);
3644 type = long_double_type_node;
3647 /* Check all other uses of type modifiers. */
3649 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3650 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3654 if ((specbits & 1 << (int) RID_LONG)
3655 && (specbits & 1 << (int) RID_SHORT))
3656 error ("both long and short specified for `%s'", name);
3657 else if (((specbits & 1 << (int) RID_LONG)
3658 || (specbits & 1 << (int) RID_SHORT))
3660 error ("long or short specified with char for `%s'", name);
3661 else if (((specbits & 1 << (int) RID_LONG)
3662 || (specbits & 1 << (int) RID_SHORT))
3663 && TREE_CODE (type) == REAL_TYPE)
3665 static int already = 0;
3667 error ("long or short specified with floating type for `%s'", name);
3668 if (! already && ! pedantic)
3670 error ("the only valid combination is `long double'");
3674 else if ((specbits & 1 << (int) RID_SIGNED)
3675 && (specbits & 1 << (int) RID_UNSIGNED))
3676 error ("both signed and unsigned specified for `%s'", name);
3677 else if (TREE_CODE (type) != INTEGER_TYPE)
3678 error ("long, short, signed or unsigned invalid for `%s'", name);
3682 if (!explicit_int && !defaulted_int && !explicit_char)
3684 error ("long, short, signed or unsigned used invalidly for `%s'",
3690 /* Discard the type modifiers if they are invalid. */
3693 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3694 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3699 if ((specbits & (1 << (int) RID_COMPLEX))
3700 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3702 error ("complex invalid for `%s'", name);
3703 specbits &= ~(1 << (int) RID_COMPLEX);
3706 /* Decide whether an integer type is signed or not.
3707 Optionally treat bit-fields as signed by default. */
3708 if (specbits & 1 << (int) RID_UNSIGNED
3709 || (bitfield && ! flag_signed_bitfields
3710 && (explicit_int || defaulted_int || explicit_char
3711 /* A typedef for plain `int' without `signed'
3712 can be controlled just like plain `int'. */
3713 || ! (typedef_decl != 0
3714 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3715 && TREE_CODE (type) != ENUMERAL_TYPE
3716 && !(specbits & 1 << (int) RID_SIGNED)))
3719 type = long_long_unsigned_type_node;
3720 else if (specbits & 1 << (int) RID_LONG)
3721 type = long_unsigned_type_node;
3722 else if (specbits & 1 << (int) RID_SHORT)
3723 type = short_unsigned_type_node;
3724 else if (type == char_type_node)
3725 type = unsigned_char_type_node;
3726 else if (typedef_decl)
3727 type = c_common_unsigned_type (type);
3729 type = unsigned_type_node;
3731 else if ((specbits & 1 << (int) RID_SIGNED)
3732 && type == char_type_node)
3733 type = signed_char_type_node;
3735 type = long_long_integer_type_node;
3736 else if (specbits & 1 << (int) RID_LONG)
3737 type = long_integer_type_node;
3738 else if (specbits & 1 << (int) RID_SHORT)
3739 type = short_integer_type_node;
3741 if (specbits & 1 << (int) RID_COMPLEX)
3743 if (pedantic && !flag_isoc99)
3744 pedwarn ("ISO C90 does not support complex types");
3745 /* If we just have "complex", it is equivalent to
3746 "complex double", but if any modifiers at all are specified it is
3747 the complex form of TYPE. E.g, "complex short" is
3748 "complex short int". */
3750 if (defaulted_int && ! longlong
3751 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3752 | (1 << (int) RID_SIGNED)
3753 | (1 << (int) RID_UNSIGNED))))
3756 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3757 type = complex_double_type_node;
3759 else if (type == integer_type_node)
3762 pedwarn ("ISO C does not support complex integer types");
3763 type = complex_integer_type_node;
3765 else if (type == float_type_node)
3766 type = complex_float_type_node;
3767 else if (type == double_type_node)
3768 type = complex_double_type_node;
3769 else if (type == long_double_type_node)
3770 type = complex_long_double_type_node;
3774 pedwarn ("ISO C does not support complex integer types");
3775 type = build_complex_type (type);
3779 /* Check the type and width of a bit-field. */
3781 check_bitfield_type_and_width (&type, width, orig_name);
3783 /* Figure out the type qualifiers for the declaration. There are
3784 two ways a declaration can become qualified. One is something
3785 like `const int i' where the `const' is explicit. Another is
3786 something like `typedef const int CI; CI i' where the type of the
3787 declaration contains the `const'. A third possibility is that
3788 there is a type qualifier on the element type of a typedefed
3789 array type, in which case we should extract that qualifier so
3790 that c_apply_type_quals_to_decls receives the full list of
3791 qualifiers to work with (C90 is not entirely clear about whether
3792 duplicate qualifiers should be diagnosed in this case, but it
3793 seems most appropriate to do so). */
3794 element_type = strip_array_types (type);
3795 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3797 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3799 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3800 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3801 if (pedantic && !flag_isoc99)
3804 pedwarn ("duplicate `const'");
3806 pedwarn ("duplicate `restrict'");
3808 pedwarn ("duplicate `volatile'");
3810 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3811 type = TYPE_MAIN_VARIANT (type);
3812 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3813 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3814 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3816 /* Warn if two storage classes are given. Default to `auto'. */
3821 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3822 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3823 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3824 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3825 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3827 /* "static __thread" and "extern __thread" are allowed. */
3828 if ((specbits & (1 << (int) RID_THREAD
3829 | 1 << (int) RID_STATIC
3830 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3833 /* Warn about storage classes that are invalid for certain
3834 kinds of declarations (parameters, typenames, etc.). */
3837 error ("multiple storage classes in declaration of `%s'", name);
3838 else if (funcdef_flag
3840 & ((1 << (int) RID_REGISTER)
3841 | (1 << (int) RID_AUTO)
3842 | (1 << (int) RID_TYPEDEF)
3843 | (1 << (int) RID_THREAD))))
3845 if (specbits & 1 << (int) RID_AUTO
3846 && (pedantic || current_scope == file_scope))
3847 pedwarn ("function definition declared `auto'");
3848 if (specbits & 1 << (int) RID_REGISTER)
3849 error ("function definition declared `register'");
3850 if (specbits & 1 << (int) RID_TYPEDEF)
3851 error ("function definition declared `typedef'");
3852 if (specbits & 1 << (int) RID_THREAD)
3853 error ("function definition declared `__thread'");
3854 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3855 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3857 else if (decl_context != NORMAL && nclasses > 0)
3859 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3863 switch (decl_context)
3866 error ("storage class specified for structure field `%s'",
3870 error ("storage class specified for parameter `%s'", name);
3873 error ("storage class specified for typename");
3876 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3877 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3878 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3881 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3883 /* `extern' with initialization is invalid if not at file scope. */
3884 if (current_scope == file_scope)
3885 warning ("`%s' initialized and declared `extern'", name);
3887 error ("`%s' has both `extern' and initializer", name);
3889 else if (current_scope == file_scope)
3891 if (specbits & 1 << (int) RID_AUTO)
3892 error ("file-scope declaration of `%s' specifies `auto'", name);
3896 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3897 error ("nested function `%s' declared `extern'", name);
3898 else if ((specbits & (1 << (int) RID_THREAD
3899 | 1 << (int) RID_EXTERN
3900 | 1 << (int) RID_STATIC))
3901 == (1 << (int) RID_THREAD))
3903 error ("function-scope `%s' implicitly auto and declared `__thread'",
3905 specbits &= ~(1 << (int) RID_THREAD);
3910 /* Now figure out the structure of the declarator proper.
3911 Descend through it, creating more complex types, until we reach
3912 the declared identifier (or NULL_TREE, in an absolute declarator). */
3914 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3916 if (type == error_mark_node)
3918 declarator = TREE_OPERAND (declarator, 0);
3922 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3923 an INDIRECT_REF (for *...),
3924 a CALL_EXPR (for ...(...)),
3925 a TREE_LIST (for nested attributes),
3926 an identifier (for the name being declared)
3927 or a null pointer (for the place in an absolute declarator
3928 where the name was omitted).
3929 For the last two cases, we have just exited the loop.
3931 At this point, TYPE is the type of elements of an array,
3932 or for a function to return, or for a pointer to point to.
3933 After this sequence of ifs, TYPE is the type of the
3934 array or function or pointer, and DECLARATOR has had its
3935 outermost layer removed. */
3937 if (array_ptr_quals != NULL_TREE || array_parm_static)
3939 /* Only the innermost declarator (making a parameter be of
3940 array type which is converted to pointer type)
3941 may have static or type qualifiers. */
3942 error ("static or type qualifiers in non-parameter array declarator");
3943 array_ptr_quals = NULL_TREE;
3944 array_parm_static = 0;
3947 if (TREE_CODE (declarator) == TREE_LIST)
3949 /* We encode a declarator with embedded attributes using
3951 tree attrs = TREE_PURPOSE (declarator);
3954 declarator = TREE_VALUE (declarator);
3955 inner_decl = declarator;
3956 while (inner_decl != NULL_TREE
3957 && TREE_CODE (inner_decl) == TREE_LIST)
3958 inner_decl = TREE_VALUE (inner_decl);
3959 if (inner_decl == NULL_TREE
3960 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3961 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3962 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3963 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3964 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3965 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3966 returned_attrs = decl_attributes (&type,
3967 chainon (returned_attrs, attrs),
3970 else if (TREE_CODE (declarator) == ARRAY_REF)
3972 tree itype = NULL_TREE;
3973 tree size = TREE_OPERAND (declarator, 1);
3974 /* The index is a signed object `sizetype' bits wide. */
3975 tree index_type = c_common_signed_type (sizetype);
3977 array_ptr_quals = TREE_TYPE (declarator);
3978 array_parm_static = TREE_STATIC (declarator);
3980 declarator = TREE_OPERAND (declarator, 0);
3982 /* Check for some types that there cannot be arrays of. */
3984 if (VOID_TYPE_P (type))
3986 error ("declaration of `%s' as array of voids", name);
3987 type = error_mark_node;
3990 if (TREE_CODE (type) == FUNCTION_TYPE)
3992 error ("declaration of `%s' as array of functions", name);
3993 type = error_mark_node;
3996 if (pedantic && !in_system_header && flexible_array_type_p (type))
3997 pedwarn ("invalid use of structure with flexible array member");
3999 if (size == error_mark_node)
4000 type = error_mark_node;
4002 if (type == error_mark_node)
4005 /* If size was specified, set ITYPE to a range-type for that size.
4006 Otherwise, ITYPE remains null. finish_decl may figure it out
4007 from an initial value. */
4011 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4012 STRIP_TYPE_NOPS (size);
4014 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4016 error ("size of array `%s' has non-integer type", name);
4017 size = integer_one_node;
4020 if (pedantic && integer_zerop (size))
4021 pedwarn ("ISO C forbids zero-size array `%s'", name);
4023 if (TREE_CODE (size) == INTEGER_CST)
4025 constant_expression_warning (size);
4026 if (tree_int_cst_sgn (size) < 0)
4028 error ("size of array `%s' is negative", name);
4029 size = integer_one_node;
4034 /* Make sure the array size remains visibly nonconstant
4035 even if it is (eg) a const variable with known value. */
4038 if (!flag_isoc99 && pedantic)
4040 if (TREE_CONSTANT (size))
4041 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4044 pedwarn ("ISO C90 forbids variable-size array `%s'",
4049 if (integer_zerop (size))
4051 /* A zero-length array cannot be represented with an
4052 unsigned index type, which is what we'll get with
4053 build_index_type. Create an open-ended range instead. */
4054 itype = build_range_type (sizetype, size, NULL_TREE);
4058 /* Compute the maximum valid index, that is, size - 1.
4059 Do the calculation in index_type, so that if it is
4060 a variable the computations will be done in the
4062 itype = fold (build (MINUS_EXPR, index_type,
4063 convert (index_type, size),
4064 convert (index_type, size_one_node)));
4066 /* If that overflowed, the array is too big.
4067 ??? While a size of INT_MAX+1 technically shouldn't
4068 cause an overflow (because we subtract 1), the overflow
4069 is recorded during the conversion to index_type, before
4070 the subtraction. Handling this case seems like an
4071 unnecessary complication. */
4072 if (TREE_OVERFLOW (itype))
4074 error ("size of array `%s' is too large", name);
4075 type = error_mark_node;
4080 itype = variable_size (itype);
4081 itype = build_index_type (itype);
4084 else if (decl_context == FIELD)
4086 if (pedantic && !flag_isoc99 && !in_system_header)
4087 pedwarn ("ISO C90 does not support flexible array members");