configure.ac: GCC_NO_EXECUTABLES was supposed to be commented in the patch from 3...
[official-gcc.git] / gcc / c-decl.c
blobac2089c2e26c264b5d6bb4e6a957083dc6aef5f3
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, shadowed_tags, and incomplete
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 /* Variable declarations with incomplete type in this scope. */
229 tree incomplete;
231 /* True if we are currently filling this scope with parameter
232 declarations. */
233 bool parm_flag : 1;
235 /* True if we already complained about forward parameter decls
236 in this scope. This prevents double warnings on
237 foo (int a; int b; ...) */
238 bool warned_forward_parm_decls : 1;
240 /* True if this is the outermost block scope of a function body.
241 This scope contains the parameters, the local variables declared
242 in the outermost block, and all the labels (except those in
243 nested functions, or declared at block scope with __label__). */
244 bool function_body : 1;
246 /* True means make a BLOCK for this scope no matter what. */
247 bool keep : 1;
250 /* The scope currently in effect. */
252 static GTY(()) struct c_scope *current_scope;
254 /* A chain of c_scope structures awaiting reuse. */
256 static GTY((deletable (""))) struct c_scope *scope_freelist;
258 /* The innermost function scope. Ordinary (not explicitly declared)
259 labels, bindings to error_mark_node, and the lazily-created
260 bindings of __func__ and its friends get this scope. */
262 static GTY(()) struct c_scope *current_function_scope;
264 /* The outermost scope, corresponding to the C "file scope". This is
265 created when the compiler is started and exists through the entire run. */
267 static GTY(()) struct c_scope *global_scope;
269 /* Append VAR to LIST in scope SCOPE. */
270 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
271 struct c_scope *s_ = (scope); \
272 tree d_ = (decl); \
273 if (s_->list##_last) \
274 TREE_CHAIN (s_->list##_last) = d_; \
275 else \
276 s_->list = d_; \
277 s_->list##_last = d_; \
278 } while (0)
280 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
281 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
282 struct c_scope *t_ = (tscope); \
283 struct c_scope *f_ = (fscope); \
284 if (t_->to##_last) \
285 TREE_CHAIN (t_->to##_last) = f_->from; \
286 else \
287 t_->to = f_->from; \
288 t_->to##_last = f_->from##_last; \
289 } while (0)
291 /* True means unconditionally make a BLOCK for the next scope pushed. */
293 static bool keep_next_level_flag;
295 /* True means the next call to pushlevel will be the outermost scope
296 of a function body, so do not push a new scope, merely cease
297 expecting parameter decls. */
299 static bool next_is_function_body;
301 /* Functions called automatically at the beginning and end of execution. */
303 tree static_ctors, static_dtors;
305 /* Forward declarations. */
307 static struct c_scope *make_scope (void);
308 static void pop_scope (void);
309 static tree match_builtin_function_types (tree, tree);
310 static int duplicate_decls (tree, tree, int, int);
311 static int redeclaration_error_message (tree, tree);
312 static tree make_label (tree, location_t);
313 static void bind_label (tree, tree, struct c_scope *);
314 static void implicit_decl_warning (tree);
315 static tree lookup_tag (enum tree_code, tree, int);
316 static tree lookup_name_current_level (tree);
317 static tree grokdeclarator (tree, tree, enum decl_context, int);
318 static tree grokparms (tree, int);
319 static void layout_array_type (tree);
320 static void store_parm_decls_newstyle (void);
321 static void store_parm_decls_oldstyle (void);
322 static tree c_make_fname_decl (tree, int);
323 static void c_expand_body_1 (tree, int);
324 static tree any_external_decl (tree);
325 static void record_external_decl (tree);
326 static void warn_if_shadowing (tree, tree);
327 static void clone_underlying_type (tree);
328 static bool flexible_array_type_p (tree);
329 static hashval_t link_hash_hash (const void *);
330 static int link_hash_eq (const void *, const void *);
332 /* States indicating how grokdeclarator() should handle declspecs marked
333 with __attribute__((deprecated)). An object declared as
334 __attribute__((deprecated)) suppresses warnings of uses of other
335 deprecated items. */
337 enum deprecated_states {
338 DEPRECATED_NORMAL,
339 DEPRECATED_SUPPRESS
342 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
344 void
345 c_print_identifier (FILE *file, tree node, int indent)
347 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
348 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
349 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
350 if (C_IS_RESERVED_WORD (node))
352 tree rid = ridpointers[C_RID_CODE (node)];
353 indent_to (file, indent + 4);
354 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
355 (void *) rid, IDENTIFIER_POINTER (rid));
359 /* Hook called at end of compilation to assume 1 elt
360 for a file-scope tentative array defn that wasn't complete before. */
362 void
363 c_finish_incomplete_decl (tree decl)
365 if (TREE_CODE (decl) == VAR_DECL)
367 tree type = TREE_TYPE (decl);
368 if (type != error_mark_node
369 && TREE_CODE (type) == ARRAY_TYPE
370 && ! DECL_EXTERNAL (decl)
371 && TYPE_DOMAIN (type) == 0)
373 warning ("%Harray '%D' assumed to have one element",
374 &DECL_SOURCE_LOCATION (decl), decl);
376 complete_array_type (type, NULL_TREE, 1);
378 layout_decl (decl, 0);
383 /* Reuse or create a struct for this scope. */
385 static struct c_scope *
386 make_scope (void)
388 struct c_scope *result;
389 if (scope_freelist)
391 result = scope_freelist;
392 scope_freelist = result->outer;
394 else
395 result = ggc_alloc_cleared (sizeof (struct c_scope));
397 return result;
400 /* Remove the topmost scope from the stack and add it to the
401 free list, updating current_function_scope if necessary. */
403 static void
404 pop_scope (void)
406 struct c_scope *scope = current_scope;
408 current_scope = scope->outer;
409 if (scope->function_body)
410 current_function_scope = scope->outer_function;
412 memset (scope, 0, sizeof (struct c_scope));
413 scope->outer = scope_freelist;
414 scope_freelist = scope;
417 /* Nonzero if we are currently in the global scope. */
420 global_bindings_p (void)
422 return current_scope == global_scope;
425 void
426 keep_next_level (void)
428 keep_next_level_flag = true;
431 /* Identify this scope as currently being filled with parameters. */
433 void
434 declare_parm_level (void)
436 current_scope->parm_flag = true;
439 /* Nonzero if currently making parm declarations. */
442 in_parm_level_p (void)
444 return current_scope->parm_flag;
447 /* Enter a new scope. The dummy parameter is for signature
448 compatibility with lang_hooks.decls.pushlevel. */
450 void
451 pushlevel (int dummy ATTRIBUTE_UNUSED)
453 if (next_is_function_body)
455 /* This is the transition from the parameters to the top level
456 of the function body. These are the same scope
457 (C99 6.2.1p4,6) so we do not push another scope structure.
458 next_is_function_body is set only by store_parm_decls, which
459 in turn is called when and only when we are about to
460 encounter the opening curly brace for the function body.
462 The outermost block of a function always gets a BLOCK node,
463 because the debugging output routines expect that each
464 function has at least one BLOCK. */
465 current_scope->parm_flag = false;
466 current_scope->function_body = true;
467 current_scope->keep = true;
468 current_scope->outer_function = current_function_scope;
469 current_function_scope = current_scope;
471 keep_next_level_flag = false;
472 next_is_function_body = false;
474 else
476 struct c_scope *scope = make_scope ();
478 scope->keep = keep_next_level_flag;
479 scope->outer = current_scope;
480 current_scope = scope;
481 keep_next_level_flag = false;
485 /* Exit a scope. Restore the state of the identifier-decl mappings
486 that were in effect when this scope was entered.
488 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
489 create a BLOCK node to record its declarations and subblocks for
490 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
491 or tags lists are nonempty.
493 The second parameter is ignored; it is present only for
494 signature compatibility with lang_hooks.decls.poplevel.
496 If FUNCTIONBODY is nonzero, this level is the body of a function,
497 even if current_scope->function_body is not set. This is used
498 by language-independent code that generates synthetic functions,
499 and cannot set current_scope->function_body.
501 FIXME: Eliminate the need for all arguments. */
503 tree
504 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
506 struct c_scope *scope = current_scope;
507 tree block;
508 tree decl;
509 tree p;
511 scope->function_body |= functionbody;
513 if (keep == KEEP_MAYBE)
514 keep = (scope->names || scope->tags);
516 keep |= scope->keep;
517 keep |= scope->function_body;
519 /* If appropriate, create a BLOCK to record the decls for the life
520 of this function. */
521 block = 0;
522 if (keep)
524 block = make_node (BLOCK);
525 BLOCK_VARS (block) = scope->names;
526 BLOCK_SUBBLOCKS (block) = scope->blocks;
527 TREE_USED (block) = 1;
530 /* In each subblock, record that this is its superior. */
531 for (p = scope->blocks; p; p = TREE_CHAIN (p))
532 BLOCK_SUPERCONTEXT (p) = block;
534 /* Clear out the variable bindings in this scope.
536 Propagate TREE_ADDRESSABLE from nested functions to their
537 containing functions.
539 Issue warnings for unused variables and labels, and errors for
540 undefined labels, if there are any. */
542 for (p = scope->names; p; p = TREE_CHAIN (p))
544 const location_t *locus = &DECL_SOURCE_LOCATION (p);
546 switch (TREE_CODE (p))
548 case LABEL_DECL:
549 if (TREE_USED (p) && !DECL_INITIAL (p))
551 error ("%Hlabel `%D' used but not defined", locus, p);
552 DECL_INITIAL (p) = error_mark_node;
554 else if (!TREE_USED (p) && warn_unused_label)
556 if (DECL_INITIAL (p))
557 warning ("%Hlabel `%D' defined but not used", locus, p);
558 else
559 warning ("%Hlabel `%D' declared but not defined", locus, p);
562 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
563 break;
565 case FUNCTION_DECL:
566 if (! TREE_ASM_WRITTEN (p)
567 && DECL_INITIAL (p) != 0
568 && TREE_ADDRESSABLE (p)
569 && DECL_ABSTRACT_ORIGIN (p) != 0
570 && DECL_ABSTRACT_ORIGIN (p) != p)
571 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
572 goto normal;
574 case VAR_DECL:
575 /* keep this in sync with stmt.c:warn_about_unused_variables.
576 No warnings when the global scope is popped because the
577 global scope isn't popped for the last translation unit,
578 so the warnings are done in c_write_global_declaration. */
579 if (warn_unused_variable && scope != global_scope
580 && !TREE_USED (p)
581 && !DECL_IN_SYSTEM_HEADER (p)
582 && DECL_NAME (p)
583 && !DECL_ARTIFICIAL (p))
584 warning ("%Hunused variable `%D'", locus, p);
585 /* fall through */
587 default:
588 normal:
589 if (DECL_NAME (p))
591 if (DECL_EXTERNAL (p) && scope != global_scope)
592 /* External decls stay in the symbol-value slot but are
593 inaccessible. */
594 C_DECL_INVISIBLE (p) = 1;
595 else
596 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
598 break;
602 /* Clear out the parameter bindings in this scope, if any.
603 Unused-parameter warnings are handled by function.c. */
604 for (p = scope->parms; p; p = TREE_CHAIN (p))
605 if (DECL_NAME (p))
606 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
608 /* Clear out the tag-meanings declared in this scope.
610 Set the TYPE_CONTEXTs for all of the tagged types belonging to
611 this scope so that they point to the appropriate construct, i.e.
612 either to the current FUNCTION_DECL node, or else to the BLOCK
613 node we just constructed.
615 Note that for tagged types whose scope is just the formal
616 parameter list for some function type specification, we can't
617 properly set their TYPE_CONTEXTs here, because we don't have a
618 pointer to the appropriate FUNCTION_TYPE node readily available
619 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
620 type nodes get set in `grokdeclarator' as soon as we have created
621 the FUNCTION_TYPE node which will represent the "scope" for these
622 "parameter list local" tagged types. */
624 decl = scope->function_body ? current_function_decl : block;
625 for (p = scope->tags; p; p = TREE_CHAIN (p))
627 if (TREE_PURPOSE (p))
628 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
629 if (decl)
630 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
633 /* Restore all name- and label-meanings from outer scopes that were
634 shadowed by this scope. */
635 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
636 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
637 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
638 else
639 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
641 /* Restore all tag-meanings from outer scopes that were shadowed by
642 this scope. */
643 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
644 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
646 /* Dispose of the block that we just made inside some higher level. */
647 if (scope->function_body)
648 DECL_INITIAL (current_function_decl) = block;
649 else if (scope->outer)
651 if (block)
652 SCOPE_LIST_APPEND (scope->outer, blocks, block);
653 /* If we did not make a block for the scope just exited, any
654 blocks made for inner scopes must be carried forward so they
655 will later become subblocks of something else. */
656 else if (scope->blocks)
657 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
660 /* Pop the current scope, and free the structure for reuse. */
661 pop_scope ();
663 return block;
666 /* Insert BLOCK at the end of the list of subblocks of the current
667 scope. This is used when a BIND_EXPR is expanded, to handle the
668 BLOCK node inside the BIND_EXPR. */
670 void
671 insert_block (tree block)
673 TREE_USED (block) = 1;
674 SCOPE_LIST_APPEND (current_scope, blocks, block);
677 /* Set the BLOCK node for the innermost scope (the one we are
678 currently in). The RTL expansion machinery requires us to provide
679 this hook, but it is not useful in function-at-a-time mode. */
681 void
682 set_block (tree block ATTRIBUTE_UNUSED)
686 /* Push a definition or a declaration of struct, union or enum tag "name".
687 "type" should be the type node.
688 We assume that the tag "name" is not already defined.
690 Note that the definition may really be just a forward reference.
691 In that case, the TYPE_SIZE will be zero. */
693 void
694 pushtag (tree name, tree type)
696 struct c_scope *b = current_scope;
698 /* Record the identifier as the type's name if it has none. */
699 if (name)
701 if (TYPE_NAME (type) == 0)
702 TYPE_NAME (type) = name;
704 if (IDENTIFIER_TAG_VALUE (name))
705 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
706 b->shadowed_tags);
707 IDENTIFIER_TAG_VALUE (name) = type;
710 b->tags = tree_cons (name, type, b->tags);
712 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
713 tagged type we just added to the current scope. This fake
714 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
715 to output a representation of a tagged type, and it also gives
716 us a convenient place to record the "scope start" address for the
717 tagged type. */
719 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
721 /* An approximation for now, so we can tell this is a function-scope tag.
722 This will be updated in poplevel. */
723 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
726 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
727 and argument types provided that the type modes match. This function
728 return a unified type given a suitable match, and 0 otherwise. */
730 static tree
731 match_builtin_function_types (tree oldtype, tree newtype)
733 tree newrettype, oldrettype;
734 tree newargs, oldargs;
735 tree trytype, tryargs;
737 /* Accept the return type of the new declaration if same modes. */
738 oldrettype = TREE_TYPE (oldtype);
739 newrettype = TREE_TYPE (newtype);
741 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
742 return 0;
744 oldargs = TYPE_ARG_TYPES (oldtype);
745 newargs = TYPE_ARG_TYPES (newtype);
746 tryargs = newargs;
748 while (oldargs || newargs)
750 if (! oldargs
751 || ! newargs
752 || ! TREE_VALUE (oldargs)
753 || ! TREE_VALUE (newargs)
754 || TYPE_MODE (TREE_VALUE (oldargs))
755 != TYPE_MODE (TREE_VALUE (newargs)))
756 return 0;
758 oldargs = TREE_CHAIN (oldargs);
759 newargs = TREE_CHAIN (newargs);
762 trytype = build_function_type (newrettype, tryargs);
763 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
766 /* Handle when a new declaration NEWDECL
767 has the same name as an old one OLDDECL
768 in the same binding contour.
769 Prints an error message if appropriate.
771 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
772 Otherwise, return 0.
774 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
775 and OLDDECL is in an outer scope and should thus not be changed. */
777 static int
778 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
779 int different_tu)
781 int comptype_flags = (different_tu ? COMPARE_DIFFERENT_TU
782 : COMPARE_STRICT);
783 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
784 comptype_flags);
785 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
786 && DECL_INITIAL (newdecl) != 0);
787 tree oldtype = TREE_TYPE (olddecl);
788 tree newtype = TREE_TYPE (newdecl);
789 int errmsg = 0;
791 if (DECL_P (olddecl))
793 if (TREE_CODE (newdecl) == FUNCTION_DECL
794 && TREE_CODE (olddecl) == FUNCTION_DECL
795 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
797 if (DECL_DECLARED_INLINE_P (newdecl)
798 && DECL_UNINLINABLE (newdecl)
799 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
800 /* Already warned elsewhere. */;
801 else if (DECL_DECLARED_INLINE_P (olddecl)
802 && DECL_UNINLINABLE (olddecl)
803 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
804 /* Already warned. */;
805 else if (DECL_DECLARED_INLINE_P (newdecl)
806 && ! DECL_DECLARED_INLINE_P (olddecl)
807 && DECL_UNINLINABLE (olddecl)
808 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
810 warning ("%Hfunction '%D' redeclared as inline",
811 &DECL_SOURCE_LOCATION (newdecl), newdecl);
812 warning ("%Hprevious declaration of function '%D' "
813 "with attribute noinline",
814 &DECL_SOURCE_LOCATION (olddecl), olddecl);
816 else if (DECL_DECLARED_INLINE_P (olddecl)
817 && DECL_UNINLINABLE (newdecl)
818 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
820 warning ("%Hfunction '%D' redeclared with attribute noinline",
821 &DECL_SOURCE_LOCATION (newdecl), newdecl);
822 warning ("%Hprevious declaration of function '%D' was inline",
823 &DECL_SOURCE_LOCATION (olddecl), olddecl);
827 DECL_ATTRIBUTES (newdecl)
828 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
831 if (TREE_CODE (newtype) == ERROR_MARK
832 || TREE_CODE (oldtype) == ERROR_MARK)
833 types_match = 0;
835 /* New decl is completely inconsistent with the old one =>
836 tell caller to replace the old one.
837 This is always an error except in the case of shadowing a builtin. */
838 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
840 if (TREE_CODE (olddecl) == FUNCTION_DECL
841 && DECL_BUILT_IN (olddecl))
843 /* If you declare a built-in or predefined function name as static,
844 the old definition is overridden,
845 but optionally warn this was a bad choice of name. */
846 if (!TREE_PUBLIC (newdecl))
848 if (warn_shadow)
849 warning ("%Hshadowing built-in function '%D'",
850 &DECL_SOURCE_LOCATION (newdecl), newdecl);
852 else
853 warning ("%Hbuilt-in function '%D' declared as non-function",
854 &DECL_SOURCE_LOCATION (newdecl), newdecl);
856 else
858 error ("%H'%D' redeclared as different kind of symbol",
859 &DECL_SOURCE_LOCATION (newdecl), newdecl);
860 error ("%Hprevious declaration of '%D'",
861 &DECL_SOURCE_LOCATION (olddecl), olddecl);
864 return 0;
867 /* For real parm decl following a forward decl, return 1 so old decl
868 will be reused. Only allow this to happen once. */
869 if (types_match && TREE_CODE (newdecl) == PARM_DECL
870 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
872 TREE_ASM_WRITTEN (olddecl) = 0;
873 return 1;
876 /* The new declaration is the same kind of object as the old one.
877 The declarations may partially match. Print warnings if they don't
878 match enough. Ultimately, copy most of the information from the new
879 decl to the old one, and keep using the old one. */
881 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
883 /* A function declaration for a built-in function. */
884 if (!TREE_PUBLIC (newdecl))
886 /* If you declare a built-in function name as static, the
887 built-in definition is overridden,
888 but optionally warn this was a bad choice of name. */
889 if (warn_shadow)
890 warning ("%Hshadowing built-in function '%D'",
891 &DECL_SOURCE_LOCATION (newdecl), newdecl);
892 /* Discard the old built-in function. */
893 return 0;
895 if (!types_match)
897 /* Accept harmless mismatch in function types.
898 This is for the ffs and fprintf builtins. */
899 tree trytype = match_builtin_function_types (oldtype, newtype);
901 if (trytype)
903 types_match = comptypes (newtype, trytype, comptype_flags);
904 if (types_match)
905 oldtype = trytype;
906 if (! different_binding_level)
907 TREE_TYPE (olddecl) = oldtype;
910 if (!types_match)
912 /* If types don't match for a built-in, throw away the built-in. */
913 warning ("%Hconflicting types for built-in function '%D'",
914 &DECL_SOURCE_LOCATION (newdecl), newdecl);
915 return 0;
918 else if (TREE_CODE (olddecl) == FUNCTION_DECL
919 && DECL_SOURCE_LINE (olddecl) == 0)
921 /* A function declaration for a predeclared function
922 that isn't actually built in. */
923 if (!TREE_PUBLIC (newdecl))
925 /* If you declare it as static, the
926 default definition is overridden. */
927 return 0;
929 else if (!types_match)
931 /* If the types don't match, preserve volatility indication.
932 Later on, we will discard everything else about the
933 default declaration. */
934 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
937 /* Permit char *foo () to match void *foo (...) if not pedantic,
938 if one of them came from a system header file. */
939 else if (!types_match
940 && TREE_CODE (olddecl) == FUNCTION_DECL
941 && TREE_CODE (newdecl) == FUNCTION_DECL
942 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
943 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
944 && (DECL_IN_SYSTEM_HEADER (olddecl)
945 || DECL_IN_SYSTEM_HEADER (newdecl))
946 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
947 && TYPE_ARG_TYPES (oldtype) == 0
948 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
949 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
951 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
952 && TYPE_ARG_TYPES (newtype) == 0
953 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
954 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
956 if (pedantic)
957 pedwarn ("%Hconflicting types for '%D'",
958 &DECL_SOURCE_LOCATION (newdecl), newdecl);
959 /* Make sure we keep void * as ret type, not char *. */
960 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
961 TREE_TYPE (newdecl) = newtype = oldtype;
963 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
964 we will come back here again. */
965 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
967 /* Permit void foo (...) to match int foo (...) if the latter is the
968 definition and implicit int was used. See c-torture/compile/920625-2.c. */
969 else if (!types_match && new_is_definition
970 && TREE_CODE (olddecl) == FUNCTION_DECL
971 && TREE_CODE (newdecl) == FUNCTION_DECL
972 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
973 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
974 && C_FUNCTION_IMPLICIT_INT (newdecl))
976 pedwarn ("%Hconflicting types for '%D'",
977 &DECL_SOURCE_LOCATION (newdecl), newdecl);
978 /* Make sure we keep void as the return type. */
979 TREE_TYPE (newdecl) = newtype = oldtype;
980 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
982 else if (!types_match
983 /* Permit char *foo (int, ...); followed by char *foo ();
984 if not pedantic. */
985 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
986 && ! pedantic
987 /* Return types must still match. */
988 && comptypes (TREE_TYPE (oldtype),
989 TREE_TYPE (newtype), comptype_flags)
990 && TYPE_ARG_TYPES (newtype) == 0))
992 error ("%Hconflicting types for '%D'",
993 &DECL_SOURCE_LOCATION (newdecl), newdecl);
994 /* Check for function type mismatch
995 involving an empty arglist vs a nonempty one. */
996 if (TREE_CODE (olddecl) == FUNCTION_DECL
997 && comptypes (TREE_TYPE (oldtype),
998 TREE_TYPE (newtype), comptype_flags)
999 && ((TYPE_ARG_TYPES (oldtype) == 0
1000 && DECL_INITIAL (olddecl) == 0)
1002 (TYPE_ARG_TYPES (newtype) == 0
1003 && DECL_INITIAL (newdecl) == 0)))
1005 /* Classify the problem further. */
1006 tree t = TYPE_ARG_TYPES (oldtype);
1007 if (t == 0)
1008 t = TYPE_ARG_TYPES (newtype);
1009 for (; t; t = TREE_CHAIN (t))
1011 tree type = TREE_VALUE (t);
1013 if (TREE_CHAIN (t) == 0
1014 && TYPE_MAIN_VARIANT (type) != void_type_node)
1016 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1017 break;
1020 if (c_type_promotes_to (type) != type)
1022 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1023 break;
1027 if (C_DECL_IMPLICIT (olddecl))
1028 error ("%Hprevious implicit declaration of '%D'",
1029 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1030 else
1031 error ("%Hprevious declaration of '%D'",
1032 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1034 /* This is safer because the initializer might contain references
1035 to variables that were declared between olddecl and newdecl. This
1036 will make the initializer invalid for olddecl in case it gets
1037 assigned to olddecl below. */
1038 if (TREE_CODE (newdecl) == VAR_DECL)
1039 DECL_INITIAL (newdecl) = 0;
1041 /* TLS cannot follow non-TLS declaration. */
1042 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1043 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1045 error ("%Hthread-local declaration of '%D' follows non thread-local "
1046 "declaration", &DECL_SOURCE_LOCATION (newdecl), newdecl);
1047 error ("%Hprevious declaration of '%D'",
1048 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1050 /* non-TLS declaration cannot follow TLS declaration. */
1051 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1052 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1054 error ("%Hnon thread-local declaration of '%D' follows "
1055 "thread-local declaration",
1056 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1057 error ("%Hprevious declaration of '%D'",
1058 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1060 else
1062 errmsg = redeclaration_error_message (newdecl, olddecl);
1063 if (errmsg)
1065 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1066 switch (errmsg)
1068 case 1:
1069 error ("%Hredefinition of '%D'", locus, newdecl);
1070 break;
1071 case 2:
1072 error ("%Hredeclaration of '%D'", locus, newdecl);
1073 break;
1074 case 3:
1075 error ("%Hconflicting declarations of '%D'", locus, newdecl);
1076 break;
1077 default:
1078 abort ();
1081 locus = &DECL_SOURCE_LOCATION (olddecl);
1082 if (DECL_INITIAL (olddecl)
1083 && current_scope == global_scope)
1084 error ("%H'%D' previously defined here", locus, olddecl);
1085 else
1086 error ("%H'%D' previously declared here", locus, olddecl);
1087 return 0;
1089 else if (TREE_CODE (newdecl) == TYPE_DECL
1090 && (DECL_IN_SYSTEM_HEADER (olddecl)
1091 || DECL_IN_SYSTEM_HEADER (newdecl)))
1093 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1094 warning ("%Hredefinition of '%D'", locus, newdecl);
1095 locus = &DECL_SOURCE_LOCATION (olddecl);
1096 if (DECL_INITIAL (olddecl)
1097 && current_scope == global_scope)
1098 warning ("%H'%D' previously defined here", locus, olddecl);
1099 else
1100 warning ("%H'%D' previously declared here", locus, olddecl);
1102 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1103 && DECL_INITIAL (olddecl) != 0
1104 && TYPE_ARG_TYPES (oldtype) == 0
1105 && TYPE_ARG_TYPES (newtype) != 0
1106 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1108 tree type, parm;
1109 int nargs;
1110 /* Prototype decl follows defn w/o prototype. */
1112 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1113 type = TYPE_ARG_TYPES (newtype),
1114 nargs = 1;
1116 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1118 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1119 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1121 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1122 warning ("%Hprototype for '%D' follows", locus, newdecl);
1123 locus = &DECL_SOURCE_LOCATION (olddecl);
1124 warning ("%Hnon-prototype definition here", locus);
1125 break;
1127 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1128 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1130 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1131 error ("%Hprototype for '%D' follows and number of "
1132 "arguments doesn't match", locus, newdecl);
1133 locus = &DECL_SOURCE_LOCATION (olddecl);
1134 error ("%Hnon-prototype definition here", locus);
1135 errmsg = 1;
1136 break;
1138 /* Type for passing arg must be consistent
1139 with that declared for the arg. */
1140 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1141 comptype_flags))
1143 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1144 error ("%Hprototype for '%D' follows and argument %d "
1145 "doesn't match", locus, newdecl, nargs);
1146 locus = &DECL_SOURCE_LOCATION (olddecl);
1147 error ("%Hnon-prototype definition here", locus);
1148 errmsg = 1;
1149 break;
1153 /* Warn about mismatches in various flags. */
1154 else
1156 const location_t *locus = &DECL_SOURCE_LOCATION (newdecl);
1158 /* Warn if function is now inline
1159 but was previously declared not inline and has been called. */
1160 if (TREE_CODE (olddecl) == FUNCTION_DECL
1161 && ! DECL_DECLARED_INLINE_P (olddecl)
1162 && DECL_DECLARED_INLINE_P (newdecl)
1163 && TREE_USED (olddecl))
1164 warning ("%H'%D' declared inline after being called",
1165 locus, newdecl);
1166 if (TREE_CODE (olddecl) == FUNCTION_DECL
1167 && ! DECL_DECLARED_INLINE_P (olddecl)
1168 && DECL_DECLARED_INLINE_P (newdecl)
1169 && DECL_INITIAL (olddecl) != 0)
1170 warning ("%H'%D' declared inline after its definition",
1171 locus, newdecl);
1173 /* If pedantic, warn when static declaration follows a non-static
1174 declaration. Otherwise, do so only for functions. */
1175 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1176 && TREE_PUBLIC (olddecl)
1177 && !TREE_PUBLIC (newdecl))
1178 warning ("%Hstatic declaration for '%D' follows non-static",
1179 locus, newdecl);
1181 /* If warn_traditional, warn when a non-static function
1182 declaration follows a static one. */
1183 if (warn_traditional && !in_system_header
1184 && TREE_CODE (olddecl) == FUNCTION_DECL
1185 && !TREE_PUBLIC (olddecl)
1186 && TREE_PUBLIC (newdecl))
1187 warning ("%Hnon-static declaration for '%D' follows static",
1188 locus, newdecl);
1190 /* Warn when const declaration follows a non-const
1191 declaration, but not for functions. */
1192 if (TREE_CODE (olddecl) != FUNCTION_DECL
1193 && !TREE_READONLY (olddecl)
1194 && TREE_READONLY (newdecl))
1195 warning ("%Hconst declaration for '%D' follows non-const",
1196 locus, newdecl);
1197 /* These bits are logically part of the type, for variables.
1198 But not for functions
1199 (where qualifiers are not valid ANSI anyway). */
1200 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1201 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1202 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1203 pedwarn ("%Htype qualifiers for '%D' conflict with previous "
1204 "declaration", locus, newdecl);
1208 /* Optionally warn about more than one declaration for the same name. */
1209 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1210 /* Don't warn about a function declaration
1211 followed by a definition. */
1212 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1213 && DECL_INITIAL (olddecl) == 0)
1214 /* Don't warn about extern decl followed by (tentative) definition. */
1215 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1217 warning ("%Hredundant redeclaration of '%D' in same scope",
1218 &DECL_SOURCE_LOCATION (newdecl), newdecl);
1219 warning ("%Hprevious declaration of '%D'",
1220 &DECL_SOURCE_LOCATION (olddecl), olddecl);
1223 /* Copy all the DECL_... slots specified in the new decl
1224 except for any that we copy here from the old type.
1226 Past this point, we don't change OLDTYPE and NEWTYPE
1227 even if we change the types of NEWDECL and OLDDECL. */
1229 if (types_match)
1231 /* When copying info to olddecl, we store into write_olddecl
1232 instead. This allows us to avoid modifying olddecl when
1233 different_binding_level is true. */
1234 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1236 /* Merge the data types specified in the two decls. */
1237 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1239 if (different_binding_level)
1241 if (TYPE_ARG_TYPES (oldtype) != 0
1242 && TYPE_ARG_TYPES (newtype) == 0)
1243 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1244 else
1245 TREE_TYPE (newdecl)
1246 = build_type_attribute_variant
1247 (newtype,
1248 merge_attributes (TYPE_ATTRIBUTES (newtype),
1249 TYPE_ATTRIBUTES (oldtype)));
1251 else
1252 TREE_TYPE (newdecl)
1253 = TREE_TYPE (olddecl)
1254 = common_type (newtype, oldtype);
1257 /* Lay the type out, unless already done. */
1258 if (oldtype != TREE_TYPE (newdecl))
1260 if (TREE_TYPE (newdecl) != error_mark_node)
1261 layout_type (TREE_TYPE (newdecl));
1262 if (TREE_CODE (newdecl) != FUNCTION_DECL
1263 && TREE_CODE (newdecl) != TYPE_DECL
1264 && TREE_CODE (newdecl) != CONST_DECL)
1265 layout_decl (newdecl, 0);
1267 else
1269 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1270 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1271 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1272 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1273 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1274 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1276 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1277 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1281 /* Keep the old rtl since we can safely use it. */
1282 COPY_DECL_RTL (olddecl, newdecl);
1284 /* Merge the type qualifiers. */
1285 if (TREE_READONLY (newdecl))
1286 TREE_READONLY (write_olddecl) = 1;
1288 if (TREE_THIS_VOLATILE (newdecl))
1290 TREE_THIS_VOLATILE (write_olddecl) = 1;
1291 if (TREE_CODE (newdecl) == VAR_DECL
1292 /* If an automatic variable is re-declared in the same
1293 function scope, but the old declaration was not
1294 volatile, make_var_volatile() would crash because the
1295 variable would have been assigned to a pseudo, not a
1296 MEM. Since this duplicate declaration is invalid
1297 anyway, we just skip the call. */
1298 && errmsg == 0)
1299 make_var_volatile (newdecl);
1302 /* Keep source location of definition rather than declaration. */
1303 /* When called with different_binding_level set, keep the old
1304 information so that meaningful diagnostics can be given. */
1305 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1306 && ! different_binding_level)
1308 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
1309 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
1312 /* Merge the unused-warning information. */
1313 if (DECL_IN_SYSTEM_HEADER (olddecl))
1314 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1315 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1316 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1318 /* Merge the initialization information. */
1319 /* When called with different_binding_level set, don't copy over
1320 DECL_INITIAL, so that we don't accidentally change function
1321 declarations into function definitions. */
1322 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1323 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1325 /* Merge the section attribute.
1326 We want to issue an error if the sections conflict but that must be
1327 done later in decl_attributes since we are called before attributes
1328 are assigned. */
1329 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1330 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1332 /* Copy the assembler name.
1333 Currently, it can only be defined in the prototype. */
1334 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1336 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1338 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1339 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1340 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1341 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1342 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1343 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1344 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1345 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1346 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1349 /* If cannot merge, then use the new type and qualifiers,
1350 and don't preserve the old rtl. */
1351 else if (! different_binding_level)
1353 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1354 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1355 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1356 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1359 /* Merge the storage class information. */
1360 merge_weak (newdecl, olddecl);
1362 /* For functions, static overrides non-static. */
1363 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1365 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1366 /* This is since we don't automatically
1367 copy the attributes of NEWDECL into OLDDECL. */
1368 /* No need to worry about different_binding_level here because
1369 then TREE_PUBLIC (newdecl) was true. */
1370 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1371 /* If this clears `static', clear it in the identifier too. */
1372 if (! TREE_PUBLIC (olddecl))
1373 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1375 if (DECL_EXTERNAL (newdecl))
1377 if (! different_binding_level || different_tu)
1379 /* Don't mess with these flags on local externs; they remain
1380 external even if there's a declaration at file scope which
1381 isn't. */
1382 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1383 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1385 /* An extern decl does not override previous storage class. */
1386 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1387 if (! DECL_EXTERNAL (newdecl))
1389 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1390 /* If we have two non-EXTERNAL file-scope decls that are
1391 the same, only one of them should be written out. */
1392 if (different_tu)
1393 TREE_ASM_WRITTEN (newdecl) = 1;
1396 else
1398 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1399 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1402 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1404 /* If we're redefining a function previously defined as extern
1405 inline, make sure we emit debug info for the inline before we
1406 throw it away, in case it was inlined into a function that hasn't
1407 been written out yet. */
1408 if (new_is_definition && DECL_INITIAL (olddecl))
1410 if (TREE_USED (olddecl))
1411 (*debug_hooks->outlining_inline_function) (olddecl);
1413 /* The new defn must not be inline. */
1414 DECL_INLINE (newdecl) = 0;
1415 DECL_UNINLINABLE (newdecl) = 1;
1417 else
1419 /* If either decl says `inline', this fn is inline,
1420 unless its definition was passed already. */
1421 if (DECL_DECLARED_INLINE_P (newdecl)
1422 || DECL_DECLARED_INLINE_P (olddecl))
1423 DECL_DECLARED_INLINE_P (newdecl) = 1;
1425 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1426 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1429 if (DECL_BUILT_IN (olddecl))
1431 /* Get rid of any built-in function if new arg types don't match it
1432 or if we have a function definition. */
1433 if (! types_match || new_is_definition)
1435 if (! different_binding_level)
1437 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1438 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1441 else
1443 /* If redeclaring a builtin function, and not a definition,
1444 it stays built in. */
1445 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1446 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1450 /* Also preserve various other info from the definition. */
1451 if (! new_is_definition)
1453 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1454 /* When called with different_binding_level set, don't copy over
1455 DECL_INITIAL, so that we don't accidentally change function
1456 declarations into function definitions. */
1457 if (! different_binding_level)
1458 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1459 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1460 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1461 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1462 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1464 /* Set DECL_INLINE on the declaration if we've got a body
1465 from which to instantiate. */
1466 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1468 DECL_INLINE (newdecl) = 1;
1469 DECL_ABSTRACT_ORIGIN (newdecl)
1470 = (different_binding_level
1471 ? DECL_ORIGIN (olddecl)
1472 : DECL_ABSTRACT_ORIGIN (olddecl));
1475 else
1477 /* If a previous declaration said inline, mark the
1478 definition as inlinable. */
1479 if (DECL_DECLARED_INLINE_P (newdecl)
1480 && ! DECL_UNINLINABLE (newdecl))
1481 DECL_INLINE (newdecl) = 1;
1484 if (different_binding_level)
1485 return 0;
1487 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1488 But preserve OLDDECL's DECL_UID. */
1490 unsigned olddecl_uid = DECL_UID (olddecl);
1492 memcpy ((char *) olddecl + sizeof (struct tree_common),
1493 (char *) newdecl + sizeof (struct tree_common),
1494 sizeof (struct tree_decl) - sizeof (struct tree_common));
1495 DECL_UID (olddecl) = olddecl_uid;
1498 /* NEWDECL contains the merged attribute lists.
1499 Update OLDDECL to be the same. */
1500 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1502 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1503 so that encode_section_info has a chance to look at the new decl
1504 flags and attributes. */
1505 if (DECL_RTL_SET_P (olddecl)
1506 && (TREE_CODE (olddecl) == FUNCTION_DECL
1507 || (TREE_CODE (olddecl) == VAR_DECL
1508 && TREE_STATIC (olddecl))))
1509 make_decl_rtl (olddecl, NULL);
1511 return 1;
1514 /* Return any external DECL associated with ID, whether or not it is
1515 currently in scope. */
1517 static tree
1518 any_external_decl (tree id)
1520 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1521 tree t;
1523 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1524 return 0;
1525 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1526 return decl;
1528 t = purpose_member (id, truly_local_externals);
1529 if (t)
1530 return TREE_VALUE (t);
1532 return 0;
1535 /* Record an external decl DECL. This only does something if a
1536 shadowing decl already exists. */
1537 static void
1538 record_external_decl (tree decl)
1540 tree name = DECL_NAME (decl);
1541 if (!IDENTIFIER_SYMBOL_VALUE (name))
1542 return;
1544 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1547 /* Check whether decl-node X shadows an existing declaration.
1548 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1549 which might be a NULL_TREE. */
1550 static void
1551 warn_if_shadowing (tree x, tree old)
1553 const char *name;
1555 /* Nothing to shadow? */
1556 if (old == 0
1557 /* Shadow warnings not wanted? */
1558 || !warn_shadow
1559 /* No shadow warnings for internally generated vars. */
1560 || DECL_SOURCE_LINE (x) == 0
1561 /* No shadow warnings for vars made for inlining. */
1562 || DECL_FROM_INLINE (x)
1563 /* Don't warn about the parm names in function declarator
1564 within a function declarator.
1565 It would be nice to avoid warning in any function
1566 declarator in a declaration, as opposed to a definition,
1567 but there is no way to tell it's not a definition. */
1568 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1569 return;
1571 name = IDENTIFIER_POINTER (DECL_NAME (x));
1572 if (TREE_CODE (old) == PARM_DECL)
1573 shadow_warning (SW_PARAM, name, old);
1574 else if (C_DECL_FILE_SCOPE (old))
1575 shadow_warning (SW_GLOBAL, name, old);
1576 else
1577 shadow_warning (SW_LOCAL, name, old);
1581 /* Subroutine of pushdecl.
1583 X is a TYPE_DECL for a typedef statement. Create a brand new
1584 ..._TYPE node (which will be just a variant of the existing
1585 ..._TYPE node with identical properties) and then install X
1586 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1588 The whole point here is to end up with a situation where each
1589 and every ..._TYPE node the compiler creates will be uniquely
1590 associated with AT MOST one node representing a typedef name.
1591 This way, even though the compiler substitutes corresponding
1592 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1593 early on, later parts of the compiler can always do the reverse
1594 translation and get back the corresponding typedef name. For
1595 example, given:
1597 typedef struct S MY_TYPE;
1598 MY_TYPE object;
1600 Later parts of the compiler might only know that `object' was of
1601 type `struct S' if it were not for code just below. With this
1602 code however, later parts of the compiler see something like:
1604 struct S' == struct S
1605 typedef struct S' MY_TYPE;
1606 struct S' object;
1608 And they can then deduce (from the node for type struct S') that
1609 the original object declaration was:
1611 MY_TYPE object;
1613 Being able to do this is important for proper support of protoize,
1614 and also for generating precise symbolic debugging information
1615 which takes full account of the programmer's (typedef) vocabulary.
1617 Obviously, we don't want to generate a duplicate ..._TYPE node if
1618 the TYPE_DECL node that we are now processing really represents a
1619 standard built-in type.
1621 Since all standard types are effectively declared at line zero
1622 in the source file, we can easily check to see if we are working
1623 on a standard type by checking the current value of lineno. */
1625 static void
1626 clone_underlying_type (tree x)
1628 if (DECL_SOURCE_LINE (x) == 0)
1630 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1631 TYPE_NAME (TREE_TYPE (x)) = x;
1633 else if (TREE_TYPE (x) != error_mark_node
1634 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1636 tree tt = TREE_TYPE (x);
1637 DECL_ORIGINAL_TYPE (x) = tt;
1638 tt = build_type_copy (tt);
1639 TYPE_NAME (tt) = x;
1640 TREE_USED (tt) = TREE_USED (x);
1641 TREE_TYPE (x) = tt;
1645 /* Record a decl-node X as belonging to the current lexical scope.
1646 Check for errors (such as an incompatible declaration for the same
1647 name already seen in the same scope).
1649 Returns either X or an old decl for the same name.
1650 If an old decl is returned, it may have been smashed
1651 to agree with what X says. */
1653 tree
1654 pushdecl (tree x)
1656 tree name = DECL_NAME (x);
1657 struct c_scope *scope = current_scope;
1659 #ifdef ENABLE_CHECKING
1660 if (error_mark_node == 0)
1661 /* Called too early. */
1662 abort ();
1663 #endif
1665 /* Functions need the lang_decl data. */
1666 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1667 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1669 /* A local extern declaration for a function doesn't constitute nesting.
1670 A local auto declaration does, since it's a forward decl
1671 for a nested function coming later. */
1672 if (current_function_decl == NULL
1673 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1674 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1675 DECL_CONTEXT (x) = current_file_decl;
1676 else
1677 DECL_CONTEXT (x) = current_function_decl;
1679 if (name)
1681 tree old;
1683 if (warn_nested_externs
1684 && scope != global_scope
1685 && DECL_EXTERNAL (x)
1686 && !DECL_IN_SYSTEM_HEADER (x))
1687 warning ("nested extern declaration of `%s'",
1688 IDENTIFIER_POINTER (name));
1690 old = lookup_name_current_level (name);
1691 if (old && duplicate_decls (x, old, 0, false))
1693 /* For PARM_DECLs, old may be a forward declaration.
1694 If so, we want to remove it from its old location
1695 (in the variables chain) and rechain it in the
1696 location given by the new declaration. */
1697 if (TREE_CODE (x) == PARM_DECL)
1699 tree *p;
1700 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1701 if (*p == old)
1703 *p = TREE_CHAIN (old);
1704 SCOPE_LIST_APPEND (scope, parms, old);
1705 break;
1708 return old;
1710 if (DECL_EXTERNAL (x) || scope == global_scope)
1712 /* Find and check against a previous, not-in-scope, external
1713 decl for this identifier. (C99 s???: If two declarations
1714 with external linkage, referring to the same object, have
1715 incompatible types, the behavior is undefined). */
1716 tree ext = any_external_decl (name);
1717 if (ext)
1719 if (duplicate_decls (x, ext, scope != global_scope,
1720 false))
1721 x = copy_node (ext);
1723 else
1724 record_external_decl (x);
1727 if (TREE_CODE (x) == TYPE_DECL)
1728 clone_underlying_type (x);
1730 /* If storing a local value, there may already be one
1731 (inherited). If so, record it for restoration when this
1732 scope ends. Take care not to do this if we are replacing an
1733 older decl in the same scope (i.e. duplicate_decls returned
1734 false, above). */
1735 if (scope != global_scope
1736 && IDENTIFIER_SYMBOL_VALUE (name)
1737 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1739 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1740 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1741 scope->shadowed);
1744 /* Install the new declaration in the requested scope. */
1745 IDENTIFIER_SYMBOL_VALUE (name) = x;
1746 C_DECL_INVISIBLE (x) = 0;
1748 /* Keep list of variables in this scope with incomplete type.
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.
1753 FIXME: Chain these off the TYPE_DECL for the incomplete type,
1754 then we don't have to do (potentially quite costly) searches
1755 in finish_struct. */
1756 if (TREE_TYPE (x) != error_mark_node
1757 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1759 tree element = TREE_TYPE (x);
1761 while (TREE_CODE (element) == ARRAY_TYPE)
1762 element = TREE_TYPE (element);
1763 if (TREE_CODE (element) == RECORD_TYPE
1764 || TREE_CODE (element) == UNION_TYPE)
1765 scope->incomplete = tree_cons (NULL_TREE, x, scope->incomplete);
1769 if (TREE_CODE (x) == PARM_DECL)
1770 SCOPE_LIST_APPEND (scope, parms, x);
1771 else
1772 SCOPE_LIST_APPEND (scope, names, x);
1774 return x;
1777 /* Record X as belonging to the global scope (C99 "file scope").
1778 This is used only internally by the Objective-C front end,
1779 and is limited to its needs. duplicate_decls is not called;
1780 if there is any preexisting decl for this identifier, it is an ICE. */
1782 tree
1783 pushdecl_top_level (tree x)
1785 tree name;
1787 if (TREE_CODE (x) != VAR_DECL)
1788 abort ();
1790 name = DECL_NAME (x);
1792 if (IDENTIFIER_SYMBOL_VALUE (name))
1793 abort ();
1795 DECL_CONTEXT (x) = current_file_decl;
1796 IDENTIFIER_SYMBOL_VALUE (name) = x;
1798 SCOPE_LIST_APPEND (global_scope, names, x);
1799 return x;
1802 /* Generate an implicit declaration for identifier FUNCTIONID as a
1803 function of type int (). */
1805 tree
1806 implicitly_declare (tree functionid)
1808 tree decl = any_external_decl (functionid);
1810 if (decl)
1812 /* Implicit declaration of a function already declared
1813 (somehow) in a different scope, or as a built-in.
1814 If this is the first time this has happened, warn;
1815 then recycle the old declaration. */
1816 if (!C_DECL_IMPLICIT (decl))
1818 implicit_decl_warning (DECL_NAME (decl));
1819 if (! C_DECL_FILE_SCOPE (decl))
1820 warning ("%Hprevious declaration of '%D'",
1821 &DECL_SOURCE_LOCATION (decl), decl);
1822 C_DECL_IMPLICIT (decl) = 1;
1824 /* If this function is global, then it must already be in the
1825 global scope, so there's no need to push it again. */
1826 if (current_scope == global_scope)
1827 return decl;
1828 /* If this is a local declaration, make a copy; we can't have
1829 the same DECL listed in two different scopes. */
1830 return pushdecl (copy_node (decl));
1833 /* Not seen before. */
1834 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1835 DECL_EXTERNAL (decl) = 1;
1836 TREE_PUBLIC (decl) = 1;
1837 C_DECL_IMPLICIT (decl) = 1;
1838 implicit_decl_warning (functionid);
1840 /* C89 says implicit declarations are in the innermost block.
1841 So we record the decl in the standard fashion. */
1842 decl = pushdecl (decl);
1844 /* No need to call objc_check_decl here - it's a function type. */
1845 rest_of_decl_compilation (decl, NULL, 0, 0);
1847 /* Write a record describing this implicit function declaration
1848 to the prototypes file (if requested). */
1849 gen_aux_info_record (decl, 0, 1, 0);
1851 /* Possibly apply some default attributes to this implicit declaration. */
1852 decl_attributes (&decl, NULL_TREE, 0);
1854 return decl;
1857 static void
1858 implicit_decl_warning (tree id)
1860 const char *name = IDENTIFIER_POINTER (id);
1861 if (mesg_implicit_function_declaration == 2)
1862 error ("implicit declaration of function `%s'", name);
1863 else if (mesg_implicit_function_declaration == 1)
1864 warning ("implicit declaration of function `%s'", name);
1867 /* Return zero if the declaration NEWDECL is valid
1868 when the declaration OLDDECL (assumed to be for the same name)
1869 has already been seen.
1870 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1871 and 3 if it is a conflicting declaration. */
1873 static int
1874 redeclaration_error_message (tree newdecl, tree olddecl)
1876 if (TREE_CODE (newdecl) == TYPE_DECL)
1878 /* Do not complain about type redeclarations where at least one
1879 declaration was in a system header. */
1880 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1881 return 0;
1882 return 1;
1884 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1886 /* Declarations of functions can insist on internal linkage
1887 but they can't be inconsistent with internal linkage,
1888 so there can be no error on that account.
1889 However defining the same name twice is no good. */
1890 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1891 /* However, defining once as extern inline and a second
1892 time in another way is ok. */
1893 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1894 && ! (DECL_DECLARED_INLINE_P (newdecl)
1895 && DECL_EXTERNAL (newdecl))))
1896 return 1;
1897 return 0;
1899 else if (C_DECL_FILE_SCOPE (newdecl))
1901 /* Objects declared at file scope: */
1902 /* If at least one is a reference, it's ok. */
1903 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1904 return 0;
1905 /* Reject two definitions. */
1906 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1907 return 1;
1908 /* Now we have two tentative defs, or one tentative and one real def. */
1909 /* Insist that the linkage match. */
1910 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1911 return 3;
1912 return 0;
1914 else if (current_scope->parm_flag
1915 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1916 return 0;
1917 else
1919 /* Newdecl has block scope. If olddecl has block scope also, then
1920 reject two definitions, and reject a definition together with an
1921 external reference. Otherwise, it is OK, because newdecl must
1922 be an extern reference to olddecl. */
1923 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1924 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1925 return 2;
1926 return 0;
1930 /* Issue an error message for a reference to an undeclared variable
1931 ID, including a reference to a builtin outside of function-call
1932 context. Establish a binding of the identifier to error_mark_node
1933 in an appropriate scope, which will suppress further errors for the
1934 same identifier. */
1935 void
1936 undeclared_variable (tree id)
1938 static bool already = false;
1939 struct c_scope *scope;
1941 if (current_function_decl == 0)
1943 error ("`%s' undeclared here (not in a function)",
1944 IDENTIFIER_POINTER (id));
1945 scope = current_scope;
1947 else
1949 error ("`%s' undeclared (first use in this function)",
1950 IDENTIFIER_POINTER (id));
1952 if (! already)
1954 error ("(Each undeclared identifier is reported only once");
1955 error ("for each function it appears in.)");
1956 already = true;
1959 scope = current_function_scope;
1962 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1963 scope->shadowed);
1964 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1967 /* Subroutine of lookup_label, declare_label, define_label: construct a
1968 LABEL_DECL with all the proper frills. */
1970 static tree
1971 make_label (tree name, location_t location)
1973 tree label = build_decl (LABEL_DECL, name, void_type_node);
1975 DECL_CONTEXT (label) = current_function_decl;
1976 DECL_MODE (label) = VOIDmode;
1977 DECL_SOURCE_LOCATION (label) = location;
1979 return label;
1982 /* Another subroutine of lookup_label, declare_label, define_label:
1983 set up the binding of name to LABEL_DECL in the given SCOPE. */
1985 static void
1986 bind_label (tree name, tree label, struct c_scope *scope)
1988 if (IDENTIFIER_LABEL_VALUE (name))
1989 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1990 scope->shadowed);
1991 IDENTIFIER_LABEL_VALUE (name) = label;
1993 SCOPE_LIST_APPEND (scope, names, label);
1996 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1997 Create one if none exists so far for the current function.
1998 This is called when a label is used in a goto expression or
1999 has its address taken. */
2001 tree
2002 lookup_label (tree name)
2004 tree label;
2006 if (current_function_decl == 0)
2008 error ("label %s referenced outside of any function",
2009 IDENTIFIER_POINTER (name));
2010 return 0;
2013 /* Use a label already defined or ref'd with this name, but not if
2014 it is inherited from a containing function and wasn't declared
2015 using __label__. */
2016 label = IDENTIFIER_LABEL_VALUE (name);
2017 if (label && (DECL_CONTEXT (label) == current_function_decl
2018 || C_DECLARED_LABEL_FLAG (label)))
2020 /* If the label has only been declared, update its apparent
2021 location to point here, for better diagnostics if it
2022 turns out not to have been defined. */
2023 if (!TREE_USED (label))
2024 DECL_SOURCE_LOCATION (label) = input_location;
2025 return label;
2028 /* No label binding for that identifier; make one. */
2029 label = make_label (name, input_location);
2031 /* Ordinary labels go in the current function scope. */
2032 bind_label (name, label, current_function_scope);
2033 return label;
2036 /* Make a label named NAME in the current function, shadowing silently
2037 any that may be inherited from containing functions or containing
2038 scopes. This is called for __label__ declarations. */
2040 /* Note that valid use, if the label being shadowed comes from another
2041 scope in the same function, requires calling declare_nonlocal_label
2042 right away. (Is this still true? -zw 2003-07-17) */
2044 tree
2045 declare_label (tree name)
2047 tree label = IDENTIFIER_LABEL_VALUE (name);
2048 tree dup;
2050 /* Check to make sure that the label hasn't already been declared
2051 at this scope */
2052 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2053 if (dup == label)
2055 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2056 error ("%Hthis is a previous declaration",
2057 &DECL_SOURCE_LOCATION (dup));
2059 /* Just use the previous declaration. */
2060 return dup;
2063 label = make_label (name, input_location);
2064 C_DECLARED_LABEL_FLAG (label) = 1;
2066 /* Declared labels go in the current scope. */
2067 bind_label (name, label, current_scope);
2068 return label;
2071 /* Define a label, specifying the location in the source file.
2072 Return the LABEL_DECL node for the label, if the definition is valid.
2073 Otherwise return 0. */
2075 tree
2076 define_label (location_t location, tree name)
2078 tree label;
2080 /* Find any preexisting label with this name. It is an error
2081 if that label has already been defined in this function, or
2082 if there is a containing function with a declared label with
2083 the same name. */
2084 label = IDENTIFIER_LABEL_VALUE (name);
2086 if (label
2087 && ((DECL_CONTEXT (label) == current_function_decl
2088 && DECL_INITIAL (label) != 0)
2089 || (DECL_CONTEXT (label) != current_function_decl
2090 && C_DECLARED_LABEL_FLAG (label))))
2092 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2093 error ("%Hduplicate label `%D'", &location, label);
2094 if (DECL_INITIAL (label))
2095 error ("%H`%D' previously defined here", prev_loc, label);
2096 else
2097 error ("%H`%D' previously declared here", prev_loc, label);
2098 return 0;
2100 else if (label && DECL_CONTEXT (label) == current_function_decl)
2102 /* The label has been used or declared already in this function,
2103 but not defined. Update its location to point to this
2104 definition. */
2105 DECL_SOURCE_LOCATION (label) = location;
2107 else
2109 /* No label binding for that identifier; make one. */
2110 label = make_label (name, location);
2112 /* Ordinary labels go in the current function scope. */
2113 bind_label (name, label, current_function_scope);
2116 if (warn_traditional && !in_system_header && lookup_name (name))
2117 warning ("%Htraditional C lacks a separate namespace for labels, "
2118 "identifier `%s' conflicts", &location,
2119 IDENTIFIER_POINTER (name));
2121 /* Mark label as having been defined. */
2122 DECL_INITIAL (label) = error_mark_node;
2123 return label;
2126 /* Return the list of declarations of the current scope. */
2128 tree
2129 getdecls (void)
2131 return current_scope->names;
2135 /* Given NAME, an IDENTIFIER_NODE,
2136 return the structure (or union or enum) definition for that name.
2137 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2138 CODE says which kind of type the caller wants;
2139 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2140 If the wrong kind of type is found, an error is reported. */
2142 static tree
2143 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2145 tree tag = IDENTIFIER_TAG_VALUE (name);
2146 int thislevel = 0;
2148 if (!tag)
2149 return 0;
2151 /* We only care about whether it's in this level if
2152 thislevel_only was set or it might be a type clash. */
2153 if (thislevel_only || TREE_CODE (tag) != code)
2155 if (current_scope == global_scope
2156 || purpose_member (name, current_scope->tags))
2157 thislevel = 1;
2160 if (thislevel_only && !thislevel)
2161 return 0;
2163 if (TREE_CODE (tag) != code)
2165 /* Definition isn't the kind we were looking for. */
2166 pending_invalid_xref = name;
2167 pending_invalid_xref_location = input_location;
2169 /* If in the same binding level as a declaration as a tag
2170 of a different type, this must not be allowed to
2171 shadow that tag, so give the error immediately.
2172 (For example, "struct foo; union foo;" is invalid.) */
2173 if (thislevel)
2174 pending_xref_error ();
2176 return tag;
2179 /* Print an error message now
2180 for a recent invalid struct, union or enum cross reference.
2181 We don't print them immediately because they are not invalid
2182 when used in the `struct foo;' construct for shadowing. */
2184 void
2185 pending_xref_error (void)
2187 if (pending_invalid_xref != 0)
2188 error ("%H`%s' defined as wrong kind of tag",
2189 &pending_invalid_xref_location,
2190 IDENTIFIER_POINTER (pending_invalid_xref));
2191 pending_invalid_xref = 0;
2195 /* Look up NAME in the current scope and its superiors
2196 in the namespace of variables, functions and typedefs.
2197 Return a ..._DECL node of some kind representing its definition,
2198 or return 0 if it is undefined. */
2200 tree
2201 lookup_name (tree name)
2203 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2204 if (decl == 0 || decl == error_mark_node)
2205 return decl;
2206 if (C_DECL_INVISIBLE (decl))
2207 return 0;
2208 return decl;
2211 /* Similar to `lookup_name' but look only at the current scope. */
2213 static tree
2214 lookup_name_current_level (tree name)
2216 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2218 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2219 return 0;
2221 if (current_scope == global_scope)
2222 return decl;
2224 /* Scan the current scope for a decl with name NAME.
2225 For PARM_DECLs, we have to look at both ->parms and ->names, since
2226 forward parameter declarations wind up on the ->names list. */
2227 if (TREE_CODE (decl) == PARM_DECL
2228 && chain_member (decl, current_scope->parms))
2229 return decl;
2230 if (chain_member (decl, current_scope->names))
2231 return decl;
2233 return 0;
2236 /* Create the predefined scalar types of C,
2237 and some nodes representing standard constants (0, 1, (void *) 0).
2238 Initialize the global scope.
2239 Make definitions for built-in primitive functions. */
2241 void
2242 c_init_decl_processing (void)
2244 tree endlink;
2245 tree ptr_ftype_void, ptr_ftype_ptr;
2246 location_t save_loc = input_location;
2248 /* Adds some ggc roots, and reserved words for c-parse.in. */
2249 c_parse_init ();
2251 current_function_decl = 0;
2253 /* Make the c_scope structure for global names. */
2254 pushlevel (0);
2255 global_scope = current_scope;
2257 /* Declarations from c_common_nodes_and_builtins must not be associated
2258 with this input file, lest we get differences between using and not
2259 using preprocessed headers. */
2260 input_location.file = "<internal>";
2261 input_location.line = 0;
2263 /* Make the DECL for the toplevel file scope. */
2264 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2266 build_common_tree_nodes (flag_signed_char);
2268 c_common_nodes_and_builtins ();
2270 /* In C, comparisons and TRUTH_* expressions have type int. */
2271 truthvalue_type_node = integer_type_node;
2272 truthvalue_true_node = integer_one_node;
2273 truthvalue_false_node = integer_zero_node;
2275 /* Even in C99, which has a real boolean type. */
2276 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2277 boolean_type_node));
2279 endlink = void_list_node;
2280 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2281 ptr_ftype_ptr
2282 = build_function_type (ptr_type_node,
2283 tree_cons (NULL_TREE, ptr_type_node, endlink));
2285 input_location = save_loc;
2287 pedantic_lvalues = pedantic;
2289 make_fname_decl = c_make_fname_decl;
2290 start_fname_decls ();
2292 first_builtin_decl = global_scope->names;
2293 last_builtin_decl = global_scope->names_last;
2296 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2297 decl, NAME is the initialization string and TYPE_DEP indicates whether
2298 NAME depended on the type of the function. As we don't yet implement
2299 delayed emission of static data, we mark the decl as emitted
2300 so it is not placed in the output. Anything using it must therefore pull
2301 out the STRING_CST initializer directly. FIXME. */
2303 static tree
2304 c_make_fname_decl (tree id, int type_dep)
2306 const char *name = fname_as_string (type_dep);
2307 tree decl, type, init;
2308 size_t length = strlen (name);
2310 type = build_array_type
2311 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2312 build_index_type (size_int (length)));
2314 decl = build_decl (VAR_DECL, id, type);
2316 TREE_STATIC (decl) = 1;
2317 TREE_READONLY (decl) = 1;
2318 DECL_ARTIFICIAL (decl) = 1;
2320 init = build_string (length + 1, name);
2321 TREE_TYPE (init) = type;
2322 DECL_INITIAL (decl) = init;
2324 TREE_USED (decl) = 1;
2326 if (current_function_decl)
2328 DECL_CONTEXT (decl) = current_function_decl;
2329 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2330 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2333 finish_decl (decl, init, NULL_TREE);
2335 return decl;
2338 /* Return a definition for a builtin function named NAME and whose data type
2339 is TYPE. TYPE should be a function type with argument types.
2340 FUNCTION_CODE tells later passes how to compile calls to this function.
2341 See tree.h for its possible values.
2343 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2344 the name to be called if we can't opencode the function. If
2345 ATTRS is nonzero, use that for the function's attribute list. */
2347 tree
2348 builtin_function (const char *name, tree type, int function_code,
2349 enum built_in_class class, const char *library_name,
2350 tree attrs)
2352 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2353 DECL_EXTERNAL (decl) = 1;
2354 TREE_PUBLIC (decl) = 1;
2355 if (library_name)
2356 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2357 make_decl_rtl (decl, NULL);
2358 pushdecl (decl);
2359 DECL_BUILT_IN_CLASS (decl) = class;
2360 DECL_FUNCTION_CODE (decl) = function_code;
2362 /* Warn if a function in the namespace for users
2363 is used without an occasion to consider it declared. */
2364 if (name[0] != '_' || name[1] != '_')
2365 C_DECL_INVISIBLE (decl) = 1;
2367 /* Possibly apply some default attributes to this built-in function. */
2368 if (attrs)
2369 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2370 else
2371 decl_attributes (&decl, NULL_TREE, 0);
2373 return decl;
2376 /* Called when a declaration is seen that contains no names to declare.
2377 If its type is a reference to a structure, union or enum inherited
2378 from a containing scope, shadow that tag name for the current scope
2379 with a forward reference.
2380 If its type defines a new named structure or union
2381 or defines an enum, it is valid but we need not do anything here.
2382 Otherwise, it is an error. */
2384 void
2385 shadow_tag (tree declspecs)
2387 shadow_tag_warned (declspecs, 0);
2390 void
2391 shadow_tag_warned (tree declspecs, int warned)
2394 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2395 no pedwarn. */
2397 int found_tag = 0;
2398 tree link;
2399 tree specs, attrs;
2401 pending_invalid_xref = 0;
2403 /* Remove the attributes from declspecs, since they will confuse the
2404 following code. */
2405 split_specs_attrs (declspecs, &specs, &attrs);
2407 for (link = specs; link; link = TREE_CHAIN (link))
2409 tree value = TREE_VALUE (link);
2410 enum tree_code code = TREE_CODE (value);
2412 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2413 /* Used to test also that TYPE_SIZE (value) != 0.
2414 That caused warning for `struct foo;' at top level in the file. */
2416 tree name = TYPE_NAME (value);
2417 tree t;
2419 found_tag++;
2421 if (name == 0)
2423 if (warned != 1 && code != ENUMERAL_TYPE)
2424 /* Empty unnamed enum OK */
2426 pedwarn ("unnamed struct/union that defines no instances");
2427 warned = 1;
2430 else
2432 t = lookup_tag (code, name, 1);
2434 if (t == 0)
2436 t = make_node (code);
2437 pushtag (name, t);
2441 else
2443 if (!warned && ! in_system_header)
2445 warning ("useless keyword or type name in empty declaration");
2446 warned = 2;
2451 if (found_tag > 1)
2452 error ("two types specified in one empty declaration");
2454 if (warned != 1)
2456 if (found_tag == 0)
2457 pedwarn ("empty declaration");
2461 /* Construct an array declarator. EXPR is the expression inside [], or
2462 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2463 to the pointer to which a parameter array is converted). STATIC_P is
2464 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2465 is nonzero is the array is [*], a VLA of unspecified length which is
2466 nevertheless a complete type (not currently implemented by GCC),
2467 zero otherwise. The declarator is constructed as an ARRAY_REF
2468 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2469 left of the [] (filled by in set_array_declarator_type) and operand 1
2470 is the expression inside; whose TREE_TYPE is the type qualifiers and
2471 which has TREE_STATIC set if "static" is used. */
2473 tree
2474 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2476 tree decl;
2477 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2478 TREE_TYPE (decl) = quals;
2479 TREE_STATIC (decl) = (static_p ? 1 : 0);
2480 if (pedantic && !flag_isoc99)
2482 if (static_p || quals != NULL_TREE)
2483 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2484 if (vla_unspec_p)
2485 pedwarn ("ISO C90 does not support `[*]' array declarators");
2487 if (vla_unspec_p)
2488 warning ("GCC does not yet properly implement `[*]' array declarators");
2489 return decl;
2492 /* Set the type of an array declarator. DECL is the declarator, as
2493 constructed by build_array_declarator; TYPE is what appears on the left
2494 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2495 abstract declarator, zero otherwise; this is used to reject static and
2496 type qualifiers in abstract declarators, where they are not in the
2497 C99 grammar. */
2499 tree
2500 set_array_declarator_type (tree decl, tree type, int abstract_p)
2502 TREE_OPERAND (decl, 0) = type;
2503 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2504 error ("static or type qualifiers in abstract declarator");
2505 return decl;
2508 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2510 tree
2511 groktypename (tree typename)
2513 tree specs, attrs;
2515 if (TREE_CODE (typename) != TREE_LIST)
2516 return typename;
2518 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2520 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2522 /* Apply attributes. */
2523 decl_attributes (&typename, attrs, 0);
2525 return typename;
2528 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2530 tree
2531 groktypename_in_parm_context (tree typename)
2533 if (TREE_CODE (typename) != TREE_LIST)
2534 return typename;
2535 return grokdeclarator (TREE_VALUE (typename),
2536 TREE_PURPOSE (typename),
2537 PARM, 0);
2540 /* Decode a declarator in an ordinary declaration or data definition.
2541 This is called as soon as the type information and variable name
2542 have been parsed, before parsing the initializer if any.
2543 Here we create the ..._DECL node, fill in its type,
2544 and put it on the list of decls for the current context.
2545 The ..._DECL node is returned as the value.
2547 Exception: for arrays where the length is not specified,
2548 the type is left null, to be filled in by `finish_decl'.
2550 Function definitions do not come here; they go to start_function
2551 instead. However, external and forward declarations of functions
2552 do go through here. Structure field declarations are done by
2553 grokfield and not through here. */
2555 tree
2556 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2558 tree decl;
2559 tree tem;
2561 /* An object declared as __attribute__((deprecated)) suppresses
2562 warnings of uses of other deprecated items. */
2563 if (lookup_attribute ("deprecated", attributes))
2564 deprecated_state = DEPRECATED_SUPPRESS;
2566 decl = grokdeclarator (declarator, declspecs,
2567 NORMAL, initialized);
2569 deprecated_state = DEPRECATED_NORMAL;
2571 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2572 && MAIN_NAME_P (DECL_NAME (decl)))
2573 warning ("%H'%D' is usually a function",
2574 &DECL_SOURCE_LOCATION (decl), decl);
2576 if (initialized)
2577 /* Is it valid for this decl to have an initializer at all?
2578 If not, set INITIALIZED to zero, which will indirectly
2579 tell `finish_decl' to ignore the initializer once it is parsed. */
2580 switch (TREE_CODE (decl))
2582 case TYPE_DECL:
2583 error ("typedef `%s' is initialized (use __typeof__ instead)",
2584 IDENTIFIER_POINTER (DECL_NAME (decl)));
2585 initialized = 0;
2586 break;
2588 case FUNCTION_DECL:
2589 error ("function `%s' is initialized like a variable",
2590 IDENTIFIER_POINTER (DECL_NAME (decl)));
2591 initialized = 0;
2592 break;
2594 case PARM_DECL:
2595 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2596 error ("parameter `%s' is initialized",
2597 IDENTIFIER_POINTER (DECL_NAME (decl)));
2598 initialized = 0;
2599 break;
2601 default:
2602 /* Don't allow initializations for incomplete types
2603 except for arrays which might be completed by the initialization. */
2605 /* This can happen if the array size is an undefined macro. We already
2606 gave a warning, so we don't need another one. */
2607 if (TREE_TYPE (decl) == error_mark_node)
2608 initialized = 0;
2609 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2611 /* A complete type is ok if size is fixed. */
2613 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2614 || C_DECL_VARIABLE_SIZE (decl))
2616 error ("variable-sized object may not be initialized");
2617 initialized = 0;
2620 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2622 error ("variable `%s' has initializer but incomplete type",
2623 IDENTIFIER_POINTER (DECL_NAME (decl)));
2624 initialized = 0;
2626 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2628 error ("elements of array `%s' have incomplete type",
2629 IDENTIFIER_POINTER (DECL_NAME (decl)));
2630 initialized = 0;
2634 if (initialized)
2636 DECL_EXTERNAL (decl) = 0;
2637 if (current_scope == global_scope)
2638 TREE_STATIC (decl) = 1;
2640 /* Tell `pushdecl' this is an initialized decl
2641 even though we don't yet have the initializer expression.
2642 Also tell `finish_decl' it may store the real initializer. */
2643 DECL_INITIAL (decl) = error_mark_node;
2646 /* If this is a function declaration, write a record describing it to the
2647 prototypes file (if requested). */
2649 if (TREE_CODE (decl) == FUNCTION_DECL)
2650 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2652 /* ANSI specifies that a tentative definition which is not merged with
2653 a non-tentative definition behaves exactly like a definition with an
2654 initializer equal to zero. (Section 3.7.2)
2656 -fno-common gives strict ANSI behavior, though this tends to break
2657 a large body of code that grew up without this rule.
2659 Thread-local variables are never common, since there's no entrenched
2660 body of code to break, and it allows more efficient variable references
2661 in the presence of dynamic linking. */
2663 if (TREE_CODE (decl) == VAR_DECL
2664 && !initialized
2665 && TREE_PUBLIC (decl)
2666 && !DECL_THREAD_LOCAL (decl)
2667 && !flag_no_common)
2668 DECL_COMMON (decl) = 1;
2670 /* Set attributes here so if duplicate decl, will have proper attributes. */
2671 decl_attributes (&decl, attributes, 0);
2673 if (TREE_CODE (decl) == FUNCTION_DECL
2674 && DECL_DECLARED_INLINE_P (decl)
2675 && DECL_UNINLINABLE (decl)
2676 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2677 warning ("%Hinline function '%D' given attribute noinline",
2678 &DECL_SOURCE_LOCATION (decl), decl);
2680 /* Add this decl to the current scope.
2681 TEM may equal DECL or it may be a previous decl of the same name. */
2682 tem = pushdecl (decl);
2684 /* For a local variable, define the RTL now. */
2685 if (current_scope != global_scope
2686 /* But not if this is a duplicate decl
2687 and we preserved the rtl from the previous one
2688 (which may or may not happen). */
2689 && !DECL_RTL_SET_P (tem)
2690 && C_DECL_FILE_SCOPE (tem))
2692 if (TREE_TYPE (tem) != error_mark_node
2693 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2694 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2695 && DECL_INITIAL (tem) != 0)))
2696 expand_decl (tem);
2699 return tem;
2702 /* Finish processing of a declaration;
2703 install its initial value.
2704 If the length of an array type is not known before,
2705 it must be determined now, from the initial value, or it is an error. */
2707 void
2708 finish_decl (tree decl, tree init, tree asmspec_tree)
2710 tree type = TREE_TYPE (decl);
2711 int was_incomplete = (DECL_SIZE (decl) == 0);
2712 const char *asmspec = 0;
2714 /* If a name was specified, get the string. */
2715 if (current_scope == global_scope)
2716 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2717 if (asmspec_tree)
2718 asmspec = TREE_STRING_POINTER (asmspec_tree);
2720 /* If `start_decl' didn't like having an initialization, ignore it now. */
2721 if (init != 0 && DECL_INITIAL (decl) == 0)
2722 init = 0;
2724 /* Don't crash if parm is initialized. */
2725 if (TREE_CODE (decl) == PARM_DECL)
2726 init = 0;
2728 if (init)
2729 store_init_value (decl, init);
2731 /* Deduce size of array from initialization, if not already known */
2732 if (TREE_CODE (type) == ARRAY_TYPE
2733 && TYPE_DOMAIN (type) == 0
2734 && TREE_CODE (decl) != TYPE_DECL)
2736 int do_default
2737 = (TREE_STATIC (decl)
2738 /* Even if pedantic, an external linkage array
2739 may have incomplete type at first. */
2740 ? pedantic && !TREE_PUBLIC (decl)
2741 : !DECL_EXTERNAL (decl));
2742 int failure
2743 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2745 /* Get the completed type made by complete_array_type. */
2746 type = TREE_TYPE (decl);
2748 if (failure == 1)
2749 error ("%Hinitializer fails to determine size of '%D'",
2750 &DECL_SOURCE_LOCATION (decl), decl);
2752 else if (failure == 2)
2754 if (do_default)
2755 error ("%Harray size missing in '%D'",
2756 &DECL_SOURCE_LOCATION (decl), decl);
2757 /* If a `static' var's size isn't known,
2758 make it extern as well as static, so it does not get
2759 allocated.
2760 If it is not `static', then do not mark extern;
2761 finish_incomplete_decl will give it a default size
2762 and it will get allocated. */
2763 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2764 DECL_EXTERNAL (decl) = 1;
2767 /* TYPE_MAX_VALUE is always one less than the number of elements
2768 in the array, because we start counting at zero. Therefore,
2769 warn only if the value is less than zero. */
2770 else if (pedantic && TYPE_DOMAIN (type) != 0
2771 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2772 error ("%Hzero or negative size array '%D'",
2773 &DECL_SOURCE_LOCATION (decl), decl);
2775 layout_decl (decl, 0);
2778 if (TREE_CODE (decl) == VAR_DECL)
2780 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2781 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2782 layout_decl (decl, 0);
2784 if (DECL_SIZE (decl) == 0
2785 /* Don't give an error if we already gave one earlier. */
2786 && TREE_TYPE (decl) != error_mark_node
2787 && (TREE_STATIC (decl)
2789 /* A static variable with an incomplete type
2790 is an error if it is initialized.
2791 Also if it is not file scope.
2792 Otherwise, let it through, but if it is not `extern'
2793 then it may cause an error message later. */
2794 (DECL_INITIAL (decl) != 0
2795 || !C_DECL_FILE_SCOPE (decl))
2797 /* An automatic variable with an incomplete type
2798 is an error. */
2799 !DECL_EXTERNAL (decl)))
2801 error ("%Hstorage size of '%D' isn't known",
2802 &DECL_SOURCE_LOCATION (decl), decl);
2803 TREE_TYPE (decl) = error_mark_node;
2806 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2807 && DECL_SIZE (decl) != 0)
2809 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2810 constant_expression_warning (DECL_SIZE (decl));
2811 else
2812 error ("%Hstorage size of '%D' isn't constant",
2813 &DECL_SOURCE_LOCATION (decl), decl);
2816 if (TREE_USED (type))
2817 TREE_USED (decl) = 1;
2820 /* If this is a function and an assembler name is specified, reset DECL_RTL
2821 so we can give it its new name. Also, update built_in_decls if it
2822 was a normal built-in. */
2823 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2825 /* ASMSPEC is given, and not the name of a register. Mark the
2826 name with a star so assemble_name won't munge it. */
2827 char *starred = alloca (strlen (asmspec) + 2);
2828 starred[0] = '*';
2829 strcpy (starred + 1, asmspec);
2831 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2833 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2834 SET_DECL_RTL (builtin, NULL_RTX);
2835 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2836 #ifdef TARGET_MEM_FUNCTIONS
2837 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2838 init_block_move_fn (starred);
2839 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2840 init_block_clear_fn (starred);
2841 #else
2842 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2843 init_block_move_fn (starred);
2844 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2845 init_block_clear_fn (starred);
2846 #endif
2848 SET_DECL_RTL (decl, NULL_RTX);
2849 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2852 /* If #pragma weak was used, mark the decl weak now. */
2853 if (current_scope == global_scope)
2854 maybe_apply_pragma_weak (decl);
2856 /* Output the assembler code and/or RTL code for variables and functions,
2857 unless the type is an undefined structure or union.
2858 If not, it will get done when the type is completed. */
2860 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2862 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2863 if (c_dialect_objc ())
2864 objc_check_decl (decl);
2866 if (C_DECL_FILE_SCOPE (decl))
2868 if (DECL_INITIAL (decl) == NULL_TREE
2869 || DECL_INITIAL (decl) == error_mark_node)
2870 /* Don't output anything
2871 when a tentative file-scope definition is seen.
2872 But at end of compilation, do output code for them. */
2873 DECL_DEFER_OUTPUT (decl) = 1;
2874 rest_of_decl_compilation (decl, asmspec, true, 0);
2876 else
2878 /* This is a local variable. If there is an ASMSPEC, the
2879 user has requested that we handle it specially. */
2880 if (asmspec)
2882 /* In conjunction with an ASMSPEC, the `register'
2883 keyword indicates that we should place the variable
2884 in a particular register. */
2885 if (DECL_REGISTER (decl))
2886 DECL_C_HARD_REGISTER (decl) = 1;
2888 /* If this is not a static variable, issue a warning.
2889 It doesn't make any sense to give an ASMSPEC for an
2890 ordinary, non-register local variable. Historically,
2891 GCC has accepted -- but ignored -- the ASMSPEC in
2892 this case. */
2893 if (TREE_CODE (decl) == VAR_DECL
2894 && !DECL_REGISTER (decl)
2895 && !TREE_STATIC (decl))
2896 warning ("%Hignoring asm-specifier for non-static local "
2897 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2898 else
2899 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2902 if (TREE_CODE (decl) != FUNCTION_DECL)
2903 add_decl_stmt (decl);
2906 if (!C_DECL_FILE_SCOPE (decl))
2908 /* Recompute the RTL of a local array now
2909 if it used to be an incomplete type. */
2910 if (was_incomplete
2911 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2913 /* If we used it already as memory, it must stay in memory. */
2914 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2915 /* If it's still incomplete now, no init will save it. */
2916 if (DECL_SIZE (decl) == 0)
2917 DECL_INITIAL (decl) = 0;
2922 /* If this was marked 'used', be sure it will be output. */
2923 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2924 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2926 if (TREE_CODE (decl) == TYPE_DECL)
2928 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2929 if (c_dialect_objc ())
2930 objc_check_decl (decl);
2931 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2934 /* At the end of a declaration, throw away any variable type sizes
2935 of types defined inside that declaration. There is no use
2936 computing them in the following function definition. */
2937 if (current_scope == global_scope)
2938 get_pending_sizes ();
2940 /* Install a cleanup (aka destructor) if one was given. */
2941 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2943 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2944 if (attr)
2946 static bool eh_initialized_p;
2948 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2949 tree cleanup_decl = lookup_name (cleanup_id);
2950 tree cleanup;
2952 /* Build "cleanup(&decl)" for the destructor. */
2953 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2954 cleanup = build_tree_list (NULL_TREE, cleanup);
2955 cleanup = build_function_call (cleanup_decl, cleanup);
2957 /* Don't warn about decl unused; the cleanup uses it. */
2958 TREE_USED (decl) = 1;
2960 /* Initialize EH, if we've been told to do so. */
2961 if (flag_exceptions && !eh_initialized_p)
2963 eh_initialized_p = true;
2964 eh_personality_libfunc
2965 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2966 ? "__gcc_personality_sj0"
2967 : "__gcc_personality_v0");
2968 using_eh_for_cleanups ();
2971 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2976 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2977 and push that on the current scope. */
2979 void
2980 push_parm_decl (tree parm)
2982 tree decl;
2984 /* Don't attempt to expand sizes while parsing this decl.
2985 (We can get here with i_s_e 1 somehow from Objective-C.) */
2986 int save_immediate_size_expand = immediate_size_expand;
2987 immediate_size_expand = 0;
2989 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2990 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2991 decl_attributes (&decl, TREE_VALUE (parm), 0);
2993 decl = pushdecl (decl);
2995 finish_decl (decl, NULL_TREE, NULL_TREE);
2997 immediate_size_expand = save_immediate_size_expand;
3000 /* Mark all the parameter declarations to date as forward decls,
3001 shift them to the variables list, and reset the parameters list.
3002 Also diagnose use of this extension. */
3004 void
3005 mark_forward_parm_decls (void)
3007 tree parm;
3009 if (pedantic && !current_scope->warned_forward_parm_decls)
3011 pedwarn ("ISO C forbids forward parameter declarations");
3012 current_scope->warned_forward_parm_decls = true;
3015 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3016 TREE_ASM_WRITTEN (parm) = 1;
3018 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3019 current_scope->parms = 0;
3020 current_scope->parms_last = 0;
3023 static GTY(()) int compound_literal_number;
3025 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3026 literal, which may be an incomplete array type completed by the
3027 initializer; INIT is a CONSTRUCTOR that initializes the compound
3028 literal. */
3030 tree
3031 build_compound_literal (tree type, tree init)
3033 /* We do not use start_decl here because we have a type, not a declarator;
3034 and do not use finish_decl because the decl should be stored inside
3035 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3036 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3037 tree complit;
3038 tree stmt;
3039 DECL_EXTERNAL (decl) = 0;
3040 TREE_PUBLIC (decl) = 0;
3041 TREE_STATIC (decl) = (current_scope == global_scope);
3042 DECL_CONTEXT (decl) = current_function_decl;
3043 TREE_USED (decl) = 1;
3044 TREE_TYPE (decl) = type;
3045 TREE_READONLY (decl) = TREE_READONLY (type);
3046 store_init_value (decl, init);
3048 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3050 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3051 if (failure)
3052 abort ();
3055 type = TREE_TYPE (decl);
3056 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3057 return error_mark_node;
3059 stmt = build_stmt (DECL_STMT, decl);
3060 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3061 TREE_SIDE_EFFECTS (complit) = 1;
3063 layout_decl (decl, 0);
3065 if (TREE_STATIC (decl))
3067 /* This decl needs a name for the assembler output. We also need
3068 a unique suffix to be added to the name. */
3069 char *name;
3071 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3072 compound_literal_number);
3073 compound_literal_number++;
3074 DECL_NAME (decl) = get_identifier (name);
3075 DECL_DEFER_OUTPUT (decl) = 1;
3076 DECL_COMDAT (decl) = 1;
3077 DECL_ARTIFICIAL (decl) = 1;
3078 pushdecl (decl);
3079 rest_of_decl_compilation (decl, NULL, 1, 0);
3082 return complit;
3085 /* Make TYPE a complete type based on INITIAL_VALUE.
3086 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3087 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3090 complete_array_type (tree type, tree initial_value, int do_default)
3092 tree maxindex = NULL_TREE;
3093 int value = 0;
3095 if (initial_value)
3097 /* Note MAXINDEX is really the maximum index,
3098 one less than the size. */
3099 if (TREE_CODE (initial_value) == STRING_CST)
3101 int eltsize
3102 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3103 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3104 / eltsize) - 1, 0);
3106 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3108 tree elts = CONSTRUCTOR_ELTS (initial_value);
3109 maxindex = build_int_2 (-1, -1);
3110 for (; elts; elts = TREE_CHAIN (elts))
3112 if (TREE_PURPOSE (elts))
3113 maxindex = TREE_PURPOSE (elts);
3114 else
3115 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3116 maxindex, integer_one_node));
3118 maxindex = copy_node (maxindex);
3120 else
3122 /* Make an error message unless that happened already. */
3123 if (initial_value != error_mark_node)
3124 value = 1;
3126 /* Prevent further error messages. */
3127 maxindex = build_int_2 (0, 0);
3131 if (!maxindex)
3133 if (do_default)
3134 maxindex = build_int_2 (0, 0);
3135 value = 2;
3138 if (maxindex)
3140 TYPE_DOMAIN (type) = build_index_type (maxindex);
3141 if (!TREE_TYPE (maxindex))
3142 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3145 /* Lay out the type now that we can get the real answer. */
3147 layout_type (type);
3149 return value;
3152 /* Determine whether TYPE is a structure with a flexible array member,
3153 or a union containing such a structure (possibly recursively). */
3155 static bool
3156 flexible_array_type_p (tree type)
3158 tree x;
3159 switch (TREE_CODE (type))
3161 case RECORD_TYPE:
3162 x = TYPE_FIELDS (type);
3163 if (x == NULL_TREE)
3164 return false;
3165 while (TREE_CHAIN (x) != NULL_TREE)
3166 x = TREE_CHAIN (x);
3167 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3168 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3169 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3170 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3171 return true;
3172 return false;
3173 case UNION_TYPE:
3174 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3176 if (flexible_array_type_p (TREE_TYPE (x)))
3177 return true;
3179 return false;
3180 default:
3181 return false;
3185 /* Given declspecs and a declarator,
3186 determine the name and type of the object declared
3187 and construct a ..._DECL node for it.
3188 (In one case we can return a ..._TYPE node instead.
3189 For invalid input we sometimes return 0.)
3191 DECLSPECS is a chain of tree_list nodes whose value fields
3192 are the storage classes and type specifiers.
3194 DECL_CONTEXT says which syntactic context this declaration is in:
3195 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3196 FUNCDEF for a function definition. Like NORMAL but a few different
3197 error messages in each case. Return value may be zero meaning
3198 this definition is too screwy to try to parse.
3199 PARM for a parameter declaration (either within a function prototype
3200 or before a function body). Make a PARM_DECL, or return void_type_node.
3201 TYPENAME if for a typename (in a cast or sizeof).
3202 Don't make a DECL node; just return the ..._TYPE node.
3203 FIELD for a struct or union field; make a FIELD_DECL.
3204 BITFIELD for a field with specified width.
3205 INITIALIZED is 1 if the decl has an initializer.
3207 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3208 It may also be so in the PARM case, for a prototype where the
3209 argument type is specified but not the name.
3211 This function is where the complicated C meanings of `static'
3212 and `extern' are interpreted. */
3214 static tree
3215 grokdeclarator (tree declarator, tree declspecs,
3216 enum decl_context decl_context, int initialized)
3218 int specbits = 0;
3219 tree spec;
3220 tree type = NULL_TREE;
3221 int longlong = 0;
3222 int constp;
3223 int restrictp;
3224 int volatilep;
3225 int type_quals = TYPE_UNQUALIFIED;
3226 int inlinep;
3227 int explicit_int = 0;
3228 int explicit_char = 0;
3229 int defaulted_int = 0;
3230 tree typedef_decl = 0;
3231 const char *name;
3232 tree typedef_type = 0;
3233 int funcdef_flag = 0;
3234 enum tree_code innermost_code = ERROR_MARK;
3235 int bitfield = 0;
3236 int size_varies = 0;
3237 tree decl_attr = NULL_TREE;
3238 tree array_ptr_quals = NULL_TREE;
3239 int array_parm_static = 0;
3240 tree returned_attrs = NULL_TREE;
3242 if (decl_context == BITFIELD)
3243 bitfield = 1, decl_context = FIELD;
3245 if (decl_context == FUNCDEF)
3246 funcdef_flag = 1, decl_context = NORMAL;
3248 /* Look inside a declarator for the name being declared
3249 and get it as a string, for an error message. */
3251 tree decl = declarator;
3252 name = 0;
3254 while (decl)
3255 switch (TREE_CODE (decl))
3257 case ARRAY_REF:
3258 case INDIRECT_REF:
3259 case CALL_EXPR:
3260 innermost_code = TREE_CODE (decl);
3261 decl = TREE_OPERAND (decl, 0);
3262 break;
3264 case TREE_LIST:
3265 decl = TREE_VALUE (decl);
3266 break;
3268 case IDENTIFIER_NODE:
3269 name = IDENTIFIER_POINTER (decl);
3270 decl = 0;
3271 break;
3273 default:
3274 abort ();
3276 if (name == 0)
3277 name = "type name";
3280 /* A function definition's declarator must have the form of
3281 a function declarator. */
3283 if (funcdef_flag && innermost_code != CALL_EXPR)
3284 return 0;
3286 /* If this looks like a function definition, make it one,
3287 even if it occurs where parms are expected.
3288 Then store_parm_decls will reject it and not use it as a parm. */
3289 if (decl_context == NORMAL && !funcdef_flag
3290 && current_scope->parm_flag)
3291 decl_context = PARM;
3293 /* Look through the decl specs and record which ones appear.
3294 Some typespecs are defined as built-in typenames.
3295 Others, the ones that are modifiers of other types,
3296 are represented by bits in SPECBITS: set the bits for
3297 the modifiers that appear. Storage class keywords are also in SPECBITS.
3299 If there is a typedef name or a type, store the type in TYPE.
3300 This includes builtin typedefs such as `int'.
3302 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3303 and did not come from a user typedef.
3305 Set LONGLONG if `long' is mentioned twice. */
3307 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3309 tree id = TREE_VALUE (spec);
3311 /* If the entire declaration is itself tagged as deprecated then
3312 suppress reports of deprecated items. */
3313 if (id && TREE_DEPRECATED (id))
3315 if (deprecated_state != DEPRECATED_SUPPRESS)
3316 warn_deprecated_use (id);
3319 if (id == ridpointers[(int) RID_INT])
3320 explicit_int = 1;
3321 if (id == ridpointers[(int) RID_CHAR])
3322 explicit_char = 1;
3324 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3326 enum rid i = C_RID_CODE (id);
3327 if ((int) i <= (int) RID_LAST_MODIFIER)
3329 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3331 if (longlong)
3332 error ("`long long long' is too long for GCC");
3333 else
3335 if (pedantic && !flag_isoc99 && ! in_system_header
3336 && warn_long_long)
3337 pedwarn ("ISO C90 does not support `long long'");
3338 longlong = 1;
3341 else if (specbits & (1 << (int) i))
3343 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3345 if (!flag_isoc99)
3346 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3348 else
3349 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3352 /* Diagnose "__thread extern". Recall that this list
3353 is in the reverse order seen in the text. */
3354 if (i == RID_THREAD
3355 && (specbits & (1 << (int) RID_EXTERN
3356 | 1 << (int) RID_STATIC)))
3358 if (specbits & 1 << (int) RID_EXTERN)
3359 error ("`__thread' before `extern'");
3360 else
3361 error ("`__thread' before `static'");
3364 specbits |= 1 << (int) i;
3365 goto found;
3368 if (type)
3369 error ("two or more data types in declaration of `%s'", name);
3370 /* Actual typedefs come to us as TYPE_DECL nodes. */
3371 else if (TREE_CODE (id) == TYPE_DECL)
3373 if (TREE_TYPE (id) == error_mark_node)
3374 ; /* Allow the type to default to int to avoid cascading errors. */
3375 else
3377 type = TREE_TYPE (id);
3378 decl_attr = DECL_ATTRIBUTES (id);
3379 typedef_decl = id;
3382 /* Built-in types come as identifiers. */
3383 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3385 tree t = lookup_name (id);
3386 if (TREE_TYPE (t) == error_mark_node)
3388 else if (!t || TREE_CODE (t) != TYPE_DECL)
3389 error ("`%s' fails to be a typedef or built in type",
3390 IDENTIFIER_POINTER (id));
3391 else
3393 type = TREE_TYPE (t);
3394 typedef_decl = t;
3397 else if (TREE_CODE (id) != ERROR_MARK)
3398 type = id;
3400 found:
3404 typedef_type = type;
3405 if (type)
3406 size_varies = C_TYPE_VARIABLE_SIZE (type);
3408 /* No type at all: default to `int', and set DEFAULTED_INT
3409 because it was not a user-defined typedef. */
3411 if (type == 0)
3413 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3414 | (1 << (int) RID_SIGNED)
3415 | (1 << (int) RID_UNSIGNED)
3416 | (1 << (int) RID_COMPLEX))))
3417 /* Don't warn about typedef foo = bar. */
3418 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3419 && ! in_system_header)
3421 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3422 and this is a function, or if -Wimplicit; prefer the former
3423 warning since it is more explicit. */
3424 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3425 && funcdef_flag)
3426 warn_about_return_type = 1;
3427 else if (warn_implicit_int || flag_isoc99)
3428 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3429 name);
3432 defaulted_int = 1;
3433 type = integer_type_node;
3436 /* Now process the modifiers that were specified
3437 and check for invalid combinations. */
3439 /* Long double is a special combination. */
3441 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3442 && TYPE_MAIN_VARIANT (type) == double_type_node)
3444 specbits &= ~(1 << (int) RID_LONG);
3445 type = long_double_type_node;
3448 /* Check all other uses of type modifiers. */
3450 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3451 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3453 int ok = 0;
3455 if ((specbits & 1 << (int) RID_LONG)
3456 && (specbits & 1 << (int) RID_SHORT))
3457 error ("both long and short specified for `%s'", name);
3458 else if (((specbits & 1 << (int) RID_LONG)
3459 || (specbits & 1 << (int) RID_SHORT))
3460 && explicit_char)
3461 error ("long or short specified with char for `%s'", name);
3462 else if (((specbits & 1 << (int) RID_LONG)
3463 || (specbits & 1 << (int) RID_SHORT))
3464 && TREE_CODE (type) == REAL_TYPE)
3466 static int already = 0;
3468 error ("long or short specified with floating type for `%s'", name);
3469 if (! already && ! pedantic)
3471 error ("the only valid combination is `long double'");
3472 already = 1;
3475 else if ((specbits & 1 << (int) RID_SIGNED)
3476 && (specbits & 1 << (int) RID_UNSIGNED))
3477 error ("both signed and unsigned specified for `%s'", name);
3478 else if (TREE_CODE (type) != INTEGER_TYPE)
3479 error ("long, short, signed or unsigned invalid for `%s'", name);
3480 else
3482 ok = 1;
3483 if (!explicit_int && !defaulted_int && !explicit_char)
3485 error ("long, short, signed or unsigned used invalidly for `%s'",
3486 name);
3487 ok = 0;
3491 /* Discard the type modifiers if they are invalid. */
3492 if (! ok)
3494 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3495 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3496 longlong = 0;
3500 if ((specbits & (1 << (int) RID_COMPLEX))
3501 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3503 error ("complex invalid for `%s'", name);
3504 specbits &= ~(1 << (int) RID_COMPLEX);
3507 /* Decide whether an integer type is signed or not.
3508 Optionally treat bitfields as signed by default. */
3509 if (specbits & 1 << (int) RID_UNSIGNED
3510 || (bitfield && ! flag_signed_bitfields
3511 && (explicit_int || defaulted_int || explicit_char
3512 /* A typedef for plain `int' without `signed'
3513 can be controlled just like plain `int'. */
3514 || ! (typedef_decl != 0
3515 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3516 && TREE_CODE (type) != ENUMERAL_TYPE
3517 && !(specbits & 1 << (int) RID_SIGNED)))
3519 if (longlong)
3520 type = long_long_unsigned_type_node;
3521 else if (specbits & 1 << (int) RID_LONG)
3522 type = long_unsigned_type_node;
3523 else if (specbits & 1 << (int) RID_SHORT)
3524 type = short_unsigned_type_node;
3525 else if (type == char_type_node)
3526 type = unsigned_char_type_node;
3527 else if (typedef_decl)
3528 type = c_common_unsigned_type (type);
3529 else
3530 type = unsigned_type_node;
3532 else if ((specbits & 1 << (int) RID_SIGNED)
3533 && type == char_type_node)
3534 type = signed_char_type_node;
3535 else if (longlong)
3536 type = long_long_integer_type_node;
3537 else if (specbits & 1 << (int) RID_LONG)
3538 type = long_integer_type_node;
3539 else if (specbits & 1 << (int) RID_SHORT)
3540 type = short_integer_type_node;
3542 if (specbits & 1 << (int) RID_COMPLEX)
3544 if (pedantic && !flag_isoc99)
3545 pedwarn ("ISO C90 does not support complex types");
3546 /* If we just have "complex", it is equivalent to
3547 "complex double", but if any modifiers at all are specified it is
3548 the complex form of TYPE. E.g, "complex short" is
3549 "complex short int". */
3551 if (defaulted_int && ! longlong
3552 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3553 | (1 << (int) RID_SIGNED)
3554 | (1 << (int) RID_UNSIGNED))))
3556 if (pedantic)
3557 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3558 type = complex_double_type_node;
3560 else if (type == integer_type_node)
3562 if (pedantic)
3563 pedwarn ("ISO C does not support complex integer types");
3564 type = complex_integer_type_node;
3566 else if (type == float_type_node)
3567 type = complex_float_type_node;
3568 else if (type == double_type_node)
3569 type = complex_double_type_node;
3570 else if (type == long_double_type_node)
3571 type = complex_long_double_type_node;
3572 else
3574 if (pedantic)
3575 pedwarn ("ISO C does not support complex integer types");
3576 type = build_complex_type (type);
3580 /* Figure out the type qualifiers for the declaration. There are
3581 two ways a declaration can become qualified. One is something
3582 like `const int i' where the `const' is explicit. Another is
3583 something like `typedef const int CI; CI i' where the type of the
3584 declaration contains the `const'. */
3585 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3586 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3587 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3588 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3589 if (constp > 1 && ! flag_isoc99)
3590 pedwarn ("duplicate `const'");
3591 if (restrictp > 1 && ! flag_isoc99)
3592 pedwarn ("duplicate `restrict'");
3593 if (volatilep > 1 && ! flag_isoc99)
3594 pedwarn ("duplicate `volatile'");
3595 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3596 type = TYPE_MAIN_VARIANT (type);
3597 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3598 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3599 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3601 /* Warn if two storage classes are given. Default to `auto'. */
3604 int nclasses = 0;
3606 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3607 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3608 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3609 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3610 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3612 /* "static __thread" and "extern __thread" are allowed. */
3613 if ((specbits & (1 << (int) RID_THREAD
3614 | 1 << (int) RID_STATIC
3615 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3616 nclasses++;
3618 /* Warn about storage classes that are invalid for certain
3619 kinds of declarations (parameters, typenames, etc.). */
3621 if (nclasses > 1)
3622 error ("multiple storage classes in declaration of `%s'", name);
3623 else if (funcdef_flag
3624 && (specbits
3625 & ((1 << (int) RID_REGISTER)
3626 | (1 << (int) RID_AUTO)
3627 | (1 << (int) RID_TYPEDEF)
3628 | (1 << (int) RID_THREAD))))
3630 if (specbits & 1 << (int) RID_AUTO
3631 && (pedantic || current_scope == global_scope))
3632 pedwarn ("function definition declared `auto'");
3633 if (specbits & 1 << (int) RID_REGISTER)
3634 error ("function definition declared `register'");
3635 if (specbits & 1 << (int) RID_TYPEDEF)
3636 error ("function definition declared `typedef'");
3637 if (specbits & 1 << (int) RID_THREAD)
3638 error ("function definition declared `__thread'");
3639 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3640 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3642 else if (decl_context != NORMAL && nclasses > 0)
3644 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3646 else
3648 switch (decl_context)
3650 case FIELD:
3651 error ("storage class specified for structure field `%s'",
3652 name);
3653 break;
3654 case PARM:
3655 error ("storage class specified for parameter `%s'", name);
3656 break;
3657 default:
3658 error ("storage class specified for typename");
3659 break;
3661 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3662 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3663 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3666 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3668 /* `extern' with initialization is invalid if not at file scope. */
3669 if (current_scope == global_scope)
3670 warning ("`%s' initialized and declared `extern'", name);
3671 else
3672 error ("`%s' has both `extern' and initializer", name);
3674 else if (current_scope == global_scope)
3676 if (specbits & 1 << (int) RID_AUTO)
3677 error ("file-scope declaration of `%s' specifies `auto'", name);
3679 else
3681 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3682 error ("nested function `%s' declared `extern'", name);
3683 else if ((specbits & (1 << (int) RID_THREAD
3684 | 1 << (int) RID_EXTERN
3685 | 1 << (int) RID_STATIC))
3686 == (1 << (int) RID_THREAD))
3688 error ("function-scope `%s' implicitly auto and declared `__thread'",
3689 name);
3690 specbits &= ~(1 << (int) RID_THREAD);
3695 /* Now figure out the structure of the declarator proper.
3696 Descend through it, creating more complex types, until we reach
3697 the declared identifier (or NULL_TREE, in an absolute declarator). */
3699 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3701 if (type == error_mark_node)
3703 declarator = TREE_OPERAND (declarator, 0);
3704 continue;
3707 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3708 an INDIRECT_REF (for *...),
3709 a CALL_EXPR (for ...(...)),
3710 a TREE_LIST (for nested attributes),
3711 an identifier (for the name being declared)
3712 or a null pointer (for the place in an absolute declarator
3713 where the name was omitted).
3714 For the last two cases, we have just exited the loop.
3716 At this point, TYPE is the type of elements of an array,
3717 or for a function to return, or for a pointer to point to.
3718 After this sequence of ifs, TYPE is the type of the
3719 array or function or pointer, and DECLARATOR has had its
3720 outermost layer removed. */
3722 if (array_ptr_quals != NULL_TREE || array_parm_static)
3724 /* Only the innermost declarator (making a parameter be of
3725 array type which is converted to pointer type)
3726 may have static or type qualifiers. */
3727 error ("static or type qualifiers in non-parameter array declarator");
3728 array_ptr_quals = NULL_TREE;
3729 array_parm_static = 0;
3732 if (TREE_CODE (declarator) == TREE_LIST)
3734 /* We encode a declarator with embedded attributes using
3735 a TREE_LIST. */
3736 tree attrs = TREE_PURPOSE (declarator);
3737 tree inner_decl;
3738 int attr_flags = 0;
3739 declarator = TREE_VALUE (declarator);
3740 inner_decl = declarator;
3741 while (inner_decl != NULL_TREE
3742 && TREE_CODE (inner_decl) == TREE_LIST)
3743 inner_decl = TREE_VALUE (inner_decl);
3744 if (inner_decl == NULL_TREE
3745 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3746 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3747 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3748 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3749 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3750 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3751 returned_attrs = decl_attributes (&type,
3752 chainon (returned_attrs, attrs),
3753 attr_flags);
3755 else if (TREE_CODE (declarator) == ARRAY_REF)
3757 tree itype = NULL_TREE;
3758 tree size = TREE_OPERAND (declarator, 1);
3759 /* The index is a signed object `sizetype' bits wide. */
3760 tree index_type = c_common_signed_type (sizetype);
3762 array_ptr_quals = TREE_TYPE (declarator);
3763 array_parm_static = TREE_STATIC (declarator);
3765 declarator = TREE_OPERAND (declarator, 0);
3767 /* Check for some types that there cannot be arrays of. */
3769 if (VOID_TYPE_P (type))
3771 error ("declaration of `%s' as array of voids", name);
3772 type = error_mark_node;
3775 if (TREE_CODE (type) == FUNCTION_TYPE)
3777 error ("declaration of `%s' as array of functions", name);
3778 type = error_mark_node;
3781 if (pedantic && flexible_array_type_p (type))
3782 pedwarn ("invalid use of structure with flexible array member");
3784 if (size == error_mark_node)
3785 type = error_mark_node;
3787 if (type == error_mark_node)
3788 continue;
3790 /* If size was specified, set ITYPE to a range-type for that size.
3791 Otherwise, ITYPE remains null. finish_decl may figure it out
3792 from an initial value. */
3794 if (size)
3796 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3797 STRIP_TYPE_NOPS (size);
3799 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3801 error ("size of array `%s' has non-integer type", name);
3802 size = integer_one_node;
3805 if (pedantic && integer_zerop (size))
3806 pedwarn ("ISO C forbids zero-size array `%s'", name);
3808 if (TREE_CODE (size) == INTEGER_CST)
3810 constant_expression_warning (size);
3811 if (tree_int_cst_sgn (size) < 0)
3813 error ("size of array `%s' is negative", name);
3814 size = integer_one_node;
3817 else
3819 /* Make sure the array size remains visibly nonconstant
3820 even if it is (eg) a const variable with known value. */
3821 size_varies = 1;
3823 if (!flag_isoc99 && pedantic)
3825 if (TREE_CONSTANT (size))
3826 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3827 name);
3828 else
3829 pedwarn ("ISO C90 forbids variable-size array `%s'",
3830 name);
3834 if (integer_zerop (size))
3836 /* A zero-length array cannot be represented with an
3837 unsigned index type, which is what we'll get with
3838 build_index_type. Create an open-ended range instead. */
3839 itype = build_range_type (sizetype, size, NULL_TREE);
3841 else
3843 /* Compute the maximum valid index, that is, size - 1.
3844 Do the calculation in index_type, so that if it is
3845 a variable the computations will be done in the
3846 proper mode. */
3847 itype = fold (build (MINUS_EXPR, index_type,
3848 convert (index_type, size),
3849 convert (index_type, size_one_node)));
3851 /* If that overflowed, the array is too big.
3852 ??? While a size of INT_MAX+1 technically shouldn't
3853 cause an overflow (because we subtract 1), the overflow
3854 is recorded during the conversion to index_type, before
3855 the subtraction. Handling this case seems like an
3856 unnecessary complication. */
3857 if (TREE_OVERFLOW (itype))
3859 error ("size of array `%s' is too large", name);
3860 type = error_mark_node;
3861 continue;
3864 if (size_varies)
3866 /* We must be able to distinguish the
3867 SAVE_EXPR_CONTEXT for the variably-sized type
3868 so that we can set it correctly in
3869 set_save_expr_context. The convention is
3870 that all SAVE_EXPRs that need to be reset
3871 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3872 tree cfd = current_function_decl;
3873 if (decl_context == PARM)
3874 current_function_decl = NULL_TREE;
3875 itype = variable_size (itype);
3876 if (decl_context == PARM)
3877 current_function_decl = cfd;
3879 itype = build_index_type (itype);
3882 else if (decl_context == FIELD)
3884 if (pedantic && !flag_isoc99 && !in_system_header)
3885 pedwarn ("ISO C90 does not support flexible array members");
3887 /* ISO C99 Flexible array members are effectively identical
3888 to GCC's zero-length array extension. */
3889 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3892 /* If pedantic, complain about arrays of incomplete types. */
3894 if (pedantic && !COMPLETE_TYPE_P (type))
3895 pedwarn ("array type has incomplete element type");
3897 /* Build the array type itself, then merge any constancy or
3898 volatility into the target type. We must do it in this order
3899 to ensure that the TYPE_MAIN_VARIANT field of the array type
3900 is set correctly. */
3902 type = build_array_type (type, itype);
3903 if (type_quals)
3904 type = c_build_qualified_type (type, type_quals);
3906 if (size_varies)
3907 C_TYPE_VARIABLE_SIZE (type) = 1;
3909 /* The GCC extension for zero-length arrays differs from
3910 ISO flexible array members in that sizeof yields zero. */
3911 if (size && integer_zerop (size))
3913 layout_type (type);
3914 TYPE_SIZE (type) = bitsize_zero_node;
3915 TYPE_SIZE_UNIT (type) = size_zero_node;
3917 if (decl_context != PARM
3918 && (array_ptr_quals != NULL_TREE || array_parm_static))
3920 error ("static or type qualifiers in non-parameter array declarator");
3921 array_ptr_quals = NULL_TREE;
3922 array_parm_static = 0;
3925 else if (TREE_CODE (declarator) == CALL_EXPR)
3927 tree arg_types;
3929 /* Declaring a function type.
3930 Make sure we have a valid type for the function to return. */
3931 if (type == error_mark_node)
3932 continue;
3934 size_varies = 0;
3936 /* Warn about some types functions can't return. */
3938 if (TREE_CODE (type) == FUNCTION_TYPE)
3940 error ("`%s' declared as function returning a function", name);
3941 type = integer_type_node;
3943 if (TREE_CODE (type) == ARRAY_TYPE)
3945 error ("`%s' declared as function returning an array", name);
3946 type = integer_type_node;
3949 /* Construct the function type and go to the next
3950 inner layer of declarator. */
3952 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3953 funcdef_flag
3954 /* Say it's a definition
3955 only for the CALL_EXPR
3956 closest to the identifier. */
3957 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3958 /* Type qualifiers before the return type of the function
3959 qualify the return type, not the function type. */
3960 if (type_quals)
3962 /* Type qualifiers on a function return type are normally
3963 permitted by the standard but have no effect, so give a
3964 warning at -Wextra. Qualifiers on a void return type have
3965 meaning as a GNU extension, and are banned on function
3966 definitions in ISO C. FIXME: strictly we shouldn't
3967 pedwarn for qualified void return types except on function
3968 definitions, but not doing so could lead to the undesirable
3969 state of a "volatile void" function return type not being
3970 warned about, and a use of the function being compiled
3971 with GNU semantics, with no diagnostics under -pedantic. */
3972 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3973 pedwarn ("ISO C forbids qualified void function return type");
3974 else if (extra_warnings
3975 && !(VOID_TYPE_P (type)
3976 && type_quals == TYPE_QUAL_VOLATILE))
3977 warning ("type qualifiers ignored on function return type");
3979 type = c_build_qualified_type (type, type_quals);
3981 type_quals = TYPE_UNQUALIFIED;
3983 type = build_function_type (type, arg_types);
3984 declarator = TREE_OPERAND (declarator, 0);
3986 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3987 the formal parameter list of this FUNCTION_TYPE to point to
3988 the FUNCTION_TYPE node itself. */
3991 tree link;
3993 for (link = last_function_parm_tags;
3994 link;
3995 link = TREE_CHAIN (link))
3996 TYPE_CONTEXT (TREE_VALUE (link)) = type;
3999 else if (TREE_CODE (declarator) == INDIRECT_REF)
4001 /* Merge any constancy or volatility into the target type
4002 for the pointer. */
4004 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4005 && type_quals)
4006 pedwarn ("ISO C forbids qualified function types");
4007 if (type_quals)
4008 type = c_build_qualified_type (type, type_quals);
4009 type_quals = TYPE_UNQUALIFIED;
4010 size_varies = 0;
4012 type = build_pointer_type (type);
4014 /* Process a list of type modifier keywords
4015 (such as const or volatile) that were given inside the `*'. */
4017 if (TREE_TYPE (declarator))
4019 tree typemodlist;
4020 int erred = 0;
4022 constp = 0;
4023 volatilep = 0;
4024 restrictp = 0;
4025 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4026 typemodlist = TREE_CHAIN (typemodlist))
4028 tree qualifier = TREE_VALUE (typemodlist);
4030 if (C_IS_RESERVED_WORD (qualifier))
4032 if (C_RID_CODE (qualifier) == RID_CONST)
4033 constp++;
4034 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4035 volatilep++;
4036 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4037 restrictp++;
4038 else
4039 erred++;
4041 else
4042 erred++;
4045 if (erred)
4046 error ("invalid type modifier within pointer declarator");
4047 if (constp > 1 && ! flag_isoc99)
4048 pedwarn ("duplicate `const'");
4049 if (volatilep > 1 && ! flag_isoc99)
4050 pedwarn ("duplicate `volatile'");
4051 if (restrictp > 1 && ! flag_isoc99)
4052 pedwarn ("duplicate `restrict'");
4054 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4055 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4056 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4059 declarator = TREE_OPERAND (declarator, 0);
4061 else
4062 abort ();
4066 /* Now TYPE has the actual type. */
4068 /* Did array size calculations overflow? */
4070 if (TREE_CODE (type) == ARRAY_TYPE
4071 && COMPLETE_TYPE_P (type)
4072 && TREE_OVERFLOW (TYPE_SIZE (type)))
4074 error ("size of array `%s' is too large", name);
4075 /* If we proceed with the array type as it is, we'll eventually
4076 crash in tree_low_cst(). */
4077 type = error_mark_node;
4080 /* If this is declaring a typedef name, return a TYPE_DECL. */
4082 if (specbits & (1 << (int) RID_TYPEDEF))
4084 tree decl;
4085 /* Note that the grammar rejects storage classes
4086 in typenames, fields or parameters */
4087 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4088 && type_quals)
4089 pedwarn ("ISO C forbids qualified function types");
4090 if (type_quals)
4091 type = c_build_qualified_type (type, type_quals);
4092 decl = build_decl (TYPE_DECL, declarator, type);
4093 if ((specbits & (1 << (int) RID_SIGNED))
4094 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4095 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4096 decl_attributes (&decl, returned_attrs, 0);
4097 return decl;
4100 /* Detect the case of an array type of unspecified size
4101 which came, as such, direct from a typedef name.
4102 We must copy the type, so that each identifier gets
4103 a distinct type, so that each identifier's size can be
4104 controlled separately by its own initializer. */
4106 if (type != 0 && typedef_type != 0
4107 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4108 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4110 type = build_array_type (TREE_TYPE (type), 0);
4111 if (size_varies)
4112 C_TYPE_VARIABLE_SIZE (type) = 1;
4115 /* If this is a type name (such as, in a cast or sizeof),
4116 compute the type and return it now. */
4118 if (decl_context == TYPENAME)
4120 /* Note that the grammar rejects storage classes
4121 in typenames, fields or parameters */
4122 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4123 && type_quals)
4124 pedwarn ("ISO C forbids const or volatile function types");
4125 if (type_quals)
4126 type = c_build_qualified_type (type, type_quals);
4127 decl_attributes (&type, returned_attrs, 0);
4128 return type;
4131 /* Aside from typedefs and type names (handle above),
4132 `void' at top level (not within pointer)
4133 is allowed only in public variables.
4134 We don't complain about parms either, but that is because
4135 a better error message can be made later. */
4137 if (VOID_TYPE_P (type) && decl_context != PARM
4138 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4139 && ((specbits & (1 << (int) RID_EXTERN))
4140 || (current_scope == global_scope
4141 && !(specbits
4142 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4144 error ("variable or field `%s' declared void", name);
4145 type = integer_type_node;
4148 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4149 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4152 tree decl;
4154 if (decl_context == PARM)
4156 tree type_as_written;
4157 tree promoted_type;
4159 /* A parameter declared as an array of T is really a pointer to T.
4160 One declared as a function is really a pointer to a function. */
4162 if (TREE_CODE (type) == ARRAY_TYPE)
4164 /* Transfer const-ness of array into that of type pointed to. */
4165 type = TREE_TYPE (type);
4166 if (type_quals)
4167 type = c_build_qualified_type (type, type_quals);
4168 type = build_pointer_type (type);
4169 type_quals = TYPE_UNQUALIFIED;
4170 if (array_ptr_quals)
4172 tree new_ptr_quals, new_ptr_attrs;
4173 int erred = 0;
4174 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4175 /* We don't yet implement attributes in this context. */
4176 if (new_ptr_attrs != NULL_TREE)
4177 warning ("attributes in parameter array declarator ignored");
4179 constp = 0;
4180 volatilep = 0;
4181 restrictp = 0;
4182 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4184 tree qualifier = TREE_VALUE (new_ptr_quals);
4186 if (C_IS_RESERVED_WORD (qualifier))
4188 if (C_RID_CODE (qualifier) == RID_CONST)
4189 constp++;
4190 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4191 volatilep++;
4192 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4193 restrictp++;
4194 else
4195 erred++;
4197 else
4198 erred++;
4201 if (erred)
4202 error ("invalid type modifier within array declarator");
4204 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4205 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4206 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4208 size_varies = 0;
4210 else if (TREE_CODE (type) == FUNCTION_TYPE)
4212 if (pedantic && type_quals)
4213 pedwarn ("ISO C forbids qualified function types");
4214 if (type_quals)
4215 type = c_build_qualified_type (type, type_quals);
4216 type = build_pointer_type (type);
4217 type_quals = TYPE_UNQUALIFIED;
4219 else if (type_quals)
4220 type = c_build_qualified_type (type, type_quals);
4222 type_as_written = type;
4224 decl = build_decl (PARM_DECL, declarator, type);
4225 if (size_varies)
4226 C_DECL_VARIABLE_SIZE (decl) = 1;
4228 /* Compute the type actually passed in the parmlist,
4229 for the case where there is no prototype.
4230 (For example, shorts and chars are passed as ints.)
4231 When there is a prototype, this is overridden later. */
4233 if (type == error_mark_node)
4234 promoted_type = type;
4235 else
4236 promoted_type = c_type_promotes_to (type);
4238 DECL_ARG_TYPE (decl) = promoted_type;
4239 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4241 else if (decl_context == FIELD)
4243 /* Structure field. It may not be a function. */
4245 if (TREE_CODE (type) == FUNCTION_TYPE)
4247 error ("field `%s' declared as a function", name);
4248 type = build_pointer_type (type);
4250 else if (TREE_CODE (type) != ERROR_MARK
4251 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4253 error ("field `%s' has incomplete type", name);
4254 type = error_mark_node;
4256 /* Move type qualifiers down to element of an array. */
4257 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4258 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4259 type_quals),
4260 TYPE_DOMAIN (type));
4261 decl = build_decl (FIELD_DECL, declarator, type);
4262 DECL_NONADDRESSABLE_P (decl) = bitfield;
4264 if (size_varies)
4265 C_DECL_VARIABLE_SIZE (decl) = 1;
4267 else if (TREE_CODE (type) == FUNCTION_TYPE)
4269 /* Every function declaration is "external"
4270 except for those which are inside a function body
4271 in which `auto' is used.
4272 That is a case not specified by ANSI C,
4273 and we use it for forward declarations for nested functions. */
4274 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4275 || current_scope == global_scope);
4277 if (specbits & (1 << (int) RID_AUTO)
4278 && (pedantic || current_scope == global_scope))
4279 pedwarn ("invalid storage class for function `%s'", name);
4280 if (specbits & (1 << (int) RID_REGISTER))
4281 error ("invalid storage class for function `%s'", name);
4282 if (specbits & (1 << (int) RID_THREAD))
4283 error ("invalid storage class for function `%s'", name);
4284 /* Function declaration not at file scope.
4285 Storage classes other than `extern' are not allowed
4286 and `extern' makes no difference. */
4287 if (current_scope != global_scope
4288 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4289 && pedantic)
4290 pedwarn ("invalid storage class for function `%s'", name);
4292 decl = build_decl (FUNCTION_DECL, declarator, type);
4293 decl = build_decl_attribute_variant (decl, decl_attr);
4295 DECL_LANG_SPECIFIC (decl)
4296 = ggc_alloc_cleared (sizeof (struct lang_decl));
4298 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4299 pedwarn ("ISO C forbids qualified function types");
4301 /* GNU C interprets a `volatile void' return type to indicate
4302 that the function does not return. */
4303 if ((type_quals & TYPE_QUAL_VOLATILE)
4304 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4305 warning ("`noreturn' function returns non-void value");
4307 if (extern_ref)
4308 DECL_EXTERNAL (decl) = 1;
4309 /* Record absence of global scope for `static' or `auto'. */
4310 TREE_PUBLIC (decl)
4311 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4313 if (defaulted_int)
4314 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4316 /* Record presence of `inline', if it is reasonable. */
4317 if (MAIN_NAME_P (declarator))
4319 if (inlinep)
4320 warning ("cannot inline function `main'");
4322 else if (inlinep)
4324 /* Record that the function is declared `inline'. */
4325 DECL_DECLARED_INLINE_P (decl) = 1;
4327 /* Do not mark bare declarations as DECL_INLINE. Doing so
4328 in the presence of multiple declarations can result in
4329 the abstract origin pointing between the declarations,
4330 which will confuse dwarf2out. */
4331 if (initialized)
4333 DECL_INLINE (decl) = 1;
4334 if (specbits & (1 << (int) RID_EXTERN))
4335 current_extern_inline = 1;
4338 /* If -finline-functions, assume it can be inlined. This does
4339 two things: let the function be deferred until it is actually
4340 needed, and let dwarf2 know that the function is inlinable. */
4341 else if (flag_inline_trees == 2 && initialized)
4342 DECL_INLINE (decl) = 1;
4344 else
4346 /* It's a variable. */
4347 /* An uninitialized decl with `extern' is a reference. */
4348 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4350 /* Move type qualifiers down to element of an array. */
4351 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4353 int saved_align = TYPE_ALIGN(type);
4354 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4355 type_quals),
4356 TYPE_DOMAIN (type));
4357 TYPE_ALIGN (type) = saved_align;
4359 else if (type_quals)
4360 type = c_build_qualified_type (type, type_quals);
4362 /* It is invalid to create an `extern' declaration for a
4363 variable if there is a global declaration that is
4364 `static'. */
4365 if (extern_ref && current_scope != global_scope)
4367 tree global_decl;
4369 global_decl = identifier_global_value (declarator);
4370 if (global_decl
4371 && TREE_CODE (global_decl) == VAR_DECL
4372 && !TREE_PUBLIC (global_decl))
4373 error ("variable previously declared `static' redeclared "
4374 "`extern'");
4377 decl = build_decl (VAR_DECL, declarator, type);
4378 if (size_varies)
4379 C_DECL_VARIABLE_SIZE (decl) = 1;
4381 if (inlinep)
4382 pedwarn ("%Hvariable '%D' declared `inline'",
4383 &DECL_SOURCE_LOCATION (decl), decl);
4385 DECL_EXTERNAL (decl) = extern_ref;
4387 /* At file scope, the presence of a `static' or `register' storage
4388 class specifier, or the absence of all storage class specifiers
4389 makes this declaration a definition (perhaps tentative). Also,
4390 the absence of both `static' and `register' makes it public. */
4391 if (current_scope == global_scope)
4393 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4394 | (1 << (int) RID_REGISTER)));
4395 TREE_STATIC (decl) = !extern_ref;
4397 /* Not at file scope, only `static' makes a static definition. */
4398 else
4400 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4401 TREE_PUBLIC (decl) = extern_ref;
4404 if (specbits & 1 << (int) RID_THREAD)
4406 if (targetm.have_tls)
4407 DECL_THREAD_LOCAL (decl) = 1;
4408 else
4409 /* A mere warning is sure to result in improper semantics
4410 at runtime. Don't bother to allow this to compile. */
4411 error ("thread-local storage not supported for this target");
4415 /* Record `register' declaration for warnings on &
4416 and in case doing stupid register allocation. */
4418 if (specbits & (1 << (int) RID_REGISTER))
4419 DECL_REGISTER (decl) = 1;
4421 /* Record constancy and volatility. */
4422 c_apply_type_quals_to_decl (type_quals, decl);
4424 /* If a type has volatile components, it should be stored in memory.
4425 Otherwise, the fact that those components are volatile
4426 will be ignored, and would even crash the compiler. */
4427 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4428 c_mark_addressable (decl);
4430 #ifdef ENABLE_CHECKING
4431 /* This is the earliest point at which we might know the assembler
4432 name of a variable. Thus, if it's known before this, die horribly. */
4433 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4434 abort ();
4435 #endif
4437 decl_attributes (&decl, returned_attrs, 0);
4439 return decl;
4443 /* Decode the parameter-list info for a function type or function definition.
4444 The argument is the value returned by `get_parm_info' (or made in parse.y
4445 if there is an identifier list instead of a parameter decl list).
4446 These two functions are separate because when a function returns
4447 or receives functions then each is called multiple times but the order
4448 of calls is different. The last call to `grokparms' is always the one
4449 that contains the formal parameter names of a function definition.
4451 Store in `last_function_parms' a chain of the decls of parms.
4452 Also store in `last_function_parm_tags' a chain of the struct, union,
4453 and enum tags declared among the parms.
4455 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4457 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4458 a mere declaration. A nonempty identifier-list gets an error message
4459 when FUNCDEF_FLAG is zero. */
4461 static tree
4462 grokparms (tree parms_info, int funcdef_flag)
4464 tree first_parm = TREE_CHAIN (parms_info);
4466 last_function_parms = TREE_PURPOSE (parms_info);
4467 last_function_parm_tags = TREE_VALUE (parms_info);
4468 last_function_parm_others = TREE_TYPE (parms_info);
4470 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4471 && !in_system_header)
4472 warning ("function declaration isn't a prototype");
4474 if (first_parm != 0
4475 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4477 if (! funcdef_flag)
4478 pedwarn ("parameter names (without types) in function declaration");
4480 last_function_parms = first_parm;
4481 return 0;
4483 else
4485 tree parm;
4486 tree typelt;
4487 /* If the arg types are incomplete in a declaration,
4488 they must include undefined tags.
4489 These tags can never be defined in the scope of the declaration,
4490 so the types can never be completed,
4491 and no call can be compiled successfully. */
4493 for (parm = last_function_parms, typelt = first_parm;
4494 parm;
4495 parm = TREE_CHAIN (parm))
4496 /* Skip over any enumeration constants declared here. */
4497 if (TREE_CODE (parm) == PARM_DECL)
4499 /* Barf if the parameter itself has an incomplete type. */
4500 tree type = TREE_VALUE (typelt);
4501 if (type == error_mark_node)
4502 continue;
4503 if (!COMPLETE_TYPE_P (type))
4505 if (funcdef_flag && DECL_NAME (parm) != 0)
4506 error ("parameter `%s' has incomplete type",
4507 IDENTIFIER_POINTER (DECL_NAME (parm)));
4508 else
4509 warning ("parameter has incomplete type");
4510 if (funcdef_flag)
4512 TREE_VALUE (typelt) = error_mark_node;
4513 TREE_TYPE (parm) = error_mark_node;
4516 typelt = TREE_CHAIN (typelt);
4519 return first_parm;
4523 /* Return a tree_list node with info on a parameter list just parsed.
4524 The TREE_PURPOSE is a list of decls of those parms.
4525 The TREE_VALUE is a list of structure, union and enum tags defined.
4526 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4527 The TREE_TYPE is a list of non-parameter decls which appeared with the
4528 parameters.
4529 This tree_list node is later fed to `grokparms'.
4531 VOID_AT_END nonzero means append `void' to the end of the type-list.
4532 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4534 tree
4535 get_parm_info (int void_at_end)
4537 tree decl, type, list;
4538 tree types = 0;
4539 tree *last_type = &types;
4540 tree tags = current_scope->tags;
4541 tree parms = current_scope->parms;
4542 tree others = current_scope->names;
4543 static bool explained_incomplete_types = false;
4544 bool gave_void_only_once_err = false;
4546 /* Just "void" (and no ellipsis) is special. There are really no parms.
4547 But if the "void" is qualified (by "const" or "volatile"), or has a
4548 storage class specifier ("register"), then the behavior is undefined;
4549 issue an error. Typedefs for "void" are OK (see DR#157). */
4550 if (void_at_end && parms != 0
4551 && TREE_CHAIN (parms) == 0
4552 && VOID_TYPE_P (TREE_TYPE (parms))
4553 && !DECL_NAME (parms))
4555 if (TREE_THIS_VOLATILE (parms)
4556 || TREE_READONLY (parms)
4557 || DECL_REGISTER (parms))
4558 error ("\"void\" as only parameter may not be qualified");
4560 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4563 /* Sanity check all of the parameter declarations. */
4564 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4566 if (TREE_CODE (decl) != PARM_DECL)
4567 abort ();
4568 if (TREE_ASM_WRITTEN (decl))
4569 abort ();
4571 /* Since there is a prototype, args are passed in their
4572 declared types. The back end may override this. */
4573 type = TREE_TYPE (decl);
4574 DECL_ARG_TYPE (decl) = type;
4575 if (PROMOTE_PROTOTYPES
4576 && INTEGRAL_TYPE_P (type)
4577 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4578 DECL_ARG_TYPE (decl) = integer_type_node;
4580 /* Check for (..., void, ...) and issue an error. */
4581 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4583 error ("\"void\" must be the only parameter");
4584 gave_void_only_once_err = true;
4587 type = build_tree_list (0, type);
4588 *last_type = type;
4589 last_type = &TREE_CHAIN (type);
4592 /* Check the list of non-parameter decls for any forward parm decls
4593 that never got real decls. */
4594 for (decl = others; decl; decl = TREE_CHAIN (decl))
4595 if (TREE_CODE (decl) == PARM_DECL)
4597 if (!TREE_ASM_WRITTEN (decl))
4598 abort ();
4600 error ("%Hparameter \"%D\" has just a forward declaration",
4601 &DECL_SOURCE_LOCATION (decl), decl);
4604 /* Warn about any struct, union or enum tags defined within this
4605 list. The scope of such types is limited to this declaration,
4606 which is rarely if ever desirable (it's impossible to call such
4607 a function with type-correct arguments). */
4608 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4610 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4611 const char *keyword;
4612 /* An anonymous union parm type is meaningful as a GNU extension.
4613 So don't warn for that. */
4614 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4615 continue;
4617 /* The keyword should not be translated. */
4618 switch (code)
4620 case RECORD_TYPE: keyword = "struct"; break;
4621 case UNION_TYPE: keyword = "union"; break;
4622 case ENUMERAL_TYPE: keyword = "enum"; break;
4623 default: abort ();
4626 if (TREE_PURPOSE (decl))
4627 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4628 warning ("\"%s %s\" declared inside parameter list",
4629 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4630 else
4631 /* The %s will be one of 'struct', 'union', or 'enum'. */
4632 warning ("anonymous %s declared inside parameter list", keyword);
4634 if (! explained_incomplete_types)
4636 warning ("its scope is only this definition or declaration,"
4637 " which is probably not what you want");
4638 explained_incomplete_types = true;
4643 if (void_at_end)
4645 type = build_tree_list (0, void_type_node);
4646 *last_type = type;
4649 list = tree_cons (parms, tags, types);
4650 TREE_TYPE (list) = others;
4651 return list;
4654 /* Get the struct, enum or union (CODE says which) with tag NAME.
4655 Define the tag as a forward-reference if it is not defined. */
4657 tree
4658 xref_tag (enum tree_code code, tree name)
4660 /* If a cross reference is requested, look up the type
4661 already defined for this tag and return it. */
4663 tree ref = lookup_tag (code, name, 0);
4664 /* If this is the right type of tag, return what we found.
4665 (This reference will be shadowed by shadow_tag later if appropriate.)
4666 If this is the wrong type of tag, do not return it. If it was the
4667 wrong type in the same scope, we will have had an error
4668 message already; if in a different scope and declaring
4669 a name, pending_xref_error will give an error message; but if in a
4670 different scope and not declaring a name, this tag should
4671 shadow the previous declaration of a different type of tag, and
4672 this would not work properly if we return the reference found.
4673 (For example, with "struct foo" in an outer scope, "union foo;"
4674 must shadow that tag with a new one of union type.) */
4675 if (ref && TREE_CODE (ref) == code)
4676 return ref;
4678 /* If no such tag is yet defined, create a forward-reference node
4679 and record it as the "definition".
4680 When a real declaration of this type is found,
4681 the forward-reference will be altered into a real type. */
4683 ref = make_node (code);
4684 if (code == ENUMERAL_TYPE)
4686 /* Give the type a default layout like unsigned int
4687 to avoid crashing if it does not get defined. */
4688 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4689 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4690 TYPE_USER_ALIGN (ref) = 0;
4691 TREE_UNSIGNED (ref) = 1;
4692 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4693 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4694 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4697 pushtag (name, ref);
4699 return ref;
4702 /* Make sure that the tag NAME is defined *in the current scope*
4703 at least as a forward reference.
4704 CODE says which kind of tag NAME ought to be. */
4706 tree
4707 start_struct (enum tree_code code, tree name)
4709 /* If there is already a tag defined at this scope
4710 (as a forward reference), just return it. */
4712 tree ref = 0;
4714 if (name != 0)
4715 ref = lookup_tag (code, name, 1);
4716 if (ref && TREE_CODE (ref) == code)
4718 if (TYPE_FIELDS (ref))
4720 if (code == UNION_TYPE)
4721 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4722 else
4723 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4726 else
4728 /* Otherwise create a forward-reference just so the tag is in scope. */
4730 ref = make_node (code);
4731 pushtag (name, ref);
4734 C_TYPE_BEING_DEFINED (ref) = 1;
4735 TYPE_PACKED (ref) = flag_pack_struct;
4736 return ref;
4739 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4740 of a structure component, returning a FIELD_DECL node.
4741 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4743 This is done during the parsing of the struct declaration.
4744 The FIELD_DECL nodes are chained together and the lot of them
4745 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4747 tree
4748 grokfield (tree declarator, tree declspecs, tree width)
4750 tree value;
4752 if (declarator == NULL_TREE && width == NULL_TREE)
4754 /* This is an unnamed decl.
4756 If we have something of the form "union { list } ;" then this
4757 is the anonymous union extension. Similarly for struct.
4759 If this is something of the form "struct foo;", then
4760 If MS extensions are enabled, this is handled as an
4761 anonymous struct.
4762 Otherwise this is a forward declaration of a structure tag.
4764 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4765 If MS extensions are enabled and foo names a structure, then
4766 again this is an anonymous struct.
4767 Otherwise this is an error.
4769 Oh what a horrid tangled web we weave. I wonder if MS consciously
4770 took this from Plan 9 or if it was an accident of implementation
4771 that took root before someone noticed the bug... */
4773 tree type = TREE_VALUE (declspecs);
4775 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4776 type = TREE_TYPE (type);
4777 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4779 if (flag_ms_extensions)
4780 ; /* ok */
4781 else if (flag_iso)
4782 goto warn_unnamed_field;
4783 else if (TYPE_NAME (type) == NULL)
4784 ; /* ok */
4785 else
4786 goto warn_unnamed_field;
4788 else
4790 warn_unnamed_field:
4791 warning ("declaration does not declare anything");
4792 return NULL_TREE;
4796 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4798 finish_decl (value, NULL_TREE, NULL_TREE);
4799 DECL_INITIAL (value) = width;
4801 if (c_dialect_objc ())
4802 objc_check_decl (value);
4803 return value;
4806 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4807 the list such that this does not present a problem later. */
4809 static void
4810 detect_field_duplicates (tree fieldlist)
4812 tree x, y;
4813 int timeout = 10;
4815 /* First, see if there are more than "a few" fields.
4816 This is trivially true if there are zero or one fields. */
4817 if (!fieldlist)
4818 return;
4819 x = TREE_CHAIN (fieldlist);
4820 if (!x)
4821 return;
4822 do {
4823 timeout--;
4824 x = TREE_CHAIN (x);
4825 } while (timeout > 0 && x);
4827 /* If there were "few" fields, avoid the overhead of allocating
4828 a hash table. Instead just do the nested traversal thing. */
4829 if (timeout > 0)
4831 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4832 if (DECL_NAME (x))
4834 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4835 if (DECL_NAME (y) == DECL_NAME (x))
4837 error ("%Hduplicate member '%D'",
4838 &DECL_SOURCE_LOCATION (x), x);
4839 DECL_NAME (x) = NULL_TREE;
4843 else
4845 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4846 void **slot;
4848 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4849 if ((y = DECL_NAME (x)) != 0)
4851 slot = htab_find_slot (htab, y, INSERT);
4852 if (*slot)
4854 error ("%Hduplicate member '%D'",
4855 &DECL_SOURCE_LOCATION (x), x);
4856 DECL_NAME (x) = NULL_TREE;
4858 *slot = y;
4861 htab_delete (htab);
4865 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4866 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4867 ATTRIBUTES are attributes to be applied to the structure. */
4869 tree
4870 finish_struct (tree t, tree fieldlist, tree attributes)
4872 tree x;
4873 int toplevel = global_scope == current_scope;
4874 int saw_named_field;
4876 /* If this type was previously laid out as a forward reference,
4877 make sure we lay it out again. */
4879 TYPE_SIZE (t) = 0;
4881 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4883 /* Nameless union parm types are useful as GCC extension. */
4884 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4885 /* Otherwise, warn about any struct or union def. in parmlist. */
4886 if (in_parm_level_p ())
4888 if (pedantic)
4889 pedwarn ("%s defined inside parms",
4890 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4891 else
4892 warning ("%s defined inside parms",
4893 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4896 if (pedantic)
4898 for (x = fieldlist; x; x = TREE_CHAIN (x))
4899 if (DECL_NAME (x) != 0)
4900 break;
4902 if (x == 0)
4903 pedwarn ("%s has no %s",
4904 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4905 fieldlist ? _("named members") : _("members"));
4908 /* Install struct as DECL_CONTEXT of each field decl.
4909 Also process specified field sizes,m which is found in the DECL_INITIAL.
4910 Store 0 there, except for ": 0" fields (so we can find them
4911 and delete them, below). */
4913 saw_named_field = 0;
4914 for (x = fieldlist; x; x = TREE_CHAIN (x))
4916 DECL_CONTEXT (x) = t;
4917 DECL_PACKED (x) |= TYPE_PACKED (t);
4919 /* If any field is const, the structure type is pseudo-const. */
4920 if (TREE_READONLY (x))
4921 C_TYPE_FIELDS_READONLY (t) = 1;
4922 else
4924 /* A field that is pseudo-const makes the structure likewise. */
4925 tree t1 = TREE_TYPE (x);
4926 while (TREE_CODE (t1) == ARRAY_TYPE)
4927 t1 = TREE_TYPE (t1);
4928 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4929 && C_TYPE_FIELDS_READONLY (t1))
4930 C_TYPE_FIELDS_READONLY (t) = 1;
4933 /* Any field that is volatile means variables of this type must be
4934 treated in some ways as volatile. */
4935 if (TREE_THIS_VOLATILE (x))
4936 C_TYPE_FIELDS_VOLATILE (t) = 1;
4938 /* Any field of nominal variable size implies structure is too. */
4939 if (C_DECL_VARIABLE_SIZE (x))
4940 C_TYPE_VARIABLE_SIZE (t) = 1;
4942 /* Detect invalid nested redefinition. */
4943 if (TREE_TYPE (x) == t)
4944 error ("nested redefinition of `%s'",
4945 IDENTIFIER_POINTER (TYPE_NAME (t)));
4947 /* Detect invalid bit-field size. */
4948 if (DECL_INITIAL (x))
4949 STRIP_NOPS (DECL_INITIAL (x));
4950 if (DECL_INITIAL (x))
4952 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4953 constant_expression_warning (DECL_INITIAL (x));
4954 else
4956 error ("%Hbit-field '%D' width not an integer constant",
4957 &DECL_SOURCE_LOCATION (x), x);
4958 DECL_INITIAL (x) = NULL;
4962 /* Detect invalid bit-field type. */
4963 if (DECL_INITIAL (x)
4964 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4965 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4966 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4968 error ("%Hbit-field '%D' has invalid type",
4969 &DECL_SOURCE_LOCATION (x), x);
4970 DECL_INITIAL (x) = NULL;
4973 if (DECL_INITIAL (x) && pedantic
4974 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4975 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4976 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4977 /* Accept an enum that's equivalent to int or unsigned int. */
4978 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4979 && (TYPE_PRECISION (TREE_TYPE (x))
4980 == TYPE_PRECISION (integer_type_node))))
4981 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
4982 &DECL_SOURCE_LOCATION (x), x);
4984 /* Detect and ignore out of range field width and process valid
4985 field widths. */
4986 if (DECL_INITIAL (x))
4988 int max_width
4989 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
4990 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
4992 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
4993 error ("%Hnegative width in bit-field '%D'",
4994 &DECL_SOURCE_LOCATION (x), x);
4995 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
4996 pedwarn ("%Hwidth of '%D' exceeds its type",
4997 &DECL_SOURCE_LOCATION (x), x);
4998 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
4999 error ("%Hzero width for bit-field '%D'",
5000 &DECL_SOURCE_LOCATION (x), x);
5001 else
5003 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5004 unsigned HOST_WIDE_INT width
5005 = tree_low_cst (DECL_INITIAL (x), 1);
5007 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5008 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5009 TREE_UNSIGNED (TREE_TYPE (x)))
5010 || (width
5011 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5012 TREE_UNSIGNED (TREE_TYPE (x))))))
5013 warning ("%H'%D' is narrower than values of its type",
5014 &DECL_SOURCE_LOCATION (x), x);
5016 DECL_SIZE (x) = bitsize_int (width);
5017 DECL_BIT_FIELD (x) = 1;
5018 SET_DECL_C_BIT_FIELD (x);
5022 DECL_INITIAL (x) = 0;
5024 /* Detect flexible array member in an invalid context. */
5025 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5026 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5027 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5028 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5030 if (TREE_CODE (t) == UNION_TYPE)
5031 error ("%Hflexible array member in union",
5032 &DECL_SOURCE_LOCATION (x));
5033 else if (TREE_CHAIN (x) != NULL_TREE)
5034 error ("%Hflexible array member not at end of struct",
5035 &DECL_SOURCE_LOCATION (x));
5036 else if (! saw_named_field)
5037 error ("%Hflexible array member in otherwise empty struct",
5038 &DECL_SOURCE_LOCATION (x));
5041 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5042 && flexible_array_type_p (TREE_TYPE (x)))
5043 pedwarn ("%Hinvalid use of structure with flexible array member",
5044 &DECL_SOURCE_LOCATION (x));
5046 if (DECL_NAME (x))
5047 saw_named_field = 1;
5050 detect_field_duplicates (fieldlist);
5052 /* Now we have the nearly final fieldlist. Record it,
5053 then lay out the structure or union (including the fields). */
5055 TYPE_FIELDS (t) = fieldlist;
5057 layout_type (t);
5059 /* Delete all zero-width bit-fields from the fieldlist */
5061 tree *fieldlistp = &fieldlist;
5062 while (*fieldlistp)
5063 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5064 *fieldlistp = TREE_CHAIN (*fieldlistp);
5065 else
5066 fieldlistp = &TREE_CHAIN (*fieldlistp);
5069 /* Now we have the truly final field list.
5070 Store it in this type and in the variants. */
5072 TYPE_FIELDS (t) = fieldlist;
5074 /* If there are lots of fields, sort so we can look through them fast.
5075 We arbitrarily consider 16 or more elts to be "a lot". */
5078 int len = 0;
5080 for (x = fieldlist; x; x = TREE_CHAIN (x))
5082 if (len > 15 || DECL_NAME (x) == NULL)
5083 break;
5084 len += 1;
5087 if (len > 15)
5089 tree *field_array;
5090 struct lang_type *space;
5091 struct sorted_fields_type *space2;
5093 len += list_length (x);
5095 /* Use the same allocation policy here that make_node uses, to
5096 ensure that this lives as long as the rest of the struct decl.
5097 All decls in an inline function need to be saved. */
5099 space = ggc_alloc (sizeof (struct lang_type));
5100 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5102 len = 0;
5103 space->s = space2;
5104 field_array = &space2->elts[0];
5105 for (x = fieldlist; x; x = TREE_CHAIN (x))
5107 field_array[len++] = x;
5109 /* If there is anonymous struct or union, break out of the loop. */
5110 if (DECL_NAME (x) == NULL)
5111 break;
5113 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5114 if (x == NULL)
5116 TYPE_LANG_SPECIFIC (t) = space;
5117 TYPE_LANG_SPECIFIC (t)->s->len = len;
5118 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5119 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5124 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5126 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5127 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5128 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5129 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5132 /* If this was supposed to be a transparent union, but we can't
5133 make it one, warn and turn off the flag. */
5134 if (TREE_CODE (t) == UNION_TYPE
5135 && TYPE_TRANSPARENT_UNION (t)
5136 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5138 TYPE_TRANSPARENT_UNION (t) = 0;
5139 warning ("union cannot be made transparent");
5142 /* If this structure or union completes the type of any previous
5143 variable declaration, lay it out and output its rtl. */
5145 if (current_scope->incomplete != NULL_TREE)
5147 tree prev = NULL_TREE;
5149 for (x = current_scope->incomplete; x; x = TREE_CHAIN (x))
5151 tree decl = TREE_VALUE (x);
5153 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5154 && TREE_CODE (decl) != TYPE_DECL)
5156 layout_decl (decl, 0);
5157 /* This is a no-op in c-lang.c or something real in objc-act.c. */
5158 if (c_dialect_objc ())
5159 objc_check_decl (decl);
5160 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5161 if (! toplevel)
5162 expand_decl (decl);
5163 /* Unlink X from the incomplete list. */
5164 if (prev)
5165 TREE_CHAIN (prev) = TREE_CHAIN (x);
5166 else
5167 current_scope->incomplete = TREE_CHAIN (x);
5169 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5170 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5172 tree element = TREE_TYPE (decl);
5173 while (TREE_CODE (element) == ARRAY_TYPE)
5174 element = TREE_TYPE (element);
5175 if (element == t)
5177 layout_array_type (TREE_TYPE (decl));
5178 if (TREE_CODE (decl) != TYPE_DECL)
5180 layout_decl (decl, 0);
5181 if (c_dialect_objc ())
5182 objc_check_decl (decl);
5183 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5184 if (! toplevel)
5185 expand_decl (decl);
5187 /* Unlink X from the incomplete list. */
5188 if (prev)
5189 TREE_CHAIN (prev) = TREE_CHAIN (x);
5190 else
5191 current_scope->incomplete = TREE_CHAIN (x);
5197 /* Finish debugging output for this type. */
5198 rest_of_type_compilation (t, toplevel);
5200 return t;
5203 /* Lay out the type T, and its element type, and so on. */
5205 static void
5206 layout_array_type (tree t)
5208 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5209 layout_array_type (TREE_TYPE (t));
5210 layout_type (t);
5213 /* Begin compiling the definition of an enumeration type.
5214 NAME is its name (or null if anonymous).
5215 Returns the type object, as yet incomplete.
5216 Also records info about it so that build_enumerator
5217 may be used to declare the individual values as they are read. */
5219 tree
5220 start_enum (tree name)
5222 tree enumtype = 0;
5224 /* If this is the real definition for a previous forward reference,
5225 fill in the contents in the same object that used to be the
5226 forward reference. */
5228 if (name != 0)
5229 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5231 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5233 enumtype = make_node (ENUMERAL_TYPE);
5234 pushtag (name, enumtype);
5237 C_TYPE_BEING_DEFINED (enumtype) = 1;
5239 if (TYPE_VALUES (enumtype) != 0)
5241 /* This enum is a named one that has been declared already. */
5242 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5244 /* Completely replace its old definition.
5245 The old enumerators remain defined, however. */
5246 TYPE_VALUES (enumtype) = 0;
5249 enum_next_value = integer_zero_node;
5250 enum_overflow = 0;
5252 if (flag_short_enums)
5253 TYPE_PACKED (enumtype) = 1;
5255 return enumtype;
5258 /* After processing and defining all the values of an enumeration type,
5259 install their decls in the enumeration type and finish it off.
5260 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5261 and ATTRIBUTES are the specified attributes.
5262 Returns ENUMTYPE. */
5264 tree
5265 finish_enum (tree enumtype, tree values, tree attributes)
5267 tree pair, tem;
5268 tree minnode = 0, maxnode = 0, enum_value_type;
5269 int precision, unsign;
5270 int toplevel = (global_scope == current_scope);
5272 if (in_parm_level_p ())
5273 warning ("enum defined inside parms");
5275 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5277 /* Calculate the maximum value of any enumerator in this type. */
5279 if (values == error_mark_node)
5280 minnode = maxnode = integer_zero_node;
5281 else
5283 minnode = maxnode = TREE_VALUE (values);
5284 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5286 tree value = TREE_VALUE (pair);
5287 if (tree_int_cst_lt (maxnode, value))
5288 maxnode = value;
5289 if (tree_int_cst_lt (value, minnode))
5290 minnode = value;
5294 /* Construct the final type of this enumeration. It is the same
5295 as one of the integral types - the narrowest one that fits, except
5296 that normally we only go as narrow as int - and signed iff any of
5297 the values are negative. */
5298 unsign = (tree_int_cst_sgn (minnode) >= 0);
5299 precision = MAX (min_precision (minnode, unsign),
5300 min_precision (maxnode, unsign));
5301 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5303 tree narrowest = c_common_type_for_size (precision, unsign);
5304 if (narrowest == 0)
5306 warning ("enumeration values exceed range of largest integer");
5307 narrowest = long_long_integer_type_node;
5310 precision = TYPE_PRECISION (narrowest);
5312 else
5313 precision = TYPE_PRECISION (integer_type_node);
5315 if (precision == TYPE_PRECISION (integer_type_node))
5316 enum_value_type = c_common_type_for_size (precision, 0);
5317 else
5318 enum_value_type = enumtype;
5320 TYPE_MIN_VALUE (enumtype) = minnode;
5321 TYPE_MAX_VALUE (enumtype) = maxnode;
5322 TYPE_PRECISION (enumtype) = precision;
5323 TREE_UNSIGNED (enumtype) = unsign;
5324 TYPE_SIZE (enumtype) = 0;
5325 layout_type (enumtype);
5327 if (values != error_mark_node)
5329 /* Change the type of the enumerators to be the enum type. We
5330 need to do this irrespective of the size of the enum, for
5331 proper type checking. Replace the DECL_INITIALs of the
5332 enumerators, and the value slots of the list, with copies
5333 that have the enum type; they cannot be modified in place
5334 because they may be shared (e.g. integer_zero_node) Finally,
5335 change the purpose slots to point to the names of the decls. */
5336 for (pair = values; pair; pair = TREE_CHAIN (pair))
5338 tree enu = TREE_PURPOSE (pair);
5340 TREE_TYPE (enu) = enumtype;
5342 /* The ISO C Standard mandates enumerators to have type int,
5343 even though the underlying type of an enum type is
5344 unspecified. Here we convert any enumerators that fit in
5345 an int to type int, to avoid promotions to unsigned types
5346 when comparing integers with enumerators that fit in the
5347 int range. When -pedantic is given, build_enumerator()
5348 would have already taken care of those that don't fit. */
5349 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5350 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5351 else
5352 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5354 TREE_PURPOSE (pair) = DECL_NAME (enu);
5355 TREE_VALUE (pair) = DECL_INITIAL (enu);
5358 TYPE_VALUES (enumtype) = values;
5361 /* Fix up all variant types of this enum type. */
5362 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5364 if (tem == enumtype)
5365 continue;
5366 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5367 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5368 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5369 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5370 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5371 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5372 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5373 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5374 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5375 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5378 /* Finish debugging output for this type. */
5379 rest_of_type_compilation (enumtype, toplevel);
5381 return enumtype;
5384 /* Build and install a CONST_DECL for one value of the
5385 current enumeration type (one that was begun with start_enum).
5386 Return a tree-list containing the CONST_DECL and its value.
5387 Assignment of sequential values by default is handled here. */
5389 tree
5390 build_enumerator (tree name, tree value)
5392 tree decl, type;
5394 /* Validate and default VALUE. */
5396 /* Remove no-op casts from the value. */
5397 if (value)
5398 STRIP_TYPE_NOPS (value);
5400 if (value != 0)
5402 if (TREE_CODE (value) == INTEGER_CST)
5404 value = default_conversion (value);
5405 constant_expression_warning (value);
5407 else
5409 error ("enumerator value for `%s' not integer constant",
5410 IDENTIFIER_POINTER (name));
5411 value = 0;
5415 /* Default based on previous value. */
5416 /* It should no longer be possible to have NON_LVALUE_EXPR
5417 in the default. */
5418 if (value == 0)
5420 value = enum_next_value;
5421 if (enum_overflow)
5422 error ("overflow in enumeration values");
5425 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5427 pedwarn ("ISO C restricts enumerator values to range of `int'");
5428 value = convert (integer_type_node, value);
5431 /* Set basis for default for next value. */
5432 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5433 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5435 /* Now create a declaration for the enum value name. */
5437 type = TREE_TYPE (value);
5438 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5439 TYPE_PRECISION (integer_type_node)),
5440 (TYPE_PRECISION (type)
5441 >= TYPE_PRECISION (integer_type_node)
5442 && TREE_UNSIGNED (type)));
5444 decl = build_decl (CONST_DECL, name, type);
5445 DECL_INITIAL (decl) = convert (type, value);
5446 pushdecl (decl);
5448 return tree_cons (decl, value, NULL_TREE);
5452 /* Create the FUNCTION_DECL for a function definition.
5453 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5454 the declaration; they describe the function's name and the type it returns,
5455 but twisted together in a fashion that parallels the syntax of C.
5457 This function creates a binding context for the function body
5458 as well as setting up the FUNCTION_DECL in current_function_decl.
5460 Returns 1 on success. If the DECLARATOR is not suitable for a function
5461 (it defines a datum instead), we return 0, which tells
5462 yyparse to report a parse error. */
5465 start_function (tree declspecs, tree declarator, tree attributes)
5467 tree decl1, old_decl;
5468 tree restype;
5469 int old_immediate_size_expand = immediate_size_expand;
5471 current_function_returns_value = 0; /* Assume, until we see it does. */
5472 current_function_returns_null = 0;
5473 current_function_returns_abnormally = 0;
5474 warn_about_return_type = 0;
5475 current_extern_inline = 0;
5477 /* Don't expand any sizes in the return type of the function. */
5478 immediate_size_expand = 0;
5480 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5482 /* If the declarator is not suitable for a function definition,
5483 cause a syntax error. */
5484 if (decl1 == 0)
5486 immediate_size_expand = old_immediate_size_expand;
5487 return 0;
5490 decl_attributes (&decl1, attributes, 0);
5492 if (DECL_DECLARED_INLINE_P (decl1)
5493 && DECL_UNINLINABLE (decl1)
5494 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5495 warning ("%Hinline function '%D' given attribute noinline",
5496 &DECL_SOURCE_LOCATION (decl1), decl1);
5498 announce_function (decl1);
5500 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5502 error ("return type is an incomplete type");
5503 /* Make it return void instead. */
5504 TREE_TYPE (decl1)
5505 = build_function_type (void_type_node,
5506 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5509 if (warn_about_return_type)
5510 pedwarn_c99 ("return type defaults to `int'");
5512 /* Save the parm names or decls from this function's declarator
5513 where store_parm_decls will find them. */
5514 current_function_parms = last_function_parms;
5515 current_function_parm_tags = last_function_parm_tags;
5516 current_function_parm_others = last_function_parm_others;
5518 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5519 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5520 DECL_INITIAL (decl1) = error_mark_node;
5522 /* If this definition isn't a prototype and we had a prototype declaration
5523 before, copy the arg type info from that prototype.
5524 But not if what we had before was a builtin function. */
5525 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5526 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5527 && !DECL_BUILT_IN (old_decl)
5528 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5529 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5530 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5532 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5533 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5536 /* Optionally warn of old-fashioned def with no previous prototype. */
5537 if (warn_strict_prototypes
5538 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5539 && C_DECL_ISNT_PROTOTYPE (old_decl))
5540 warning ("function declaration isn't a prototype");
5541 /* Optionally warn of any global def with no previous prototype. */
5542 else if (warn_missing_prototypes
5543 && TREE_PUBLIC (decl1)
5544 && ! MAIN_NAME_P (DECL_NAME (decl1))
5545 && C_DECL_ISNT_PROTOTYPE (old_decl))
5546 warning ("%Hno previous prototype for '%D'",
5547 &DECL_SOURCE_LOCATION (decl1), decl1);
5548 /* Optionally warn of any def with no previous prototype
5549 if the function has already been used. */
5550 else if (warn_missing_prototypes
5551 && old_decl != 0 && TREE_USED (old_decl)
5552 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5553 warning ("%H'%D' was used with no prototype before its definition",
5554 &DECL_SOURCE_LOCATION (decl1), decl1);
5555 /* Optionally warn of any global def with no previous declaration. */
5556 else if (warn_missing_declarations
5557 && TREE_PUBLIC (decl1)
5558 && old_decl == 0
5559 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5560 warning ("%Hno previous declaration for '%D'",
5561 &DECL_SOURCE_LOCATION (decl1), decl1);
5562 /* Optionally warn of any def with no previous declaration
5563 if the function has already been used. */
5564 else if (warn_missing_declarations
5565 && old_decl != 0 && TREE_USED (old_decl)
5566 && C_DECL_IMPLICIT (old_decl))
5567 warning ("%H`%D' was used with no declaration before its definition",
5568 &DECL_SOURCE_LOCATION (decl1), decl1);
5570 /* This is a definition, not a reference.
5571 So normally clear DECL_EXTERNAL.
5572 However, `extern inline' acts like a declaration
5573 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5574 DECL_EXTERNAL (decl1) = current_extern_inline;
5576 /* This function exists in static storage.
5577 (This does not mean `static' in the C sense!) */
5578 TREE_STATIC (decl1) = 1;
5580 /* A nested function is not global. */
5581 if (current_function_decl != 0)
5582 TREE_PUBLIC (decl1) = 0;
5584 #ifdef ENABLE_CHECKING
5585 /* This is the earliest point at which we might know the assembler
5586 name of the function. Thus, if it's set before this, die horribly. */
5587 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5588 abort ();
5589 #endif
5591 /* If #pragma weak was used, mark the decl weak now. */
5592 if (current_scope == global_scope)
5593 maybe_apply_pragma_weak (decl1);
5595 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5596 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5598 tree args;
5599 int argct = 0;
5600 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5602 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5603 != integer_type_node)
5604 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5606 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5607 args = TREE_CHAIN (args))
5609 tree type = args ? TREE_VALUE (args) : 0;
5611 if (type == void_type_node)
5612 break;
5614 ++argct;
5615 switch (argct)
5617 case 1:
5618 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5619 pedwarn ("%Hfirst argument of '%D' should be `int'",
5620 locus, decl1);
5621 break;
5623 case 2:
5624 if (TREE_CODE (type) != POINTER_TYPE
5625 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5626 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5627 != char_type_node))
5628 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5629 locus, decl1);
5630 break;
5632 case 3:
5633 if (TREE_CODE (type) != POINTER_TYPE
5634 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5635 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5636 != char_type_node))
5637 pedwarn ("%Hthird argument of '%D' should probably be "
5638 "'char **'", locus, decl1);
5639 break;
5643 /* It is intentional that this message does not mention the third
5644 argument because it's only mentioned in an appendix of the
5645 standard. */
5646 if (argct > 0 && (argct < 2 || argct > 3))
5647 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5649 if (! TREE_PUBLIC (decl1))
5650 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5653 /* Record the decl so that the function name is defined.
5654 If we already have a decl for this name, and it is a FUNCTION_DECL,
5655 use the old decl. */
5657 current_function_decl = pushdecl (decl1);
5659 pushlevel (0);
5660 declare_parm_level ();
5662 make_decl_rtl (current_function_decl, NULL);
5664 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5665 /* Promote the value to int before returning it. */
5666 if (c_promoting_integer_type_p (restype))
5668 /* It retains unsignedness if not really getting wider. */
5669 if (TREE_UNSIGNED (restype)
5670 && (TYPE_PRECISION (restype)
5671 == TYPE_PRECISION (integer_type_node)))
5672 restype = unsigned_type_node;
5673 else
5674 restype = integer_type_node;
5676 DECL_RESULT (current_function_decl)
5677 = build_decl (RESULT_DECL, NULL_TREE, restype);
5679 /* If this fcn was already referenced via a block-scope `extern' decl
5680 (or an implicit decl), propagate certain information about the usage. */
5681 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5682 TREE_ADDRESSABLE (current_function_decl) = 1;
5684 immediate_size_expand = old_immediate_size_expand;
5686 start_fname_decls ();
5688 return 1;
5691 /* Subroutine of store_parm_decls which handles new-style function
5692 definitions (prototype format). The parms already have decls, so we
5693 need only record them as in effect and complain if any redundant
5694 old-style parm decls were written. */
5695 static void
5696 store_parm_decls_newstyle (void)
5698 tree decl, last;
5699 tree fndecl = current_function_decl;
5700 tree parms = current_function_parms;
5701 tree tags = current_function_parm_tags;
5702 tree others = current_function_parm_others;
5704 if (current_scope->parms || current_scope->names || current_scope->tags)
5706 error ("%Hold-style parameter declarations in prototyped "
5707 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5709 /* Get rid of the old-style declarations. */
5710 poplevel (0, 0, 0);
5711 pushlevel (0);
5714 /* Now make all the parameter declarations visible in the function body.
5715 We can bypass most of the grunt work of pushdecl. */
5716 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5718 DECL_CONTEXT (decl) = current_function_decl;
5719 if (DECL_NAME (decl) == 0)
5720 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5721 else
5723 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5724 current_scope->shadowed
5725 = tree_cons (DECL_NAME (decl),
5726 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5727 current_scope->shadowed);
5728 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5731 current_scope->parms = parms;
5732 current_scope->parms_last = last;
5734 /* Record the parameter list in the function declaration. */
5735 DECL_ARGUMENTS (fndecl) = parms;
5737 /* Now make all the ancillary declarations visible, likewise. */
5738 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5740 DECL_CONTEXT (decl) = current_function_decl;
5741 if (DECL_NAME (decl)
5742 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5744 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5745 current_scope->shadowed
5746 = tree_cons (DECL_NAME (decl),
5747 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5748 current_scope->shadowed);
5749 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5752 current_scope->names = others;
5753 current_scope->names_last = last;
5755 /* And all the tag declarations. */
5756 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5757 if (TREE_PURPOSE (decl))
5759 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5760 current_scope->shadowed_tags
5761 = tree_cons (TREE_PURPOSE (decl),
5762 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5763 current_scope->shadowed_tags);
5764 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5766 current_scope->tags = tags;
5769 /* Subroutine of store_parm_decls which handles old-style function
5770 definitions (separate parameter list and declarations). */
5772 static void
5773 store_parm_decls_oldstyle (void)
5775 tree parm, decl, last;
5776 tree fndecl = current_function_decl;
5778 /* This is the identifier list from the function declarator. */
5779 tree parmids = current_function_parms;
5781 /* We use DECL_WEAK as a flag to show which parameters have been
5782 seen already, since it is not used on PARM_DECL. */
5783 #ifdef ENABLE_CHECKING
5784 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5785 if (DECL_WEAK (parm))
5786 abort ();
5787 #endif
5789 /* Match each formal parameter name with its declaration. Save each
5790 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5791 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5793 if (TREE_VALUE (parm) == 0)
5795 error ("%Hparameter name missing from parameter list",
5796 &DECL_SOURCE_LOCATION (fndecl));
5797 TREE_PURPOSE (parm) = 0;
5798 continue;
5801 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5802 if (decl && DECL_CONTEXT (decl) == fndecl)
5804 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5805 /* If we got something other than a PARM_DECL it is an error. */
5806 if (TREE_CODE (decl) != PARM_DECL)
5807 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5808 /* If the declaration is already marked, we have a duplicate
5809 name. Complain and ignore the duplicate. */
5810 else if (DECL_WEAK (decl))
5812 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5813 TREE_PURPOSE (parm) = 0;
5814 continue;
5816 /* If the declaration says "void", complain and turn it into
5817 an int. */
5818 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5820 error ("%Hparameter \"%D\" declared void", locus, decl);
5821 TREE_TYPE (decl) = integer_type_node;
5822 DECL_ARG_TYPE (decl) = integer_type_node;
5823 layout_decl (decl, 0);
5826 /* If no declaration found, default to int. */
5827 else
5829 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5830 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5831 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5832 DECL_SOURCE_LOCATION (decl) = *locus;
5833 pushdecl (decl);
5835 if (flag_isoc99)
5836 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5837 else if (extra_warnings)
5838 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5841 TREE_PURPOSE (parm) = decl;
5842 DECL_WEAK (decl) = 1;
5845 /* Now examine the parms chain for incomplete declarations
5846 and declarations with no corresponding names. */
5848 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5850 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5852 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5854 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5855 TREE_TYPE (parm) = error_mark_node;
5858 if (! DECL_WEAK (parm))
5860 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5861 locus, parm);
5863 /* Pretend the parameter was not missing.
5864 This gets us to a standard state and minimizes
5865 further error messages. */
5866 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5870 /* Chain the declarations together in the order of the list of
5871 names. Store that chain in the function decl, replacing the
5872 list of names. Update the current scope to match. */
5873 DECL_ARGUMENTS (fndecl) = 0;
5875 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5876 if (TREE_PURPOSE (parm))
5877 break;
5878 if (parm && TREE_PURPOSE (parm))
5880 last = TREE_PURPOSE (parm);
5881 DECL_ARGUMENTS (fndecl) = last;
5882 current_scope->parms = last;
5883 DECL_WEAK (last) = 0;
5885 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5886 if (TREE_PURPOSE (parm))
5888 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5889 last = TREE_PURPOSE (parm);
5890 DECL_WEAK (last) = 0;
5892 current_scope->parms_last = last;
5893 TREE_CHAIN (last) = 0;
5896 /* If there was a previous prototype,
5897 set the DECL_ARG_TYPE of each argument according to
5898 the type previously specified, and report any mismatches. */
5900 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5902 tree type;
5903 for (parm = DECL_ARGUMENTS (fndecl),
5904 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5905 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5906 != void_type_node));
5907 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5909 if (parm == 0 || type == 0
5910 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5912 error ("number of arguments doesn't match prototype");
5913 error ("%Hprototype declaration",
5914 &current_function_prototype_locus);
5915 break;
5917 /* Type for passing arg must be consistent with that
5918 declared for the arg. ISO C says we take the unqualified
5919 type for parameters declared with qualified type. */
5920 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5921 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5922 COMPARE_STRICT))
5924 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5925 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5927 /* Adjust argument to match prototype. E.g. a previous
5928 `int foo(float);' prototype causes
5929 `int foo(x) float x; {...}' to be treated like
5930 `int foo(float x) {...}'. This is particularly
5931 useful for argument types like uid_t. */
5932 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5934 if (PROMOTE_PROTOTYPES
5935 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5936 && TYPE_PRECISION (TREE_TYPE (parm))
5937 < TYPE_PRECISION (integer_type_node))
5938 DECL_ARG_TYPE (parm) = integer_type_node;
5940 if (pedantic)
5942 pedwarn ("promoted argument \"%D\" "
5943 "doesn't match prototype", parm);
5944 pedwarn ("%Hprototype declaration",
5945 &current_function_prototype_locus);
5948 else
5950 error ("argument \"%D\" doesn't match prototype", parm);
5951 error ("%Hprototype declaration",
5952 &current_function_prototype_locus);
5956 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5959 /* Otherwise, create a prototype that would match. */
5961 else
5963 tree actual = 0, last = 0, type;
5965 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5967 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5968 if (last)
5969 TREE_CHAIN (last) = type;
5970 else
5971 actual = type;
5972 last = type;
5974 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5975 if (last)
5976 TREE_CHAIN (last) = type;
5977 else
5978 actual = type;
5980 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5981 of the type of this function, but we need to avoid having this
5982 affect the types of other similarly-typed functions, so we must
5983 first force the generation of an identical (but separate) type
5984 node for the relevant function type. The new node we create
5985 will be a variant of the main variant of the original function
5986 type. */
5988 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5990 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5994 /* Store the parameter declarations into the current function declaration.
5995 This is called after parsing the parameter declarations, before
5996 digesting the body of the function.
5998 For an old-style definition, construct a prototype out of the old-style
5999 parameter declarations and inject it into the function's type. */
6001 void
6002 store_parm_decls (void)
6004 tree fndecl = current_function_decl;
6006 /* The function containing FNDECL, if any. */
6007 tree context = decl_function_context (fndecl);
6009 /* True if this definition is written with a prototype. */
6010 bool prototype = (current_function_parms
6011 && TREE_CODE (current_function_parms) != TREE_LIST);
6013 if (prototype)
6014 store_parm_decls_newstyle ();
6015 else
6016 store_parm_decls_oldstyle ();
6018 /* The next call to pushlevel will be a function body. */
6020 next_is_function_body = true;
6022 /* Write a record describing this function definition to the prototypes
6023 file (if requested). */
6025 gen_aux_info_record (fndecl, 1, 0, prototype);
6027 /* Initialize the RTL code for the function. */
6028 init_function_start (fndecl);
6030 /* Begin the statement tree for this function. */
6031 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6033 /* If this is a nested function, save away the sizes of any
6034 variable-size types so that we can expand them when generating
6035 RTL. */
6036 if (context)
6038 tree t;
6040 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6041 = nreverse (get_pending_sizes ());
6042 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6044 t = TREE_CHAIN (t))
6045 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6048 /* This function is being processed in whole-function mode. */
6049 cfun->x_whole_function_mode_p = 1;
6051 /* Even though we're inside a function body, we still don't want to
6052 call expand_expr to calculate the size of a variable-sized array.
6053 We haven't necessarily assigned RTL to all variables yet, so it's
6054 not safe to try to expand expressions involving them. */
6055 immediate_size_expand = 0;
6056 cfun->x_dont_save_pending_sizes_p = 1;
6059 /* Finish up a function declaration and compile that function
6060 all the way to assembler language output. The free the storage
6061 for the function definition.
6063 This is called after parsing the body of the function definition.
6065 NESTED is nonzero if the function being finished is nested in another.
6066 CAN_DEFER_P is nonzero if the function may be deferred. */
6068 void
6069 finish_function (int nested, int can_defer_p)
6071 tree fndecl = current_function_decl;
6073 /* When a function declaration is totally empty, e.g.
6074 void foo(void) { }
6075 (the argument list is irrelevant) the compstmt rule will not
6076 bother calling pushlevel/poplevel, which means we get here with
6077 the scope stack out of sync. Detect this situation by noticing
6078 that current_scope is still as store_parm_decls left it, and do
6079 a dummy push/pop to get back to consistency.
6080 Note that the call to pushlevel does not actually push another
6081 scope - see there for details. */
6083 if (current_scope->parm_flag && next_is_function_body)
6085 pushlevel (0);
6086 poplevel (0, 0, 0);
6089 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6091 /* Must mark the RESULT_DECL as being in this function. */
6093 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6095 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6097 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6098 != integer_type_node)
6100 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6101 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6102 if (!warn_main)
6103 pedwarn ("%Hreturn type of '%D' is not `int'",
6104 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6106 else
6108 #ifdef DEFAULT_MAIN_RETURN
6109 /* Make it so that `main' always returns success by default. */
6110 DEFAULT_MAIN_RETURN;
6111 #else
6112 if (flag_isoc99)
6113 c_expand_return (integer_zero_node);
6114 #endif
6118 finish_fname_decls ();
6120 /* Tie off the statement tree for this function. */
6121 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6123 /* Complain if there's just no return statement. */
6124 if (warn_return_type
6125 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6126 && !current_function_returns_value && !current_function_returns_null
6127 /* Don't complain if we abort. */
6128 && !current_function_returns_abnormally
6129 /* Don't warn for main(). */
6130 && !MAIN_NAME_P (DECL_NAME (fndecl))
6131 /* Or if they didn't actually specify a return type. */
6132 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6133 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6134 inline function, as we might never be compiled separately. */
6135 && DECL_INLINE (fndecl))
6136 warning ("no return statement in function returning non-void");
6138 /* Clear out memory we no longer need. */
6139 free_after_parsing (cfun);
6140 /* Since we never call rest_of_compilation, we never clear
6141 CFUN. Do so explicitly. */
6142 free_after_compilation (cfun);
6143 cfun = NULL;
6145 if (flag_unit_at_a_time && can_defer_p)
6147 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6148 current_function_decl = NULL;
6149 return;
6152 if (! nested)
6154 /* Function is parsed.
6155 Generate RTL for the body of this function or defer
6156 it for later expansion. */
6157 bool uninlinable = true;
6159 /* There's no reason to do any of the work here if we're only doing
6160 semantic analysis; this code just generates RTL. */
6161 if (flag_syntax_only)
6163 current_function_decl = NULL;
6164 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6165 return;
6168 if (flag_inline_trees)
6170 /* First, cache whether the current function is inlinable. Some
6171 predicates depend on cfun and current_function_decl to
6172 function completely. */
6173 timevar_push (TV_INTEGRATION);
6174 uninlinable = !tree_inlinable_function_p (fndecl);
6176 if (can_defer_p
6177 /* We defer functions marked inline *even if* the function
6178 itself is not inlinable. This is because we don't yet
6179 know if the function will actually be used; we may be
6180 able to avoid emitting it entirely. */
6181 && (!uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6182 /* Save function tree for inlining. Should return 0 if the
6183 language does not support function deferring or the
6184 function could not be deferred. */
6185 && defer_fn (fndecl))
6187 /* Let the back-end know that this function exists. */
6188 (*debug_hooks->deferred_inline_function) (fndecl);
6189 timevar_pop (TV_INTEGRATION);
6190 current_function_decl = NULL;
6191 return;
6194 /* Then, inline any functions called in it. */
6195 optimize_inline_calls (fndecl);
6196 timevar_pop (TV_INTEGRATION);
6199 c_expand_body (fndecl);
6201 /* Keep the function body if it's needed for inlining or dumping. */
6202 if (uninlinable && !dump_enabled_p (TDI_all))
6204 /* Allow the body of the function to be garbage collected. */
6205 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6208 /* Let the error reporting routines know that we're outside a
6209 function. For a nested function, this value is used in
6210 c_pop_function_context and then reset via pop_function_context. */
6211 current_function_decl = NULL;
6215 /* Generate the RTL for a deferred function FNDECL. */
6217 void
6218 c_expand_deferred_function (tree fndecl)
6220 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6221 function was deferred, e.g. in duplicate_decls. */
6222 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6224 if (flag_inline_trees)
6226 timevar_push (TV_INTEGRATION);
6227 optimize_inline_calls (fndecl);
6228 timevar_pop (TV_INTEGRATION);
6230 c_expand_body (fndecl);
6231 current_function_decl = NULL;
6235 /* Called to move the SAVE_EXPRs for parameter declarations in a
6236 nested function into the nested function. DATA is really the
6237 nested FUNCTION_DECL. */
6239 static tree
6240 set_save_expr_context (tree *tp,
6241 int *walk_subtrees,
6242 void *data)
6244 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6245 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6246 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6247 circularity. */
6248 else if (DECL_P (*tp))
6249 *walk_subtrees = 0;
6251 return NULL_TREE;
6254 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6255 then we are already in the process of generating RTL for another
6256 function. If can_defer_p is zero, we won't attempt to defer the
6257 generation of RTL. */
6259 static void
6260 c_expand_body_1 (tree fndecl, int nested_p)
6262 timevar_push (TV_EXPAND);
6264 if (nested_p)
6266 /* Make sure that we will evaluate variable-sized types involved
6267 in our function's type. */
6268 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6269 /* Squirrel away our current state. */
6270 push_function_context ();
6273 /* Initialize the RTL code for the function. */
6274 current_function_decl = fndecl;
6275 input_location = DECL_SOURCE_LOCATION (fndecl);
6276 init_function_start (fndecl);
6278 /* This function is being processed in whole-function mode. */
6279 cfun->x_whole_function_mode_p = 1;
6281 /* Even though we're inside a function body, we still don't want to
6282 call expand_expr to calculate the size of a variable-sized array.
6283 We haven't necessarily assigned RTL to all variables yet, so it's
6284 not safe to try to expand expressions involving them. */
6285 immediate_size_expand = 0;
6286 cfun->x_dont_save_pending_sizes_p = 1;
6288 /* Set up parameters and prepare for return, for the function. */
6289 expand_function_start (fndecl, 0);
6291 /* If the function has a variably modified type, there may be
6292 SAVE_EXPRs in the parameter types. Their context must be set to
6293 refer to this function; they cannot be expanded in the containing
6294 function. */
6295 if (decl_function_context (fndecl)
6296 && variably_modified_type_p (TREE_TYPE (fndecl)))
6297 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6298 NULL);
6300 /* If this function is `main', emit a call to `__main'
6301 to run global initializers, etc. */
6302 if (DECL_NAME (fndecl)
6303 && MAIN_NAME_P (DECL_NAME (fndecl))
6304 && C_DECL_FILE_SCOPE (fndecl))
6305 expand_main_function ();
6307 /* Generate the RTL for this function. */
6308 expand_stmt (DECL_SAVED_TREE (fndecl));
6310 /* We hard-wired immediate_size_expand to zero above.
6311 expand_function_end will decrement this variable. So, we set the
6312 variable to one here, so that after the decrement it will remain
6313 zero. */
6314 immediate_size_expand = 1;
6316 /* Allow language dialects to perform special processing. */
6317 if (lang_expand_function_end)
6318 (*lang_expand_function_end) ();
6320 /* Generate rtl for function exit. */
6321 expand_function_end ();
6323 /* If this is a nested function, protect the local variables in the stack
6324 above us from being collected while we're compiling this function. */
6325 if (nested_p)
6326 ggc_push_context ();
6328 /* Run the optimizers and output the assembler code for this function. */
6329 rest_of_compilation (fndecl);
6331 /* Undo the GC context switch. */
6332 if (nested_p)
6333 ggc_pop_context ();
6335 /* With just -Wextra, complain only if function returns both with
6336 and without a value. */
6337 if (extra_warnings
6338 && current_function_returns_value
6339 && current_function_returns_null)
6340 warning ("this function may return with or without a value");
6342 /* If requested, warn about function definitions where the function will
6343 return a value (usually of some struct or union type) which itself will
6344 take up a lot of stack space. */
6346 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6348 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6350 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6351 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6352 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6353 larger_than_size))
6355 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
6356 unsigned int size_as_int
6357 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6359 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6360 warning ("%Hsize of return value of '%D' is %u bytes",
6361 locus, fndecl, size_as_int);
6362 else
6363 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6364 locus, fndecl, larger_than_size);
6368 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6369 && ! flag_inline_trees)
6371 /* Stop pointing to the local nodes about to be freed.
6372 But DECL_INITIAL must remain nonzero so we know this
6373 was an actual function definition.
6374 For a nested function, this is done in c_pop_function_context.
6375 If rest_of_compilation set this to 0, leave it 0. */
6376 if (DECL_INITIAL (fndecl) != 0)
6377 DECL_INITIAL (fndecl) = error_mark_node;
6379 DECL_ARGUMENTS (fndecl) = 0;
6382 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6384 if (targetm.have_ctors_dtors)
6385 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6386 DEFAULT_INIT_PRIORITY);
6387 else
6388 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6391 if (DECL_STATIC_DESTRUCTOR (fndecl))
6393 if (targetm.have_ctors_dtors)
6394 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6395 DEFAULT_INIT_PRIORITY);
6396 else
6397 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6400 if (nested_p)
6401 /* Return to the enclosing function. */
6402 pop_function_context ();
6403 timevar_pop (TV_EXPAND);
6406 /* Like c_expand_body_1 but only for unnested functions. */
6408 void
6409 c_expand_body (tree fndecl)
6411 c_expand_body_1 (fndecl, 0);
6414 /* Check the declarations given in a for-loop for satisfying the C99
6415 constraints. */
6416 void
6417 check_for_loop_decls (void)
6419 tree t;
6421 if (!flag_isoc99)
6423 /* If we get here, declarations have been used in a for loop without
6424 the C99 for loop scope. This doesn't make much sense, so don't
6425 allow it. */
6426 error ("'for' loop initial declaration used outside C99 mode");
6427 return;
6429 /* C99 subclause 6.8.5 paragraph 3:
6431 [#3] The declaration part of a for statement shall only
6432 declare identifiers for objects having storage class auto or
6433 register.
6435 It isn't clear whether, in this sentence, "identifiers" binds to
6436 "shall only declare" or to "objects" - that is, whether all identifiers
6437 declared must be identifiers for objects, or whether the restriction
6438 only applies to those that are. (A question on this in comp.std.c
6439 in November 2000 received no answer.) We implement the strictest
6440 interpretation, to avoid creating an extension which later causes
6441 problems. */
6443 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6445 if (TREE_PURPOSE (t) != 0)
6447 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6449 if (code == RECORD_TYPE)
6450 error ("'struct %s' declared in 'for' loop initial declaration",
6451 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6452 else if (code == UNION_TYPE)
6453 error ("'union %s' declared in 'for' loop initial declaration",
6454 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6455 else
6456 error ("'enum %s' declared in 'for' loop initial declaration",
6457 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6461 for (t = getdecls (); t; t = TREE_CHAIN (t))
6463 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6464 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6465 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6466 "initial declaration", locus, t);
6467 else if (TREE_STATIC (t))
6468 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6469 "initial declaration", locus, t);
6470 else if (DECL_EXTERNAL (t))
6471 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6472 "initial declaration", locus, t);
6476 /* Save and restore the variables in this file and elsewhere
6477 that keep track of the progress of compilation of the current function.
6478 Used for nested functions. */
6480 struct language_function GTY(())
6482 struct c_language_function base;
6483 int returns_value;
6484 int returns_null;
6485 int returns_abnormally;
6486 int warn_about_return_type;
6487 int extern_inline;
6490 /* Save and reinitialize the variables
6491 used during compilation of a C function. */
6493 void
6494 c_push_function_context (struct function *f)
6496 struct language_function *p;
6497 p = ggc_alloc (sizeof (struct language_function));
6498 f->language = p;
6500 p->base.x_stmt_tree = c_stmt_tree;
6501 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6502 p->returns_value = current_function_returns_value;
6503 p->returns_null = current_function_returns_null;
6504 p->returns_abnormally = current_function_returns_abnormally;
6505 p->warn_about_return_type = warn_about_return_type;
6506 p->extern_inline = current_extern_inline;
6509 /* Restore the variables used during compilation of a C function. */
6511 void
6512 c_pop_function_context (struct function *f)
6514 struct language_function *p = f->language;
6516 if (DECL_SAVED_INSNS (current_function_decl) == 0
6517 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6519 /* Stop pointing to the local nodes about to be freed. */
6520 /* But DECL_INITIAL must remain nonzero so we know this
6521 was an actual function definition. */
6522 DECL_INITIAL (current_function_decl) = error_mark_node;
6523 DECL_ARGUMENTS (current_function_decl) = 0;
6526 c_stmt_tree = p->base.x_stmt_tree;
6527 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6528 current_function_returns_value = p->returns_value;
6529 current_function_returns_null = p->returns_null;
6530 current_function_returns_abnormally = p->returns_abnormally;
6531 warn_about_return_type = p->warn_about_return_type;
6532 current_extern_inline = p->extern_inline;
6534 f->language = NULL;
6537 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6539 void
6540 c_dup_lang_specific_decl (tree decl)
6542 struct lang_decl *ld;
6544 if (!DECL_LANG_SPECIFIC (decl))
6545 return;
6547 ld = ggc_alloc (sizeof (struct lang_decl));
6548 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6549 DECL_LANG_SPECIFIC (decl) = ld;
6552 /* The functions below are required for functionality of doing
6553 function at once processing in the C front end. Currently these
6554 functions are not called from anywhere in the C front end, but as
6555 these changes continue, that will change. */
6557 /* Returns nonzero if the current statement is a full expression,
6558 i.e. temporaries created during that statement should be destroyed
6559 at the end of the statement. */
6562 stmts_are_full_exprs_p (void)
6564 return 0;
6567 /* Returns the stmt_tree (if any) to which statements are currently
6568 being added. If there is no active statement-tree, NULL is
6569 returned. */
6571 stmt_tree
6572 current_stmt_tree (void)
6574 return &c_stmt_tree;
6577 /* Returns the stack of SCOPE_STMTs for the current function. */
6579 tree *
6580 current_scope_stmt_stack (void)
6582 return &c_scope_stmt_stack;
6585 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6586 C. */
6589 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6591 return 0;
6594 /* Dummy function in place of callback used by C++. */
6596 void
6597 extract_interface_info (void)
6601 /* Return a new COMPOUND_STMT, after adding it to the current
6602 statement tree. */
6604 tree
6605 c_begin_compound_stmt (void)
6607 tree stmt;
6609 /* Create the COMPOUND_STMT. */
6610 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6612 return stmt;
6615 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6616 common code. */
6618 void
6619 c_expand_decl_stmt (tree t)
6621 tree decl = DECL_STMT_DECL (t);
6623 /* Expand nested functions. */
6624 if (TREE_CODE (decl) == FUNCTION_DECL
6625 && DECL_CONTEXT (decl) == current_function_decl
6626 && DECL_SAVED_TREE (decl))
6627 c_expand_body_1 (decl, 1);
6630 /* Return the global value of T as a symbol. */
6632 tree
6633 identifier_global_value (tree t)
6635 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6636 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6637 return decl;
6639 /* Shadowed by something else; find the true global value. */
6640 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6641 if (DECL_NAME (decl) == t)
6642 return decl;
6644 /* Only local values for this decl. */
6645 return 0;
6648 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6649 otherwise the name is found in ridpointers from RID_INDEX. */
6651 void
6652 record_builtin_type (enum rid rid_index, const char *name, tree type)
6654 tree id;
6655 if (name == 0)
6656 id = ridpointers[(int) rid_index];
6657 else
6658 id = get_identifier (name);
6659 pushdecl (build_decl (TYPE_DECL, id, type));
6662 /* Build the void_list_node (void_type_node having been created). */
6663 tree
6664 build_void_list_node (void)
6666 tree t = build_tree_list (NULL_TREE, void_type_node);
6667 return t;
6670 /* Return something to represent absolute declarators containing a *.
6671 TARGET is the absolute declarator that the * contains.
6672 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6673 to apply to the pointer type, represented as identifiers, possible mixed
6674 with attributes.
6676 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6677 if attributes are present) and whose type is the modifier list. */
6679 tree
6680 make_pointer_declarator (tree type_quals_attrs, tree target)
6682 tree quals, attrs;
6683 tree itarget = target;
6684 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6685 if (attrs != NULL_TREE)
6686 itarget = tree_cons (attrs, target, NULL_TREE);
6687 return build1 (INDIRECT_REF, quals, itarget);
6690 /* A wrapper around lhd_set_decl_assembler_name that gives static
6691 variables their C names if they are at file scope and only one
6692 translation unit is being compiled, for backwards compatibility
6693 with certain bizzare assembler hacks (like crtstuff.c). */
6695 void
6696 c_static_assembler_name (tree decl)
6698 if (num_in_fnames == 1
6699 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6700 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6701 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6702 else
6703 lhd_set_decl_assembler_name (decl);
6706 /* Hash and equality functions for link_hash_table: key off
6707 DECL_ASSEMBLER_NAME. */
6709 static hashval_t
6710 link_hash_hash (const void *x_p)
6712 tree x = (tree)x_p;
6713 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6716 static int
6717 link_hash_eq (const void *x1_p, const void *x2_p)
6719 tree x1 = (tree)x1_p;
6720 tree x2 = (tree)x2_p;
6721 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6724 /* Propagate information between definitions and uses between multiple
6725 translation units in TU_LIST based on linkage rules. */
6727 void
6728 merge_translation_unit_decls (void)
6730 const tree tu_list = current_file_decl;
6731 tree tu;
6732 tree decl;
6733 htab_t link_hash_table;
6734 tree block;
6736 /* Create the BLOCK that poplevel would have created, but don't
6737 actually call poplevel since that's expensive. */
6738 block = make_node (BLOCK);
6739 BLOCK_VARS (block) = current_scope->names;
6740 TREE_USED (block) = 1;
6741 DECL_INITIAL (current_file_decl) = block;
6743 /* If only one translation unit seen, no copying necessary. */
6744 if (TREE_CHAIN (tu_list) == NULL_TREE)
6745 return;
6747 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6749 /* Enter any actual definitions into the hash table. */
6750 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6751 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6752 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6754 PTR *slot;
6755 slot = htab_find_slot (link_hash_table, decl, INSERT);
6757 /* If we've already got a definition, work out which one is
6758 the real one, put it into the hash table, and make the
6759 other one DECL_EXTERNAL. This is important to avoid
6760 putting out two definitions of the same symbol in the
6761 assembly output. */
6762 if (*slot != NULL)
6764 tree old_decl = (tree) *slot;
6766 /* If this is weak or common or whatever, suppress it
6767 in favour of the other definition. */
6768 if (DECL_WEAK (decl))
6769 DECL_EXTERNAL (decl) = 1;
6770 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6771 DECL_EXTERNAL (old_decl) = 1;
6772 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6773 DECL_EXTERNAL (decl) = 1;
6774 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6775 DECL_EXTERNAL (old_decl) = 1;
6777 if (DECL_EXTERNAL (decl))
6779 DECL_INITIAL (decl) = NULL_TREE;
6780 DECL_COMMON (decl) = 0;
6781 DECL_ONE_ONLY (decl) = 0;
6782 DECL_WEAK (decl) = 0;
6784 else if (DECL_EXTERNAL (old_decl))
6786 DECL_INITIAL (old_decl) = NULL_TREE;
6787 DECL_COMMON (old_decl) = 0;
6788 DECL_ONE_ONLY (old_decl) = 0;
6789 DECL_WEAK (old_decl) = 0;
6790 *slot = decl;
6792 else
6794 error ("%Hredefinition of global '%D'",
6795 &DECL_SOURCE_LOCATION (decl), decl);
6796 error ("%H'%D' previously defined here",
6797 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6800 else
6801 *slot = decl;
6804 /* Now insert the desired information from all the definitions
6805 into any plain declarations. */
6806 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6807 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6808 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6810 tree global_decl;
6811 global_decl = htab_find (link_hash_table, decl);
6813 if (! global_decl)
6814 continue;
6816 /* Print any appropriate error messages, and partially merge
6817 the decls. */
6818 (void) duplicate_decls (decl, global_decl, true, true);
6821 htab_delete (link_hash_table);
6824 /* Perform final processing on file-scope data. */
6826 void
6827 c_write_global_declarations(void)
6829 tree link;
6831 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6833 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6834 int len = list_length (globals);
6835 tree *vec = xmalloc (sizeof (tree) * len);
6836 int i;
6837 tree decl;
6839 /* Process the decls in the order they were written. */
6841 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6842 vec[i] = decl;
6844 wrapup_global_declarations (vec, len);
6846 check_global_declarations (vec, len);
6848 /* Clean up. */
6849 free (vec);
6853 /* Reset the parser's state in preparation for a new file. */
6855 void
6856 c_reset_state (void)
6858 tree link;
6859 tree file_scope_decl;
6861 /* Pop the global scope. */
6862 if (current_scope != global_scope)
6863 current_scope = global_scope;
6864 file_scope_decl = current_file_decl;
6865 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6866 truly_local_externals = NULL_TREE;
6868 /* Start a new global binding level. */
6869 pushlevel (0);
6870 global_scope = current_scope;
6871 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6872 TREE_CHAIN (current_file_decl) = file_scope_decl;
6874 /* Reintroduce the builtin declarations. */
6875 for (link = first_builtin_decl;
6876 link != TREE_CHAIN (last_builtin_decl);
6877 link = TREE_CHAIN (link))
6878 pushdecl (copy_node (link));
6881 #include "gt-c-decl.h"