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"
55 #include "langhooks-def.h"
57 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 { NORMAL, /* Ordinary declaration */
60 FUNCDEF, /* Function definition */
61 PARM, /* Declaration of parm before function body */
62 FIELD, /* Declaration inside struct or union */
63 BITFIELD, /* Likewise but with specified width */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
80 static tree enum_next_value;
82 /* Nonzero means that there was overflow computing enum_next_value. */
84 static int enum_overflow;
86 /* Parsing a function declarator leaves a list of parameter names
87 or a chain of parameter decls here. */
89 static tree last_function_parms;
91 /* ... and a chain of structure and enum types declared in the
94 static tree last_function_parm_tags;
96 /* ... and a chain of all non-parameter declarations (such as
97 CONST_DECLs from enumerations) here. */
99 static tree last_function_parm_vars;
101 /* After parsing the declarator that starts a function definition,
102 `start_function' puts the list of parameter names or chain of decls here
103 for `store_parm_decls' to find. */
105 static tree current_function_parms;
107 /* Similar, for last_function_parm_tags. */
109 static tree current_function_parm_tags;
111 /* And for last_function_parm_vars. */
113 static tree current_function_parm_vars;
115 /* Similar, for the file and line that the prototype came from if this is
116 an old-style definition. */
118 static location_t current_function_prototype_locus;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* The current scope statement stack. */
126 static GTY(()) tree c_scope_stmt_stack;
128 /* A list of external DECLs that appeared at block scope when there was
129 some other global meaning for that identifier. */
130 static GTY(()) tree truly_local_externals;
132 /* A list of the builtin file-scope DECLs. */
134 static GTY(()) tree builtin_decls;
136 /* A DECL for the current file-scope context. */
138 static GTY(()) tree current_file_decl;
140 /* Set to 0 at beginning of a function definition, set to 1 if
141 a return statement that specifies a return value is seen. */
143 int current_function_returns_value;
145 /* Set to 0 at beginning of a function definition, set to 1 if
146 a return statement with no argument is seen. */
148 int current_function_returns_null;
150 /* Set to 0 at beginning of a function definition, set to 1 if
151 a call to a noreturn function is seen. */
153 int current_function_returns_abnormally;
155 /* Set to nonzero by `grokdeclarator' for a function
156 whose return type is defaulted, if warnings for this are desired. */
158 static int warn_about_return_type;
160 /* Nonzero when starting a function declared `extern inline'. */
162 static int current_extern_inline;
164 /* For each binding contour we allocate a c_scope structure
165 * which records the names defined in that contour.
168 * 1) one for each function definition,
169 * where internal declarations of the parameters appear.
170 * 2) one for each compound statement,
171 * to record its declarations.
173 * The current meaning of a name can be found by searching the nested
174 * scopes from the current one out to the global one.
177 struct c_scope GTY(())
179 /* The scope containing this one. */
180 struct c_scope *outer;
182 /* The next outermost function scope. */
183 struct c_scope *outer_function;
185 /* All variables, constants, functions, labels, and typedef names.
186 They are in the reverse of the order supplied. */
189 /* All parameter declarations. Used only in the outermost scope of
190 a function. Again, in the reverse of the order supplied. */
193 /* All structure, union, and enum type tags. */
196 /* For each scope, a list of shadowed outer-scope definitions
197 to be restored when this scope is popped.
198 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
199 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
202 /* For each scope, a list of shadowed outer-scope tag definitions
203 to be restored when this scope is popped.
204 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
205 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
208 /* For each scope (except the global one), a chain of BLOCK nodes
209 for all the scopes that were entered and exited one level down. */
212 /* Variable declarations with incomplete type in this scope. */
213 tree incomplete_list;
215 /* True if we are currently filling this scope with parameter
219 /* True if we already complained about forward parameter decls
220 in this scope. This prevents double warnings on
221 foo (int a; int b; ...) */
222 bool warned_forward_parm_decls : 1;
224 /* True if this is the outermost block scope of a function body.
225 This scope contains the parameters, the local variables declared
226 in the outermost block, and all the labels (except those in
227 nested functions, or declared at block scope with __label__). */
228 bool function_body : 1;
230 /* True means make a BLOCK for this scope no matter what. */
234 /* The scope currently in effect. */
236 static GTY(()) struct c_scope *current_scope;
238 /* A chain of c_scope structures awaiting reuse. */
240 static GTY((deletable (""))) struct c_scope *scope_freelist;
242 /* The innermost function scope. Ordinary (not explicitly declared)
243 labels, bindings to error_mark_node, and the lazily-created
244 bindings of __func__ and its friends get this scope. */
246 static GTY(()) struct c_scope *current_function_scope;
248 /* The outermost scope, corresponding to the C "file scope". This is
249 created when the compiler is started and exists through the entire run. */
251 static GTY(()) struct c_scope *global_scope;
253 /* True means unconditionally make a BLOCK for the next scope pushed. */
255 static bool keep_next_level_flag;
257 /* True means the next call to pushlevel will be the outermost scope
258 of a function body, so do not push a new scope, merely cease
259 expecting parameter decls. */
261 static bool next_is_function_body;
263 /* Functions called automatically at the beginning and end of execution. */
265 tree static_ctors, static_dtors;
267 /* Forward declarations. */
269 static struct c_scope *make_scope (void);
270 static void pop_scope (void);
271 static tree match_builtin_function_types (tree, tree);
272 static int duplicate_decls (tree, tree, int, int);
273 static int redeclaration_error_message (tree, tree);
274 static tree make_label (tree, location_t);
275 static void bind_label (tree, tree, struct c_scope *);
276 static void implicit_decl_warning (tree);
277 static tree lookup_tag (enum tree_code, tree, int);
278 static tree lookup_name_current_level (tree);
279 static tree grokdeclarator (tree, tree, enum decl_context, int);
280 static tree grokparms (tree, int);
281 static void layout_array_type (tree);
282 static void store_parm_decls_newstyle (void);
283 static void store_parm_decls_oldstyle (void);
284 static tree c_make_fname_decl (tree, int);
285 static void c_expand_body_1 (tree, int);
286 static tree any_external_decl (tree);
287 static void record_external_decl (tree);
288 static void warn_if_shadowing (tree, tree);
289 static void clone_underlying_type (tree);
290 static void pushdecl_function_level (tree, tree);
291 static bool flexible_array_type_p (tree);
292 static hashval_t link_hash_hash (const void *);
293 static int link_hash_eq (const void *, const void *);
295 /* States indicating how grokdeclarator() should handle declspecs marked
296 with __attribute__((deprecated)). An object declared as
297 __attribute__((deprecated)) suppresses warnings of uses of other
300 enum deprecated_states {
305 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
308 c_print_identifier (FILE *file, tree node, int indent)
310 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
311 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
312 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
313 if (C_IS_RESERVED_WORD (node))
315 tree rid = ridpointers[C_RID_CODE (node)];
316 indent_to (file, indent + 4);
317 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
318 (void *) rid, IDENTIFIER_POINTER (rid));
322 /* Hook called at end of compilation to assume 1 elt
323 for a file-scope tentative array defn that wasn't complete before. */
326 c_finish_incomplete_decl (tree decl)
328 if (TREE_CODE (decl) == VAR_DECL)
330 tree type = TREE_TYPE (decl);
331 if (type != error_mark_node
332 && TREE_CODE (type) == ARRAY_TYPE
333 && ! DECL_EXTERNAL (decl)
334 && TYPE_DOMAIN (type) == 0)
336 warning ("%Harray '%D' assumed to have one element",
337 &DECL_SOURCE_LOCATION (decl), decl);
339 complete_array_type (type, NULL_TREE, 1);
341 layout_decl (decl, 0);
346 /* Reuse or create a struct for this scope. */
348 static struct c_scope *
351 struct c_scope *result;
354 result = scope_freelist;
355 scope_freelist = result->outer;
358 result = ggc_alloc_cleared (sizeof (struct c_scope));
363 /* Remove the topmost scope from the stack and add it to the
364 free list, updating current_function_scope if necessary. */
369 struct c_scope *scope = current_scope;
371 current_scope = scope->outer;
372 if (scope->function_body)
373 current_function_scope = scope->outer_function;
375 memset (scope, 0, sizeof (struct c_scope));
376 scope->outer = scope_freelist;
377 scope_freelist = scope;
380 /* Nonzero if we are currently in the global scope. */
383 global_bindings_p (void)
385 return current_scope == global_scope;
389 keep_next_level (void)
391 keep_next_level_flag = true;
394 /* Identify this scope as currently being filled with parameters. */
397 declare_parm_level (void)
399 current_scope->parm_flag = true;
402 /* Nonzero if currently making parm declarations. */
405 in_parm_level_p (void)
407 return current_scope->parm_flag;
410 /* Enter a new scope. The dummy parameter is for signature
411 compatibility with lang_hooks.decls.pushlevel. */
414 pushlevel (int dummy ATTRIBUTE_UNUSED)
416 if (next_is_function_body)
418 /* This is the transition from the parameters to the top level
419 of the function body. These are the same scope
420 (C99 6.2.1p4,6) so we do not push another scope structure.
421 next_is_function_body is set only by store_parm_decls, which
422 in turn is called when and only when we are about to
423 encounter the opening curly brace for the function body.
425 The outermost block of a function always gets a BLOCK node,
426 because the debugging output routines expect that each
427 function has at least one BLOCK. */
428 current_scope->parm_flag = false;
429 current_scope->function_body = true;
430 current_scope->keep = true;
431 current_scope->outer_function = current_function_scope;
432 current_function_scope = current_scope;
434 keep_next_level_flag = false;
435 next_is_function_body = false;
439 struct c_scope *scope = make_scope ();
441 scope->keep = keep_next_level_flag;
442 scope->outer = current_scope;
443 current_scope = scope;
444 keep_next_level_flag = false;
448 /* Exit a scope. Restore the state of the identifier-decl mappings
449 that were in effect when this scope was entered.
451 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
452 create a BLOCK node to record its declarations and subblocks for
453 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
454 or tags lists are nonempty.
456 If REVERSE is nonzero, reverse the order of decls before putting
459 If FUNCTIONBODY is nonzero, this level is the body of a function,
460 even if current_scope->function_body is not set. This is used
461 by language-independent code that generates synthetic functions,
462 and cannot set current_scope->function_body.
464 FIXME: Eliminate the need for all arguments. */
467 poplevel (int keep, int reverse, int functionbody)
472 tree decls = current_scope->names;
473 tree tags = current_scope->tags;
474 tree subblocks = current_scope->blocks;
476 functionbody |= current_scope->function_body;
478 if (keep == KEEP_MAYBE)
479 keep = (current_scope->names || current_scope->tags);
481 keep |= current_scope->keep;
482 keep |= functionbody;
484 /* We used to warn about unused variables in expand_end_bindings,
485 i.e. while generating RTL. But in function-at-a-time mode we may
486 choose to never expand a function at all (e.g. auto inlining), so
487 we do this explicitly now.
488 No warnings when the global scope is popped because the global
489 scope isn't popped for the last translation unit, so the warnings
490 are done in c_write_global_declaration. */
491 if (current_scope != global_scope)
492 warn_about_unused_variables (decls);
494 /* Clear out the name-meanings declared in this scope.
495 Propagate TREE_ADDRESSABLE from nested functions to their
496 containing functions. */
497 for (link = decls; link; link = TREE_CHAIN (link))
499 if (TREE_CODE (link) == LABEL_DECL)
501 if (TREE_USED (link) && DECL_INITIAL (link) == 0)
503 error ("%Hlabel `%D' used but not defined",
504 &DECL_SOURCE_LOCATION (link), link);
505 /* Avoid crashing later. */
506 DECL_INITIAL (link) = error_mark_node;
508 else if (!TREE_USED (link) && warn_unused_label)
510 if (DECL_INITIAL (link) != 0)
511 warning ("%Hlabel `%D' defined but not used",
512 &DECL_SOURCE_LOCATION (link), link);
514 warning ("%Hlabel `%D' declared but not defined",
515 &DECL_SOURCE_LOCATION (link), link);
517 IDENTIFIER_LABEL_VALUE (DECL_NAME (link)) = 0;
519 else if (DECL_NAME (link) != 0)
521 if (DECL_EXTERNAL (link)
522 && current_scope != global_scope)
523 /* External decls stay in the symbol-value slot but are
525 C_DECL_INVISIBLE (link) = 1;
527 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
530 if (TREE_CODE (link) == FUNCTION_DECL
531 && ! TREE_ASM_WRITTEN (link)
532 && DECL_INITIAL (link) != 0
533 && TREE_ADDRESSABLE (link)
534 && DECL_ABSTRACT_ORIGIN (link) != 0
535 && DECL_ABSTRACT_ORIGIN (link) != link)
536 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (link)) = 1;
539 /* Clear out the parameter bindings declared in this scope.
540 Unused-parameter warnings are handled by function.c. */
541 for (link = current_scope->parms; link; link = TREE_CHAIN (link))
542 if (DECL_NAME (link))
543 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (link)) = 0;
545 /* Clear out the tag-meanings declared in this scope. */
546 for (link = tags; link; link = TREE_CHAIN (link))
547 if (TREE_PURPOSE (link))
548 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = 0;
550 /* Restore all name- and label-meanings from outer scopes that were
551 shadowed by this scope. */
553 for (link = current_scope->shadowed; link; link = TREE_CHAIN (link))
554 if (TREE_VALUE (link) && TREE_CODE (TREE_VALUE (link)) == LABEL_DECL)
555 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
557 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
559 /* Restore all tag-meanings from outer scopes that were shadowed by
562 for (link = current_scope->shadowed_tags; link;
563 link = TREE_CHAIN (link))
564 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
566 /* If this is the outermost block of a function, remove all
567 PARM_DECLs from current_scope->names; they are already
568 stored in DECL_ARGUMENTS of cfun->decl in proper order, should
569 not be put in BLOCK_VARS, and furthermore reversing them will
570 cause trouble later. They are all together at the end of the
572 if (functionbody && decls)
574 if (TREE_CODE (decls) == PARM_DECL)
579 while (TREE_CHAIN (link)
580 && TREE_CODE (TREE_CHAIN (link)) != PARM_DECL)
581 link = TREE_CHAIN (link);
583 TREE_CHAIN (link) = 0;
587 /* Get the decls in the order they were written.
588 Usually current_scope->names is in reverse order.
589 But parameter decls were previously put in forward order. */
592 decls = nreverse (decls);
594 /* If appropriate, create a BLOCK to record the decls for the life
600 block = make_node (BLOCK);
601 BLOCK_VARS (block) = decls;
602 BLOCK_SUBBLOCKS (block) = subblocks;
603 TREE_USED (block) = 1;
606 /* In each subblock, record that this is its superior. */
608 for (link = subblocks; link; link = TREE_CHAIN (link))
609 BLOCK_SUPERCONTEXT (link) = block;
611 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
612 binding contour so that they point to the appropriate construct, i.e.
613 either to the current FUNCTION_DECL node, or else to the BLOCK node
616 Note that for tagged types whose scope is just the formal parameter
617 list for some function type specification, we can't properly set
618 their TYPE_CONTEXTs here, because we don't have a pointer to the
619 appropriate FUNCTION_TYPE node readily available to us. For those
620 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
621 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
622 node which will represent the "scope" for these "parameter list local"
625 decl = functionbody ? current_function_decl : block;
627 for (link = tags; link; link = TREE_CHAIN (link))
628 TYPE_CONTEXT (TREE_VALUE (link)) = decl;
630 /* Pop the current scope, and free the structure for reuse. */
633 /* Dispose of the block that we just made inside some higher level. */
635 DECL_INITIAL (current_function_decl) = block;
636 else if (block && current_scope)
637 current_scope->blocks
638 = chainon (current_scope->blocks, block);
639 /* If we did not make a block for the scope just exited, any blocks
640 made for inner scopes (since they cannot be recorded as subblocks
641 here) must be carried forward so they will later become subblocks
642 of something else. */
643 else if (! block && subblocks)
644 current_scope->blocks
645 = chainon (current_scope->blocks, subblocks);
650 /* Insert BLOCK at the end of the list of subblocks of the current
651 scope. This is used when a BIND_EXPR is expanded, to handle the
652 BLOCK node inside the BIND_EXPR. */
655 insert_block (tree block)
657 TREE_USED (block) = 1;
658 current_scope->blocks
659 = chainon (current_scope->blocks, block);
662 /* Set the BLOCK node for the innermost scope (the one we are
663 currently in). The RTL expansion machinery requires us to provide
664 this hook, but it is not useful in function-at-a-time mode. */
667 set_block (tree block ATTRIBUTE_UNUSED)
671 /* Push a definition or a declaration of struct, union or enum tag "name".
672 "type" should be the type node.
673 We assume that the tag "name" is not already defined.
675 Note that the definition may really be just a forward reference.
676 In that case, the TYPE_SIZE will be zero. */
679 pushtag (tree name, tree type)
681 struct c_scope *b = current_scope;
683 /* Record the identifier as the type's name if it has none. */
686 if (TYPE_NAME (type) == 0)
687 TYPE_NAME (type) = name;
689 if (IDENTIFIER_TAG_VALUE (name))
690 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
692 IDENTIFIER_TAG_VALUE (name) = type;
695 b->tags = tree_cons (name, type, b->tags);
697 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
698 tagged type we just added to the current scope. This fake
699 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
700 to output a representation of a tagged type, and it also gives
701 us a convenient place to record the "scope start" address for the
704 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
706 /* An approximation for now, so we can tell this is a function-scope tag.
707 This will be updated in poplevel. */
708 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
711 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
712 and argument types provided that the type modes match. This function
713 return a unified type given a suitable match, and 0 otherwise. */
716 match_builtin_function_types (tree oldtype, tree newtype)
718 tree newrettype, oldrettype;
719 tree newargs, oldargs;
720 tree trytype, tryargs;
722 /* Accept the return type of the new declaration if same modes. */
723 oldrettype = TREE_TYPE (oldtype);
724 newrettype = TREE_TYPE (newtype);
726 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
729 oldargs = TYPE_ARG_TYPES (oldtype);
730 newargs = TYPE_ARG_TYPES (newtype);
733 while (oldargs || newargs)
737 || ! TREE_VALUE (oldargs)
738 || ! TREE_VALUE (newargs)
739 || TYPE_MODE (TREE_VALUE (oldargs))
740 != TYPE_MODE (TREE_VALUE (newargs)))
743 oldargs = TREE_CHAIN (oldargs);
744 newargs = TREE_CHAIN (newargs);
747 trytype = build_function_type (newrettype, tryargs);
748 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
751 /* Handle when a new declaration NEWDECL
752 has the same name as an old one OLDDECL
753 in the same binding contour.
754 Prints an error message if appropriate.
756 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
759 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
760 and OLDDECL is in an outer scope and should thus not be changed. */
763 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
766 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
768 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
770 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
771 && DECL_INITIAL (newdecl) != 0);
772 tree oldtype = TREE_TYPE (olddecl);
773 tree newtype = TREE_TYPE (newdecl);
776 if (DECL_P (olddecl))
778 if (TREE_CODE (newdecl) == FUNCTION_DECL
779 && TREE_CODE (olddecl) == FUNCTION_DECL
780 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
782 if (DECL_DECLARED_INLINE_P (newdecl)
783 && DECL_UNINLINABLE (newdecl)
784 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
785 /* Already warned elsewhere. */;
786 else if (DECL_DECLARED_INLINE_P (olddecl)
787 && DECL_UNINLINABLE (olddecl)
788 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
789 /* Already warned. */;
790 else if (DECL_DECLARED_INLINE_P (newdecl)
791 && ! DECL_DECLARED_INLINE_P (olddecl)
792 && DECL_UNINLINABLE (olddecl)
793 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
795 warning ("%Hfunction '%D' redeclared as inline",
796 &DECL_SOURCE_LOCATION (newdecl), newdecl);
797 warning ("%Hprevious declaration of function '%D' "
798 "with attribute noinline",
799 &DECL_SOURCE_LOCATION (olddecl), olddecl);
801 else if (DECL_DECLARED_INLINE_P (olddecl)
802 && DECL_UNINLINABLE (newdecl)
803 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
805 warning ("%Hfunction '%D' redeclared with attribute noinline",
806 &DECL_SOURCE_LOCATION (newdecl), newdecl);
807 warning ("%Hprevious declaration of function '%D' was inline",
808 &DECL_SOURCE_LOCATION (olddecl), olddecl);
812 DECL_ATTRIBUTES (newdecl)
813 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
816 if (TREE_CODE (newtype) == ERROR_MARK
817 || TREE_CODE (oldtype) == ERROR_MARK)
820 /* New decl is completely inconsistent with the old one =>
821 tell caller to replace the old one.
822 This is always an error except in the case of shadowing a builtin. */
823 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
825 if (TREE_CODE (olddecl) == FUNCTION_DECL
826 && DECL_BUILT_IN (olddecl))
828 /* If you declare a built-in or predefined function name as static,
829 the old definition is overridden,
830 but optionally warn this was a bad choice of name. */
831 if (!TREE_PUBLIC (newdecl))
834 warning ("%Hshadowing built-in function '%D'",
835 &DECL_SOURCE_LOCATION (newdecl), newdecl);
838 warning ("%Hbuilt-in function '%D' declared as non-function",
839 &DECL_SOURCE_LOCATION (newdecl), newdecl);
843 error ("%H'%D' redeclared as different kind of symbol",
844 &DECL_SOURCE_LOCATION (newdecl), newdecl);
845 error ("%Hprevious declaration of '%D'",
846 &DECL_SOURCE_LOCATION (olddecl), olddecl);
852 /* For real parm decl following a forward decl, return 1 so old decl
853 will be reused. Only allow this to happen once. */
854 if (types_match && TREE_CODE (newdecl) == PARM_DECL
855 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
857 TREE_ASM_WRITTEN (olddecl) = 0;
861 /* The new declaration is the same kind of object as the old one.
862 The declarations may partially match. Print warnings if they don't
863 match enough. Ultimately, copy most of the information from the new
864 decl to the old one, and keep using the old one. */
866 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
868 /* A function declaration for a built-in function. */
869 if (!TREE_PUBLIC (newdecl))
871 /* If you declare a built-in function name as static, the
872 built-in definition is overridden,
873 but optionally warn this was a bad choice of name. */
875 warning ("%Hshadowing built-in function '%D'",
876 &DECL_SOURCE_LOCATION (newdecl), newdecl);
877 /* Discard the old built-in function. */
882 /* Accept harmless mismatch in function types.
883 This is for the ffs and fprintf builtins. */
884 tree trytype = match_builtin_function_types (oldtype, newtype);
888 types_match = comptypes (newtype, trytype, comptype_flags);
891 if (! different_binding_level)
892 TREE_TYPE (olddecl) = oldtype;
897 /* If types don't match for a built-in, throw away the built-in. */
898 warning ("%Hconflicting types for built-in function '%D'",
899 &DECL_SOURCE_LOCATION (newdecl), newdecl);
903 else if (TREE_CODE (olddecl) == FUNCTION_DECL
904 && DECL_SOURCE_LINE (olddecl) == 0)
906 /* A function declaration for a predeclared function
907 that isn't actually built in. */
908 if (!TREE_PUBLIC (newdecl))
910 /* If you declare it as static, the
911 default definition is overridden. */
914 else if (!types_match)
916 /* If the types don't match, preserve volatility indication.
917 Later on, we will discard everything else about the
918 default declaration. */
919 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
922 /* Permit char *foo () to match void *foo (...) if not pedantic,
923 if one of them came from a system header file. */
924 else if (!types_match
925 && TREE_CODE (olddecl) == FUNCTION_DECL
926 && TREE_CODE (newdecl) == FUNCTION_DECL
927 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
928 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
929 && (DECL_IN_SYSTEM_HEADER (olddecl)
930 || DECL_IN_SYSTEM_HEADER (newdecl))
931 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
932 && TYPE_ARG_TYPES (oldtype) == 0
933 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
934 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
936 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
937 && TYPE_ARG_TYPES (newtype) == 0
938 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
939 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
942 pedwarn ("%Hconflicting types for '%D'",
943 &DECL_SOURCE_LOCATION (newdecl), newdecl);
944 /* Make sure we keep void * as ret type, not char *. */
945 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
946 TREE_TYPE (newdecl) = newtype = oldtype;
948 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
949 we will come back here again. */
950 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
952 /* Permit void foo (...) to match int foo (...) if the latter is the
953 definition and implicit int was used. See c-torture/compile/920625-2.c. */
954 else if (!types_match && new_is_definition
955 && TREE_CODE (olddecl) == FUNCTION_DECL
956 && TREE_CODE (newdecl) == FUNCTION_DECL
957 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
958 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
959 && C_FUNCTION_IMPLICIT_INT (newdecl))
961 pedwarn ("%Hconflicting types for '%D'",
962 &DECL_SOURCE_LOCATION (newdecl), newdecl);
963 /* Make sure we keep void as the return type. */
964 TREE_TYPE (newdecl) = newtype = oldtype;
965 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
967 else if (!types_match
968 /* Permit char *foo (int, ...); followed by char *foo ();
970 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
972 /* Return types must still match. */
973 && comptypes (TREE_TYPE (oldtype),
974 TREE_TYPE (newtype), comptype_flags)
975 && TYPE_ARG_TYPES (newtype) == 0))
977 error ("%Hconflicting types for '%D'",
978 &DECL_SOURCE_LOCATION (newdecl), newdecl);
979 /* Check for function type mismatch
980 involving an empty arglist vs a nonempty one. */
981 if (TREE_CODE (olddecl) == FUNCTION_DECL
982 && comptypes (TREE_TYPE (oldtype),
983 TREE_TYPE (newtype), comptype_flags)
984 && ((TYPE_ARG_TYPES (oldtype) == 0
985 && DECL_INITIAL (olddecl) == 0)
987 (TYPE_ARG_TYPES (newtype) == 0
988 && DECL_INITIAL (newdecl) == 0)))
990 /* Classify the problem further. */
991 tree t = TYPE_ARG_TYPES (oldtype);
993 t = TYPE_ARG_TYPES (newtype);
994 for (; t; t = TREE_CHAIN (t))
996 tree type = TREE_VALUE (t);
998 if (TREE_CHAIN (t) == 0
999 && TYPE_MAIN_VARIANT (type) != void_type_node)
1001 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1005 if (c_type_promotes_to (type) != type)
1007 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1012 if (C_DECL_IMPLICIT (olddecl))
1013 error ("%Hprevious implicit declaration of '%D'",
1014 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1016 error ("%Hprevious declaration of '%D'",
1017 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1019 /* This is safer because the initializer might contain references
1020 to variables that were declared between olddecl and newdecl. This
1021 will make the initializer invalid for olddecl in case it gets
1022 assigned to olddecl below. */
1023 if (TREE_CODE (newdecl) == VAR_DECL)
1024 DECL_INITIAL (newdecl) = 0;
1026 /* TLS cannot follow non-TLS declaration. */
1027 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1028 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1030 error ("%Hthread-local declaration of '%D' follows non thread-local "
1031 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1032 error ("%Hprevious declaration of '%D'",
1033 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1035 /* non-TLS declaration cannot follow TLS declaration. */
1036 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1037 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1039 error ("%Hnon thread-local declaration of '%D' follows "
1040 "thread-local declaration",
1041 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1042 error ("%Hprevious declaration of '%D'",
1043 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1047 errmsg = redeclaration_error_message (newdecl, olddecl);
1050 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1054 error ("%Hredefinition of '%D'", locus, newdecl);
1057 error ("%Hredeclaration of '%D'", locus, newdecl);
1060 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1066 locus = &DECL_SOURCE_LOCATION (olddecl);
1067 if (DECL_INITIAL (olddecl)
1068 && current_scope == global_scope)
1069 error ("%H'%D' previously defined here", locus, olddecl);
1071 error ("%H'%D' previously declared here", locus, olddecl);
1074 else if (TREE_CODE (newdecl) == TYPE_DECL
1075 && (DECL_IN_SYSTEM_HEADER (olddecl)
1076 || DECL_IN_SYSTEM_HEADER (newdecl)))
1078 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1079 warning ("%Hredefinition of '%D'", locus, newdecl);
1080 locus = &DECL_SOURCE_LOCATION (olddecl);
1081 if (DECL_INITIAL (olddecl)
1082 && current_scope == global_scope)
1083 warning ("%H'%D' previously defined here", locus, olddecl);
1085 warning ("%H'%D' previously declared here", locus, olddecl);
1087 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1088 && DECL_INITIAL (olddecl) != 0
1089 && TYPE_ARG_TYPES (oldtype) == 0
1090 && TYPE_ARG_TYPES (newtype) != 0
1091 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1095 /* Prototype decl follows defn w/o prototype. */
1097 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1098 type = TYPE_ARG_TYPES (newtype),
1101 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1103 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1104 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1106 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1107 warning ("%Hprototype for '%D' follows", locus, newdecl);
1108 locus = &DECL_SOURCE_LOCATION (olddecl);
1109 warning ("%Hnon-prototype definition here", locus);
1112 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1113 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1115 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1116 error ("%Hprototype for '%D' follows and number of "
1117 "arguments doesn't match", locus, newdecl);
1118 locus = &DECL_SOURCE_LOCATION (olddecl);
1119 error ("%Hnon-prototype definition here", locus);
1123 /* Type for passing arg must be consistent
1124 with that declared for the arg. */
1125 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1128 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1129 error ("%Hprototype for '%D' follows and argument %d "
1130 "doesn't match", locus, newdecl, nargs);
1131 locus = &DECL_SOURCE_LOCATION (olddecl);
1132 error ("%Hnon-prototype definition here", locus);
1138 /* Warn about mismatches in various flags. */
1141 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1143 /* Warn if function is now inline
1144 but was previously declared not inline and has been called. */
1145 if (TREE_CODE (olddecl) == FUNCTION_DECL
1146 && ! DECL_DECLARED_INLINE_P (olddecl)
1147 && DECL_DECLARED_INLINE_P (newdecl)
1148 && TREE_USED (olddecl))
1149 warning ("%H'%D' declared inline after being called",
1151 if (TREE_CODE (olddecl) == FUNCTION_DECL
1152 && ! DECL_DECLARED_INLINE_P (olddecl)
1153 && DECL_DECLARED_INLINE_P (newdecl)
1154 && DECL_INITIAL (olddecl) != 0)
1155 warning ("%H'%D' declared inline after its definition",
1158 /* If pedantic, warn when static declaration follows a non-static
1159 declaration. Otherwise, do so only for functions. */
1160 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1161 && TREE_PUBLIC (olddecl)
1162 && !TREE_PUBLIC (newdecl))
1163 warning ("%Hstatic declaration for '%D' follows non-static",
1166 /* If warn_traditional, warn when a non-static function
1167 declaration follows a static one. */
1168 if (warn_traditional && !in_system_header
1169 && TREE_CODE (olddecl) == FUNCTION_DECL
1170 && !TREE_PUBLIC (olddecl)
1171 && TREE_PUBLIC (newdecl))
1172 warning ("%Hnon-static declaration for '%D' follows static",
1175 /* Warn when const declaration follows a non-const
1176 declaration, but not for functions. */
1177 if (TREE_CODE (olddecl) != FUNCTION_DECL
1178 && !TREE_READONLY (olddecl)
1179 && TREE_READONLY (newdecl))
1180 warning ("%Hconst declaration for '%D' follows non-const",
1182 /* These bits are logically part of the type, for variables.
1183 But not for functions
1184 (where qualifiers are not valid ANSI anyway). */
1185 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1186 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1187 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1188 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1189 "declaration", locus, newdecl);
1193 /* Optionally warn about more than one declaration for the same name. */
1194 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1195 /* Don't warn about a function declaration
1196 followed by a definition. */
1197 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1198 && DECL_INITIAL (olddecl) == 0)
1199 /* Don't warn about extern decl followed by (tentative) definition. */
1200 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1202 warning ("%Hredundant redeclaration of '%D' in same scope",
1203 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1204 warning ("%Hprevious declaration of '%D'",
1205 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1208 /* Copy all the DECL_... slots specified in the new decl
1209 except for any that we copy here from the old type.
1211 Past this point, we don't change OLDTYPE and NEWTYPE
1212 even if we change the types of NEWDECL and OLDDECL. */
1216 /* When copying info to olddecl, we store into write_olddecl
1217 instead. This allows us to avoid modifying olddecl when
1218 different_binding_level is true. */
1219 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1221 /* Merge the data types specified in the two decls. */
1222 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1224 if (different_binding_level)
1226 if (TYPE_ARG_TYPES (oldtype) != 0
1227 && TYPE_ARG_TYPES (newtype) == 0)
1228 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1231 = build_type_attribute_variant
1233 merge_attributes (TYPE_ATTRIBUTES (newtype),
1234 TYPE_ATTRIBUTES (oldtype)));
1238 = TREE_TYPE (olddecl)
1239 = common_type (newtype, oldtype);
1242 /* Lay the type out, unless already done. */
1243 if (oldtype != TREE_TYPE (newdecl))
1245 if (TREE_TYPE (newdecl) != error_mark_node)
1246 layout_type (TREE_TYPE (newdecl));
1247 if (TREE_CODE (newdecl) != FUNCTION_DECL
1248 && TREE_CODE (newdecl) != TYPE_DECL
1249 && TREE_CODE (newdecl) != CONST_DECL)
1250 layout_decl (newdecl, 0);
1254 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1255 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1256 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1257 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1258 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1259 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1261 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1262 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1266 /* Keep the old rtl since we can safely use it. */
1267 COPY_DECL_RTL (olddecl, newdecl);
1269 /* Merge the type qualifiers. */
1270 if (TREE_READONLY (newdecl))
1271 TREE_READONLY (write_olddecl) = 1;
1273 if (TREE_THIS_VOLATILE (newdecl))
1275 TREE_THIS_VOLATILE (write_olddecl) = 1;
1276 if (TREE_CODE (newdecl) == VAR_DECL
1277 /* If an automatic variable is re-declared in the same
1278 function scope, but the old declaration was not
1279 volatile, make_var_volatile() would crash because the
1280 variable would have been assigned to a pseudo, not a
1281 MEM. Since this duplicate declaration is invalid
1282 anyway, we just skip the call. */
1284 make_var_volatile (newdecl);
1287 /* Keep source location of definition rather than declaration. */
1288 /* When called with different_binding_level set, keep the old
1289 information so that meaningful diagnostics can be given. */
1290 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1291 && ! different_binding_level)
1293 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1294 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1297 /* Merge the unused-warning information. */
1298 if (DECL_IN_SYSTEM_HEADER (olddecl))
1299 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1300 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1301 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1303 /* Merge the initialization information. */
1304 /* When called with different_binding_level set, don't copy over
1305 DECL_INITIAL, so that we don't accidentally change function
1306 declarations into function definitions. */
1307 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1308 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1310 /* Merge the section attribute.
1311 We want to issue an error if the sections conflict but that must be
1312 done later in decl_attributes since we are called before attributes
1314 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1315 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1317 /* Copy the assembler name.
1318 Currently, it can only be defined in the prototype. */
1319 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1321 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1323 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1324 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1325 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1326 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1327 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1328 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1329 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1330 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1331 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1334 /* If cannot merge, then use the new type and qualifiers,
1335 and don't preserve the old rtl. */
1336 else if (! different_binding_level)
1338 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1339 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1340 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1341 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1344 /* Merge the storage class information. */
1345 merge_weak (newdecl, olddecl);
1347 /* For functions, static overrides non-static. */
1348 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1350 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1351 /* This is since we don't automatically
1352 copy the attributes of NEWDECL into OLDDECL. */
1353 /* No need to worry about different_binding_level here because
1354 then TREE_PUBLIC (newdecl) was true. */
1355 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1356 /* If this clears `static', clear it in the identifier too. */
1357 if (! TREE_PUBLIC (olddecl))
1358 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1360 if (DECL_EXTERNAL (newdecl))
1362 if (! different_binding_level || different_tu)
1364 /* Don't mess with these flags on local externs; they remain
1365 external even if there's a declaration at file scope which
1367 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1368 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1370 /* An extern decl does not override previous storage class. */
1371 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1372 if (! DECL_EXTERNAL (newdecl))
1374 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1375 /* If we have two non-EXTERNAL file-scope decls that are
1376 the same, only one of them should be written out. */
1378 TREE_ASM_WRITTEN (newdecl) = 1;
1383 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1384 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1387 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1389 /* If we're redefining a function previously defined as extern
1390 inline, make sure we emit debug info for the inline before we
1391 throw it away, in case it was inlined into a function that hasn't
1392 been written out yet. */
1393 if (new_is_definition && DECL_INITIAL (olddecl))
1395 if (TREE_USED (olddecl))
1396 (*debug_hooks->outlining_inline_function) (olddecl);
1398 /* The new defn must not be inline. */
1399 DECL_INLINE (newdecl) = 0;
1400 DECL_UNINLINABLE (newdecl) = 1;
1404 /* If either decl says `inline', this fn is inline,
1405 unless its definition was passed already. */
1406 if (DECL_DECLARED_INLINE_P (newdecl)
1407 || DECL_DECLARED_INLINE_P (olddecl))
1408 DECL_DECLARED_INLINE_P (newdecl) = 1;
1410 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1411 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1414 if (DECL_BUILT_IN (olddecl))
1416 /* Get rid of any built-in function if new arg types don't match it
1417 or if we have a function definition. */
1418 if (! types_match || new_is_definition)
1420 if (! different_binding_level)
1422 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1423 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1428 /* If redeclaring a builtin function, and not a definition,
1429 it stays built in. */
1430 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1431 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1435 /* Also preserve various other info from the definition. */
1436 if (! new_is_definition)
1438 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1439 /* When called with different_binding_level set, don't copy over
1440 DECL_INITIAL, so that we don't accidentally change function
1441 declarations into function definitions. */
1442 if (! different_binding_level)
1443 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1444 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1445 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1446 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1447 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1449 /* Set DECL_INLINE on the declaration if we've got a body
1450 from which to instantiate. */
1451 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1453 DECL_INLINE (newdecl) = 1;
1454 DECL_ABSTRACT_ORIGIN (newdecl)
1455 = (different_binding_level
1456 ? DECL_ORIGIN (olddecl)
1457 : DECL_ABSTRACT_ORIGIN (olddecl));
1462 /* If a previous declaration said inline, mark the
1463 definition as inlinable. */
1464 if (DECL_DECLARED_INLINE_P (newdecl)
1465 && ! DECL_UNINLINABLE (newdecl))
1466 DECL_INLINE (newdecl) = 1;
1469 if (different_binding_level)
1472 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1473 But preserve OLDDECL's DECL_UID. */
1475 unsigned olddecl_uid = DECL_UID (olddecl);
1477 memcpy ((char *) olddecl + sizeof (struct tree_common),
1478 (char *) newdecl + sizeof (struct tree_common),
1479 sizeof (struct tree_decl) - sizeof (struct tree_common));
1480 DECL_UID (olddecl) = olddecl_uid;
1483 /* NEWDECL contains the merged attribute lists.
1484 Update OLDDECL to be the same. */
1485 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1487 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1488 so that encode_section_info has a chance to look at the new decl
1489 flags and attributes. */
1490 if (DECL_RTL_SET_P (olddecl)
1491 && (TREE_CODE (olddecl) == FUNCTION_DECL
1492 || (TREE_CODE (olddecl) == VAR_DECL
1493 && TREE_STATIC (olddecl))))
1494 make_decl_rtl (olddecl, NULL);
1499 /* Return any external DECL associated with ID, whether or not it is
1500 currently in scope. */
1503 any_external_decl (tree id)
1505 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1508 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1510 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1513 t = purpose_member (id, truly_local_externals);
1515 return TREE_VALUE (t);
1520 /* Record an external decl DECL. This only does something if a
1521 shadowing decl already exists. */
1523 record_external_decl (tree decl)
1525 tree name = DECL_NAME (decl);
1526 if (!IDENTIFIER_SYMBOL_VALUE (name))
1529 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1532 /* Check whether decl-node X shadows an existing declaration.
1533 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1534 which might be a NULL_TREE. */
1536 warn_if_shadowing (tree x, tree old)
1540 /* Nothing to shadow? */
1542 /* Shadow warnings not wanted? */
1544 /* No shadow warnings for internally generated vars. */
1545 || DECL_SOURCE_LINE (x) == 0
1546 /* No shadow warnings for vars made for inlining. */
1547 || DECL_FROM_INLINE (x)
1548 /* Don't warn about the parm names in function declarator
1549 within a function declarator.
1550 It would be nice to avoid warning in any function
1551 declarator in a declaration, as opposed to a definition,
1552 but there is no way to tell it's not a definition. */
1553 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1556 name = IDENTIFIER_POINTER (DECL_NAME (x));
1557 if (TREE_CODE (old) == PARM_DECL)
1558 shadow_warning (SW_PARAM, name, old);
1559 else if (C_DECL_FILE_SCOPE (old))
1560 shadow_warning (SW_GLOBAL, name, old);
1562 shadow_warning (SW_LOCAL, name, old);
1566 /* Subroutine of pushdecl.
1568 X is a TYPE_DECL for a typedef statement. Create a brand new
1569 ..._TYPE node (which will be just a variant of the existing
1570 ..._TYPE node with identical properties) and then install X
1571 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1573 The whole point here is to end up with a situation where each
1574 and every ..._TYPE node the compiler creates will be uniquely
1575 associated with AT MOST one node representing a typedef name.
1576 This way, even though the compiler substitutes corresponding
1577 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1578 early on, later parts of the compiler can always do the reverse
1579 translation and get back the corresponding typedef name. For
1582 typedef struct S MY_TYPE;
1585 Later parts of the compiler might only know that `object' was of
1586 type `struct S' if it were not for code just below. With this
1587 code however, later parts of the compiler see something like:
1589 struct S' == struct S
1590 typedef struct S' MY_TYPE;
1593 And they can then deduce (from the node for type struct S') that
1594 the original object declaration was:
1598 Being able to do this is important for proper support of protoize,
1599 and also for generating precise symbolic debugging information
1600 which takes full account of the programmer's (typedef) vocabulary.
1602 Obviously, we don't want to generate a duplicate ..._TYPE node if
1603 the TYPE_DECL node that we are now processing really represents a
1604 standard built-in type.
1606 Since all standard types are effectively declared at line zero
1607 in the source file, we can easily check to see if we are working
1608 on a standard type by checking the current value of lineno. */
1611 clone_underlying_type (tree x)
1613 if (DECL_SOURCE_LINE (x) == 0)
1615 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1616 TYPE_NAME (TREE_TYPE (x)) = x;
1618 else if (TREE_TYPE (x) != error_mark_node
1619 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1621 tree tt = TREE_TYPE (x);
1622 DECL_ORIGINAL_TYPE (x) = tt;
1623 tt = build_type_copy (tt);
1625 TREE_USED (tt) = TREE_USED (x);
1630 /* Record a decl-node X as belonging to the current lexical scope.
1631 Check for errors (such as an incompatible declaration for the same
1632 name already seen in the same scope).
1634 Returns either X or an old decl for the same name.
1635 If an old decl is returned, it may have been smashed
1636 to agree with what X says. */
1641 tree name = DECL_NAME (x);
1642 struct c_scope *scope = current_scope;
1644 #ifdef ENABLE_CHECKING
1645 if (error_mark_node == 0)
1646 /* Called too early. */
1650 /* Functions need the lang_decl data. */
1651 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1652 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1654 /* A local extern declaration for a function doesn't constitute nesting.
1655 A local auto declaration does, since it's a forward decl
1656 for a nested function coming later. */
1657 if (current_function_decl == NULL
1658 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1659 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1660 DECL_CONTEXT (x) = current_file_decl;
1662 DECL_CONTEXT (x) = current_function_decl;
1668 if (warn_nested_externs
1669 && scope != global_scope
1670 && DECL_EXTERNAL (x)
1671 && !DECL_IN_SYSTEM_HEADER (x))
1672 warning ("nested extern declaration of `%s'",
1673 IDENTIFIER_POINTER (name));
1675 old = lookup_name_current_level (name);
1676 if (old && duplicate_decls (x, old, 0, false))
1678 /* For PARM_DECLs, old may be a forward declaration.
1679 If so, we want to remove it from its old location
1680 (in the variables chain) and rechain it in the
1681 location given by the new declaration. */
1682 if (TREE_CODE (x) == PARM_DECL)
1685 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1688 *p = TREE_CHAIN (old);
1689 TREE_CHAIN (old) = scope->parms;
1695 if (DECL_EXTERNAL (x) || scope == global_scope)
1697 /* Find and check against a previous, not-in-scope, external
1698 decl for this identifier. (C99 s???: If two declarations
1699 with external linkage, referring to the same object, have
1700 incompatible types, the behavior is undefined). */
1701 tree ext = any_external_decl (name);
1704 if (duplicate_decls (x, ext, scope != global_scope,
1706 x = copy_node (ext);
1709 record_external_decl (x);
1712 if (TREE_CODE (x) == TYPE_DECL)
1713 clone_underlying_type (x);
1715 /* If storing a local value, there may already be one
1716 (inherited). If so, record it for restoration when this
1717 scope ends. Take care not to do this if we are replacing an
1718 older decl in the same scope (i.e. duplicate_decls returned
1720 if (scope != global_scope
1721 && IDENTIFIER_SYMBOL_VALUE (name)
1722 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1724 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1725 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1729 /* Install the new declaration in the requested scope. */
1730 IDENTIFIER_SYMBOL_VALUE (name) = x;
1731 C_DECL_INVISIBLE (x) = 0;
1733 /* Keep list of variables in this scope with incomplete type.
1734 If the input is erroneous, we can have error_mark in the type
1735 slot (e.g. "f(void a, ...)") - that doesn't count as an
1738 FIXME: Chain these off the TYPE_DECL for the incomplete type,
1739 then we don't have to do (potentially quite costly) searches
1740 in finish_struct. */
1741 if (TREE_TYPE (x) != error_mark_node
1742 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1744 tree element = TREE_TYPE (x);
1746 while (TREE_CODE (element) == ARRAY_TYPE)
1747 element = TREE_TYPE (element);
1748 if (TREE_CODE (element) == RECORD_TYPE
1749 || TREE_CODE (element) == UNION_TYPE)
1750 scope->incomplete_list = tree_cons (NULL_TREE, x,
1751 scope->incomplete_list);
1755 /* Put decls on list in reverse order.
1756 We will reverse them later if necessary. */
1757 if (TREE_CODE (x) == PARM_DECL)
1759 TREE_CHAIN (x) = scope->parms;
1764 TREE_CHAIN (x) = scope->names;
1771 /* Record X as belonging to the global scope (C99 "file scope").
1772 This is used only internally by the Objective-C front end,
1773 and is limited to its needs. It will hork if there is _any_
1774 visible binding for X (not just a global one). */
1776 pushdecl_top_level (tree x)
1780 if (TREE_CODE (x) != VAR_DECL)
1783 name = DECL_NAME (x);
1784 old = IDENTIFIER_SYMBOL_VALUE (name);
1788 if (DECL_CONTEXT (old))
1791 if (!duplicate_decls (x, old, 0, false))
1797 DECL_CONTEXT (x) = current_file_decl;
1798 IDENTIFIER_SYMBOL_VALUE (name) = x;
1799 TREE_CHAIN (x) = global_scope->names;
1800 global_scope->names = x;
1804 /* Record X as belonging to the outermost scope of the current
1805 function. This is used only internally, by c_make_fname_decl and
1806 undeclared_variable, and is limited to their needs. The NAME is
1807 provided as a separate argument because undeclared_variable wants to
1808 use error_mark_node for X. For VAR_DECLs, duplicate_decls is not
1809 called; if there is any preexisting decl for this identifier, it is
1812 pushdecl_function_level (tree x, tree name)
1814 struct c_scope *scope = current_function_scope;
1816 if (x == error_mark_node)
1817 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1819 else if (TREE_CODE (x) == VAR_DECL)
1821 if (name != DECL_NAME (x))
1823 if (IDENTIFIER_SYMBOL_VALUE (name))
1826 DECL_CONTEXT (x) = current_function_decl;
1827 TREE_CHAIN (x) = scope->names;
1831 IDENTIFIER_SYMBOL_VALUE (name) = x;
1834 /* Generate an implicit declaration for identifier FUNCTIONID as a
1835 function of type int (). */
1838 implicitly_declare (tree functionid)
1840 tree decl = any_external_decl (functionid);
1842 if (decl && decl != error_mark_node)
1844 /* Implicit declaration of a function already declared
1845 (somehow) in a different scope, or as a built-in.
1846 If this is the first time this has happened, warn;
1847 then recycle the old declaration. */
1848 if (!C_DECL_IMPLICIT (decl))
1850 implicit_decl_warning (DECL_NAME (decl));
1851 if (! C_DECL_FILE_SCOPE (decl))
1852 warning ("%Hprevious declaration of '%D'",
1853 &DECL_SOURCE_LOCATION (decl), decl);
1854 C_DECL_IMPLICIT (decl) = 1;
1856 /* If this function is global, then it must already be in the
1857 global scope, so there's no need to push it again. */
1858 if (current_scope == global_scope)
1860 /* If this is a local declaration, make a copy; we can't have
1861 the same DECL listed in two different scopes. */
1862 return pushdecl (copy_node (decl));
1865 /* Not seen before. */
1866 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1867 DECL_EXTERNAL (decl) = 1;
1868 TREE_PUBLIC (decl) = 1;
1869 C_DECL_IMPLICIT (decl) = 1;
1870 implicit_decl_warning (functionid);
1872 /* C89 says implicit declarations are in the innermost block.
1873 So we record the decl in the standard fashion. */
1874 decl = pushdecl (decl);
1876 /* No need to call objc_check_decl here - it's a function type. */
1877 rest_of_decl_compilation (decl, NULL, 0, 0);
1879 /* Write a record describing this implicit function declaration
1880 to the prototypes file (if requested). */
1881 gen_aux_info_record (decl, 0, 1, 0);
1883 /* Possibly apply some default attributes to this implicit declaration. */
1884 decl_attributes (&decl, NULL_TREE, 0);
1890 implicit_decl_warning (tree id)
1892 const char *name = IDENTIFIER_POINTER (id);
1893 if (mesg_implicit_function_declaration == 2)
1894 error ("implicit declaration of function `%s'", name);
1895 else if (mesg_implicit_function_declaration == 1)
1896 warning ("implicit declaration of function `%s'", name);
1899 /* Return zero if the declaration NEWDECL is valid
1900 when the declaration OLDDECL (assumed to be for the same name)
1901 has already been seen.
1902 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1903 and 3 if it is a conflicting declaration. */
1906 redeclaration_error_message (tree newdecl, tree olddecl)
1908 if (TREE_CODE (newdecl) == TYPE_DECL)
1910 /* Do not complain about type redeclarations where at least one
1911 declaration was in a system header. */
1912 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1916 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1918 /* Declarations of functions can insist on internal linkage
1919 but they can't be inconsistent with internal linkage,
1920 so there can be no error on that account.
1921 However defining the same name twice is no good. */
1922 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1923 /* However, defining once as extern inline and a second
1924 time in another way is ok. */
1925 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1926 && ! (DECL_DECLARED_INLINE_P (newdecl)
1927 && DECL_EXTERNAL (newdecl))))
1931 else if (C_DECL_FILE_SCOPE (newdecl))
1933 /* Objects declared at file scope: */
1934 /* If at least one is a reference, it's ok. */
1935 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1937 /* Reject two definitions. */
1938 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1940 /* Now we have two tentative defs, or one tentative and one real def. */
1941 /* Insist that the linkage match. */
1942 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1946 else if (current_scope->parm_flag
1947 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1951 /* Newdecl has block scope. If olddecl has block scope also, then
1952 reject two definitions, and reject a definition together with an
1953 external reference. Otherwise, it is OK, because newdecl must
1954 be an extern reference to olddecl. */
1955 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1956 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1962 /* Issue an error message for a reference to an undeclared variable
1963 ID, including a reference to a builtin outside of function-call
1964 context. Establish a binding of the identifier to error_mark_node
1965 in an appropriate scope, which will suppress further errors for the
1968 undeclared_variable (tree id)
1970 static bool already = false;
1972 if (current_function_decl == 0)
1974 error ("`%s' undeclared here (not in a function)",
1975 IDENTIFIER_POINTER (id));
1976 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1980 error ("`%s' undeclared (first use in this function)",
1981 IDENTIFIER_POINTER (id));
1985 error ("(Each undeclared identifier is reported only once");
1986 error ("for each function it appears in.)");
1990 pushdecl_function_level (error_mark_node, id);
1994 /* Subroutine of lookup_label, declare_label, define_label: construct a
1995 LABEL_DECL with all the proper frills. */
1998 make_label (tree name, location_t location)
2000 tree label = build_decl (LABEL_DECL, name, void_type_node);
2002 DECL_CONTEXT (label) = current_function_decl;
2003 DECL_MODE (label) = VOIDmode;
2004 DECL_SOURCE_LOCATION (label) = location;
2009 /* Another subroutine of lookup_label, declare_label, define_label:
2010 set up the binding of name to LABEL_DECL in the given SCOPE. */
2013 bind_label (tree name, tree label, struct c_scope *scope)
2015 if (IDENTIFIER_LABEL_VALUE (name))
2016 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
2018 IDENTIFIER_LABEL_VALUE (name) = label;
2020 TREE_CHAIN (label) = scope->names;
2021 scope->names = label;
2024 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2025 Create one if none exists so far for the current function.
2026 This is called when a label is used in a goto expression or
2027 has its address taken. */
2030 lookup_label (tree name)
2034 if (current_function_decl == 0)
2036 error ("label %s referenced outside of any function",
2037 IDENTIFIER_POINTER (name));
2041 /* Use a label already defined or ref'd with this name, but not if
2042 it is inherited from a containing function and wasn't declared
2044 label = IDENTIFIER_LABEL_VALUE (name);
2045 if (label && (DECL_CONTEXT (label) == current_function_decl
2046 || C_DECLARED_LABEL_FLAG (label)))
2048 /* If the label has only been declared, update its apparent
2049 location to point here, for better diagnostics if it
2050 turns out not to have been defined. */
2051 if (!TREE_USED (label))
2052 DECL_SOURCE_LOCATION (label) = input_location;
2056 /* No label binding for that identifier; make one. */
2057 label = make_label (name, input_location);
2059 /* Ordinary labels go in the current function scope. */
2060 bind_label (name, label, current_function_scope);
2064 /* Make a label named NAME in the current function, shadowing silently
2065 any that may be inherited from containing functions or containing
2066 scopes. This is called for __label__ declarations. */
2068 /* Note that valid use, if the label being shadowed comes from another
2069 scope in the same function, requires calling declare_nonlocal_label
2070 right away. (Is this still true? -zw 2003-07-17) */
2073 declare_label (tree name)
2075 tree label = IDENTIFIER_LABEL_VALUE (name);
2078 /* Check to make sure that the label hasn't already been declared
2080 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2083 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2084 error ("%Hthis is a previous declaration",
2085 &DECL_SOURCE_LOCATION (dup));
2087 /* Just use the previous declaration. */
2091 label = make_label (name, input_location);
2092 C_DECLARED_LABEL_FLAG (label) = 1;
2094 /* Declared labels go in the current scope. */
2095 bind_label (name, label, current_scope);
2099 /* Define a label, specifying the location in the source file.
2100 Return the LABEL_DECL node for the label, if the definition is valid.
2101 Otherwise return 0. */
2104 define_label (location_t location, tree name)
2108 /* Find any preexisting label with this name. It is an error
2109 if that label has already been defined in this function, or
2110 if there is a containing function with a declared label with
2112 label = IDENTIFIER_LABEL_VALUE (name);
2115 && ((DECL_CONTEXT (label) == current_function_decl
2116 && DECL_INITIAL (label) != 0)
2117 || (DECL_CONTEXT (label) != current_function_decl
2118 && C_DECLARED_LABEL_FLAG (label))))
2120 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2121 error ("%Hduplicate label `%D'", &location, label);
2122 if (DECL_INITIAL (label))
2123 error ("%H`%D' previously defined here", prev_loc, label);
2125 error ("%H`%D' previously declared here", prev_loc, label);
2128 else if (label && DECL_CONTEXT (label) == current_function_decl)
2130 /* The label has been used or declared already in this function,
2131 but not defined. Update its location to point to this
2133 DECL_SOURCE_LOCATION (label) = location;
2137 /* No label binding for that identifier; make one. */
2138 label = make_label (name, location);
2140 /* Ordinary labels go in the current function scope. */
2141 bind_label (name, label, current_function_scope);
2144 if (warn_traditional && !in_system_header && lookup_name (name))
2145 warning ("%Htraditional C lacks a separate namespace for labels, "
2146 "identifier `%s' conflicts", &location,
2147 IDENTIFIER_POINTER (name));
2149 /* Mark label as having been defined. */
2150 DECL_INITIAL (label) = error_mark_node;
2154 /* Return the list of declarations of the current scope.
2155 Note that this list is in reverse order. */
2160 return current_scope->names;
2163 /* Return the list of type-tags (for structs, etc) of the current scope. */
2168 return current_scope->tags;
2171 /* Given NAME, an IDENTIFIER_NODE,
2172 return the structure (or union or enum) definition for that name.
2173 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2174 CODE says which kind of type the caller wants;
2175 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2176 If the wrong kind of type is found, an error is reported. */
2179 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2181 tree tag = IDENTIFIER_TAG_VALUE (name);
2187 /* We only care about whether it's in this level if
2188 thislevel_only was set or it might be a type clash. */
2189 if (thislevel_only || TREE_CODE (tag) != code)
2191 if (current_scope == global_scope
2192 || purpose_member (name, current_scope->tags))
2196 if (thislevel_only && !thislevel)
2199 if (TREE_CODE (tag) != code)
2201 /* Definition isn't the kind we were looking for. */
2202 pending_invalid_xref = name;
2203 pending_invalid_xref_location = input_location;
2205 /* If in the same binding level as a declaration as a tag
2206 of a different type, this must not be allowed to
2207 shadow that tag, so give the error immediately.
2208 (For example, "struct foo; union foo;" is invalid.) */
2210 pending_xref_error ();
2215 /* Print an error message now
2216 for a recent invalid struct, union or enum cross reference.
2217 We don't print them immediately because they are not invalid
2218 when used in the `struct foo;' construct for shadowing. */
2221 pending_xref_error (void)
2223 if (pending_invalid_xref != 0)
2224 error ("%H`%s' defined as wrong kind of tag",
2225 &pending_invalid_xref_location,
2226 IDENTIFIER_POINTER (pending_invalid_xref));
2227 pending_invalid_xref = 0;
2231 /* Look up NAME in the current scope and its superiors
2232 in the namespace of variables, functions and typedefs.
2233 Return a ..._DECL node of some kind representing its definition,
2234 or return 0 if it is undefined. */
2237 lookup_name (tree name)
2239 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2240 if (decl == 0 || decl == error_mark_node)
2242 if (C_DECL_INVISIBLE (decl))
2247 /* Similar to `lookup_name' but look only at the current scope. */
2250 lookup_name_current_level (tree name)
2252 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2254 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2257 if (current_scope == global_scope)
2260 /* Scan the current scope for a decl with name NAME.
2261 For PARM_DECLs, we have to look at both ->parms and ->names, since
2262 forward parameter declarations wind up on the ->names list. */
2263 if (TREE_CODE (decl) == PARM_DECL
2264 && chain_member (decl, current_scope->parms))
2266 if (chain_member (decl, current_scope->names))
2272 /* Create the predefined scalar types of C,
2273 and some nodes representing standard constants (0, 1, (void *) 0).
2274 Initialize the global scope.
2275 Make definitions for built-in primitive functions. */
2278 c_init_decl_processing (void)
2281 tree ptr_ftype_void, ptr_ftype_ptr;
2282 location_t save_loc = input_location;
2284 /* Adds some ggc roots, and reserved words for c-parse.in. */
2287 current_function_decl = 0;
2289 /* Make the c_scope structure for global names. */
2291 global_scope = current_scope;
2293 /* Declarations from c_common_nodes_and_builtins must not be associated
2294 with this input file, lest we get differences between using and not
2295 using preprocessed headers. */
2296 input_location.file = "<internal>";
2297 input_location.line = 0;
2299 /* Make the DECL for the toplevel file scope. */
2300 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2302 build_common_tree_nodes (flag_signed_char);
2304 c_common_nodes_and_builtins ();
2306 /* In C, comparisons and TRUTH_* expressions have type int. */
2307 truthvalue_type_node = integer_type_node;
2308 truthvalue_true_node = integer_one_node;
2309 truthvalue_false_node = integer_zero_node;
2311 /* Even in C99, which has a real boolean type. */
2312 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2313 boolean_type_node));
2315 endlink = void_list_node;
2316 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2318 = build_function_type (ptr_type_node,
2319 tree_cons (NULL_TREE, ptr_type_node, endlink));
2321 input_location = save_loc;
2323 pedantic_lvalues = pedantic;
2325 make_fname_decl = c_make_fname_decl;
2326 start_fname_decls ();
2328 builtin_decls = global_scope->names;
2331 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2332 decl, NAME is the initialization string and TYPE_DEP indicates whether
2333 NAME depended on the type of the function. As we don't yet implement
2334 delayed emission of static data, we mark the decl as emitted
2335 so it is not placed in the output. Anything using it must therefore pull
2336 out the STRING_CST initializer directly. This does mean that these names
2337 are string merging candidates, which is wrong for C99's __func__. FIXME. */
2340 c_make_fname_decl (tree id, int type_dep)
2342 const char *name = fname_as_string (type_dep);
2343 tree decl, type, init;
2344 size_t length = strlen (name);
2346 type = build_array_type
2347 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2348 build_index_type (size_int (length)));
2350 decl = build_decl (VAR_DECL, id, type);
2352 TREE_STATIC (decl) = 1;
2353 TREE_READONLY (decl) = 1;
2354 DECL_ARTIFICIAL (decl) = 1;
2356 init = build_string (length + 1, name);
2357 TREE_TYPE (init) = type;
2358 DECL_INITIAL (decl) = init;
2360 TREE_USED (decl) = 1;
2362 if (current_function_decl)
2363 pushdecl_function_level (decl, DECL_NAME (decl));
2365 finish_decl (decl, init, NULL_TREE);
2370 /* Return a definition for a builtin function named NAME and whose data type
2371 is TYPE. TYPE should be a function type with argument types.
2372 FUNCTION_CODE tells later passes how to compile calls to this function.
2373 See tree.h for its possible values.
2375 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2376 the name to be called if we can't opencode the function. If
2377 ATTRS is nonzero, use that for the function's attribute list. */
2380 builtin_function (const char *name, tree type, int function_code,
2381 enum built_in_class class, const char *library_name,
2384 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2385 DECL_EXTERNAL (decl) = 1;
2386 TREE_PUBLIC (decl) = 1;
2388 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2389 make_decl_rtl (decl, NULL);
2391 DECL_BUILT_IN_CLASS (decl) = class;
2392 DECL_FUNCTION_CODE (decl) = function_code;
2394 /* Warn if a function in the namespace for users
2395 is used without an occasion to consider it declared. */
2396 if (name[0] != '_' || name[1] != '_')
2397 C_DECL_INVISIBLE (decl) = 1;
2399 /* Possibly apply some default attributes to this built-in function. */
2401 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2403 decl_attributes (&decl, NULL_TREE, 0);
2408 /* Apply default attributes to a function, if a system function with default
2412 c_insert_default_attributes (tree decl)
2414 if (!TREE_PUBLIC (decl))
2416 c_common_insert_default_attributes (decl);
2419 /* Called when a declaration is seen that contains no names to declare.
2420 If its type is a reference to a structure, union or enum inherited
2421 from a containing scope, shadow that tag name for the current scope
2422 with a forward reference.
2423 If its type defines a new named structure or union
2424 or defines an enum, it is valid but we need not do anything here.
2425 Otherwise, it is an error. */
2428 shadow_tag (tree declspecs)
2430 shadow_tag_warned (declspecs, 0);
2434 shadow_tag_warned (tree declspecs, int warned)
2437 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2444 pending_invalid_xref = 0;
2446 /* Remove the attributes from declspecs, since they will confuse the
2448 split_specs_attrs (declspecs, &specs, &attrs);
2450 for (link = specs; link; link = TREE_CHAIN (link))
2452 tree value = TREE_VALUE (link);
2453 enum tree_code code = TREE_CODE (value);
2455 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2456 /* Used to test also that TYPE_SIZE (value) != 0.
2457 That caused warning for `struct foo;' at top level in the file. */
2459 tree name = TYPE_NAME (value);
2466 if (warned != 1 && code != ENUMERAL_TYPE)
2467 /* Empty unnamed enum OK */
2469 pedwarn ("unnamed struct/union that defines no instances");
2475 t = lookup_tag (code, name, 1);
2479 t = make_node (code);
2486 if (!warned && ! in_system_header)
2488 warning ("useless keyword or type name in empty declaration");
2495 error ("two types specified in one empty declaration");
2500 pedwarn ("empty declaration");
2504 /* Construct an array declarator. EXPR is the expression inside [], or
2505 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2506 to the pointer to which a parameter array is converted). STATIC_P is
2507 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2508 is nonzero is the array is [*], a VLA of unspecified length which is
2509 nevertheless a complete type (not currently implemented by GCC),
2510 zero otherwise. The declarator is constructed as an ARRAY_REF
2511 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2512 left of the [] (filled by in set_array_declarator_type) and operand 1
2513 is the expression inside; whose TREE_TYPE is the type qualifiers and
2514 which has TREE_STATIC set if "static" is used. */
2517 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2520 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2521 TREE_TYPE (decl) = quals;
2522 TREE_STATIC (decl) = (static_p ? 1 : 0);
2523 if (pedantic && !flag_isoc99)
2525 if (static_p || quals != NULL_TREE)
2526 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2528 pedwarn ("ISO C90 does not support `[*]' array declarators");
2531 warning ("GCC does not yet properly implement `[*]' array declarators");
2535 /* Set the type of an array declarator. DECL is the declarator, as
2536 constructed by build_array_declarator; TYPE is what appears on the left
2537 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2538 abstract declarator, zero otherwise; this is used to reject static and
2539 type qualifiers in abstract declarators, where they are not in the
2543 set_array_declarator_type (tree decl, tree type, int abstract_p)
2545 TREE_OPERAND (decl, 0) = type;
2546 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2547 error ("static or type qualifiers in abstract declarator");
2551 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2554 groktypename (tree typename)
2558 if (TREE_CODE (typename) != TREE_LIST)
2561 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2563 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2565 /* Apply attributes. */
2566 decl_attributes (&typename, attrs, 0);
2571 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2574 groktypename_in_parm_context (tree typename)
2576 if (TREE_CODE (typename) != TREE_LIST)
2578 return grokdeclarator (TREE_VALUE (typename),
2579 TREE_PURPOSE (typename),
2583 /* Decode a declarator in an ordinary declaration or data definition.
2584 This is called as soon as the type information and variable name
2585 have been parsed, before parsing the initializer if any.
2586 Here we create the ..._DECL node, fill in its type,
2587 and put it on the list of decls for the current context.
2588 The ..._DECL node is returned as the value.
2590 Exception: for arrays where the length is not specified,
2591 the type is left null, to be filled in by `finish_decl'.
2593 Function definitions do not come here; they go to start_function
2594 instead. However, external and forward declarations of functions
2595 do go through here. Structure field declarations are done by
2596 grokfield and not through here. */
2599 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2604 /* An object declared as __attribute__((deprecated)) suppresses
2605 warnings of uses of other deprecated items. */
2606 if (lookup_attribute ("deprecated", attributes))
2607 deprecated_state = DEPRECATED_SUPPRESS;
2609 decl = grokdeclarator (declarator, declspecs,
2610 NORMAL, initialized);
2612 deprecated_state = DEPRECATED_NORMAL;
2614 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2615 && MAIN_NAME_P (DECL_NAME (decl)))
2616 warning ("%H'%D' is usually a function",
2617 &DECL_SOURCE_LOCATION (decl), decl);
2620 /* Is it valid for this decl to have an initializer at all?
2621 If not, set INITIALIZED to zero, which will indirectly
2622 tell `finish_decl' to ignore the initializer once it is parsed. */
2623 switch (TREE_CODE (decl))
2626 error ("typedef `%s' is initialized (use __typeof__ instead)",
2627 IDENTIFIER_POINTER (DECL_NAME (decl)));
2632 error ("function `%s' is initialized like a variable",
2633 IDENTIFIER_POINTER (DECL_NAME (decl)));
2638 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2639 error ("parameter `%s' is initialized",
2640 IDENTIFIER_POINTER (DECL_NAME (decl)));
2645 /* Don't allow initializations for incomplete types
2646 except for arrays which might be completed by the initialization. */
2648 /* This can happen if the array size is an undefined macro. We already
2649 gave a warning, so we don't need another one. */
2650 if (TREE_TYPE (decl) == error_mark_node)
2652 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2654 /* A complete type is ok if size is fixed. */
2656 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2657 || C_DECL_VARIABLE_SIZE (decl))
2659 error ("variable-sized object may not be initialized");
2663 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2665 error ("variable `%s' has initializer but incomplete type",
2666 IDENTIFIER_POINTER (DECL_NAME (decl)));
2669 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2671 error ("elements of array `%s' have incomplete type",
2672 IDENTIFIER_POINTER (DECL_NAME (decl)));
2679 DECL_EXTERNAL (decl) = 0;
2680 if (current_scope == global_scope)
2681 TREE_STATIC (decl) = 1;
2683 /* Tell `pushdecl' this is an initialized decl
2684 even though we don't yet have the initializer expression.
2685 Also tell `finish_decl' it may store the real initializer. */
2686 DECL_INITIAL (decl) = error_mark_node;
2689 /* If this is a function declaration, write a record describing it to the
2690 prototypes file (if requested). */
2692 if (TREE_CODE (decl) == FUNCTION_DECL)
2693 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2695 /* ANSI specifies that a tentative definition which is not merged with
2696 a non-tentative definition behaves exactly like a definition with an
2697 initializer equal to zero. (Section 3.7.2)
2699 -fno-common gives strict ANSI behavior, though this tends to break
2700 a large body of code that grew up without this rule.
2702 Thread-local variables are never common, since there's no entrenched
2703 body of code to break, and it allows more efficient variable references
2704 in the presence of dynamic linking. */
2706 if (TREE_CODE (decl) == VAR_DECL
2708 && TREE_PUBLIC (decl)
2709 && !DECL_THREAD_LOCAL (decl)
2711 DECL_COMMON (decl) = 1;
2713 /* Set attributes here so if duplicate decl, will have proper attributes. */
2714 decl_attributes (&decl, attributes, 0);
2716 if (TREE_CODE (decl) == FUNCTION_DECL
2717 && DECL_DECLARED_INLINE_P (decl)
2718 && DECL_UNINLINABLE (decl)
2719 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2720 warning ("%Hinline function '%D' given attribute noinline",
2721 &DECL_SOURCE_LOCATION (decl), decl);
2723 /* Add this decl to the current scope.
2724 TEM may equal DECL or it may be a previous decl of the same name. */
2725 tem = pushdecl (decl);
2727 /* For a local variable, define the RTL now. */
2728 if (current_scope != global_scope
2729 /* But not if this is a duplicate decl
2730 and we preserved the rtl from the previous one
2731 (which may or may not happen). */
2732 && !DECL_RTL_SET_P (tem)
2733 && C_DECL_FILE_SCOPE (tem))
2735 if (TREE_TYPE (tem) != error_mark_node
2736 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2737 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2738 && DECL_INITIAL (tem) != 0)))
2745 /* Finish processing of a declaration;
2746 install its initial value.
2747 If the length of an array type is not known before,
2748 it must be determined now, from the initial value, or it is an error. */
2751 finish_decl (tree decl, tree init, tree asmspec_tree)
2753 tree type = TREE_TYPE (decl);
2754 int was_incomplete = (DECL_SIZE (decl) == 0);
2755 const char *asmspec = 0;
2757 /* If a name was specified, get the string. */
2758 if (current_scope == global_scope)
2759 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2761 asmspec = TREE_STRING_POINTER (asmspec_tree);
2763 /* If `start_decl' didn't like having an initialization, ignore it now. */
2764 if (init != 0 && DECL_INITIAL (decl) == 0)
2767 /* Don't crash if parm is initialized. */
2768 if (TREE_CODE (decl) == PARM_DECL)
2772 store_init_value (decl, init);
2774 /* Deduce size of array from initialization, if not already known */
2775 if (TREE_CODE (type) == ARRAY_TYPE
2776 && TYPE_DOMAIN (type) == 0
2777 && TREE_CODE (decl) != TYPE_DECL)
2780 = (TREE_STATIC (decl)
2781 /* Even if pedantic, an external linkage array
2782 may have incomplete type at first. */
2783 ? pedantic && !TREE_PUBLIC (decl)
2784 : !DECL_EXTERNAL (decl));
2786 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2788 /* Get the completed type made by complete_array_type. */
2789 type = TREE_TYPE (decl);
2792 error ("%Hinitializer fails to determine size of '%D'",
2793 &DECL_SOURCE_LOCATION (decl), decl);
2795 else if (failure == 2)
2798 error ("%Harray size missing in '%D'",
2799 &DECL_SOURCE_LOCATION (decl), decl);
2800 /* If a `static' var's size isn't known,
2801 make it extern as well as static, so it does not get
2803 If it is not `static', then do not mark extern;
2804 finish_incomplete_decl will give it a default size
2805 and it will get allocated. */
2806 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2807 DECL_EXTERNAL (decl) = 1;
2810 /* TYPE_MAX_VALUE is always one less than the number of elements
2811 in the array, because we start counting at zero. Therefore,
2812 warn only if the value is less than zero. */
2813 else if (pedantic && TYPE_DOMAIN (type) != 0
2814 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2815 error ("%Hzero or negative size array '%D'",
2816 &DECL_SOURCE_LOCATION (decl), decl);
2818 layout_decl (decl, 0);
2821 if (TREE_CODE (decl) == VAR_DECL)
2823 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2824 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2825 layout_decl (decl, 0);
2827 if (DECL_SIZE (decl) == 0
2828 /* Don't give an error if we already gave one earlier. */
2829 && TREE_TYPE (decl) != error_mark_node
2830 && (TREE_STATIC (decl)
2832 /* A static variable with an incomplete type
2833 is an error if it is initialized.
2834 Also if it is not file scope.
2835 Otherwise, let it through, but if it is not `extern'
2836 then it may cause an error message later. */
2837 (DECL_INITIAL (decl) != 0
2838 || !C_DECL_FILE_SCOPE (decl))
2840 /* An automatic variable with an incomplete type
2842 !DECL_EXTERNAL (decl)))
2844 error ("%Hstorage size of '%D' isn't known",
2845 &DECL_SOURCE_LOCATION (decl), decl);
2846 TREE_TYPE (decl) = error_mark_node;
2849 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2850 && DECL_SIZE (decl) != 0)
2852 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2853 constant_expression_warning (DECL_SIZE (decl));
2855 error ("%Hstorage size of '%D' isn't constant",
2856 &DECL_SOURCE_LOCATION (decl), decl);
2859 if (TREE_USED (type))
2860 TREE_USED (decl) = 1;
2863 /* If this is a function and an assembler name is specified, reset DECL_RTL
2864 so we can give it its new name. Also, update built_in_decls if it
2865 was a normal built-in. */
2866 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2868 /* ASMSPEC is given, and not the name of a register. Mark the
2869 name with a star so assemble_name won't munge it. */
2870 char *starred = alloca (strlen (asmspec) + 2);
2872 strcpy (starred + 1, asmspec);
2874 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2876 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2877 SET_DECL_RTL (builtin, NULL_RTX);
2878 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2879 #ifdef TARGET_MEM_FUNCTIONS
2880 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2881 init_block_move_fn (starred);
2882 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2883 init_block_clear_fn (starred);
2885 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2886 init_block_move_fn (starred);
2887 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2888 init_block_clear_fn (starred);
2891 SET_DECL_RTL (decl, NULL_RTX);
2892 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2895 /* If #pragma weak was used, mark the decl weak now. */
2896 if (current_scope == global_scope)
2897 maybe_apply_pragma_weak (decl);
2899 /* Output the assembler code and/or RTL code for variables and functions,
2900 unless the type is an undefined structure or union.
2901 If not, it will get done when the type is completed. */
2903 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2905 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2906 if (c_dialect_objc ())
2907 objc_check_decl (decl);
2909 if (C_DECL_FILE_SCOPE (decl))
2911 if (DECL_INITIAL (decl) == NULL_TREE
2912 || DECL_INITIAL (decl) == error_mark_node)
2913 /* Don't output anything
2914 when a tentative file-scope definition is seen.
2915 But at end of compilation, do output code for them. */
2916 DECL_DEFER_OUTPUT (decl) = 1;
2917 rest_of_decl_compilation (decl, asmspec, true, 0);
2921 /* This is a local variable. If there is an ASMSPEC, the
2922 user has requested that we handle it specially. */
2925 /* In conjunction with an ASMSPEC, the `register'
2926 keyword indicates that we should place the variable
2927 in a particular register. */
2928 if (DECL_REGISTER (decl))
2929 DECL_C_HARD_REGISTER (decl) = 1;
2931 /* If this is not a static variable, issue a warning.
2932 It doesn't make any sense to give an ASMSPEC for an
2933 ordinary, non-register local variable. Historically,
2934 GCC has accepted -- but ignored -- the ASMSPEC in
2936 if (TREE_CODE (decl) == VAR_DECL
2937 && !DECL_REGISTER (decl)
2938 && !TREE_STATIC (decl))
2939 warning ("%Hignoring asm-specifier for non-static local "
2940 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2942 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2945 if (TREE_CODE (decl) != FUNCTION_DECL)
2946 add_decl_stmt (decl);
2949 if (!C_DECL_FILE_SCOPE (decl))
2951 /* Recompute the RTL of a local array now
2952 if it used to be an incomplete type. */
2954 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2956 /* If we used it already as memory, it must stay in memory. */
2957 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2958 /* If it's still incomplete now, no init will save it. */
2959 if (DECL_SIZE (decl) == 0)
2960 DECL_INITIAL (decl) = 0;
2965 /* If this was marked 'used', be sure it will be output. */
2966 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2967 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2969 if (TREE_CODE (decl) == TYPE_DECL)
2971 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2972 if (c_dialect_objc ())
2973 objc_check_decl (decl);
2974 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2977 /* At the end of a declaration, throw away any variable type sizes
2978 of types defined inside that declaration. There is no use
2979 computing them in the following function definition. */
2980 if (current_scope == global_scope)
2981 get_pending_sizes ();
2983 /* Install a cleanup (aka destructor) if one was given. */
2984 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2986 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2989 static bool eh_initialized_p;
2991 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2992 tree cleanup_decl = lookup_name (cleanup_id);
2995 /* Build "cleanup(&decl)" for the destructor. */
2996 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2997 cleanup = build_tree_list (NULL_TREE, cleanup);
2998 cleanup = build_function_call (cleanup_decl, cleanup);
3000 /* Don't warn about decl unused; the cleanup uses it. */
3001 TREE_USED (decl) = 1;
3003 /* Initialize EH, if we've been told to do so. */
3004 if (flag_exceptions && !eh_initialized_p)
3006 eh_initialized_p = true;
3007 eh_personality_libfunc
3008 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3009 ? "__gcc_personality_sj0"
3010 : "__gcc_personality_v0");
3011 using_eh_for_cleanups ();
3014 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3019 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3020 and push that on the current scope. */
3023 push_parm_decl (tree parm)
3027 /* Don't attempt to expand sizes while parsing this decl.
3028 (We can get here with i_s_e 1 somehow from Objective-C.) */
3029 int save_immediate_size_expand = immediate_size_expand;
3030 immediate_size_expand = 0;
3032 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3033 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3034 decl_attributes (&decl, TREE_VALUE (parm), 0);
3036 decl = pushdecl (decl);
3038 finish_decl (decl, NULL_TREE, NULL_TREE);
3040 immediate_size_expand = save_immediate_size_expand;
3043 /* Shift all the existing parameter decls to the variables list,
3044 and reset the parameters list. Used when a ; terminating
3045 forward parameter decls is encountered. */
3048 mark_forward_parm_decls (void)
3052 if (pedantic && !current_scope->warned_forward_parm_decls)
3054 pedwarn ("ISO C forbids forward parameter declarations");
3055 current_scope->warned_forward_parm_decls = true;
3058 for (last = 0, parm = current_scope->parms;
3060 last = parm, parm = TREE_CHAIN (parm))
3061 TREE_ASM_WRITTEN (parm) = 1;
3063 TREE_CHAIN (last) = current_scope->names;
3064 current_scope->names = current_scope->parms;
3065 current_scope->parms = 0;
3068 static GTY(()) int compound_literal_number;
3070 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3071 literal, which may be an incomplete array type completed by the
3072 initializer; INIT is a CONSTRUCTOR that initializes the compound
3076 build_compound_literal (tree type, tree init)
3078 /* We do not use start_decl here because we have a type, not a declarator;
3079 and do not use finish_decl because the decl should be stored inside
3080 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3081 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3084 DECL_EXTERNAL (decl) = 0;
3085 TREE_PUBLIC (decl) = 0;
3086 TREE_STATIC (decl) = (current_scope == global_scope);
3087 DECL_CONTEXT (decl) = current_function_decl;
3088 TREE_USED (decl) = 1;
3089 TREE_TYPE (decl) = type;
3090 TREE_READONLY (decl) = TREE_READONLY (type);
3091 store_init_value (decl, init);
3093 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3095 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3100 type = TREE_TYPE (decl);
3101 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3102 return error_mark_node;
3104 stmt = build_stmt (DECL_STMT, decl);
3105 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3106 TREE_SIDE_EFFECTS (complit) = 1;
3108 layout_decl (decl, 0);
3110 if (TREE_STATIC (decl))
3112 /* This decl needs a name for the assembler output. We also need
3113 a unique suffix to be added to the name. */
3116 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3117 compound_literal_number);
3118 compound_literal_number++;
3119 DECL_NAME (decl) = get_identifier (name);
3120 DECL_DEFER_OUTPUT (decl) = 1;
3121 DECL_COMDAT (decl) = 1;
3122 DECL_ARTIFICIAL (decl) = 1;
3124 rest_of_decl_compilation (decl, NULL, 1, 0);
3130 /* Make TYPE a complete type based on INITIAL_VALUE.
3131 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3132 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3135 complete_array_type (tree type, tree initial_value, int do_default)
3137 tree maxindex = NULL_TREE;
3142 /* Note MAXINDEX is really the maximum index,
3143 one less than the size. */
3144 if (TREE_CODE (initial_value) == STRING_CST)
3147 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3148 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3151 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3153 tree elts = CONSTRUCTOR_ELTS (initial_value);
3154 maxindex = build_int_2 (-1, -1);
3155 for (; elts; elts = TREE_CHAIN (elts))
3157 if (TREE_PURPOSE (elts))
3158 maxindex = TREE_PURPOSE (elts);
3160 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3161 maxindex, integer_one_node));
3163 maxindex = copy_node (maxindex);
3167 /* Make an error message unless that happened already. */
3168 if (initial_value != error_mark_node)
3171 /* Prevent further error messages. */
3172 maxindex = build_int_2 (0, 0);
3179 maxindex = build_int_2 (0, 0);
3185 TYPE_DOMAIN (type) = build_index_type (maxindex);
3186 if (!TREE_TYPE (maxindex))
3187 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3190 /* Lay out the type now that we can get the real answer. */
3197 /* Determine whether TYPE is a structure with a flexible array member,
3198 or a union containing such a structure (possibly recursively). */
3201 flexible_array_type_p (tree type)
3204 switch (TREE_CODE (type))
3207 x = TYPE_FIELDS (type);
3210 while (TREE_CHAIN (x) != NULL_TREE)
3212 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3213 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3214 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3215 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3219 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3221 if (flexible_array_type_p (TREE_TYPE (x)))
3230 /* Given declspecs and a declarator,
3231 determine the name and type of the object declared
3232 and construct a ..._DECL node for it.
3233 (In one case we can return a ..._TYPE node instead.
3234 For invalid input we sometimes return 0.)
3236 DECLSPECS is a chain of tree_list nodes whose value fields
3237 are the storage classes and type specifiers.
3239 DECL_CONTEXT says which syntactic context this declaration is in:
3240 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3241 FUNCDEF for a function definition. Like NORMAL but a few different
3242 error messages in each case. Return value may be zero meaning
3243 this definition is too screwy to try to parse.
3244 PARM for a parameter declaration (either within a function prototype
3245 or before a function body). Make a PARM_DECL, or return void_type_node.
3246 TYPENAME if for a typename (in a cast or sizeof).
3247 Don't make a DECL node; just return the ..._TYPE node.
3248 FIELD for a struct or union field; make a FIELD_DECL.
3249 BITFIELD for a field with specified width.
3250 INITIALIZED is 1 if the decl has an initializer.
3252 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3253 It may also be so in the PARM case, for a prototype where the
3254 argument type is specified but not the name.
3256 This function is where the complicated C meanings of `static'
3257 and `extern' are interpreted. */
3260 grokdeclarator (tree declarator, tree declspecs,
3261 enum decl_context decl_context, int initialized)
3265 tree type = NULL_TREE;
3270 int type_quals = TYPE_UNQUALIFIED;
3272 int explicit_int = 0;
3273 int explicit_char = 0;
3274 int defaulted_int = 0;
3275 tree typedef_decl = 0;
3277 tree typedef_type = 0;
3278 int funcdef_flag = 0;
3279 enum tree_code innermost_code = ERROR_MARK;
3281 int size_varies = 0;
3282 tree decl_attr = NULL_TREE;
3283 tree array_ptr_quals = NULL_TREE;
3284 int array_parm_static = 0;
3285 tree returned_attrs = NULL_TREE;
3287 if (decl_context == BITFIELD)
3288 bitfield = 1, decl_context = FIELD;
3290 if (decl_context == FUNCDEF)
3291 funcdef_flag = 1, decl_context = NORMAL;
3293 /* Look inside a declarator for the name being declared
3294 and get it as a string, for an error message. */
3296 tree decl = declarator;
3300 switch (TREE_CODE (decl))
3305 innermost_code = TREE_CODE (decl);
3306 decl = TREE_OPERAND (decl, 0);
3310 decl = TREE_VALUE (decl);
3313 case IDENTIFIER_NODE:
3314 name = IDENTIFIER_POINTER (decl);
3325 /* A function definition's declarator must have the form of
3326 a function declarator. */
3328 if (funcdef_flag && innermost_code != CALL_EXPR)
3331 /* If this looks like a function definition, make it one,
3332 even if it occurs where parms are expected.
3333 Then store_parm_decls will reject it and not use it as a parm. */
3334 if (decl_context == NORMAL && !funcdef_flag
3335 && current_scope->parm_flag)
3336 decl_context = PARM;
3338 /* Look through the decl specs and record which ones appear.
3339 Some typespecs are defined as built-in typenames.
3340 Others, the ones that are modifiers of other types,
3341 are represented by bits in SPECBITS: set the bits for
3342 the modifiers that appear. Storage class keywords are also in SPECBITS.
3344 If there is a typedef name or a type, store the type in TYPE.
3345 This includes builtin typedefs such as `int'.
3347 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3348 and did not come from a user typedef.
3350 Set LONGLONG if `long' is mentioned twice. */
3352 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3354 tree id = TREE_VALUE (spec);
3356 /* If the entire declaration is itself tagged as deprecated then
3357 suppress reports of deprecated items. */
3358 if (id && TREE_DEPRECATED (id))
3360 if (deprecated_state != DEPRECATED_SUPPRESS)
3361 warn_deprecated_use (id);
3364 if (id == ridpointers[(int) RID_INT])
3366 if (id == ridpointers[(int) RID_CHAR])
3369 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3371 enum rid i = C_RID_CODE (id);
3372 if ((int) i <= (int) RID_LAST_MODIFIER)
3374 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3377 error ("`long long long' is too long for GCC");
3380 if (pedantic && !flag_isoc99 && ! in_system_header
3382 pedwarn ("ISO C90 does not support `long long'");
3386 else if (specbits & (1 << (int) i))
3388 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3391 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3394 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3397 /* Diagnose "__thread extern". Recall that this list
3398 is in the reverse order seen in the text. */
3400 && (specbits & (1 << (int) RID_EXTERN
3401 | 1 << (int) RID_STATIC)))
3403 if (specbits & 1 << (int) RID_EXTERN)
3404 error ("`__thread' before `extern'");
3406 error ("`__thread' before `static'");
3409 specbits |= 1 << (int) i;
3414 error ("two or more data types in declaration of `%s'", name);
3415 /* Actual typedefs come to us as TYPE_DECL nodes. */
3416 else if (TREE_CODE (id) == TYPE_DECL)
3418 if (TREE_TYPE (id) == error_mark_node)
3419 ; /* Allow the type to default to int to avoid cascading errors. */
3422 type = TREE_TYPE (id);
3423 decl_attr = DECL_ATTRIBUTES (id);
3427 /* Built-in types come as identifiers. */
3428 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3430 tree t = lookup_name (id);
3431 if (TREE_TYPE (t) == error_mark_node)
3433 else if (!t || TREE_CODE (t) != TYPE_DECL)
3434 error ("`%s' fails to be a typedef or built in type",
3435 IDENTIFIER_POINTER (id));
3438 type = TREE_TYPE (t);
3442 else if (TREE_CODE (id) != ERROR_MARK)
3449 typedef_type = type;
3451 size_varies = C_TYPE_VARIABLE_SIZE (type);
3453 /* No type at all: default to `int', and set DEFAULTED_INT
3454 because it was not a user-defined typedef. */
3458 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3459 | (1 << (int) RID_SIGNED)
3460 | (1 << (int) RID_UNSIGNED)
3461 | (1 << (int) RID_COMPLEX))))
3462 /* Don't warn about typedef foo = bar. */
3463 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3464 && ! in_system_header)
3466 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3467 and this is a function, or if -Wimplicit; prefer the former
3468 warning since it is more explicit. */
3469 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3471 warn_about_return_type = 1;
3472 else if (warn_implicit_int || flag_isoc99)
3473 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3478 type = integer_type_node;
3481 /* Now process the modifiers that were specified
3482 and check for invalid combinations. */
3484 /* Long double is a special combination. */
3486 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3487 && TYPE_MAIN_VARIANT (type) == double_type_node)
3489 specbits &= ~(1 << (int) RID_LONG);
3490 type = long_double_type_node;
3493 /* Check all other uses of type modifiers. */
3495 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3496 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3500 if ((specbits & 1 << (int) RID_LONG)
3501 && (specbits & 1 << (int) RID_SHORT))
3502 error ("both long and short specified for `%s'", name);
3503 else if (((specbits & 1 << (int) RID_LONG)
3504 || (specbits & 1 << (int) RID_SHORT))
3506 error ("long or short specified with char for `%s'", name);
3507 else if (((specbits & 1 << (int) RID_LONG)
3508 || (specbits & 1 << (int) RID_SHORT))
3509 && TREE_CODE (type) == REAL_TYPE)
3511 static int already = 0;
3513 error ("long or short specified with floating type for `%s'", name);
3514 if (! already && ! pedantic)
3516 error ("the only valid combination is `long double'");
3520 else if ((specbits & 1 << (int) RID_SIGNED)
3521 && (specbits & 1 << (int) RID_UNSIGNED))
3522 error ("both signed and unsigned specified for `%s'", name);
3523 else if (TREE_CODE (type) != INTEGER_TYPE)
3524 error ("long, short, signed or unsigned invalid for `%s'", name);
3528 if (!explicit_int && !defaulted_int && !explicit_char)
3530 error ("long, short, signed or unsigned used invalidly for `%s'",
3536 /* Discard the type modifiers if they are invalid. */
3539 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3540 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3545 if ((specbits & (1 << (int) RID_COMPLEX))
3546 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3548 error ("complex invalid for `%s'", name);
3549 specbits &= ~(1 << (int) RID_COMPLEX);
3552 /* Decide whether an integer type is signed or not.
3553 Optionally treat bitfields as signed by default. */
3554 if (specbits & 1 << (int) RID_UNSIGNED
3555 || (bitfield && ! flag_signed_bitfields
3556 && (explicit_int || defaulted_int || explicit_char
3557 /* A typedef for plain `int' without `signed'
3558 can be controlled just like plain `int'. */
3559 || ! (typedef_decl != 0
3560 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3561 && TREE_CODE (type) != ENUMERAL_TYPE
3562 && !(specbits & 1 << (int) RID_SIGNED)))
3565 type = long_long_unsigned_type_node;
3566 else if (specbits & 1 << (int) RID_LONG)
3567 type = long_unsigned_type_node;
3568 else if (specbits & 1 << (int) RID_SHORT)
3569 type = short_unsigned_type_node;
3570 else if (type == char_type_node)
3571 type = unsigned_char_type_node;
3572 else if (typedef_decl)
3573 type = c_common_unsigned_type (type);
3575 type = unsigned_type_node;
3577 else if ((specbits & 1 << (int) RID_SIGNED)
3578 && type == char_type_node)
3579 type = signed_char_type_node;
3581 type = long_long_integer_type_node;
3582 else if (specbits & 1 << (int) RID_LONG)
3583 type = long_integer_type_node;
3584 else if (specbits & 1 << (int) RID_SHORT)
3585 type = short_integer_type_node;
3587 if (specbits & 1 << (int) RID_COMPLEX)
3589 if (pedantic && !flag_isoc99)
3590 pedwarn ("ISO C90 does not support complex types");
3591 /* If we just have "complex", it is equivalent to
3592 "complex double", but if any modifiers at all are specified it is
3593 the complex form of TYPE. E.g, "complex short" is
3594 "complex short int". */
3596 if (defaulted_int && ! longlong
3597 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3598 | (1 << (int) RID_SIGNED)
3599 | (1 << (int) RID_UNSIGNED))))
3602 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3603 type = complex_double_type_node;
3605 else if (type == integer_type_node)
3608 pedwarn ("ISO C does not support complex integer types");
3609 type = complex_integer_type_node;
3611 else if (type == float_type_node)
3612 type = complex_float_type_node;
3613 else if (type == double_type_node)
3614 type = complex_double_type_node;
3615 else if (type == long_double_type_node)
3616 type = complex_long_double_type_node;
3620 pedwarn ("ISO C does not support complex integer types");
3621 type = build_complex_type (type);
3625 /* Figure out the type qualifiers for the declaration. There are
3626 two ways a declaration can become qualified. One is something
3627 like `const int i' where the `const' is explicit. Another is
3628 something like `typedef const int CI; CI i' where the type of the
3629 declaration contains the `const'. */
3630 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3631 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3632 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3633 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3634 if (constp > 1 && ! flag_isoc99)
3635 pedwarn ("duplicate `const'");
3636 if (restrictp > 1 && ! flag_isoc99)
3637 pedwarn ("duplicate `restrict'");
3638 if (volatilep > 1 && ! flag_isoc99)
3639 pedwarn ("duplicate `volatile'");
3640 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3641 type = TYPE_MAIN_VARIANT (type);
3642 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3643 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3644 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3646 /* Warn if two storage classes are given. Default to `auto'. */
3651 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3652 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3653 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3654 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3655 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3657 /* "static __thread" and "extern __thread" are allowed. */
3658 if ((specbits & (1 << (int) RID_THREAD
3659 | 1 << (int) RID_STATIC
3660 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3663 /* Warn about storage classes that are invalid for certain
3664 kinds of declarations (parameters, typenames, etc.). */
3667 error ("multiple storage classes in declaration of `%s'", name);
3668 else if (funcdef_flag
3670 & ((1 << (int) RID_REGISTER)
3671 | (1 << (int) RID_AUTO)
3672 | (1 << (int) RID_TYPEDEF)
3673 | (1 << (int) RID_THREAD))))
3675 if (specbits & 1 << (int) RID_AUTO
3676 && (pedantic || current_scope == global_scope))
3677 pedwarn ("function definition declared `auto'");
3678 if (specbits & 1 << (int) RID_REGISTER)
3679 error ("function definition declared `register'");
3680 if (specbits & 1 << (int) RID_TYPEDEF)
3681 error ("function definition declared `typedef'");
3682 if (specbits & 1 << (int) RID_THREAD)
3683 error ("function definition declared `__thread'");
3684 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3685 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3687 else if (decl_context != NORMAL && nclasses > 0)
3689 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3693 switch (decl_context)
3696 error ("storage class specified for structure field `%s'",
3700 error ("storage class specified for parameter `%s'", name);
3703 error ("storage class specified for typename");
3706 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3707 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3708 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3711 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3713 /* `extern' with initialization is invalid if not at file scope. */
3714 if (current_scope == global_scope)
3715 warning ("`%s' initialized and declared `extern'", name);
3717 error ("`%s' has both `extern' and initializer", name);
3719 else if (current_scope == global_scope)
3721 if (specbits & 1 << (int) RID_AUTO)
3722 error ("file-scope declaration of `%s' specifies `auto'", name);
3726 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3727 error ("nested function `%s' declared `extern'", name);
3728 else if ((specbits & (1 << (int) RID_THREAD
3729 | 1 << (int) RID_EXTERN
3730 | 1 << (int) RID_STATIC))
3731 == (1 << (int) RID_THREAD))
3733 error ("function-scope `%s' implicitly auto and declared `__thread'",
3735 specbits &= ~(1 << (int) RID_THREAD);
3740 /* Now figure out the structure of the declarator proper.
3741 Descend through it, creating more complex types, until we reach
3742 the declared identifier (or NULL_TREE, in an absolute declarator). */
3744 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3746 if (type == error_mark_node)
3748 declarator = TREE_OPERAND (declarator, 0);
3752 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3753 an INDIRECT_REF (for *...),
3754 a CALL_EXPR (for ...(...)),
3755 a TREE_LIST (for nested attributes),
3756 an identifier (for the name being declared)
3757 or a null pointer (for the place in an absolute declarator
3758 where the name was omitted).
3759 For the last two cases, we have just exited the loop.
3761 At this point, TYPE is the type of elements of an array,
3762 or for a function to return, or for a pointer to point to.
3763 After this sequence of ifs, TYPE is the type of the
3764 array or function or pointer, and DECLARATOR has had its
3765 outermost layer removed. */
3767 if (array_ptr_quals != NULL_TREE || array_parm_static)
3769 /* Only the innermost declarator (making a parameter be of
3770 array type which is converted to pointer type)
3771 may have static or type qualifiers. */
3772 error ("static or type qualifiers in non-parameter array declarator");
3773 array_ptr_quals = NULL_TREE;
3774 array_parm_static = 0;
3777 if (TREE_CODE (declarator) == TREE_LIST)
3779 /* We encode a declarator with embedded attributes using
3781 tree attrs = TREE_PURPOSE (declarator);
3784 declarator = TREE_VALUE (declarator);
3785 inner_decl = declarator;
3786 while (inner_decl != NULL_TREE
3787 && TREE_CODE (inner_decl) == TREE_LIST)
3788 inner_decl = TREE_VALUE (inner_decl);
3789 if (inner_decl == NULL_TREE
3790 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3791 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3792 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3793 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3794 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3795 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3796 returned_attrs = decl_attributes (&type,
3797 chainon (returned_attrs, attrs),
3800 else if (TREE_CODE (declarator) == ARRAY_REF)
3802 tree itype = NULL_TREE;
3803 tree size = TREE_OPERAND (declarator, 1);
3804 /* The index is a signed object `sizetype' bits wide. */
3805 tree index_type = c_common_signed_type (sizetype);
3807 array_ptr_quals = TREE_TYPE (declarator);
3808 array_parm_static = TREE_STATIC (declarator);
3810 declarator = TREE_OPERAND (declarator, 0);
3812 /* Check for some types that there cannot be arrays of. */
3814 if (VOID_TYPE_P (type))
3816 error ("declaration of `%s' as array of voids", name);
3817 type = error_mark_node;
3820 if (TREE_CODE (type) == FUNCTION_TYPE)
3822 error ("declaration of `%s' as array of functions", name);
3823 type = error_mark_node;
3826 if (pedantic && flexible_array_type_p (type))
3827 pedwarn ("invalid use of structure with flexible array member");
3829 if (size == error_mark_node)
3830 type = error_mark_node;
3832 if (type == error_mark_node)
3835 /* If size was specified, set ITYPE to a range-type for that size.
3836 Otherwise, ITYPE remains null. finish_decl may figure it out
3837 from an initial value. */
3841 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3842 STRIP_TYPE_NOPS (size);
3844 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3846 error ("size of array `%s' has non-integer type", name);
3847 size = integer_one_node;
3850 if (pedantic && integer_zerop (size))
3851 pedwarn ("ISO C forbids zero-size array `%s'", name);
3853 if (TREE_CODE (size) == INTEGER_CST)
3855 constant_expression_warning (size);
3856 if (tree_int_cst_sgn (size) < 0)
3858 error ("size of array `%s' is negative", name);
3859 size = integer_one_node;
3864 /* Make sure the array size remains visibly nonconstant
3865 even if it is (eg) a const variable with known value. */
3868 if (!flag_isoc99 && pedantic)
3870 if (TREE_CONSTANT (size))
3871 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3874 pedwarn ("ISO C90 forbids variable-size array `%s'",
3879 if (integer_zerop (size))
3881 /* A zero-length array cannot be represented with an
3882 unsigned index type, which is what we'll get with
3883 build_index_type. Create an open-ended range instead. */
3884 itype = build_range_type (sizetype, size, NULL_TREE);
3888 /* Compute the maximum valid index, that is, size - 1.
3889 Do the calculation in index_type, so that if it is
3890 a variable the computations will be done in the
3892 itype = fold (build (MINUS_EXPR, index_type,
3893 convert (index_type, size),
3894 convert (index_type, size_one_node)));
3896 /* If that overflowed, the array is too big.
3897 ??? While a size of INT_MAX+1 technically shouldn't
3898 cause an overflow (because we subtract 1), the overflow
3899 is recorded during the conversion to index_type, before
3900 the subtraction. Handling this case seems like an
3901 unnecessary complication. */
3902 if (TREE_OVERFLOW (itype))
3904 error ("size of array `%s' is too large", name);
3905 type = error_mark_node;
3911 /* We must be able to distinguish the
3912 SAVE_EXPR_CONTEXT for the variably-sized type
3913 so that we can set it correctly in
3914 set_save_expr_context. The convention is
3915 that all SAVE_EXPRs that need to be reset
3916 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3917 tree cfd = current_function_decl;
3918 if (decl_context == PARM)
3919 current_function_decl = NULL_TREE;
3920 itype = variable_size (itype);
3921 if (decl_context == PARM)
3922 current_function_decl = cfd;
3924 itype = build_index_type (itype);
3927 else if (decl_context == FIELD)
3929 if (pedantic && !flag_isoc99 && !in_system_header)
3930 pedwarn ("ISO C90 does not support flexible array members");
3932 /* ISO C99 Flexible array members are effectively identical
3933 to GCC's zero-length array extension. */
3934 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3937 /* If pedantic, complain about arrays of incomplete types. */
3939 if (pedantic && !COMPLETE_TYPE_P (type))
3940 pedwarn ("array type has incomplete element type");
3942 /* Build the array type itself, then merge any constancy or
3943 volatility into the target type. We must do it in this order
3944 to ensure that the TYPE_MAIN_VARIANT field of the array type
3945 is set correctly. */
3947 type = build_array_type (type, itype);
3949 type = c_build_qualified_type (type, type_quals);
3952 C_TYPE_VARIABLE_SIZE (type) = 1;
3954 /* The GCC extension for zero-length arrays differs from
3955 ISO flexible array members in that sizeof yields zero. */
3956 if (size && integer_zerop (size))
3959 TYPE_SIZE (type) = bitsize_zero_node;
3960 TYPE_SIZE_UNIT (type) = size_zero_node;
3962 if (decl_context != PARM
3963 && (array_ptr_quals != NULL_TREE || array_parm_static))
3965 error ("static or type qualifiers in non-parameter array declarator");
3966 array_ptr_quals = NULL_TREE;
3967 array_parm_static = 0;
3970 else if (TREE_CODE (declarator) == CALL_EXPR)
3974 /* Declaring a function type.
3975 Make sure we have a valid type for the function to return. */
3976 if (type == error_mark_node)
3981 /* Warn about some types functions can't return. */
3983 if (TREE_CODE (type) == FUNCTION_TYPE)
3985 error ("`%s' declared as function returning a function", name);
3986 type = integer_type_node;
3988 if (TREE_CODE (type) == ARRAY_TYPE)
3990 error ("`%s' declared as function returning an array", name);
3991 type = integer_type_node;
3994 /* Construct the function type and go to the next
3995 inner layer of declarator. */
3997 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3999 /* Say it's a definition
4000 only for the CALL_EXPR
4001 closest to the identifier. */
4002 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4003 /* Type qualifiers before the return type of the function
4004 qualify the return type, not the function type. */
4007 /* Type qualifiers on a function return type are normally
4008 permitted by the standard but have no effect, so give a
4009 warning at -Wextra. Qualifiers on a void return type have
4010 meaning as a GNU extension, and are banned on function
4011 definitions in ISO C. FIXME: strictly we shouldn't
4012 pedwarn for qualified void return types except on function
4013 definitions, but not doing so could lead to the undesirable
4014 state of a "volatile void" function return type not being
4015 warned about, and a use of the function being compiled
4016 with GNU semantics, with no diagnostics under -pedantic. */
4017 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4018 pedwarn ("ISO C forbids qualified void function return type");
4019 else if (extra_warnings
4020 && !(VOID_TYPE_P (type)
4021 && type_quals == TYPE_QUAL_VOLATILE))
4022 warning ("type qualifiers ignored on function return type");
4024 type = c_build_qualified_type (type, type_quals);
4026 type_quals = TYPE_UNQUALIFIED;
4028 type = build_function_type (type, arg_types);
4029 declarator = TREE_OPERAND (declarator, 0);
4031 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4032 the formal parameter list of this FUNCTION_TYPE to point to
4033 the FUNCTION_TYPE node itself. */
4038 for (link = last_function_parm_tags;
4040 link = TREE_CHAIN (link))
4041 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4044 else if (TREE_CODE (declarator) == INDIRECT_REF)
4046 /* Merge any constancy or volatility into the target type
4049 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4051 pedwarn ("ISO C forbids qualified function types");
4053 type = c_build_qualified_type (type, type_quals);
4054 type_quals = TYPE_UNQUALIFIED;
4057 type = build_pointer_type (type);
4059 /* Process a list of type modifier keywords
4060 (such as const or volatile) that were given inside the `*'. */
4062 if (TREE_TYPE (declarator))
4070 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4071 typemodlist = TREE_CHAIN (typemodlist))
4073 tree qualifier = TREE_VALUE (typemodlist);
4075 if (C_IS_RESERVED_WORD (qualifier))
4077 if (C_RID_CODE (qualifier) == RID_CONST)
4079 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4081 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4091 error ("invalid type modifier within pointer declarator");
4092 if (constp > 1 && ! flag_isoc99)
4093 pedwarn ("duplicate `const'");
4094 if (volatilep > 1 && ! flag_isoc99)
4095 pedwarn ("duplicate `volatile'");
4096 if (restrictp > 1 && ! flag_isoc99)
4097 pedwarn ("duplicate `restrict'");
4099 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4100 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4101 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4104 declarator = TREE_OPERAND (declarator, 0);
4111 /* Now TYPE has the actual type. */
4113 /* Did array size calculations overflow? */
4115 if (TREE_CODE (type) == ARRAY_TYPE
4116 && COMPLETE_TYPE_P (type)
4117 && TREE_OVERFLOW (TYPE_SIZE (type)))
4119 error ("size of array `%s' is too large", name);
4120 /* If we proceed with the array type as it is, we'll eventually
4121 crash in tree_low_cst(). */
4122 type = error_mark_node;
4125 /* If this is declaring a typedef name, return a TYPE_DECL. */
4127 if (specbits & (1 << (int) RID_TYPEDEF))
4130 /* Note that the grammar rejects storage classes
4131 in typenames, fields or parameters */
4132 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4134 pedwarn ("ISO C forbids qualified function types");
4136 type = c_build_qualified_type (type, type_quals);
4137 decl = build_decl (TYPE_DECL, declarator, type);
4138 if ((specbits & (1 << (int) RID_SIGNED))
4139 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4140 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4141 decl_attributes (&decl, returned_attrs, 0);
4145 /* Detect the case of an array type of unspecified size
4146 which came, as such, direct from a typedef name.
4147 We must copy the type, so that each identifier gets
4148 a distinct type, so that each identifier's size can be
4149 controlled separately by its own initializer. */
4151 if (type != 0 && typedef_type != 0
4152 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4153 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4155 type = build_array_type (TREE_TYPE (type), 0);
4157 C_TYPE_VARIABLE_SIZE (type) = 1;
4160 /* If this is a type name (such as, in a cast or sizeof),
4161 compute the type and return it now. */
4163 if (decl_context == TYPENAME)
4165 /* Note that the grammar rejects storage classes
4166 in typenames, fields or parameters */
4167 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4169 pedwarn ("ISO C forbids const or volatile function types");
4171 type = c_build_qualified_type (type, type_quals);
4172 decl_attributes (&type, returned_attrs, 0);
4176 /* Aside from typedefs and type names (handle above),
4177 `void' at top level (not within pointer)
4178 is allowed only in public variables.
4179 We don't complain about parms either, but that is because
4180 a better error message can be made later. */
4182 if (VOID_TYPE_P (type) && decl_context != PARM
4183 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4184 && ((specbits & (1 << (int) RID_EXTERN))
4185 || (current_scope == global_scope
4187 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4189 error ("variable or field `%s' declared void", name);
4190 type = integer_type_node;
4193 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4194 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4199 if (decl_context == PARM)
4201 tree type_as_written;
4204 /* A parameter declared as an array of T is really a pointer to T.
4205 One declared as a function is really a pointer to a function. */
4207 if (TREE_CODE (type) == ARRAY_TYPE)
4209 /* Transfer const-ness of array into that of type pointed to. */
4210 type = TREE_TYPE (type);
4212 type = c_build_qualified_type (type, type_quals);
4213 type = build_pointer_type (type);
4214 type_quals = TYPE_UNQUALIFIED;
4215 if (array_ptr_quals)
4217 tree new_ptr_quals, new_ptr_attrs;
4219 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4220 /* We don't yet implement attributes in this context. */
4221 if (new_ptr_attrs != NULL_TREE)
4222 warning ("attributes in parameter array declarator ignored");
4227 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4229 tree qualifier = TREE_VALUE (new_ptr_quals);
4231 if (C_IS_RESERVED_WORD (qualifier))
4233 if (C_RID_CODE (qualifier) == RID_CONST)
4235 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4237 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4247 error ("invalid type modifier within array declarator");
4249 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4250 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4251 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4255 else if (TREE_CODE (type) == FUNCTION_TYPE)
4257 if (pedantic && type_quals)
4258 pedwarn ("ISO C forbids qualified function types");
4260 type = c_build_qualified_type (type, type_quals);
4261 type = build_pointer_type (type);
4262 type_quals = TYPE_UNQUALIFIED;
4264 else if (type_quals)
4265 type = c_build_qualified_type (type, type_quals);
4267 type_as_written = type;
4269 decl = build_decl (PARM_DECL, declarator, type);
4271 C_DECL_VARIABLE_SIZE (decl) = 1;
4273 /* Compute the type actually passed in the parmlist,
4274 for the case where there is no prototype.
4275 (For example, shorts and chars are passed as ints.)
4276 When there is a prototype, this is overridden later. */
4278 if (type == error_mark_node)
4279 promoted_type = type;
4281 promoted_type = c_type_promotes_to (type);
4283 DECL_ARG_TYPE (decl) = promoted_type;
4284 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4286 else if (decl_context == FIELD)
4288 /* Structure field. It may not be a function. */
4290 if (TREE_CODE (type) == FUNCTION_TYPE)
4292 error ("field `%s' declared as a function", name);
4293 type = build_pointer_type (type);
4295 else if (TREE_CODE (type) != ERROR_MARK
4296 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4298 error ("field `%s' has incomplete type", name);
4299 type = error_mark_node;
4301 /* Move type qualifiers down to element of an array. */
4302 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4303 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4305 TYPE_DOMAIN (type));
4306 decl = build_decl (FIELD_DECL, declarator, type);
4307 DECL_NONADDRESSABLE_P (decl) = bitfield;
4310 C_DECL_VARIABLE_SIZE (decl) = 1;
4312 else if (TREE_CODE (type) == FUNCTION_TYPE)
4314 /* Every function declaration is "external"
4315 except for those which are inside a function body
4316 in which `auto' is used.
4317 That is a case not specified by ANSI C,
4318 and we use it for forward declarations for nested functions. */
4319 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4320 || current_scope == global_scope);
4322 if (specbits & (1 << (int) RID_AUTO)
4323 && (pedantic || current_scope == global_scope))
4324 pedwarn ("invalid storage class for function `%s'", name);
4325 if (specbits & (1 << (int) RID_REGISTER))
4326 error ("invalid storage class for function `%s'", name);
4327 if (specbits & (1 << (int) RID_THREAD))
4328 error ("invalid storage class for function `%s'", name);
4329 /* Function declaration not at file scope.
4330 Storage classes other than `extern' are not allowed
4331 and `extern' makes no difference. */
4332 if (current_scope != global_scope
4333 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4335 pedwarn ("invalid storage class for function `%s'", name);
4337 decl = build_decl (FUNCTION_DECL, declarator, type);
4338 decl = build_decl_attribute_variant (decl, decl_attr);
4340 DECL_LANG_SPECIFIC (decl)
4341 = ggc_alloc_cleared (sizeof (struct lang_decl));
4343 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4344 pedwarn ("ISO C forbids qualified function types");
4346 /* GNU C interprets a `volatile void' return type to indicate
4347 that the function does not return. */
4348 if ((type_quals & TYPE_QUAL_VOLATILE)
4349 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4350 warning ("`noreturn' function returns non-void value");
4353 DECL_EXTERNAL (decl) = 1;
4354 /* Record absence of global scope for `static' or `auto'. */
4356 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4359 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4361 /* Record presence of `inline', if it is reasonable. */
4362 if (MAIN_NAME_P (declarator))
4365 warning ("cannot inline function `main'");
4369 /* Assume that otherwise the function can be inlined. */
4370 DECL_DECLARED_INLINE_P (decl) = 1;
4372 /* Do not mark bare declarations as DECL_INLINE. Doing so
4373 in the presence of multiple declarations can result in
4374 the abstract origin pointing between the declarations,
4375 which will confuse dwarf2out. */
4378 DECL_INLINE (decl) = 1;
4379 if (specbits & (1 << (int) RID_EXTERN))
4380 current_extern_inline = 1;
4383 /* If -finline-functions, assume it can be inlined. This does
4384 two things: let the function be deferred until it is actually
4385 needed, and let dwarf2 know that the function is inlinable. */
4386 else if (flag_inline_trees == 2 && initialized)
4388 if (!DECL_INLINE (decl))
4389 DID_INLINE_FUNC (decl) = 1;
4390 DECL_INLINE (decl) = 1;
4391 DECL_DECLARED_INLINE_P (decl) = 0;
4396 /* It's a variable. */
4397 /* An uninitialized decl with `extern' is a reference. */
4398 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4400 /* Move type qualifiers down to element of an array. */
4401 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4403 int saved_align = TYPE_ALIGN(type);
4404 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4406 TYPE_DOMAIN (type));
4407 TYPE_ALIGN (type) = saved_align;
4409 else if (type_quals)
4410 type = c_build_qualified_type (type, type_quals);
4412 /* It is invalid to create an `extern' declaration for a
4413 variable if there is a global declaration that is
4415 if (extern_ref && current_scope != global_scope)
4419 global_decl = identifier_global_value (declarator);
4421 && TREE_CODE (global_decl) == VAR_DECL
4422 && !TREE_PUBLIC (global_decl))
4423 error ("variable previously declared `static' redeclared "
4427 decl = build_decl (VAR_DECL, declarator, type);
4429 C_DECL_VARIABLE_SIZE (decl) = 1;
4432 pedwarn ("%Hvariable '%D' declared `inline'",
4433 &DECL_SOURCE_LOCATION (decl), decl);
4435 DECL_EXTERNAL (decl) = extern_ref;
4437 /* At file scope, the presence of a `static' or `register' storage
4438 class specifier, or the absence of all storage class specifiers
4439 makes this declaration a definition (perhaps tentative). Also,
4440 the absence of both `static' and `register' makes it public. */
4441 if (current_scope == global_scope)
4443 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4444 | (1 << (int) RID_REGISTER)));
4445 TREE_STATIC (decl) = !extern_ref;
4447 /* Not at file scope, only `static' makes a static definition. */
4450 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4451 TREE_PUBLIC (decl) = extern_ref;
4454 if (specbits & 1 << (int) RID_THREAD)
4456 if (targetm.have_tls)
4457 DECL_THREAD_LOCAL (decl) = 1;
4459 /* A mere warning is sure to result in improper semantics
4460 at runtime. Don't bother to allow this to compile. */
4461 error ("thread-local storage not supported for this target");
4465 /* Record `register' declaration for warnings on &
4466 and in case doing stupid register allocation. */
4468 if (specbits & (1 << (int) RID_REGISTER))
4469 DECL_REGISTER (decl) = 1;
4471 /* Record constancy and volatility. */
4472 c_apply_type_quals_to_decl (type_quals, decl);
4474 /* If a type has volatile components, it should be stored in memory.
4475 Otherwise, the fact that those components are volatile
4476 will be ignored, and would even crash the compiler. */
4477 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4478 c_mark_addressable (decl);
4480 #ifdef ENABLE_CHECKING
4481 /* This is the earliest point at which we might know the assembler
4482 name of a variable. Thus, if it's known before this, die horribly. */
4483 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4487 decl_attributes (&decl, returned_attrs, 0);
4493 /* Decode the parameter-list info for a function type or function definition.
4494 The argument is the value returned by `get_parm_info' (or made in parse.y
4495 if there is an identifier list instead of a parameter decl list).
4496 These two functions are separate because when a function returns
4497 or receives functions then each is called multiple times but the order
4498 of calls is different. The last call to `grokparms' is always the one
4499 that contains the formal parameter names of a function definition.
4501 Store in `last_function_parms' a chain of the decls of parms.
4502 Also store in `last_function_parm_tags' a chain of the struct, union,
4503 and enum tags declared among the parms.
4505 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4507 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4508 a mere declaration. A nonempty identifier-list gets an error message
4509 when FUNCDEF_FLAG is zero. */
4512 grokparms (tree parms_info, int funcdef_flag)
4514 tree first_parm = TREE_CHAIN (parms_info);
4516 last_function_parms = TREE_PURPOSE (parms_info);
4517 last_function_parm_tags = TREE_VALUE (parms_info);
4518 last_function_parm_vars = TREE_TYPE (parms_info);
4520 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4521 && !in_system_header)
4522 warning ("function declaration isn't a prototype");
4525 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4528 pedwarn ("parameter names (without types) in function declaration");
4530 last_function_parms = first_parm;
4537 /* If the arg types are incomplete in a declaration,
4538 they must include undefined tags.
4539 These tags can never be defined in the scope of the declaration,
4540 so the types can never be completed,
4541 and no call can be compiled successfully. */
4543 for (parm = last_function_parms, typelt = first_parm;
4545 parm = TREE_CHAIN (parm))
4546 /* Skip over any enumeration constants declared here. */
4547 if (TREE_CODE (parm) == PARM_DECL)
4549 /* Barf if the parameter itself has an incomplete type. */
4550 tree type = TREE_VALUE (typelt);
4551 if (type == error_mark_node)
4553 if (!COMPLETE_TYPE_P (type))
4555 if (funcdef_flag && DECL_NAME (parm) != 0)
4556 error ("parameter `%s' has incomplete type",
4557 IDENTIFIER_POINTER (DECL_NAME (parm)));
4559 warning ("parameter has incomplete type");
4562 TREE_VALUE (typelt) = error_mark_node;
4563 TREE_TYPE (parm) = error_mark_node;
4566 typelt = TREE_CHAIN (typelt);
4573 /* Return a tree_list node with info on a parameter list just parsed.
4574 The TREE_PURPOSE is a list of decls of those parms.
4575 The TREE_VALUE is a list of structure, union and enum tags defined.
4576 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4577 The TREE_TYPE is a list of non-parameter decls which appeared with the
4579 This tree_list node is later fed to `grokparms'.
4581 VOID_AT_END nonzero means append `void' to the end of the type-list.
4582 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4585 get_parm_info (int void_at_end)
4587 tree decl, type, list;
4589 tree *last_type = &types;
4590 tree tags = current_scope->tags;
4591 tree parms = current_scope->parms;
4592 tree others = current_scope->names;
4593 static bool explained_incomplete_types = false;
4594 bool gave_void_only_once_err = false;
4596 /* Just "void" (and no ellipsis) is special. There are really no parms.
4597 But if the "void" is qualified (by "const" or "volatile"), or has a
4598 storage class specifier ("register"), then the behavior is undefined;
4599 issue an error. Typedefs for "void" are OK (see DR#157). */
4600 if (void_at_end && parms != 0
4601 && TREE_CHAIN (parms) == 0
4602 && VOID_TYPE_P (TREE_TYPE (parms))
4603 && !DECL_NAME (parms))
4605 if (TREE_THIS_VOLATILE (parms)
4606 || TREE_READONLY (parms)
4607 || DECL_REGISTER (parms))
4608 error ("\"void\" as only parameter may not be qualified");
4610 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4614 current_scope->parms = parms = nreverse (parms);
4616 /* Sanity check all of the parameter declarations. */
4617 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4619 if (TREE_CODE (decl) != PARM_DECL)
4621 if (TREE_ASM_WRITTEN (decl))
4624 /* Since there is a prototype, args are passed in their
4625 declared types. The back end may override this. */
4626 type = TREE_TYPE (decl);
4627 DECL_ARG_TYPE (decl) = type;
4628 if (PROMOTE_PROTOTYPES
4629 && INTEGRAL_TYPE_P (type)
4630 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4631 DECL_ARG_TYPE (decl) = integer_type_node;
4633 /* Check for (..., void, ...) and issue an error. */
4634 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4636 error ("\"void\" must be the only parameter");
4637 gave_void_only_once_err = true;
4640 type = build_tree_list (0, type);
4642 last_type = &TREE_CHAIN (type);
4645 /* Check the list of non-parameter decls for any forward parm decls
4646 that never got real decls. */
4647 for (decl = others; decl; decl = TREE_CHAIN (decl))
4648 if (TREE_CODE (decl) == PARM_DECL)
4650 if (!TREE_ASM_WRITTEN (decl))
4653 error ("%Hparameter \"%D\" has just a forward declaration",
4654 &DECL_SOURCE_LOCATION (decl), decl);
4657 /* Warn about any struct, union or enum tags defined within this
4658 list. The scope of such types is limited to this declaration,
4659 which is rarely if ever desirable (it's impossible to call such
4660 a function with type-correct arguments). */
4661 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4663 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4664 const char *keyword;
4665 /* An anonymous union parm type is meaningful as a GNU extension.
4666 So don't warn for that. */
4667 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4670 /* The keyword should not be translated. */
4673 case RECORD_TYPE: keyword = "struct"; break;
4674 case UNION_TYPE: keyword = "union"; break;
4675 case ENUMERAL_TYPE: keyword = "enum"; break;
4679 if (TREE_PURPOSE (decl))
4680 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4681 warning ("\"%s %s\" declared inside parameter list",
4682 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4684 /* The %s will be one of 'struct', 'union', or 'enum'. */
4685 warning ("anonymous %s declared inside parameter list", keyword);
4687 if (! explained_incomplete_types)
4689 warning ("its scope is only this definition or declaration,"
4690 " which is probably not what you want");
4691 explained_incomplete_types = true;
4698 type = build_tree_list (0, void_type_node);
4702 list = tree_cons (parms, tags, types);
4703 TREE_TYPE (list) = others;
4707 /* Get the struct, enum or union (CODE says which) with tag NAME.
4708 Define the tag as a forward-reference if it is not defined. */
4711 xref_tag (enum tree_code code, tree name)
4713 /* If a cross reference is requested, look up the type
4714 already defined for this tag and return it. */
4716 tree ref = lookup_tag (code, name, 0);
4717 /* If this is the right type of tag, return what we found.
4718 (This reference will be shadowed by shadow_tag later if appropriate.)
4719 If this is the wrong type of tag, do not return it. If it was the
4720 wrong type in the same scope, we will have had an error
4721 message already; if in a different scope and declaring
4722 a name, pending_xref_error will give an error message; but if in a
4723 different scope and not declaring a name, this tag should
4724 shadow the previous declaration of a different type of tag, and
4725 this would not work properly if we return the reference found.
4726 (For example, with "struct foo" in an outer scope, "union foo;"
4727 must shadow that tag with a new one of union type.) */
4728 if (ref && TREE_CODE (ref) == code)
4731 /* If no such tag is yet defined, create a forward-reference node
4732 and record it as the "definition".
4733 When a real declaration of this type is found,
4734 the forward-reference will be altered into a real type. */
4736 ref = make_node (code);
4737 if (code == ENUMERAL_TYPE)
4739 /* Give the type a default layout like unsigned int
4740 to avoid crashing if it does not get defined. */
4741 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4742 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4743 TYPE_USER_ALIGN (ref) = 0;
4744 TREE_UNSIGNED (ref) = 1;
4745 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4746 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4747 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4750 pushtag (name, ref);
4755 /* Make sure that the tag NAME is defined *in the current scope*
4756 at least as a forward reference.
4757 CODE says which kind of tag NAME ought to be. */
4760 start_struct (enum tree_code code, tree name)
4762 /* If there is already a tag defined at this scope
4763 (as a forward reference), just return it. */
4768 ref = lookup_tag (code, name, 1);
4769 if (ref && TREE_CODE (ref) == code)
4771 if (TYPE_FIELDS (ref))
4773 if (code == UNION_TYPE)
4774 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4776 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4781 /* Otherwise create a forward-reference just so the tag is in scope. */
4783 ref = make_node (code);
4784 pushtag (name, ref);
4787 C_TYPE_BEING_DEFINED (ref) = 1;
4788 TYPE_PACKED (ref) = flag_pack_struct;
4792 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4793 of a structure component, returning a FIELD_DECL node.
4794 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4796 This is done during the parsing of the struct declaration.
4797 The FIELD_DECL nodes are chained together and the lot of them
4798 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4801 grokfield (tree declarator, tree declspecs, tree width)
4805 if (declarator == NULL_TREE && width == NULL_TREE)
4807 /* This is an unnamed decl.
4809 If we have something of the form "union { list } ;" then this
4810 is the anonymous union extension. Similarly for struct.
4812 If this is something of the form "struct foo;", then
4813 If MS extensions are enabled, this is handled as an
4815 Otherwise this is a forward declaration of a structure tag.
4817 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4818 If MS extensions are enabled and foo names a structure, then
4819 again this is an anonymous struct.
4820 Otherwise this is an error.
4822 Oh what a horrid tangled web we weave. I wonder if MS consciously
4823 took this from Plan 9 or if it was an accident of implementation
4824 that took root before someone noticed the bug... */
4826 tree type = TREE_VALUE (declspecs);
4828 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4829 type = TREE_TYPE (type);
4830 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4832 if (flag_ms_extensions)
4835 goto warn_unnamed_field;
4836 else if (TYPE_NAME (type) == NULL)
4839 goto warn_unnamed_field;
4844 warning ("declaration does not declare anything");
4849 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4851 finish_decl (value, NULL_TREE, NULL_TREE);
4852 DECL_INITIAL (value) = width;
4854 if (c_dialect_objc ())
4855 objc_check_decl (value);
4859 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4860 the list such that this does not present a problem later. */
4863 detect_field_duplicates (tree fieldlist)
4868 /* First, see if there are more than "a few" fields.
4869 This is trivially true if there are zero or one fields. */
4872 x = TREE_CHAIN (fieldlist);
4878 } while (timeout > 0 && x);
4880 /* If there were "few" fields, avoid the overhead of allocating
4881 a hash table. Instead just do the nested traversal thing. */
4884 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4887 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4888 if (DECL_NAME (y) == DECL_NAME (x))
4890 error ("%Hduplicate member '%D'",
4891 &DECL_SOURCE_LOCATION (x), x);
4892 DECL_NAME (x) = NULL_TREE;
4898 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4901 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4902 if ((y = DECL_NAME (x)) != 0)
4904 slot = htab_find_slot (htab, y, INSERT);
4907 error ("%Hduplicate member '%D'",
4908 &DECL_SOURCE_LOCATION (x), x);
4909 DECL_NAME (x) = NULL_TREE;
4918 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4919 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4920 ATTRIBUTES are attributes to be applied to the structure. */
4923 finish_struct (tree t, tree fieldlist, tree attributes)
4926 int toplevel = global_scope == current_scope;
4927 int saw_named_field;
4929 /* If this type was previously laid out as a forward reference,
4930 make sure we lay it out again. */
4934 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4936 /* Nameless union parm types are useful as GCC extension. */
4937 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4938 /* Otherwise, warn about any struct or union def. in parmlist. */
4939 if (in_parm_level_p ())
4942 pedwarn ("%s defined inside parms",
4943 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4945 warning ("%s defined inside parms",
4946 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4951 for (x = fieldlist; x; x = TREE_CHAIN (x))
4952 if (DECL_NAME (x) != 0)
4956 pedwarn ("%s has no %s",
4957 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4958 fieldlist ? _("named members") : _("members"));
4961 /* Install struct as DECL_CONTEXT of each field decl.
4962 Also process specified field sizes,m which is found in the DECL_INITIAL.
4963 Store 0 there, except for ": 0" fields (so we can find them
4964 and delete them, below). */
4966 saw_named_field = 0;
4967 for (x = fieldlist; x; x = TREE_CHAIN (x))
4969 DECL_CONTEXT (x) = t;
4970 DECL_PACKED (x) |= TYPE_PACKED (t);
4972 /* If any field is const, the structure type is pseudo-const. */
4973 if (TREE_READONLY (x))
4974 C_TYPE_FIELDS_READONLY (t) = 1;
4977 /* A field that is pseudo-const makes the structure likewise. */
4978 tree t1 = TREE_TYPE (x);
4979 while (TREE_CODE (t1) == ARRAY_TYPE)
4980 t1 = TREE_TYPE (t1);
4981 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4982 && C_TYPE_FIELDS_READONLY (t1))
4983 C_TYPE_FIELDS_READONLY (t) = 1;
4986 /* Any field that is volatile means variables of this type must be
4987 treated in some ways as volatile. */
4988 if (TREE_THIS_VOLATILE (x))
4989 C_TYPE_FIELDS_VOLATILE (t) = 1;
4991 /* Any field of nominal variable size implies structure is too. */
4992 if (C_DECL_VARIABLE_SIZE (x))
4993 C_TYPE_VARIABLE_SIZE (t) = 1;
4995 /* Detect invalid nested redefinition. */
4996 if (TREE_TYPE (x) == t)
4997 error ("nested redefinition of `%s'",
4998 IDENTIFIER_POINTER (TYPE_NAME (t)));
5000 /* Detect invalid bit-field size. */
5001 if (DECL_INITIAL (x))
5002 STRIP_NOPS (DECL_INITIAL (x));
5003 if (DECL_INITIAL (x))
5005 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
5006 constant_expression_warning (DECL_INITIAL (x));
5009 error ("%Hbit-field '%D' width not an integer constant",
5010 &DECL_SOURCE_LOCATION (x), x);
5011 DECL_INITIAL (x) = NULL;
5015 /* Detect invalid bit-field type. */
5016 if (DECL_INITIAL (x)
5017 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
5018 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
5019 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
5021 error ("%Hbit-field '%D' has invalid type",
5022 &DECL_SOURCE_LOCATION (x), x);
5023 DECL_INITIAL (x) = NULL;
5026 if (DECL_INITIAL (x) && pedantic
5027 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
5028 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
5029 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
5030 /* Accept an enum that's equivalent to int or unsigned int. */
5031 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5032 && (TYPE_PRECISION (TREE_TYPE (x))
5033 == TYPE_PRECISION (integer_type_node))))
5034 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
5035 &DECL_SOURCE_LOCATION (x), x);
5037 /* Detect and ignore out of range field width and process valid
5039 if (DECL_INITIAL (x))
5042 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
5043 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5045 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5046 error ("%Hnegative width in bit-field '%D'",
5047 &DECL_SOURCE_LOCATION (x), x);
5048 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5049 pedwarn ("%Hwidth of '%D' exceeds its type",
5050 &DECL_SOURCE_LOCATION (x), x);
5051 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5052 error ("%Hzero width for bit-field '%D'",
5053 &DECL_SOURCE_LOCATION (x), x);
5056 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5057 unsigned HOST_WIDE_INT width
5058 = tree_low_cst (DECL_INITIAL (x), 1);
5060 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5061 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5062 TREE_UNSIGNED (TREE_TYPE (x)))
5064 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5065 TREE_UNSIGNED (TREE_TYPE (x))))))
5066 warning ("%H'%D' is narrower than values of its type",
5067 &DECL_SOURCE_LOCATION (x), x);
5069 DECL_SIZE (x) = bitsize_int (width);
5070 DECL_BIT_FIELD (x) = 1;
5071 SET_DECL_C_BIT_FIELD (x);
5075 DECL_INITIAL (x) = 0;
5077 /* Detect flexible array member in an invalid context. */
5078 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5079 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5080 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5081 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5083 if (TREE_CODE (t) == UNION_TYPE)
5084 error ("%Hflexible array member in union",
5085 &DECL_SOURCE_LOCATION (x));
5086 else if (TREE_CHAIN (x) != NULL_TREE)
5087 error ("%Hflexible array member not at end of struct",
5088 &DECL_SOURCE_LOCATION (x));
5089 else if (! saw_named_field)
5090 error ("%Hflexible array member in otherwise empty struct",
5091 &DECL_SOURCE_LOCATION (x));
5094 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5095 && flexible_array_type_p (TREE_TYPE (x)))
5096 pedwarn ("%Hinvalid use of structure with flexible array member",
5097 &DECL_SOURCE_LOCATION (x));
5100 saw_named_field = 1;
5103 detect_field_duplicates (fieldlist);
5105 /* Now we have the nearly final fieldlist. Record it,
5106 then lay out the structure or union (including the fields). */
5108 TYPE_FIELDS (t) = fieldlist;
5112 /* Delete all zero-width bit-fields from the fieldlist */
5114 tree *fieldlistp = &fieldlist;
5116 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5117 *fieldlistp = TREE_CHAIN (*fieldlistp);
5119 fieldlistp = &TREE_CHAIN (*fieldlistp);
5122 /* Now we have the truly final field list.
5123 Store it in this type and in the variants. */
5125 TYPE_FIELDS (t) = fieldlist;
5127 /* If there are lots of fields, sort so we can look through them fast.
5128 We arbitrarily consider 16 or more elts to be "a lot". */
5133 for (x = fieldlist; x; x = TREE_CHAIN (x))
5135 if (len > 15 || DECL_NAME (x) == NULL)
5143 struct lang_type *space;
5144 struct sorted_fields_type *space2;
5146 len += list_length (x);
5148 /* Use the same allocation policy here that make_node uses, to
5149 ensure that this lives as long as the rest of the struct decl.
5150 All decls in an inline function need to be saved. */
5152 space = ggc_alloc (sizeof (struct lang_type));
5153 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5157 field_array = &space2->elts[0];
5158 for (x = fieldlist; x; x = TREE_CHAIN (x))
5160 field_array[len++] = x;
5162 /* If there is anonymous struct or union, break out of the loop. */
5163 if (DECL_NAME (x) == NULL)
5166 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5169 TYPE_LANG_SPECIFIC (t) = space;
5170 TYPE_LANG_SPECIFIC (t)->s->len = len;
5171 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5172 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5177 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5179 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5180 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5181 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5182 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5185 /* If this was supposed to be a transparent union, but we can't
5186 make it one, warn and turn off the flag. */
5187 if (TREE_CODE (t) == UNION_TYPE
5188 && TYPE_TRANSPARENT_UNION (t)
5189 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5191 TYPE_TRANSPARENT_UNION (t) = 0;
5192 warning ("union cannot be made transparent");
5195 /* If this structure or union completes the type of any previous
5196 variable declaration, lay it out and output its rtl. */
5198 if (current_scope->incomplete_list != NULL_TREE)
5200 tree prev = NULL_TREE;
5202 for (x = current_scope->incomplete_list; x; x = TREE_CHAIN (x))
5204 tree decl = TREE_VALUE (x);
5206 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5207 && TREE_CODE (decl) != TYPE_DECL)
5209 layout_decl (decl, 0);
5210 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5211 if (c_dialect_objc ())
5212 objc_check_decl (decl);
5213 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5216 /* Unlink X from the incomplete list. */
5218 TREE_CHAIN (prev) = TREE_CHAIN (x);
5220 current_scope->incomplete_list = TREE_CHAIN (x);
5222 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5223 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5225 tree element = TREE_TYPE (decl);
5226 while (TREE_CODE (element) == ARRAY_TYPE)
5227 element = TREE_TYPE (element);
5230 layout_array_type (TREE_TYPE (decl));
5231 if (TREE_CODE (decl) != TYPE_DECL)
5233 layout_decl (decl, 0);
5234 if (c_dialect_objc ())
5235 objc_check_decl (decl);
5236 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5240 /* Unlink X from the incomplete list. */
5242 TREE_CHAIN (prev) = TREE_CHAIN (x);
5244 current_scope->incomplete_list = TREE_CHAIN (x);
5250 /* Finish debugging output for this type. */
5251 rest_of_type_compilation (t, toplevel);
5256 /* Lay out the type T, and its element type, and so on. */
5259 layout_array_type (tree t)
5261 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5262 layout_array_type (TREE_TYPE (t));
5266 /* Begin compiling the definition of an enumeration type.
5267 NAME is its name (or null if anonymous).
5268 Returns the type object, as yet incomplete.
5269 Also records info about it so that build_enumerator
5270 may be used to declare the individual values as they are read. */
5273 start_enum (tree name)
5277 /* If this is the real definition for a previous forward reference,
5278 fill in the contents in the same object that used to be the
5279 forward reference. */
5282 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5284 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5286 enumtype = make_node (ENUMERAL_TYPE);
5287 pushtag (name, enumtype);
5290 C_TYPE_BEING_DEFINED (enumtype) = 1;
5292 if (TYPE_VALUES (enumtype) != 0)
5294 /* This enum is a named one that has been declared already. */
5295 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5297 /* Completely replace its old definition.
5298 The old enumerators remain defined, however. */
5299 TYPE_VALUES (enumtype) = 0;
5302 enum_next_value = integer_zero_node;
5305 if (flag_short_enums)
5306 TYPE_PACKED (enumtype) = 1;
5311 /* After processing and defining all the values of an enumeration type,
5312 install their decls in the enumeration type and finish it off.
5313 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5314 and ATTRIBUTES are the specified attributes.
5315 Returns ENUMTYPE. */
5318 finish_enum (tree enumtype, tree values, tree attributes)
5321 tree minnode = 0, maxnode = 0, enum_value_type;
5322 int precision, unsign;
5323 int toplevel = (global_scope == current_scope);
5325 if (in_parm_level_p ())
5326 warning ("enum defined inside parms");
5328 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5330 /* Calculate the maximum value of any enumerator in this type. */
5332 if (values == error_mark_node)
5333 minnode = maxnode = integer_zero_node;
5336 minnode = maxnode = TREE_VALUE (values);
5337 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5339 tree value = TREE_VALUE (pair);
5340 if (tree_int_cst_lt (maxnode, value))
5342 if (tree_int_cst_lt (value, minnode))
5347 /* Construct the final type of this enumeration. It is the same
5348 as one of the integral types - the narrowest one that fits, except
5349 that normally we only go as narrow as int - and signed iff any of
5350 the values are negative. */
5351 unsign = (tree_int_cst_sgn (minnode) >= 0);
5352 precision = MAX (min_precision (minnode, unsign),
5353 min_precision (maxnode, unsign));
5354 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5356 tree narrowest = c_common_type_for_size (precision, unsign);
5359 warning ("enumeration values exceed range of largest integer");
5360 narrowest = long_long_integer_type_node;
5363 precision = TYPE_PRECISION (narrowest);
5366 precision = TYPE_PRECISION (integer_type_node);
5368 if (precision == TYPE_PRECISION (integer_type_node))
5369 enum_value_type = c_common_type_for_size (precision, 0);
5371 enum_value_type = enumtype;
5373 TYPE_MIN_VALUE (enumtype) = minnode;
5374 TYPE_MAX_VALUE (enumtype) = maxnode;
5375 TYPE_PRECISION (enumtype) = precision;
5376 TREE_UNSIGNED (enumtype) = unsign;
5377 TYPE_SIZE (enumtype) = 0;
5378 layout_type (enumtype);
5380 if (values != error_mark_node)
5382 /* Change the type of the enumerators to be the enum type. We
5383 need to do this irrespective of the size of the enum, for
5384 proper type checking. Replace the DECL_INITIALs of the
5385 enumerators, and the value slots of the list, with copies
5386 that have the enum type; they cannot be modified in place
5387 because they may be shared (e.g. integer_zero_node) Finally,
5388 change the purpose slots to point to the names of the decls. */
5389 for (pair = values; pair; pair = TREE_CHAIN (pair))
5391 tree enu = TREE_PURPOSE (pair);
5393 TREE_TYPE (enu) = enumtype;
5395 /* The ISO C Standard mandates enumerators to have type int,
5396 even though the underlying type of an enum type is
5397 unspecified. Here we convert any enumerators that fit in
5398 an int to type int, to avoid promotions to unsigned types
5399 when comparing integers with enumerators that fit in the
5400 int range. When -pedantic is given, build_enumerator()
5401 would have already taken care of those that don't fit. */
5402 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5403 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5405 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5407 TREE_PURPOSE (pair) = DECL_NAME (enu);
5408 TREE_VALUE (pair) = DECL_INITIAL (enu);
5411 TYPE_VALUES (enumtype) = values;
5414 /* Fix up all variant types of this enum type. */
5415 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5417 if (tem == enumtype)
5419 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5420 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5421 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5422 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5423 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5424 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5425 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5426 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5427 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5428 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5431 /* Finish debugging output for this type. */
5432 rest_of_type_compilation (enumtype, toplevel);
5437 /* Build and install a CONST_DECL for one value of the
5438 current enumeration type (one that was begun with start_enum).
5439 Return a tree-list containing the CONST_DECL and its value.
5440 Assignment of sequential values by default is handled here. */
5443 build_enumerator (tree name, tree value)
5447 /* Validate and default VALUE. */
5449 /* Remove no-op casts from the value. */
5451 STRIP_TYPE_NOPS (value);
5455 if (TREE_CODE (value) == INTEGER_CST)
5457 value = default_conversion (value);
5458 constant_expression_warning (value);
5462 error ("enumerator value for `%s' not integer constant",
5463 IDENTIFIER_POINTER (name));
5468 /* Default based on previous value. */
5469 /* It should no longer be possible to have NON_LVALUE_EXPR
5473 value = enum_next_value;
5475 error ("overflow in enumeration values");
5478 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5480 pedwarn ("ISO C restricts enumerator values to range of `int'");
5481 value = convert (integer_type_node, value);
5484 /* Set basis for default for next value. */
5485 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5486 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5488 /* Now create a declaration for the enum value name. */
5490 type = TREE_TYPE (value);
5491 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5492 TYPE_PRECISION (integer_type_node)),
5493 (TYPE_PRECISION (type)
5494 >= TYPE_PRECISION (integer_type_node)
5495 && TREE_UNSIGNED (type)));
5497 decl = build_decl (CONST_DECL, name, type);
5498 DECL_INITIAL (decl) = convert (type, value);
5501 return tree_cons (decl, value, NULL_TREE);
5505 /* Create the FUNCTION_DECL for a function definition.
5506 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5507 the declaration; they describe the function's name and the type it returns,
5508 but twisted together in a fashion that parallels the syntax of C.
5510 This function creates a binding context for the function body
5511 as well as setting up the FUNCTION_DECL in current_function_decl.
5513 Returns 1 on success. If the DECLARATOR is not suitable for a function
5514 (it defines a datum instead), we return 0, which tells
5515 yyparse to report a parse error. */
5518 start_function (tree declspecs, tree declarator, tree attributes)
5520 tree decl1, old_decl;
5522 int old_immediate_size_expand = immediate_size_expand;
5524 current_function_returns_value = 0; /* Assume, until we see it does. */
5525 current_function_returns_null = 0;
5526 current_function_returns_abnormally = 0;
5527 warn_about_return_type = 0;
5528 current_extern_inline = 0;
5530 /* Don't expand any sizes in the return type of the function. */
5531 immediate_size_expand = 0;
5533 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5535 /* If the declarator is not suitable for a function definition,
5536 cause a syntax error. */
5539 immediate_size_expand = old_immediate_size_expand;
5543 decl_attributes (&decl1, attributes, 0);
5545 if (DECL_DECLARED_INLINE_P (decl1)
5546 && DECL_UNINLINABLE (decl1)
5547 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5548 warning ("%Hinline function '%D' given attribute noinline",
5549 &DECL_SOURCE_LOCATION (decl1), decl1);
5551 announce_function (decl1);
5553 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5555 error ("return type is an incomplete type");
5556 /* Make it return void instead. */
5558 = build_function_type (void_type_node,
5559 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5562 if (warn_about_return_type)
5563 pedwarn_c99 ("return type defaults to `int'");
5565 /* Save the parm names or decls from this function's declarator
5566 where store_parm_decls will find them. */
5567 current_function_parms = last_function_parms;
5568 current_function_parm_tags = last_function_parm_tags;
5569 current_function_parm_vars = last_function_parm_vars;
5571 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5572 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5573 DECL_INITIAL (decl1) = error_mark_node;
5575 /* If this definition isn't a prototype and we had a prototype declaration
5576 before, copy the arg type info from that prototype.
5577 But not if what we had before was a builtin function. */
5578 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5579 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5580 && !DECL_BUILT_IN (old_decl)
5581 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5582 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5583 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5585 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5586 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5589 /* Optionally warn of old-fashioned def with no previous prototype. */
5590 if (warn_strict_prototypes
5591 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5592 && C_DECL_ISNT_PROTOTYPE (old_decl))
5593 warning ("function declaration isn't a prototype");
5594 /* Optionally warn of any global def with no previous prototype. */
5595 else if (warn_missing_prototypes
5596 && TREE_PUBLIC (decl1)
5597 && ! MAIN_NAME_P (DECL_NAME (decl1))
5598 && C_DECL_ISNT_PROTOTYPE (old_decl))
5599 warning ("%Hno previous prototype for '%D'",
5600 &DECL_SOURCE_LOCATION (decl1), decl1);
5601 /* Optionally warn of any def with no previous prototype
5602 if the function has already been used. */
5603 else if (warn_missing_prototypes
5604 && old_decl != 0 && TREE_USED (old_decl)
5605 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5606 warning ("%H'%D' was used with no prototype before its definition",
5607 &DECL_SOURCE_LOCATION (decl1), decl1);
5608 /* Optionally warn of any global def with no previous declaration. */
5609 else if (warn_missing_declarations
5610 && TREE_PUBLIC (decl1)
5612 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5613 warning ("%Hno previous declaration for '%D'",
5614 &DECL_SOURCE_LOCATION (decl1), decl1);
5615 /* Optionally warn of any def with no previous declaration
5616 if the function has already been used. */
5617 else if (warn_missing_declarations
5618 && old_decl != 0 && TREE_USED (old_decl)
5619 && C_DECL_IMPLICIT (old_decl))
5620 warning ("%H`%D' was used with no declaration before its definition",
5621 &DECL_SOURCE_LOCATION (decl1), decl1);
5623 /* This is a definition, not a reference.
5624 So normally clear DECL_EXTERNAL.
5625 However, `extern inline' acts like a declaration
5626 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5627 DECL_EXTERNAL (decl1) = current_extern_inline;
5629 /* This function exists in static storage.
5630 (This does not mean `static' in the C sense!) */
5631 TREE_STATIC (decl1) = 1;
5633 /* A nested function is not global. */
5634 if (current_function_decl != 0)
5635 TREE_PUBLIC (decl1) = 0;
5637 #ifdef ENABLE_CHECKING
5638 /* This is the earliest point at which we might know the assembler
5639 name of the function. Thus, if it's set before this, die horribly. */
5640 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5644 /* If #pragma weak was used, mark the decl weak now. */
5645 if (current_scope == global_scope)
5646 maybe_apply_pragma_weak (decl1);
5648 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5649 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5653 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5655 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5656 != integer_type_node)
5657 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5659 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5660 args = TREE_CHAIN (args))
5662 tree type = args ? TREE_VALUE (args) : 0;
5664 if (type == void_type_node)
5671 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5672 pedwarn ("%Hfirst argument of '%D' should be `int'",
5677 if (TREE_CODE (type) != POINTER_TYPE
5678 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5679 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5681 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5686 if (TREE_CODE (type) != POINTER_TYPE
5687 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5688 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5690 pedwarn ("%Hthird argument of '%D' should probably be "
5691 "'char **'", locus, decl1);
5696 /* It is intentional that this message does not mention the third
5697 argument because it's only mentioned in an appendix of the
5699 if (argct > 0 && (argct < 2 || argct > 3))
5700 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5702 if (! TREE_PUBLIC (decl1))
5703 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5706 /* Record the decl so that the function name is defined.
5707 If we already have a decl for this name, and it is a FUNCTION_DECL,
5708 use the old decl. */
5710 current_function_decl = pushdecl (decl1);
5713 declare_parm_level ();
5715 make_decl_rtl (current_function_decl, NULL);
5717 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5718 /* Promote the value to int before returning it. */
5719 if (c_promoting_integer_type_p (restype))
5721 /* It retains unsignedness if not really getting wider. */
5722 if (TREE_UNSIGNED (restype)
5723 && (TYPE_PRECISION (restype)
5724 == TYPE_PRECISION (integer_type_node)))
5725 restype = unsigned_type_node;
5727 restype = integer_type_node;
5729 DECL_RESULT (current_function_decl)
5730 = build_decl (RESULT_DECL, NULL_TREE, restype);
5732 /* If this fcn was already referenced via a block-scope `extern' decl
5733 (or an implicit decl), propagate certain information about the usage. */
5734 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5735 TREE_ADDRESSABLE (current_function_decl) = 1;
5737 immediate_size_expand = old_immediate_size_expand;
5739 start_fname_decls ();
5744 /* Subroutine of store_parm_decls which handles new-style function
5745 definitions (prototype format). The parms already have decls, so we
5746 need only record them as in effect and complain if any redundant
5747 old-style parm decls were written. */
5749 store_parm_decls_newstyle (void)
5752 tree fndecl = current_function_decl;
5753 tree parms = current_function_parms;
5754 tree tags = current_function_parm_tags;
5755 tree vars = current_function_parm_vars;
5757 if (current_scope->parms || current_scope->names || current_scope->tags)
5759 error ("%Hold-style parameter declarations in prototyped "
5760 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5762 /* Get rid of the old-style declarations. */
5767 /* Now make all the parameter declarations visible in the function body.
5768 We can bypass most of the grunt work of pushdecl. */
5769 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5771 DECL_CONTEXT (decl) = current_function_decl;
5773 if (DECL_NAME (decl) == 0)
5774 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5777 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5778 current_scope->shadowed
5779 = tree_cons (DECL_NAME (decl),
5780 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5781 current_scope->shadowed);
5782 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5785 current_scope->parms = parms;
5787 /* Record the parameter list in the function declaration. */
5788 DECL_ARGUMENTS (fndecl) = parms;
5790 /* Now make all the ancillary declarations visible, likewise. */
5791 for (decl = vars; decl; decl = TREE_CHAIN (decl))
5793 DECL_CONTEXT (decl) = current_function_decl;
5794 if (DECL_NAME (decl)
5795 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5797 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5798 current_scope->shadowed
5799 = tree_cons (DECL_NAME (decl),
5800 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5801 current_scope->shadowed);
5802 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5805 current_scope->names = vars;
5807 /* And all the tag declarations. */
5808 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5809 if (TREE_PURPOSE (decl))
5811 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5812 current_scope->shadowed_tags
5813 = tree_cons (TREE_PURPOSE (decl),
5814 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5815 current_scope->shadowed_tags);
5816 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5818 current_scope->tags = tags;
5821 /* Subroutine of store_parm_decls which handles old-style function
5822 definitions (separate parameter list and declarations). */
5825 store_parm_decls_oldstyle (void)
5827 tree parm, decl, last;
5828 tree fndecl = current_function_decl;
5830 /* This is the identifier list from the function declarator. */
5831 tree parmids = current_function_parms;
5833 /* We use DECL_WEAK as a flag to show which parameters have been
5834 seen already, since it is not used on PARM_DECL or CONST_DECL. */
5835 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5836 DECL_WEAK (parm) = 0;
5838 /* Match each formal parameter name with its declaration. Save each
5839 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5840 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5842 if (TREE_VALUE (parm) == 0)
5844 error ("%Hparameter name missing from parameter list",
5845 &DECL_SOURCE_LOCATION (fndecl));
5846 TREE_PURPOSE (parm) = 0;
5850 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5851 if (decl && DECL_CONTEXT (decl) == fndecl)
5853 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5854 /* If we got something other than a PARM_DECL it is an error. */
5855 if (TREE_CODE (decl) != PARM_DECL)
5856 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5857 /* If the declaration is already marked, we have a duplicate
5858 name. Complain and ignore the duplicate. */
5859 else if (DECL_WEAK (decl))
5861 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5862 TREE_PURPOSE (parm) = 0;
5865 /* If the declaration says "void", complain and turn it into
5867 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5869 error ("%Hparameter \"%D\" declared void", locus, decl);
5870 TREE_TYPE (decl) = integer_type_node;
5871 DECL_ARG_TYPE (decl) = integer_type_node;
5872 layout_decl (decl, 0);
5875 /* If no declaration found, default to int. */
5878 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5879 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5880 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5881 DECL_SOURCE_LOCATION (decl) = *locus;
5885 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5886 else if (extra_warnings)
5887 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5890 TREE_PURPOSE (parm) = decl;
5891 DECL_WEAK (decl) = 1;
5894 /* Now examine the parms chain for incomplete declarations
5895 and declarations with no corresponding names. */
5897 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5899 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5901 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5903 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5904 TREE_TYPE (parm) = error_mark_node;
5907 if (! DECL_WEAK (parm))
5909 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5912 /* Pretend the parameter was not missing.
5913 This gets us to a standard state and minimizes
5914 further error messages. */
5915 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5919 /* Chain the declarations together in the order of the list of
5920 names. Store that chain in the function decl, replacing the
5921 list of names. Update the current scope to match. */
5922 DECL_ARGUMENTS (fndecl) = 0;
5924 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5925 if (TREE_PURPOSE (parm))
5927 if (parm && TREE_PURPOSE (parm))
5929 last = TREE_PURPOSE (parm);
5930 DECL_ARGUMENTS (fndecl) = last;
5931 current_scope->parms = last;
5932 DECL_WEAK (last) = 0;
5934 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5935 if (TREE_PURPOSE (parm))
5937 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5938 last = TREE_PURPOSE (parm);
5939 DECL_WEAK (last) = 0;
5941 TREE_CHAIN (last) = 0;
5944 /* If there was a previous prototype,
5945 set the DECL_ARG_TYPE of each argument according to
5946 the type previously specified, and report any mismatches. */
5948 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5951 for (parm = DECL_ARGUMENTS (fndecl),
5952 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5953 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5954 != void_type_node));
5955 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5957 if (parm == 0 || type == 0
5958 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5960 error ("number of arguments doesn't match prototype");
5961 error ("%Hprototype declaration",
5962 ¤t_function_prototype_locus);
5965 /* Type for passing arg must be consistent with that
5966 declared for the arg. ISO C says we take the unqualified
5967 type for parameters declared with qualified type. */
5968 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5969 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5972 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5973 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5975 /* Adjust argument to match prototype. E.g. a previous
5976 `int foo(float);' prototype causes
5977 `int foo(x) float x; {...}' to be treated like
5978 `int foo(float x) {...}'. This is particularly
5979 useful for argument types like uid_t. */
5980 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5982 if (PROMOTE_PROTOTYPES
5983 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5984 && TYPE_PRECISION (TREE_TYPE (parm))
5985 < TYPE_PRECISION (integer_type_node))
5986 DECL_ARG_TYPE (parm) = integer_type_node;
5990 pedwarn ("promoted argument \"%D\" "
5991 "doesn't match prototype", parm);
5992 pedwarn ("%Hprototype declaration",
5993 ¤t_function_prototype_locus);
5998 error ("argument \"%D\" doesn't match prototype", parm);
5999 error ("%Hprototype declaration",
6000 ¤t_function_prototype_locus);
6004 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6007 /* Otherwise, create a prototype that would match. */
6011 tree actual = 0, last = 0, type;
6013 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6015 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6017 TREE_CHAIN (last) = type;
6022 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6024 TREE_CHAIN (last) = type;
6028 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6029 of the type of this function, but we need to avoid having this
6030 affect the types of other similarly-typed functions, so we must
6031 first force the generation of an identical (but separate) type
6032 node for the relevant function type. The new node we create
6033 will be a variant of the main variant of the original function
6036 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6038 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6042 /* Store the parameter declarations into the current function declaration.
6043 This is called after parsing the parameter declarations, before
6044 digesting the body of the function.
6046 For an old-style definition, construct a prototype out of the old-style
6047 parameter declarations and inject it into the function's type. */
6050 store_parm_decls (void)
6052 tree fndecl = current_function_decl;
6054 /* The function containing FNDECL, if any. */
6055 tree context = decl_function_context (fndecl);
6057 /* True if this definition is written with a prototype. */
6058 bool prototype = (current_function_parms
6059 && TREE_CODE (current_function_parms) != TREE_LIST);
6062 store_parm_decls_newstyle ();
6064 store_parm_decls_oldstyle ();
6066 /* The next call to pushlevel will be a function body. */
6068 next_is_function_body = true;
6070 /* Write a record describing this function definition to the prototypes
6071 file (if requested). */
6073 gen_aux_info_record (fndecl, 1, 0, prototype);
6075 /* Initialize the RTL code for the function. */
6076 init_function_start (fndecl);
6078 /* Begin the statement tree for this function. */
6079 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6081 /* If this is a nested function, save away the sizes of any
6082 variable-size types so that we can expand them when generating
6088 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6089 = nreverse (get_pending_sizes ());
6090 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6093 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6096 /* This function is being processed in whole-function mode. */
6097 cfun->x_whole_function_mode_p = 1;
6099 /* Even though we're inside a function body, we still don't want to
6100 call expand_expr to calculate the size of a variable-sized array.
6101 We haven't necessarily assigned RTL to all variables yet, so it's
6102 not safe to try to expand expressions involving them. */
6103 immediate_size_expand = 0;
6104 cfun->x_dont_save_pending_sizes_p = 1;
6107 /* Finish up a function declaration and compile that function
6108 all the way to assembler language output. The free the storage
6109 for the function definition.
6111 This is called after parsing the body of the function definition.
6113 NESTED is nonzero if the function being finished is nested in another.
6114 CAN_DEFER_P is nonzero if the function may be deferred. */
6117 finish_function (int nested, int can_defer_p)
6119 tree fndecl = current_function_decl;
6121 /* When a function declaration is totally empty, e.g.
6123 (the argument list is irrelevant) the compstmt rule will not
6124 bother calling pushlevel/poplevel, which means we get here with
6125 the scope stack out of sync. Detect this situation by noticing
6126 that current_scope is still as store_parm_decls left it, and do
6127 a dummy push/pop to get back to consistency.
6128 Note that the call to pushlevel does not actually push another
6129 scope - see there for details. */
6131 if (current_scope->parm_flag && next_is_function_body)
6137 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6139 /* Must mark the RESULT_DECL as being in this function. */
6141 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6143 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6145 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6146 != integer_type_node)
6148 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6149 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6151 pedwarn ("%Hreturn type of '%D' is not `int'",
6152 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6156 #ifdef DEFAULT_MAIN_RETURN
6157 /* Make it so that `main' always returns success by default. */
6158 DEFAULT_MAIN_RETURN;
6161 c_expand_return (integer_zero_node);
6166 finish_fname_decls ();
6168 /* Tie off the statement tree for this function. */
6169 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6171 /* Complain if there's just no return statement. */
6172 if (warn_return_type
6173 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6174 && !current_function_returns_value && !current_function_returns_null
6175 /* Don't complain if we abort. */
6176 && !current_function_returns_abnormally
6177 /* Don't warn for main(). */
6178 && !MAIN_NAME_P (DECL_NAME (fndecl))
6179 /* Or if they didn't actually specify a return type. */
6180 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6181 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6182 inline function, as we might never be compiled separately. */
6183 && DECL_INLINE (fndecl))
6184 warning ("no return statement in function returning non-void");
6186 /* Clear out memory we no longer need. */
6187 free_after_parsing (cfun);
6188 /* Since we never call rest_of_compilation, we never clear
6189 CFUN. Do so explicitly. */
6190 free_after_compilation (cfun);
6193 if (flag_unit_at_a_time && can_defer_p)
6195 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6196 current_function_decl = NULL;
6202 /* Function is parsed.
6203 Generate RTL for the body of this function or defer
6204 it for later expansion. */
6205 int uninlinable = 1;
6207 /* There's no reason to do any of the work here if we're only doing
6208 semantic analysis; this code just generates RTL. */
6209 if (flag_syntax_only)
6211 current_function_decl = NULL;
6212 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6216 if (flag_inline_trees)
6218 /* First, cache whether the current function is inlinable. Some
6219 predicates depend on cfun and current_function_decl to
6220 function completely. */
6221 timevar_push (TV_INTEGRATION);
6222 uninlinable = ! tree_inlinable_function_p (fndecl, 0);
6225 /* We defer functions marked inline *even if* the function
6226 itself is not inlinable. This is because we don't yet
6227 know if the function will actually be used; we may be
6228 able to avoid emitting it entirely. */
6229 && (! uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6230 /* Save function tree for inlining. Should return 0 if the
6231 language does not support function deferring or the
6232 function could not be deferred. */
6233 && defer_fn (fndecl))
6235 /* Let the back-end know that this function exists. */
6236 (*debug_hooks->deferred_inline_function) (fndecl);
6237 timevar_pop (TV_INTEGRATION);
6238 current_function_decl = NULL;
6242 /* Then, inline any functions called in it. */
6243 optimize_inline_calls (fndecl);
6244 timevar_pop (TV_INTEGRATION);
6247 c_expand_body (fndecl);
6249 /* Keep the function body if it's needed for inlining or dumping. */
6250 if (uninlinable && !dump_enabled_p (TDI_all))
6252 /* Allow the body of the function to be garbage collected. */
6253 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6256 /* Let the error reporting routines know that we're outside a
6257 function. For a nested function, this value is used in
6258 c_pop_function_context and then reset via pop_function_context. */
6259 current_function_decl = NULL;
6263 /* Generate the RTL for a deferred function FNDECL. */
6266 c_expand_deferred_function (tree fndecl)
6268 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6269 function was deferred, e.g. in duplicate_decls. */
6270 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6272 if (flag_inline_trees)
6274 timevar_push (TV_INTEGRATION);
6275 optimize_inline_calls (fndecl);
6276 timevar_pop (TV_INTEGRATION);
6278 c_expand_body (fndecl);
6279 current_function_decl = NULL;
6283 /* Called to move the SAVE_EXPRs for parameter declarations in a
6284 nested function into the nested function. DATA is really the
6285 nested FUNCTION_DECL. */
6288 set_save_expr_context (tree *tp,
6292 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6293 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6294 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6296 else if (DECL_P (*tp))
6302 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6303 then we are already in the process of generating RTL for another
6304 function. If can_defer_p is zero, we won't attempt to defer the
6305 generation of RTL. */
6308 c_expand_body_1 (tree fndecl, int nested_p)
6310 timevar_push (TV_EXPAND);
6314 /* Make sure that we will evaluate variable-sized types involved
6315 in our function's type. */
6316 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6317 /* Squirrel away our current state. */
6318 push_function_context ();
6321 /* Initialize the RTL code for the function. */
6322 current_function_decl = fndecl;
6323 input_location = DECL_SOURCE_LOCATION (fndecl);
6324 init_function_start (fndecl);
6326 /* This function is being processed in whole-function mode. */
6327 cfun->x_whole_function_mode_p = 1;
6329 /* Even though we're inside a function body, we still don't want to
6330 call expand_expr to calculate the size of a variable-sized array.
6331 We haven't necessarily assigned RTL to all variables yet, so it's
6332 not safe to try to expand expressions involving them. */
6333 immediate_size_expand = 0;
6334 cfun->x_dont_save_pending_sizes_p = 1;
6336 /* Set up parameters and prepare for return, for the function. */
6337 expand_function_start (fndecl, 0);
6339 /* If the function has a variably modified type, there may be
6340 SAVE_EXPRs in the parameter types. Their context must be set to
6341 refer to this function; they cannot be expanded in the containing
6343 if (decl_function_context (fndecl)
6344 && variably_modified_type_p (TREE_TYPE (fndecl)))
6345 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6348 /* If this function is `main', emit a call to `__main'
6349 to run global initializers, etc. */
6350 if (DECL_NAME (fndecl)
6351 && MAIN_NAME_P (DECL_NAME (fndecl))
6352 && C_DECL_FILE_SCOPE (fndecl))
6353 expand_main_function ();
6355 /* Generate the RTL for this function. */
6356 expand_stmt (DECL_SAVED_TREE (fndecl));
6358 /* We hard-wired immediate_size_expand to zero above.
6359 expand_function_end will decrement this variable. So, we set the
6360 variable to one here, so that after the decrement it will remain
6362 immediate_size_expand = 1;
6364 /* Allow language dialects to perform special processing. */
6365 if (lang_expand_function_end)
6366 (*lang_expand_function_end) ();
6368 /* Generate rtl for function exit. */
6369 expand_function_end ();
6371 /* If this is a nested function, protect the local variables in the stack
6372 above us from being collected while we're compiling this function. */
6374 ggc_push_context ();
6376 /* Run the optimizers and output the assembler code for this function. */
6377 rest_of_compilation (fndecl);
6379 /* Undo the GC context switch. */
6383 /* With just -Wextra, complain only if function returns both with
6384 and without a value. */
6386 && current_function_returns_value
6387 && current_function_returns_null)
6388 warning ("this function may return with or without a value");
6390 /* If requested, warn about function definitions where the function will
6391 return a value (usually of some struct or union type) which itself will
6392 take up a lot of stack space. */
6394 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6396 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6398 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6399 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6400 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6403 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
6404 unsigned int size_as_int
6405 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6407 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6408 warning ("%Hsize of return value of '%D' is %u bytes",
6409 locus, fndecl, size_as_int);
6411 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6412 locus, fndecl, larger_than_size);
6416 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6417 && ! flag_inline_trees)
6419 /* Stop pointing to the local nodes about to be freed.
6420 But DECL_INITIAL must remain nonzero so we know this
6421 was an actual function definition.
6422 For a nested function, this is done in c_pop_function_context.
6423 If rest_of_compilation set this to 0, leave it 0. */
6424 if (DECL_INITIAL (fndecl) != 0)
6425 DECL_INITIAL (fndecl) = error_mark_node;
6427 DECL_ARGUMENTS (fndecl) = 0;
6430 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6432 if (targetm.have_ctors_dtors)
6433 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6434 DEFAULT_INIT_PRIORITY);
6436 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6439 if (DECL_STATIC_DESTRUCTOR (fndecl))
6441 if (targetm.have_ctors_dtors)
6442 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6443 DEFAULT_INIT_PRIORITY);
6445 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6449 /* Return to the enclosing function. */
6450 pop_function_context ();
6451 timevar_pop (TV_EXPAND);
6454 /* Like c_expand_body_1 but only for unnested functions. */
6457 c_expand_body (tree fndecl)
6459 c_expand_body_1 (fndecl, 0);
6462 /* Check the declarations given in a for-loop for satisfying the C99
6465 check_for_loop_decls (void)
6471 /* If we get here, declarations have been used in a for loop without
6472 the C99 for loop scope. This doesn't make much sense, so don't
6474 error ("`for' loop initial declaration used outside C99 mode");
6477 /* C99 subclause 6.8.5 paragraph 3:
6479 [#3] The declaration part of a for statement shall only
6480 declare identifiers for objects having storage class auto or
6483 It isn't clear whether, in this sentence, "identifiers" binds to
6484 "shall only declare" or to "objects" - that is, whether all identifiers
6485 declared must be identifiers for objects, or whether the restriction
6486 only applies to those that are. (A question on this in comp.std.c
6487 in November 2000 received no answer.) We implement the strictest
6488 interpretation, to avoid creating an extension which later causes
6491 for (t = gettags (); t; t = TREE_CHAIN (t))
6493 if (TREE_PURPOSE (t) != 0)
6495 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6497 if (code == RECORD_TYPE)
6498 error ("`struct %s' declared in `for' loop initial declaration",
6499 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6500 else if (code == UNION_TYPE)
6501 error ("`union %s' declared in `for' loop initial declaration",
6502 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6504 error ("`enum %s' declared in `for' loop initial declaration",
6505 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6509 for (t = getdecls (); t; t = TREE_CHAIN (t))
6511 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6512 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6513 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6514 "initial declaration", locus, t);
6515 else if (TREE_STATIC (t))
6516 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6517 "initial declaration", locus, t);
6518 else if (DECL_EXTERNAL (t))
6519 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6520 "initial declaration", locus, t);
6524 /* Save and restore the variables in this file and elsewhere
6525 that keep track of the progress of compilation of the current function.
6526 Used for nested functions. */
6528 struct language_function GTY(())
6530 struct c_language_function base;
6533 int returns_abnormally;
6534 int warn_about_return_type;
6538 /* Save and reinitialize the variables
6539 used during compilation of a C function. */
6542 c_push_function_context (struct function *f)
6544 struct language_function *p;
6545 p = ggc_alloc (sizeof (struct language_function));
6548 p->base.x_stmt_tree = c_stmt_tree;
6549 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6550 p->returns_value = current_function_returns_value;
6551 p->returns_null = current_function_returns_null;
6552 p->returns_abnormally = current_function_returns_abnormally;
6553 p->warn_about_return_type = warn_about_return_type;
6554 p->extern_inline = current_extern_inline;
6557 /* Restore the variables used during compilation of a C function. */
6560 c_pop_function_context (struct function *f)
6562 struct language_function *p = f->language;
6564 if (DECL_SAVED_INSNS (current_function_decl) == 0
6565 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6567 /* Stop pointing to the local nodes about to be freed. */
6568 /* But DECL_INITIAL must remain nonzero so we know this
6569 was an actual function definition. */
6570 DECL_INITIAL (current_function_decl) = error_mark_node;
6571 DECL_ARGUMENTS (current_function_decl) = 0;
6574 c_stmt_tree = p->base.x_stmt_tree;
6575 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6576 current_function_returns_value = p->returns_value;
6577 current_function_returns_null = p->returns_null;
6578 current_function_returns_abnormally = p->returns_abnormally;
6579 warn_about_return_type = p->warn_about_return_type;
6580 current_extern_inline = p->extern_inline;
6585 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6588 c_dup_lang_specific_decl (tree decl)
6590 struct lang_decl *ld;
6592 if (!DECL_LANG_SPECIFIC (decl))
6595 ld = ggc_alloc (sizeof (struct lang_decl));
6596 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6597 DECL_LANG_SPECIFIC (decl) = ld;
6600 /* The functions below are required for functionality of doing
6601 function at once processing in the C front end. Currently these
6602 functions are not called from anywhere in the C front end, but as
6603 these changes continue, that will change. */
6605 /* Returns nonzero if the current statement is a full expression,
6606 i.e. temporaries created during that statement should be destroyed
6607 at the end of the statement. */
6610 stmts_are_full_exprs_p (void)
6615 /* Returns the stmt_tree (if any) to which statements are currently
6616 being added. If there is no active statement-tree, NULL is
6620 current_stmt_tree (void)
6622 return &c_stmt_tree;
6625 /* Returns the stack of SCOPE_STMTs for the current function. */
6628 current_scope_stmt_stack (void)
6630 return &c_scope_stmt_stack;
6633 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6637 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6642 /* Dummy function in place of callback used by C++. */
6645 extract_interface_info (void)
6649 /* Return a new COMPOUND_STMT, after adding it to the current
6653 c_begin_compound_stmt (void)
6657 /* Create the COMPOUND_STMT. */
6658 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6663 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6667 c_expand_decl_stmt (tree t)
6669 tree decl = DECL_STMT_DECL (t);
6671 /* Expand nested functions. */
6672 if (TREE_CODE (decl) == FUNCTION_DECL
6673 && DECL_CONTEXT (decl) == current_function_decl
6674 && DECL_SAVED_TREE (decl))
6675 c_expand_body_1 (decl, 1);
6678 /* Return the global value of T as a symbol. */
6681 identifier_global_value (tree t)
6683 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6684 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6687 /* Shadowed by something else; find the true global value. */
6688 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6689 if (DECL_NAME (decl) == t)
6692 /* Only local values for this decl. */
6696 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6697 otherwise the name is found in ridpointers from RID_INDEX. */
6700 record_builtin_type (enum rid rid_index, const char *name, tree type)
6704 id = ridpointers[(int) rid_index];
6706 id = get_identifier (name);
6707 pushdecl (build_decl (TYPE_DECL, id, type));
6710 /* Build the void_list_node (void_type_node having been created). */
6712 build_void_list_node (void)
6714 tree t = build_tree_list (NULL_TREE, void_type_node);
6718 /* Return something to represent absolute declarators containing a *.
6719 TARGET is the absolute declarator that the * contains.
6720 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6721 to apply to the pointer type, represented as identifiers, possible mixed
6724 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6725 if attributes are present) and whose type is the modifier list. */
6728 make_pointer_declarator (tree type_quals_attrs, tree target)
6731 tree itarget = target;
6732 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6733 if (attrs != NULL_TREE)
6734 itarget = tree_cons (attrs, target, NULL_TREE);
6735 return build1 (INDIRECT_REF, quals, itarget);
6738 /* A wrapper around lhd_set_decl_assembler_name that gives static
6739 variables their C names if they are at file scope and only one
6740 translation unit is being compiled, for backwards compatibility
6741 with certain bizzare assembler hacks (like crtstuff.c). */
6744 c_static_assembler_name (tree decl)
6746 if (num_in_fnames == 1
6747 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6748 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6749 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6751 lhd_set_decl_assembler_name (decl);
6754 /* Hash and equality functions for link_hash_table: key off
6755 DECL_ASSEMBLER_NAME. */
6758 link_hash_hash (const void *x_p)
6761 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6765 link_hash_eq (const void *x1_p, const void *x2_p)
6767 tree x1 = (tree)x1_p;
6768 tree x2 = (tree)x2_p;
6769 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6772 /* Propagate information between definitions and uses between multiple
6773 translation units in TU_LIST based on linkage rules. */
6776 merge_translation_unit_decls (void)
6778 const tree tu_list = current_file_decl;
6781 htab_t link_hash_table;
6784 /* Create the BLOCK that poplevel would have created, but don't
6785 actually call poplevel since that's expensive. */
6786 block = make_node (BLOCK);
6787 BLOCK_VARS (block) = current_scope->names;
6788 TREE_USED (block) = 1;
6789 DECL_INITIAL (current_file_decl) = block;
6791 /* If only one translation unit seen, no copying necessary. */
6792 if (TREE_CHAIN (tu_list) == NULL_TREE)
6795 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6797 /* Enter any actual definitions into the hash table. */
6798 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6799 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6800 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6803 slot = htab_find_slot (link_hash_table, decl, INSERT);
6805 /* If we've already got a definition, work out which one is
6806 the real one, put it into the hash table, and make the
6807 other one DECL_EXTERNAL. This is important to avoid
6808 putting out two definitions of the same symbol in the
6812 tree old_decl = (tree) *slot;
6814 /* If this is weak or common or whatever, suppress it
6815 in favour of the other definition. */
6816 if (DECL_WEAK (decl))
6817 DECL_EXTERNAL (decl) = 1;
6818 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6819 DECL_EXTERNAL (old_decl) = 1;
6820 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6821 DECL_EXTERNAL (decl) = 1;
6822 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6823 DECL_EXTERNAL (old_decl) = 1;
6825 if (DECL_EXTERNAL (decl))
6827 DECL_INITIAL (decl) = NULL_TREE;
6828 DECL_COMMON (decl) = 0;
6829 DECL_ONE_ONLY (decl) = 0;
6830 DECL_WEAK (decl) = 0;
6832 else if (DECL_EXTERNAL (old_decl))
6834 DECL_INITIAL (old_decl) = NULL_TREE;
6835 DECL_COMMON (old_decl) = 0;
6836 DECL_ONE_ONLY (old_decl) = 0;
6837 DECL_WEAK (old_decl) = 0;
6842 error ("%Hredefinition of global '%D'",
6843 &DECL_SOURCE_LOCATION (decl), decl);
6844 error ("%H'%D' previously defined here",
6845 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6852 /* Now insert the desired information from all the definitions
6853 into any plain declarations. */
6854 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6855 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6856 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6859 global_decl = htab_find (link_hash_table, decl);
6864 /* Print any appropriate error messages, and partially merge
6866 (void) duplicate_decls (decl, global_decl, true, true);
6869 htab_delete (link_hash_table);
6872 /* Perform final processing on file-scope data. */
6875 c_write_global_declarations(void)
6879 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6881 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6882 int len = list_length (globals);
6883 tree *vec = xmalloc (sizeof (tree) * len);
6887 /* Process the decls in reverse order--earliest first.
6888 Put them into VEC from back to front, then take out from front. */
6890 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6891 vec[len - i - 1] = decl;
6893 wrapup_global_declarations (vec, len);
6895 check_global_declarations (vec, len);
6902 /* Reset the parser's state in preparation for a new file. */
6905 c_reset_state (void)
6908 tree file_scope_decl;
6910 /* Pop the global scope. */
6911 if (current_scope != global_scope)
6912 current_scope = global_scope;
6913 file_scope_decl = current_file_decl;
6914 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6915 truly_local_externals = NULL_TREE;
6917 /* Start a new global binding level. */
6919 global_scope = current_scope;
6920 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6921 TREE_CHAIN (current_file_decl) = file_scope_decl;
6923 /* Reintroduce the global declarations. */
6924 for (link = builtin_decls; link; link = TREE_CHAIN (link))
6925 pushdecl (copy_node (link));
6928 #include "gt-c-decl.h"