2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / c-decl.c
blobdb04a877f1a1a3f7a8596cfe0e460dd3f56dabea
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 TYPENAME}; /* Typename (inside cast or sizeof) */
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
78 that case. */
80 static tree enum_next_value;
82 /* Nonzero means that there was overflow computing enum_next_value. */
84 static int enum_overflow;
86 /* Parsing a function declarator leaves a list of parameter names
87 or a chain of parameter decls here. */
89 static tree last_function_parms;
91 /* ... and a chain of structure and enum types declared in the
92 parmlist here. */
94 static tree last_function_parm_tags;
96 /* ... and a chain of all non-parameter declarations (such as
97 CONST_DECLs from enumerations) here. */
99 static tree last_function_parm_others;
101 /* After parsing the declarator that starts a function definition,
102 `start_function' puts the list of parameter names or chain of decls here
103 for `store_parm_decls' to find. */
105 static tree current_function_parms;
107 /* Similar, for last_function_parm_tags. */
109 static tree current_function_parm_tags;
111 /* And for last_function_parm_others. */
113 static tree current_function_parm_others;
115 /* Similar, for the file and line that the prototype came from if this is
116 an old-style definition. */
118 static location_t current_function_prototype_locus;
120 /* The current statement tree. */
122 static GTY(()) struct stmt_tree_s c_stmt_tree;
124 /* The current scope statement stack. */
126 static GTY(()) tree c_scope_stmt_stack;
128 /* State saving variables. */
129 int c_in_iteration_stmt;
130 int c_in_case_stmt;
132 /* A list of external DECLs that appeared at block scope when there was
133 some other global meaning for that identifier. */
134 static GTY(()) tree truly_local_externals;
136 /* All the builtins; this is a subset of the entries of global_scope. */
138 static GTY(()) tree first_builtin_decl;
139 static GTY(()) tree last_builtin_decl;
141 /* A DECL for the current file-scope context. */
143 static GTY(()) tree current_file_decl;
145 /* Set to 0 at beginning of a function definition, set to 1 if
146 a return statement that specifies a return value is seen. */
148 int current_function_returns_value;
150 /* Set to 0 at beginning of a function definition, set to 1 if
151 a return statement with no argument is seen. */
153 int current_function_returns_null;
155 /* Set to 0 at beginning of a function definition, set to 1 if
156 a call to a noreturn function is seen. */
158 int current_function_returns_abnormally;
160 /* Set to nonzero by `grokdeclarator' for a function
161 whose return type is defaulted, if warnings for this are desired. */
163 static int warn_about_return_type;
165 /* Nonzero when starting a function declared `extern inline'. */
167 static int current_extern_inline;
169 /* Each c_scope structure describes the complete contents of one scope.
170 Three scopes are distinguished specially: the innermost or current
171 scope, the innermost function scope, and the outermost or file scope.
173 Most declarations are recorded in the current scope.
175 All normal label declarations are recorded in the innermost
176 function scope, as are bindings of undeclared identifiers to
177 error_mark_node. (GCC permits nested functions as an extension,
178 hence the 'innermost' qualifier.) Explicitly declared labels
179 (using the __label__ extension) appear in the current scope.
181 Being in the global scope (current_scope == global_scope) causes
182 special behavior in several places below. Also, under some
183 conditions the Objective-C front end records declarations in the
184 global scope even though that isn't the current scope.
186 The order of the names, parms, and blocks lists matters, and they
187 are frequently appended to. To avoid having to walk all the way to
188 the end of the list on each insertion, or reverse the lists later,
189 we maintain a pointer to the last list entry for each of the lists.
191 The order of the tags, shadowed, and shadowed_tags
192 lists does not matter, so we just prepend to these lists. */
194 struct c_scope GTY(())
196 /* The scope containing this one. */
197 struct c_scope *outer;
199 /* The next outermost function scope. */
200 struct c_scope *outer_function;
202 /* All variables, constants, functions, labels, and typedef names. */
203 tree names;
204 tree names_last;
206 /* All parameter declarations. Used only in the outermost scope of
207 a function. */
208 tree parms;
209 tree parms_last;
211 /* All structure, union, and enum type tags. */
212 tree tags;
214 /* For each scope, a list of shadowed outer-scope definitions
215 to be restored when this scope is popped.
216 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
217 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
218 tree shadowed;
220 /* For each scope, a list of shadowed outer-scope tag definitions
221 to be restored when this scope is popped.
222 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
223 whose TREE_VALUE is its old definition (a kind of ..._TYPE node). */
224 tree shadowed_tags;
226 /* For each scope (except the global one), a chain of BLOCK nodes
227 for all the scopes that were entered and exited one level down. */
228 tree blocks;
229 tree blocks_last;
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, tree *);
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 check_bitfield_type_and_width (tree *, tree *, const char *);
328 static void clone_underlying_type (tree);
329 static bool flexible_array_type_p (tree);
330 static hashval_t link_hash_hash (const void *);
331 static int link_hash_eq (const void *, const void *);
333 /* States indicating how grokdeclarator() should handle declspecs marked
334 with __attribute__((deprecated)). An object declared as
335 __attribute__((deprecated)) suppresses warnings of uses of other
336 deprecated items. */
338 enum deprecated_states {
339 DEPRECATED_NORMAL,
340 DEPRECATED_SUPPRESS
343 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
345 void
346 c_print_identifier (FILE *file, tree node, int indent)
348 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
349 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
350 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
351 if (C_IS_RESERVED_WORD (node))
353 tree rid = ridpointers[C_RID_CODE (node)];
354 indent_to (file, indent + 4);
355 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
356 (void *) rid, IDENTIFIER_POINTER (rid));
360 /* Hook called at end of compilation to assume 1 elt
361 for a file-scope tentative array defn that wasn't complete before. */
363 void
364 c_finish_incomplete_decl (tree decl)
366 if (TREE_CODE (decl) == VAR_DECL)
368 tree type = TREE_TYPE (decl);
369 if (type != error_mark_node
370 && TREE_CODE (type) == ARRAY_TYPE
371 && ! DECL_EXTERNAL (decl)
372 && TYPE_DOMAIN (type) == 0)
374 warning ("%Jarray '%D' assumed to have one element", 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 /* The Objective-C front-end often needs to determine the current scope. */
419 void *
420 get_current_scope (void)
422 return current_scope;
425 /* The following function is used only by Objective-C. It needs to live here
426 because it accesses the innards of c_scope. */
428 void
429 objc_mark_locals_volatile (void *enclosing_blk)
431 struct c_scope *scope;
433 for (scope = current_scope;
434 scope && scope != enclosing_blk;
435 scope = scope->outer)
437 tree decl;
439 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
441 DECL_REGISTER (decl) = 0;
442 TREE_THIS_VOLATILE (decl) = 1;
444 /* Do not climb up past the current function. */
445 if (scope->function_body)
446 break;
450 /* Nonzero if we are currently in the global scope. */
453 global_bindings_p (void)
455 return current_scope == global_scope;
458 void
459 keep_next_level (void)
461 keep_next_level_flag = true;
464 /* Identify this scope as currently being filled with parameters. */
466 void
467 declare_parm_level (void)
469 current_scope->parm_flag = true;
472 /* Nonzero if currently making parm declarations. */
475 in_parm_level_p (void)
477 return current_scope->parm_flag;
480 /* Enter a new scope. The dummy parameter is for signature
481 compatibility with lang_hooks.decls.pushlevel. */
483 void
484 pushlevel (int dummy ATTRIBUTE_UNUSED)
486 if (next_is_function_body)
488 /* This is the transition from the parameters to the top level
489 of the function body. These are the same scope
490 (C99 6.2.1p4,6) so we do not push another scope structure.
491 next_is_function_body is set only by store_parm_decls, which
492 in turn is called when and only when we are about to
493 encounter the opening curly brace for the function body.
495 The outermost block of a function always gets a BLOCK node,
496 because the debugging output routines expect that each
497 function has at least one BLOCK. */
498 current_scope->parm_flag = false;
499 current_scope->function_body = true;
500 current_scope->keep = true;
501 current_scope->outer_function = current_function_scope;
502 current_function_scope = current_scope;
504 keep_next_level_flag = false;
505 next_is_function_body = false;
507 else
509 struct c_scope *scope = make_scope ();
511 scope->keep = keep_next_level_flag;
512 scope->outer = current_scope;
513 current_scope = scope;
514 keep_next_level_flag = false;
518 /* Exit a scope. Restore the state of the identifier-decl mappings
519 that were in effect when this scope was entered.
521 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
522 create a BLOCK node to record its declarations and subblocks for
523 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
524 or tags lists are nonempty.
526 The second parameter is ignored; it is present only for
527 signature compatibility with lang_hooks.decls.poplevel.
529 If FUNCTIONBODY is nonzero, this level is the body of a function,
530 even if current_scope->function_body is not set. This is used
531 by language-independent code that generates synthetic functions,
532 and cannot set current_scope->function_body.
534 FIXME: Eliminate the need for all arguments. */
536 tree
537 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
539 struct c_scope *scope = current_scope;
540 tree block;
541 tree decl;
542 tree p;
544 /* The following line does not use |= due to a bug in HP's C compiler. */
545 scope->function_body = scope->function_body | functionbody;
547 if (keep == KEEP_MAYBE)
548 keep = (scope->names || scope->tags);
550 keep |= scope->keep;
551 keep |= scope->function_body;
553 /* If appropriate, create a BLOCK to record the decls for the life
554 of this function. */
555 block = 0;
556 if (keep)
558 block = make_node (BLOCK);
559 BLOCK_VARS (block) = scope->names;
560 BLOCK_SUBBLOCKS (block) = scope->blocks;
561 TREE_USED (block) = 1;
564 /* In each subblock, record that this is its superior. */
565 for (p = scope->blocks; p; p = TREE_CHAIN (p))
566 BLOCK_SUPERCONTEXT (p) = block;
568 /* Clear out the variable bindings in this scope.
570 Propagate TREE_ADDRESSABLE from nested functions to their
571 containing functions.
573 Issue warnings for unused variables and labels, and errors for
574 undefined labels, if there are any. */
576 for (p = scope->names; p; p = TREE_CHAIN (p))
578 switch (TREE_CODE (p))
580 case LABEL_DECL:
581 if (TREE_USED (p) && !DECL_INITIAL (p))
583 error ("%Jlabel `%D' used but not defined", p, p);
584 DECL_INITIAL (p) = error_mark_node;
586 else if (!TREE_USED (p) && warn_unused_label)
588 if (DECL_INITIAL (p))
589 warning ("%Jlabel `%D' defined but not used", p, p);
590 else
591 warning ("%Jlabel `%D' declared but not defined", p, p);
594 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
595 break;
597 case FUNCTION_DECL:
598 if (! TREE_ASM_WRITTEN (p)
599 && DECL_INITIAL (p) != 0
600 && TREE_ADDRESSABLE (p)
601 && DECL_ABSTRACT_ORIGIN (p) != 0
602 && DECL_ABSTRACT_ORIGIN (p) != p)
603 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
604 goto normal;
606 case VAR_DECL:
607 /* Keep this in sync with stmt.c:warn_about_unused_variables.
608 No warnings when the global scope is popped because the
609 global scope isn't popped for the last translation unit,
610 so the warnings are done in c_write_global_declaration. */
611 if (warn_unused_variable && scope != global_scope
612 && !TREE_USED (p)
613 && !DECL_IN_SYSTEM_HEADER (p)
614 && DECL_NAME (p)
615 && !DECL_ARTIFICIAL (p))
616 warning ("%Junused variable `%D'", p, p);
617 /* fall through */
619 default:
620 normal:
621 if (DECL_NAME (p))
623 if (DECL_EXTERNAL (p) && scope != global_scope)
624 /* External decls stay in the symbol-value slot but are
625 inaccessible. */
626 C_DECL_INVISIBLE (p) = 1;
627 else
628 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
630 break;
634 /* Clear out the parameter bindings in this scope, if any.
635 Unused-parameter warnings are handled by function.c. */
636 for (p = scope->parms; p; p = TREE_CHAIN (p))
637 if (DECL_NAME (p))
638 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
640 /* Clear out the tag-meanings declared in this scope.
642 Set the TYPE_CONTEXTs for all of the tagged types belonging to
643 this scope so that they point to the appropriate construct, i.e.
644 either to the current FUNCTION_DECL node, or else to the BLOCK
645 node we just constructed.
647 Note that for tagged types whose scope is just the formal
648 parameter list for some function type specification, we can't
649 properly set their TYPE_CONTEXTs here, because we don't have a
650 pointer to the appropriate FUNCTION_TYPE node readily available
651 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
652 type nodes get set in `grokdeclarator' as soon as we have created
653 the FUNCTION_TYPE node which will represent the "scope" for these
654 "parameter list local" tagged types. */
656 decl = scope->function_body ? current_function_decl : block;
657 for (p = scope->tags; p; p = TREE_CHAIN (p))
659 if (TREE_PURPOSE (p))
660 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
661 if (decl)
662 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
665 /* Restore all name- and label-meanings from outer scopes that were
666 shadowed by this scope. */
667 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
668 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
669 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
670 else
671 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
673 /* Restore all tag-meanings from outer scopes that were shadowed by
674 this scope. */
675 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
676 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
678 /* Dispose of the block that we just made inside some higher level. */
679 if (scope->function_body && current_function_decl)
680 DECL_INITIAL (current_function_decl) = block;
681 else if (scope->outer)
683 if (block)
684 SCOPE_LIST_APPEND (scope->outer, blocks, block);
685 /* If we did not make a block for the scope just exited, any
686 blocks made for inner scopes must be carried forward so they
687 will later become subblocks of something else. */
688 else if (scope->blocks)
689 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
692 /* Pop the current scope, and free the structure for reuse. */
693 pop_scope ();
695 return block;
698 /* Insert BLOCK at the end of the list of subblocks of the current
699 scope. This is used when a BIND_EXPR is expanded, to handle the
700 BLOCK node inside the BIND_EXPR. */
702 void
703 insert_block (tree block)
705 TREE_USED (block) = 1;
706 SCOPE_LIST_APPEND (current_scope, blocks, block);
709 /* Set the BLOCK node for the innermost scope (the one we are
710 currently in). The RTL expansion machinery requires us to provide
711 this hook, but it is not useful in function-at-a-time mode. */
713 void
714 set_block (tree block ATTRIBUTE_UNUSED)
718 /* Push a definition or a declaration of struct, union or enum tag "name".
719 "type" should be the type node.
720 We assume that the tag "name" is not already defined.
722 Note that the definition may really be just a forward reference.
723 In that case, the TYPE_SIZE will be zero. */
725 void
726 pushtag (tree name, tree type)
728 struct c_scope *b = current_scope;
730 /* Record the identifier as the type's name if it has none. */
731 if (name)
733 if (TYPE_NAME (type) == 0)
734 TYPE_NAME (type) = name;
736 if (IDENTIFIER_TAG_VALUE (name))
737 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
738 b->shadowed_tags);
739 IDENTIFIER_TAG_VALUE (name) = type;
742 b->tags = tree_cons (name, type, b->tags);
744 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
745 tagged type we just added to the current scope. This fake
746 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
747 to output a representation of a tagged type, and it also gives
748 us a convenient place to record the "scope start" address for the
749 tagged type. */
751 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
753 /* An approximation for now, so we can tell this is a function-scope tag.
754 This will be updated in poplevel. */
755 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
758 /* Subroutine of duplicate_decls. Allow harmless mismatches in return
759 and argument types provided that the type modes match. This function
760 return a unified type given a suitable match, and 0 otherwise. */
762 static tree
763 match_builtin_function_types (tree oldtype, tree newtype)
765 tree newrettype, oldrettype;
766 tree newargs, oldargs;
767 tree trytype, tryargs;
769 /* Accept the return type of the new declaration if same modes. */
770 oldrettype = TREE_TYPE (oldtype);
771 newrettype = TREE_TYPE (newtype);
773 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
774 return 0;
776 oldargs = TYPE_ARG_TYPES (oldtype);
777 newargs = TYPE_ARG_TYPES (newtype);
778 tryargs = newargs;
780 while (oldargs || newargs)
782 if (! oldargs
783 || ! newargs
784 || ! TREE_VALUE (oldargs)
785 || ! TREE_VALUE (newargs)
786 || TYPE_MODE (TREE_VALUE (oldargs))
787 != TYPE_MODE (TREE_VALUE (newargs)))
788 return 0;
790 oldargs = TREE_CHAIN (oldargs);
791 newargs = TREE_CHAIN (newargs);
794 trytype = build_function_type (newrettype, tryargs);
795 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
798 /* Handle when a new declaration NEWDECL
799 has the same name as an old one OLDDECL
800 in the same binding contour.
801 Prints an error message if appropriate.
803 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
804 Otherwise, return 0.
806 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external declaration,
807 and OLDDECL is in an outer scope and should thus not be changed. */
809 static int
810 duplicate_decls (tree newdecl, tree olddecl, int different_binding_level,
811 int different_tu)
813 int types_match = comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl),
814 COMPARE_STRICT);
815 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
816 && DECL_INITIAL (newdecl) != 0);
817 tree oldtype = TREE_TYPE (olddecl);
818 tree newtype = TREE_TYPE (newdecl);
819 int errmsg = 0;
821 if (DECL_P (olddecl))
823 if (TREE_CODE (newdecl) == FUNCTION_DECL
824 && TREE_CODE (olddecl) == FUNCTION_DECL
825 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
827 if (DECL_DECLARED_INLINE_P (newdecl)
828 && DECL_UNINLINABLE (newdecl)
829 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
830 /* Already warned elsewhere. */;
831 else if (DECL_DECLARED_INLINE_P (olddecl)
832 && DECL_UNINLINABLE (olddecl)
833 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
834 /* Already warned. */;
835 else if (DECL_DECLARED_INLINE_P (newdecl)
836 && ! DECL_DECLARED_INLINE_P (olddecl)
837 && DECL_UNINLINABLE (olddecl)
838 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
840 warning ("%Jfunction '%D' redeclared as inline",
841 newdecl, newdecl);
842 warning ("%Jprevious declaration of function '%D' "
843 "with attribute noinline", olddecl, olddecl);
845 else if (DECL_DECLARED_INLINE_P (olddecl)
846 && DECL_UNINLINABLE (newdecl)
847 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
849 warning ("%Jfunction '%D' redeclared with attribute noinline",
850 newdecl, newdecl);
851 warning ("%Jprevious declaration of function '%D' was inline",
852 olddecl, olddecl);
856 DECL_ATTRIBUTES (newdecl)
857 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
860 if (TREE_CODE (newtype) == ERROR_MARK
861 || TREE_CODE (oldtype) == ERROR_MARK)
862 types_match = 0;
864 /* New decl is completely inconsistent with the old one =>
865 tell caller to replace the old one.
866 This is always an error except in the case of shadowing a builtin. */
867 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
869 if (TREE_CODE (olddecl) == FUNCTION_DECL
870 && DECL_BUILT_IN (olddecl))
872 /* If you declare a built-in or predefined function name as static,
873 the old definition is overridden,
874 but optionally warn this was a bad choice of name. */
875 if (!TREE_PUBLIC (newdecl))
877 if (warn_shadow)
878 warning ("%Jshadowing built-in function '%D'",
879 newdecl, newdecl);
881 else
882 warning ("%Jbuilt-in function '%D' declared as non-function",
883 newdecl, newdecl);
885 else
887 error ("%J'%D' redeclared as different kind of symbol",
888 newdecl, newdecl);
889 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
892 return 0;
895 /* For real parm decl following a forward decl, return 1 so old decl
896 will be reused. Only allow this to happen once. */
897 if (types_match && TREE_CODE (newdecl) == PARM_DECL
898 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
900 TREE_ASM_WRITTEN (olddecl) = 0;
901 return 1;
904 /* The new declaration is the same kind of object as the old one.
905 The declarations may partially match. Print warnings if they don't
906 match enough. Ultimately, copy most of the information from the new
907 decl to the old one, and keep using the old one. */
909 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
911 /* A function declaration for a built-in function. */
912 if (!TREE_PUBLIC (newdecl))
914 /* If you declare a built-in function name as static, the
915 built-in definition is overridden,
916 but optionally warn this was a bad choice of name. */
917 if (warn_shadow)
918 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
919 /* Discard the old built-in function. */
920 return 0;
922 if (!types_match)
924 /* Accept harmless mismatch in function types.
925 This is for the ffs and fprintf builtins. */
926 tree trytype = match_builtin_function_types (oldtype, newtype);
928 if (trytype)
930 types_match = comptypes (newtype, trytype, COMPARE_STRICT);
931 if (types_match)
932 oldtype = trytype;
933 if (! different_binding_level)
934 TREE_TYPE (olddecl) = oldtype;
937 if (!types_match)
939 /* If types don't match for a built-in, throw away the built-in. */
940 warning ("%Jconflicting types for built-in function '%D'",
941 newdecl, newdecl);
942 return 0;
945 else if (TREE_CODE (olddecl) == FUNCTION_DECL
946 && DECL_SOURCE_LINE (olddecl) == 0)
948 /* A function declaration for a predeclared function
949 that isn't actually built in. */
950 if (!TREE_PUBLIC (newdecl))
952 /* If you declare it as static, the
953 default definition is overridden. */
954 return 0;
956 else if (!types_match)
958 /* If the types don't match, preserve volatility indication.
959 Later on, we will discard everything else about the
960 default declaration. */
961 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
964 /* Permit char *foo () to match void *foo (...) if not pedantic,
965 if one of them came from a system header file. */
966 else if (!types_match
967 && TREE_CODE (olddecl) == FUNCTION_DECL
968 && TREE_CODE (newdecl) == FUNCTION_DECL
969 && TREE_CODE (TREE_TYPE (oldtype)) == POINTER_TYPE
970 && TREE_CODE (TREE_TYPE (newtype)) == POINTER_TYPE
971 && (DECL_IN_SYSTEM_HEADER (olddecl)
972 || DECL_IN_SYSTEM_HEADER (newdecl))
973 && ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (newtype))) == void_type_node
974 && TYPE_ARG_TYPES (oldtype) == 0
975 && self_promoting_args_p (TYPE_ARG_TYPES (newtype))
976 && TREE_TYPE (TREE_TYPE (oldtype)) == char_type_node)
978 (TREE_TYPE (TREE_TYPE (newtype)) == char_type_node
979 && TYPE_ARG_TYPES (newtype) == 0
980 && self_promoting_args_p (TYPE_ARG_TYPES (oldtype))
981 && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)))
983 if (pedantic)
984 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
985 /* Make sure we keep void * as ret type, not char *. */
986 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (oldtype))) == void_type_node)
987 TREE_TYPE (newdecl) = newtype = oldtype;
989 /* Set DECL_IN_SYSTEM_HEADER, so that if we see another declaration
990 we will come back here again. */
991 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
993 /* Permit void foo (...) to match int foo (...) if the latter is the
994 definition and implicit int was used. See c-torture/compile/920625-2.c. */
995 else if (!types_match && new_is_definition
996 && TREE_CODE (olddecl) == FUNCTION_DECL
997 && TREE_CODE (newdecl) == FUNCTION_DECL
998 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
999 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1000 && C_FUNCTION_IMPLICIT_INT (newdecl))
1002 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1003 /* Make sure we keep void as the return type. */
1004 TREE_TYPE (newdecl) = newtype = oldtype;
1005 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1007 else if (!types_match
1008 /* Permit char *foo (int, ...); followed by char *foo ();
1009 if not pedantic. */
1010 && ! (TREE_CODE (olddecl) == FUNCTION_DECL
1011 && ! pedantic
1012 /* Return types must still match. */
1013 && comptypes (TREE_TYPE (oldtype),
1014 TREE_TYPE (newtype), COMPARE_STRICT)
1015 && TYPE_ARG_TYPES (newtype) == 0))
1017 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1018 /* Check for function type mismatch
1019 involving an empty arglist vs a nonempty one. */
1020 if (TREE_CODE (olddecl) == FUNCTION_DECL
1021 && comptypes (TREE_TYPE (oldtype),
1022 TREE_TYPE (newtype), COMPARE_STRICT)
1023 && ((TYPE_ARG_TYPES (oldtype) == 0
1024 && DECL_INITIAL (olddecl) == 0)
1026 (TYPE_ARG_TYPES (newtype) == 0
1027 && DECL_INITIAL (newdecl) == 0)))
1029 /* Classify the problem further. */
1030 tree t = TYPE_ARG_TYPES (oldtype);
1031 if (t == 0)
1032 t = TYPE_ARG_TYPES (newtype);
1033 for (; t; t = TREE_CHAIN (t))
1035 tree type = TREE_VALUE (t);
1037 if (TREE_CHAIN (t) == 0
1038 && TYPE_MAIN_VARIANT (type) != void_type_node)
1040 error ("a parameter list with an ellipsis can't match an empty parameter name list declaration");
1041 break;
1044 if (c_type_promotes_to (type) != type)
1046 error ("an argument type that has a default promotion can't match an empty parameter name list declaration");
1047 break;
1051 if (C_DECL_IMPLICIT (olddecl))
1052 error ("%Jprevious implicit declaration of '%D'", olddecl, olddecl);
1053 else
1054 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1056 /* This is safer because the initializer might contain references
1057 to variables that were declared between olddecl and newdecl. This
1058 will make the initializer invalid for olddecl in case it gets
1059 assigned to olddecl below. */
1060 if (TREE_CODE (newdecl) == VAR_DECL)
1061 DECL_INITIAL (newdecl) = 0;
1063 /* TLS cannot follow non-TLS declaration. */
1064 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1065 && !DECL_THREAD_LOCAL (olddecl) && DECL_THREAD_LOCAL (newdecl))
1067 error ("%Jthread-local declaration of '%D' follows non thread-local "
1068 "declaration", newdecl, newdecl);
1069 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1071 /* non-TLS declaration cannot follow TLS declaration. */
1072 else if (TREE_CODE (olddecl) == VAR_DECL && TREE_CODE (newdecl) == VAR_DECL
1073 && DECL_THREAD_LOCAL (olddecl) && !DECL_THREAD_LOCAL (newdecl))
1075 error ("%Jnon thread-local declaration of '%D' follows "
1076 "thread-local declaration", newdecl, newdecl);
1077 error ("%Jprevious declaration of '%D'", olddecl, olddecl);
1079 else
1081 errmsg = redeclaration_error_message (newdecl, olddecl);
1082 if (errmsg)
1084 switch (errmsg)
1086 case 1:
1087 error ("%Jredefinition of '%D'", newdecl, newdecl);
1088 break;
1089 case 2:
1090 error ("%Jredeclaration of '%D'", newdecl, newdecl);
1091 break;
1092 case 3:
1093 error ("%Jconflicting declarations of '%D'", newdecl, newdecl);
1094 break;
1095 default:
1096 abort ();
1099 if (DECL_INITIAL (olddecl)
1100 && current_scope == global_scope)
1101 error ("%J'%D' previously defined here", olddecl, olddecl);
1102 else
1103 error ("%J'%D' previously declared here", olddecl, olddecl);
1104 return 0;
1106 else if (TREE_CODE (newdecl) == TYPE_DECL
1107 && (DECL_IN_SYSTEM_HEADER (olddecl)
1108 || DECL_IN_SYSTEM_HEADER (newdecl)))
1110 warning ("%Jredefinition of '%D'", newdecl, newdecl);
1111 if (DECL_INITIAL (olddecl) && current_scope == global_scope)
1112 warning ("%J'%D' previously defined here", olddecl, olddecl);
1113 else
1114 warning ("%J'%D' previously declared here", olddecl, olddecl);
1116 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1117 && DECL_INITIAL (olddecl) != 0
1118 && TYPE_ARG_TYPES (oldtype) == 0
1119 && TYPE_ARG_TYPES (newtype) != 0
1120 && TYPE_ACTUAL_ARG_TYPES (oldtype) != 0)
1122 tree type, parm;
1123 int nargs;
1124 /* Prototype decl follows defn w/o prototype. */
1126 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
1127 type = TYPE_ARG_TYPES (newtype),
1128 nargs = 1;
1130 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
1132 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1133 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1135 warning ("%Jprototype for '%D' follows", newdecl, newdecl);
1136 warning ("%Jnon-prototype definition here", olddecl);
1137 break;
1139 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
1140 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
1142 error ("%Jprototype for '%D' follows and number of "
1143 "arguments doesn't match", newdecl, newdecl);
1144 error ("%Jnon-prototype definition here", olddecl);
1145 errmsg = 1;
1146 break;
1148 /* Type for passing arg must be consistent
1149 with that declared for the arg. */
1150 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
1151 COMPARE_STRICT))
1153 error ("%Jprototype for '%D' follows and argument %d "
1154 "doesn't match", newdecl, newdecl, nargs);
1155 error ("%Jnon-prototype definition here", olddecl);
1156 errmsg = 1;
1157 break;
1161 /* Warn about mismatches in various flags. */
1162 else
1164 /* Warn if function is now inline
1165 but was previously declared not inline and has been called. */
1166 if (TREE_CODE (olddecl) == FUNCTION_DECL
1167 && ! DECL_DECLARED_INLINE_P (olddecl)
1168 && DECL_DECLARED_INLINE_P (newdecl)
1169 && TREE_USED (olddecl))
1170 warning ("%J'%D' declared inline after being called",
1171 newdecl, newdecl);
1172 if (TREE_CODE (olddecl) == FUNCTION_DECL
1173 && ! DECL_DECLARED_INLINE_P (olddecl)
1174 && DECL_DECLARED_INLINE_P (newdecl)
1175 && DECL_INITIAL (olddecl) != 0)
1176 warning ("%J'%D' declared inline after its definition",
1177 newdecl, newdecl);
1179 /* If pedantic, warn when static declaration follows a non-static
1180 declaration. Otherwise, do so only for functions. */
1181 if ((pedantic || TREE_CODE (olddecl) == FUNCTION_DECL)
1182 && TREE_PUBLIC (olddecl)
1183 && !TREE_PUBLIC (newdecl))
1184 warning ("%Jstatic declaration for '%D' follows non-static",
1185 newdecl, newdecl);
1187 /* If warn_traditional, warn when a non-static function
1188 declaration follows a static one. */
1189 if (warn_traditional && !in_system_header
1190 && TREE_CODE (olddecl) == FUNCTION_DECL
1191 && !TREE_PUBLIC (olddecl)
1192 && TREE_PUBLIC (newdecl))
1193 warning ("%Jnon-static declaration for '%D' follows static",
1194 newdecl, newdecl);
1196 /* Warn when const declaration follows a non-const
1197 declaration, but not for functions. */
1198 if (TREE_CODE (olddecl) != FUNCTION_DECL
1199 && !TREE_READONLY (olddecl)
1200 && TREE_READONLY (newdecl))
1201 warning ("%Jconst declaration for '%D' follows non-const",
1202 newdecl, newdecl);
1203 /* These bits are logically part of the type, for variables.
1204 But not for functions
1205 (where qualifiers are not valid ANSI anyway). */
1206 else if (pedantic && TREE_CODE (olddecl) != FUNCTION_DECL
1207 && (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
1208 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl)))
1209 pedwarn ("%Jtype qualifiers for '%D' conflict with previous "
1210 "declaration", newdecl, newdecl);
1214 /* Optionally warn about more than one declaration for the same name. */
1215 if (errmsg == 0 && warn_redundant_decls && DECL_SOURCE_LINE (olddecl) != 0
1216 /* Don't warn about a function declaration
1217 followed by a definition. */
1218 && !(TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl) != 0
1219 && DECL_INITIAL (olddecl) == 0)
1220 /* Don't warn about extern decl followed by (tentative) definition. */
1221 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
1223 warning ("%Jredundant redeclaration of '%D' in same scope",
1224 newdecl, newdecl);
1225 warning ("%Jprevious declaration of '%D'", olddecl, olddecl);
1228 /* Copy all the DECL_... slots specified in the new decl
1229 except for any that we copy here from the old type.
1231 Past this point, we don't change OLDTYPE and NEWTYPE
1232 even if we change the types of NEWDECL and OLDDECL. */
1234 if (types_match)
1236 /* When copying info to olddecl, we store into write_olddecl
1237 instead. This allows us to avoid modifying olddecl when
1238 different_binding_level is true. */
1239 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1241 /* Merge the data types specified in the two decls. */
1242 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1244 if (different_binding_level)
1246 if (TYPE_ARG_TYPES (oldtype) != 0
1247 && TYPE_ARG_TYPES (newtype) == 0)
1248 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1249 else
1250 TREE_TYPE (newdecl)
1251 = build_type_attribute_variant
1252 (newtype,
1253 merge_attributes (TYPE_ATTRIBUTES (newtype),
1254 TYPE_ATTRIBUTES (oldtype)));
1256 else
1257 TREE_TYPE (newdecl)
1258 = TREE_TYPE (olddecl)
1259 = common_type (newtype, oldtype);
1262 /* Lay the type out, unless already done. */
1263 if (oldtype != TREE_TYPE (newdecl))
1265 if (TREE_TYPE (newdecl) != error_mark_node)
1266 layout_type (TREE_TYPE (newdecl));
1267 if (TREE_CODE (newdecl) != FUNCTION_DECL
1268 && TREE_CODE (newdecl) != TYPE_DECL
1269 && TREE_CODE (newdecl) != CONST_DECL)
1270 layout_decl (newdecl, 0);
1272 else
1274 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1275 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1276 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1277 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1278 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1279 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1281 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1282 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1286 /* Keep the old rtl since we can safely use it. */
1287 COPY_DECL_RTL (olddecl, newdecl);
1289 /* Merge the type qualifiers. */
1290 if (TREE_READONLY (newdecl))
1291 TREE_READONLY (write_olddecl) = 1;
1293 if (TREE_THIS_VOLATILE (newdecl))
1295 TREE_THIS_VOLATILE (write_olddecl) = 1;
1296 if (TREE_CODE (newdecl) == VAR_DECL
1297 /* If an automatic variable is re-declared in the same
1298 function scope, but the old declaration was not
1299 volatile, make_var_volatile() would crash because the
1300 variable would have been assigned to a pseudo, not a
1301 MEM. Since this duplicate declaration is invalid
1302 anyway, we just skip the call. */
1303 && errmsg == 0)
1304 make_var_volatile (newdecl);
1307 /* Keep source location of definition rather than declaration. */
1308 /* When called with different_binding_level set, keep the old
1309 information so that meaningful diagnostics can be given. */
1310 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1311 && ! different_binding_level)
1312 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1314 /* Merge the unused-warning information. */
1315 if (DECL_IN_SYSTEM_HEADER (olddecl))
1316 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1317 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1318 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1320 /* Merge the initialization information. */
1321 /* When called with different_binding_level set, don't copy over
1322 DECL_INITIAL, so that we don't accidentally change function
1323 declarations into function definitions. */
1324 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1325 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1327 /* Merge the section attribute.
1328 We want to issue an error if the sections conflict but that must be
1329 done later in decl_attributes since we are called before attributes
1330 are assigned. */
1331 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1332 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1334 /* Copy the assembler name.
1335 Currently, it can only be defined in the prototype. */
1336 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1338 /* If either declaration has a nondefault visibility, use it. */
1339 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1341 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1342 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1344 warning ("%J'%D': visibility attribute ignored because it",
1345 newdecl, newdecl);
1346 warning ("%Jconflicts with previous declaration here", olddecl);
1348 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1351 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1353 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1354 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1355 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1356 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1357 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1358 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1359 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1360 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1361 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1364 /* If cannot merge, then use the new type and qualifiers,
1365 and don't preserve the old rtl. */
1366 else if (! different_binding_level)
1368 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1369 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1370 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1371 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1374 /* Merge the storage class information. */
1375 merge_weak (newdecl, olddecl);
1377 /* For functions, static overrides non-static. */
1378 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1380 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1381 /* This is since we don't automatically
1382 copy the attributes of NEWDECL into OLDDECL. */
1383 /* No need to worry about different_binding_level here because
1384 then TREE_PUBLIC (newdecl) was true. */
1385 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1386 /* If this clears `static', clear it in the identifier too. */
1387 if (! TREE_PUBLIC (olddecl))
1388 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1390 if (DECL_EXTERNAL (newdecl))
1392 if (! different_binding_level || different_tu)
1394 /* Don't mess with these flags on local externs; they remain
1395 external even if there's a declaration at file scope which
1396 isn't. */
1397 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1398 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1400 /* An extern decl does not override previous storage class. */
1401 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1402 if (! DECL_EXTERNAL (newdecl))
1404 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1405 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1406 /* If we have two non-EXTERNAL file-scope decls that are
1407 the same, only one of them should be written out. */
1408 if (different_tu)
1409 TREE_ASM_WRITTEN (newdecl) = 1;
1412 else
1414 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1415 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1418 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1420 /* If we're redefining a function previously defined as extern
1421 inline, make sure we emit debug info for the inline before we
1422 throw it away, in case it was inlined into a function that hasn't
1423 been written out yet. */
1424 if (new_is_definition && DECL_INITIAL (olddecl))
1426 if (TREE_USED (olddecl)
1427 /* In unit-at-a-time mode we never inline re-defined extern
1428 inline functions. */
1429 && !flag_unit_at_a_time)
1430 (*debug_hooks->outlining_inline_function) (olddecl);
1432 /* The new defn must not be inline. */
1433 DECL_INLINE (newdecl) = 0;
1434 DECL_UNINLINABLE (newdecl) = 1;
1436 else
1438 /* If either decl says `inline', this fn is inline,
1439 unless its definition was passed already. */
1440 if (DECL_DECLARED_INLINE_P (newdecl)
1441 || DECL_DECLARED_INLINE_P (olddecl))
1442 DECL_DECLARED_INLINE_P (newdecl) = 1;
1444 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1445 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1448 if (DECL_BUILT_IN (olddecl))
1450 /* Get rid of any built-in function if new arg types don't match it
1451 or if we have a function definition. */
1452 if (! types_match || new_is_definition)
1454 if (! different_binding_level)
1456 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1457 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1460 else
1462 /* If redeclaring a builtin function, and not a definition,
1463 it stays built in. */
1464 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1465 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1469 /* Also preserve various other info from the definition. */
1470 if (! new_is_definition)
1472 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1473 /* When called with different_binding_level set, don't copy over
1474 DECL_INITIAL, so that we don't accidentally change function
1475 declarations into function definitions. */
1476 if (! different_binding_level)
1477 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1478 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1479 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1480 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1481 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1483 /* Set DECL_INLINE on the declaration if we've got a body
1484 from which to instantiate. */
1485 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1487 DECL_INLINE (newdecl) = 1;
1488 DECL_ABSTRACT_ORIGIN (newdecl)
1489 = (different_binding_level
1490 ? DECL_ORIGIN (olddecl)
1491 : DECL_ABSTRACT_ORIGIN (olddecl));
1494 else
1496 /* If a previous declaration said inline, mark the
1497 definition as inlinable. */
1498 if (DECL_DECLARED_INLINE_P (newdecl)
1499 && ! DECL_UNINLINABLE (newdecl))
1500 DECL_INLINE (newdecl) = 1;
1503 if (different_binding_level)
1504 return 0;
1506 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1507 But preserve OLDDECL's DECL_UID. */
1509 unsigned olddecl_uid = DECL_UID (olddecl);
1511 memcpy ((char *) olddecl + sizeof (struct tree_common),
1512 (char *) newdecl + sizeof (struct tree_common),
1513 sizeof (struct tree_decl) - sizeof (struct tree_common));
1514 DECL_UID (olddecl) = olddecl_uid;
1517 /* NEWDECL contains the merged attribute lists.
1518 Update OLDDECL to be the same. */
1519 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1521 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1522 so that encode_section_info has a chance to look at the new decl
1523 flags and attributes. */
1524 if (DECL_RTL_SET_P (olddecl)
1525 && (TREE_CODE (olddecl) == FUNCTION_DECL
1526 || (TREE_CODE (olddecl) == VAR_DECL
1527 && TREE_STATIC (olddecl))))
1528 make_decl_rtl (olddecl, NULL);
1530 return 1;
1533 /* Return any external DECL associated with ID, whether or not it is
1534 currently in scope. */
1536 static tree
1537 any_external_decl (tree id)
1539 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1540 tree t;
1542 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1543 return 0;
1544 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1545 return decl;
1547 t = purpose_member (id, truly_local_externals);
1548 if (t)
1549 return TREE_VALUE (t);
1551 return 0;
1554 /* Record an external decl DECL. This only does something if a
1555 shadowing decl already exists. */
1556 static void
1557 record_external_decl (tree decl)
1559 tree name = DECL_NAME (decl);
1560 if (!IDENTIFIER_SYMBOL_VALUE (name))
1561 return;
1563 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1566 /* Check whether decl-node X shadows an existing declaration.
1567 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1568 which might be a NULL_TREE. */
1569 static void
1570 warn_if_shadowing (tree x, tree old)
1572 const char *name;
1574 /* Nothing to shadow? */
1575 if (old == 0
1576 /* Shadow warnings not wanted? */
1577 || !warn_shadow
1578 /* No shadow warnings for internally generated vars. */
1579 || DECL_SOURCE_LINE (x) == 0
1580 /* No shadow warnings for vars made for inlining. */
1581 || DECL_FROM_INLINE (x)
1582 /* Don't warn about the parm names in function declarator
1583 within a function declarator.
1584 It would be nice to avoid warning in any function
1585 declarator in a declaration, as opposed to a definition,
1586 but there is no way to tell it's not a definition. */
1587 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1588 return;
1590 name = IDENTIFIER_POINTER (DECL_NAME (x));
1591 if (TREE_CODE (old) == PARM_DECL)
1592 shadow_warning (SW_PARAM, name, old);
1593 else if (DECL_FILE_SCOPE_P (old))
1594 shadow_warning (SW_GLOBAL, name, old);
1595 else
1596 shadow_warning (SW_LOCAL, name, old);
1600 /* Subroutine of pushdecl.
1602 X is a TYPE_DECL for a typedef statement. Create a brand new
1603 ..._TYPE node (which will be just a variant of the existing
1604 ..._TYPE node with identical properties) and then install X
1605 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1607 The whole point here is to end up with a situation where each
1608 and every ..._TYPE node the compiler creates will be uniquely
1609 associated with AT MOST one node representing a typedef name.
1610 This way, even though the compiler substitutes corresponding
1611 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1612 early on, later parts of the compiler can always do the reverse
1613 translation and get back the corresponding typedef name. For
1614 example, given:
1616 typedef struct S MY_TYPE;
1617 MY_TYPE object;
1619 Later parts of the compiler might only know that `object' was of
1620 type `struct S' if it were not for code just below. With this
1621 code however, later parts of the compiler see something like:
1623 struct S' == struct S
1624 typedef struct S' MY_TYPE;
1625 struct S' object;
1627 And they can then deduce (from the node for type struct S') that
1628 the original object declaration was:
1630 MY_TYPE object;
1632 Being able to do this is important for proper support of protoize,
1633 and also for generating precise symbolic debugging information
1634 which takes full account of the programmer's (typedef) vocabulary.
1636 Obviously, we don't want to generate a duplicate ..._TYPE node if
1637 the TYPE_DECL node that we are now processing really represents a
1638 standard built-in type.
1640 Since all standard types are effectively declared at line zero
1641 in the source file, we can easily check to see if we are working
1642 on a standard type by checking the current value of lineno. */
1644 static void
1645 clone_underlying_type (tree x)
1647 if (DECL_SOURCE_LINE (x) == 0)
1649 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1650 TYPE_NAME (TREE_TYPE (x)) = x;
1652 else if (TREE_TYPE (x) != error_mark_node
1653 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1655 tree tt = TREE_TYPE (x);
1656 DECL_ORIGINAL_TYPE (x) = tt;
1657 tt = build_type_copy (tt);
1658 TYPE_NAME (tt) = x;
1659 TREE_USED (tt) = TREE_USED (x);
1660 TREE_TYPE (x) = tt;
1664 /* Record a decl-node X as belonging to the current lexical scope.
1665 Check for errors (such as an incompatible declaration for the same
1666 name already seen in the same scope).
1668 Returns either X or an old decl for the same name.
1669 If an old decl is returned, it may have been smashed
1670 to agree with what X says. */
1672 tree
1673 pushdecl (tree x)
1675 tree name = DECL_NAME (x);
1676 struct c_scope *scope = current_scope;
1678 #ifdef ENABLE_CHECKING
1679 if (error_mark_node == 0)
1680 /* Called too early. */
1681 abort ();
1682 #endif
1684 /* Functions need the lang_decl data. */
1685 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1686 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1688 /* A local extern declaration for a function doesn't constitute nesting.
1689 A local auto declaration does, since it's a forward decl
1690 for a nested function coming later. */
1691 if (current_function_decl == NULL
1692 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1693 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1694 DECL_CONTEXT (x) = current_file_decl;
1695 else
1696 DECL_CONTEXT (x) = current_function_decl;
1698 if (name)
1700 tree old;
1702 if (warn_nested_externs
1703 && scope != global_scope
1704 && DECL_EXTERNAL (x)
1705 && !DECL_IN_SYSTEM_HEADER (x))
1706 warning ("nested extern declaration of `%s'",
1707 IDENTIFIER_POINTER (name));
1709 old = lookup_name_current_level (name);
1710 if (old && duplicate_decls (x, old, 0, false))
1712 /* For PARM_DECLs, old may be a forward declaration.
1713 If so, we want to remove it from its old location
1714 (in the variables chain) and rechain it in the
1715 location given by the new declaration. */
1716 if (TREE_CODE (x) == PARM_DECL)
1718 tree *p;
1719 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1720 if (*p == old)
1722 *p = TREE_CHAIN (old);
1723 SCOPE_LIST_APPEND (scope, parms, old);
1724 break;
1727 return old;
1729 if (DECL_EXTERNAL (x) || scope == global_scope)
1731 /* Find and check against a previous, not-in-scope, external
1732 decl for this identifier. (C99 6.2.7p2: All declarations
1733 that refer to the same object or function shall have
1734 compatible type; otherwise, the behavior is undefined.) */
1735 tree ext = any_external_decl (name);
1736 if (ext)
1738 if (duplicate_decls (x, ext, scope != global_scope,
1739 false))
1740 x = copy_node (ext);
1742 else
1743 record_external_decl (x);
1746 if (TREE_CODE (x) == TYPE_DECL)
1747 clone_underlying_type (x);
1749 /* If storing a local value, there may already be one
1750 (inherited). If so, record it for restoration when this
1751 scope ends. Take care not to do this if we are replacing an
1752 older decl in the same scope (i.e. duplicate_decls returned
1753 false, above). */
1754 if (scope != global_scope
1755 && IDENTIFIER_SYMBOL_VALUE (name)
1756 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1758 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1759 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1760 scope->shadowed);
1763 /* Install the new declaration in the requested scope. */
1764 IDENTIFIER_SYMBOL_VALUE (name) = x;
1765 C_DECL_INVISIBLE (x) = 0;
1767 /* If x's type is incomplete because it's based on a
1768 structure or union which has not yet been fully declared,
1769 attach it to that structure or union type, so we can go
1770 back and complete the variable declaration later, if the
1771 structure or union gets fully declared.
1773 If the input is erroneous, we can have error_mark in the type
1774 slot (e.g. "f(void a, ...)") - that doesn't count as an
1775 incomplete type. */
1776 if (TREE_TYPE (x) != error_mark_node
1777 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1779 tree element = TREE_TYPE (x);
1781 while (TREE_CODE (element) == ARRAY_TYPE)
1782 element = TREE_TYPE (element);
1783 element = TYPE_MAIN_VARIANT (element);
1785 if ((TREE_CODE (element) == RECORD_TYPE
1786 || TREE_CODE (element) == UNION_TYPE)
1787 && (TREE_CODE (x) != TYPE_DECL
1788 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1789 && !COMPLETE_TYPE_P (element))
1790 C_TYPE_INCOMPLETE_VARS (element)
1791 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1795 if (TREE_CODE (x) == PARM_DECL)
1796 SCOPE_LIST_APPEND (scope, parms, x);
1797 else
1798 SCOPE_LIST_APPEND (scope, names, x);
1800 return x;
1803 /* Record X as belonging to the global scope (C99 "file scope").
1804 This is used only internally by the Objective-C front end,
1805 and is limited to its needs. duplicate_decls is not called;
1806 if there is any preexisting decl for this identifier, it is an ICE. */
1808 tree
1809 pushdecl_top_level (tree x)
1811 tree name;
1813 if (TREE_CODE (x) != VAR_DECL)
1814 abort ();
1816 name = DECL_NAME (x);
1818 if (IDENTIFIER_SYMBOL_VALUE (name))
1819 abort ();
1821 DECL_CONTEXT (x) = current_file_decl;
1822 IDENTIFIER_SYMBOL_VALUE (name) = x;
1824 SCOPE_LIST_APPEND (global_scope, names, x);
1825 return x;
1828 /* Generate an implicit declaration for identifier FUNCTIONID as a
1829 function of type int (). */
1831 tree
1832 implicitly_declare (tree functionid)
1834 tree decl = any_external_decl (functionid);
1836 if (decl)
1838 /* Implicit declaration of a function already declared
1839 (somehow) in a different scope, or as a built-in.
1840 If this is the first time this has happened, warn;
1841 then recycle the old declaration. */
1842 if (!C_DECL_IMPLICIT (decl))
1844 implicit_decl_warning (DECL_NAME (decl));
1845 if (! DECL_FILE_SCOPE_P (decl))
1846 warning ("%Jprevious declaration of '%D'", decl, decl);
1847 C_DECL_IMPLICIT (decl) = 1;
1849 /* If this function is global, then it must already be in the
1850 global scope, so there's no need to push it again. */
1851 if (current_scope == global_scope)
1852 return decl;
1853 /* If this is a local declaration, make a copy; we can't have
1854 the same DECL listed in two different scopes. */
1855 return pushdecl (copy_node (decl));
1858 /* Not seen before. */
1859 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1860 DECL_EXTERNAL (decl) = 1;
1861 TREE_PUBLIC (decl) = 1;
1862 C_DECL_IMPLICIT (decl) = 1;
1863 implicit_decl_warning (functionid);
1865 /* C89 says implicit declarations are in the innermost block.
1866 So we record the decl in the standard fashion. */
1867 decl = pushdecl (decl);
1869 /* No need to call objc_check_decl here - it's a function type. */
1870 rest_of_decl_compilation (decl, NULL, 0, 0);
1872 /* Write a record describing this implicit function declaration
1873 to the prototypes file (if requested). */
1874 gen_aux_info_record (decl, 0, 1, 0);
1876 /* Possibly apply some default attributes to this implicit declaration. */
1877 decl_attributes (&decl, NULL_TREE, 0);
1879 return decl;
1882 static void
1883 implicit_decl_warning (tree id)
1885 const char *name = IDENTIFIER_POINTER (id);
1886 if (mesg_implicit_function_declaration == 2)
1887 error ("implicit declaration of function `%s'", name);
1888 else if (mesg_implicit_function_declaration == 1)
1889 warning ("implicit declaration of function `%s'", name);
1892 /* Return zero if the declaration NEWDECL is valid
1893 when the declaration OLDDECL (assumed to be for the same name)
1894 has already been seen.
1895 Otherwise return 1 if NEWDECL is a redefinition, 2 if it is a redeclaration,
1896 and 3 if it is a conflicting declaration. */
1898 static int
1899 redeclaration_error_message (tree newdecl, tree olddecl)
1901 if (TREE_CODE (newdecl) == TYPE_DECL)
1903 /* Do not complain about type redeclarations where at least one
1904 declaration was in a system header. */
1905 if (DECL_IN_SYSTEM_HEADER (olddecl) || DECL_IN_SYSTEM_HEADER (newdecl))
1906 return 0;
1907 return 1;
1909 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1911 /* Declarations of functions can insist on internal linkage
1912 but they can't be inconsistent with internal linkage,
1913 so there can be no error on that account.
1914 However defining the same name twice is no good. */
1915 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0
1916 /* However, defining once as extern inline and a second
1917 time in another way is ok. */
1918 && ! (DECL_DECLARED_INLINE_P (olddecl) && DECL_EXTERNAL (olddecl)
1919 && ! (DECL_DECLARED_INLINE_P (newdecl)
1920 && DECL_EXTERNAL (newdecl))))
1921 return 1;
1922 return 0;
1924 else if (DECL_FILE_SCOPE_P (newdecl))
1926 /* Objects declared at file scope: */
1927 /* If at least one is a reference, it's ok. */
1928 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
1929 return 0;
1930 /* Reject two definitions. */
1931 if (DECL_INITIAL (olddecl) != 0 && DECL_INITIAL (newdecl) != 0)
1932 return 1;
1933 /* Now we have two tentative defs, or one tentative and one real def. */
1934 /* Insist that the linkage match. */
1935 if (TREE_PUBLIC (olddecl) != TREE_PUBLIC (newdecl))
1936 return 3;
1937 return 0;
1939 else if (current_scope->parm_flag
1940 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1941 return 0;
1942 else
1944 /* Newdecl has block scope. If olddecl has block scope also, then
1945 reject two definitions, and reject a definition together with an
1946 external reference. Otherwise, it is OK, because newdecl must
1947 be an extern reference to olddecl. */
1948 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))
1949 && DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl))
1950 return 2;
1951 return 0;
1955 /* Issue an error message for a reference to an undeclared variable
1956 ID, including a reference to a builtin outside of function-call
1957 context. Establish a binding of the identifier to error_mark_node
1958 in an appropriate scope, which will suppress further errors for the
1959 same identifier. */
1960 void
1961 undeclared_variable (tree id)
1963 static bool already = false;
1964 struct c_scope *scope;
1966 if (current_function_decl == 0)
1968 error ("`%s' undeclared here (not in a function)",
1969 IDENTIFIER_POINTER (id));
1970 scope = current_scope;
1972 else
1974 error ("`%s' undeclared (first use in this function)",
1975 IDENTIFIER_POINTER (id));
1977 if (! already)
1979 error ("(Each undeclared identifier is reported only once");
1980 error ("for each function it appears in.)");
1981 already = true;
1984 scope = current_function_scope;
1987 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1988 scope->shadowed);
1989 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1992 /* Subroutine of lookup_label, declare_label, define_label: construct a
1993 LABEL_DECL with all the proper frills. */
1995 static tree
1996 make_label (tree name, location_t location)
1998 tree label = build_decl (LABEL_DECL, name, void_type_node);
2000 DECL_CONTEXT (label) = current_function_decl;
2001 DECL_MODE (label) = VOIDmode;
2002 DECL_SOURCE_LOCATION (label) = location;
2004 return label;
2007 /* Another subroutine of lookup_label, declare_label, define_label:
2008 set up the binding of name to LABEL_DECL in the given SCOPE. */
2010 static void
2011 bind_label (tree name, tree label, struct c_scope *scope)
2013 if (IDENTIFIER_LABEL_VALUE (name))
2014 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
2015 scope->shadowed);
2016 IDENTIFIER_LABEL_VALUE (name) = label;
2018 SCOPE_LIST_APPEND (scope, names, label);
2021 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2022 Create one if none exists so far for the current function.
2023 This is called when a label is used in a goto expression or
2024 has its address taken. */
2026 tree
2027 lookup_label (tree name)
2029 tree label;
2031 if (current_function_decl == 0)
2033 error ("label %s referenced outside of any function",
2034 IDENTIFIER_POINTER (name));
2035 return 0;
2038 /* Use a label already defined or ref'd with this name, but not if
2039 it is inherited from a containing function and wasn't declared
2040 using __label__. */
2041 label = IDENTIFIER_LABEL_VALUE (name);
2042 if (label && (DECL_CONTEXT (label) == current_function_decl
2043 || C_DECLARED_LABEL_FLAG (label)))
2045 /* If the label has only been declared, update its apparent
2046 location to point here, for better diagnostics if it
2047 turns out not to have been defined. */
2048 if (!TREE_USED (label))
2049 DECL_SOURCE_LOCATION (label) = input_location;
2050 return label;
2053 /* No label binding for that identifier; make one. */
2054 label = make_label (name, input_location);
2056 /* Ordinary labels go in the current function scope. */
2057 bind_label (name, label, current_function_scope);
2058 return label;
2061 /* Make a label named NAME in the current function, shadowing silently
2062 any that may be inherited from containing functions or containing
2063 scopes. This is called for __label__ declarations. */
2065 /* Note that valid use, if the label being shadowed comes from another
2066 scope in the same function, requires calling declare_nonlocal_label
2067 right away. (Is this still true? -zw 2003-07-17) */
2069 tree
2070 declare_label (tree name)
2072 tree label = IDENTIFIER_LABEL_VALUE (name);
2073 tree dup;
2075 /* Check to make sure that the label hasn't already been declared
2076 at this scope */
2077 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2078 if (dup == label)
2080 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2081 error ("%Jthis is a previous declaration", dup);
2083 /* Just use the previous declaration. */
2084 return dup;
2087 label = make_label (name, input_location);
2088 C_DECLARED_LABEL_FLAG (label) = 1;
2090 /* Declared labels go in the current scope. */
2091 bind_label (name, label, current_scope);
2092 return label;
2095 /* Define a label, specifying the location in the source file.
2096 Return the LABEL_DECL node for the label, if the definition is valid.
2097 Otherwise return 0. */
2099 tree
2100 define_label (location_t location, tree name)
2102 tree label;
2104 /* Find any preexisting label with this name. It is an error
2105 if that label has already been defined in this function, or
2106 if there is a containing function with a declared label with
2107 the same name. */
2108 label = IDENTIFIER_LABEL_VALUE (name);
2110 if (label
2111 && ((DECL_CONTEXT (label) == current_function_decl
2112 && DECL_INITIAL (label) != 0)
2113 || (DECL_CONTEXT (label) != current_function_decl
2114 && C_DECLARED_LABEL_FLAG (label))))
2116 error ("%Hduplicate label `%D'", &location, label);
2117 if (DECL_INITIAL (label))
2118 error ("%J`%D' previously defined here", label, label);
2119 else
2120 error ("%J`%D' previously declared here", label, label);
2121 return 0;
2123 else if (label && DECL_CONTEXT (label) == current_function_decl)
2125 /* The label has been used or declared already in this function,
2126 but not defined. Update its location to point to this
2127 definition. */
2128 DECL_SOURCE_LOCATION (label) = location;
2130 else
2132 /* No label binding for that identifier; make one. */
2133 label = make_label (name, location);
2135 /* Ordinary labels go in the current function scope. */
2136 bind_label (name, label, current_function_scope);
2139 if (warn_traditional && !in_system_header && lookup_name (name))
2140 warning ("%Htraditional C lacks a separate namespace for labels, "
2141 "identifier `%s' conflicts", &location,
2142 IDENTIFIER_POINTER (name));
2144 /* Mark label as having been defined. */
2145 DECL_INITIAL (label) = error_mark_node;
2146 return label;
2149 /* Return the list of declarations of the current scope. */
2151 tree
2152 getdecls (void)
2154 return current_scope->names;
2158 /* Given NAME, an IDENTIFIER_NODE,
2159 return the structure (or union or enum) definition for that name.
2160 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2161 CODE says which kind of type the caller wants;
2162 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2163 If the wrong kind of type is found, an error is reported. */
2165 static tree
2166 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2168 tree tag = IDENTIFIER_TAG_VALUE (name);
2169 int thislevel = 0;
2171 if (!tag)
2172 return 0;
2174 /* We only care about whether it's in this level if
2175 thislevel_only was set or it might be a type clash. */
2176 if (thislevel_only || TREE_CODE (tag) != code)
2178 if (current_scope == global_scope
2179 || purpose_member (name, current_scope->tags))
2180 thislevel = 1;
2183 if (thislevel_only && !thislevel)
2184 return 0;
2186 if (TREE_CODE (tag) != code)
2188 /* Definition isn't the kind we were looking for. */
2189 pending_invalid_xref = name;
2190 pending_invalid_xref_location = input_location;
2192 /* If in the same binding level as a declaration as a tag
2193 of a different type, this must not be allowed to
2194 shadow that tag, so give the error immediately.
2195 (For example, "struct foo; union foo;" is invalid.) */
2196 if (thislevel)
2197 pending_xref_error ();
2199 return tag;
2202 /* Print an error message now
2203 for a recent invalid struct, union or enum cross reference.
2204 We don't print them immediately because they are not invalid
2205 when used in the `struct foo;' construct for shadowing. */
2207 void
2208 pending_xref_error (void)
2210 if (pending_invalid_xref != 0)
2211 error ("%H`%s' defined as wrong kind of tag",
2212 &pending_invalid_xref_location,
2213 IDENTIFIER_POINTER (pending_invalid_xref));
2214 pending_invalid_xref = 0;
2218 /* Look up NAME in the current scope and its superiors
2219 in the namespace of variables, functions and typedefs.
2220 Return a ..._DECL node of some kind representing its definition,
2221 or return 0 if it is undefined. */
2223 tree
2224 lookup_name (tree name)
2226 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2227 if (decl == 0 || decl == error_mark_node)
2228 return decl;
2229 if (C_DECL_INVISIBLE (decl))
2230 return 0;
2231 return decl;
2234 /* Similar to `lookup_name' but look only at the current scope. */
2236 static tree
2237 lookup_name_current_level (tree name)
2239 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2241 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2242 return 0;
2244 if (current_scope == global_scope)
2245 return decl;
2247 /* Scan the current scope for a decl with name NAME.
2248 For PARM_DECLs, we have to look at both ->parms and ->names, since
2249 forward parameter declarations wind up on the ->names list. */
2250 if (TREE_CODE (decl) == PARM_DECL
2251 && chain_member (decl, current_scope->parms))
2252 return decl;
2253 if (chain_member (decl, current_scope->names))
2254 return decl;
2256 return 0;
2259 /* Create the predefined scalar types of C,
2260 and some nodes representing standard constants (0, 1, (void *) 0).
2261 Initialize the global scope.
2262 Make definitions for built-in primitive functions. */
2264 void
2265 c_init_decl_processing (void)
2267 tree endlink;
2268 tree ptr_ftype_void, ptr_ftype_ptr;
2269 location_t save_loc = input_location;
2271 /* Adds some ggc roots, and reserved words for c-parse.in. */
2272 c_parse_init ();
2274 current_function_decl = 0;
2276 /* Make the c_scope structure for global names. */
2277 pushlevel (0);
2278 global_scope = current_scope;
2280 /* Declarations from c_common_nodes_and_builtins must not be associated
2281 with this input file, lest we get differences between using and not
2282 using preprocessed headers. */
2283 input_location.file = "<internal>";
2284 input_location.line = 0;
2286 /* Make the DECL for the toplevel file scope. */
2287 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2289 build_common_tree_nodes (flag_signed_char);
2291 c_common_nodes_and_builtins ();
2293 /* In C, comparisons and TRUTH_* expressions have type int. */
2294 truthvalue_type_node = integer_type_node;
2295 truthvalue_true_node = integer_one_node;
2296 truthvalue_false_node = integer_zero_node;
2298 /* Even in C99, which has a real boolean type. */
2299 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2300 boolean_type_node));
2302 endlink = void_list_node;
2303 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2304 ptr_ftype_ptr
2305 = build_function_type (ptr_type_node,
2306 tree_cons (NULL_TREE, ptr_type_node, endlink));
2308 input_location = save_loc;
2310 pedantic_lvalues = pedantic;
2312 make_fname_decl = c_make_fname_decl;
2313 start_fname_decls ();
2315 first_builtin_decl = global_scope->names;
2316 last_builtin_decl = global_scope->names_last;
2319 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2320 decl, NAME is the initialization string and TYPE_DEP indicates whether
2321 NAME depended on the type of the function. As we don't yet implement
2322 delayed emission of static data, we mark the decl as emitted
2323 so it is not placed in the output. Anything using it must therefore pull
2324 out the STRING_CST initializer directly. FIXME. */
2326 static tree
2327 c_make_fname_decl (tree id, int type_dep)
2329 const char *name = fname_as_string (type_dep);
2330 tree decl, type, init;
2331 size_t length = strlen (name);
2333 type = build_array_type
2334 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2335 build_index_type (size_int (length)));
2337 decl = build_decl (VAR_DECL, id, type);
2339 TREE_STATIC (decl) = 1;
2340 TREE_READONLY (decl) = 1;
2341 DECL_ARTIFICIAL (decl) = 1;
2343 init = build_string (length + 1, name);
2344 TREE_TYPE (init) = type;
2345 DECL_INITIAL (decl) = init;
2347 TREE_USED (decl) = 1;
2349 if (current_function_decl)
2351 DECL_CONTEXT (decl) = current_function_decl;
2352 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2353 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2356 finish_decl (decl, init, NULL_TREE);
2358 return decl;
2361 /* Return a definition for a builtin function named NAME and whose data type
2362 is TYPE. TYPE should be a function type with argument types.
2363 FUNCTION_CODE tells later passes how to compile calls to this function.
2364 See tree.h for its possible values.
2366 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2367 the name to be called if we can't opencode the function. If
2368 ATTRS is nonzero, use that for the function's attribute list. */
2370 tree
2371 builtin_function (const char *name, tree type, int function_code,
2372 enum built_in_class class, const char *library_name,
2373 tree attrs)
2375 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2376 DECL_EXTERNAL (decl) = 1;
2377 TREE_PUBLIC (decl) = 1;
2378 if (library_name)
2379 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2380 make_decl_rtl (decl, NULL);
2381 pushdecl (decl);
2382 DECL_BUILT_IN_CLASS (decl) = class;
2383 DECL_FUNCTION_CODE (decl) = function_code;
2385 /* Warn if a function in the namespace for users
2386 is used without an occasion to consider it declared. */
2387 if (name[0] != '_' || name[1] != '_')
2388 C_DECL_INVISIBLE (decl) = 1;
2390 /* Possibly apply some default attributes to this built-in function. */
2391 if (attrs)
2392 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2393 else
2394 decl_attributes (&decl, NULL_TREE, 0);
2396 return decl;
2399 /* Called when a declaration is seen that contains no names to declare.
2400 If its type is a reference to a structure, union or enum inherited
2401 from a containing scope, shadow that tag name for the current scope
2402 with a forward reference.
2403 If its type defines a new named structure or union
2404 or defines an enum, it is valid but we need not do anything here.
2405 Otherwise, it is an error. */
2407 void
2408 shadow_tag (tree declspecs)
2410 shadow_tag_warned (declspecs, 0);
2413 void
2414 shadow_tag_warned (tree declspecs, int warned)
2417 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2418 no pedwarn. */
2420 int found_tag = 0;
2421 tree link;
2422 tree specs, attrs;
2424 pending_invalid_xref = 0;
2426 /* Remove the attributes from declspecs, since they will confuse the
2427 following code. */
2428 split_specs_attrs (declspecs, &specs, &attrs);
2430 for (link = specs; link; link = TREE_CHAIN (link))
2432 tree value = TREE_VALUE (link);
2433 enum tree_code code = TREE_CODE (value);
2435 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2436 /* Used to test also that TYPE_SIZE (value) != 0.
2437 That caused warning for `struct foo;' at top level in the file. */
2439 tree name = TYPE_NAME (value);
2440 tree t;
2442 found_tag++;
2444 if (name == 0)
2446 if (warned != 1 && code != ENUMERAL_TYPE)
2447 /* Empty unnamed enum OK */
2449 pedwarn ("unnamed struct/union that defines no instances");
2450 warned = 1;
2453 else
2455 t = lookup_tag (code, name, 1);
2457 if (t == 0)
2459 t = make_node (code);
2460 pushtag (name, t);
2464 else
2466 if (!warned && ! in_system_header)
2468 warning ("useless keyword or type name in empty declaration");
2469 warned = 2;
2474 if (found_tag > 1)
2475 error ("two types specified in one empty declaration");
2477 if (warned != 1)
2479 if (found_tag == 0)
2480 pedwarn ("empty declaration");
2484 /* Construct an array declarator. EXPR is the expression inside [], or
2485 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2486 to the pointer to which a parameter array is converted). STATIC_P is
2487 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2488 is nonzero is the array is [*], a VLA of unspecified length which is
2489 nevertheless a complete type (not currently implemented by GCC),
2490 zero otherwise. The declarator is constructed as an ARRAY_REF
2491 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2492 left of the [] (filled by in set_array_declarator_type) and operand 1
2493 is the expression inside; whose TREE_TYPE is the type qualifiers and
2494 which has TREE_STATIC set if "static" is used. */
2496 tree
2497 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2499 tree decl;
2500 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2501 TREE_TYPE (decl) = quals;
2502 TREE_STATIC (decl) = (static_p ? 1 : 0);
2503 if (pedantic && !flag_isoc99)
2505 if (static_p || quals != NULL_TREE)
2506 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2507 if (vla_unspec_p)
2508 pedwarn ("ISO C90 does not support `[*]' array declarators");
2510 if (vla_unspec_p)
2511 warning ("GCC does not yet properly implement `[*]' array declarators");
2512 return decl;
2515 /* Set the type of an array declarator. DECL is the declarator, as
2516 constructed by build_array_declarator; TYPE is what appears on the left
2517 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2518 abstract declarator, zero otherwise; this is used to reject static and
2519 type qualifiers in abstract declarators, where they are not in the
2520 C99 grammar. */
2522 tree
2523 set_array_declarator_type (tree decl, tree type, int abstract_p)
2525 TREE_OPERAND (decl, 0) = type;
2526 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2527 error ("static or type qualifiers in abstract declarator");
2528 return decl;
2531 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2533 tree
2534 groktypename (tree typename)
2536 tree specs, attrs;
2538 if (TREE_CODE (typename) != TREE_LIST)
2539 return typename;
2541 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2543 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2544 NULL);
2546 /* Apply attributes. */
2547 decl_attributes (&typename, attrs, 0);
2549 return typename;
2552 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2554 tree
2555 groktypename_in_parm_context (tree typename)
2557 if (TREE_CODE (typename) != TREE_LIST)
2558 return typename;
2559 return grokdeclarator (TREE_VALUE (typename),
2560 TREE_PURPOSE (typename),
2561 PARM, 0, NULL);
2564 /* Decode a declarator in an ordinary declaration or data definition.
2565 This is called as soon as the type information and variable name
2566 have been parsed, before parsing the initializer if any.
2567 Here we create the ..._DECL node, fill in its type,
2568 and put it on the list of decls for the current context.
2569 The ..._DECL node is returned as the value.
2571 Exception: for arrays where the length is not specified,
2572 the type is left null, to be filled in by `finish_decl'.
2574 Function definitions do not come here; they go to start_function
2575 instead. However, external and forward declarations of functions
2576 do go through here. Structure field declarations are done by
2577 grokfield and not through here. */
2579 tree
2580 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2582 tree decl;
2583 tree tem;
2585 /* An object declared as __attribute__((deprecated)) suppresses
2586 warnings of uses of other deprecated items. */
2587 if (lookup_attribute ("deprecated", attributes))
2588 deprecated_state = DEPRECATED_SUPPRESS;
2590 decl = grokdeclarator (declarator, declspecs,
2591 NORMAL, initialized, NULL);
2593 deprecated_state = DEPRECATED_NORMAL;
2595 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2596 && MAIN_NAME_P (DECL_NAME (decl)))
2597 warning ("%J'%D' is usually a function", decl, decl);
2599 if (initialized)
2600 /* Is it valid for this decl to have an initializer at all?
2601 If not, set INITIALIZED to zero, which will indirectly
2602 tell `finish_decl' to ignore the initializer once it is parsed. */
2603 switch (TREE_CODE (decl))
2605 case TYPE_DECL:
2606 error ("typedef `%s' is initialized (use __typeof__ instead)",
2607 IDENTIFIER_POINTER (DECL_NAME (decl)));
2608 initialized = 0;
2609 break;
2611 case FUNCTION_DECL:
2612 error ("function `%s' is initialized like a variable",
2613 IDENTIFIER_POINTER (DECL_NAME (decl)));
2614 initialized = 0;
2615 break;
2617 case PARM_DECL:
2618 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2619 error ("parameter `%s' is initialized",
2620 IDENTIFIER_POINTER (DECL_NAME (decl)));
2621 initialized = 0;
2622 break;
2624 default:
2625 /* Don't allow initializations for incomplete types
2626 except for arrays which might be completed by the initialization. */
2628 /* This can happen if the array size is an undefined macro. We already
2629 gave a warning, so we don't need another one. */
2630 if (TREE_TYPE (decl) == error_mark_node)
2631 initialized = 0;
2632 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2634 /* A complete type is ok if size is fixed. */
2636 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2637 || C_DECL_VARIABLE_SIZE (decl))
2639 error ("variable-sized object may not be initialized");
2640 initialized = 0;
2643 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2645 error ("variable `%s' has initializer but incomplete type",
2646 IDENTIFIER_POINTER (DECL_NAME (decl)));
2647 initialized = 0;
2649 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2651 error ("elements of array `%s' have incomplete type",
2652 IDENTIFIER_POINTER (DECL_NAME (decl)));
2653 initialized = 0;
2657 if (initialized)
2659 DECL_EXTERNAL (decl) = 0;
2660 if (current_scope == global_scope)
2661 TREE_STATIC (decl) = 1;
2663 /* Tell `pushdecl' this is an initialized decl
2664 even though we don't yet have the initializer expression.
2665 Also tell `finish_decl' it may store the real initializer. */
2666 DECL_INITIAL (decl) = error_mark_node;
2669 /* If this is a function declaration, write a record describing it to the
2670 prototypes file (if requested). */
2672 if (TREE_CODE (decl) == FUNCTION_DECL)
2673 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2675 /* ANSI specifies that a tentative definition which is not merged with
2676 a non-tentative definition behaves exactly like a definition with an
2677 initializer equal to zero. (Section 3.7.2)
2679 -fno-common gives strict ANSI behavior, though this tends to break
2680 a large body of code that grew up without this rule.
2682 Thread-local variables are never common, since there's no entrenched
2683 body of code to break, and it allows more efficient variable references
2684 in the presence of dynamic linking. */
2686 if (TREE_CODE (decl) == VAR_DECL
2687 && !initialized
2688 && TREE_PUBLIC (decl)
2689 && !DECL_THREAD_LOCAL (decl)
2690 && !flag_no_common)
2691 DECL_COMMON (decl) = 1;
2693 /* Set attributes here so if duplicate decl, will have proper attributes. */
2694 decl_attributes (&decl, attributes, 0);
2696 if (TREE_CODE (decl) == FUNCTION_DECL
2697 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2699 tree ce = declarator;
2701 if (TREE_CODE (ce) == INDIRECT_REF)
2702 ce = TREE_OPERAND (declarator, 0);
2703 if (TREE_CODE (ce) == CALL_EXPR)
2705 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2706 for (; args; args = TREE_CHAIN (args))
2708 tree type = TREE_TYPE (args);
2709 if (INTEGRAL_TYPE_P (type)
2710 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2711 DECL_ARG_TYPE (args) = integer_type_node;
2716 if (TREE_CODE (decl) == FUNCTION_DECL
2717 && DECL_DECLARED_INLINE_P (decl)
2718 && DECL_UNINLINABLE (decl)
2719 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2720 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2722 /* Add this decl to the current scope.
2723 TEM may equal DECL or it may be a previous decl of the same name. */
2724 tem = pushdecl (decl);
2726 /* For a local variable, define the RTL now. */
2727 if (current_scope != global_scope
2728 /* But not if this is a duplicate decl
2729 and we preserved the rtl from the previous one
2730 (which may or may not happen). */
2731 && !DECL_RTL_SET_P (tem)
2732 && DECL_FILE_SCOPE_P (tem))
2734 if (TREE_TYPE (tem) != error_mark_node
2735 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2736 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2737 && DECL_INITIAL (tem) != 0)))
2738 expand_decl (tem);
2741 return tem;
2744 /* Finish processing of a declaration;
2745 install its initial value.
2746 If the length of an array type is not known before,
2747 it must be determined now, from the initial value, or it is an error. */
2749 void
2750 finish_decl (tree decl, tree init, tree asmspec_tree)
2752 tree type = TREE_TYPE (decl);
2753 int was_incomplete = (DECL_SIZE (decl) == 0);
2754 const char *asmspec = 0;
2756 /* If a name was specified, get the string. */
2757 if (current_scope == global_scope)
2758 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2759 if (asmspec_tree)
2760 asmspec = TREE_STRING_POINTER (asmspec_tree);
2762 /* If `start_decl' didn't like having an initialization, ignore it now. */
2763 if (init != 0 && DECL_INITIAL (decl) == 0)
2764 init = 0;
2766 /* Don't crash if parm is initialized. */
2767 if (TREE_CODE (decl) == PARM_DECL)
2768 init = 0;
2770 if (init)
2771 store_init_value (decl, init);
2773 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2774 || TREE_CODE (decl) == FUNCTION_DECL
2775 || TREE_CODE (decl) == FIELD_DECL))
2776 objc_check_decl (decl);
2778 /* Deduce size of array from initialization, if not already known. */
2779 if (TREE_CODE (type) == ARRAY_TYPE
2780 && TYPE_DOMAIN (type) == 0
2781 && TREE_CODE (decl) != TYPE_DECL)
2783 int do_default
2784 = (TREE_STATIC (decl)
2785 /* Even if pedantic, an external linkage array
2786 may have incomplete type at first. */
2787 ? pedantic && !TREE_PUBLIC (decl)
2788 : !DECL_EXTERNAL (decl));
2789 int failure
2790 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2792 /* Get the completed type made by complete_array_type. */
2793 type = TREE_TYPE (decl);
2795 if (failure == 1)
2796 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2798 else if (failure == 2)
2800 if (do_default)
2801 error ("%Jarray size missing in '%D'", decl, decl);
2802 /* If a `static' var's size isn't known,
2803 make it extern as well as static, so it does not get
2804 allocated.
2805 If it is not `static', then do not mark extern;
2806 finish_incomplete_decl will give it a default size
2807 and it will get allocated. */
2808 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2809 DECL_EXTERNAL (decl) = 1;
2812 /* TYPE_MAX_VALUE is always one less than the number of elements
2813 in the array, because we start counting at zero. Therefore,
2814 warn only if the value is less than zero. */
2815 else if (pedantic && TYPE_DOMAIN (type) != 0
2816 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2817 error ("%Jzero or negative size array '%D'", decl, decl);
2819 layout_decl (decl, 0);
2822 if (TREE_CODE (decl) == VAR_DECL)
2824 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2825 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2826 layout_decl (decl, 0);
2828 if (DECL_SIZE (decl) == 0
2829 /* Don't give an error if we already gave one earlier. */
2830 && TREE_TYPE (decl) != error_mark_node
2831 && (TREE_STATIC (decl)
2833 /* A static variable with an incomplete type
2834 is an error if it is initialized.
2835 Also if it is not file scope.
2836 Otherwise, let it through, but if it is not `extern'
2837 then it may cause an error message later. */
2838 (DECL_INITIAL (decl) != 0
2839 || !DECL_FILE_SCOPE_P (decl))
2841 /* An automatic variable with an incomplete type
2842 is an error. */
2843 !DECL_EXTERNAL (decl)))
2845 error ("%Jstorage size of '%D' isn't known", decl, decl);
2846 TREE_TYPE (decl) = error_mark_node;
2849 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2850 && DECL_SIZE (decl) != 0)
2852 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2853 constant_expression_warning (DECL_SIZE (decl));
2854 else
2855 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2858 if (TREE_USED (type))
2859 TREE_USED (decl) = 1;
2862 /* If this is a function and an assembler name is specified, reset DECL_RTL
2863 so we can give it its new name. Also, update built_in_decls if it
2864 was a normal built-in. */
2865 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2867 /* ASMSPEC is given, and not the name of a register. Mark the
2868 name with a star so assemble_name won't munge it. */
2869 char *starred = alloca (strlen (asmspec) + 2);
2870 starred[0] = '*';
2871 strcpy (starred + 1, asmspec);
2873 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2875 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2876 SET_DECL_RTL (builtin, NULL_RTX);
2877 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2878 #ifdef TARGET_MEM_FUNCTIONS
2879 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2880 init_block_move_fn (starred);
2881 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2882 init_block_clear_fn (starred);
2883 #else
2884 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2885 init_block_move_fn (starred);
2886 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2887 init_block_clear_fn (starred);
2888 #endif
2890 SET_DECL_RTL (decl, NULL_RTX);
2891 change_decl_assembler_name (decl, get_identifier (starred));
2894 /* If #pragma weak was used, mark the decl weak now. */
2895 if (current_scope == global_scope)
2896 maybe_apply_pragma_weak (decl);
2898 /* Output the assembler code and/or RTL code for variables and functions,
2899 unless the type is an undefined structure or union.
2900 If not, it will get done when the type is completed. */
2902 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2904 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2905 if (c_dialect_objc ())
2906 objc_check_decl (decl);
2908 if (DECL_FILE_SCOPE_P (decl))
2910 if (DECL_INITIAL (decl) == NULL_TREE
2911 || DECL_INITIAL (decl) == error_mark_node)
2912 /* Don't output anything
2913 when a tentative file-scope definition is seen.
2914 But at end of compilation, do output code for them. */
2915 DECL_DEFER_OUTPUT (decl) = 1;
2916 rest_of_decl_compilation (decl, asmspec, true, 0);
2918 else
2920 /* This is a local variable. If there is an ASMSPEC, the
2921 user has requested that we handle it specially. */
2922 if (asmspec)
2924 /* In conjunction with an ASMSPEC, the `register'
2925 keyword indicates that we should place the variable
2926 in a particular register. */
2927 if (DECL_REGISTER (decl))
2928 DECL_C_HARD_REGISTER (decl) = 1;
2930 /* If this is not a static variable, issue a warning.
2931 It doesn't make any sense to give an ASMSPEC for an
2932 ordinary, non-register local variable. Historically,
2933 GCC has accepted -- but ignored -- the ASMSPEC in
2934 this case. */
2935 if (TREE_CODE (decl) == VAR_DECL
2936 && !DECL_REGISTER (decl)
2937 && !TREE_STATIC (decl))
2938 warning ("%Jignoring asm-specifier for non-static local "
2939 "variable '%D'", decl, decl);
2940 else
2941 change_decl_assembler_name (decl, get_identifier (asmspec));
2944 if (TREE_CODE (decl) != FUNCTION_DECL)
2945 add_decl_stmt (decl);
2948 if (!DECL_FILE_SCOPE_P (decl))
2950 /* Recompute the RTL of a local array now
2951 if it used to be an incomplete type. */
2952 if (was_incomplete
2953 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2955 /* If we used it already as memory, it must stay in memory. */
2956 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2957 /* If it's still incomplete now, no init will save it. */
2958 if (DECL_SIZE (decl) == 0)
2959 DECL_INITIAL (decl) = 0;
2964 /* If this was marked 'used', be sure it will be output. */
2965 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2966 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2968 if (TREE_CODE (decl) == TYPE_DECL)
2969 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2971 /* At the end of a declaration, throw away any variable type sizes
2972 of types defined inside that declaration. There is no use
2973 computing them in the following function definition. */
2974 if (current_scope == global_scope)
2975 get_pending_sizes ();
2977 /* Install a cleanup (aka destructor) if one was given. */
2978 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2980 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2981 if (attr)
2983 static bool eh_initialized_p;
2985 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2986 tree cleanup_decl = lookup_name (cleanup_id);
2987 tree cleanup;
2989 /* Build "cleanup(&decl)" for the destructor. */
2990 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2991 cleanup = build_tree_list (NULL_TREE, cleanup);
2992 cleanup = build_function_call (cleanup_decl, cleanup);
2994 /* Don't warn about decl unused; the cleanup uses it. */
2995 TREE_USED (decl) = 1;
2997 /* Initialize EH, if we've been told to do so. */
2998 if (flag_exceptions && !eh_initialized_p)
3000 eh_initialized_p = true;
3001 eh_personality_libfunc
3002 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3003 ? "__gcc_personality_sj0"
3004 : "__gcc_personality_v0");
3005 using_eh_for_cleanups ();
3008 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3013 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3014 and push that on the current scope. */
3016 void
3017 push_parm_decl (tree parm)
3019 tree decl;
3021 /* Don't attempt to expand sizes while parsing this decl.
3022 (We can get here with i_s_e 1 somehow from Objective-C.) */
3023 int save_immediate_size_expand = immediate_size_expand;
3024 immediate_size_expand = 0;
3026 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3027 TREE_PURPOSE (TREE_PURPOSE (parm)),
3028 PARM, 0, NULL);
3029 decl_attributes (&decl, TREE_VALUE (parm), 0);
3031 decl = pushdecl (decl);
3033 finish_decl (decl, NULL_TREE, NULL_TREE);
3035 immediate_size_expand = save_immediate_size_expand;
3038 /* Mark all the parameter declarations to date as forward decls,
3039 shift them to the variables list, and reset the parameters list.
3040 Also diagnose use of this extension. */
3042 void
3043 mark_forward_parm_decls (void)
3045 tree parm;
3047 if (pedantic && !current_scope->warned_forward_parm_decls)
3049 pedwarn ("ISO C forbids forward parameter declarations");
3050 current_scope->warned_forward_parm_decls = true;
3053 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3054 TREE_ASM_WRITTEN (parm) = 1;
3056 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3057 current_scope->parms = 0;
3058 current_scope->parms_last = 0;
3061 static GTY(()) int compound_literal_number;
3063 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3064 literal, which may be an incomplete array type completed by the
3065 initializer; INIT is a CONSTRUCTOR that initializes the compound
3066 literal. */
3068 tree
3069 build_compound_literal (tree type, tree init)
3071 /* We do not use start_decl here because we have a type, not a declarator;
3072 and do not use finish_decl because the decl should be stored inside
3073 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3074 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3075 tree complit;
3076 tree stmt;
3077 DECL_EXTERNAL (decl) = 0;
3078 TREE_PUBLIC (decl) = 0;
3079 TREE_STATIC (decl) = (current_scope == global_scope);
3080 DECL_CONTEXT (decl) = current_function_decl;
3081 TREE_USED (decl) = 1;
3082 TREE_TYPE (decl) = type;
3083 TREE_READONLY (decl) = TREE_READONLY (type);
3084 store_init_value (decl, init);
3086 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3088 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3089 if (failure)
3090 abort ();
3093 type = TREE_TYPE (decl);
3094 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3095 return error_mark_node;
3097 stmt = build_stmt (DECL_STMT, decl);
3098 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3099 TREE_SIDE_EFFECTS (complit) = 1;
3101 layout_decl (decl, 0);
3103 if (TREE_STATIC (decl))
3105 /* This decl needs a name for the assembler output. We also need
3106 a unique suffix to be added to the name. */
3107 char *name;
3109 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3110 compound_literal_number);
3111 compound_literal_number++;
3112 DECL_NAME (decl) = get_identifier (name);
3113 DECL_DEFER_OUTPUT (decl) = 1;
3114 DECL_COMDAT (decl) = 1;
3115 DECL_ARTIFICIAL (decl) = 1;
3116 pushdecl (decl);
3117 rest_of_decl_compilation (decl, NULL, 1, 0);
3120 return complit;
3123 /* Make TYPE a complete type based on INITIAL_VALUE.
3124 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3125 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3128 complete_array_type (tree type, tree initial_value, int do_default)
3130 tree maxindex = NULL_TREE;
3131 int value = 0;
3133 if (initial_value)
3135 /* Note MAXINDEX is really the maximum index,
3136 one less than the size. */
3137 if (TREE_CODE (initial_value) == STRING_CST)
3139 int eltsize
3140 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3141 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3142 / eltsize) - 1, 0);
3144 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3146 tree elts = CONSTRUCTOR_ELTS (initial_value);
3147 maxindex = build_int_2 (-1, -1);
3148 for (; elts; elts = TREE_CHAIN (elts))
3150 if (TREE_PURPOSE (elts))
3151 maxindex = TREE_PURPOSE (elts);
3152 else
3153 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3154 maxindex, integer_one_node));
3156 maxindex = copy_node (maxindex);
3158 else
3160 /* Make an error message unless that happened already. */
3161 if (initial_value != error_mark_node)
3162 value = 1;
3164 /* Prevent further error messages. */
3165 maxindex = build_int_2 (0, 0);
3169 if (!maxindex)
3171 if (do_default)
3172 maxindex = build_int_2 (0, 0);
3173 value = 2;
3176 if (maxindex)
3178 TYPE_DOMAIN (type) = build_index_type (maxindex);
3179 if (!TREE_TYPE (maxindex))
3180 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3183 /* Lay out the type now that we can get the real answer. */
3185 layout_type (type);
3187 return value;
3190 /* Determine whether TYPE is a structure with a flexible array member,
3191 or a union containing such a structure (possibly recursively). */
3193 static bool
3194 flexible_array_type_p (tree type)
3196 tree x;
3197 switch (TREE_CODE (type))
3199 case RECORD_TYPE:
3200 x = TYPE_FIELDS (type);
3201 if (x == NULL_TREE)
3202 return false;
3203 while (TREE_CHAIN (x) != NULL_TREE)
3204 x = TREE_CHAIN (x);
3205 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3206 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3207 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3208 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3209 return true;
3210 return false;
3211 case UNION_TYPE:
3212 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3214 if (flexible_array_type_p (TREE_TYPE (x)))
3215 return true;
3217 return false;
3218 default:
3219 return false;
3223 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3224 replacing with appropriate values if they are invalid. */
3225 static void
3226 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3228 tree type_mv;
3229 unsigned int max_width;
3230 unsigned HOST_WIDE_INT w;
3231 const char *name = orig_name ? orig_name: _("<anonymous>");
3233 /* Necessary? */
3234 STRIP_NOPS (*width);
3236 /* Detect and ignore out of range field width and process valid
3237 field widths. */
3238 if (TREE_CODE (*width) != INTEGER_CST)
3240 error ("bit-field `%s' width not an integer constant", name);
3241 *width = integer_one_node;
3243 else
3245 constant_expression_warning (*width);
3246 if (tree_int_cst_sgn (*width) < 0)
3248 error ("negative width in bit-field `%s'", name);
3249 *width = integer_one_node;
3251 else if (integer_zerop (*width) && orig_name)
3253 error ("zero width for bit-field `%s'", name);
3254 *width = integer_one_node;
3258 /* Detect invalid bit-field type. */
3259 if (TREE_CODE (*type) != INTEGER_TYPE
3260 && TREE_CODE (*type) != BOOLEAN_TYPE
3261 && TREE_CODE (*type) != ENUMERAL_TYPE)
3263 error ("bit-field `%s' has invalid type", name);
3264 *type = unsigned_type_node;
3267 type_mv = TYPE_MAIN_VARIANT (*type);
3268 if (pedantic
3269 && type_mv != integer_type_node
3270 && type_mv != unsigned_type_node
3271 && type_mv != boolean_type_node)
3272 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3274 if (type_mv == boolean_type_node)
3275 max_width = CHAR_TYPE_SIZE;
3276 else
3277 max_width = TYPE_PRECISION (*type);
3279 if (0 < compare_tree_int (*width, max_width))
3281 error ("width of `%s' exceeds its type", name);
3282 w = max_width;
3283 *width = build_int_2 (w, 0);
3285 else
3286 w = tree_low_cst (*width, 1);
3288 if (TREE_CODE (*type) == ENUMERAL_TYPE
3289 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3290 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3291 warning ("`%s' is narrower than values of its type", name);
3294 /* Given declspecs and a declarator,
3295 determine the name and type of the object declared
3296 and construct a ..._DECL node for it.
3297 (In one case we can return a ..._TYPE node instead.
3298 For invalid input we sometimes return 0.)
3300 DECLSPECS is a chain of tree_list nodes whose value fields
3301 are the storage classes and type specifiers.
3303 DECL_CONTEXT says which syntactic context this declaration is in:
3304 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3305 FUNCDEF for a function definition. Like NORMAL but a few different
3306 error messages in each case. Return value may be zero meaning
3307 this definition is too screwy to try to parse.
3308 PARM for a parameter declaration (either within a function prototype
3309 or before a function body). Make a PARM_DECL, or return void_type_node.
3310 TYPENAME if for a typename (in a cast or sizeof).
3311 Don't make a DECL node; just return the ..._TYPE node.
3312 FIELD for a struct or union field; make a FIELD_DECL.
3313 INITIALIZED is 1 if the decl has an initializer.
3314 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3315 representing the width of the bit-field.
3317 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3318 It may also be so in the PARM case, for a prototype where the
3319 argument type is specified but not the name.
3321 This function is where the complicated C meanings of `static'
3322 and `extern' are interpreted. */
3324 static tree
3325 grokdeclarator (tree declarator, tree declspecs,
3326 enum decl_context decl_context, int initialized, tree *width)
3328 int specbits = 0;
3329 tree spec;
3330 tree type = NULL_TREE;
3331 int longlong = 0;
3332 int constp;
3333 int restrictp;
3334 int volatilep;
3335 int type_quals = TYPE_UNQUALIFIED;
3336 int inlinep;
3337 int explicit_int = 0;
3338 int explicit_char = 0;
3339 int defaulted_int = 0;
3340 tree typedef_decl = 0;
3341 const char *name, *orig_name;
3342 tree typedef_type = 0;
3343 int funcdef_flag = 0;
3344 enum tree_code innermost_code = ERROR_MARK;
3345 int size_varies = 0;
3346 tree decl_attr = NULL_TREE;
3347 tree array_ptr_quals = NULL_TREE;
3348 int array_parm_static = 0;
3349 tree returned_attrs = NULL_TREE;
3350 bool bitfield = width != NULL;
3352 if (decl_context == FUNCDEF)
3353 funcdef_flag = 1, decl_context = NORMAL;
3355 /* Look inside a declarator for the name being declared
3356 and get it as a string, for an error message. */
3358 tree decl = declarator;
3359 name = 0;
3361 while (decl)
3362 switch (TREE_CODE (decl))
3364 case ARRAY_REF:
3365 case INDIRECT_REF:
3366 case CALL_EXPR:
3367 innermost_code = TREE_CODE (decl);
3368 decl = TREE_OPERAND (decl, 0);
3369 break;
3371 case TREE_LIST:
3372 decl = TREE_VALUE (decl);
3373 break;
3375 case IDENTIFIER_NODE:
3376 name = IDENTIFIER_POINTER (decl);
3377 decl = 0;
3378 break;
3380 default:
3381 abort ();
3383 orig_name = name;
3384 if (name == 0)
3385 name = "type name";
3388 /* A function definition's declarator must have the form of
3389 a function declarator. */
3391 if (funcdef_flag && innermost_code != CALL_EXPR)
3392 return 0;
3394 /* If this looks like a function definition, make it one,
3395 even if it occurs where parms are expected.
3396 Then store_parm_decls will reject it and not use it as a parm. */
3397 if (decl_context == NORMAL && !funcdef_flag
3398 && current_scope->parm_flag)
3399 decl_context = PARM;
3401 /* Look through the decl specs and record which ones appear.
3402 Some typespecs are defined as built-in typenames.
3403 Others, the ones that are modifiers of other types,
3404 are represented by bits in SPECBITS: set the bits for
3405 the modifiers that appear. Storage class keywords are also in SPECBITS.
3407 If there is a typedef name or a type, store the type in TYPE.
3408 This includes builtin typedefs such as `int'.
3410 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3411 and did not come from a user typedef.
3413 Set LONGLONG if `long' is mentioned twice. */
3415 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3417 tree id = TREE_VALUE (spec);
3419 /* If the entire declaration is itself tagged as deprecated then
3420 suppress reports of deprecated items. */
3421 if (id && TREE_DEPRECATED (id))
3423 if (deprecated_state != DEPRECATED_SUPPRESS)
3424 warn_deprecated_use (id);
3427 if (id == ridpointers[(int) RID_INT])
3428 explicit_int = 1;
3429 if (id == ridpointers[(int) RID_CHAR])
3430 explicit_char = 1;
3432 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3434 enum rid i = C_RID_CODE (id);
3435 if ((int) i <= (int) RID_LAST_MODIFIER)
3437 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3439 if (longlong)
3440 error ("`long long long' is too long for GCC");
3441 else
3443 if (pedantic && !flag_isoc99 && ! in_system_header
3444 && warn_long_long)
3445 pedwarn ("ISO C90 does not support `long long'");
3446 longlong = 1;
3449 else if (specbits & (1 << (int) i))
3451 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3453 if (!flag_isoc99)
3454 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3456 else
3457 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3460 /* Diagnose "__thread extern". Recall that this list
3461 is in the reverse order seen in the text. */
3462 if (i == RID_THREAD
3463 && (specbits & (1 << (int) RID_EXTERN
3464 | 1 << (int) RID_STATIC)))
3466 if (specbits & 1 << (int) RID_EXTERN)
3467 error ("`__thread' before `extern'");
3468 else
3469 error ("`__thread' before `static'");
3472 specbits |= 1 << (int) i;
3473 goto found;
3476 if (type)
3477 error ("two or more data types in declaration of `%s'", name);
3478 /* Actual typedefs come to us as TYPE_DECL nodes. */
3479 else if (TREE_CODE (id) == TYPE_DECL)
3481 if (TREE_TYPE (id) == error_mark_node)
3482 ; /* Allow the type to default to int to avoid cascading errors. */
3483 else
3485 type = TREE_TYPE (id);
3486 decl_attr = DECL_ATTRIBUTES (id);
3487 typedef_decl = id;
3490 /* Built-in types come as identifiers. */
3491 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3493 tree t = lookup_name (id);
3494 if (TREE_TYPE (t) == error_mark_node)
3496 else if (!t || TREE_CODE (t) != TYPE_DECL)
3497 error ("`%s' fails to be a typedef or built in type",
3498 IDENTIFIER_POINTER (id));
3499 else
3501 type = TREE_TYPE (t);
3502 typedef_decl = t;
3505 else if (TREE_CODE (id) != ERROR_MARK)
3506 type = id;
3508 found:
3512 typedef_type = type;
3513 if (type)
3514 size_varies = C_TYPE_VARIABLE_SIZE (type);
3516 /* No type at all: default to `int', and set DEFAULTED_INT
3517 because it was not a user-defined typedef. */
3519 if (type == 0)
3521 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3522 | (1 << (int) RID_SIGNED)
3523 | (1 << (int) RID_UNSIGNED)
3524 | (1 << (int) RID_COMPLEX))))
3525 /* Don't warn about typedef foo = bar. */
3526 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3527 && ! in_system_header)
3529 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3530 and this is a function, or if -Wimplicit; prefer the former
3531 warning since it is more explicit. */
3532 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3533 && funcdef_flag)
3534 warn_about_return_type = 1;
3535 else if (warn_implicit_int || flag_isoc99)
3536 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3537 name);
3540 defaulted_int = 1;
3541 type = integer_type_node;
3544 /* Now process the modifiers that were specified
3545 and check for invalid combinations. */
3547 /* Long double is a special combination. */
3549 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3550 && TYPE_MAIN_VARIANT (type) == double_type_node)
3552 specbits &= ~(1 << (int) RID_LONG);
3553 type = long_double_type_node;
3556 /* Check all other uses of type modifiers. */
3558 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3559 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3561 int ok = 0;
3563 if ((specbits & 1 << (int) RID_LONG)
3564 && (specbits & 1 << (int) RID_SHORT))
3565 error ("both long and short specified for `%s'", name);
3566 else if (((specbits & 1 << (int) RID_LONG)
3567 || (specbits & 1 << (int) RID_SHORT))
3568 && explicit_char)
3569 error ("long or short specified with char for `%s'", name);
3570 else if (((specbits & 1 << (int) RID_LONG)
3571 || (specbits & 1 << (int) RID_SHORT))
3572 && TREE_CODE (type) == REAL_TYPE)
3574 static int already = 0;
3576 error ("long or short specified with floating type for `%s'", name);
3577 if (! already && ! pedantic)
3579 error ("the only valid combination is `long double'");
3580 already = 1;
3583 else if ((specbits & 1 << (int) RID_SIGNED)
3584 && (specbits & 1 << (int) RID_UNSIGNED))
3585 error ("both signed and unsigned specified for `%s'", name);
3586 else if (TREE_CODE (type) != INTEGER_TYPE)
3587 error ("long, short, signed or unsigned invalid for `%s'", name);
3588 else
3590 ok = 1;
3591 if (!explicit_int && !defaulted_int && !explicit_char)
3593 error ("long, short, signed or unsigned used invalidly for `%s'",
3594 name);
3595 ok = 0;
3599 /* Discard the type modifiers if they are invalid. */
3600 if (! ok)
3602 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3603 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3604 longlong = 0;
3608 if ((specbits & (1 << (int) RID_COMPLEX))
3609 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3611 error ("complex invalid for `%s'", name);
3612 specbits &= ~(1 << (int) RID_COMPLEX);
3615 /* Decide whether an integer type is signed or not.
3616 Optionally treat bit-fields as signed by default. */
3617 if (specbits & 1 << (int) RID_UNSIGNED
3618 || (bitfield && ! flag_signed_bitfields
3619 && (explicit_int || defaulted_int || explicit_char
3620 /* A typedef for plain `int' without `signed'
3621 can be controlled just like plain `int'. */
3622 || ! (typedef_decl != 0
3623 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3624 && TREE_CODE (type) != ENUMERAL_TYPE
3625 && !(specbits & 1 << (int) RID_SIGNED)))
3627 if (longlong)
3628 type = long_long_unsigned_type_node;
3629 else if (specbits & 1 << (int) RID_LONG)
3630 type = long_unsigned_type_node;
3631 else if (specbits & 1 << (int) RID_SHORT)
3632 type = short_unsigned_type_node;
3633 else if (type == char_type_node)
3634 type = unsigned_char_type_node;
3635 else if (typedef_decl)
3636 type = c_common_unsigned_type (type);
3637 else
3638 type = unsigned_type_node;
3640 else if ((specbits & 1 << (int) RID_SIGNED)
3641 && type == char_type_node)
3642 type = signed_char_type_node;
3643 else if (longlong)
3644 type = long_long_integer_type_node;
3645 else if (specbits & 1 << (int) RID_LONG)
3646 type = long_integer_type_node;
3647 else if (specbits & 1 << (int) RID_SHORT)
3648 type = short_integer_type_node;
3650 if (specbits & 1 << (int) RID_COMPLEX)
3652 if (pedantic && !flag_isoc99)
3653 pedwarn ("ISO C90 does not support complex types");
3654 /* If we just have "complex", it is equivalent to
3655 "complex double", but if any modifiers at all are specified it is
3656 the complex form of TYPE. E.g, "complex short" is
3657 "complex short int". */
3659 if (defaulted_int && ! longlong
3660 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3661 | (1 << (int) RID_SIGNED)
3662 | (1 << (int) RID_UNSIGNED))))
3664 if (pedantic)
3665 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3666 type = complex_double_type_node;
3668 else if (type == integer_type_node)
3670 if (pedantic)
3671 pedwarn ("ISO C does not support complex integer types");
3672 type = complex_integer_type_node;
3674 else if (type == float_type_node)
3675 type = complex_float_type_node;
3676 else if (type == double_type_node)
3677 type = complex_double_type_node;
3678 else if (type == long_double_type_node)
3679 type = complex_long_double_type_node;
3680 else
3682 if (pedantic)
3683 pedwarn ("ISO C does not support complex integer types");
3684 type = build_complex_type (type);
3688 /* Check the type and width of a bit-field. */
3689 if (bitfield)
3690 check_bitfield_type_and_width (&type, width, orig_name);
3692 /* Figure out the type qualifiers for the declaration. There are
3693 two ways a declaration can become qualified. One is something
3694 like `const int i' where the `const' is explicit. Another is
3695 something like `typedef const int CI; CI i' where the type of the
3696 declaration contains the `const'. */
3697 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (type);
3698 restrictp = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (type);
3699 volatilep = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (type);
3700 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3701 if (constp > 1 && ! flag_isoc99)
3702 pedwarn ("duplicate `const'");
3703 if (restrictp > 1 && ! flag_isoc99)
3704 pedwarn ("duplicate `restrict'");
3705 if (volatilep > 1 && ! flag_isoc99)
3706 pedwarn ("duplicate `volatile'");
3707 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3708 type = TYPE_MAIN_VARIANT (type);
3709 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3710 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3711 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3713 /* Warn if two storage classes are given. Default to `auto'. */
3716 int nclasses = 0;
3718 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3719 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3720 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3721 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3722 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3724 /* "static __thread" and "extern __thread" are allowed. */
3725 if ((specbits & (1 << (int) RID_THREAD
3726 | 1 << (int) RID_STATIC
3727 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3728 nclasses++;
3730 /* Warn about storage classes that are invalid for certain
3731 kinds of declarations (parameters, typenames, etc.). */
3733 if (nclasses > 1)
3734 error ("multiple storage classes in declaration of `%s'", name);
3735 else if (funcdef_flag
3736 && (specbits
3737 & ((1 << (int) RID_REGISTER)
3738 | (1 << (int) RID_AUTO)
3739 | (1 << (int) RID_TYPEDEF)
3740 | (1 << (int) RID_THREAD))))
3742 if (specbits & 1 << (int) RID_AUTO
3743 && (pedantic || current_scope == global_scope))
3744 pedwarn ("function definition declared `auto'");
3745 if (specbits & 1 << (int) RID_REGISTER)
3746 error ("function definition declared `register'");
3747 if (specbits & 1 << (int) RID_TYPEDEF)
3748 error ("function definition declared `typedef'");
3749 if (specbits & 1 << (int) RID_THREAD)
3750 error ("function definition declared `__thread'");
3751 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3752 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3754 else if (decl_context != NORMAL && nclasses > 0)
3756 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3758 else
3760 switch (decl_context)
3762 case FIELD:
3763 error ("storage class specified for structure field `%s'",
3764 name);
3765 break;
3766 case PARM:
3767 error ("storage class specified for parameter `%s'", name);
3768 break;
3769 default:
3770 error ("storage class specified for typename");
3771 break;
3773 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3774 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3775 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3778 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3780 /* `extern' with initialization is invalid if not at file scope. */
3781 if (current_scope == global_scope)
3782 warning ("`%s' initialized and declared `extern'", name);
3783 else
3784 error ("`%s' has both `extern' and initializer", name);
3786 else if (current_scope == global_scope)
3788 if (specbits & 1 << (int) RID_AUTO)
3789 error ("file-scope declaration of `%s' specifies `auto'", name);
3791 else
3793 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3794 error ("nested function `%s' declared `extern'", name);
3795 else if ((specbits & (1 << (int) RID_THREAD
3796 | 1 << (int) RID_EXTERN
3797 | 1 << (int) RID_STATIC))
3798 == (1 << (int) RID_THREAD))
3800 error ("function-scope `%s' implicitly auto and declared `__thread'",
3801 name);
3802 specbits &= ~(1 << (int) RID_THREAD);
3807 /* Now figure out the structure of the declarator proper.
3808 Descend through it, creating more complex types, until we reach
3809 the declared identifier (or NULL_TREE, in an absolute declarator). */
3811 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3813 if (type == error_mark_node)
3815 declarator = TREE_OPERAND (declarator, 0);
3816 continue;
3819 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3820 an INDIRECT_REF (for *...),
3821 a CALL_EXPR (for ...(...)),
3822 a TREE_LIST (for nested attributes),
3823 an identifier (for the name being declared)
3824 or a null pointer (for the place in an absolute declarator
3825 where the name was omitted).
3826 For the last two cases, we have just exited the loop.
3828 At this point, TYPE is the type of elements of an array,
3829 or for a function to return, or for a pointer to point to.
3830 After this sequence of ifs, TYPE is the type of the
3831 array or function or pointer, and DECLARATOR has had its
3832 outermost layer removed. */
3834 if (array_ptr_quals != NULL_TREE || array_parm_static)
3836 /* Only the innermost declarator (making a parameter be of
3837 array type which is converted to pointer type)
3838 may have static or type qualifiers. */
3839 error ("static or type qualifiers in non-parameter array declarator");
3840 array_ptr_quals = NULL_TREE;
3841 array_parm_static = 0;
3844 if (TREE_CODE (declarator) == TREE_LIST)
3846 /* We encode a declarator with embedded attributes using
3847 a TREE_LIST. */
3848 tree attrs = TREE_PURPOSE (declarator);
3849 tree inner_decl;
3850 int attr_flags = 0;
3851 declarator = TREE_VALUE (declarator);
3852 inner_decl = declarator;
3853 while (inner_decl != NULL_TREE
3854 && TREE_CODE (inner_decl) == TREE_LIST)
3855 inner_decl = TREE_VALUE (inner_decl);
3856 if (inner_decl == NULL_TREE
3857 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3858 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3859 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3860 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3861 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3862 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3863 returned_attrs = decl_attributes (&type,
3864 chainon (returned_attrs, attrs),
3865 attr_flags);
3867 else if (TREE_CODE (declarator) == ARRAY_REF)
3869 tree itype = NULL_TREE;
3870 tree size = TREE_OPERAND (declarator, 1);
3871 /* The index is a signed object `sizetype' bits wide. */
3872 tree index_type = c_common_signed_type (sizetype);
3874 array_ptr_quals = TREE_TYPE (declarator);
3875 array_parm_static = TREE_STATIC (declarator);
3877 declarator = TREE_OPERAND (declarator, 0);
3879 /* Check for some types that there cannot be arrays of. */
3881 if (VOID_TYPE_P (type))
3883 error ("declaration of `%s' as array of voids", name);
3884 type = error_mark_node;
3887 if (TREE_CODE (type) == FUNCTION_TYPE)
3889 error ("declaration of `%s' as array of functions", name);
3890 type = error_mark_node;
3893 if (pedantic && flexible_array_type_p (type))
3894 pedwarn ("invalid use of structure with flexible array member");
3896 if (size == error_mark_node)
3897 type = error_mark_node;
3899 if (type == error_mark_node)
3900 continue;
3902 /* If size was specified, set ITYPE to a range-type for that size.
3903 Otherwise, ITYPE remains null. finish_decl may figure it out
3904 from an initial value. */
3906 if (size)
3908 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3909 STRIP_TYPE_NOPS (size);
3911 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3913 error ("size of array `%s' has non-integer type", name);
3914 size = integer_one_node;
3917 if (pedantic && integer_zerop (size))
3918 pedwarn ("ISO C forbids zero-size array `%s'", name);
3920 if (TREE_CODE (size) == INTEGER_CST)
3922 constant_expression_warning (size);
3923 if (tree_int_cst_sgn (size) < 0)
3925 error ("size of array `%s' is negative", name);
3926 size = integer_one_node;
3929 else
3931 /* Make sure the array size remains visibly nonconstant
3932 even if it is (eg) a const variable with known value. */
3933 size_varies = 1;
3935 if (!flag_isoc99 && pedantic)
3937 if (TREE_CONSTANT (size))
3938 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3939 name);
3940 else
3941 pedwarn ("ISO C90 forbids variable-size array `%s'",
3942 name);
3946 if (integer_zerop (size))
3948 /* A zero-length array cannot be represented with an
3949 unsigned index type, which is what we'll get with
3950 build_index_type. Create an open-ended range instead. */
3951 itype = build_range_type (sizetype, size, NULL_TREE);
3953 else
3955 /* Compute the maximum valid index, that is, size - 1.
3956 Do the calculation in index_type, so that if it is
3957 a variable the computations will be done in the
3958 proper mode. */
3959 itype = fold (build (MINUS_EXPR, index_type,
3960 convert (index_type, size),
3961 convert (index_type, size_one_node)));
3963 /* If that overflowed, the array is too big.
3964 ??? While a size of INT_MAX+1 technically shouldn't
3965 cause an overflow (because we subtract 1), the overflow
3966 is recorded during the conversion to index_type, before
3967 the subtraction. Handling this case seems like an
3968 unnecessary complication. */
3969 if (TREE_OVERFLOW (itype))
3971 error ("size of array `%s' is too large", name);
3972 type = error_mark_node;
3973 continue;
3976 if (size_varies)
3978 /* We must be able to distinguish the
3979 SAVE_EXPR_CONTEXT for the variably-sized type
3980 so that we can set it correctly in
3981 set_save_expr_context. The convention is
3982 that all SAVE_EXPRs that need to be reset
3983 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3984 tree cfd = current_function_decl;
3985 if (decl_context == PARM)
3986 current_function_decl = NULL_TREE;
3987 itype = variable_size (itype);
3988 if (decl_context == PARM)
3989 current_function_decl = cfd;
3991 itype = build_index_type (itype);
3994 else if (decl_context == FIELD)
3996 if (pedantic && !flag_isoc99 && !in_system_header)
3997 pedwarn ("ISO C90 does not support flexible array members");
3999 /* ISO C99 Flexible array members are effectively identical
4000 to GCC's zero-length array extension. */
4001 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4004 /* If pedantic, complain about arrays of incomplete types. */
4006 if (pedantic && !COMPLETE_TYPE_P (type))
4007 pedwarn ("array type has incomplete element type");
4009 /* Build the array type itself, then merge any constancy or
4010 volatility into the target type. We must do it in this order
4011 to ensure that the TYPE_MAIN_VARIANT field of the array type
4012 is set correctly. */
4014 type = build_array_type (type, itype);
4015 if (type_quals)
4016 type = c_build_qualified_type (type, type_quals);
4018 if (size_varies)
4019 C_TYPE_VARIABLE_SIZE (type) = 1;
4021 /* The GCC extension for zero-length arrays differs from
4022 ISO flexible array members in that sizeof yields zero. */
4023 if (size && integer_zerop (size))
4025 layout_type (type);
4026 TYPE_SIZE (type) = bitsize_zero_node;
4027 TYPE_SIZE_UNIT (type) = size_zero_node;
4029 if (decl_context != PARM
4030 && (array_ptr_quals != NULL_TREE || array_parm_static))
4032 error ("static or type qualifiers in non-parameter array declarator");
4033 array_ptr_quals = NULL_TREE;
4034 array_parm_static = 0;
4037 else if (TREE_CODE (declarator) == CALL_EXPR)
4039 tree arg_types;
4041 /* Declaring a function type.
4042 Make sure we have a valid type for the function to return. */
4043 if (type == error_mark_node)
4044 continue;
4046 size_varies = 0;
4048 /* Warn about some types functions can't return. */
4050 if (TREE_CODE (type) == FUNCTION_TYPE)
4052 error ("`%s' declared as function returning a function", name);
4053 type = integer_type_node;
4055 if (TREE_CODE (type) == ARRAY_TYPE)
4057 error ("`%s' declared as function returning an array", name);
4058 type = integer_type_node;
4061 /* Construct the function type and go to the next
4062 inner layer of declarator. */
4064 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4065 funcdef_flag
4066 /* Say it's a definition
4067 only for the CALL_EXPR
4068 closest to the identifier. */
4069 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4070 /* Type qualifiers before the return type of the function
4071 qualify the return type, not the function type. */
4072 if (type_quals)
4074 /* Type qualifiers on a function return type are normally
4075 permitted by the standard but have no effect, so give a
4076 warning at -Wextra. Qualifiers on a void return type have
4077 meaning as a GNU extension, and are banned on function
4078 definitions in ISO C. FIXME: strictly we shouldn't
4079 pedwarn for qualified void return types except on function
4080 definitions, but not doing so could lead to the undesirable
4081 state of a "volatile void" function return type not being
4082 warned about, and a use of the function being compiled
4083 with GNU semantics, with no diagnostics under -pedantic. */
4084 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4085 pedwarn ("ISO C forbids qualified void function return type");
4086 else if (extra_warnings
4087 && !(VOID_TYPE_P (type)
4088 && type_quals == TYPE_QUAL_VOLATILE))
4089 warning ("type qualifiers ignored on function return type");
4091 type = c_build_qualified_type (type, type_quals);
4093 type_quals = TYPE_UNQUALIFIED;
4095 type = build_function_type (type, arg_types);
4096 declarator = TREE_OPERAND (declarator, 0);
4098 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4099 the formal parameter list of this FUNCTION_TYPE to point to
4100 the FUNCTION_TYPE node itself. */
4103 tree link;
4105 for (link = last_function_parm_tags;
4106 link;
4107 link = TREE_CHAIN (link))
4108 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4111 else if (TREE_CODE (declarator) == INDIRECT_REF)
4113 /* Merge any constancy or volatility into the target type
4114 for the pointer. */
4116 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4117 && type_quals)
4118 pedwarn ("ISO C forbids qualified function types");
4119 if (type_quals)
4120 type = c_build_qualified_type (type, type_quals);
4121 type_quals = TYPE_UNQUALIFIED;
4122 size_varies = 0;
4124 type = build_pointer_type (type);
4126 /* Process a list of type modifier keywords
4127 (such as const or volatile) that were given inside the `*'. */
4129 if (TREE_TYPE (declarator))
4131 tree typemodlist;
4132 int erred = 0;
4134 constp = 0;
4135 volatilep = 0;
4136 restrictp = 0;
4137 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4138 typemodlist = TREE_CHAIN (typemodlist))
4140 tree qualifier = TREE_VALUE (typemodlist);
4142 if (C_IS_RESERVED_WORD (qualifier))
4144 if (C_RID_CODE (qualifier) == RID_CONST)
4145 constp++;
4146 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4147 volatilep++;
4148 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4149 restrictp++;
4150 else
4151 erred++;
4153 else
4154 erred++;
4157 if (erred)
4158 error ("invalid type modifier within pointer declarator");
4159 if (constp > 1 && ! flag_isoc99)
4160 pedwarn ("duplicate `const'");
4161 if (volatilep > 1 && ! flag_isoc99)
4162 pedwarn ("duplicate `volatile'");
4163 if (restrictp > 1 && ! flag_isoc99)
4164 pedwarn ("duplicate `restrict'");
4166 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4167 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4168 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4171 declarator = TREE_OPERAND (declarator, 0);
4173 else
4174 abort ();
4178 /* Now TYPE has the actual type. */
4180 /* Did array size calculations overflow? */
4182 if (TREE_CODE (type) == ARRAY_TYPE
4183 && COMPLETE_TYPE_P (type)
4184 && TREE_OVERFLOW (TYPE_SIZE (type)))
4186 error ("size of array `%s' is too large", name);
4187 /* If we proceed with the array type as it is, we'll eventually
4188 crash in tree_low_cst(). */
4189 type = error_mark_node;
4192 /* If this is declaring a typedef name, return a TYPE_DECL. */
4194 if (specbits & (1 << (int) RID_TYPEDEF))
4196 tree decl;
4197 /* Note that the grammar rejects storage classes
4198 in typenames, fields or parameters */
4199 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4200 && type_quals)
4201 pedwarn ("ISO C forbids qualified function types");
4202 if (type_quals)
4203 type = c_build_qualified_type (type, type_quals);
4204 decl = build_decl (TYPE_DECL, declarator, type);
4205 if ((specbits & (1 << (int) RID_SIGNED))
4206 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4207 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4208 decl_attributes (&decl, returned_attrs, 0);
4209 return decl;
4212 /* Detect the case of an array type of unspecified size
4213 which came, as such, direct from a typedef name.
4214 We must copy the type, so that each identifier gets
4215 a distinct type, so that each identifier's size can be
4216 controlled separately by its own initializer. */
4218 if (type != 0 && typedef_type != 0
4219 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4220 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4222 type = build_array_type (TREE_TYPE (type), 0);
4223 if (size_varies)
4224 C_TYPE_VARIABLE_SIZE (type) = 1;
4227 /* If this is a type name (such as, in a cast or sizeof),
4228 compute the type and return it now. */
4230 if (decl_context == TYPENAME)
4232 /* Note that the grammar rejects storage classes
4233 in typenames, fields or parameters */
4234 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4235 && type_quals)
4236 pedwarn ("ISO C forbids const or volatile function types");
4237 if (type_quals)
4238 type = c_build_qualified_type (type, type_quals);
4239 decl_attributes (&type, returned_attrs, 0);
4240 return type;
4243 /* Aside from typedefs and type names (handle above),
4244 `void' at top level (not within pointer)
4245 is allowed only in public variables.
4246 We don't complain about parms either, but that is because
4247 a better error message can be made later. */
4249 if (VOID_TYPE_P (type) && decl_context != PARM
4250 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4251 && ((specbits & (1 << (int) RID_EXTERN))
4252 || (current_scope == global_scope
4253 && !(specbits
4254 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4256 error ("variable or field `%s' declared void", name);
4257 type = integer_type_node;
4260 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4261 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4264 tree decl;
4266 if (decl_context == PARM)
4268 tree type_as_written;
4269 tree promoted_type;
4271 /* A parameter declared as an array of T is really a pointer to T.
4272 One declared as a function is really a pointer to a function. */
4274 if (TREE_CODE (type) == ARRAY_TYPE)
4276 /* Transfer const-ness of array into that of type pointed to. */
4277 type = TREE_TYPE (type);
4278 if (type_quals)
4279 type = c_build_qualified_type (type, type_quals);
4280 type = build_pointer_type (type);
4281 type_quals = TYPE_UNQUALIFIED;
4282 if (array_ptr_quals)
4284 tree new_ptr_quals, new_ptr_attrs;
4285 int erred = 0;
4286 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4287 /* We don't yet implement attributes in this context. */
4288 if (new_ptr_attrs != NULL_TREE)
4289 warning ("attributes in parameter array declarator ignored");
4291 constp = 0;
4292 volatilep = 0;
4293 restrictp = 0;
4294 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4296 tree qualifier = TREE_VALUE (new_ptr_quals);
4298 if (C_IS_RESERVED_WORD (qualifier))
4300 if (C_RID_CODE (qualifier) == RID_CONST)
4301 constp++;
4302 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4303 volatilep++;
4304 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4305 restrictp++;
4306 else
4307 erred++;
4309 else
4310 erred++;
4313 if (erred)
4314 error ("invalid type modifier within array declarator");
4316 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4317 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4318 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4320 size_varies = 0;
4322 else if (TREE_CODE (type) == FUNCTION_TYPE)
4324 if (pedantic && type_quals)
4325 pedwarn ("ISO C forbids qualified function types");
4326 if (type_quals)
4327 type = c_build_qualified_type (type, type_quals);
4328 type = build_pointer_type (type);
4329 type_quals = TYPE_UNQUALIFIED;
4331 else if (type_quals)
4332 type = c_build_qualified_type (type, type_quals);
4334 type_as_written = type;
4336 decl = build_decl (PARM_DECL, declarator, type);
4337 if (size_varies)
4338 C_DECL_VARIABLE_SIZE (decl) = 1;
4340 /* Compute the type actually passed in the parmlist,
4341 for the case where there is no prototype.
4342 (For example, shorts and chars are passed as ints.)
4343 When there is a prototype, this is overridden later. */
4345 if (type == error_mark_node)
4346 promoted_type = type;
4347 else
4348 promoted_type = c_type_promotes_to (type);
4350 DECL_ARG_TYPE (decl) = promoted_type;
4351 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4353 else if (decl_context == FIELD)
4355 /* Structure field. It may not be a function. */
4357 if (TREE_CODE (type) == FUNCTION_TYPE)
4359 error ("field `%s' declared as a function", name);
4360 type = build_pointer_type (type);
4362 else if (TREE_CODE (type) != ERROR_MARK
4363 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4365 error ("field `%s' has incomplete type", name);
4366 type = error_mark_node;
4368 /* Move type qualifiers down to element of an array. */
4369 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4370 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4371 type_quals),
4372 TYPE_DOMAIN (type));
4373 decl = build_decl (FIELD_DECL, declarator, type);
4374 DECL_NONADDRESSABLE_P (decl) = bitfield;
4376 if (size_varies)
4377 C_DECL_VARIABLE_SIZE (decl) = 1;
4379 else if (TREE_CODE (type) == FUNCTION_TYPE)
4381 /* Every function declaration is "external"
4382 except for those which are inside a function body
4383 in which `auto' is used.
4384 That is a case not specified by ANSI C,
4385 and we use it for forward declarations for nested functions. */
4386 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4387 || current_scope == global_scope);
4389 if (specbits & (1 << (int) RID_AUTO)
4390 && (pedantic || current_scope == global_scope))
4391 pedwarn ("invalid storage class for function `%s'", name);
4392 if (specbits & (1 << (int) RID_REGISTER))
4393 error ("invalid storage class for function `%s'", name);
4394 if (specbits & (1 << (int) RID_THREAD))
4395 error ("invalid storage class for function `%s'", name);
4396 /* Function declaration not at file scope.
4397 Storage classes other than `extern' are not allowed
4398 and `extern' makes no difference. */
4399 if (current_scope != global_scope
4400 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4401 && pedantic)
4402 pedwarn ("invalid storage class for function `%s'", name);
4404 decl = build_decl (FUNCTION_DECL, declarator, type);
4405 decl = build_decl_attribute_variant (decl, decl_attr);
4407 DECL_LANG_SPECIFIC (decl)
4408 = ggc_alloc_cleared (sizeof (struct lang_decl));
4410 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4411 pedwarn ("ISO C forbids qualified function types");
4413 /* GNU C interprets a `volatile void' return type to indicate
4414 that the function does not return. */
4415 if ((type_quals & TYPE_QUAL_VOLATILE)
4416 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4417 warning ("`noreturn' function returns non-void value");
4419 if (extern_ref)
4420 DECL_EXTERNAL (decl) = 1;
4421 /* Record absence of global scope for `static' or `auto'. */
4422 TREE_PUBLIC (decl)
4423 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4425 if (defaulted_int)
4426 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4428 /* Record presence of `inline', if it is reasonable. */
4429 if (MAIN_NAME_P (declarator))
4431 if (inlinep)
4432 warning ("cannot inline function `main'");
4434 else if (inlinep)
4436 /* Record that the function is declared `inline'. */
4437 DECL_DECLARED_INLINE_P (decl) = 1;
4439 /* Do not mark bare declarations as DECL_INLINE. Doing so
4440 in the presence of multiple declarations can result in
4441 the abstract origin pointing between the declarations,
4442 which will confuse dwarf2out. */
4443 if (initialized)
4445 DECL_INLINE (decl) = 1;
4446 if (specbits & (1 << (int) RID_EXTERN))
4447 current_extern_inline = 1;
4450 /* If -finline-functions, assume it can be inlined. This does
4451 two things: let the function be deferred until it is actually
4452 needed, and let dwarf2 know that the function is inlinable. */
4453 else if (flag_inline_trees == 2 && initialized)
4454 DECL_INLINE (decl) = 1;
4456 else
4458 /* It's a variable. */
4459 /* An uninitialized decl with `extern' is a reference. */
4460 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4462 /* Move type qualifiers down to element of an array. */
4463 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4465 int saved_align = TYPE_ALIGN(type);
4466 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4467 type_quals),
4468 TYPE_DOMAIN (type));
4469 TYPE_ALIGN (type) = saved_align;
4471 else if (type_quals)
4472 type = c_build_qualified_type (type, type_quals);
4474 /* It is invalid to create an `extern' declaration for a
4475 variable if there is a global declaration that is
4476 `static'. */
4477 if (extern_ref && current_scope != global_scope)
4479 tree global_decl;
4481 global_decl = identifier_global_value (declarator);
4482 if (global_decl
4483 && TREE_CODE (global_decl) == VAR_DECL
4484 && !TREE_PUBLIC (global_decl))
4485 error ("variable previously declared `static' redeclared "
4486 "`extern'");
4489 decl = build_decl (VAR_DECL, declarator, type);
4490 if (size_varies)
4491 C_DECL_VARIABLE_SIZE (decl) = 1;
4493 if (inlinep)
4494 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4496 DECL_EXTERNAL (decl) = extern_ref;
4498 /* At file scope, the presence of a `static' or `register' storage
4499 class specifier, or the absence of all storage class specifiers
4500 makes this declaration a definition (perhaps tentative). Also,
4501 the absence of both `static' and `register' makes it public. */
4502 if (current_scope == global_scope)
4504 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4505 | (1 << (int) RID_REGISTER)));
4506 TREE_STATIC (decl) = !extern_ref;
4508 /* Not at file scope, only `static' makes a static definition. */
4509 else
4511 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4512 TREE_PUBLIC (decl) = extern_ref;
4515 if (specbits & 1 << (int) RID_THREAD)
4517 if (targetm.have_tls)
4518 DECL_THREAD_LOCAL (decl) = 1;
4519 else
4520 /* A mere warning is sure to result in improper semantics
4521 at runtime. Don't bother to allow this to compile. */
4522 error ("thread-local storage not supported for this target");
4526 /* Record `register' declaration for warnings on &
4527 and in case doing stupid register allocation. */
4529 if (specbits & (1 << (int) RID_REGISTER))
4530 DECL_REGISTER (decl) = 1;
4532 /* Record constancy and volatility. */
4533 c_apply_type_quals_to_decl (type_quals, decl);
4535 /* If a type has volatile components, it should be stored in memory.
4536 Otherwise, the fact that those components are volatile
4537 will be ignored, and would even crash the compiler. */
4538 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4539 c_mark_addressable (decl);
4541 #ifdef ENABLE_CHECKING
4542 /* This is the earliest point at which we might know the assembler
4543 name of a variable. Thus, if it's known before this, die horribly. */
4544 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4545 abort ();
4546 #endif
4548 decl_attributes (&decl, returned_attrs, 0);
4550 return decl;
4554 /* Decode the parameter-list info for a function type or function definition.
4555 The argument is the value returned by `get_parm_info' (or made in parse.y
4556 if there is an identifier list instead of a parameter decl list).
4557 These two functions are separate because when a function returns
4558 or receives functions then each is called multiple times but the order
4559 of calls is different. The last call to `grokparms' is always the one
4560 that contains the formal parameter names of a function definition.
4562 Store in `last_function_parms' a chain of the decls of parms.
4563 Also store in `last_function_parm_tags' a chain of the struct, union,
4564 and enum tags declared among the parms.
4566 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4568 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4569 a mere declaration. A nonempty identifier-list gets an error message
4570 when FUNCDEF_FLAG is zero. */
4572 static tree
4573 grokparms (tree parms_info, int funcdef_flag)
4575 tree first_parm = TREE_CHAIN (parms_info);
4577 last_function_parms = TREE_PURPOSE (parms_info);
4578 last_function_parm_tags = TREE_VALUE (parms_info);
4579 last_function_parm_others = TREE_TYPE (parms_info);
4581 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4582 && !in_system_header)
4583 warning ("function declaration isn't a prototype");
4585 if (first_parm != 0
4586 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4588 if (! funcdef_flag)
4589 pedwarn ("parameter names (without types) in function declaration");
4591 last_function_parms = first_parm;
4592 return 0;
4594 else
4596 tree parm;
4597 tree typelt;
4598 /* If the arg types are incomplete in a declaration,
4599 they must include undefined tags.
4600 These tags can never be defined in the scope of the declaration,
4601 so the types can never be completed,
4602 and no call can be compiled successfully. */
4604 for (parm = last_function_parms, typelt = first_parm;
4605 parm;
4606 parm = TREE_CHAIN (parm))
4607 /* Skip over any enumeration constants declared here. */
4608 if (TREE_CODE (parm) == PARM_DECL)
4610 /* Barf if the parameter itself has an incomplete type. */
4611 tree type = TREE_VALUE (typelt);
4612 if (type == error_mark_node)
4613 continue;
4614 if (!COMPLETE_TYPE_P (type))
4616 if (funcdef_flag && DECL_NAME (parm) != 0)
4617 error ("parameter `%s' has incomplete type",
4618 IDENTIFIER_POINTER (DECL_NAME (parm)));
4619 else
4620 warning ("parameter has incomplete type");
4621 if (funcdef_flag)
4623 TREE_VALUE (typelt) = error_mark_node;
4624 TREE_TYPE (parm) = error_mark_node;
4627 typelt = TREE_CHAIN (typelt);
4630 return first_parm;
4634 /* Return a tree_list node with info on a parameter list just parsed.
4635 The TREE_PURPOSE is a list of decls of those parms.
4636 The TREE_VALUE is a list of structure, union and enum tags defined.
4637 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4638 The TREE_TYPE is a list of non-parameter decls which appeared with the
4639 parameters.
4640 This tree_list node is later fed to `grokparms'.
4642 VOID_AT_END nonzero means append `void' to the end of the type-list.
4643 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4645 tree
4646 get_parm_info (int void_at_end)
4648 tree decl, type, list;
4649 tree types = 0;
4650 tree *last_type = &types;
4651 tree tags = current_scope->tags;
4652 tree parms = current_scope->parms;
4653 tree others = current_scope->names;
4654 static bool explained_incomplete_types = false;
4655 bool gave_void_only_once_err = false;
4657 /* Just "void" (and no ellipsis) is special. There are really no parms.
4658 But if the "void" is qualified (by "const" or "volatile"), or has a
4659 storage class specifier ("register"), then the behavior is undefined;
4660 issue an error. Typedefs for "void" are OK (see DR#157). */
4661 if (void_at_end && parms != 0
4662 && TREE_CHAIN (parms) == 0
4663 && VOID_TYPE_P (TREE_TYPE (parms))
4664 && !DECL_NAME (parms))
4666 if (TREE_THIS_VOLATILE (parms)
4667 || TREE_READONLY (parms)
4668 || DECL_REGISTER (parms))
4669 error ("\"void\" as only parameter may not be qualified");
4671 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4674 /* Sanity check all of the parameter declarations. */
4675 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4677 if (TREE_CODE (decl) != PARM_DECL)
4678 abort ();
4679 if (TREE_ASM_WRITTEN (decl))
4680 abort ();
4682 /* Since there is a prototype, args are passed in their
4683 declared types. The back end may override this. */
4684 type = TREE_TYPE (decl);
4685 DECL_ARG_TYPE (decl) = type;
4687 /* Check for (..., void, ...) and issue an error. */
4688 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4690 error ("\"void\" must be the only parameter");
4691 gave_void_only_once_err = true;
4694 type = build_tree_list (0, type);
4695 *last_type = type;
4696 last_type = &TREE_CHAIN (type);
4699 /* Check the list of non-parameter decls for any forward parm decls
4700 that never got real decls. */
4701 for (decl = others; decl; decl = TREE_CHAIN (decl))
4702 if (TREE_CODE (decl) == PARM_DECL)
4704 if (!TREE_ASM_WRITTEN (decl))
4705 abort ();
4707 error ("%Jparameter \"%D\" has just a forward declaration",
4708 decl, decl);
4711 /* Warn about any struct, union or enum tags defined within this
4712 list. The scope of such types is limited to this declaration,
4713 which is rarely if ever desirable (it's impossible to call such
4714 a function with type-correct arguments). */
4715 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4717 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4718 const char *keyword;
4719 /* An anonymous union parm type is meaningful as a GNU extension.
4720 So don't warn for that. */
4721 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4722 continue;
4724 /* The keyword should not be translated. */
4725 switch (code)
4727 case RECORD_TYPE: keyword = "struct"; break;
4728 case UNION_TYPE: keyword = "union"; break;
4729 case ENUMERAL_TYPE: keyword = "enum"; break;
4730 default: abort ();
4733 if (TREE_PURPOSE (decl))
4734 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4735 warning ("\"%s %s\" declared inside parameter list",
4736 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4737 else
4738 /* The %s will be one of 'struct', 'union', or 'enum'. */
4739 warning ("anonymous %s declared inside parameter list", keyword);
4741 if (! explained_incomplete_types)
4743 warning ("its scope is only this definition or declaration,"
4744 " which is probably not what you want");
4745 explained_incomplete_types = true;
4750 if (void_at_end)
4752 type = build_tree_list (0, void_type_node);
4753 *last_type = type;
4756 list = tree_cons (parms, tags, types);
4757 TREE_TYPE (list) = others;
4758 return list;
4761 /* Get the struct, enum or union (CODE says which) with tag NAME.
4762 Define the tag as a forward-reference if it is not defined. */
4764 tree
4765 xref_tag (enum tree_code code, tree name)
4767 /* If a cross reference is requested, look up the type
4768 already defined for this tag and return it. */
4770 tree ref = lookup_tag (code, name, 0);
4771 /* If this is the right type of tag, return what we found.
4772 (This reference will be shadowed by shadow_tag later if appropriate.)
4773 If this is the wrong type of tag, do not return it. If it was the
4774 wrong type in the same scope, we will have had an error
4775 message already; if in a different scope and declaring
4776 a name, pending_xref_error will give an error message; but if in a
4777 different scope and not declaring a name, this tag should
4778 shadow the previous declaration of a different type of tag, and
4779 this would not work properly if we return the reference found.
4780 (For example, with "struct foo" in an outer scope, "union foo;"
4781 must shadow that tag with a new one of union type.) */
4782 if (ref && TREE_CODE (ref) == code)
4783 return ref;
4785 /* If no such tag is yet defined, create a forward-reference node
4786 and record it as the "definition".
4787 When a real declaration of this type is found,
4788 the forward-reference will be altered into a real type. */
4790 ref = make_node (code);
4791 if (code == ENUMERAL_TYPE)
4793 /* Give the type a default layout like unsigned int
4794 to avoid crashing if it does not get defined. */
4795 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4796 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4797 TYPE_USER_ALIGN (ref) = 0;
4798 TREE_UNSIGNED (ref) = 1;
4799 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4800 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4801 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4804 pushtag (name, ref);
4806 return ref;
4809 /* Make sure that the tag NAME is defined *in the current scope*
4810 at least as a forward reference.
4811 CODE says which kind of tag NAME ought to be. */
4813 tree
4814 start_struct (enum tree_code code, tree name)
4816 /* If there is already a tag defined at this scope
4817 (as a forward reference), just return it. */
4819 tree ref = 0;
4821 if (name != 0)
4822 ref = lookup_tag (code, name, 1);
4823 if (ref && TREE_CODE (ref) == code)
4825 if (TYPE_FIELDS (ref))
4827 if (code == UNION_TYPE)
4828 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4829 else
4830 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4833 else
4835 /* Otherwise create a forward-reference just so the tag is in scope. */
4837 ref = make_node (code);
4838 pushtag (name, ref);
4841 C_TYPE_BEING_DEFINED (ref) = 1;
4842 TYPE_PACKED (ref) = flag_pack_struct;
4843 return ref;
4846 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4847 of a structure component, returning a FIELD_DECL node.
4848 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4850 This is done during the parsing of the struct declaration.
4851 The FIELD_DECL nodes are chained together and the lot of them
4852 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4854 tree
4855 grokfield (tree declarator, tree declspecs, tree width)
4857 tree value;
4859 if (declarator == NULL_TREE && width == NULL_TREE)
4861 /* This is an unnamed decl.
4863 If we have something of the form "union { list } ;" then this
4864 is the anonymous union extension. Similarly for struct.
4866 If this is something of the form "struct foo;", then
4867 If MS extensions are enabled, this is handled as an
4868 anonymous struct.
4869 Otherwise this is a forward declaration of a structure tag.
4871 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4872 If MS extensions are enabled and foo names a structure, then
4873 again this is an anonymous struct.
4874 Otherwise this is an error.
4876 Oh what a horrid tangled web we weave. I wonder if MS consciously
4877 took this from Plan 9 or if it was an accident of implementation
4878 that took root before someone noticed the bug... */
4880 tree type = TREE_VALUE (declspecs);
4882 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4883 type = TREE_TYPE (type);
4884 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4886 if (flag_ms_extensions)
4887 ; /* ok */
4888 else if (flag_iso)
4889 goto warn_unnamed_field;
4890 else if (TYPE_NAME (type) == NULL)
4891 ; /* ok */
4892 else
4893 goto warn_unnamed_field;
4895 else
4897 warn_unnamed_field:
4898 warning ("declaration does not declare anything");
4899 return NULL_TREE;
4903 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4904 width ? &width : NULL);
4906 finish_decl (value, NULL_TREE, NULL_TREE);
4907 DECL_INITIAL (value) = width;
4909 return value;
4912 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4913 the list such that this does not present a problem later. */
4915 static void
4916 detect_field_duplicates (tree fieldlist)
4918 tree x, y;
4919 int timeout = 10;
4921 /* First, see if there are more than "a few" fields.
4922 This is trivially true if there are zero or one fields. */
4923 if (!fieldlist)
4924 return;
4925 x = TREE_CHAIN (fieldlist);
4926 if (!x)
4927 return;
4928 do {
4929 timeout--;
4930 x = TREE_CHAIN (x);
4931 } while (timeout > 0 && x);
4933 /* If there were "few" fields, avoid the overhead of allocating
4934 a hash table. Instead just do the nested traversal thing. */
4935 if (timeout > 0)
4937 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4938 if (DECL_NAME (x))
4940 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4941 if (DECL_NAME (y) == DECL_NAME (x))
4943 error ("%Jduplicate member '%D'", x, x);
4944 DECL_NAME (x) = NULL_TREE;
4948 else
4950 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4951 void **slot;
4953 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4954 if ((y = DECL_NAME (x)) != 0)
4956 slot = htab_find_slot (htab, y, INSERT);
4957 if (*slot)
4959 error ("%Jduplicate member '%D'", x, x);
4960 DECL_NAME (x) = NULL_TREE;
4962 *slot = y;
4965 htab_delete (htab);
4969 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4970 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4971 ATTRIBUTES are attributes to be applied to the structure. */
4973 tree
4974 finish_struct (tree t, tree fieldlist, tree attributes)
4976 tree x;
4977 int toplevel = global_scope == current_scope;
4978 int saw_named_field;
4980 /* If this type was previously laid out as a forward reference,
4981 make sure we lay it out again. */
4983 TYPE_SIZE (t) = 0;
4985 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4987 /* Nameless union parm types are useful as GCC extension. */
4988 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4989 /* Otherwise, warn about any struct or union def. in parmlist. */
4990 if (in_parm_level_p ())
4992 if (pedantic)
4993 pedwarn ("%s defined inside parms",
4994 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4995 else
4996 warning ("%s defined inside parms",
4997 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
5000 if (pedantic)
5002 for (x = fieldlist; x; x = TREE_CHAIN (x))
5003 if (DECL_NAME (x) != 0)
5004 break;
5006 if (x == 0)
5007 pedwarn ("%s has no %s",
5008 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5009 fieldlist ? _("named members") : _("members"));
5012 /* Install struct as DECL_CONTEXT of each field decl.
5013 Also process specified field sizes,m which is found in the DECL_INITIAL.
5014 Store 0 there, except for ": 0" fields (so we can find them
5015 and delete them, below). */
5017 saw_named_field = 0;
5018 for (x = fieldlist; x; x = TREE_CHAIN (x))
5020 DECL_CONTEXT (x) = t;
5021 DECL_PACKED (x) |= TYPE_PACKED (t);
5023 /* If any field is const, the structure type is pseudo-const. */
5024 if (TREE_READONLY (x))
5025 C_TYPE_FIELDS_READONLY (t) = 1;
5026 else
5028 /* A field that is pseudo-const makes the structure likewise. */
5029 tree t1 = TREE_TYPE (x);
5030 while (TREE_CODE (t1) == ARRAY_TYPE)
5031 t1 = TREE_TYPE (t1);
5032 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5033 && C_TYPE_FIELDS_READONLY (t1))
5034 C_TYPE_FIELDS_READONLY (t) = 1;
5037 /* Any field that is volatile means variables of this type must be
5038 treated in some ways as volatile. */
5039 if (TREE_THIS_VOLATILE (x))
5040 C_TYPE_FIELDS_VOLATILE (t) = 1;
5042 /* Any field of nominal variable size implies structure is too. */
5043 if (C_DECL_VARIABLE_SIZE (x))
5044 C_TYPE_VARIABLE_SIZE (t) = 1;
5046 /* Detect invalid nested redefinition. */
5047 if (TREE_TYPE (x) == t)
5048 error ("nested redefinition of `%s'",
5049 IDENTIFIER_POINTER (TYPE_NAME (t)));
5051 if (DECL_INITIAL (x))
5053 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5054 DECL_SIZE (x) = bitsize_int (width);
5055 DECL_BIT_FIELD (x) = 1;
5056 SET_DECL_C_BIT_FIELD (x);
5059 DECL_INITIAL (x) = 0;
5061 /* Detect flexible array member in an invalid context. */
5062 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5063 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5064 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5065 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5067 if (TREE_CODE (t) == UNION_TYPE)
5068 error ("%Jflexible array member in union", x);
5069 else if (TREE_CHAIN (x) != NULL_TREE)
5070 error ("%Jflexible array member not at end of struct", x);
5071 else if (! saw_named_field)
5072 error ("%Jflexible array member in otherwise empty struct", x);
5075 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5076 && flexible_array_type_p (TREE_TYPE (x)))
5077 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5079 if (DECL_NAME (x))
5080 saw_named_field = 1;
5083 detect_field_duplicates (fieldlist);
5085 /* Now we have the nearly final fieldlist. Record it,
5086 then lay out the structure or union (including the fields). */
5088 TYPE_FIELDS (t) = fieldlist;
5090 layout_type (t);
5092 /* Delete all zero-width bit-fields from the fieldlist. */
5094 tree *fieldlistp = &fieldlist;
5095 while (*fieldlistp)
5096 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5097 *fieldlistp = TREE_CHAIN (*fieldlistp);
5098 else
5099 fieldlistp = &TREE_CHAIN (*fieldlistp);
5102 /* Now we have the truly final field list.
5103 Store it in this type and in the variants. */
5105 TYPE_FIELDS (t) = fieldlist;
5107 /* If there are lots of fields, sort so we can look through them fast.
5108 We arbitrarily consider 16 or more elts to be "a lot". */
5111 int len = 0;
5113 for (x = fieldlist; x; x = TREE_CHAIN (x))
5115 if (len > 15 || DECL_NAME (x) == NULL)
5116 break;
5117 len += 1;
5120 if (len > 15)
5122 tree *field_array;
5123 struct lang_type *space;
5124 struct sorted_fields_type *space2;
5126 len += list_length (x);
5128 /* Use the same allocation policy here that make_node uses, to
5129 ensure that this lives as long as the rest of the struct decl.
5130 All decls in an inline function need to be saved. */
5132 space = ggc_alloc (sizeof (struct lang_type));
5133 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5135 len = 0;
5136 space->s = space2;
5137 field_array = &space2->elts[0];
5138 for (x = fieldlist; x; x = TREE_CHAIN (x))
5140 field_array[len++] = x;
5142 /* If there is anonymous struct or union, break out of the loop. */
5143 if (DECL_NAME (x) == NULL)
5144 break;
5146 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5147 if (x == NULL)
5149 TYPE_LANG_SPECIFIC (t) = space;
5150 TYPE_LANG_SPECIFIC (t)->s->len = len;
5151 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5152 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5157 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5159 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5160 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5161 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5162 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5165 /* If this was supposed to be a transparent union, but we can't
5166 make it one, warn and turn off the flag. */
5167 if (TREE_CODE (t) == UNION_TYPE
5168 && TYPE_TRANSPARENT_UNION (t)
5169 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5171 TYPE_TRANSPARENT_UNION (t) = 0;
5172 warning ("union cannot be made transparent");
5175 /* If this structure or union completes the type of any previous
5176 variable declaration, lay it out and output its rtl. */
5177 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5179 x = TREE_CHAIN (x))
5181 tree decl = TREE_VALUE (x);
5182 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5183 layout_array_type (TREE_TYPE (decl));
5184 if (TREE_CODE (decl) != TYPE_DECL)
5186 layout_decl (decl, 0);
5187 if (c_dialect_objc ())
5188 objc_check_decl (decl);
5189 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5190 if (! toplevel)
5191 expand_decl (decl);
5194 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5196 /* Finish debugging output for this type. */
5197 rest_of_type_compilation (t, toplevel);
5199 return t;
5202 /* Lay out the type T, and its element type, and so on. */
5204 static void
5205 layout_array_type (tree t)
5207 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5208 layout_array_type (TREE_TYPE (t));
5209 layout_type (t);
5212 /* Begin compiling the definition of an enumeration type.
5213 NAME is its name (or null if anonymous).
5214 Returns the type object, as yet incomplete.
5215 Also records info about it so that build_enumerator
5216 may be used to declare the individual values as they are read. */
5218 tree
5219 start_enum (tree name)
5221 tree enumtype = 0;
5223 /* If this is the real definition for a previous forward reference,
5224 fill in the contents in the same object that used to be the
5225 forward reference. */
5227 if (name != 0)
5228 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5230 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5232 enumtype = make_node (ENUMERAL_TYPE);
5233 pushtag (name, enumtype);
5236 C_TYPE_BEING_DEFINED (enumtype) = 1;
5238 if (TYPE_VALUES (enumtype) != 0)
5240 /* This enum is a named one that has been declared already. */
5241 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5243 /* Completely replace its old definition.
5244 The old enumerators remain defined, however. */
5245 TYPE_VALUES (enumtype) = 0;
5248 enum_next_value = integer_zero_node;
5249 enum_overflow = 0;
5251 if (flag_short_enums)
5252 TYPE_PACKED (enumtype) = 1;
5254 return enumtype;
5257 /* After processing and defining all the values of an enumeration type,
5258 install their decls in the enumeration type and finish it off.
5259 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5260 and ATTRIBUTES are the specified attributes.
5261 Returns ENUMTYPE. */
5263 tree
5264 finish_enum (tree enumtype, tree values, tree attributes)
5266 tree pair, tem;
5267 tree minnode = 0, maxnode = 0, enum_value_type;
5268 int precision, unsign;
5269 int toplevel = (global_scope == current_scope);
5271 if (in_parm_level_p ())
5272 warning ("enum defined inside parms");
5274 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5276 /* Calculate the maximum value of any enumerator in this type. */
5278 if (values == error_mark_node)
5279 minnode = maxnode = integer_zero_node;
5280 else
5282 minnode = maxnode = TREE_VALUE (values);
5283 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5285 tree value = TREE_VALUE (pair);
5286 if (tree_int_cst_lt (maxnode, value))
5287 maxnode = value;
5288 if (tree_int_cst_lt (value, minnode))
5289 minnode = value;
5293 /* Construct the final type of this enumeration. It is the same
5294 as one of the integral types - the narrowest one that fits, except
5295 that normally we only go as narrow as int - and signed iff any of
5296 the values are negative. */
5297 unsign = (tree_int_cst_sgn (minnode) >= 0);
5298 precision = MAX (min_precision (minnode, unsign),
5299 min_precision (maxnode, unsign));
5300 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5302 tree narrowest = c_common_type_for_size (precision, unsign);
5303 if (narrowest == 0)
5305 warning ("enumeration values exceed range of largest integer");
5306 narrowest = long_long_integer_type_node;
5309 precision = TYPE_PRECISION (narrowest);
5311 else
5312 precision = TYPE_PRECISION (integer_type_node);
5314 if (precision == TYPE_PRECISION (integer_type_node))
5315 enum_value_type = c_common_type_for_size (precision, 0);
5316 else
5317 enum_value_type = enumtype;
5319 TYPE_MIN_VALUE (enumtype) = minnode;
5320 TYPE_MAX_VALUE (enumtype) = maxnode;
5321 TYPE_PRECISION (enumtype) = precision;
5322 TREE_UNSIGNED (enumtype) = unsign;
5323 TYPE_SIZE (enumtype) = 0;
5324 layout_type (enumtype);
5326 if (values != error_mark_node)
5328 /* Change the type of the enumerators to be the enum type. We
5329 need to do this irrespective of the size of the enum, for
5330 proper type checking. Replace the DECL_INITIALs of the
5331 enumerators, and the value slots of the list, with copies
5332 that have the enum type; they cannot be modified in place
5333 because they may be shared (e.g. integer_zero_node) Finally,
5334 change the purpose slots to point to the names of the decls. */
5335 for (pair = values; pair; pair = TREE_CHAIN (pair))
5337 tree enu = TREE_PURPOSE (pair);
5339 TREE_TYPE (enu) = enumtype;
5341 /* The ISO C Standard mandates enumerators to have type int,
5342 even though the underlying type of an enum type is
5343 unspecified. Here we convert any enumerators that fit in
5344 an int to type int, to avoid promotions to unsigned types
5345 when comparing integers with enumerators that fit in the
5346 int range. When -pedantic is given, build_enumerator()
5347 would have already taken care of those that don't fit. */
5348 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5349 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5350 else
5351 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5353 TREE_PURPOSE (pair) = DECL_NAME (enu);
5354 TREE_VALUE (pair) = DECL_INITIAL (enu);
5357 TYPE_VALUES (enumtype) = values;
5360 /* Fix up all variant types of this enum type. */
5361 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5363 if (tem == enumtype)
5364 continue;
5365 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5366 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5367 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5368 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5369 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5370 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5371 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5372 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5373 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5374 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5377 /* Finish debugging output for this type. */
5378 rest_of_type_compilation (enumtype, toplevel);
5380 return enumtype;
5383 /* Build and install a CONST_DECL for one value of the
5384 current enumeration type (one that was begun with start_enum).
5385 Return a tree-list containing the CONST_DECL and its value.
5386 Assignment of sequential values by default is handled here. */
5388 tree
5389 build_enumerator (tree name, tree value)
5391 tree decl, type;
5393 /* Validate and default VALUE. */
5395 /* Remove no-op casts from the value. */
5396 if (value)
5397 STRIP_TYPE_NOPS (value);
5399 if (value != 0)
5401 if (TREE_CODE (value) == INTEGER_CST)
5403 value = default_conversion (value);
5404 constant_expression_warning (value);
5406 else
5408 error ("enumerator value for `%s' not integer constant",
5409 IDENTIFIER_POINTER (name));
5410 value = 0;
5414 /* Default based on previous value. */
5415 /* It should no longer be possible to have NON_LVALUE_EXPR
5416 in the default. */
5417 if (value == 0)
5419 value = enum_next_value;
5420 if (enum_overflow)
5421 error ("overflow in enumeration values");
5424 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5426 pedwarn ("ISO C restricts enumerator values to range of `int'");
5427 value = convert (integer_type_node, value);
5430 /* Set basis for default for next value. */
5431 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5432 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5434 /* Now create a declaration for the enum value name. */
5436 type = TREE_TYPE (value);
5437 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5438 TYPE_PRECISION (integer_type_node)),
5439 (TYPE_PRECISION (type)
5440 >= TYPE_PRECISION (integer_type_node)
5441 && TREE_UNSIGNED (type)));
5443 decl = build_decl (CONST_DECL, name, type);
5444 DECL_INITIAL (decl) = convert (type, value);
5445 pushdecl (decl);
5447 return tree_cons (decl, value, NULL_TREE);
5451 /* Create the FUNCTION_DECL for a function definition.
5452 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5453 the declaration; they describe the function's name and the type it returns,
5454 but twisted together in a fashion that parallels the syntax of C.
5456 This function creates a binding context for the function body
5457 as well as setting up the FUNCTION_DECL in current_function_decl.
5459 Returns 1 on success. If the DECLARATOR is not suitable for a function
5460 (it defines a datum instead), we return 0, which tells
5461 yyparse to report a parse error. */
5464 start_function (tree declspecs, tree declarator, tree attributes)
5466 tree decl1, old_decl;
5467 tree restype;
5468 int old_immediate_size_expand = immediate_size_expand;
5470 current_function_returns_value = 0; /* Assume, until we see it does. */
5471 current_function_returns_null = 0;
5472 current_function_returns_abnormally = 0;
5473 warn_about_return_type = 0;
5474 current_extern_inline = 0;
5475 c_in_iteration_stmt = 0;
5476 c_in_case_stmt = 0;
5478 /* Don't expand any sizes in the return type of the function. */
5479 immediate_size_expand = 0;
5481 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5483 /* If the declarator is not suitable for a function definition,
5484 cause a syntax error. */
5485 if (decl1 == 0)
5487 immediate_size_expand = old_immediate_size_expand;
5488 return 0;
5491 decl_attributes (&decl1, attributes, 0);
5493 if (DECL_DECLARED_INLINE_P (decl1)
5494 && DECL_UNINLINABLE (decl1)
5495 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5496 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5498 announce_function (decl1);
5500 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5502 error ("return type is an incomplete type");
5503 /* Make it return void instead. */
5504 TREE_TYPE (decl1)
5505 = build_function_type (void_type_node,
5506 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5509 if (warn_about_return_type)
5510 pedwarn_c99 ("return type defaults to `int'");
5512 /* Save the parm names or decls from this function's declarator
5513 where store_parm_decls will find them. */
5514 current_function_parms = last_function_parms;
5515 current_function_parm_tags = last_function_parm_tags;
5516 current_function_parm_others = last_function_parm_others;
5518 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5519 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5520 DECL_INITIAL (decl1) = error_mark_node;
5522 /* If this definition isn't a prototype and we had a prototype declaration
5523 before, copy the arg type info from that prototype.
5524 But not if what we had before was a builtin function. */
5525 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5526 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5527 && !DECL_BUILT_IN (old_decl)
5528 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5529 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5530 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5532 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5533 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5536 /* Optionally warn of old-fashioned def with no previous prototype. */
5537 if (warn_strict_prototypes
5538 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5539 && C_DECL_ISNT_PROTOTYPE (old_decl))
5540 warning ("function declaration isn't a prototype");
5541 /* Optionally warn of any global def with no previous prototype. */
5542 else if (warn_missing_prototypes
5543 && TREE_PUBLIC (decl1)
5544 && ! MAIN_NAME_P (DECL_NAME (decl1))
5545 && C_DECL_ISNT_PROTOTYPE (old_decl))
5546 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5547 /* Optionally warn of any def with no previous prototype
5548 if the function has already been used. */
5549 else if (warn_missing_prototypes
5550 && old_decl != 0 && TREE_USED (old_decl)
5551 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5552 warning ("%J'%D' was used with no prototype before its definition",
5553 decl1, decl1);
5554 /* Optionally warn of any global def with no previous declaration. */
5555 else if (warn_missing_declarations
5556 && TREE_PUBLIC (decl1)
5557 && old_decl == 0
5558 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5559 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5560 /* Optionally warn of any def with no previous declaration
5561 if the function has already been used. */
5562 else if (warn_missing_declarations
5563 && old_decl != 0 && TREE_USED (old_decl)
5564 && C_DECL_IMPLICIT (old_decl))
5565 warning ("%J`%D' was used with no declaration before its definition",
5566 decl1, decl1);
5568 /* This is a definition, not a reference.
5569 So normally clear DECL_EXTERNAL.
5570 However, `extern inline' acts like a declaration
5571 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5572 DECL_EXTERNAL (decl1) = current_extern_inline;
5574 /* This function exists in static storage.
5575 (This does not mean `static' in the C sense!) */
5576 TREE_STATIC (decl1) = 1;
5578 /* A nested function is not global. */
5579 if (current_function_decl != 0)
5580 TREE_PUBLIC (decl1) = 0;
5582 #ifdef ENABLE_CHECKING
5583 /* This is the earliest point at which we might know the assembler
5584 name of the function. Thus, if it's set before this, die horribly. */
5585 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5586 abort ();
5587 #endif
5589 /* If #pragma weak was used, mark the decl weak now. */
5590 if (current_scope == global_scope)
5591 maybe_apply_pragma_weak (decl1);
5593 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5594 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5596 tree args;
5597 int argct = 0;
5599 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5600 != integer_type_node)
5601 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5603 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5604 args = TREE_CHAIN (args))
5606 tree type = args ? TREE_VALUE (args) : 0;
5608 if (type == void_type_node)
5609 break;
5611 ++argct;
5612 switch (argct)
5614 case 1:
5615 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5616 pedwarn ("%Jfirst argument of '%D' should be `int'",
5617 decl1, decl1);
5618 break;
5620 case 2:
5621 if (TREE_CODE (type) != POINTER_TYPE
5622 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5623 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5624 != char_type_node))
5625 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5626 decl1, decl1);
5627 break;
5629 case 3:
5630 if (TREE_CODE (type) != POINTER_TYPE
5631 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5632 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5633 != char_type_node))
5634 pedwarn ("%Jthird argument of '%D' should probably be "
5635 "'char **'", decl1, decl1);
5636 break;
5640 /* It is intentional that this message does not mention the third
5641 argument because it's only mentioned in an appendix of the
5642 standard. */
5643 if (argct > 0 && (argct < 2 || argct > 3))
5644 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5646 if (! TREE_PUBLIC (decl1))
5647 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5650 /* Record the decl so that the function name is defined.
5651 If we already have a decl for this name, and it is a FUNCTION_DECL,
5652 use the old decl. */
5654 current_function_decl = pushdecl (decl1);
5656 pushlevel (0);
5657 declare_parm_level ();
5659 make_decl_rtl (current_function_decl, NULL);
5661 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5662 /* Promote the value to int before returning it. */
5663 if (c_promoting_integer_type_p (restype))
5665 /* It retains unsignedness if not really getting wider. */
5666 if (TREE_UNSIGNED (restype)
5667 && (TYPE_PRECISION (restype)
5668 == TYPE_PRECISION (integer_type_node)))
5669 restype = unsigned_type_node;
5670 else
5671 restype = integer_type_node;
5673 DECL_RESULT (current_function_decl)
5674 = build_decl (RESULT_DECL, NULL_TREE, restype);
5676 /* If this fcn was already referenced via a block-scope `extern' decl
5677 (or an implicit decl), propagate certain information about the usage. */
5678 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5679 TREE_ADDRESSABLE (current_function_decl) = 1;
5681 immediate_size_expand = old_immediate_size_expand;
5683 start_fname_decls ();
5685 return 1;
5688 /* Subroutine of store_parm_decls which handles new-style function
5689 definitions (prototype format). The parms already have decls, so we
5690 need only record them as in effect and complain if any redundant
5691 old-style parm decls were written. */
5692 static void
5693 store_parm_decls_newstyle (void)
5695 tree decl, last;
5696 tree fndecl = current_function_decl;
5697 tree parms = current_function_parms;
5698 tree tags = current_function_parm_tags;
5699 tree others = current_function_parm_others;
5701 if (current_scope->parms || current_scope->names || current_scope->tags)
5703 error ("%Jold-style parameter declarations in prototyped "
5704 "function definition", fndecl);
5706 /* Get rid of the old-style declarations. */
5707 poplevel (0, 0, 0);
5708 pushlevel (0);
5711 /* Now make all the parameter declarations visible in the function body.
5712 We can bypass most of the grunt work of pushdecl. */
5713 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5715 DECL_CONTEXT (decl) = current_function_decl;
5716 if (DECL_NAME (decl) == 0)
5717 error ("%Jparameter name omitted", decl);
5718 else
5720 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5721 current_scope->shadowed
5722 = tree_cons (DECL_NAME (decl),
5723 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5724 current_scope->shadowed);
5725 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5728 current_scope->parms = parms;
5729 current_scope->parms_last = last;
5731 /* Record the parameter list in the function declaration. */
5732 DECL_ARGUMENTS (fndecl) = parms;
5734 /* Now make all the ancillary declarations visible, likewise. */
5735 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5737 DECL_CONTEXT (decl) = current_function_decl;
5738 if (DECL_NAME (decl)
5739 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5741 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5742 current_scope->shadowed
5743 = tree_cons (DECL_NAME (decl),
5744 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5745 current_scope->shadowed);
5746 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5749 current_scope->names = others;
5750 current_scope->names_last = last;
5752 /* And all the tag declarations. */
5753 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5754 if (TREE_PURPOSE (decl))
5756 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5757 current_scope->shadowed_tags
5758 = tree_cons (TREE_PURPOSE (decl),
5759 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5760 current_scope->shadowed_tags);
5761 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5763 current_scope->tags = tags;
5766 /* Subroutine of store_parm_decls which handles old-style function
5767 definitions (separate parameter list and declarations). */
5769 static void
5770 store_parm_decls_oldstyle (void)
5772 tree parm, decl, last;
5773 tree fndecl = current_function_decl;
5775 /* This is the identifier list from the function declarator. */
5776 tree parmids = current_function_parms;
5778 /* We use DECL_WEAK as a flag to show which parameters have been
5779 seen already, since it is not used on PARM_DECL. */
5780 #ifdef ENABLE_CHECKING
5781 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5782 if (DECL_WEAK (parm))
5783 abort ();
5784 #endif
5786 /* Match each formal parameter name with its declaration. Save each
5787 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5788 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5790 if (TREE_VALUE (parm) == 0)
5792 error ("%Jparameter name missing from parameter list", fndecl);
5793 TREE_PURPOSE (parm) = 0;
5794 continue;
5797 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5798 if (decl && DECL_CONTEXT (decl) == fndecl)
5800 /* If we got something other than a PARM_DECL it is an error. */
5801 if (TREE_CODE (decl) != PARM_DECL)
5802 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5803 /* If the declaration is already marked, we have a duplicate
5804 name. Complain and ignore the duplicate. */
5805 else if (DECL_WEAK (decl))
5807 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5808 TREE_PURPOSE (parm) = 0;
5809 continue;
5811 /* If the declaration says "void", complain and turn it into
5812 an int. */
5813 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5815 error ("%Jparameter \"%D\" declared void", decl, decl);
5816 TREE_TYPE (decl) = integer_type_node;
5817 DECL_ARG_TYPE (decl) = integer_type_node;
5818 layout_decl (decl, 0);
5821 /* If no declaration found, default to int. */
5822 else
5824 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5825 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5826 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5827 pushdecl (decl);
5829 if (flag_isoc99)
5830 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5831 else if (extra_warnings)
5832 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5835 TREE_PURPOSE (parm) = decl;
5836 DECL_WEAK (decl) = 1;
5839 /* Now examine the parms chain for incomplete declarations
5840 and declarations with no corresponding names. */
5842 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5844 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5846 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5847 TREE_TYPE (parm) = error_mark_node;
5850 if (! DECL_WEAK (parm))
5852 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5853 parm, parm);
5855 /* Pretend the parameter was not missing.
5856 This gets us to a standard state and minimizes
5857 further error messages. */
5858 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5862 /* Chain the declarations together in the order of the list of
5863 names. Store that chain in the function decl, replacing the
5864 list of names. Update the current scope to match. */
5865 DECL_ARGUMENTS (fndecl) = 0;
5867 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5868 if (TREE_PURPOSE (parm))
5869 break;
5870 if (parm && TREE_PURPOSE (parm))
5872 last = TREE_PURPOSE (parm);
5873 DECL_ARGUMENTS (fndecl) = last;
5874 current_scope->parms = last;
5875 DECL_WEAK (last) = 0;
5877 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5878 if (TREE_PURPOSE (parm))
5880 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5881 last = TREE_PURPOSE (parm);
5882 DECL_WEAK (last) = 0;
5884 current_scope->parms_last = last;
5885 TREE_CHAIN (last) = 0;
5888 /* If there was a previous prototype,
5889 set the DECL_ARG_TYPE of each argument according to
5890 the type previously specified, and report any mismatches. */
5892 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5894 tree type;
5895 for (parm = DECL_ARGUMENTS (fndecl),
5896 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5897 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5898 != void_type_node));
5899 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5901 if (parm == 0 || type == 0
5902 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5904 error ("number of arguments doesn't match prototype");
5905 error ("%Hprototype declaration",
5906 &current_function_prototype_locus);
5907 break;
5909 /* Type for passing arg must be consistent with that
5910 declared for the arg. ISO C says we take the unqualified
5911 type for parameters declared with qualified type. */
5912 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5913 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5914 COMPARE_STRICT))
5916 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5917 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5919 /* Adjust argument to match prototype. E.g. a previous
5920 `int foo(float);' prototype causes
5921 `int foo(x) float x; {...}' to be treated like
5922 `int foo(float x) {...}'. This is particularly
5923 useful for argument types like uid_t. */
5924 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5926 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5927 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5928 && TYPE_PRECISION (TREE_TYPE (parm))
5929 < TYPE_PRECISION (integer_type_node))
5930 DECL_ARG_TYPE (parm) = integer_type_node;
5932 if (pedantic)
5934 pedwarn ("promoted argument \"%D\" "
5935 "doesn't match prototype", parm);
5936 pedwarn ("%Hprototype declaration",
5937 &current_function_prototype_locus);
5940 else
5942 error ("argument \"%D\" doesn't match prototype", parm);
5943 error ("%Hprototype declaration",
5944 &current_function_prototype_locus);
5948 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5951 /* Otherwise, create a prototype that would match. */
5953 else
5955 tree actual = 0, last = 0, type;
5957 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5959 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5960 if (last)
5961 TREE_CHAIN (last) = type;
5962 else
5963 actual = type;
5964 last = type;
5966 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5967 if (last)
5968 TREE_CHAIN (last) = type;
5969 else
5970 actual = type;
5972 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5973 of the type of this function, but we need to avoid having this
5974 affect the types of other similarly-typed functions, so we must
5975 first force the generation of an identical (but separate) type
5976 node for the relevant function type. The new node we create
5977 will be a variant of the main variant of the original function
5978 type. */
5980 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5982 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5986 /* Store the parameter declarations into the current function declaration.
5987 This is called after parsing the parameter declarations, before
5988 digesting the body of the function.
5990 For an old-style definition, construct a prototype out of the old-style
5991 parameter declarations and inject it into the function's type. */
5993 void
5994 store_parm_decls (void)
5996 tree fndecl = current_function_decl;
5998 /* The function containing FNDECL, if any. */
5999 tree context = decl_function_context (fndecl);
6001 /* True if this definition is written with a prototype. */
6002 bool prototype = (current_function_parms
6003 && TREE_CODE (current_function_parms) != TREE_LIST);
6005 if (prototype)
6006 store_parm_decls_newstyle ();
6007 else
6008 store_parm_decls_oldstyle ();
6010 /* The next call to pushlevel will be a function body. */
6012 next_is_function_body = true;
6014 /* Write a record describing this function definition to the prototypes
6015 file (if requested). */
6017 gen_aux_info_record (fndecl, 1, 0, prototype);
6019 /* Initialize the RTL code for the function. */
6020 allocate_struct_function (fndecl);
6022 /* Begin the statement tree for this function. */
6023 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6025 /* If this is a nested function, save away the sizes of any
6026 variable-size types so that we can expand them when generating
6027 RTL. */
6028 if (context)
6030 tree t;
6032 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6033 = nreverse (get_pending_sizes ());
6034 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6036 t = TREE_CHAIN (t))
6037 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6040 /* This function is being processed in whole-function mode. */
6041 cfun->x_whole_function_mode_p = 1;
6043 /* Even though we're inside a function body, we still don't want to
6044 call expand_expr to calculate the size of a variable-sized array.
6045 We haven't necessarily assigned RTL to all variables yet, so it's
6046 not safe to try to expand expressions involving them. */
6047 immediate_size_expand = 0;
6048 cfun->x_dont_save_pending_sizes_p = 1;
6051 /* Finish up a function declaration and compile that function
6052 all the way to assembler language output. The free the storage
6053 for the function definition.
6055 This is called after parsing the body of the function definition. */
6057 void
6058 finish_function (void)
6060 tree fndecl = current_function_decl;
6062 /* When a function declaration is totally empty, e.g.
6063 void foo(void) { }
6064 (the argument list is irrelevant) the compstmt rule will not
6065 bother calling pushlevel/poplevel, which means we get here with
6066 the scope stack out of sync. Detect this situation by noticing
6067 that current_scope is still as store_parm_decls left it, and do
6068 a dummy push/pop to get back to consistency.
6069 Note that the call to pushlevel does not actually push another
6070 scope - see there for details. */
6072 if (current_scope->parm_flag && next_is_function_body)
6074 pushlevel (0);
6075 poplevel (0, 0, 0);
6078 if (TREE_CODE (fndecl) == FUNCTION_DECL
6079 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6081 tree args = DECL_ARGUMENTS (fndecl);
6082 for (; args; args = TREE_CHAIN (args))
6084 tree type = TREE_TYPE (args);
6085 if (INTEGRAL_TYPE_P (type)
6086 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6087 DECL_ARG_TYPE (args) = integer_type_node;
6091 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6092 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6094 /* Must mark the RESULT_DECL as being in this function. */
6096 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6097 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6099 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6101 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6102 != integer_type_node)
6104 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6105 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6106 if (!warn_main)
6107 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6109 else
6111 #ifdef DEFAULT_MAIN_RETURN
6112 /* Make it so that `main' always returns success by default. */
6113 DEFAULT_MAIN_RETURN;
6114 #else
6115 if (flag_isoc99)
6116 c_expand_return (integer_zero_node);
6117 #endif
6121 finish_fname_decls ();
6123 /* Tie off the statement tree for this function. */
6124 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6126 /* Complain if there's just no return statement. */
6127 if (warn_return_type
6128 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6129 && !current_function_returns_value && !current_function_returns_null
6130 /* Don't complain if we abort. */
6131 && !current_function_returns_abnormally
6132 /* Don't warn for main(). */
6133 && !MAIN_NAME_P (DECL_NAME (fndecl))
6134 /* Or if they didn't actually specify a return type. */
6135 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6136 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6137 inline function, as we might never be compiled separately. */
6138 && DECL_INLINE (fndecl))
6139 warning ("no return statement in function returning non-void");
6141 /* With just -Wextra, complain only if function returns both with
6142 and without a value. */
6143 if (extra_warnings
6144 && current_function_returns_value
6145 && current_function_returns_null)
6146 warning ("this function may return with or without a value");
6148 /* We're leaving the context of this function, so zap cfun. It's still in
6149 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6150 cfun = NULL;
6152 /* ??? Objc emits functions after finalizing the compilation unit.
6153 This should be cleaned up later and this conditional removed. */
6154 if (!cgraph_global_info_ready)
6155 cgraph_finalize_function (fndecl, false);
6156 else
6157 c_expand_body (fndecl);
6158 current_function_decl = NULL;
6161 /* Generate the RTL for a deferred function FNDECL. */
6163 void
6164 c_expand_deferred_function (tree fndecl)
6166 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6167 function was deferred, e.g. in duplicate_decls. */
6168 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6170 if (flag_inline_trees)
6172 timevar_push (TV_INTEGRATION);
6173 optimize_inline_calls (fndecl);
6174 timevar_pop (TV_INTEGRATION);
6176 c_expand_body (fndecl);
6177 current_function_decl = NULL;
6181 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6182 then we are already in the process of generating RTL for another
6183 function. */
6185 static void
6186 c_expand_body_1 (tree fndecl, int nested_p)
6188 if (nested_p)
6190 /* Make sure that we will evaluate variable-sized types involved
6191 in our function's type. */
6192 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6194 /* Squirrel away our current state. */
6195 push_function_context ();
6198 tree_rest_of_compilation (fndecl, nested_p);
6200 if (nested_p)
6201 /* Return to the enclosing function. */
6202 pop_function_context ();
6204 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6206 if (targetm.have_ctors_dtors)
6207 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6208 DEFAULT_INIT_PRIORITY);
6209 else
6210 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6213 if (DECL_STATIC_DESTRUCTOR (fndecl))
6215 if (targetm.have_ctors_dtors)
6216 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6217 DEFAULT_INIT_PRIORITY);
6218 else
6219 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6223 /* Like c_expand_body_1 but only for unnested functions. */
6225 void
6226 c_expand_body (tree fndecl)
6229 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6230 c_expand_body_1 (fndecl, 0);
6233 /* Check the declarations given in a for-loop for satisfying the C99
6234 constraints. */
6235 void
6236 check_for_loop_decls (void)
6238 tree t;
6240 if (!flag_isoc99)
6242 /* If we get here, declarations have been used in a for loop without
6243 the C99 for loop scope. This doesn't make much sense, so don't
6244 allow it. */
6245 error ("'for' loop initial declaration used outside C99 mode");
6246 return;
6248 /* C99 subclause 6.8.5 paragraph 3:
6250 [#3] The declaration part of a for statement shall only
6251 declare identifiers for objects having storage class auto or
6252 register.
6254 It isn't clear whether, in this sentence, "identifiers" binds to
6255 "shall only declare" or to "objects" - that is, whether all identifiers
6256 declared must be identifiers for objects, or whether the restriction
6257 only applies to those that are. (A question on this in comp.std.c
6258 in November 2000 received no answer.) We implement the strictest
6259 interpretation, to avoid creating an extension which later causes
6260 problems. */
6262 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6264 if (TREE_PURPOSE (t) != 0)
6266 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6268 if (code == RECORD_TYPE)
6269 error ("'struct %s' declared in 'for' loop initial declaration",
6270 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6271 else if (code == UNION_TYPE)
6272 error ("'union %s' declared in 'for' loop initial declaration",
6273 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6274 else
6275 error ("'enum %s' declared in 'for' loop initial declaration",
6276 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6280 for (t = getdecls (); t; t = TREE_CHAIN (t))
6282 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6283 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6284 "initial declaration", t, t);
6285 else if (TREE_STATIC (t))
6286 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6287 "initial declaration", t, t);
6288 else if (DECL_EXTERNAL (t))
6289 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6290 "initial declaration", t, t);
6294 /* Save and reinitialize the variables
6295 used during compilation of a C function. */
6297 void
6298 c_push_function_context (struct function *f)
6300 struct language_function *p;
6301 p = ggc_alloc (sizeof (struct language_function));
6302 f->language = p;
6304 p->base.x_stmt_tree = c_stmt_tree;
6305 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6306 p->x_in_iteration_stmt = c_in_iteration_stmt;
6307 p->x_in_case_stmt = c_in_case_stmt;
6308 p->returns_value = current_function_returns_value;
6309 p->returns_null = current_function_returns_null;
6310 p->returns_abnormally = current_function_returns_abnormally;
6311 p->warn_about_return_type = warn_about_return_type;
6312 p->extern_inline = current_extern_inline;
6315 /* Restore the variables used during compilation of a C function. */
6317 void
6318 c_pop_function_context (struct function *f)
6320 struct language_function *p = f->language;
6322 if (DECL_SAVED_INSNS (current_function_decl) == 0
6323 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6325 /* Stop pointing to the local nodes about to be freed. */
6326 /* But DECL_INITIAL must remain nonzero so we know this
6327 was an actual function definition. */
6328 DECL_INITIAL (current_function_decl) = error_mark_node;
6329 DECL_ARGUMENTS (current_function_decl) = 0;
6332 c_stmt_tree = p->base.x_stmt_tree;
6333 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6334 c_in_iteration_stmt = p->x_in_iteration_stmt;
6335 c_in_case_stmt = p->x_in_case_stmt;
6336 current_function_returns_value = p->returns_value;
6337 current_function_returns_null = p->returns_null;
6338 current_function_returns_abnormally = p->returns_abnormally;
6339 warn_about_return_type = p->warn_about_return_type;
6340 current_extern_inline = p->extern_inline;
6342 f->language = NULL;
6345 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6347 void
6348 c_dup_lang_specific_decl (tree decl)
6350 struct lang_decl *ld;
6352 if (!DECL_LANG_SPECIFIC (decl))
6353 return;
6355 ld = ggc_alloc (sizeof (struct lang_decl));
6356 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6357 DECL_LANG_SPECIFIC (decl) = ld;
6360 /* The functions below are required for functionality of doing
6361 function at once processing in the C front end. Currently these
6362 functions are not called from anywhere in the C front end, but as
6363 these changes continue, that will change. */
6365 /* Returns nonzero if the current statement is a full expression,
6366 i.e. temporaries created during that statement should be destroyed
6367 at the end of the statement. */
6370 stmts_are_full_exprs_p (void)
6372 return 0;
6375 /* Returns the stmt_tree (if any) to which statements are currently
6376 being added. If there is no active statement-tree, NULL is
6377 returned. */
6379 stmt_tree
6380 current_stmt_tree (void)
6382 return &c_stmt_tree;
6385 /* Returns the stack of SCOPE_STMTs for the current function. */
6387 tree *
6388 current_scope_stmt_stack (void)
6390 return &c_scope_stmt_stack;
6393 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6394 C. */
6397 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6399 return 0;
6402 /* Dummy function in place of callback used by C++. */
6404 void
6405 extract_interface_info (void)
6409 /* Return a new COMPOUND_STMT, after adding it to the current
6410 statement tree. */
6412 tree
6413 c_begin_compound_stmt (void)
6415 tree stmt;
6417 /* Create the COMPOUND_STMT. */
6418 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6420 return stmt;
6423 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6424 common code. */
6426 void
6427 c_expand_decl_stmt (tree t)
6429 tree decl = DECL_STMT_DECL (t);
6431 /* Expand nested functions. */
6432 if (TREE_CODE (decl) == FUNCTION_DECL
6433 && DECL_CONTEXT (decl) == current_function_decl
6434 && DECL_SAVED_TREE (decl))
6435 c_expand_body_1 (decl, 1);
6438 /* Return the global value of T as a symbol. */
6440 tree
6441 identifier_global_value (tree t)
6443 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6444 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6445 return decl;
6447 /* Shadowed by something else; find the true global value. */
6448 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6449 if (DECL_NAME (decl) == t)
6450 return decl;
6452 /* Only local values for this decl. */
6453 return 0;
6456 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6457 otherwise the name is found in ridpointers from RID_INDEX. */
6459 void
6460 record_builtin_type (enum rid rid_index, const char *name, tree type)
6462 tree id;
6463 if (name == 0)
6464 id = ridpointers[(int) rid_index];
6465 else
6466 id = get_identifier (name);
6467 pushdecl (build_decl (TYPE_DECL, id, type));
6470 /* Build the void_list_node (void_type_node having been created). */
6471 tree
6472 build_void_list_node (void)
6474 tree t = build_tree_list (NULL_TREE, void_type_node);
6475 return t;
6478 /* Return something to represent absolute declarators containing a *.
6479 TARGET is the absolute declarator that the * contains.
6480 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6481 to apply to the pointer type, represented as identifiers, possible mixed
6482 with attributes.
6484 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6485 if attributes are present) and whose type is the modifier list. */
6487 tree
6488 make_pointer_declarator (tree type_quals_attrs, tree target)
6490 tree quals, attrs;
6491 tree itarget = target;
6492 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6493 if (attrs != NULL_TREE)
6494 itarget = tree_cons (attrs, target, NULL_TREE);
6495 return build1 (INDIRECT_REF, quals, itarget);
6498 /* A wrapper around lhd_set_decl_assembler_name that gives static
6499 variables their C names if they are at file scope and only one
6500 translation unit is being compiled, for backwards compatibility
6501 with certain bizarre assembler hacks (like crtstuff.c). */
6503 void
6504 c_static_assembler_name (tree decl)
6506 if (num_in_fnames == 1
6507 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6508 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6509 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6510 else
6511 lhd_set_decl_assembler_name (decl);
6514 /* Hash and equality functions for link_hash_table: key off
6515 DECL_ASSEMBLER_NAME. */
6517 static hashval_t
6518 link_hash_hash (const void *x_p)
6520 tree x = (tree)x_p;
6521 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6524 static int
6525 link_hash_eq (const void *x1_p, const void *x2_p)
6527 tree x1 = (tree)x1_p;
6528 tree x2 = (tree)x2_p;
6529 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6532 /* Propagate information between definitions and uses between multiple
6533 translation units in TU_LIST based on linkage rules. */
6535 void
6536 merge_translation_unit_decls (void)
6538 const tree tu_list = current_file_decl;
6539 tree tu;
6540 tree decl;
6541 htab_t link_hash_table;
6542 tree block;
6544 /* Create the BLOCK that poplevel would have created, but don't
6545 actually call poplevel since that's expensive. */
6546 block = make_node (BLOCK);
6547 BLOCK_VARS (block) = current_scope->names;
6548 TREE_USED (block) = 1;
6549 DECL_INITIAL (current_file_decl) = block;
6551 /* If only one translation unit seen, no copying necessary. */
6552 if (TREE_CHAIN (tu_list) == NULL_TREE)
6553 return;
6555 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6557 /* Enter any actual definitions into the hash table. */
6558 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6559 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6560 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6562 PTR *slot;
6563 slot = htab_find_slot (link_hash_table, decl, INSERT);
6565 /* If we've already got a definition, work out which one is
6566 the real one, put it into the hash table, and make the
6567 other one DECL_EXTERNAL. This is important to avoid
6568 putting out two definitions of the same symbol in the
6569 assembly output. */
6570 if (*slot != NULL)
6572 tree old_decl = (tree) *slot;
6574 /* If this is weak or common or whatever, suppress it
6575 in favor of the other definition. */
6576 if (DECL_WEAK (decl))
6577 DECL_EXTERNAL (decl) = 1;
6578 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6579 DECL_EXTERNAL (old_decl) = 1;
6580 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6581 DECL_EXTERNAL (decl) = 1;
6582 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6583 DECL_EXTERNAL (old_decl) = 1;
6585 if (DECL_EXTERNAL (decl))
6587 DECL_INITIAL (decl) = NULL_TREE;
6588 DECL_COMMON (decl) = 0;
6589 DECL_ONE_ONLY (decl) = 0;
6590 DECL_WEAK (decl) = 0;
6592 else if (DECL_EXTERNAL (old_decl))
6594 DECL_INITIAL (old_decl) = NULL_TREE;
6595 DECL_COMMON (old_decl) = 0;
6596 DECL_ONE_ONLY (old_decl) = 0;
6597 DECL_WEAK (old_decl) = 0;
6598 *slot = decl;
6600 else
6602 error ("%Jredefinition of global '%D'", decl, decl);
6603 error ("%J'%D' previously defined here", old_decl, old_decl);
6606 else
6607 *slot = decl;
6610 /* Now insert the desired information from all the definitions
6611 into any plain declarations. */
6612 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6613 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6614 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6616 tree global_decl;
6617 global_decl = htab_find (link_hash_table, decl);
6619 if (! global_decl)
6620 continue;
6622 /* Print any appropriate error messages, and partially merge
6623 the decls. */
6624 (void) duplicate_decls (decl, global_decl, true, true);
6627 htab_delete (link_hash_table);
6630 /* Perform final processing on file-scope data. */
6632 void
6633 c_write_global_declarations(void)
6635 tree link;
6637 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6639 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6640 int len = list_length (globals);
6641 tree *vec = xmalloc (sizeof (tree) * len);
6642 int i;
6643 tree decl;
6645 /* Process the decls in the order they were written. */
6647 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6648 vec[i] = decl;
6650 wrapup_global_declarations (vec, len);
6652 check_global_declarations (vec, len);
6654 /* Clean up. */
6655 free (vec);
6659 /* Reset the parser's state in preparation for a new file. */
6661 void
6662 c_reset_state (void)
6664 tree link;
6665 tree file_scope_decl;
6667 /* Pop the global scope. */
6668 if (current_scope != global_scope)
6669 current_scope = global_scope;
6670 file_scope_decl = current_file_decl;
6671 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6672 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6673 truly_local_externals = NULL_TREE;
6675 /* Start a new global binding level. */
6676 pushlevel (0);
6677 global_scope = current_scope;
6678 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6679 TREE_CHAIN (current_file_decl) = file_scope_decl;
6681 /* Reintroduce the builtin declarations. */
6682 for (link = first_builtin_decl;
6683 link != TREE_CHAIN (last_builtin_decl);
6684 link = TREE_CHAIN (link))
6685 pushdecl (copy_node (link));
6688 #include "gt-c-decl.h"