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 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 BITFIELD, /* Likewise but with specified width */
65 TYPENAME}; /* Typename (inside cast or sizeof) */
68 /* Nonzero if we have seen an invalid cross reference
69 to a struct, union, or enum, but not yet printed the message. */
71 tree pending_invalid_xref;
72 /* File and line to appear in the eventual error message. */
73 location_t pending_invalid_xref_location;
75 /* While defining an enum type, this is 1 plus the last enumerator
76 constant value. Note that will do not have to save this or `enum_overflow'
77 around nested function definition since such a definition could only
78 occur in an enum value expression and we don't use these variables in
81 static tree enum_next_value;
83 /* Nonzero means that there was overflow computing enum_next_value. */
85 static int enum_overflow;
87 /* Parsing a function declarator leaves a list of parameter names
88 or a chain of parameter decls here. */
90 static tree last_function_parms;
92 /* ... and a chain of structure and enum types declared in the
95 static tree last_function_parm_tags;
97 /* ... and a chain of all non-parameter declarations (such as
98 CONST_DECLs from enumerations) here. */
100 static tree last_function_parm_others;
102 /* After parsing the declarator that starts a function definition,
103 `start_function' puts the list of parameter names or chain of decls here
104 for `store_parm_decls' to find. */
106 static tree current_function_parms;
108 /* Similar, for last_function_parm_tags. */
110 static tree current_function_parm_tags;
112 /* And for last_function_parm_others. */
114 static tree current_function_parm_others;
116 /* Similar, for the file and line that the prototype came from if this is
117 an old-style definition. */
119 static location_t current_function_prototype_locus;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
125 /* The current scope statement stack. */
127 static GTY(()) tree c_scope_stmt_stack;
129 /* A list of external DECLs that appeared at block scope when there was
130 some other global meaning for that identifier. */
131 static GTY(()) tree truly_local_externals;
133 /* All the builtins; this is a subset of the entries of global_scope. */
135 static GTY(()) tree first_builtin_decl;
136 static GTY(()) tree last_builtin_decl;
138 /* A DECL for the current file-scope context. */
140 static GTY(()) tree current_file_decl;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement that specifies a return value is seen. */
145 int current_function_returns_value;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement with no argument is seen. */
150 int current_function_returns_null;
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a call to a noreturn function is seen. */
155 int current_function_returns_abnormally;
157 /* Set to nonzero by `grokdeclarator' for a function
158 whose return type is defaulted, if warnings for this are desired. */
160 static int warn_about_return_type;
162 /* Nonzero when starting a function declared `extern inline'. */
164 static int current_extern_inline;
166 /* Each c_scope structure describes the complete contents of one scope.
167 Three scopes are distinguished specially: the innermost or current
168 scope, the innermost function scope, and the outermost or file scope.
170 Most declarations are recorded in the current scope.
172 All normal label declarations are recorded in the innermost
173 function scope, as are bindings of undeclared identifiers to
174 error_mark_node. (GCC permits nested functions as an extension,
175 hence the 'innermost' qualifier.) Explicitly declared labels
176 (using the __label__ extension) appear in the current scope.
178 Being in the global scope (current_scope == global_scope) causes
179 special behavior in several places below. Also, under some
180 conditions the Objective-C front end records declarations in the
181 global scope even though that isn't the current scope.
183 The order of the names, parms, and blocks lists matters, and they
184 are frequently appended to. To avoid having to walk all the way to
185 the end of the list on each insertion, or reverse the lists later,
186 we maintain a pointer to the last list entry for each of the lists.
188 The order of the tags, shadowed, and shadowed_tags
189 lists does not matter, so we just prepend to these lists. */
191 struct c_scope GTY(())
193 /* The scope containing this one. */
194 struct c_scope *outer;
196 /* The next outermost function scope. */
197 struct c_scope *outer_function;
199 /* All variables, constants, functions, labels, and typedef names. */
203 /* All parameter declarations. Used only in the outermost scope of
208 /* All structure, union, and enum type tags. */
211 /* For each scope, a list of shadowed outer-scope definitions
212 to be restored when this scope is popped.
213 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
214 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
217 /* For each scope, a list of shadowed outer-scope tag definitions
218 to be restored when this scope is popped.
219 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
220 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
223 /* For each scope (except the global one), a chain of BLOCK nodes
224 for all the scopes that were entered and exited one level down. */
228 /* True if we are currently filling this scope with parameter
232 /* True if we already complained about forward parameter decls
233 in this scope. This prevents double warnings on
234 foo (int a; int b; ...) */
235 bool warned_forward_parm_decls : 1;
237 /* True if this is the outermost block scope of a function body.
238 This scope contains the parameters, the local variables declared
239 in the outermost block, and all the labels (except those in
240 nested functions, or declared at block scope with __label__). */
241 bool function_body : 1;
243 /* True means make a BLOCK for this scope no matter what. */
247 /* The scope currently in effect. */
249 static GTY(()) struct c_scope *current_scope;
251 /* A chain of c_scope structures awaiting reuse. */
253 static GTY((deletable (""))) struct c_scope *scope_freelist;
255 /* The innermost function scope. Ordinary (not explicitly declared)
256 labels, bindings to error_mark_node, and the lazily-created
257 bindings of __func__ and its friends get this scope. */
259 static GTY(()) struct c_scope *current_function_scope;
261 /* The outermost scope, corresponding to the C "file scope". This is
262 created when the compiler is started and exists through the entire run. */
264 static GTY(()) struct c_scope *global_scope;
266 /* Append VAR to LIST in scope SCOPE. */
267 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
268 struct c_scope *s_ = (scope); \
270 if (s_->list##_last) \
271 TREE_CHAIN (s_->list##_last) = d_; \
274 s_->list##_last = d_; \
277 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
278 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
279 struct c_scope *t_ = (tscope); \
280 struct c_scope *f_ = (fscope); \
282 TREE_CHAIN (t_->to##_last) = f_->from; \
285 t_->to##_last = f_->from##_last; \
288 /* True means unconditionally make a BLOCK for the next scope pushed. */
290 static bool keep_next_level_flag;
292 /* True means the next call to pushlevel will be the outermost scope
293 of a function body, so do not push a new scope, merely cease
294 expecting parameter decls. */
296 static bool next_is_function_body;
298 /* Functions called automatically at the beginning and end of execution. */
300 tree static_ctors, static_dtors;
302 /* Forward declarations. */
304 static struct c_scope *make_scope (void);
305 static void pop_scope (void);
306 static tree match_builtin_function_types (tree, tree);
307 static int duplicate_decls (tree, tree, int, int);
308 static int redeclaration_error_message (tree, tree);
309 static tree make_label (tree, location_t);
310 static void bind_label (tree, tree, struct c_scope *);
311 static void implicit_decl_warning (tree);
312 static tree lookup_tag (enum tree_code, tree, int);
313 static tree lookup_name_current_level (tree);
314 static tree grokdeclarator (tree, tree, enum decl_context, int);
315 static tree grokparms (tree, int);
316 static void layout_array_type (tree);
317 static void store_parm_decls_newstyle (void);
318 static void store_parm_decls_oldstyle (void);
319 static tree c_make_fname_decl (tree, int);
320 static void c_expand_body_1 (tree, int);
321 static tree any_external_decl (tree);
322 static void record_external_decl (tree);
323 static void warn_if_shadowing (tree, tree);
324 static void clone_underlying_type (tree);
325 static bool flexible_array_type_p (tree);
326 static hashval_t link_hash_hash (const void *);
327 static int link_hash_eq (const void *, const void *);
329 /* States indicating how grokdeclarator() should handle declspecs marked
330 with __attribute__((deprecated)). An object declared as
331 __attribute__((deprecated)) suppresses warnings of uses of other
334 enum deprecated_states {
339 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
342 c_print_identifier (FILE *file, tree node, int indent)
344 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
345 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
346 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
347 if (C_IS_RESERVED_WORD (node))
349 tree rid = ridpointers[C_RID_CODE (node)];
350 indent_to (file, indent + 4);
351 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
352 (void *) rid, IDENTIFIER_POINTER (rid));
356 /* Hook called at end of compilation to assume 1 elt
357 for a file-scope tentative array defn that wasn't complete before. */
360 c_finish_incomplete_decl (tree decl)
362 if (TREE_CODE (decl) == VAR_DECL)
364 tree type = TREE_TYPE (decl);
365 if (type != error_mark_node
366 && TREE_CODE (type) == ARRAY_TYPE
367 && ! DECL_EXTERNAL (decl)
368 && TYPE_DOMAIN (type) == 0)
370 warning ("%Harray '%D' assumed to have one element",
371 &DECL_SOURCE_LOCATION (decl), decl);
373 complete_array_type (type, NULL_TREE, 1);
375 layout_decl (decl, 0);
380 /* Reuse or create a struct for this scope. */
382 static struct c_scope *
385 struct c_scope *result;
388 result = scope_freelist;
389 scope_freelist = result->outer;
392 result = ggc_alloc_cleared (sizeof (struct c_scope));
397 /* Remove the topmost scope from the stack and add it to the
398 free list, updating current_function_scope if necessary. */
403 struct c_scope *scope = current_scope;
405 current_scope = scope->outer;
406 if (scope->function_body)
407 current_function_scope = scope->outer_function;
409 memset (scope, 0, sizeof (struct c_scope));
410 scope->outer = scope_freelist;
411 scope_freelist = scope;
414 /* Nonzero if we are currently in the global scope. */
417 global_bindings_p (void)
419 return current_scope == global_scope;
423 keep_next_level (void)
425 keep_next_level_flag = true;
428 /* Identify this scope as currently being filled with parameters. */
431 declare_parm_level (void)
433 current_scope->parm_flag = true;
436 /* Nonzero if currently making parm declarations. */
439 in_parm_level_p (void)
441 return current_scope->parm_flag;
444 /* Enter a new scope. The dummy parameter is for signature
445 compatibility with lang_hooks.decls.pushlevel. */
448 pushlevel (int dummy ATTRIBUTE_UNUSED)
450 if (next_is_function_body)
452 /* This is the transition from the parameters to the top level
453 of the function body. These are the same scope
454 (C99 6.2.1p4,6) so we do not push another scope structure.
455 next_is_function_body is set only by store_parm_decls, which
456 in turn is called when and only when we are about to
457 encounter the opening curly brace for the function body.
459 The outermost block of a function always gets a BLOCK node,
460 because the debugging output routines expect that each
461 function has at least one BLOCK. */
462 current_scope->parm_flag = false;
463 current_scope->function_body = true;
464 current_scope->keep = true;
465 current_scope->outer_function = current_function_scope;
466 current_function_scope = current_scope;
468 keep_next_level_flag = false;
469 next_is_function_body = false;
473 struct c_scope *scope = make_scope ();
475 scope->keep = keep_next_level_flag;
476 scope->outer = current_scope;
477 current_scope = scope;
478 keep_next_level_flag = false;
482 /* Exit a scope. Restore the state of the identifier-decl mappings
483 that were in effect when this scope was entered.
485 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
486 create a BLOCK node to record its declarations and subblocks for
487 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
488 or tags lists are nonempty.
490 The second parameter is ignored; it is present only for
491 signature compatibility with lang_hooks.decls.poplevel.
493 If FUNCTIONBODY is nonzero, this level is the body of a function,
494 even if current_scope->function_body is not set. This is used
495 by language-independent code that generates synthetic functions,
496 and cannot set current_scope->function_body.
498 FIXME: Eliminate the need for all arguments. */
501 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
503 struct c_scope *scope = current_scope;
508 scope->function_body |= functionbody;
510 if (keep == KEEP_MAYBE)
511 keep = (scope->names || scope->tags);
514 keep |= scope->function_body;
516 /* If appropriate, create a BLOCK to record the decls for the life
521 block = make_node (BLOCK);
522 BLOCK_VARS (block) = scope->names;
523 BLOCK_SUBBLOCKS (block) = scope->blocks;
524 TREE_USED (block) = 1;
527 /* In each subblock, record that this is its superior. */
528 for (p = scope->blocks; p; p = TREE_CHAIN (p))
529 BLOCK_SUPERCONTEXT (p) = block;
531 /* Clear out the variable bindings in this scope.
533 Propagate TREE_ADDRESSABLE from nested functions to their
534 containing functions.
536 Issue warnings for unused variables and labels, and errors for
537 undefined labels, if there are any. */
539 for (p = scope->names; p; p = TREE_CHAIN (p))
541 const location_t *locus = &DECL_SOURCE_LOCATION (p);
543 switch (TREE_CODE (p))
546 if (TREE_USED (p) && !DECL_INITIAL (p))
548 error ("%Hlabel `%D' used but not defined", locus, p);
549 DECL_INITIAL (p) = error_mark_node;
551 else if (!TREE_USED (p) && warn_unused_label)
553 if (DECL_INITIAL (p))
554 warning ("%Hlabel `%D' defined but not used", locus, p);
556 warning ("%Hlabel `%D' declared but not defined", locus, p);
559 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
563 if (! TREE_ASM_WRITTEN (p)
564 && DECL_INITIAL (p) != 0
565 && TREE_ADDRESSABLE (p)
566 && DECL_ABSTRACT_ORIGIN (p) != 0
567 && DECL_ABSTRACT_ORIGIN (p) != p)
568 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
572 /* Keep this in sync with stmt.c:warn_about_unused_variables.
573 No warnings when the global scope is popped because the
574 global scope isn't popped for the last translation unit,
575 so the warnings are done in c_write_global_declaration. */
576 if (warn_unused_variable && scope != global_scope
578 && !DECL_IN_SYSTEM_HEADER (p)
580 && !DECL_ARTIFICIAL (p))
581 warning ("%Hunused variable `%D'", locus, p);
588 if (DECL_EXTERNAL (p) && scope != global_scope)
589 /* External decls stay in the symbol-value slot but are
591 C_DECL_INVISIBLE (p) = 1;
593 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
599 /* Clear out the parameter bindings in this scope, if any.
600 Unused-parameter warnings are handled by function.c. */
601 for (p = scope->parms; p; p = TREE_CHAIN (p))
603 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
605 /* Clear out the tag-meanings declared in this scope.
607 Set the TYPE_CONTEXTs for all of the tagged types belonging to
608 this scope so that they point to the appropriate construct, i.e.
609 either to the current FUNCTION_DECL node, or else to the BLOCK
610 node we just constructed.
612 Note that for tagged types whose scope is just the formal
613 parameter list for some function type specification, we can't
614 properly set their TYPE_CONTEXTs here, because we don't have a
615 pointer to the appropriate FUNCTION_TYPE node readily available
616 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
617 type nodes get set in `grokdeclarator' as soon as we have created
618 the FUNCTION_TYPE node which will represent the "scope" for these
619 "parameter list local" tagged types. */
621 decl = scope->function_body ? current_function_decl : block;
622 for (p = scope->tags; p; p = TREE_CHAIN (p))
624 if (TREE_PURPOSE (p))
625 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
627 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
630 /* Restore all name- and label-meanings from outer scopes that were
631 shadowed by this scope. */
632 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
633 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
634 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
636 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
638 /* Restore all tag-meanings from outer scopes that were shadowed by
640 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
641 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
643 /* Dispose of the block that we just made inside some higher level. */
644 if (scope->function_body)
645 DECL_INITIAL (current_function_decl) = block;
646 else if (scope->outer)
649 SCOPE_LIST_APPEND (scope->outer, blocks, block);
650 /* If we did not make a block for the scope just exited, any
651 blocks made for inner scopes must be carried forward so they
652 will later become subblocks of something else. */
653 else if (scope->blocks)
654 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
657 /* Pop the current scope, and free the structure for reuse. */
663 /* Insert BLOCK at the end of the list of subblocks of the current
664 scope. This is used when a BIND_EXPR is expanded, to handle the
665 BLOCK node inside the BIND_EXPR. */
668 insert_block (tree block)
670 TREE_USED (block) = 1;
671 SCOPE_LIST_APPEND (current_scope, blocks, block);
674 /* Set the BLOCK node for the innermost scope (the one we are
675 currently in). The RTL expansion machinery requires us to provide
676 this hook, but it is not useful in function-at-a-time mode. */
679 set_block (tree block ATTRIBUTE_UNUSED)
683 /* Push a definition or a declaration of struct, union or enum tag "name".
684 "type" should be the type node.
685 We assume that the tag "name" is not already defined.
687 Note that the definition may really be just a forward reference.
688 In that case, the TYPE_SIZE will be zero. */
691 pushtag (tree name, tree type)
693 struct c_scope *b = current_scope;
695 /* Record the identifier as the type's name if it has none. */
698 if (TYPE_NAME (type) == 0)
699 TYPE_NAME (type) = name;
701 if (IDENTIFIER_TAG_VALUE (name))
702 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
704 IDENTIFIER_TAG_VALUE (name) = type;
707 b->tags = tree_cons (name, type, b->tags);
709 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
710 tagged type we just added to the current scope. This fake
711 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
712 to output a representation of a tagged type, and it also gives
713 us a convenient place to record the "scope start" address for the
716 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
718 /* An approximation for now, so we can tell this is a function-scope tag.
719 This will be updated in poplevel. */
720 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
723 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
724 and argument types provided that the type modes match. This function
725 return a unified type given a suitable match, and 0 otherwise. */
728 match_builtin_function_types (tree oldtype, tree newtype)
730 tree newrettype, oldrettype;
731 tree newargs, oldargs;
732 tree trytype, tryargs;
734 /* Accept the return type of the new declaration if same modes. */
735 oldrettype = TREE_TYPE (oldtype);
736 newrettype = TREE_TYPE (newtype);
738 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
741 oldargs = TYPE_ARG_TYPES (oldtype);
742 newargs = TYPE_ARG_TYPES (newtype);
745 while (oldargs || newargs)
749 || ! TREE_VALUE (oldargs)
750 || ! TREE_VALUE (newargs)
751 || TYPE_MODE (TREE_VALUE (oldargs))
752 != TYPE_MODE (TREE_VALUE (newargs)))
755 oldargs = TREE_CHAIN (oldargs);
756 newargs = TREE_CHAIN (newargs);
759 trytype = build_function_type (newrettype, tryargs);
760 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
763 /* Handle when a new declaration NEWDECL
764 has the same name as an old one OLDDECL
765 in the same binding contour.
766 Prints an error message if appropriate.
768 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
771 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
772 and OLDDECL is in an outer scope and should thus not be changed. */
775 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
778 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
780 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
782 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
783 && DECL_INITIAL (newdecl) != 0);
784 tree oldtype = TREE_TYPE (olddecl);
785 tree newtype = TREE_TYPE (newdecl);
788 if (DECL_P (olddecl))
790 if (TREE_CODE (newdecl) == FUNCTION_DECL
791 && TREE_CODE (olddecl) == FUNCTION_DECL
792 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
794 if (DECL_DECLARED_INLINE_P (newdecl)
795 && DECL_UNINLINABLE (newdecl)
796 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
797 /* Already warned elsewhere. */;
798 else if (DECL_DECLARED_INLINE_P (olddecl)
799 && DECL_UNINLINABLE (olddecl)
800 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
801 /* Already warned. */;
802 else if (DECL_DECLARED_INLINE_P (newdecl)
803 && ! DECL_DECLARED_INLINE_P (olddecl)
804 && DECL_UNINLINABLE (olddecl)
805 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
807 warning ("%Hfunction '%D' redeclared as inline",
808 &DECL_SOURCE_LOCATION (newdecl), newdecl);
809 warning ("%Hprevious declaration of function '%D' "
810 "with attribute noinline",
811 &DECL_SOURCE_LOCATION (olddecl), olddecl);
813 else if (DECL_DECLARED_INLINE_P (olddecl)
814 && DECL_UNINLINABLE (newdecl)
815 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
817 warning ("%Hfunction '%D' redeclared with attribute noinline",
818 &DECL_SOURCE_LOCATION (newdecl), newdecl);
819 warning ("%Hprevious declaration of function '%D' was inline",
820 &DECL_SOURCE_LOCATION (olddecl), olddecl);
824 DECL_ATTRIBUTES (newdecl)
825 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
828 if (TREE_CODE (newtype) == ERROR_MARK
829 || TREE_CODE (oldtype) == ERROR_MARK)
832 /* New decl is completely inconsistent with the old one =>
833 tell caller to replace the old one.
834 This is always an error except in the case of shadowing a builtin. */
835 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
837 if (TREE_CODE (olddecl) == FUNCTION_DECL
838 && DECL_BUILT_IN (olddecl))
840 /* If you declare a built-in or predefined function name as static,
841 the old definition is overridden,
842 but optionally warn this was a bad choice of name. */
843 if (!TREE_PUBLIC (newdecl))
846 warning ("%Hshadowing built-in function '%D'",
847 &DECL_SOURCE_LOCATION (newdecl), newdecl);
850 warning ("%Hbuilt-in function '%D' declared as non-function",
851 &DECL_SOURCE_LOCATION (newdecl), newdecl);
855 error ("%H'%D' redeclared as different kind of symbol",
856 &DECL_SOURCE_LOCATION (newdecl), newdecl);
857 error ("%Hprevious declaration of '%D'",
858 &DECL_SOURCE_LOCATION (olddecl), olddecl);
864 /* For real parm decl following a forward decl, return 1 so old decl
865 will be reused. Only allow this to happen once. */
866 if (types_match && TREE_CODE (newdecl) == PARM_DECL
867 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
869 TREE_ASM_WRITTEN (olddecl) = 0;
873 /* The new declaration is the same kind of object as the old one.
874 The declarations may partially match. Print warnings if they don't
875 match enough. Ultimately, copy most of the information from the new
876 decl to the old one, and keep using the old one. */
878 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
880 /* A function declaration for a built-in function. */
881 if (!TREE_PUBLIC (newdecl))
883 /* If you declare a built-in function name as static, the
884 built-in definition is overridden,
885 but optionally warn this was a bad choice of name. */
887 warning ("%Hshadowing built-in function '%D'",
888 &DECL_SOURCE_LOCATION (newdecl), newdecl);
889 /* Discard the old built-in function. */
894 /* Accept harmless mismatch in function types.
895 This is for the ffs and fprintf builtins. */
896 tree trytype = match_builtin_function_types (oldtype, newtype);
900 types_match = comptypes (newtype, trytype, comptype_flags);
903 if (! different_binding_level)
904 TREE_TYPE (olddecl) = oldtype;
909 /* If types don't match for a built-in, throw away the built-in. */
910 warning ("%Hconflicting types for built-in function '%D'",
911 &DECL_SOURCE_LOCATION (newdecl), newdecl);
915 else if (TREE_CODE (olddecl) == FUNCTION_DECL
916 && DECL_SOURCE_LINE (olddecl) == 0)
918 /* A function declaration for a predeclared function
919 that isn't actually built in. */
920 if (!TREE_PUBLIC (newdecl))
922 /* If you declare it as static, the
923 default definition is overridden. */
926 else if (!types_match)
928 /* If the types don't match, preserve volatility indication.
929 Later on, we will discard everything else about the
930 default declaration. */
931 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
934 /* Permit char *foo () to match void *foo (...) if not pedantic,
935 if one of them came from a system header file. */
936 else if (!types_match
937 && TREE_CODE (olddecl) == FUNCTION_DECL
938 && TREE_CODE (newdecl) == FUNCTION_DECL
939 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
940 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
941 && (DECL_IN_SYSTEM_HEADER (olddecl)
942 || DECL_IN_SYSTEM_HEADER (newdecl))
943 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
944 && TYPE_ARG_TYPES (oldtype) == 0
945 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
946 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
948 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
949 && TYPE_ARG_TYPES (newtype) == 0
950 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
951 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
954 pedwarn ("%Hconflicting types for '%D'",
955 &DECL_SOURCE_LOCATION (newdecl), newdecl);
956 /* Make sure we keep void * as ret type, not char *. */
957 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
958 TREE_TYPE (newdecl) = newtype = oldtype;
960 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
961 we will come back here again. */
962 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
964 /* Permit void foo (...) to match int foo (...) if the latter is the
965 definition and implicit int was used. See c-torture/compile/920625-2.c. */
966 else if (!types_match && new_is_definition
967 && TREE_CODE (olddecl) == FUNCTION_DECL
968 && TREE_CODE (newdecl) == FUNCTION_DECL
969 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
970 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
971 && C_FUNCTION_IMPLICIT_INT (newdecl))
973 pedwarn ("%Hconflicting types for '%D'",
974 &DECL_SOURCE_LOCATION (newdecl), newdecl);
975 /* Make sure we keep void as the return type. */
976 TREE_TYPE (newdecl) = newtype = oldtype;
977 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
979 else if (!types_match
980 /* Permit char *foo (int, ...); followed by char *foo ();
982 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
984 /* Return types must still match. */
985 && comptypes (TREE_TYPE (oldtype),
986 TREE_TYPE (newtype), comptype_flags)
987 && TYPE_ARG_TYPES (newtype) == 0))
989 error ("%Hconflicting types for '%D'",
990 &DECL_SOURCE_LOCATION (newdecl), newdecl);
991 /* Check for function type mismatch
992 involving an empty arglist vs a nonempty one. */
993 if (TREE_CODE (olddecl) == FUNCTION_DECL
994 && comptypes (TREE_TYPE (oldtype),
995 TREE_TYPE (newtype), comptype_flags)
996 && ((TYPE_ARG_TYPES (oldtype) == 0
997 && DECL_INITIAL (olddecl) == 0)
999 (TYPE_ARG_TYPES (newtype) == 0
1000 && DECL_INITIAL (newdecl) == 0)))
1002 /* Classify the problem further. */
1003 tree t = TYPE_ARG_TYPES (oldtype);
1005 t = TYPE_ARG_TYPES (newtype);
1006 for (; t; t = TREE_CHAIN (t))
1008 tree type = TREE_VALUE (t);
1010 if (TREE_CHAIN (t) == 0
1011 && TYPE_MAIN_VARIANT (type) != void_type_node)
1013 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1017 if (c_type_promotes_to (type) != type)
1019 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1024 if (C_DECL_IMPLICIT (olddecl))
1025 error ("%Hprevious implicit declaration of '%D'",
1026 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1028 error ("%Hprevious declaration of '%D'",
1029 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1031 /* This is safer because the initializer might contain references
1032 to variables that were declared between olddecl and newdecl. This
1033 will make the initializer invalid for olddecl in case it gets
1034 assigned to olddecl below. */
1035 if (TREE_CODE (newdecl) == VAR_DECL)
1036 DECL_INITIAL (newdecl) = 0;
1038 /* TLS cannot follow non-TLS declaration. */
1039 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1040 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1042 error ("%Hthread-local declaration of '%D' follows non thread-local "
1043 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1044 error ("%Hprevious declaration of '%D'",
1045 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1047 /* non-TLS declaration cannot follow TLS declaration. */
1048 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1049 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1051 error ("%Hnon thread-local declaration of '%D' follows "
1052 "thread-local declaration",
1053 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1054 error ("%Hprevious declaration of '%D'",
1055 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1059 errmsg = redeclaration_error_message (newdecl, olddecl);
1062 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1066 error ("%Hredefinition of '%D'", locus, newdecl);
1069 error ("%Hredeclaration of '%D'", locus, newdecl);
1072 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1078 locus = &DECL_SOURCE_LOCATION (olddecl);
1079 if (DECL_INITIAL (olddecl)
1080 && current_scope == global_scope)
1081 error ("%H'%D' previously defined here", locus, olddecl);
1083 error ("%H'%D' previously declared here", locus, olddecl);
1086 else if (TREE_CODE (newdecl) == TYPE_DECL
1087 && (DECL_IN_SYSTEM_HEADER (olddecl)
1088 || DECL_IN_SYSTEM_HEADER (newdecl)))
1090 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1091 warning ("%Hredefinition of '%D'", locus, newdecl);
1092 locus = &DECL_SOURCE_LOCATION (olddecl);
1093 if (DECL_INITIAL (olddecl)
1094 && current_scope == global_scope)
1095 warning ("%H'%D' previously defined here", locus, olddecl);
1097 warning ("%H'%D' previously declared here", locus, olddecl);
1099 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1100 && DECL_INITIAL (olddecl) != 0
1101 && TYPE_ARG_TYPES (oldtype) == 0
1102 && TYPE_ARG_TYPES (newtype) != 0
1103 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1107 /* Prototype decl follows defn w/o prototype. */
1109 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1110 type = TYPE_ARG_TYPES (newtype),
1113 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1115 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1116 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1118 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1119 warning ("%Hprototype for '%D' follows", locus, newdecl);
1120 locus = &DECL_SOURCE_LOCATION (olddecl);
1121 warning ("%Hnon-prototype definition here", locus);
1124 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1125 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1127 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1128 error ("%Hprototype for '%D' follows and number of "
1129 "arguments doesn't match", locus, newdecl);
1130 locus = &DECL_SOURCE_LOCATION (olddecl);
1131 error ("%Hnon-prototype definition here", locus);
1135 /* Type for passing arg must be consistent
1136 with that declared for the arg. */
1137 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1140 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1141 error ("%Hprototype for '%D' follows and argument %d "
1142 "doesn't match", locus, newdecl, nargs);
1143 locus = &DECL_SOURCE_LOCATION (olddecl);
1144 error ("%Hnon-prototype definition here", locus);
1150 /* Warn about mismatches in various flags. */
1153 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1155 /* Warn if function is now inline
1156 but was previously declared not inline and has been called. */
1157 if (TREE_CODE (olddecl) == FUNCTION_DECL
1158 && ! DECL_DECLARED_INLINE_P (olddecl)
1159 && DECL_DECLARED_INLINE_P (newdecl)
1160 && TREE_USED (olddecl))
1161 warning ("%H'%D' declared inline after being called",
1163 if (TREE_CODE (olddecl) == FUNCTION_DECL
1164 && ! DECL_DECLARED_INLINE_P (olddecl)
1165 && DECL_DECLARED_INLINE_P (newdecl)
1166 && DECL_INITIAL (olddecl) != 0)
1167 warning ("%H'%D' declared inline after its definition",
1170 /* If pedantic, warn when static declaration follows a non-static
1171 declaration. Otherwise, do so only for functions. */
1172 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1173 && TREE_PUBLIC (olddecl)
1174 && !TREE_PUBLIC (newdecl))
1175 warning ("%Hstatic declaration for '%D' follows non-static",
1178 /* If warn_traditional, warn when a non-static function
1179 declaration follows a static one. */
1180 if (warn_traditional && !in_system_header
1181 && TREE_CODE (olddecl) == FUNCTION_DECL
1182 && !TREE_PUBLIC (olddecl)
1183 && TREE_PUBLIC (newdecl))
1184 warning ("%Hnon-static declaration for '%D' follows static",
1187 /* Warn when const declaration follows a non-const
1188 declaration, but not for functions. */
1189 if (TREE_CODE (olddecl) != FUNCTION_DECL
1190 && !TREE_READONLY (olddecl)
1191 && TREE_READONLY (newdecl))
1192 warning ("%Hconst declaration for '%D' follows non-const",
1194 /* These bits are logically part of the type, for variables.
1195 But not for functions
1196 (where qualifiers are not valid ANSI anyway). */
1197 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1198 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1199 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1200 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1201 "declaration", locus, newdecl);
1205 /* Optionally warn about more than one declaration for the same name. */
1206 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1207 /* Don't warn about a function declaration
1208 followed by a definition. */
1209 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1210 && DECL_INITIAL (olddecl) == 0)
1211 /* Don't warn about extern decl followed by (tentative) definition. */
1212 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1214 warning ("%Hredundant redeclaration of '%D' in same scope",
1215 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1216 warning ("%Hprevious declaration of '%D'",
1217 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1220 /* Copy all the DECL_... slots specified in the new decl
1221 except for any that we copy here from the old type.
1223 Past this point, we don't change OLDTYPE and NEWTYPE
1224 even if we change the types of NEWDECL and OLDDECL. */
1228 /* When copying info to olddecl, we store into write_olddecl
1229 instead. This allows us to avoid modifying olddecl when
1230 different_binding_level is true. */
1231 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1233 /* Merge the data types specified in the two decls. */
1234 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1236 if (different_binding_level)
1238 if (TYPE_ARG_TYPES (oldtype) != 0
1239 && TYPE_ARG_TYPES (newtype) == 0)
1240 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1243 = build_type_attribute_variant
1245 merge_attributes (TYPE_ATTRIBUTES (newtype),
1246 TYPE_ATTRIBUTES (oldtype)));
1250 = TREE_TYPE (olddecl)
1251 = common_type (newtype, oldtype);
1254 /* Lay the type out, unless already done. */
1255 if (oldtype != TREE_TYPE (newdecl))
1257 if (TREE_TYPE (newdecl) != error_mark_node)
1258 layout_type (TREE_TYPE (newdecl));
1259 if (TREE_CODE (newdecl) != FUNCTION_DECL
1260 && TREE_CODE (newdecl) != TYPE_DECL
1261 && TREE_CODE (newdecl) != CONST_DECL)
1262 layout_decl (newdecl, 0);
1266 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1267 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1268 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1269 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1270 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1271 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1273 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1274 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1278 /* Keep the old rtl since we can safely use it. */
1279 COPY_DECL_RTL (olddecl, newdecl);
1281 /* Merge the type qualifiers. */
1282 if (TREE_READONLY (newdecl))
1283 TREE_READONLY (write_olddecl) = 1;
1285 if (TREE_THIS_VOLATILE (newdecl))
1287 TREE_THIS_VOLATILE (write_olddecl) = 1;
1288 if (TREE_CODE (newdecl) == VAR_DECL
1289 /* If an automatic variable is re-declared in the same
1290 function scope, but the old declaration was not
1291 volatile, make_var_volatile() would crash because the
1292 variable would have been assigned to a pseudo, not a
1293 MEM. Since this duplicate declaration is invalid
1294 anyway, we just skip the call. */
1296 make_var_volatile (newdecl);
1299 /* Keep source location of definition rather than declaration. */
1300 /* When called with different_binding_level set, keep the old
1301 information so that meaningful diagnostics can be given. */
1302 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1303 && ! different_binding_level)
1305 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1306 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1309 /* Merge the unused-warning information. */
1310 if (DECL_IN_SYSTEM_HEADER (olddecl))
1311 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1312 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1313 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1315 /* Merge the initialization information. */
1316 /* When called with different_binding_level set, don't copy over
1317 DECL_INITIAL, so that we don't accidentally change function
1318 declarations into function definitions. */
1319 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1320 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1322 /* Merge the section attribute.
1323 We want to issue an error if the sections conflict but that must be
1324 done later in decl_attributes since we are called before attributes
1326 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1327 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1329 /* Copy the assembler name.
1330 Currently, it can only be defined in the prototype. */
1331 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1333 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1335 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1336 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1337 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1338 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1339 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1340 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1341 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1342 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1343 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1346 /* If cannot merge, then use the new type and qualifiers,
1347 and don't preserve the old rtl. */
1348 else if (! different_binding_level)
1350 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1351 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1352 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1353 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1356 /* Merge the storage class information. */
1357 merge_weak (newdecl, olddecl);
1359 /* For functions, static overrides non-static. */
1360 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1362 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1363 /* This is since we don't automatically
1364 copy the attributes of NEWDECL into OLDDECL. */
1365 /* No need to worry about different_binding_level here because
1366 then TREE_PUBLIC (newdecl) was true. */
1367 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1368 /* If this clears `static', clear it in the identifier too. */
1369 if (! TREE_PUBLIC (olddecl))
1370 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1372 if (DECL_EXTERNAL (newdecl))
1374 if (! different_binding_level || different_tu)
1376 /* Don't mess with these flags on local externs; they remain
1377 external even if there's a declaration at file scope which
1379 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1380 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1382 /* An extern decl does not override previous storage class. */
1383 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1384 if (! DECL_EXTERNAL (newdecl))
1386 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1387 /* If we have two non-EXTERNAL file-scope decls that are
1388 the same, only one of them should be written out. */
1390 TREE_ASM_WRITTEN (newdecl) = 1;
1395 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1396 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1399 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1401 /* If we're redefining a function previously defined as extern
1402 inline, make sure we emit debug info for the inline before we
1403 throw it away, in case it was inlined into a function that hasn't
1404 been written out yet. */
1405 if (new_is_definition && DECL_INITIAL (olddecl))
1407 if (TREE_USED (olddecl))
1408 (*debug_hooks->outlining_inline_function) (olddecl);
1410 /* The new defn must not be inline. */
1411 DECL_INLINE (newdecl) = 0;
1412 DECL_UNINLINABLE (newdecl) = 1;
1416 /* If either decl says `inline', this fn is inline,
1417 unless its definition was passed already. */
1418 if (DECL_DECLARED_INLINE_P (newdecl)
1419 || DECL_DECLARED_INLINE_P (olddecl))
1420 DECL_DECLARED_INLINE_P (newdecl) = 1;
1422 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1423 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1426 if (DECL_BUILT_IN (olddecl))
1428 /* Get rid of any built-in function if new arg types don't match it
1429 or if we have a function definition. */
1430 if (! types_match || new_is_definition)
1432 if (! different_binding_level)
1434 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1435 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1440 /* If redeclaring a builtin function, and not a definition,
1441 it stays built in. */
1442 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1443 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1447 /* Also preserve various other info from the definition. */
1448 if (! new_is_definition)
1450 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1451 /* When called with different_binding_level set, don't copy over
1452 DECL_INITIAL, so that we don't accidentally change function
1453 declarations into function definitions. */
1454 if (! different_binding_level)
1455 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1456 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1457 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1458 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1459 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1461 /* Set DECL_INLINE on the declaration if we've got a body
1462 from which to instantiate. */
1463 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1465 DECL_INLINE (newdecl) = 1;
1466 DECL_ABSTRACT_ORIGIN (newdecl)
1467 = (different_binding_level
1468 ? DECL_ORIGIN (olddecl)
1469 : DECL_ABSTRACT_ORIGIN (olddecl));
1474 /* If a previous declaration said inline, mark the
1475 definition as inlinable. */
1476 if (DECL_DECLARED_INLINE_P (newdecl)
1477 && ! DECL_UNINLINABLE (newdecl))
1478 DECL_INLINE (newdecl) = 1;
1481 if (different_binding_level)
1484 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1485 But preserve OLDDECL's DECL_UID. */
1487 unsigned olddecl_uid = DECL_UID (olddecl);
1489 memcpy ((char *) olddecl + sizeof (struct tree_common),
1490 (char *) newdecl + sizeof (struct tree_common),
1491 sizeof (struct tree_decl) - sizeof (struct tree_common));
1492 DECL_UID (olddecl) = olddecl_uid;
1495 /* NEWDECL contains the merged attribute lists.
1496 Update OLDDECL to be the same. */
1497 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1499 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1500 so that encode_section_info has a chance to look at the new decl
1501 flags and attributes. */
1502 if (DECL_RTL_SET_P (olddecl)
1503 && (TREE_CODE (olddecl) == FUNCTION_DECL
1504 || (TREE_CODE (olddecl) == VAR_DECL
1505 && TREE_STATIC (olddecl))))
1506 make_decl_rtl (olddecl, NULL);
1511 /* Return any external DECL associated with ID, whether or not it is
1512 currently in scope. */
1515 any_external_decl (tree id)
1517 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1520 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1522 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1525 t = purpose_member (id, truly_local_externals);
1527 return TREE_VALUE (t);
1532 /* Record an external decl DECL. This only does something if a
1533 shadowing decl already exists. */
1535 record_external_decl (tree decl)
1537 tree name = DECL_NAME (decl);
1538 if (!IDENTIFIER_SYMBOL_VALUE (name))
1541 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1544 /* Check whether decl-node X shadows an existing declaration.
1545 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1546 which might be a NULL_TREE. */
1548 warn_if_shadowing (tree x, tree old)
1552 /* Nothing to shadow? */
1554 /* Shadow warnings not wanted? */
1556 /* No shadow warnings for internally generated vars. */
1557 || DECL_SOURCE_LINE (x) == 0
1558 /* No shadow warnings for vars made for inlining. */
1559 || DECL_FROM_INLINE (x)
1560 /* Don't warn about the parm names in function declarator
1561 within a function declarator.
1562 It would be nice to avoid warning in any function
1563 declarator in a declaration, as opposed to a definition,
1564 but there is no way to tell it's not a definition. */
1565 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1568 name = IDENTIFIER_POINTER (DECL_NAME (x));
1569 if (TREE_CODE (old) == PARM_DECL)
1570 shadow_warning (SW_PARAM, name, old);
1571 else if (DECL_FILE_SCOPE_P (old))
1572 shadow_warning (SW_GLOBAL, name, old);
1574 shadow_warning (SW_LOCAL, name, old);
1578 /* Subroutine of pushdecl.
1580 X is a TYPE_DECL for a typedef statement. Create a brand new
1581 ..._TYPE node (which will be just a variant of the existing
1582 ..._TYPE node with identical properties) and then install X
1583 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1585 The whole point here is to end up with a situation where each
1586 and every ..._TYPE node the compiler creates will be uniquely
1587 associated with AT MOST one node representing a typedef name.
1588 This way, even though the compiler substitutes corresponding
1589 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1590 early on, later parts of the compiler can always do the reverse
1591 translation and get back the corresponding typedef name. For
1594 typedef struct S MY_TYPE;
1597 Later parts of the compiler might only know that `object' was of
1598 type `struct S' if it were not for code just below. With this
1599 code however, later parts of the compiler see something like:
1601 struct S' == struct S
1602 typedef struct S' MY_TYPE;
1605 And they can then deduce (from the node for type struct S') that
1606 the original object declaration was:
1610 Being able to do this is important for proper support of protoize,
1611 and also for generating precise symbolic debugging information
1612 which takes full account of the programmer's (typedef) vocabulary.
1614 Obviously, we don't want to generate a duplicate ..._TYPE node if
1615 the TYPE_DECL node that we are now processing really represents a
1616 standard built-in type.
1618 Since all standard types are effectively declared at line zero
1619 in the source file, we can easily check to see if we are working
1620 on a standard type by checking the current value of lineno. */
1623 clone_underlying_type (tree x)
1625 if (DECL_SOURCE_LINE (x) == 0)
1627 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1628 TYPE_NAME (TREE_TYPE (x)) = x;
1630 else if (TREE_TYPE (x) != error_mark_node
1631 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1633 tree tt = TREE_TYPE (x);
1634 DECL_ORIGINAL_TYPE (x) = tt;
1635 tt = build_type_copy (tt);
1637 TREE_USED (tt) = TREE_USED (x);
1642 /* Record a decl-node X as belonging to the current lexical scope.
1643 Check for errors (such as an incompatible declaration for the same
1644 name already seen in the same scope).
1646 Returns either X or an old decl for the same name.
1647 If an old decl is returned, it may have been smashed
1648 to agree with what X says. */
1653 tree name = DECL_NAME (x);
1654 struct c_scope *scope = current_scope;
1656 #ifdef ENABLE_CHECKING
1657 if (error_mark_node == 0)
1658 /* Called too early. */
1662 /* Functions need the lang_decl data. */
1663 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1664 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1666 /* A local extern declaration for a function doesn't constitute nesting.
1667 A local auto declaration does, since it's a forward decl
1668 for a nested function coming later. */
1669 if (current_function_decl == NULL
1670 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1671 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1672 DECL_CONTEXT (x) = current_file_decl;
1674 DECL_CONTEXT (x) = current_function_decl;
1680 if (warn_nested_externs
1681 && scope != global_scope
1682 && DECL_EXTERNAL (x)
1683 && !DECL_IN_SYSTEM_HEADER (x))
1684 warning ("nested extern declaration of `%s'",
1685 IDENTIFIER_POINTER (name));
1687 old = lookup_name_current_level (name);
1688 if (old && duplicate_decls (x, old, 0, false))
1690 /* For PARM_DECLs, old may be a forward declaration.
1691 If so, we want to remove it from its old location
1692 (in the variables chain) and rechain it in the
1693 location given by the new declaration. */
1694 if (TREE_CODE (x) == PARM_DECL)
1697 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1700 *p = TREE_CHAIN (old);
1701 SCOPE_LIST_APPEND (scope, parms, old);
1707 if (DECL_EXTERNAL (x) || scope == global_scope)
1709 /* Find and check against a previous, not-in-scope, external
1710 decl for this identifier. (C99 s???: If two declarations
1711 with external linkage, referring to the same object, have
1712 incompatible types, the behavior is undefined). */
1713 tree ext = any_external_decl (name);
1716 if (duplicate_decls (x, ext, scope != global_scope,
1718 x = copy_node (ext);
1721 record_external_decl (x);
1724 if (TREE_CODE (x) == TYPE_DECL)
1725 clone_underlying_type (x);
1727 /* If storing a local value, there may already be one
1728 (inherited). If so, record it for restoration when this
1729 scope ends. Take care not to do this if we are replacing an
1730 older decl in the same scope (i.e. duplicate_decls returned
1732 if (scope != global_scope
1733 && IDENTIFIER_SYMBOL_VALUE (name)
1734 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1736 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1737 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1741 /* Install the new declaration in the requested scope. */
1742 IDENTIFIER_SYMBOL_VALUE (name) = x;
1743 C_DECL_INVISIBLE (x) = 0;
1745 /* If x's type is incomplete because it's based on a
1746 structure or union which has not yet been fully declared,
1747 attach it to that structure or union type, so we can go
1748 back and complete the variable declaration later, if the
1749 structure or union gets fully declared.
1751 If the input is erroneous, we can have error_mark in the type
1752 slot (e.g. "f(void a, ...)") - that doesn't count as an
1754 if (TREE_TYPE (x) != error_mark_node
1755 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1757 tree element = TREE_TYPE (x);
1759 while (TREE_CODE (element) == ARRAY_TYPE)
1760 element = TREE_TYPE (element);
1761 element = TYPE_MAIN_VARIANT (element);
1763 if ((TREE_CODE (element) == RECORD_TYPE
1764 || TREE_CODE (element) == UNION_TYPE)
1765 && (TREE_CODE (x) != TYPE_DECL
1766 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE))
1767 C_TYPE_INCOMPLETE_VARS (element)
1768 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1772 if (TREE_CODE (x) == PARM_DECL)
1773 SCOPE_LIST_APPEND (scope, parms, x);
1775 SCOPE_LIST_APPEND (scope, names, x);
1780 /* Record X as belonging to the global scope (C99 "file scope").
1781 This is used only internally by the Objective-C front end,
1782 and is limited to its needs. duplicate_decls is not called;
1783 if there is any preexisting decl for this identifier, it is an ICE. */
1786 pushdecl_top_level (tree x)
1790 if (TREE_CODE (x) != VAR_DECL)
1793 name = DECL_NAME (x);
1795 if (IDENTIFIER_SYMBOL_VALUE (name))
1798 DECL_CONTEXT (x) = current_file_decl;
1799 IDENTIFIER_SYMBOL_VALUE (name) = x;
1801 SCOPE_LIST_APPEND (global_scope, names, x);
1805 /* Generate an implicit declaration for identifier FUNCTIONID as a
1806 function of type int (). */
1809 implicitly_declare (tree functionid)
1811 tree decl = any_external_decl (functionid);
1815 /* Implicit declaration of a function already declared
1816 (somehow) in a different scope, or as a built-in.
1817 If this is the first time this has happened, warn;
1818 then recycle the old declaration. */
1819 if (!C_DECL_IMPLICIT (decl))
1821 implicit_decl_warning (DECL_NAME (decl));
1822 if (! DECL_FILE_SCOPE_P (decl))
1823 warning ("%Hprevious declaration of '%D'",
1824 &DECL_SOURCE_LOCATION (decl), decl);
1825 C_DECL_IMPLICIT (decl) = 1;
1827 /* If this function is global, then it must already be in the
1828 global scope, so there's no need to push it again. */
1829 if (current_scope == global_scope)
1831 /* If this is a local declaration, make a copy; we can't have
1832 the same DECL listed in two different scopes. */
1833 return pushdecl (copy_node (decl));
1836 /* Not seen before. */
1837 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1838 DECL_EXTERNAL (decl) = 1;
1839 TREE_PUBLIC (decl) = 1;
1840 C_DECL_IMPLICIT (decl) = 1;
1841 implicit_decl_warning (functionid);
1843 /* C89 says implicit declarations are in the innermost block.
1844 So we record the decl in the standard fashion. */
1845 decl = pushdecl (decl);
1847 /* No need to call objc_check_decl here - it's a function type. */
1848 rest_of_decl_compilation (decl, NULL, 0, 0);
1850 /* Write a record describing this implicit function declaration
1851 to the prototypes file (if requested). */
1852 gen_aux_info_record (decl, 0, 1, 0);
1854 /* Possibly apply some default attributes to this implicit declaration. */
1855 decl_attributes (&decl, NULL_TREE, 0);
1861 implicit_decl_warning (tree id)
1863 const char *name = IDENTIFIER_POINTER (id);
1864 if (mesg_implicit_function_declaration == 2)
1865 error ("implicit declaration of function `%s'", name);
1866 else if (mesg_implicit_function_declaration == 1)
1867 warning ("implicit declaration of function `%s'", name);
1870 /* Return zero if the declaration NEWDECL is valid
1871 when the declaration OLDDECL (assumed to be for the same name)
1872 has already been seen.
1873 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1874 and 3 if it is a conflicting declaration. */
1877 redeclaration_error_message (tree newdecl, tree olddecl)
1879 if (TREE_CODE (newdecl) == TYPE_DECL)
1881 /* Do not complain about type redeclarations where at least one
1882 declaration was in a system header. */
1883 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1887 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1889 /* Declarations of functions can insist on internal linkage
1890 but they can't be inconsistent with internal linkage,
1891 so there can be no error on that account.
1892 However defining the same name twice is no good. */
1893 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1894 /* However, defining once as extern inline and a second
1895 time in another way is ok. */
1896 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1897 && ! (DECL_DECLARED_INLINE_P (newdecl)
1898 && DECL_EXTERNAL (newdecl))))
1902 else if (DECL_FILE_SCOPE_P (newdecl))
1904 /* Objects declared at file scope: */
1905 /* If at least one is a reference, it's ok. */
1906 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1908 /* Reject two definitions. */
1909 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1911 /* Now we have two tentative defs, or one tentative and one real def. */
1912 /* Insist that the linkage match. */
1913 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1917 else if (current_scope->parm_flag
1918 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1922 /* Newdecl has block scope. If olddecl has block scope also, then
1923 reject two definitions, and reject a definition together with an
1924 external reference. Otherwise, it is OK, because newdecl must
1925 be an extern reference to olddecl. */
1926 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1927 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1933 /* Issue an error message for a reference to an undeclared variable
1934 ID, including a reference to a builtin outside of function-call
1935 context. Establish a binding of the identifier to error_mark_node
1936 in an appropriate scope, which will suppress further errors for the
1939 undeclared_variable (tree id)
1941 static bool already = false;
1942 struct c_scope *scope;
1944 if (current_function_decl == 0)
1946 error ("`%s' undeclared here (not in a function)",
1947 IDENTIFIER_POINTER (id));
1948 scope = current_scope;
1952 error ("`%s' undeclared (first use in this function)",
1953 IDENTIFIER_POINTER (id));
1957 error ("(Each undeclared identifier is reported only once");
1958 error ("for each function it appears in.)");
1962 scope = current_function_scope;
1965 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1967 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1970 /* Subroutine of lookup_label, declare_label, define_label: construct a
1971 LABEL_DECL with all the proper frills. */
1974 make_label (tree name, location_t location)
1976 tree label = build_decl (LABEL_DECL, name, void_type_node);
1978 DECL_CONTEXT (label) = current_function_decl;
1979 DECL_MODE (label) = VOIDmode;
1980 DECL_SOURCE_LOCATION (label) = location;
1985 /* Another subroutine of lookup_label, declare_label, define_label:
1986 set up the binding of name to LABEL_DECL in the given SCOPE. */
1989 bind_label (tree name, tree label, struct c_scope *scope)
1991 if (IDENTIFIER_LABEL_VALUE (name))
1992 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1994 IDENTIFIER_LABEL_VALUE (name) = label;
1996 SCOPE_LIST_APPEND (scope, names, label);
1999 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2000 Create one if none exists so far for the current function.
2001 This is called when a label is used in a goto expression or
2002 has its address taken. */
2005 lookup_label (tree name)
2009 if (current_function_decl == 0)
2011 error ("label %s referenced outside of any function",
2012 IDENTIFIER_POINTER (name));
2016 /* Use a label already defined or ref'd with this name, but not if
2017 it is inherited from a containing function and wasn't declared
2019 label = IDENTIFIER_LABEL_VALUE (name);
2020 if (label && (DECL_CONTEXT (label) == current_function_decl
2021 || C_DECLARED_LABEL_FLAG (label)))
2023 /* If the label has only been declared, update its apparent
2024 location to point here, for better diagnostics if it
2025 turns out not to have been defined. */
2026 if (!TREE_USED (label))
2027 DECL_SOURCE_LOCATION (label) = input_location;
2031 /* No label binding for that identifier; make one. */
2032 label = make_label (name, input_location);
2034 /* Ordinary labels go in the current function scope. */
2035 bind_label (name, label, current_function_scope);
2039 /* Make a label named NAME in the current function, shadowing silently
2040 any that may be inherited from containing functions or containing
2041 scopes. This is called for __label__ declarations. */
2043 /* Note that valid use, if the label being shadowed comes from another
2044 scope in the same function, requires calling declare_nonlocal_label
2045 right away. (Is this still true? -zw 2003-07-17) */
2048 declare_label (tree name)
2050 tree label = IDENTIFIER_LABEL_VALUE (name);
2053 /* Check to make sure that the label hasn't already been declared
2055 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2058 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2059 error ("%Hthis is a previous declaration",
2060 &DECL_SOURCE_LOCATION (dup));
2062 /* Just use the previous declaration. */
2066 label = make_label (name, input_location);
2067 C_DECLARED_LABEL_FLAG (label) = 1;
2069 /* Declared labels go in the current scope. */
2070 bind_label (name, label, current_scope);
2074 /* Define a label, specifying the location in the source file.
2075 Return the LABEL_DECL node for the label, if the definition is valid.
2076 Otherwise return 0. */
2079 define_label (location_t location, tree name)
2083 /* Find any preexisting label with this name. It is an error
2084 if that label has already been defined in this function, or
2085 if there is a containing function with a declared label with
2087 label = IDENTIFIER_LABEL_VALUE (name);
2090 && ((DECL_CONTEXT (label) == current_function_decl
2091 && DECL_INITIAL (label) != 0)
2092 || (DECL_CONTEXT (label) != current_function_decl
2093 && C_DECLARED_LABEL_FLAG (label))))
2095 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2096 error ("%Hduplicate label `%D'", &location, label);
2097 if (DECL_INITIAL (label))
2098 error ("%H`%D' previously defined here", prev_loc, label);
2100 error ("%H`%D' previously declared here", prev_loc, label);
2103 else if (label && DECL_CONTEXT (label) == current_function_decl)
2105 /* The label has been used or declared already in this function,
2106 but not defined. Update its location to point to this
2108 DECL_SOURCE_LOCATION (label) = location;
2112 /* No label binding for that identifier; make one. */
2113 label = make_label (name, location);
2115 /* Ordinary labels go in the current function scope. */
2116 bind_label (name, label, current_function_scope);
2119 if (warn_traditional && !in_system_header && lookup_name (name))
2120 warning ("%Htraditional C lacks a separate namespace for labels, "
2121 "identifier `%s' conflicts", &location,
2122 IDENTIFIER_POINTER (name));
2124 /* Mark label as having been defined. */
2125 DECL_INITIAL (label) = error_mark_node;
2129 /* Return the list of declarations of the current scope. */
2134 return current_scope->names;
2138 /* Given NAME, an IDENTIFIER_NODE,
2139 return the structure (or union or enum) definition for that name.
2140 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2141 CODE says which kind of type the caller wants;
2142 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2143 If the wrong kind of type is found, an error is reported. */
2146 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2148 tree tag = IDENTIFIER_TAG_VALUE (name);
2154 /* We only care about whether it's in this level if
2155 thislevel_only was set or it might be a type clash. */
2156 if (thislevel_only || TREE_CODE (tag) != code)
2158 if (current_scope == global_scope
2159 || purpose_member (name, current_scope->tags))
2163 if (thislevel_only && !thislevel)
2166 if (TREE_CODE (tag) != code)
2168 /* Definition isn't the kind we were looking for. */
2169 pending_invalid_xref = name;
2170 pending_invalid_xref_location = input_location;
2172 /* If in the same binding level as a declaration as a tag
2173 of a different type, this must not be allowed to
2174 shadow that tag, so give the error immediately.
2175 (For example, "struct foo; union foo;" is invalid.) */
2177 pending_xref_error ();
2182 /* Print an error message now
2183 for a recent invalid struct, union or enum cross reference.
2184 We don't print them immediately because they are not invalid
2185 when used in the `struct foo;' construct for shadowing. */
2188 pending_xref_error (void)
2190 if (pending_invalid_xref != 0)
2191 error ("%H`%s' defined as wrong kind of tag",
2192 &pending_invalid_xref_location,
2193 IDENTIFIER_POINTER (pending_invalid_xref));
2194 pending_invalid_xref = 0;
2198 /* Look up NAME in the current scope and its superiors
2199 in the namespace of variables, functions and typedefs.
2200 Return a ..._DECL node of some kind representing its definition,
2201 or return 0 if it is undefined. */
2204 lookup_name (tree name)
2206 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2207 if (decl == 0 || decl == error_mark_node)
2209 if (C_DECL_INVISIBLE (decl))
2214 /* Similar to `lookup_name' but look only at the current scope. */
2217 lookup_name_current_level (tree name)
2219 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2221 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2224 if (current_scope == global_scope)
2227 /* Scan the current scope for a decl with name NAME.
2228 For PARM_DECLs, we have to look at both ->parms and ->names, since
2229 forward parameter declarations wind up on the ->names list. */
2230 if (TREE_CODE (decl) == PARM_DECL
2231 && chain_member (decl, current_scope->parms))
2233 if (chain_member (decl, current_scope->names))
2239 /* Create the predefined scalar types of C,
2240 and some nodes representing standard constants (0, 1, (void *) 0).
2241 Initialize the global scope.
2242 Make definitions for built-in primitive functions. */
2245 c_init_decl_processing (void)
2248 tree ptr_ftype_void, ptr_ftype_ptr;
2249 location_t save_loc = input_location;
2251 /* Adds some ggc roots, and reserved words for c-parse.in. */
2254 current_function_decl = 0;
2256 /* Make the c_scope structure for global names. */
2258 global_scope = current_scope;
2260 /* Declarations from c_common_nodes_and_builtins must not be associated
2261 with this input file, lest we get differences between using and not
2262 using preprocessed headers. */
2263 input_location.file = "<internal>";
2264 input_location.line = 0;
2266 /* Make the DECL for the toplevel file scope. */
2267 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2269 build_common_tree_nodes (flag_signed_char);
2271 c_common_nodes_and_builtins ();
2273 /* In C, comparisons and TRUTH_* expressions have type int. */
2274 truthvalue_type_node = integer_type_node;
2275 truthvalue_true_node = integer_one_node;
2276 truthvalue_false_node = integer_zero_node;
2278 /* Even in C99, which has a real boolean type. */
2279 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2280 boolean_type_node));
2282 endlink = void_list_node;
2283 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2285 = build_function_type (ptr_type_node,
2286 tree_cons (NULL_TREE, ptr_type_node, endlink));
2288 input_location = save_loc;
2290 pedantic_lvalues = pedantic;
2292 make_fname_decl = c_make_fname_decl;
2293 start_fname_decls ();
2295 first_builtin_decl = global_scope->names;
2296 last_builtin_decl = global_scope->names_last;
2299 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2300 decl, NAME is the initialization string and TYPE_DEP indicates whether
2301 NAME depended on the type of the function. As we don't yet implement
2302 delayed emission of static data, we mark the decl as emitted
2303 so it is not placed in the output. Anything using it must therefore pull
2304 out the STRING_CST initializer directly. FIXME. */
2307 c_make_fname_decl (tree id, int type_dep)
2309 const char *name = fname_as_string (type_dep);
2310 tree decl, type, init;
2311 size_t length = strlen (name);
2313 type = build_array_type
2314 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2315 build_index_type (size_int (length)));
2317 decl = build_decl (VAR_DECL, id, type);
2319 TREE_STATIC (decl) = 1;
2320 TREE_READONLY (decl) = 1;
2321 DECL_ARTIFICIAL (decl) = 1;
2323 init = build_string (length + 1, name);
2324 TREE_TYPE (init) = type;
2325 DECL_INITIAL (decl) = init;
2327 TREE_USED (decl) = 1;
2329 if (current_function_decl)
2331 DECL_CONTEXT (decl) = current_function_decl;
2332 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2333 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2336 finish_decl (decl, init, NULL_TREE);
2341 /* Return a definition for a builtin function named NAME and whose data type
2342 is TYPE. TYPE should be a function type with argument types.
2343 FUNCTION_CODE tells later passes how to compile calls to this function.
2344 See tree.h for its possible values.
2346 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2347 the name to be called if we can't opencode the function. If
2348 ATTRS is nonzero, use that for the function's attribute list. */
2351 builtin_function (const char *name, tree type, int function_code,
2352 enum built_in_class class, const char *library_name,
2355 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2356 DECL_EXTERNAL (decl) = 1;
2357 TREE_PUBLIC (decl) = 1;
2359 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2360 make_decl_rtl (decl, NULL);
2362 DECL_BUILT_IN_CLASS (decl) = class;
2363 DECL_FUNCTION_CODE (decl) = function_code;
2365 /* Warn if a function in the namespace for users
2366 is used without an occasion to consider it declared. */
2367 if (name[0] != '_' || name[1] != '_')
2368 C_DECL_INVISIBLE (decl) = 1;
2370 /* Possibly apply some default attributes to this built-in function. */
2372 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2374 decl_attributes (&decl, NULL_TREE, 0);
2379 /* Called when a declaration is seen that contains no names to declare.
2380 If its type is a reference to a structure, union or enum inherited
2381 from a containing scope, shadow that tag name for the current scope
2382 with a forward reference.
2383 If its type defines a new named structure or union
2384 or defines an enum, it is valid but we need not do anything here.
2385 Otherwise, it is an error. */
2388 shadow_tag (tree declspecs)
2390 shadow_tag_warned (declspecs, 0);
2394 shadow_tag_warned (tree declspecs, int warned)
2397 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2404 pending_invalid_xref = 0;
2406 /* Remove the attributes from declspecs, since they will confuse the
2408 split_specs_attrs (declspecs, &specs, &attrs);
2410 for (link = specs; link; link = TREE_CHAIN (link))
2412 tree value = TREE_VALUE (link);
2413 enum tree_code code = TREE_CODE (value);
2415 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2416 /* Used to test also that TYPE_SIZE (value) != 0.
2417 That caused warning for `struct foo;' at top level in the file. */
2419 tree name = TYPE_NAME (value);
2426 if (warned != 1 && code != ENUMERAL_TYPE)
2427 /* Empty unnamed enum OK */
2429 pedwarn ("unnamed struct/union that defines no instances");
2435 t = lookup_tag (code, name, 1);
2439 t = make_node (code);
2446 if (!warned && ! in_system_header)
2448 warning ("useless keyword or type name in empty declaration");
2455 error ("two types specified in one empty declaration");
2460 pedwarn ("empty declaration");
2464 /* Construct an array declarator. EXPR is the expression inside [], or
2465 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2466 to the pointer to which a parameter array is converted). STATIC_P is
2467 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2468 is nonzero is the array is [*], a VLA of unspecified length which is
2469 nevertheless a complete type (not currently implemented by GCC),
2470 zero otherwise. The declarator is constructed as an ARRAY_REF
2471 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2472 left of the [] (filled by in set_array_declarator_type) and operand 1
2473 is the expression inside; whose TREE_TYPE is the type qualifiers and
2474 which has TREE_STATIC set if "static" is used. */
2477 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2480 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2481 TREE_TYPE (decl) = quals;
2482 TREE_STATIC (decl) = (static_p ? 1 : 0);
2483 if (pedantic && !flag_isoc99)
2485 if (static_p || quals != NULL_TREE)
2486 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2488 pedwarn ("ISO C90 does not support `[*]' array declarators");
2491 warning ("GCC does not yet properly implement `[*]' array declarators");
2495 /* Set the type of an array declarator. DECL is the declarator, as
2496 constructed by build_array_declarator; TYPE is what appears on the left
2497 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2498 abstract declarator, zero otherwise; this is used to reject static and
2499 type qualifiers in abstract declarators, where they are not in the
2503 set_array_declarator_type (tree decl, tree type, int abstract_p)
2505 TREE_OPERAND (decl, 0) = type;
2506 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2507 error ("static or type qualifiers in abstract declarator");
2511 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2514 groktypename (tree typename)
2518 if (TREE_CODE (typename) != TREE_LIST)
2521 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2523 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2525 /* Apply attributes. */
2526 decl_attributes (&typename, attrs, 0);
2531 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2534 groktypename_in_parm_context (tree typename)
2536 if (TREE_CODE (typename) != TREE_LIST)
2538 return grokdeclarator (TREE_VALUE (typename),
2539 TREE_PURPOSE (typename),
2543 /* Decode a declarator in an ordinary declaration or data definition.
2544 This is called as soon as the type information and variable name
2545 have been parsed, before parsing the initializer if any.
2546 Here we create the ..._DECL node, fill in its type,
2547 and put it on the list of decls for the current context.
2548 The ..._DECL node is returned as the value.
2550 Exception: for arrays where the length is not specified,
2551 the type is left null, to be filled in by `finish_decl'.
2553 Function definitions do not come here; they go to start_function
2554 instead. However, external and forward declarations of functions
2555 do go through here. Structure field declarations are done by
2556 grokfield and not through here. */
2559 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2564 /* An object declared as __attribute__((deprecated)) suppresses
2565 warnings of uses of other deprecated items. */
2566 if (lookup_attribute ("deprecated", attributes))
2567 deprecated_state = DEPRECATED_SUPPRESS;
2569 decl = grokdeclarator (declarator, declspecs,
2570 NORMAL, initialized);
2572 deprecated_state = DEPRECATED_NORMAL;
2574 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2575 && MAIN_NAME_P (DECL_NAME (decl)))
2576 warning ("%H'%D' is usually a function",
2577 &DECL_SOURCE_LOCATION (decl), decl);
2580 /* Is it valid for this decl to have an initializer at all?
2581 If not, set INITIALIZED to zero, which will indirectly
2582 tell `finish_decl' to ignore the initializer once it is parsed. */
2583 switch (TREE_CODE (decl))
2586 error ("typedef `%s' is initialized (use __typeof__ instead)",
2587 IDENTIFIER_POINTER (DECL_NAME (decl)));
2592 error ("function `%s' is initialized like a variable",
2593 IDENTIFIER_POINTER (DECL_NAME (decl)));
2598 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2599 error ("parameter `%s' is initialized",
2600 IDENTIFIER_POINTER (DECL_NAME (decl)));
2605 /* Don't allow initializations for incomplete types
2606 except for arrays which might be completed by the initialization. */
2608 /* This can happen if the array size is an undefined macro. We already
2609 gave a warning, so we don't need another one. */
2610 if (TREE_TYPE (decl) == error_mark_node)
2612 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2614 /* A complete type is ok if size is fixed. */
2616 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2617 || C_DECL_VARIABLE_SIZE (decl))
2619 error ("variable-sized object may not be initialized");
2623 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2625 error ("variable `%s' has initializer but incomplete type",
2626 IDENTIFIER_POINTER (DECL_NAME (decl)));
2629 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2631 error ("elements of array `%s' have incomplete type",
2632 IDENTIFIER_POINTER (DECL_NAME (decl)));
2639 DECL_EXTERNAL (decl) = 0;
2640 if (current_scope == global_scope)
2641 TREE_STATIC (decl) = 1;
2643 /* Tell `pushdecl' this is an initialized decl
2644 even though we don't yet have the initializer expression.
2645 Also tell `finish_decl' it may store the real initializer. */
2646 DECL_INITIAL (decl) = error_mark_node;
2649 /* If this is a function declaration, write a record describing it to the
2650 prototypes file (if requested). */
2652 if (TREE_CODE (decl) == FUNCTION_DECL)
2653 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2655 /* ANSI specifies that a tentative definition which is not merged with
2656 a non-tentative definition behaves exactly like a definition with an
2657 initializer equal to zero. (Section 3.7.2)
2659 -fno-common gives strict ANSI behavior, though this tends to break
2660 a large body of code that grew up without this rule.
2662 Thread-local variables are never common, since there's no entrenched
2663 body of code to break, and it allows more efficient variable references
2664 in the presence of dynamic linking. */
2666 if (TREE_CODE (decl) == VAR_DECL
2668 && TREE_PUBLIC (decl)
2669 && !DECL_THREAD_LOCAL (decl)
2671 DECL_COMMON (decl) = 1;
2673 /* Set attributes here so if duplicate decl, will have proper attributes. */
2674 decl_attributes (&decl, attributes, 0);
2676 if (TREE_CODE (decl) == FUNCTION_DECL
2677 && DECL_DECLARED_INLINE_P (decl)
2678 && DECL_UNINLINABLE (decl)
2679 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2680 warning ("%Hinline function '%D' given attribute noinline",
2681 &DECL_SOURCE_LOCATION (decl), decl);
2683 /* Add this decl to the current scope.
2684 TEM may equal DECL or it may be a previous decl of the same name. */
2685 tem = pushdecl (decl);
2687 /* For a local variable, define the RTL now. */
2688 if (current_scope != global_scope
2689 /* But not if this is a duplicate decl
2690 and we preserved the rtl from the previous one
2691 (which may or may not happen). */
2692 && !DECL_RTL_SET_P (tem)
2693 && DECL_FILE_SCOPE_P (tem))
2695 if (TREE_TYPE (tem) != error_mark_node
2696 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2697 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2698 && DECL_INITIAL (tem) != 0)))
2705 /* Finish processing of a declaration;
2706 install its initial value.
2707 If the length of an array type is not known before,
2708 it must be determined now, from the initial value, or it is an error. */
2711 finish_decl (tree decl, tree init, tree asmspec_tree)
2713 tree type = TREE_TYPE (decl);
2714 int was_incomplete = (DECL_SIZE (decl) == 0);
2715 const char *asmspec = 0;
2717 /* If a name was specified, get the string. */
2718 if (current_scope == global_scope)
2719 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2721 asmspec = TREE_STRING_POINTER (asmspec_tree);
2723 /* If `start_decl' didn't like having an initialization, ignore it now. */
2724 if (init != 0 && DECL_INITIAL (decl) == 0)
2727 /* Don't crash if parm is initialized. */
2728 if (TREE_CODE (decl) == PARM_DECL)
2732 store_init_value (decl, init);
2734 /* Deduce size of array from initialization, if not already known */
2735 if (TREE_CODE (type) == ARRAY_TYPE
2736 && TYPE_DOMAIN (type) == 0
2737 && TREE_CODE (decl) != TYPE_DECL)
2740 = (TREE_STATIC (decl)
2741 /* Even if pedantic, an external linkage array
2742 may have incomplete type at first. */
2743 ? pedantic && !TREE_PUBLIC (decl)
2744 : !DECL_EXTERNAL (decl));
2746 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2748 /* Get the completed type made by complete_array_type. */
2749 type = TREE_TYPE (decl);
2752 error ("%Hinitializer fails to determine size of '%D'",
2753 &DECL_SOURCE_LOCATION (decl), decl);
2755 else if (failure == 2)
2758 error ("%Harray size missing in '%D'",
2759 &DECL_SOURCE_LOCATION (decl), decl);
2760 /* If a `static' var's size isn't known,
2761 make it extern as well as static, so it does not get
2763 If it is not `static', then do not mark extern;
2764 finish_incomplete_decl will give it a default size
2765 and it will get allocated. */
2766 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2767 DECL_EXTERNAL (decl) = 1;
2770 /* TYPE_MAX_VALUE is always one less than the number of elements
2771 in the array, because we start counting at zero. Therefore,
2772 warn only if the value is less than zero. */
2773 else if (pedantic && TYPE_DOMAIN (type) != 0
2774 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2775 error ("%Hzero or negative size array '%D'",
2776 &DECL_SOURCE_LOCATION (decl), decl);
2778 layout_decl (decl, 0);
2781 if (TREE_CODE (decl) == VAR_DECL)
2783 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2784 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2785 layout_decl (decl, 0);
2787 if (DECL_SIZE (decl) == 0
2788 /* Don't give an error if we already gave one earlier. */
2789 && TREE_TYPE (decl) != error_mark_node
2790 && (TREE_STATIC (decl)
2792 /* A static variable with an incomplete type
2793 is an error if it is initialized.
2794 Also if it is not file scope.
2795 Otherwise, let it through, but if it is not `extern'
2796 then it may cause an error message later. */
2797 (DECL_INITIAL (decl) != 0
2798 || !DECL_FILE_SCOPE_P (decl))
2800 /* An automatic variable with an incomplete type
2802 !DECL_EXTERNAL (decl)))
2804 error ("%Hstorage size of '%D' isn't known",
2805 &DECL_SOURCE_LOCATION (decl), decl);
2806 TREE_TYPE (decl) = error_mark_node;
2809 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2810 && DECL_SIZE (decl) != 0)
2812 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2813 constant_expression_warning (DECL_SIZE (decl));
2815 error ("%Hstorage size of '%D' isn't constant",
2816 &DECL_SOURCE_LOCATION (decl), decl);
2819 if (TREE_USED (type))
2820 TREE_USED (decl) = 1;
2823 /* If this is a function and an assembler name is specified, reset DECL_RTL
2824 so we can give it its new name. Also, update built_in_decls if it
2825 was a normal built-in. */
2826 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2828 /* ASMSPEC is given, and not the name of a register. Mark the
2829 name with a star so assemble_name won't munge it. */
2830 char *starred = alloca (strlen (asmspec) + 2);
2832 strcpy (starred + 1, asmspec);
2834 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2836 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2837 SET_DECL_RTL (builtin, NULL_RTX);
2838 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2839 #ifdef TARGET_MEM_FUNCTIONS
2840 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2841 init_block_move_fn (starred);
2842 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2843 init_block_clear_fn (starred);
2845 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2846 init_block_move_fn (starred);
2847 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2848 init_block_clear_fn (starred);
2851 SET_DECL_RTL (decl, NULL_RTX);
2852 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2855 /* If #pragma weak was used, mark the decl weak now. */
2856 if (current_scope == global_scope)
2857 maybe_apply_pragma_weak (decl);
2859 /* Output the assembler code and/or RTL code for variables and functions,
2860 unless the type is an undefined structure or union.
2861 If not, it will get done when the type is completed. */
2863 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2865 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2866 if (c_dialect_objc ())
2867 objc_check_decl (decl);
2869 if (DECL_FILE_SCOPE_P (decl))
2871 if (DECL_INITIAL (decl) == NULL_TREE
2872 || DECL_INITIAL (decl) == error_mark_node)
2873 /* Don't output anything
2874 when a tentative file-scope definition is seen.
2875 But at end of compilation, do output code for them. */
2876 DECL_DEFER_OUTPUT (decl) = 1;
2877 rest_of_decl_compilation (decl, asmspec, true, 0);
2881 /* This is a local variable. If there is an ASMSPEC, the
2882 user has requested that we handle it specially. */
2885 /* In conjunction with an ASMSPEC, the `register'
2886 keyword indicates that we should place the variable
2887 in a particular register. */
2888 if (DECL_REGISTER (decl))
2889 DECL_C_HARD_REGISTER (decl) = 1;
2891 /* If this is not a static variable, issue a warning.
2892 It doesn't make any sense to give an ASMSPEC for an
2893 ordinary, non-register local variable. Historically,
2894 GCC has accepted -- but ignored -- the ASMSPEC in
2896 if (TREE_CODE (decl) == VAR_DECL
2897 && !DECL_REGISTER (decl)
2898 && !TREE_STATIC (decl))
2899 warning ("%Hignoring asm-specifier for non-static local "
2900 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2902 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2905 if (TREE_CODE (decl) != FUNCTION_DECL)
2906 add_decl_stmt (decl);
2909 if (!DECL_FILE_SCOPE_P (decl))
2911 /* Recompute the RTL of a local array now
2912 if it used to be an incomplete type. */
2914 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2916 /* If we used it already as memory, it must stay in memory. */
2917 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2918 /* If it's still incomplete now, no init will save it. */
2919 if (DECL_SIZE (decl) == 0)
2920 DECL_INITIAL (decl) = 0;
2925 /* If this was marked 'used', be sure it will be output. */
2926 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2927 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2929 if (TREE_CODE (decl) == TYPE_DECL)
2931 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2932 if (c_dialect_objc ())
2933 objc_check_decl (decl);
2934 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2937 /* At the end of a declaration, throw away any variable type sizes
2938 of types defined inside that declaration. There is no use
2939 computing them in the following function definition. */
2940 if (current_scope == global_scope)
2941 get_pending_sizes ();
2943 /* Install a cleanup (aka destructor) if one was given. */
2944 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2946 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2949 static bool eh_initialized_p;
2951 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2952 tree cleanup_decl = lookup_name (cleanup_id);
2955 /* Build "cleanup(&decl)" for the destructor. */
2956 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2957 cleanup = build_tree_list (NULL_TREE, cleanup);
2958 cleanup = build_function_call (cleanup_decl, cleanup);
2960 /* Don't warn about decl unused; the cleanup uses it. */
2961 TREE_USED (decl) = 1;
2963 /* Initialize EH, if we've been told to do so. */
2964 if (flag_exceptions && !eh_initialized_p)
2966 eh_initialized_p = true;
2967 eh_personality_libfunc
2968 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2969 ? "__gcc_personality_sj0"
2970 : "__gcc_personality_v0");
2971 using_eh_for_cleanups ();
2974 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2979 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2980 and push that on the current scope. */
2983 push_parm_decl (tree parm)
2987 /* Don't attempt to expand sizes while parsing this decl.
2988 (We can get here with i_s_e 1 somehow from Objective-C.) */
2989 int save_immediate_size_expand = immediate_size_expand;
2990 immediate_size_expand = 0;
2992 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2993 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2994 decl_attributes (&decl, TREE_VALUE (parm), 0);
2996 decl = pushdecl (decl);
2998 finish_decl (decl, NULL_TREE, NULL_TREE);
3000 immediate_size_expand = save_immediate_size_expand;
3003 /* Mark all the parameter declarations to date as forward decls,
3004 shift them to the variables list, and reset the parameters list.
3005 Also diagnose use of this extension. */
3008 mark_forward_parm_decls (void)
3012 if (pedantic && !current_scope->warned_forward_parm_decls)
3014 pedwarn ("ISO C forbids forward parameter declarations");
3015 current_scope->warned_forward_parm_decls = true;
3018 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3019 TREE_ASM_WRITTEN (parm) = 1;
3021 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3022 current_scope->parms = 0;
3023 current_scope->parms_last = 0;
3026 static GTY(()) int compound_literal_number;
3028 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3029 literal, which may be an incomplete array type completed by the
3030 initializer; INIT is a CONSTRUCTOR that initializes the compound
3034 build_compound_literal (tree type, tree init)
3036 /* We do not use start_decl here because we have a type, not a declarator;
3037 and do not use finish_decl because the decl should be stored inside
3038 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3039 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3042 DECL_EXTERNAL (decl) = 0;
3043 TREE_PUBLIC (decl) = 0;
3044 TREE_STATIC (decl) = (current_scope == global_scope);
3045 DECL_CONTEXT (decl) = current_function_decl;
3046 TREE_USED (decl) = 1;
3047 TREE_TYPE (decl) = type;
3048 TREE_READONLY (decl) = TREE_READONLY (type);
3049 store_init_value (decl, init);
3051 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3053 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3058 type = TREE_TYPE (decl);
3059 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3060 return error_mark_node;
3062 stmt = build_stmt (DECL_STMT, decl);
3063 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3064 TREE_SIDE_EFFECTS (complit) = 1;
3066 layout_decl (decl, 0);
3068 if (TREE_STATIC (decl))
3070 /* This decl needs a name for the assembler output. We also need
3071 a unique suffix to be added to the name. */
3074 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3075 compound_literal_number);
3076 compound_literal_number++;
3077 DECL_NAME (decl) = get_identifier (name);
3078 DECL_DEFER_OUTPUT (decl) = 1;
3079 DECL_COMDAT (decl) = 1;
3080 DECL_ARTIFICIAL (decl) = 1;
3082 rest_of_decl_compilation (decl, NULL, 1, 0);
3088 /* Make TYPE a complete type based on INITIAL_VALUE.
3089 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3090 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3093 complete_array_type (tree type, tree initial_value, int do_default)
3095 tree maxindex = NULL_TREE;
3100 /* Note MAXINDEX is really the maximum index,
3101 one less than the size. */
3102 if (TREE_CODE (initial_value) == STRING_CST)
3105 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3106 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3109 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3111 tree elts = CONSTRUCTOR_ELTS (initial_value);
3112 maxindex = build_int_2 (-1, -1);
3113 for (; elts; elts = TREE_CHAIN (elts))
3115 if (TREE_PURPOSE (elts))
3116 maxindex = TREE_PURPOSE (elts);
3118 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3119 maxindex, integer_one_node));
3121 maxindex = copy_node (maxindex);
3125 /* Make an error message unless that happened already. */
3126 if (initial_value != error_mark_node)
3129 /* Prevent further error messages. */
3130 maxindex = build_int_2 (0, 0);
3137 maxindex = build_int_2 (0, 0);
3143 TYPE_DOMAIN (type) = build_index_type (maxindex);
3144 if (!TREE_TYPE (maxindex))
3145 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3148 /* Lay out the type now that we can get the real answer. */
3155 /* Determine whether TYPE is a structure with a flexible array member,
3156 or a union containing such a structure (possibly recursively). */
3159 flexible_array_type_p (tree type)
3162 switch (TREE_CODE (type))
3165 x = TYPE_FIELDS (type);
3168 while (TREE_CHAIN (x) != NULL_TREE)
3170 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3171 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3172 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3173 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3177 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3179 if (flexible_array_type_p (TREE_TYPE (x)))
3188 /* Given declspecs and a declarator,
3189 determine the name and type of the object declared
3190 and construct a ..._DECL node for it.
3191 (In one case we can return a ..._TYPE node instead.
3192 For invalid input we sometimes return 0.)
3194 DECLSPECS is a chain of tree_list nodes whose value fields
3195 are the storage classes and type specifiers.
3197 DECL_CONTEXT says which syntactic context this declaration is in:
3198 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3199 FUNCDEF for a function definition. Like NORMAL but a few different
3200 error messages in each case. Return value may be zero meaning
3201 this definition is too screwy to try to parse.
3202 PARM for a parameter declaration (either within a function prototype
3203 or before a function body). Make a PARM_DECL, or return void_type_node.
3204 TYPENAME if for a typename (in a cast or sizeof).
3205 Don't make a DECL node; just return the ..._TYPE node.
3206 FIELD for a struct or union field; make a FIELD_DECL.
3207 BITFIELD for a field with specified width.
3208 INITIALIZED is 1 if the decl has an initializer.
3210 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3211 It may also be so in the PARM case, for a prototype where the
3212 argument type is specified but not the name.
3214 This function is where the complicated C meanings of `static'
3215 and `extern' are interpreted. */
3218 grokdeclarator (tree declarator, tree declspecs,
3219 enum decl_context decl_context, int initialized)
3223 tree type = NULL_TREE;
3228 int type_quals = TYPE_UNQUALIFIED;
3230 int explicit_int = 0;
3231 int explicit_char = 0;
3232 int defaulted_int = 0;
3233 tree typedef_decl = 0;
3235 tree typedef_type = 0;
3236 int funcdef_flag = 0;
3237 enum tree_code innermost_code = ERROR_MARK;
3239 int size_varies = 0;
3240 tree decl_attr = NULL_TREE;
3241 tree array_ptr_quals = NULL_TREE;
3242 int array_parm_static = 0;
3243 tree returned_attrs = NULL_TREE;
3245 if (decl_context == BITFIELD)
3246 bitfield = 1, decl_context = FIELD;
3248 if (decl_context == FUNCDEF)
3249 funcdef_flag = 1, decl_context = NORMAL;
3251 /* Look inside a declarator for the name being declared
3252 and get it as a string, for an error message. */
3254 tree decl = declarator;
3258 switch (TREE_CODE (decl))
3263 innermost_code = TREE_CODE (decl);
3264 decl = TREE_OPERAND (decl, 0);
3268 decl = TREE_VALUE (decl);
3271 case IDENTIFIER_NODE:
3272 name = IDENTIFIER_POINTER (decl);
3283 /* A function definition's declarator must have the form of
3284 a function declarator. */
3286 if (funcdef_flag && innermost_code != CALL_EXPR)
3289 /* If this looks like a function definition, make it one,
3290 even if it occurs where parms are expected.
3291 Then store_parm_decls will reject it and not use it as a parm. */
3292 if (decl_context == NORMAL && !funcdef_flag
3293 && current_scope->parm_flag)
3294 decl_context = PARM;
3296 /* Look through the decl specs and record which ones appear.
3297 Some typespecs are defined as built-in typenames.
3298 Others, the ones that are modifiers of other types,
3299 are represented by bits in SPECBITS: set the bits for
3300 the modifiers that appear. Storage class keywords are also in SPECBITS.
3302 If there is a typedef name or a type, store the type in TYPE.
3303 This includes builtin typedefs such as `int'.
3305 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3306 and did not come from a user typedef.
3308 Set LONGLONG if `long' is mentioned twice. */
3310 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3312 tree id = TREE_VALUE (spec);
3314 /* If the entire declaration is itself tagged as deprecated then
3315 suppress reports of deprecated items. */
3316 if (id && TREE_DEPRECATED (id))
3318 if (deprecated_state != DEPRECATED_SUPPRESS)
3319 warn_deprecated_use (id);
3322 if (id == ridpointers[(int) RID_INT])
3324 if (id == ridpointers[(int) RID_CHAR])
3327 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3329 enum rid i = C_RID_CODE (id);
3330 if ((int) i <= (int) RID_LAST_MODIFIER)
3332 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3335 error ("`long long long' is too long for GCC");
3338 if (pedantic && !flag_isoc99 && ! in_system_header
3340 pedwarn ("ISO C90 does not support `long long'");
3344 else if (specbits & (1 << (int) i))
3346 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3349 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3352 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3355 /* Diagnose "__thread extern". Recall that this list
3356 is in the reverse order seen in the text. */
3358 && (specbits & (1 << (int) RID_EXTERN
3359 | 1 << (int) RID_STATIC)))
3361 if (specbits & 1 << (int) RID_EXTERN)
3362 error ("`__thread' before `extern'");
3364 error ("`__thread' before `static'");
3367 specbits |= 1 << (int) i;
3372 error ("two or more data types in declaration of `%s'", name);
3373 /* Actual typedefs come to us as TYPE_DECL nodes. */
3374 else if (TREE_CODE (id) == TYPE_DECL)
3376 if (TREE_TYPE (id) == error_mark_node)
3377 ; /* Allow the type to default to int to avoid cascading errors. */
3380 type = TREE_TYPE (id);
3381 decl_attr = DECL_ATTRIBUTES (id);
3385 /* Built-in types come as identifiers. */
3386 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3388 tree t = lookup_name (id);
3389 if (TREE_TYPE (t) == error_mark_node)
3391 else if (!t || TREE_CODE (t) != TYPE_DECL)
3392 error ("`%s' fails to be a typedef or built in type",
3393 IDENTIFIER_POINTER (id));
3396 type = TREE_TYPE (t);
3400 else if (TREE_CODE (id) != ERROR_MARK)
3407 typedef_type = type;
3409 size_varies = C_TYPE_VARIABLE_SIZE (type);
3411 /* No type at all: default to `int', and set DEFAULTED_INT
3412 because it was not a user-defined typedef. */
3416 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3417 | (1 << (int) RID_SIGNED)
3418 | (1 << (int) RID_UNSIGNED)
3419 | (1 << (int) RID_COMPLEX))))
3420 /* Don't warn about typedef foo = bar. */
3421 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3422 && ! in_system_header)
3424 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3425 and this is a function, or if -Wimplicit; prefer the former
3426 warning since it is more explicit. */
3427 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3429 warn_about_return_type = 1;
3430 else if (warn_implicit_int || flag_isoc99)
3431 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3436 type = integer_type_node;
3439 /* Now process the modifiers that were specified
3440 and check for invalid combinations. */
3442 /* Long double is a special combination. */
3444 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3445 && TYPE_MAIN_VARIANT (type) == double_type_node)
3447 specbits &= ~(1 << (int) RID_LONG);
3448 type = long_double_type_node;
3451 /* Check all other uses of type modifiers. */
3453 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3454 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3458 if ((specbits & 1 << (int) RID_LONG)
3459 && (specbits & 1 << (int) RID_SHORT))
3460 error ("both long and short specified for `%s'", name);
3461 else if (((specbits & 1 << (int) RID_LONG)
3462 || (specbits & 1 << (int) RID_SHORT))
3464 error ("long or short specified with char for `%s'", name);
3465 else if (((specbits & 1 << (int) RID_LONG)
3466 || (specbits & 1 << (int) RID_SHORT))
3467 && TREE_CODE (type) == REAL_TYPE)
3469 static int already = 0;
3471 error ("long or short specified with floating type for `%s'", name);
3472 if (! already && ! pedantic)
3474 error ("the only valid combination is `long double'");
3478 else if ((specbits & 1 << (int) RID_SIGNED)
3479 && (specbits & 1 << (int) RID_UNSIGNED))
3480 error ("both signed and unsigned specified for `%s'", name);
3481 else if (TREE_CODE (type) != INTEGER_TYPE)
3482 error ("long, short, signed or unsigned invalid for `%s'", name);
3486 if (!explicit_int && !defaulted_int && !explicit_char)
3488 error ("long, short, signed or unsigned used invalidly for `%s'",
3494 /* Discard the type modifiers if they are invalid. */
3497 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3498 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3503 if ((specbits & (1 << (int) RID_COMPLEX))
3504 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3506 error ("complex invalid for `%s'", name);
3507 specbits &= ~(1 << (int) RID_COMPLEX);
3510 /* Decide whether an integer type is signed or not.
3511 Optionally treat bitfields as signed by default. */
3512 if (specbits & 1 << (int) RID_UNSIGNED
3513 || (bitfield && ! flag_signed_bitfields
3514 && (explicit_int || defaulted_int || explicit_char
3515 /* A typedef for plain `int' without `signed'
3516 can be controlled just like plain `int'. */
3517 || ! (typedef_decl != 0
3518 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3519 && TREE_CODE (type) != ENUMERAL_TYPE
3520 && !(specbits & 1 << (int) RID_SIGNED)))
3523 type = long_long_unsigned_type_node;
3524 else if (specbits & 1 << (int) RID_LONG)
3525 type = long_unsigned_type_node;
3526 else if (specbits & 1 << (int) RID_SHORT)
3527 type = short_unsigned_type_node;
3528 else if (type == char_type_node)
3529 type = unsigned_char_type_node;
3530 else if (typedef_decl)
3531 type = c_common_unsigned_type (type);
3533 type = unsigned_type_node;
3535 else if ((specbits & 1 << (int) RID_SIGNED)
3536 && type == char_type_node)
3537 type = signed_char_type_node;
3539 type = long_long_integer_type_node;
3540 else if (specbits & 1 << (int) RID_LONG)
3541 type = long_integer_type_node;
3542 else if (specbits & 1 << (int) RID_SHORT)
3543 type = short_integer_type_node;
3545 if (specbits & 1 << (int) RID_COMPLEX)
3547 if (pedantic && !flag_isoc99)
3548 pedwarn ("ISO C90 does not support complex types");
3549 /* If we just have "complex", it is equivalent to
3550 "complex double", but if any modifiers at all are specified it is
3551 the complex form of TYPE. E.g, "complex short" is
3552 "complex short int". */
3554 if (defaulted_int && ! longlong
3555 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3556 | (1 << (int) RID_SIGNED)
3557 | (1 << (int) RID_UNSIGNED))))
3560 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3561 type = complex_double_type_node;
3563 else if (type == integer_type_node)
3566 pedwarn ("ISO C does not support complex integer types");
3567 type = complex_integer_type_node;
3569 else if (type == float_type_node)
3570 type = complex_float_type_node;
3571 else if (type == double_type_node)
3572 type = complex_double_type_node;
3573 else if (type == long_double_type_node)
3574 type = complex_long_double_type_node;
3578 pedwarn ("ISO C does not support complex integer types");
3579 type = build_complex_type (type);
3583 /* Figure out the type qualifiers for the declaration. There are
3584 two ways a declaration can become qualified. One is something
3585 like `const int i' where the `const' is explicit. Another is
3586 something like `typedef const int CI; CI i' where the type of the
3587 declaration contains the `const'. */
3588 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3589 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3590 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3591 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3592 if (constp > 1 && ! flag_isoc99)
3593 pedwarn ("duplicate `const'");
3594 if (restrictp > 1 && ! flag_isoc99)
3595 pedwarn ("duplicate `restrict'");
3596 if (volatilep > 1 && ! flag_isoc99)
3597 pedwarn ("duplicate `volatile'");
3598 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3599 type = TYPE_MAIN_VARIANT (type);
3600 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3601 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3602 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3604 /* Warn if two storage classes are given. Default to `auto'. */
3609 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3610 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3611 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3612 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3613 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3615 /* "static __thread" and "extern __thread" are allowed. */
3616 if ((specbits & (1 << (int) RID_THREAD
3617 | 1 << (int) RID_STATIC
3618 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3621 /* Warn about storage classes that are invalid for certain
3622 kinds of declarations (parameters, typenames, etc.). */
3625 error ("multiple storage classes in declaration of `%s'", name);
3626 else if (funcdef_flag
3628 & ((1 << (int) RID_REGISTER)
3629 | (1 << (int) RID_AUTO)
3630 | (1 << (int) RID_TYPEDEF)
3631 | (1 << (int) RID_THREAD))))
3633 if (specbits & 1 << (int) RID_AUTO
3634 && (pedantic || current_scope == global_scope))
3635 pedwarn ("function definition declared `auto'");
3636 if (specbits & 1 << (int) RID_REGISTER)
3637 error ("function definition declared `register'");
3638 if (specbits & 1 << (int) RID_TYPEDEF)
3639 error ("function definition declared `typedef'");
3640 if (specbits & 1 << (int) RID_THREAD)
3641 error ("function definition declared `__thread'");
3642 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3643 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3645 else if (decl_context != NORMAL && nclasses > 0)
3647 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3651 switch (decl_context)
3654 error ("storage class specified for structure field `%s'",
3658 error ("storage class specified for parameter `%s'", name);
3661 error ("storage class specified for typename");
3664 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3665 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3666 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3669 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3671 /* `extern' with initialization is invalid if not at file scope. */
3672 if (current_scope == global_scope)
3673 warning ("`%s' initialized and declared `extern'", name);
3675 error ("`%s' has both `extern' and initializer", name);
3677 else if (current_scope == global_scope)
3679 if (specbits & 1 << (int) RID_AUTO)
3680 error ("file-scope declaration of `%s' specifies `auto'", name);
3684 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3685 error ("nested function `%s' declared `extern'", name);
3686 else if ((specbits & (1 << (int) RID_THREAD
3687 | 1 << (int) RID_EXTERN
3688 | 1 << (int) RID_STATIC))
3689 == (1 << (int) RID_THREAD))
3691 error ("function-scope `%s' implicitly auto and declared `__thread'",
3693 specbits &= ~(1 << (int) RID_THREAD);
3698 /* Now figure out the structure of the declarator proper.
3699 Descend through it, creating more complex types, until we reach
3700 the declared identifier (or NULL_TREE, in an absolute declarator). */
3702 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3704 if (type == error_mark_node)
3706 declarator = TREE_OPERAND (declarator, 0);
3710 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3711 an INDIRECT_REF (for *...),
3712 a CALL_EXPR (for ...(...)),
3713 a TREE_LIST (for nested attributes),
3714 an identifier (for the name being declared)
3715 or a null pointer (for the place in an absolute declarator
3716 where the name was omitted).
3717 For the last two cases, we have just exited the loop.
3719 At this point, TYPE is the type of elements of an array,
3720 or for a function to return, or for a pointer to point to.
3721 After this sequence of ifs, TYPE is the type of the
3722 array or function or pointer, and DECLARATOR has had its
3723 outermost layer removed. */
3725 if (array_ptr_quals != NULL_TREE || array_parm_static)
3727 /* Only the innermost declarator (making a parameter be of
3728 array type which is converted to pointer type)
3729 may have static or type qualifiers. */
3730 error ("static or type qualifiers in non-parameter array declarator");
3731 array_ptr_quals = NULL_TREE;
3732 array_parm_static = 0;
3735 if (TREE_CODE (declarator) == TREE_LIST)
3737 /* We encode a declarator with embedded attributes using
3739 tree attrs = TREE_PURPOSE (declarator);
3742 declarator = TREE_VALUE (declarator);
3743 inner_decl = declarator;
3744 while (inner_decl != NULL_TREE
3745 && TREE_CODE (inner_decl) == TREE_LIST)
3746 inner_decl = TREE_VALUE (inner_decl);
3747 if (inner_decl == NULL_TREE
3748 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3749 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3750 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3751 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3752 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3753 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3754 returned_attrs = decl_attributes (&type,
3755 chainon (returned_attrs, attrs),
3758 else if (TREE_CODE (declarator) == ARRAY_REF)
3760 tree itype = NULL_TREE;
3761 tree size = TREE_OPERAND (declarator, 1);
3762 /* The index is a signed object `sizetype' bits wide. */
3763 tree index_type = c_common_signed_type (sizetype);
3765 array_ptr_quals = TREE_TYPE (declarator);
3766 array_parm_static = TREE_STATIC (declarator);
3768 declarator = TREE_OPERAND (declarator, 0);
3770 /* Check for some types that there cannot be arrays of. */
3772 if (VOID_TYPE_P (type))
3774 error ("declaration of `%s' as array of voids", name);
3775 type = error_mark_node;
3778 if (TREE_CODE (type) == FUNCTION_TYPE)
3780 error ("declaration of `%s' as array of functions", name);
3781 type = error_mark_node;
3784 if (pedantic && flexible_array_type_p (type))
3785 pedwarn ("invalid use of structure with flexible array member");
3787 if (size == error_mark_node)
3788 type = error_mark_node;
3790 if (type == error_mark_node)
3793 /* If size was specified, set ITYPE to a range-type for that size.
3794 Otherwise, ITYPE remains null. finish_decl may figure it out
3795 from an initial value. */
3799 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3800 STRIP_TYPE_NOPS (size);
3802 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3804 error ("size of array `%s' has non-integer type", name);
3805 size = integer_one_node;
3808 if (pedantic && integer_zerop (size))
3809 pedwarn ("ISO C forbids zero-size array `%s'", name);
3811 if (TREE_CODE (size) == INTEGER_CST)
3813 constant_expression_warning (size);
3814 if (tree_int_cst_sgn (size) < 0)
3816 error ("size of array `%s' is negative", name);
3817 size = integer_one_node;
3822 /* Make sure the array size remains visibly nonconstant
3823 even if it is (eg) a const variable with known value. */
3826 if (!flag_isoc99 && pedantic)
3828 if (TREE_CONSTANT (size))
3829 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3832 pedwarn ("ISO C90 forbids variable-size array `%s'",
3837 if (integer_zerop (size))
3839 /* A zero-length array cannot be represented with an
3840 unsigned index type, which is what we'll get with
3841 build_index_type. Create an open-ended range instead. */
3842 itype = build_range_type (sizetype, size, NULL_TREE);
3846 /* Compute the maximum valid index, that is, size - 1.
3847 Do the calculation in index_type, so that if it is
3848 a variable the computations will be done in the
3850 itype = fold (build (MINUS_EXPR, index_type,
3851 convert (index_type, size),
3852 convert (index_type, size_one_node)));
3854 /* If that overflowed, the array is too big.
3855 ??? While a size of INT_MAX+1 technically shouldn't
3856 cause an overflow (because we subtract 1), the overflow
3857 is recorded during the conversion to index_type, before
3858 the subtraction. Handling this case seems like an
3859 unnecessary complication. */
3860 if (TREE_OVERFLOW (itype))
3862 error ("size of array `%s' is too large", name);
3863 type = error_mark_node;
3869 /* We must be able to distinguish the
3870 SAVE_EXPR_CONTEXT for the variably-sized type
3871 so that we can set it correctly in
3872 set_save_expr_context. The convention is
3873 that all SAVE_EXPRs that need to be reset
3874 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3875 tree cfd = current_function_decl;
3876 if (decl_context == PARM)
3877 current_function_decl = NULL_TREE;
3878 itype = variable_size (itype);
3879 if (decl_context == PARM)
3880 current_function_decl = cfd;
3882 itype = build_index_type (itype);
3885 else if (decl_context == FIELD)
3887 if (pedantic && !flag_isoc99 && !in_system_header)
3888 pedwarn ("ISO C90 does not support flexible array members");
3890 /* ISO C99 Flexible array members are effectively identical
3891 to GCC's zero-length array extension. */
3892 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3895 /* If pedantic, complain about arrays of incomplete types. */
3897 if (pedantic && !COMPLETE_TYPE_P (type))
3898 pedwarn ("array type has incomplete element type");
3900 /* Build the array type itself, then merge any constancy or
3901 volatility into the target type. We must do it in this order
3902 to ensure that the TYPE_MAIN_VARIANT field of the array type
3903 is set correctly. */
3905 type = build_array_type (type, itype);
3907 type = c_build_qualified_type (type, type_quals);
3910 C_TYPE_VARIABLE_SIZE (type) = 1;
3912 /* The GCC extension for zero-length arrays differs from
3913 ISO flexible array members in that sizeof yields zero. */
3914 if (size && integer_zerop (size))
3917 TYPE_SIZE (type) = bitsize_zero_node;
3918 TYPE_SIZE_UNIT (type) = size_zero_node;
3920 if (decl_context != PARM
3921 && (array_ptr_quals != NULL_TREE || array_parm_static))
3923 error ("static or type qualifiers in non-parameter array declarator");
3924 array_ptr_quals = NULL_TREE;
3925 array_parm_static = 0;
3928 else if (TREE_CODE (declarator) == CALL_EXPR)
3932 /* Declaring a function type.
3933 Make sure we have a valid type for the function to return. */
3934 if (type == error_mark_node)
3939 /* Warn about some types functions can't return. */
3941 if (TREE_CODE (type) == FUNCTION_TYPE)
3943 error ("`%s' declared as function returning a function", name);
3944 type = integer_type_node;
3946 if (TREE_CODE (type) == ARRAY_TYPE)
3948 error ("`%s' declared as function returning an array", name);
3949 type = integer_type_node;
3952 /* Construct the function type and go to the next
3953 inner layer of declarator. */
3955 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3957 /* Say it's a definition
3958 only for the CALL_EXPR
3959 closest to the identifier. */
3960 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3961 /* Type qualifiers before the return type of the function
3962 qualify the return type, not the function type. */
3965 /* Type qualifiers on a function return type are normally
3966 permitted by the standard but have no effect, so give a
3967 warning at -Wextra. Qualifiers on a void return type have
3968 meaning as a GNU extension, and are banned on function
3969 definitions in ISO C. FIXME: strictly we shouldn't
3970 pedwarn for qualified void return types except on function
3971 definitions, but not doing so could lead to the undesirable
3972 state of a "volatile void" function return type not being
3973 warned about, and a use of the function being compiled
3974 with GNU semantics, with no diagnostics under -pedantic. */
3975 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3976 pedwarn ("ISO C forbids qualified void function return type");
3977 else if (extra_warnings
3978 && !(VOID_TYPE_P (type)
3979 && type_quals == TYPE_QUAL_VOLATILE))
3980 warning ("type qualifiers ignored on function return type");
3982 type = c_build_qualified_type (type, type_quals);
3984 type_quals = TYPE_UNQUALIFIED;
3986 type = build_function_type (type, arg_types);
3987 declarator = TREE_OPERAND (declarator, 0);
3989 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3990 the formal parameter list of this FUNCTION_TYPE to point to
3991 the FUNCTION_TYPE node itself. */
3996 for (link = last_function_parm_tags;
3998 link = TREE_CHAIN (link))
3999 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4002 else if (TREE_CODE (declarator) == INDIRECT_REF)
4004 /* Merge any constancy or volatility into the target type
4007 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4009 pedwarn ("ISO C forbids qualified function types");
4011 type = c_build_qualified_type (type, type_quals);
4012 type_quals = TYPE_UNQUALIFIED;
4015 type = build_pointer_type (type);
4017 /* Process a list of type modifier keywords
4018 (such as const or volatile) that were given inside the `*'. */
4020 if (TREE_TYPE (declarator))
4028 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4029 typemodlist = TREE_CHAIN (typemodlist))
4031 tree qualifier = TREE_VALUE (typemodlist);
4033 if (C_IS_RESERVED_WORD (qualifier))
4035 if (C_RID_CODE (qualifier) == RID_CONST)
4037 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4039 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4049 error ("invalid type modifier within pointer declarator");
4050 if (constp > 1 && ! flag_isoc99)
4051 pedwarn ("duplicate `const'");
4052 if (volatilep > 1 && ! flag_isoc99)
4053 pedwarn ("duplicate `volatile'");
4054 if (restrictp > 1 && ! flag_isoc99)
4055 pedwarn ("duplicate `restrict'");
4057 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4058 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4059 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4062 declarator = TREE_OPERAND (declarator, 0);
4069 /* Now TYPE has the actual type. */
4071 /* Did array size calculations overflow? */
4073 if (TREE_CODE (type) == ARRAY_TYPE
4074 && COMPLETE_TYPE_P (type)
4075 && TREE_OVERFLOW (TYPE_SIZE (type)))
4077 error ("size of array `%s' is too large", name);
4078 /* If we proceed with the array type as it is, we'll eventually
4079 crash in tree_low_cst(). */
4080 type = error_mark_node;
4083 /* If this is declaring a typedef name, return a TYPE_DECL. */
4085 if (specbits & (1 << (int) RID_TYPEDEF))
4088 /* Note that the grammar rejects storage classes
4089 in typenames, fields or parameters */
4090 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4092 pedwarn ("ISO C forbids qualified function types");
4094 type = c_build_qualified_type (type, type_quals);
4095 decl = build_decl (TYPE_DECL, declarator, type);
4096 if ((specbits & (1 << (int) RID_SIGNED))
4097 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4098 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4099 decl_attributes (&decl, returned_attrs, 0);
4103 /* Detect the case of an array type of unspecified size
4104 which came, as such, direct from a typedef name.
4105 We must copy the type, so that each identifier gets
4106 a distinct type, so that each identifier's size can be
4107 controlled separately by its own initializer. */
4109 if (type != 0 && typedef_type != 0
4110 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4111 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4113 type = build_array_type (TREE_TYPE (type), 0);
4115 C_TYPE_VARIABLE_SIZE (type) = 1;
4118 /* If this is a type name (such as, in a cast or sizeof),
4119 compute the type and return it now. */
4121 if (decl_context == TYPENAME)
4123 /* Note that the grammar rejects storage classes
4124 in typenames, fields or parameters */
4125 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4127 pedwarn ("ISO C forbids const or volatile function types");
4129 type = c_build_qualified_type (type, type_quals);
4130 decl_attributes (&type, returned_attrs, 0);
4134 /* Aside from typedefs and type names (handle above),
4135 `void' at top level (not within pointer)
4136 is allowed only in public variables.
4137 We don't complain about parms either, but that is because
4138 a better error message can be made later. */
4140 if (VOID_TYPE_P (type) && decl_context != PARM
4141 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4142 && ((specbits & (1 << (int) RID_EXTERN))
4143 || (current_scope == global_scope
4145 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4147 error ("variable or field `%s' declared void", name);
4148 type = integer_type_node;
4151 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4152 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4157 if (decl_context == PARM)
4159 tree type_as_written;
4162 /* A parameter declared as an array of T is really a pointer to T.
4163 One declared as a function is really a pointer to a function. */
4165 if (TREE_CODE (type) == ARRAY_TYPE)
4167 /* Transfer const-ness of array into that of type pointed to. */
4168 type = TREE_TYPE (type);
4170 type = c_build_qualified_type (type, type_quals);
4171 type = build_pointer_type (type);
4172 type_quals = TYPE_UNQUALIFIED;
4173 if (array_ptr_quals)
4175 tree new_ptr_quals, new_ptr_attrs;
4177 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4178 /* We don't yet implement attributes in this context. */
4179 if (new_ptr_attrs != NULL_TREE)
4180 warning ("attributes in parameter array declarator ignored");
4185 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4187 tree qualifier = TREE_VALUE (new_ptr_quals);
4189 if (C_IS_RESERVED_WORD (qualifier))
4191 if (C_RID_CODE (qualifier) == RID_CONST)
4193 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4195 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4205 error ("invalid type modifier within array declarator");
4207 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4208 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4209 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4213 else if (TREE_CODE (type) == FUNCTION_TYPE)
4215 if (pedantic && type_quals)
4216 pedwarn ("ISO C forbids qualified function types");
4218 type = c_build_qualified_type (type, type_quals);
4219 type = build_pointer_type (type);
4220 type_quals = TYPE_UNQUALIFIED;
4222 else if (type_quals)
4223 type = c_build_qualified_type (type, type_quals);
4225 type_as_written = type;
4227 decl = build_decl (PARM_DECL, declarator, type);
4229 C_DECL_VARIABLE_SIZE (decl) = 1;
4231 /* Compute the type actually passed in the parmlist,
4232 for the case where there is no prototype.
4233 (For example, shorts and chars are passed as ints.)
4234 When there is a prototype, this is overridden later. */
4236 if (type == error_mark_node)
4237 promoted_type = type;
4239 promoted_type = c_type_promotes_to (type);
4241 DECL_ARG_TYPE (decl) = promoted_type;
4242 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4244 else if (decl_context == FIELD)
4246 /* Structure field. It may not be a function. */
4248 if (TREE_CODE (type) == FUNCTION_TYPE)
4250 error ("field `%s' declared as a function", name);
4251 type = build_pointer_type (type);
4253 else if (TREE_CODE (type) != ERROR_MARK
4254 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4256 error ("field `%s' has incomplete type", name);
4257 type = error_mark_node;
4259 /* Move type qualifiers down to element of an array. */
4260 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4261 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4263 TYPE_DOMAIN (type));
4264 decl = build_decl (FIELD_DECL, declarator, type);
4265 DECL_NONADDRESSABLE_P (decl) = bitfield;
4268 C_DECL_VARIABLE_SIZE (decl) = 1;
4270 else if (TREE_CODE (type) == FUNCTION_TYPE)
4272 /* Every function declaration is "external"
4273 except for those which are inside a function body
4274 in which `auto' is used.
4275 That is a case not specified by ANSI C,
4276 and we use it for forward declarations for nested functions. */
4277 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4278 || current_scope == global_scope);
4280 if (specbits & (1 << (int) RID_AUTO)
4281 && (pedantic || current_scope == global_scope))
4282 pedwarn ("invalid storage class for function `%s'", name);
4283 if (specbits & (1 << (int) RID_REGISTER))
4284 error ("invalid storage class for function `%s'", name);
4285 if (specbits & (1 << (int) RID_THREAD))
4286 error ("invalid storage class for function `%s'", name);
4287 /* Function declaration not at file scope.
4288 Storage classes other than `extern' are not allowed
4289 and `extern' makes no difference. */
4290 if (current_scope != global_scope
4291 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4293 pedwarn ("invalid storage class for function `%s'", name);
4295 decl = build_decl (FUNCTION_DECL, declarator, type);
4296 decl = build_decl_attribute_variant (decl, decl_attr);
4298 DECL_LANG_SPECIFIC (decl)
4299 = ggc_alloc_cleared (sizeof (struct lang_decl));
4301 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4302 pedwarn ("ISO C forbids qualified function types");
4304 /* GNU C interprets a `volatile void' return type to indicate
4305 that the function does not return. */
4306 if ((type_quals & TYPE_QUAL_VOLATILE)
4307 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4308 warning ("`noreturn' function returns non-void value");
4311 DECL_EXTERNAL (decl) = 1;
4312 /* Record absence of global scope for `static' or `auto'. */
4314 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4317 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4319 /* Record presence of `inline', if it is reasonable. */
4320 if (MAIN_NAME_P (declarator))
4323 warning ("cannot inline function `main'");
4327 /* Record that the function is declared `inline'. */
4328 DECL_DECLARED_INLINE_P (decl) = 1;
4330 /* Do not mark bare declarations as DECL_INLINE. Doing so
4331 in the presence of multiple declarations can result in
4332 the abstract origin pointing between the declarations,
4333 which will confuse dwarf2out. */
4336 DECL_INLINE (decl) = 1;
4337 if (specbits & (1 << (int) RID_EXTERN))
4338 current_extern_inline = 1;
4341 /* If -finline-functions, assume it can be inlined. This does
4342 two things: let the function be deferred until it is actually
4343 needed, and let dwarf2 know that the function is inlinable. */
4344 else if (flag_inline_trees == 2 && initialized)
4345 DECL_INLINE (decl) = 1;
4349 /* It's a variable. */
4350 /* An uninitialized decl with `extern' is a reference. */
4351 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4353 /* Move type qualifiers down to element of an array. */
4354 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4356 int saved_align = TYPE_ALIGN(type);
4357 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4359 TYPE_DOMAIN (type));
4360 TYPE_ALIGN (type) = saved_align;
4362 else if (type_quals)
4363 type = c_build_qualified_type (type, type_quals);
4365 /* It is invalid to create an `extern' declaration for a
4366 variable if there is a global declaration that is
4368 if (extern_ref && current_scope != global_scope)
4372 global_decl = identifier_global_value (declarator);
4374 && TREE_CODE (global_decl) == VAR_DECL
4375 && !TREE_PUBLIC (global_decl))
4376 error ("variable previously declared `static' redeclared "
4380 decl = build_decl (VAR_DECL, declarator, type);
4382 C_DECL_VARIABLE_SIZE (decl) = 1;
4385 pedwarn ("%Hvariable '%D' declared `inline'",
4386 &DECL_SOURCE_LOCATION (decl), decl);
4388 DECL_EXTERNAL (decl) = extern_ref;
4390 /* At file scope, the presence of a `static' or `register' storage
4391 class specifier, or the absence of all storage class specifiers
4392 makes this declaration a definition (perhaps tentative). Also,
4393 the absence of both `static' and `register' makes it public. */
4394 if (current_scope == global_scope)
4396 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4397 | (1 << (int) RID_REGISTER)));
4398 TREE_STATIC (decl) = !extern_ref;
4400 /* Not at file scope, only `static' makes a static definition. */
4403 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4404 TREE_PUBLIC (decl) = extern_ref;
4407 if (specbits & 1 << (int) RID_THREAD)
4409 if (targetm.have_tls)
4410 DECL_THREAD_LOCAL (decl) = 1;
4412 /* A mere warning is sure to result in improper semantics
4413 at runtime. Don't bother to allow this to compile. */
4414 error ("thread-local storage not supported for this target");
4418 /* Record `register' declaration for warnings on &
4419 and in case doing stupid register allocation. */
4421 if (specbits & (1 << (int) RID_REGISTER))
4422 DECL_REGISTER (decl) = 1;
4424 /* Record constancy and volatility. */
4425 c_apply_type_quals_to_decl (type_quals, decl);
4427 /* If a type has volatile components, it should be stored in memory.
4428 Otherwise, the fact that those components are volatile
4429 will be ignored, and would even crash the compiler. */
4430 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4431 c_mark_addressable (decl);
4433 #ifdef ENABLE_CHECKING
4434 /* This is the earliest point at which we might know the assembler
4435 name of a variable. Thus, if it's known before this, die horribly. */
4436 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4440 decl_attributes (&decl, returned_attrs, 0);
4446 /* Decode the parameter-list info for a function type or function definition.
4447 The argument is the value returned by `get_parm_info' (or made in parse.y
4448 if there is an identifier list instead of a parameter decl list).
4449 These two functions are separate because when a function returns
4450 or receives functions then each is called multiple times but the order
4451 of calls is different. The last call to `grokparms' is always the one
4452 that contains the formal parameter names of a function definition.
4454 Store in `last_function_parms' a chain of the decls of parms.
4455 Also store in `last_function_parm_tags' a chain of the struct, union,
4456 and enum tags declared among the parms.
4458 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4460 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4461 a mere declaration. A nonempty identifier-list gets an error message
4462 when FUNCDEF_FLAG is zero. */
4465 grokparms (tree parms_info, int funcdef_flag)
4467 tree first_parm = TREE_CHAIN (parms_info);
4469 last_function_parms = TREE_PURPOSE (parms_info);
4470 last_function_parm_tags = TREE_VALUE (parms_info);
4471 last_function_parm_others = TREE_TYPE (parms_info);
4473 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4474 && !in_system_header)
4475 warning ("function declaration isn't a prototype");
4478 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4481 pedwarn ("parameter names (without types) in function declaration");
4483 last_function_parms = first_parm;
4490 /* If the arg types are incomplete in a declaration,
4491 they must include undefined tags.
4492 These tags can never be defined in the scope of the declaration,
4493 so the types can never be completed,
4494 and no call can be compiled successfully. */
4496 for (parm = last_function_parms, typelt = first_parm;
4498 parm = TREE_CHAIN (parm))
4499 /* Skip over any enumeration constants declared here. */
4500 if (TREE_CODE (parm) == PARM_DECL)
4502 /* Barf if the parameter itself has an incomplete type. */
4503 tree type = TREE_VALUE (typelt);
4504 if (type == error_mark_node)
4506 if (!COMPLETE_TYPE_P (type))
4508 if (funcdef_flag && DECL_NAME (parm) != 0)
4509 error ("parameter `%s' has incomplete type",
4510 IDENTIFIER_POINTER (DECL_NAME (parm)));
4512 warning ("parameter has incomplete type");
4515 TREE_VALUE (typelt) = error_mark_node;
4516 TREE_TYPE (parm) = error_mark_node;
4519 typelt = TREE_CHAIN (typelt);
4526 /* Return a tree_list node with info on a parameter list just parsed.
4527 The TREE_PURPOSE is a list of decls of those parms.
4528 The TREE_VALUE is a list of structure, union and enum tags defined.
4529 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4530 The TREE_TYPE is a list of non-parameter decls which appeared with the
4532 This tree_list node is later fed to `grokparms'.
4534 VOID_AT_END nonzero means append `void' to the end of the type-list.
4535 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4538 get_parm_info (int void_at_end)
4540 tree decl, type, list;
4542 tree *last_type = &types;
4543 tree tags = current_scope->tags;
4544 tree parms = current_scope->parms;
4545 tree others = current_scope->names;
4546 static bool explained_incomplete_types = false;
4547 bool gave_void_only_once_err = false;
4549 /* Just "void" (and no ellipsis) is special. There are really no parms.
4550 But if the "void" is qualified (by "const" or "volatile"), or has a
4551 storage class specifier ("register"), then the behavior is undefined;
4552 issue an error. Typedefs for "void" are OK (see DR#157). */
4553 if (void_at_end && parms != 0
4554 && TREE_CHAIN (parms) == 0
4555 && VOID_TYPE_P (TREE_TYPE (parms))
4556 && !DECL_NAME (parms))
4558 if (TREE_THIS_VOLATILE (parms)
4559 || TREE_READONLY (parms)
4560 || DECL_REGISTER (parms))
4561 error ("\"void\" as only parameter may not be qualified");
4563 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4566 /* Sanity check all of the parameter declarations. */
4567 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4569 if (TREE_CODE (decl) != PARM_DECL)
4571 if (TREE_ASM_WRITTEN (decl))
4574 /* Since there is a prototype, args are passed in their
4575 declared types. The back end may override this. */
4576 type = TREE_TYPE (decl);
4577 DECL_ARG_TYPE (decl) = type;
4578 if (PROMOTE_PROTOTYPES
4579 && INTEGRAL_TYPE_P (type)
4580 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4581 DECL_ARG_TYPE (decl) = integer_type_node;
4583 /* Check for (..., void, ...) and issue an error. */
4584 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4586 error ("\"void\" must be the only parameter");
4587 gave_void_only_once_err = true;
4590 type = build_tree_list (0, type);
4592 last_type = &TREE_CHAIN (type);
4595 /* Check the list of non-parameter decls for any forward parm decls
4596 that never got real decls. */
4597 for (decl = others; decl; decl = TREE_CHAIN (decl))
4598 if (TREE_CODE (decl) == PARM_DECL)
4600 if (!TREE_ASM_WRITTEN (decl))
4603 error ("%Hparameter \"%D\" has just a forward declaration",
4604 &DECL_SOURCE_LOCATION (decl), decl);
4607 /* Warn about any struct, union or enum tags defined within this
4608 list. The scope of such types is limited to this declaration,
4609 which is rarely if ever desirable (it's impossible to call such
4610 a function with type-correct arguments). */
4611 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4613 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4614 const char *keyword;
4615 /* An anonymous union parm type is meaningful as a GNU extension.
4616 So don't warn for that. */
4617 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4620 /* The keyword should not be translated. */
4623 case RECORD_TYPE: keyword = "struct"; break;
4624 case UNION_TYPE: keyword = "union"; break;
4625 case ENUMERAL_TYPE: keyword = "enum"; break;
4629 if (TREE_PURPOSE (decl))
4630 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4631 warning ("\"%s %s\" declared inside parameter list",
4632 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4634 /* The %s will be one of 'struct', 'union', or 'enum'. */
4635 warning ("anonymous %s declared inside parameter list", keyword);
4637 if (! explained_incomplete_types)
4639 warning ("its scope is only this definition or declaration,"
4640 " which is probably not what you want");
4641 explained_incomplete_types = true;
4648 type = build_tree_list (0, void_type_node);
4652 list = tree_cons (parms, tags, types);
4653 TREE_TYPE (list) = others;
4657 /* Get the struct, enum or union (CODE says which) with tag NAME.
4658 Define the tag as a forward-reference if it is not defined. */
4661 xref_tag (enum tree_code code, tree name)
4663 /* If a cross reference is requested, look up the type
4664 already defined for this tag and return it. */
4666 tree ref = lookup_tag (code, name, 0);
4667 /* If this is the right type of tag, return what we found.
4668 (This reference will be shadowed by shadow_tag later if appropriate.)
4669 If this is the wrong type of tag, do not return it. If it was the
4670 wrong type in the same scope, we will have had an error
4671 message already; if in a different scope and declaring
4672 a name, pending_xref_error will give an error message; but if in a
4673 different scope and not declaring a name, this tag should
4674 shadow the previous declaration of a different type of tag, and
4675 this would not work properly if we return the reference found.
4676 (For example, with "struct foo" in an outer scope, "union foo;"
4677 must shadow that tag with a new one of union type.) */
4678 if (ref && TREE_CODE (ref) == code)
4681 /* If no such tag is yet defined, create a forward-reference node
4682 and record it as the "definition".
4683 When a real declaration of this type is found,
4684 the forward-reference will be altered into a real type. */
4686 ref = make_node (code);
4687 if (code == ENUMERAL_TYPE)
4689 /* Give the type a default layout like unsigned int
4690 to avoid crashing if it does not get defined. */
4691 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4692 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4693 TYPE_USER_ALIGN (ref) = 0;
4694 TREE_UNSIGNED (ref) = 1;
4695 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4696 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4697 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4700 pushtag (name, ref);
4705 /* Make sure that the tag NAME is defined *in the current scope*
4706 at least as a forward reference.
4707 CODE says which kind of tag NAME ought to be. */
4710 start_struct (enum tree_code code, tree name)
4712 /* If there is already a tag defined at this scope
4713 (as a forward reference), just return it. */
4718 ref = lookup_tag (code, name, 1);
4719 if (ref && TREE_CODE (ref) == code)
4721 if (TYPE_FIELDS (ref))
4723 if (code == UNION_TYPE)
4724 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4726 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4731 /* Otherwise create a forward-reference just so the tag is in scope. */
4733 ref = make_node (code);
4734 pushtag (name, ref);
4737 C_TYPE_BEING_DEFINED (ref) = 1;
4738 TYPE_PACKED (ref) = flag_pack_struct;
4742 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4743 of a structure component, returning a FIELD_DECL node.
4744 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4746 This is done during the parsing of the struct declaration.
4747 The FIELD_DECL nodes are chained together and the lot of them
4748 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4751 grokfield (tree declarator, tree declspecs, tree width)
4755 if (declarator == NULL_TREE && width == NULL_TREE)
4757 /* This is an unnamed decl.
4759 If we have something of the form "union { list } ;" then this
4760 is the anonymous union extension. Similarly for struct.
4762 If this is something of the form "struct foo;", then
4763 If MS extensions are enabled, this is handled as an
4765 Otherwise this is a forward declaration of a structure tag.
4767 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4768 If MS extensions are enabled and foo names a structure, then
4769 again this is an anonymous struct.
4770 Otherwise this is an error.
4772 Oh what a horrid tangled web we weave. I wonder if MS consciously
4773 took this from Plan 9 or if it was an accident of implementation
4774 that took root before someone noticed the bug... */
4776 tree type = TREE_VALUE (declspecs);
4778 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4779 type = TREE_TYPE (type);
4780 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4782 if (flag_ms_extensions)
4785 goto warn_unnamed_field;
4786 else if (TYPE_NAME (type) == NULL)
4789 goto warn_unnamed_field;
4794 warning ("declaration does not declare anything");
4799 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4801 finish_decl (value, NULL_TREE, NULL_TREE);
4802 DECL_INITIAL (value) = width;
4804 if (c_dialect_objc ())
4805 objc_check_decl (value);
4809 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4810 the list such that this does not present a problem later. */
4813 detect_field_duplicates (tree fieldlist)
4818 /* First, see if there are more than "a few" fields.
4819 This is trivially true if there are zero or one fields. */
4822 x = TREE_CHAIN (fieldlist);
4828 } while (timeout > 0 && x);
4830 /* If there were "few" fields, avoid the overhead of allocating
4831 a hash table. Instead just do the nested traversal thing. */
4834 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4837 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4838 if (DECL_NAME (y) == DECL_NAME (x))
4840 error ("%Hduplicate member '%D'",
4841 &DECL_SOURCE_LOCATION (x), x);
4842 DECL_NAME (x) = NULL_TREE;
4848 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4851 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4852 if ((y = DECL_NAME (x)) != 0)
4854 slot = htab_find_slot (htab, y, INSERT);
4857 error ("%Hduplicate member '%D'",
4858 &DECL_SOURCE_LOCATION (x), x);
4859 DECL_NAME (x) = NULL_TREE;
4868 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4869 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4870 ATTRIBUTES are attributes to be applied to the structure. */
4873 finish_struct (tree t, tree fieldlist, tree attributes)
4876 int toplevel = global_scope == current_scope;
4877 int saw_named_field;
4879 /* If this type was previously laid out as a forward reference,
4880 make sure we lay it out again. */
4884 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4886 /* Nameless union parm types are useful as GCC extension. */
4887 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4888 /* Otherwise, warn about any struct or union def. in parmlist. */
4889 if (in_parm_level_p ())
4892 pedwarn ("%s defined inside parms",
4893 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4895 warning ("%s defined inside parms",
4896 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4901 for (x = fieldlist; x; x = TREE_CHAIN (x))
4902 if (DECL_NAME (x) != 0)
4906 pedwarn ("%s has no %s",
4907 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4908 fieldlist ? _("named members") : _("members"));
4911 /* Install struct as DECL_CONTEXT of each field decl.
4912 Also process specified field sizes,m which is found in the DECL_INITIAL.
4913 Store 0 there, except for ": 0" fields (so we can find them
4914 and delete them, below). */
4916 saw_named_field = 0;
4917 for (x = fieldlist; x; x = TREE_CHAIN (x))
4919 DECL_CONTEXT (x) = t;
4920 DECL_PACKED (x) |= TYPE_PACKED (t);
4922 /* If any field is const, the structure type is pseudo-const. */
4923 if (TREE_READONLY (x))
4924 C_TYPE_FIELDS_READONLY (t) = 1;
4927 /* A field that is pseudo-const makes the structure likewise. */
4928 tree t1 = TREE_TYPE (x);
4929 while (TREE_CODE (t1) == ARRAY_TYPE)
4930 t1 = TREE_TYPE (t1);
4931 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4932 && C_TYPE_FIELDS_READONLY (t1))
4933 C_TYPE_FIELDS_READONLY (t) = 1;
4936 /* Any field that is volatile means variables of this type must be
4937 treated in some ways as volatile. */
4938 if (TREE_THIS_VOLATILE (x))
4939 C_TYPE_FIELDS_VOLATILE (t) = 1;
4941 /* Any field of nominal variable size implies structure is too. */
4942 if (C_DECL_VARIABLE_SIZE (x))
4943 C_TYPE_VARIABLE_SIZE (t) = 1;
4945 /* Detect invalid nested redefinition. */
4946 if (TREE_TYPE (x) == t)
4947 error ("nested redefinition of `%s'",
4948 IDENTIFIER_POINTER (TYPE_NAME (t)));
4950 /* Detect invalid bit-field size. */
4951 if (DECL_INITIAL (x))
4952 STRIP_NOPS (DECL_INITIAL (x));
4953 if (DECL_INITIAL (x))
4955 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4956 constant_expression_warning (DECL_INITIAL (x));
4959 error ("%Hbit-field '%D' width not an integer constant",
4960 &DECL_SOURCE_LOCATION (x), x);
4961 DECL_INITIAL (x) = NULL;
4965 /* Detect invalid bit-field type. */
4966 if (DECL_INITIAL (x)
4967 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4968 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4969 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4971 error ("%Hbit-field '%D' has invalid type",
4972 &DECL_SOURCE_LOCATION (x), x);
4973 DECL_INITIAL (x) = NULL;
4976 if (DECL_INITIAL (x) && pedantic
4977 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4978 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4979 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4980 /* Accept an enum that's equivalent to int or unsigned int. */
4981 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4982 && (TYPE_PRECISION (TREE_TYPE (x))
4983 == TYPE_PRECISION (integer_type_node))))
4984 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
4985 &DECL_SOURCE_LOCATION (x), x);
4987 /* Detect and ignore out of range field width and process valid
4989 if (DECL_INITIAL (x))
4992 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
4993 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
4995 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
4996 error ("%Hnegative width in bit-field '%D'",
4997 &DECL_SOURCE_LOCATION (x), x);
4998 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
4999 pedwarn ("%Hwidth of '%D' exceeds its type",
5000 &DECL_SOURCE_LOCATION (x), x);
5001 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5002 error ("%Hzero width for bit-field '%D'",
5003 &DECL_SOURCE_LOCATION (x), x);
5006 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5007 unsigned HOST_WIDE_INT width
5008 = tree_low_cst (DECL_INITIAL (x), 1);
5010 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5011 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5012 TREE_UNSIGNED (TREE_TYPE (x)))
5014 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5015 TREE_UNSIGNED (TREE_TYPE (x))))))
5016 warning ("%H'%D' is narrower than values of its type",
5017 &DECL_SOURCE_LOCATION (x), x);
5019 DECL_SIZE (x) = bitsize_int (width);
5020 DECL_BIT_FIELD (x) = 1;
5021 SET_DECL_C_BIT_FIELD (x);
5025 DECL_INITIAL (x) = 0;
5027 /* Detect flexible array member in an invalid context. */
5028 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5029 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5030 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5031 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5033 if (TREE_CODE (t) == UNION_TYPE)
5034 error ("%Hflexible array member in union",
5035 &DECL_SOURCE_LOCATION (x));
5036 else if (TREE_CHAIN (x) != NULL_TREE)
5037 error ("%Hflexible array member not at end of struct",
5038 &DECL_SOURCE_LOCATION (x));
5039 else if (! saw_named_field)
5040 error ("%Hflexible array member in otherwise empty struct",
5041 &DECL_SOURCE_LOCATION (x));
5044 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5045 && flexible_array_type_p (TREE_TYPE (x)))
5046 pedwarn ("%Hinvalid use of structure with flexible array member",
5047 &DECL_SOURCE_LOCATION (x));
5050 saw_named_field = 1;
5053 detect_field_duplicates (fieldlist);
5055 /* Now we have the nearly final fieldlist. Record it,
5056 then lay out the structure or union (including the fields). */
5058 TYPE_FIELDS (t) = fieldlist;
5062 /* Delete all zero-width bit-fields from the fieldlist */
5064 tree *fieldlistp = &fieldlist;
5066 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5067 *fieldlistp = TREE_CHAIN (*fieldlistp);
5069 fieldlistp = &TREE_CHAIN (*fieldlistp);
5072 /* Now we have the truly final field list.
5073 Store it in this type and in the variants. */
5075 TYPE_FIELDS (t) = fieldlist;
5077 /* If there are lots of fields, sort so we can look through them fast.
5078 We arbitrarily consider 16 or more elts to be "a lot". */
5083 for (x = fieldlist; x; x = TREE_CHAIN (x))
5085 if (len > 15 || DECL_NAME (x) == NULL)
5093 struct lang_type *space;
5094 struct sorted_fields_type *space2;
5096 len += list_length (x);
5098 /* Use the same allocation policy here that make_node uses, to
5099 ensure that this lives as long as the rest of the struct decl.
5100 All decls in an inline function need to be saved. */
5102 space = ggc_alloc (sizeof (struct lang_type));
5103 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5107 field_array = &space2->elts[0];
5108 for (x = fieldlist; x; x = TREE_CHAIN (x))
5110 field_array[len++] = x;
5112 /* If there is anonymous struct or union, break out of the loop. */
5113 if (DECL_NAME (x) == NULL)
5116 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5119 TYPE_LANG_SPECIFIC (t) = space;
5120 TYPE_LANG_SPECIFIC (t)->s->len = len;
5121 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5122 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5127 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5129 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5130 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5131 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5132 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5135 /* If this was supposed to be a transparent union, but we can't
5136 make it one, warn and turn off the flag. */
5137 if (TREE_CODE (t) == UNION_TYPE
5138 && TYPE_TRANSPARENT_UNION (t)
5139 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5141 TYPE_TRANSPARENT_UNION (t) = 0;
5142 warning ("union cannot be made transparent");
5145 /* If this structure or union completes the type of any previous
5146 variable declaration, lay it out and output its rtl. */
5147 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5151 tree decl = TREE_VALUE (x);
5152 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5153 layout_array_type (TREE_TYPE (decl));
5154 if (TREE_CODE (decl) != TYPE_DECL)
5156 layout_decl (decl, 0);
5157 if (c_dialect_objc ())
5158 objc_check_decl (decl);
5159 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5164 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5166 /* Finish debugging output for this type. */
5167 rest_of_type_compilation (t, toplevel);
5172 /* Lay out the type T, and its element type, and so on. */
5175 layout_array_type (tree t)
5177 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5178 layout_array_type (TREE_TYPE (t));
5182 /* Begin compiling the definition of an enumeration type.
5183 NAME is its name (or null if anonymous).
5184 Returns the type object, as yet incomplete.
5185 Also records info about it so that build_enumerator
5186 may be used to declare the individual values as they are read. */
5189 start_enum (tree name)
5193 /* If this is the real definition for a previous forward reference,
5194 fill in the contents in the same object that used to be the
5195 forward reference. */
5198 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5200 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5202 enumtype = make_node (ENUMERAL_TYPE);
5203 pushtag (name, enumtype);
5206 C_TYPE_BEING_DEFINED (enumtype) = 1;
5208 if (TYPE_VALUES (enumtype) != 0)
5210 /* This enum is a named one that has been declared already. */
5211 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5213 /* Completely replace its old definition.
5214 The old enumerators remain defined, however. */
5215 TYPE_VALUES (enumtype) = 0;
5218 enum_next_value = integer_zero_node;
5221 if (flag_short_enums)
5222 TYPE_PACKED (enumtype) = 1;
5227 /* After processing and defining all the values of an enumeration type,
5228 install their decls in the enumeration type and finish it off.
5229 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5230 and ATTRIBUTES are the specified attributes.
5231 Returns ENUMTYPE. */
5234 finish_enum (tree enumtype, tree values, tree attributes)
5237 tree minnode = 0, maxnode = 0, enum_value_type;
5238 int precision, unsign;
5239 int toplevel = (global_scope == current_scope);
5241 if (in_parm_level_p ())
5242 warning ("enum defined inside parms");
5244 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5246 /* Calculate the maximum value of any enumerator in this type. */
5248 if (values == error_mark_node)
5249 minnode = maxnode = integer_zero_node;
5252 minnode = maxnode = TREE_VALUE (values);
5253 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5255 tree value = TREE_VALUE (pair);
5256 if (tree_int_cst_lt (maxnode, value))
5258 if (tree_int_cst_lt (value, minnode))
5263 /* Construct the final type of this enumeration. It is the same
5264 as one of the integral types - the narrowest one that fits, except
5265 that normally we only go as narrow as int - and signed iff any of
5266 the values are negative. */
5267 unsign = (tree_int_cst_sgn (minnode) >= 0);
5268 precision = MAX (min_precision (minnode, unsign),
5269 min_precision (maxnode, unsign));
5270 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5272 tree narrowest = c_common_type_for_size (precision, unsign);
5275 warning ("enumeration values exceed range of largest integer");
5276 narrowest = long_long_integer_type_node;
5279 precision = TYPE_PRECISION (narrowest);
5282 precision = TYPE_PRECISION (integer_type_node);
5284 if (precision == TYPE_PRECISION (integer_type_node))
5285 enum_value_type = c_common_type_for_size (precision, 0);
5287 enum_value_type = enumtype;
5289 TYPE_MIN_VALUE (enumtype) = minnode;
5290 TYPE_MAX_VALUE (enumtype) = maxnode;
5291 TYPE_PRECISION (enumtype) = precision;
5292 TREE_UNSIGNED (enumtype) = unsign;
5293 TYPE_SIZE (enumtype) = 0;
5294 layout_type (enumtype);
5296 if (values != error_mark_node)
5298 /* Change the type of the enumerators to be the enum type. We
5299 need to do this irrespective of the size of the enum, for
5300 proper type checking. Replace the DECL_INITIALs of the
5301 enumerators, and the value slots of the list, with copies
5302 that have the enum type; they cannot be modified in place
5303 because they may be shared (e.g. integer_zero_node) Finally,
5304 change the purpose slots to point to the names of the decls. */
5305 for (pair = values; pair; pair = TREE_CHAIN (pair))
5307 tree enu = TREE_PURPOSE (pair);
5309 TREE_TYPE (enu) = enumtype;
5311 /* The ISO C Standard mandates enumerators to have type int,
5312 even though the underlying type of an enum type is
5313 unspecified. Here we convert any enumerators that fit in
5314 an int to type int, to avoid promotions to unsigned types
5315 when comparing integers with enumerators that fit in the
5316 int range. When -pedantic is given, build_enumerator()
5317 would have already taken care of those that don't fit. */
5318 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5319 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5321 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5323 TREE_PURPOSE (pair) = DECL_NAME (enu);
5324 TREE_VALUE (pair) = DECL_INITIAL (enu);
5327 TYPE_VALUES (enumtype) = values;
5330 /* Fix up all variant types of this enum type. */
5331 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5333 if (tem == enumtype)
5335 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5336 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5337 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5338 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5339 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5340 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5341 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5342 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5343 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5344 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5347 /* Finish debugging output for this type. */
5348 rest_of_type_compilation (enumtype, toplevel);
5353 /* Build and install a CONST_DECL for one value of the
5354 current enumeration type (one that was begun with start_enum).
5355 Return a tree-list containing the CONST_DECL and its value.
5356 Assignment of sequential values by default is handled here. */
5359 build_enumerator (tree name, tree value)
5363 /* Validate and default VALUE. */
5365 /* Remove no-op casts from the value. */
5367 STRIP_TYPE_NOPS (value);
5371 if (TREE_CODE (value) == INTEGER_CST)
5373 value = default_conversion (value);
5374 constant_expression_warning (value);
5378 error ("enumerator value for `%s' not integer constant",
5379 IDENTIFIER_POINTER (name));
5384 /* Default based on previous value. */
5385 /* It should no longer be possible to have NON_LVALUE_EXPR
5389 value = enum_next_value;
5391 error ("overflow in enumeration values");
5394 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5396 pedwarn ("ISO C restricts enumerator values to range of `int'");
5397 value = convert (integer_type_node, value);
5400 /* Set basis for default for next value. */
5401 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5402 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5404 /* Now create a declaration for the enum value name. */
5406 type = TREE_TYPE (value);
5407 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5408 TYPE_PRECISION (integer_type_node)),
5409 (TYPE_PRECISION (type)
5410 >= TYPE_PRECISION (integer_type_node)
5411 && TREE_UNSIGNED (type)));
5413 decl = build_decl (CONST_DECL, name, type);
5414 DECL_INITIAL (decl) = convert (type, value);
5417 return tree_cons (decl, value, NULL_TREE);
5421 /* Create the FUNCTION_DECL for a function definition.
5422 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5423 the declaration; they describe the function's name and the type it returns,
5424 but twisted together in a fashion that parallels the syntax of C.
5426 This function creates a binding context for the function body
5427 as well as setting up the FUNCTION_DECL in current_function_decl.
5429 Returns 1 on success. If the DECLARATOR is not suitable for a function
5430 (it defines a datum instead), we return 0, which tells
5431 yyparse to report a parse error. */
5434 start_function (tree declspecs, tree declarator, tree attributes)
5436 tree decl1, old_decl;
5438 int old_immediate_size_expand = immediate_size_expand;
5440 current_function_returns_value = 0; /* Assume, until we see it does. */
5441 current_function_returns_null = 0;
5442 current_function_returns_abnormally = 0;
5443 warn_about_return_type = 0;
5444 current_extern_inline = 0;
5446 /* Don't expand any sizes in the return type of the function. */
5447 immediate_size_expand = 0;
5449 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5451 /* If the declarator is not suitable for a function definition,
5452 cause a syntax error. */
5455 immediate_size_expand = old_immediate_size_expand;
5459 decl_attributes (&decl1, attributes, 0);
5461 if (DECL_DECLARED_INLINE_P (decl1)
5462 && DECL_UNINLINABLE (decl1)
5463 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5464 warning ("%Hinline function '%D' given attribute noinline",
5465 &DECL_SOURCE_LOCATION (decl1), decl1);
5467 announce_function (decl1);
5469 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5471 error ("return type is an incomplete type");
5472 /* Make it return void instead. */
5474 = build_function_type (void_type_node,
5475 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5478 if (warn_about_return_type)
5479 pedwarn_c99 ("return type defaults to `int'");
5481 /* Save the parm names or decls from this function's declarator
5482 where store_parm_decls will find them. */
5483 current_function_parms = last_function_parms;
5484 current_function_parm_tags = last_function_parm_tags;
5485 current_function_parm_others = last_function_parm_others;
5487 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5488 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5489 DECL_INITIAL (decl1) = error_mark_node;
5491 /* If this definition isn't a prototype and we had a prototype declaration
5492 before, copy the arg type info from that prototype.
5493 But not if what we had before was a builtin function. */
5494 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5495 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5496 && !DECL_BUILT_IN (old_decl)
5497 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5498 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5499 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5501 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5502 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5505 /* Optionally warn of old-fashioned def with no previous prototype. */
5506 if (warn_strict_prototypes
5507 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5508 && C_DECL_ISNT_PROTOTYPE (old_decl))
5509 warning ("function declaration isn't a prototype");
5510 /* Optionally warn of any global def with no previous prototype. */
5511 else if (warn_missing_prototypes
5512 && TREE_PUBLIC (decl1)
5513 && ! MAIN_NAME_P (DECL_NAME (decl1))
5514 && C_DECL_ISNT_PROTOTYPE (old_decl))
5515 warning ("%Hno previous prototype for '%D'",
5516 &DECL_SOURCE_LOCATION (decl1), decl1);
5517 /* Optionally warn of any def with no previous prototype
5518 if the function has already been used. */
5519 else if (warn_missing_prototypes
5520 && old_decl != 0 && TREE_USED (old_decl)
5521 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5522 warning ("%H'%D' was used with no prototype before its definition",
5523 &DECL_SOURCE_LOCATION (decl1), decl1);
5524 /* Optionally warn of any global def with no previous declaration. */
5525 else if (warn_missing_declarations
5526 && TREE_PUBLIC (decl1)
5528 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5529 warning ("%Hno previous declaration for '%D'",
5530 &DECL_SOURCE_LOCATION (decl1), decl1);
5531 /* Optionally warn of any def with no previous declaration
5532 if the function has already been used. */
5533 else if (warn_missing_declarations
5534 && old_decl != 0 && TREE_USED (old_decl)
5535 && C_DECL_IMPLICIT (old_decl))
5536 warning ("%H`%D' was used with no declaration before its definition",
5537 &DECL_SOURCE_LOCATION (decl1), decl1);
5539 /* This is a definition, not a reference.
5540 So normally clear DECL_EXTERNAL.
5541 However, `extern inline' acts like a declaration
5542 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5543 DECL_EXTERNAL (decl1) = current_extern_inline;
5545 /* This function exists in static storage.
5546 (This does not mean `static' in the C sense!) */
5547 TREE_STATIC (decl1) = 1;
5549 /* A nested function is not global. */
5550 if (current_function_decl != 0)
5551 TREE_PUBLIC (decl1) = 0;
5553 #ifdef ENABLE_CHECKING
5554 /* This is the earliest point at which we might know the assembler
5555 name of the function. Thus, if it's set before this, die horribly. */
5556 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5560 /* If #pragma weak was used, mark the decl weak now. */
5561 if (current_scope == global_scope)
5562 maybe_apply_pragma_weak (decl1);
5564 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5565 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5569 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5571 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5572 != integer_type_node)
5573 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5575 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5576 args = TREE_CHAIN (args))
5578 tree type = args ? TREE_VALUE (args) : 0;
5580 if (type == void_type_node)
5587 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5588 pedwarn ("%Hfirst argument of '%D' should be `int'",
5593 if (TREE_CODE (type) != POINTER_TYPE
5594 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5595 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5597 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5602 if (TREE_CODE (type) != POINTER_TYPE
5603 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5604 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5606 pedwarn ("%Hthird argument of '%D' should probably be "
5607 "'char **'", locus, decl1);
5612 /* It is intentional that this message does not mention the third
5613 argument because it's only mentioned in an appendix of the
5615 if (argct > 0 && (argct < 2 || argct > 3))
5616 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5618 if (! TREE_PUBLIC (decl1))
5619 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5622 /* Record the decl so that the function name is defined.
5623 If we already have a decl for this name, and it is a FUNCTION_DECL,
5624 use the old decl. */
5626 current_function_decl = pushdecl (decl1);
5629 declare_parm_level ();
5631 make_decl_rtl (current_function_decl, NULL);
5633 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5634 /* Promote the value to int before returning it. */
5635 if (c_promoting_integer_type_p (restype))
5637 /* It retains unsignedness if not really getting wider. */
5638 if (TREE_UNSIGNED (restype)
5639 && (TYPE_PRECISION (restype)
5640 == TYPE_PRECISION (integer_type_node)))
5641 restype = unsigned_type_node;
5643 restype = integer_type_node;
5645 DECL_RESULT (current_function_decl)
5646 = build_decl (RESULT_DECL, NULL_TREE, restype);
5648 /* If this fcn was already referenced via a block-scope `extern' decl
5649 (or an implicit decl), propagate certain information about the usage. */
5650 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5651 TREE_ADDRESSABLE (current_function_decl) = 1;
5653 immediate_size_expand = old_immediate_size_expand;
5655 start_fname_decls ();
5660 /* Subroutine of store_parm_decls which handles new-style function
5661 definitions (prototype format). The parms already have decls, so we
5662 need only record them as in effect and complain if any redundant
5663 old-style parm decls were written. */
5665 store_parm_decls_newstyle (void)
5668 tree fndecl = current_function_decl;
5669 tree parms = current_function_parms;
5670 tree tags = current_function_parm_tags;
5671 tree others = current_function_parm_others;
5673 if (current_scope->parms || current_scope->names || current_scope->tags)
5675 error ("%Hold-style parameter declarations in prototyped "
5676 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5678 /* Get rid of the old-style declarations. */
5683 /* Now make all the parameter declarations visible in the function body.
5684 We can bypass most of the grunt work of pushdecl. */
5685 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5687 DECL_CONTEXT (decl) = current_function_decl;
5688 if (DECL_NAME (decl) == 0)
5689 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5692 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5693 current_scope->shadowed
5694 = tree_cons (DECL_NAME (decl),
5695 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5696 current_scope->shadowed);
5697 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5700 current_scope->parms = parms;
5701 current_scope->parms_last = last;
5703 /* Record the parameter list in the function declaration. */
5704 DECL_ARGUMENTS (fndecl) = parms;
5706 /* Now make all the ancillary declarations visible, likewise. */
5707 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5709 DECL_CONTEXT (decl) = current_function_decl;
5710 if (DECL_NAME (decl)
5711 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5713 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5714 current_scope->shadowed
5715 = tree_cons (DECL_NAME (decl),
5716 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5717 current_scope->shadowed);
5718 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5721 current_scope->names = others;
5722 current_scope->names_last = last;
5724 /* And all the tag declarations. */
5725 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5726 if (TREE_PURPOSE (decl))
5728 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5729 current_scope->shadowed_tags
5730 = tree_cons (TREE_PURPOSE (decl),
5731 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5732 current_scope->shadowed_tags);
5733 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5735 current_scope->tags = tags;
5738 /* Subroutine of store_parm_decls which handles old-style function
5739 definitions (separate parameter list and declarations). */
5742 store_parm_decls_oldstyle (void)
5744 tree parm, decl, last;
5745 tree fndecl = current_function_decl;
5747 /* This is the identifier list from the function declarator. */
5748 tree parmids = current_function_parms;
5750 /* We use DECL_WEAK as a flag to show which parameters have been
5751 seen already, since it is not used on PARM_DECL. */
5752 #ifdef ENABLE_CHECKING
5753 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5754 if (DECL_WEAK (parm))
5758 /* Match each formal parameter name with its declaration. Save each
5759 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5760 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5762 if (TREE_VALUE (parm) == 0)
5764 error ("%Hparameter name missing from parameter list",
5765 &DECL_SOURCE_LOCATION (fndecl));
5766 TREE_PURPOSE (parm) = 0;
5770 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5771 if (decl && DECL_CONTEXT (decl) == fndecl)
5773 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5774 /* If we got something other than a PARM_DECL it is an error. */
5775 if (TREE_CODE (decl) != PARM_DECL)
5776 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5777 /* If the declaration is already marked, we have a duplicate
5778 name. Complain and ignore the duplicate. */
5779 else if (DECL_WEAK (decl))
5781 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5782 TREE_PURPOSE (parm) = 0;
5785 /* If the declaration says "void", complain and turn it into
5787 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5789 error ("%Hparameter \"%D\" declared void", locus, decl);
5790 TREE_TYPE (decl) = integer_type_node;
5791 DECL_ARG_TYPE (decl) = integer_type_node;
5792 layout_decl (decl, 0);
5795 /* If no declaration found, default to int. */
5798 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5799 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5800 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5801 DECL_SOURCE_LOCATION (decl) = *locus;
5805 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5806 else if (extra_warnings)
5807 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5810 TREE_PURPOSE (parm) = decl;
5811 DECL_WEAK (decl) = 1;
5814 /* Now examine the parms chain for incomplete declarations
5815 and declarations with no corresponding names. */
5817 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5819 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5821 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5823 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5824 TREE_TYPE (parm) = error_mark_node;
5827 if (! DECL_WEAK (parm))
5829 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5832 /* Pretend the parameter was not missing.
5833 This gets us to a standard state and minimizes
5834 further error messages. */
5835 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5839 /* Chain the declarations together in the order of the list of
5840 names. Store that chain in the function decl, replacing the
5841 list of names. Update the current scope to match. */
5842 DECL_ARGUMENTS (fndecl) = 0;
5844 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5845 if (TREE_PURPOSE (parm))
5847 if (parm && TREE_PURPOSE (parm))
5849 last = TREE_PURPOSE (parm);
5850 DECL_ARGUMENTS (fndecl) = last;
5851 current_scope->parms = last;
5852 DECL_WEAK (last) = 0;
5854 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5855 if (TREE_PURPOSE (parm))
5857 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5858 last = TREE_PURPOSE (parm);
5859 DECL_WEAK (last) = 0;
5861 current_scope->parms_last = last;
5862 TREE_CHAIN (last) = 0;
5865 /* If there was a previous prototype,
5866 set the DECL_ARG_TYPE of each argument according to
5867 the type previously specified, and report any mismatches. */
5869 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5872 for (parm = DECL_ARGUMENTS (fndecl),
5873 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5874 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5875 != void_type_node));
5876 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5878 if (parm == 0 || type == 0
5879 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5881 error ("number of arguments doesn't match prototype");
5882 error ("%Hprototype declaration",
5883 ¤t_function_prototype_locus);
5886 /* Type for passing arg must be consistent with that
5887 declared for the arg. ISO C says we take the unqualified
5888 type for parameters declared with qualified type. */
5889 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5890 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5893 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5894 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5896 /* Adjust argument to match prototype. E.g. a previous
5897 `int foo(float);' prototype causes
5898 `int foo(x) float x; {...}' to be treated like
5899 `int foo(float x) {...}'. This is particularly
5900 useful for argument types like uid_t. */
5901 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5903 if (PROMOTE_PROTOTYPES
5904 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5905 && TYPE_PRECISION (TREE_TYPE (parm))
5906 < TYPE_PRECISION (integer_type_node))
5907 DECL_ARG_TYPE (parm) = integer_type_node;
5911 pedwarn ("promoted argument \"%D\" "
5912 "doesn't match prototype", parm);
5913 pedwarn ("%Hprototype declaration",
5914 ¤t_function_prototype_locus);
5919 error ("argument \"%D\" doesn't match prototype", parm);
5920 error ("%Hprototype declaration",
5921 ¤t_function_prototype_locus);
5925 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5928 /* Otherwise, create a prototype that would match. */
5932 tree actual = 0, last = 0, type;
5934 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5936 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5938 TREE_CHAIN (last) = type;
5943 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5945 TREE_CHAIN (last) = type;
5949 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5950 of the type of this function, but we need to avoid having this
5951 affect the types of other similarly-typed functions, so we must
5952 first force the generation of an identical (but separate) type
5953 node for the relevant function type. The new node we create
5954 will be a variant of the main variant of the original function
5957 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5959 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5963 /* Store the parameter declarations into the current function declaration.
5964 This is called after parsing the parameter declarations, before
5965 digesting the body of the function.
5967 For an old-style definition, construct a prototype out of the old-style
5968 parameter declarations and inject it into the function's type. */
5971 store_parm_decls (void)
5973 tree fndecl = current_function_decl;
5975 /* The function containing FNDECL, if any. */
5976 tree context = decl_function_context (fndecl);
5978 /* True if this definition is written with a prototype. */
5979 bool prototype = (current_function_parms
5980 && TREE_CODE (current_function_parms) != TREE_LIST);
5983 store_parm_decls_newstyle ();
5985 store_parm_decls_oldstyle ();
5987 /* The next call to pushlevel will be a function body. */
5989 next_is_function_body = true;
5991 /* Write a record describing this function definition to the prototypes
5992 file (if requested). */
5994 gen_aux_info_record (fndecl, 1, 0, prototype);
5996 /* Initialize the RTL code for the function. */
5997 allocate_struct_function (fndecl);
5999 /* Begin the statement tree for this function. */
6000 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6002 /* If this is a nested function, save away the sizes of any
6003 variable-size types so that we can expand them when generating
6009 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6010 = nreverse (get_pending_sizes ());
6011 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6014 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6017 /* This function is being processed in whole-function mode. */
6018 cfun->x_whole_function_mode_p = 1;
6020 /* Even though we're inside a function body, we still don't want to
6021 call expand_expr to calculate the size of a variable-sized array.
6022 We haven't necessarily assigned RTL to all variables yet, so it's
6023 not safe to try to expand expressions involving them. */
6024 immediate_size_expand = 0;
6025 cfun->x_dont_save_pending_sizes_p = 1;
6028 /* Finish up a function declaration and compile that function
6029 all the way to assembler language output. The free the storage
6030 for the function definition.
6032 This is called after parsing the body of the function definition.
6034 NESTED is nonzero if the function being finished is nested in another.
6035 CAN_DEFER_P is nonzero if the function may be deferred. */
6038 finish_function (int nested, int can_defer_p)
6040 tree fndecl = current_function_decl;
6042 /* When a function declaration is totally empty, e.g.
6044 (the argument list is irrelevant) the compstmt rule will not
6045 bother calling pushlevel/poplevel, which means we get here with
6046 the scope stack out of sync. Detect this situation by noticing
6047 that current_scope is still as store_parm_decls left it, and do
6048 a dummy push/pop to get back to consistency.
6049 Note that the call to pushlevel does not actually push another
6050 scope - see there for details. */
6052 if (current_scope->parm_flag && next_is_function_body)
6058 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6060 /* Must mark the RESULT_DECL as being in this function. */
6062 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6064 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6066 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6067 != integer_type_node)
6069 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6070 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6072 pedwarn ("%Hreturn type of '%D' is not `int'",
6073 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6077 #ifdef DEFAULT_MAIN_RETURN
6078 /* Make it so that `main' always returns success by default. */
6079 DEFAULT_MAIN_RETURN;
6082 c_expand_return (integer_zero_node);
6087 finish_fname_decls ();
6089 /* Tie off the statement tree for this function. */
6090 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6092 /* Complain if there's just no return statement. */
6093 if (warn_return_type
6094 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6095 && !current_function_returns_value && !current_function_returns_null
6096 /* Don't complain if we abort. */
6097 && !current_function_returns_abnormally
6098 /* Don't warn for main(). */
6099 && !MAIN_NAME_P (DECL_NAME (fndecl))
6100 /* Or if they didn't actually specify a return type. */
6101 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6102 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6103 inline function, as we might never be compiled separately. */
6104 && DECL_INLINE (fndecl))
6105 warning ("no return statement in function returning non-void");
6107 /* We're leaving the context of this function, so zap cfun. It's still in
6108 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6111 if (flag_unit_at_a_time && can_defer_p)
6113 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6114 current_function_decl = NULL;
6120 /* Function is parsed.
6121 Generate RTL for the body of this function or defer
6122 it for later expansion. */
6123 bool uninlinable = true;
6125 /* There's no reason to do any of the work here if we're only doing
6126 semantic analysis; this code just generates RTL. */
6127 if (flag_syntax_only)
6129 current_function_decl = NULL;
6130 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6134 if (flag_inline_trees)
6136 /* First, cache whether the current function is inlinable. Some
6137 predicates depend on cfun and current_function_decl to
6138 function completely. */
6139 timevar_push (TV_INTEGRATION);
6140 uninlinable = !tree_inlinable_function_p (fndecl);
6143 /* We defer functions marked inline *even if* the function
6144 itself is not inlinable. This is because we don't yet
6145 know if the function will actually be used; we may be
6146 able to avoid emitting it entirely. */
6147 && (!uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6148 /* Save function tree for inlining. Should return 0 if the
6149 language does not support function deferring or the
6150 function could not be deferred. */
6151 && defer_fn (fndecl))
6153 /* Let the back-end know that this function exists. */
6154 (*debug_hooks->deferred_inline_function) (fndecl);
6155 timevar_pop (TV_INTEGRATION);
6156 current_function_decl = NULL;
6160 /* Then, inline any functions called in it. */
6161 optimize_inline_calls (fndecl);
6162 timevar_pop (TV_INTEGRATION);
6165 c_expand_body (fndecl);
6167 /* Keep the function body if it's needed for inlining or dumping. */
6168 if (uninlinable && !dump_enabled_p (TDI_all))
6170 /* Allow the body of the function to be garbage collected. */
6171 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6174 /* Let the error reporting routines know that we're outside a
6175 function. For a nested function, this value is used in
6176 c_pop_function_context and then reset via pop_function_context. */
6177 current_function_decl = NULL;
6181 /* Generate the RTL for a deferred function FNDECL. */
6184 c_expand_deferred_function (tree fndecl)
6186 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6187 function was deferred, e.g. in duplicate_decls. */
6188 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6190 if (flag_inline_trees)
6192 timevar_push (TV_INTEGRATION);
6193 optimize_inline_calls (fndecl);
6194 timevar_pop (TV_INTEGRATION);
6196 c_expand_body (fndecl);
6197 current_function_decl = NULL;
6201 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6202 then we are already in the process of generating RTL for another
6203 function. If can_defer_p is zero, we won't attempt to defer the
6204 generation of RTL. */
6207 c_expand_body_1 (tree fndecl, int nested_p)
6211 /* Make sure that we will evaluate variable-sized types involved
6212 in our function's type. */
6213 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6216 tree_rest_of_compilation (fndecl);
6218 /* With just -Wextra, complain only if function returns both with
6219 and without a value. */
6221 && current_function_returns_value
6222 && current_function_returns_null)
6223 warning ("this function may return with or without a value");
6225 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6227 if (targetm.have_ctors_dtors)
6228 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6229 DEFAULT_INIT_PRIORITY);
6231 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6234 if (DECL_STATIC_DESTRUCTOR (fndecl))
6236 if (targetm.have_ctors_dtors)
6237 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6238 DEFAULT_INIT_PRIORITY);
6240 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6244 /* Like c_expand_body_1 but only for unnested functions. */
6247 c_expand_body (tree fndecl)
6249 c_expand_body_1 (fndecl, 0);
6252 /* Check the declarations given in a for-loop for satisfying the C99
6255 check_for_loop_decls (void)
6261 /* If we get here, declarations have been used in a for loop without
6262 the C99 for loop scope. This doesn't make much sense, so don't
6264 error ("'for' loop initial declaration used outside C99 mode");
6267 /* C99 subclause 6.8.5 paragraph 3:
6269 [#3] The declaration part of a for statement shall only
6270 declare identifiers for objects having storage class auto or
6273 It isn't clear whether, in this sentence, "identifiers" binds to
6274 "shall only declare" or to "objects" - that is, whether all identifiers
6275 declared must be identifiers for objects, or whether the restriction
6276 only applies to those that are. (A question on this in comp.std.c
6277 in November 2000 received no answer.) We implement the strictest
6278 interpretation, to avoid creating an extension which later causes
6281 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6283 if (TREE_PURPOSE (t) != 0)
6285 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6287 if (code == RECORD_TYPE)
6288 error ("'struct %s' declared in 'for' loop initial declaration",
6289 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6290 else if (code == UNION_TYPE)
6291 error ("'union %s' declared in 'for' loop initial declaration",
6292 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6294 error ("'enum %s' declared in 'for' loop initial declaration",
6295 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6299 for (t = getdecls (); t; t = TREE_CHAIN (t))
6301 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6302 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6303 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6304 "initial declaration", locus, t);
6305 else if (TREE_STATIC (t))
6306 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6307 "initial declaration", locus, t);
6308 else if (DECL_EXTERNAL (t))
6309 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6310 "initial declaration", locus, t);
6314 /* Save and restore the variables in this file and elsewhere
6315 that keep track of the progress of compilation of the current function.
6316 Used for nested functions. */
6318 struct language_function GTY(())
6320 struct c_language_function base;
6323 int returns_abnormally;
6324 int warn_about_return_type;
6328 /* Save and reinitialize the variables
6329 used during compilation of a C function. */
6332 c_push_function_context (struct function *f)
6334 struct language_function *p;
6335 p = ggc_alloc (sizeof (struct language_function));
6338 p->base.x_stmt_tree = c_stmt_tree;
6339 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6340 p->returns_value = current_function_returns_value;
6341 p->returns_null = current_function_returns_null;
6342 p->returns_abnormally = current_function_returns_abnormally;
6343 p->warn_about_return_type = warn_about_return_type;
6344 p->extern_inline = current_extern_inline;
6347 /* Restore the variables used during compilation of a C function. */
6350 c_pop_function_context (struct function *f)
6352 struct language_function *p = f->language;
6354 if (DECL_SAVED_INSNS (current_function_decl) == 0
6355 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6357 /* Stop pointing to the local nodes about to be freed. */
6358 /* But DECL_INITIAL must remain nonzero so we know this
6359 was an actual function definition. */
6360 DECL_INITIAL (current_function_decl) = error_mark_node;
6361 DECL_ARGUMENTS (current_function_decl) = 0;
6364 c_stmt_tree = p->base.x_stmt_tree;
6365 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6366 current_function_returns_value = p->returns_value;
6367 current_function_returns_null = p->returns_null;
6368 current_function_returns_abnormally = p->returns_abnormally;
6369 warn_about_return_type = p->warn_about_return_type;
6370 current_extern_inline = p->extern_inline;
6375 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6378 c_dup_lang_specific_decl (tree decl)
6380 struct lang_decl *ld;
6382 if (!DECL_LANG_SPECIFIC (decl))
6385 ld = ggc_alloc (sizeof (struct lang_decl));
6386 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6387 DECL_LANG_SPECIFIC (decl) = ld;
6390 /* The functions below are required for functionality of doing
6391 function at once processing in the C front end. Currently these
6392 functions are not called from anywhere in the C front end, but as
6393 these changes continue, that will change. */
6395 /* Returns nonzero if the current statement is a full expression,
6396 i.e. temporaries created during that statement should be destroyed
6397 at the end of the statement. */
6400 stmts_are_full_exprs_p (void)
6405 /* Returns the stmt_tree (if any) to which statements are currently
6406 being added. If there is no active statement-tree, NULL is
6410 current_stmt_tree (void)
6412 return &c_stmt_tree;
6415 /* Returns the stack of SCOPE_STMTs for the current function. */
6418 current_scope_stmt_stack (void)
6420 return &c_scope_stmt_stack;
6423 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6427 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6432 /* Dummy function in place of callback used by C++. */
6435 extract_interface_info (void)
6439 /* Return a new COMPOUND_STMT, after adding it to the current
6443 c_begin_compound_stmt (void)
6447 /* Create the COMPOUND_STMT. */
6448 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6453 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6457 c_expand_decl_stmt (tree t)
6459 tree decl = DECL_STMT_DECL (t);
6461 /* Expand nested functions. */
6462 if (TREE_CODE (decl) == FUNCTION_DECL
6463 && DECL_CONTEXT (decl) == current_function_decl
6464 && DECL_SAVED_TREE (decl))
6465 c_expand_body_1 (decl, 1);
6468 /* Return the global value of T as a symbol. */
6471 identifier_global_value (tree t)
6473 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6474 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6477 /* Shadowed by something else; find the true global value. */
6478 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6479 if (DECL_NAME (decl) == t)
6482 /* Only local values for this decl. */
6486 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6487 otherwise the name is found in ridpointers from RID_INDEX. */
6490 record_builtin_type (enum rid rid_index, const char *name, tree type)
6494 id = ridpointers[(int) rid_index];
6496 id = get_identifier (name);
6497 pushdecl (build_decl (TYPE_DECL, id, type));
6500 /* Build the void_list_node (void_type_node having been created). */
6502 build_void_list_node (void)
6504 tree t = build_tree_list (NULL_TREE, void_type_node);
6508 /* Return something to represent absolute declarators containing a *.
6509 TARGET is the absolute declarator that the * contains.
6510 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6511 to apply to the pointer type, represented as identifiers, possible mixed
6514 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6515 if attributes are present) and whose type is the modifier list. */
6518 make_pointer_declarator (tree type_quals_attrs, tree target)
6521 tree itarget = target;
6522 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6523 if (attrs != NULL_TREE)
6524 itarget = tree_cons (attrs, target, NULL_TREE);
6525 return build1 (INDIRECT_REF, quals, itarget);
6528 /* A wrapper around lhd_set_decl_assembler_name that gives static
6529 variables their C names if they are at file scope and only one
6530 translation unit is being compiled, for backwards compatibility
6531 with certain bizarre assembler hacks (like crtstuff.c). */
6534 c_static_assembler_name (tree decl)
6536 if (num_in_fnames == 1
6537 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6538 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6539 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6541 lhd_set_decl_assembler_name (decl);
6544 /* Hash and equality functions for link_hash_table: key off
6545 DECL_ASSEMBLER_NAME. */
6548 link_hash_hash (const void *x_p)
6551 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6555 link_hash_eq (const void *x1_p, const void *x2_p)
6557 tree x1 = (tree)x1_p;
6558 tree x2 = (tree)x2_p;
6559 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6562 /* Propagate information between definitions and uses between multiple
6563 translation units in TU_LIST based on linkage rules. */
6566 merge_translation_unit_decls (void)
6568 const tree tu_list = current_file_decl;
6571 htab_t link_hash_table;
6574 /* Create the BLOCK that poplevel would have created, but don't
6575 actually call poplevel since that's expensive. */
6576 block = make_node (BLOCK);
6577 BLOCK_VARS (block) = current_scope->names;
6578 TREE_USED (block) = 1;
6579 DECL_INITIAL (current_file_decl) = block;
6581 /* If only one translation unit seen, no copying necessary. */
6582 if (TREE_CHAIN (tu_list) == NULL_TREE)
6585 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6587 /* Enter any actual definitions into the hash table. */
6588 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6589 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6590 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6593 slot = htab_find_slot (link_hash_table, decl, INSERT);
6595 /* If we've already got a definition, work out which one is
6596 the real one, put it into the hash table, and make the
6597 other one DECL_EXTERNAL. This is important to avoid
6598 putting out two definitions of the same symbol in the
6602 tree old_decl = (tree) *slot;
6604 /* If this is weak or common or whatever, suppress it
6605 in favor of the other definition. */
6606 if (DECL_WEAK (decl))
6607 DECL_EXTERNAL (decl) = 1;
6608 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6609 DECL_EXTERNAL (old_decl) = 1;
6610 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6611 DECL_EXTERNAL (decl) = 1;
6612 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6613 DECL_EXTERNAL (old_decl) = 1;
6615 if (DECL_EXTERNAL (decl))
6617 DECL_INITIAL (decl) = NULL_TREE;
6618 DECL_COMMON (decl) = 0;
6619 DECL_ONE_ONLY (decl) = 0;
6620 DECL_WEAK (decl) = 0;
6622 else if (DECL_EXTERNAL (old_decl))
6624 DECL_INITIAL (old_decl) = NULL_TREE;
6625 DECL_COMMON (old_decl) = 0;
6626 DECL_ONE_ONLY (old_decl) = 0;
6627 DECL_WEAK (old_decl) = 0;
6632 error ("%Hredefinition of global '%D'",
6633 &DECL_SOURCE_LOCATION (decl), decl);
6634 error ("%H'%D' previously defined here",
6635 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6642 /* Now insert the desired information from all the definitions
6643 into any plain declarations. */
6644 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6645 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6646 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6649 global_decl = htab_find (link_hash_table, decl);
6654 /* Print any appropriate error messages, and partially merge
6656 (void) duplicate_decls (decl, global_decl, true, true);
6659 htab_delete (link_hash_table);
6662 /* Perform final processing on file-scope data. */
6665 c_write_global_declarations(void)
6669 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6671 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6672 int len = list_length (globals);
6673 tree *vec = xmalloc (sizeof (tree) * len);
6677 /* Process the decls in the order they were written. */
6679 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6682 wrapup_global_declarations (vec, len);
6684 check_global_declarations (vec, len);
6691 /* Reset the parser's state in preparation for a new file. */
6694 c_reset_state (void)
6697 tree file_scope_decl;
6699 /* Pop the global scope. */
6700 if (current_scope != global_scope)
6701 current_scope = global_scope;
6702 file_scope_decl = current_file_decl;
6703 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6704 truly_local_externals = NULL_TREE;
6706 /* Start a new global binding level. */
6708 global_scope = current_scope;
6709 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6710 TREE_CHAIN (current_file_decl) = file_scope_decl;
6712 /* Reintroduce the builtin declarations. */
6713 for (link = first_builtin_decl;
6714 link != TREE_CHAIN (last_builtin_decl);
6715 link = TREE_CHAIN (link))
6716 pushdecl (copy_node (link));
6719 #include "gt-c-decl.h"