PR c++/3478
[official-gcc.git] / gcc / c-decl.c
blob639ad221f2f95f60cab89f9ea78a86f1eaea4aa2
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, 2004 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 make_label (tree, location_t);
310 static void bind_label (tree, tree, struct c_scope *);
311 static void implicit_decl_warning (tree);
312 static tree lookup_tag (enum tree_code, tree, int);
313 static tree lookup_name_current_level (tree);
314 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
315 static tree grokparms (tree, int);
316 static void layout_array_type (tree);
317 static void store_parm_decls_newstyle (void);
318 static void store_parm_decls_oldstyle (void);
319 static tree c_make_fname_decl (tree, int);
320 static void c_expand_body_1 (tree, int);
321 static tree any_external_decl (tree);
322 static void record_external_decl (tree);
323 static void warn_if_shadowing (tree, tree);
324 static void check_bitfield_type_and_width (tree *, tree *, const char *);
325 static void clone_underlying_type (tree);
326 static bool flexible_array_type_p (tree);
327 static hashval_t link_hash_hash (const void *);
328 static int link_hash_eq (const void *, const void *);
330 /* States indicating how grokdeclarator() should handle declspecs marked
331 with __attribute__((deprecated)). An object declared as
332 __attribute__((deprecated)) suppresses warnings of uses of other
333 deprecated items. */
335 enum deprecated_states {
336 DEPRECATED_NORMAL,
337 DEPRECATED_SUPPRESS
340 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
342 void
343 c_print_identifier (FILE *file, tree node, int indent)
345 print_node (file, "symbol", IDENTIFIER_SYMBOL_VALUE (node), indent + 4);
346 print_node (file, "tag", IDENTIFIER_TAG_VALUE (node), indent + 4);
347 print_node (file, "label", IDENTIFIER_LABEL_VALUE (node), indent + 4);
348 if (C_IS_RESERVED_WORD (node))
350 tree rid = ridpointers[C_RID_CODE (node)];
351 indent_to (file, indent + 4);
352 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
353 (void *) rid, IDENTIFIER_POINTER (rid));
357 /* Hook called at end of compilation to assume 1 elt
358 for a file-scope tentative array defn that wasn't complete before. */
360 void
361 c_finish_incomplete_decl (tree decl)
363 if (TREE_CODE (decl) == VAR_DECL)
365 tree type = TREE_TYPE (decl);
366 if (type != error_mark_node
367 && TREE_CODE (type) == ARRAY_TYPE
368 && ! DECL_EXTERNAL (decl)
369 && TYPE_DOMAIN (type) == 0)
371 warning ("%Jarray '%D' assumed to have one element", decl, decl);
373 complete_array_type (type, NULL_TREE, 1);
375 layout_decl (decl, 0);
380 /* Reuse or create a struct for this scope. */
382 static struct c_scope *
383 make_scope (void)
385 struct c_scope *result;
386 if (scope_freelist)
388 result = scope_freelist;
389 scope_freelist = result->outer;
391 else
392 result = ggc_alloc_cleared (sizeof (struct c_scope));
394 return result;
397 /* Remove the topmost scope from the stack and add it to the
398 free list, updating current_function_scope if necessary. */
400 static void
401 pop_scope (void)
403 struct c_scope *scope = current_scope;
405 current_scope = scope->outer;
406 if (scope->function_body)
407 current_function_scope = scope->outer_function;
409 memset (scope, 0, sizeof (struct c_scope));
410 scope->outer = scope_freelist;
411 scope_freelist = scope;
414 /* The Objective-C front-end often needs to determine the current scope. */
416 void *
417 get_current_scope (void)
419 return current_scope;
422 /* The following function is used only by Objective-C. It needs to live here
423 because it accesses the innards of c_scope. */
425 void
426 objc_mark_locals_volatile (void *enclosing_blk)
428 struct c_scope *scope;
430 for (scope = current_scope;
431 scope && scope != enclosing_blk;
432 scope = scope->outer)
434 tree decl;
436 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
438 DECL_REGISTER (decl) = 0;
439 TREE_THIS_VOLATILE (decl) = 1;
441 /* Do not climb up past the current function. */
442 if (scope->function_body)
443 break;
447 /* Nonzero if we are currently in the global scope. */
450 global_bindings_p (void)
452 return current_scope == global_scope;
455 void
456 keep_next_level (void)
458 keep_next_level_flag = true;
461 /* Identify this scope as currently being filled with parameters. */
463 void
464 declare_parm_level (void)
466 current_scope->parm_flag = true;
469 /* Nonzero if currently making parm declarations. */
472 in_parm_level_p (void)
474 return current_scope->parm_flag;
477 /* Enter a new scope. The dummy parameter is for signature
478 compatibility with lang_hooks.decls.pushlevel. */
480 void
481 pushlevel (int dummy ATTRIBUTE_UNUSED)
483 if (next_is_function_body)
485 /* This is the transition from the parameters to the top level
486 of the function body. These are the same scope
487 (C99 6.2.1p4,6) so we do not push another scope structure.
488 next_is_function_body is set only by store_parm_decls, which
489 in turn is called when and only when we are about to
490 encounter the opening curly brace for the function body.
492 The outermost block of a function always gets a BLOCK node,
493 because the debugging output routines expect that each
494 function has at least one BLOCK. */
495 current_scope->parm_flag = false;
496 current_scope->function_body = true;
497 current_scope->keep = true;
498 current_scope->outer_function = current_function_scope;
499 current_function_scope = current_scope;
501 keep_next_level_flag = false;
502 next_is_function_body = false;
504 else
506 struct c_scope *scope = make_scope ();
508 scope->keep = keep_next_level_flag;
509 scope->outer = current_scope;
510 current_scope = scope;
511 keep_next_level_flag = false;
515 /* Exit a scope. Restore the state of the identifier-decl mappings
516 that were in effect when this scope was entered.
518 If KEEP is KEEP_YES (1), this scope had explicit declarations, so
519 create a BLOCK node to record its declarations and subblocks for
520 debugging output. If KEEP is KEEP_MAYBE, do so only if the names
521 or tags lists are nonempty.
523 The second parameter is ignored; it is present only for
524 signature compatibility with lang_hooks.decls.poplevel.
526 If FUNCTIONBODY is nonzero, this level is the body of a function,
527 even if current_scope->function_body is not set. This is used
528 by language-independent code that generates synthetic functions,
529 and cannot set current_scope->function_body.
531 FIXME: Eliminate the need for all arguments. */
533 tree
534 poplevel (int keep, int dummy ATTRIBUTE_UNUSED, int functionbody)
536 struct c_scope *scope = current_scope;
537 tree block;
538 tree decl;
539 tree p;
541 /* The following line does not use |= due to a bug in HP's C compiler. */
542 scope->function_body = scope->function_body | functionbody;
544 if (keep == KEEP_MAYBE)
545 keep = (scope->names || scope->tags);
547 keep |= scope->keep;
548 keep |= scope->function_body;
550 /* If appropriate, create a BLOCK to record the decls for the life
551 of this function. */
552 block = 0;
553 if (keep)
555 block = make_node (BLOCK);
556 BLOCK_VARS (block) = scope->names;
557 BLOCK_SUBBLOCKS (block) = scope->blocks;
558 TREE_USED (block) = 1;
561 /* In each subblock, record that this is its superior. */
562 for (p = scope->blocks; p; p = TREE_CHAIN (p))
563 BLOCK_SUPERCONTEXT (p) = block;
565 /* Clear out the variable bindings in this scope.
567 Propagate TREE_ADDRESSABLE from nested functions to their
568 containing functions.
570 Issue warnings for unused variables and labels, and errors for
571 undefined labels, if there are any. */
573 for (p = scope->names; p; p = TREE_CHAIN (p))
575 switch (TREE_CODE (p))
577 case LABEL_DECL:
578 if (TREE_USED (p) && !DECL_INITIAL (p))
580 error ("%Jlabel `%D' used but not defined", p, p);
581 DECL_INITIAL (p) = error_mark_node;
583 else if (!TREE_USED (p) && warn_unused_label)
585 if (DECL_INITIAL (p))
586 warning ("%Jlabel `%D' defined but not used", p, p);
587 else
588 warning ("%Jlabel `%D' declared but not defined", p, p);
591 IDENTIFIER_LABEL_VALUE (DECL_NAME (p)) = 0;
592 break;
594 case FUNCTION_DECL:
595 if (! TREE_ASM_WRITTEN (p)
596 && DECL_INITIAL (p) != 0
597 && TREE_ADDRESSABLE (p)
598 && DECL_ABSTRACT_ORIGIN (p) != 0
599 && DECL_ABSTRACT_ORIGIN (p) != p)
600 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
601 goto normal;
603 case VAR_DECL:
604 /* Keep this in sync with stmt.c:warn_about_unused_variables.
605 No warnings when the global scope is popped because the
606 global scope isn't popped for the last translation unit,
607 so the warnings are done in c_write_global_declaration. */
608 if (warn_unused_variable && scope != global_scope
609 && !TREE_USED (p)
610 && !DECL_IN_SYSTEM_HEADER (p)
611 && DECL_NAME (p)
612 && !DECL_ARTIFICIAL (p))
613 warning ("%Junused variable `%D'", p, p);
614 /* fall through */
616 default:
617 normal:
618 if (DECL_NAME (p))
620 if (DECL_EXTERNAL (p) && scope != global_scope)
621 /* External decls stay in the symbol-value slot but are
622 inaccessible. */
623 C_DECL_INVISIBLE (p) = 1;
624 else
625 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
627 break;
631 /* Clear out the parameter bindings in this scope, if any.
632 Unused-parameter warnings are handled by function.c. */
633 for (p = scope->parms; p; p = TREE_CHAIN (p))
634 if (DECL_NAME (p))
635 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (p)) = 0;
637 /* Clear out the tag-meanings declared in this scope.
639 Set the TYPE_CONTEXTs for all of the tagged types belonging to
640 this scope so that they point to the appropriate construct, i.e.
641 either to the current FUNCTION_DECL node, or else to the BLOCK
642 node we just constructed.
644 Note that for tagged types whose scope is just the formal
645 parameter list for some function type specification, we can't
646 properly set their TYPE_CONTEXTs here, because we don't have a
647 pointer to the appropriate FUNCTION_TYPE node readily available
648 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
649 type nodes get set in `grokdeclarator' as soon as we have created
650 the FUNCTION_TYPE node which will represent the "scope" for these
651 "parameter list local" tagged types. */
653 decl = scope->function_body ? current_function_decl : block;
654 for (p = scope->tags; p; p = TREE_CHAIN (p))
656 if (TREE_PURPOSE (p))
657 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = 0;
658 if (decl)
659 TYPE_CONTEXT (TREE_VALUE (p)) = decl;
662 /* Restore all name- and label-meanings from outer scopes that were
663 shadowed by this scope. */
664 for (p = scope->shadowed; p; p = TREE_CHAIN (p))
665 if (TREE_VALUE (p) && TREE_CODE (TREE_VALUE (p)) == LABEL_DECL)
666 IDENTIFIER_LABEL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
667 else
668 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
670 /* Restore all tag-meanings from outer scopes that were shadowed by
671 this scope. */
672 for (p = scope->shadowed_tags; p; p = TREE_CHAIN (p))
673 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (p)) = TREE_VALUE (p);
675 /* Dispose of the block that we just made inside some higher level. */
676 if (scope->function_body && current_function_decl)
677 DECL_INITIAL (current_function_decl) = block;
678 else if (scope->outer)
680 if (block)
681 SCOPE_LIST_APPEND (scope->outer, blocks, block);
682 /* If we did not make a block for the scope just exited, any
683 blocks made for inner scopes must be carried forward so they
684 will later become subblocks of something else. */
685 else if (scope->blocks)
686 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
689 /* Pop the current scope, and free the structure for reuse. */
690 pop_scope ();
692 return block;
695 /* Insert BLOCK at the end of the list of subblocks of the current
696 scope. This is used when a BIND_EXPR is expanded, to handle the
697 BLOCK node inside the BIND_EXPR. */
699 void
700 insert_block (tree block)
702 TREE_USED (block) = 1;
703 SCOPE_LIST_APPEND (current_scope, blocks, block);
706 /* Set the BLOCK node for the innermost scope (the one we are
707 currently in). The RTL expansion machinery requires us to provide
708 this hook, but it is not useful in function-at-a-time mode. */
710 void
711 set_block (tree block ATTRIBUTE_UNUSED)
715 /* Push a definition or a declaration of struct, union or enum tag "name".
716 "type" should be the type node.
717 We assume that the tag "name" is not already defined.
719 Note that the definition may really be just a forward reference.
720 In that case, the TYPE_SIZE will be zero. */
722 void
723 pushtag (tree name, tree type)
725 struct c_scope *b = current_scope;
727 /* Record the identifier as the type's name if it has none. */
728 if (name)
730 if (TYPE_NAME (type) == 0)
731 TYPE_NAME (type) = name;
733 if (IDENTIFIER_TAG_VALUE (name))
734 b->shadowed_tags = tree_cons (name, IDENTIFIER_TAG_VALUE (name),
735 b->shadowed_tags);
736 IDENTIFIER_TAG_VALUE (name) = type;
739 b->tags = tree_cons (name, type, b->tags);
741 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
742 tagged type we just added to the current scope. This fake
743 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
744 to output a representation of a tagged type, and it also gives
745 us a convenient place to record the "scope start" address for the
746 tagged type. */
748 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
750 /* An approximation for now, so we can tell this is a function-scope tag.
751 This will be updated in poplevel. */
752 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
755 /* Subroutine of compare_decls. Allow harmless mismatches in return
756 and argument types provided that the type modes match. This function
757 return a unified type given a suitable match, and 0 otherwise. */
759 static tree
760 match_builtin_function_types (tree newtype, tree oldtype)
762 tree newrettype, oldrettype;
763 tree newargs, oldargs;
764 tree trytype, tryargs;
766 /* Accept the return type of the new declaration if same modes. */
767 oldrettype = TREE_TYPE (oldtype);
768 newrettype = TREE_TYPE (newtype);
770 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
771 return 0;
773 oldargs = TYPE_ARG_TYPES (oldtype);
774 newargs = TYPE_ARG_TYPES (newtype);
775 tryargs = newargs;
777 while (oldargs || newargs)
779 if (! oldargs
780 || ! newargs
781 || ! TREE_VALUE (oldargs)
782 || ! TREE_VALUE (newargs)
783 || TYPE_MODE (TREE_VALUE (oldargs))
784 != TYPE_MODE (TREE_VALUE (newargs)))
785 return 0;
787 oldargs = TREE_CHAIN (oldargs);
788 newargs = TREE_CHAIN (newargs);
791 trytype = build_function_type (newrettype, tryargs);
792 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
795 /* Subroutine of diagnose_mismathed_decls. Check for function type
796 mismatch involving an empty arglist vs a nonempty one and give clearer
797 diagnostics. */
798 static void
799 diagnose_arglist_conflict (tree newdecl, tree olddecl,
800 tree newtype, tree oldtype)
802 tree t;
804 if (TREE_CODE (olddecl) != FUNCTION_DECL
805 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
806 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
808 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
809 return;
811 t = TYPE_ARG_TYPES (oldtype);
812 if (t == 0)
813 t = TYPE_ARG_TYPES (newtype);
814 for (; t; t = TREE_CHAIN (t))
816 tree type = TREE_VALUE (t);
818 if (TREE_CHAIN (t) == 0
819 && TYPE_MAIN_VARIANT (type) != void_type_node)
821 inform ("a parameter list with an ellipsis can't match"
822 "an empty parameter name list declaration");
823 break;
826 if (c_type_promotes_to (type) != type)
828 inform ("an argument type that has a default promotion can't match"
829 "an empty parameter name list declaration");
830 break;
835 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
836 old-style function definition, NEWDECL is a prototype declaration.
837 Diagnose inconsistencies in the argument list. Returns TRUE if
838 the prototype is compatible, FALSE if not. */
839 static bool
840 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
842 tree type, parm;
843 int nargs;
844 /* Prototype decl follows defn w/o prototype. */
846 for (parm = TYPE_ACTUAL_ARG_TYPES (oldtype),
847 type = TYPE_ARG_TYPES (newtype),
848 nargs = 1;
850 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type), nargs++)
852 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
853 && TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
855 /* End of list. */
856 warning ("%Jprototype for '%D' follows non-prototype definition",
857 newdecl, newdecl);
858 return true;
861 if (TYPE_MAIN_VARIANT (TREE_VALUE (parm)) == void_type_node
862 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
864 error ("%Jprototype for '%D' with different number of arguments "
865 "follows non-prototype definition", newdecl, newdecl);
866 return false;
868 /* Type for passing arg must be consistent
869 with that declared for the arg. */
870 if (! comptypes (TREE_VALUE (parm), TREE_VALUE (type),
871 COMPARE_STRICT))
873 error ("%Jprototype for '%D' with incompatible argument %d "
874 "follows non-prototype definition", newdecl, newdecl, nargs);
875 return false;
880 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
881 first in a pair of mismatched declarations, using the diagnostic
882 function DIAG. */
883 static void
884 locate_old_decl (tree decl, void (*diag)(const char *, ...))
886 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
888 else if (DECL_INITIAL (decl))
889 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
890 else if (C_DECL_IMPLICIT (decl))
891 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
892 else
893 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
896 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
897 Returns true if the caller should proceed to merge the two, false
898 if OLDDECL should simply be discarded. As a side effect, issues
899 all necessary diagnostics for invalid or poor-style combinations.
900 If it returns true, writes the types of NEWDECL and OLDDECL to
901 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
902 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
904 static bool
905 diagnose_mismatched_decls (tree newdecl, tree olddecl,
906 tree *newtypep, tree *oldtypep)
908 tree newtype, oldtype;
909 bool pedwarned = false;
910 bool warned = false;
912 /* If we have error_mark_node for either decl or type, just discard
913 the previous decl - we're in an error cascade already. */
914 if (olddecl == error_mark_node || newdecl == error_mark_node)
915 return false;
916 oldtype = TREE_TYPE (olddecl);
917 newtype = TREE_TYPE (newdecl);
918 if (oldtype == error_mark_node || newtype == error_mark_node)
919 return false;
921 /* Two different categories of symbol altogether. This is an error
922 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
923 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
925 if (TREE_CODE (olddecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
927 error ("%J'%D' redeclared as different kind of symbol",
928 newdecl, newdecl);
929 locate_old_decl (olddecl, error);
931 else if (TREE_PUBLIC (newdecl))
932 warning ("%Jbuilt-in function '%D' declared as non-function",
933 newdecl, newdecl);
934 else if (warn_shadow)
935 warning ("%Jshadowing built-in function '%D'",
936 newdecl, newdecl);
937 return false;
940 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
942 if (TREE_CODE (olddecl) == FUNCTION_DECL && DECL_BUILT_IN (olddecl))
944 /* Accept harmless mismatch in function types.
945 This is for the ffs and fprintf builtins. */
946 tree trytype = match_builtin_function_types (newtype, oldtype);
948 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
949 oldtype = trytype;
950 else
952 /* If types don't match for a built-in, throw away the
953 built-in. No point in calling locate_old_decl here, it
954 won't print anything. */
955 warning ("%Jconflicting types for built-in function '%D'",
956 newdecl, newdecl);
957 return false;
960 else if (TREE_CODE (olddecl) == FUNCTION_DECL
961 && DECL_SOURCE_LINE (olddecl) == 0)
963 /* A conflicting function declaration for a predeclared
964 function that isn't actually built in. Objective C uses
965 these. The new declaration silently overrides everything
966 but the volatility (i.e. noreturn) indication. See also
967 below. FIXME: Make Objective C use normal builtins. */
968 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
969 return false;
971 /* Permit void foo (...) to match int foo (...) if the latter is
972 the definition and implicit int was used. See
973 c-torture/compile/920625-2.c. */
974 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
975 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
976 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
977 && C_FUNCTION_IMPLICIT_INT (newdecl))
979 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
980 /* Make sure we keep void as the return type. */
981 TREE_TYPE (newdecl) = newtype = oldtype;
982 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
983 pedwarned = true;
985 else
987 error ("%Jconflicting types for '%D'", newdecl, newdecl);
988 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
989 locate_old_decl (olddecl, error);
990 return false;
994 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
995 but silently ignore the redeclaration if either is in a system
996 header. (Conflicting redeclarations were handled above.) */
997 if (TREE_CODE (newdecl) == TYPE_DECL)
999 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1000 return true; /* allow OLDDECL to continue in use */
1002 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1003 locate_old_decl (olddecl, error);
1004 return false;
1007 /* Function declarations can either be 'static' or 'extern' (no
1008 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1009 can never conflict with each other on account of linkage (6.2.2p4).
1010 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1011 two definitions if one is 'extern inline' and one is not. The non-
1012 extern-inline definition supersedes the extern-inline definition. */
1013 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1015 if (DECL_BUILT_IN (olddecl) && !TREE_PUBLIC (newdecl))
1017 /* If you declare a built-in function name as static, the
1018 built-in definition is overridden,
1019 but optionally warn this was a bad choice of name. */
1020 if (warn_shadow)
1021 warning ("%Jshadowing built-in function '%D'", newdecl, newdecl);
1022 /* Discard the old built-in function. */
1023 return false;
1026 if (DECL_INITIAL (newdecl))
1028 if (DECL_INITIAL (olddecl)
1029 && !(DECL_DECLARED_INLINE_P (olddecl)
1030 && DECL_EXTERNAL (olddecl)
1031 && !(DECL_DECLARED_INLINE_P (newdecl)
1032 && DECL_EXTERNAL (newdecl))))
1034 error ("%Jredefinition of '%D'", newdecl, newdecl);
1035 locate_old_decl (olddecl, error);
1036 return false;
1039 /* If we have a prototype after an old-style function definition,
1040 the argument types must be checked specially. */
1041 else if (DECL_INITIAL (olddecl)
1042 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1043 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1044 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1046 locate_old_decl (olddecl, error);
1047 return false;
1049 /* Mismatched non-static and static is considered poor style.
1050 We only diagnose static then non-static if -Wtraditional,
1051 because it is the most convenient way to get some effects
1052 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1053 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1054 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1056 /* A static function declaration for a predeclared function
1057 that isn't actually built in, silently overrides the
1058 default. Objective C uses these. See also above.
1059 FIXME: Make Objective C use normal builtins. */
1060 if (TREE_CODE (olddecl) == FUNCTION_DECL
1061 && DECL_SOURCE_LINE (olddecl) == 0)
1062 return false;
1063 else
1065 warning ("%Jstatic declaration of '%D' follows "
1066 "non-static declaration", newdecl, newdecl);
1067 warned = true;
1070 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1071 && warn_traditional)
1073 warning ("%Jnon-static declaration of '%D' follows "
1074 "static declaration", newdecl, newdecl);
1075 warned = true;
1078 else if (TREE_CODE (newdecl) == VAR_DECL)
1080 /* Only variables can be thread-local, and all declarations must
1081 agree on this property. */
1082 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1084 if (DECL_THREAD_LOCAL (newdecl))
1085 error ("%Jthread-local declaration of '%D' follows "
1086 "non-thread-local declaration", newdecl, newdecl);
1087 else
1088 error ("%Jnon-thread-local declaration of '%D' follows "
1089 "thread-local declaration", newdecl, newdecl);
1091 locate_old_decl (olddecl, error);
1092 return false;
1095 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1096 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1098 error ("%Jredefinition of '%D'", newdecl, newdecl);
1099 locate_old_decl (olddecl, error);
1100 return false;
1103 /* Objects declared at file scope: if at least one is 'extern',
1104 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1105 if (DECL_FILE_SCOPE_P (newdecl))
1107 if (!DECL_EXTERNAL (newdecl)
1108 && !DECL_EXTERNAL (olddecl)
1109 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1111 if (TREE_PUBLIC (newdecl))
1112 error ("%Jnon-static declaration of '%D' follows "
1113 "static declaration", newdecl, newdecl);
1114 else
1115 error ("%Jstatic declaration of '%D' follows "
1116 "non-static declaration", newdecl, newdecl);
1118 locate_old_decl (olddecl, error);
1119 return false;
1122 /* Two objects with the same name declared at the same block
1123 scope must both be external references (6.7p3). */
1124 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1125 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1127 if (DECL_EXTERNAL (newdecl))
1128 error ("%Jextern declaration of '%D' follows "
1129 "declaration with no linkage", newdecl, newdecl);
1130 else if (DECL_EXTERNAL (olddecl))
1131 error ("%Jdeclaration of '%D' with no linkage follows "
1132 "extern declaration", newdecl, newdecl);
1133 else
1134 error ("%Jredeclaration of '%D' with no linkage",
1135 newdecl, newdecl);
1137 locate_old_decl (olddecl, error);
1138 return false;
1142 /* warnings */
1143 /* All decls must agree on a non-default visibility. */
1144 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1145 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1146 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1148 warning ("%Jredeclaration of '%D' with different visibility "
1149 "(old visibility preserved)", newdecl, newdecl);
1150 warned = true;
1153 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1155 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1156 if (DECL_DECLARED_INLINE_P (newdecl)
1157 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1159 warning ("%Jinline declaration of '%D' follows "
1160 "declaration with attribute noinline", newdecl, newdecl);
1161 warned = true;
1163 else if (DECL_DECLARED_INLINE_P (olddecl)
1164 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1166 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1167 "inline declaration ", newdecl, newdecl);
1168 warned = true;
1171 /* Inline declaration after use or definition.
1172 ??? Should we still warn about this now we have unit-at-a-time
1173 mode and can get it right? */
1174 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1176 if (TREE_USED (olddecl))
1178 warning ("%J'%D' declared inline after being called");
1179 warned = true;
1181 else if (DECL_INITIAL (olddecl))
1183 warning ("%J'%D' declared inline after its definition");
1184 warned = true;
1188 else /* VAR_DECL */
1190 /* These bits are only type qualifiers when applied to objects. */
1191 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1193 if (TREE_THIS_VOLATILE (newdecl))
1194 pedwarn ("%Jvolatile declaration of '%D' follows "
1195 "non-volatile declaration", newdecl, newdecl);
1196 else
1197 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1198 "volatile declaration", newdecl, newdecl);
1199 pedwarned = true;
1201 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1203 if (TREE_READONLY (newdecl))
1204 pedwarn ("%Jconst declaration of '%D' follows "
1205 "non-const declaration", newdecl, newdecl);
1206 else
1207 pedwarn ("%Jnon-const declaration of '%D' follows "
1208 "const declaration", newdecl, newdecl);
1209 pedwarned = true;
1213 /* Optional warning for completely redundant decls. */
1214 if (!warned && !pedwarned
1215 && warn_redundant_decls
1216 /* Don't warn about a function declaration followed by a
1217 definition. */
1218 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1219 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1220 /* Don't warn about an extern followed by a definition. */
1221 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl)))
1223 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1224 warned = true;
1227 /* Report location of previous decl/defn in a consistent manner. */
1228 if (warned || pedwarned)
1229 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1231 *newtypep = newtype;
1232 *oldtypep = oldtype;
1233 return true;
1236 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1237 consistent with OLDDECL, but carries new information. Merge the
1238 new information into OLDDECL. If DIFFERENT_BINDING_LEVEL or
1239 DIFFERENT_TU is true, avoid completely merging the decls, as this
1240 will break assumptions elsewhere. This function issues no
1241 diagnostics. */
1243 static void
1244 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype,
1245 bool different_binding_level, bool different_tu)
1247 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1248 && DECL_INITIAL (newdecl) != 0);
1250 /* When copying info to olddecl, we store into write_olddecl
1251 instead. This allows us to avoid modifying olddecl when
1252 different_binding_level is true. */
1253 tree write_olddecl = different_binding_level ? newdecl : olddecl;
1255 /* For real parm decl following a forward decl, return 1 so old decl
1256 will be reused. Only allow this to happen once. */
1257 if (TREE_CODE (newdecl) == PARM_DECL
1258 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1260 TREE_ASM_WRITTEN (olddecl) = 0;
1261 return;
1264 DECL_ATTRIBUTES (newdecl)
1265 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1267 /* Merge the data types specified in the two decls. */
1268 if (TREE_CODE (newdecl) != FUNCTION_DECL || !DECL_BUILT_IN (olddecl))
1270 if (different_binding_level)
1272 if (TYPE_ARG_TYPES (oldtype) != 0
1273 && TYPE_ARG_TYPES (newtype) == 0)
1274 TREE_TYPE (newdecl) = common_type (newtype, oldtype);
1275 else
1276 TREE_TYPE (newdecl)
1277 = build_type_attribute_variant
1278 (newtype,
1279 merge_attributes (TYPE_ATTRIBUTES (newtype),
1280 TYPE_ATTRIBUTES (oldtype)));
1282 else
1283 TREE_TYPE (newdecl)
1284 = TREE_TYPE (olddecl)
1285 = common_type (newtype, oldtype);
1288 /* Lay the type out, unless already done. */
1289 if (oldtype != TREE_TYPE (newdecl))
1291 if (TREE_TYPE (newdecl) != error_mark_node)
1292 layout_type (TREE_TYPE (newdecl));
1293 if (TREE_CODE (newdecl) != FUNCTION_DECL
1294 && TREE_CODE (newdecl) != TYPE_DECL
1295 && TREE_CODE (newdecl) != CONST_DECL)
1296 layout_decl (newdecl, 0);
1298 else
1300 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1301 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1302 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1303 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1304 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1305 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1307 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1308 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1312 /* Keep the old rtl since we can safely use it. */
1313 COPY_DECL_RTL (olddecl, newdecl);
1315 /* Merge the type qualifiers. */
1316 if (TREE_READONLY (newdecl))
1317 TREE_READONLY (write_olddecl) = 1;
1319 if (TREE_THIS_VOLATILE (newdecl))
1321 TREE_THIS_VOLATILE (write_olddecl) = 1;
1322 if (TREE_CODE (newdecl) == VAR_DECL)
1323 make_var_volatile (newdecl);
1326 /* Keep source location of definition rather than declaration. */
1327 /* When called with different_binding_level set, keep the old
1328 information so that meaningful diagnostics can be given. */
1329 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0
1330 && ! different_binding_level)
1331 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1333 /* Merge the unused-warning information. */
1334 if (DECL_IN_SYSTEM_HEADER (olddecl))
1335 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1336 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1337 DECL_IN_SYSTEM_HEADER (write_olddecl) = 1;
1339 /* Merge the initialization information. */
1340 /* When called with different_binding_level set, don't copy over
1341 DECL_INITIAL, so that we don't accidentally change function
1342 declarations into function definitions. */
1343 if (DECL_INITIAL (newdecl) == 0 && ! different_binding_level)
1344 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1346 /* Merge the section attribute.
1347 We want to issue an error if the sections conflict but that must be
1348 done later in decl_attributes since we are called before attributes
1349 are assigned. */
1350 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1351 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1353 /* Copy the assembler name.
1354 Currently, it can only be defined in the prototype. */
1355 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1357 /* If either declaration has a nondefault visibility, use it. */
1358 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1359 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1361 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1363 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1364 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1365 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1366 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1367 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1368 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1369 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1370 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1371 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
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 && cgraph_function_possibly_inlined_p (olddecl))
1431 (*debug_hooks->outlining_inline_function) (olddecl);
1433 /* The new defn must not be inline. */
1434 DECL_INLINE (newdecl) = 0;
1435 DECL_UNINLINABLE (newdecl) = 1;
1437 else
1439 /* If either decl says `inline', this fn is inline,
1440 unless its definition was passed already. */
1441 if (DECL_DECLARED_INLINE_P (newdecl)
1442 || DECL_DECLARED_INLINE_P (olddecl))
1443 DECL_DECLARED_INLINE_P (newdecl) = 1;
1445 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1446 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1449 if (DECL_BUILT_IN (olddecl))
1451 /* Get rid of any built-in function if we have a function
1452 definition. */
1453 if (new_is_definition)
1455 if (! different_binding_level)
1457 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1458 DECL_BUILT_IN_CLASS (olddecl) = NOT_BUILT_IN;
1461 else
1463 /* If redeclaring a builtin function, and not a definition,
1464 it stays built in. */
1465 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1466 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1470 /* Also preserve various other info from the definition. */
1471 if (! new_is_definition)
1473 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1474 /* When called with different_binding_level set, don't copy over
1475 DECL_INITIAL, so that we don't accidentally change function
1476 declarations into function definitions. */
1477 if (! different_binding_level)
1478 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1479 DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl);
1480 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1481 DECL_ESTIMATED_INSNS (newdecl) = DECL_ESTIMATED_INSNS (olddecl);
1482 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1484 /* Set DECL_INLINE on the declaration if we've got a body
1485 from which to instantiate. */
1486 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1488 DECL_INLINE (newdecl) = 1;
1489 DECL_ABSTRACT_ORIGIN (newdecl)
1490 = (different_binding_level
1491 ? DECL_ORIGIN (olddecl)
1492 : DECL_ABSTRACT_ORIGIN (olddecl));
1495 else
1497 /* If a previous declaration said inline, mark the
1498 definition as inlinable. */
1499 if (DECL_DECLARED_INLINE_P (newdecl)
1500 && ! DECL_UNINLINABLE (newdecl))
1501 DECL_INLINE (newdecl) = 1;
1504 if (different_binding_level)
1505 return;
1507 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1508 But preserve OLDDECL's DECL_UID. */
1510 unsigned olddecl_uid = DECL_UID (olddecl);
1512 memcpy ((char *) olddecl + sizeof (struct tree_common),
1513 (char *) newdecl + sizeof (struct tree_common),
1514 sizeof (struct tree_decl) - sizeof (struct tree_common));
1515 DECL_UID (olddecl) = olddecl_uid;
1518 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1519 so that encode_section_info has a chance to look at the new decl
1520 flags and attributes. */
1521 if (DECL_RTL_SET_P (olddecl)
1522 && (TREE_CODE (olddecl) == FUNCTION_DECL
1523 || (TREE_CODE (olddecl) == VAR_DECL
1524 && TREE_STATIC (olddecl))))
1525 make_decl_rtl (olddecl, NULL);
1528 /* Handle when a new declaration NEWDECL has the same name as an old
1529 one OLDDECL in the same binding contour. Prints an error message
1530 if appropriate.
1532 If safely possible, alter OLDDECL to look like NEWDECL, and return
1533 true. Otherwise, return false.
1535 When DIFFERENT_BINDING_LEVEL is true, NEWDECL is an external
1536 declaration, and OLDDECL is in an outer scope and should thus not
1537 be changed. */
1539 static bool
1540 duplicate_decls (tree newdecl, tree olddecl,
1541 bool different_binding_level, bool different_tu)
1543 tree newtype, oldtype;
1545 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1546 return false;
1548 merge_decls (newdecl, olddecl, newtype, oldtype,
1549 different_binding_level, different_tu);
1550 return !different_binding_level;
1554 /* Return any external DECL associated with ID, whether or not it is
1555 currently in scope. */
1557 static tree
1558 any_external_decl (tree id)
1560 tree decl = IDENTIFIER_SYMBOL_VALUE (id);
1561 tree t;
1563 if (decl == 0 || TREE_CODE (decl) == ERROR_MARK)
1564 return 0;
1565 else if (TREE_CODE (decl) != TYPE_DECL && DECL_EXTERNAL (decl))
1566 return decl;
1568 t = purpose_member (id, truly_local_externals);
1569 if (t)
1570 return TREE_VALUE (t);
1572 return 0;
1575 /* Record an external decl DECL. This only does something if a
1576 shadowing decl already exists. */
1577 static void
1578 record_external_decl (tree decl)
1580 tree name = DECL_NAME (decl);
1581 if (!IDENTIFIER_SYMBOL_VALUE (name))
1582 return;
1584 truly_local_externals = tree_cons (name, decl, truly_local_externals);
1587 /* Check whether decl-node X shadows an existing declaration.
1588 OLD is the old IDENTIFIER_SYMBOL_VALUE of the DECL_NAME of X,
1589 which might be a NULL_TREE. */
1590 static void
1591 warn_if_shadowing (tree x, tree old)
1593 const char *name;
1595 /* Nothing to shadow? */
1596 if (old == 0
1597 /* Shadow warnings not wanted? */
1598 || !warn_shadow
1599 /* No shadow warnings for internally generated vars. */
1600 || DECL_SOURCE_LINE (x) == 0
1601 /* No shadow warnings for vars made for inlining. */
1602 || DECL_FROM_INLINE (x)
1603 /* Don't warn about the parm names in function declarator
1604 within a function declarator.
1605 It would be nice to avoid warning in any function
1606 declarator in a declaration, as opposed to a definition,
1607 but there is no way to tell it's not a definition. */
1608 || (TREE_CODE (x) == PARM_DECL && current_scope->outer->parm_flag))
1609 return;
1611 name = IDENTIFIER_POINTER (DECL_NAME (x));
1612 if (TREE_CODE (old) == PARM_DECL)
1613 shadow_warning (SW_PARAM, name, old);
1614 else if (DECL_FILE_SCOPE_P (old))
1615 shadow_warning (SW_GLOBAL, name, old);
1616 else
1617 shadow_warning (SW_LOCAL, name, old);
1621 /* Subroutine of pushdecl.
1623 X is a TYPE_DECL for a typedef statement. Create a brand new
1624 ..._TYPE node (which will be just a variant of the existing
1625 ..._TYPE node with identical properties) and then install X
1626 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1628 The whole point here is to end up with a situation where each
1629 and every ..._TYPE node the compiler creates will be uniquely
1630 associated with AT MOST one node representing a typedef name.
1631 This way, even though the compiler substitutes corresponding
1632 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1633 early on, later parts of the compiler can always do the reverse
1634 translation and get back the corresponding typedef name. For
1635 example, given:
1637 typedef struct S MY_TYPE;
1638 MY_TYPE object;
1640 Later parts of the compiler might only know that `object' was of
1641 type `struct S' if it were not for code just below. With this
1642 code however, later parts of the compiler see something like:
1644 struct S' == struct S
1645 typedef struct S' MY_TYPE;
1646 struct S' object;
1648 And they can then deduce (from the node for type struct S') that
1649 the original object declaration was:
1651 MY_TYPE object;
1653 Being able to do this is important for proper support of protoize,
1654 and also for generating precise symbolic debugging information
1655 which takes full account of the programmer's (typedef) vocabulary.
1657 Obviously, we don't want to generate a duplicate ..._TYPE node if
1658 the TYPE_DECL node that we are now processing really represents a
1659 standard built-in type.
1661 Since all standard types are effectively declared at line zero
1662 in the source file, we can easily check to see if we are working
1663 on a standard type by checking the current value of lineno. */
1665 static void
1666 clone_underlying_type (tree x)
1668 if (DECL_SOURCE_LINE (x) == 0)
1670 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1671 TYPE_NAME (TREE_TYPE (x)) = x;
1673 else if (TREE_TYPE (x) != error_mark_node
1674 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1676 tree tt = TREE_TYPE (x);
1677 DECL_ORIGINAL_TYPE (x) = tt;
1678 tt = build_type_copy (tt);
1679 TYPE_NAME (tt) = x;
1680 TREE_USED (tt) = TREE_USED (x);
1681 TREE_TYPE (x) = tt;
1685 /* Record a decl-node X as belonging to the current lexical scope.
1686 Check for errors (such as an incompatible declaration for the same
1687 name already seen in the same scope).
1689 Returns either X or an old decl for the same name.
1690 If an old decl is returned, it may have been smashed
1691 to agree with what X says. */
1693 tree
1694 pushdecl (tree x)
1696 tree name = DECL_NAME (x);
1697 struct c_scope *scope = current_scope;
1699 #ifdef ENABLE_CHECKING
1700 if (error_mark_node == 0)
1701 /* Called too early. */
1702 abort ();
1703 #endif
1705 /* Functions need the lang_decl data. */
1706 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1707 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1709 /* A local extern declaration for a function doesn't constitute nesting.
1710 A local auto declaration does, since it's a forward decl
1711 for a nested function coming later. */
1712 if (current_function_decl == NULL
1713 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1714 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1715 DECL_CONTEXT (x) = current_file_decl;
1716 else
1717 DECL_CONTEXT (x) = current_function_decl;
1719 if (name)
1721 tree old;
1723 if (warn_nested_externs
1724 && scope != global_scope
1725 && DECL_EXTERNAL (x)
1726 && !DECL_IN_SYSTEM_HEADER (x))
1727 warning ("nested extern declaration of `%s'",
1728 IDENTIFIER_POINTER (name));
1730 old = lookup_name_current_level (name);
1731 if (old && duplicate_decls (x, old, 0, false))
1733 /* For PARM_DECLs, old may be a forward declaration.
1734 If so, we want to remove it from its old location
1735 (in the variables chain) and rechain it in the
1736 location given by the new declaration. */
1737 if (TREE_CODE (x) == PARM_DECL)
1739 tree *p;
1740 for (p = &scope->names; *p; p = &TREE_CHAIN (*p))
1741 if (*p == old)
1743 *p = TREE_CHAIN (old);
1744 SCOPE_LIST_APPEND (scope, parms, old);
1745 break;
1748 return old;
1750 if (DECL_EXTERNAL (x) || scope == global_scope)
1752 /* Find and check against a previous, not-in-scope, external
1753 decl for this identifier. (C99 6.2.7p2: All declarations
1754 that refer to the same object or function shall have
1755 compatible type; otherwise, the behavior is undefined.) */
1756 tree ext = any_external_decl (name);
1757 if (ext)
1759 if (duplicate_decls (x, ext, scope != global_scope,
1760 false))
1761 x = copy_node (ext);
1763 else
1764 record_external_decl (x);
1767 if (TREE_CODE (x) == TYPE_DECL)
1768 clone_underlying_type (x);
1770 /* If storing a local value, there may already be one
1771 (inherited). If so, record it for restoration when this
1772 scope ends. Take care not to do this if we are replacing an
1773 older decl in the same scope (i.e. duplicate_decls returned
1774 false, above). */
1775 if (scope != global_scope
1776 && IDENTIFIER_SYMBOL_VALUE (name)
1777 && IDENTIFIER_SYMBOL_VALUE (name) != old)
1779 warn_if_shadowing (x, IDENTIFIER_SYMBOL_VALUE (name));
1780 scope->shadowed = tree_cons (name, IDENTIFIER_SYMBOL_VALUE (name),
1781 scope->shadowed);
1784 /* Install the new declaration in the requested scope. */
1785 IDENTIFIER_SYMBOL_VALUE (name) = x;
1786 C_DECL_INVISIBLE (x) = 0;
1788 /* If x's type is incomplete because it's based on a
1789 structure or union which has not yet been fully declared,
1790 attach it to that structure or union type, so we can go
1791 back and complete the variable declaration later, if the
1792 structure or union gets fully declared.
1794 If the input is erroneous, we can have error_mark in the type
1795 slot (e.g. "f(void a, ...)") - that doesn't count as an
1796 incomplete type. */
1797 if (TREE_TYPE (x) != error_mark_node
1798 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1800 tree element = TREE_TYPE (x);
1802 while (TREE_CODE (element) == ARRAY_TYPE)
1803 element = TREE_TYPE (element);
1804 element = TYPE_MAIN_VARIANT (element);
1806 if ((TREE_CODE (element) == RECORD_TYPE
1807 || TREE_CODE (element) == UNION_TYPE)
1808 && (TREE_CODE (x) != TYPE_DECL
1809 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1810 && !COMPLETE_TYPE_P (element))
1811 C_TYPE_INCOMPLETE_VARS (element)
1812 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1816 if (TREE_CODE (x) == PARM_DECL)
1817 SCOPE_LIST_APPEND (scope, parms, x);
1818 else
1819 SCOPE_LIST_APPEND (scope, names, x);
1821 return x;
1824 /* Record X as belonging to the global scope (C99 "file scope").
1825 This is used only internally by the Objective-C front end,
1826 and is limited to its needs. duplicate_decls is not called;
1827 if there is any preexisting decl for this identifier, it is an ICE. */
1829 tree
1830 pushdecl_top_level (tree x)
1832 tree name;
1834 if (TREE_CODE (x) != VAR_DECL)
1835 abort ();
1837 name = DECL_NAME (x);
1839 if (IDENTIFIER_SYMBOL_VALUE (name))
1840 abort ();
1842 DECL_CONTEXT (x) = current_file_decl;
1843 IDENTIFIER_SYMBOL_VALUE (name) = x;
1845 SCOPE_LIST_APPEND (global_scope, names, x);
1846 return x;
1849 /* Generate an implicit declaration for identifier FUNCTIONID as a
1850 function of type int (). */
1852 tree
1853 implicitly_declare (tree functionid)
1855 tree decl = any_external_decl (functionid);
1857 if (decl)
1859 /* Implicit declaration of a function already declared
1860 (somehow) in a different scope, or as a built-in.
1861 If this is the first time this has happened, warn;
1862 then recycle the old declaration. */
1863 if (!C_DECL_IMPLICIT (decl))
1865 implicit_decl_warning (DECL_NAME (decl));
1866 if (! DECL_FILE_SCOPE_P (decl))
1867 warning ("%Jprevious declaration of '%D'", decl, decl);
1868 C_DECL_IMPLICIT (decl) = 1;
1870 /* If this function is global, then it must already be in the
1871 global scope, so there's no need to push it again. */
1872 if (current_scope == global_scope)
1873 return decl;
1874 /* If this is a local declaration, make a copy; we can't have
1875 the same DECL listed in two different scopes. */
1876 return pushdecl (copy_node (decl));
1879 /* Not seen before. */
1880 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1881 DECL_EXTERNAL (decl) = 1;
1882 TREE_PUBLIC (decl) = 1;
1883 C_DECL_IMPLICIT (decl) = 1;
1884 implicit_decl_warning (functionid);
1886 /* C89 says implicit declarations are in the innermost block.
1887 So we record the decl in the standard fashion. */
1888 decl = pushdecl (decl);
1890 /* No need to call objc_check_decl here - it's a function type. */
1891 rest_of_decl_compilation (decl, NULL, 0, 0);
1893 /* Write a record describing this implicit function declaration
1894 to the prototypes file (if requested). */
1895 gen_aux_info_record (decl, 0, 1, 0);
1897 /* Possibly apply some default attributes to this implicit declaration. */
1898 decl_attributes (&decl, NULL_TREE, 0);
1900 return decl;
1903 static void
1904 implicit_decl_warning (tree id)
1906 const char *name = IDENTIFIER_POINTER (id);
1907 if (mesg_implicit_function_declaration == 2)
1908 error ("implicit declaration of function `%s'", name);
1909 else if (mesg_implicit_function_declaration == 1)
1910 warning ("implicit declaration of function `%s'", name);
1913 /* Issue an error message for a reference to an undeclared variable
1914 ID, including a reference to a builtin outside of function-call
1915 context. Establish a binding of the identifier to error_mark_node
1916 in an appropriate scope, which will suppress further errors for the
1917 same identifier. */
1918 void
1919 undeclared_variable (tree id)
1921 static bool already = false;
1922 struct c_scope *scope;
1924 if (current_function_decl == 0)
1926 error ("`%s' undeclared here (not in a function)",
1927 IDENTIFIER_POINTER (id));
1928 scope = current_scope;
1930 else
1932 error ("`%s' undeclared (first use in this function)",
1933 IDENTIFIER_POINTER (id));
1935 if (! already)
1937 error ("(Each undeclared identifier is reported only once");
1938 error ("for each function it appears in.)");
1939 already = true;
1942 scope = current_function_scope;
1945 scope->shadowed = tree_cons (id, IDENTIFIER_SYMBOL_VALUE (id),
1946 scope->shadowed);
1947 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1950 /* Subroutine of lookup_label, declare_label, define_label: construct a
1951 LABEL_DECL with all the proper frills. */
1953 static tree
1954 make_label (tree name, location_t location)
1956 tree label = build_decl (LABEL_DECL, name, void_type_node);
1958 DECL_CONTEXT (label) = current_function_decl;
1959 DECL_MODE (label) = VOIDmode;
1960 DECL_SOURCE_LOCATION (label) = location;
1962 return label;
1965 /* Another subroutine of lookup_label, declare_label, define_label:
1966 set up the binding of name to LABEL_DECL in the given SCOPE. */
1968 static void
1969 bind_label (tree name, tree label, struct c_scope *scope)
1971 if (IDENTIFIER_LABEL_VALUE (name))
1972 scope->shadowed = tree_cons (name, IDENTIFIER_LABEL_VALUE (name),
1973 scope->shadowed);
1974 IDENTIFIER_LABEL_VALUE (name) = label;
1976 SCOPE_LIST_APPEND (scope, names, label);
1979 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
1980 Create one if none exists so far for the current function.
1981 This is called when a label is used in a goto expression or
1982 has its address taken. */
1984 tree
1985 lookup_label (tree name)
1987 tree label;
1989 if (current_function_decl == 0)
1991 error ("label %s referenced outside of any function",
1992 IDENTIFIER_POINTER (name));
1993 return 0;
1996 /* Use a label already defined or ref'd with this name, but not if
1997 it is inherited from a containing function and wasn't declared
1998 using __label__. */
1999 label = IDENTIFIER_LABEL_VALUE (name);
2000 if (label && (DECL_CONTEXT (label) == current_function_decl
2001 || C_DECLARED_LABEL_FLAG (label)))
2003 /* If the label has only been declared, update its apparent
2004 location to point here, for better diagnostics if it
2005 turns out not to have been defined. */
2006 if (!TREE_USED (label))
2007 DECL_SOURCE_LOCATION (label) = input_location;
2008 return label;
2011 /* No label binding for that identifier; make one. */
2012 label = make_label (name, input_location);
2014 /* Ordinary labels go in the current function scope. */
2015 bind_label (name, label, current_function_scope);
2016 return label;
2019 /* Make a label named NAME in the current function, shadowing silently
2020 any that may be inherited from containing functions or containing
2021 scopes. This is called for __label__ declarations. */
2023 /* Note that valid use, if the label being shadowed comes from another
2024 scope in the same function, requires calling declare_nonlocal_label
2025 right away. (Is this still true? -zw 2003-07-17) */
2027 tree
2028 declare_label (tree name)
2030 tree label = IDENTIFIER_LABEL_VALUE (name);
2031 tree dup;
2033 /* Check to make sure that the label hasn't already been declared
2034 at this scope */
2035 for (dup = current_scope->names; dup; dup = TREE_CHAIN (dup))
2036 if (dup == label)
2038 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2039 error ("%Jthis is a previous declaration", dup);
2041 /* Just use the previous declaration. */
2042 return dup;
2045 label = make_label (name, input_location);
2046 C_DECLARED_LABEL_FLAG (label) = 1;
2048 /* Declared labels go in the current scope. */
2049 bind_label (name, label, current_scope);
2050 return label;
2053 /* Define a label, specifying the location in the source file.
2054 Return the LABEL_DECL node for the label, if the definition is valid.
2055 Otherwise return 0. */
2057 tree
2058 define_label (location_t location, tree name)
2060 tree label;
2062 /* Find any preexisting label with this name. It is an error
2063 if that label has already been defined in this function, or
2064 if there is a containing function with a declared label with
2065 the same name. */
2066 label = IDENTIFIER_LABEL_VALUE (name);
2068 if (label
2069 && ((DECL_CONTEXT (label) == current_function_decl
2070 && DECL_INITIAL (label) != 0)
2071 || (DECL_CONTEXT (label) != current_function_decl
2072 && C_DECLARED_LABEL_FLAG (label))))
2074 error ("%Hduplicate label `%D'", &location, label);
2075 if (DECL_INITIAL (label))
2076 error ("%J`%D' previously defined here", label, label);
2077 else
2078 error ("%J`%D' previously declared here", label, label);
2079 return 0;
2081 else if (label && DECL_CONTEXT (label) == current_function_decl)
2083 /* The label has been used or declared already in this function,
2084 but not defined. Update its location to point to this
2085 definition. */
2086 DECL_SOURCE_LOCATION (label) = location;
2088 else
2090 /* No label binding for that identifier; make one. */
2091 label = make_label (name, location);
2093 /* Ordinary labels go in the current function scope. */
2094 bind_label (name, label, current_function_scope);
2097 if (warn_traditional && !in_system_header && lookup_name (name))
2098 warning ("%Htraditional C lacks a separate namespace for labels, "
2099 "identifier `%s' conflicts", &location,
2100 IDENTIFIER_POINTER (name));
2102 /* Mark label as having been defined. */
2103 DECL_INITIAL (label) = error_mark_node;
2104 return label;
2107 /* Return the list of declarations of the current scope. */
2109 tree
2110 getdecls (void)
2112 return current_scope->names;
2116 /* Given NAME, an IDENTIFIER_NODE,
2117 return the structure (or union or enum) definition for that name.
2118 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2119 CODE says which kind of type the caller wants;
2120 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2121 If the wrong kind of type is found, an error is reported. */
2123 static tree
2124 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2126 tree tag = IDENTIFIER_TAG_VALUE (name);
2127 int thislevel = 0;
2129 if (!tag)
2130 return 0;
2132 /* We only care about whether it's in this level if
2133 thislevel_only was set or it might be a type clash. */
2134 if (thislevel_only || TREE_CODE (tag) != code)
2136 if (current_scope == global_scope
2137 || purpose_member (name, current_scope->tags))
2138 thislevel = 1;
2141 if (thislevel_only && !thislevel)
2142 return 0;
2144 if (TREE_CODE (tag) != code)
2146 /* Definition isn't the kind we were looking for. */
2147 pending_invalid_xref = name;
2148 pending_invalid_xref_location = input_location;
2150 /* If in the same binding level as a declaration as a tag
2151 of a different type, this must not be allowed to
2152 shadow that tag, so give the error immediately.
2153 (For example, "struct foo; union foo;" is invalid.) */
2154 if (thislevel)
2155 pending_xref_error ();
2157 return tag;
2160 /* Print an error message now
2161 for a recent invalid struct, union or enum cross reference.
2162 We don't print them immediately because they are not invalid
2163 when used in the `struct foo;' construct for shadowing. */
2165 void
2166 pending_xref_error (void)
2168 if (pending_invalid_xref != 0)
2169 error ("%H`%s' defined as wrong kind of tag",
2170 &pending_invalid_xref_location,
2171 IDENTIFIER_POINTER (pending_invalid_xref));
2172 pending_invalid_xref = 0;
2176 /* Look up NAME in the current scope and its superiors
2177 in the namespace of variables, functions and typedefs.
2178 Return a ..._DECL node of some kind representing its definition,
2179 or return 0 if it is undefined. */
2181 tree
2182 lookup_name (tree name)
2184 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2185 if (decl == 0 || decl == error_mark_node)
2186 return decl;
2187 if (C_DECL_INVISIBLE (decl))
2188 return 0;
2189 return decl;
2192 /* Similar to `lookup_name' but look only at the current scope. */
2194 static tree
2195 lookup_name_current_level (tree name)
2197 tree decl = IDENTIFIER_SYMBOL_VALUE (name);
2199 if (decl == 0 || decl == error_mark_node || C_DECL_INVISIBLE (decl))
2200 return 0;
2202 if (current_scope == global_scope)
2203 return decl;
2205 /* Scan the current scope for a decl with name NAME.
2206 For PARM_DECLs, we have to look at both ->parms and ->names, since
2207 forward parameter declarations wind up on the ->names list. */
2208 if (TREE_CODE (decl) == PARM_DECL
2209 && chain_member (decl, current_scope->parms))
2210 return decl;
2211 if (chain_member (decl, current_scope->names))
2212 return decl;
2214 return 0;
2217 /* Create the predefined scalar types of C,
2218 and some nodes representing standard constants (0, 1, (void *) 0).
2219 Initialize the global scope.
2220 Make definitions for built-in primitive functions. */
2222 void
2223 c_init_decl_processing (void)
2225 tree endlink;
2226 tree ptr_ftype_void, ptr_ftype_ptr;
2227 location_t save_loc = input_location;
2229 /* Adds some ggc roots, and reserved words for c-parse.in. */
2230 c_parse_init ();
2232 current_function_decl = 0;
2234 /* Make the c_scope structure for global names. */
2235 pushlevel (0);
2236 global_scope = current_scope;
2238 /* Declarations from c_common_nodes_and_builtins must not be associated
2239 with this input file, lest we get differences between using and not
2240 using preprocessed headers. */
2241 input_location.file = "<internal>";
2242 input_location.line = 0;
2244 /* Make the DECL for the toplevel file scope. */
2245 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
2247 build_common_tree_nodes (flag_signed_char);
2249 c_common_nodes_and_builtins ();
2251 /* In C, comparisons and TRUTH_* expressions have type int. */
2252 truthvalue_type_node = integer_type_node;
2253 truthvalue_true_node = integer_one_node;
2254 truthvalue_false_node = integer_zero_node;
2256 /* Even in C99, which has a real boolean type. */
2257 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2258 boolean_type_node));
2260 endlink = void_list_node;
2261 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2262 ptr_ftype_ptr
2263 = build_function_type (ptr_type_node,
2264 tree_cons (NULL_TREE, ptr_type_node, endlink));
2266 input_location = save_loc;
2268 pedantic_lvalues = pedantic;
2270 make_fname_decl = c_make_fname_decl;
2271 start_fname_decls ();
2273 first_builtin_decl = global_scope->names;
2274 last_builtin_decl = global_scope->names_last;
2277 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2278 decl, NAME is the initialization string and TYPE_DEP indicates whether
2279 NAME depended on the type of the function. As we don't yet implement
2280 delayed emission of static data, we mark the decl as emitted
2281 so it is not placed in the output. Anything using it must therefore pull
2282 out the STRING_CST initializer directly. FIXME. */
2284 static tree
2285 c_make_fname_decl (tree id, int type_dep)
2287 const char *name = fname_as_string (type_dep);
2288 tree decl, type, init;
2289 size_t length = strlen (name);
2291 type = build_array_type
2292 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2293 build_index_type (size_int (length)));
2295 decl = build_decl (VAR_DECL, id, type);
2297 TREE_STATIC (decl) = 1;
2298 TREE_READONLY (decl) = 1;
2299 DECL_ARTIFICIAL (decl) = 1;
2301 init = build_string (length + 1, name);
2302 TREE_TYPE (init) = type;
2303 DECL_INITIAL (decl) = init;
2305 TREE_USED (decl) = 1;
2307 if (current_function_decl)
2309 DECL_CONTEXT (decl) = current_function_decl;
2310 IDENTIFIER_SYMBOL_VALUE (id) = decl;
2311 SCOPE_LIST_APPEND (current_function_scope, names, decl);
2314 finish_decl (decl, init, NULL_TREE);
2316 return decl;
2319 /* Return a definition for a builtin function named NAME and whose data type
2320 is TYPE. TYPE should be a function type with argument types.
2321 FUNCTION_CODE tells later passes how to compile calls to this function.
2322 See tree.h for its possible values.
2324 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2325 the name to be called if we can't opencode the function. If
2326 ATTRS is nonzero, use that for the function's attribute list. */
2328 tree
2329 builtin_function (const char *name, tree type, int function_code,
2330 enum built_in_class class, const char *library_name,
2331 tree attrs)
2333 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
2334 DECL_EXTERNAL (decl) = 1;
2335 TREE_PUBLIC (decl) = 1;
2336 if (library_name)
2337 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2338 make_decl_rtl (decl, NULL);
2339 pushdecl (decl);
2340 DECL_BUILT_IN_CLASS (decl) = class;
2341 DECL_FUNCTION_CODE (decl) = function_code;
2343 /* Warn if a function in the namespace for users
2344 is used without an occasion to consider it declared. */
2345 if (name[0] != '_' || name[1] != '_')
2346 C_DECL_INVISIBLE (decl) = 1;
2348 /* Possibly apply some default attributes to this built-in function. */
2349 if (attrs)
2350 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2351 else
2352 decl_attributes (&decl, NULL_TREE, 0);
2354 return decl;
2357 /* Called when a declaration is seen that contains no names to declare.
2358 If its type is a reference to a structure, union or enum inherited
2359 from a containing scope, shadow that tag name for the current scope
2360 with a forward reference.
2361 If its type defines a new named structure or union
2362 or defines an enum, it is valid but we need not do anything here.
2363 Otherwise, it is an error. */
2365 void
2366 shadow_tag (tree declspecs)
2368 shadow_tag_warned (declspecs, 0);
2371 void
2372 shadow_tag_warned (tree declspecs, int warned)
2375 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2376 no pedwarn. */
2378 int found_tag = 0;
2379 tree link;
2380 tree specs, attrs;
2382 pending_invalid_xref = 0;
2384 /* Remove the attributes from declspecs, since they will confuse the
2385 following code. */
2386 split_specs_attrs (declspecs, &specs, &attrs);
2388 for (link = specs; link; link = TREE_CHAIN (link))
2390 tree value = TREE_VALUE (link);
2391 enum tree_code code = TREE_CODE (value);
2393 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2394 /* Used to test also that TYPE_SIZE (value) != 0.
2395 That caused warning for `struct foo;' at top level in the file. */
2397 tree name = TYPE_NAME (value);
2398 tree t;
2400 found_tag++;
2402 if (name == 0)
2404 if (warned != 1 && code != ENUMERAL_TYPE)
2405 /* Empty unnamed enum OK */
2407 pedwarn ("unnamed struct/union that defines no instances");
2408 warned = 1;
2411 else
2413 t = lookup_tag (code, name, 1);
2415 if (t == 0)
2417 t = make_node (code);
2418 pushtag (name, t);
2422 else
2424 if (!warned && ! in_system_header)
2426 warning ("useless keyword or type name in empty declaration");
2427 warned = 2;
2432 if (found_tag > 1)
2433 error ("two types specified in one empty declaration");
2435 if (warned != 1)
2437 if (found_tag == 0)
2438 pedwarn ("empty declaration");
2442 /* Construct an array declarator. EXPR is the expression inside [], or
2443 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2444 to the pointer to which a parameter array is converted). STATIC_P is
2445 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2446 is nonzero is the array is [*], a VLA of unspecified length which is
2447 nevertheless a complete type (not currently implemented by GCC),
2448 zero otherwise. The declarator is constructed as an ARRAY_REF
2449 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2450 left of the [] (filled by in set_array_declarator_type) and operand 1
2451 is the expression inside; whose TREE_TYPE is the type qualifiers and
2452 which has TREE_STATIC set if "static" is used. */
2454 tree
2455 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2457 tree decl;
2458 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2459 TREE_TYPE (decl) = quals;
2460 TREE_STATIC (decl) = (static_p ? 1 : 0);
2461 if (pedantic && !flag_isoc99)
2463 if (static_p || quals != NULL_TREE)
2464 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2465 if (vla_unspec_p)
2466 pedwarn ("ISO C90 does not support `[*]' array declarators");
2468 if (vla_unspec_p)
2469 warning ("GCC does not yet properly implement `[*]' array declarators");
2470 return decl;
2473 /* Set the type of an array declarator. DECL is the declarator, as
2474 constructed by build_array_declarator; TYPE is what appears on the left
2475 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2476 abstract declarator, zero otherwise; this is used to reject static and
2477 type qualifiers in abstract declarators, where they are not in the
2478 C99 grammar. */
2480 tree
2481 set_array_declarator_type (tree decl, tree type, int abstract_p)
2483 TREE_OPERAND (decl, 0) = type;
2484 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2485 error ("static or type qualifiers in abstract declarator");
2486 return decl;
2489 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2491 tree
2492 groktypename (tree typename)
2494 tree specs, attrs;
2496 if (TREE_CODE (typename) != TREE_LIST)
2497 return typename;
2499 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2501 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2502 NULL);
2504 /* Apply attributes. */
2505 decl_attributes (&typename, attrs, 0);
2507 return typename;
2510 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2512 tree
2513 groktypename_in_parm_context (tree typename)
2515 if (TREE_CODE (typename) != TREE_LIST)
2516 return typename;
2517 return grokdeclarator (TREE_VALUE (typename),
2518 TREE_PURPOSE (typename),
2519 PARM, 0, NULL);
2522 /* Decode a declarator in an ordinary declaration or data definition.
2523 This is called as soon as the type information and variable name
2524 have been parsed, before parsing the initializer if any.
2525 Here we create the ..._DECL node, fill in its type,
2526 and put it on the list of decls for the current context.
2527 The ..._DECL node is returned as the value.
2529 Exception: for arrays where the length is not specified,
2530 the type is left null, to be filled in by `finish_decl'.
2532 Function definitions do not come here; they go to start_function
2533 instead. However, external and forward declarations of functions
2534 do go through here. Structure field declarations are done by
2535 grokfield and not through here. */
2537 tree
2538 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2540 tree decl;
2541 tree tem;
2543 /* An object declared as __attribute__((deprecated)) suppresses
2544 warnings of uses of other deprecated items. */
2545 if (lookup_attribute ("deprecated", attributes))
2546 deprecated_state = DEPRECATED_SUPPRESS;
2548 decl = grokdeclarator (declarator, declspecs,
2549 NORMAL, initialized, NULL);
2551 deprecated_state = DEPRECATED_NORMAL;
2553 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2554 && MAIN_NAME_P (DECL_NAME (decl)))
2555 warning ("%J'%D' is usually a function", decl, decl);
2557 if (initialized)
2558 /* Is it valid for this decl to have an initializer at all?
2559 If not, set INITIALIZED to zero, which will indirectly
2560 tell `finish_decl' to ignore the initializer once it is parsed. */
2561 switch (TREE_CODE (decl))
2563 case TYPE_DECL:
2564 error ("typedef `%s' is initialized (use __typeof__ instead)",
2565 IDENTIFIER_POINTER (DECL_NAME (decl)));
2566 initialized = 0;
2567 break;
2569 case FUNCTION_DECL:
2570 error ("function `%s' is initialized like a variable",
2571 IDENTIFIER_POINTER (DECL_NAME (decl)));
2572 initialized = 0;
2573 break;
2575 case PARM_DECL:
2576 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2577 error ("parameter `%s' is initialized",
2578 IDENTIFIER_POINTER (DECL_NAME (decl)));
2579 initialized = 0;
2580 break;
2582 default:
2583 /* Don't allow initializations for incomplete types
2584 except for arrays which might be completed by the initialization. */
2586 /* This can happen if the array size is an undefined macro. We already
2587 gave a warning, so we don't need another one. */
2588 if (TREE_TYPE (decl) == error_mark_node)
2589 initialized = 0;
2590 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2592 /* A complete type is ok if size is fixed. */
2594 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2595 || C_DECL_VARIABLE_SIZE (decl))
2597 error ("variable-sized object may not be initialized");
2598 initialized = 0;
2601 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2603 error ("variable `%s' has initializer but incomplete type",
2604 IDENTIFIER_POINTER (DECL_NAME (decl)));
2605 initialized = 0;
2607 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2609 error ("elements of array `%s' have incomplete type",
2610 IDENTIFIER_POINTER (DECL_NAME (decl)));
2611 initialized = 0;
2615 if (initialized)
2617 DECL_EXTERNAL (decl) = 0;
2618 if (current_scope == global_scope)
2619 TREE_STATIC (decl) = 1;
2621 /* Tell `pushdecl' this is an initialized decl
2622 even though we don't yet have the initializer expression.
2623 Also tell `finish_decl' it may store the real initializer. */
2624 DECL_INITIAL (decl) = error_mark_node;
2627 /* If this is a function declaration, write a record describing it to the
2628 prototypes file (if requested). */
2630 if (TREE_CODE (decl) == FUNCTION_DECL)
2631 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2633 /* ANSI specifies that a tentative definition which is not merged with
2634 a non-tentative definition behaves exactly like a definition with an
2635 initializer equal to zero. (Section 3.7.2)
2637 -fno-common gives strict ANSI behavior, though this tends to break
2638 a large body of code that grew up without this rule.
2640 Thread-local variables are never common, since there's no entrenched
2641 body of code to break, and it allows more efficient variable references
2642 in the presence of dynamic linking. */
2644 if (TREE_CODE (decl) == VAR_DECL
2645 && !initialized
2646 && TREE_PUBLIC (decl)
2647 && !DECL_THREAD_LOCAL (decl)
2648 && !flag_no_common)
2649 DECL_COMMON (decl) = 1;
2651 /* Set attributes here so if duplicate decl, will have proper attributes. */
2652 decl_attributes (&decl, attributes, 0);
2654 if (TREE_CODE (decl) == FUNCTION_DECL
2655 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2657 tree ce = declarator;
2659 if (TREE_CODE (ce) == INDIRECT_REF)
2660 ce = TREE_OPERAND (declarator, 0);
2661 if (TREE_CODE (ce) == CALL_EXPR)
2663 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2664 for (; args; args = TREE_CHAIN (args))
2666 tree type = TREE_TYPE (args);
2667 if (INTEGRAL_TYPE_P (type)
2668 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2669 DECL_ARG_TYPE (args) = integer_type_node;
2674 if (TREE_CODE (decl) == FUNCTION_DECL
2675 && DECL_DECLARED_INLINE_P (decl)
2676 && DECL_UNINLINABLE (decl)
2677 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2678 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2680 /* Add this decl to the current scope.
2681 TEM may equal DECL or it may be a previous decl of the same name. */
2682 tem = pushdecl (decl);
2684 /* For a local variable, define the RTL now. */
2685 if (current_scope != global_scope
2686 /* But not if this is a duplicate decl
2687 and we preserved the rtl from the previous one
2688 (which may or may not happen). */
2689 && !DECL_RTL_SET_P (tem)
2690 && DECL_FILE_SCOPE_P (tem))
2692 if (TREE_TYPE (tem) != error_mark_node
2693 && (COMPLETE_TYPE_P (TREE_TYPE (tem))
2694 || (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
2695 && DECL_INITIAL (tem) != 0)))
2696 expand_decl (tem);
2699 return tem;
2702 /* Finish processing of a declaration;
2703 install its initial value.
2704 If the length of an array type is not known before,
2705 it must be determined now, from the initial value, or it is an error. */
2707 void
2708 finish_decl (tree decl, tree init, tree asmspec_tree)
2710 tree type = TREE_TYPE (decl);
2711 int was_incomplete = (DECL_SIZE (decl) == 0);
2712 const char *asmspec = 0;
2714 /* If a name was specified, get the string. */
2715 if (current_scope == global_scope)
2716 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2717 if (asmspec_tree)
2718 asmspec = TREE_STRING_POINTER (asmspec_tree);
2720 /* If `start_decl' didn't like having an initialization, ignore it now. */
2721 if (init != 0 && DECL_INITIAL (decl) == 0)
2722 init = 0;
2724 /* Don't crash if parm is initialized. */
2725 if (TREE_CODE (decl) == PARM_DECL)
2726 init = 0;
2728 if (init)
2729 store_init_value (decl, init);
2731 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2732 || TREE_CODE (decl) == FUNCTION_DECL
2733 || TREE_CODE (decl) == FIELD_DECL))
2734 objc_check_decl (decl);
2736 /* Deduce size of array from initialization, if not already known. */
2737 if (TREE_CODE (type) == ARRAY_TYPE
2738 && TYPE_DOMAIN (type) == 0
2739 && TREE_CODE (decl) != TYPE_DECL)
2741 int do_default
2742 = (TREE_STATIC (decl)
2743 /* Even if pedantic, an external linkage array
2744 may have incomplete type at first. */
2745 ? pedantic && !TREE_PUBLIC (decl)
2746 : !DECL_EXTERNAL (decl));
2747 int failure
2748 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2750 /* Get the completed type made by complete_array_type. */
2751 type = TREE_TYPE (decl);
2753 if (failure == 1)
2754 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2756 else if (failure == 2)
2758 if (do_default)
2759 error ("%Jarray size missing in '%D'", decl, decl);
2760 /* If a `static' var's size isn't known,
2761 make it extern as well as static, so it does not get
2762 allocated.
2763 If it is not `static', then do not mark extern;
2764 finish_incomplete_decl will give it a default size
2765 and it will get allocated. */
2766 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2767 DECL_EXTERNAL (decl) = 1;
2770 /* TYPE_MAX_VALUE is always one less than the number of elements
2771 in the array, because we start counting at zero. Therefore,
2772 warn only if the value is less than zero. */
2773 else if (pedantic && TYPE_DOMAIN (type) != 0
2774 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2775 error ("%Jzero or negative size array '%D'", decl, decl);
2777 layout_decl (decl, 0);
2780 if (TREE_CODE (decl) == VAR_DECL)
2782 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2783 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2784 layout_decl (decl, 0);
2786 if (DECL_SIZE (decl) == 0
2787 /* Don't give an error if we already gave one earlier. */
2788 && TREE_TYPE (decl) != error_mark_node
2789 && (TREE_STATIC (decl)
2791 /* A static variable with an incomplete type
2792 is an error if it is initialized.
2793 Also if it is not file scope.
2794 Otherwise, let it through, but if it is not `extern'
2795 then it may cause an error message later. */
2796 (DECL_INITIAL (decl) != 0
2797 || !DECL_FILE_SCOPE_P (decl))
2799 /* An automatic variable with an incomplete type
2800 is an error. */
2801 !DECL_EXTERNAL (decl)))
2803 error ("%Jstorage size of '%D' isn't known", decl, decl);
2804 TREE_TYPE (decl) = error_mark_node;
2807 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2808 && DECL_SIZE (decl) != 0)
2810 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2811 constant_expression_warning (DECL_SIZE (decl));
2812 else
2813 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2816 if (TREE_USED (type))
2817 TREE_USED (decl) = 1;
2820 /* If this is a function and an assembler name is specified, reset DECL_RTL
2821 so we can give it its new name. Also, update built_in_decls if it
2822 was a normal built-in. */
2823 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2825 /* ASMSPEC is given, and not the name of a register. Mark the
2826 name with a star so assemble_name won't munge it. */
2827 char *starred = alloca (strlen (asmspec) + 2);
2828 starred[0] = '*';
2829 strcpy (starred + 1, asmspec);
2831 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2833 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2834 SET_DECL_RTL (builtin, NULL_RTX);
2835 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2836 #ifdef TARGET_MEM_FUNCTIONS
2837 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2838 init_block_move_fn (starred);
2839 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2840 init_block_clear_fn (starred);
2841 #else
2842 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2843 init_block_move_fn (starred);
2844 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2845 init_block_clear_fn (starred);
2846 #endif
2848 SET_DECL_RTL (decl, NULL_RTX);
2849 change_decl_assembler_name (decl, get_identifier (starred));
2852 /* If #pragma weak was used, mark the decl weak now. */
2853 if (current_scope == global_scope)
2854 maybe_apply_pragma_weak (decl);
2856 /* Output the assembler code and/or RTL code for variables and functions,
2857 unless the type is an undefined structure or union.
2858 If not, it will get done when the type is completed. */
2860 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2862 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2863 if (c_dialect_objc ())
2864 objc_check_decl (decl);
2866 if (DECL_FILE_SCOPE_P (decl))
2868 if (DECL_INITIAL (decl) == NULL_TREE
2869 || DECL_INITIAL (decl) == error_mark_node)
2870 /* Don't output anything
2871 when a tentative file-scope definition is seen.
2872 But at end of compilation, do output code for them. */
2873 DECL_DEFER_OUTPUT (decl) = 1;
2874 rest_of_decl_compilation (decl, asmspec, true, 0);
2876 else
2878 /* This is a local variable. If there is an ASMSPEC, the
2879 user has requested that we handle it specially. */
2880 if (asmspec)
2882 /* In conjunction with an ASMSPEC, the `register'
2883 keyword indicates that we should place the variable
2884 in a particular register. */
2885 if (DECL_REGISTER (decl))
2886 DECL_C_HARD_REGISTER (decl) = 1;
2888 /* If this is not a static variable, issue a warning.
2889 It doesn't make any sense to give an ASMSPEC for an
2890 ordinary, non-register local variable. Historically,
2891 GCC has accepted -- but ignored -- the ASMSPEC in
2892 this case. */
2893 if (TREE_CODE (decl) == VAR_DECL
2894 && !DECL_REGISTER (decl)
2895 && !TREE_STATIC (decl))
2896 warning ("%Jignoring asm-specifier for non-static local "
2897 "variable '%D'", decl, decl);
2898 else
2899 change_decl_assembler_name (decl, get_identifier (asmspec));
2902 if (TREE_CODE (decl) != FUNCTION_DECL)
2903 add_decl_stmt (decl);
2906 if (!DECL_FILE_SCOPE_P (decl))
2908 /* Recompute the RTL of a local array now
2909 if it used to be an incomplete type. */
2910 if (was_incomplete
2911 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2913 /* If we used it already as memory, it must stay in memory. */
2914 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2915 /* If it's still incomplete now, no init will save it. */
2916 if (DECL_SIZE (decl) == 0)
2917 DECL_INITIAL (decl) = 0;
2922 /* If this was marked 'used', be sure it will be output. */
2923 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2924 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2926 if (TREE_CODE (decl) == TYPE_DECL)
2927 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2929 /* At the end of a declaration, throw away any variable type sizes
2930 of types defined inside that declaration. There is no use
2931 computing them in the following function definition. */
2932 if (current_scope == global_scope)
2933 get_pending_sizes ();
2935 /* Install a cleanup (aka destructor) if one was given. */
2936 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2938 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2939 if (attr)
2941 static bool eh_initialized_p;
2943 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2944 tree cleanup_decl = lookup_name (cleanup_id);
2945 tree cleanup;
2947 /* Build "cleanup(&decl)" for the destructor. */
2948 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2949 cleanup = build_tree_list (NULL_TREE, cleanup);
2950 cleanup = build_function_call (cleanup_decl, cleanup);
2952 /* Don't warn about decl unused; the cleanup uses it. */
2953 TREE_USED (decl) = 1;
2955 /* Initialize EH, if we've been told to do so. */
2956 if (flag_exceptions && !eh_initialized_p)
2958 eh_initialized_p = true;
2959 eh_personality_libfunc
2960 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2961 ? "__gcc_personality_sj0"
2962 : "__gcc_personality_v0");
2963 using_eh_for_cleanups ();
2966 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2971 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2972 and push that on the current scope. */
2974 void
2975 push_parm_decl (tree parm)
2977 tree decl;
2979 /* Don't attempt to expand sizes while parsing this decl.
2980 (We can get here with i_s_e 1 somehow from Objective-C.) */
2981 int save_immediate_size_expand = immediate_size_expand;
2982 immediate_size_expand = 0;
2984 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
2985 TREE_PURPOSE (TREE_PURPOSE (parm)),
2986 PARM, 0, NULL);
2987 decl_attributes (&decl, TREE_VALUE (parm), 0);
2989 decl = pushdecl (decl);
2991 finish_decl (decl, NULL_TREE, NULL_TREE);
2993 immediate_size_expand = save_immediate_size_expand;
2996 /* Mark all the parameter declarations to date as forward decls,
2997 shift them to the variables list, and reset the parameters list.
2998 Also diagnose use of this extension. */
3000 void
3001 mark_forward_parm_decls (void)
3003 tree parm;
3005 if (pedantic && !current_scope->warned_forward_parm_decls)
3007 pedwarn ("ISO C forbids forward parameter declarations");
3008 current_scope->warned_forward_parm_decls = true;
3011 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
3012 TREE_ASM_WRITTEN (parm) = 1;
3014 SCOPE_LIST_CONCAT (current_scope, names, current_scope, parms);
3015 current_scope->parms = 0;
3016 current_scope->parms_last = 0;
3019 static GTY(()) int compound_literal_number;
3021 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3022 literal, which may be an incomplete array type completed by the
3023 initializer; INIT is a CONSTRUCTOR that initializes the compound
3024 literal. */
3026 tree
3027 build_compound_literal (tree type, tree init)
3029 /* We do not use start_decl here because we have a type, not a declarator;
3030 and do not use finish_decl because the decl should be stored inside
3031 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3032 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3033 tree complit;
3034 tree stmt;
3035 DECL_EXTERNAL (decl) = 0;
3036 TREE_PUBLIC (decl) = 0;
3037 TREE_STATIC (decl) = (current_scope == global_scope);
3038 DECL_CONTEXT (decl) = current_function_decl;
3039 TREE_USED (decl) = 1;
3040 TREE_TYPE (decl) = type;
3041 TREE_READONLY (decl) = TREE_READONLY (type);
3042 store_init_value (decl, init);
3044 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3046 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3047 if (failure)
3048 abort ();
3051 type = TREE_TYPE (decl);
3052 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3053 return error_mark_node;
3055 stmt = build_stmt (DECL_STMT, decl);
3056 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3057 TREE_SIDE_EFFECTS (complit) = 1;
3059 layout_decl (decl, 0);
3061 if (TREE_STATIC (decl))
3063 /* This decl needs a name for the assembler output. We also need
3064 a unique suffix to be added to the name. */
3065 char *name;
3067 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3068 compound_literal_number);
3069 compound_literal_number++;
3070 DECL_NAME (decl) = get_identifier (name);
3071 DECL_DEFER_OUTPUT (decl) = 1;
3072 DECL_COMDAT (decl) = 1;
3073 DECL_ARTIFICIAL (decl) = 1;
3074 pushdecl (decl);
3075 rest_of_decl_compilation (decl, NULL, 1, 0);
3078 return complit;
3081 /* Make TYPE a complete type based on INITIAL_VALUE.
3082 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3083 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3086 complete_array_type (tree type, tree initial_value, int do_default)
3088 tree maxindex = NULL_TREE;
3089 int value = 0;
3091 if (initial_value)
3093 /* Note MAXINDEX is really the maximum index,
3094 one less than the size. */
3095 if (TREE_CODE (initial_value) == STRING_CST)
3097 int eltsize
3098 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3099 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3100 / eltsize) - 1, 0);
3102 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3104 tree elts = CONSTRUCTOR_ELTS (initial_value);
3105 maxindex = build_int_2 (-1, -1);
3106 for (; elts; elts = TREE_CHAIN (elts))
3108 if (TREE_PURPOSE (elts))
3109 maxindex = TREE_PURPOSE (elts);
3110 else
3111 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3112 maxindex, integer_one_node));
3114 maxindex = copy_node (maxindex);
3116 else
3118 /* Make an error message unless that happened already. */
3119 if (initial_value != error_mark_node)
3120 value = 1;
3122 /* Prevent further error messages. */
3123 maxindex = build_int_2 (0, 0);
3127 if (!maxindex)
3129 if (do_default)
3130 maxindex = build_int_2 (0, 0);
3131 value = 2;
3134 if (maxindex)
3136 TYPE_DOMAIN (type) = build_index_type (maxindex);
3137 if (!TREE_TYPE (maxindex))
3138 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3141 /* Lay out the type now that we can get the real answer. */
3143 layout_type (type);
3145 return value;
3148 /* Determine whether TYPE is a structure with a flexible array member,
3149 or a union containing such a structure (possibly recursively). */
3151 static bool
3152 flexible_array_type_p (tree type)
3154 tree x;
3155 switch (TREE_CODE (type))
3157 case RECORD_TYPE:
3158 x = TYPE_FIELDS (type);
3159 if (x == NULL_TREE)
3160 return false;
3161 while (TREE_CHAIN (x) != NULL_TREE)
3162 x = TREE_CHAIN (x);
3163 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3164 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3165 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3166 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3167 return true;
3168 return false;
3169 case UNION_TYPE:
3170 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3172 if (flexible_array_type_p (TREE_TYPE (x)))
3173 return true;
3175 return false;
3176 default:
3177 return false;
3181 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3182 replacing with appropriate values if they are invalid. */
3183 static void
3184 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3186 tree type_mv;
3187 unsigned int max_width;
3188 unsigned HOST_WIDE_INT w;
3189 const char *name = orig_name ? orig_name: _("<anonymous>");
3191 /* Necessary? */
3192 STRIP_NOPS (*width);
3194 /* Detect and ignore out of range field width and process valid
3195 field widths. */
3196 if (TREE_CODE (*width) != INTEGER_CST)
3198 error ("bit-field `%s' width not an integer constant", name);
3199 *width = integer_one_node;
3201 else
3203 constant_expression_warning (*width);
3204 if (tree_int_cst_sgn (*width) < 0)
3206 error ("negative width in bit-field `%s'", name);
3207 *width = integer_one_node;
3209 else if (integer_zerop (*width) && orig_name)
3211 error ("zero width for bit-field `%s'", name);
3212 *width = integer_one_node;
3216 /* Detect invalid bit-field type. */
3217 if (TREE_CODE (*type) != INTEGER_TYPE
3218 && TREE_CODE (*type) != BOOLEAN_TYPE
3219 && TREE_CODE (*type) != ENUMERAL_TYPE)
3221 error ("bit-field `%s' has invalid type", name);
3222 *type = unsigned_type_node;
3225 type_mv = TYPE_MAIN_VARIANT (*type);
3226 if (pedantic
3227 && type_mv != integer_type_node
3228 && type_mv != unsigned_type_node
3229 && type_mv != boolean_type_node)
3230 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3232 if (type_mv == boolean_type_node)
3233 max_width = CHAR_TYPE_SIZE;
3234 else
3235 max_width = TYPE_PRECISION (*type);
3237 if (0 < compare_tree_int (*width, max_width))
3239 error ("width of `%s' exceeds its type", name);
3240 w = max_width;
3241 *width = build_int_2 (w, 0);
3243 else
3244 w = tree_low_cst (*width, 1);
3246 if (TREE_CODE (*type) == ENUMERAL_TYPE
3247 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3248 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3249 warning ("`%s' is narrower than values of its type", name);
3252 /* Given declspecs and a declarator,
3253 determine the name and type of the object declared
3254 and construct a ..._DECL node for it.
3255 (In one case we can return a ..._TYPE node instead.
3256 For invalid input we sometimes return 0.)
3258 DECLSPECS is a chain of tree_list nodes whose value fields
3259 are the storage classes and type specifiers.
3261 DECL_CONTEXT says which syntactic context this declaration is in:
3262 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3263 FUNCDEF for a function definition. Like NORMAL but a few different
3264 error messages in each case. Return value may be zero meaning
3265 this definition is too screwy to try to parse.
3266 PARM for a parameter declaration (either within a function prototype
3267 or before a function body). Make a PARM_DECL, or return void_type_node.
3268 TYPENAME if for a typename (in a cast or sizeof).
3269 Don't make a DECL node; just return the ..._TYPE node.
3270 FIELD for a struct or union field; make a FIELD_DECL.
3271 INITIALIZED is 1 if the decl has an initializer.
3272 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3273 representing the width of the bit-field.
3275 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3276 It may also be so in the PARM case, for a prototype where the
3277 argument type is specified but not the name.
3279 This function is where the complicated C meanings of `static'
3280 and `extern' are interpreted. */
3282 static tree
3283 grokdeclarator (tree declarator, tree declspecs,
3284 enum decl_context decl_context, int initialized, tree *width)
3286 int specbits = 0;
3287 tree spec;
3288 tree type = NULL_TREE;
3289 int longlong = 0;
3290 int constp;
3291 int restrictp;
3292 int volatilep;
3293 int type_quals = TYPE_UNQUALIFIED;
3294 int inlinep;
3295 int explicit_int = 0;
3296 int explicit_char = 0;
3297 int defaulted_int = 0;
3298 tree typedef_decl = 0;
3299 const char *name, *orig_name;
3300 tree typedef_type = 0;
3301 int funcdef_flag = 0;
3302 enum tree_code innermost_code = ERROR_MARK;
3303 int size_varies = 0;
3304 tree decl_attr = NULL_TREE;
3305 tree array_ptr_quals = NULL_TREE;
3306 int array_parm_static = 0;
3307 tree returned_attrs = NULL_TREE;
3308 bool bitfield = width != NULL;
3309 tree element_type;
3311 if (decl_context == FUNCDEF)
3312 funcdef_flag = 1, decl_context = NORMAL;
3314 /* Look inside a declarator for the name being declared
3315 and get it as a string, for an error message. */
3317 tree decl = declarator;
3318 name = 0;
3320 while (decl)
3321 switch (TREE_CODE (decl))
3323 case ARRAY_REF:
3324 case INDIRECT_REF:
3325 case CALL_EXPR:
3326 innermost_code = TREE_CODE (decl);
3327 decl = TREE_OPERAND (decl, 0);
3328 break;
3330 case TREE_LIST:
3331 decl = TREE_VALUE (decl);
3332 break;
3334 case IDENTIFIER_NODE:
3335 name = IDENTIFIER_POINTER (decl);
3336 decl = 0;
3337 break;
3339 default:
3340 abort ();
3342 orig_name = name;
3343 if (name == 0)
3344 name = "type name";
3347 /* A function definition's declarator must have the form of
3348 a function declarator. */
3350 if (funcdef_flag && innermost_code != CALL_EXPR)
3351 return 0;
3353 /* If this looks like a function definition, make it one,
3354 even if it occurs where parms are expected.
3355 Then store_parm_decls will reject it and not use it as a parm. */
3356 if (decl_context == NORMAL && !funcdef_flag
3357 && current_scope->parm_flag)
3358 decl_context = PARM;
3360 /* Look through the decl specs and record which ones appear.
3361 Some typespecs are defined as built-in typenames.
3362 Others, the ones that are modifiers of other types,
3363 are represented by bits in SPECBITS: set the bits for
3364 the modifiers that appear. Storage class keywords are also in SPECBITS.
3366 If there is a typedef name or a type, store the type in TYPE.
3367 This includes builtin typedefs such as `int'.
3369 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3370 and did not come from a user typedef.
3372 Set LONGLONG if `long' is mentioned twice. */
3374 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3376 tree id = TREE_VALUE (spec);
3378 /* If the entire declaration is itself tagged as deprecated then
3379 suppress reports of deprecated items. */
3380 if (id && TREE_DEPRECATED (id))
3382 if (deprecated_state != DEPRECATED_SUPPRESS)
3383 warn_deprecated_use (id);
3386 if (id == ridpointers[(int) RID_INT])
3387 explicit_int = 1;
3388 if (id == ridpointers[(int) RID_CHAR])
3389 explicit_char = 1;
3391 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3393 enum rid i = C_RID_CODE (id);
3394 if ((int) i <= (int) RID_LAST_MODIFIER)
3396 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3398 if (longlong)
3399 error ("`long long long' is too long for GCC");
3400 else
3402 if (pedantic && !flag_isoc99 && ! in_system_header
3403 && warn_long_long)
3404 pedwarn ("ISO C90 does not support `long long'");
3405 longlong = 1;
3408 else if (specbits & (1 << (int) i))
3410 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3412 if (!flag_isoc99)
3413 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3415 else
3416 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3419 /* Diagnose "__thread extern". Recall that this list
3420 is in the reverse order seen in the text. */
3421 if (i == RID_THREAD
3422 && (specbits & (1 << (int) RID_EXTERN
3423 | 1 << (int) RID_STATIC)))
3425 if (specbits & 1 << (int) RID_EXTERN)
3426 error ("`__thread' before `extern'");
3427 else
3428 error ("`__thread' before `static'");
3431 specbits |= 1 << (int) i;
3432 goto found;
3435 if (type)
3436 error ("two or more data types in declaration of `%s'", name);
3437 /* Actual typedefs come to us as TYPE_DECL nodes. */
3438 else if (TREE_CODE (id) == TYPE_DECL)
3440 if (TREE_TYPE (id) == error_mark_node)
3441 ; /* Allow the type to default to int to avoid cascading errors. */
3442 else
3444 type = TREE_TYPE (id);
3445 decl_attr = DECL_ATTRIBUTES (id);
3446 typedef_decl = id;
3449 /* Built-in types come as identifiers. */
3450 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3452 tree t = lookup_name (id);
3453 if (TREE_TYPE (t) == error_mark_node)
3455 else if (!t || TREE_CODE (t) != TYPE_DECL)
3456 error ("`%s' fails to be a typedef or built in type",
3457 IDENTIFIER_POINTER (id));
3458 else
3460 type = TREE_TYPE (t);
3461 typedef_decl = t;
3464 else if (TREE_CODE (id) != ERROR_MARK)
3465 type = id;
3467 found:
3471 typedef_type = type;
3472 if (type)
3473 size_varies = C_TYPE_VARIABLE_SIZE (type);
3475 /* No type at all: default to `int', and set DEFAULTED_INT
3476 because it was not a user-defined typedef. */
3478 if (type == 0)
3480 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3481 | (1 << (int) RID_SIGNED)
3482 | (1 << (int) RID_UNSIGNED)
3483 | (1 << (int) RID_COMPLEX))))
3484 /* Don't warn about typedef foo = bar. */
3485 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3486 && ! in_system_header)
3488 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3489 and this is a function, or if -Wimplicit; prefer the former
3490 warning since it is more explicit. */
3491 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3492 && funcdef_flag)
3493 warn_about_return_type = 1;
3494 else if (warn_implicit_int || flag_isoc99)
3495 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3496 name);
3499 defaulted_int = 1;
3500 type = integer_type_node;
3503 /* Now process the modifiers that were specified
3504 and check for invalid combinations. */
3506 /* Long double is a special combination. */
3508 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3509 && TYPE_MAIN_VARIANT (type) == double_type_node)
3511 specbits &= ~(1 << (int) RID_LONG);
3512 type = long_double_type_node;
3515 /* Check all other uses of type modifiers. */
3517 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3518 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3520 int ok = 0;
3522 if ((specbits & 1 << (int) RID_LONG)
3523 && (specbits & 1 << (int) RID_SHORT))
3524 error ("both long and short specified for `%s'", name);
3525 else if (((specbits & 1 << (int) RID_LONG)
3526 || (specbits & 1 << (int) RID_SHORT))
3527 && explicit_char)
3528 error ("long or short specified with char for `%s'", name);
3529 else if (((specbits & 1 << (int) RID_LONG)
3530 || (specbits & 1 << (int) RID_SHORT))
3531 && TREE_CODE (type) == REAL_TYPE)
3533 static int already = 0;
3535 error ("long or short specified with floating type for `%s'", name);
3536 if (! already && ! pedantic)
3538 error ("the only valid combination is `long double'");
3539 already = 1;
3542 else if ((specbits & 1 << (int) RID_SIGNED)
3543 && (specbits & 1 << (int) RID_UNSIGNED))
3544 error ("both signed and unsigned specified for `%s'", name);
3545 else if (TREE_CODE (type) != INTEGER_TYPE)
3546 error ("long, short, signed or unsigned invalid for `%s'", name);
3547 else
3549 ok = 1;
3550 if (!explicit_int && !defaulted_int && !explicit_char)
3552 error ("long, short, signed or unsigned used invalidly for `%s'",
3553 name);
3554 ok = 0;
3558 /* Discard the type modifiers if they are invalid. */
3559 if (! ok)
3561 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3562 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3563 longlong = 0;
3567 if ((specbits & (1 << (int) RID_COMPLEX))
3568 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3570 error ("complex invalid for `%s'", name);
3571 specbits &= ~(1 << (int) RID_COMPLEX);
3574 /* Decide whether an integer type is signed or not.
3575 Optionally treat bit-fields as signed by default. */
3576 if (specbits & 1 << (int) RID_UNSIGNED
3577 || (bitfield && ! flag_signed_bitfields
3578 && (explicit_int || defaulted_int || explicit_char
3579 /* A typedef for plain `int' without `signed'
3580 can be controlled just like plain `int'. */
3581 || ! (typedef_decl != 0
3582 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3583 && TREE_CODE (type) != ENUMERAL_TYPE
3584 && !(specbits & 1 << (int) RID_SIGNED)))
3586 if (longlong)
3587 type = long_long_unsigned_type_node;
3588 else if (specbits & 1 << (int) RID_LONG)
3589 type = long_unsigned_type_node;
3590 else if (specbits & 1 << (int) RID_SHORT)
3591 type = short_unsigned_type_node;
3592 else if (type == char_type_node)
3593 type = unsigned_char_type_node;
3594 else if (typedef_decl)
3595 type = c_common_unsigned_type (type);
3596 else
3597 type = unsigned_type_node;
3599 else if ((specbits & 1 << (int) RID_SIGNED)
3600 && type == char_type_node)
3601 type = signed_char_type_node;
3602 else if (longlong)
3603 type = long_long_integer_type_node;
3604 else if (specbits & 1 << (int) RID_LONG)
3605 type = long_integer_type_node;
3606 else if (specbits & 1 << (int) RID_SHORT)
3607 type = short_integer_type_node;
3609 if (specbits & 1 << (int) RID_COMPLEX)
3611 if (pedantic && !flag_isoc99)
3612 pedwarn ("ISO C90 does not support complex types");
3613 /* If we just have "complex", it is equivalent to
3614 "complex double", but if any modifiers at all are specified it is
3615 the complex form of TYPE. E.g, "complex short" is
3616 "complex short int". */
3618 if (defaulted_int && ! longlong
3619 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3620 | (1 << (int) RID_SIGNED)
3621 | (1 << (int) RID_UNSIGNED))))
3623 if (pedantic)
3624 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3625 type = complex_double_type_node;
3627 else if (type == integer_type_node)
3629 if (pedantic)
3630 pedwarn ("ISO C does not support complex integer types");
3631 type = complex_integer_type_node;
3633 else if (type == float_type_node)
3634 type = complex_float_type_node;
3635 else if (type == double_type_node)
3636 type = complex_double_type_node;
3637 else if (type == long_double_type_node)
3638 type = complex_long_double_type_node;
3639 else
3641 if (pedantic)
3642 pedwarn ("ISO C does not support complex integer types");
3643 type = build_complex_type (type);
3647 /* Check the type and width of a bit-field. */
3648 if (bitfield)
3649 check_bitfield_type_and_width (&type, width, orig_name);
3651 /* Figure out the type qualifiers for the declaration. There are
3652 two ways a declaration can become qualified. One is something
3653 like `const int i' where the `const' is explicit. Another is
3654 something like `typedef const int CI; CI i' where the type of the
3655 declaration contains the `const'. A third possibility is that
3656 there is a type qualifier on the element type of a typedefed
3657 array type, in which case we should extract that qualifier so
3658 that c_apply_type_quals_to_decls receives the full list of
3659 qualifiers to work with (C90 is not entirely clear about whether
3660 duplicate qualifiers should be diagnosed in this case, but it
3661 seems most appropriate to do so). */
3662 element_type = strip_array_types (type);
3663 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3664 restrictp
3665 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3666 volatilep
3667 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3668 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3669 if (constp > 1 && ! flag_isoc99)
3670 pedwarn ("duplicate `const'");
3671 if (restrictp > 1 && ! flag_isoc99)
3672 pedwarn ("duplicate `restrict'");
3673 if (volatilep > 1 && ! flag_isoc99)
3674 pedwarn ("duplicate `volatile'");
3675 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3676 type = TYPE_MAIN_VARIANT (type);
3677 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3678 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3679 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3681 /* Warn if two storage classes are given. Default to `auto'. */
3684 int nclasses = 0;
3686 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3687 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3688 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3689 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3690 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3692 /* "static __thread" and "extern __thread" are allowed. */
3693 if ((specbits & (1 << (int) RID_THREAD
3694 | 1 << (int) RID_STATIC
3695 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3696 nclasses++;
3698 /* Warn about storage classes that are invalid for certain
3699 kinds of declarations (parameters, typenames, etc.). */
3701 if (nclasses > 1)
3702 error ("multiple storage classes in declaration of `%s'", name);
3703 else if (funcdef_flag
3704 && (specbits
3705 & ((1 << (int) RID_REGISTER)
3706 | (1 << (int) RID_AUTO)
3707 | (1 << (int) RID_TYPEDEF)
3708 | (1 << (int) RID_THREAD))))
3710 if (specbits & 1 << (int) RID_AUTO
3711 && (pedantic || current_scope == global_scope))
3712 pedwarn ("function definition declared `auto'");
3713 if (specbits & 1 << (int) RID_REGISTER)
3714 error ("function definition declared `register'");
3715 if (specbits & 1 << (int) RID_TYPEDEF)
3716 error ("function definition declared `typedef'");
3717 if (specbits & 1 << (int) RID_THREAD)
3718 error ("function definition declared `__thread'");
3719 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3720 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3722 else if (decl_context != NORMAL && nclasses > 0)
3724 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3726 else
3728 switch (decl_context)
3730 case FIELD:
3731 error ("storage class specified for structure field `%s'",
3732 name);
3733 break;
3734 case PARM:
3735 error ("storage class specified for parameter `%s'", name);
3736 break;
3737 default:
3738 error ("storage class specified for typename");
3739 break;
3741 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3742 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3743 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3746 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3748 /* `extern' with initialization is invalid if not at file scope. */
3749 if (current_scope == global_scope)
3750 warning ("`%s' initialized and declared `extern'", name);
3751 else
3752 error ("`%s' has both `extern' and initializer", name);
3754 else if (current_scope == global_scope)
3756 if (specbits & 1 << (int) RID_AUTO)
3757 error ("file-scope declaration of `%s' specifies `auto'", name);
3759 else
3761 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3762 error ("nested function `%s' declared `extern'", name);
3763 else if ((specbits & (1 << (int) RID_THREAD
3764 | 1 << (int) RID_EXTERN
3765 | 1 << (int) RID_STATIC))
3766 == (1 << (int) RID_THREAD))
3768 error ("function-scope `%s' implicitly auto and declared `__thread'",
3769 name);
3770 specbits &= ~(1 << (int) RID_THREAD);
3775 /* Now figure out the structure of the declarator proper.
3776 Descend through it, creating more complex types, until we reach
3777 the declared identifier (or NULL_TREE, in an absolute declarator). */
3779 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3781 if (type == error_mark_node)
3783 declarator = TREE_OPERAND (declarator, 0);
3784 continue;
3787 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3788 an INDIRECT_REF (for *...),
3789 a CALL_EXPR (for ...(...)),
3790 a TREE_LIST (for nested attributes),
3791 an identifier (for the name being declared)
3792 or a null pointer (for the place in an absolute declarator
3793 where the name was omitted).
3794 For the last two cases, we have just exited the loop.
3796 At this point, TYPE is the type of elements of an array,
3797 or for a function to return, or for a pointer to point to.
3798 After this sequence of ifs, TYPE is the type of the
3799 array or function or pointer, and DECLARATOR has had its
3800 outermost layer removed. */
3802 if (array_ptr_quals != NULL_TREE || array_parm_static)
3804 /* Only the innermost declarator (making a parameter be of
3805 array type which is converted to pointer type)
3806 may have static or type qualifiers. */
3807 error ("static or type qualifiers in non-parameter array declarator");
3808 array_ptr_quals = NULL_TREE;
3809 array_parm_static = 0;
3812 if (TREE_CODE (declarator) == TREE_LIST)
3814 /* We encode a declarator with embedded attributes using
3815 a TREE_LIST. */
3816 tree attrs = TREE_PURPOSE (declarator);
3817 tree inner_decl;
3818 int attr_flags = 0;
3819 declarator = TREE_VALUE (declarator);
3820 inner_decl = declarator;
3821 while (inner_decl != NULL_TREE
3822 && TREE_CODE (inner_decl) == TREE_LIST)
3823 inner_decl = TREE_VALUE (inner_decl);
3824 if (inner_decl == NULL_TREE
3825 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3826 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3827 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3828 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3829 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3830 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3831 returned_attrs = decl_attributes (&type,
3832 chainon (returned_attrs, attrs),
3833 attr_flags);
3835 else if (TREE_CODE (declarator) == ARRAY_REF)
3837 tree itype = NULL_TREE;
3838 tree size = TREE_OPERAND (declarator, 1);
3839 /* The index is a signed object `sizetype' bits wide. */
3840 tree index_type = c_common_signed_type (sizetype);
3842 array_ptr_quals = TREE_TYPE (declarator);
3843 array_parm_static = TREE_STATIC (declarator);
3845 declarator = TREE_OPERAND (declarator, 0);
3847 /* Check for some types that there cannot be arrays of. */
3849 if (VOID_TYPE_P (type))
3851 error ("declaration of `%s' as array of voids", name);
3852 type = error_mark_node;
3855 if (TREE_CODE (type) == FUNCTION_TYPE)
3857 error ("declaration of `%s' as array of functions", name);
3858 type = error_mark_node;
3861 if (pedantic && flexible_array_type_p (type))
3862 pedwarn ("invalid use of structure with flexible array member");
3864 if (size == error_mark_node)
3865 type = error_mark_node;
3867 if (type == error_mark_node)
3868 continue;
3870 /* If size was specified, set ITYPE to a range-type for that size.
3871 Otherwise, ITYPE remains null. finish_decl may figure it out
3872 from an initial value. */
3874 if (size)
3876 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3877 STRIP_TYPE_NOPS (size);
3879 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3881 error ("size of array `%s' has non-integer type", name);
3882 size = integer_one_node;
3885 if (pedantic && integer_zerop (size))
3886 pedwarn ("ISO C forbids zero-size array `%s'", name);
3888 if (TREE_CODE (size) == INTEGER_CST)
3890 constant_expression_warning (size);
3891 if (tree_int_cst_sgn (size) < 0)
3893 error ("size of array `%s' is negative", name);
3894 size = integer_one_node;
3897 else
3899 /* Make sure the array size remains visibly nonconstant
3900 even if it is (eg) a const variable with known value. */
3901 size_varies = 1;
3903 if (!flag_isoc99 && pedantic)
3905 if (TREE_CONSTANT (size))
3906 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3907 name);
3908 else
3909 pedwarn ("ISO C90 forbids variable-size array `%s'",
3910 name);
3914 if (integer_zerop (size))
3916 /* A zero-length array cannot be represented with an
3917 unsigned index type, which is what we'll get with
3918 build_index_type. Create an open-ended range instead. */
3919 itype = build_range_type (sizetype, size, NULL_TREE);
3921 else
3923 /* Compute the maximum valid index, that is, size - 1.
3924 Do the calculation in index_type, so that if it is
3925 a variable the computations will be done in the
3926 proper mode. */
3927 itype = fold (build (MINUS_EXPR, index_type,
3928 convert (index_type, size),
3929 convert (index_type, size_one_node)));
3931 /* If that overflowed, the array is too big.
3932 ??? While a size of INT_MAX+1 technically shouldn't
3933 cause an overflow (because we subtract 1), the overflow
3934 is recorded during the conversion to index_type, before
3935 the subtraction. Handling this case seems like an
3936 unnecessary complication. */
3937 if (TREE_OVERFLOW (itype))
3939 error ("size of array `%s' is too large", name);
3940 type = error_mark_node;
3941 continue;
3944 if (size_varies)
3946 /* We must be able to distinguish the
3947 SAVE_EXPR_CONTEXT for the variably-sized type
3948 so that we can set it correctly in
3949 set_save_expr_context. The convention is
3950 that all SAVE_EXPRs that need to be reset
3951 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3952 tree cfd = current_function_decl;
3953 if (decl_context == PARM)
3954 current_function_decl = NULL_TREE;
3955 itype = variable_size (itype);
3956 if (decl_context == PARM)
3957 current_function_decl = cfd;
3959 itype = build_index_type (itype);
3962 else if (decl_context == FIELD)
3964 if (pedantic && !flag_isoc99 && !in_system_header)
3965 pedwarn ("ISO C90 does not support flexible array members");
3967 /* ISO C99 Flexible array members are effectively identical
3968 to GCC's zero-length array extension. */
3969 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3972 /* If pedantic, complain about arrays of incomplete types. */
3974 if (pedantic && !COMPLETE_TYPE_P (type))
3975 pedwarn ("array type has incomplete element type");
3977 /* Build the array type itself, then merge any constancy or
3978 volatility into the target type. We must do it in this order
3979 to ensure that the TYPE_MAIN_VARIANT field of the array type
3980 is set correctly. */
3982 type = build_array_type (type, itype);
3983 if (type_quals)
3984 type = c_build_qualified_type (type, type_quals);
3986 if (size_varies)
3987 C_TYPE_VARIABLE_SIZE (type) = 1;
3989 /* The GCC extension for zero-length arrays differs from
3990 ISO flexible array members in that sizeof yields zero. */
3991 if (size && integer_zerop (size))
3993 layout_type (type);
3994 TYPE_SIZE (type) = bitsize_zero_node;
3995 TYPE_SIZE_UNIT (type) = size_zero_node;
3997 if (decl_context != PARM
3998 && (array_ptr_quals != NULL_TREE || array_parm_static))
4000 error ("static or type qualifiers in non-parameter array declarator");
4001 array_ptr_quals = NULL_TREE;
4002 array_parm_static = 0;
4005 else if (TREE_CODE (declarator) == CALL_EXPR)
4007 tree arg_types;
4009 /* Declaring a function type.
4010 Make sure we have a valid type for the function to return. */
4011 if (type == error_mark_node)
4012 continue;
4014 size_varies = 0;
4016 /* Warn about some types functions can't return. */
4018 if (TREE_CODE (type) == FUNCTION_TYPE)
4020 error ("`%s' declared as function returning a function", name);
4021 type = integer_type_node;
4023 if (TREE_CODE (type) == ARRAY_TYPE)
4025 error ("`%s' declared as function returning an array", name);
4026 type = integer_type_node;
4029 /* Construct the function type and go to the next
4030 inner layer of declarator. */
4032 arg_types = grokparms (TREE_OPERAND (declarator, 1),
4033 funcdef_flag
4034 /* Say it's a definition
4035 only for the CALL_EXPR
4036 closest to the identifier. */
4037 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE);
4038 /* Type qualifiers before the return type of the function
4039 qualify the return type, not the function type. */
4040 if (type_quals)
4042 /* Type qualifiers on a function return type are normally
4043 permitted by the standard but have no effect, so give a
4044 warning at -Wextra. Qualifiers on a void return type have
4045 meaning as a GNU extension, and are banned on function
4046 definitions in ISO C. FIXME: strictly we shouldn't
4047 pedwarn for qualified void return types except on function
4048 definitions, but not doing so could lead to the undesirable
4049 state of a "volatile void" function return type not being
4050 warned about, and a use of the function being compiled
4051 with GNU semantics, with no diagnostics under -pedantic. */
4052 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4053 pedwarn ("ISO C forbids qualified void function return type");
4054 else if (extra_warnings
4055 && !(VOID_TYPE_P (type)
4056 && type_quals == TYPE_QUAL_VOLATILE))
4057 warning ("type qualifiers ignored on function return type");
4059 type = c_build_qualified_type (type, type_quals);
4061 type_quals = TYPE_UNQUALIFIED;
4063 type = build_function_type (type, arg_types);
4064 declarator = TREE_OPERAND (declarator, 0);
4066 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4067 the formal parameter list of this FUNCTION_TYPE to point to
4068 the FUNCTION_TYPE node itself. */
4071 tree link;
4073 for (link = last_function_parm_tags;
4074 link;
4075 link = TREE_CHAIN (link))
4076 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4079 else if (TREE_CODE (declarator) == INDIRECT_REF)
4081 /* Merge any constancy or volatility into the target type
4082 for the pointer. */
4084 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4085 && type_quals)
4086 pedwarn ("ISO C forbids qualified function types");
4087 if (type_quals)
4088 type = c_build_qualified_type (type, type_quals);
4089 type_quals = TYPE_UNQUALIFIED;
4090 size_varies = 0;
4092 type = build_pointer_type (type);
4094 /* Process a list of type modifier keywords
4095 (such as const or volatile) that were given inside the `*'. */
4097 if (TREE_TYPE (declarator))
4099 tree typemodlist;
4100 int erred = 0;
4102 constp = 0;
4103 volatilep = 0;
4104 restrictp = 0;
4105 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4106 typemodlist = TREE_CHAIN (typemodlist))
4108 tree qualifier = TREE_VALUE (typemodlist);
4110 if (C_IS_RESERVED_WORD (qualifier))
4112 if (C_RID_CODE (qualifier) == RID_CONST)
4113 constp++;
4114 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4115 volatilep++;
4116 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4117 restrictp++;
4118 else
4119 erred++;
4121 else
4122 erred++;
4125 if (erred)
4126 error ("invalid type modifier within pointer declarator");
4127 if (constp > 1 && ! flag_isoc99)
4128 pedwarn ("duplicate `const'");
4129 if (volatilep > 1 && ! flag_isoc99)
4130 pedwarn ("duplicate `volatile'");
4131 if (restrictp > 1 && ! flag_isoc99)
4132 pedwarn ("duplicate `restrict'");
4134 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4135 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4136 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4139 declarator = TREE_OPERAND (declarator, 0);
4141 else
4142 abort ();
4146 /* Now TYPE has the actual type. */
4148 /* Did array size calculations overflow? */
4150 if (TREE_CODE (type) == ARRAY_TYPE
4151 && COMPLETE_TYPE_P (type)
4152 && TREE_OVERFLOW (TYPE_SIZE (type)))
4154 error ("size of array `%s' is too large", name);
4155 /* If we proceed with the array type as it is, we'll eventually
4156 crash in tree_low_cst(). */
4157 type = error_mark_node;
4160 /* If this is declaring a typedef name, return a TYPE_DECL. */
4162 if (specbits & (1 << (int) RID_TYPEDEF))
4164 tree decl;
4165 /* Note that the grammar rejects storage classes
4166 in typenames, fields or parameters */
4167 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4168 && type_quals)
4169 pedwarn ("ISO C forbids qualified function types");
4170 if (type_quals)
4171 type = c_build_qualified_type (type, type_quals);
4172 decl = build_decl (TYPE_DECL, declarator, type);
4173 if ((specbits & (1 << (int) RID_SIGNED))
4174 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4175 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4176 decl_attributes (&decl, returned_attrs, 0);
4177 return decl;
4180 /* Detect the case of an array type of unspecified size
4181 which came, as such, direct from a typedef name.
4182 We must copy the type, so that each identifier gets
4183 a distinct type, so that each identifier's size can be
4184 controlled separately by its own initializer. */
4186 if (type != 0 && typedef_type != 0
4187 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4188 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4190 type = build_array_type (TREE_TYPE (type), 0);
4191 if (size_varies)
4192 C_TYPE_VARIABLE_SIZE (type) = 1;
4195 /* If this is a type name (such as, in a cast or sizeof),
4196 compute the type and return it now. */
4198 if (decl_context == TYPENAME)
4200 /* Note that the grammar rejects storage classes
4201 in typenames, fields or parameters */
4202 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4203 && type_quals)
4204 pedwarn ("ISO C forbids const or volatile function types");
4205 if (type_quals)
4206 type = c_build_qualified_type (type, type_quals);
4207 decl_attributes (&type, returned_attrs, 0);
4208 return type;
4211 /* Aside from typedefs and type names (handle above),
4212 `void' at top level (not within pointer)
4213 is allowed only in public variables.
4214 We don't complain about parms either, but that is because
4215 a better error message can be made later. */
4217 if (VOID_TYPE_P (type) && decl_context != PARM
4218 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4219 && ((specbits & (1 << (int) RID_EXTERN))
4220 || (current_scope == global_scope
4221 && !(specbits
4222 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4224 error ("variable or field `%s' declared void", name);
4225 type = integer_type_node;
4228 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4229 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4232 tree decl;
4234 if (decl_context == PARM)
4236 tree type_as_written;
4237 tree promoted_type;
4239 /* A parameter declared as an array of T is really a pointer to T.
4240 One declared as a function is really a pointer to a function. */
4242 if (TREE_CODE (type) == ARRAY_TYPE)
4244 /* Transfer const-ness of array into that of type pointed to. */
4245 type = TREE_TYPE (type);
4246 if (type_quals)
4247 type = c_build_qualified_type (type, type_quals);
4248 type = build_pointer_type (type);
4249 type_quals = TYPE_UNQUALIFIED;
4250 if (array_ptr_quals)
4252 tree new_ptr_quals, new_ptr_attrs;
4253 int erred = 0;
4254 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4255 /* We don't yet implement attributes in this context. */
4256 if (new_ptr_attrs != NULL_TREE)
4257 warning ("attributes in parameter array declarator ignored");
4259 constp = 0;
4260 volatilep = 0;
4261 restrictp = 0;
4262 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4264 tree qualifier = TREE_VALUE (new_ptr_quals);
4266 if (C_IS_RESERVED_WORD (qualifier))
4268 if (C_RID_CODE (qualifier) == RID_CONST)
4269 constp++;
4270 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4271 volatilep++;
4272 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4273 restrictp++;
4274 else
4275 erred++;
4277 else
4278 erred++;
4281 if (erred)
4282 error ("invalid type modifier within array declarator");
4284 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4285 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4286 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4288 size_varies = 0;
4290 else if (TREE_CODE (type) == FUNCTION_TYPE)
4292 if (pedantic && type_quals)
4293 pedwarn ("ISO C forbids qualified function types");
4294 if (type_quals)
4295 type = c_build_qualified_type (type, type_quals);
4296 type = build_pointer_type (type);
4297 type_quals = TYPE_UNQUALIFIED;
4299 else if (type_quals)
4300 type = c_build_qualified_type (type, type_quals);
4302 type_as_written = type;
4304 decl = build_decl (PARM_DECL, declarator, type);
4305 if (size_varies)
4306 C_DECL_VARIABLE_SIZE (decl) = 1;
4308 /* Compute the type actually passed in the parmlist,
4309 for the case where there is no prototype.
4310 (For example, shorts and chars are passed as ints.)
4311 When there is a prototype, this is overridden later. */
4313 if (type == error_mark_node)
4314 promoted_type = type;
4315 else
4316 promoted_type = c_type_promotes_to (type);
4318 DECL_ARG_TYPE (decl) = promoted_type;
4319 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4321 else if (decl_context == FIELD)
4323 /* Structure field. It may not be a function. */
4325 if (TREE_CODE (type) == FUNCTION_TYPE)
4327 error ("field `%s' declared as a function", name);
4328 type = build_pointer_type (type);
4330 else if (TREE_CODE (type) != ERROR_MARK
4331 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4333 error ("field `%s' has incomplete type", name);
4334 type = error_mark_node;
4336 /* Move type qualifiers down to element of an array. */
4337 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4338 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4339 type_quals),
4340 TYPE_DOMAIN (type));
4341 decl = build_decl (FIELD_DECL, declarator, type);
4342 DECL_NONADDRESSABLE_P (decl) = bitfield;
4344 if (size_varies)
4345 C_DECL_VARIABLE_SIZE (decl) = 1;
4347 else if (TREE_CODE (type) == FUNCTION_TYPE)
4349 /* Every function declaration is "external"
4350 except for those which are inside a function body
4351 in which `auto' is used.
4352 That is a case not specified by ANSI C,
4353 and we use it for forward declarations for nested functions. */
4354 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4355 || current_scope == global_scope);
4357 if (specbits & (1 << (int) RID_AUTO)
4358 && (pedantic || current_scope == global_scope))
4359 pedwarn ("invalid storage class for function `%s'", name);
4360 if (specbits & (1 << (int) RID_REGISTER))
4361 error ("invalid storage class for function `%s'", name);
4362 if (specbits & (1 << (int) RID_THREAD))
4363 error ("invalid storage class for function `%s'", name);
4364 /* Function declaration not at file scope.
4365 Storage classes other than `extern' are not allowed
4366 and `extern' makes no difference. */
4367 if (current_scope != global_scope
4368 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4369 && pedantic)
4370 pedwarn ("invalid storage class for function `%s'", name);
4372 decl = build_decl (FUNCTION_DECL, declarator, type);
4373 decl = build_decl_attribute_variant (decl, decl_attr);
4375 DECL_LANG_SPECIFIC (decl)
4376 = ggc_alloc_cleared (sizeof (struct lang_decl));
4378 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4379 pedwarn ("ISO C forbids qualified function types");
4381 /* GNU C interprets a `volatile void' return type to indicate
4382 that the function does not return. */
4383 if ((type_quals & TYPE_QUAL_VOLATILE)
4384 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4385 warning ("`noreturn' function returns non-void value");
4387 if (extern_ref)
4388 DECL_EXTERNAL (decl) = 1;
4389 /* Record absence of global scope for `static' or `auto'. */
4390 TREE_PUBLIC (decl)
4391 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4393 if (defaulted_int)
4394 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4396 /* Record presence of `inline', if it is reasonable. */
4397 if (MAIN_NAME_P (declarator))
4399 if (inlinep)
4400 warning ("cannot inline function `main'");
4402 else if (inlinep)
4404 /* Record that the function is declared `inline'. */
4405 DECL_DECLARED_INLINE_P (decl) = 1;
4407 /* Do not mark bare declarations as DECL_INLINE. Doing so
4408 in the presence of multiple declarations can result in
4409 the abstract origin pointing between the declarations,
4410 which will confuse dwarf2out. */
4411 if (initialized)
4413 DECL_INLINE (decl) = 1;
4414 if (specbits & (1 << (int) RID_EXTERN))
4415 current_extern_inline = 1;
4418 /* If -finline-functions, assume it can be inlined. This does
4419 two things: let the function be deferred until it is actually
4420 needed, and let dwarf2 know that the function is inlinable. */
4421 else if (flag_inline_trees == 2 && initialized)
4422 DECL_INLINE (decl) = 1;
4424 else
4426 /* It's a variable. */
4427 /* An uninitialized decl with `extern' is a reference. */
4428 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4430 /* Move type qualifiers down to element of an array. */
4431 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4433 int saved_align = TYPE_ALIGN(type);
4434 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4435 type_quals),
4436 TYPE_DOMAIN (type));
4437 TYPE_ALIGN (type) = saved_align;
4439 else if (type_quals)
4440 type = c_build_qualified_type (type, type_quals);
4442 /* It is invalid to create an `extern' declaration for a
4443 variable if there is a global declaration that is
4444 `static'. */
4445 if (extern_ref && current_scope != global_scope)
4447 tree global_decl;
4449 global_decl = identifier_global_value (declarator);
4450 if (global_decl
4451 && TREE_CODE (global_decl) == VAR_DECL
4452 && !TREE_PUBLIC (global_decl))
4453 error ("variable previously declared `static' redeclared "
4454 "`extern'");
4457 decl = build_decl (VAR_DECL, declarator, type);
4458 if (size_varies)
4459 C_DECL_VARIABLE_SIZE (decl) = 1;
4461 if (inlinep)
4462 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4464 DECL_EXTERNAL (decl) = extern_ref;
4466 /* At file scope, the presence of a `static' or `register' storage
4467 class specifier, or the absence of all storage class specifiers
4468 makes this declaration a definition (perhaps tentative). Also,
4469 the absence of both `static' and `register' makes it public. */
4470 if (current_scope == global_scope)
4472 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4473 | (1 << (int) RID_REGISTER)));
4474 TREE_STATIC (decl) = !extern_ref;
4476 /* Not at file scope, only `static' makes a static definition. */
4477 else
4479 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4480 TREE_PUBLIC (decl) = extern_ref;
4483 if (specbits & 1 << (int) RID_THREAD)
4485 if (targetm.have_tls)
4486 DECL_THREAD_LOCAL (decl) = 1;
4487 else
4488 /* A mere warning is sure to result in improper semantics
4489 at runtime. Don't bother to allow this to compile. */
4490 error ("thread-local storage not supported for this target");
4494 /* Record `register' declaration for warnings on &
4495 and in case doing stupid register allocation. */
4497 if (specbits & (1 << (int) RID_REGISTER))
4498 DECL_REGISTER (decl) = 1;
4500 /* Record constancy and volatility. */
4501 c_apply_type_quals_to_decl (type_quals, decl);
4503 /* If a type has volatile components, it should be stored in memory.
4504 Otherwise, the fact that those components are volatile
4505 will be ignored, and would even crash the compiler. */
4506 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4507 c_mark_addressable (decl);
4509 #ifdef ENABLE_CHECKING
4510 /* This is the earliest point at which we might know the assembler
4511 name of a variable. Thus, if it's known before this, die horribly. */
4512 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4513 abort ();
4514 #endif
4516 decl_attributes (&decl, returned_attrs, 0);
4518 return decl;
4522 /* Decode the parameter-list info for a function type or function definition.
4523 The argument is the value returned by `get_parm_info' (or made in parse.y
4524 if there is an identifier list instead of a parameter decl list).
4525 These two functions are separate because when a function returns
4526 or receives functions then each is called multiple times but the order
4527 of calls is different. The last call to `grokparms' is always the one
4528 that contains the formal parameter names of a function definition.
4530 Store in `last_function_parms' a chain of the decls of parms.
4531 Also store in `last_function_parm_tags' a chain of the struct, union,
4532 and enum tags declared among the parms.
4534 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4536 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4537 a mere declaration. A nonempty identifier-list gets an error message
4538 when FUNCDEF_FLAG is zero. */
4540 static tree
4541 grokparms (tree parms_info, int funcdef_flag)
4543 tree first_parm = TREE_CHAIN (parms_info);
4545 last_function_parms = TREE_PURPOSE (parms_info);
4546 last_function_parm_tags = TREE_VALUE (parms_info);
4547 last_function_parm_others = TREE_TYPE (parms_info);
4549 if (warn_strict_prototypes && first_parm == 0 && !funcdef_flag
4550 && !in_system_header)
4551 warning ("function declaration isn't a prototype");
4553 if (first_parm != 0
4554 && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
4556 if (! funcdef_flag)
4557 pedwarn ("parameter names (without types) in function declaration");
4559 last_function_parms = first_parm;
4560 return 0;
4562 else
4564 tree parm;
4565 tree typelt;
4566 /* If the arg types are incomplete in a declaration,
4567 they must include undefined tags.
4568 These tags can never be defined in the scope of the declaration,
4569 so the types can never be completed,
4570 and no call can be compiled successfully. */
4572 for (parm = last_function_parms, typelt = first_parm;
4573 parm;
4574 parm = TREE_CHAIN (parm))
4575 /* Skip over any enumeration constants declared here. */
4576 if (TREE_CODE (parm) == PARM_DECL)
4578 /* Barf if the parameter itself has an incomplete type. */
4579 tree type = TREE_VALUE (typelt);
4580 if (type == error_mark_node)
4581 continue;
4582 if (!COMPLETE_TYPE_P (type))
4584 if (funcdef_flag && DECL_NAME (parm) != 0)
4585 error ("parameter `%s' has incomplete type",
4586 IDENTIFIER_POINTER (DECL_NAME (parm)));
4587 else
4588 warning ("parameter has incomplete type");
4589 if (funcdef_flag)
4591 TREE_VALUE (typelt) = error_mark_node;
4592 TREE_TYPE (parm) = error_mark_node;
4595 typelt = TREE_CHAIN (typelt);
4598 return first_parm;
4602 /* Return a tree_list node with info on a parameter list just parsed.
4603 The TREE_PURPOSE is a list of decls of those parms.
4604 The TREE_VALUE is a list of structure, union and enum tags defined.
4605 The TREE_CHAIN is a list of argument types to go in the FUNCTION_TYPE.
4606 The TREE_TYPE is a list of non-parameter decls which appeared with the
4607 parameters.
4608 This tree_list node is later fed to `grokparms'.
4610 VOID_AT_END nonzero means append `void' to the end of the type-list.
4611 Zero means the parmlist ended with an ellipsis so don't append `void'. */
4613 tree
4614 get_parm_info (int void_at_end)
4616 tree decl, type, list;
4617 tree types = 0;
4618 tree *last_type = &types;
4619 tree tags = current_scope->tags;
4620 tree parms = current_scope->parms;
4621 tree others = current_scope->names;
4622 static bool explained_incomplete_types = false;
4623 bool gave_void_only_once_err = false;
4625 /* Just "void" (and no ellipsis) is special. There are really no parms.
4626 But if the "void" is qualified (by "const" or "volatile"), or has a
4627 storage class specifier ("register"), then the behavior is undefined;
4628 issue an error. Typedefs for "void" are OK (see DR#157). */
4629 if (void_at_end && parms != 0
4630 && TREE_CHAIN (parms) == 0
4631 && VOID_TYPE_P (TREE_TYPE (parms))
4632 && !DECL_NAME (parms))
4634 if (TREE_THIS_VOLATILE (parms)
4635 || TREE_READONLY (parms)
4636 || DECL_REGISTER (parms))
4637 error ("\"void\" as only parameter may not be qualified");
4639 return tree_cons (0, 0, tree_cons (0, void_type_node, 0));
4642 /* Sanity check all of the parameter declarations. */
4643 for (decl = parms; decl; decl = TREE_CHAIN (decl))
4645 if (TREE_CODE (decl) != PARM_DECL)
4646 abort ();
4647 if (TREE_ASM_WRITTEN (decl))
4648 abort ();
4650 /* Since there is a prototype, args are passed in their
4651 declared types. The back end may override this. */
4652 type = TREE_TYPE (decl);
4653 DECL_ARG_TYPE (decl) = type;
4655 /* Check for (..., void, ...) and issue an error. */
4656 if (VOID_TYPE_P (type) && !DECL_NAME (decl) && !gave_void_only_once_err)
4658 error ("\"void\" must be the only parameter");
4659 gave_void_only_once_err = true;
4662 type = build_tree_list (0, type);
4663 *last_type = type;
4664 last_type = &TREE_CHAIN (type);
4667 /* Check the list of non-parameter decls for any forward parm decls
4668 that never got real decls. */
4669 for (decl = others; decl; decl = TREE_CHAIN (decl))
4670 if (TREE_CODE (decl) == PARM_DECL)
4672 if (!TREE_ASM_WRITTEN (decl))
4673 abort ();
4675 error ("%Jparameter \"%D\" has just a forward declaration",
4676 decl, decl);
4679 /* Warn about any struct, union or enum tags defined within this
4680 list. The scope of such types is limited to this declaration,
4681 which is rarely if ever desirable (it's impossible to call such
4682 a function with type-correct arguments). */
4683 for (decl = tags; decl; decl = TREE_CHAIN (decl))
4685 enum tree_code code = TREE_CODE (TREE_VALUE (decl));
4686 const char *keyword;
4687 /* An anonymous union parm type is meaningful as a GNU extension.
4688 So don't warn for that. */
4689 if (code == UNION_TYPE && TREE_PURPOSE (decl) == 0 && !pedantic)
4690 continue;
4692 /* The keyword should not be translated. */
4693 switch (code)
4695 case RECORD_TYPE: keyword = "struct"; break;
4696 case UNION_TYPE: keyword = "union"; break;
4697 case ENUMERAL_TYPE: keyword = "enum"; break;
4698 default: abort ();
4701 if (TREE_PURPOSE (decl))
4702 /* The first %s will be one of 'struct', 'union', or 'enum'. */
4703 warning ("\"%s %s\" declared inside parameter list",
4704 keyword, IDENTIFIER_POINTER (TREE_PURPOSE (decl)));
4705 else
4706 /* The %s will be one of 'struct', 'union', or 'enum'. */
4707 warning ("anonymous %s declared inside parameter list", keyword);
4709 if (! explained_incomplete_types)
4711 warning ("its scope is only this definition or declaration,"
4712 " which is probably not what you want");
4713 explained_incomplete_types = true;
4718 if (void_at_end)
4720 type = build_tree_list (0, void_type_node);
4721 *last_type = type;
4724 list = tree_cons (parms, tags, types);
4725 TREE_TYPE (list) = others;
4726 return list;
4729 /* Get the struct, enum or union (CODE says which) with tag NAME.
4730 Define the tag as a forward-reference if it is not defined. */
4732 tree
4733 xref_tag (enum tree_code code, tree name)
4735 /* If a cross reference is requested, look up the type
4736 already defined for this tag and return it. */
4738 tree ref = lookup_tag (code, name, 0);
4739 /* If this is the right type of tag, return what we found.
4740 (This reference will be shadowed by shadow_tag later if appropriate.)
4741 If this is the wrong type of tag, do not return it. If it was the
4742 wrong type in the same scope, we will have had an error
4743 message already; if in a different scope and declaring
4744 a name, pending_xref_error will give an error message; but if in a
4745 different scope and not declaring a name, this tag should
4746 shadow the previous declaration of a different type of tag, and
4747 this would not work properly if we return the reference found.
4748 (For example, with "struct foo" in an outer scope, "union foo;"
4749 must shadow that tag with a new one of union type.) */
4750 if (ref && TREE_CODE (ref) == code)
4751 return ref;
4753 /* If no such tag is yet defined, create a forward-reference node
4754 and record it as the "definition".
4755 When a real declaration of this type is found,
4756 the forward-reference will be altered into a real type. */
4758 ref = make_node (code);
4759 if (code == ENUMERAL_TYPE)
4761 /* Give the type a default layout like unsigned int
4762 to avoid crashing if it does not get defined. */
4763 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4764 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4765 TYPE_USER_ALIGN (ref) = 0;
4766 TREE_UNSIGNED (ref) = 1;
4767 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4768 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4769 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4772 pushtag (name, ref);
4774 return ref;
4777 /* Make sure that the tag NAME is defined *in the current scope*
4778 at least as a forward reference.
4779 CODE says which kind of tag NAME ought to be. */
4781 tree
4782 start_struct (enum tree_code code, tree name)
4784 /* If there is already a tag defined at this scope
4785 (as a forward reference), just return it. */
4787 tree ref = 0;
4789 if (name != 0)
4790 ref = lookup_tag (code, name, 1);
4791 if (ref && TREE_CODE (ref) == code)
4793 if (TYPE_FIELDS (ref))
4795 if (code == UNION_TYPE)
4796 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4797 else
4798 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4801 else
4803 /* Otherwise create a forward-reference just so the tag is in scope. */
4805 ref = make_node (code);
4806 pushtag (name, ref);
4809 C_TYPE_BEING_DEFINED (ref) = 1;
4810 TYPE_PACKED (ref) = flag_pack_struct;
4811 return ref;
4814 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4815 of a structure component, returning a FIELD_DECL node.
4816 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4818 This is done during the parsing of the struct declaration.
4819 The FIELD_DECL nodes are chained together and the lot of them
4820 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4822 tree
4823 grokfield (tree declarator, tree declspecs, tree width)
4825 tree value;
4827 if (declarator == NULL_TREE && width == NULL_TREE)
4829 /* This is an unnamed decl.
4831 If we have something of the form "union { list } ;" then this
4832 is the anonymous union extension. Similarly for struct.
4834 If this is something of the form "struct foo;", then
4835 If MS extensions are enabled, this is handled as an
4836 anonymous struct.
4837 Otherwise this is a forward declaration of a structure tag.
4839 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4840 If MS extensions are enabled and foo names a structure, then
4841 again this is an anonymous struct.
4842 Otherwise this is an error.
4844 Oh what a horrid tangled web we weave. I wonder if MS consciously
4845 took this from Plan 9 or if it was an accident of implementation
4846 that took root before someone noticed the bug... */
4848 tree type = TREE_VALUE (declspecs);
4850 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4851 type = TREE_TYPE (type);
4852 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4854 if (flag_ms_extensions)
4855 ; /* ok */
4856 else if (flag_iso)
4857 goto warn_unnamed_field;
4858 else if (TYPE_NAME (type) == NULL)
4859 ; /* ok */
4860 else
4861 goto warn_unnamed_field;
4863 else
4865 warn_unnamed_field:
4866 warning ("declaration does not declare anything");
4867 return NULL_TREE;
4871 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4872 width ? &width : NULL);
4874 finish_decl (value, NULL_TREE, NULL_TREE);
4875 DECL_INITIAL (value) = width;
4877 return value;
4880 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4881 the list such that this does not present a problem later. */
4883 static void
4884 detect_field_duplicates (tree fieldlist)
4886 tree x, y;
4887 int timeout = 10;
4889 /* First, see if there are more than "a few" fields.
4890 This is trivially true if there are zero or one fields. */
4891 if (!fieldlist)
4892 return;
4893 x = TREE_CHAIN (fieldlist);
4894 if (!x)
4895 return;
4896 do {
4897 timeout--;
4898 x = TREE_CHAIN (x);
4899 } while (timeout > 0 && x);
4901 /* If there were "few" fields, avoid the overhead of allocating
4902 a hash table. Instead just do the nested traversal thing. */
4903 if (timeout > 0)
4905 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
4906 if (DECL_NAME (x))
4908 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
4909 if (DECL_NAME (y) == DECL_NAME (x))
4911 error ("%Jduplicate member '%D'", x, x);
4912 DECL_NAME (x) = NULL_TREE;
4916 else
4918 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
4919 void **slot;
4921 for (x = fieldlist; x ; x = TREE_CHAIN (x))
4922 if ((y = DECL_NAME (x)) != 0)
4924 slot = htab_find_slot (htab, y, INSERT);
4925 if (*slot)
4927 error ("%Jduplicate member '%D'", x, x);
4928 DECL_NAME (x) = NULL_TREE;
4930 *slot = y;
4933 htab_delete (htab);
4937 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
4938 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
4939 ATTRIBUTES are attributes to be applied to the structure. */
4941 tree
4942 finish_struct (tree t, tree fieldlist, tree attributes)
4944 tree x;
4945 int toplevel = global_scope == current_scope;
4946 int saw_named_field;
4948 /* If this type was previously laid out as a forward reference,
4949 make sure we lay it out again. */
4951 TYPE_SIZE (t) = 0;
4953 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
4955 /* Nameless union parm types are useful as GCC extension. */
4956 if (! (TREE_CODE (t) == UNION_TYPE && TYPE_NAME (t) == 0) && !pedantic)
4957 /* Otherwise, warn about any struct or union def. in parmlist. */
4958 if (in_parm_level_p ())
4960 if (pedantic)
4961 pedwarn ("%s defined inside parms",
4962 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4963 else
4964 warning ("%s defined inside parms",
4965 TREE_CODE (t) == UNION_TYPE ? _("union") : _("structure"));
4968 if (pedantic)
4970 for (x = fieldlist; x; x = TREE_CHAIN (x))
4971 if (DECL_NAME (x) != 0)
4972 break;
4974 if (x == 0)
4975 pedwarn ("%s has no %s",
4976 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
4977 fieldlist ? _("named members") : _("members"));
4980 /* Install struct as DECL_CONTEXT of each field decl.
4981 Also process specified field sizes,m which is found in the DECL_INITIAL.
4982 Store 0 there, except for ": 0" fields (so we can find them
4983 and delete them, below). */
4985 saw_named_field = 0;
4986 for (x = fieldlist; x; x = TREE_CHAIN (x))
4988 DECL_CONTEXT (x) = t;
4989 DECL_PACKED (x) |= TYPE_PACKED (t);
4991 /* If any field is const, the structure type is pseudo-const. */
4992 if (TREE_READONLY (x))
4993 C_TYPE_FIELDS_READONLY (t) = 1;
4994 else
4996 /* A field that is pseudo-const makes the structure likewise. */
4997 tree t1 = TREE_TYPE (x);
4998 while (TREE_CODE (t1) == ARRAY_TYPE)
4999 t1 = TREE_TYPE (t1);
5000 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5001 && C_TYPE_FIELDS_READONLY (t1))
5002 C_TYPE_FIELDS_READONLY (t) = 1;
5005 /* Any field that is volatile means variables of this type must be
5006 treated in some ways as volatile. */
5007 if (TREE_THIS_VOLATILE (x))
5008 C_TYPE_FIELDS_VOLATILE (t) = 1;
5010 /* Any field of nominal variable size implies structure is too. */
5011 if (C_DECL_VARIABLE_SIZE (x))
5012 C_TYPE_VARIABLE_SIZE (t) = 1;
5014 /* Detect invalid nested redefinition. */
5015 if (TREE_TYPE (x) == t)
5016 error ("nested redefinition of `%s'",
5017 IDENTIFIER_POINTER (TYPE_NAME (t)));
5019 if (DECL_INITIAL (x))
5021 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5022 DECL_SIZE (x) = bitsize_int (width);
5023 DECL_BIT_FIELD (x) = 1;
5024 SET_DECL_C_BIT_FIELD (x);
5027 DECL_INITIAL (x) = 0;
5029 /* Detect flexible array member in an invalid context. */
5030 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5031 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5032 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5033 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5035 if (TREE_CODE (t) == UNION_TYPE)
5036 error ("%Jflexible array member in union", x);
5037 else if (TREE_CHAIN (x) != NULL_TREE)
5038 error ("%Jflexible array member not at end of struct", x);
5039 else if (! saw_named_field)
5040 error ("%Jflexible array member in otherwise empty struct", x);
5043 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5044 && flexible_array_type_p (TREE_TYPE (x)))
5045 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5047 if (DECL_NAME (x))
5048 saw_named_field = 1;
5051 detect_field_duplicates (fieldlist);
5053 /* Now we have the nearly final fieldlist. Record it,
5054 then lay out the structure or union (including the fields). */
5056 TYPE_FIELDS (t) = fieldlist;
5058 layout_type (t);
5060 /* Delete all zero-width bit-fields from the fieldlist. */
5062 tree *fieldlistp = &fieldlist;
5063 while (*fieldlistp)
5064 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5065 *fieldlistp = TREE_CHAIN (*fieldlistp);
5066 else
5067 fieldlistp = &TREE_CHAIN (*fieldlistp);
5070 /* Now we have the truly final field list.
5071 Store it in this type and in the variants. */
5073 TYPE_FIELDS (t) = fieldlist;
5075 /* If there are lots of fields, sort so we can look through them fast.
5076 We arbitrarily consider 16 or more elts to be "a lot". */
5079 int len = 0;
5081 for (x = fieldlist; x; x = TREE_CHAIN (x))
5083 if (len > 15 || DECL_NAME (x) == NULL)
5084 break;
5085 len += 1;
5088 if (len > 15)
5090 tree *field_array;
5091 struct lang_type *space;
5092 struct sorted_fields_type *space2;
5094 len += list_length (x);
5096 /* Use the same allocation policy here that make_node uses, to
5097 ensure that this lives as long as the rest of the struct decl.
5098 All decls in an inline function need to be saved. */
5100 space = ggc_alloc (sizeof (struct lang_type));
5101 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5103 len = 0;
5104 space->s = space2;
5105 field_array = &space2->elts[0];
5106 for (x = fieldlist; x; x = TREE_CHAIN (x))
5108 field_array[len++] = x;
5110 /* If there is anonymous struct or union, break out of the loop. */
5111 if (DECL_NAME (x) == NULL)
5112 break;
5114 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5115 if (x == NULL)
5117 TYPE_LANG_SPECIFIC (t) = space;
5118 TYPE_LANG_SPECIFIC (t)->s->len = len;
5119 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5120 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5125 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5127 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5128 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5129 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5130 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5133 /* If this was supposed to be a transparent union, but we can't
5134 make it one, warn and turn off the flag. */
5135 if (TREE_CODE (t) == UNION_TYPE
5136 && TYPE_TRANSPARENT_UNION (t)
5137 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5139 TYPE_TRANSPARENT_UNION (t) = 0;
5140 warning ("union cannot be made transparent");
5143 /* If this structure or union completes the type of any previous
5144 variable declaration, lay it out and output its rtl. */
5145 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5147 x = TREE_CHAIN (x))
5149 tree decl = TREE_VALUE (x);
5150 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5151 layout_array_type (TREE_TYPE (decl));
5152 if (TREE_CODE (decl) != TYPE_DECL)
5154 layout_decl (decl, 0);
5155 if (c_dialect_objc ())
5156 objc_check_decl (decl);
5157 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5158 if (! toplevel)
5159 expand_decl (decl);
5162 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5164 /* Finish debugging output for this type. */
5165 rest_of_type_compilation (t, toplevel);
5167 return t;
5170 /* Lay out the type T, and its element type, and so on. */
5172 static void
5173 layout_array_type (tree t)
5175 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5176 layout_array_type (TREE_TYPE (t));
5177 layout_type (t);
5180 /* Begin compiling the definition of an enumeration type.
5181 NAME is its name (or null if anonymous).
5182 Returns the type object, as yet incomplete.
5183 Also records info about it so that build_enumerator
5184 may be used to declare the individual values as they are read. */
5186 tree
5187 start_enum (tree name)
5189 tree enumtype = 0;
5191 /* If this is the real definition for a previous forward reference,
5192 fill in the contents in the same object that used to be the
5193 forward reference. */
5195 if (name != 0)
5196 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5198 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5200 enumtype = make_node (ENUMERAL_TYPE);
5201 pushtag (name, enumtype);
5204 C_TYPE_BEING_DEFINED (enumtype) = 1;
5206 if (TYPE_VALUES (enumtype) != 0)
5208 /* This enum is a named one that has been declared already. */
5209 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5211 /* Completely replace its old definition.
5212 The old enumerators remain defined, however. */
5213 TYPE_VALUES (enumtype) = 0;
5216 enum_next_value = integer_zero_node;
5217 enum_overflow = 0;
5219 if (flag_short_enums)
5220 TYPE_PACKED (enumtype) = 1;
5222 return enumtype;
5225 /* After processing and defining all the values of an enumeration type,
5226 install their decls in the enumeration type and finish it off.
5227 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5228 and ATTRIBUTES are the specified attributes.
5229 Returns ENUMTYPE. */
5231 tree
5232 finish_enum (tree enumtype, tree values, tree attributes)
5234 tree pair, tem;
5235 tree minnode = 0, maxnode = 0, enum_value_type;
5236 int precision, unsign;
5237 int toplevel = (global_scope == current_scope);
5239 if (in_parm_level_p ())
5240 warning ("enum defined inside parms");
5242 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5244 /* Calculate the maximum value of any enumerator in this type. */
5246 if (values == error_mark_node)
5247 minnode = maxnode = integer_zero_node;
5248 else
5250 minnode = maxnode = TREE_VALUE (values);
5251 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5253 tree value = TREE_VALUE (pair);
5254 if (tree_int_cst_lt (maxnode, value))
5255 maxnode = value;
5256 if (tree_int_cst_lt (value, minnode))
5257 minnode = value;
5261 /* Construct the final type of this enumeration. It is the same
5262 as one of the integral types - the narrowest one that fits, except
5263 that normally we only go as narrow as int - and signed iff any of
5264 the values are negative. */
5265 unsign = (tree_int_cst_sgn (minnode) >= 0);
5266 precision = MAX (min_precision (minnode, unsign),
5267 min_precision (maxnode, unsign));
5268 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5270 tree narrowest = c_common_type_for_size (precision, unsign);
5271 if (narrowest == 0)
5273 warning ("enumeration values exceed range of largest integer");
5274 narrowest = long_long_integer_type_node;
5277 precision = TYPE_PRECISION (narrowest);
5279 else
5280 precision = TYPE_PRECISION (integer_type_node);
5282 if (precision == TYPE_PRECISION (integer_type_node))
5283 enum_value_type = c_common_type_for_size (precision, 0);
5284 else
5285 enum_value_type = enumtype;
5287 TYPE_MIN_VALUE (enumtype) = minnode;
5288 TYPE_MAX_VALUE (enumtype) = maxnode;
5289 TYPE_PRECISION (enumtype) = precision;
5290 TREE_UNSIGNED (enumtype) = unsign;
5291 TYPE_SIZE (enumtype) = 0;
5292 layout_type (enumtype);
5294 if (values != error_mark_node)
5296 /* Change the type of the enumerators to be the enum type. We
5297 need to do this irrespective of the size of the enum, for
5298 proper type checking. Replace the DECL_INITIALs of the
5299 enumerators, and the value slots of the list, with copies
5300 that have the enum type; they cannot be modified in place
5301 because they may be shared (e.g. integer_zero_node) Finally,
5302 change the purpose slots to point to the names of the decls. */
5303 for (pair = values; pair; pair = TREE_CHAIN (pair))
5305 tree enu = TREE_PURPOSE (pair);
5307 TREE_TYPE (enu) = enumtype;
5309 /* The ISO C Standard mandates enumerators to have type int,
5310 even though the underlying type of an enum type is
5311 unspecified. Here we convert any enumerators that fit in
5312 an int to type int, to avoid promotions to unsigned types
5313 when comparing integers with enumerators that fit in the
5314 int range. When -pedantic is given, build_enumerator()
5315 would have already taken care of those that don't fit. */
5316 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5317 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5318 else
5319 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5321 TREE_PURPOSE (pair) = DECL_NAME (enu);
5322 TREE_VALUE (pair) = DECL_INITIAL (enu);
5325 TYPE_VALUES (enumtype) = values;
5328 /* Fix up all variant types of this enum type. */
5329 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5331 if (tem == enumtype)
5332 continue;
5333 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5334 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5335 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5336 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5337 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5338 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5339 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5340 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5341 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5342 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5345 /* Finish debugging output for this type. */
5346 rest_of_type_compilation (enumtype, toplevel);
5348 return enumtype;
5351 /* Build and install a CONST_DECL for one value of the
5352 current enumeration type (one that was begun with start_enum).
5353 Return a tree-list containing the CONST_DECL and its value.
5354 Assignment of sequential values by default is handled here. */
5356 tree
5357 build_enumerator (tree name, tree value)
5359 tree decl, type;
5361 /* Validate and default VALUE. */
5363 /* Remove no-op casts from the value. */
5364 if (value)
5365 STRIP_TYPE_NOPS (value);
5367 if (value != 0)
5369 if (TREE_CODE (value) == INTEGER_CST)
5371 value = default_conversion (value);
5372 constant_expression_warning (value);
5374 else
5376 error ("enumerator value for `%s' not integer constant",
5377 IDENTIFIER_POINTER (name));
5378 value = 0;
5382 /* Default based on previous value. */
5383 /* It should no longer be possible to have NON_LVALUE_EXPR
5384 in the default. */
5385 if (value == 0)
5387 value = enum_next_value;
5388 if (enum_overflow)
5389 error ("overflow in enumeration values");
5392 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5394 pedwarn ("ISO C restricts enumerator values to range of `int'");
5395 value = convert (integer_type_node, value);
5398 /* Set basis for default for next value. */
5399 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5400 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5402 /* Now create a declaration for the enum value name. */
5404 type = TREE_TYPE (value);
5405 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5406 TYPE_PRECISION (integer_type_node)),
5407 (TYPE_PRECISION (type)
5408 >= TYPE_PRECISION (integer_type_node)
5409 && TREE_UNSIGNED (type)));
5411 decl = build_decl (CONST_DECL, name, type);
5412 DECL_INITIAL (decl) = convert (type, value);
5413 pushdecl (decl);
5415 return tree_cons (decl, value, NULL_TREE);
5419 /* Create the FUNCTION_DECL for a function definition.
5420 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5421 the declaration; they describe the function's name and the type it returns,
5422 but twisted together in a fashion that parallels the syntax of C.
5424 This function creates a binding context for the function body
5425 as well as setting up the FUNCTION_DECL in current_function_decl.
5427 Returns 1 on success. If the DECLARATOR is not suitable for a function
5428 (it defines a datum instead), we return 0, which tells
5429 yyparse to report a parse error. */
5432 start_function (tree declspecs, tree declarator, tree attributes)
5434 tree decl1, old_decl;
5435 tree restype;
5436 int old_immediate_size_expand = immediate_size_expand;
5438 current_function_returns_value = 0; /* Assume, until we see it does. */
5439 current_function_returns_null = 0;
5440 current_function_returns_abnormally = 0;
5441 warn_about_return_type = 0;
5442 current_extern_inline = 0;
5443 c_in_iteration_stmt = 0;
5444 c_in_case_stmt = 0;
5446 /* Don't expand any sizes in the return type of the function. */
5447 immediate_size_expand = 0;
5449 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5451 /* If the declarator is not suitable for a function definition,
5452 cause a syntax error. */
5453 if (decl1 == 0)
5455 immediate_size_expand = old_immediate_size_expand;
5456 return 0;
5459 decl_attributes (&decl1, attributes, 0);
5461 if (DECL_DECLARED_INLINE_P (decl1)
5462 && DECL_UNINLINABLE (decl1)
5463 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5464 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5466 announce_function (decl1);
5468 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5470 error ("return type is an incomplete type");
5471 /* Make it return void instead. */
5472 TREE_TYPE (decl1)
5473 = build_function_type (void_type_node,
5474 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5477 if (warn_about_return_type)
5478 pedwarn_c99 ("return type defaults to `int'");
5480 /* Save the parm names or decls from this function's declarator
5481 where store_parm_decls will find them. */
5482 current_function_parms = last_function_parms;
5483 current_function_parm_tags = last_function_parm_tags;
5484 current_function_parm_others = last_function_parm_others;
5486 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5487 error_mark_node is replaced below (in poplevel) with the BLOCK. */
5488 DECL_INITIAL (decl1) = error_mark_node;
5490 /* If this definition isn't a prototype and we had a prototype declaration
5491 before, copy the arg type info from that prototype.
5492 But not if what we had before was a builtin function. */
5493 old_decl = lookup_name_current_level (DECL_NAME (decl1));
5494 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5495 && !DECL_BUILT_IN (old_decl)
5496 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5497 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5498 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5500 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5501 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5504 /* Optionally warn of old-fashioned def with no previous prototype. */
5505 if (warn_strict_prototypes
5506 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5507 && C_DECL_ISNT_PROTOTYPE (old_decl))
5508 warning ("function declaration isn't a prototype");
5509 /* Optionally warn of any global def with no previous prototype. */
5510 else if (warn_missing_prototypes
5511 && TREE_PUBLIC (decl1)
5512 && ! MAIN_NAME_P (DECL_NAME (decl1))
5513 && C_DECL_ISNT_PROTOTYPE (old_decl))
5514 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5515 /* Optionally warn of any def with no previous prototype
5516 if the function has already been used. */
5517 else if (warn_missing_prototypes
5518 && old_decl != 0 && TREE_USED (old_decl)
5519 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5520 warning ("%J'%D' was used with no prototype before its definition",
5521 decl1, decl1);
5522 /* Optionally warn of any global def with no previous declaration. */
5523 else if (warn_missing_declarations
5524 && TREE_PUBLIC (decl1)
5525 && old_decl == 0
5526 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5527 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5528 /* Optionally warn of any def with no previous declaration
5529 if the function has already been used. */
5530 else if (warn_missing_declarations
5531 && old_decl != 0 && TREE_USED (old_decl)
5532 && C_DECL_IMPLICIT (old_decl))
5533 warning ("%J`%D' was used with no declaration before its definition",
5534 decl1, decl1);
5536 /* This is a definition, not a reference.
5537 So normally clear DECL_EXTERNAL.
5538 However, `extern inline' acts like a declaration
5539 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5540 DECL_EXTERNAL (decl1) = current_extern_inline;
5542 /* This function exists in static storage.
5543 (This does not mean `static' in the C sense!) */
5544 TREE_STATIC (decl1) = 1;
5546 /* A nested function is not global. */
5547 if (current_function_decl != 0)
5548 TREE_PUBLIC (decl1) = 0;
5550 #ifdef ENABLE_CHECKING
5551 /* This is the earliest point at which we might know the assembler
5552 name of the function. Thus, if it's set before this, die horribly. */
5553 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5554 abort ();
5555 #endif
5557 /* If #pragma weak was used, mark the decl weak now. */
5558 if (current_scope == global_scope)
5559 maybe_apply_pragma_weak (decl1);
5561 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5562 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5564 tree args;
5565 int argct = 0;
5567 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5568 != integer_type_node)
5569 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5571 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5572 args = TREE_CHAIN (args))
5574 tree type = args ? TREE_VALUE (args) : 0;
5576 if (type == void_type_node)
5577 break;
5579 ++argct;
5580 switch (argct)
5582 case 1:
5583 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5584 pedwarn ("%Jfirst argument of '%D' should be `int'",
5585 decl1, decl1);
5586 break;
5588 case 2:
5589 if (TREE_CODE (type) != POINTER_TYPE
5590 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5591 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5592 != char_type_node))
5593 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5594 decl1, decl1);
5595 break;
5597 case 3:
5598 if (TREE_CODE (type) != POINTER_TYPE
5599 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5600 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5601 != char_type_node))
5602 pedwarn ("%Jthird argument of '%D' should probably be "
5603 "'char **'", decl1, decl1);
5604 break;
5608 /* It is intentional that this message does not mention the third
5609 argument because it's only mentioned in an appendix of the
5610 standard. */
5611 if (argct > 0 && (argct < 2 || argct > 3))
5612 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5614 if (! TREE_PUBLIC (decl1))
5615 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5618 /* Record the decl so that the function name is defined.
5619 If we already have a decl for this name, and it is a FUNCTION_DECL,
5620 use the old decl. */
5622 current_function_decl = pushdecl (decl1);
5624 pushlevel (0);
5625 declare_parm_level ();
5627 make_decl_rtl (current_function_decl, NULL);
5629 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5630 /* Promote the value to int before returning it. */
5631 if (c_promoting_integer_type_p (restype))
5633 /* It retains unsignedness if not really getting wider. */
5634 if (TREE_UNSIGNED (restype)
5635 && (TYPE_PRECISION (restype)
5636 == TYPE_PRECISION (integer_type_node)))
5637 restype = unsigned_type_node;
5638 else
5639 restype = integer_type_node;
5641 DECL_RESULT (current_function_decl)
5642 = build_decl (RESULT_DECL, NULL_TREE, restype);
5644 /* If this fcn was already referenced via a block-scope `extern' decl
5645 (or an implicit decl), propagate certain information about the usage. */
5646 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5647 TREE_ADDRESSABLE (current_function_decl) = 1;
5649 immediate_size_expand = old_immediate_size_expand;
5651 start_fname_decls ();
5653 return 1;
5656 /* Subroutine of store_parm_decls which handles new-style function
5657 definitions (prototype format). The parms already have decls, so we
5658 need only record them as in effect and complain if any redundant
5659 old-style parm decls were written. */
5660 static void
5661 store_parm_decls_newstyle (void)
5663 tree decl, last;
5664 tree fndecl = current_function_decl;
5665 tree parms = current_function_parms;
5666 tree tags = current_function_parm_tags;
5667 tree others = current_function_parm_others;
5669 if (current_scope->parms || current_scope->names || current_scope->tags)
5671 error ("%Jold-style parameter declarations in prototyped "
5672 "function definition", fndecl);
5674 /* Get rid of the old-style declarations. */
5675 poplevel (0, 0, 0);
5676 pushlevel (0);
5679 /* Now make all the parameter declarations visible in the function body.
5680 We can bypass most of the grunt work of pushdecl. */
5681 for (last = 0, decl = parms; decl; last = decl, decl = TREE_CHAIN (decl))
5683 DECL_CONTEXT (decl) = current_function_decl;
5684 if (DECL_NAME (decl) == 0)
5685 error ("%Jparameter name omitted", decl);
5686 else
5688 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5689 current_scope->shadowed
5690 = tree_cons (DECL_NAME (decl),
5691 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5692 current_scope->shadowed);
5693 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5696 current_scope->parms = parms;
5697 current_scope->parms_last = last;
5699 /* Record the parameter list in the function declaration. */
5700 DECL_ARGUMENTS (fndecl) = parms;
5702 /* Now make all the ancillary declarations visible, likewise. */
5703 for (last = 0, decl = others; decl; last = decl, decl = TREE_CHAIN (decl))
5705 DECL_CONTEXT (decl) = current_function_decl;
5706 if (DECL_NAME (decl)
5707 && TYPE_MAIN_VARIANT (TREE_TYPE (decl)) != void_type_node)
5709 if (IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)))
5710 current_scope->shadowed
5711 = tree_cons (DECL_NAME (decl),
5712 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)),
5713 current_scope->shadowed);
5714 IDENTIFIER_SYMBOL_VALUE (DECL_NAME (decl)) = decl;
5717 current_scope->names = others;
5718 current_scope->names_last = last;
5720 /* And all the tag declarations. */
5721 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5722 if (TREE_PURPOSE (decl))
5724 if (IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)))
5725 current_scope->shadowed_tags
5726 = tree_cons (TREE_PURPOSE (decl),
5727 IDENTIFIER_SYMBOL_VALUE (TREE_PURPOSE (decl)),
5728 current_scope->shadowed_tags);
5729 IDENTIFIER_TAG_VALUE (TREE_PURPOSE (decl)) = TREE_VALUE (decl);
5731 current_scope->tags = tags;
5734 /* Subroutine of store_parm_decls which handles old-style function
5735 definitions (separate parameter list and declarations). */
5737 static void
5738 store_parm_decls_oldstyle (void)
5740 tree parm, decl, last;
5741 tree fndecl = current_function_decl;
5743 /* This is the identifier list from the function declarator. */
5744 tree parmids = current_function_parms;
5746 /* We use DECL_WEAK as a flag to show which parameters have been
5747 seen already, since it is not used on PARM_DECL. */
5748 #ifdef ENABLE_CHECKING
5749 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5750 if (DECL_WEAK (parm))
5751 abort ();
5752 #endif
5754 /* Match each formal parameter name with its declaration. Save each
5755 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5756 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5758 if (TREE_VALUE (parm) == 0)
5760 error ("%Jparameter name missing from parameter list", fndecl);
5761 TREE_PURPOSE (parm) = 0;
5762 continue;
5765 decl = IDENTIFIER_SYMBOL_VALUE (TREE_VALUE (parm));
5766 if (decl && DECL_CONTEXT (decl) == fndecl)
5768 /* If we got something other than a PARM_DECL it is an error. */
5769 if (TREE_CODE (decl) != PARM_DECL)
5770 error ("%J\"%D\" declared as a non-parameter", decl, decl);
5771 /* If the declaration is already marked, we have a duplicate
5772 name. Complain and ignore the duplicate. */
5773 else if (DECL_WEAK (decl))
5775 error ("%Jmultiple parameters named \"%D\"", decl, decl);
5776 TREE_PURPOSE (parm) = 0;
5777 continue;
5779 /* If the declaration says "void", complain and turn it into
5780 an int. */
5781 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5783 error ("%Jparameter \"%D\" declared void", decl, decl);
5784 TREE_TYPE (decl) = integer_type_node;
5785 DECL_ARG_TYPE (decl) = integer_type_node;
5786 layout_decl (decl, 0);
5789 /* If no declaration found, default to int. */
5790 else
5792 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5793 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5794 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5795 pushdecl (decl);
5797 if (flag_isoc99)
5798 pedwarn ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5799 else if (extra_warnings)
5800 warning ("%Jtype of \"%D\" defaults to \"int\"", decl, decl);
5803 TREE_PURPOSE (parm) = decl;
5804 DECL_WEAK (decl) = 1;
5807 /* Now examine the parms chain for incomplete declarations
5808 and declarations with no corresponding names. */
5810 for (parm = current_scope->parms; parm; parm = TREE_CHAIN (parm))
5812 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5814 error ("%Jparameter \"%D\" has incomplete type", parm, parm);
5815 TREE_TYPE (parm) = error_mark_node;
5818 if (! DECL_WEAK (parm))
5820 error ("%Jdeclaration for parameter \"%D\" but no such parameter",
5821 parm, parm);
5823 /* Pretend the parameter was not missing.
5824 This gets us to a standard state and minimizes
5825 further error messages. */
5826 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5830 /* Chain the declarations together in the order of the list of
5831 names. Store that chain in the function decl, replacing the
5832 list of names. Update the current scope to match. */
5833 DECL_ARGUMENTS (fndecl) = 0;
5835 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5836 if (TREE_PURPOSE (parm))
5837 break;
5838 if (parm && TREE_PURPOSE (parm))
5840 last = TREE_PURPOSE (parm);
5841 DECL_ARGUMENTS (fndecl) = last;
5842 current_scope->parms = last;
5843 DECL_WEAK (last) = 0;
5845 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5846 if (TREE_PURPOSE (parm))
5848 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5849 last = TREE_PURPOSE (parm);
5850 DECL_WEAK (last) = 0;
5852 current_scope->parms_last = last;
5853 TREE_CHAIN (last) = 0;
5856 /* If there was a previous prototype,
5857 set the DECL_ARG_TYPE of each argument according to
5858 the type previously specified, and report any mismatches. */
5860 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5862 tree type;
5863 for (parm = DECL_ARGUMENTS (fndecl),
5864 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5865 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5866 != void_type_node));
5867 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5869 if (parm == 0 || type == 0
5870 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5872 error ("number of arguments doesn't match prototype");
5873 error ("%Hprototype declaration",
5874 &current_function_prototype_locus);
5875 break;
5877 /* Type for passing arg must be consistent with that
5878 declared for the arg. ISO C says we take the unqualified
5879 type for parameters declared with qualified type. */
5880 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5881 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5882 COMPARE_STRICT))
5884 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5885 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5887 /* Adjust argument to match prototype. E.g. a previous
5888 `int foo(float);' prototype causes
5889 `int foo(x) float x; {...}' to be treated like
5890 `int foo(float x) {...}'. This is particularly
5891 useful for argument types like uid_t. */
5892 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5894 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5895 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5896 && TYPE_PRECISION (TREE_TYPE (parm))
5897 < TYPE_PRECISION (integer_type_node))
5898 DECL_ARG_TYPE (parm) = integer_type_node;
5900 if (pedantic)
5902 pedwarn ("promoted argument \"%D\" "
5903 "doesn't match prototype", parm);
5904 pedwarn ("%Hprototype declaration",
5905 &current_function_prototype_locus);
5908 else
5910 error ("argument \"%D\" doesn't match prototype", parm);
5911 error ("%Hprototype declaration",
5912 &current_function_prototype_locus);
5916 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5919 /* Otherwise, create a prototype that would match. */
5921 else
5923 tree actual = 0, last = 0, type;
5925 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
5927 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
5928 if (last)
5929 TREE_CHAIN (last) = type;
5930 else
5931 actual = type;
5932 last = type;
5934 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
5935 if (last)
5936 TREE_CHAIN (last) = type;
5937 else
5938 actual = type;
5940 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
5941 of the type of this function, but we need to avoid having this
5942 affect the types of other similarly-typed functions, so we must
5943 first force the generation of an identical (but separate) type
5944 node for the relevant function type. The new node we create
5945 will be a variant of the main variant of the original function
5946 type. */
5948 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
5950 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
5954 /* Store the parameter declarations into the current function declaration.
5955 This is called after parsing the parameter declarations, before
5956 digesting the body of the function.
5958 For an old-style definition, construct a prototype out of the old-style
5959 parameter declarations and inject it into the function's type. */
5961 void
5962 store_parm_decls (void)
5964 tree fndecl = current_function_decl;
5966 /* The function containing FNDECL, if any. */
5967 tree context = decl_function_context (fndecl);
5969 /* True if this definition is written with a prototype. */
5970 bool prototype = (current_function_parms
5971 && TREE_CODE (current_function_parms) != TREE_LIST);
5973 if (prototype)
5974 store_parm_decls_newstyle ();
5975 else
5976 store_parm_decls_oldstyle ();
5978 /* The next call to pushlevel will be a function body. */
5980 next_is_function_body = true;
5982 /* Write a record describing this function definition to the prototypes
5983 file (if requested). */
5985 gen_aux_info_record (fndecl, 1, 0, prototype);
5987 /* Initialize the RTL code for the function. */
5988 allocate_struct_function (fndecl);
5990 /* Begin the statement tree for this function. */
5991 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
5993 /* If this is a nested function, save away the sizes of any
5994 variable-size types so that we can expand them when generating
5995 RTL. */
5996 if (context)
5998 tree t;
6000 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6001 = nreverse (get_pending_sizes ());
6002 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6004 t = TREE_CHAIN (t))
6005 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6008 /* This function is being processed in whole-function mode. */
6009 cfun->x_whole_function_mode_p = 1;
6011 /* Even though we're inside a function body, we still don't want to
6012 call expand_expr to calculate the size of a variable-sized array.
6013 We haven't necessarily assigned RTL to all variables yet, so it's
6014 not safe to try to expand expressions involving them. */
6015 immediate_size_expand = 0;
6016 cfun->x_dont_save_pending_sizes_p = 1;
6019 /* Finish up a function declaration and compile that function
6020 all the way to assembler language output. The free the storage
6021 for the function definition.
6023 This is called after parsing the body of the function definition. */
6025 void
6026 finish_function (void)
6028 tree fndecl = current_function_decl;
6030 /* When a function declaration is totally empty, e.g.
6031 void foo(void) { }
6032 (the argument list is irrelevant) the compstmt rule will not
6033 bother calling pushlevel/poplevel, which means we get here with
6034 the scope stack out of sync. Detect this situation by noticing
6035 that current_scope is still as store_parm_decls left it, and do
6036 a dummy push/pop to get back to consistency.
6037 Note that the call to pushlevel does not actually push another
6038 scope - see there for details. */
6040 if (current_scope->parm_flag && next_is_function_body)
6042 pushlevel (0);
6043 poplevel (0, 0, 0);
6046 if (TREE_CODE (fndecl) == FUNCTION_DECL
6047 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6049 tree args = DECL_ARGUMENTS (fndecl);
6050 for (; args; args = TREE_CHAIN (args))
6052 tree type = TREE_TYPE (args);
6053 if (INTEGRAL_TYPE_P (type)
6054 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6055 DECL_ARG_TYPE (args) = integer_type_node;
6059 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6060 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6062 /* Must mark the RESULT_DECL as being in this function. */
6064 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6065 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6067 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6069 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6070 != integer_type_node)
6072 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6073 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6074 if (!warn_main)
6075 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6077 else
6079 #ifdef DEFAULT_MAIN_RETURN
6080 /* Make it so that `main' always returns success by default. */
6081 DEFAULT_MAIN_RETURN;
6082 #else
6083 if (flag_isoc99)
6084 c_expand_return (integer_zero_node);
6085 #endif
6089 finish_fname_decls ();
6091 /* Tie off the statement tree for this function. */
6092 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6094 /* Complain if there's just no return statement. */
6095 if (warn_return_type
6096 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6097 && !current_function_returns_value && !current_function_returns_null
6098 /* Don't complain if we abort. */
6099 && !current_function_returns_abnormally
6100 /* Don't warn for main(). */
6101 && !MAIN_NAME_P (DECL_NAME (fndecl))
6102 /* Or if they didn't actually specify a return type. */
6103 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6104 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6105 inline function, as we might never be compiled separately. */
6106 && DECL_INLINE (fndecl))
6107 warning ("no return statement in function returning non-void");
6109 /* With just -Wextra, complain only if function returns both with
6110 and without a value. */
6111 if (extra_warnings
6112 && current_function_returns_value
6113 && current_function_returns_null)
6114 warning ("this function may return with or without a value");
6116 /* We're leaving the context of this function, so zap cfun. It's still in
6117 DECL_SAVED_INSNS, and we'll restore it in tree_rest_of_compilation. */
6118 cfun = NULL;
6120 /* ??? Objc emits functions after finalizing the compilation unit.
6121 This should be cleaned up later and this conditional removed. */
6122 if (!cgraph_global_info_ready)
6123 cgraph_finalize_function (fndecl, false);
6124 else
6125 c_expand_body (fndecl);
6126 current_function_decl = NULL;
6129 /* Generate the RTL for a deferred function FNDECL. */
6131 void
6132 c_expand_deferred_function (tree fndecl)
6134 /* DECL_INLINE or DECL_RESULT might got cleared after the inline
6135 function was deferred, e.g. in duplicate_decls. */
6136 if (DECL_INLINE (fndecl) && DECL_RESULT (fndecl))
6138 if (flag_inline_trees)
6140 timevar_push (TV_INTEGRATION);
6141 optimize_inline_calls (fndecl);
6142 timevar_pop (TV_INTEGRATION);
6144 c_expand_body (fndecl);
6145 current_function_decl = NULL;
6149 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6150 then we are already in the process of generating RTL for another
6151 function. */
6153 static void
6154 c_expand_body_1 (tree fndecl, int nested_p)
6156 if (nested_p)
6158 /* Make sure that we will evaluate variable-sized types involved
6159 in our function's type. */
6160 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6162 /* Squirrel away our current state. */
6163 push_function_context ();
6166 tree_rest_of_compilation (fndecl, nested_p);
6168 if (nested_p)
6169 /* Return to the enclosing function. */
6170 pop_function_context ();
6172 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6174 if (targetm.have_ctors_dtors)
6175 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (fndecl), 0),
6176 DEFAULT_INIT_PRIORITY);
6177 else
6178 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6181 if (DECL_STATIC_DESTRUCTOR (fndecl))
6183 if (targetm.have_ctors_dtors)
6184 (* targetm.asm_out.destructor) (XEXP (DECL_RTL (fndecl), 0),
6185 DEFAULT_INIT_PRIORITY);
6186 else
6187 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6191 /* Like c_expand_body_1 but only for unnested functions. */
6193 void
6194 c_expand_body (tree fndecl)
6197 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6198 c_expand_body_1 (fndecl, 0);
6201 /* Check the declarations given in a for-loop for satisfying the C99
6202 constraints. */
6203 void
6204 check_for_loop_decls (void)
6206 tree t;
6208 if (!flag_isoc99)
6210 /* If we get here, declarations have been used in a for loop without
6211 the C99 for loop scope. This doesn't make much sense, so don't
6212 allow it. */
6213 error ("'for' loop initial declaration used outside C99 mode");
6214 return;
6216 /* C99 subclause 6.8.5 paragraph 3:
6218 [#3] The declaration part of a for statement shall only
6219 declare identifiers for objects having storage class auto or
6220 register.
6222 It isn't clear whether, in this sentence, "identifiers" binds to
6223 "shall only declare" or to "objects" - that is, whether all identifiers
6224 declared must be identifiers for objects, or whether the restriction
6225 only applies to those that are. (A question on this in comp.std.c
6226 in November 2000 received no answer.) We implement the strictest
6227 interpretation, to avoid creating an extension which later causes
6228 problems. */
6230 for (t = current_scope->tags; t; t = TREE_CHAIN (t))
6232 if (TREE_PURPOSE (t) != 0)
6234 enum tree_code code = TREE_CODE (TREE_VALUE (t));
6236 if (code == RECORD_TYPE)
6237 error ("'struct %s' declared in 'for' loop initial declaration",
6238 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6239 else if (code == UNION_TYPE)
6240 error ("'union %s' declared in 'for' loop initial declaration",
6241 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6242 else
6243 error ("'enum %s' declared in 'for' loop initial declaration",
6244 IDENTIFIER_POINTER (TREE_PURPOSE (t)));
6248 for (t = getdecls (); t; t = TREE_CHAIN (t))
6250 if (TREE_CODE (t) != VAR_DECL && DECL_NAME (t))
6251 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6252 "initial declaration", t, t);
6253 else if (TREE_STATIC (t))
6254 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6255 "initial declaration", t, t);
6256 else if (DECL_EXTERNAL (t))
6257 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6258 "initial declaration", t, t);
6262 /* Save and reinitialize the variables
6263 used during compilation of a C function. */
6265 void
6266 c_push_function_context (struct function *f)
6268 struct language_function *p;
6269 p = ggc_alloc (sizeof (struct language_function));
6270 f->language = p;
6272 p->base.x_stmt_tree = c_stmt_tree;
6273 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6274 p->x_in_iteration_stmt = c_in_iteration_stmt;
6275 p->x_in_case_stmt = c_in_case_stmt;
6276 p->returns_value = current_function_returns_value;
6277 p->returns_null = current_function_returns_null;
6278 p->returns_abnormally = current_function_returns_abnormally;
6279 p->warn_about_return_type = warn_about_return_type;
6280 p->extern_inline = current_extern_inline;
6283 /* Restore the variables used during compilation of a C function. */
6285 void
6286 c_pop_function_context (struct function *f)
6288 struct language_function *p = f->language;
6290 if (DECL_SAVED_INSNS (current_function_decl) == 0
6291 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6293 /* Stop pointing to the local nodes about to be freed. */
6294 /* But DECL_INITIAL must remain nonzero so we know this
6295 was an actual function definition. */
6296 DECL_INITIAL (current_function_decl) = error_mark_node;
6297 DECL_ARGUMENTS (current_function_decl) = 0;
6300 c_stmt_tree = p->base.x_stmt_tree;
6301 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6302 c_in_iteration_stmt = p->x_in_iteration_stmt;
6303 c_in_case_stmt = p->x_in_case_stmt;
6304 current_function_returns_value = p->returns_value;
6305 current_function_returns_null = p->returns_null;
6306 current_function_returns_abnormally = p->returns_abnormally;
6307 warn_about_return_type = p->warn_about_return_type;
6308 current_extern_inline = p->extern_inline;
6310 f->language = NULL;
6313 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6315 void
6316 c_dup_lang_specific_decl (tree decl)
6318 struct lang_decl *ld;
6320 if (!DECL_LANG_SPECIFIC (decl))
6321 return;
6323 ld = ggc_alloc (sizeof (struct lang_decl));
6324 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6325 DECL_LANG_SPECIFIC (decl) = ld;
6328 /* The functions below are required for functionality of doing
6329 function at once processing in the C front end. Currently these
6330 functions are not called from anywhere in the C front end, but as
6331 these changes continue, that will change. */
6333 /* Returns nonzero if the current statement is a full expression,
6334 i.e. temporaries created during that statement should be destroyed
6335 at the end of the statement. */
6338 stmts_are_full_exprs_p (void)
6340 return 0;
6343 /* Returns the stmt_tree (if any) to which statements are currently
6344 being added. If there is no active statement-tree, NULL is
6345 returned. */
6347 stmt_tree
6348 current_stmt_tree (void)
6350 return &c_stmt_tree;
6353 /* Returns the stack of SCOPE_STMTs for the current function. */
6355 tree *
6356 current_scope_stmt_stack (void)
6358 return &c_scope_stmt_stack;
6361 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6362 C. */
6365 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6367 return 0;
6370 /* Dummy function in place of callback used by C++. */
6372 void
6373 extract_interface_info (void)
6377 /* Return a new COMPOUND_STMT, after adding it to the current
6378 statement tree. */
6380 tree
6381 c_begin_compound_stmt (void)
6383 tree stmt;
6385 /* Create the COMPOUND_STMT. */
6386 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6388 return stmt;
6391 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6392 common code. */
6394 void
6395 c_expand_decl_stmt (tree t)
6397 tree decl = DECL_STMT_DECL (t);
6399 /* Expand nested functions. */
6400 if (TREE_CODE (decl) == FUNCTION_DECL
6401 && DECL_CONTEXT (decl) == current_function_decl
6402 && DECL_SAVED_TREE (decl))
6403 c_expand_body_1 (decl, 1);
6406 /* Return the global value of T as a symbol. */
6408 tree
6409 identifier_global_value (tree t)
6411 tree decl = IDENTIFIER_SYMBOL_VALUE (t);
6412 if (decl == 0 || DECL_FILE_SCOPE_P (decl))
6413 return decl;
6415 /* Shadowed by something else; find the true global value. */
6416 for (decl = global_scope->names; decl; decl = TREE_CHAIN (decl))
6417 if (DECL_NAME (decl) == t)
6418 return decl;
6420 /* Only local values for this decl. */
6421 return 0;
6424 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6425 otherwise the name is found in ridpointers from RID_INDEX. */
6427 void
6428 record_builtin_type (enum rid rid_index, const char *name, tree type)
6430 tree id;
6431 if (name == 0)
6432 id = ridpointers[(int) rid_index];
6433 else
6434 id = get_identifier (name);
6435 pushdecl (build_decl (TYPE_DECL, id, type));
6438 /* Build the void_list_node (void_type_node having been created). */
6439 tree
6440 build_void_list_node (void)
6442 tree t = build_tree_list (NULL_TREE, void_type_node);
6443 return t;
6446 /* Return something to represent absolute declarators containing a *.
6447 TARGET is the absolute declarator that the * contains.
6448 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6449 to apply to the pointer type, represented as identifiers, possible mixed
6450 with attributes.
6452 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6453 if attributes are present) and whose type is the modifier list. */
6455 tree
6456 make_pointer_declarator (tree type_quals_attrs, tree target)
6458 tree quals, attrs;
6459 tree itarget = target;
6460 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6461 if (attrs != NULL_TREE)
6462 itarget = tree_cons (attrs, target, NULL_TREE);
6463 return build1 (INDIRECT_REF, quals, itarget);
6466 /* A wrapper around lhd_set_decl_assembler_name that gives static
6467 variables their C names if they are at file scope and only one
6468 translation unit is being compiled, for backwards compatibility
6469 with certain bizarre assembler hacks (like crtstuff.c). */
6471 void
6472 c_static_assembler_name (tree decl)
6474 if (num_in_fnames == 1
6475 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6476 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6477 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6478 else
6479 lhd_set_decl_assembler_name (decl);
6482 /* Hash and equality functions for link_hash_table: key off
6483 DECL_ASSEMBLER_NAME. */
6485 static hashval_t
6486 link_hash_hash (const void *x_p)
6488 tree x = (tree)x_p;
6489 return (hashval_t) (long)DECL_ASSEMBLER_NAME (x);
6492 static int
6493 link_hash_eq (const void *x1_p, const void *x2_p)
6495 tree x1 = (tree)x1_p;
6496 tree x2 = (tree)x2_p;
6497 return DECL_ASSEMBLER_NAME (x1) == DECL_ASSEMBLER_NAME (x2);
6500 /* Propagate information between definitions and uses between multiple
6501 translation units in TU_LIST based on linkage rules. */
6503 void
6504 merge_translation_unit_decls (void)
6506 const tree tu_list = current_file_decl;
6507 tree tu;
6508 tree decl;
6509 htab_t link_hash_table;
6510 tree block;
6512 /* Create the BLOCK that poplevel would have created, but don't
6513 actually call poplevel since that's expensive. */
6514 block = make_node (BLOCK);
6515 BLOCK_VARS (block) = current_scope->names;
6516 TREE_USED (block) = 1;
6517 DECL_INITIAL (current_file_decl) = block;
6519 /* If only one translation unit seen, no copying necessary. */
6520 if (TREE_CHAIN (tu_list) == NULL_TREE)
6521 return;
6523 link_hash_table = htab_create (1021, link_hash_hash, link_hash_eq, NULL);
6525 /* Enter any actual definitions into the hash table. */
6526 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6527 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6528 if (TREE_PUBLIC (decl) && ! DECL_EXTERNAL (decl))
6530 PTR *slot;
6531 slot = htab_find_slot (link_hash_table, decl, INSERT);
6533 /* If we've already got a definition, work out which one is
6534 the real one, put it into the hash table, and make the
6535 other one DECL_EXTERNAL. This is important to avoid
6536 putting out two definitions of the same symbol in the
6537 assembly output. */
6538 if (*slot != NULL)
6540 tree old_decl = (tree) *slot;
6542 /* If this is weak or common or whatever, suppress it
6543 in favor of the other definition. */
6544 if (DECL_WEAK (decl))
6545 DECL_EXTERNAL (decl) = 1;
6546 else if (DECL_WEAK (old_decl) && ! DECL_WEAK (decl))
6547 DECL_EXTERNAL (old_decl) = 1;
6548 else if (DECL_COMMON (decl) || DECL_ONE_ONLY (decl))
6549 DECL_EXTERNAL (decl) = 1;
6550 else if (DECL_COMMON (old_decl) || DECL_ONE_ONLY (old_decl))
6551 DECL_EXTERNAL (old_decl) = 1;
6553 if (DECL_EXTERNAL (decl))
6555 DECL_INITIAL (decl) = NULL_TREE;
6556 DECL_COMMON (decl) = 0;
6557 DECL_ONE_ONLY (decl) = 0;
6558 DECL_WEAK (decl) = 0;
6560 else if (DECL_EXTERNAL (old_decl))
6562 DECL_INITIAL (old_decl) = NULL_TREE;
6563 DECL_COMMON (old_decl) = 0;
6564 DECL_ONE_ONLY (old_decl) = 0;
6565 DECL_WEAK (old_decl) = 0;
6566 *slot = decl;
6568 else
6570 error ("%Jredefinition of global '%D'", decl, decl);
6571 error ("%J'%D' previously defined here", old_decl, old_decl);
6574 else
6575 *slot = decl;
6578 /* Now insert the desired information from all the definitions
6579 into any plain declarations. */
6580 for (tu = tu_list; tu; tu = TREE_CHAIN (tu))
6581 for (decl = BLOCK_VARS (DECL_INITIAL (tu)); decl; decl = TREE_CHAIN (decl))
6582 if (TREE_PUBLIC (decl) && DECL_EXTERNAL (decl))
6584 tree global_decl;
6585 global_decl = htab_find (link_hash_table, decl);
6587 if (! global_decl)
6588 continue;
6590 /* Print any appropriate error messages, and partially merge
6591 the decls. */
6592 (void) duplicate_decls (decl, global_decl, true, true);
6595 htab_delete (link_hash_table);
6598 /* Perform final processing on file-scope data. */
6600 void
6601 c_write_global_declarations(void)
6603 tree link;
6605 for (link = current_file_decl; link; link = TREE_CHAIN (link))
6607 tree globals = BLOCK_VARS (DECL_INITIAL (link));
6608 int len = list_length (globals);
6609 tree *vec = xmalloc (sizeof (tree) * len);
6610 int i;
6611 tree decl;
6613 /* Process the decls in the order they were written. */
6615 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6616 vec[i] = decl;
6618 wrapup_global_declarations (vec, len);
6620 check_global_declarations (vec, len);
6622 /* Clean up. */
6623 free (vec);
6627 /* Reset the parser's state in preparation for a new file. */
6629 void
6630 c_reset_state (void)
6632 tree link;
6633 tree file_scope_decl;
6635 /* Pop the global scope. */
6636 if (current_scope != global_scope)
6637 current_scope = global_scope;
6638 file_scope_decl = current_file_decl;
6639 DECL_INITIAL (file_scope_decl) = poplevel (1, 0, 0);
6640 BLOCK_SUPERCONTEXT (DECL_INITIAL (file_scope_decl)) = file_scope_decl;
6641 truly_local_externals = NULL_TREE;
6643 /* Start a new global binding level. */
6644 pushlevel (0);
6645 global_scope = current_scope;
6646 current_file_decl = build_decl (TRANSLATION_UNIT_DECL, NULL, NULL);
6647 TREE_CHAIN (current_file_decl) = file_scope_decl;
6649 /* Reintroduce the builtin declarations. */
6650 for (link = first_builtin_decl;
6651 link != TREE_CHAIN (last_builtin_decl);
6652 link = TREE_CHAIN (link))
6653 pushdecl (copy_node (link));
6656 #include "gt-c-decl.h"