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"
56 #include "langhooks-def.h"
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
60 { NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
80 static tree enum_next_value;
82 /* Nonzero means that there was overflow computing enum_next_value. */
84 static int enum_overflow;
86 /* These #defines are for clarity in working with the information block
87 returned by get_parm_info. */
88 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
89 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
90 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
91 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
93 /* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
97 static location_t current_function_prototype_locus;
99 /* The current statement tree. */
101 static GTY(()) struct stmt_tree_s c_stmt_tree;
103 /* The current scope statement stack. */
105 static GTY(()) tree c_scope_stmt_stack;
107 /* State saving variables. */
108 int c_in_iteration_stmt;
111 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
112 included in this invocation. Note that the current translation
113 unit is not included in this list. */
115 static GTY(()) tree all_translation_units;
117 /* A list of decls to be made automatically visible in each file scope. */
118 static GTY(()) tree visible_builtins;
120 /* Set to 0 at beginning of a function definition, set to 1 if
121 a return statement that specifies a return value is seen. */
123 int current_function_returns_value;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement with no argument is seen. */
128 int current_function_returns_null;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a call to a noreturn function is seen. */
133 int current_function_returns_abnormally;
135 /* Set to nonzero by `grokdeclarator' for a function
136 whose return type is defaulted, if warnings for this are desired. */
138 static int warn_about_return_type;
140 /* Nonzero when starting a function declared `extern inline'. */
142 static int current_extern_inline;
144 /* True means global_bindings_p should return false even if the scope stack
145 says we are in file scope. */
146 bool c_override_global_bindings_to_false;
149 /* Each c_binding structure describes one binding of an identifier to
150 a decl. All the decls in a scope - irrespective of namespace - are
151 chained together by the ->prev field, which (as the name implies)
152 runs in reverse order. All the decls in a given namespace bound to
153 a given identifier are chained by the ->shadowed field, which runs
154 from inner to outer scopes. Finally, the ->contour field points
155 back to the relevant scope structure; this is mainly used to make
156 decls in the externals scope invisible (see below).
158 The ->decl field usually points to a DECL node, but there are two
159 exceptions. In the namespace of type tags, the bound entity is a
160 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
161 identifier is encountered, it is bound to error_mark_node to
162 suppress further errors about that identifier in the current
165 struct c_binding GTY((chain_next ("%h.prev")))
167 tree decl; /* the decl bound */
168 tree id; /* the identifier it's bound to */
169 struct c_binding *prev; /* the previous decl in this scope */
170 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
171 struct c_scope *contour; /* the scope in which this decl is bound */
174 #define I_SYMBOL_BINDING(node) \
175 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
176 #define I_SYMBOL_DECL(node) \
177 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
179 #define I_TAG_BINDING(node) \
180 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
181 #define I_TAG_DECL(node) \
182 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
184 #define I_LABEL_BINDING(node) \
185 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
186 #define I_LABEL_DECL(node) \
187 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
189 /* Each C symbol points to three linked lists of c_binding structures.
190 These describe the values of the identifier in the three different
191 namespaces defined by the language. */
193 struct lang_identifier GTY(())
195 struct c_common_identifier common_id;
196 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
197 struct c_binding *tag_binding; /* struct/union/enum tags */
198 struct c_binding *label_binding; /* labels */
201 /* Validate c-lang.c's assumptions. */
202 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
203 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
205 /* The resulting tree type. */
208 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
209 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)")))
211 union tree_node GTY ((tag ("0"),
212 desc ("tree_node_structure (&%h)")))
214 struct lang_identifier GTY ((tag ("1"))) identifier;
217 /* Each c_scope structure describes the complete contents of one
218 scope. Four scopes are distinguished specially: the innermost or
219 current scope, the innermost function scope, the file scope (always
220 the second to outermost) and the outermost or external scope.
222 Most declarations are recorded in the current scope.
224 All normal label declarations are recorded in the innermost
225 function scope, as are bindings of undeclared identifiers to
226 error_mark_node. (GCC permits nested functions as an extension,
227 hence the 'innermost' qualifier.) Explicitly declared labels
228 (using the __label__ extension) appear in the current scope.
230 Being in the file scope (current_scope == file_scope) causes
231 special behavior in several places below. Also, under some
232 conditions the Objective-C front end records declarations in the
233 file scope even though that isn't the current scope.
235 All declarations with external linkage are recorded in the external
236 scope, even if they aren't visible there; this models the fact that
237 such declarations are visible to the entire program, and (with a
238 bit of cleverness, see pushdecl) allows diagnosis of some violations
239 of C99 6.2.2p7 and 6.2.7p2:
241 If, within the same translation unit, the same identifier appears
242 with both internal and external linkage, the behavior is
245 All declarations that refer to the same object or function shall
246 have compatible type; otherwise, the behavior is undefined.
248 Initially only the built-in declarations, which describe compiler
249 intrinsic functions plus a subset of the standard library, are in
252 The order of the blocks list matters, and it is frequently appended
253 to. To avoid having to walk all the way to the end of the list on
254 each insertion, or reverse the list later, we maintain a pointer to
255 the last list entry. (FIXME: It should be feasible to use a reversed
258 The bindings list is strictly in reverse order of declarations;
259 pop_scope relies on this. */
262 struct c_scope GTY((chain_next ("%h.outer")))
264 /* The scope containing this one. */
265 struct c_scope *outer;
267 /* The next outermost function scope. */
268 struct c_scope *outer_function;
270 /* All bindings in this scope. */
271 struct c_binding *bindings;
273 /* For each scope (except the global one), a chain of BLOCK nodes
274 for all the scopes that were entered and exited one level down. */
278 /* The depth of this scope. Used to keep the ->shadowed chain of
279 bindings sorted innermost to outermost. */
280 unsigned int depth : 28;
282 /* True if we are currently filling this scope with parameter
284 BOOL_BITFIELD parm_flag : 1;
286 /* True if we already complained about forward parameter decls
287 in this scope. This prevents double warnings on
288 foo (int a; int b; ...) */
289 BOOL_BITFIELD warned_forward_parm_decls : 1;
291 /* True if this is the outermost block scope of a function body.
292 This scope contains the parameters, the local variables declared
293 in the outermost block, and all the labels (except those in
294 nested functions, or declared at block scope with __label__). */
295 BOOL_BITFIELD function_body : 1;
297 /* True means make a BLOCK for this scope no matter what. */
298 BOOL_BITFIELD keep : 1;
301 /* The scope currently in effect. */
303 static GTY(()) struct c_scope *current_scope;
305 /* The innermost function scope. Ordinary (not explicitly declared)
306 labels, bindings to error_mark_node, and the lazily-created
307 bindings of __func__ and its friends get this scope. */
309 static GTY(()) struct c_scope *current_function_scope;
311 /* The C file scope. This is reset for each input translation unit. */
313 static GTY(()) struct c_scope *file_scope;
315 /* The outermost scope. This is used for all declarations with
316 external linkage, and only these, hence the name. */
318 static GTY(()) struct c_scope *external_scope;
320 /* A chain of c_scope structures awaiting reuse. */
322 static GTY((deletable)) struct c_scope *scope_freelist;
324 /* A chain of c_binding structures awaiting reuse. */
326 static GTY((deletable)) struct c_binding *binding_freelist;
328 /* Append VAR to LIST in scope SCOPE. */
329 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
330 struct c_scope *s_ = (scope); \
332 if (s_->list##_last) \
333 TREE_CHAIN (s_->list##_last) = d_; \
336 s_->list##_last = d_; \
339 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
340 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
341 struct c_scope *t_ = (tscope); \
342 struct c_scope *f_ = (fscope); \
344 TREE_CHAIN (t_->to##_last) = f_->from; \
347 t_->to##_last = f_->from##_last; \
350 /* True means unconditionally make a BLOCK for the next scope pushed. */
352 static bool keep_next_level_flag;
354 /* True means the next call to push_scope will be the outermost scope
355 of a function body, so do not push a new scope, merely cease
356 expecting parameter decls. */
358 static bool next_is_function_body;
360 /* Functions called automatically at the beginning and end of execution. */
362 tree static_ctors, static_dtors;
364 /* Forward declarations. */
365 static tree lookup_name_in_scope (tree, struct c_scope *);
366 static tree c_make_fname_decl (tree, int);
367 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
368 static tree grokparms (tree, int);
369 static void layout_array_type (tree);
371 /* States indicating how grokdeclarator() should handle declspecs marked
372 with __attribute__((deprecated)). An object declared as
373 __attribute__((deprecated)) suppresses warnings of uses of other
376 enum deprecated_states {
381 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
384 c_print_identifier (FILE *file, tree node, int indent)
386 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
387 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
388 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
389 if (C_IS_RESERVED_WORD (node))
391 tree rid = ridpointers[C_RID_CODE (node)];
392 indent_to (file, indent + 4);
393 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
394 (void *) rid, IDENTIFIER_POINTER (rid));
398 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
399 which may be any of several kinds of DECL or TYPE or error_mark_node,
400 in the scope SCOPE. */
402 bind (tree name, tree decl, struct c_scope *scope)
404 struct c_binding *b, **here;
406 if (binding_freelist)
408 b = binding_freelist;
409 binding_freelist = b->prev;
412 b = ggc_alloc (sizeof (struct c_binding));
419 b->prev = scope->bindings;
425 switch (TREE_CODE (decl))
427 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
430 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
436 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
442 /* Locate the appropriate place in the chain of shadowed decls
443 to insert this binding. Normally, scope == current_scope and
444 this does nothing. */
445 while (*here && (*here)->contour->depth > scope->depth)
446 here = &(*here)->shadowed;
452 /* Clear the binding structure B, stick it on the binding_freelist,
453 and return the former value of b->prev. This is used by pop_scope
454 and get_parm_info to iterate destructively over all the bindings
455 from a given scope. */
456 static struct c_binding *
457 free_binding_and_advance (struct c_binding *b)
459 struct c_binding *prev = b->prev;
465 b->prev = binding_freelist;
466 binding_freelist = b;
472 /* Hook called at end of compilation to assume 1 elt
473 for a file-scope tentative array defn that wasn't complete before. */
476 c_finish_incomplete_decl (tree decl)
478 if (TREE_CODE (decl) == VAR_DECL)
480 tree type = TREE_TYPE (decl);
481 if (type != error_mark_node
482 && TREE_CODE (type) == ARRAY_TYPE
483 && ! DECL_EXTERNAL (decl)
484 && TYPE_DOMAIN (type) == 0)
486 warning ("%Jarray '%D' assumed to have one element", decl, decl);
488 complete_array_type (type, NULL_TREE, 1);
490 layout_decl (decl, 0);
495 /* The Objective-C front-end often needs to determine the current scope. */
498 get_current_scope (void)
500 return current_scope;
503 /* The following function is used only by Objective-C. It needs to live here
504 because it accesses the innards of c_scope. */
507 objc_mark_locals_volatile (void *enclosing_blk)
509 struct c_scope *scope;
512 for (scope = current_scope;
513 scope && scope != enclosing_blk;
514 scope = scope->outer)
516 for (b = scope->bindings; b; b = b->prev)
518 if (TREE_CODE (b->decl) == VAR_DECL
519 || TREE_CODE (b->decl) == PARM_DECL)
521 C_DECL_REGISTER (b->decl) = 0;
522 DECL_REGISTER (b->decl) = 0;
523 TREE_THIS_VOLATILE (b->decl) = 1;
527 /* Do not climb up past the current function. */
528 if (scope->function_body)
533 /* Nonzero if we are currently in file scope. */
536 global_bindings_p (void)
538 return current_scope == file_scope && !c_override_global_bindings_to_false;
542 keep_next_level (void)
544 keep_next_level_flag = true;
547 /* Identify this scope as currently being filled with parameters. */
550 declare_parm_level (void)
552 current_scope->parm_flag = true;
558 if (next_is_function_body)
560 /* This is the transition from the parameters to the top level
561 of the function body. These are the same scope
562 (C99 6.2.1p4,6) so we do not push another scope structure.
563 next_is_function_body is set only by store_parm_decls, which
564 in turn is called when and only when we are about to
565 encounter the opening curly brace for the function body.
567 The outermost block of a function always gets a BLOCK node,
568 because the debugging output routines expect that each
569 function has at least one BLOCK. */
570 current_scope->parm_flag = false;
571 current_scope->function_body = true;
572 current_scope->keep = true;
573 current_scope->outer_function = current_function_scope;
574 current_function_scope = current_scope;
576 keep_next_level_flag = false;
577 next_is_function_body = false;
581 struct c_scope *scope;
584 scope = scope_freelist;
585 scope_freelist = scope->outer;
588 scope = ggc_alloc_cleared (sizeof (struct c_scope));
590 scope->keep = keep_next_level_flag;
591 scope->outer = current_scope;
592 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
594 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
596 if (current_scope && scope->depth == 0)
599 sorry ("GCC supports only %u nested scopes\n", scope->depth);
602 current_scope = scope;
603 keep_next_level_flag = false;
607 /* Exit a scope. Restore the state of the identifier-decl mappings
608 that were in effect when this scope was entered. Return a BLOCK
609 node containing all the DECLs in this scope that are of interest
610 to debug info generation. */
615 struct c_scope *scope = current_scope;
616 tree block, context, p;
619 bool functionbody = scope->function_body;
620 bool keep = functionbody || scope->keep || scope->bindings;
622 /* If appropriate, create a BLOCK to record the decls for the life
627 block = make_node (BLOCK);
628 BLOCK_SUBBLOCKS (block) = scope->blocks;
629 TREE_USED (block) = 1;
631 /* In each subblock, record that this is its superior. */
632 for (p = scope->blocks; p; p = TREE_CHAIN (p))
633 BLOCK_SUPERCONTEXT (p) = block;
635 BLOCK_VARS (block) = 0;
638 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
639 scope must be set so that they point to the appropriate
640 construct, i.e. either to the current FUNCTION_DECL node, or
641 else to the BLOCK node we just constructed.
643 Note that for tagged types whose scope is just the formal
644 parameter list for some function type specification, we can't
645 properly set their TYPE_CONTEXTs here, because we don't have a
646 pointer to the appropriate FUNCTION_TYPE node readily available
647 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
648 type nodes get set in `grokdeclarator' as soon as we have created
649 the FUNCTION_TYPE node which will represent the "scope" for these
650 "parameter list local" tagged types. */
651 if (scope->function_body)
652 context = current_function_decl;
653 else if (scope == file_scope)
655 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
656 TREE_CHAIN (file_decl) = all_translation_units;
657 all_translation_units = file_decl;
663 /* Clear all bindings in this scope. */
664 for (b = scope->bindings; b; b = free_binding_and_advance (b))
667 switch (TREE_CODE (p))
670 /* Warnings for unused labels, errors for undefined labels. */
671 if (TREE_USED (p) && !DECL_INITIAL (p))
673 error ("%Jlabel `%D' used but not defined", p, p);
674 DECL_INITIAL (p) = error_mark_node;
676 else if (!TREE_USED (p) && warn_unused_label)
678 if (DECL_INITIAL (p))
679 warning ("%Jlabel `%D' defined but not used", p, p);
681 warning ("%Jlabel `%D' declared but not defined", p, p);
683 /* Labels go in BLOCK_VARS. */
684 TREE_CHAIN (p) = BLOCK_VARS (block);
685 BLOCK_VARS (block) = p;
687 #ifdef ENABLE_CHECKING
688 if (I_LABEL_BINDING (b->id) != b) abort ();
690 I_LABEL_BINDING (b->id) = b->shadowed;
696 TYPE_CONTEXT (p) = context;
698 /* Types may not have tag-names, in which case the type
699 appears in the bindings list with b->id NULL. */
702 #ifdef ENABLE_CHECKING
703 if (I_TAG_BINDING (b->id) != b) abort ();
705 I_TAG_BINDING (b->id) = b->shadowed;
710 /* Propagate TREE_ADDRESSABLE from nested functions to their
711 containing functions. */
712 if (! TREE_ASM_WRITTEN (p)
713 && DECL_INITIAL (p) != 0
714 && TREE_ADDRESSABLE (p)
715 && DECL_ABSTRACT_ORIGIN (p) != 0
716 && DECL_ABSTRACT_ORIGIN (p) != p)
717 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
721 /* Warnings for unused variables. Keep this in sync with
722 stmt.c:warn_about_unused_variables, which we cannot use
723 since it expects a different data structure. */
724 if (warn_unused_variable
726 && !DECL_IN_SYSTEM_HEADER (p)
728 && !DECL_ARTIFICIAL (p)
729 && (scope != file_scope
730 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
731 && !TREE_THIS_VOLATILE (p)))
732 && scope != external_scope)
733 warning ("%Junused variable `%D'", p, p);
739 /* All of these go in BLOCK_VARS, but only if this is the
740 binding in the home scope. */
741 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
743 TREE_CHAIN (p) = BLOCK_VARS (block);
744 BLOCK_VARS (block) = p;
746 /* If this is the file scope, must set DECL_CONTEXT on these. */
747 if (!C_DECL_IN_EXTERNAL_SCOPE (p) && scope == file_scope)
748 DECL_CONTEXT (p) = context;
751 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
752 already been put there by store_parm_decls. Unused-
753 parameter warnings are handled by function.c.
754 error_mark_node obviously does not go in BLOCK_VARS and
755 does not get unused-variable warnings. */
758 /* It is possible for a decl not to have a name. We get
759 here with b->id NULL in this case. */
762 #ifdef ENABLE_CHECKING
763 if (I_SYMBOL_BINDING (b->id) != b) abort ();
765 I_SYMBOL_BINDING (b->id) = b->shadowed;
775 /* Dispose of the block that we just made inside some higher level. */
776 if ((scope->function_body || scope == file_scope) && context)
778 DECL_INITIAL (context) = block;
779 BLOCK_SUPERCONTEXT (block) = context;
781 else if (scope->outer)
784 SCOPE_LIST_APPEND (scope->outer, blocks, block);
785 /* If we did not make a block for the scope just exited, any
786 blocks made for inner scopes must be carried forward so they
787 will later become subblocks of something else. */
788 else if (scope->blocks)
789 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
792 /* Pop the current scope, and free the structure for reuse. */
793 current_scope = scope->outer;
794 if (scope->function_body)
795 current_function_scope = scope->outer_function;
797 memset (scope, 0, sizeof (struct c_scope));
798 scope->outer = scope_freelist;
799 scope_freelist = scope;
805 push_file_scope (void)
810 file_scope = current_scope;
812 start_fname_decls ();
814 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
815 bind (DECL_NAME (decl), decl, file_scope);
819 pop_file_scope (void)
821 /* In case there were missing closebraces, get us back to the global
823 while (current_scope != file_scope)
826 /* __FUNCTION__ is defined at file scope (""). This
827 call may not be necessary as my tests indicate it
828 still works without it. */
829 finish_fname_decls ();
831 /* Kludge: don't actually pop the file scope if generating a
832 precompiled header, so that macros and local symbols are still
833 visible to the PCH generator. */
837 /* And pop off the file scope. */
841 cpp_undef_all (parse_in);
844 /* Insert BLOCK at the end of the list of subblocks of the current
845 scope. This is used when a BIND_EXPR is expanded, to handle the
846 BLOCK node inside the BIND_EXPR. */
849 insert_block (tree block)
851 TREE_USED (block) = 1;
852 SCOPE_LIST_APPEND (current_scope, blocks, block);
855 /* Push a definition or a declaration of struct, union or enum tag "name".
856 "type" should be the type node.
857 We assume that the tag "name" is not already defined.
859 Note that the definition may really be just a forward reference.
860 In that case, the TYPE_SIZE will be zero. */
863 pushtag (tree name, tree type)
865 /* Record the identifier as the type's name if it has none. */
866 if (name && !TYPE_NAME (type))
867 TYPE_NAME (type) = name;
868 bind (name, type, current_scope);
870 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
871 tagged type we just added to the current scope. This fake
872 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
873 to output a representation of a tagged type, and it also gives
874 us a convenient place to record the "scope start" address for the
877 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
879 /* An approximation for now, so we can tell this is a function-scope tag.
880 This will be updated in pop_scope. */
881 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
884 /* Subroutine of compare_decls. Allow harmless mismatches in return
885 and argument types provided that the type modes match. This function
886 return a unified type given a suitable match, and 0 otherwise. */
889 match_builtin_function_types (tree newtype, tree oldtype)
891 tree newrettype, oldrettype;
892 tree newargs, oldargs;
893 tree trytype, tryargs;
895 /* Accept the return type of the new declaration if same modes. */
896 oldrettype = TREE_TYPE (oldtype);
897 newrettype = TREE_TYPE (newtype);
899 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
902 oldargs = TYPE_ARG_TYPES (oldtype);
903 newargs = TYPE_ARG_TYPES (newtype);
906 while (oldargs || newargs)
910 || ! TREE_VALUE (oldargs)
911 || ! TREE_VALUE (newargs)
912 || TYPE_MODE (TREE_VALUE (oldargs))
913 != TYPE_MODE (TREE_VALUE (newargs)))
916 oldargs = TREE_CHAIN (oldargs);
917 newargs = TREE_CHAIN (newargs);
920 trytype = build_function_type (newrettype, tryargs);
921 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
924 /* Subroutine of diagnose_mismatched_decls. Check for function type
925 mismatch involving an empty arglist vs a nonempty one and give clearer
928 diagnose_arglist_conflict (tree newdecl, tree olddecl,
929 tree newtype, tree oldtype)
933 if (TREE_CODE (olddecl) != FUNCTION_DECL
934 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
935 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
937 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
940 t = TYPE_ARG_TYPES (oldtype);
942 t = TYPE_ARG_TYPES (newtype);
943 for (; t; t = TREE_CHAIN (t))
945 tree type = TREE_VALUE (t);
947 if (TREE_CHAIN (t) == 0
948 && TYPE_MAIN_VARIANT (type) != void_type_node)
950 inform ("a parameter list with an ellipsis can't match "
951 "an empty parameter name list declaration");
955 if (c_type_promotes_to (type) != type)
957 inform ("an argument type that has a default promotion can't match "
958 "an empty parameter name list declaration");
964 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
965 old-style function definition, NEWDECL is a prototype declaration.
966 Diagnose inconsistencies in the argument list. Returns TRUE if
967 the prototype is compatible, FALSE if not. */
969 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
971 tree newargs, oldargs;
974 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
975 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
977 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
978 newargs = TYPE_ARG_TYPES (newtype);
983 tree oldargtype = TREE_VALUE (oldargs);
984 tree newargtype = TREE_VALUE (newargs);
986 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
989 /* Reaching the end of just one list means the two decls don't
990 agree on the number of arguments. */
991 if (END_OF_ARGLIST (oldargtype))
993 error ("%Jprototype for '%D' declares more arguments "
994 "than previous old-style definition", newdecl, newdecl);
997 else if (END_OF_ARGLIST (newargtype))
999 error ("%Jprototype for '%D' declares fewer arguments "
1000 "than previous old-style definition", newdecl, newdecl);
1004 /* Type for passing arg must be consistent with that declared
1006 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
1008 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1009 newdecl, newdecl, i);
1013 oldargs = TREE_CHAIN (oldargs);
1014 newargs = TREE_CHAIN (newargs);
1018 /* If we get here, no errors were found, but do issue a warning
1019 for this poor-style construct. */
1020 warning ("%Jprototype for '%D' follows non-prototype definition",
1023 #undef END_OF_ARGLIST
1026 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1027 first in a pair of mismatched declarations, using the diagnostic
1030 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1032 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1034 else if (DECL_INITIAL (decl))
1035 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1036 else if (C_DECL_IMPLICIT (decl))
1037 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1039 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1042 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1043 Returns true if the caller should proceed to merge the two, false
1044 if OLDDECL should simply be discarded. As a side effect, issues
1045 all necessary diagnostics for invalid or poor-style combinations.
1046 If it returns true, writes the types of NEWDECL and OLDDECL to
1047 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1048 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1051 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1052 tree *newtypep, tree *oldtypep)
1054 tree newtype, oldtype;
1055 bool pedwarned = false;
1056 bool warned = false;
1058 /* If we have error_mark_node for either decl or type, just discard
1059 the previous decl - we're in an error cascade already. */
1060 if (olddecl == error_mark_node || newdecl == error_mark_node)
1062 *oldtypep = oldtype = TREE_TYPE (olddecl);
1063 *newtypep = newtype = TREE_TYPE (newdecl);
1064 if (oldtype == error_mark_node || newtype == error_mark_node)
1067 /* Two different categories of symbol altogether. This is an error
1068 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1069 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1071 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1072 && DECL_BUILT_IN (olddecl)
1073 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1075 error ("%J'%D' redeclared as different kind of symbol",
1077 locate_old_decl (olddecl, error);
1079 else if (TREE_PUBLIC (newdecl))
1080 warning ("%Jbuilt-in function '%D' declared as non-function",
1082 else if (warn_shadow)
1083 warning ("%Jdeclaration of '%D' shadows a built-in function",
1088 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1090 if (TREE_CODE (olddecl) == FUNCTION_DECL
1091 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1093 /* Accept harmless mismatch in function types.
1094 This is for the ffs and fprintf builtins. */
1095 tree trytype = match_builtin_function_types (newtype, oldtype);
1097 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1098 *oldtypep = oldtype = trytype;
1101 /* If types don't match for a built-in, throw away the
1102 built-in. No point in calling locate_old_decl here, it
1103 won't print anything. */
1104 warning ("%Jconflicting types for built-in function '%D'",
1109 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1110 && DECL_SOURCE_LINE (olddecl) == 0)
1112 /* A conflicting function declaration for a predeclared
1113 function that isn't actually built in. Objective C uses
1114 these. The new declaration silently overrides everything
1115 but the volatility (i.e. noreturn) indication. See also
1116 below. FIXME: Make Objective C use normal builtins. */
1117 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1120 /* Permit void foo (...) to match int foo (...) if the latter is
1121 the definition and implicit int was used. See
1122 c-torture/compile/920625-2.c. */
1123 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1124 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1125 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1126 && C_FUNCTION_IMPLICIT_INT (newdecl))
1128 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1129 /* Make sure we keep void as the return type. */
1130 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1131 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1136 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1137 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1138 locate_old_decl (olddecl, error);
1143 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1144 but silently ignore the redeclaration if either is in a system
1145 header. (Conflicting redeclarations were handled above.) */
1146 if (TREE_CODE (newdecl) == TYPE_DECL)
1148 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1149 return true; /* Allow OLDDECL to continue in use. */
1151 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1152 locate_old_decl (olddecl, error);
1156 /* Function declarations can either be 'static' or 'extern' (no
1157 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1158 can never conflict with each other on account of linkage (6.2.2p4).
1159 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1160 two definitions if one is 'extern inline' and one is not. The non-
1161 extern-inline definition supersedes the extern-inline definition. */
1162 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1164 /* If you declare a built-in function name as static, or
1165 define the built-in with an old-style definition (so we
1166 can't validate the argument list) the built-in definition is
1167 overridden, but optionally warn this was a bad choice of name. */
1168 if (DECL_BUILT_IN (olddecl)
1169 && !C_DECL_DECLARED_BUILTIN (olddecl)
1170 && (!TREE_PUBLIC (newdecl)
1171 || (DECL_INITIAL (newdecl)
1172 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1175 warning ("%Jdeclaration of '%D' shadows a built-in function",
1177 /* Discard the old built-in function. */
1181 if (DECL_INITIAL (newdecl))
1183 if (DECL_INITIAL (olddecl)
1184 && !(DECL_DECLARED_INLINE_P (olddecl)
1185 && DECL_EXTERNAL (olddecl)
1186 && !(DECL_DECLARED_INLINE_P (newdecl)
1187 && DECL_EXTERNAL (newdecl))))
1189 error ("%Jredefinition of '%D'", newdecl, newdecl);
1190 locate_old_decl (olddecl, error);
1194 /* If we have a prototype after an old-style function definition,
1195 the argument types must be checked specially. */
1196 else if (DECL_INITIAL (olddecl)
1197 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1198 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1199 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1201 locate_old_decl (olddecl, error);
1204 /* Mismatched non-static and static is considered poor style.
1205 We only diagnose static then non-static if -Wtraditional,
1206 because it is the most convenient way to get some effects
1207 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1208 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1209 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1211 /* A static function declaration for a predeclared function
1212 that isn't actually built in, silently overrides the
1213 default. Objective C uses these. See also above.
1214 FIXME: Make Objective C use normal builtins. */
1215 if (TREE_CODE (olddecl) == FUNCTION_DECL
1216 && DECL_SOURCE_LINE (olddecl) == 0)
1220 warning ("%Jstatic declaration of '%D' follows "
1221 "non-static declaration", newdecl, newdecl);
1225 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1226 && warn_traditional)
1228 warning ("%Jnon-static declaration of '%D' follows "
1229 "static declaration", newdecl, newdecl);
1233 else if (TREE_CODE (newdecl) == VAR_DECL)
1235 /* Only variables can be thread-local, and all declarations must
1236 agree on this property. */
1237 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1239 if (DECL_THREAD_LOCAL (newdecl))
1240 error ("%Jthread-local declaration of '%D' follows "
1241 "non-thread-local declaration", newdecl, newdecl);
1243 error ("%Jnon-thread-local declaration of '%D' follows "
1244 "thread-local declaration", newdecl, newdecl);
1246 locate_old_decl (olddecl, error);
1250 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1251 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1253 error ("%Jredefinition of '%D'", newdecl, newdecl);
1254 locate_old_decl (olddecl, error);
1258 /* Objects declared at file scope: if at least one is 'extern',
1259 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1260 if (DECL_FILE_SCOPE_P (newdecl))
1262 if (!DECL_EXTERNAL (newdecl)
1263 && !DECL_EXTERNAL (olddecl)
1264 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1266 if (TREE_PUBLIC (newdecl))
1267 error ("%Jnon-static declaration of '%D' follows "
1268 "static declaration", newdecl, newdecl);
1270 error ("%Jstatic declaration of '%D' follows "
1271 "non-static declaration", newdecl, newdecl);
1273 locate_old_decl (olddecl, error);
1277 /* Two objects with the same name declared at the same block
1278 scope must both be external references (6.7p3). */
1279 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1280 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1282 if (DECL_EXTERNAL (newdecl))
1283 error ("%Jextern declaration of '%D' follows "
1284 "declaration with no linkage", newdecl, newdecl);
1285 else if (DECL_EXTERNAL (olddecl))
1286 error ("%Jdeclaration of '%D' with no linkage follows "
1287 "extern declaration", newdecl, newdecl);
1289 error ("%Jredeclaration of '%D' with no linkage",
1292 locate_old_decl (olddecl, error);
1298 /* All decls must agree on a non-default visibility. */
1299 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1300 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1301 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1303 warning ("%Jredeclaration of '%D' with different visibility "
1304 "(old visibility preserved)", newdecl, newdecl);
1308 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1310 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1311 if (DECL_DECLARED_INLINE_P (newdecl)
1312 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1314 warning ("%Jinline declaration of '%D' follows "
1315 "declaration with attribute noinline", newdecl, newdecl);
1318 else if (DECL_DECLARED_INLINE_P (olddecl)
1319 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1321 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1322 "inline declaration ", newdecl, newdecl);
1326 /* Inline declaration after use or definition.
1327 ??? Should we still warn about this now we have unit-at-a-time
1328 mode and can get it right? */
1329 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1331 if (TREE_USED (olddecl))
1333 warning ("%J'%D' declared inline after being called",
1337 else if (DECL_INITIAL (olddecl))
1339 warning ("%J'%D' declared inline after its definition",
1345 else /* PARM_DECL, VAR_DECL */
1347 /* Redeclaration of a parameter is a constraint violation (this is
1348 not explicitly stated, but follows from C99 6.7p3 [no more than
1349 one declaration of the same identifier with no linkage in the
1350 same scope, except type tags] and 6.2.2p6 [parameters have no
1351 linkage]). We must check for a forward parameter declaration,
1352 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1353 an extension, the mandatory diagnostic for which is handled by
1354 mark_forward_parm_decls. */
1356 if (TREE_CODE (newdecl) == PARM_DECL
1357 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1359 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1360 locate_old_decl (olddecl, error);
1364 /* These bits are only type qualifiers when applied to objects. */
1365 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1367 if (TREE_THIS_VOLATILE (newdecl))
1368 pedwarn ("%Jvolatile declaration of '%D' follows "
1369 "non-volatile declaration", newdecl, newdecl);
1371 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1372 "volatile declaration", newdecl, newdecl);
1375 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1377 if (TREE_READONLY (newdecl))
1378 pedwarn ("%Jconst declaration of '%D' follows "
1379 "non-const declaration", newdecl, newdecl);
1381 pedwarn ("%Jnon-const declaration of '%D' follows "
1382 "const declaration", newdecl, newdecl);
1387 /* Optional warning for completely redundant decls. */
1388 if (!warned && !pedwarned
1389 && warn_redundant_decls
1390 /* Don't warn about a function declaration followed by a
1392 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1393 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1394 /* Don't warn about an extern followed by a definition. */
1395 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1396 /* Don't warn about forward parameter decls. */
1397 && !(TREE_CODE (newdecl) == PARM_DECL
1398 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1400 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1404 /* Report location of previous decl/defn in a consistent manner. */
1405 if (warned || pedwarned)
1406 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1411 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1412 consistent with OLDDECL, but carries new information. Merge the
1413 new information into OLDDECL. This function issues no
1417 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1419 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1420 && DECL_INITIAL (newdecl) != 0);
1422 /* For real parm decl following a forward decl, rechain the old decl
1423 in its new location and clear TREE_ASM_WRITTEN (it's not a
1424 forward decl anymore). */
1425 if (TREE_CODE (newdecl) == PARM_DECL
1426 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1428 struct c_binding *b, **here;
1430 for (here = ¤t_scope->bindings; *here; here = &(*here)->prev)
1431 if ((*here)->decl == olddecl)
1438 b->prev = current_scope->bindings;
1439 current_scope->bindings = b;
1441 TREE_ASM_WRITTEN (olddecl) = 0;
1444 DECL_ATTRIBUTES (newdecl)
1445 = targetm.merge_decl_attributes (olddecl, newdecl);
1447 /* Merge the data types specified in the two decls. */
1449 = TREE_TYPE (olddecl)
1450 = common_type (newtype, oldtype);
1452 /* Lay the type out, unless already done. */
1453 if (oldtype != TREE_TYPE (newdecl))
1455 if (TREE_TYPE (newdecl) != error_mark_node)
1456 layout_type (TREE_TYPE (newdecl));
1457 if (TREE_CODE (newdecl) != FUNCTION_DECL
1458 && TREE_CODE (newdecl) != TYPE_DECL
1459 && TREE_CODE (newdecl) != CONST_DECL)
1460 layout_decl (newdecl, 0);
1464 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1465 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1466 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1467 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1468 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1469 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1471 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1472 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1476 /* Keep the old rtl since we can safely use it. */
1477 COPY_DECL_RTL (olddecl, newdecl);
1479 /* Merge the type qualifiers. */
1480 if (TREE_READONLY (newdecl))
1481 TREE_READONLY (olddecl) = 1;
1483 if (TREE_THIS_VOLATILE (newdecl))
1485 TREE_THIS_VOLATILE (olddecl) = 1;
1486 if (TREE_CODE (newdecl) == VAR_DECL)
1487 make_var_volatile (newdecl);
1490 /* Keep source location of definition rather than declaration. */
1491 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1492 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1494 /* Merge the unused-warning information. */
1495 if (DECL_IN_SYSTEM_HEADER (olddecl))
1496 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1497 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1498 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1500 /* Merge the initialization information. */
1501 if (DECL_INITIAL (newdecl) == 0)
1502 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1504 /* Merge the section attribute.
1505 We want to issue an error if the sections conflict but that must be
1506 done later in decl_attributes since we are called before attributes
1508 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1509 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1511 /* Copy the assembler name.
1512 Currently, it can only be defined in the prototype. */
1513 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1515 /* If either declaration has a nondefault visibility, use it. */
1516 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1517 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1519 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1521 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1522 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1523 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1524 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1525 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1526 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1527 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1528 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1529 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1532 /* Merge the storage class information. */
1533 merge_weak (newdecl, olddecl);
1535 /* For functions, static overrides non-static. */
1536 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1538 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1539 /* This is since we don't automatically
1540 copy the attributes of NEWDECL into OLDDECL. */
1541 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1542 /* If this clears `static', clear it in the identifier too. */
1543 if (! TREE_PUBLIC (olddecl))
1544 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1546 if (DECL_EXTERNAL (newdecl))
1548 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1549 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1551 /* An extern decl does not override previous storage class. */
1552 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1553 if (! DECL_EXTERNAL (newdecl))
1555 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1556 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1561 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1562 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1565 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1567 /* If we're redefining a function previously defined as extern
1568 inline, make sure we emit debug info for the inline before we
1569 throw it away, in case it was inlined into a function that hasn't
1570 been written out yet. */
1571 if (new_is_definition && DECL_INITIAL (olddecl))
1573 if (TREE_USED (olddecl)
1574 /* In unit-at-a-time mode we never inline re-defined extern
1575 inline functions. */
1576 && !flag_unit_at_a_time
1577 && cgraph_function_possibly_inlined_p (olddecl))
1578 (*debug_hooks->outlining_inline_function) (olddecl);
1580 /* The new defn must not be inline. */
1581 DECL_INLINE (newdecl) = 0;
1582 DECL_UNINLINABLE (newdecl) = 1;
1586 /* If either decl says `inline', this fn is inline,
1587 unless its definition was passed already. */
1588 if (DECL_DECLARED_INLINE_P (newdecl)
1589 || DECL_DECLARED_INLINE_P (olddecl))
1590 DECL_DECLARED_INLINE_P (newdecl) = 1;
1592 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1593 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1596 if (DECL_BUILT_IN (olddecl))
1598 /* If redeclaring a builtin function, it stays built in.
1599 But it gets tagged as having been declared. */
1600 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1601 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1602 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1605 /* Also preserve various other info from the definition. */
1606 if (! new_is_definition)
1608 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1609 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1610 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1611 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1612 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1614 /* Set DECL_INLINE on the declaration if we've got a body
1615 from which to instantiate. */
1616 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1618 DECL_INLINE (newdecl) = 1;
1619 DECL_ABSTRACT_ORIGIN (newdecl)
1620 = DECL_ABSTRACT_ORIGIN (olddecl);
1625 /* If a previous declaration said inline, mark the
1626 definition as inlinable. */
1627 if (DECL_DECLARED_INLINE_P (newdecl)
1628 && ! DECL_UNINLINABLE (newdecl))
1629 DECL_INLINE (newdecl) = 1;
1633 /* This bit must not get wiped out. */
1634 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1636 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1637 But preserve OLDDECL's DECL_UID. */
1639 unsigned olddecl_uid = DECL_UID (olddecl);
1641 memcpy ((char *) olddecl + sizeof (struct tree_common),
1642 (char *) newdecl + sizeof (struct tree_common),
1643 sizeof (struct tree_decl) - sizeof (struct tree_common));
1644 DECL_UID (olddecl) = olddecl_uid;
1647 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1648 so that encode_section_info has a chance to look at the new decl
1649 flags and attributes. */
1650 if (DECL_RTL_SET_P (olddecl)
1651 && (TREE_CODE (olddecl) == FUNCTION_DECL
1652 || (TREE_CODE (olddecl) == VAR_DECL
1653 && TREE_STATIC (olddecl))))
1654 make_decl_rtl (olddecl, NULL);
1657 /* Handle when a new declaration NEWDECL has the same name as an old
1658 one OLDDECL in the same binding contour. Prints an error message
1661 If safely possible, alter OLDDECL to look like NEWDECL, and return
1662 true. Otherwise, return false. */
1665 duplicate_decls (tree newdecl, tree olddecl)
1667 tree newtype, oldtype;
1669 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1672 merge_decls (newdecl, olddecl, newtype, oldtype);
1677 /* Check whether decl-node NEW shadows an existing declaration. */
1679 warn_if_shadowing (tree new)
1681 struct c_binding *b;
1683 /* Shadow warnings wanted? */
1685 /* No shadow warnings for internally generated vars. */
1686 || DECL_SOURCE_LINE (new) == 0
1687 /* No shadow warnings for vars made for inlining. */
1688 || DECL_FROM_INLINE (new)
1689 /* Don't warn about the parm names in function declarator
1690 within a function declarator. It would be nice to avoid
1691 warning in any function declarator in a declaration, as
1692 opposed to a definition, but there is no way to tell
1693 it's not a definition at this point. */
1694 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1697 /* Is anything being shadowed? Do not be confused by a second binding
1698 to the same decl in the externals scope. */
1699 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1700 if (b->decl && b->decl != new && b->contour != external_scope)
1704 if (TREE_CODE (old) == PARM_DECL)
1705 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1706 else if (DECL_FILE_SCOPE_P (old))
1707 warning ("%Jdeclaration of '%D' shadows a global declaration",
1709 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1710 warning ("%Jdeclaration of '%D' shadows a built-in function",
1713 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1715 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1716 warning ("%Jshadowed declaration is here", old);
1723 /* Subroutine of pushdecl.
1725 X is a TYPE_DECL for a typedef statement. Create a brand new
1726 ..._TYPE node (which will be just a variant of the existing
1727 ..._TYPE node with identical properties) and then install X
1728 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1730 The whole point here is to end up with a situation where each
1731 and every ..._TYPE node the compiler creates will be uniquely
1732 associated with AT MOST one node representing a typedef name.
1733 This way, even though the compiler substitutes corresponding
1734 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1735 early on, later parts of the compiler can always do the reverse
1736 translation and get back the corresponding typedef name. For
1739 typedef struct S MY_TYPE;
1742 Later parts of the compiler might only know that `object' was of
1743 type `struct S' if it were not for code just below. With this
1744 code however, later parts of the compiler see something like:
1746 struct S' == struct S
1747 typedef struct S' MY_TYPE;
1750 And they can then deduce (from the node for type struct S') that
1751 the original object declaration was:
1755 Being able to do this is important for proper support of protoize,
1756 and also for generating precise symbolic debugging information
1757 which takes full account of the programmer's (typedef) vocabulary.
1759 Obviously, we don't want to generate a duplicate ..._TYPE node if
1760 the TYPE_DECL node that we are now processing really represents a
1761 standard built-in type.
1763 Since all standard types are effectively declared at line zero
1764 in the source file, we can easily check to see if we are working
1765 on a standard type by checking the current value of lineno. */
1768 clone_underlying_type (tree x)
1770 if (DECL_SOURCE_LINE (x) == 0)
1772 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1773 TYPE_NAME (TREE_TYPE (x)) = x;
1775 else if (TREE_TYPE (x) != error_mark_node
1776 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1778 tree tt = TREE_TYPE (x);
1779 DECL_ORIGINAL_TYPE (x) = tt;
1780 tt = build_type_copy (tt);
1782 TREE_USED (tt) = TREE_USED (x);
1787 /* Record a decl-node X as belonging to the current lexical scope.
1788 Check for errors (such as an incompatible declaration for the same
1789 name already seen in the same scope).
1791 Returns either X or an old decl for the same name.
1792 If an old decl is returned, it may have been smashed
1793 to agree with what X says. */
1798 tree name = DECL_NAME (x);
1799 struct c_scope *scope = current_scope;
1800 struct c_binding *b;
1802 /* Functions need the lang_decl data. */
1803 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1804 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1806 /* Must set DECL_CONTEXT for everything not at file scope or
1807 DECL_FILE_SCOPE_P won't work. Local externs don't count
1808 unless they have initializers (which generate code). */
1809 if (current_function_decl
1810 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1811 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1812 DECL_CONTEXT (x) = current_function_decl;
1814 /* Anonymous decls are just inserted in the scope. */
1817 bind (name, x, scope);
1821 /* First, see if there is another declaration with the same name in
1822 the current scope. If there is, duplicate_decls may do all the
1823 work for us. If duplicate_decls returns false, that indicates
1824 two incompatible decls in the same scope; we are to silently
1825 replace the old one (duplicate_decls has issued all appropriate
1826 diagnostics). In particular, we should not consider possible
1827 duplicates in the external scope, or shadowing. */
1828 b = I_SYMBOL_BINDING (name);
1829 if (b && b->contour == scope)
1831 if (duplicate_decls (x, b->decl))
1834 goto skip_external_and_shadow_checks;
1837 /* All declarations with external linkage, and all external
1838 references, go in the external scope, no matter what scope is
1839 current. However, the binding in that scope is ignored for
1840 purposes of normal name lookup. A separate binding structure is
1841 created in the requested scope; this governs the normal
1842 visibility of the symbol.
1844 The binding in the externals scope is used exclusively for
1845 detecting duplicate declarations of the same object, no matter
1846 what scope they are in; this is what we do here. (C99 6.2.7p2:
1847 All declarations that refer to the same object or function shall
1848 have compatible type; otherwise, the behavior is undefined.) */
1849 if (DECL_EXTERNAL (x) || scope == file_scope)
1851 if (warn_nested_externs
1852 && scope != file_scope
1853 && !DECL_IN_SYSTEM_HEADER (x))
1854 warning ("nested extern declaration of `%s'",
1855 IDENTIFIER_POINTER (name));
1857 while (b && b->contour != external_scope)
1860 /* The point of the same_translation_unit_p check here is,
1861 we want to detect a duplicate decl for a construct like
1862 foo() { extern bar(); } ... static bar(); but not if
1863 they are in different translation units. In any case,
1864 the static does not go in the externals scope. */
1866 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1867 || same_translation_unit_p (x, b->decl))
1868 && duplicate_decls (x, b->decl))
1870 bind (name, b->decl, scope);
1873 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1875 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1876 bind (name, x, external_scope);
1880 warn_if_shadowing (x);
1882 skip_external_and_shadow_checks:
1883 if (TREE_CODE (x) == TYPE_DECL)
1884 clone_underlying_type (x);
1886 bind (name, x, scope);
1888 /* If x's type is incomplete because it's based on a
1889 structure or union which has not yet been fully declared,
1890 attach it to that structure or union type, so we can go
1891 back and complete the variable declaration later, if the
1892 structure or union gets fully declared.
1894 If the input is erroneous, we can have error_mark in the type
1895 slot (e.g. "f(void a, ...)") - that doesn't count as an
1897 if (TREE_TYPE (x) != error_mark_node
1898 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1900 tree element = TREE_TYPE (x);
1902 while (TREE_CODE (element) == ARRAY_TYPE)
1903 element = TREE_TYPE (element);
1904 element = TYPE_MAIN_VARIANT (element);
1906 if ((TREE_CODE (element) == RECORD_TYPE
1907 || TREE_CODE (element) == UNION_TYPE)
1908 && (TREE_CODE (x) != TYPE_DECL
1909 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1910 && !COMPLETE_TYPE_P (element))
1911 C_TYPE_INCOMPLETE_VARS (element)
1912 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1917 /* Record X as belonging to file scope.
1918 This is used only internally by the Objective-C front end,
1919 and is limited to its needs. duplicate_decls is not called;
1920 if there is any preexisting decl for this identifier, it is an ICE. */
1923 pushdecl_top_level (tree x)
1927 if (TREE_CODE (x) != VAR_DECL)
1930 name = DECL_NAME (x);
1932 if (I_SYMBOL_BINDING (name))
1935 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1937 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1938 bind (name, x, external_scope);
1941 bind (name, x, file_scope);
1947 implicit_decl_warning (tree id, tree olddecl)
1949 void (*diag) (const char *, ...);
1950 switch (mesg_implicit_function_declaration)
1953 case 1: diag = warning; break;
1954 case 2: diag = error; break;
1958 diag (N_("implicit declaration of function '%E'"), id);
1960 locate_old_decl (olddecl, diag);
1963 /* Generate an implicit declaration for identifier FUNCTIONID as a
1964 function of type int (). */
1967 implicitly_declare (tree functionid)
1969 tree decl = lookup_name_in_scope (functionid, external_scope);
1973 /* FIXME: Objective-C has weird not-really-builtin functions
1974 which are supposed to be visible automatically. They wind up
1975 in the external scope because they're pushed before the file
1976 scope gets created. Catch this here and rebind them into the
1978 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1980 bind (functionid, decl, file_scope);
1985 /* Implicit declaration of a function already declared
1986 (somehow) in a different scope, or as a built-in.
1987 If this is the first time this has happened, warn;
1988 then recycle the old declaration. */
1989 if (!C_DECL_IMPLICIT (decl))
1991 implicit_decl_warning (functionid, decl);
1992 C_DECL_IMPLICIT (decl) = 1;
1994 bind (functionid, decl, current_scope);
1999 /* Not seen before. */
2000 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2001 DECL_EXTERNAL (decl) = 1;
2002 TREE_PUBLIC (decl) = 1;
2003 C_DECL_IMPLICIT (decl) = 1;
2004 implicit_decl_warning (functionid, 0);
2006 /* C89 says implicit declarations are in the innermost block.
2007 So we record the decl in the standard fashion. */
2008 decl = pushdecl (decl);
2010 /* No need to call objc_check_decl here - it's a function type. */
2011 rest_of_decl_compilation (decl, NULL, 0, 0);
2013 /* Write a record describing this implicit function declaration
2014 to the prototypes file (if requested). */
2015 gen_aux_info_record (decl, 0, 1, 0);
2017 /* Possibly apply some default attributes to this implicit declaration. */
2018 decl_attributes (&decl, NULL_TREE, 0);
2023 /* Issue an error message for a reference to an undeclared variable
2024 ID, including a reference to a builtin outside of function-call
2025 context. Establish a binding of the identifier to error_mark_node
2026 in an appropriate scope, which will suppress further errors for the
2029 undeclared_variable (tree id)
2031 static bool already = false;
2032 struct c_scope *scope;
2034 if (current_function_decl == 0)
2036 error ("'%E' undeclared here (not in a function)", id);
2037 scope = current_scope;
2041 error ("'%E' undeclared (first use in this function)", id);
2045 error ("(Each undeclared identifier is reported only once");
2046 error ("for each function it appears in.)");
2050 /* If we are parsing old-style parameter decls, current_function_decl
2051 will be nonnull but current_function_scope will be null. */
2052 scope = current_function_scope ? current_function_scope : current_scope;
2054 bind (id, error_mark_node, scope);
2057 /* Subroutine of lookup_label, declare_label, define_label: construct a
2058 LABEL_DECL with all the proper frills. */
2061 make_label (tree name, location_t location)
2063 tree label = build_decl (LABEL_DECL, name, void_type_node);
2065 DECL_CONTEXT (label) = current_function_decl;
2066 DECL_MODE (label) = VOIDmode;
2067 DECL_SOURCE_LOCATION (label) = location;
2072 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2073 Create one if none exists so far for the current function.
2074 This is called when a label is used in a goto expression or
2075 has its address taken. */
2078 lookup_label (tree name)
2082 if (current_function_decl == 0)
2084 error ("label %s referenced outside of any function",
2085 IDENTIFIER_POINTER (name));
2089 /* Use a label already defined or ref'd with this name, but not if
2090 it is inherited from a containing function and wasn't declared
2092 label = I_LABEL_DECL (name);
2093 if (label && (DECL_CONTEXT (label) == current_function_decl
2094 || C_DECLARED_LABEL_FLAG (label)))
2096 /* If the label has only been declared, update its apparent
2097 location to point here, for better diagnostics if it
2098 turns out not to have been defined. */
2099 if (!TREE_USED (label))
2100 DECL_SOURCE_LOCATION (label) = input_location;
2104 /* No label binding for that identifier; make one. */
2105 label = make_label (name, input_location);
2107 /* Ordinary labels go in the current function scope. */
2108 bind (name, label, current_function_scope);
2112 /* Make a label named NAME in the current function, shadowing silently
2113 any that may be inherited from containing functions or containing
2114 scopes. This is called for __label__ declarations. */
2116 /* Note that valid use, if the label being shadowed comes from another
2117 scope in the same function, requires calling declare_nonlocal_label
2118 right away. (Is this still true? -zw 2003-07-17) */
2121 declare_label (tree name)
2123 struct c_binding *b = I_LABEL_BINDING (name);
2126 /* Check to make sure that the label hasn't already been declared
2128 if (b && b->contour == current_scope)
2130 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2131 locate_old_decl (b->decl, error);
2133 /* Just use the previous declaration. */
2137 label = make_label (name, input_location);
2138 C_DECLARED_LABEL_FLAG (label) = 1;
2140 /* Declared labels go in the current scope. */
2141 bind (name, label, current_scope);
2145 /* Define a label, specifying the location in the source file.
2146 Return the LABEL_DECL node for the label, if the definition is valid.
2147 Otherwise return 0. */
2150 define_label (location_t location, tree name)
2152 /* Find any preexisting label with this name. It is an error
2153 if that label has already been defined in this function, or
2154 if there is a containing function with a declared label with
2156 tree label = I_LABEL_DECL (name);
2159 && ((DECL_CONTEXT (label) == current_function_decl
2160 && DECL_INITIAL (label) != 0)
2161 || (DECL_CONTEXT (label) != current_function_decl
2162 && C_DECLARED_LABEL_FLAG (label))))
2164 error ("%Hduplicate label `%D'", &location, label);
2165 locate_old_decl (label, error);
2168 else if (label && DECL_CONTEXT (label) == current_function_decl)
2170 /* The label has been used or declared already in this function,
2171 but not defined. Update its location to point to this
2173 DECL_SOURCE_LOCATION (label) = location;
2177 /* No label binding for that identifier; make one. */
2178 label = make_label (name, location);
2180 /* Ordinary labels go in the current function scope. */
2181 bind (name, label, current_function_scope);
2184 if (warn_traditional && !in_system_header && lookup_name (name))
2185 warning ("%Htraditional C lacks a separate namespace for labels, "
2186 "identifier `%s' conflicts", &location,
2187 IDENTIFIER_POINTER (name));
2189 /* Mark label as having been defined. */
2190 DECL_INITIAL (label) = error_mark_node;
2194 /* Given NAME, an IDENTIFIER_NODE,
2195 return the structure (or union or enum) definition for that name.
2196 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2197 CODE says which kind of type the caller wants;
2198 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2199 If the wrong kind of type is found, an error is reported. */
2202 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2204 struct c_binding *b = I_TAG_BINDING (name);
2210 /* We only care about whether it's in this level if
2211 thislevel_only was set or it might be a type clash. */
2212 if (thislevel_only || TREE_CODE (b->decl) != code)
2214 /* For our purposes, a tag in the external scope is the same as
2215 a tag in the file scope. (Primarily relevant to Objective-C
2216 and its builtin structure tags, which get pushed before the
2217 file scope is created.) */
2218 if (b->contour == current_scope
2219 || (current_scope == file_scope && b->contour == external_scope))
2223 if (thislevel_only && !thislevel)
2226 if (TREE_CODE (b->decl) != code)
2228 /* Definition isn't the kind we were looking for. */
2229 pending_invalid_xref = name;
2230 pending_invalid_xref_location = input_location;
2232 /* If in the same binding level as a declaration as a tag
2233 of a different type, this must not be allowed to
2234 shadow that tag, so give the error immediately.
2235 (For example, "struct foo; union foo;" is invalid.) */
2237 pending_xref_error ();
2242 /* Print an error message now
2243 for a recent invalid struct, union or enum cross reference.
2244 We don't print them immediately because they are not invalid
2245 when used in the `struct foo;' construct for shadowing. */
2248 pending_xref_error (void)
2250 if (pending_invalid_xref != 0)
2251 error ("%H`%s' defined as wrong kind of tag",
2252 &pending_invalid_xref_location,
2253 IDENTIFIER_POINTER (pending_invalid_xref));
2254 pending_invalid_xref = 0;
2258 /* Look up NAME in the current scope and its superiors
2259 in the namespace of variables, functions and typedefs.
2260 Return a ..._DECL node of some kind representing its definition,
2261 or return 0 if it is undefined. */
2264 lookup_name (tree name)
2266 struct c_binding *b = I_SYMBOL_BINDING (name);
2267 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2272 /* Similar to `lookup_name' but look only at the indicated scope. */
2275 lookup_name_in_scope (tree name, struct c_scope *scope)
2277 struct c_binding *b;
2279 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2280 if (b->contour == scope)
2285 /* Create the predefined scalar types of C,
2286 and some nodes representing standard constants (0, 1, (void *) 0).
2287 Initialize the global scope.
2288 Make definitions for built-in primitive functions. */
2291 c_init_decl_processing (void)
2294 tree ptr_ftype_void, ptr_ftype_ptr;
2295 location_t save_loc = input_location;
2297 /* Adds some ggc roots, and reserved words for c-parse.in. */
2300 current_function_decl = 0;
2302 /* Make the externals scope. */
2304 external_scope = current_scope;
2306 /* Declarations from c_common_nodes_and_builtins must not be associated
2307 with this input file, lest we get differences between using and not
2308 using preprocessed headers. */
2309 input_location.file = "<internal>";
2310 input_location.line = 0;
2312 build_common_tree_nodes (flag_signed_char);
2314 c_common_nodes_and_builtins ();
2316 /* In C, comparisons and TRUTH_* expressions have type int. */
2317 truthvalue_type_node = integer_type_node;
2318 truthvalue_true_node = integer_one_node;
2319 truthvalue_false_node = integer_zero_node;
2321 /* Even in C99, which has a real boolean type. */
2322 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2323 boolean_type_node));
2325 endlink = void_list_node;
2326 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2328 = build_function_type (ptr_type_node,
2329 tree_cons (NULL_TREE, ptr_type_node, endlink));
2331 input_location = save_loc;
2333 pedantic_lvalues = true;
2335 make_fname_decl = c_make_fname_decl;
2336 start_fname_decls ();
2339 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2340 decl, NAME is the initialization string and TYPE_DEP indicates whether
2341 NAME depended on the type of the function. As we don't yet implement
2342 delayed emission of static data, we mark the decl as emitted
2343 so it is not placed in the output. Anything using it must therefore pull
2344 out the STRING_CST initializer directly. FIXME. */
2347 c_make_fname_decl (tree id, int type_dep)
2349 const char *name = fname_as_string (type_dep);
2350 tree decl, type, init;
2351 size_t length = strlen (name);
2353 type = build_array_type
2354 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2355 build_index_type (size_int (length)));
2357 decl = build_decl (VAR_DECL, id, type);
2359 TREE_STATIC (decl) = 1;
2360 TREE_READONLY (decl) = 1;
2361 DECL_ARTIFICIAL (decl) = 1;
2363 init = build_string (length + 1, name);
2364 TREE_TYPE (init) = type;
2365 DECL_INITIAL (decl) = init;
2367 TREE_USED (decl) = 1;
2369 if (current_function_decl)
2371 DECL_CONTEXT (decl) = current_function_decl;
2372 bind (id, decl, current_function_scope);
2375 finish_decl (decl, init, NULL_TREE);
2380 /* Return a definition for a builtin function named NAME and whose data type
2381 is TYPE. TYPE should be a function type with argument types.
2382 FUNCTION_CODE tells later passes how to compile calls to this function.
2383 See tree.h for its possible values.
2385 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2386 the name to be called if we can't opencode the function. If
2387 ATTRS is nonzero, use that for the function's attribute list. */
2390 builtin_function (const char *name, tree type, int function_code,
2391 enum built_in_class class, const char *library_name,
2394 tree id = get_identifier (name);
2395 tree decl = build_decl (FUNCTION_DECL, id, type);
2396 TREE_PUBLIC (decl) = 1;
2397 DECL_EXTERNAL (decl) = 1;
2398 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2399 DECL_BUILT_IN_CLASS (decl) = class;
2400 DECL_FUNCTION_CODE (decl) = function_code;
2402 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2403 make_decl_rtl (decl, NULL);
2405 /* Should never be called on a symbol with a preexisting meaning. */
2406 if (I_SYMBOL_BINDING (id))
2409 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2410 bind (id, decl, external_scope);
2412 /* Builtins in the implementation namespace are made visible without
2413 needing to be explicitly declared. See push_file_scope. */
2414 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2416 TREE_CHAIN (decl) = visible_builtins;
2417 visible_builtins = decl;
2420 /* Possibly apply some default attributes to this built-in function. */
2422 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2424 decl_attributes (&decl, NULL_TREE, 0);
2429 /* Called when a declaration is seen that contains no names to declare.
2430 If its type is a reference to a structure, union or enum inherited
2431 from a containing scope, shadow that tag name for the current scope
2432 with a forward reference.
2433 If its type defines a new named structure or union
2434 or defines an enum, it is valid but we need not do anything here.
2435 Otherwise, it is an error. */
2438 shadow_tag (tree declspecs)
2440 shadow_tag_warned (declspecs, 0);
2444 shadow_tag_warned (tree declspecs, int warned)
2447 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2454 pending_invalid_xref = 0;
2456 /* Remove the attributes from declspecs, since they will confuse the
2458 split_specs_attrs (declspecs, &specs, &attrs);
2460 for (link = specs; link; link = TREE_CHAIN (link))
2462 tree value = TREE_VALUE (link);
2463 enum tree_code code = TREE_CODE (value);
2465 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2466 /* Used to test also that TYPE_SIZE (value) != 0.
2467 That caused warning for `struct foo;' at top level in the file. */
2469 tree name = TYPE_NAME (value);
2476 if (warned != 1 && code != ENUMERAL_TYPE)
2477 /* Empty unnamed enum OK */
2479 pedwarn ("unnamed struct/union that defines no instances");
2485 t = lookup_tag (code, name, 1);
2489 t = make_node (code);
2496 if (!warned && ! in_system_header)
2498 warning ("useless keyword or type name in empty declaration");
2505 error ("two types specified in one empty declaration");
2510 pedwarn ("empty declaration");
2514 /* Construct an array declarator. EXPR is the expression inside [], or
2515 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2516 to the pointer to which a parameter array is converted). STATIC_P is
2517 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2518 is nonzero is the array is [*], a VLA of unspecified length which is
2519 nevertheless a complete type (not currently implemented by GCC),
2520 zero otherwise. The declarator is constructed as an ARRAY_REF
2521 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2522 left of the [] (filled by in set_array_declarator_type) and operand 1
2523 is the expression inside; whose TREE_TYPE is the type qualifiers and
2524 which has TREE_STATIC set if "static" is used. */
2527 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2530 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2531 TREE_TYPE (decl) = quals;
2532 TREE_STATIC (decl) = (static_p ? 1 : 0);
2533 if (pedantic && !flag_isoc99)
2535 if (static_p || quals != NULL_TREE)
2536 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2538 pedwarn ("ISO C90 does not support `[*]' array declarators");
2541 warning ("GCC does not yet properly implement `[*]' array declarators");
2545 /* Set the type of an array declarator. DECL is the declarator, as
2546 constructed by build_array_declarator; TYPE is what appears on the left
2547 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2548 abstract declarator, zero otherwise; this is used to reject static and
2549 type qualifiers in abstract declarators, where they are not in the
2553 set_array_declarator_type (tree decl, tree type, int abstract_p)
2555 TREE_OPERAND (decl, 0) = type;
2556 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2557 error ("static or type qualifiers in abstract declarator");
2561 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2564 groktypename (tree typename)
2568 if (TREE_CODE (typename) != TREE_LIST)
2571 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2573 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2576 /* Apply attributes. */
2577 decl_attributes (&typename, attrs, 0);
2582 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2585 groktypename_in_parm_context (tree typename)
2587 if (TREE_CODE (typename) != TREE_LIST)
2589 return grokdeclarator (TREE_VALUE (typename),
2590 TREE_PURPOSE (typename),
2594 /* Decode a declarator in an ordinary declaration or data definition.
2595 This is called as soon as the type information and variable name
2596 have been parsed, before parsing the initializer if any.
2597 Here we create the ..._DECL node, fill in its type,
2598 and put it on the list of decls for the current context.
2599 The ..._DECL node is returned as the value.
2601 Exception: for arrays where the length is not specified,
2602 the type is left null, to be filled in by `finish_decl'.
2604 Function definitions do not come here; they go to start_function
2605 instead. However, external and forward declarations of functions
2606 do go through here. Structure field declarations are done by
2607 grokfield and not through here. */
2610 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2615 /* An object declared as __attribute__((deprecated)) suppresses
2616 warnings of uses of other deprecated items. */
2617 if (lookup_attribute ("deprecated", attributes))
2618 deprecated_state = DEPRECATED_SUPPRESS;
2620 decl = grokdeclarator (declarator, declspecs,
2621 NORMAL, initialized, NULL);
2623 deprecated_state = DEPRECATED_NORMAL;
2625 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2626 && MAIN_NAME_P (DECL_NAME (decl)))
2627 warning ("%J'%D' is usually a function", decl, decl);
2630 /* Is it valid for this decl to have an initializer at all?
2631 If not, set INITIALIZED to zero, which will indirectly
2632 tell 'finish_decl' to ignore the initializer once it is parsed. */
2633 switch (TREE_CODE (decl))
2636 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2641 error ("function '%D' is initialized like a variable", decl);
2646 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2647 error ("parameter '%D' is initialized", decl);
2652 /* Don't allow initializations for incomplete types except for
2653 arrays which might be completed by the initialization. */
2655 /* This can happen if the array size is an undefined macro.
2656 We already gave a warning, so we don't need another one. */
2657 if (TREE_TYPE (decl) == error_mark_node)
2659 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2661 /* A complete type is ok if size is fixed. */
2663 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2664 || C_DECL_VARIABLE_SIZE (decl))
2666 error ("variable-sized object may not be initialized");
2670 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2672 error ("variable '%D' has initializer but incomplete type", decl);
2675 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2677 error ("elements of array '%D' have incomplete type", decl);
2684 DECL_EXTERNAL (decl) = 0;
2685 if (current_scope == file_scope)
2686 TREE_STATIC (decl) = 1;
2688 /* Tell 'pushdecl' this is an initialized decl
2689 even though we don't yet have the initializer expression.
2690 Also tell 'finish_decl' it may store the real initializer. */
2691 DECL_INITIAL (decl) = error_mark_node;
2694 /* If this is a function declaration, write a record describing it to the
2695 prototypes file (if requested). */
2697 if (TREE_CODE (decl) == FUNCTION_DECL)
2698 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2700 /* ANSI specifies that a tentative definition which is not merged with
2701 a non-tentative definition behaves exactly like a definition with an
2702 initializer equal to zero. (Section 3.7.2)
2704 -fno-common gives strict ANSI behavior, though this tends to break
2705 a large body of code that grew up without this rule.
2707 Thread-local variables are never common, since there's no entrenched
2708 body of code to break, and it allows more efficient variable references
2709 in the presence of dynamic linking. */
2711 if (TREE_CODE (decl) == VAR_DECL
2713 && TREE_PUBLIC (decl)
2714 && !DECL_THREAD_LOCAL (decl)
2716 DECL_COMMON (decl) = 1;
2718 /* Set attributes here so if duplicate decl, will have proper attributes. */
2719 decl_attributes (&decl, attributes, 0);
2721 if (TREE_CODE (decl) == FUNCTION_DECL
2722 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2724 tree ce = declarator;
2726 if (TREE_CODE (ce) == INDIRECT_REF)
2727 ce = TREE_OPERAND (declarator, 0);
2728 if (TREE_CODE (ce) == CALL_EXPR)
2730 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2731 for (; args; args = TREE_CHAIN (args))
2733 tree type = TREE_TYPE (args);
2734 if (INTEGRAL_TYPE_P (type)
2735 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2736 DECL_ARG_TYPE (args) = integer_type_node;
2741 if (TREE_CODE (decl) == FUNCTION_DECL
2742 && DECL_DECLARED_INLINE_P (decl)
2743 && DECL_UNINLINABLE (decl)
2744 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2745 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2747 /* Add this decl to the current scope.
2748 TEM may equal DECL or it may be a previous decl of the same name. */
2749 tem = pushdecl (decl);
2754 /* Finish processing of a declaration;
2755 install its initial value.
2756 If the length of an array type is not known before,
2757 it must be determined now, from the initial value, or it is an error. */
2760 finish_decl (tree decl, tree init, tree asmspec_tree)
2762 tree type = TREE_TYPE (decl);
2763 int was_incomplete = (DECL_SIZE (decl) == 0);
2764 const char *asmspec = 0;
2766 /* If a name was specified, get the string. */
2767 if (current_scope == file_scope)
2768 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2770 asmspec = TREE_STRING_POINTER (asmspec_tree);
2772 /* If `start_decl' didn't like having an initialization, ignore it now. */
2773 if (init != 0 && DECL_INITIAL (decl) == 0)
2776 /* Don't crash if parm is initialized. */
2777 if (TREE_CODE (decl) == PARM_DECL)
2781 store_init_value (decl, init);
2783 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2784 || TREE_CODE (decl) == FUNCTION_DECL
2785 || TREE_CODE (decl) == FIELD_DECL))
2786 objc_check_decl (decl);
2788 /* Deduce size of array from initialization, if not already known. */
2789 if (TREE_CODE (type) == ARRAY_TYPE
2790 && TYPE_DOMAIN (type) == 0
2791 && TREE_CODE (decl) != TYPE_DECL)
2794 = (TREE_STATIC (decl)
2795 /* Even if pedantic, an external linkage array
2796 may have incomplete type at first. */
2797 ? pedantic && !TREE_PUBLIC (decl)
2798 : !DECL_EXTERNAL (decl));
2800 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2802 /* Get the completed type made by complete_array_type. */
2803 type = TREE_TYPE (decl);
2806 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2808 else if (failure == 2)
2811 error ("%Jarray size missing in '%D'", decl, decl);
2812 /* If a `static' var's size isn't known,
2813 make it extern as well as static, so it does not get
2815 If it is not `static', then do not mark extern;
2816 finish_incomplete_decl will give it a default size
2817 and it will get allocated. */
2818 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2819 DECL_EXTERNAL (decl) = 1;
2822 /* TYPE_MAX_VALUE is always one less than the number of elements
2823 in the array, because we start counting at zero. Therefore,
2824 warn only if the value is less than zero. */
2825 else if (pedantic && TYPE_DOMAIN (type) != 0
2826 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2827 error ("%Jzero or negative size array '%D'", decl, decl);
2829 layout_decl (decl, 0);
2832 if (TREE_CODE (decl) == VAR_DECL)
2834 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2835 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2836 layout_decl (decl, 0);
2838 if (DECL_SIZE (decl) == 0
2839 /* Don't give an error if we already gave one earlier. */
2840 && TREE_TYPE (decl) != error_mark_node
2841 && (TREE_STATIC (decl)
2843 /* A static variable with an incomplete type
2844 is an error if it is initialized.
2845 Also if it is not file scope.
2846 Otherwise, let it through, but if it is not `extern'
2847 then it may cause an error message later. */
2848 (DECL_INITIAL (decl) != 0
2849 || !DECL_FILE_SCOPE_P (decl))
2851 /* An automatic variable with an incomplete type
2853 !DECL_EXTERNAL (decl)))
2855 error ("%Jstorage size of '%D' isn't known", decl, decl);
2856 TREE_TYPE (decl) = error_mark_node;
2859 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2860 && DECL_SIZE (decl) != 0)
2862 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2863 constant_expression_warning (DECL_SIZE (decl));
2865 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2868 if (TREE_USED (type))
2869 TREE_USED (decl) = 1;
2872 /* If this is a function and an assembler name is specified, reset DECL_RTL
2873 so we can give it its new name. Also, update built_in_decls if it
2874 was a normal built-in. */
2875 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2877 /* ASMSPEC is given, and not the name of a register. Mark the
2878 name with a star so assemble_name won't munge it. */
2879 char *starred = alloca (strlen (asmspec) + 2);
2881 strcpy (starred + 1, asmspec);
2883 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2885 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2886 SET_DECL_RTL (builtin, NULL_RTX);
2887 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2888 #ifdef TARGET_MEM_FUNCTIONS
2889 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2890 init_block_move_fn (starred);
2891 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2892 init_block_clear_fn (starred);
2894 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2895 init_block_move_fn (starred);
2896 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2897 init_block_clear_fn (starred);
2900 SET_DECL_RTL (decl, NULL_RTX);
2901 change_decl_assembler_name (decl, get_identifier (starred));
2904 /* If #pragma weak was used, mark the decl weak now. */
2905 if (current_scope == file_scope)
2906 maybe_apply_pragma_weak (decl);
2908 /* Output the assembler code and/or RTL code for variables and functions,
2909 unless the type is an undefined structure or union.
2910 If not, it will get done when the type is completed. */
2912 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2914 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2915 if (c_dialect_objc ())
2916 objc_check_decl (decl);
2918 if (DECL_FILE_SCOPE_P (decl))
2920 if (DECL_INITIAL (decl) == NULL_TREE
2921 || DECL_INITIAL (decl) == error_mark_node)
2922 /* Don't output anything
2923 when a tentative file-scope definition is seen.
2924 But at end of compilation, do output code for them. */
2925 DECL_DEFER_OUTPUT (decl) = 1;
2926 rest_of_decl_compilation (decl, asmspec, true, 0);
2930 /* This is a local variable. If there is an ASMSPEC, the
2931 user has requested that we handle it specially. */
2934 /* In conjunction with an ASMSPEC, the `register'
2935 keyword indicates that we should place the variable
2936 in a particular register. */
2937 if (C_DECL_REGISTER (decl))
2939 DECL_C_HARD_REGISTER (decl) = 1;
2940 /* This cannot be done for a structure with volatile
2941 fields, on which DECL_REGISTER will have been
2943 if (!DECL_REGISTER (decl))
2944 error ("cannot put object with volatile field into register");
2947 /* If this is not a static variable, issue a warning.
2948 It doesn't make any sense to give an ASMSPEC for an
2949 ordinary, non-register local variable. Historically,
2950 GCC has accepted -- but ignored -- the ASMSPEC in
2952 if (TREE_CODE (decl) == VAR_DECL
2953 && !C_DECL_REGISTER (decl)
2954 && !TREE_STATIC (decl))
2955 warning ("%Jignoring asm-specifier for non-static local "
2956 "variable '%D'", decl, decl);
2958 change_decl_assembler_name (decl, get_identifier (asmspec));
2961 if (TREE_CODE (decl) != FUNCTION_DECL)
2962 add_decl_stmt (decl);
2965 if (!DECL_FILE_SCOPE_P (decl))
2967 /* Recompute the RTL of a local array now
2968 if it used to be an incomplete type. */
2970 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2972 /* If we used it already as memory, it must stay in memory. */
2973 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2974 /* If it's still incomplete now, no init will save it. */
2975 if (DECL_SIZE (decl) == 0)
2976 DECL_INITIAL (decl) = 0;
2981 /* If this was marked 'used', be sure it will be output. */
2982 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2983 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2985 if (TREE_CODE (decl) == TYPE_DECL)
2987 if (!DECL_FILE_SCOPE_P (decl)
2988 && variably_modified_type_p (TREE_TYPE (decl)))
2989 add_decl_stmt (decl);
2991 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2994 /* At the end of a declaration, throw away any variable type sizes
2995 of types defined inside that declaration. There is no use
2996 computing them in the following function definition. */
2997 if (current_scope == file_scope)
2998 get_pending_sizes ();
3000 /* Install a cleanup (aka destructor) if one was given. */
3001 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3003 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3006 static bool eh_initialized_p;
3008 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3009 tree cleanup_decl = lookup_name (cleanup_id);
3012 /* Build "cleanup(&decl)" for the destructor. */
3013 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3014 cleanup = build_tree_list (NULL_TREE, cleanup);
3015 cleanup = build_function_call (cleanup_decl, cleanup);
3017 /* Don't warn about decl unused; the cleanup uses it. */
3018 TREE_USED (decl) = 1;
3020 /* Initialize EH, if we've been told to do so. */
3021 if (flag_exceptions && !eh_initialized_p)
3023 eh_initialized_p = true;
3024 eh_personality_libfunc
3025 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3026 ? "__gcc_personality_sj0"
3027 : "__gcc_personality_v0");
3028 using_eh_for_cleanups ();
3031 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3036 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3037 and push that on the current scope. */
3040 push_parm_decl (tree parm)
3044 /* Don't attempt to expand sizes while parsing this decl.
3045 (We can get here with i_s_e 1 somehow from Objective-C.) */
3046 int save_immediate_size_expand = immediate_size_expand;
3047 immediate_size_expand = 0;
3049 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3050 TREE_PURPOSE (TREE_PURPOSE (parm)),
3052 decl_attributes (&decl, TREE_VALUE (parm), 0);
3054 decl = pushdecl (decl);
3056 finish_decl (decl, NULL_TREE, NULL_TREE);
3058 immediate_size_expand = save_immediate_size_expand;
3061 /* Mark all the parameter declarations to date as forward decls.
3062 Also diagnose use of this extension. */
3065 mark_forward_parm_decls (void)
3067 struct c_binding *b;
3069 if (pedantic && !current_scope->warned_forward_parm_decls)
3071 pedwarn ("ISO C forbids forward parameter declarations");
3072 current_scope->warned_forward_parm_decls = true;
3075 for (b = current_scope->bindings; b; b = b->prev)
3076 if (TREE_CODE (b->decl) == PARM_DECL)
3077 TREE_ASM_WRITTEN (b->decl) = 1;
3080 static GTY(()) int compound_literal_number;
3082 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3083 literal, which may be an incomplete array type completed by the
3084 initializer; INIT is a CONSTRUCTOR that initializes the compound
3088 build_compound_literal (tree type, tree init)
3090 /* We do not use start_decl here because we have a type, not a declarator;
3091 and do not use finish_decl because the decl should be stored inside
3092 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3093 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3096 DECL_EXTERNAL (decl) = 0;
3097 TREE_PUBLIC (decl) = 0;
3098 TREE_STATIC (decl) = (current_scope == file_scope);
3099 DECL_CONTEXT (decl) = current_function_decl;
3100 TREE_USED (decl) = 1;
3101 TREE_TYPE (decl) = type;
3102 TREE_READONLY (decl) = TYPE_READONLY (type);
3103 store_init_value (decl, init);
3105 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3107 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3112 type = TREE_TYPE (decl);
3113 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3114 return error_mark_node;
3116 stmt = build_stmt (DECL_STMT, decl);
3117 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3118 TREE_SIDE_EFFECTS (complit) = 1;
3120 layout_decl (decl, 0);
3122 if (TREE_STATIC (decl))
3124 /* This decl needs a name for the assembler output. We also need
3125 a unique suffix to be added to the name. */
3128 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3129 compound_literal_number);
3130 compound_literal_number++;
3131 DECL_NAME (decl) = get_identifier (name);
3132 DECL_DEFER_OUTPUT (decl) = 1;
3133 DECL_COMDAT (decl) = 1;
3134 DECL_ARTIFICIAL (decl) = 1;
3136 rest_of_decl_compilation (decl, NULL, 1, 0);
3142 /* Make TYPE a complete type based on INITIAL_VALUE.
3143 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3144 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3147 complete_array_type (tree type, tree initial_value, int do_default)
3149 tree maxindex = NULL_TREE;
3154 /* Note MAXINDEX is really the maximum index,
3155 one less than the size. */
3156 if (TREE_CODE (initial_value) == STRING_CST)
3159 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3160 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3163 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3165 tree elts = CONSTRUCTOR_ELTS (initial_value);
3166 maxindex = build_int_2 (-1, -1);
3167 for (; elts; elts = TREE_CHAIN (elts))
3169 if (TREE_PURPOSE (elts))
3170 maxindex = TREE_PURPOSE (elts);
3172 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3173 maxindex, integer_one_node));
3175 maxindex = copy_node (maxindex);
3179 /* Make an error message unless that happened already. */
3180 if (initial_value != error_mark_node)
3183 /* Prevent further error messages. */
3184 maxindex = build_int_2 (0, 0);
3191 maxindex = build_int_2 (0, 0);
3197 TYPE_DOMAIN (type) = build_index_type (maxindex);
3198 if (!TREE_TYPE (maxindex))
3199 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3202 /* Lay out the type now that we can get the real answer. */
3209 /* Determine whether TYPE is a structure with a flexible array member,
3210 or a union containing such a structure (possibly recursively). */
3213 flexible_array_type_p (tree type)
3216 switch (TREE_CODE (type))
3219 x = TYPE_FIELDS (type);
3222 while (TREE_CHAIN (x) != NULL_TREE)
3224 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3225 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3226 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3227 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3231 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3233 if (flexible_array_type_p (TREE_TYPE (x)))
3242 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3243 replacing with appropriate values if they are invalid. */
3245 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3248 unsigned int max_width;
3249 unsigned HOST_WIDE_INT w;
3250 const char *name = orig_name ? orig_name: _("<anonymous>");
3253 STRIP_NOPS (*width);
3255 /* Detect and ignore out of range field width and process valid
3257 if (TREE_CODE (*width) != INTEGER_CST)
3259 error ("bit-field `%s' width not an integer constant", name);
3260 *width = integer_one_node;
3264 constant_expression_warning (*width);
3265 if (tree_int_cst_sgn (*width) < 0)
3267 error ("negative width in bit-field `%s'", name);
3268 *width = integer_one_node;
3270 else if (integer_zerop (*width) && orig_name)
3272 error ("zero width for bit-field `%s'", name);
3273 *width = integer_one_node;
3277 /* Detect invalid bit-field type. */
3278 if (TREE_CODE (*type) != INTEGER_TYPE
3279 && TREE_CODE (*type) != BOOLEAN_TYPE
3280 && TREE_CODE (*type) != ENUMERAL_TYPE)
3282 error ("bit-field `%s' has invalid type", name);
3283 *type = unsigned_type_node;
3286 type_mv = TYPE_MAIN_VARIANT (*type);
3288 && type_mv != integer_type_node
3289 && type_mv != unsigned_type_node
3290 && type_mv != boolean_type_node)
3291 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3293 if (type_mv == boolean_type_node)
3294 max_width = CHAR_TYPE_SIZE;
3296 max_width = TYPE_PRECISION (*type);
3298 if (0 < compare_tree_int (*width, max_width))
3300 error ("width of `%s' exceeds its type", name);
3302 *width = build_int_2 (w, 0);
3305 w = tree_low_cst (*width, 1);
3307 if (TREE_CODE (*type) == ENUMERAL_TYPE
3308 && (w < min_precision (TYPE_MIN_VALUE (*type), TYPE_UNSIGNED (*type))
3309 || w < min_precision (TYPE_MAX_VALUE (*type),
3310 TYPE_UNSIGNED (*type))))
3311 warning ("`%s' is narrower than values of its type", name);
3314 /* Given declspecs and a declarator,
3315 determine the name and type of the object declared
3316 and construct a ..._DECL node for it.
3317 (In one case we can return a ..._TYPE node instead.
3318 For invalid input we sometimes return 0.)
3320 DECLSPECS is a chain of tree_list nodes whose value fields
3321 are the storage classes and type specifiers.
3323 DECL_CONTEXT says which syntactic context this declaration is in:
3324 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3325 FUNCDEF for a function definition. Like NORMAL but a few different
3326 error messages in each case. Return value may be zero meaning
3327 this definition is too screwy to try to parse.
3328 PARM for a parameter declaration (either within a function prototype
3329 or before a function body). Make a PARM_DECL, or return void_type_node.
3330 TYPENAME if for a typename (in a cast or sizeof).
3331 Don't make a DECL node; just return the ..._TYPE node.
3332 FIELD for a struct or union field; make a FIELD_DECL.
3333 INITIALIZED is 1 if the decl has an initializer.
3334 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3335 representing the width of the bit-field.
3337 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3338 It may also be so in the PARM case, for a prototype where the
3339 argument type is specified but not the name.
3341 This function is where the complicated C meanings of `static'
3342 and `extern' are interpreted. */
3345 grokdeclarator (tree declarator, tree declspecs,
3346 enum decl_context decl_context, int initialized, tree *width)
3350 tree type = NULL_TREE;
3355 int type_quals = TYPE_UNQUALIFIED;
3357 int explicit_int = 0;
3358 int explicit_char = 0;
3359 int defaulted_int = 0;
3360 tree typedef_decl = 0;
3361 const char *name, *orig_name;
3362 tree typedef_type = 0;
3363 int funcdef_flag = 0;
3364 enum tree_code innermost_code = ERROR_MARK;
3365 int size_varies = 0;
3366 tree decl_attr = NULL_TREE;
3367 tree array_ptr_quals = NULL_TREE;
3368 int array_parm_static = 0;
3369 tree returned_attrs = NULL_TREE;
3370 bool bitfield = width != NULL;
3372 tree arg_info = NULL_TREE;
3374 if (decl_context == FUNCDEF)
3375 funcdef_flag = 1, decl_context = NORMAL;
3377 /* Look inside a declarator for the name being declared
3378 and get it as a string, for an error message. */
3380 tree decl = declarator;
3384 switch (TREE_CODE (decl))
3389 innermost_code = TREE_CODE (decl);
3390 decl = TREE_OPERAND (decl, 0);
3394 decl = TREE_VALUE (decl);
3397 case IDENTIFIER_NODE:
3398 name = IDENTIFIER_POINTER (decl);
3410 /* A function definition's declarator must have the form of
3411 a function declarator. */
3413 if (funcdef_flag && innermost_code != CALL_EXPR)
3416 /* If this looks like a function definition, make it one,
3417 even if it occurs where parms are expected.
3418 Then store_parm_decls will reject it and not use it as a parm. */
3419 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3420 decl_context = PARM;
3422 /* Look through the decl specs and record which ones appear.
3423 Some typespecs are defined as built-in typenames.
3424 Others, the ones that are modifiers of other types,
3425 are represented by bits in SPECBITS: set the bits for
3426 the modifiers that appear. Storage class keywords are also in SPECBITS.
3428 If there is a typedef name or a type, store the type in TYPE.
3429 This includes builtin typedefs such as `int'.
3431 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3432 and did not come from a user typedef.
3434 Set LONGLONG if `long' is mentioned twice. */
3436 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3438 tree id = TREE_VALUE (spec);
3440 /* If the entire declaration is itself tagged as deprecated then
3441 suppress reports of deprecated items. */
3442 if (id && TREE_DEPRECATED (id))
3444 if (deprecated_state != DEPRECATED_SUPPRESS)
3445 warn_deprecated_use (id);
3448 if (id == ridpointers[(int) RID_INT])
3450 if (id == ridpointers[(int) RID_CHAR])
3453 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3455 enum rid i = C_RID_CODE (id);
3456 if ((int) i <= (int) RID_LAST_MODIFIER)
3458 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3461 error ("`long long long' is too long for GCC");
3464 if (pedantic && !flag_isoc99 && ! in_system_header
3466 pedwarn ("ISO C90 does not support `long long'");
3470 else if (specbits & (1 << (int) i))
3472 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3474 if (pedantic && !flag_isoc99)
3475 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3478 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3481 /* Diagnose "__thread extern". Recall that this list
3482 is in the reverse order seen in the text. */
3484 && (specbits & (1 << (int) RID_EXTERN
3485 | 1 << (int) RID_STATIC)))
3487 if (specbits & 1 << (int) RID_EXTERN)
3488 error ("`__thread' before `extern'");
3490 error ("`__thread' before `static'");
3493 specbits |= 1 << (int) i;
3498 error ("two or more data types in declaration of `%s'", name);
3499 /* Actual typedefs come to us as TYPE_DECL nodes. */
3500 else if (TREE_CODE (id) == TYPE_DECL)
3502 if (TREE_TYPE (id) == error_mark_node)
3503 ; /* Allow the type to default to int to avoid cascading errors. */
3506 type = TREE_TYPE (id);
3507 decl_attr = DECL_ATTRIBUTES (id);
3511 /* Built-in types come as identifiers. */
3512 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3514 tree t = lookup_name (id);
3515 if (!t || TREE_CODE (t) != TYPE_DECL)
3516 error ("`%s' fails to be a typedef or built in type",
3517 IDENTIFIER_POINTER (id));
3518 else if (TREE_TYPE (t) == error_mark_node)
3522 type = TREE_TYPE (t);
3526 else if (TREE_CODE (id) != ERROR_MARK)
3533 typedef_type = type;
3535 size_varies = C_TYPE_VARIABLE_SIZE (type);
3537 /* No type at all: default to `int', and set DEFAULTED_INT
3538 because it was not a user-defined typedef. */
3542 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3543 | (1 << (int) RID_SIGNED)
3544 | (1 << (int) RID_UNSIGNED)
3545 | (1 << (int) RID_COMPLEX))))
3546 /* Don't warn about typedef foo = bar. */
3547 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3548 && ! in_system_header)
3550 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3551 and this is a function, or if -Wimplicit; prefer the former
3552 warning since it is more explicit. */
3553 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3555 warn_about_return_type = 1;
3556 else if (warn_implicit_int || flag_isoc99)
3557 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3562 type = integer_type_node;
3565 /* Now process the modifiers that were specified
3566 and check for invalid combinations. */
3568 /* Long double is a special combination. */
3570 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3571 && TYPE_MAIN_VARIANT (type) == double_type_node)
3573 specbits &= ~(1 << (int) RID_LONG);
3574 type = long_double_type_node;
3577 /* Check all other uses of type modifiers. */
3579 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3580 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3584 if ((specbits & 1 << (int) RID_LONG)
3585 && (specbits & 1 << (int) RID_SHORT))
3586 error ("both long and short specified for `%s'", name);
3587 else if (((specbits & 1 << (int) RID_LONG)
3588 || (specbits & 1 << (int) RID_SHORT))
3590 error ("long or short specified with char for `%s'", name);
3591 else if (((specbits & 1 << (int) RID_LONG)
3592 || (specbits & 1 << (int) RID_SHORT))
3593 && TREE_CODE (type) == REAL_TYPE)
3595 static int already = 0;
3597 error ("long or short specified with floating type for `%s'", name);
3598 if (! already && ! pedantic)
3600 error ("the only valid combination is `long double'");
3604 else if ((specbits & 1 << (int) RID_SIGNED)
3605 && (specbits & 1 << (int) RID_UNSIGNED))
3606 error ("both signed and unsigned specified for `%s'", name);
3607 else if (TREE_CODE (type) != INTEGER_TYPE)
3608 error ("long, short, signed or unsigned invalid for `%s'", name);
3612 if (!explicit_int && !defaulted_int && !explicit_char)
3614 error ("long, short, signed or unsigned used invalidly for `%s'",
3620 /* Discard the type modifiers if they are invalid. */
3623 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3624 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3629 if ((specbits & (1 << (int) RID_COMPLEX))
3630 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3632 error ("complex invalid for `%s'", name);
3633 specbits &= ~(1 << (int) RID_COMPLEX);
3636 /* Decide whether an integer type is signed or not.
3637 Optionally treat bit-fields as signed by default. */
3638 if (specbits & 1 << (int) RID_UNSIGNED
3639 || (bitfield && ! flag_signed_bitfields
3640 && (explicit_int || defaulted_int || explicit_char
3641 /* A typedef for plain `int' without `signed'
3642 can be controlled just like plain `int'. */
3643 || ! (typedef_decl != 0
3644 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3645 && TREE_CODE (type) != ENUMERAL_TYPE
3646 && !(specbits & 1 << (int) RID_SIGNED)))
3649 type = long_long_unsigned_type_node;
3650 else if (specbits & 1 << (int) RID_LONG)
3651 type = long_unsigned_type_node;
3652 else if (specbits & 1 << (int) RID_SHORT)
3653 type = short_unsigned_type_node;
3654 else if (type == char_type_node)
3655 type = unsigned_char_type_node;
3656 else if (typedef_decl)
3657 type = c_common_unsigned_type (type);
3659 type = unsigned_type_node;
3661 else if ((specbits & 1 << (int) RID_SIGNED)
3662 && type == char_type_node)
3663 type = signed_char_type_node;
3665 type = long_long_integer_type_node;
3666 else if (specbits & 1 << (int) RID_LONG)
3667 type = long_integer_type_node;
3668 else if (specbits & 1 << (int) RID_SHORT)
3669 type = short_integer_type_node;
3671 if (specbits & 1 << (int) RID_COMPLEX)
3673 if (pedantic && !flag_isoc99)
3674 pedwarn ("ISO C90 does not support complex types");
3675 /* If we just have "complex", it is equivalent to
3676 "complex double", but if any modifiers at all are specified it is
3677 the complex form of TYPE. E.g, "complex short" is
3678 "complex short int". */
3680 if (defaulted_int && ! longlong
3681 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3682 | (1 << (int) RID_SIGNED)
3683 | (1 << (int) RID_UNSIGNED))))
3686 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3687 type = complex_double_type_node;
3689 else if (type == integer_type_node)
3692 pedwarn ("ISO C does not support complex integer types");
3693 type = complex_integer_type_node;
3695 else if (type == float_type_node)
3696 type = complex_float_type_node;
3697 else if (type == double_type_node)
3698 type = complex_double_type_node;
3699 else if (type == long_double_type_node)
3700 type = complex_long_double_type_node;
3704 pedwarn ("ISO C does not support complex integer types");
3705 type = build_complex_type (type);
3709 /* Check the type and width of a bit-field. */
3711 check_bitfield_type_and_width (&type, width, orig_name);
3713 /* Figure out the type qualifiers for the declaration. There are
3714 two ways a declaration can become qualified. One is something
3715 like `const int i' where the `const' is explicit. Another is
3716 something like `typedef const int CI; CI i' where the type of the
3717 declaration contains the `const'. A third possibility is that
3718 there is a type qualifier on the element type of a typedefed
3719 array type, in which case we should extract that qualifier so
3720 that c_apply_type_quals_to_decls receives the full list of
3721 qualifiers to work with (C90 is not entirely clear about whether
3722 duplicate qualifiers should be diagnosed in this case, but it
3723 seems most appropriate to do so). */
3724 element_type = strip_array_types (type);
3725 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3727 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3729 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3730 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3731 if (pedantic && !flag_isoc99)
3734 pedwarn ("duplicate `const'");
3736 pedwarn ("duplicate `restrict'");
3738 pedwarn ("duplicate `volatile'");
3740 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3741 type = TYPE_MAIN_VARIANT (type);
3742 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3743 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3744 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3746 /* Warn if two storage classes are given. Default to `auto'. */
3751 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3752 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3753 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3754 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3755 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3757 /* "static __thread" and "extern __thread" are allowed. */
3758 if ((specbits & (1 << (int) RID_THREAD
3759 | 1 << (int) RID_STATIC
3760 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3763 /* Warn about storage classes that are invalid for certain
3764 kinds of declarations (parameters, typenames, etc.). */
3767 error ("multiple storage classes in declaration of `%s'", name);
3768 else if (funcdef_flag
3770 & ((1 << (int) RID_REGISTER)
3771 | (1 << (int) RID_AUTO)
3772 | (1 << (int) RID_TYPEDEF)
3773 | (1 << (int) RID_THREAD))))
3775 if (specbits & 1 << (int) RID_AUTO
3776 && (pedantic || current_scope == file_scope))
3777 pedwarn ("function definition declared `auto'");
3778 if (specbits & 1 << (int) RID_REGISTER)
3779 error ("function definition declared `register'");
3780 if (specbits & 1 << (int) RID_TYPEDEF)
3781 error ("function definition declared `typedef'");
3782 if (specbits & 1 << (int) RID_THREAD)
3783 error ("function definition declared `__thread'");
3784 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3785 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3787 else if (decl_context != NORMAL && nclasses > 0)
3789 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3793 switch (decl_context)
3796 error ("storage class specified for structure field `%s'",
3800 error ("storage class specified for parameter `%s'", name);
3803 error ("storage class specified for typename");
3806 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3807 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3808 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3811 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3813 /* `extern' with initialization is invalid if not at file scope. */
3814 if (current_scope == file_scope)
3815 warning ("`%s' initialized and declared `extern'", name);
3817 error ("`%s' has both `extern' and initializer", name);
3819 else if (current_scope == file_scope)
3821 if (specbits & 1 << (int) RID_AUTO)
3822 error ("file-scope declaration of `%s' specifies `auto'", name);
3826 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3827 error ("nested function `%s' declared `extern'", name);
3828 else if ((specbits & (1 << (int) RID_THREAD
3829 | 1 << (int) RID_EXTERN
3830 | 1 << (int) RID_STATIC))
3831 == (1 << (int) RID_THREAD))
3833 error ("function-scope `%s' implicitly auto and declared `__thread'",
3835 specbits &= ~(1 << (int) RID_THREAD);
3840 /* Now figure out the structure of the declarator proper.
3841 Descend through it, creating more complex types, until we reach
3842 the declared identifier (or NULL_TREE, in an absolute declarator). */
3844 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3846 if (type == error_mark_node)
3848 declarator = TREE_OPERAND (declarator, 0);
3852 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3853 an INDIRECT_REF (for *...),
3854 a CALL_EXPR (for ...(...)),
3855 a TREE_LIST (for nested attributes),
3856 an identifier (for the name being declared)
3857 or a null pointer (for the place in an absolute declarator
3858 where the name was omitted).
3859 For the last two cases, we have just exited the loop.
3861 At this point, TYPE is the type of elements of an array,
3862 or for a function to return, or for a pointer to point to.
3863 After this sequence of ifs, TYPE is the type of the
3864 array or function or pointer, and DECLARATOR has had its
3865 outermost layer removed. */
3867 if (array_ptr_quals != NULL_TREE || array_parm_static)
3869 /* Only the innermost declarator (making a parameter be of
3870 array type which is converted to pointer type)
3871 may have static or type qualifiers. */
3872 error ("static or type qualifiers in non-parameter array declarator");
3873 array_ptr_quals = NULL_TREE;
3874 array_parm_static = 0;
3877 if (TREE_CODE (declarator) == TREE_LIST)
3879 /* We encode a declarator with embedded attributes using
3881 tree attrs = TREE_PURPOSE (declarator);
3884 declarator = TREE_VALUE (declarator);
3885 inner_decl = declarator;
3886 while (inner_decl != NULL_TREE
3887 && TREE_CODE (inner_decl) == TREE_LIST)
3888 inner_decl = TREE_VALUE (inner_decl);
3889 if (inner_decl == NULL_TREE
3890 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3891 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3892 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3893 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3894 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3895 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3896 returned_attrs = decl_attributes (&type,
3897 chainon (returned_attrs, attrs),
3900 else if (TREE_CODE (declarator) == ARRAY_REF)
3902 tree itype = NULL_TREE;
3903 tree size = TREE_OPERAND (declarator, 1);
3904 /* The index is a signed object `sizetype' bits wide. */
3905 tree index_type = c_common_signed_type (sizetype);
3907 array_ptr_quals = TREE_TYPE (declarator);
3908 array_parm_static = TREE_STATIC (declarator);
3910 declarator = TREE_OPERAND (declarator, 0);
3912 /* Check for some types that there cannot be arrays of. */
3914 if (VOID_TYPE_P (type))
3916 error ("declaration of `%s' as array of voids", name);
3917 type = error_mark_node;
3920 if (TREE_CODE (type) == FUNCTION_TYPE)
3922 error ("declaration of `%s' as array of functions", name);
3923 type = error_mark_node;
3926 if (pedantic && flexible_array_type_p (type))
3927 pedwarn ("invalid use of structure with flexible array member");
3929 if (size == error_mark_node)
3930 type = error_mark_node;
3932 if (type == error_mark_node)
3935 /* If size was specified, set ITYPE to a range-type for that size.
3936 Otherwise, ITYPE remains null. finish_decl may figure it out
3937 from an initial value. */
3941 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3942 STRIP_TYPE_NOPS (size);
3944 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3946 error ("size of array `%s' has non-integer type", name);
3947 size = integer_one_node;
3950 if (pedantic && integer_zerop (size))
3951 pedwarn ("ISO C forbids zero-size array `%s'", name);
3953 if (TREE_CODE (size) == INTEGER_CST)
3955 constant_expression_warning (size);
3956 if (tree_int_cst_sgn (size) < 0)
3958 error ("size of array `%s' is negative", name);
3959 size = integer_one_node;
3964 /* Make sure the array size remains visibly nonconstant
3965 even if it is (eg) a const variable with known value. */
3968 if (!flag_isoc99 && pedantic)
3970 if (TREE_CONSTANT (size))
3971 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3974 pedwarn ("ISO C90 forbids variable-size array `%s'",
3979 if (integer_zerop (size))
3981 /* A zero-length array cannot be represented with an
3982 unsigned index type, which is what we'll get with
3983 build_index_type. Create an open-ended range instead. */
3984 itype = build_range_type (sizetype, size, NULL_TREE);
3988 /* Compute the maximum valid index, that is, size - 1.
3989 Do the calculation in index_type, so that if it is
3990 a variable the computations will be done in the
3992 itype = fold (build (MINUS_EXPR, index_type,
3993 convert (index_type, size),
3994 convert (index_type, size_one_node)));
3996 /* If that overflowed, the array is too big.
3997 ??? While a size of INT_MAX+1 technically shouldn't
3998 cause an overflow (because we subtract 1), the overflow
3999 is recorded during the conversion to index_type, before
4000 the subtraction. Handling this case seems like an
4001 unnecessary complication. */
4002 if (TREE_OVERFLOW (itype))
4004 error ("size of array `%s' is too large", name);
4005 type = error_mark_node;
4011 /* We must be able to distinguish the
4012 SAVE_EXPR_CONTEXT for the variably-sized type
4013 so that we can set it correctly in
4014 set_save_expr_context. The convention is
4015 that all SAVE_EXPRs that need to be reset
4016 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4017 tree cfd = current_function_decl;
4018 if (decl_context == PARM)
4019 current_function_decl = NULL_TREE;
4020 itype = variable_size (itype);
4021 if (decl_context == PARM)
4022 current_function_decl = cfd;
4024 itype = build_index_type (itype);
4027 else if (decl_context == FIELD)
4029 if (pedantic && !flag_isoc99 && !in_system_header)
4030 pedwarn ("ISO C90 does not support flexible array members");
4032 /* ISO C99 Flexible array members are effectively identical
4033 to GCC's zero-length array extension. */
4034 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4037 /* If pedantic, complain about arrays of incomplete types. */
4039 if (pedantic && !COMPLETE_TYPE_P (type))
4040 pedwarn ("array type has incomplete element type");
4042 /* Build the array type itself, then merge any constancy or
4043 volatility into the target type. We must do it in this order
4044 to ensure that the TYPE_MAIN_VARIANT field of the array type
4045 is set correctly. */
4047 type = build_array_type (type, itype);
4049 type = c_build_qualified_type (type, type_quals);
4052 C_TYPE_VARIABLE_SIZE (type) = 1;
4054 /* The GCC extension for zero-length arrays differs from
4055 ISO flexible array members in that sizeof yields zero. */
4056 if (size && integer_zerop (size))
4059 TYPE_SIZE (type) = bitsize_zero_node;
4060 TYPE_SIZE_UNIT (type) = size_zero_node;
4062 if (decl_context != PARM
4063 && (array_ptr_quals != NULL_TREE || array_parm_static))
4065 error ("static or type qualifiers in non-parameter array declarator");
4066 array_ptr_quals = NULL_TREE;
4067 array_parm_static = 0;
4070 else if (TREE_CODE (declarator) == CALL_EXPR)
4072 /* Say it's a definition only for the CALL_EXPR closest to
4074 bool really_funcdef = (funcdef_flag
4075 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4076 == IDENTIFIER_NODE));
4079 /* Declaring a function type.
4080 Make sure we have a valid type for the function to return. */
4081 if (type == error_mark_node)