Fix thinko
[official-gcc.git] / gcc / c-decl.c
blob721946ec6c8ac889c7934744600ac18aaee580f3
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 && (TREE_CODE (x) != TYPE_DECL
1766 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE))
1767 scope->incomplete = tree_cons (NULL_TREE, x, scope->incomplete);
1771 if (TREE_CODE (x) == PARM_DECL)
1772 SCOPE_LIST_APPEND (scope, parms, x);
1773 else
1774 SCOPE_LIST_APPEND (scope, names, x);
1776 return x;
1779 /* Record X as belonging to the global scope (C99 "file scope").
1780 This is used only internally by the Objective-C front end,
1781 and is limited to its needs. duplicate_decls is not called;
1782 if there is any preexisting decl for this identifier, it is an ICE. */
1784 tree
1785 pushdecl_top_level (tree x)
1787 tree name;
1789 if (TREE_CODE (x) != VAR_DECL)
1790 abort ();
1792 name = DECL_NAME (x);
1794 if (IDENTIFIER_SYMBOL_VALUE (name))
1795 abort ();
1797 DECL_CONTEXT (x) = current_file_decl;
1798 IDENTIFIER_SYMBOL_VALUE (name) = x;
1800 SCOPE_LIST_APPEND (global_scope, names, x);
1801 return x;
1804 /* Generate an implicit declaration for identifier FUNCTIONID as a
1805 function of type int (). */
1807 tree
1808 implicitly_declare (tree functionid)
1810 tree decl = any_external_decl (functionid);
1812 if (decl)
1814 /* Implicit declaration of a function already declared
1815 (somehow) in a different scope, or as a built-in.
1816 If this is the first time this has happened, warn;
1817 then recycle the old declaration. */
1818 if (!C_DECL_IMPLICIT (decl))
1820 implicit_decl_warning (DECL_NAME (decl));
1821 if (! C_DECL_FILE_SCOPE (decl))
1822 warning ("%Hprevious declaration of '%D'",
1823 &DECL_SOURCE_LOCATION (decl), decl);
1824 C_DECL_IMPLICIT (decl) = 1;
1826 /* If this function is global, then it must already be in the
1827 global scope, so there's no need to push it again. */
1828 if (current_scope == global_scope)
1829 return decl;
1830 /* If this is a local declaration, make a copy; we can't have
1831 the same DECL listed in two different scopes. */
1832 return pushdecl (copy_node (decl));
1835 /* Not seen before. */
1836 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1837 DECL_EXTERNAL (decl) = 1;
1838 TREE_PUBLIC (decl) = 1;
1839 C_DECL_IMPLICIT (decl) = 1;
1840 implicit_decl_warning (functionid);
1842 /* C89 says implicit declarations are in the innermost block.
1843 So we record the decl in the standard fashion. */
1844 decl = pushdecl (decl);
1846 /* No need to call objc_check_decl here - it's a function type. */
1847 rest_of_decl_compilation (decl, NULL, 0, 0);
1849 /* Write a record describing this implicit function declaration
1850 to the prototypes file (if requested). */
1851 gen_aux_info_record (decl, 0, 1, 0);
1853 /* Possibly apply some default attributes to this implicit declaration. */
1854 decl_attributes (&decl, NULL_TREE, 0);
1856 return decl;
1859 static void
1860 implicit_decl_warning (tree id)
1862 const char *name = IDENTIFIER_POINTER (id);
1863 if (mesg_implicit_function_declaration == 2)
1864 error ("implicit declaration of function `%s'", name);
1865 else if (mesg_implicit_function_declaration == 1)
1866 warning ("implicit declaration of function `%s'", name);
1869 /* Return zero if the declaration NEWDECL is valid
1870 when the declaration OLDDECL (assumed to be for the same name)
1871 has already been seen.
1872 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1873 and 3 if it is a conflicting declaration. */
1875 static int
1876 redeclaration_error_message (tree newdecl, tree olddecl)
1878 if (TREE_CODE (newdecl) == TYPE_DECL)
1880 /* Do not complain about type redeclarations where at least one
1881 declaration was in a system header. */
1882 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1883 return 0;
1884 return 1;
1886 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1888 /* Declarations of functions can insist on internal linkage
1889 but they can't be inconsistent with internal linkage,
1890 so there can be no error on that account.
1891 However defining the same name twice is no good. */
1892 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1893 /* However, defining once as extern inline and a second
1894 time in another way is ok. */
1895 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1896 && ! (DECL_DECLARED_INLINE_P (newdecl)
1897 && DECL_EXTERNAL (newdecl))))
1898 return 1;
1899 return 0;
1901 else if (C_DECL_FILE_SCOPE (newdecl))
1903 /* Objects declared at file scope: */
1904 /* If at least one is a reference, it's ok. */
1905 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1906 return 0;
1907 /* Reject two definitions. */
1908 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1909 return 1;
1910 /* Now we have two tentative defs, or one tentative and one real def. */
1911 /* Insist that the linkage match. */
1912 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1913 return 3;
1914 return 0;
1916 else if (current_scope->parm_flag
1917 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1918 return 0;
1919 else
1921 /* Newdecl has block scope. If olddecl has block scope also, then
1922 reject two definitions, and reject a definition together with an
1923 external reference. Otherwise, it is OK, because newdecl must
1924 be an extern reference to olddecl. */
1925 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1926 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1927 return 2;
1928 return 0;
1932 /* Issue an error message for a reference to an undeclared variable
1933 ID, including a reference to a builtin outside of function-call
1934 context. Establish a binding of the identifier to error_mark_node
1935 in an appropriate scope, which will suppress further errors for the
1936 same identifier. */
1937 void
1938 undeclared_variable (tree id)
1940 static bool already = false;
1941 struct c_scope *scope;
1943 if (current_function_decl == 0)
1945 error ("`%s' undeclared here (not in a function)",
1946 IDENTIFIER_POINTER (id));
1947 scope = current_scope;
1949 else
1951 error ("`%s' undeclared (first use in this function)",
1952 IDENTIFIER_POINTER (id));
1954 if (! already)
1956 error ("(Each undeclared identifier is reported only once");
1957 error ("for each function it appears in.)");
1958 already = true;
1961 scope = current_function_scope;
1964 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1965 scope->shadowed);
1966 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1969 /* Subroutine of lookup_label, declare_label, define_label: construct a
1970 LABEL_DECL with all the proper frills. */
1972 static tree
1973 make_label (tree name, location_t location)
1975 tree label = build_decl (LABEL_DECL, name, void_type_node);
1977 DECL_CONTEXT (label) = current_function_decl;
1978 DECL_MODE (label) = VOIDmode;
1979 DECL_SOURCE_LOCATION (label) = location;
1981 return label;
1984 /* Another subroutine of lookup_label, declare_label, define_label:
1985 set up the binding of name to LABEL_DECL in the given SCOPE. */
1987 static void
1988 bind_label (tree name, tree label, struct c_scope *scope)
1990 if (IDENTIFIER_LABEL_VALUE (name))
1991 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1992 scope->shadowed);
1993 IDENTIFIER_LABEL_VALUE (name) = label;
1995 SCOPE_LIST_APPEND (scope, names, label);
1998 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1999 Create one if none exists so far for the current function.
2000 This is called when a label is used in a goto expression or
2001 has its address taken. */
2003 tree
2004 lookup_label (tree name)
2006 tree label;
2008 if (current_function_decl == 0)
2010 error ("label %s referenced outside of any function",
2011 IDENTIFIER_POINTER (name));
2012 return 0;
2015 /* Use a label already defined or ref'd with this name, but not if
2016 it is inherited from a containing function and wasn't declared
2017 using __label__. */
2018 label = IDENTIFIER_LABEL_VALUE (name);
2019 if (label && (DECL_CONTEXT (label) == current_function_decl
2020 || C_DECLARED_LABEL_FLAG (label)))
2022 /* If the label has only been declared, update its apparent
2023 location to point here, for better diagnostics if it
2024 turns out not to have been defined. */
2025 if (!TREE_USED (label))
2026 DECL_SOURCE_LOCATION (label) = input_location;
2027 return label;
2030 /* No label binding for that identifier; make one. */
2031 label = make_label (name, input_location);
2033 /* Ordinary labels go in the current function scope. */
2034 bind_label (name, label, current_function_scope);
2035 return label;
2038 /* Make a label named NAME in the current function, shadowing silently
2039 any that may be inherited from containing functions or containing
2040 scopes. This is called for __label__ declarations. */
2042 /* Note that valid use, if the label being shadowed comes from another
2043 scope in the same function, requires calling declare_nonlocal_label
2044 right away. (Is this still true? -zw 2003-07-17) */
2046 tree
2047 declare_label (tree name)
2049 tree label = IDENTIFIER_LABEL_VALUE (name);
2050 tree dup;
2052 /* Check to make sure that the label hasn't already been declared
2053 at this scope */
2054 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2055 if (dup == label)
2057 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2058 error ("%Hthis is a previous declaration",
2059 &DECL_SOURCE_LOCATION (dup));
2061 /* Just use the previous declaration. */
2062 return dup;
2065 label = make_label (name, input_location);
2066 C_DECLARED_LABEL_FLAG (label) = 1;
2068 /* Declared labels go in the current scope. */
2069 bind_label (name, label, current_scope);
2070 return label;
2073 /* Define a label, specifying the location in the source file.
2074 Return the LABEL_DECL node for the label, if the definition is valid.
2075 Otherwise return 0. */
2077 tree
2078 define_label (location_t location, tree name)
2080 tree label;
2082 /* Find any preexisting label with this name. It is an error
2083 if that label has already been defined in this function, or
2084 if there is a containing function with a declared label with
2085 the same name. */
2086 label = IDENTIFIER_LABEL_VALUE (name);
2088 if (label
2089 && ((DECL_CONTEXT (label) == current_function_decl
2090 && DECL_INITIAL (label) != 0)
2091 || (DECL_CONTEXT (label) != current_function_decl
2092 && C_DECLARED_LABEL_FLAG (label))))
2094 location_t *prev_loc = &DECL_SOURCE_LOCATION (label);
2095 error ("%Hduplicate label `%D'", &location, label);
2096 if (DECL_INITIAL (label))
2097 error ("%H`%D' previously defined here", prev_loc, label);
2098 else
2099 error ("%H`%D' previously declared here", prev_loc, label);
2100 return 0;
2102 else if (label && DECL_CONTEXT (label) == current_function_decl)
2104 /* The label has been used or declared already in this function,
2105 but not defined. Update its location to point to this
2106 definition. */
2107 DECL_SOURCE_LOCATION (label) = location;
2109 else
2111 /* No label binding for that identifier; make one. */
2112 label = make_label (name, location);
2114 /* Ordinary labels go in the current function scope. */
2115 bind_label (name, label, current_function_scope);
2118 if (warn_traditional && !in_system_header && lookup_name (name))
2119 warning ("%Htraditional C lacks a separate namespace for labels, "
2120 "identifier `%s' conflicts", &location,
2121 IDENTIFIER_POINTER (name));
2123 /* Mark label as having been defined. */
2124 DECL_INITIAL (label) = error_mark_node;
2125 return label;
2128 /* Return the list of declarations of the current scope. */
2130 tree
2131 getdecls (void)
2133 return current_scope->names;
2137 /* Given NAME, an IDENTIFIER_NODE,
2138 return the structure (or union or enum) definition for that name.
2139 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2140 CODE says which kind of type the caller wants;
2141 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2142 If the wrong kind of type is found, an error is reported. */
2144 static tree
2145 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2147 tree tag = IDENTIFIER_TAG_VALUE (name);
2148 int thislevel = 0;
2150 if (!tag)
2151 return 0;
2153 /* We only care about whether it's in this level if
2154 thislevel_only was set or it might be a type clash. */
2155 if (thislevel_only || TREE_CODE (tag) != code)
2157 if (current_scope == global_scope
2158 || purpose_member (name, current_scope->tags))
2159 thislevel = 1;
2162 if (thislevel_only && !thislevel)
2163 return 0;
2165 if (TREE_CODE (tag) != code)
2167 /* Definition isn't the kind we were looking for. */
2168 pending_invalid_xref = name;
2169 pending_invalid_xref_location = input_location;
2171 /* If in the same binding level as a declaration as a tag
2172 of a different type, this must not be allowed to
2173 shadow that tag, so give the error immediately.
2174 (For example, "struct foo; union foo;" is invalid.) */
2175 if (thislevel)
2176 pending_xref_error ();
2178 return tag;
2181 /* Print an error message now
2182 for a recent invalid struct, union or enum cross reference.
2183 We don't print them immediately because they are not invalid
2184 when used in the `struct foo;' construct for shadowing. */
2186 void
2187 pending_xref_error (void)
2189 if (pending_invalid_xref != 0)
2190 error ("%H`%s' defined as wrong kind of tag",
2191 &pending_invalid_xref_location,
2192 IDENTIFIER_POINTER (pending_invalid_xref));
2193 pending_invalid_xref = 0;
2197 /* Look up NAME in the current scope and its superiors
2198 in the namespace of variables, functions and typedefs.
2199 Return a ..._DECL node of some kind representing its definition,
2200 or return 0 if it is undefined. */
2202 tree
2203 lookup_name (tree name)
2205 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2206 if (decl == 0 || decl == error_mark_node)
2207 return decl;
2208 if (C_DECL_INVISIBLE (decl))
2209 return 0;
2210 return decl;
2213 /* Similar to `lookup_name' but look only at the current scope. */
2215 static tree
2216 lookup_name_current_level (tree name)
2218 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2220 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2221 return 0;
2223 if (current_scope == global_scope)
2224 return decl;
2226 /* Scan the current scope for a decl with name NAME.
2227 For PARM_DECLs, we have to look at both ->parms and ->names, since
2228 forward parameter declarations wind up on the ->names list. */
2229 if (TREE_CODE (decl) == PARM_DECL
2230 && chain_member (decl, current_scope->parms))
2231 return decl;
2232 if (chain_member (decl, current_scope->names))
2233 return decl;
2235 return 0;
2238 /* Create the predefined scalar types of C,
2239 and some nodes representing standard constants (0, 1, (void *) 0).
2240 Initialize the global scope.
2241 Make definitions for built-in primitive functions. */
2243 void
2244 c_init_decl_processing (void)
2246 tree endlink;
2247 tree ptr_ftype_void, ptr_ftype_ptr;
2248 location_t save_loc = input_location;
2250 /* Adds some ggc roots, and reserved words for c-parse.in. */
2251 c_parse_init ();
2253 current_function_decl = 0;
2255 /* Make the c_scope structure for global names. */
2256 pushlevel (0);
2257 global_scope = current_scope;
2259 /* Declarations from c_common_nodes_and_builtins must not be associated
2260 with this input file, lest we get differences between using and not
2261 using preprocessed headers. */
2262 input_location.file = "<internal>";
2263 input_location.line = 0;
2265 /* Make the DECL for the toplevel file scope. */
2266 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2268 build_common_tree_nodes (flag_signed_char);
2270 c_common_nodes_and_builtins ();
2272 /* In C, comparisons and TRUTH_* expressions have type int. */
2273 truthvalue_type_node = integer_type_node;
2274 truthvalue_true_node = integer_one_node;
2275 truthvalue_false_node = integer_zero_node;
2277 /* Even in C99, which has a real boolean type. */
2278 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2279 boolean_type_node));
2281 endlink = void_list_node;
2282 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2283 ptr_ftype_ptr
2284 = build_function_type (ptr_type_node,
2285 tree_cons (NULL_TREE, ptr_type_node, endlink));
2287 input_location = save_loc;
2289 pedantic_lvalues = pedantic;
2291 make_fname_decl = c_make_fname_decl;
2292 start_fname_decls ();
2294 first_builtin_decl = global_scope->names;
2295 last_builtin_decl = global_scope->names_last;
2298 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2299 decl, NAME is the initialization string and TYPE_DEP indicates whether
2300 NAME depended on the type of the function. As we don't yet implement
2301 delayed emission of static data, we mark the decl as emitted
2302 so it is not placed in the output. Anything using it must therefore pull
2303 out the STRING_CST initializer directly. FIXME. */
2305 static tree
2306 c_make_fname_decl (tree id, int type_dep)
2308 const char *name = fname_as_string (type_dep);
2309 tree decl, type, init;
2310 size_t length = strlen (name);
2312 type = build_array_type
2313 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2314 build_index_type (size_int (length)));
2316 decl = build_decl (VAR_DECL, id, type);
2318 TREE_STATIC (decl) = 1;
2319 TREE_READONLY (decl) = 1;
2320 DECL_ARTIFICIAL (decl) = 1;
2322 init = build_string (length + 1, name);
2323 TREE_TYPE (init) = type;
2324 DECL_INITIAL (decl) = init;
2326 TREE_USED (decl) = 1;
2328 if (current_function_decl)
2330 DECL_CONTEXT (decl) = current_function_decl;
2331 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2332 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2335 finish_decl (decl, init, NULL_TREE);
2337 return decl;
2340 /* Return a definition for a builtin function named NAME and whose data type
2341 is TYPE. TYPE should be a function type with argument types.
2342 FUNCTION_CODE tells later passes how to compile calls to this function.
2343 See tree.h for its possible values.
2345 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2346 the name to be called if we can't opencode the function. If
2347 ATTRS is nonzero, use that for the function's attribute list. */
2349 tree
2350 builtin_function (const char *name, tree type, int function_code,
2351 enum built_in_class class, const char *library_name,
2352 tree attrs)
2354 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2355 DECL_EXTERNAL (decl) = 1;
2356 TREE_PUBLIC (decl) = 1;
2357 if (library_name)
2358 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2359 make_decl_rtl (decl, NULL);
2360 pushdecl (decl);
2361 DECL_BUILT_IN_CLASS (decl) = class;
2362 DECL_FUNCTION_CODE (decl) = function_code;
2364 /* Warn if a function in the namespace for users
2365 is used without an occasion to consider it declared. */
2366 if (name[0] != '_' || name[1] != '_')
2367 C_DECL_INVISIBLE (decl) = 1;
2369 /* Possibly apply some default attributes to this built-in function. */
2370 if (attrs)
2371 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2372 else
2373 decl_attributes (&decl, NULL_TREE, 0);
2375 return decl;
2378 /* Called when a declaration is seen that contains no names to declare.
2379 If its type is a reference to a structure, union or enum inherited
2380 from a containing scope, shadow that tag name for the current scope
2381 with a forward reference.
2382 If its type defines a new named structure or union
2383 or defines an enum, it is valid but we need not do anything here.
2384 Otherwise, it is an error. */
2386 void
2387 shadow_tag (tree declspecs)
2389 shadow_tag_warned (declspecs, 0);
2392 void
2393 shadow_tag_warned (tree declspecs, int warned)
2396 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2397 no pedwarn. */
2399 int found_tag = 0;
2400 tree link;
2401 tree specs, attrs;
2403 pending_invalid_xref = 0;
2405 /* Remove the attributes from declspecs, since they will confuse the
2406 following code. */
2407 split_specs_attrs (declspecs, &specs, &attrs);
2409 for (link = specs; link; link = TREE_CHAIN (link))
2411 tree value = TREE_VALUE (link);
2412 enum tree_code code = TREE_CODE (value);
2414 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2415 /* Used to test also that TYPE_SIZE (value) != 0.
2416 That caused warning for `struct foo;' at top level in the file. */
2418 tree name = TYPE_NAME (value);
2419 tree t;
2421 found_tag++;
2423 if (name == 0)
2425 if (warned != 1 && code != ENUMERAL_TYPE)
2426 /* Empty unnamed enum OK */
2428 pedwarn ("unnamed struct/union that defines no instances");
2429 warned = 1;
2432 else
2434 t = lookup_tag (code, name, 1);
2436 if (t == 0)
2438 t = make_node (code);
2439 pushtag (name, t);
2443 else
2445 if (!warned && ! in_system_header)
2447 warning ("useless keyword or type name in empty declaration");
2448 warned = 2;
2453 if (found_tag > 1)
2454 error ("two types specified in one empty declaration");
2456 if (warned != 1)
2458 if (found_tag == 0)
2459 pedwarn ("empty declaration");
2463 /* Construct an array declarator. EXPR is the expression inside [], or
2464 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2465 to the pointer to which a parameter array is converted). STATIC_P is
2466 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2467 is nonzero is the array is [*], a VLA of unspecified length which is
2468 nevertheless a complete type (not currently implemented by GCC),
2469 zero otherwise. The declarator is constructed as an ARRAY_REF
2470 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2471 left of the [] (filled by in set_array_declarator_type) and operand 1
2472 is the expression inside; whose TREE_TYPE is the type qualifiers and
2473 which has TREE_STATIC set if "static" is used. */
2475 tree
2476 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2478 tree decl;
2479 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2480 TREE_TYPE (decl) = quals;
2481 TREE_STATIC (decl) = (static_p ? 1 : 0);
2482 if (pedantic && !flag_isoc99)
2484 if (static_p || quals != NULL_TREE)
2485 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2486 if (vla_unspec_p)
2487 pedwarn ("ISO C90 does not support `[*]' array declarators");
2489 if (vla_unspec_p)
2490 warning ("GCC does not yet properly implement `[*]' array declarators");
2491 return decl;
2494 /* Set the type of an array declarator. DECL is the declarator, as
2495 constructed by build_array_declarator; TYPE is what appears on the left
2496 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2497 abstract declarator, zero otherwise; this is used to reject static and
2498 type qualifiers in abstract declarators, where they are not in the
2499 C99 grammar. */
2501 tree
2502 set_array_declarator_type (tree decl, tree type, int abstract_p)
2504 TREE_OPERAND (decl, 0) = type;
2505 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2506 error ("static or type qualifiers in abstract declarator");
2507 return decl;
2510 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2512 tree
2513 groktypename (tree typename)
2515 tree specs, attrs;
2517 if (TREE_CODE (typename) != TREE_LIST)
2518 return typename;
2520 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2522 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0);
2524 /* Apply attributes. */
2525 decl_attributes (&typename, attrs, 0);
2527 return typename;
2530 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2532 tree
2533 groktypename_in_parm_context (tree typename)
2535 if (TREE_CODE (typename) != TREE_LIST)
2536 return typename;
2537 return grokdeclarator (TREE_VALUE (typename),
2538 TREE_PURPOSE (typename),
2539 PARM, 0);
2542 /* Decode a declarator in an ordinary declaration or data definition.
2543 This is called as soon as the type information and variable name
2544 have been parsed, before parsing the initializer if any.
2545 Here we create the ..._DECL node, fill in its type,
2546 and put it on the list of decls for the current context.
2547 The ..._DECL node is returned as the value.
2549 Exception: for arrays where the length is not specified,
2550 the type is left null, to be filled in by `finish_decl'.
2552 Function definitions do not come here; they go to start_function
2553 instead. However, external and forward declarations of functions
2554 do go through here. Structure field declarations are done by
2555 grokfield and not through here. */
2557 tree
2558 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2560 tree decl;
2561 tree tem;
2563 /* An object declared as __attribute__((deprecated)) suppresses
2564 warnings of uses of other deprecated items. */
2565 if (lookup_attribute ("deprecated", attributes))
2566 deprecated_state = DEPRECATED_SUPPRESS;
2568 decl = grokdeclarator (declarator, declspecs,
2569 NORMAL, initialized);
2571 deprecated_state = DEPRECATED_NORMAL;
2573 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2574 && MAIN_NAME_P (DECL_NAME (decl)))
2575 warning ("%H'%D' is usually a function",
2576 &DECL_SOURCE_LOCATION (decl), decl);
2578 if (initialized)
2579 /* Is it valid for this decl to have an initializer at all?
2580 If not, set INITIALIZED to zero, which will indirectly
2581 tell `finish_decl' to ignore the initializer once it is parsed. */
2582 switch (TREE_CODE (decl))
2584 case TYPE_DECL:
2585 error ("typedef `%s' is initialized (use __typeof__ instead)",
2586 IDENTIFIER_POINTER (DECL_NAME (decl)));
2587 initialized = 0;
2588 break;
2590 case FUNCTION_DECL:
2591 error ("function `%s' is initialized like a variable",
2592 IDENTIFIER_POINTER (DECL_NAME (decl)));
2593 initialized = 0;
2594 break;
2596 case PARM_DECL:
2597 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2598 error ("parameter `%s' is initialized",
2599 IDENTIFIER_POINTER (DECL_NAME (decl)));
2600 initialized = 0;
2601 break;
2603 default:
2604 /* Don't allow initializations for incomplete types
2605 except for arrays which might be completed by the initialization. */
2607 /* This can happen if the array size is an undefined macro. We already
2608 gave a warning, so we don't need another one. */
2609 if (TREE_TYPE (decl) == error_mark_node)
2610 initialized = 0;
2611 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2613 /* A complete type is ok if size is fixed. */
2615 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2616 || C_DECL_VARIABLE_SIZE (decl))
2618 error ("variable-sized object may not be initialized");
2619 initialized = 0;
2622 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2624 error ("variable `%s' has initializer but incomplete type",
2625 IDENTIFIER_POINTER (DECL_NAME (decl)));
2626 initialized = 0;
2628 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2630 error ("elements of array `%s' have incomplete type",
2631 IDENTIFIER_POINTER (DECL_NAME (decl)));
2632 initialized = 0;
2636 if (initialized)
2638 DECL_EXTERNAL (decl) = 0;
2639 if (current_scope == global_scope)
2640 TREE_STATIC (decl) = 1;
2642 /* Tell `pushdecl' this is an initialized decl
2643 even though we don't yet have the initializer expression.
2644 Also tell `finish_decl' it may store the real initializer. */
2645 DECL_INITIAL (decl) = error_mark_node;
2648 /* If this is a function declaration, write a record describing it to the
2649 prototypes file (if requested). */
2651 if (TREE_CODE (decl) == FUNCTION_DECL)
2652 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2654 /* ANSI specifies that a tentative definition which is not merged with
2655 a non-tentative definition behaves exactly like a definition with an
2656 initializer equal to zero. (Section 3.7.2)
2658 -fno-common gives strict ANSI behavior, though this tends to break
2659 a large body of code that grew up without this rule.
2661 Thread-local variables are never common, since there's no entrenched
2662 body of code to break, and it allows more efficient variable references
2663 in the presence of dynamic linking. */
2665 if (TREE_CODE (decl) == VAR_DECL
2666 && !initialized
2667 && TREE_PUBLIC (decl)
2668 && !DECL_THREAD_LOCAL (decl)
2669 && !flag_no_common)
2670 DECL_COMMON (decl) = 1;
2672 /* Set attributes here so if duplicate decl, will have proper attributes. */
2673 decl_attributes (&decl, attributes, 0);
2675 if (TREE_CODE (decl) == FUNCTION_DECL
2676 && DECL_DECLARED_INLINE_P (decl)
2677 && DECL_UNINLINABLE (decl)
2678 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2679 warning ("%Hinline function '%D' given attribute noinline",
2680 &DECL_SOURCE_LOCATION (decl), decl);
2682 /* Add this decl to the current scope.
2683 TEM may equal DECL or it may be a previous decl of the same name. */
2684 tem = pushdecl (decl);
2686 /* For a local variable, define the RTL now. */
2687 if (current_scope != global_scope
2688 /* But not if this is a duplicate decl
2689 and we preserved the rtl from the previous one
2690 (which may or may not happen). */
2691 && !DECL_RTL_SET_P (tem)
2692 && C_DECL_FILE_SCOPE (tem))
2694 if (TREE_TYPE (tem) != error_mark_node
2695 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2696 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2697 && DECL_INITIAL (tem) != 0)))
2698 expand_decl (tem);
2701 return tem;
2704 /* Finish processing of a declaration;
2705 install its initial value.
2706 If the length of an array type is not known before,
2707 it must be determined now, from the initial value, or it is an error. */
2709 void
2710 finish_decl (tree decl, tree init, tree asmspec_tree)
2712 tree type = TREE_TYPE (decl);
2713 int was_incomplete = (DECL_SIZE (decl) == 0);
2714 const char *asmspec = 0;
2716 /* If a name was specified, get the string. */
2717 if (current_scope == global_scope)
2718 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2719 if (asmspec_tree)
2720 asmspec = TREE_STRING_POINTER (asmspec_tree);
2722 /* If `start_decl' didn't like having an initialization, ignore it now. */
2723 if (init != 0 && DECL_INITIAL (decl) == 0)
2724 init = 0;
2726 /* Don't crash if parm is initialized. */
2727 if (TREE_CODE (decl) == PARM_DECL)
2728 init = 0;
2730 if (init)
2731 store_init_value (decl, init);
2733 /* Deduce size of array from initialization, if not already known */
2734 if (TREE_CODE (type) == ARRAY_TYPE
2735 && TYPE_DOMAIN (type) == 0
2736 && TREE_CODE (decl) != TYPE_DECL)
2738 int do_default
2739 = (TREE_STATIC (decl)
2740 /* Even if pedantic, an external linkage array
2741 may have incomplete type at first. */
2742 ? pedantic && !TREE_PUBLIC (decl)
2743 : !DECL_EXTERNAL (decl));
2744 int failure
2745 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2747 /* Get the completed type made by complete_array_type. */
2748 type = TREE_TYPE (decl);
2750 if (failure == 1)
2751 error ("%Hinitializer fails to determine size of '%D'",
2752 &DECL_SOURCE_LOCATION (decl), decl);
2754 else if (failure == 2)
2756 if (do_default)
2757 error ("%Harray size missing in '%D'",
2758 &DECL_SOURCE_LOCATION (decl), decl);
2759 /* If a `static' var's size isn't known,
2760 make it extern as well as static, so it does not get
2761 allocated.
2762 If it is not `static', then do not mark extern;
2763 finish_incomplete_decl will give it a default size
2764 and it will get allocated. */
2765 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2766 DECL_EXTERNAL (decl) = 1;
2769 /* TYPE_MAX_VALUE is always one less than the number of elements
2770 in the array, because we start counting at zero. Therefore,
2771 warn only if the value is less than zero. */
2772 else if (pedantic && TYPE_DOMAIN (type) != 0
2773 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2774 error ("%Hzero or negative size array '%D'",
2775 &DECL_SOURCE_LOCATION (decl), decl);
2777 layout_decl (decl, 0);
2780 if (TREE_CODE (decl) == VAR_DECL)
2782 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2783 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2784 layout_decl (decl, 0);
2786 if (DECL_SIZE (decl) == 0
2787 /* Don't give an error if we already gave one earlier. */
2788 && TREE_TYPE (decl) != error_mark_node
2789 && (TREE_STATIC (decl)
2791 /* A static variable with an incomplete type
2792 is an error if it is initialized.
2793 Also if it is not file scope.
2794 Otherwise, let it through, but if it is not `extern'
2795 then it may cause an error message later. */
2796 (DECL_INITIAL (decl) != 0
2797 || !C_DECL_FILE_SCOPE (decl))
2799 /* An automatic variable with an incomplete type
2800 is an error. */
2801 !DECL_EXTERNAL (decl)))
2803 error ("%Hstorage size of '%D' isn't known",
2804 &DECL_SOURCE_LOCATION (decl), decl);
2805 TREE_TYPE (decl) = error_mark_node;
2808 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2809 && DECL_SIZE (decl) != 0)
2811 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2812 constant_expression_warning (DECL_SIZE (decl));
2813 else
2814 error ("%Hstorage size of '%D' isn't constant",
2815 &DECL_SOURCE_LOCATION (decl), decl);
2818 if (TREE_USED (type))
2819 TREE_USED (decl) = 1;
2822 /* If this is a function and an assembler name is specified, reset DECL_RTL
2823 so we can give it its new name. Also, update built_in_decls if it
2824 was a normal built-in. */
2825 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2827 /* ASMSPEC is given, and not the name of a register. Mark the
2828 name with a star so assemble_name won't munge it. */
2829 char *starred = alloca (strlen (asmspec) + 2);
2830 starred[0] = '*';
2831 strcpy (starred + 1, asmspec);
2833 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2835 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2836 SET_DECL_RTL (builtin, NULL_RTX);
2837 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2838 #ifdef TARGET_MEM_FUNCTIONS
2839 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2840 init_block_move_fn (starred);
2841 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2842 init_block_clear_fn (starred);
2843 #else
2844 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2845 init_block_move_fn (starred);
2846 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2847 init_block_clear_fn (starred);
2848 #endif
2850 SET_DECL_RTL (decl, NULL_RTX);
2851 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (starred));
2854 /* If #pragma weak was used, mark the decl weak now. */
2855 if (current_scope == global_scope)
2856 maybe_apply_pragma_weak (decl);
2858 /* Output the assembler code and/or RTL code for variables and functions,
2859 unless the type is an undefined structure or union.
2860 If not, it will get done when the type is completed. */
2862 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2864 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2865 if (c_dialect_objc ())
2866 objc_check_decl (decl);
2868 if (C_DECL_FILE_SCOPE (decl))
2870 if (DECL_INITIAL (decl) == NULL_TREE
2871 || DECL_INITIAL (decl) == error_mark_node)
2872 /* Don't output anything
2873 when a tentative file-scope definition is seen.
2874 But at end of compilation, do output code for them. */
2875 DECL_DEFER_OUTPUT (decl) = 1;
2876 rest_of_decl_compilation (decl, asmspec, true, 0);
2878 else
2880 /* This is a local variable. If there is an ASMSPEC, the
2881 user has requested that we handle it specially. */
2882 if (asmspec)
2884 /* In conjunction with an ASMSPEC, the `register'
2885 keyword indicates that we should place the variable
2886 in a particular register. */
2887 if (DECL_REGISTER (decl))
2888 DECL_C_HARD_REGISTER (decl) = 1;
2890 /* If this is not a static variable, issue a warning.
2891 It doesn't make any sense to give an ASMSPEC for an
2892 ordinary, non-register local variable. Historically,
2893 GCC has accepted -- but ignored -- the ASMSPEC in
2894 this case. */
2895 if (TREE_CODE (decl) == VAR_DECL
2896 && !DECL_REGISTER (decl)
2897 && !TREE_STATIC (decl))
2898 warning ("%Hignoring asm-specifier for non-static local "
2899 "variable '%D'", &DECL_SOURCE_LOCATION (decl), decl);
2900 else
2901 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
2904 if (TREE_CODE (decl) != FUNCTION_DECL)
2905 add_decl_stmt (decl);
2908 if (!C_DECL_FILE_SCOPE (decl))
2910 /* Recompute the RTL of a local array now
2911 if it used to be an incomplete type. */
2912 if (was_incomplete
2913 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2915 /* If we used it already as memory, it must stay in memory. */
2916 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2917 /* If it's still incomplete now, no init will save it. */
2918 if (DECL_SIZE (decl) == 0)
2919 DECL_INITIAL (decl) = 0;
2924 /* If this was marked 'used', be sure it will be output. */
2925 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2926 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2928 if (TREE_CODE (decl) == TYPE_DECL)
2930 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2931 if (c_dialect_objc ())
2932 objc_check_decl (decl);
2933 rest_of_decl_compilation (decl, NULL, C_DECL_FILE_SCOPE (decl), 0);
2936 /* At the end of a declaration, throw away any variable type sizes
2937 of types defined inside that declaration. There is no use
2938 computing them in the following function definition. */
2939 if (current_scope == global_scope)
2940 get_pending_sizes ();
2942 /* Install a cleanup (aka destructor) if one was given. */
2943 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2945 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2946 if (attr)
2948 static bool eh_initialized_p;
2950 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2951 tree cleanup_decl = lookup_name (cleanup_id);
2952 tree cleanup;
2954 /* Build "cleanup(&decl)" for the destructor. */
2955 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2956 cleanup = build_tree_list (NULL_TREE, cleanup);
2957 cleanup = build_function_call (cleanup_decl, cleanup);
2959 /* Don't warn about decl unused; the cleanup uses it. */
2960 TREE_USED (decl) = 1;
2962 /* Initialize EH, if we've been told to do so. */
2963 if (flag_exceptions && !eh_initialized_p)
2965 eh_initialized_p = true;
2966 eh_personality_libfunc
2967 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2968 ? "__gcc_personality_sj0"
2969 : "__gcc_personality_v0");
2970 using_eh_for_cleanups ();
2973 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2978 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2979 and push that on the current scope. */
2981 void
2982 push_parm_decl (tree parm)
2984 tree decl;
2986 /* Don't attempt to expand sizes while parsing this decl.
2987 (We can get here with i_s_e 1 somehow from Objective-C.) */
2988 int save_immediate_size_expand = immediate_size_expand;
2989 immediate_size_expand = 0;
2991 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2992 TREE_PURPOSE (TREE_PURPOSE (parm)), PARM, 0);
2993 decl_attributes (&decl, TREE_VALUE (parm), 0);
2995 decl = pushdecl (decl);
2997 finish_decl (decl, NULL_TREE, NULL_TREE);
2999 immediate_size_expand = save_immediate_size_expand;
3002 /* Mark all the parameter declarations to date as forward decls,
3003 shift them to the variables list, and reset the parameters list.
3004 Also diagnose use of this extension. */
3006 void
3007 mark_forward_parm_decls (void)
3009 tree parm;
3011 if (pedantic && !current_scope->warned_forward_parm_decls)
3013 pedwarn ("ISO C forbids forward parameter declarations");
3014 current_scope->warned_forward_parm_decls = true;
3017 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3018 TREE_ASM_WRITTEN (parm) = 1;
3020 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3021 current_scope->parms = 0;
3022 current_scope->parms_last = 0;
3025 static GTY(()) int compound_literal_number;
3027 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3028 literal, which may be an incomplete array type completed by the
3029 initializer; INIT is a CONSTRUCTOR that initializes the compound
3030 literal. */
3032 tree
3033 build_compound_literal (tree type, tree init)
3035 /* We do not use start_decl here because we have a type, not a declarator;
3036 and do not use finish_decl because the decl should be stored inside
3037 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3038 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3039 tree complit;
3040 tree stmt;
3041 DECL_EXTERNAL (decl) = 0;
3042 TREE_PUBLIC (decl) = 0;
3043 TREE_STATIC (decl) = (current_scope == global_scope);
3044 DECL_CONTEXT (decl) = current_function_decl;
3045 TREE_USED (decl) = 1;
3046 TREE_TYPE (decl) = type;
3047 TREE_READONLY (decl) = TREE_READONLY (type);
3048 store_init_value (decl, init);
3050 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3052 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3053 if (failure)
3054 abort ();
3057 type = TREE_TYPE (decl);
3058 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3059 return error_mark_node;
3061 stmt = build_stmt (DECL_STMT, decl);
3062 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3063 TREE_SIDE_EFFECTS (complit) = 1;
3065 layout_decl (decl, 0);
3067 if (TREE_STATIC (decl))
3069 /* This decl needs a name for the assembler output. We also need
3070 a unique suffix to be added to the name. */
3071 char *name;
3073 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3074 compound_literal_number);
3075 compound_literal_number++;
3076 DECL_NAME (decl) = get_identifier (name);
3077 DECL_DEFER_OUTPUT (decl) = 1;
3078 DECL_COMDAT (decl) = 1;
3079 DECL_ARTIFICIAL (decl) = 1;
3080 pushdecl (decl);
3081 rest_of_decl_compilation (decl, NULL, 1, 0);
3084 return complit;
3087 /* Make TYPE a complete type based on INITIAL_VALUE.
3088 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3089 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3092 complete_array_type (tree type, tree initial_value, int do_default)
3094 tree maxindex = NULL_TREE;
3095 int value = 0;
3097 if (initial_value)
3099 /* Note MAXINDEX is really the maximum index,
3100 one less than the size. */
3101 if (TREE_CODE (initial_value) == STRING_CST)
3103 int eltsize
3104 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3105 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3106 / eltsize) - 1, 0);
3108 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3110 tree elts = CONSTRUCTOR_ELTS (initial_value);
3111 maxindex = build_int_2 (-1, -1);
3112 for (; elts; elts = TREE_CHAIN (elts))
3114 if (TREE_PURPOSE (elts))
3115 maxindex = TREE_PURPOSE (elts);
3116 else
3117 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3118 maxindex, integer_one_node));
3120 maxindex = copy_node (maxindex);
3122 else
3124 /* Make an error message unless that happened already. */
3125 if (initial_value != error_mark_node)
3126 value = 1;
3128 /* Prevent further error messages. */
3129 maxindex = build_int_2 (0, 0);
3133 if (!maxindex)
3135 if (do_default)
3136 maxindex = build_int_2 (0, 0);
3137 value = 2;
3140 if (maxindex)
3142 TYPE_DOMAIN (type) = build_index_type (maxindex);
3143 if (!TREE_TYPE (maxindex))
3144 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3147 /* Lay out the type now that we can get the real answer. */
3149 layout_type (type);
3151 return value;
3154 /* Determine whether TYPE is a structure with a flexible array member,
3155 or a union containing such a structure (possibly recursively). */
3157 static bool
3158 flexible_array_type_p (tree type)
3160 tree x;
3161 switch (TREE_CODE (type))
3163 case RECORD_TYPE:
3164 x = TYPE_FIELDS (type);
3165 if (x == NULL_TREE)
3166 return false;
3167 while (TREE_CHAIN (x) != NULL_TREE)
3168 x = TREE_CHAIN (x);
3169 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3170 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3171 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3172 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3173 return true;
3174 return false;
3175 case UNION_TYPE:
3176 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3178 if (flexible_array_type_p (TREE_TYPE (x)))
3179 return true;
3181 return false;
3182 default:
3183 return false;
3187 /* Given declspecs and a declarator,
3188 determine the name and type of the object declared
3189 and construct a ..._DECL node for it.
3190 (In one case we can return a ..._TYPE node instead.
3191 For invalid input we sometimes return 0.)
3193 DECLSPECS is a chain of tree_list nodes whose value fields
3194 are the storage classes and type specifiers.
3196 DECL_CONTEXT says which syntactic context this declaration is in:
3197 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3198 FUNCDEF for a function definition. Like NORMAL but a few different
3199 error messages in each case. Return value may be zero meaning
3200 this definition is too screwy to try to parse.
3201 PARM for a parameter declaration (either within a function prototype
3202 or before a function body). Make a PARM_DECL, or return void_type_node.
3203 TYPENAME if for a typename (in a cast or sizeof).
3204 Don't make a DECL node; just return the ..._TYPE node.
3205 FIELD for a struct or union field; make a FIELD_DECL.
3206 BITFIELD for a field with specified width.
3207 INITIALIZED is 1 if the decl has an initializer.
3209 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3210 It may also be so in the PARM case, for a prototype where the
3211 argument type is specified but not the name.
3213 This function is where the complicated C meanings of `static'
3214 and `extern' are interpreted. */
3216 static tree
3217 grokdeclarator (tree declarator, tree declspecs,
3218 enum decl_context decl_context, int initialized)
3220 int specbits = 0;
3221 tree spec;
3222 tree type = NULL_TREE;
3223 int longlong = 0;
3224 int constp;
3225 int restrictp;
3226 int volatilep;
3227 int type_quals = TYPE_UNQUALIFIED;
3228 int inlinep;
3229 int explicit_int = 0;
3230 int explicit_char = 0;
3231 int defaulted_int = 0;
3232 tree typedef_decl = 0;
3233 const char *name;
3234 tree typedef_type = 0;
3235 int funcdef_flag = 0;
3236 enum tree_code innermost_code = ERROR_MARK;
3237 int bitfield = 0;
3238 int size_varies = 0;
3239 tree decl_attr = NULL_TREE;
3240 tree array_ptr_quals = NULL_TREE;
3241 int array_parm_static = 0;
3242 tree returned_attrs = NULL_TREE;
3244 if (decl_context == BITFIELD)
3245 bitfield = 1, decl_context = FIELD;
3247 if (decl_context == FUNCDEF)
3248 funcdef_flag = 1, decl_context = NORMAL;
3250 /* Look inside a declarator for the name being declared
3251 and get it as a string, for an error message. */
3253 tree decl = declarator;
3254 name = 0;
3256 while (decl)
3257 switch (TREE_CODE (decl))
3259 case ARRAY_REF:
3260 case INDIRECT_REF:
3261 case CALL_EXPR:
3262 innermost_code = TREE_CODE (decl);
3263 decl = TREE_OPERAND (decl, 0);
3264 break;
3266 case TREE_LIST:
3267 decl = TREE_VALUE (decl);
3268 break;
3270 case IDENTIFIER_NODE:
3271 name = IDENTIFIER_POINTER (decl);
3272 decl = 0;
3273 break;
3275 default:
3276 abort ();
3278 if (name == 0)
3279 name = "type name";
3282 /* A function definition's declarator must have the form of
3283 a function declarator. */
3285 if (funcdef_flag && innermost_code != CALL_EXPR)
3286 return 0;
3288 /* If this looks like a function definition, make it one,
3289 even if it occurs where parms are expected.
3290 Then store_parm_decls will reject it and not use it as a parm. */
3291 if (decl_context == NORMAL && !funcdef_flag
3292 && current_scope->parm_flag)
3293 decl_context = PARM;
3295 /* Look through the decl specs and record which ones appear.
3296 Some typespecs are defined as built-in typenames.
3297 Others, the ones that are modifiers of other types,
3298 are represented by bits in SPECBITS: set the bits for
3299 the modifiers that appear. Storage class keywords are also in SPECBITS.
3301 If there is a typedef name or a type, store the type in TYPE.
3302 This includes builtin typedefs such as `int'.
3304 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3305 and did not come from a user typedef.
3307 Set LONGLONG if `long' is mentioned twice. */
3309 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3311 tree id = TREE_VALUE (spec);
3313 /* If the entire declaration is itself tagged as deprecated then
3314 suppress reports of deprecated items. */
3315 if (id && TREE_DEPRECATED (id))
3317 if (deprecated_state != DEPRECATED_SUPPRESS)
3318 warn_deprecated_use (id);
3321 if (id == ridpointers[(int) RID_INT])
3322 explicit_int = 1;
3323 if (id == ridpointers[(int) RID_CHAR])
3324 explicit_char = 1;
3326 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3328 enum rid i = C_RID_CODE (id);
3329 if ((int) i <= (int) RID_LAST_MODIFIER)
3331 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3333 if (longlong)
3334 error ("`long long long' is too long for GCC");
3335 else
3337 if (pedantic && !flag_isoc99 && ! in_system_header
3338 && warn_long_long)
3339 pedwarn ("ISO C90 does not support `long long'");
3340 longlong = 1;
3343 else if (specbits & (1 << (int) i))
3345 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3347 if (!flag_isoc99)
3348 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3350 else
3351 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3354 /* Diagnose "__thread extern". Recall that this list
3355 is in the reverse order seen in the text. */
3356 if (i == RID_THREAD
3357 && (specbits & (1 << (int) RID_EXTERN
3358 | 1 << (int) RID_STATIC)))
3360 if (specbits & 1 << (int) RID_EXTERN)
3361 error ("`__thread' before `extern'");
3362 else
3363 error ("`__thread' before `static'");
3366 specbits |= 1 << (int) i;
3367 goto found;
3370 if (type)
3371 error ("two or more data types in declaration of `%s'", name);
3372 /* Actual typedefs come to us as TYPE_DECL nodes. */
3373 else if (TREE_CODE (id) == TYPE_DECL)
3375 if (TREE_TYPE (id) == error_mark_node)
3376 ; /* Allow the type to default to int to avoid cascading errors. */
3377 else
3379 type = TREE_TYPE (id);
3380 decl_attr = DECL_ATTRIBUTES (id);
3381 typedef_decl = id;
3384 /* Built-in types come as identifiers. */
3385 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3387 tree t = lookup_name (id);
3388 if (TREE_TYPE (t) == error_mark_node)
3390 else if (!t || TREE_CODE (t) != TYPE_DECL)
3391 error ("`%s' fails to be a typedef or built in type",
3392 IDENTIFIER_POINTER (id));
3393 else
3395 type = TREE_TYPE (t);
3396 typedef_decl = t;
3399 else if (TREE_CODE (id) != ERROR_MARK)
3400 type = id;
3402 found:
3406 typedef_type = type;
3407 if (type)
3408 size_varies = C_TYPE_VARIABLE_SIZE (type);
3410 /* No type at all: default to `int', and set DEFAULTED_INT
3411 because it was not a user-defined typedef. */
3413 if (type == 0)
3415 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3416 | (1 << (int) RID_SIGNED)
3417 | (1 << (int) RID_UNSIGNED)
3418 | (1 << (int) RID_COMPLEX))))
3419 /* Don't warn about typedef foo = bar. */
3420 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3421 && ! in_system_header)
3423 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3424 and this is a function, or if -Wimplicit; prefer the former
3425 warning since it is more explicit. */
3426 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3427 && funcdef_flag)
3428 warn_about_return_type = 1;
3429 else if (warn_implicit_int || flag_isoc99)
3430 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3431 name);
3434 defaulted_int = 1;
3435 type = integer_type_node;
3438 /* Now process the modifiers that were specified
3439 and check for invalid combinations. */
3441 /* Long double is a special combination. */
3443 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3444 && TYPE_MAIN_VARIANT (type) == double_type_node)
3446 specbits &= ~(1 << (int) RID_LONG);
3447 type = long_double_type_node;
3450 /* Check all other uses of type modifiers. */
3452 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3453 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3455 int ok = 0;
3457 if ((specbits & 1 << (int) RID_LONG)
3458 && (specbits & 1 << (int) RID_SHORT))
3459 error ("both long and short specified for `%s'", name);
3460 else if (((specbits & 1 << (int) RID_LONG)
3461 || (specbits & 1 << (int) RID_SHORT))
3462 && explicit_char)
3463 error ("long or short specified with char for `%s'", name);
3464 else if (((specbits & 1 << (int) RID_LONG)
3465 || (specbits & 1 << (int) RID_SHORT))
3466 && TREE_CODE (type) == REAL_TYPE)
3468 static int already = 0;
3470 error ("long or short specified with floating type for `%s'", name);
3471 if (! already && ! pedantic)
3473 error ("the only valid combination is `long double'");
3474 already = 1;
3477 else if ((specbits & 1 << (int) RID_SIGNED)
3478 && (specbits & 1 << (int) RID_UNSIGNED))
3479 error ("both signed and unsigned specified for `%s'", name);
3480 else if (TREE_CODE (type) != INTEGER_TYPE)
3481 error ("long, short, signed or unsigned invalid for `%s'", name);
3482 else
3484 ok = 1;
3485 if (!explicit_int && !defaulted_int && !explicit_char)
3487 error ("long, short, signed or unsigned used invalidly for `%s'",
3488 name);
3489 ok = 0;
3493 /* Discard the type modifiers if they are invalid. */
3494 if (! ok)
3496 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3497 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3498 longlong = 0;
3502 if ((specbits & (1 << (int) RID_COMPLEX))
3503 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3505 error ("complex invalid for `%s'", name);
3506 specbits &= ~(1 << (int) RID_COMPLEX);
3509 /* Decide whether an integer type is signed or not.
3510 Optionally treat bitfields as signed by default. */
3511 if (specbits & 1 << (int) RID_UNSIGNED
3512 || (bitfield && ! flag_signed_bitfields
3513 && (explicit_int || defaulted_int || explicit_char
3514 /* A typedef for plain `int' without `signed'
3515 can be controlled just like plain `int'. */
3516 || ! (typedef_decl != 0
3517 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3518 && TREE_CODE (type) != ENUMERAL_TYPE
3519 && !(specbits & 1 << (int) RID_SIGNED)))
3521 if (longlong)
3522 type = long_long_unsigned_type_node;
3523 else if (specbits & 1 << (int) RID_LONG)
3524 type = long_unsigned_type_node;
3525 else if (specbits & 1 << (int) RID_SHORT)
3526 type = short_unsigned_type_node;
3527 else if (type == char_type_node)
3528 type = unsigned_char_type_node;
3529 else if (typedef_decl)
3530 type = c_common_unsigned_type (type);
3531 else
3532 type = unsigned_type_node;
3534 else if ((specbits & 1 << (int) RID_SIGNED)
3535 && type == char_type_node)
3536 type = signed_char_type_node;
3537 else if (longlong)
3538 type = long_long_integer_type_node;
3539 else if (specbits & 1 << (int) RID_LONG)
3540 type = long_integer_type_node;
3541 else if (specbits & 1 << (int) RID_SHORT)
3542 type = short_integer_type_node;
3544 if (specbits & 1 << (int) RID_COMPLEX)
3546 if (pedantic && !flag_isoc99)
3547 pedwarn ("ISO C90 does not support complex types");
3548 /* If we just have "complex", it is equivalent to
3549 "complex double", but if any modifiers at all are specified it is
3550 the complex form of TYPE. E.g, "complex short" is
3551 "complex short int". */
3553 if (defaulted_int && ! longlong
3554 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3555 | (1 << (int) RID_SIGNED)
3556 | (1 << (int) RID_UNSIGNED))))
3558 if (pedantic)
3559 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3560 type = complex_double_type_node;
3562 else if (type == integer_type_node)
3564 if (pedantic)
3565 pedwarn ("ISO C does not support complex integer types");
3566 type = complex_integer_type_node;
3568 else if (type == float_type_node)
3569 type = complex_float_type_node;
3570 else if (type == double_type_node)
3571 type = complex_double_type_node;
3572 else if (type == long_double_type_node)
3573 type = complex_long_double_type_node;
3574 else
3576 if (pedantic)
3577 pedwarn ("ISO C does not support complex integer types");
3578 type = build_complex_type (type);
3582 /* Figure out the type qualifiers for the declaration. There are
3583 two ways a declaration can become qualified. One is something
3584 like `const int i' where the `const' is explicit. Another is
3585 something like `typedef const int CI; CI i' where the type of the
3586 declaration contains the `const'. */
3587 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3588 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3589 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3590 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3591 if (constp > 1 && ! flag_isoc99)
3592 pedwarn ("duplicate `const'");
3593 if (restrictp > 1 && ! flag_isoc99)
3594 pedwarn ("duplicate `restrict'");
3595 if (volatilep > 1 && ! flag_isoc99)
3596 pedwarn ("duplicate `volatile'");
3597 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3598 type = TYPE_MAIN_VARIANT (type);
3599 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3600 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3601 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3603 /* Warn if two storage classes are given. Default to `auto'. */
3606 int nclasses = 0;
3608 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3609 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3610 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3611 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3612 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3614 /* "static __thread" and "extern __thread" are allowed. */
3615 if ((specbits & (1 << (int) RID_THREAD
3616 | 1 << (int) RID_STATIC
3617 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3618 nclasses++;
3620 /* Warn about storage classes that are invalid for certain
3621 kinds of declarations (parameters, typenames, etc.). */
3623 if (nclasses > 1)
3624 error ("multiple storage classes in declaration of `%s'", name);
3625 else if (funcdef_flag
3626 && (specbits
3627 & ((1 << (int) RID_REGISTER)
3628 | (1 << (int) RID_AUTO)
3629 | (1 << (int) RID_TYPEDEF)
3630 | (1 << (int) RID_THREAD))))
3632 if (specbits & 1 << (int) RID_AUTO
3633 && (pedantic || current_scope == global_scope))
3634 pedwarn ("function definition declared `auto'");
3635 if (specbits & 1 << (int) RID_REGISTER)
3636 error ("function definition declared `register'");
3637 if (specbits & 1 << (int) RID_TYPEDEF)
3638 error ("function definition declared `typedef'");
3639 if (specbits & 1 << (int) RID_THREAD)
3640 error ("function definition declared `__thread'");
3641 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3642 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3644 else if (decl_context != NORMAL && nclasses > 0)
3646 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3648 else
3650 switch (decl_context)
3652 case FIELD:
3653 error ("storage class specified for structure field `%s'",
3654 name);
3655 break;
3656 case PARM:
3657 error ("storage class specified for parameter `%s'", name);
3658 break;
3659 default:
3660 error ("storage class specified for typename");
3661 break;
3663 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3664 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3665 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3668 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3670 /* `extern' with initialization is invalid if not at file scope. */
3671 if (current_scope == global_scope)
3672 warning ("`%s' initialized and declared `extern'", name);
3673 else
3674 error ("`%s' has both `extern' and initializer", name);
3676 else if (current_scope == global_scope)
3678 if (specbits & 1 << (int) RID_AUTO)
3679 error ("file-scope declaration of `%s' specifies `auto'", name);
3681 else
3683 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3684 error ("nested function `%s' declared `extern'", name);
3685 else if ((specbits & (1 << (int) RID_THREAD
3686 | 1 << (int) RID_EXTERN
3687 | 1 << (int) RID_STATIC))
3688 == (1 << (int) RID_THREAD))
3690 error ("function-scope `%s' implicitly auto and declared `__thread'",
3691 name);
3692 specbits &= ~(1 << (int) RID_THREAD);
3697 /* Now figure out the structure of the declarator proper.
3698 Descend through it, creating more complex types, until we reach
3699 the declared identifier (or NULL_TREE, in an absolute declarator). */
3701 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3703 if (type == error_mark_node)
3705 declarator = TREE_OPERAND (declarator, 0);
3706 continue;
3709 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3710 an INDIRECT_REF (for *...),
3711 a CALL_EXPR (for ...(...)),
3712 a TREE_LIST (for nested attributes),
3713 an identifier (for the name being declared)
3714 or a null pointer (for the place in an absolute declarator
3715 where the name was omitted).
3716 For the last two cases, we have just exited the loop.
3718 At this point, TYPE is the type of elements of an array,
3719 or for a function to return, or for a pointer to point to.
3720 After this sequence of ifs, TYPE is the type of the
3721 array or function or pointer, and DECLARATOR has had its
3722 outermost layer removed. */
3724 if (array_ptr_quals != NULL_TREE || array_parm_static)
3726 /* Only the innermost declarator (making a parameter be of
3727 array type which is converted to pointer type)
3728 may have static or type qualifiers. */
3729 error ("static or type qualifiers in non-parameter array declarator");
3730 array_ptr_quals = NULL_TREE;
3731 array_parm_static = 0;
3734 if (TREE_CODE (declarator) == TREE_LIST)
3736 /* We encode a declarator with embedded attributes using
3737 a TREE_LIST. */
3738 tree attrs = TREE_PURPOSE (declarator);
3739 tree inner_decl;
3740 int attr_flags = 0;
3741 declarator = TREE_VALUE (declarator);
3742 inner_decl = declarator;
3743 while (inner_decl != NULL_TREE
3744 && TREE_CODE (inner_decl) == TREE_LIST)
3745 inner_decl = TREE_VALUE (inner_decl);
3746 if (inner_decl == NULL_TREE
3747 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3748 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3749 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3750 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3751 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3752 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3753 returned_attrs = decl_attributes (&type,
3754 chainon (returned_attrs, attrs),
3755 attr_flags);
3757 else if (TREE_CODE (declarator) == ARRAY_REF)
3759 tree itype = NULL_TREE;
3760 tree size = TREE_OPERAND (declarator, 1);
3761 /* The index is a signed object `sizetype' bits wide. */
3762 tree index_type = c_common_signed_type (sizetype);
3764 array_ptr_quals = TREE_TYPE (declarator);
3765 array_parm_static = TREE_STATIC (declarator);
3767 declarator = TREE_OPERAND (declarator, 0);
3769 /* Check for some types that there cannot be arrays of. */
3771 if (VOID_TYPE_P (type))
3773 error ("declaration of `%s' as array of voids", name);
3774 type = error_mark_node;
3777 if (TREE_CODE (type) == FUNCTION_TYPE)
3779 error ("declaration of `%s' as array of functions", name);
3780 type = error_mark_node;
3783 if (pedantic && flexible_array_type_p (type))
3784 pedwarn ("invalid use of structure with flexible array member");
3786 if (size == error_mark_node)
3787 type = error_mark_node;
3789 if (type == error_mark_node)
3790 continue;
3792 /* If size was specified, set ITYPE to a range-type for that size.
3793 Otherwise, ITYPE remains null. finish_decl may figure it out
3794 from an initial value. */
3796 if (size)
3798 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3799 STRIP_TYPE_NOPS (size);
3801 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3803 error ("size of array `%s' has non-integer type", name);
3804 size = integer_one_node;
3807 if (pedantic && integer_zerop (size))
3808 pedwarn ("ISO C forbids zero-size array `%s'", name);
3810 if (TREE_CODE (size) == INTEGER_CST)
3812 constant_expression_warning (size);
3813 if (tree_int_cst_sgn (size) < 0)
3815 error ("size of array `%s' is negative", name);
3816 size = integer_one_node;
3819 else
3821 /* Make sure the array size remains visibly nonconstant
3822 even if it is (eg) a const variable with known value. */
3823 size_varies = 1;
3825 if (!flag_isoc99 && pedantic)
3827 if (TREE_CONSTANT (size))
3828 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3829 name);
3830 else
3831 pedwarn ("ISO C90 forbids variable-size array `%s'",
3832 name);
3836 if (integer_zerop (size))
3838 /* A zero-length array cannot be represented with an
3839 unsigned index type, which is what we'll get with
3840 build_index_type. Create an open-ended range instead. */
3841 itype = build_range_type (sizetype, size, NULL_TREE);
3843 else
3845 /* Compute the maximum valid index, that is, size - 1.
3846 Do the calculation in index_type, so that if it is
3847 a variable the computations will be done in the
3848 proper mode. */
3849 itype = fold (build (MINUS_EXPR, index_type,
3850 convert (index_type, size),
3851 convert (index_type, size_one_node)));
3853 /* If that overflowed, the array is too big.
3854 ??? While a size of INT_MAX+1 technically shouldn't
3855 cause an overflow (because we subtract 1), the overflow
3856 is recorded during the conversion to index_type, before
3857 the subtraction. Handling this case seems like an
3858 unnecessary complication. */
3859 if (TREE_OVERFLOW (itype))
3861 error ("size of array `%s' is too large", name);
3862 type = error_mark_node;
3863 continue;
3866 if (size_varies)
3868 /* We must be able to distinguish the
3869 SAVE_EXPR_CONTEXT for the variably-sized type
3870 so that we can set it correctly in
3871 set_save_expr_context. The convention is
3872 that all SAVE_EXPRs that need to be reset
3873 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3874 tree cfd = current_function_decl;
3875 if (decl_context == PARM)
3876 current_function_decl = NULL_TREE;
3877 itype = variable_size (itype);
3878 if (decl_context == PARM)
3879 current_function_decl = cfd;
3881 itype = build_index_type (itype);
3884 else if (decl_context == FIELD)
3886 if (pedantic && !flag_isoc99 && !in_system_header)
3887 pedwarn ("ISO C90 does not support flexible array members");
3889 /* ISO C99 Flexible array members are effectively identical
3890 to GCC's zero-length array extension. */
3891 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3894 /* If pedantic, complain about arrays of incomplete types. */
3896 if (pedantic && !COMPLETE_TYPE_P (type))
3897 pedwarn ("array type has incomplete element type");
3899 /* Build the array type itself, then merge any constancy or
3900 volatility into the target type. We must do it in this order
3901 to ensure that the TYPE_MAIN_VARIANT field of the array type
3902 is set correctly. */
3904 type = build_array_type (type, itype);
3905 if (type_quals)
3906 type = c_build_qualified_type (type, type_quals);
3908 if (size_varies)
3909 C_TYPE_VARIABLE_SIZE (type) = 1;
3911 /* The GCC extension for zero-length arrays differs from
3912 ISO flexible array members in that sizeof yields zero. */
3913 if (size && integer_zerop (size))
3915 layout_type (type);
3916 TYPE_SIZE (type) = bitsize_zero_node;
3917 TYPE_SIZE_UNIT (type) = size_zero_node;
3919 if (decl_context != PARM
3920 && (array_ptr_quals != NULL_TREE || array_parm_static))
3922 error ("static or type qualifiers in non-parameter array declarator");
3923 array_ptr_quals = NULL_TREE;
3924 array_parm_static = 0;
3927 else if (TREE_CODE (declarator) == CALL_EXPR)
3929 tree arg_types;
3931 /* Declaring a function type.
3932 Make sure we have a valid type for the function to return. */
3933 if (type == error_mark_node)
3934 continue;
3936 size_varies = 0;
3938 /* Warn about some types functions can't return. */
3940 if (TREE_CODE (type) == FUNCTION_TYPE)
3942 error ("`%s' declared as function returning a function", name);
3943 type = integer_type_node;
3945 if (TREE_CODE (type) == ARRAY_TYPE)
3947 error ("`%s' declared as function returning an array", name);
3948 type = integer_type_node;
3951 /* Construct the function type and go to the next
3952 inner layer of declarator. */
3954 arg_types = grokparms (TREE_OPERAND (declarator, 1),
3955 funcdef_flag
3956 /* Say it's a definition
3957 only for the CALL_EXPR
3958 closest to the identifier. */
3959 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
3960 /* Type qualifiers before the return type of the function
3961 qualify the return type, not the function type. */
3962 if (type_quals)
3964 /* Type qualifiers on a function return type are normally
3965 permitted by the standard but have no effect, so give a
3966 warning at -Wextra. Qualifiers on a void return type have
3967 meaning as a GNU extension, and are banned on function
3968 definitions in ISO C. FIXME: strictly we shouldn't
3969 pedwarn for qualified void return types except on function
3970 definitions, but not doing so could lead to the undesirable
3971 state of a "volatile void" function return type not being
3972 warned about, and a use of the function being compiled
3973 with GNU semantics, with no diagnostics under -pedantic. */
3974 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
3975 pedwarn ("ISO C forbids qualified void function return type");
3976 else if (extra_warnings
3977 && !(VOID_TYPE_P (type)
3978 && type_quals == TYPE_QUAL_VOLATILE))
3979 warning ("type qualifiers ignored on function return type");
3981 type = c_build_qualified_type (type, type_quals);
3983 type_quals = TYPE_UNQUALIFIED;
3985 type = build_function_type (type, arg_types);
3986 declarator = TREE_OPERAND (declarator, 0);
3988 /* Set the TYPE_CONTEXTs for each tagged type which is local to
3989 the formal parameter list of this FUNCTION_TYPE to point to
3990 the FUNCTION_TYPE node itself. */
3993 tree link;
3995 for (link = last_function_parm_tags;
3996 link;
3997 link = TREE_CHAIN (link))
3998 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4001 else if (TREE_CODE (declarator) == INDIRECT_REF)
4003 /* Merge any constancy or volatility into the target type
4004 for the pointer. */
4006 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4007 && type_quals)
4008 pedwarn ("ISO C forbids qualified function types");
4009 if (type_quals)
4010 type = c_build_qualified_type (type, type_quals);
4011 type_quals = TYPE_UNQUALIFIED;
4012 size_varies = 0;
4014 type = build_pointer_type (type);
4016 /* Process a list of type modifier keywords
4017 (such as const or volatile) that were given inside the `*'. */
4019 if (TREE_TYPE (declarator))
4021 tree typemodlist;
4022 int erred = 0;
4024 constp = 0;
4025 volatilep = 0;
4026 restrictp = 0;
4027 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4028 typemodlist = TREE_CHAIN (typemodlist))
4030 tree qualifier = TREE_VALUE (typemodlist);
4032 if (C_IS_RESERVED_WORD (qualifier))
4034 if (C_RID_CODE (qualifier) == RID_CONST)
4035 constp++;
4036 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4037 volatilep++;
4038 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4039 restrictp++;
4040 else
4041 erred++;
4043 else
4044 erred++;
4047 if (erred)
4048 error ("invalid type modifier within pointer declarator");
4049 if (constp > 1 && ! flag_isoc99)
4050 pedwarn ("duplicate `const'");
4051 if (volatilep > 1 && ! flag_isoc99)
4052 pedwarn ("duplicate `volatile'");
4053 if (restrictp > 1 && ! flag_isoc99)
4054 pedwarn ("duplicate `restrict'");
4056 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4057 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4058 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4061 declarator = TREE_OPERAND (declarator, 0);
4063 else
4064 abort ();
4068 /* Now TYPE has the actual type. */
4070 /* Did array size calculations overflow? */
4072 if (TREE_CODE (type) == ARRAY_TYPE
4073 && COMPLETE_TYPE_P (type)
4074 && TREE_OVERFLOW (TYPE_SIZE (type)))
4076 error ("size of array `%s' is too large", name);
4077 /* If we proceed with the array type as it is, we'll eventually
4078 crash in tree_low_cst(). */
4079 type = error_mark_node;
4082 /* If this is declaring a typedef name, return a TYPE_DECL. */
4084 if (specbits & (1 << (int) RID_TYPEDEF))
4086 tree decl;
4087 /* Note that the grammar rejects storage classes
4088 in typenames, fields or parameters */
4089 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4090 && type_quals)
4091 pedwarn ("ISO C forbids qualified function types");
4092 if (type_quals)
4093 type = c_build_qualified_type (type, type_quals);
4094 decl = build_decl (TYPE_DECL, declarator, type);
4095 if ((specbits & (1 << (int) RID_SIGNED))
4096 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4097 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4098 decl_attributes (&decl, returned_attrs, 0);
4099 return decl;
4102 /* Detect the case of an array type of unspecified size
4103 which came, as such, direct from a typedef name.
4104 We must copy the type, so that each identifier gets
4105 a distinct type, so that each identifier's size can be
4106 controlled separately by its own initializer. */
4108 if (type != 0 && typedef_type != 0
4109 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4110 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4112 type = build_array_type (TREE_TYPE (type), 0);
4113 if (size_varies)
4114 C_TYPE_VARIABLE_SIZE (type) = 1;
4117 /* If this is a type name (such as, in a cast or sizeof),
4118 compute the type and return it now. */
4120 if (decl_context == TYPENAME)
4122 /* Note that the grammar rejects storage classes
4123 in typenames, fields or parameters */
4124 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4125 && type_quals)
4126 pedwarn ("ISO C forbids const or volatile function types");
4127 if (type_quals)
4128 type = c_build_qualified_type (type, type_quals);
4129 decl_attributes (&type, returned_attrs, 0);
4130 return type;
4133 /* Aside from typedefs and type names (handle above),
4134 `void' at top level (not within pointer)
4135 is allowed only in public variables.
4136 We don't complain about parms either, but that is because
4137 a better error message can be made later. */
4139 if (VOID_TYPE_P (type) && decl_context != PARM
4140 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4141 && ((specbits & (1 << (int) RID_EXTERN))
4142 || (current_scope == global_scope
4143 && !(specbits
4144 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4146 error ("variable or field `%s' declared void", name);
4147 type = integer_type_node;
4150 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4151 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4154 tree decl;
4156 if (decl_context == PARM)
4158 tree type_as_written;
4159 tree promoted_type;
4161 /* A parameter declared as an array of T is really a pointer to T.
4162 One declared as a function is really a pointer to a function. */
4164 if (TREE_CODE (type) == ARRAY_TYPE)
4166 /* Transfer const-ness of array into that of type pointed to. */
4167 type = TREE_TYPE (type);
4168 if (type_quals)
4169 type = c_build_qualified_type (type, type_quals);
4170 type = build_pointer_type (type);
4171 type_quals = TYPE_UNQUALIFIED;
4172 if (array_ptr_quals)
4174 tree new_ptr_quals, new_ptr_attrs;
4175 int erred = 0;
4176 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4177 /* We don't yet implement attributes in this context. */
4178 if (new_ptr_attrs != NULL_TREE)
4179 warning ("attributes in parameter array declarator ignored");
4181 constp = 0;
4182 volatilep = 0;
4183 restrictp = 0;
4184 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4186 tree qualifier = TREE_VALUE (new_ptr_quals);
4188 if (C_IS_RESERVED_WORD (qualifier))
4190 if (C_RID_CODE (qualifier) == RID_CONST)
4191 constp++;
4192 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4193 volatilep++;
4194 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4195 restrictp++;
4196 else
4197 erred++;
4199 else
4200 erred++;
4203 if (erred)
4204 error ("invalid type modifier within array declarator");
4206 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4207 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4208 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4210 size_varies = 0;
4212 else if (TREE_CODE (type) == FUNCTION_TYPE)
4214 if (pedantic && type_quals)
4215 pedwarn ("ISO C forbids qualified function types");
4216 if (type_quals)
4217 type = c_build_qualified_type (type, type_quals);
4218 type = build_pointer_type (type);
4219 type_quals = TYPE_UNQUALIFIED;
4221 else if (type_quals)
4222 type = c_build_qualified_type (type, type_quals);
4224 type_as_written = type;
4226 decl = build_decl (PARM_DECL, declarator, type);
4227 if (size_varies)
4228 C_DECL_VARIABLE_SIZE (decl) = 1;
4230 /* Compute the type actually passed in the parmlist,
4231 for the case where there is no prototype.
4232 (For example, shorts and chars are passed as ints.)
4233 When there is a prototype, this is overridden later. */
4235 if (type == error_mark_node)
4236 promoted_type = type;
4237 else
4238 promoted_type = c_type_promotes_to (type);
4240 DECL_ARG_TYPE (decl) = promoted_type;
4241 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4243 else if (decl_context == FIELD)
4245 /* Structure field. It may not be a function. */
4247 if (TREE_CODE (type) == FUNCTION_TYPE)
4249 error ("field `%s' declared as a function", name);
4250 type = build_pointer_type (type);
4252 else if (TREE_CODE (type) != ERROR_MARK
4253 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4255 error ("field `%s' has incomplete type", name);
4256 type = error_mark_node;
4258 /* Move type qualifiers down to element of an array. */
4259 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4260 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4261 type_quals),
4262 TYPE_DOMAIN (type));
4263 decl = build_decl (FIELD_DECL, declarator, type);
4264 DECL_NONADDRESSABLE_P (decl) = bitfield;
4266 if (size_varies)
4267 C_DECL_VARIABLE_SIZE (decl) = 1;
4269 else if (TREE_CODE (type) == FUNCTION_TYPE)
4271 /* Every function declaration is "external"
4272 except for those which are inside a function body
4273 in which `auto' is used.
4274 That is a case not specified by ANSI C,
4275 and we use it for forward declarations for nested functions. */
4276 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4277 || current_scope == global_scope);
4279 if (specbits & (1 << (int) RID_AUTO)
4280 && (pedantic || current_scope == global_scope))
4281 pedwarn ("invalid storage class for function `%s'", name);
4282 if (specbits & (1 << (int) RID_REGISTER))
4283 error ("invalid storage class for function `%s'", name);
4284 if (specbits & (1 << (int) RID_THREAD))
4285 error ("invalid storage class for function `%s'", name);
4286 /* Function declaration not at file scope.
4287 Storage classes other than `extern' are not allowed
4288 and `extern' makes no difference. */
4289 if (current_scope != global_scope
4290 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4291 && pedantic)
4292 pedwarn ("invalid storage class for function `%s'", name);
4294 decl = build_decl (FUNCTION_DECL, declarator, type);
4295 decl = build_decl_attribute_variant (decl, decl_attr);
4297 DECL_LANG_SPECIFIC (decl)
4298 = ggc_alloc_cleared (sizeof (struct lang_decl));
4300 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4301 pedwarn ("ISO C forbids qualified function types");
4303 /* GNU C interprets a `volatile void' return type to indicate
4304 that the function does not return. */
4305 if ((type_quals & TYPE_QUAL_VOLATILE)
4306 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4307 warning ("`noreturn' function returns non-void value");
4309 if (extern_ref)
4310 DECL_EXTERNAL (decl) = 1;
4311 /* Record absence of global scope for `static' or `auto'. */
4312 TREE_PUBLIC (decl)
4313 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4315 if (defaulted_int)
4316 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4318 /* Record presence of `inline', if it is reasonable. */
4319 if (MAIN_NAME_P (declarator))
4321 if (inlinep)
4322 warning ("cannot inline function `main'");
4324 else if (inlinep)
4326 /* Record that the function is declared `inline'. */
4327 DECL_DECLARED_INLINE_P (decl) = 1;
4329 /* Do not mark bare declarations as DECL_INLINE. Doing so
4330 in the presence of multiple declarations can result in
4331 the abstract origin pointing between the declarations,
4332 which will confuse dwarf2out. */
4333 if (initialized)
4335 DECL_INLINE (decl) = 1;
4336 if (specbits & (1 << (int) RID_EXTERN))
4337 current_extern_inline = 1;
4340 /* If -finline-functions, assume it can be inlined. This does
4341 two things: let the function be deferred until it is actually
4342 needed, and let dwarf2 know that the function is inlinable. */
4343 else if (flag_inline_trees == 2 && initialized)
4344 DECL_INLINE (decl) = 1;
4346 else
4348 /* It's a variable. */
4349 /* An uninitialized decl with `extern' is a reference. */
4350 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4352 /* Move type qualifiers down to element of an array. */
4353 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4355 int saved_align = TYPE_ALIGN(type);
4356 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4357 type_quals),
4358 TYPE_DOMAIN (type));
4359 TYPE_ALIGN (type) = saved_align;
4361 else if (type_quals)
4362 type = c_build_qualified_type (type, type_quals);
4364 /* It is invalid to create an `extern' declaration for a
4365 variable if there is a global declaration that is
4366 `static'. */
4367 if (extern_ref && current_scope != global_scope)
4369 tree global_decl;
4371 global_decl = identifier_global_value (declarator);
4372 if (global_decl
4373 && TREE_CODE (global_decl) == VAR_DECL
4374 && !TREE_PUBLIC (global_decl))
4375 error ("variable previously declared `static' redeclared "
4376 "`extern'");
4379 decl = build_decl (VAR_DECL, declarator, type);
4380 if (size_varies)
4381 C_DECL_VARIABLE_SIZE (decl) = 1;
4383 if (inlinep)
4384 pedwarn ("%Hvariable '%D' declared `inline'",
4385 &DECL_SOURCE_LOCATION (decl), decl);
4387 DECL_EXTERNAL (decl) = extern_ref;
4389 /* At file scope, the presence of a `static' or `register' storage
4390 class specifier, or the absence of all storage class specifiers
4391 makes this declaration a definition (perhaps tentative). Also,
4392 the absence of both `static' and `register' makes it public. */
4393 if (current_scope == global_scope)
4395 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4396 | (1 << (int) RID_REGISTER)));
4397 TREE_STATIC (decl) = !extern_ref;
4399 /* Not at file scope, only `static' makes a static definition. */
4400 else
4402 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4403 TREE_PUBLIC (decl) = extern_ref;
4406 if (specbits & 1 << (int) RID_THREAD)
4408 if (targetm.have_tls)
4409 DECL_THREAD_LOCAL (decl) = 1;
4410 else
4411 /* A mere warning is sure to result in improper semantics
4412 at runtime. Don't bother to allow this to compile. */
4413 error ("thread-local storage not supported for this target");
4417 /* Record `register' declaration for warnings on &
4418 and in case doing stupid register allocation. */
4420 if (specbits & (1 << (int) RID_REGISTER))
4421 DECL_REGISTER (decl) = 1;
4423 /* Record constancy and volatility. */
4424 c_apply_type_quals_to_decl (type_quals, decl);
4426 /* If a type has volatile components, it should be stored in memory.
4427 Otherwise, the fact that those components are volatile
4428 will be ignored, and would even crash the compiler. */
4429 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4430 c_mark_addressable (decl);
4432 #ifdef ENABLE_CHECKING
4433 /* This is the earliest point at which we might know the assembler
4434 name of a variable. Thus, if it's known before this, die horribly. */
4435 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4436 abort ();
4437 #endif
4439 decl_attributes (&decl, returned_attrs, 0);
4441 return decl;
4445 /* Decode the parameter-list info for a function type or function definition.
4446 The argument is the value returned by `get_parm_info' (or made in parse.y
4447 if there is an identifier list instead of a parameter decl list).
4448 These two functions are separate because when a function returns
4449 or receives functions then each is called multiple times but the order
4450 of calls is different. The last call to `grokparms' is always the one
4451 that contains the formal parameter names of a function definition.
4453 Store in `last_function_parms' a chain of the decls of parms.
4454 Also store in `last_function_parm_tags' a chain of the struct, union,
4455 and enum tags declared among the parms.
4457 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4459 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4460 a mere declaration. A nonempty identifier-list gets an error message
4461 when FUNCDEF_FLAG is zero. */
4463 static tree
4464 grokparms (tree parms_info, int funcdef_flag)
4466 tree first_parm = TREE_CHAIN (parms_info);
4468 last_function_parms = TREE_PURPOSE (parms_info);
4469 last_function_parm_tags = TREE_VALUE (parms_info);
4470 last_function_parm_others = TREE_TYPE (parms_info);
4472 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4473 && !in_system_header)
4474 warning ("function declaration isn't a prototype");
4476 if (first_parm != 0
4477 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4479 if (! funcdef_flag)
4480 pedwarn ("parameter names (without types) in function declaration");
4482 last_function_parms = first_parm;
4483 return 0;
4485 else
4487 tree parm;
4488 tree typelt;
4489 /* If the arg types are incomplete in a declaration,
4490 they must include undefined tags.
4491 These tags can never be defined in the scope of the declaration,
4492 so the types can never be completed,
4493 and no call can be compiled successfully. */
4495 for (parm = last_function_parms, typelt = first_parm;
4496 parm;
4497 parm = TREE_CHAIN (parm))
4498 /* Skip over any enumeration constants declared here. */
4499 if (TREE_CODE (parm) == PARM_DECL)
4501 /* Barf if the parameter itself has an incomplete type. */
4502 tree type = TREE_VALUE (typelt);
4503 if (type == error_mark_node)
4504 continue;
4505 if (!COMPLETE_TYPE_P (type))
4507 if (funcdef_flag && DECL_NAME (parm) != 0)
4508 error ("parameter `%s' has incomplete type",
4509 IDENTIFIER_POINTER (DECL_NAME (parm)));
4510 else
4511 warning ("parameter has incomplete type");
4512 if (funcdef_flag)
4514 TREE_VALUE (typelt) = error_mark_node;
4515 TREE_TYPE (parm) = error_mark_node;
4518 typelt = TREE_CHAIN (typelt);
4521 return first_parm;
4525 /* Return a tree_list node with info on a parameter list just parsed.
4526 The TREE_PURPOSE is a list of decls of those parms.
4527 The TREE_VALUE is a list of structure, union and enum tags defined.
4528 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4529 The TREE_TYPE is a list of non-parameter decls which appeared with the
4530 parameters.
4531 This tree_list node is later fed to `grokparms'.
4533 VOID_AT_END nonzero means append `void' to the end of the type-list.
4534 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4536 tree
4537 get_parm_info (int void_at_end)
4539 tree decl, type, list;
4540 tree types = 0;
4541 tree *last_type = &types;
4542 tree tags = current_scope->tags;
4543 tree parms = current_scope->parms;
4544 tree others = current_scope->names;
4545 static bool explained_incomplete_types = false;
4546 bool gave_void_only_once_err = false;
4548 /* Just "void" (and no ellipsis) is special. There are really no parms.
4549 But if the "void" is qualified (by "const" or "volatile"), or has a
4550 storage class specifier ("register"), then the behavior is undefined;
4551 issue an error. Typedefs for "void" are OK (see DR#157). */
4552 if (void_at_end && parms != 0
4553 && TREE_CHAIN (parms) == 0
4554 && VOID_TYPE_P (TREE_TYPE (parms))
4555 && !DECL_NAME (parms))
4557 if (TREE_THIS_VOLATILE (parms)
4558 || TREE_READONLY (parms)
4559 || DECL_REGISTER (parms))
4560 error ("\"void\" as only parameter may not be qualified");
4562 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4565 /* Sanity check all of the parameter declarations. */
4566 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4568 if (TREE_CODE (decl) != PARM_DECL)
4569 abort ();
4570 if (TREE_ASM_WRITTEN (decl))
4571 abort ();
4573 /* Since there is a prototype, args are passed in their
4574 declared types. The back end may override this. */
4575 type = TREE_TYPE (decl);
4576 DECL_ARG_TYPE (decl) = type;
4577 if (PROMOTE_PROTOTYPES
4578 && INTEGRAL_TYPE_P (type)
4579 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4580 DECL_ARG_TYPE (decl) = integer_type_node;
4582 /* Check for (..., void, ...) and issue an error. */
4583 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4585 error ("\"void\" must be the only parameter");
4586 gave_void_only_once_err = true;
4589 type = build_tree_list (0, type);
4590 *last_type = type;
4591 last_type = &TREE_CHAIN (type);
4594 /* Check the list of non-parameter decls for any forward parm decls
4595 that never got real decls. */
4596 for (decl = others; decl; decl = TREE_CHAIN (decl))
4597 if (TREE_CODE (decl) == PARM_DECL)
4599 if (!TREE_ASM_WRITTEN (decl))
4600 abort ();
4602 error ("%Hparameter \"%D\" has just a forward declaration",
4603 &DECL_SOURCE_LOCATION (decl), decl);
4606 /* Warn about any struct, union or enum tags defined within this
4607 list. The scope of such types is limited to this declaration,
4608 which is rarely if ever desirable (it's impossible to call such
4609 a function with type-correct arguments). */
4610 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4612 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4613 const char *keyword;
4614 /* An anonymous union parm type is meaningful as a GNU extension.
4615 So don't warn for that. */
4616 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4617 continue;
4619 /* The keyword should not be translated. */
4620 switch (code)
4622 case RECORD_TYPE: keyword = "struct"; break;
4623 case UNION_TYPE: keyword = "union"; break;
4624 case ENUMERAL_TYPE: keyword = "enum"; break;
4625 default: abort ();
4628 if (TREE_PURPOSE (decl))
4629 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4630 warning ("\"%s %s\" declared inside parameter list",
4631 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4632 else
4633 /* The %s will be one of 'struct', 'union', or 'enum'. */
4634 warning ("anonymous %s declared inside parameter list", keyword);
4636 if (! explained_incomplete_types)
4638 warning ("its scope is only this definition or declaration,"
4639 " which is probably not what you want");
4640 explained_incomplete_types = true;
4645 if (void_at_end)
4647 type = build_tree_list (0, void_type_node);
4648 *last_type = type;
4651 list = tree_cons (parms, tags, types);
4652 TREE_TYPE (list) = others;
4653 return list;
4656 /* Get the struct, enum or union (CODE says which) with tag NAME.
4657 Define the tag as a forward-reference if it is not defined. */
4659 tree
4660 xref_tag (enum tree_code code, tree name)
4662 /* If a cross reference is requested, look up the type
4663 already defined for this tag and return it. */
4665 tree ref = lookup_tag (code, name, 0);
4666 /* If this is the right type of tag, return what we found.
4667 (This reference will be shadowed by shadow_tag later if appropriate.)
4668 If this is the wrong type of tag, do not return it. If it was the
4669 wrong type in the same scope, we will have had an error
4670 message already; if in a different scope and declaring
4671 a name, pending_xref_error will give an error message; but if in a
4672 different scope and not declaring a name, this tag should
4673 shadow the previous declaration of a different type of tag, and
4674 this would not work properly if we return the reference found.
4675 (For example, with "struct foo" in an outer scope, "union foo;"
4676 must shadow that tag with a new one of union type.) */
4677 if (ref && TREE_CODE (ref) == code)
4678 return ref;
4680 /* If no such tag is yet defined, create a forward-reference node
4681 and record it as the "definition".
4682 When a real declaration of this type is found,
4683 the forward-reference will be altered into a real type. */
4685 ref = make_node (code);
4686 if (code == ENUMERAL_TYPE)
4688 /* Give the type a default layout like unsigned int
4689 to avoid crashing if it does not get defined. */
4690 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4691 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4692 TYPE_USER_ALIGN (ref) = 0;
4693 TREE_UNSIGNED (ref) = 1;
4694 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4695 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4696 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4699 pushtag (name, ref);
4701 return ref;
4704 /* Make sure that the tag NAME is defined *in the current scope*
4705 at least as a forward reference.
4706 CODE says which kind of tag NAME ought to be. */
4708 tree
4709 start_struct (enum tree_code code, tree name)
4711 /* If there is already a tag defined at this scope
4712 (as a forward reference), just return it. */
4714 tree ref = 0;
4716 if (name != 0)
4717 ref = lookup_tag (code, name, 1);
4718 if (ref && TREE_CODE (ref) == code)
4720 if (TYPE_FIELDS (ref))
4722 if (code == UNION_TYPE)
4723 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4724 else
4725 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4728 else
4730 /* Otherwise create a forward-reference just so the tag is in scope. */
4732 ref = make_node (code);
4733 pushtag (name, ref);
4736 C_TYPE_BEING_DEFINED (ref) = 1;
4737 TYPE_PACKED (ref) = flag_pack_struct;
4738 return ref;
4741 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4742 of a structure component, returning a FIELD_DECL node.
4743 WIDTH is non-NULL for bit fields only, and is an INTEGER_CST node.
4745 This is done during the parsing of the struct declaration.
4746 The FIELD_DECL nodes are chained together and the lot of them
4747 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4749 tree
4750 grokfield (tree declarator, tree declspecs, tree width)
4752 tree value;
4754 if (declarator == NULL_TREE && width == NULL_TREE)
4756 /* This is an unnamed decl.
4758 If we have something of the form "union { list } ;" then this
4759 is the anonymous union extension. Similarly for struct.
4761 If this is something of the form "struct foo;", then
4762 If MS extensions are enabled, this is handled as an
4763 anonymous struct.
4764 Otherwise this is a forward declaration of a structure tag.
4766 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4767 If MS extensions are enabled and foo names a structure, then
4768 again this is an anonymous struct.
4769 Otherwise this is an error.
4771 Oh what a horrid tangled web we weave. I wonder if MS consciously
4772 took this from Plan 9 or if it was an accident of implementation
4773 that took root before someone noticed the bug... */
4775 tree type = TREE_VALUE (declspecs);
4777 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4778 type = TREE_TYPE (type);
4779 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4781 if (flag_ms_extensions)
4782 ; /* ok */
4783 else if (flag_iso)
4784 goto warn_unnamed_field;
4785 else if (TYPE_NAME (type) == NULL)
4786 ; /* ok */
4787 else
4788 goto warn_unnamed_field;
4790 else
4792 warn_unnamed_field:
4793 warning ("declaration does not declare anything");
4794 return NULL_TREE;
4798 value = grokdeclarator (declarator, declspecs, width ? BITFIELD : FIELD, 0);
4800 finish_decl (value, NULL_TREE, NULL_TREE);
4801 DECL_INITIAL (value) = width;
4803 if (c_dialect_objc ())
4804 objc_check_decl (value);
4805 return value;
4808 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4809 the list such that this does not present a problem later. */
4811 static void
4812 detect_field_duplicates (tree fieldlist)
4814 tree x, y;
4815 int timeout = 10;
4817 /* First, see if there are more than "a few" fields.
4818 This is trivially true if there are zero or one fields. */
4819 if (!fieldlist)
4820 return;
4821 x = TREE_CHAIN (fieldlist);
4822 if (!x)
4823 return;
4824 do {
4825 timeout--;
4826 x = TREE_CHAIN (x);
4827 } while (timeout > 0 && x);
4829 /* If there were "few" fields, avoid the overhead of allocating
4830 a hash table. Instead just do the nested traversal thing. */
4831 if (timeout > 0)
4833 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4834 if (DECL_NAME (x))
4836 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4837 if (DECL_NAME (y) == DECL_NAME (x))
4839 error ("%Hduplicate member '%D'",
4840 &DECL_SOURCE_LOCATION (x), x);
4841 DECL_NAME (x) = NULL_TREE;
4845 else
4847 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4848 void **slot;
4850 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4851 if ((y = DECL_NAME (x)) != 0)
4853 slot = htab_find_slot (htab, y, INSERT);
4854 if (*slot)
4856 error ("%Hduplicate member '%D'",
4857 &DECL_SOURCE_LOCATION (x), x);
4858 DECL_NAME (x) = NULL_TREE;
4860 *slot = y;
4863 htab_delete (htab);
4867 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4868 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4869 ATTRIBUTES are attributes to be applied to the structure. */
4871 tree
4872 finish_struct (tree t, tree fieldlist, tree attributes)
4874 tree x;
4875 int toplevel = global_scope == current_scope;
4876 int saw_named_field;
4878 /* If this type was previously laid out as a forward reference,
4879 make sure we lay it out again. */
4881 TYPE_SIZE (t) = 0;
4883 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4885 /* Nameless union parm types are useful as GCC extension. */
4886 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4887 /* Otherwise, warn about any struct or union def. in parmlist. */
4888 if (in_parm_level_p ())
4890 if (pedantic)
4891 pedwarn ("%s defined inside parms",
4892 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4893 else
4894 warning ("%s defined inside parms",
4895 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4898 if (pedantic)
4900 for (x = fieldlist; x; x = TREE_CHAIN (x))
4901 if (DECL_NAME (x) != 0)
4902 break;
4904 if (x == 0)
4905 pedwarn ("%s has no %s",
4906 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4907 fieldlist ? _("named members") : _("members"));
4910 /* Install struct as DECL_CONTEXT of each field decl.
4911 Also process specified field sizes,m which is found in the DECL_INITIAL.
4912 Store 0 there, except for ": 0" fields (so we can find them
4913 and delete them, below). */
4915 saw_named_field = 0;
4916 for (x = fieldlist; x; x = TREE_CHAIN (x))
4918 DECL_CONTEXT (x) = t;
4919 DECL_PACKED (x) |= TYPE_PACKED (t);
4921 /* If any field is const, the structure type is pseudo-const. */
4922 if (TREE_READONLY (x))
4923 C_TYPE_FIELDS_READONLY (t) = 1;
4924 else
4926 /* A field that is pseudo-const makes the structure likewise. */
4927 tree t1 = TREE_TYPE (x);
4928 while (TREE_CODE (t1) == ARRAY_TYPE)
4929 t1 = TREE_TYPE (t1);
4930 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
4931 && C_TYPE_FIELDS_READONLY (t1))
4932 C_TYPE_FIELDS_READONLY (t) = 1;
4935 /* Any field that is volatile means variables of this type must be
4936 treated in some ways as volatile. */
4937 if (TREE_THIS_VOLATILE (x))
4938 C_TYPE_FIELDS_VOLATILE (t) = 1;
4940 /* Any field of nominal variable size implies structure is too. */
4941 if (C_DECL_VARIABLE_SIZE (x))
4942 C_TYPE_VARIABLE_SIZE (t) = 1;
4944 /* Detect invalid nested redefinition. */
4945 if (TREE_TYPE (x) == t)
4946 error ("nested redefinition of `%s'",
4947 IDENTIFIER_POINTER (TYPE_NAME (t)));
4949 /* Detect invalid bit-field size. */
4950 if (DECL_INITIAL (x))
4951 STRIP_NOPS (DECL_INITIAL (x));
4952 if (DECL_INITIAL (x))
4954 if (TREE_CODE (DECL_INITIAL (x)) == INTEGER_CST)
4955 constant_expression_warning (DECL_INITIAL (x));
4956 else
4958 error ("%Hbit-field '%D' width not an integer constant",
4959 &DECL_SOURCE_LOCATION (x), x);
4960 DECL_INITIAL (x) = NULL;
4964 /* Detect invalid bit-field type. */
4965 if (DECL_INITIAL (x)
4966 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
4967 && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE
4968 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
4970 error ("%Hbit-field '%D' has invalid type",
4971 &DECL_SOURCE_LOCATION (x), x);
4972 DECL_INITIAL (x) = NULL;
4975 if (DECL_INITIAL (x) && pedantic
4976 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != integer_type_node
4977 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != unsigned_type_node
4978 && TYPE_MAIN_VARIANT (TREE_TYPE (x)) != boolean_type_node
4979 /* Accept an enum that's equivalent to int or unsigned int. */
4980 && !(TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
4981 && (TYPE_PRECISION (TREE_TYPE (x))
4982 == TYPE_PRECISION (integer_type_node))))
4983 pedwarn ("%Hbit-field '%D' type invalid in ISO C",
4984 &DECL_SOURCE_LOCATION (x), x);
4986 /* Detect and ignore out of range field width and process valid
4987 field widths. */
4988 if (DECL_INITIAL (x))
4990 int max_width
4991 = (TYPE_MAIN_VARIANT (TREE_TYPE (x)) == boolean_type_node
4992 ? CHAR_TYPE_SIZE : TYPE_PRECISION (TREE_TYPE (x)));
4994 if (tree_int_cst_sgn (DECL_INITIAL (x)) < 0)
4995 error ("%Hnegative width in bit-field '%D'",
4996 &DECL_SOURCE_LOCATION (x), x);
4997 else if (0 < compare_tree_int (DECL_INITIAL (x), max_width))
4998 pedwarn ("%Hwidth of '%D' exceeds its type",
4999 &DECL_SOURCE_LOCATION (x), x);
5000 else if (integer_zerop (DECL_INITIAL (x)) && DECL_NAME (x) != 0)
5001 error ("%Hzero width for bit-field '%D'",
5002 &DECL_SOURCE_LOCATION (x), x);
5003 else
5005 /* The test above has assured us that TREE_INT_CST_HIGH is 0. */
5006 unsigned HOST_WIDE_INT width
5007 = tree_low_cst (DECL_INITIAL (x), 1);
5009 if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
5010 && (width < min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
5011 TREE_UNSIGNED (TREE_TYPE (x)))
5012 || (width
5013 < min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
5014 TREE_UNSIGNED (TREE_TYPE (x))))))
5015 warning ("%H'%D' is narrower than values of its type",
5016 &DECL_SOURCE_LOCATION (x), x);
5018 DECL_SIZE (x) = bitsize_int (width);
5019 DECL_BIT_FIELD (x) = 1;
5020 SET_DECL_C_BIT_FIELD (x);
5024 DECL_INITIAL (x) = 0;
5026 /* Detect flexible array member in an invalid context. */
5027 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5028 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5029 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5030 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5032 if (TREE_CODE (t) == UNION_TYPE)
5033 error ("%Hflexible array member in union",
5034 &DECL_SOURCE_LOCATION (x));
5035 else if (TREE_CHAIN (x) != NULL_TREE)
5036 error ("%Hflexible array member not at end of struct",
5037 &DECL_SOURCE_LOCATION (x));
5038 else if (! saw_named_field)
5039 error ("%Hflexible array member in otherwise empty struct",
5040 &DECL_SOURCE_LOCATION (x));
5043 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5044 && flexible_array_type_p (TREE_TYPE (x)))
5045 pedwarn ("%Hinvalid use of structure with flexible array member",
5046 &DECL_SOURCE_LOCATION (x));
5048 if (DECL_NAME (x))
5049 saw_named_field = 1;
5052 detect_field_duplicates (fieldlist);
5054 /* Now we have the nearly final fieldlist. Record it,
5055 then lay out the structure or union (including the fields). */
5057 TYPE_FIELDS (t) = fieldlist;
5059 layout_type (t);
5061 /* Delete all zero-width bit-fields from the fieldlist */
5063 tree *fieldlistp = &fieldlist;
5064 while (*fieldlistp)
5065 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5066 *fieldlistp = TREE_CHAIN (*fieldlistp);
5067 else
5068 fieldlistp = &TREE_CHAIN (*fieldlistp);
5071 /* Now we have the truly final field list.
5072 Store it in this type and in the variants. */
5074 TYPE_FIELDS (t) = fieldlist;
5076 /* If there are lots of fields, sort so we can look through them fast.
5077 We arbitrarily consider 16 or more elts to be "a lot". */
5080 int len = 0;
5082 for (x = fieldlist; x; x = TREE_CHAIN (x))
5084 if (len > 15 || DECL_NAME (x) == NULL)
5085 break;
5086 len += 1;
5089 if (len > 15)
5091 tree *field_array;
5092 struct lang_type *space;
5093 struct sorted_fields_type *space2;
5095 len += list_length (x);
5097 /* Use the same allocation policy here that make_node uses, to
5098 ensure that this lives as long as the rest of the struct decl.
5099 All decls in an inline function need to be saved. */
5101 space = ggc_alloc (sizeof (struct lang_type));
5102 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5104 len = 0;
5105 space->s = space2;
5106 field_array = &space2->elts[0];
5107 for (x = fieldlist; x; x = TREE_CHAIN (x))
5109 field_array[len++] = x;
5111 /* If there is anonymous struct or union, break out of the loop. */
5112 if (DECL_NAME (x) == NULL)
5113 break;
5115 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5116 if (x == NULL)
5118 TYPE_LANG_SPECIFIC (t) = space;
5119 TYPE_LANG_SPECIFIC (t)->s->len = len;
5120 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5121 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5126 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5128 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5129 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5130 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5131 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5134 /* If this was supposed to be a transparent union, but we can't
5135 make it one, warn and turn off the flag. */
5136 if (TREE_CODE (t) == UNION_TYPE
5137 && TYPE_TRANSPARENT_UNION (t)
5138 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5140 TYPE_TRANSPARENT_UNION (t) = 0;
5141 warning ("union cannot be made transparent");
5144 /* If this structure or union completes the type of any previous
5145 variable declaration, lay it out and output its rtl. */
5147 if (current_scope->incomplete != NULL_TREE)
5149 tree prev = NULL_TREE;
5151 for (x = current_scope->incomplete; x; x = TREE_CHAIN (x))
5153 tree decl = TREE_VALUE (x);
5155 if (TYPE_MAIN_VARIANT (TREE_TYPE (decl)) == TYPE_MAIN_VARIANT (t)
5156 && TREE_CODE (decl) != TYPE_DECL)
5158 layout_decl (decl, 0);
5159 /* This is a no-op in c-lang.c or something real in
5160 objc-act.c. */
5161 if (c_dialect_objc ())
5162 objc_check_decl (decl);
5163 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5164 if (! toplevel)
5165 expand_decl (decl);
5166 /* Unlink X from the incomplete list. */
5167 if (prev)
5168 TREE_CHAIN (prev) = TREE_CHAIN (x);
5169 else
5170 current_scope->incomplete = TREE_CHAIN (x);
5172 else if (!COMPLETE_TYPE_P (TREE_TYPE (decl))
5173 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5175 tree element = TREE_TYPE (decl);
5176 while (TREE_CODE (element) == ARRAY_TYPE)
5177 element = TREE_TYPE (element);
5178 if (element == t)
5180 layout_array_type (TREE_TYPE (decl));
5181 if (TREE_CODE (decl) != TYPE_DECL)
5183 layout_decl (decl, 0);
5184 if (c_dialect_objc ())
5185 objc_check_decl (decl);
5186 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5187 if (! toplevel)
5188 expand_decl (decl);
5190 /* Unlink X from the incomplete list. */
5191 if (prev)
5192 TREE_CHAIN (prev) = TREE_CHAIN (x);
5193 else
5194 current_scope->incomplete = TREE_CHAIN (x);
5196 else
5197 prev = x;
5199 else
5200 prev = x;
5204 /* Finish debugging output for this type. */
5205 rest_of_type_compilation (t, toplevel);
5207 return t;
5210 /* Lay out the type T, and its element type, and so on. */
5212 static void
5213 layout_array_type (tree t)
5215 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5216 layout_array_type (TREE_TYPE (t));
5217 layout_type (t);
5220 /* Begin compiling the definition of an enumeration type.
5221 NAME is its name (or null if anonymous).
5222 Returns the type object, as yet incomplete.
5223 Also records info about it so that build_enumerator
5224 may be used to declare the individual values as they are read. */
5226 tree
5227 start_enum (tree name)
5229 tree enumtype = 0;
5231 /* If this is the real definition for a previous forward reference,
5232 fill in the contents in the same object that used to be the
5233 forward reference. */
5235 if (name != 0)
5236 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5238 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5240 enumtype = make_node (ENUMERAL_TYPE);
5241 pushtag (name, enumtype);
5244 C_TYPE_BEING_DEFINED (enumtype) = 1;
5246 if (TYPE_VALUES (enumtype) != 0)
5248 /* This enum is a named one that has been declared already. */
5249 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5251 /* Completely replace its old definition.
5252 The old enumerators remain defined, however. */
5253 TYPE_VALUES (enumtype) = 0;
5256 enum_next_value = integer_zero_node;
5257 enum_overflow = 0;
5259 if (flag_short_enums)
5260 TYPE_PACKED (enumtype) = 1;
5262 return enumtype;
5265 /* After processing and defining all the values of an enumeration type,
5266 install their decls in the enumeration type and finish it off.
5267 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5268 and ATTRIBUTES are the specified attributes.
5269 Returns ENUMTYPE. */
5271 tree
5272 finish_enum (tree enumtype, tree values, tree attributes)
5274 tree pair, tem;
5275 tree minnode = 0, maxnode = 0, enum_value_type;
5276 int precision, unsign;
5277 int toplevel = (global_scope == current_scope);
5279 if (in_parm_level_p ())
5280 warning ("enum defined inside parms");
5282 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5284 /* Calculate the maximum value of any enumerator in this type. */
5286 if (values == error_mark_node)
5287 minnode = maxnode = integer_zero_node;
5288 else
5290 minnode = maxnode = TREE_VALUE (values);
5291 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5293 tree value = TREE_VALUE (pair);
5294 if (tree_int_cst_lt (maxnode, value))
5295 maxnode = value;
5296 if (tree_int_cst_lt (value, minnode))
5297 minnode = value;
5301 /* Construct the final type of this enumeration. It is the same
5302 as one of the integral types - the narrowest one that fits, except
5303 that normally we only go as narrow as int - and signed iff any of
5304 the values are negative. */
5305 unsign = (tree_int_cst_sgn (minnode) >= 0);
5306 precision = MAX (min_precision (minnode, unsign),
5307 min_precision (maxnode, unsign));
5308 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5310 tree narrowest = c_common_type_for_size (precision, unsign);
5311 if (narrowest == 0)
5313 warning ("enumeration values exceed range of largest integer");
5314 narrowest = long_long_integer_type_node;
5317 precision = TYPE_PRECISION (narrowest);
5319 else
5320 precision = TYPE_PRECISION (integer_type_node);
5322 if (precision == TYPE_PRECISION (integer_type_node))
5323 enum_value_type = c_common_type_for_size (precision, 0);
5324 else
5325 enum_value_type = enumtype;
5327 TYPE_MIN_VALUE (enumtype) = minnode;
5328 TYPE_MAX_VALUE (enumtype) = maxnode;
5329 TYPE_PRECISION (enumtype) = precision;
5330 TREE_UNSIGNED (enumtype) = unsign;
5331 TYPE_SIZE (enumtype) = 0;
5332 layout_type (enumtype);
5334 if (values != error_mark_node)
5336 /* Change the type of the enumerators to be the enum type. We
5337 need to do this irrespective of the size of the enum, for
5338 proper type checking. Replace the DECL_INITIALs of the
5339 enumerators, and the value slots of the list, with copies
5340 that have the enum type; they cannot be modified in place
5341 because they may be shared (e.g. integer_zero_node) Finally,
5342 change the purpose slots to point to the names of the decls. */
5343 for (pair = values; pair; pair = TREE_CHAIN (pair))
5345 tree enu = TREE_PURPOSE (pair);
5347 TREE_TYPE (enu) = enumtype;
5349 /* The ISO C Standard mandates enumerators to have type int,
5350 even though the underlying type of an enum type is
5351 unspecified. Here we convert any enumerators that fit in
5352 an int to type int, to avoid promotions to unsigned types
5353 when comparing integers with enumerators that fit in the
5354 int range. When -pedantic is given, build_enumerator()
5355 would have already taken care of those that don't fit. */
5356 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5357 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5358 else
5359 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5361 TREE_PURPOSE (pair) = DECL_NAME (enu);
5362 TREE_VALUE (pair) = DECL_INITIAL (enu);
5365 TYPE_VALUES (enumtype) = values;
5368 /* Fix up all variant types of this enum type. */
5369 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5371 if (tem == enumtype)
5372 continue;
5373 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5374 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5375 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5376 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5377 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5378 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5379 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5380 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5381 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5382 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5385 /* Finish debugging output for this type. */
5386 rest_of_type_compilation (enumtype, toplevel);
5388 return enumtype;
5391 /* Build and install a CONST_DECL for one value of the
5392 current enumeration type (one that was begun with start_enum).
5393 Return a tree-list containing the CONST_DECL and its value.
5394 Assignment of sequential values by default is handled here. */
5396 tree
5397 build_enumerator (tree name, tree value)
5399 tree decl, type;
5401 /* Validate and default VALUE. */
5403 /* Remove no-op casts from the value. */
5404 if (value)
5405 STRIP_TYPE_NOPS (value);
5407 if (value != 0)
5409 if (TREE_CODE (value) == INTEGER_CST)
5411 value = default_conversion (value);
5412 constant_expression_warning (value);
5414 else
5416 error ("enumerator value for `%s' not integer constant",
5417 IDENTIFIER_POINTER (name));
5418 value = 0;
5422 /* Default based on previous value. */
5423 /* It should no longer be possible to have NON_LVALUE_EXPR
5424 in the default. */
5425 if (value == 0)
5427 value = enum_next_value;
5428 if (enum_overflow)
5429 error ("overflow in enumeration values");
5432 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5434 pedwarn ("ISO C restricts enumerator values to range of `int'");
5435 value = convert (integer_type_node, value);
5438 /* Set basis for default for next value. */
5439 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5440 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5442 /* Now create a declaration for the enum value name. */
5444 type = TREE_TYPE (value);
5445 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5446 TYPE_PRECISION (integer_type_node)),
5447 (TYPE_PRECISION (type)
5448 >= TYPE_PRECISION (integer_type_node)
5449 && TREE_UNSIGNED (type)));
5451 decl = build_decl (CONST_DECL, name, type);
5452 DECL_INITIAL (decl) = convert (type, value);
5453 pushdecl (decl);
5455 return tree_cons (decl, value, NULL_TREE);
5459 /* Create the FUNCTION_DECL for a function definition.
5460 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5461 the declaration; they describe the function's name and the type it returns,
5462 but twisted together in a fashion that parallels the syntax of C.
5464 This function creates a binding context for the function body
5465 as well as setting up the FUNCTION_DECL in current_function_decl.
5467 Returns 1 on success. If the DECLARATOR is not suitable for a function
5468 (it defines a datum instead), we return 0, which tells
5469 yyparse to report a parse error. */
5472 start_function (tree declspecs, tree declarator, tree attributes)
5474 tree decl1, old_decl;
5475 tree restype;
5476 int old_immediate_size_expand = immediate_size_expand;
5478 current_function_returns_value = 0; /* Assume, until we see it does. */
5479 current_function_returns_null = 0;
5480 current_function_returns_abnormally = 0;
5481 warn_about_return_type = 0;
5482 current_extern_inline = 0;
5484 /* Don't expand any sizes in the return type of the function. */
5485 immediate_size_expand = 0;
5487 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1);
5489 /* If the declarator is not suitable for a function definition,
5490 cause a syntax error. */
5491 if (decl1 == 0)
5493 immediate_size_expand = old_immediate_size_expand;
5494 return 0;
5497 decl_attributes (&decl1, attributes, 0);
5499 if (DECL_DECLARED_INLINE_P (decl1)
5500 && DECL_UNINLINABLE (decl1)
5501 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5502 warning ("%Hinline function '%D' given attribute noinline",
5503 &DECL_SOURCE_LOCATION (decl1), decl1);
5505 announce_function (decl1);
5507 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5509 error ("return type is an incomplete type");
5510 /* Make it return void instead. */
5511 TREE_TYPE (decl1)
5512 = build_function_type (void_type_node,
5513 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5516 if (warn_about_return_type)
5517 pedwarn_c99 ("return type defaults to `int'");
5519 /* Save the parm names or decls from this function's declarator
5520 where store_parm_decls will find them. */
5521 current_function_parms = last_function_parms;
5522 current_function_parm_tags = last_function_parm_tags;
5523 current_function_parm_others = last_function_parm_others;
5525 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5526 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5527 DECL_INITIAL (decl1) = error_mark_node;
5529 /* If this definition isn't a prototype and we had a prototype declaration
5530 before, copy the arg type info from that prototype.
5531 But not if what we had before was a builtin function. */
5532 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5533 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5534 && !DECL_BUILT_IN (old_decl)
5535 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5536 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5537 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5539 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5540 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5543 /* Optionally warn of old-fashioned def with no previous prototype. */
5544 if (warn_strict_prototypes
5545 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5546 && C_DECL_ISNT_PROTOTYPE (old_decl))
5547 warning ("function declaration isn't a prototype");
5548 /* Optionally warn of any global def with no previous prototype. */
5549 else if (warn_missing_prototypes
5550 && TREE_PUBLIC (decl1)
5551 && ! MAIN_NAME_P (DECL_NAME (decl1))
5552 && C_DECL_ISNT_PROTOTYPE (old_decl))
5553 warning ("%Hno previous prototype for '%D'",
5554 &DECL_SOURCE_LOCATION (decl1), decl1);
5555 /* Optionally warn of any def with no previous prototype
5556 if the function has already been used. */
5557 else if (warn_missing_prototypes
5558 && old_decl != 0 && TREE_USED (old_decl)
5559 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5560 warning ("%H'%D' was used with no prototype before its definition",
5561 &DECL_SOURCE_LOCATION (decl1), decl1);
5562 /* Optionally warn of any global def with no previous declaration. */
5563 else if (warn_missing_declarations
5564 && TREE_PUBLIC (decl1)
5565 && old_decl == 0
5566 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5567 warning ("%Hno previous declaration for '%D'",
5568 &DECL_SOURCE_LOCATION (decl1), decl1);
5569 /* Optionally warn of any def with no previous declaration
5570 if the function has already been used. */
5571 else if (warn_missing_declarations
5572 && old_decl != 0 && TREE_USED (old_decl)
5573 && C_DECL_IMPLICIT (old_decl))
5574 warning ("%H`%D' was used with no declaration before its definition",
5575 &DECL_SOURCE_LOCATION (decl1), decl1);
5577 /* This is a definition, not a reference.
5578 So normally clear DECL_EXTERNAL.
5579 However, `extern inline' acts like a declaration
5580 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5581 DECL_EXTERNAL (decl1) = current_extern_inline;
5583 /* This function exists in static storage.
5584 (This does not mean `static' in the C sense!) */
5585 TREE_STATIC (decl1) = 1;
5587 /* A nested function is not global. */
5588 if (current_function_decl != 0)
5589 TREE_PUBLIC (decl1) = 0;
5591 #ifdef ENABLE_CHECKING
5592 /* This is the earliest point at which we might know the assembler
5593 name of the function. Thus, if it's set before this, die horribly. */
5594 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5595 abort ();
5596 #endif
5598 /* If #pragma weak was used, mark the decl weak now. */
5599 if (current_scope == global_scope)
5600 maybe_apply_pragma_weak (decl1);
5602 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5603 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5605 tree args;
5606 int argct = 0;
5607 const location_t *locus = &DECL_SOURCE_LOCATION (decl1);
5609 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5610 != integer_type_node)
5611 pedwarn ("%Hreturn type of '%D' is not `int'", locus, decl1);
5613 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5614 args = TREE_CHAIN (args))
5616 tree type = args ? TREE_VALUE (args) : 0;
5618 if (type == void_type_node)
5619 break;
5621 ++argct;
5622 switch (argct)
5624 case 1:
5625 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5626 pedwarn ("%Hfirst argument of '%D' should be `int'",
5627 locus, decl1);
5628 break;
5630 case 2:
5631 if (TREE_CODE (type) != POINTER_TYPE
5632 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5633 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5634 != char_type_node))
5635 pedwarn ("%Hsecond argument of '%D' should be 'char **'",
5636 locus, decl1);
5637 break;
5639 case 3:
5640 if (TREE_CODE (type) != POINTER_TYPE
5641 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5642 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5643 != char_type_node))
5644 pedwarn ("%Hthird argument of '%D' should probably be "
5645 "'char **'", locus, decl1);
5646 break;
5650 /* It is intentional that this message does not mention the third
5651 argument because it's only mentioned in an appendix of the
5652 standard. */
5653 if (argct > 0 && (argct < 2 || argct > 3))
5654 pedwarn ("%H'%D' takes only zero or two arguments", locus, decl1);
5656 if (! TREE_PUBLIC (decl1))
5657 pedwarn ("%H'%D' is normally a non-static function", locus, decl1);
5660 /* Record the decl so that the function name is defined.
5661 If we already have a decl for this name, and it is a FUNCTION_DECL,
5662 use the old decl. */
5664 current_function_decl = pushdecl (decl1);
5666 pushlevel (0);
5667 declare_parm_level ();
5669 make_decl_rtl (current_function_decl, NULL);
5671 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5672 /* Promote the value to int before returning it. */
5673 if (c_promoting_integer_type_p (restype))
5675 /* It retains unsignedness if not really getting wider. */
5676 if (TREE_UNSIGNED (restype)
5677 && (TYPE_PRECISION (restype)
5678 == TYPE_PRECISION (integer_type_node)))
5679 restype = unsigned_type_node;
5680 else
5681 restype = integer_type_node;
5683 DECL_RESULT (current_function_decl)
5684 = build_decl (RESULT_DECL, NULL_TREE, restype);
5686 /* If this fcn was already referenced via a block-scope `extern' decl
5687 (or an implicit decl), propagate certain information about the usage. */
5688 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5689 TREE_ADDRESSABLE (current_function_decl) = 1;
5691 immediate_size_expand = old_immediate_size_expand;
5693 start_fname_decls ();
5695 return 1;
5698 /* Subroutine of store_parm_decls which handles new-style function
5699 definitions (prototype format). The parms already have decls, so we
5700 need only record them as in effect and complain if any redundant
5701 old-style parm decls were written. */
5702 static void
5703 store_parm_decls_newstyle (void)
5705 tree decl, last;
5706 tree fndecl = current_function_decl;
5707 tree parms = current_function_parms;
5708 tree tags = current_function_parm_tags;
5709 tree others = current_function_parm_others;
5711 if (current_scope->parms || current_scope->names || current_scope->tags)
5713 error ("%Hold-style parameter declarations in prototyped "
5714 "function definition", &DECL_SOURCE_LOCATION (fndecl));
5716 /* Get rid of the old-style declarations. */
5717 poplevel (0, 0, 0);
5718 pushlevel (0);
5721 /* Now make all the parameter declarations visible in the function body.
5722 We can bypass most of the grunt work of pushdecl. */
5723 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5725 DECL_CONTEXT (decl) = current_function_decl;
5726 if (DECL_NAME (decl) == 0)
5727 error ("%Hparameter name omitted", &DECL_SOURCE_LOCATION (decl));
5728 else
5730 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5731 current_scope->shadowed
5732 = tree_cons (DECL_NAME (decl),
5733 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5734 current_scope->shadowed);
5735 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5738 current_scope->parms = parms;
5739 current_scope->parms_last = last;
5741 /* Record the parameter list in the function declaration. */
5742 DECL_ARGUMENTS (fndecl) = parms;
5744 /* Now make all the ancillary declarations visible, likewise. */
5745 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5747 DECL_CONTEXT (decl) = current_function_decl;
5748 if (DECL_NAME (decl)
5749 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5751 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5752 current_scope->shadowed
5753 = tree_cons (DECL_NAME (decl),
5754 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5755 current_scope->shadowed);
5756 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5759 current_scope->names = others;
5760 current_scope->names_last = last;
5762 /* And all the tag declarations. */
5763 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5764 if (TREE_PURPOSE (decl))
5766 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5767 current_scope->shadowed_tags
5768 = tree_cons (TREE_PURPOSE (decl),
5769 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5770 current_scope->shadowed_tags);
5771 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5773 current_scope->tags = tags;
5776 /* Subroutine of store_parm_decls which handles old-style function
5777 definitions (separate parameter list and declarations). */
5779 static void
5780 store_parm_decls_oldstyle (void)
5782 tree parm, decl, last;
5783 tree fndecl = current_function_decl;
5785 /* This is the identifier list from the function declarator. */
5786 tree parmids = current_function_parms;
5788 /* We use DECL_WEAK as a flag to show which parameters have been
5789 seen already, since it is not used on PARM_DECL. */
5790 #ifdef ENABLE_CHECKING
5791 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5792 if (DECL_WEAK (parm))
5793 abort ();
5794 #endif
5796 /* Match each formal parameter name with its declaration. Save each
5797 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5798 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5800 if (TREE_VALUE (parm) == 0)
5802 error ("%Hparameter name missing from parameter list",
5803 &DECL_SOURCE_LOCATION (fndecl));
5804 TREE_PURPOSE (parm) = 0;
5805 continue;
5808 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5809 if (decl && DECL_CONTEXT (decl) == fndecl)
5811 const location_t *locus = &DECL_SOURCE_LOCATION (decl);
5812 /* If we got something other than a PARM_DECL it is an error. */
5813 if (TREE_CODE (decl) != PARM_DECL)
5814 error ("%H\"%D\" declared as a non-parameter", locus, decl);
5815 /* If the declaration is already marked, we have a duplicate
5816 name. Complain and ignore the duplicate. */
5817 else if (DECL_WEAK (decl))
5819 error ("%Hmultiple parameters named \"%D\"", locus, decl);
5820 TREE_PURPOSE (parm) = 0;
5821 continue;
5823 /* If the declaration says "void", complain and turn it into
5824 an int. */
5825 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5827 error ("%Hparameter \"%D\" declared void", locus, decl);
5828 TREE_TYPE (decl) = integer_type_node;
5829 DECL_ARG_TYPE (decl) = integer_type_node;
5830 layout_decl (decl, 0);
5833 /* If no declaration found, default to int. */
5834 else
5836 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
5837 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5838 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5839 DECL_SOURCE_LOCATION (decl) = *locus;
5840 pushdecl (decl);
5842 if (flag_isoc99)
5843 pedwarn ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5844 else if (extra_warnings)
5845 warning ("%Htype of \"%D\" defaults to \"int\"", locus, decl);
5848 TREE_PURPOSE (parm) = decl;
5849 DECL_WEAK (decl) = 1;
5852 /* Now examine the parms chain for incomplete declarations
5853 and declarations with no corresponding names. */
5855 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5857 const location_t *locus = &DECL_SOURCE_LOCATION (parm);
5859 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5861 error ("%Hparameter \"%D\" has incomplete type", locus, parm);
5862 TREE_TYPE (parm) = error_mark_node;
5865 if (! DECL_WEAK (parm))
5867 error ("%Hdeclaration for parameter \"%D\" but no such parameter",
5868 locus, parm);
5870 /* Pretend the parameter was not missing.
5871 This gets us to a standard state and minimizes
5872 further error messages. */
5873 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5877 /* Chain the declarations together in the order of the list of
5878 names. Store that chain in the function decl, replacing the
5879 list of names. Update the current scope to match. */
5880 DECL_ARGUMENTS (fndecl) = 0;
5882 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5883 if (TREE_PURPOSE (parm))
5884 break;
5885 if (parm && TREE_PURPOSE (parm))
5887 last = TREE_PURPOSE (parm);
5888 DECL_ARGUMENTS (fndecl) = last;
5889 current_scope->parms = last;
5890 DECL_WEAK (last) = 0;
5892 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5893 if (TREE_PURPOSE (parm))
5895 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5896 last = TREE_PURPOSE (parm);
5897 DECL_WEAK (last) = 0;
5899 current_scope->parms_last = last;
5900 TREE_CHAIN (last) = 0;
5903 /* If there was a previous prototype,
5904 set the DECL_ARG_TYPE of each argument according to
5905 the type previously specified, and report any mismatches. */
5907 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5909 tree type;
5910 for (parm = DECL_ARGUMENTS (fndecl),
5911 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5912 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5913 != void_type_node));
5914 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5916 if (parm == 0 || type == 0
5917 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5919 error ("number of arguments doesn't match prototype");
5920 error ("%Hprototype declaration",
5921 &current_function_prototype_locus);
5922 break;
5924 /* Type for passing arg must be consistent with that
5925 declared for the arg. ISO C says we take the unqualified
5926 type for parameters declared with qualified type. */
5927 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5928 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5929 COMPARE_STRICT))
5931 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5932 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5934 /* Adjust argument to match prototype. E.g. a previous
5935 `int foo(float);' prototype causes
5936 `int foo(x) float x; {...}' to be treated like
5937 `int foo(float x) {...}'. This is particularly
5938 useful for argument types like uid_t. */
5939 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5941 if (PROMOTE_PROTOTYPES
5942 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5943 && TYPE_PRECISION (TREE_TYPE (parm))
5944 < TYPE_PRECISION (integer_type_node))
5945 DECL_ARG_TYPE (parm) = integer_type_node;
5947 if (pedantic)
5949 pedwarn ("promoted argument \"%D\" "
5950 "doesn't match prototype", parm);
5951 pedwarn ("%Hprototype declaration",
5952 &current_function_prototype_locus);
5955 else
5957 error ("argument \"%D\" doesn't match prototype", parm);
5958 error ("%Hprototype declaration",
5959 &current_function_prototype_locus);
5963 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5966 /* Otherwise, create a prototype that would match. */
5968 else
5970 tree actual = 0, last = 0, type;
5972 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5974 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5975 if (last)
5976 TREE_CHAIN (last) = type;
5977 else
5978 actual = type;
5979 last = type;
5981 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5982 if (last)
5983 TREE_CHAIN (last) = type;
5984 else
5985 actual = type;
5987 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5988 of the type of this function, but we need to avoid having this
5989 affect the types of other similarly-typed functions, so we must
5990 first force the generation of an identical (but separate) type
5991 node for the relevant function type. The new node we create
5992 will be a variant of the main variant of the original function
5993 type. */
5995 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5997 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6001 /* Store the parameter declarations into the current function declaration.
6002 This is called after parsing the parameter declarations, before
6003 digesting the body of the function.
6005 For an old-style definition, construct a prototype out of the old-style
6006 parameter declarations and inject it into the function's type. */
6008 void
6009 store_parm_decls (void)
6011 tree fndecl = current_function_decl;
6013 /* The function containing FNDECL, if any. */
6014 tree context = decl_function_context (fndecl);
6016 /* True if this definition is written with a prototype. */
6017 bool prototype = (current_function_parms
6018 && TREE_CODE (current_function_parms) != TREE_LIST);
6020 if (prototype)
6021 store_parm_decls_newstyle ();
6022 else
6023 store_parm_decls_oldstyle ();
6025 /* The next call to pushlevel will be a function body. */
6027 next_is_function_body = true;
6029 /* Write a record describing this function definition to the prototypes
6030 file (if requested). */
6032 gen_aux_info_record (fndecl, 1, 0, prototype);
6034 /* Initialize the RTL code for the function. */
6035 init_function_start (fndecl);
6037 /* Begin the statement tree for this function. */
6038 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6040 /* If this is a nested function, save away the sizes of any
6041 variable-size types so that we can expand them when generating
6042 RTL. */
6043 if (context)
6045 tree t;
6047 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6048 = nreverse (get_pending_sizes ());
6049 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6051 t = TREE_CHAIN (t))
6052 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6055 /* This function is being processed in whole-function mode. */
6056 cfun->x_whole_function_mode_p = 1;
6058 /* Even though we're inside a function body, we still don't want to
6059 call expand_expr to calculate the size of a variable-sized array.
6060 We haven't necessarily assigned RTL to all variables yet, so it's
6061 not safe to try to expand expressions involving them. */
6062 immediate_size_expand = 0;
6063 cfun->x_dont_save_pending_sizes_p = 1;
6066 /* Finish up a function declaration and compile that function
6067 all the way to assembler language output. The free the storage
6068 for the function definition.
6070 This is called after parsing the body of the function definition.
6072 NESTED is nonzero if the function being finished is nested in another.
6073 CAN_DEFER_P is nonzero if the function may be deferred. */
6075 void
6076 finish_function (int nested, int can_defer_p)
6078 tree fndecl = current_function_decl;
6080 /* When a function declaration is totally empty, e.g.
6081 void foo(void) { }
6082 (the argument list is irrelevant) the compstmt rule will not
6083 bother calling pushlevel/poplevel, which means we get here with
6084 the scope stack out of sync. Detect this situation by noticing
6085 that current_scope is still as store_parm_decls left it, and do
6086 a dummy push/pop to get back to consistency.
6087 Note that the call to pushlevel does not actually push another
6088 scope - see there for details. */
6090 if (current_scope->parm_flag && next_is_function_body)
6092 pushlevel (0);
6093 poplevel (0, 0, 0);
6096 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6098 /* Must mark the RESULT_DECL as being in this function. */
6100 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6102 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6104 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6105 != integer_type_node)
6107 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6108 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6109 if (!warn_main)
6110 pedwarn ("%Hreturn type of '%D' is not `int'",
6111 &DECL_SOURCE_LOCATION (fndecl), fndecl);
6113 else
6115 #ifdef DEFAULT_MAIN_RETURN
6116 /* Make it so that `main' always returns success by default. */
6117 DEFAULT_MAIN_RETURN;
6118 #else
6119 if (flag_isoc99)
6120 c_expand_return (integer_zero_node);
6121 #endif
6125 finish_fname_decls ();
6127 /* Tie off the statement tree for this function. */
6128 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6130 /* Complain if there's just no return statement. */
6131 if (warn_return_type
6132 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6133 && !current_function_returns_value && !current_function_returns_null
6134 /* Don't complain if we abort. */
6135 && !current_function_returns_abnormally
6136 /* Don't warn for main(). */
6137 && !MAIN_NAME_P (DECL_NAME (fndecl))
6138 /* Or if they didn't actually specify a return type. */
6139 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6140 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6141 inline function, as we might never be compiled separately. */
6142 && DECL_INLINE (fndecl))
6143 warning ("no return statement in function returning non-void");
6145 /* Clear out memory we no longer need. */
6146 free_after_parsing (cfun);
6147 /* Since we never call rest_of_compilation, we never clear
6148 CFUN. Do so explicitly. */
6149 free_after_compilation (cfun);
6150 cfun = NULL;
6152 if (flag_unit_at_a_time && can_defer_p)
6154 cgraph_finalize_function (fndecl, DECL_SAVED_TREE (fndecl));
6155 current_function_decl = NULL;
6156 return;
6159 if (! nested)
6161 /* Function is parsed.
6162 Generate RTL for the body of this function or defer
6163 it for later expansion. */
6164 bool uninlinable = true;
6166 /* There's no reason to do any of the work here if we're only doing
6167 semantic analysis; this code just generates RTL. */
6168 if (flag_syntax_only)
6170 current_function_decl = NULL;
6171 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6172 return;
6175 if (flag_inline_trees)
6177 /* First, cache whether the current function is inlinable. Some
6178 predicates depend on cfun and current_function_decl to
6179 function completely. */
6180 timevar_push (TV_INTEGRATION);
6181 uninlinable = !tree_inlinable_function_p (fndecl);
6183 if (can_defer_p
6184 /* We defer functions marked inline *even if* the function
6185 itself is not inlinable. This is because we don't yet
6186 know if the function will actually be used; we may be
6187 able to avoid emitting it entirely. */
6188 && (!uninlinable || DECL_DECLARED_INLINE_P (fndecl))
6189 /* Save function tree for inlining. Should return 0 if the
6190 language does not support function deferring or the
6191 function could not be deferred. */
6192 && defer_fn (fndecl))
6194 /* Let the back-end know that this function exists. */
6195 (*debug_hooks->deferred_inline_function) (fndecl);
6196 timevar_pop (TV_INTEGRATION);
6197 current_function_decl = NULL;
6198 return;
6201 /* Then, inline any functions called in it. */
6202 optimize_inline_calls (fndecl);
6203 timevar_pop (TV_INTEGRATION);
6206 c_expand_body (fndecl);
6208 /* Keep the function body if it's needed for inlining or dumping. */
6209 if (uninlinable && !dump_enabled_p (TDI_all))
6211 /* Allow the body of the function to be garbage collected. */
6212 DECL_SAVED_TREE (fndecl) = NULL_TREE;
6215 /* Let the error reporting routines know that we're outside a
6216 function. For a nested function, this value is used in
6217 c_pop_function_context and then reset via pop_function_context. */
6218 current_function_decl = NULL;
6222 /* Generate the RTL for a deferred function FNDECL. */
6224 void
6225 c_expand_deferred_function (tree fndecl)
6227 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6228 function was deferred, e.g. in duplicate_decls. */
6229 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6231 if (flag_inline_trees)
6233 timevar_push (TV_INTEGRATION);
6234 optimize_inline_calls (fndecl);
6235 timevar_pop (TV_INTEGRATION);
6237 c_expand_body (fndecl);
6238 current_function_decl = NULL;
6242 /* Called to move the SAVE_EXPRs for parameter declarations in a
6243 nested function into the nested function. DATA is really the
6244 nested FUNCTION_DECL. */
6246 static tree
6247 set_save_expr_context (tree *tp,
6248 int *walk_subtrees,
6249 void *data)
6251 if (TREE_CODE (*tp) == SAVE_EXPR && !SAVE_EXPR_CONTEXT (*tp))
6252 SAVE_EXPR_CONTEXT (*tp) = (tree) data;
6253 /* Do not walk back into the SAVE_EXPR_CONTEXT; that will cause
6254 circularity. */
6255 else if (DECL_P (*tp))
6256 *walk_subtrees = 0;
6258 return NULL_TREE;
6261 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6262 then we are already in the process of generating RTL for another
6263 function. If can_defer_p is zero, we won't attempt to defer the
6264 generation of RTL. */
6266 static void
6267 c_expand_body_1 (tree fndecl, int nested_p)
6269 timevar_push (TV_EXPAND);
6271 if (nested_p)
6273 /* Make sure that we will evaluate variable-sized types involved
6274 in our function's type. */
6275 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6276 /* Squirrel away our current state. */
6277 push_function_context ();
6280 /* Initialize the RTL code for the function. */
6281 current_function_decl = fndecl;
6282 input_location = DECL_SOURCE_LOCATION (fndecl);
6283 init_function_start (fndecl);
6285 /* This function is being processed in whole-function mode. */
6286 cfun->x_whole_function_mode_p = 1;
6288 /* Even though we're inside a function body, we still don't want to
6289 call expand_expr to calculate the size of a variable-sized array.
6290 We haven't necessarily assigned RTL to all variables yet, so it's
6291 not safe to try to expand expressions involving them. */
6292 immediate_size_expand = 0;
6293 cfun->x_dont_save_pending_sizes_p = 1;
6295 /* Set up parameters and prepare for return, for the function. */
6296 expand_function_start (fndecl, 0);
6298 /* If the function has a variably modified type, there may be
6299 SAVE_EXPRs in the parameter types. Their context must be set to
6300 refer to this function; they cannot be expanded in the containing
6301 function. */
6302 if (decl_function_context (fndecl)
6303 && variably_modified_type_p (TREE_TYPE (fndecl)))
6304 walk_tree (&TREE_TYPE (fndecl), set_save_expr_context, fndecl,
6305 NULL);
6307 /* If this function is `main', emit a call to `__main'
6308 to run global initializers, etc. */
6309 if (DECL_NAME (fndecl)
6310 && MAIN_NAME_P (DECL_NAME (fndecl))
6311 && C_DECL_FILE_SCOPE (fndecl))
6312 expand_main_function ();
6314 /* Generate the RTL for this function. */
6315 expand_stmt (DECL_SAVED_TREE (fndecl));
6317 /* We hard-wired immediate_size_expand to zero above.
6318 expand_function_end will decrement this variable. So, we set the
6319 variable to one here, so that after the decrement it will remain
6320 zero. */
6321 immediate_size_expand = 1;
6323 /* Allow language dialects to perform special processing. */
6324 if (lang_expand_function_end)
6325 (*lang_expand_function_end) ();
6327 /* Generate rtl for function exit. */
6328 expand_function_end ();
6330 /* If this is a nested function, protect the local variables in the stack
6331 above us from being collected while we're compiling this function. */
6332 if (nested_p)
6333 ggc_push_context ();
6335 /* Run the optimizers and output the assembler code for this function. */
6336 rest_of_compilation (fndecl);
6338 /* Undo the GC context switch. */
6339 if (nested_p)
6340 ggc_pop_context ();
6342 /* With just -Wextra, complain only if function returns both with
6343 and without a value. */
6344 if (extra_warnings
6345 && current_function_returns_value
6346 && current_function_returns_null)
6347 warning ("this function may return with or without a value");
6349 /* If requested, warn about function definitions where the function will
6350 return a value (usually of some struct or union type) which itself will
6351 take up a lot of stack space. */
6353 if (warn_larger_than && !DECL_EXTERNAL (fndecl) && TREE_TYPE (fndecl))
6355 tree ret_type = TREE_TYPE (TREE_TYPE (fndecl));
6357 if (ret_type && TYPE_SIZE_UNIT (ret_type)
6358 && TREE_CODE (TYPE_SIZE_UNIT (ret_type)) == INTEGER_CST
6359 && 0 < compare_tree_int (TYPE_SIZE_UNIT (ret_type),
6360 larger_than_size))
6362 const location_t *locus = &DECL_SOURCE_LOCATION (fndecl);
6363 unsigned int size_as_int
6364 = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (ret_type));
6366 if (compare_tree_int (TYPE_SIZE_UNIT (ret_type), size_as_int) == 0)
6367 warning ("%Hsize of return value of '%D' is %u bytes",
6368 locus, fndecl, size_as_int);
6369 else
6370 warning ("%Hsize of return value of '%D' is larger than %wd bytes",
6371 locus, fndecl, larger_than_size);
6375 if (DECL_SAVED_INSNS (fndecl) == 0 && ! nested_p
6376 && ! flag_inline_trees)
6378 /* Stop pointing to the local nodes about to be freed.
6379 But DECL_INITIAL must remain nonzero so we know this
6380 was an actual function definition.
6381 For a nested function, this is done in c_pop_function_context.
6382 If rest_of_compilation set this to 0, leave it 0. */
6383 if (DECL_INITIAL (fndecl) != 0)
6384 DECL_INITIAL (fndecl) = error_mark_node;
6386 DECL_ARGUMENTS (fndecl) = 0;
6389 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6391 if (targetm.have_ctors_dtors)
6392 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6393 DEFAULT_INIT_PRIORITY);
6394 else
6395 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6398 if (DECL_STATIC_DESTRUCTOR (fndecl))
6400 if (targetm.have_ctors_dtors)
6401 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6402 DEFAULT_INIT_PRIORITY);
6403 else
6404 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6407 if (nested_p)
6408 /* Return to the enclosing function. */
6409 pop_function_context ();
6410 timevar_pop (TV_EXPAND);
6413 /* Like c_expand_body_1 but only for unnested functions. */
6415 void
6416 c_expand_body (tree fndecl)
6418 c_expand_body_1 (fndecl, 0);
6421 /* Check the declarations given in a for-loop for satisfying the C99
6422 constraints. */
6423 void
6424 check_for_loop_decls (void)
6426 tree t;
6428 if (!flag_isoc99)
6430 /* If we get here, declarations have been used in a for loop without
6431 the C99 for loop scope. This doesn't make much sense, so don't
6432 allow it. */
6433 error ("'for' loop initial declaration used outside C99 mode");
6434 return;
6436 /* C99 subclause 6.8.5 paragraph 3:
6438 [#3] The declaration part of a for statement shall only
6439 declare identifiers for objects having storage class auto or
6440 register.
6442 It isn't clear whether, in this sentence, "identifiers" binds to
6443 "shall only declare" or to "objects" - that is, whether all identifiers
6444 declared must be identifiers for objects, or whether the restriction
6445 only applies to those that are. (A question on this in comp.std.c
6446 in November 2000 received no answer.) We implement the strictest
6447 interpretation, to avoid creating an extension which later causes
6448 problems. */
6450 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6452 if (TREE_PURPOSE (t) != 0)
6454 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6456 if (code == RECORD_TYPE)
6457 error ("'struct %s' declared in 'for' loop initial declaration",
6458 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6459 else if (code == UNION_TYPE)
6460 error ("'union %s' declared in 'for' loop initial declaration",
6461 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6462 else
6463 error ("'enum %s' declared in 'for' loop initial declaration",
6464 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6468 for (t = getdecls (); t; t = TREE_CHAIN (t))
6470 const location_t *locus = &DECL_SOURCE_LOCATION (t);
6471 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6472 error ("%Hdeclaration of non-variable '%D' in 'for' loop "
6473 "initial declaration", locus, t);
6474 else if (TREE_STATIC (t))
6475 error ("%Hdeclaration of static variable '%D' in 'for' loop "
6476 "initial declaration", locus, t);
6477 else if (DECL_EXTERNAL (t))
6478 error ("%Hdeclaration of 'extern' variable '%D' in 'for' loop "
6479 "initial declaration", locus, t);
6483 /* Save and restore the variables in this file and elsewhere
6484 that keep track of the progress of compilation of the current function.
6485 Used for nested functions. */
6487 struct language_function GTY(())
6489 struct c_language_function base;
6490 int returns_value;
6491 int returns_null;
6492 int returns_abnormally;
6493 int warn_about_return_type;
6494 int extern_inline;
6497 /* Save and reinitialize the variables
6498 used during compilation of a C function. */
6500 void
6501 c_push_function_context (struct function *f)
6503 struct language_function *p;
6504 p = ggc_alloc (sizeof (struct language_function));
6505 f->language = p;
6507 p->base.x_stmt_tree = c_stmt_tree;
6508 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6509 p->returns_value = current_function_returns_value;
6510 p->returns_null = current_function_returns_null;
6511 p->returns_abnormally = current_function_returns_abnormally;
6512 p->warn_about_return_type = warn_about_return_type;
6513 p->extern_inline = current_extern_inline;
6516 /* Restore the variables used during compilation of a C function. */
6518 void
6519 c_pop_function_context (struct function *f)
6521 struct language_function *p = f->language;
6523 if (DECL_SAVED_INSNS (current_function_decl) == 0
6524 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6526 /* Stop pointing to the local nodes about to be freed. */
6527 /* But DECL_INITIAL must remain nonzero so we know this
6528 was an actual function definition. */
6529 DECL_INITIAL (current_function_decl) = error_mark_node;
6530 DECL_ARGUMENTS (current_function_decl) = 0;
6533 c_stmt_tree = p->base.x_stmt_tree;
6534 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6535 current_function_returns_value = p->returns_value;
6536 current_function_returns_null = p->returns_null;
6537 current_function_returns_abnormally = p->returns_abnormally;
6538 warn_about_return_type = p->warn_about_return_type;
6539 current_extern_inline = p->extern_inline;
6541 f->language = NULL;
6544 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6546 void
6547 c_dup_lang_specific_decl (tree decl)
6549 struct lang_decl *ld;
6551 if (!DECL_LANG_SPECIFIC (decl))
6552 return;
6554 ld = ggc_alloc (sizeof (struct lang_decl));
6555 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6556 DECL_LANG_SPECIFIC (decl) = ld;
6559 /* The functions below are required for functionality of doing
6560 function at once processing in the C front end. Currently these
6561 functions are not called from anywhere in the C front end, but as
6562 these changes continue, that will change. */
6564 /* Returns nonzero if the current statement is a full expression,
6565 i.e. temporaries created during that statement should be destroyed
6566 at the end of the statement. */
6569 stmts_are_full_exprs_p (void)
6571 return 0;
6574 /* Returns the stmt_tree (if any) to which statements are currently
6575 being added. If there is no active statement-tree, NULL is
6576 returned. */
6578 stmt_tree
6579 current_stmt_tree (void)
6581 return &c_stmt_tree;
6584 /* Returns the stack of SCOPE_STMTs for the current function. */
6586 tree *
6587 current_scope_stmt_stack (void)
6589 return &c_scope_stmt_stack;
6592 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6593 C. */
6596 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6598 return 0;
6601 /* Dummy function in place of callback used by C++. */
6603 void
6604 extract_interface_info (void)
6608 /* Return a new COMPOUND_STMT, after adding it to the current
6609 statement tree. */
6611 tree
6612 c_begin_compound_stmt (void)
6614 tree stmt;
6616 /* Create the COMPOUND_STMT. */
6617 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6619 return stmt;
6622 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6623 common code. */
6625 void
6626 c_expand_decl_stmt (tree t)
6628 tree decl = DECL_STMT_DECL (t);
6630 /* Expand nested functions. */
6631 if (TREE_CODE (decl) == FUNCTION_DECL
6632 && DECL_CONTEXT (decl) == current_function_decl
6633 && DECL_SAVED_TREE (decl))
6634 c_expand_body_1 (decl, 1);
6637 /* Return the global value of T as a symbol. */
6639 tree
6640 identifier_global_value (tree t)
6642 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6643 if (decl == 0 || C_DECL_FILE_SCOPE (decl))
6644 return decl;
6646 /* Shadowed by something else; find the true global value. */
6647 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6648 if (DECL_NAME (decl) == t)
6649 return decl;
6651 /* Only local values for this decl. */
6652 return 0;
6655 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6656 otherwise the name is found in ridpointers from RID_INDEX. */
6658 void
6659 record_builtin_type (enum rid rid_index, const char *name, tree type)
6661 tree id;
6662 if (name == 0)
6663 id = ridpointers[(int) rid_index];
6664 else
6665 id = get_identifier (name);
6666 pushdecl (build_decl (TYPE_DECL, id, type));
6669 /* Build the void_list_node (void_type_node having been created). */
6670 tree
6671 build_void_list_node (void)
6673 tree t = build_tree_list (NULL_TREE, void_type_node);
6674 return t;
6677 /* Return something to represent absolute declarators containing a *.
6678 TARGET is the absolute declarator that the * contains.
6679 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6680 to apply to the pointer type, represented as identifiers, possible mixed
6681 with attributes.
6683 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6684 if attributes are present) and whose type is the modifier list. */
6686 tree
6687 make_pointer_declarator (tree type_quals_attrs, tree target)
6689 tree quals, attrs;
6690 tree itarget = target;
6691 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6692 if (attrs != NULL_TREE)
6693 itarget = tree_cons (attrs, target, NULL_TREE);
6694 return build1 (INDIRECT_REF, quals, itarget);
6697 /* A wrapper around lhd_set_decl_assembler_name that gives static
6698 variables their C names if they are at file scope and only one
6699 translation unit is being compiled, for backwards compatibility
6700 with certain bizarre assembler hacks (like crtstuff.c). */
6702 void
6703 c_static_assembler_name (tree decl)
6705 if (num_in_fnames == 1
6706 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6707 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6708 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6709 else
6710 lhd_set_decl_assembler_name (decl);
6713 /* Hash and equality functions for link_hash_table: key off
6714 DECL_ASSEMBLER_NAME. */
6716 static hashval_t
6717 link_hash_hash (const void *x_p)
6719 tree x = (tree)x_p;
6720 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6723 static int
6724 link_hash_eq (const void *x1_p, const void *x2_p)
6726 tree x1 = (tree)x1_p;
6727 tree x2 = (tree)x2_p;
6728 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6731 /* Propagate information between definitions and uses between multiple
6732 translation units in TU_LIST based on linkage rules. */
6734 void
6735 merge_translation_unit_decls (void)
6737 const tree tu_list = current_file_decl;
6738 tree tu;
6739 tree decl;
6740 htab_t link_hash_table;
6741 tree block;
6743 /* Create the BLOCK that poplevel would have created, but don't
6744 actually call poplevel since that's expensive. */
6745 block = make_node (BLOCK);
6746 BLOCK_VARS (block) = current_scope->names;
6747 TREE_USED (block) = 1;
6748 DECL_INITIAL (current_file_decl) = block;
6750 /* If only one translation unit seen, no copying necessary. */
6751 if (TREE_CHAIN (tu_list) == NULL_TREE)
6752 return;
6754 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6756 /* Enter any actual definitions into the hash table. */
6757 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6758 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6759 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6761 PTR *slot;
6762 slot = htab_find_slot (link_hash_table, decl, INSERT);
6764 /* If we've already got a definition, work out which one is
6765 the real one, put it into the hash table, and make the
6766 other one DECL_EXTERNAL. This is important to avoid
6767 putting out two definitions of the same symbol in the
6768 assembly output. */
6769 if (*slot != NULL)
6771 tree old_decl = (tree) *slot;
6773 /* If this is weak or common or whatever, suppress it
6774 in favor of the other definition. */
6775 if (DECL_WEAK (decl))
6776 DECL_EXTERNAL (decl) = 1;
6777 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6778 DECL_EXTERNAL (old_decl) = 1;
6779 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6780 DECL_EXTERNAL (decl) = 1;
6781 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6782 DECL_EXTERNAL (old_decl) = 1;
6784 if (DECL_EXTERNAL (decl))
6786 DECL_INITIAL (decl) = NULL_TREE;
6787 DECL_COMMON (decl) = 0;
6788 DECL_ONE_ONLY (decl) = 0;
6789 DECL_WEAK (decl) = 0;
6791 else if (DECL_EXTERNAL (old_decl))
6793 DECL_INITIAL (old_decl) = NULL_TREE;
6794 DECL_COMMON (old_decl) = 0;
6795 DECL_ONE_ONLY (old_decl) = 0;
6796 DECL_WEAK (old_decl) = 0;
6797 *slot = decl;
6799 else
6801 error ("%Hredefinition of global '%D'",
6802 &DECL_SOURCE_LOCATION (decl), decl);
6803 error ("%H'%D' previously defined here",
6804 &DECL_SOURCE_LOCATION (old_decl), old_decl);
6807 else
6808 *slot = decl;
6811 /* Now insert the desired information from all the definitions
6812 into any plain declarations. */
6813 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6814 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6815 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6817 tree global_decl;
6818 global_decl = htab_find (link_hash_table, decl);
6820 if (! global_decl)
6821 continue;
6823 /* Print any appropriate error messages, and partially merge
6824 the decls. */
6825 (void) duplicate_decls (decl, global_decl, true, true);
6828 htab_delete (link_hash_table);
6831 /* Perform final processing on file-scope data. */
6833 void
6834 c_write_global_declarations(void)
6836 tree link;
6838 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6840 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6841 int len = list_length (globals);
6842 tree *vec = xmalloc (sizeof (tree) * len);
6843 int i;
6844 tree decl;
6846 /* Process the decls in the order they were written. */
6848 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6849 vec[i] = decl;
6851 wrapup_global_declarations (vec, len);
6853 check_global_declarations (vec, len);
6855 /* Clean up. */
6856 free (vec);
6860 /* Reset the parser's state in preparation for a new file. */
6862 void
6863 c_reset_state (void)
6865 tree link;
6866 tree file_scope_decl;
6868 /* Pop the global scope. */
6869 if (current_scope != global_scope)
6870 current_scope = global_scope;
6871 file_scope_decl = current_file_decl;
6872 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6873 truly_local_externals = NULL_TREE;
6875 /* Start a new global binding level. */
6876 pushlevel (0);
6877 global_scope = current_scope;
6878 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6879 TREE_CHAIN (current_file_decl) = file_scope_decl;
6881 /* Reintroduce the builtin declarations. */
6882 for (link = first_builtin_decl;
6883 link != TREE_CHAIN (last_builtin_decl);
6884 link = TREE_CHAIN (link))
6885 pushdecl (copy_node (link));
6888 #include "gt-c-decl.h"