* diagnostic.c (announce_function): Move to toplev.c.
[official-gcc.git] / gcc / c-decl.c
blobf4896f3f8bc3cca132a0fccfcd802fbc9a12dd7b
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
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. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "cgraph.h"
53 #include "hashtab.h"
54 #include "libfuncs.h"
55 #include "except.h"
56 #include "langhooks-def.h"
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 enum decl_context
60 { NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
64 BITFIELD, /* Likewise but with specified width */
65 TYPENAME}; /* Typename (inside cast or sizeof) */
68 /* Nonzero if we have seen an invalid cross reference
69 to a struct, union, or enum, but not yet printed the message. */
71 tree pending_invalid_xref;
72 /* File and line to appear in the eventual error message. */
73 location_t pending_invalid_xref_location;
75 /* While defining an enum type, this is 1 plus the last enumerator
76 constant value. Note that will do not have to save this or `enum_overflow'
77 around nested function definition since such a definition could only
78 occur in an enum value expression and we don't use these variables in
79 that case. */
81 static tree enum_next_value;
83 /* Nonzero means that there was overflow computing enum_next_value. */
85 static int enum_overflow;
87 /* Parsing a function declarator leaves a list of parameter names
88 or a chain of parameter decls here. */
90 static tree last_function_parms;
92 /* ... and a chain of structure and enum types declared in the
93 parmlist here. */
95 static tree last_function_parm_tags;
97 /* ... and a chain of all non-parameter declarations (such as
98 CONST_DECLs from enumerations) here. */
100 static tree last_function_parm_others;
102 /* After parsing the declarator that starts a function definition,
103 `start_function' puts the list of parameter names or chain of decls here
104 for `store_parm_decls' to find. */
106 static tree current_function_parms;
108 /* Similar, for last_function_parm_tags. */
110 static tree current_function_parm_tags;
112 /* And for last_function_parm_others. */
114 static tree current_function_parm_others;
116 /* Similar, for the file and line that the prototype came from if this is
117 an old-style definition. */
119 static location_t current_function_prototype_locus;
121 /* The current statement tree. */
123 static GTY(()) struct stmt_tree_s c_stmt_tree;
125 /* The current scope statement stack. */
127 static GTY(()) tree c_scope_stmt_stack;
129 /* A list of external DECLs that appeared at block scope when there was
130 some other global meaning for that identifier. */
131 static GTY(()) tree truly_local_externals;
133 /* All the builtins; this is a subset of the entries of global_scope. */
135 static GTY(()) tree first_builtin_decl;
136 static GTY(()) tree last_builtin_decl;
138 /* A DECL for the current file-scope context. */
140 static GTY(()) tree current_file_decl;
142 /* Set to 0 at beginning of a function definition, set to 1 if
143 a return statement that specifies a return value is seen. */
145 int current_function_returns_value;
147 /* Set to 0 at beginning of a function definition, set to 1 if
148 a return statement with no argument is seen. */
150 int current_function_returns_null;
152 /* Set to 0 at beginning of a function definition, set to 1 if
153 a call to a noreturn function is seen. */
155 int current_function_returns_abnormally;
157 /* Set to nonzero by `grokdeclarator' for a function
158 whose return type is defaulted, if warnings for this are desired. */
160 static int warn_about_return_type;
162 /* Nonzero when starting a function declared `extern inline'. */
164 static int current_extern_inline;
166 /* Each c_scope structure describes the complete contents of one scope.
167 Three scopes are distinguished specially: the innermost or current
168 scope, the innermost function scope, and the outermost or file scope.
170 Most declarations are recorded in the current scope.
172 All normal label declarations are recorded in the innermost
173 function scope, as are bindings of undeclared identifiers to
174 error_mark_node. (GCC permits nested functions as an extension,
175 hence the 'innermost' qualifier.) Explicitly declared labels
176 (using the __label__ extension) appear in the current scope.
178 Being in the global scope (current_scope == global_scope) causes
179 special behavior in several places below. Also, under some
180 conditions the Objective-C front end records declarations in the
181 global scope even though that isn't the current scope.
183 The order of the names, parms, and blocks lists matters, and they
184 are frequently appended to. To avoid having to walk all the way to
185 the end of the list on each insertion, or reverse the lists later,
186 we maintain a pointer to the last list entry for each of the lists.
188 The order of the tags, shadowed, and shadowed_tags
189 lists does not matter, so we just prepend to these lists. */
191 struct c_scope GTY(())
193 /* The scope containing this one. */
194 struct c_scope *outer;
196 /* The next outermost function scope. */
197 struct c_scope *outer_function;
199 /* All variables, constants, functions, labels, and typedef names. */
200 tree names;
201 tree names_last;
203 /* All parameter declarations. Used only in the outermost scope of
204 a function. */
205 tree parms;
206 tree parms_last;
208 /* All structure, union, and enum type tags. */
209 tree tags;
211 /* For each scope, a list of shadowed outer-scope definitions
212 to be restored when this scope is popped.
213 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
214 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
215 tree shadowed;
217 /* For each scope, a list of shadowed outer-scope tag definitions
218 to be restored when this scope is popped.
219 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
220 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
221 tree shadowed_tags;
223 /* For each scope (except the global one), a chain of BLOCK nodes
224 for all the scopes that were entered and exited one level down. */
225 tree blocks;
226 tree blocks_last;
228 /* True if we are currently filling this scope with parameter
229 declarations. */
230 bool parm_flag : 1;
232 /* True if we already complained about forward parameter decls
233 in this scope. This prevents double warnings on
234 foo (int a; int b; ...) */
235 bool warned_forward_parm_decls : 1;
237 /* True if this is the outermost block scope of a function body.
238 This scope contains the parameters, the local variables declared
239 in the outermost block, and all the labels (except those in
240 nested functions, or declared at block scope with __label__). */
241 bool function_body : 1;
243 /* True means make a BLOCK for this scope no matter what. */
244 bool keep : 1;
247 /* The scope currently in effect. */
249 static GTY(()) struct c_scope *current_scope;
251 /* A chain of c_scope structures awaiting reuse. */
253 static GTY((deletable (""))) struct c_scope *scope_freelist;
255 /* The innermost function scope. Ordinary (not explicitly declared)
256 labels, bindings to error_mark_node, and the lazily-created
257 bindings of __func__ and its friends get this scope. */
259 static GTY(()) struct c_scope *current_function_scope;
261 /* The outermost scope, corresponding to the C "file scope". This is
262 created when the compiler is started and exists through the entire run. */
264 static GTY(()) struct c_scope *global_scope;
266 /* Append VAR to LIST in scope SCOPE. */
267 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
268 struct c_scope *s_ = (scope); \
269 tree d_ = (decl); \
270 if (s_->list##_last) \
271 TREE_CHAIN (s_->list##_last) = d_; \
272 else \
273 s_->list = d_; \
274 s_->list##_last = d_; \
275 } while (0)
277 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
278 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
279 struct c_scope *t_ = (tscope); \
280 struct c_scope *f_ = (fscope); \
281 if (t_->to##_last) \
282 TREE_CHAIN (t_->to##_last) = f_->from; \
283 else \
284 t_->to = f_->from; \
285 t_->to##_last = f_->from##_last; \
286 } while (0)
288 /* True means unconditionally make a BLOCK for the next scope pushed. */
290 static bool keep_next_level_flag;
292 /* True means the next call to pushlevel will be the outermost scope
293 of a function body, so do not push a new scope, merely cease
294 expecting parameter decls. */
296 static bool next_is_function_body;
298 /* Functions called automatically at the beginning and end of execution. */
300 tree static_ctors, static_dtors;
302 /* Forward declarations. */
304 static struct c_scope *make_scope (void);
305 static void pop_scope (void);
306 static tree match_builtin_function_types (tree, tree);
307 static int duplicate_decls (tree, tree, int, int);
308 static int redeclaration_error_message (tree, tree);
309 static tree make_label (tree, location_t);
310 static void bind_label (tree, tree, struct c_scope *);
311 static void implicit_decl_warning (tree);
312 static tree lookup_tag (enum tree_code, tree, int);
313 static tree lookup_name_current_level (tree);
314 static tree grokdeclarator (tree, tree, enum decl_context, int);
315 static tree grokparms (tree, int);
316 static void layout_array_type (tree);
317 static void store_parm_decls_newstyle (void);
318 static void store_parm_decls_oldstyle (void);
319 static tree c_make_fname_decl (tree, int);
320 static void c_expand_body_1 (tree, int);
321 static tree any_external_decl (tree);
322 static void record_external_decl (tree);
323 static void warn_if_shadowing (tree, tree);
324 static void clone_underlying_type (tree);
325 static bool flexible_array_type_p (tree);
326 static hashval_t link_hash_hash (const void *);
327 static int link_hash_eq (const void *, const void *);
329 /* States indicating how grokdeclarator() should handle declspecs marked
330 with __attribute__((deprecated)). An object declared as
331 __attribute__((deprecated)) suppresses warnings of uses of other
332 deprecated items. */
334 enum deprecated_states {
335 DEPRECATED_NORMAL,
336 DEPRECATED_SUPPRESS
339 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
341 void
342 c_print_identifier (FILE *file, tree node, int indent)
344 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
345 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
346 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
347 if (C_IS_RESERVED_WORD (node))
349 tree rid = ridpointers[C_RID_CODE (node)];
350 indent_to (file, indent + 4);
351 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
352 (void *) rid, IDENTIFIER_POINTER (rid));
356 /* Hook called at end of compilation to assume 1 elt
357 for a file-scope tentative array defn that wasn't complete before. */
359 void
360 c_finish_incomplete_decl (tree decl)
362 if (TREE_CODE (decl) == VAR_DECL)
364 tree type = TREE_TYPE (decl);
365 if (type != error_mark_node
366 && TREE_CODE (type) == ARRAY_TYPE
367 && ! DECL_EXTERNAL (decl)
368 && TYPE_DOMAIN (type) == 0)
370 warning ("%Harray '%D' assumed to have one element",
371 &DECL_SOURCE_LOCATION (decl), decl);
373 complete_array_type (type, NULL_TREE, 1);
375 layout_decl (decl, 0);
380 /* Reuse or create a struct for this scope. */
382 static struct c_scope *
383 make_scope (void)
385 struct c_scope *result;
386 if (scope_freelist)
388 result = scope_freelist;
389 scope_freelist = result->outer;
391 else
392 result = ggc_alloc_cleared (sizeof (struct c_scope));
394 return result;
397 /* Remove the topmost scope from the stack and add it to the
398 free list, updating current_function_scope if necessary. */
400 static void
401 pop_scope (void)
403 struct c_scope *scope = current_scope;
405 current_scope = scope->outer;
406 if (scope->function_body)
407 current_function_scope = scope->outer_function;
409 memset (scope, 0, sizeof (struct c_scope));
410 scope->outer = scope_freelist;
411 scope_freelist = scope;
414 /* Nonzero if we are currently in the global scope. */
417 global_bindings_p (void)
419 return current_scope == global_scope;
422 void
423 keep_next_level (void)
425 keep_next_level_flag = true;
428 /* Identify this scope as currently being filled with parameters. */
430 void
431 declare_parm_level (void)
433 current_scope->parm_flag = true;
436 /* Nonzero if currently making parm declarations. */
439 in_parm_level_p (void)
441 return current_scope->parm_flag;
444 /* Enter a new scope. The dummy parameter is for signature
445 compatibility with lang_hooks.decls.pushlevel. */
447 void
448 pushlevel (int dummy ATTRIBUTE_UNUSED)
450 if (next_is_function_body)
452 /* This is the transition from the parameters to the top level
453 of the function body. These are the same scope
454 (C99 6.2.1p4,6) so we do not push another scope structure.
455 next_is_function_body is set only by store_parm_decls, which
456 in turn is called when and only when we are about to
457 encounter the opening curly brace for the function body.
459 The outermost block of a function always gets a BLOCK node,
460 because the debugging output routines expect that each
461 function has at least one BLOCK. */
462 current_scope->parm_flag = false;
463 current_scope->function_body = true;
464 current_scope->keep = true;
465 current_scope->outer_function = current_function_scope;
466 current_function_scope = current_scope;
468 keep_next_level_flag = false;
469 next_is_function_body = false;
471 else
473 struct c_scope *scope = make_scope ();
475 scope->keep = keep_next_level_flag;
476 scope->outer = current_scope;
477 current_scope = scope;
478 keep_next_level_flag = false;
482 /* Exit a scope. Restore the state of the identifier-decl mappings
483 that were in effect when this scope was entered.
485 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
486 create a BLOCK node to record its declarations and subblocks for
487 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
488 or tags lists are nonempty.
490 The second parameter is ignored; it is present only for
491 signature compatibility with lang_hooks.decls.poplevel.
493 If FUNCTIONBODY is nonzero, this level is the body of a function,
494 even if current_scope->function_body is not set. This is used
495 by language-independent code that generates synthetic functions,
496 and cannot set current_scope->function_body.
498 FIXME: Eliminate the need for all arguments. */
500 tree
501 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
503 struct c_scope *scope = current_scope;
504 tree block;
505 tree decl;
506 tree p;
508 scope->function_body |= functionbody;
510 if (keep == KEEP_MAYBE)
511 keep = (scope->names || scope->tags);
513 keep |= scope->keep;
514 keep |= scope->function_body;
516 /* If appropriate, create a BLOCK to record the decls for the life
517 of this function. */
518 block = 0;
519 if (keep)
521 block = make_node (BLOCK);
522 BLOCK_VARS (block) = scope->names;
523 BLOCK_SUBBLOCKS (block) = scope->blocks;
524 TREE_USED (block) = 1;
527 /* In each subblock, record that this is its superior. */
528 for (p = scope->blocks; p; p = TREE_CHAIN (p))
529 BLOCK_SUPERCONTEXT (p) = block;
531 /* Clear out the variable bindings in this scope.
533 Propagate TREE_ADDRESSABLE from nested functions to their
534 containing functions.
536 Issue warnings for unused variables and labels, and errors for
537 undefined labels, if there are any. */
539 for (p = scope->names; p; p = TREE_CHAIN (p))
541 const location_t *locus = &DECL_SOURCE_LOCATION (p);
543 switch (TREE_CODE (p))
545 case LABEL_DECL:
546 if (TREE_USED (p) && !DECL_INITIAL (p))
548 error ("%Hlabel `%D' used but not defined", locus, p);
549 DECL_INITIAL (p) = error_mark_node;
551 else if (!TREE_USED (p) && warn_unused_label)
553 if (DECL_INITIAL (p))
554 warning ("%Hlabel `%D' defined but not used", locus, p);
555 else
556 warning ("%Hlabel `%D' declared but not defined", locus, p);
559 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
560 break;
562 case FUNCTION_DECL:
563 if (! TREE_ASM_WRITTEN (p)
564 && DECL_INITIAL (p) != 0
565 && TREE_ADDRESSABLE (p)
566 && DECL_ABSTRACT_ORIGIN (p) != 0
567 && DECL_ABSTRACT_ORIGIN (p) != p)
568 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
569 goto normal;
571 case VAR_DECL:
572 /* Keep this in sync with stmt.c:warn_about_unused_variables.
573 No warnings when the global scope is popped because the
574 global scope isn't popped for the last translation unit,
575 so the warnings are done in c_write_global_declaration. */
576 if (warn_unused_variable && scope != global_scope
577 && !TREE_USED (p)
578 && !DECL_IN_SYSTEM_HEADER (p)
579 && DECL_NAME (p)
580 && !DECL_ARTIFICIAL (p))
581 warning ("%Hunused variable `%D'", locus, p);
582 /* fall through */
584 default:
585 normal:
586 if (DECL_NAME (p))
588 if (DECL_EXTERNAL (p) && scope != global_scope)
589 /* External decls stay in the symbol-value slot but are
590 inaccessible. */
591 C_DECL_INVISIBLE (p) = 1;
592 else
593 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
595 break;
599 /* Clear out the parameter bindings in this scope, if any.
600 Unused-parameter warnings are handled by function.c. */
601 for (p = scope->parms; p; p = TREE_CHAIN (p))
602 if (DECL_NAME (p))
603 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
605 /* Clear out the tag-meanings declared in this scope.
607 Set the TYPE_CONTEXTs for all of the tagged types belonging to
608 this scope so that they point to the appropriate construct, i.e.
609 either to the current FUNCTION_DECL node, or else to the BLOCK
610 node we just constructed.
612 Note that for tagged types whose scope is just the formal
613 parameter list for some function type specification, we can't
614 properly set their TYPE_CONTEXTs here, because we don't have a
615 pointer to the appropriate FUNCTION_TYPE node readily available
616 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
617 type nodes get set in `grokdeclarator' as soon as we have created
618 the FUNCTION_TYPE node which will represent the "scope" for these
619 "parameter list local" tagged types. */
621 decl = scope->function_body ? current_function_decl : block;
622 for (p = scope->tags; p; p = TREE_CHAIN (p))
624 if (TREE_PURPOSE (p))
625 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
626 if (decl)
627 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
630 /* Restore all name- and label-meanings from outer scopes that were
631 shadowed by this scope. */
632 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
633 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
634 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
635 else
636 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
638 /* Restore all tag-meanings from outer scopes that were shadowed by
639 this scope. */
640 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
641 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
643 /* Dispose of the block that we just made inside some higher level. */
644 if (scope->function_body)
645 DECL_INITIAL (current_function_decl) = block;
646 else if (scope->outer)
648 if (block)
649 SCOPE_LIST_APPEND (scope->outer, blocks, block);
650 /* If we did not make a block for the scope just exited, any
651 blocks made for inner scopes must be carried forward so they
652 will later become subblocks of something else. */
653 else if (scope->blocks)
654 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
657 /* Pop the current scope, and free the structure for reuse. */
658 pop_scope ();
660 return block;
663 /* Insert BLOCK at the end of the list of subblocks of the current
664 scope. This is used when a BIND_EXPR is expanded, to handle the
665 BLOCK node inside the BIND_EXPR. */
667 void
668 insert_block (tree block)
670 TREE_USED (block) = 1;
671 SCOPE_LIST_APPEND (current_scope, blocks, block);
674 /* Set the BLOCK node for the innermost scope (the one we are
675 currently in). The RTL expansion machinery requires us to provide
676 this hook, but it is not useful in function-at-a-time mode. */
678 void
679 set_block (tree block ATTRIBUTE_UNUSED)
683 /* Push a definition or a declaration of struct, union or enum tag "name".
684 "type" should be the type node.
685 We assume that the tag "name" is not already defined.
687 Note that the definition may really be just a forward reference.
688 In that case, the TYPE_SIZE will be zero. */
690 void
691 pushtag (tree name, tree type)
693 struct c_scope *b = current_scope;
695 /* Record the identifier as the type's name if it has none. */
696 if (name)
698 if (TYPE_NAME (type) == 0)
699 TYPE_NAME (type) = name;
701 if (IDENTIFIER_TAG_VALUE (name))
702 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
703 b->shadowed_tags);
704 IDENTIFIER_TAG_VALUE (name) = type;
707 b->tags = tree_cons (name, type, b->tags);
709 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
710 tagged type we just added to the current scope. This fake
711 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
712 to output a representation of a tagged type, and it also gives
713 us a convenient place to record the "scope start" address for the
714 tagged type. */
716 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
718 /* An approximation for now, so we can tell this is a function-scope tag.
719 This will be updated in poplevel. */
720 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
723 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
724 and argument types provided that the type modes match. This function
725 return a unified type given a suitable match, and 0 otherwise. */
727 static tree
728 match_builtin_function_types (tree oldtype, tree newtype)
730 tree newrettype, oldrettype;
731 tree newargs, oldargs;
732 tree trytype, tryargs;
734 /* Accept the return type of the new declaration if same modes. */
735 oldrettype = TREE_TYPE (oldtype);
736 newrettype = TREE_TYPE (newtype);
738 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
739 return 0;
741 oldargs = TYPE_ARG_TYPES (oldtype);
742 newargs = TYPE_ARG_TYPES (newtype);
743 tryargs = newargs;
745 while (oldargs || newargs)
747 if (! oldargs
748 || ! newargs
749 || ! TREE_VALUE (oldargs)
750 || ! TREE_VALUE (newargs)
751 || TYPE_MODE (TREE_VALUE (oldargs))
752 != TYPE_MODE (TREE_VALUE (newargs)))
753 return 0;
755 oldargs = TREE_CHAIN (oldargs);
756 newargs = TREE_CHAIN (newargs);
759 trytype = build_function_type (newrettype, tryargs);
760 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
763 /* Handle when a new declaration NEWDECL
764 has the same name as an old one OLDDECL
765 in the same binding contour.
766 Prints an error message if appropriate.
768 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
769 Otherwise, return 0.
771 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
772 and OLDDECL is in an outer scope and should thus not be changed. */
774 static int
775 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
776 int different_tu)
778 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
779 COMPARE_STRICT);
780 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
781 && DECL_INITIAL (newdecl) != 0);
782 tree oldtype = TREE_TYPE (olddecl);
783 tree newtype = TREE_TYPE (newdecl);
784 int errmsg = 0;
786 if (DECL_P (olddecl))
788 if (TREE_CODE (newdecl) == FUNCTION_DECL
789 && TREE_CODE (olddecl) == FUNCTION_DECL
790 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
792 if (DECL_DECLARED_INLINE_P (newdecl)
793 && DECL_UNINLINABLE (newdecl)
794 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
795 /* Already warned elsewhere. */;
796 else if (DECL_DECLARED_INLINE_P (olddecl)
797 && DECL_UNINLINABLE (olddecl)
798 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
799 /* Already warned. */;
800 else if (DECL_DECLARED_INLINE_P (newdecl)
801 && ! DECL_DECLARED_INLINE_P (olddecl)
802 && DECL_UNINLINABLE (olddecl)
803 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
805 warning ("%Hfunction '%D' redeclared as inline",
806 &DECL_SOURCE_LOCATION (newdecl), newdecl);
807 warning ("%Hprevious declaration of function '%D' "
808 "with attribute noinline",
809 &DECL_SOURCE_LOCATION (olddecl), olddecl);
811 else if (DECL_DECLARED_INLINE_P (olddecl)
812 && DECL_UNINLINABLE (newdecl)
813 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
815 warning ("%Hfunction '%D' redeclared with attribute noinline",
816 &DECL_SOURCE_LOCATION (newdecl), newdecl);
817 warning ("%Hprevious declaration of function '%D' was inline",
818 &DECL_SOURCE_LOCATION (olddecl), olddecl);
822 DECL_ATTRIBUTES (newdecl)
823 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
826 if (TREE_CODE (newtype) == ERROR_MARK
827 || TREE_CODE (oldtype) == ERROR_MARK)
828 types_match = 0;
830 /* New decl is completely inconsistent with the old one =>
831 tell caller to replace the old one.
832 This is always an error except in the case of shadowing a builtin. */
833 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
835 if (TREE_CODE (olddecl) == FUNCTION_DECL
836 && DECL_BUILT_IN (olddecl))
838 /* If you declare a built-in or predefined function name as static,
839 the old definition is overridden,
840 but optionally warn this was a bad choice of name. */
841 if (!TREE_PUBLIC (newdecl))
843 if (warn_shadow)
844 warning ("%Hshadowing built-in function '%D'",
845 &DECL_SOURCE_LOCATION (newdecl), newdecl);
847 else
848 warning ("%Hbuilt-in function '%D' declared as non-function",
849 &DECL_SOURCE_LOCATION (newdecl), newdecl);
851 else
853 error ("%H'%D' redeclared as different kind of symbol",
854 &DECL_SOURCE_LOCATION (newdecl), newdecl);
855 error ("%Hprevious declaration of '%D'",
856 &DECL_SOURCE_LOCATION (olddecl), olddecl);
859 return 0;
862 /* For real parm decl following a forward decl, return 1 so old decl
863 will be reused. Only allow this to happen once. */
864 if (types_match && TREE_CODE (newdecl) == PARM_DECL
865 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
867 TREE_ASM_WRITTEN (olddecl) = 0;
868 return 1;
871 /* The new declaration is the same kind of object as the old one.
872 The declarations may partially match. Print warnings if they don't
873 match enough. Ultimately, copy most of the information from the new
874 decl to the old one, and keep using the old one. */
876 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
878 /* A function declaration for a built-in function. */
879 if (!TREE_PUBLIC (newdecl))
881 /* If you declare a built-in function name as static, the
882 built-in definition is overridden,
883 but optionally warn this was a bad choice of name. */
884 if (warn_shadow)
885 warning ("%Hshadowing built-in function '%D'",
886 &DECL_SOURCE_LOCATION (newdecl), newdecl);
887 /* Discard the old built-in function. */
888 return 0;
890 if (!types_match)
892 /* Accept harmless mismatch in function types.
893 This is for the ffs and fprintf builtins. */
894 tree trytype = match_builtin_function_types (oldtype, newtype);
896 if (trytype)
898 types_match = comptypes (newtype, trytype, COMPARE_STRICT);
899 if (types_match)
900 oldtype = trytype;
901 if (! different_binding_level)
902 TREE_TYPE (olddecl) = oldtype;
905 if (!types_match)
907 /* If types don't match for a built-in, throw away the built-in. */
908 warning ("%Hconflicting types for built-in function '%D'",
909 &DECL_SOURCE_LOCATION (newdecl), newdecl);
910 return 0;
913 else if (TREE_CODE (olddecl) == FUNCTION_DECL
914 && DECL_SOURCE_LINE (olddecl) == 0)
916 /* A function declaration for a predeclared function
917 that isn't actually built in. */
918 if (!TREE_PUBLIC (newdecl))
920 /* If you declare it as static, the
921 default definition is overridden. */
922 return 0;
924 else if (!types_match)
926 /* If the types don't match, preserve volatility indication.
927 Later on, we will discard everything else about the
928 default declaration. */
929 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
932 /* Permit char *foo () to match void *foo (...) if not pedantic,
933 if one of them came from a system header file. */
934 else if (!types_match
935 && TREE_CODE (olddecl) == FUNCTION_DECL
936 && TREE_CODE (newdecl) == FUNCTION_DECL
937 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
938 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
939 && (DECL_IN_SYSTEM_HEADER (olddecl)
940 || DECL_IN_SYSTEM_HEADER (newdecl))
941 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
942 && TYPE_ARG_TYPES (oldtype) == 0
943 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
944 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
946 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
947 && TYPE_ARG_TYPES (newtype) == 0
948 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
949 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
951 if (pedantic)
952 pedwarn ("%Hconflicting types for '%D'",
953 &DECL_SOURCE_LOCATION (newdecl), newdecl);
954 /* Make sure we keep void * as ret type, not char *. */
955 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
956 TREE_TYPE (newdecl) = newtype = oldtype;
958 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
959 we will come back here again. */
960 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
962 /* Permit void foo (...) to match int foo (...) if the latter is the
963 definition and implicit int was used. See c-torture/compile/920625-2.c. */
964 else if (!types_match && new_is_definition
965 && TREE_CODE (olddecl) == FUNCTION_DECL
966 && TREE_CODE (newdecl) == FUNCTION_DECL
967 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
968 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
969 && C_FUNCTION_IMPLICIT_INT (newdecl))
971 pedwarn ("%Hconflicting types for '%D'",
972 &DECL_SOURCE_LOCATION (newdecl), newdecl);
973 /* Make sure we keep void as the return type. */
974 TREE_TYPE (newdecl) = newtype = oldtype;
975 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
977 else if (!types_match
978 /* Permit char *foo (int, ...); followed by char *foo ();
979 if not pedantic. */
980 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
981 && ! pedantic
982 /* Return types must still match. */
983 && comptypes (TREE_TYPE (oldtype),
984 TREE_TYPE (newtype), COMPARE_STRICT)
985 && TYPE_ARG_TYPES (newtype) == 0))
987 error ("%Hconflicting types for '%D'",
988 &DECL_SOURCE_LOCATION (newdecl), newdecl);
989 /* Check for function type mismatch
990 involving an empty arglist vs a nonempty one. */
991 if (TREE_CODE (olddecl) == FUNCTION_DECL
992 && comptypes (TREE_TYPE (oldtype),
993 TREE_TYPE (newtype), COMPARE_STRICT)
994 && ((TYPE_ARG_TYPES (oldtype) == 0
995 && DECL_INITIAL (olddecl) == 0)
997 (TYPE_ARG_TYPES (newtype) == 0
998 && DECL_INITIAL (newdecl) == 0)))
1000 /* Classify the problem further. */
1001 tree t = TYPE_ARG_TYPES (oldtype);
1002 if (t == 0)
1003 t = TYPE_ARG_TYPES (newtype);
1004 for (; t; t = TREE_CHAIN (t))
1006 tree type = TREE_VALUE (t);
1008 if (TREE_CHAIN (t) == 0
1009 && TYPE_MAIN_VARIANT (type) != void_type_node)
1011 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1012 break;
1015 if (c_type_promotes_to (type) != type)
1017 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1018 break;
1022 if (C_DECL_IMPLICIT (olddecl))
1023 error ("%Hprevious implicit declaration of '%D'",
1024 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1025 else
1026 error ("%Hprevious declaration of '%D'",
1027 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1029 /* This is safer because the initializer might contain references
1030 to variables that were declared between olddecl and newdecl. This
1031 will make the initializer invalid for olddecl in case it gets
1032 assigned to olddecl below. */
1033 if (TREE_CODE (newdecl) == VAR_DECL)
1034 DECL_INITIAL (newdecl) = 0;
1036 /* TLS cannot follow non-TLS declaration. */
1037 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1038 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1040 error ("%Hthread-local declaration of '%D' follows non thread-local "
1041 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1042 error ("%Hprevious declaration of '%D'",
1043 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1045 /* non-TLS declaration cannot follow TLS declaration. */
1046 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1047 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1049 error ("%Hnon thread-local declaration of '%D' follows "
1050 "thread-local declaration",
1051 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1052 error ("%Hprevious declaration of '%D'",
1053 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1055 else
1057 errmsg = redeclaration_error_message (newdecl, olddecl);
1058 if (errmsg)
1060 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1061 switch (errmsg)
1063 case 1:
1064 error ("%Hredefinition of '%D'", locus, newdecl);
1065 break;
1066 case 2:
1067 error ("%Hredeclaration of '%D'", locus, newdecl);
1068 break;
1069 case 3:
1070 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1071 break;
1072 default:
1073 abort ();
1076 locus = &DECL_SOURCE_LOCATION (olddecl);
1077 if (DECL_INITIAL (olddecl)
1078 && current_scope == global_scope)
1079 error ("%H'%D' previously defined here", locus, olddecl);
1080 else
1081 error ("%H'%D' previously declared here", locus, olddecl);
1082 return 0;
1084 else if (TREE_CODE (newdecl) == TYPE_DECL
1085 && (DECL_IN_SYSTEM_HEADER (olddecl)
1086 || DECL_IN_SYSTEM_HEADER (newdecl)))
1088 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1089 warning ("%Hredefinition of '%D'", locus, newdecl);
1090 locus = &DECL_SOURCE_LOCATION (olddecl);
1091 if (DECL_INITIAL (olddecl)
1092 && current_scope == global_scope)
1093 warning ("%H'%D' previously defined here", locus, olddecl);
1094 else
1095 warning ("%H'%D' previously declared here", locus, olddecl);
1097 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1098 && DECL_INITIAL (olddecl) != 0
1099 && TYPE_ARG_TYPES (oldtype) == 0
1100 && TYPE_ARG_TYPES (newtype) != 0
1101 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1103 tree type, parm;
1104 int nargs;
1105 /* Prototype decl follows defn w/o prototype. */
1107 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1108 type = TYPE_ARG_TYPES (newtype),
1109 nargs = 1;
1111 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1113 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1114 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1116 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1117 warning ("%Hprototype for '%D' follows", locus, newdecl);
1118 locus = &DECL_SOURCE_LOCATION (olddecl);
1119 warning ("%Hnon-prototype definition here", locus);
1120 break;
1122 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1123 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1125 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1126 error ("%Hprototype for '%D' follows and number of "
1127 "arguments doesn't match", locus, newdecl);
1128 locus = &DECL_SOURCE_LOCATION (olddecl);
1129 error ("%Hnon-prototype definition here", locus);
1130 errmsg = 1;
1131 break;
1133 /* Type for passing arg must be consistent
1134 with that declared for the arg. */
1135 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1136 COMPARE_STRICT))
1138 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1139 error ("%Hprototype for '%D' follows and argument %d "
1140 "doesn't match", locus, newdecl, nargs);
1141 locus = &DECL_SOURCE_LOCATION (olddecl);
1142 error ("%Hnon-prototype definition here", locus);
1143 errmsg = 1;
1144 break;
1148 /* Warn about mismatches in various flags. */
1149 else
1151 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1153 /* Warn if function is now inline
1154 but was previously declared not inline and has been called. */
1155 if (TREE_CODE (olddecl) == FUNCTION_DECL
1156 && ! DECL_DECLARED_INLINE_P (olddecl)
1157 && DECL_DECLARED_INLINE_P (newdecl)
1158 && TREE_USED (olddecl))
1159 warning ("%H'%D' declared inline after being called",
1160 locus, newdecl);
1161 if (TREE_CODE (olddecl) == FUNCTION_DECL
1162 && ! DECL_DECLARED_INLINE_P (olddecl)
1163 && DECL_DECLARED_INLINE_P (newdecl)
1164 && DECL_INITIAL (olddecl) != 0)
1165 warning ("%H'%D' declared inline after its definition",
1166 locus, newdecl);
1168 /* If pedantic, warn when static declaration follows a non-static
1169 declaration. Otherwise, do so only for functions. */
1170 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1171 && TREE_PUBLIC (olddecl)
1172 && !TREE_PUBLIC (newdecl))
1173 warning ("%Hstatic declaration for '%D' follows non-static",
1174 locus, newdecl);
1176 /* If warn_traditional, warn when a non-static function
1177 declaration follows a static one. */
1178 if (warn_traditional && !in_system_header
1179 && TREE_CODE (olddecl) == FUNCTION_DECL
1180 && !TREE_PUBLIC (olddecl)
1181 && TREE_PUBLIC (newdecl))
1182 warning ("%Hnon-static declaration for '%D' follows static",
1183 locus, newdecl);
1185 /* Warn when const declaration follows a non-const
1186 declaration, but not for functions. */
1187 if (TREE_CODE (olddecl) != FUNCTION_DECL
1188 && !TREE_READONLY (olddecl)
1189 && TREE_READONLY (newdecl))
1190 warning ("%Hconst declaration for '%D' follows non-const",
1191 locus, newdecl);
1192 /* These bits are logically part of the type, for variables.
1193 But not for functions
1194 (where qualifiers are not valid ANSI anyway). */
1195 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1196 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1197 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1198 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1199 "declaration", locus, newdecl);
1203 /* Optionally warn about more than one declaration for the same name. */
1204 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1205 /* Don't warn about a function declaration
1206 followed by a definition. */
1207 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1208 && DECL_INITIAL (olddecl) == 0)
1209 /* Don't warn about extern decl followed by (tentative) definition. */
1210 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1212 warning ("%Hredundant redeclaration of '%D' in same scope",
1213 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1214 warning ("%Hprevious declaration of '%D'",
1215 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1218 /* Copy all the DECL_... slots specified in the new decl
1219 except for any that we copy here from the old type.
1221 Past this point, we don't change OLDTYPE and NEWTYPE
1222 even if we change the types of NEWDECL and OLDDECL. */
1224 if (types_match)
1226 /* When copying info to olddecl, we store into write_olddecl
1227 instead. This allows us to avoid modifying olddecl when
1228 different_binding_level is true. */
1229 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1231 /* Merge the data types specified in the two decls. */
1232 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1234 if (different_binding_level)
1236 if (TYPE_ARG_TYPES (oldtype) != 0
1237 && TYPE_ARG_TYPES (newtype) == 0)
1238 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1239 else
1240 TREE_TYPE (newdecl)
1241 = build_type_attribute_variant
1242 (newtype,
1243 merge_attributes (TYPE_ATTRIBUTES (newtype),
1244 TYPE_ATTRIBUTES (oldtype)));
1246 else
1247 TREE_TYPE (newdecl)
1248 = TREE_TYPE (olddecl)
1249 = common_type (newtype, oldtype);
1252 /* Lay the type out, unless already done. */
1253 if (oldtype != TREE_TYPE (newdecl))
1255 if (TREE_TYPE (newdecl) != error_mark_node)
1256 layout_type (TREE_TYPE (newdecl));
1257 if (TREE_CODE (newdecl) != FUNCTION_DECL
1258 && TREE_CODE (newdecl) != TYPE_DECL
1259 && TREE_CODE (newdecl) != CONST_DECL)
1260 layout_decl (newdecl, 0);
1262 else
1264 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1265 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1266 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1267 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1268 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1269 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1271 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1272 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1276 /* Keep the old rtl since we can safely use it. */
1277 COPY_DECL_RTL (olddecl, newdecl);
1279 /* Merge the type qualifiers. */
1280 if (TREE_READONLY (newdecl))
1281 TREE_READONLY (write_olddecl) = 1;
1283 if (TREE_THIS_VOLATILE (newdecl))
1285 TREE_THIS_VOLATILE (write_olddecl) = 1;
1286 if (TREE_CODE (newdecl) == VAR_DECL
1287 /* If an automatic variable is re-declared in the same
1288 function scope, but the old declaration was not
1289 volatile, make_var_volatile() would crash because the
1290 variable would have been assigned to a pseudo, not a
1291 MEM. Since this duplicate declaration is invalid
1292 anyway, we just skip the call. */
1293 && errmsg == 0)
1294 make_var_volatile (newdecl);
1297 /* Keep source location of definition rather than declaration. */
1298 /* When called with different_binding_level set, keep the old
1299 information so that meaningful diagnostics can be given. */
1300 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1301 && ! different_binding_level)
1303 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1304 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1307 /* Merge the unused-warning information. */
1308 if (DECL_IN_SYSTEM_HEADER (olddecl))
1309 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1310 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1311 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1313 /* Merge the initialization information. */
1314 /* When called with different_binding_level set, don't copy over
1315 DECL_INITIAL, so that we don't accidentally change function
1316 declarations into function definitions. */
1317 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1318 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1320 /* Merge the section attribute.
1321 We want to issue an error if the sections conflict but that must be
1322 done later in decl_attributes since we are called before attributes
1323 are assigned. */
1324 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1325 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1327 /* Copy the assembler name.
1328 Currently, it can only be defined in the prototype. */
1329 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1331 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1333 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1334 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1335 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1336 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1337 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1338 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1339 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1340 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1341 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1344 /* If cannot merge, then use the new type and qualifiers,
1345 and don't preserve the old rtl. */
1346 else if (! different_binding_level)
1348 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1349 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1350 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1351 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1354 /* Merge the storage class information. */
1355 merge_weak (newdecl, olddecl);
1357 /* For functions, static overrides non-static. */
1358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1360 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1361 /* This is since we don't automatically
1362 copy the attributes of NEWDECL into OLDDECL. */
1363 /* No need to worry about different_binding_level here because
1364 then TREE_PUBLIC (newdecl) was true. */
1365 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1366 /* If this clears `static', clear it in the identifier too. */
1367 if (! TREE_PUBLIC (olddecl))
1368 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1370 if (DECL_EXTERNAL (newdecl))
1372 if (! different_binding_level || different_tu)
1374 /* Don't mess with these flags on local externs; they remain
1375 external even if there's a declaration at file scope which
1376 isn't. */
1377 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1378 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1380 /* An extern decl does not override previous storage class. */
1381 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1382 if (! DECL_EXTERNAL (newdecl))
1384 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1385 /* If we have two non-EXTERNAL file-scope decls that are
1386 the same, only one of them should be written out. */
1387 if (different_tu)
1388 TREE_ASM_WRITTEN (newdecl) = 1;
1391 else
1393 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1394 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1397 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1399 /* If we're redefining a function previously defined as extern
1400 inline, make sure we emit debug info for the inline before we
1401 throw it away, in case it was inlined into a function that hasn't
1402 been written out yet. */
1403 if (new_is_definition && DECL_INITIAL (olddecl))
1405 if (TREE_USED (olddecl))
1406 (*debug_hooks->outlining_inline_function) (olddecl);
1408 /* The new defn must not be inline. */
1409 DECL_INLINE (newdecl) = 0;
1410 DECL_UNINLINABLE (newdecl) = 1;
1412 else
1414 /* If either decl says `inline', this fn is inline,
1415 unless its definition was passed already. */
1416 if (DECL_DECLARED_INLINE_P (newdecl)
1417 || DECL_DECLARED_INLINE_P (olddecl))
1418 DECL_DECLARED_INLINE_P (newdecl) = 1;
1420 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1421 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1424 if (DECL_BUILT_IN (olddecl))
1426 /* Get rid of any built-in function if new arg types don't match it
1427 or if we have a function definition. */
1428 if (! types_match || new_is_definition)
1430 if (! different_binding_level)
1432 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1433 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1436 else
1438 /* If redeclaring a builtin function, and not a definition,
1439 it stays built in. */
1440 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1441 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1445 /* Also preserve various other info from the definition. */
1446 if (! new_is_definition)
1448 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1449 /* When called with different_binding_level set, don't copy over
1450 DECL_INITIAL, so that we don't accidentally change function
1451 declarations into function definitions. */
1452 if (! different_binding_level)
1453 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1454 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1455 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1456 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1457 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1459 /* Set DECL_INLINE on the declaration if we've got a body
1460 from which to instantiate. */
1461 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1463 DECL_INLINE (newdecl) = 1;
1464 DECL_ABSTRACT_ORIGIN (newdecl)
1465 = (different_binding_level
1466 ? DECL_ORIGIN (olddecl)
1467 : DECL_ABSTRACT_ORIGIN (olddecl));
1470 else
1472 /* If a previous declaration said inline, mark the
1473 definition as inlinable. */
1474 if (DECL_DECLARED_INLINE_P (newdecl)
1475 && ! DECL_UNINLINABLE (newdecl))
1476 DECL_INLINE (newdecl) = 1;
1479 if (different_binding_level)
1480 return 0;
1482 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1483 But preserve OLDDECL's DECL_UID. */
1485 unsigned olddecl_uid = DECL_UID (olddecl);
1487 memcpy ((char *) olddecl + sizeof (struct tree_common),
1488 (char *) newdecl + sizeof (struct tree_common),
1489 sizeof (struct tree_decl) - sizeof (struct tree_common));
1490 DECL_UID (olddecl) = olddecl_uid;
1493 /* NEWDECL contains the merged attribute lists.
1494 Update OLDDECL to be the same. */
1495 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1497 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1498 so that encode_section_info has a chance to look at the new decl
1499 flags and attributes. */
1500 if (DECL_RTL_SET_P (olddecl)
1501 && (TREE_CODE (olddecl) == FUNCTION_DECL
1502 || (TREE_CODE (olddecl) == VAR_DECL
1503 && TREE_STATIC (olddecl))))
1504 make_decl_rtl (olddecl, NULL);
1506 return 1;
1509 /* Return any external DECL associated with ID, whether or not it is
1510 currently in scope. */
1512 static tree
1513 any_external_decl (tree id)
1515 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1516 tree t;
1518 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1519 return 0;
1520 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1521 return decl;
1523 t = purpose_member (id, truly_local_externals);
1524 if (t)
1525 return TREE_VALUE (t);
1527 return 0;
1530 /* Record an external decl DECL. This only does something if a
1531 shadowing decl already exists. */
1532 static void
1533 record_external_decl (tree decl)
1535 tree name = DECL_NAME (decl);
1536 if (!IDENTIFIER_SYMBOL_VALUE (name))
1537 return;
1539 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1542 /* Check whether decl-node X shadows an existing declaration.
1543 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1544 which might be a NULL_TREE. */
1545 static void
1546 warn_if_shadowing (tree x, tree old)
1548 const char *name;
1550 /* Nothing to shadow? */
1551 if (old == 0
1552 /* Shadow warnings not wanted? */
1553 || !warn_shadow
1554 /* No shadow warnings for internally generated vars. */
1555 || DECL_SOURCE_LINE (x) == 0
1556 /* No shadow warnings for vars made for inlining. */
1557 || DECL_FROM_INLINE (x)
1558 /* Don't warn about the parm names in function declarator
1559 within a function declarator.
1560 It would be nice to avoid warning in any function
1561 declarator in a declaration, as opposed to a definition,
1562 but there is no way to tell it's not a definition. */
1563 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1564 return;
1566 name = IDENTIFIER_POINTER (DECL_NAME (x));
1567 if (TREE_CODE (old) == PARM_DECL)
1568 shadow_warning (SW_PARAM, name, old);
1569 else if (DECL_FILE_SCOPE_P (old))
1570 shadow_warning (SW_GLOBAL, name, old);
1571 else
1572 shadow_warning (SW_LOCAL, name, old);
1576 /* Subroutine of pushdecl.
1578 X is a TYPE_DECL for a typedef statement. Create a brand new
1579 ..._TYPE node (which will be just a variant of the existing
1580 ..._TYPE node with identical properties) and then install X
1581 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1583 The whole point here is to end up with a situation where each
1584 and every ..._TYPE node the compiler creates will be uniquely
1585 associated with AT MOST one node representing a typedef name.
1586 This way, even though the compiler substitutes corresponding
1587 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1588 early on, later parts of the compiler can always do the reverse
1589 translation and get back the corresponding typedef name. For
1590 example, given:
1592 typedef struct S MY_TYPE;
1593 MY_TYPE object;
1595 Later parts of the compiler might only know that `object' was of
1596 type `struct S' if it were not for code just below. With this
1597 code however, later parts of the compiler see something like:
1599 struct S' == struct S
1600 typedef struct S' MY_TYPE;
1601 struct S' object;
1603 And they can then deduce (from the node for type struct S') that
1604 the original object declaration was:
1606 MY_TYPE object;
1608 Being able to do this is important for proper support of protoize,
1609 and also for generating precise symbolic debugging information
1610 which takes full account of the programmer's (typedef) vocabulary.
1612 Obviously, we don't want to generate a duplicate ..._TYPE node if
1613 the TYPE_DECL node that we are now processing really represents a
1614 standard built-in type.
1616 Since all standard types are effectively declared at line zero
1617 in the source file, we can easily check to see if we are working
1618 on a standard type by checking the current value of lineno. */
1620 static void
1621 clone_underlying_type (tree x)
1623 if (DECL_SOURCE_LINE (x) == 0)
1625 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1626 TYPE_NAME (TREE_TYPE (x)) = x;
1628 else if (TREE_TYPE (x) != error_mark_node
1629 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1631 tree tt = TREE_TYPE (x);
1632 DECL_ORIGINAL_TYPE (x) = tt;
1633 tt = build_type_copy (tt);
1634 TYPE_NAME (tt) = x;
1635 TREE_USED (tt) = TREE_USED (x);
1636 TREE_TYPE (x) = tt;
1640 /* Record a decl-node X as belonging to the current lexical scope.
1641 Check for errors (such as an incompatible declaration for the same
1642 name already seen in the same scope).
1644 Returns either X or an old decl for the same name.
1645 If an old decl is returned, it may have been smashed
1646 to agree with what X says. */
1648 tree
1649 pushdecl (tree x)
1651 tree name = DECL_NAME (x);
1652 struct c_scope *scope = current_scope;
1654 #ifdef ENABLE_CHECKING
1655 if (error_mark_node == 0)
1656 /* Called too early. */
1657 abort ();
1658 #endif
1660 /* Functions need the lang_decl data. */
1661 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1662 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1664 /* A local extern declaration for a function doesn't constitute nesting.
1665 A local auto declaration does, since it's a forward decl
1666 for a nested function coming later. */
1667 if (current_function_decl == NULL
1668 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1669 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1670 DECL_CONTEXT (x) = current_file_decl;
1671 else
1672 DECL_CONTEXT (x) = current_function_decl;
1674 if (name)
1676 tree old;
1678 if (warn_nested_externs
1679 && scope != global_scope
1680 && DECL_EXTERNAL (x)
1681 && !DECL_IN_SYSTEM_HEADER (x))
1682 warning ("nested extern declaration of `%s'",
1683 IDENTIFIER_POINTER (name));
1685 old = lookup_name_current_level (name);
1686 if (old && duplicate_decls (x, old, 0, false))
1688 /* For PARM_DECLs, old may be a forward declaration.
1689 If so, we want to remove it from its old location
1690 (in the variables chain) and rechain it in the
1691 location given by the new declaration. */
1692 if (TREE_CODE (x) == PARM_DECL)
1694 tree *p;
1695 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1696 if (*p == old)
1698 *p = TREE_CHAIN (old);
1699 SCOPE_LIST_APPEND (scope, parms, old);
1700 break;
1703 return old;
1705 if (DECL_EXTERNAL (x) || scope == global_scope)
1707 /* Find and check against a previous, not-in-scope, external
1708 decl for this identifier. (C99 s???: If two declarations
1709 with external linkage, referring to the same object, have
1710 incompatible types, the behavior is undefined). */
1711 tree ext = any_external_decl (name);
1712 if (ext)
1714 if (duplicate_decls (x, ext, scope != global_scope,
1715 false))
1716 x = copy_node (ext);
1718 else
1719 record_external_decl (x);
1722 if (TREE_CODE (x) == TYPE_DECL)
1723 clone_underlying_type (x);
1725 /* If storing a local value, there may already be one
1726 (inherited). If so, record it for restoration when this
1727 scope ends. Take care not to do this if we are replacing an
1728 older decl in the same scope (i.e. duplicate_decls returned
1729 false, above). */
1730 if (scope != global_scope
1731 && IDENTIFIER_SYMBOL_VALUE (name)
1732 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1734 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1735 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1736 scope->shadowed);
1739 /* Install the new declaration in the requested scope. */
1740 IDENTIFIER_SYMBOL_VALUE (name) = x;
1741 C_DECL_INVISIBLE (x) = 0;
1743 /* If x's type is incomplete because it's based on a
1744 structure or union which has not yet been fully declared,
1745 attach it to that structure or union type, so we can go
1746 back and complete the variable declaration later, if the
1747 structure or union gets fully declared.
1749 If the input is erroneous, we can have error_mark in the type
1750 slot (e.g. "f(void a, ...)") - that doesn't count as an
1751 incomplete type. */
1752 if (TREE_TYPE (x) != error_mark_node
1753 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1755 tree element = TREE_TYPE (x);
1757 while (TREE_CODE (element) == ARRAY_TYPE)
1758 element = TREE_TYPE (element);
1759 element = TYPE_MAIN_VARIANT (element);
1761 if ((TREE_CODE (element) == RECORD_TYPE
1762 || TREE_CODE (element) == UNION_TYPE)
1763 && (TREE_CODE (x) != TYPE_DECL
1764 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1765 && !COMPLETE_TYPE_P (element))
1766 C_TYPE_INCOMPLETE_VARS (element)
1767 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1771 if (TREE_CODE (x) == PARM_DECL)
1772 SCOPE_LIST_APPEND (scope, parms, x);
1773 else
1774 SCOPE_LIST_APPEND (scope, names, x);
1776 return x;
1779 /* Record X as belonging to the global scope (C99 "file scope").
1780 This is used only internally by the Objective-C front end,
1781 and is limited to its needs. duplicate_decls is not called;
1782 if there is any preexisting decl for this identifier, it is an ICE. */
1784 tree
1785 pushdecl_top_level (tree x)
1787 tree name;
1789 if (TREE_CODE (x) != VAR_DECL)
1790 abort ();
1792 name = DECL_NAME (x);
1794 if (IDENTIFIER_SYMBOL_VALUE (name))
1795 abort ();
1797 DECL_CONTEXT (x) = current_file_decl;
1798 IDENTIFIER_SYMBOL_VALUE (name) = x;
1800 SCOPE_LIST_APPEND (global_scope, names, x);
1801 return x;
1804 /* Generate an implicit declaration for identifier FUNCTIONID as a
1805 function of type int (). */
1807 tree
1808 implicitly_declare (tree functionid)
1810 tree decl = any_external_decl (functionid);
1812 if (decl)
1814 /* Implicit declaration of a function already declared
1815 (somehow) in a different scope, or as a built-in.
1816 If this is the first time this has happened, warn;
1817 then recycle the old declaration. */
1818 if (!C_DECL_IMPLICIT (decl))
1820 implicit_decl_warning (DECL_NAME (decl));
1821 if (! DECL_FILE_SCOPE_P (decl))
1822 warning ("%Hprevious declaration of '%D'",
1823 &DECL_SOURCE_LOCATION (decl), decl);
1824 C_DECL_IMPLICIT (decl) = 1;
1826 /* If this function is global, then it must already be in the
1827 global scope, so there's no need to push it again. */
1828 if (current_scope == global_scope)
1829 return decl;
1830 /* If this is a local declaration, make a copy; we can't have
1831 the same DECL listed in two different scopes. */
1832 return pushdecl (copy_node (decl));
1835 /* Not seen before. */
1836 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1837 DECL_EXTERNAL (decl) = 1;
1838 TREE_PUBLIC (decl) = 1;
1839 C_DECL_IMPLICIT (decl) = 1;
1840 implicit_decl_warning (functionid);
1842 /* C89 says implicit declarations are in the innermost block.
1843 So we record the decl in the standard fashion. */
1844 decl = pushdecl (decl);
1846 /* No need to call objc_check_decl here - it's a function type. */
1847 rest_of_decl_compilation (decl, NULL, 0, 0);
1849 /* Write a record describing this implicit function declaration
1850 to the prototypes file (if requested). */
1851 gen_aux_info_record (decl, 0, 1, 0);
1853 /* Possibly apply some default attributes to this implicit declaration. */
1854 decl_attributes (&decl, NULL_TREE, 0);
1856 return decl;
1859 static void
1860 implicit_decl_warning (tree id)
1862 const char *name = IDENTIFIER_POINTER (id);
1863 if (mesg_implicit_function_declaration == 2)
1864 error ("implicit declaration of function `%s'", name);
1865 else if (mesg_implicit_function_declaration == 1)
1866 warning ("implicit declaration of function `%s'", name);
1869 /* Return zero if the declaration NEWDECL is valid
1870 when the declaration OLDDECL (assumed to be for the same name)
1871 has already been seen.
1872 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1873 and 3 if it is a conflicting declaration. */
1875 static int
1876 redeclaration_error_message (tree newdecl, tree olddecl)
1878 if (TREE_CODE (newdecl) == TYPE_DECL)
1880 /* Do not complain about type redeclarations where at least one
1881 declaration was in a system header. */
1882 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1883 return 0;
1884 return 1;
1886 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1888 /* Declarations of functions can insist on internal linkage
1889 but they can't be inconsistent with internal linkage,
1890 so there can be no error on that account.
1891 However defining the same name twice is no good. */
1892 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1893 /* However, defining once as extern inline and a second
1894 time in another way is ok. */
1895 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1896 && ! (DECL_DECLARED_INLINE_P (newdecl)
1897 && DECL_EXTERNAL (newdecl))))
1898 return 1;
1899 return 0;
1901 else if (DECL_FILE_SCOPE_P (newdecl))
1903 /* Objects declared at file scope: */
1904 /* If at least one is a reference, it's ok. */
1905 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1906 return 0;
1907 /* Reject two definitions. */
1908 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1909 return 1;
1910 /* Now we have two tentative defs, or one tentative and one real def. */
1911 /* Insist that the linkage match. */
1912 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1913 return 3;
1914 return 0;
1916 else if (current_scope->parm_flag
1917 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1918 return 0;
1919 else
1921 /* Newdecl has block scope. If olddecl has block scope also, then
1922 reject two definitions, and reject a definition together with an
1923 external reference. Otherwise, it is OK, because newdecl must
1924 be an extern reference to olddecl. */
1925 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1926 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1927 return 2;
1928 return 0;
1932 /* Issue an error message for a reference to an undeclared variable
1933 ID, including a reference to a builtin outside of function-call
1934 context. Establish a binding of the identifier to error_mark_node
1935 in an appropriate scope, which will suppress further errors for the
1936 same identifier. */
1937 void
1938 undeclared_variable (tree id)
1940 static bool already = false;
1941 struct c_scope *scope;
1943 if (current_function_decl == 0)
1945 error ("`%s' undeclared here (not in a function)",
1946 IDENTIFIER_POINTER (id));
1947 scope = current_scope;
1949 else
1951 error ("`%s' undeclared (first use in this function)",
1952 IDENTIFIER_POINTER (id));
1954 if (! already)
1956 error ("(Each undeclared identifier is reported only once");
1957 error ("for each function it appears in.)");
1958 already = true;
1961 scope = current_function_scope;
1964 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1965 scope->shadowed);
1966 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1969 /* Subroutine of lookup_label, declare_label, define_label: construct a
1970 LABEL_DECL with all the proper frills. */
1972 static tree
1973 make_label (tree name, location_t location)
1975 tree label = build_decl (LABEL_DECL, name, void_type_node);
1977 DECL_CONTEXT (label) = current_function_decl;
1978 DECL_MODE (label) = VOIDmode;
1979 DECL_SOURCE_LOCATION (label) = location;
1981 return label;
1984 /* Another subroutine of lookup_label, declare_label, define_label:
1985 set up the binding of name to LABEL_DECL in the given SCOPE. */
1987 static void
1988 bind_label (tree name, tree label, struct c_scope *scope)
1990 if (IDENTIFIER_LABEL_VALUE (name))
1991 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1992 scope->shadowed);
1993 IDENTIFIER_LABEL_VALUE (name) = label;
1995 SCOPE_LIST_APPEND (scope, names, label);
1998 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1999 Create one if none exists so far for the current function.
2000 This is called when a label is used in a goto expression or
2001 has its address taken. */
2003 tree
2004 lookup_label (tree name)
2006 tree label;
2008 if (current_function_decl == 0)
2010 error ("label %s referenced outside of any function",
2011 IDENTIFIER_POINTER (name));
2012 return 0;
2015 /* Use a label already defined or ref'd with this name, but not if
2016 it is inherited from a containing function and wasn't declared
2017 using __label__. */
2018 label = IDENTIFIER_LABEL_VALUE (name);
2019 if (label && (DECL_CONTEXT (label) == current_function_decl
2020 || C_DECLARED_LABEL_FLAG (label)))
2022 /* If the label has only been declared, update its apparent
2023 location to point here, for better diagnostics if it
2024 turns out not to have been defined. */
2025 if (!TREE_USED (label))
2026 DECL_SOURCE_LOCATION (label) = input_location;
2027 return label;
2030 /* No label binding for that identifier; make one. */
2031 label = make_label (name, input_location);
2033 /* Ordinary labels go in the current function scope. */
2034 bind_label (name, label, current_function_scope);
2035 return label;
2038 /* Make a label named NAME in the current function, shadowing silently
2039 any that may be inherited from containing functions or containing
2040 scopes. This is called for __label__ declarations. */
2042 /* Note that valid use, if the label being shadowed comes from another
2043 scope in the same function, requires calling declare_nonlocal_label
2044 right away. (Is this still true? -zw 2003-07-17) */
2046 tree
2047 declare_label (tree name)
2049 tree label = IDENTIFIER_LABEL_VALUE (name);
2050 tree dup;
2052 /* Check to make sure that the label hasn't already been declared
2053 at this scope */
2054 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2055 if (dup == label)
2057 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2058 error ("%Hthis is a previous declaration",
2059 &DECL_SOURCE_LOCATION (dup));
2061 /* Just use the previous declaration. */
2062 return dup;
2065 label = make_label (name, input_location);
2066 C_DECLARED_LABEL_FLAG (label) = 1;
2068 /* Declared labels go in the current scope. */
2069 bind_label (name, label, current_scope);
2070 return label;
2073 /* Define a label, specifying the location in the source file.
2074 Return the LABEL_DECL node for the label, if the definition is valid.
2075 Otherwise return 0. */
2077 tree
2078 define_label (location_t location, tree name)
2080 tree label;
2082 /* Find any preexisting label with this name. It is an error
2083 if that label has already been defined in this function, or
2084 if there is a containing function with a declared label with
2085 the same name. */
2086 label = IDENTIFIER_LABEL_VALUE (name);
2088 if (label
2089 && ((DECL_CONTEXT (label) == current_function_decl
2090 && DECL_INITIAL (label) != 0)
2091 || (DECL_CONTEXT (label) != current_function_decl
2092 && C_DECLARED_LABEL_FLAG (label))))
2094 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2095 error ("%Hduplicate label `%D'", &location, label);
2096 if (DECL_INITIAL (label))
2097 error ("%H`%D' previously defined here", prev_loc, label);
2098 else
2099 error ("%H`%D' previously declared here", prev_loc, label);
2100 return 0;
2102 else if (label && DECL_CONTEXT (label) == current_function_decl)
2104 /* The label has been used or declared already in this function,
2105 but not defined. Update its location to point to this
2106 definition. */
2107 DECL_SOURCE_LOCATION (label) = location;
2109 else
2111 /* No label binding for that identifier; make one. */
2112 label = make_label (name, location);
2114 /* Ordinary labels go in the current function scope. */
2115 bind_label (name, label, current_function_scope);
2118 if (warn_traditional && !in_system_header && lookup_name (name))
2119 warning ("%Htraditional C lacks a separate namespace for labels, "
2120 "identifier `%s' conflicts", &location,
2121 IDENTIFIER_POINTER (name));
2123 /* Mark label as having been defined. */
2124 DECL_INITIAL (label) = error_mark_node;
2125 return label;
2128 /* Return the list of declarations of the current scope. */
2130 tree
2131 getdecls (void)
2133 return current_scope->names;
2137 /* Given NAME, an IDENTIFIER_NODE,
2138 return the structure (or union or enum) definition for that name.
2139 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2140 CODE says which kind of type the caller wants;
2141 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2142 If the wrong kind of type is found, an error is reported. */
2144 static tree
2145 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2147 tree tag = IDENTIFIER_TAG_VALUE (name);
2148 int thislevel = 0;
2150 if (!tag)
2151 return 0;
2153 /* We only care about whether it's in this level if
2154 thislevel_only was set or it might be a type clash. */
2155 if (thislevel_only || TREE_CODE (tag) != code)
2157 if (current_scope == global_scope
2158 || purpose_member (name, current_scope->tags))
2159 thislevel = 1;
2162 if (thislevel_only && !thislevel)
2163 return 0;
2165 if (TREE_CODE (tag) != code)
2167 /* Definition isn't the kind we were looking for. */
2168 pending_invalid_xref = name;
2169 pending_invalid_xref_location = input_location;
2171 /* If in the same binding level as a declaration as a tag
2172 of a different type, this must not be allowed to
2173 shadow that tag, so give the error immediately.
2174 (For example, "struct foo; union foo;" is invalid.) */
2175 if (thislevel)
2176 pending_xref_error ();
2178 return tag;
2181 /* Print an error message now
2182 for a recent invalid struct, union or enum cross reference.
2183 We don't print them immediately because they are not invalid
2184 when used in the `struct foo;' construct for shadowing. */
2186 void
2187 pending_xref_error (void)
2189 if (pending_invalid_xref != 0)
2190 error ("%H`%s' defined as wrong kind of tag",
2191 &pending_invalid_xref_location,
2192 IDENTIFIER_POINTER (pending_invalid_xref));
2193 pending_invalid_xref = 0;
2197 /* Look up NAME in the current scope and its superiors
2198 in the namespace of variables, functions and typedefs.
2199 Return a ..._DECL node of some kind representing its definition,
2200 or return 0 if it is undefined. */
2202 tree
2203 lookup_name (tree name)
2205 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2206 if (decl == 0 || decl == error_mark_node)
2207 return decl;
2208 if (C_DECL_INVISIBLE (decl))
2209 return 0;
2210 return decl;
2213 /* Similar to `lookup_name' but look only at the current scope. */
2215 static tree
2216 lookup_name_current_level (tree name)
2218 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2220 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2221 return 0;
2223 if (current_scope == global_scope)
2224 return decl;
2226 /* Scan the current scope for a decl with name NAME.
2227 For PARM_DECLs, we have to look at both ->parms and ->names, since
2228 forward parameter declarations wind up on the ->names list. */
2229 if (TREE_CODE (decl) == PARM_DECL
2230 && chain_member (decl, current_scope->parms))
2231 return decl;
2232 if (chain_member (decl, current_scope->names))
2233 return decl;
2235 return 0;
2238 /* Create the predefined scalar types of C,
2239 and some nodes representing standard constants (0, 1, (void *) 0).
2240 Initialize the global scope.
2241 Make definitions for built-in primitive functions. */
2243 void
2244 c_init_decl_processing (void)
2246 tree endlink;
2247 tree ptr_ftype_void, ptr_ftype_ptr;
2248 location_t save_loc = input_location;
2250 /* Adds some ggc roots, and reserved words for c-parse.in. */
2251 c_parse_init ();
2253 current_function_decl = 0;
2255 /* Make the c_scope structure for global names. */
2256 pushlevel (0);
2257 global_scope = current_scope;
2259 /* Declarations from c_common_nodes_and_builtins must not be associated
2260 with this input file, lest we get differences between using and not
2261 using preprocessed headers. */
2262 input_location.file = "<internal>";
2263 input_location.line = 0;
2265 /* Make the DECL for the toplevel file scope. */
2266 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2268 build_common_tree_nodes (flag_signed_char);
2270 c_common_nodes_and_builtins ();
2272 /* In C, comparisons and TRUTH_* expressions have type int. */
2273 truthvalue_type_node = integer_type_node;
2274 truthvalue_true_node = integer_one_node;
2275 truthvalue_false_node = integer_zero_node;
2277 /* Even in C99, which has a real boolean type. */
2278 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2279 boolean_type_node));
2281 endlink = void_list_node;
2282 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2283 ptr_ftype_ptr
2284 = build_function_type (ptr_type_node,
2285 tree_cons (NULL_TREE, ptr_type_node, endlink));
2287 input_location = save_loc;
2289 pedantic_lvalues = pedantic;
2291 make_fname_decl = c_make_fname_decl;
2292 start_fname_decls ();
2294 first_builtin_decl = global_scope->names;
2295 last_builtin_decl = global_scope->names_last;
2298 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2299 decl, NAME is the initialization string and TYPE_DEP indicates whether
2300 NAME depended on the type of the function. As we don't yet implement
2301 delayed emission of static data, we mark the decl as emitted
2302 so it is not placed in the output. Anything using it must therefore pull
2303 out the STRING_CST initializer directly. FIXME. */
2305 static tree
2306 c_make_fname_decl (tree id, int type_dep)
2308 const char *name = fname_as_string (type_dep);
2309 tree decl, type, init;
2310 size_t length = strlen (name);
2312 type = build_array_type
2313 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2314 build_index_type (size_int (length)));
2316 decl = build_decl (VAR_DECL, id, type);
2318 TREE_STATIC (decl) = 1;
2319 TREE_READONLY (decl) = 1;
2320 DECL_ARTIFICIAL (decl) = 1;
2322 init = build_string (length + 1, name);
2323 TREE_TYPE (init) = type;
2324 DECL_INITIAL (decl) = init;
2326 TREE_USED (decl) = 1;
2328 if (current_function_decl)
2330 DECL_CONTEXT (decl) = current_function_decl;
2331 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2332 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2335 finish_decl (decl, init, NULL_TREE);
2337 return decl;
2340 /* Return a definition for a builtin function named NAME and whose data type
2341 is TYPE. TYPE should be a function type with argument types.
2342 FUNCTION_CODE tells later passes how to compile calls to this function.
2343 See tree.h for its possible values.
2345 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2346 the name to be called if we can't opencode the function. If
2347 ATTRS is nonzero, use that for the function's attribute list. */
2349 tree
2350 builtin_function (const char *name, tree type, int function_code,
2351 enum built_in_class class, const char *library_name,
2352 tree attrs)
2354 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2355 DECL_EXTERNAL (decl) = 1;
2356 TREE_PUBLIC (decl) = 1;
2357 if (library_name)
2358 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2359 make_decl_rtl (decl, NULL);
2360 pushdecl (decl);
2361 DECL_BUILT_IN_CLASS (decl) = class;
2362 DECL_FUNCTION_CODE (decl) = function_code;
2364 /* Warn if a function in the namespace for users
2365 is used without an occasion to consider it declared. */
2366 if (name[0] != '_' || name[1] != '_')
2367 C_DECL_INVISIBLE (decl) = 1;
2369 /* Possibly apply some default attributes to this built-in function. */
2370 if (attrs)
2371 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2372 else
2373 decl_attributes (&decl, NULL_TREE, 0);
2375 return decl;
2378 /* Called when a declaration is seen that contains no names to declare.
2379 If its type is a reference to a structure, union or enum inherited
2380 from a containing scope, shadow that tag name for the current scope
2381 with a forward reference.
2382 If its type defines a new named structure or union
2383 or defines an enum, it is valid but we need not do anything here.
2384 Otherwise, it is an error. */
2386 void
2387 shadow_tag (tree declspecs)
2389 shadow_tag_warned (declspecs, 0);
2392 void
2393 shadow_tag_warned (tree declspecs, int warned)
2396 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2397 no pedwarn. */
2399 int found_tag = 0;
2400 tree link;
2401 tree specs, attrs;
2403 pending_invalid_xref = 0;
2405 /* Remove the attributes from declspecs, since they will confuse the
2406 following code. */
2407 split_specs_attrs (declspecs, &specs, &attrs);
2409 for (link = specs; link; link = TREE_CHAIN (link))
2411 tree value = TREE_VALUE (link);
2412 enum tree_code code = TREE_CODE (value);
2414 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2415 /* Used to test also that TYPE_SIZE (value) != 0.
2416 That caused warning for `struct foo;' at top level in the file. */
2418 tree name = TYPE_NAME (value);
2419 tree t;
2421 found_tag++;
2423 if (name == 0)
2425 if (warned != 1 && code != ENUMERAL_TYPE)
2426 /* Empty unnamed enum OK */
2428 pedwarn ("unnamed struct/union that defines no instances");
2429 warned = 1;
2432 else
2434 t = lookup_tag (code, name, 1);
2436 if (t == 0)
2438 t = make_node (code);
2439 pushtag (name, t);
2443 else
2445 if (!warned && ! in_system_header)
2447 warning ("useless keyword or type name in empty declaration");
2448 warned = 2;
2453 if (found_tag > 1)
2454 error ("two types specified in one empty declaration");
2456 if (warned != 1)
2458 if (found_tag == 0)
2459 pedwarn ("empty declaration");
2463 /* Construct an array declarator. EXPR is the expression inside [], or
2464 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2465 to the pointer to which a parameter array is converted). STATIC_P is
2466 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2467 is nonzero is the array is [*], a VLA of unspecified length which is
2468 nevertheless a complete type (not currently implemented by GCC),
2469 zero otherwise. The declarator is constructed as an ARRAY_REF
2470 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2471 left of the [] (filled by in set_array_declarator_type) and operand 1
2472 is the expression inside; whose TREE_TYPE is the type qualifiers and
2473 which has TREE_STATIC set if "static" is used. */
2475 tree
2476 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2478 tree decl;
2479 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2480 TREE_TYPE (decl) = quals;
2481 TREE_STATIC (decl) = (static_p ? 1 : 0);
2482 if (pedantic && !flag_isoc99)
2484 if (static_p || quals != NULL_TREE)
2485 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2486 if (vla_unspec_p)
2487 pedwarn ("ISO C90 does not support `[*]' array declarators");
2489 if (vla_unspec_p)
2490 warning ("GCC does not yet properly implement `[*]' array declarators");
2491 return decl;
2494 /* Set the type of an array declarator. DECL is the declarator, as
2495 constructed by build_array_declarator; TYPE is what appears on the left
2496 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2497 abstract declarator, zero otherwise; this is used to reject static and
2498 type qualifiers in abstract declarators, where they are not in the
2499 C99 grammar. */
2501 tree
2502 set_array_declarator_type (tree decl, tree type, int abstract_p)
2504 TREE_OPERAND (decl, 0) = type;
2505 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2506 error ("static or type qualifiers in abstract declarator");
2507 return decl;
2510 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2512 tree
2513 groktypename (tree typename)
2515 tree specs, attrs;
2517 if (TREE_CODE (typename) != TREE_LIST)
2518 return typename;
2520 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2522 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2524 /* Apply attributes. */
2525 decl_attributes (&typename, attrs, 0);
2527 return typename;
2530 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2532 tree
2533 groktypename_in_parm_context (tree typename)
2535 if (TREE_CODE (typename) != TREE_LIST)
2536 return typename;
2537 return grokdeclarator (TREE_VALUE (typename),
2538 TREE_PURPOSE (typename),
2539 PARM, 0);
2542 /* Decode a declarator in an ordinary declaration or data definition.
2543 This is called as soon as the type information and variable name
2544 have been parsed, before parsing the initializer if any.
2545 Here we create the ..._DECL node, fill in its type,
2546 and put it on the list of decls for the current context.
2547 The ..._DECL node is returned as the value.
2549 Exception: for arrays where the length is not specified,
2550 the type is left null, to be filled in by `finish_decl'.
2552 Function definitions do not come here; they go to start_function
2553 instead. However, external and forward declarations of functions
2554 do go through here. Structure field declarations are done by
2555 grokfield and not through here. */
2557 tree
2558 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2560 tree decl;
2561 tree tem;
2563 /* An object declared as __attribute__((deprecated)) suppresses
2564 warnings of uses of other deprecated items. */
2565 if (lookup_attribute ("deprecated", attributes))
2566 deprecated_state = DEPRECATED_SUPPRESS;
2568 decl = grokdeclarator (declarator, declspecs,
2569 NORMAL, initialized);
2571 deprecated_state = DEPRECATED_NORMAL;
2573 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2574 && MAIN_NAME_P (DECL_NAME (decl)))
2575 warning ("%H'%D' is usually a function",
2576 &DECL_SOURCE_LOCATION (decl), decl);
2578 if (initialized)
2579 /* Is it valid for this decl to have an initializer at all?
2580 If not, set INITIALIZED to zero, which will indirectly
2581 tell `finish_decl' to ignore the initializer once it is parsed. */
2582 switch (TREE_CODE (decl))
2584 case TYPE_DECL:
2585 error ("typedef `%s' is initialized (use __typeof__ instead)",
2586 IDENTIFIER_POINTER (DECL_NAME (decl)));
2587 initialized = 0;
2588 break;
2590 case FUNCTION_DECL:
2591 error ("function `%s' is initialized like a variable",
2592 IDENTIFIER_POINTER (DECL_NAME (decl)));
2593 initialized = 0;
2594 break;
2596 case PARM_DECL:
2597 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2598 error ("parameter `%s' is initialized",
2599 IDENTIFIER_POINTER (DECL_NAME (decl)));
2600 initialized = 0;
2601 break;
2603 default:
2604 /* Don't allow initializations for incomplete types
2605 except for arrays which might be completed by the initialization. */
2607 /* This can happen if the array size is an undefined macro. We already
2608 gave a warning, so we don't need another one. */
2609 if (TREE_TYPE (decl) == error_mark_node)
2610 initialized = 0;
2611 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2613 /* A complete type is ok if size is fixed. */
2615 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2616 || C_DECL_VARIABLE_SIZE (decl))
2618 error ("variable-sized object may not be initialized");
2619 initialized = 0;
2622 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2624 error ("variable `%s' has initializer but incomplete type",
2625 IDENTIFIER_POINTER (DECL_NAME (decl)));
2626 initialized = 0;
2628 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2630 error ("elements of array `%s' have incomplete type",
2631 IDENTIFIER_POINTER (DECL_NAME (decl)));
2632 initialized = 0;
2636 if (initialized)
2638 DECL_EXTERNAL (decl) = 0;
2639 if (current_scope == global_scope)
2640 TREE_STATIC (decl) = 1;
2642 /* Tell `pushdecl' this is an initialized decl
2643 even though we don't yet have the initializer expression.
2644 Also tell `finish_decl' it may store the real initializer. */
2645 DECL_INITIAL (decl) = error_mark_node;
2648 /* If this is a function declaration, write a record describing it to the
2649 prototypes file (if requested). */
2651 if (TREE_CODE (decl) == FUNCTION_DECL)
2652 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2654 /* ANSI specifies that a tentative definition which is not merged with
2655 a non-tentative definition behaves exactly like a definition with an
2656 initializer equal to zero. (Section 3.7.2)
2658 -fno-common gives strict ANSI behavior, though this tends to break
2659 a large body of code that grew up without this rule.
2661 Thread-local variables are never common, since there's no entrenched
2662 body of code to break, and it allows more efficient variable references
2663 in the presence of dynamic linking. */
2665 if (TREE_CODE (decl) == VAR_DECL
2666 && !initialized
2667 && TREE_PUBLIC (decl)
2668 && !DECL_THREAD_LOCAL (decl)
2669 && !flag_no_common)
2670 DECL_COMMON (decl) = 1;
2672 /* Set attributes here so if duplicate decl, will have proper attributes. */
2673 decl_attributes (&decl, attributes, 0);
2675 if (TREE_CODE (decl) == FUNCTION_DECL
2676 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2678 tree ce = declarator;
2680 if (TREE_CODE (ce) == INDIRECT_REF)
2681 ce = TREE_OPERAND (declarator, 0);
2682 if (TREE_CODE (ce) == CALL_EXPR)
2684 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2685 for (; args; args = TREE_CHAIN (args))
2687 tree type = TREE_TYPE (args);
2688 if (INTEGRAL_TYPE_P (type)
2689 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2690 DECL_ARG_TYPE (args) = integer_type_node;
2695 if (TREE_CODE (decl) == FUNCTION_DECL
2696 && DECL_DECLARED_INLINE_P (decl)
2697 && DECL_UNINLINABLE (decl)
2698 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2699 warning ("%Hinline function '%D' given attribute noinline",
2700 &DECL_SOURCE_LOCATION (decl), decl);
2702 /* Add this decl to the current scope.
2703 TEM may equal DECL or it may be a previous decl of the same name. */
2704 tem = pushdecl (decl);
2706 /* For a local variable, define the RTL now. */
2707 if (current_scope != global_scope
2708 /* But not if this is a duplicate decl
2709 and we preserved the rtl from the previous one
2710 (which may or may not happen). */
2711 && !DECL_RTL_SET_P (tem)
2712 && DECL_FILE_SCOPE_P (tem))
2714 if (TREE_TYPE (tem) != error_mark_node
2715 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2716 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2717 && DECL_INITIAL (tem) != 0)))
2718 expand_decl (tem);
2721 return tem;
2724 /* Finish processing of a declaration;
2725 install its initial value.
2726 If the length of an array type is not known before,
2727 it must be determined now, from the initial value, or it is an error. */
2729 void
2730 finish_decl (tree decl, tree init, tree asmspec_tree)
2732 tree type = TREE_TYPE (decl);
2733 int was_incomplete = (DECL_SIZE (decl) == 0);
2734 const char *asmspec = 0;
2736 /* If a name was specified, get the string. */
2737 if (current_scope == global_scope)
2738 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2739 if (asmspec_tree)
2740 asmspec = TREE_STRING_POINTER (asmspec_tree);
2742 /* If `start_decl' didn't like having an initialization, ignore it now. */
2743 if (init != 0 && DECL_INITIAL (decl) == 0)
2744 init = 0;
2746 /* Don't crash if parm is initialized. */
2747 if (TREE_CODE (decl) == PARM_DECL)
2748 init = 0;
2750 if (init)
2751 store_init_value (decl, init);
2753 /* Deduce size of array from initialization, if not already known */
2754 if (TREE_CODE (type) == ARRAY_TYPE
2755 && TYPE_DOMAIN (type) == 0
2756 && TREE_CODE (decl) != TYPE_DECL)
2758 int do_default
2759 = (TREE_STATIC (decl)
2760 /* Even if pedantic, an external linkage array
2761 may have incomplete type at first. */
2762 ? pedantic && !TREE_PUBLIC (decl)
2763 : !DECL_EXTERNAL (decl));
2764 int failure
2765 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2767 /* Get the completed type made by complete_array_type. */
2768 type = TREE_TYPE (decl);
2770 if (failure == 1)
2771 error ("%Hinitializer fails to determine size of '%D'",
2772 &DECL_SOURCE_LOCATION (decl), decl);
2774 else if (failure == 2)
2776 if (do_default)
2777 error ("%Harray size missing in '%D'",
2778 &DECL_SOURCE_LOCATION (decl), decl);
2779 /* If a `static' var's size isn't known,
2780 make it extern as well as static, so it does not get
2781 allocated.
2782 If it is not `static', then do not mark extern;
2783 finish_incomplete_decl will give it a default size
2784 and it will get allocated. */
2785 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2786 DECL_EXTERNAL (decl) = 1;
2789 /* TYPE_MAX_VALUE is always one less than the number of elements
2790 in the array, because we start counting at zero. Therefore,
2791 warn only if the value is less than zero. */
2792 else if (pedantic && TYPE_DOMAIN (type) != 0
2793 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2794 error ("%Hzero or negative size array '%D'",
2795 &DECL_SOURCE_LOCATION (decl), decl);
2797 layout_decl (decl, 0);
2800 if (TREE_CODE (decl) == VAR_DECL)
2802 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2803 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2804 layout_decl (decl, 0);
2806 if (DECL_SIZE (decl) == 0
2807 /* Don't give an error if we already gave one earlier. */
2808 && TREE_TYPE (decl) != error_mark_node
2809 && (TREE_STATIC (decl)
2811 /* A static variable with an incomplete type
2812 is an error if it is initialized.
2813 Also if it is not file scope.
2814 Otherwise, let it through, but if it is not `extern'
2815 then it may cause an error message later. */
2816 (DECL_INITIAL (decl) != 0
2817 || !DECL_FILE_SCOPE_P (decl))
2819 /* An automatic variable with an incomplete type
2820 is an error. */
2821 !DECL_EXTERNAL (decl)))
2823 error ("%Hstorage size of '%D' isn't known",
2824 &DECL_SOURCE_LOCATION (decl), decl);
2825 TREE_TYPE (decl) = error_mark_node;
2828 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2829 && DECL_SIZE (decl) != 0)
2831 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2832 constant_expression_warning (DECL_SIZE (decl));
2833 else
2834 error ("%Hstorage size of '%D' isn't constant",
2835 &DECL_SOURCE_LOCATION (decl), decl);
2838 if (TREE_USED (type))
2839 TREE_USED (decl) = 1;
2842 /* If this is a function and an assembler name is specified, reset DECL_RTL
2843 so we can give it its new name. Also, update built_in_decls if it
2844 was a normal built-in. */
2845 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2847 /* ASMSPEC is given, and not the name of a register. Mark the
2848 name with a star so assemble_name won't munge it. */
2849 char *starred = alloca (strlen (asmspec) + 2);
2850 starred[0] = '*';
2851 strcpy (starred + 1, asmspec);
2853 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2855 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2856 SET_DECL_RTL (builtin, NULL_RTX);
2857 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2858 #ifdef TARGET_MEM_FUNCTIONS
2859 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2860 init_block_move_fn (starred);
2861 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2862 init_block_clear_fn (starred);
2863 #else
2864 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2865 init_block_move_fn (starred);
2866 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2867 init_block_clear_fn (starred);
2868 #endif
2870 SET_DECL_RTL (decl, NULL_RTX);
2871 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2874 /* If #pragma weak was used, mark the decl weak now. */
2875 if (current_scope == global_scope)
2876 maybe_apply_pragma_weak (decl);
2878 /* Output the assembler code and/or RTL code for variables and functions,
2879 unless the type is an undefined structure or union.
2880 If not, it will get done when the type is completed. */
2882 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2884 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2885 if (c_dialect_objc ())
2886 objc_check_decl (decl);
2888 if (DECL_FILE_SCOPE_P (decl))
2890 if (DECL_INITIAL (decl) == NULL_TREE
2891 || DECL_INITIAL (decl) == error_mark_node)
2892 /* Don't output anything
2893 when a tentative file-scope definition is seen.
2894 But at end of compilation, do output code for them. */
2895 DECL_DEFER_OUTPUT (decl) = 1;
2896 rest_of_decl_compilation (decl, asmspec, true, 0);
2898 else
2900 /* This is a local variable. If there is an ASMSPEC, the
2901 user has requested that we handle it specially. */
2902 if (asmspec)
2904 /* In conjunction with an ASMSPEC, the `register'
2905 keyword indicates that we should place the variable
2906 in a particular register. */
2907 if (DECL_REGISTER (decl))
2908 DECL_C_HARD_REGISTER (decl) = 1;
2910 /* If this is not a static variable, issue a warning.
2911 It doesn't make any sense to give an ASMSPEC for an
2912 ordinary, non-register local variable. Historically,
2913 GCC has accepted -- but ignored -- the ASMSPEC in
2914 this case. */
2915 if (TREE_CODE (decl) == VAR_DECL
2916 && !DECL_REGISTER (decl)
2917 && !TREE_STATIC (decl))
2918 warning ("%Hignoring asm-specifier for non-static local "
2919 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2920 else
2921 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2924 if (TREE_CODE (decl) != FUNCTION_DECL)
2925 add_decl_stmt (decl);
2928 if (!DECL_FILE_SCOPE_P (decl))
2930 /* Recompute the RTL of a local array now
2931 if it used to be an incomplete type. */
2932 if (was_incomplete
2933 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2935 /* If we used it already as memory, it must stay in memory. */
2936 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2937 /* If it's still incomplete now, no init will save it. */
2938 if (DECL_SIZE (decl) == 0)
2939 DECL_INITIAL (decl) = 0;
2944 /* If this was marked 'used', be sure it will be output. */
2945 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2946 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2948 if (TREE_CODE (decl) == TYPE_DECL)
2950 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2951 if (c_dialect_objc ())
2952 objc_check_decl (decl);
2953 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2956 /* At the end of a declaration, throw away any variable type sizes
2957 of types defined inside that declaration. There is no use
2958 computing them in the following function definition. */
2959 if (current_scope == global_scope)
2960 get_pending_sizes ();
2962 /* Install a cleanup (aka destructor) if one was given. */
2963 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2965 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2966 if (attr)
2968 static bool eh_initialized_p;
2970 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2971 tree cleanup_decl = lookup_name (cleanup_id);
2972 tree cleanup;
2974 /* Build "cleanup(&decl)" for the destructor. */
2975 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2976 cleanup = build_tree_list (NULL_TREE, cleanup);
2977 cleanup = build_function_call (cleanup_decl, cleanup);
2979 /* Don't warn about decl unused; the cleanup uses it. */
2980 TREE_USED (decl) = 1;
2982 /* Initialize EH, if we've been told to do so. */
2983 if (flag_exceptions && !eh_initialized_p)
2985 eh_initialized_p = true;
2986 eh_personality_libfunc
2987 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2988 ? "__gcc_personality_sj0"
2989 : "__gcc_personality_v0");
2990 using_eh_for_cleanups ();
2993 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2998 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2999 and push that on the current scope. */
3001 void
3002 push_parm_decl (tree parm)
3004 tree decl;
3006 /* Don't attempt to expand sizes while parsing this decl.
3007 (We can get here with i_s_e 1 somehow from Objective-C.) */
3008 int save_immediate_size_expand = immediate_size_expand;
3009 immediate_size_expand = 0;
3011 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3012 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
3013 decl_attributes (&decl, TREE_VALUE (parm), 0);
3015 decl = pushdecl (decl);
3017 finish_decl (decl, NULL_TREE, NULL_TREE);
3019 immediate_size_expand = save_immediate_size_expand;
3022 /* Mark all the parameter declarations to date as forward decls,
3023 shift them to the variables list, and reset the parameters list.
3024 Also diagnose use of this extension. */
3026 void
3027 mark_forward_parm_decls (void)
3029 tree parm;
3031 if (pedantic && !current_scope->warned_forward_parm_decls)
3033 pedwarn ("ISO C forbids forward parameter declarations");
3034 current_scope->warned_forward_parm_decls = true;
3037 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3038 TREE_ASM_WRITTEN (parm) = 1;
3040 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3041 current_scope->parms = 0;
3042 current_scope->parms_last = 0;
3045 static GTY(()) int compound_literal_number;
3047 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3048 literal, which may be an incomplete array type completed by the
3049 initializer; INIT is a CONSTRUCTOR that initializes the compound
3050 literal. */
3052 tree
3053 build_compound_literal (tree type, tree init)
3055 /* We do not use start_decl here because we have a type, not a declarator;
3056 and do not use finish_decl because the decl should be stored inside
3057 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3058 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3059 tree complit;
3060 tree stmt;
3061 DECL_EXTERNAL (decl) = 0;
3062 TREE_PUBLIC (decl) = 0;
3063 TREE_STATIC (decl) = (current_scope == global_scope);
3064 DECL_CONTEXT (decl) = current_function_decl;
3065 TREE_USED (decl) = 1;
3066 TREE_TYPE (decl) = type;
3067 TREE_READONLY (decl) = TREE_READONLY (type);
3068 store_init_value (decl, init);
3070 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3072 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3073 if (failure)
3074 abort ();
3077 type = TREE_TYPE (decl);
3078 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3079 return error_mark_node;
3081 stmt = build_stmt (DECL_STMT, decl);
3082 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3083 TREE_SIDE_EFFECTS (complit) = 1;
3085 layout_decl (decl, 0);
3087 if (TREE_STATIC (decl))
3089 /* This decl needs a name for the assembler output. We also need
3090 a unique suffix to be added to the name. */
3091 char *name;
3093 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3094 compound_literal_number);
3095 compound_literal_number++;
3096 DECL_NAME (decl) = get_identifier (name);
3097 DECL_DEFER_OUTPUT (decl) = 1;
3098 DECL_COMDAT (decl) = 1;
3099 DECL_ARTIFICIAL (decl) = 1;
3100 pushdecl (decl);
3101 rest_of_decl_compilation (decl, NULL, 1, 0);
3104 return complit;
3107 /* Make TYPE a complete type based on INITIAL_VALUE.
3108 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3109 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3112 complete_array_type (tree type, tree initial_value, int do_default)
3114 tree maxindex = NULL_TREE;
3115 int value = 0;
3117 if (initial_value)
3119 /* Note MAXINDEX is really the maximum index,
3120 one less than the size. */
3121 if (TREE_CODE (initial_value) == STRING_CST)
3123 int eltsize
3124 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3125 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3126 / eltsize) - 1, 0);
3128 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3130 tree elts = CONSTRUCTOR_ELTS (initial_value);
3131 maxindex = build_int_2 (-1, -1);
3132 for (; elts; elts = TREE_CHAIN (elts))
3134 if (TREE_PURPOSE (elts))
3135 maxindex = TREE_PURPOSE (elts);
3136 else
3137 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3138 maxindex, integer_one_node));
3140 maxindex = copy_node (maxindex);
3142 else
3144 /* Make an error message unless that happened already. */
3145 if (initial_value != error_mark_node)
3146 value = 1;
3148 /* Prevent further error messages. */
3149 maxindex = build_int_2 (0, 0);
3153 if (!maxindex)
3155 if (do_default)
3156 maxindex = build_int_2 (0, 0);
3157 value = 2;
3160 if (maxindex)
3162 TYPE_DOMAIN (type) = build_index_type (maxindex);
3163 if (!TREE_TYPE (maxindex))
3164 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3167 /* Lay out the type now that we can get the real answer. */
3169 layout_type (type);
3171 return value;
3174 /* Determine whether TYPE is a structure with a flexible array member,
3175 or a union containing such a structure (possibly recursively). */
3177 static bool
3178 flexible_array_type_p (tree type)
3180 tree x;
3181 switch (TREE_CODE (type))
3183 case RECORD_TYPE:
3184 x = TYPE_FIELDS (type);
3185 if (x == NULL_TREE)
3186 return false;
3187 while (TREE_CHAIN (x) != NULL_TREE)
3188 x = TREE_CHAIN (x);
3189 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3190 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3191 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3192 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3193 return true;
3194 return false;
3195 case UNION_TYPE:
3196 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3198 if (flexible_array_type_p (TREE_TYPE (x)))
3199 return true;
3201 return false;
3202 default:
3203 return false;
3207 /* Given declspecs and a declarator,
3208 determine the name and type of the object declared
3209 and construct a ..._DECL node for it.
3210 (In one case we can return a ..._TYPE node instead.
3211 For invalid input we sometimes return 0.)
3213 DECLSPECS is a chain of tree_list nodes whose value fields
3214 are the storage classes and type specifiers.
3216 DECL_CONTEXT says which syntactic context this declaration is in:
3217 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3218 FUNCDEF for a function definition. Like NORMAL but a few different
3219 error messages in each case. Return value may be zero meaning
3220 this definition is too screwy to try to parse.
3221 PARM for a parameter declaration (either within a function prototype
3222 or before a function body). Make a PARM_DECL, or return void_type_node.
3223 TYPENAME if for a typename (in a cast or sizeof).
3224 Don't make a DECL node; just return the ..._TYPE node.
3225 FIELD for a struct or union field; make a FIELD_DECL.
3226 BITFIELD for a field with specified width.
3227 INITIALIZED is 1 if the decl has an initializer.
3229 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3230 It may also be so in the PARM case, for a prototype where the
3231 argument type is specified but not the name.
3233 This function is where the complicated C meanings of `static'
3234 and `extern' are interpreted. */
3236 static tree
3237 grokdeclarator (tree declarator, tree declspecs,
3238 enum decl_context decl_context, int initialized)
3240 int specbits = 0;
3241 tree spec;
3242 tree type = NULL_TREE;
3243 int longlong = 0;
3244 int constp;
3245 int restrictp;
3246 int volatilep;
3247 int type_quals = TYPE_UNQUALIFIED;
3248 int inlinep;
3249 int explicit_int = 0;
3250 int explicit_char = 0;
3251 int defaulted_int = 0;
3252 tree typedef_decl = 0;
3253 const char *name;
3254 tree typedef_type = 0;
3255 int funcdef_flag = 0;
3256 enum tree_code innermost_code = ERROR_MARK;
3257 int bitfield = 0;
3258 int size_varies = 0;
3259 tree decl_attr = NULL_TREE;
3260 tree array_ptr_quals = NULL_TREE;
3261 int array_parm_static = 0;
3262 tree returned_attrs = NULL_TREE;
3264 if (decl_context == BITFIELD)
3265 bitfield = 1, decl_context = FIELD;
3267 if (decl_context == FUNCDEF)
3268 funcdef_flag = 1, decl_context = NORMAL;
3270 /* Look inside a declarator for the name being declared
3271 and get it as a string, for an error message. */
3273 tree decl = declarator;
3274 name = 0;
3276 while (decl)
3277 switch (TREE_CODE (decl))
3279 case ARRAY_REF:
3280 case INDIRECT_REF:
3281 case CALL_EXPR:
3282 innermost_code = TREE_CODE (decl);
3283 decl = TREE_OPERAND (decl, 0);
3284 break;
3286 case TREE_LIST:
3287 decl = TREE_VALUE (decl);
3288 break;
3290 case IDENTIFIER_NODE:
3291 name = IDENTIFIER_POINTER (decl);
3292 decl = 0;
3293 break;
3295 default:
3296 abort ();
3298 if (name == 0)
3299 name = "type name";
3302 /* A function definition's declarator must have the form of
3303 a function declarator. */
3305 if (funcdef_flag && innermost_code != CALL_EXPR)
3306 return 0;
3308 /* If this looks like a function definition, make it one,
3309 even if it occurs where parms are expected.
3310 Then store_parm_decls will reject it and not use it as a parm. */
3311 if (decl_context == NORMAL && !funcdef_flag
3312 && current_scope->parm_flag)
3313 decl_context = PARM;
3315 /* Look through the decl specs and record which ones appear.
3316 Some typespecs are defined as built-in typenames.
3317 Others, the ones that are modifiers of other types,
3318 are represented by bits in SPECBITS: set the bits for
3319 the modifiers that appear. Storage class keywords are also in SPECBITS.
3321 If there is a typedef name or a type, store the type in TYPE.
3322 This includes builtin typedefs such as `int'.
3324 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3325 and did not come from a user typedef.
3327 Set LONGLONG if `long' is mentioned twice. */
3329 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3331 tree id = TREE_VALUE (spec);
3333 /* If the entire declaration is itself tagged as deprecated then
3334 suppress reports of deprecated items. */
3335 if (id && TREE_DEPRECATED (id))
3337 if (deprecated_state != DEPRECATED_SUPPRESS)
3338 warn_deprecated_use (id);
3341 if (id == ridpointers[(int) RID_INT])
3342 explicit_int = 1;
3343 if (id == ridpointers[(int) RID_CHAR])
3344 explicit_char = 1;
3346 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3348 enum rid i = C_RID_CODE (id);
3349 if ((int) i <= (int) RID_LAST_MODIFIER)
3351 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3353 if (longlong)
3354 error ("`long long long' is too long for GCC");
3355 else
3357 if (pedantic && !flag_isoc99 && ! in_system_header
3358 && warn_long_long)
3359 pedwarn ("ISO C90 does not support `long long'");
3360 longlong = 1;
3363 else if (specbits & (1 << (int) i))
3365 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3367 if (!flag_isoc99)
3368 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3370 else
3371 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3374 /* Diagnose "__thread extern". Recall that this list
3375 is in the reverse order seen in the text. */
3376 if (i == RID_THREAD
3377 && (specbits & (1 << (int) RID_EXTERN
3378 | 1 << (int) RID_STATIC)))
3380 if (specbits & 1 << (int) RID_EXTERN)
3381 error ("`__thread' before `extern'");
3382 else
3383 error ("`__thread' before `static'");
3386 specbits |= 1 << (int) i;
3387 goto found;
3390 if (type)
3391 error ("two or more data types in declaration of `%s'", name);
3392 /* Actual typedefs come to us as TYPE_DECL nodes. */
3393 else if (TREE_CODE (id) == TYPE_DECL)
3395 if (TREE_TYPE (id) == error_mark_node)
3396 ; /* Allow the type to default to int to avoid cascading errors. */
3397 else
3399 type = TREE_TYPE (id);
3400 decl_attr = DECL_ATTRIBUTES (id);
3401 typedef_decl = id;
3404 /* Built-in types come as identifiers. */
3405 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3407 tree t = lookup_name (id);
3408 if (TREE_TYPE (t) == error_mark_node)
3410 else if (!t || TREE_CODE (t) != TYPE_DECL)
3411 error ("`%s' fails to be a typedef or built in type",
3412 IDENTIFIER_POINTER (id));
3413 else
3415 type = TREE_TYPE (t);
3416 typedef_decl = t;
3419 else if (TREE_CODE (id) != ERROR_MARK)
3420 type = id;
3422 found:
3426 typedef_type = type;
3427 if (type)
3428 size_varies = C_TYPE_VARIABLE_SIZE (type);
3430 /* No type at all: default to `int', and set DEFAULTED_INT
3431 because it was not a user-defined typedef. */
3433 if (type == 0)
3435 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3436 | (1 << (int) RID_SIGNED)
3437 | (1 << (int) RID_UNSIGNED)
3438 | (1 << (int) RID_COMPLEX))))
3439 /* Don't warn about typedef foo = bar. */
3440 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3441 && ! in_system_header)
3443 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3444 and this is a function, or if -Wimplicit; prefer the former
3445 warning since it is more explicit. */
3446 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3447 && funcdef_flag)
3448 warn_about_return_type = 1;
3449 else if (warn_implicit_int || flag_isoc99)
3450 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3451 name);
3454 defaulted_int = 1;
3455 type = integer_type_node;
3458 /* Now process the modifiers that were specified
3459 and check for invalid combinations. */
3461 /* Long double is a special combination. */
3463 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3464 && TYPE_MAIN_VARIANT (type) == double_type_node)
3466 specbits &= ~(1 << (int) RID_LONG);
3467 type = long_double_type_node;
3470 /* Check all other uses of type modifiers. */
3472 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3473 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3475 int ok = 0;
3477 if ((specbits & 1 << (int) RID_LONG)
3478 && (specbits & 1 << (int) RID_SHORT))
3479 error ("both long and short specified for `%s'", name);
3480 else if (((specbits & 1 << (int) RID_LONG)
3481 || (specbits & 1 << (int) RID_SHORT))
3482 && explicit_char)
3483 error ("long or short specified with char for `%s'", name);
3484 else if (((specbits & 1 << (int) RID_LONG)
3485 || (specbits & 1 << (int) RID_SHORT))
3486 && TREE_CODE (type) == REAL_TYPE)
3488 static int already = 0;
3490 error ("long or short specified with floating type for `%s'", name);
3491 if (! already && ! pedantic)
3493 error ("the only valid combination is `long double'");
3494 already = 1;
3497 else if ((specbits & 1 << (int) RID_SIGNED)
3498 && (specbits & 1 << (int) RID_UNSIGNED))
3499 error ("both signed and unsigned specified for `%s'", name);
3500 else if (TREE_CODE (type) != INTEGER_TYPE)
3501 error ("long, short, signed or unsigned invalid for `%s'", name);
3502 else
3504 ok = 1;
3505 if (!explicit_int && !defaulted_int && !explicit_char)
3507 error ("long, short, signed or unsigned used invalidly for `%s'",
3508 name);
3509 ok = 0;
3513 /* Discard the type modifiers if they are invalid. */
3514 if (! ok)
3516 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3517 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3518 longlong = 0;
3522 if ((specbits & (1 << (int) RID_COMPLEX))
3523 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3525 error ("complex invalid for `%s'", name);
3526 specbits &= ~(1 << (int) RID_COMPLEX);
3529 /* Decide whether an integer type is signed or not.
3530 Optionally treat bitfields as signed by default. */
3531 if (specbits & 1 << (int) RID_UNSIGNED
3532 || (bitfield && ! flag_signed_bitfields
3533 && (explicit_int || defaulted_int || explicit_char
3534 /* A typedef for plain `int' without `signed'
3535 can be controlled just like plain `int'. */
3536 || ! (typedef_decl != 0
3537 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3538 && TREE_CODE (type) != ENUMERAL_TYPE
3539 && !(specbits & 1 << (int) RID_SIGNED)))
3541 if (longlong)
3542 type = long_long_unsigned_type_node;
3543 else if (specbits & 1 << (int) RID_LONG)
3544 type = long_unsigned_type_node;
3545 else if (specbits & 1 << (int) RID_SHORT)
3546 type = short_unsigned_type_node;
3547 else if (type == char_type_node)
3548 type = unsigned_char_type_node;
3549 else if (typedef_decl)
3550 type = c_common_unsigned_type (type);
3551 else
3552 type = unsigned_type_node;
3554 else if ((specbits & 1 << (int) RID_SIGNED)
3555 && type == char_type_node)
3556 type = signed_char_type_node;
3557 else if (longlong)
3558 type = long_long_integer_type_node;
3559 else if (specbits & 1 << (int) RID_LONG)
3560 type = long_integer_type_node;
3561 else if (specbits & 1 << (int) RID_SHORT)
3562 type = short_integer_type_node;
3564 if (specbits & 1 << (int) RID_COMPLEX)
3566 if (pedantic && !flag_isoc99)
3567 pedwarn ("ISO C90 does not support complex types");
3568 /* If we just have "complex", it is equivalent to
3569 "complex double", but if any modifiers at all are specified it is
3570 the complex form of TYPE. E.g, "complex short" is
3571 "complex short int". */
3573 if (defaulted_int && ! longlong
3574 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3575 | (1 << (int) RID_SIGNED)
3576 | (1 << (int) RID_UNSIGNED))))
3578 if (pedantic)
3579 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3580 type = complex_double_type_node;
3582 else if (type == integer_type_node)
3584 if (pedantic)
3585 pedwarn ("ISO C does not support complex integer types");
3586 type = complex_integer_type_node;
3588 else if (type == float_type_node)
3589 type = complex_float_type_node;
3590 else if (type == double_type_node)
3591 type = complex_double_type_node;
3592 else if (type == long_double_type_node)
3593 type = complex_long_double_type_node;
3594 else
3596 if (pedantic)
3597 pedwarn ("ISO C does not support complex integer types");
3598 type = build_complex_type (type);
3602 /* Figure out the type qualifiers for the declaration. There are
3603 two ways a declaration can become qualified. One is something
3604 like `const int i' where the `const' is explicit. Another is
3605 something like `typedef const int CI; CI i' where the type of the
3606 declaration contains the `const'. */
3607 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3608 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3609 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3610 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3611 if (constp > 1 && ! flag_isoc99)
3612 pedwarn ("duplicate `const'");
3613 if (restrictp > 1 && ! flag_isoc99)
3614 pedwarn ("duplicate `restrict'");
3615 if (volatilep > 1 && ! flag_isoc99)
3616 pedwarn ("duplicate `volatile'");
3617 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3618 type = TYPE_MAIN_VARIANT (type);
3619 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3620 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3621 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3623 /* Warn if two storage classes are given. Default to `auto'. */
3626 int nclasses = 0;
3628 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3629 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3630 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3631 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3632 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3634 /* "static __thread" and "extern __thread" are allowed. */
3635 if ((specbits & (1 << (int) RID_THREAD
3636 | 1 << (int) RID_STATIC
3637 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3638 nclasses++;
3640 /* Warn about storage classes that are invalid for certain
3641 kinds of declarations (parameters, typenames, etc.). */
3643 if (nclasses > 1)
3644 error ("multiple storage classes in declaration of `%s'", name);
3645 else if (funcdef_flag
3646 && (specbits
3647 & ((1 << (int) RID_REGISTER)
3648 | (1 << (int) RID_AUTO)
3649 | (1 << (int) RID_TYPEDEF)
3650 | (1 << (int) RID_THREAD))))
3652 if (specbits & 1 << (int) RID_AUTO
3653 && (pedantic || current_scope == global_scope))
3654 pedwarn ("function definition declared `auto'");
3655 if (specbits & 1 << (int) RID_REGISTER)
3656 error ("function definition declared `register'");
3657 if (specbits & 1 << (int) RID_TYPEDEF)
3658 error ("function definition declared `typedef'");
3659 if (specbits & 1 << (int) RID_THREAD)
3660 error ("function definition declared `__thread'");
3661 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3662 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3664 else if (decl_context != NORMAL && nclasses > 0)
3666 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3668 else
3670 switch (decl_context)
3672 case FIELD:
3673 error ("storage class specified for structure field `%s'",
3674 name);
3675 break;
3676 case PARM:
3677 error ("storage class specified for parameter `%s'", name);
3678 break;
3679 default:
3680 error ("storage class specified for typename");
3681 break;
3683 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3684 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3685 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3688 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3690 /* `extern' with initialization is invalid if not at file scope. */
3691 if (current_scope == global_scope)
3692 warning ("`%s' initialized and declared `extern'", name);
3693 else
3694 error ("`%s' has both `extern' and initializer", name);
3696 else if (current_scope == global_scope)
3698 if (specbits & 1 << (int) RID_AUTO)
3699 error ("file-scope declaration of `%s' specifies `auto'", name);
3701 else
3703 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3704 error ("nested function `%s' declared `extern'", name);
3705 else if ((specbits & (1 << (int) RID_THREAD
3706 | 1 << (int) RID_EXTERN
3707 | 1 << (int) RID_STATIC))
3708 == (1 << (int) RID_THREAD))
3710 error ("function-scope `%s' implicitly auto and declared `__thread'",
3711 name);
3712 specbits &= ~(1 << (int) RID_THREAD);
3717 /* Now figure out the structure of the declarator proper.
3718 Descend through it, creating more complex types, until we reach
3719 the declared identifier (or NULL_TREE, in an absolute declarator). */
3721 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3723 if (type == error_mark_node)
3725 declarator = TREE_OPERAND (declarator, 0);
3726 continue;
3729 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3730 an INDIRECT_REF (for *...),
3731 a CALL_EXPR (for ...(...)),
3732 a TREE_LIST (for nested attributes),
3733 an identifier (for the name being declared)
3734 or a null pointer (for the place in an absolute declarator
3735 where the name was omitted).
3736 For the last two cases, we have just exited the loop.
3738 At this point, TYPE is the type of elements of an array,
3739 or for a function to return, or for a pointer to point to.
3740 After this sequence of ifs, TYPE is the type of the
3741 array or function or pointer, and DECLARATOR has had its
3742 outermost layer removed. */
3744 if (array_ptr_quals != NULL_TREE || array_parm_static)
3746 /* Only the innermost declarator (making a parameter be of
3747 array type which is converted to pointer type)
3748 may have static or type qualifiers. */
3749 error ("static or type qualifiers in non-parameter array declarator");
3750 array_ptr_quals = NULL_TREE;
3751 array_parm_static = 0;
3754 if (TREE_CODE (declarator) == TREE_LIST)
3756 /* We encode a declarator with embedded attributes using
3757 a TREE_LIST. */
3758 tree attrs = TREE_PURPOSE (declarator);
3759 tree inner_decl;
3760 int attr_flags = 0;
3761 declarator = TREE_VALUE (declarator);
3762 inner_decl = declarator;
3763 while (inner_decl != NULL_TREE
3764 && TREE_CODE (inner_decl) == TREE_LIST)
3765 inner_decl = TREE_VALUE (inner_decl);
3766 if (inner_decl == NULL_TREE
3767 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3768 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3769 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3770 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3771 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3772 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3773 returned_attrs = decl_attributes (&type,
3774 chainon (returned_attrs, attrs),
3775 attr_flags);
3777 else if (TREE_CODE (declarator) == ARRAY_REF)
3779 tree itype = NULL_TREE;
3780 tree size = TREE_OPERAND (declarator, 1);
3781 /* The index is a signed object `sizetype' bits wide. */
3782 tree index_type = c_common_signed_type (sizetype);
3784 array_ptr_quals = TREE_TYPE (declarator);
3785 array_parm_static = TREE_STATIC (declarator);
3787 declarator = TREE_OPERAND (declarator, 0);
3789 /* Check for some types that there cannot be arrays of. */
3791 if (VOID_TYPE_P (type))
3793 error ("declaration of `%s' as array of voids", name);
3794 type = error_mark_node;
3797 if (TREE_CODE (type) == FUNCTION_TYPE)
3799 error ("declaration of `%s' as array of functions", name);
3800 type = error_mark_node;
3803 if (pedantic && flexible_array_type_p (type))
3804 pedwarn ("invalid use of structure with flexible array member");
3806 if (size == error_mark_node)
3807 type = error_mark_node;
3809 if (type == error_mark_node)
3810 continue;
3812 /* If size was specified, set ITYPE to a range-type for that size.
3813 Otherwise, ITYPE remains null. finish_decl may figure it out
3814 from an initial value. */
3816 if (size)
3818 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3819 STRIP_TYPE_NOPS (size);
3821 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3823 error ("size of array `%s' has non-integer type", name);
3824 size = integer_one_node;
3827 if (pedantic && integer_zerop (size))
3828 pedwarn ("ISO C forbids zero-size array `%s'", name);
3830 if (TREE_CODE (size) == INTEGER_CST)
3832 constant_expression_warning (size);
3833 if (tree_int_cst_sgn (size) < 0)
3835 error ("size of array `%s' is negative", name);
3836 size = integer_one_node;
3839 else
3841 /* Make sure the array size remains visibly nonconstant
3842 even if it is (eg) a const variable with known value. */
3843 size_varies = 1;
3845 if (!flag_isoc99 && pedantic)
3847 if (TREE_CONSTANT (size))
3848 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3849 name);
3850 else
3851 pedwarn ("ISO C90 forbids variable-size array `%s'",
3852 name);
3856 if (integer_zerop (size))
3858 /* A zero-length array cannot be represented with an
3859 unsigned index type, which is what we'll get with
3860 build_index_type. Create an open-ended range instead. */
3861 itype = build_range_type (sizetype, size, NULL_TREE);
3863 else
3865 /* Compute the maximum valid index, that is, size - 1.
3866 Do the calculation in index_type, so that if it is
3867 a variable the computations will be done in the
3868 proper mode. */
3869 itype = fold (build (MINUS_EXPR, index_type,
3870 convert (index_type, size),
3871 convert (index_type, size_one_node)));
3873 /* If that overflowed, the array is too big.
3874 ??? While a size of INT_MAX+1 technically shouldn't
3875 cause an overflow (because we subtract 1), the overflow
3876 is recorded during the conversion to index_type, before
3877 the subtraction. Handling this case seems like an
3878 unnecessary complication. */
3879 if (TREE_OVERFLOW (itype))
3881 error ("size of array `%s' is too large", name);
3882 type = error_mark_node;
3883 continue;
3886 if (size_varies)
3888 /* We must be able to distinguish the
3889 SAVE_EXPR_CONTEXT for the variably-sized type
3890 so that we can set it correctly in
3891 set_save_expr_context. The convention is
3892 that all SAVE_EXPRs that need to be reset
3893 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3894 tree cfd = current_function_decl;
3895 if (decl_context == PARM)
3896 current_function_decl = NULL_TREE;
3897 itype = variable_size (itype);
3898 if (decl_context == PARM)
3899 current_function_decl = cfd;
3901 itype = build_index_type (itype);
3904 else if (decl_context == FIELD)
3906 if (pedantic && !flag_isoc99 && !in_system_header)
3907 pedwarn ("ISO C90 does not support flexible array members");
3909 /* ISO C99 Flexible array members are effectively identical
3910 to GCC's zero-length array extension. */
3911 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3914 /* If pedantic, complain about arrays of incomplete types. */
3916 if (pedantic && !COMPLETE_TYPE_P (type))
3917 pedwarn ("array type has incomplete element type");
3919 /* Build the array type itself, then merge any constancy or
3920 volatility into the target type. We must do it in this order
3921 to ensure that the TYPE_MAIN_VARIANT field of the array type
3922 is set correctly. */
3924 type = build_array_type (type, itype);
3925 if (type_quals)
3926 type = c_build_qualified_type (type, type_quals);
3928 if (size_varies)
3929 C_TYPE_VARIABLE_SIZE (type) = 1;
3931 /* The GCC extension for zero-length arrays differs from
3932 ISO flexible array members in that sizeof yields zero. */
3933 if (size && integer_zerop (size))
3935 layout_type (type);
3936 TYPE_SIZE (type) = bitsize_zero_node;
3937 TYPE_SIZE_UNIT (type) = size_zero_node;
3939 if (decl_context != PARM
3940 && (array_ptr_quals != NULL_TREE || array_parm_static))
3942 error ("static or type qualifiers in non-parameter array declarator");
3943 array_ptr_quals = NULL_TREE;
3944 array_parm_static = 0;
3947 else if (TREE_CODE (declarator) == CALL_EXPR)
3949 tree arg_types;
3951 /* Declaring a function type.
3952 Make sure we have a valid type for the function to return. */
3953 if (type == error_mark_node)
3954 continue;
3956 size_varies = 0;
3958 /* Warn about some types functions can't return. */
3960 if (TREE_CODE (type) == FUNCTION_TYPE)
3962 error ("`%s' declared as function returning a function", name);
3963 type = integer_type_node;
3965 if (TREE_CODE (type) == ARRAY_TYPE)
3967 error ("`%s' declared as function returning an array", name);
3968 type = integer_type_node;
3971 /* Construct the function type and go to the next
3972 inner layer of declarator. */
3974 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3975 funcdef_flag
3976 /* Say it's a definition
3977 only for the CALL_EXPR
3978 closest to the identifier. */
3979 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3980 /* Type qualifiers before the return type of the function
3981 qualify the return type, not the function type. */
3982 if (type_quals)
3984 /* Type qualifiers on a function return type are normally
3985 permitted by the standard but have no effect, so give a
3986 warning at -Wextra. Qualifiers on a void return type have
3987 meaning as a GNU extension, and are banned on function
3988 definitions in ISO C. FIXME: strictly we shouldn't
3989 pedwarn for qualified void return types except on function
3990 definitions, but not doing so could lead to the undesirable
3991 state of a "volatile void" function return type not being
3992 warned about, and a use of the function being compiled
3993 with GNU semantics, with no diagnostics under -pedantic. */
3994 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3995 pedwarn ("ISO C forbids qualified void function return type");
3996 else if (extra_warnings
3997 && !(VOID_TYPE_P (type)
3998 && type_quals == TYPE_QUAL_VOLATILE))
3999 warning ("type qualifiers ignored on function return type");
4001 type = c_build_qualified_type (type, type_quals);
4003 type_quals = TYPE_UNQUALIFIED;
4005 type = build_function_type (type, arg_types);
4006 declarator = TREE_OPERAND (declarator, 0);
4008 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4009 the formal parameter list of this FUNCTION_TYPE to point to
4010 the FUNCTION_TYPE node itself. */
4013 tree link;
4015 for (link = last_function_parm_tags;
4016 link;
4017 link = TREE_CHAIN (link))
4018 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4021 else if (TREE_CODE (declarator) == INDIRECT_REF)
4023 /* Merge any constancy or volatility into the target type
4024 for the pointer. */
4026 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4027 && type_quals)
4028 pedwarn ("ISO C forbids qualified function types");
4029 if (type_quals)
4030 type = c_build_qualified_type (type, type_quals);
4031 type_quals = TYPE_UNQUALIFIED;
4032 size_varies = 0;
4034 type = build_pointer_type (type);
4036 /* Process a list of type modifier keywords
4037 (such as const or volatile) that were given inside the `*'. */
4039 if (TREE_TYPE (declarator))
4041 tree typemodlist;
4042 int erred = 0;
4044 constp = 0;
4045 volatilep = 0;
4046 restrictp = 0;
4047 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4048 typemodlist = TREE_CHAIN (typemodlist))
4050 tree qualifier = TREE_VALUE (typemodlist);
4052 if (C_IS_RESERVED_WORD (qualifier))
4054 if (C_RID_CODE (qualifier) == RID_CONST)
4055 constp++;
4056 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4057 volatilep++;
4058 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4059 restrictp++;
4060 else
4061 erred++;
4063 else
4064 erred++;
4067 if (erred)
4068 error ("invalid type modifier within pointer declarator");
4069 if (constp > 1 && ! flag_isoc99)
4070 pedwarn ("duplicate `const'");
4071 if (volatilep > 1 && ! flag_isoc99)
4072 pedwarn ("duplicate `volatile'");
4073 if (restrictp > 1 && ! flag_isoc99)
4074 pedwarn ("duplicate `restrict'");
4076 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4077 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4078 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4081 declarator = TREE_OPERAND (declarator, 0);
4083 else
4084 abort ();
4088 /* Now TYPE has the actual type. */
4090 /* Did array size calculations overflow? */
4092 if (TREE_CODE (type) == ARRAY_TYPE
4093 && COMPLETE_TYPE_P (type)
4094 && TREE_OVERFLOW (TYPE_SIZE (type)))
4096 error ("size of array `%s' is too large", name);
4097 /* If we proceed with the array type as it is, we'll eventually
4098 crash in tree_low_cst(). */
4099 type = error_mark_node;
4102 /* If this is declaring a typedef name, return a TYPE_DECL. */
4104 if (specbits & (1 << (int) RID_TYPEDEF))
4106 tree decl;
4107 /* Note that the grammar rejects storage classes
4108 in typenames, fields or parameters */
4109 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4110 && type_quals)
4111 pedwarn ("ISO C forbids qualified function types");
4112 if (type_quals)
4113 type = c_build_qualified_type (type, type_quals);
4114 decl = build_decl (TYPE_DECL, declarator, type);
4115 if ((specbits & (1 << (int) RID_SIGNED))
4116 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4117 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4118 decl_attributes (&decl, returned_attrs, 0);
4119 return decl;
4122 /* Detect the case of an array type of unspecified size
4123 which came, as such, direct from a typedef name.
4124 We must copy the type, so that each identifier gets
4125 a distinct type, so that each identifier's size can be
4126 controlled separately by its own initializer. */
4128 if (type != 0 && typedef_type != 0
4129 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4130 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4132 type = build_array_type (TREE_TYPE (type), 0);
4133 if (size_varies)
4134 C_TYPE_VARIABLE_SIZE (type) = 1;
4137 /* If this is a type name (such as, in a cast or sizeof),
4138 compute the type and return it now. */
4140 if (decl_context == TYPENAME)
4142 /* Note that the grammar rejects storage classes
4143 in typenames, fields or parameters */
4144 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4145 && type_quals)
4146 pedwarn ("ISO C forbids const or volatile function types");
4147 if (type_quals)
4148 type = c_build_qualified_type (type, type_quals);
4149 decl_attributes (&type, returned_attrs, 0);
4150 return type;
4153 /* Aside from typedefs and type names (handle above),
4154 `void' at top level (not within pointer)
4155 is allowed only in public variables.
4156 We don't complain about parms either, but that is because
4157 a better error message can be made later. */
4159 if (VOID_TYPE_P (type) && decl_context != PARM
4160 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4161 && ((specbits & (1 << (int) RID_EXTERN))
4162 || (current_scope == global_scope
4163 && !(specbits
4164 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4166 error ("variable or field `%s' declared void", name);
4167 type = integer_type_node;
4170 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4171 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4174 tree decl;
4176 if (decl_context == PARM)
4178 tree type_as_written;
4179 tree promoted_type;
4181 /* A parameter declared as an array of T is really a pointer to T.
4182 One declared as a function is really a pointer to a function. */
4184 if (TREE_CODE (type) == ARRAY_TYPE)
4186 /* Transfer const-ness of array into that of type pointed to. */
4187 type = TREE_TYPE (type);
4188 if (type_quals)
4189 type = c_build_qualified_type (type, type_quals);
4190 type = build_pointer_type (type);
4191 type_quals = TYPE_UNQUALIFIED;
4192 if (array_ptr_quals)
4194 tree new_ptr_quals, new_ptr_attrs;
4195 int erred = 0;
4196 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4197 /* We don't yet implement attributes in this context. */
4198 if (new_ptr_attrs != NULL_TREE)
4199 warning ("attributes in parameter array declarator ignored");
4201 constp = 0;
4202 volatilep = 0;
4203 restrictp = 0;
4204 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4206 tree qualifier = TREE_VALUE (new_ptr_quals);
4208 if (C_IS_RESERVED_WORD (qualifier))
4210 if (C_RID_CODE (qualifier) == RID_CONST)
4211 constp++;
4212 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4213 volatilep++;
4214 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4215 restrictp++;
4216 else
4217 erred++;
4219 else
4220 erred++;
4223 if (erred)
4224 error ("invalid type modifier within array declarator");
4226 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4227 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4228 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4230 size_varies = 0;
4232 else if (TREE_CODE (type) == FUNCTION_TYPE)
4234 if (pedantic && type_quals)
4235 pedwarn ("ISO C forbids qualified function types");
4236 if (type_quals)
4237 type = c_build_qualified_type (type, type_quals);
4238 type = build_pointer_type (type);
4239 type_quals = TYPE_UNQUALIFIED;
4241 else if (type_quals)
4242 type = c_build_qualified_type (type, type_quals);
4244 type_as_written = type;
4246 decl = build_decl (PARM_DECL, declarator, type);
4247 if (size_varies)
4248 C_DECL_VARIABLE_SIZE (decl) = 1;
4250 /* Compute the type actually passed in the parmlist,
4251 for the case where there is no prototype.
4252 (For example, shorts and chars are passed as ints.)
4253 When there is a prototype, this is overridden later. */
4255 if (type == error_mark_node)
4256 promoted_type = type;
4257 else
4258 promoted_type = c_type_promotes_to (type);
4260 DECL_ARG_TYPE (decl) = promoted_type;
4261 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4263 else if (decl_context == FIELD)
4265 /* Structure field. It may not be a function. */
4267 if (TREE_CODE (type) == FUNCTION_TYPE)
4269 error ("field `%s' declared as a function", name);
4270 type = build_pointer_type (type);
4272 else if (TREE_CODE (type) != ERROR_MARK
4273 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4275 error ("field `%s' has incomplete type", name);
4276 type = error_mark_node;
4278 /* Move type qualifiers down to element of an array. */
4279 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4280 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4281 type_quals),
4282 TYPE_DOMAIN (type));
4283 decl = build_decl (FIELD_DECL, declarator, type);
4284 DECL_NONADDRESSABLE_P (decl) = bitfield;
4286 if (size_varies)
4287 C_DECL_VARIABLE_SIZE (decl) = 1;
4289 else if (TREE_CODE (type) == FUNCTION_TYPE)
4291 /* Every function declaration is "external"
4292 except for those which are inside a function body
4293 in which `auto' is used.
4294 That is a case not specified by ANSI C,
4295 and we use it for forward declarations for nested functions. */
4296 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4297 || current_scope == global_scope);
4299 if (specbits & (1 << (int) RID_AUTO)
4300 && (pedantic || current_scope == global_scope))
4301 pedwarn ("invalid storage class for function `%s'", name);
4302 if (specbits & (1 << (int) RID_REGISTER))
4303 error ("invalid storage class for function `%s'", name);
4304 if (specbits & (1 << (int) RID_THREAD))
4305 error ("invalid storage class for function `%s'", name);
4306 /* Function declaration not at file scope.
4307 Storage classes other than `extern' are not allowed
4308 and `extern' makes no difference. */
4309 if (current_scope != global_scope
4310 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4311 && pedantic)
4312 pedwarn ("invalid storage class for function `%s'", name);
4314 decl = build_decl (FUNCTION_DECL, declarator, type);
4315 decl = build_decl_attribute_variant (decl, decl_attr);
4317 DECL_LANG_SPECIFIC (decl)
4318 = ggc_alloc_cleared (sizeof (struct lang_decl));
4320 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4321 pedwarn ("ISO C forbids qualified function types");
4323 /* GNU C interprets a `volatile void' return type to indicate
4324 that the function does not return. */
4325 if ((type_quals & TYPE_QUAL_VOLATILE)
4326 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4327 warning ("`noreturn' function returns non-void value");
4329 if (extern_ref)
4330 DECL_EXTERNAL (decl) = 1;
4331 /* Record absence of global scope for `static' or `auto'. */
4332 TREE_PUBLIC (decl)
4333 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4335 if (defaulted_int)
4336 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4338 /* Record presence of `inline', if it is reasonable. */
4339 if (MAIN_NAME_P (declarator))
4341 if (inlinep)
4342 warning ("cannot inline function `main'");
4344 else if (inlinep)
4346 /* Record that the function is declared `inline'. */
4347 DECL_DECLARED_INLINE_P (decl) = 1;
4349 /* Do not mark bare declarations as DECL_INLINE. Doing so
4350 in the presence of multiple declarations can result in
4351 the abstract origin pointing between the declarations,
4352 which will confuse dwarf2out. */
4353 if (initialized)
4355 DECL_INLINE (decl) = 1;
4356 if (specbits & (1 << (int) RID_EXTERN))
4357 current_extern_inline = 1;
4360 /* If -finline-functions, assume it can be inlined. This does
4361 two things: let the function be deferred until it is actually
4362 needed, and let dwarf2 know that the function is inlinable. */
4363 else if (flag_inline_trees == 2 && initialized)
4364 DECL_INLINE (decl) = 1;
4366 else
4368 /* It's a variable. */
4369 /* An uninitialized decl with `extern' is a reference. */
4370 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4372 /* Move type qualifiers down to element of an array. */
4373 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4375 int saved_align = TYPE_ALIGN(type);
4376 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4377 type_quals),
4378 TYPE_DOMAIN (type));
4379 TYPE_ALIGN (type) = saved_align;
4381 else if (type_quals)
4382 type = c_build_qualified_type (type, type_quals);
4384 /* It is invalid to create an `extern' declaration for a
4385 variable if there is a global declaration that is
4386 `static'. */
4387 if (extern_ref && current_scope != global_scope)
4389 tree global_decl;
4391 global_decl = identifier_global_value (declarator);
4392 if (global_decl
4393 && TREE_CODE (global_decl) == VAR_DECL
4394 && !TREE_PUBLIC (global_decl))
4395 error ("variable previously declared `static' redeclared "
4396 "`extern'");
4399 decl = build_decl (VAR_DECL, declarator, type);
4400 if (size_varies)
4401 C_DECL_VARIABLE_SIZE (decl) = 1;
4403 if (inlinep)
4404 pedwarn ("%Hvariable '%D' declared `inline'",
4405 &DECL_SOURCE_LOCATION (decl), decl);
4407 DECL_EXTERNAL (decl) = extern_ref;
4409 /* At file scope, the presence of a `static' or `register' storage
4410 class specifier, or the absence of all storage class specifiers
4411 makes this declaration a definition (perhaps tentative). Also,
4412 the absence of both `static' and `register' makes it public. */
4413 if (current_scope == global_scope)
4415 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4416 | (1 << (int) RID_REGISTER)));
4417 TREE_STATIC (decl) = !extern_ref;
4419 /* Not at file scope, only `static' makes a static definition. */
4420 else
4422 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4423 TREE_PUBLIC (decl) = extern_ref;
4426 if (specbits & 1 << (int) RID_THREAD)
4428 if (targetm.have_tls)
4429 DECL_THREAD_LOCAL (decl) = 1;
4430 else
4431 /* A mere warning is sure to result in improper semantics
4432 at runtime. Don't bother to allow this to compile. */
4433 error ("thread-local storage not supported for this target");
4437 /* Record `register' declaration for warnings on &
4438 and in case doing stupid register allocation. */
4440 if (specbits & (1 << (int) RID_REGISTER))
4441 DECL_REGISTER (decl) = 1;
4443 /* Record constancy and volatility. */
4444 c_apply_type_quals_to_decl (type_quals, decl);
4446 /* If a type has volatile components, it should be stored in memory.
4447 Otherwise, the fact that those components are volatile
4448 will be ignored, and would even crash the compiler. */
4449 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4450 c_mark_addressable (decl);
4452 #ifdef ENABLE_CHECKING
4453 /* This is the earliest point at which we might know the assembler
4454 name of a variable. Thus, if it's known before this, die horribly. */
4455 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4456 abort ();
4457 #endif
4459 decl_attributes (&decl, returned_attrs, 0);
4461 return decl;
4465 /* Decode the parameter-list info for a function type or function definition.
4466 The argument is the value returned by `get_parm_info' (or made in parse.y
4467 if there is an identifier list instead of a parameter decl list).
4468 These two functions are separate because when a function returns
4469 or receives functions then each is called multiple times but the order
4470 of calls is different. The last call to `grokparms' is always the one
4471 that contains the formal parameter names of a function definition.
4473 Store in `last_function_parms' a chain of the decls of parms.
4474 Also store in `last_function_parm_tags' a chain of the struct, union,
4475 and enum tags declared among the parms.
4477 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4479 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4480 a mere declaration. A nonempty identifier-list gets an error message
4481 when FUNCDEF_FLAG is zero. */
4483 static tree
4484 grokparms (tree parms_info, int funcdef_flag)
4486 tree first_parm = TREE_CHAIN (parms_info);
4488 last_function_parms = TREE_PURPOSE (parms_info);
4489 last_function_parm_tags = TREE_VALUE (parms_info);
4490 last_function_parm_others = TREE_TYPE (parms_info);
4492 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4493 && !in_system_header)
4494 warning ("function declaration isn't a prototype");
4496 if (first_parm != 0
4497 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4499 if (! funcdef_flag)
4500 pedwarn ("parameter names (without types) in function declaration");
4502 last_function_parms = first_parm;
4503 return 0;
4505 else
4507 tree parm;
4508 tree typelt;
4509 /* If the arg types are incomplete in a declaration,
4510 they must include undefined tags.
4511 These tags can never be defined in the scope of the declaration,
4512 so the types can never be completed,
4513 and no call can be compiled successfully. */
4515 for (parm = last_function_parms, typelt = first_parm;
4516 parm;
4517 parm = TREE_CHAIN (parm))
4518 /* Skip over any enumeration constants declared here. */
4519 if (TREE_CODE (parm) == PARM_DECL)
4521 /* Barf if the parameter itself has an incomplete type. */
4522 tree type = TREE_VALUE (typelt);
4523 if (type == error_mark_node)
4524 continue;
4525 if (!COMPLETE_TYPE_P (type))
4527 if (funcdef_flag && DECL_NAME (parm) != 0)
4528 error ("parameter `%s' has incomplete type",
4529 IDENTIFIER_POINTER (DECL_NAME (parm)));
4530 else
4531 warning ("parameter has incomplete type");
4532 if (funcdef_flag)
4534 TREE_VALUE (typelt) = error_mark_node;
4535 TREE_TYPE (parm) = error_mark_node;
4538 typelt = TREE_CHAIN (typelt);
4541 return first_parm;
4545 /* Return a tree_list node with info on a parameter list just parsed.
4546 The TREE_PURPOSE is a list of decls of those parms.
4547 The TREE_VALUE is a list of structure, union and enum tags defined.
4548 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4549 The TREE_TYPE is a list of non-parameter decls which appeared with the
4550 parameters.
4551 This tree_list node is later fed to `grokparms'.
4553 VOID_AT_END nonzero means append `void' to the end of the type-list.
4554 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4556 tree
4557 get_parm_info (int void_at_end)
4559 tree decl, type, list;
4560 tree types = 0;
4561 tree *last_type = &types;
4562 tree tags = current_scope->tags;
4563 tree parms = current_scope->parms;
4564 tree others = current_scope->names;
4565 static bool explained_incomplete_types = false;
4566 bool gave_void_only_once_err = false;
4568 /* Just "void" (and no ellipsis) is special. There are really no parms.
4569 But if the "void" is qualified (by "const" or "volatile"), or has a
4570 storage class specifier ("register"), then the behavior is undefined;
4571 issue an error. Typedefs for "void" are OK (see DR#157). */
4572 if (void_at_end && parms != 0
4573 && TREE_CHAIN (parms) == 0
4574 && VOID_TYPE_P (TREE_TYPE (parms))
4575 && !DECL_NAME (parms))
4577 if (TREE_THIS_VOLATILE (parms)
4578 || TREE_READONLY (parms)
4579 || DECL_REGISTER (parms))
4580 error ("\"void\" as only parameter may not be qualified");
4582 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4585 /* Sanity check all of the parameter declarations. */
4586 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4588 if (TREE_CODE (decl) != PARM_DECL)
4589 abort ();
4590 if (TREE_ASM_WRITTEN (decl))
4591 abort ();
4593 /* Since there is a prototype, args are passed in their
4594 declared types. The back end may override this. */
4595 type = TREE_TYPE (decl);
4596 DECL_ARG_TYPE (decl) = type;
4598 /* Check for (..., void, ...) and issue an error. */
4599 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4601 error ("\"void\" must be the only parameter");
4602 gave_void_only_once_err = true;
4605 type = build_tree_list (0, type);
4606 *last_type = type;
4607 last_type = &TREE_CHAIN (type);
4610 /* Check the list of non-parameter decls for any forward parm decls
4611 that never got real decls. */
4612 for (decl = others; decl; decl = TREE_CHAIN (decl))
4613 if (TREE_CODE (decl) == PARM_DECL)
4615 if (!TREE_ASM_WRITTEN (decl))
4616 abort ();
4618 error ("%Hparameter \"%D\" has just a forward declaration",
4619 &DECL_SOURCE_LOCATION (decl), decl);
4622 /* Warn about any struct, union or enum tags defined within this
4623 list. The scope of such types is limited to this declaration,
4624 which is rarely if ever desirable (it's impossible to call such
4625 a function with type-correct arguments). */
4626 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4628 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4629 const char *keyword;
4630 /* An anonymous union parm type is meaningful as a GNU extension.
4631 So don't warn for that. */
4632 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4633 continue;
4635 /* The keyword should not be translated. */
4636 switch (code)
4638 case RECORD_TYPE: keyword = "struct"; break;
4639 case UNION_TYPE: keyword = "union"; break;
4640 case ENUMERAL_TYPE: keyword = "enum"; break;
4641 default: abort ();
4644 if (TREE_PURPOSE (decl))
4645 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4646 warning ("\"%s %s\" declared inside parameter list",
4647 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4648 else
4649 /* The %s will be one of 'struct', 'union', or 'enum'. */
4650 warning ("anonymous %s declared inside parameter list", keyword);
4652 if (! explained_incomplete_types)
4654 warning ("its scope is only this definition or declaration,"
4655 " which is probably not what you want");
4656 explained_incomplete_types = true;
4661 if (void_at_end)
4663 type = build_tree_list (0, void_type_node);
4664 *last_type = type;
4667 list = tree_cons (parms, tags, types);
4668 TREE_TYPE (list) = others;
4669 return list;
4672 /* Get the struct, enum or union (CODE says which) with tag NAME.
4673 Define the tag as a forward-reference if it is not defined. */
4675 tree
4676 xref_tag (enum tree_code code, tree name)
4678 /* If a cross reference is requested, look up the type
4679 already defined for this tag and return it. */
4681 tree ref = lookup_tag (code, name, 0);
4682 /* If this is the right type of tag, return what we found.
4683 (This reference will be shadowed by shadow_tag later if appropriate.)
4684 If this is the wrong type of tag, do not return it. If it was the
4685 wrong type in the same scope, we will have had an error
4686 message already; if in a different scope and declaring
4687 a name, pending_xref_error will give an error message; but if in a
4688 different scope and not declaring a name, this tag should
4689 shadow the previous declaration of a different type of tag, and
4690 this would not work properly if we return the reference found.
4691 (For example, with "struct foo" in an outer scope, "union foo;"
4692 must shadow that tag with a new one of union type.) */
4693 if (ref && TREE_CODE (ref) == code)
4694 return ref;
4696 /* If no such tag is yet defined, create a forward-reference node
4697 and record it as the "definition".
4698 When a real declaration of this type is found,
4699 the forward-reference will be altered into a real type. */
4701 ref = make_node (code);
4702 if (code == ENUMERAL_TYPE)
4704 /* Give the type a default layout like unsigned int
4705 to avoid crashing if it does not get defined. */
4706 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4707 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4708 TYPE_USER_ALIGN (ref) = 0;
4709 TREE_UNSIGNED (ref) = 1;
4710 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4711 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4712 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4715 pushtag (name, ref);
4717 return ref;
4720 /* Make sure that the tag NAME is defined *in the current scope*
4721 at least as a forward reference.
4722 CODE says which kind of tag NAME ought to be. */
4724 tree
4725 start_struct (enum tree_code code, tree name)
4727 /* If there is already a tag defined at this scope
4728 (as a forward reference), just return it. */
4730 tree ref = 0;
4732 if (name != 0)
4733 ref = lookup_tag (code, name, 1);
4734 if (ref && TREE_CODE (ref) == code)
4736 if (TYPE_FIELDS (ref))
4738 if (code == UNION_TYPE)
4739 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4740 else
4741 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4744 else
4746 /* Otherwise create a forward-reference just so the tag is in scope. */
4748 ref = make_node (code);
4749 pushtag (name, ref);
4752 C_TYPE_BEING_DEFINED (ref) = 1;
4753 TYPE_PACKED (ref) = flag_pack_struct;
4754 return ref;
4757 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4758 of a structure component, returning a FIELD_DECL node.
4759 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4761 This is done during the parsing of the struct declaration.
4762 The FIELD_DECL nodes are chained together and the lot of them
4763 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4765 tree
4766 grokfield (tree declarator, tree declspecs, tree width)
4768 tree value;
4770 if (declarator == NULL_TREE && width == NULL_TREE)
4772 /* This is an unnamed decl.
4774 If we have something of the form "union { list } ;" then this
4775 is the anonymous union extension. Similarly for struct.
4777 If this is something of the form "struct foo;", then
4778 If MS extensions are enabled, this is handled as an
4779 anonymous struct.
4780 Otherwise this is a forward declaration of a structure tag.
4782 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4783 If MS extensions are enabled and foo names a structure, then
4784 again this is an anonymous struct.
4785 Otherwise this is an error.
4787 Oh what a horrid tangled web we weave. I wonder if MS consciously
4788 took this from Plan 9 or if it was an accident of implementation
4789 that took root before someone noticed the bug... */
4791 tree type = TREE_VALUE (declspecs);
4793 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4794 type = TREE_TYPE (type);
4795 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4797 if (flag_ms_extensions)
4798 ; /* ok */
4799 else if (flag_iso)
4800 goto warn_unnamed_field;
4801 else if (TYPE_NAME (type) == NULL)
4802 ; /* ok */
4803 else
4804 goto warn_unnamed_field;
4806 else
4808 warn_unnamed_field:
4809 warning ("declaration does not declare anything");
4810 return NULL_TREE;
4814 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4816 finish_decl (value, NULL_TREE, NULL_TREE);
4817 DECL_INITIAL (value) = width;
4819 if (c_dialect_objc ())
4820 objc_check_decl (value);
4821 return value;
4824 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4825 the list such that this does not present a problem later. */
4827 static void
4828 detect_field_duplicates (tree fieldlist)
4830 tree x, y;
4831 int timeout = 10;
4833 /* First, see if there are more than "a few" fields.
4834 This is trivially true if there are zero or one fields. */
4835 if (!fieldlist)
4836 return;
4837 x = TREE_CHAIN (fieldlist);
4838 if (!x)
4839 return;
4840 do {
4841 timeout--;
4842 x = TREE_CHAIN (x);
4843 } while (timeout > 0 && x);
4845 /* If there were "few" fields, avoid the overhead of allocating
4846 a hash table. Instead just do the nested traversal thing. */
4847 if (timeout > 0)
4849 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4850 if (DECL_NAME (x))
4852 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4853 if (DECL_NAME (y) == DECL_NAME (x))
4855 error ("%Hduplicate member '%D'",
4856 &DECL_SOURCE_LOCATION (x), x);
4857 DECL_NAME (x) = NULL_TREE;
4861 else
4863 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4864 void **slot;
4866 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4867 if ((y = DECL_NAME (x)) != 0)
4869 slot = htab_find_slot (htab, y, INSERT);
4870 if (*slot)
4872 error ("%Hduplicate member '%D'",
4873 &DECL_SOURCE_LOCATION (x), x);
4874 DECL_NAME (x) = NULL_TREE;
4876 *slot = y;
4879 htab_delete (htab);
4883 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4884 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4885 ATTRIBUTES are attributes to be applied to the structure. */
4887 tree
4888 finish_struct (tree t, tree fieldlist, tree attributes)
4890 tree x;
4891 int toplevel = global_scope == current_scope;
4892 int saw_named_field;
4894 /* If this type was previously laid out as a forward reference,
4895 make sure we lay it out again. */
4897 TYPE_SIZE (t) = 0;
4899 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4901 /* Nameless union parm types are useful as GCC extension. */
4902 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4903 /* Otherwise, warn about any struct or union def. in parmlist. */
4904 if (in_parm_level_p ())
4906 if (pedantic)
4907 pedwarn ("%s defined inside parms",
4908 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4909 else
4910 warning ("%s defined inside parms",
4911 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4914 if (pedantic)
4916 for (x = fieldlist; x; x = TREE_CHAIN (x))
4917 if (DECL_NAME (x) != 0)
4918 break;
4920 if (x == 0)
4921 pedwarn ("%s has no %s",
4922 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4923 fieldlist ? _("named members") : _("members"));
4926 /* Install struct as DECL_CONTEXT of each field decl.
4927 Also process specified field sizes,m which is found in the DECL_INITIAL.
4928 Store 0 there, except for ": 0" fields (so we can find them
4929 and delete them, below). */
4931 saw_named_field = 0;
4932 for (x = fieldlist; x; x = TREE_CHAIN (x))
4934 DECL_CONTEXT (x) = t;
4935 DECL_PACKED (x) |= TYPE_PACKED (t);
4937 /* If any field is const, the structure type is pseudo-const. */
4938 if (TREE_READONLY (x))
4939 C_TYPE_FIELDS_READONLY (t) = 1;
4940 else
4942 /* A field that is pseudo-const makes the structure likewise. */
4943 tree t1 = TREE_TYPE (x);
4944 while (TREE_CODE (t1) == ARRAY_TYPE)
4945 t1 = TREE_TYPE (t1);
4946 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4947 && C_TYPE_FIELDS_READONLY (t1))
4948 C_TYPE_FIELDS_READONLY (t) = 1;
4951 /* Any field that is volatile means variables of this type must be
4952 treated in some ways as volatile. */
4953 if (TREE_THIS_VOLATILE (x))
4954 C_TYPE_FIELDS_VOLATILE (t) = 1;
4956 /* Any field of nominal variable size implies structure is too. */
4957 if (C_DECL_VARIABLE_SIZE (x))
4958 C_TYPE_VARIABLE_SIZE (t) = 1;
4960 /* Detect invalid nested redefinition. */
4961 if (TREE_TYPE (x) == t)
4962 error ("nested redefinition of `%s'",
4963 IDENTIFIER_POINTER (TYPE_NAME (t)));
4965 /* Detect invalid bit-field size. */
4966 if (DECL_INITIAL (x))
4967 STRIP_NOPS (DECL_INITIAL (x));
4968 if (DECL_INITIAL (x))
4970 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4971 constant_expression_warning (DECL_INITIAL (x));
4972 else
4974 error ("%Hbit-field '%D' width not an integer constant",
4975 &DECL_SOURCE_LOCATION (x), x);
4976 DECL_INITIAL (x) = NULL;
4980 /* Detect invalid bit-field type. */
4981 if (DECL_INITIAL (x)
4982 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4983 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4984 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4986 error ("%Hbit-field '%D' has invalid type",
4987 &DECL_SOURCE_LOCATION (x), x);
4988 DECL_INITIAL (x) = NULL;
4991 if (DECL_INITIAL (x) && pedantic
4992 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4993 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4994 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4995 /* Accept an enum that's equivalent to int or unsigned int. */
4996 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4997 && (TYPE_PRECISION (TREE_TYPE (x))
4998 == TYPE_PRECISION (integer_type_node))))
4999 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
5000 &DECL_SOURCE_LOCATION (x), x);
5002 /* Detect and ignore out of range field width and process valid
5003 field widths. */
5004 if (DECL_INITIAL (x))
5006 int max_width
5007 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
5008 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
5010 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
5011 error ("%Hnegative width in bit-field '%D'",
5012 &DECL_SOURCE_LOCATION (x), x);
5013 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
5014 pedwarn ("%Hwidth of '%D' exceeds its type",
5015 &DECL_SOURCE_LOCATION (x), x);
5016 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5017 error ("%Hzero width for bit-field '%D'",
5018 &DECL_SOURCE_LOCATION (x), x);
5019 else
5021 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5022 unsigned HOST_WIDE_INT width
5023 = tree_low_cst (DECL_INITIAL (x), 1);
5025 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5026 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5027 TREE_UNSIGNED (TREE_TYPE (x)))
5028 || (width
5029 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5030 TREE_UNSIGNED (TREE_TYPE (x))))))
5031 warning ("%H'%D' is narrower than values of its type",
5032 &DECL_SOURCE_LOCATION (x), x);
5034 DECL_SIZE (x) = bitsize_int (width);
5035 DECL_BIT_FIELD (x) = 1;
5036 SET_DECL_C_BIT_FIELD (x);
5040 DECL_INITIAL (x) = 0;
5042 /* Detect flexible array member in an invalid context. */
5043 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5044 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5045 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5046 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5048 if (TREE_CODE (t) == UNION_TYPE)
5049 error ("%Hflexible array member in union",
5050 &DECL_SOURCE_LOCATION (x));
5051 else if (TREE_CHAIN (x) != NULL_TREE)
5052 error ("%Hflexible array member not at end of struct",
5053 &DECL_SOURCE_LOCATION (x));
5054 else if (! saw_named_field)
5055 error ("%Hflexible array member in otherwise empty struct",
5056 &DECL_SOURCE_LOCATION (x));
5059 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5060 && flexible_array_type_p (TREE_TYPE (x)))
5061 pedwarn ("%Hinvalid use of structure with flexible array member",
5062 &DECL_SOURCE_LOCATION (x));
5064 if (DECL_NAME (x))
5065 saw_named_field = 1;
5068 detect_field_duplicates (fieldlist);
5070 /* Now we have the nearly final fieldlist. Record it,
5071 then lay out the structure or union (including the fields). */
5073 TYPE_FIELDS (t) = fieldlist;
5075 layout_type (t);
5077 /* Delete all zero-width bit-fields from the fieldlist */
5079 tree *fieldlistp = &fieldlist;
5080 while (*fieldlistp)
5081 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5082 *fieldlistp = TREE_CHAIN (*fieldlistp);
5083 else
5084 fieldlistp = &TREE_CHAIN (*fieldlistp);
5087 /* Now we have the truly final field list.
5088 Store it in this type and in the variants. */
5090 TYPE_FIELDS (t) = fieldlist;
5092 /* If there are lots of fields, sort so we can look through them fast.
5093 We arbitrarily consider 16 or more elts to be "a lot". */
5096 int len = 0;
5098 for (x = fieldlist; x; x = TREE_CHAIN (x))
5100 if (len > 15 || DECL_NAME (x) == NULL)
5101 break;
5102 len += 1;
5105 if (len > 15)
5107 tree *field_array;
5108 struct lang_type *space;
5109 struct sorted_fields_type *space2;
5111 len += list_length (x);
5113 /* Use the same allocation policy here that make_node uses, to
5114 ensure that this lives as long as the rest of the struct decl.
5115 All decls in an inline function need to be saved. */
5117 space = ggc_alloc (sizeof (struct lang_type));
5118 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5120 len = 0;
5121 space->s = space2;
5122 field_array = &space2->elts[0];
5123 for (x = fieldlist; x; x = TREE_CHAIN (x))
5125 field_array[len++] = x;
5127 /* If there is anonymous struct or union, break out of the loop. */
5128 if (DECL_NAME (x) == NULL)
5129 break;
5131 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5132 if (x == NULL)
5134 TYPE_LANG_SPECIFIC (t) = space;
5135 TYPE_LANG_SPECIFIC (t)->s->len = len;
5136 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5137 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5142 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5144 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5145 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5146 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5147 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5150 /* If this was supposed to be a transparent union, but we can't
5151 make it one, warn and turn off the flag. */
5152 if (TREE_CODE (t) == UNION_TYPE
5153 && TYPE_TRANSPARENT_UNION (t)
5154 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5156 TYPE_TRANSPARENT_UNION (t) = 0;
5157 warning ("union cannot be made transparent");
5160 /* If this structure or union completes the type of any previous
5161 variable declaration, lay it out and output its rtl. */
5162 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5164 x = TREE_CHAIN (x))
5166 tree decl = TREE_VALUE (x);
5167 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5168 layout_array_type (TREE_TYPE (decl));
5169 if (TREE_CODE (decl) != TYPE_DECL)
5171 layout_decl (decl, 0);
5172 if (c_dialect_objc ())
5173 objc_check_decl (decl);
5174 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5175 if (! toplevel)
5176 expand_decl (decl);
5179 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5181 /* Finish debugging output for this type. */
5182 rest_of_type_compilation (t, toplevel);
5184 return t;
5187 /* Lay out the type T, and its element type, and so on. */
5189 static void
5190 layout_array_type (tree t)
5192 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5193 layout_array_type (TREE_TYPE (t));
5194 layout_type (t);
5197 /* Begin compiling the definition of an enumeration type.
5198 NAME is its name (or null if anonymous).
5199 Returns the type object, as yet incomplete.
5200 Also records info about it so that build_enumerator
5201 may be used to declare the individual values as they are read. */
5203 tree
5204 start_enum (tree name)
5206 tree enumtype = 0;
5208 /* If this is the real definition for a previous forward reference,
5209 fill in the contents in the same object that used to be the
5210 forward reference. */
5212 if (name != 0)
5213 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5215 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5217 enumtype = make_node (ENUMERAL_TYPE);
5218 pushtag (name, enumtype);
5221 C_TYPE_BEING_DEFINED (enumtype) = 1;
5223 if (TYPE_VALUES (enumtype) != 0)
5225 /* This enum is a named one that has been declared already. */
5226 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5228 /* Completely replace its old definition.
5229 The old enumerators remain defined, however. */
5230 TYPE_VALUES (enumtype) = 0;
5233 enum_next_value = integer_zero_node;
5234 enum_overflow = 0;
5236 if (flag_short_enums)
5237 TYPE_PACKED (enumtype) = 1;
5239 return enumtype;
5242 /* After processing and defining all the values of an enumeration type,
5243 install their decls in the enumeration type and finish it off.
5244 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5245 and ATTRIBUTES are the specified attributes.
5246 Returns ENUMTYPE. */
5248 tree
5249 finish_enum (tree enumtype, tree values, tree attributes)
5251 tree pair, tem;
5252 tree minnode = 0, maxnode = 0, enum_value_type;
5253 int precision, unsign;
5254 int toplevel = (global_scope == current_scope);
5256 if (in_parm_level_p ())
5257 warning ("enum defined inside parms");
5259 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5261 /* Calculate the maximum value of any enumerator in this type. */
5263 if (values == error_mark_node)
5264 minnode = maxnode = integer_zero_node;
5265 else
5267 minnode = maxnode = TREE_VALUE (values);
5268 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5270 tree value = TREE_VALUE (pair);
5271 if (tree_int_cst_lt (maxnode, value))
5272 maxnode = value;
5273 if (tree_int_cst_lt (value, minnode))
5274 minnode = value;
5278 /* Construct the final type of this enumeration. It is the same
5279 as one of the integral types - the narrowest one that fits, except
5280 that normally we only go as narrow as int - and signed iff any of
5281 the values are negative. */
5282 unsign = (tree_int_cst_sgn (minnode) >= 0);
5283 precision = MAX (min_precision (minnode, unsign),
5284 min_precision (maxnode, unsign));
5285 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5287 tree narrowest = c_common_type_for_size (precision, unsign);
5288 if (narrowest == 0)
5290 warning ("enumeration values exceed range of largest integer");
5291 narrowest = long_long_integer_type_node;
5294 precision = TYPE_PRECISION (narrowest);
5296 else
5297 precision = TYPE_PRECISION (integer_type_node);
5299 if (precision == TYPE_PRECISION (integer_type_node))
5300 enum_value_type = c_common_type_for_size (precision, 0);
5301 else
5302 enum_value_type = enumtype;
5304 TYPE_MIN_VALUE (enumtype) = minnode;
5305 TYPE_MAX_VALUE (enumtype) = maxnode;
5306 TYPE_PRECISION (enumtype) = precision;
5307 TREE_UNSIGNED (enumtype) = unsign;
5308 TYPE_SIZE (enumtype) = 0;
5309 layout_type (enumtype);
5311 if (values != error_mark_node)
5313 /* Change the type of the enumerators to be the enum type. We
5314 need to do this irrespective of the size of the enum, for
5315 proper type checking. Replace the DECL_INITIALs of the
5316 enumerators, and the value slots of the list, with copies
5317 that have the enum type; they cannot be modified in place
5318 because they may be shared (e.g. integer_zero_node) Finally,
5319 change the purpose slots to point to the names of the decls. */
5320 for (pair = values; pair; pair = TREE_CHAIN (pair))
5322 tree enu = TREE_PURPOSE (pair);
5324 TREE_TYPE (enu) = enumtype;
5326 /* The ISO C Standard mandates enumerators to have type int,
5327 even though the underlying type of an enum type is
5328 unspecified. Here we convert any enumerators that fit in
5329 an int to type int, to avoid promotions to unsigned types
5330 when comparing integers with enumerators that fit in the
5331 int range. When -pedantic is given, build_enumerator()
5332 would have already taken care of those that don't fit. */
5333 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5334 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5335 else
5336 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5338 TREE_PURPOSE (pair) = DECL_NAME (enu);
5339 TREE_VALUE (pair) = DECL_INITIAL (enu);
5342 TYPE_VALUES (enumtype) = values;
5345 /* Fix up all variant types of this enum type. */
5346 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5348 if (tem == enumtype)
5349 continue;
5350 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5351 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5352 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5353 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5354 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5355 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5356 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5357 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5358 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5359 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5362 /* Finish debugging output for this type. */
5363 rest_of_type_compilation (enumtype, toplevel);
5365 return enumtype;
5368 /* Build and install a CONST_DECL for one value of the
5369 current enumeration type (one that was begun with start_enum).
5370 Return a tree-list containing the CONST_DECL and its value.
5371 Assignment of sequential values by default is handled here. */
5373 tree
5374 build_enumerator (tree name, tree value)
5376 tree decl, type;
5378 /* Validate and default VALUE. */
5380 /* Remove no-op casts from the value. */
5381 if (value)
5382 STRIP_TYPE_NOPS (value);
5384 if (value != 0)
5386 if (TREE_CODE (value) == INTEGER_CST)
5388 value = default_conversion (value);
5389 constant_expression_warning (value);
5391 else
5393 error ("enumerator value for `%s' not integer constant",
5394 IDENTIFIER_POINTER (name));
5395 value = 0;
5399 /* Default based on previous value. */
5400 /* It should no longer be possible to have NON_LVALUE_EXPR
5401 in the default. */
5402 if (value == 0)
5404 value = enum_next_value;
5405 if (enum_overflow)
5406 error ("overflow in enumeration values");
5409 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5411 pedwarn ("ISO C restricts enumerator values to range of `int'");
5412 value = convert (integer_type_node, value);
5415 /* Set basis for default for next value. */
5416 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5417 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5419 /* Now create a declaration for the enum value name. */
5421 type = TREE_TYPE (value);
5422 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5423 TYPE_PRECISION (integer_type_node)),
5424 (TYPE_PRECISION (type)
5425 >= TYPE_PRECISION (integer_type_node)
5426 && TREE_UNSIGNED (type)));
5428 decl = build_decl (CONST_DECL, name, type);
5429 DECL_INITIAL (decl) = convert (type, value);
5430 pushdecl (decl);
5432 return tree_cons (decl, value, NULL_TREE);
5436 /* Create the FUNCTION_DECL for a function definition.
5437 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5438 the declaration; they describe the function's name and the type it returns,
5439 but twisted together in a fashion that parallels the syntax of C.
5441 This function creates a binding context for the function body
5442 as well as setting up the FUNCTION_DECL in current_function_decl.
5444 Returns 1 on success. If the DECLARATOR is not suitable for a function
5445 (it defines a datum instead), we return 0, which tells
5446 yyparse to report a parse error. */
5449 start_function (tree declspecs, tree declarator, tree attributes)
5451 tree decl1, old_decl;
5452 tree restype;
5453 int old_immediate_size_expand = immediate_size_expand;
5455 current_function_returns_value = 0; /* Assume, until we see it does. */
5456 current_function_returns_null = 0;
5457 current_function_returns_abnormally = 0;
5458 warn_about_return_type = 0;
5459 current_extern_inline = 0;
5461 /* Don't expand any sizes in the return type of the function. */
5462 immediate_size_expand = 0;
5464 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5466 /* If the declarator is not suitable for a function definition,
5467 cause a syntax error. */
5468 if (decl1 == 0)
5470 immediate_size_expand = old_immediate_size_expand;
5471 return 0;
5474 decl_attributes (&decl1, attributes, 0);
5476 if (DECL_DECLARED_INLINE_P (decl1)
5477 && DECL_UNINLINABLE (decl1)
5478 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5479 warning ("%Hinline function '%D' given attribute noinline",
5480 &DECL_SOURCE_LOCATION (decl1), decl1);
5482 announce_function (decl1);
5484 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5486 error ("return type is an incomplete type");
5487 /* Make it return void instead. */
5488 TREE_TYPE (decl1)
5489 = build_function_type (void_type_node,
5490 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5493 if (warn_about_return_type)
5494 pedwarn_c99 ("return type defaults to `int'");
5496 /* Save the parm names or decls from this function's declarator
5497 where store_parm_decls will find them. */
5498 current_function_parms = last_function_parms;
5499 current_function_parm_tags = last_function_parm_tags;
5500 current_function_parm_others = last_function_parm_others;
5502 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5503 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5504 DECL_INITIAL (decl1) = error_mark_node;
5506 /* If this definition isn't a prototype and we had a prototype declaration
5507 before, copy the arg type info from that prototype.
5508 But not if what we had before was a builtin function. */
5509 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5510 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5511 && !DECL_BUILT_IN (old_decl)
5512 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5513 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5514 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5516 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5517 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5520 /* Optionally warn of old-fashioned def with no previous prototype. */
5521 if (warn_strict_prototypes
5522 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5523 && C_DECL_ISNT_PROTOTYPE (old_decl))
5524 warning ("function declaration isn't a prototype");
5525 /* Optionally warn of any global def with no previous prototype. */
5526 else if (warn_missing_prototypes
5527 && TREE_PUBLIC (decl1)
5528 && ! MAIN_NAME_P (DECL_NAME (decl1))
5529 && C_DECL_ISNT_PROTOTYPE (old_decl))
5530 warning ("%Hno previous prototype for '%D'",
5531 &DECL_SOURCE_LOCATION (decl1), decl1);
5532 /* Optionally warn of any def with no previous prototype
5533 if the function has already been used. */
5534 else if (warn_missing_prototypes
5535 && old_decl != 0 && TREE_USED (old_decl)
5536 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5537 warning ("%H'%D' was used with no prototype before its definition",
5538 &DECL_SOURCE_LOCATION (decl1), decl1);
5539 /* Optionally warn of any global def with no previous declaration. */
5540 else if (warn_missing_declarations
5541 && TREE_PUBLIC (decl1)
5542 && old_decl == 0
5543 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5544 warning ("%Hno previous declaration for '%D'",
5545 &DECL_SOURCE_LOCATION (decl1), decl1);
5546 /* Optionally warn of any def with no previous declaration
5547 if the function has already been used. */
5548 else if (warn_missing_declarations
5549 && old_decl != 0 && TREE_USED (old_decl)
5550 && C_DECL_IMPLICIT (old_decl))
5551 warning ("%H`%D' was used with no declaration before its definition",
5552 &DECL_SOURCE_LOCATION (decl1), decl1);
5554 /* This is a definition, not a reference.
5555 So normally clear DECL_EXTERNAL.
5556 However, `extern inline' acts like a declaration
5557 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5558 DECL_EXTERNAL (decl1) = current_extern_inline;
5560 /* This function exists in static storage.
5561 (This does not mean `static' in the C sense!) */
5562 TREE_STATIC (decl1) = 1;
5564 /* A nested function is not global. */
5565 if (current_function_decl != 0)
5566 TREE_PUBLIC (decl1) = 0;
5568 #ifdef ENABLE_CHECKING
5569 /* This is the earliest point at which we might know the assembler
5570 name of the function. Thus, if it's set before this, die horribly. */
5571 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5572 abort ();
5573 #endif
5575 /* If #pragma weak was used, mark the decl weak now. */
5576 if (current_scope == global_scope)
5577 maybe_apply_pragma_weak (decl1);
5579 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5580 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5582 tree args;
5583 int argct = 0;
5584 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5586 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5587 != integer_type_node)
5588 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5590 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5591 args = TREE_CHAIN (args))
5593 tree type = args ? TREE_VALUE (args) : 0;
5595 if (type == void_type_node)
5596 break;
5598 ++argct;
5599 switch (argct)
5601 case 1:
5602 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5603 pedwarn ("%Hfirst argument of '%D' should be `int'",
5604 locus, decl1);
5605 break;
5607 case 2:
5608 if (TREE_CODE (type) != POINTER_TYPE
5609 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5610 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5611 != char_type_node))
5612 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5613 locus, decl1);
5614 break;
5616 case 3:
5617 if (TREE_CODE (type) != POINTER_TYPE
5618 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5619 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5620 != char_type_node))
5621 pedwarn ("%Hthird argument of '%D' should probably be "
5622 "'char **'", locus, decl1);
5623 break;
5627 /* It is intentional that this message does not mention the third
5628 argument because it's only mentioned in an appendix of the
5629 standard. */
5630 if (argct > 0 && (argct < 2 || argct > 3))
5631 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5633 if (! TREE_PUBLIC (decl1))
5634 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5637 /* Record the decl so that the function name is defined.
5638 If we already have a decl for this name, and it is a FUNCTION_DECL,
5639 use the old decl. */
5641 current_function_decl = pushdecl (decl1);
5643 pushlevel (0);
5644 declare_parm_level ();
5646 make_decl_rtl (current_function_decl, NULL);
5648 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5649 /* Promote the value to int before returning it. */
5650 if (c_promoting_integer_type_p (restype))
5652 /* It retains unsignedness if not really getting wider. */
5653 if (TREE_UNSIGNED (restype)
5654 && (TYPE_PRECISION (restype)
5655 == TYPE_PRECISION (integer_type_node)))
5656 restype = unsigned_type_node;
5657 else
5658 restype = integer_type_node;
5660 DECL_RESULT (current_function_decl)
5661 = build_decl (RESULT_DECL, NULL_TREE, restype);
5663 /* If this fcn was already referenced via a block-scope `extern' decl
5664 (or an implicit decl), propagate certain information about the usage. */
5665 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5666 TREE_ADDRESSABLE (current_function_decl) = 1;
5668 immediate_size_expand = old_immediate_size_expand;
5670 start_fname_decls ();
5672 return 1;
5675 /* Subroutine of store_parm_decls which handles new-style function
5676 definitions (prototype format). The parms already have decls, so we
5677 need only record them as in effect and complain if any redundant
5678 old-style parm decls were written. */
5679 static void
5680 store_parm_decls_newstyle (void)
5682 tree decl, last;
5683 tree fndecl = current_function_decl;
5684 tree parms = current_function_parms;
5685 tree tags = current_function_parm_tags;
5686 tree others = current_function_parm_others;
5688 if (current_scope->parms || current_scope->names || current_scope->tags)
5690 error ("%Hold-style parameter declarations in prototyped "
5691 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5693 /* Get rid of the old-style declarations. */
5694 poplevel (0, 0, 0);
5695 pushlevel (0);
5698 /* Now make all the parameter declarations visible in the function body.
5699 We can bypass most of the grunt work of pushdecl. */
5700 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5702 DECL_CONTEXT (decl) = current_function_decl;
5703 if (DECL_NAME (decl) == 0)
5704 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5705 else
5707 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5708 current_scope->shadowed
5709 = tree_cons (DECL_NAME (decl),
5710 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5711 current_scope->shadowed);
5712 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5715 current_scope->parms = parms;
5716 current_scope->parms_last = last;
5718 /* Record the parameter list in the function declaration. */
5719 DECL_ARGUMENTS (fndecl) = parms;
5721 /* Now make all the ancillary declarations visible, likewise. */
5722 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5724 DECL_CONTEXT (decl) = current_function_decl;
5725 if (DECL_NAME (decl)
5726 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5728 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5729 current_scope->shadowed
5730 = tree_cons (DECL_NAME (decl),
5731 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5732 current_scope->shadowed);
5733 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5736 current_scope->names = others;
5737 current_scope->names_last = last;
5739 /* And all the tag declarations. */
5740 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5741 if (TREE_PURPOSE (decl))
5743 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5744 current_scope->shadowed_tags
5745 = tree_cons (TREE_PURPOSE (decl),
5746 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5747 current_scope->shadowed_tags);
5748 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5750 current_scope->tags = tags;
5753 /* Subroutine of store_parm_decls which handles old-style function
5754 definitions (separate parameter list and declarations). */
5756 static void
5757 store_parm_decls_oldstyle (void)
5759 tree parm, decl, last;
5760 tree fndecl = current_function_decl;
5762 /* This is the identifier list from the function declarator. */
5763 tree parmids = current_function_parms;
5765 /* We use DECL_WEAK as a flag to show which parameters have been
5766 seen already, since it is not used on PARM_DECL. */
5767 #ifdef ENABLE_CHECKING
5768 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5769 if (DECL_WEAK (parm))
5770 abort ();
5771 #endif
5773 /* Match each formal parameter name with its declaration. Save each
5774 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5775 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5777 if (TREE_VALUE (parm) == 0)
5779 error ("%Hparameter name missing from parameter list",
5780 &DECL_SOURCE_LOCATION (fndecl));
5781 TREE_PURPOSE (parm) = 0;
5782 continue;
5785 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5786 if (decl && DECL_CONTEXT (decl) == fndecl)
5788 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5789 /* If we got something other than a PARM_DECL it is an error. */
5790 if (TREE_CODE (decl) != PARM_DECL)
5791 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5792 /* If the declaration is already marked, we have a duplicate
5793 name. Complain and ignore the duplicate. */
5794 else if (DECL_WEAK (decl))
5796 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5797 TREE_PURPOSE (parm) = 0;
5798 continue;
5800 /* If the declaration says "void", complain and turn it into
5801 an int. */
5802 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5804 error ("%Hparameter \"%D\" declared void", locus, decl);
5805 TREE_TYPE (decl) = integer_type_node;
5806 DECL_ARG_TYPE (decl) = integer_type_node;
5807 layout_decl (decl, 0);
5810 /* If no declaration found, default to int. */
5811 else
5813 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5814 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5815 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5816 DECL_SOURCE_LOCATION (decl) = *locus;
5817 pushdecl (decl);
5819 if (flag_isoc99)
5820 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5821 else if (extra_warnings)
5822 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5825 TREE_PURPOSE (parm) = decl;
5826 DECL_WEAK (decl) = 1;
5829 /* Now examine the parms chain for incomplete declarations
5830 and declarations with no corresponding names. */
5832 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5834 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5836 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5838 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5839 TREE_TYPE (parm) = error_mark_node;
5842 if (! DECL_WEAK (parm))
5844 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5845 locus, parm);
5847 /* Pretend the parameter was not missing.
5848 This gets us to a standard state and minimizes
5849 further error messages. */
5850 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5854 /* Chain the declarations together in the order of the list of
5855 names. Store that chain in the function decl, replacing the
5856 list of names. Update the current scope to match. */
5857 DECL_ARGUMENTS (fndecl) = 0;
5859 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5860 if (TREE_PURPOSE (parm))
5861 break;
5862 if (parm && TREE_PURPOSE (parm))
5864 last = TREE_PURPOSE (parm);
5865 DECL_ARGUMENTS (fndecl) = last;
5866 current_scope->parms = last;
5867 DECL_WEAK (last) = 0;
5869 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5870 if (TREE_PURPOSE (parm))
5872 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5873 last = TREE_PURPOSE (parm);
5874 DECL_WEAK (last) = 0;
5876 current_scope->parms_last = last;
5877 TREE_CHAIN (last) = 0;
5880 /* If there was a previous prototype,
5881 set the DECL_ARG_TYPE of each argument according to
5882 the type previously specified, and report any mismatches. */
5884 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5886 tree type;
5887 for (parm = DECL_ARGUMENTS (fndecl),
5888 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5889 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5890 != void_type_node));
5891 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5893 if (parm == 0 || type == 0
5894 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5896 error ("number of arguments doesn't match prototype");
5897 error ("%Hprototype declaration",
5898 &current_function_prototype_locus);
5899 break;
5901 /* Type for passing arg must be consistent with that
5902 declared for the arg. ISO C says we take the unqualified
5903 type for parameters declared with qualified type. */
5904 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5905 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5906 COMPARE_STRICT))
5908 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5909 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5911 /* Adjust argument to match prototype. E.g. a previous
5912 `int foo(float);' prototype causes
5913 `int foo(x) float x; {...}' to be treated like
5914 `int foo(float x) {...}'. This is particularly
5915 useful for argument types like uid_t. */
5916 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5918 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5919 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5920 && TYPE_PRECISION (TREE_TYPE (parm))
5921 < TYPE_PRECISION (integer_type_node))
5922 DECL_ARG_TYPE (parm) = integer_type_node;
5924 if (pedantic)
5926 pedwarn ("promoted argument \"%D\" "
5927 "doesn't match prototype", parm);
5928 pedwarn ("%Hprototype declaration",
5929 &current_function_prototype_locus);
5932 else
5934 error ("argument \"%D\" doesn't match prototype", parm);
5935 error ("%Hprototype declaration",
5936 &current_function_prototype_locus);
5940 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5943 /* Otherwise, create a prototype that would match. */
5945 else
5947 tree actual = 0, last = 0, type;
5949 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5951 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5952 if (last)
5953 TREE_CHAIN (last) = type;
5954 else
5955 actual = type;
5956 last = type;
5958 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5959 if (last)
5960 TREE_CHAIN (last) = type;
5961 else
5962 actual = type;
5964 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5965 of the type of this function, but we need to avoid having this
5966 affect the types of other similarly-typed functions, so we must
5967 first force the generation of an identical (but separate) type
5968 node for the relevant function type. The new node we create
5969 will be a variant of the main variant of the original function
5970 type. */
5972 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5974 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5978 /* Store the parameter declarations into the current function declaration.
5979 This is called after parsing the parameter declarations, before
5980 digesting the body of the function.
5982 For an old-style definition, construct a prototype out of the old-style
5983 parameter declarations and inject it into the function's type. */
5985 void
5986 store_parm_decls (void)
5988 tree fndecl = current_function_decl;
5990 /* The function containing FNDECL, if any. */
5991 tree context = decl_function_context (fndecl);
5993 /* True if this definition is written with a prototype. */
5994 bool prototype = (current_function_parms
5995 && TREE_CODE (current_function_parms) != TREE_LIST);
5997 if (prototype)
5998 store_parm_decls_newstyle ();
5999 else
6000 store_parm_decls_oldstyle ();
6002 /* The next call to pushlevel will be a function body. */
6004 next_is_function_body = true;
6006 /* Write a record describing this function definition to the prototypes
6007 file (if requested). */
6009 gen_aux_info_record (fndecl, 1, 0, prototype);
6011 /* Initialize the RTL code for the function. */
6012 allocate_struct_function (fndecl);
6014 /* Begin the statement tree for this function. */
6015 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6017 /* If this is a nested function, save away the sizes of any
6018 variable-size types so that we can expand them when generating
6019 RTL. */
6020 if (context)
6022 tree t;
6024 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6025 = nreverse (get_pending_sizes ());
6026 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6028 t = TREE_CHAIN (t))
6029 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6032 /* This function is being processed in whole-function mode. */
6033 cfun->x_whole_function_mode_p = 1;
6035 /* Even though we're inside a function body, we still don't want to
6036 call expand_expr to calculate the size of a variable-sized array.
6037 We haven't necessarily assigned RTL to all variables yet, so it's
6038 not safe to try to expand expressions involving them. */
6039 immediate_size_expand = 0;
6040 cfun->x_dont_save_pending_sizes_p = 1;
6043 /* Finish up a function declaration and compile that function
6044 all the way to assembler language output. The free the storage
6045 for the function definition.
6047 This is called after parsing the body of the function definition. */
6049 void
6050 finish_function ()
6052 tree fndecl = current_function_decl;
6054 /* When a function declaration is totally empty, e.g.
6055 void foo(void) { }
6056 (the argument list is irrelevant) the compstmt rule will not
6057 bother calling pushlevel/poplevel, which means we get here with
6058 the scope stack out of sync. Detect this situation by noticing
6059 that current_scope is still as store_parm_decls left it, and do
6060 a dummy push/pop to get back to consistency.
6061 Note that the call to pushlevel does not actually push another
6062 scope - see there for details. */
6064 if (current_scope->parm_flag && next_is_function_body)
6066 pushlevel (0);
6067 poplevel (0, 0, 0);
6070 if (TREE_CODE (fndecl) == FUNCTION_DECL
6071 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6073 tree args = DECL_ARGUMENTS (fndecl);
6074 for (; args; args = TREE_CHAIN (args))
6076 tree type = TREE_TYPE (args);
6077 if (INTEGRAL_TYPE_P (type)
6078 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6079 DECL_ARG_TYPE (args) = integer_type_node;
6083 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6085 /* Must mark the RESULT_DECL as being in this function. */
6087 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6089 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6091 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6092 != integer_type_node)
6094 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6095 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6096 if (!warn_main)
6097 pedwarn ("%Hreturn type of '%D' is not `int'",
6098 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6100 else
6102 #ifdef DEFAULT_MAIN_RETURN
6103 /* Make it so that `main' always returns success by default. */
6104 DEFAULT_MAIN_RETURN;
6105 #else
6106 if (flag_isoc99)
6107 c_expand_return (integer_zero_node);
6108 #endif
6112 finish_fname_decls ();
6114 /* Tie off the statement tree for this function. */
6115 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6117 /* Complain if there's just no return statement. */
6118 if (warn_return_type
6119 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6120 && !current_function_returns_value && !current_function_returns_null
6121 /* Don't complain if we abort. */
6122 && !current_function_returns_abnormally
6123 /* Don't warn for main(). */
6124 && !MAIN_NAME_P (DECL_NAME (fndecl))
6125 /* Or if they didn't actually specify a return type. */
6126 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6127 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6128 inline function, as we might never be compiled separately. */
6129 && DECL_INLINE (fndecl))
6130 warning ("no return statement in function returning non-void");
6132 /* With just -Wextra, complain only if function returns both with
6133 and without a value. */
6134 if (extra_warnings
6135 && current_function_returns_value
6136 && current_function_returns_null)
6137 warning ("this function may return with or without a value");
6139 /* We're leaving the context of this function, so zap cfun. It's still in
6140 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6141 cfun = NULL;
6143 /* ??? Objc emits functions after finalizing the compilation unit.
6144 This should be cleaned up later and this conditional removed. */
6145 if (!cgraph_global_info_ready)
6146 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6147 else
6148 c_expand_body (fndecl);
6149 current_function_decl = NULL;
6152 /* Generate the RTL for a deferred function FNDECL. */
6154 void
6155 c_expand_deferred_function (tree fndecl)
6157 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6158 function was deferred, e.g. in duplicate_decls. */
6159 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6161 if (flag_inline_trees)
6163 timevar_push (TV_INTEGRATION);
6164 optimize_inline_calls (fndecl);
6165 timevar_pop (TV_INTEGRATION);
6167 c_expand_body (fndecl);
6168 current_function_decl = NULL;
6172 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6173 then we are already in the process of generating RTL for another
6174 function. */
6176 static void
6177 c_expand_body_1 (tree fndecl, int nested_p)
6179 if (nested_p)
6181 /* Make sure that we will evaluate variable-sized types involved
6182 in our function's type. */
6183 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6186 tree_rest_of_compilation (fndecl);
6188 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6190 if (targetm.have_ctors_dtors)
6191 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6192 DEFAULT_INIT_PRIORITY);
6193 else
6194 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6197 if (DECL_STATIC_DESTRUCTOR (fndecl))
6199 if (targetm.have_ctors_dtors)
6200 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6201 DEFAULT_INIT_PRIORITY);
6202 else
6203 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6207 /* Like c_expand_body_1 but only for unnested functions. */
6209 void
6210 c_expand_body (tree fndecl)
6212 c_expand_body_1 (fndecl, 0);
6215 /* Check the declarations given in a for-loop for satisfying the C99
6216 constraints. */
6217 void
6218 check_for_loop_decls (void)
6220 tree t;
6222 if (!flag_isoc99)
6224 /* If we get here, declarations have been used in a for loop without
6225 the C99 for loop scope. This doesn't make much sense, so don't
6226 allow it. */
6227 error ("'for' loop initial declaration used outside C99 mode");
6228 return;
6230 /* C99 subclause 6.8.5 paragraph 3:
6232 [#3] The declaration part of a for statement shall only
6233 declare identifiers for objects having storage class auto or
6234 register.
6236 It isn't clear whether, in this sentence, "identifiers" binds to
6237 "shall only declare" or to "objects" - that is, whether all identifiers
6238 declared must be identifiers for objects, or whether the restriction
6239 only applies to those that are. (A question on this in comp.std.c
6240 in November 2000 received no answer.) We implement the strictest
6241 interpretation, to avoid creating an extension which later causes
6242 problems. */
6244 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6246 if (TREE_PURPOSE (t) != 0)
6248 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6250 if (code == RECORD_TYPE)
6251 error ("'struct %s' declared in 'for' loop initial declaration",
6252 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6253 else if (code == UNION_TYPE)
6254 error ("'union %s' declared in 'for' loop initial declaration",
6255 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6256 else
6257 error ("'enum %s' declared in 'for' loop initial declaration",
6258 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6262 for (t = getdecls (); t; t = TREE_CHAIN (t))
6264 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6265 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6266 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6267 "initial declaration", locus, t);
6268 else if (TREE_STATIC (t))
6269 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6270 "initial declaration", locus, t);
6271 else if (DECL_EXTERNAL (t))
6272 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6273 "initial declaration", locus, t);
6277 /* Save and restore the variables in this file and elsewhere
6278 that keep track of the progress of compilation of the current function.
6279 Used for nested functions. */
6281 struct language_function GTY(())
6283 struct c_language_function base;
6284 int returns_value;
6285 int returns_null;
6286 int returns_abnormally;
6287 int warn_about_return_type;
6288 int extern_inline;
6291 /* Save and reinitialize the variables
6292 used during compilation of a C function. */
6294 void
6295 c_push_function_context (struct function *f)
6297 struct language_function *p;
6298 p = ggc_alloc (sizeof (struct language_function));
6299 f->language = p;
6301 p->base.x_stmt_tree = c_stmt_tree;
6302 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6303 p->returns_value = current_function_returns_value;
6304 p->returns_null = current_function_returns_null;
6305 p->returns_abnormally = current_function_returns_abnormally;
6306 p->warn_about_return_type = warn_about_return_type;
6307 p->extern_inline = current_extern_inline;
6310 /* Restore the variables used during compilation of a C function. */
6312 void
6313 c_pop_function_context (struct function *f)
6315 struct language_function *p = f->language;
6317 if (DECL_SAVED_INSNS (current_function_decl) == 0
6318 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6320 /* Stop pointing to the local nodes about to be freed. */
6321 /* But DECL_INITIAL must remain nonzero so we know this
6322 was an actual function definition. */
6323 DECL_INITIAL (current_function_decl) = error_mark_node;
6324 DECL_ARGUMENTS (current_function_decl) = 0;
6327 c_stmt_tree = p->base.x_stmt_tree;
6328 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6329 current_function_returns_value = p->returns_value;
6330 current_function_returns_null = p->returns_null;
6331 current_function_returns_abnormally = p->returns_abnormally;
6332 warn_about_return_type = p->warn_about_return_type;
6333 current_extern_inline = p->extern_inline;
6335 f->language = NULL;
6338 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6340 void
6341 c_dup_lang_specific_decl (tree decl)
6343 struct lang_decl *ld;
6345 if (!DECL_LANG_SPECIFIC (decl))
6346 return;
6348 ld = ggc_alloc (sizeof (struct lang_decl));
6349 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6350 DECL_LANG_SPECIFIC (decl) = ld;
6353 /* The functions below are required for functionality of doing
6354 function at once processing in the C front end. Currently these
6355 functions are not called from anywhere in the C front end, but as
6356 these changes continue, that will change. */
6358 /* Returns nonzero if the current statement is a full expression,
6359 i.e. temporaries created during that statement should be destroyed
6360 at the end of the statement. */
6363 stmts_are_full_exprs_p (void)
6365 return 0;
6368 /* Returns the stmt_tree (if any) to which statements are currently
6369 being added. If there is no active statement-tree, NULL is
6370 returned. */
6372 stmt_tree
6373 current_stmt_tree (void)
6375 return &c_stmt_tree;
6378 /* Returns the stack of SCOPE_STMTs for the current function. */
6380 tree *
6381 current_scope_stmt_stack (void)
6383 return &c_scope_stmt_stack;
6386 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6387 C. */
6390 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6392 return 0;
6395 /* Dummy function in place of callback used by C++. */
6397 void
6398 extract_interface_info (void)
6402 /* Return a new COMPOUND_STMT, after adding it to the current
6403 statement tree. */
6405 tree
6406 c_begin_compound_stmt (void)
6408 tree stmt;
6410 /* Create the COMPOUND_STMT. */
6411 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6413 return stmt;
6416 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6417 common code. */
6419 void
6420 c_expand_decl_stmt (tree t)
6422 tree decl = DECL_STMT_DECL (t);
6424 /* Expand nested functions. */
6425 if (TREE_CODE (decl) == FUNCTION_DECL
6426 && DECL_CONTEXT (decl) == current_function_decl
6427 && DECL_SAVED_TREE (decl))
6428 c_expand_body_1 (decl, 1);
6431 /* Return the global value of T as a symbol. */
6433 tree
6434 identifier_global_value (tree t)
6436 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6437 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6438 return decl;
6440 /* Shadowed by something else; find the true global value. */
6441 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6442 if (DECL_NAME (decl) == t)
6443 return decl;
6445 /* Only local values for this decl. */
6446 return 0;
6449 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6450 otherwise the name is found in ridpointers from RID_INDEX. */
6452 void
6453 record_builtin_type (enum rid rid_index, const char *name, tree type)
6455 tree id;
6456 if (name == 0)
6457 id = ridpointers[(int) rid_index];
6458 else
6459 id = get_identifier (name);
6460 pushdecl (build_decl (TYPE_DECL, id, type));
6463 /* Build the void_list_node (void_type_node having been created). */
6464 tree
6465 build_void_list_node (void)
6467 tree t = build_tree_list (NULL_TREE, void_type_node);
6468 return t;
6471 /* Return something to represent absolute declarators containing a *.
6472 TARGET is the absolute declarator that the * contains.
6473 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6474 to apply to the pointer type, represented as identifiers, possible mixed
6475 with attributes.
6477 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6478 if attributes are present) and whose type is the modifier list. */
6480 tree
6481 make_pointer_declarator (tree type_quals_attrs, tree target)
6483 tree quals, attrs;
6484 tree itarget = target;
6485 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6486 if (attrs != NULL_TREE)
6487 itarget = tree_cons (attrs, target, NULL_TREE);
6488 return build1 (INDIRECT_REF, quals, itarget);
6491 /* A wrapper around lhd_set_decl_assembler_name that gives static
6492 variables their C names if they are at file scope and only one
6493 translation unit is being compiled, for backwards compatibility
6494 with certain bizarre assembler hacks (like crtstuff.c). */
6496 void
6497 c_static_assembler_name (tree decl)
6499 if (num_in_fnames == 1
6500 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6501 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6502 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6503 else
6504 lhd_set_decl_assembler_name (decl);
6507 /* Hash and equality functions for link_hash_table: key off
6508 DECL_ASSEMBLER_NAME. */
6510 static hashval_t
6511 link_hash_hash (const void *x_p)
6513 tree x = (tree)x_p;
6514 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6517 static int
6518 link_hash_eq (const void *x1_p, const void *x2_p)
6520 tree x1 = (tree)x1_p;
6521 tree x2 = (tree)x2_p;
6522 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6525 /* Propagate information between definitions and uses between multiple
6526 translation units in TU_LIST based on linkage rules. */
6528 void
6529 merge_translation_unit_decls (void)
6531 const tree tu_list = current_file_decl;
6532 tree tu;
6533 tree decl;
6534 htab_t link_hash_table;
6535 tree block;
6537 /* Create the BLOCK that poplevel would have created, but don't
6538 actually call poplevel since that's expensive. */
6539 block = make_node (BLOCK);
6540 BLOCK_VARS (block) = current_scope->names;
6541 TREE_USED (block) = 1;
6542 DECL_INITIAL (current_file_decl) = block;
6544 /* If only one translation unit seen, no copying necessary. */
6545 if (TREE_CHAIN (tu_list) == NULL_TREE)
6546 return;
6548 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6550 /* Enter any actual definitions into the hash table. */
6551 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6552 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6553 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6555 PTR *slot;
6556 slot = htab_find_slot (link_hash_table, decl, INSERT);
6558 /* If we've already got a definition, work out which one is
6559 the real one, put it into the hash table, and make the
6560 other one DECL_EXTERNAL. This is important to avoid
6561 putting out two definitions of the same symbol in the
6562 assembly output. */
6563 if (*slot != NULL)
6565 tree old_decl = (tree) *slot;
6567 /* If this is weak or common or whatever, suppress it
6568 in favor of the other definition. */
6569 if (DECL_WEAK (decl))
6570 DECL_EXTERNAL (decl) = 1;
6571 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6572 DECL_EXTERNAL (old_decl) = 1;
6573 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6574 DECL_EXTERNAL (decl) = 1;
6575 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6576 DECL_EXTERNAL (old_decl) = 1;
6578 if (DECL_EXTERNAL (decl))
6580 DECL_INITIAL (decl) = NULL_TREE;
6581 DECL_COMMON (decl) = 0;
6582 DECL_ONE_ONLY (decl) = 0;
6583 DECL_WEAK (decl) = 0;
6585 else if (DECL_EXTERNAL (old_decl))
6587 DECL_INITIAL (old_decl) = NULL_TREE;
6588 DECL_COMMON (old_decl) = 0;
6589 DECL_ONE_ONLY (old_decl) = 0;
6590 DECL_WEAK (old_decl) = 0;
6591 *slot = decl;
6593 else
6595 error ("%Hredefinition of global '%D'",
6596 &DECL_SOURCE_LOCATION (decl), decl);
6597 error ("%H'%D' previously defined here",
6598 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6601 else
6602 *slot = decl;
6605 /* Now insert the desired information from all the definitions
6606 into any plain declarations. */
6607 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6608 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6609 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6611 tree global_decl;
6612 global_decl = htab_find (link_hash_table, decl);
6614 if (! global_decl)
6615 continue;
6617 /* Print any appropriate error messages, and partially merge
6618 the decls. */
6619 (void) duplicate_decls (decl, global_decl, true, true);
6622 htab_delete (link_hash_table);
6625 /* Perform final processing on file-scope data. */
6627 void
6628 c_write_global_declarations(void)
6630 tree link;
6632 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6634 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6635 int len = list_length (globals);
6636 tree *vec = xmalloc (sizeof (tree) * len);
6637 int i;
6638 tree decl;
6640 /* Process the decls in the order they were written. */
6642 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6643 vec[i] = decl;
6645 wrapup_global_declarations (vec, len);
6647 check_global_declarations (vec, len);
6649 /* Clean up. */
6650 free (vec);
6654 /* Reset the parser's state in preparation for a new file. */
6656 void
6657 c_reset_state (void)
6659 tree link;
6660 tree file_scope_decl;
6662 /* Pop the global scope. */
6663 if (current_scope != global_scope)
6664 current_scope = global_scope;
6665 file_scope_decl = current_file_decl;
6666 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6667 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6668 truly_local_externals = NULL_TREE;
6670 /* Start a new global binding level. */
6671 pushlevel (0);
6672 global_scope = current_scope;
6673 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6674 TREE_CHAIN (current_file_decl) = file_scope_decl;
6676 /* Reintroduce the builtin declarations. */
6677 for (link = first_builtin_decl;
6678 link != TREE_CHAIN (last_builtin_decl);
6679 link = TREE_CHAIN (link))
6680 pushdecl (copy_node (link));
6683 #include "gt-c-decl.h"