* doc/extend.texi (ARM Built-in Functions): Replace with correct
[official-gcc.git] / gcc / c-decl.c
blobe74347997c5780bfbf0d3f552f49a8d29836f4a7
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 /* These #defines are for clarity in working with the information block
87 returned by get_parm_info. */
88 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
89 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
90 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
91 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
93 /* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
97 static location_t current_function_prototype_locus;
99 /* The current statement tree. */
101 static GTY(()) struct stmt_tree_s c_stmt_tree;
103 /* The current scope statement stack. */
105 static GTY(()) tree c_scope_stmt_stack;
107 /* State saving variables. */
108 int c_in_iteration_stmt;
109 int c_in_case_stmt;
111 /* A DECL for the current file-scope context. */
113 static GTY(()) tree current_file_decl;
115 /* A list of decls to be made automatically visible in each file scope. */
116 static GTY(()) tree visible_builtins;
118 /* Set to 0 at beginning of a function definition, set to 1 if
119 a return statement that specifies a return value is seen. */
121 int current_function_returns_value;
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement with no argument is seen. */
126 int current_function_returns_null;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a call to a noreturn function is seen. */
131 int current_function_returns_abnormally;
133 /* Set to nonzero by `grokdeclarator' for a function
134 whose return type is defaulted, if warnings for this are desired. */
136 static int warn_about_return_type;
138 /* Nonzero when starting a function declared `extern inline'. */
140 static int current_extern_inline;
142 /* True means global_bindings_p should return false even if the scope stack
143 says we are in file scope. */
144 bool c_override_global_bindings_to_false;
147 /* Each c_binding structure describes one binding of an identifier to
148 a decl. All the decls in a scope - irrespective of namespace - are
149 chained together by the ->prev field, which (as the name implies)
150 runs in reverse order. All the decls in a given namespace bound to
151 a given identifier are chained by the ->shadowed field, which runs
152 from inner to outer scopes. Finally, the ->contour field points
153 back to the relevant scope structure; this is mainly used to make
154 decls in the externals scope invisible (see below).
156 The ->decl field usually points to a DECL node, but there are two
157 exceptions. In the namespace of type tags, the bound entity is a
158 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
159 identifier is encountered, it is bound to error_mark_node to
160 suppress further errors about that identifier in the current
161 function. */
163 struct c_binding GTY(())
165 tree decl; /* the decl bound */
166 tree id; /* the identifier it's bound to */
167 struct c_binding *prev; /* the previous decl in this scope */
168 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
169 struct c_scope *contour; /* the scope in which this decl is bound */
172 #define I_SYMBOL_BINDING(node) \
173 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
174 #define I_SYMBOL_DECL(node) \
175 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
177 #define I_TAG_BINDING(node) \
178 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
179 #define I_TAG_DECL(node) \
180 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
182 #define I_LABEL_BINDING(node) \
183 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
184 #define I_LABEL_DECL(node) \
185 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
187 /* Each c_scope structure describes the complete contents of one
188 scope. Four scopes are distinguished specially: the innermost or
189 current scope, the innermost function scope, the file scope (always
190 the second to outermost) and the outermost or external scope.
192 Most declarations are recorded in the current scope.
194 All normal label declarations are recorded in the innermost
195 function scope, as are bindings of undeclared identifiers to
196 error_mark_node. (GCC permits nested functions as an extension,
197 hence the 'innermost' qualifier.) Explicitly declared labels
198 (using the __label__ extension) appear in the current scope.
200 Being in the file scope (current_scope == file_scope) causes
201 special behavior in several places below. Also, under some
202 conditions the Objective-C front end records declarations in the
203 file scope even though that isn't the current scope.
205 All declarations with external linkage are recorded in the external
206 scope, even if they aren't visible there; this models the fact that
207 such declarations are visible to the entire program, and (with a
208 bit of cleverness, see pushdecl) allows diagnosis of some violations
209 of C99 6.2.2p7 and 6.2.7p2:
211 If, within the same translation unit, the same identifier appears
212 with both internal and external linkage, the behavior is
213 undefined.
215 All declarations that refer to the same object or function shall
216 have compatible type; otherwise, the behavior is undefined.
218 Initially only the built-in declarations, which describe compiler
219 intrinsic functions plus a subset of the standard library, are in
220 this scope.
222 The order of the blocks list matters, and it is frequently appended
223 to. To avoid having to walk all the way to the end of the list on
224 each insertion, or reverse the list later, we maintain a pointer to
225 the last list entry. (FIXME: It should be feasible to use a reversed
226 list here.)
228 The bindings list is strictly in reverse order of declarations;
229 pop_scope relies on this. */
232 struct c_scope GTY(())
234 /* The scope containing this one. */
235 struct c_scope *outer;
237 /* The next outermost function scope. */
238 struct c_scope *outer_function;
240 /* All bindings in this scope. */
241 struct c_binding *bindings;
243 /* For each scope (except the global one), a chain of BLOCK nodes
244 for all the scopes that were entered and exited one level down. */
245 tree blocks;
246 tree blocks_last;
248 /* The depth of this scope. Used to keep the ->shadowed chain of
249 bindings sorted innermost to outermost. */
250 unsigned int depth : 28;
252 /* True if we are currently filling this scope with parameter
253 declarations. */
254 BOOL_BITFIELD parm_flag : 1;
256 /* True if we already complained about forward parameter decls
257 in this scope. This prevents double warnings on
258 foo (int a; int b; ...) */
259 BOOL_BITFIELD warned_forward_parm_decls : 1;
261 /* True if this is the outermost block scope of a function body.
262 This scope contains the parameters, the local variables declared
263 in the outermost block, and all the labels (except those in
264 nested functions, or declared at block scope with __label__). */
265 BOOL_BITFIELD function_body : 1;
267 /* True means make a BLOCK for this scope no matter what. */
268 BOOL_BITFIELD keep : 1;
271 /* The scope currently in effect. */
273 static GTY(()) struct c_scope *current_scope;
275 /* The innermost function scope. Ordinary (not explicitly declared)
276 labels, bindings to error_mark_node, and the lazily-created
277 bindings of __func__ and its friends get this scope. */
279 static GTY(()) struct c_scope *current_function_scope;
281 /* The C file scope. This is reset for each input translation unit. */
283 static GTY(()) struct c_scope *file_scope;
285 /* The outermost scope. This is used for all declarations with
286 external linkage, and only these, hence the name. */
288 static GTY(()) struct c_scope *external_scope;
290 /* A chain of c_scope structures awaiting reuse. */
292 static GTY((deletable (""))) struct c_scope *scope_freelist;
294 /* A chain of c_binding structures awaiting reuse. */
296 static GTY((deletable (""))) struct c_binding *binding_freelist;
298 /* Append VAR to LIST in scope SCOPE. */
299 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
300 struct c_scope *s_ = (scope); \
301 tree d_ = (decl); \
302 if (s_->list##_last) \
303 TREE_CHAIN (s_->list##_last) = d_; \
304 else \
305 s_->list = d_; \
306 s_->list##_last = d_; \
307 } while (0)
309 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
310 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
311 struct c_scope *t_ = (tscope); \
312 struct c_scope *f_ = (fscope); \
313 if (t_->to##_last) \
314 TREE_CHAIN (t_->to##_last) = f_->from; \
315 else \
316 t_->to = f_->from; \
317 t_->to##_last = f_->from##_last; \
318 } while (0)
320 /* True means unconditionally make a BLOCK for the next scope pushed. */
322 static bool keep_next_level_flag;
324 /* True means the next call to push_scope will be the outermost scope
325 of a function body, so do not push a new scope, merely cease
326 expecting parameter decls. */
328 static bool next_is_function_body;
330 /* Functions called automatically at the beginning and end of execution. */
332 tree static_ctors, static_dtors;
334 /* Forward declarations. */
335 static tree lookup_name_in_scope (tree, struct c_scope *);
336 static tree c_make_fname_decl (tree, int);
337 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
338 static tree grokparms (tree, int);
339 static void layout_array_type (tree);
341 /* States indicating how grokdeclarator() should handle declspecs marked
342 with __attribute__((deprecated)). An object declared as
343 __attribute__((deprecated)) suppresses warnings of uses of other
344 deprecated items. */
346 enum deprecated_states {
347 DEPRECATED_NORMAL,
348 DEPRECATED_SUPPRESS
351 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
353 void
354 c_print_identifier (FILE *file, tree node, int indent)
356 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
357 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
358 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
359 if (C_IS_RESERVED_WORD (node))
361 tree rid = ridpointers[C_RID_CODE (node)];
362 indent_to (file, indent + 4);
363 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
364 (void *) rid, IDENTIFIER_POINTER (rid));
368 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
369 which may be any of several kinds of DECL or TYPE or error_mark_node,
370 in the scope SCOPE. */
371 static void
372 bind (tree name, tree decl, struct c_scope *scope)
374 struct c_binding *b, **here;
376 if (binding_freelist)
378 b = binding_freelist;
379 binding_freelist = b->prev;
381 else
382 b = ggc_alloc (sizeof (struct c_binding));
384 b->shadowed = 0;
385 b->decl = decl;
386 b->id = name;
387 b->contour = scope;
389 b->prev = scope->bindings;
390 scope->bindings = b;
392 if (!name)
393 return;
395 switch (TREE_CODE (decl))
397 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
398 case ENUMERAL_TYPE:
399 case UNION_TYPE:
400 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
401 case VAR_DECL:
402 case FUNCTION_DECL:
403 case TYPE_DECL:
404 case CONST_DECL:
405 case PARM_DECL:
406 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
408 default:
409 abort ();
412 /* Locate the appropriate place in the chain of shadowed decls
413 to insert this binding. Normally, scope == current_scope and
414 this does nothing. */
415 while (*here && (*here)->contour->depth > scope->depth)
416 here = &(*here)->shadowed;
418 b->shadowed = *here;
419 *here = b;
422 /* Clear the binding structure B, stick it on the binding_freelist,
423 and return the former value of b->prev. This is used by pop_scope
424 and get_parm_info to iterate destructively over all the bindings
425 from a given scope. */
426 static struct c_binding *
427 free_binding_and_advance (struct c_binding *b)
429 struct c_binding *prev = b->prev;
431 b->id = 0;
432 b->decl = 0;
433 b->contour = 0;
434 b->shadowed = 0;
435 b->prev = binding_freelist;
436 binding_freelist = b;
438 return prev;
442 /* Hook called at end of compilation to assume 1 elt
443 for a file-scope tentative array defn that wasn't complete before. */
445 void
446 c_finish_incomplete_decl (tree decl)
448 if (TREE_CODE (decl) == VAR_DECL)
450 tree type = TREE_TYPE (decl);
451 if (type != error_mark_node
452 && TREE_CODE (type) == ARRAY_TYPE
453 && ! DECL_EXTERNAL (decl)
454 && TYPE_DOMAIN (type) == 0)
456 warning ("%Jarray '%D' assumed to have one element", decl, decl);
458 complete_array_type (type, NULL_TREE, 1);
460 layout_decl (decl, 0);
465 /* The Objective-C front-end often needs to determine the current scope. */
467 void *
468 get_current_scope (void)
470 return current_scope;
473 /* The following function is used only by Objective-C. It needs to live here
474 because it accesses the innards of c_scope. */
476 void
477 objc_mark_locals_volatile (void *enclosing_blk)
479 struct c_scope *scope;
480 struct c_binding *b;
482 for (scope = current_scope;
483 scope && scope != enclosing_blk;
484 scope = scope->outer)
486 for (b = scope->bindings; b; b = b->prev)
488 if (TREE_CODE (b->decl) == VAR_DECL
489 || TREE_CODE (b->decl) == PARM_DECL)
491 DECL_REGISTER (b->decl) = 0;
492 TREE_THIS_VOLATILE (b->decl) = 1;
496 /* Do not climb up past the current function. */
497 if (scope->function_body)
498 break;
502 /* Nonzero if we are currently in file scope. */
505 global_bindings_p (void)
507 return current_scope == file_scope && !c_override_global_bindings_to_false;
510 void
511 keep_next_level (void)
513 keep_next_level_flag = true;
516 /* Identify this scope as currently being filled with parameters. */
518 void
519 declare_parm_level (void)
521 current_scope->parm_flag = true;
524 void
525 push_scope (void)
527 if (next_is_function_body)
529 /* This is the transition from the parameters to the top level
530 of the function body. These are the same scope
531 (C99 6.2.1p4,6) so we do not push another scope structure.
532 next_is_function_body is set only by store_parm_decls, which
533 in turn is called when and only when we are about to
534 encounter the opening curly brace for the function body.
536 The outermost block of a function always gets a BLOCK node,
537 because the debugging output routines expect that each
538 function has at least one BLOCK. */
539 current_scope->parm_flag = false;
540 current_scope->function_body = true;
541 current_scope->keep = true;
542 current_scope->outer_function = current_function_scope;
543 current_function_scope = current_scope;
545 keep_next_level_flag = false;
546 next_is_function_body = false;
548 else
550 struct c_scope *scope;
551 if (scope_freelist)
553 scope = scope_freelist;
554 scope_freelist = scope->outer;
556 else
557 scope = ggc_alloc_cleared (sizeof (struct c_scope));
559 scope->keep = keep_next_level_flag;
560 scope->outer = current_scope;
561 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
563 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
564 possible. */
565 if (current_scope && scope->depth == 0)
567 scope->depth--;
568 sorry ("GCC supports only %u nested scopes\n", scope->depth);
571 current_scope = scope;
572 keep_next_level_flag = false;
576 /* Exit a scope. Restore the state of the identifier-decl mappings
577 that were in effect when this scope was entered. Return a BLOCK
578 node containing all the DECLs in this scope that are of interest
579 to debug info generation. */
581 tree
582 pop_scope (void)
584 struct c_scope *scope = current_scope;
585 tree block, context, p;
586 struct c_binding *b;
588 bool functionbody = scope->function_body;
589 bool keep = functionbody || scope->keep || scope->bindings;
591 /* If appropriate, create a BLOCK to record the decls for the life
592 of this function. */
593 block = 0;
594 if (keep)
596 block = make_node (BLOCK);
597 BLOCK_SUBBLOCKS (block) = scope->blocks;
598 TREE_USED (block) = 1;
600 /* In each subblock, record that this is its superior. */
601 for (p = scope->blocks; p; p = TREE_CHAIN (p))
602 BLOCK_SUPERCONTEXT (p) = block;
604 BLOCK_VARS (block) = 0;
607 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
608 scope must be set so that they point to the appropriate
609 construct, i.e. either to the current FUNCTION_DECL node, or
610 else to the BLOCK node we just constructed.
612 Note that for tagged types whose scope is just the formal
613 parameter list for some function type specification, we can't
614 properly set their TYPE_CONTEXTs here, because we don't have a
615 pointer to the appropriate FUNCTION_TYPE node readily available
616 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
617 type nodes get set in `grokdeclarator' as soon as we have created
618 the FUNCTION_TYPE node which will represent the "scope" for these
619 "parameter list local" tagged types. */
620 if (scope->function_body)
621 context = current_function_decl;
622 else if (scope == file_scope)
623 context = current_file_decl;
624 else
625 context = block;
627 /* Clear all bindings in this scope. */
628 for (b = scope->bindings; b; b = free_binding_and_advance (b))
630 p = b->decl;
631 switch (TREE_CODE (p))
633 case LABEL_DECL:
634 /* Warnings for unused labels, errors for undefined labels. */
635 if (TREE_USED (p) && !DECL_INITIAL (p))
637 error ("%Jlabel `%D' used but not defined", p, p);
638 DECL_INITIAL (p) = error_mark_node;
640 else if (!TREE_USED (p) && warn_unused_label)
642 if (DECL_INITIAL (p))
643 warning ("%Jlabel `%D' defined but not used", p, p);
644 else
645 warning ("%Jlabel `%D' declared but not defined", p, p);
647 /* Labels go in BLOCK_VARS. */
648 TREE_CHAIN (p) = BLOCK_VARS (block);
649 BLOCK_VARS (block) = p;
651 #ifdef ENABLE_CHECKING
652 if (I_LABEL_BINDING (b->id) != b) abort ();
653 #endif
654 I_LABEL_BINDING (b->id) = b->shadowed;
655 break;
657 case ENUMERAL_TYPE:
658 case UNION_TYPE:
659 case RECORD_TYPE:
660 TYPE_CONTEXT (p) = context;
662 /* Types may not have tag-names, in which case the type
663 appears in the bindings list with b->id NULL. */
664 if (b->id)
666 #ifdef ENABLE_CHECKING
667 if (I_TAG_BINDING (b->id) != b) abort ();
668 #endif
669 I_TAG_BINDING (b->id) = b->shadowed;
671 break;
673 case FUNCTION_DECL:
674 /* Propagate TREE_ADDRESSABLE from nested functions to their
675 containing functions. */
676 if (! TREE_ASM_WRITTEN (p)
677 && DECL_INITIAL (p) != 0
678 && TREE_ADDRESSABLE (p)
679 && DECL_ABSTRACT_ORIGIN (p) != 0
680 && DECL_ABSTRACT_ORIGIN (p) != p)
681 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
682 goto common_symbol;
684 case VAR_DECL:
685 /* Warnings for unused variables. Keep this in sync with
686 stmt.c:warn_about_unused_variables, which we cannot use
687 since it expects a different data structure. */
688 if (warn_unused_variable
689 && !TREE_USED (p)
690 && !DECL_IN_SYSTEM_HEADER (p)
691 && DECL_NAME (p)
692 && !DECL_ARTIFICIAL (p)
693 && (scope != file_scope
694 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
695 && !TREE_THIS_VOLATILE (p)))
696 && scope != external_scope)
697 warning ("%Junused variable `%D'", p, p);
699 /* Fall through. */
700 case TYPE_DECL:
701 case CONST_DECL:
702 common_symbol:
703 /* All of these go in BLOCK_VARS, but only if this is the
704 binding in the home scope. */
705 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
707 TREE_CHAIN (p) = BLOCK_VARS (block);
708 BLOCK_VARS (block) = p;
711 /* Fall through. */
712 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
713 already been put there by store_parm_decls. Unused-
714 parameter warnings are handled by function.c.
715 error_mark_node obviously does not go in BLOCK_VARS and
716 does not get unused-variable warnings. */
717 case PARM_DECL:
718 case ERROR_MARK:
719 /* It is possible for a decl not to have a name. We get
720 here with b->id NULL in this case. */
721 if (b->id)
723 #ifdef ENABLE_CHECKING
724 if (I_SYMBOL_BINDING (b->id) != b) abort ();
725 #endif
726 I_SYMBOL_BINDING (b->id) = b->shadowed;
728 break;
730 default:
731 abort ();
736 /* Dispose of the block that we just made inside some higher level. */
737 if ((scope->function_body || scope == file_scope) && context)
739 DECL_INITIAL (context) = block;
740 BLOCK_SUPERCONTEXT (block) = context;
742 else if (scope->outer)
744 if (block)
745 SCOPE_LIST_APPEND (scope->outer, blocks, block);
746 /* If we did not make a block for the scope just exited, any
747 blocks made for inner scopes must be carried forward so they
748 will later become subblocks of something else. */
749 else if (scope->blocks)
750 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
753 /* Pop the current scope, and free the structure for reuse. */
754 current_scope = scope->outer;
755 if (scope->function_body)
756 current_function_scope = scope->outer_function;
758 memset (scope, 0, sizeof (struct c_scope));
759 scope->outer = scope_freelist;
760 scope_freelist = scope;
762 return block;
765 void
766 push_file_scope (void)
768 tree decl;
769 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
770 TREE_CHAIN (file_decl) = current_file_decl;
771 current_file_decl = file_decl;
773 push_scope ();
774 file_scope = current_scope;
776 start_fname_decls ();
778 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
779 bind (DECL_NAME (decl), decl, file_scope);
782 void
783 pop_file_scope (void)
785 /* In case there were missing closebraces, get us back to the global
786 binding level. */
787 while (current_scope != file_scope)
788 pop_scope ();
790 /* __FUNCTION__ is defined at file scope (""). This
791 call may not be necessary as my tests indicate it
792 still works without it. */
793 finish_fname_decls ();
795 /* Kludge: don't actually pop the file scope if generating a
796 precompiled header, so that macros and local symbols are still
797 visible to the PCH generator. */
798 if (pch_file)
799 return;
801 /* And pop off the file scope. */
802 pop_scope ();
803 file_scope = 0;
805 cpp_undef_all (parse_in);
808 /* Insert BLOCK at the end of the list of subblocks of the current
809 scope. This is used when a BIND_EXPR is expanded, to handle the
810 BLOCK node inside the BIND_EXPR. */
812 void
813 insert_block (tree block)
815 TREE_USED (block) = 1;
816 SCOPE_LIST_APPEND (current_scope, blocks, block);
819 /* Push a definition or a declaration of struct, union or enum tag "name".
820 "type" should be the type node.
821 We assume that the tag "name" is not already defined.
823 Note that the definition may really be just a forward reference.
824 In that case, the TYPE_SIZE will be zero. */
826 static void
827 pushtag (tree name, tree type)
829 /* Record the identifier as the type's name if it has none. */
830 if (name && !TYPE_NAME (type))
831 TYPE_NAME (type) = name;
832 bind (name, type, current_scope);
834 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
835 tagged type we just added to the current scope. This fake
836 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
837 to output a representation of a tagged type, and it also gives
838 us a convenient place to record the "scope start" address for the
839 tagged type. */
841 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
843 /* An approximation for now, so we can tell this is a function-scope tag.
844 This will be updated in pop_scope. */
845 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
848 /* Subroutine of compare_decls. Allow harmless mismatches in return
849 and argument types provided that the type modes match. This function
850 return a unified type given a suitable match, and 0 otherwise. */
852 static tree
853 match_builtin_function_types (tree newtype, tree oldtype)
855 tree newrettype, oldrettype;
856 tree newargs, oldargs;
857 tree trytype, tryargs;
859 /* Accept the return type of the new declaration if same modes. */
860 oldrettype = TREE_TYPE (oldtype);
861 newrettype = TREE_TYPE (newtype);
863 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
864 return 0;
866 oldargs = TYPE_ARG_TYPES (oldtype);
867 newargs = TYPE_ARG_TYPES (newtype);
868 tryargs = newargs;
870 while (oldargs || newargs)
872 if (! oldargs
873 || ! newargs
874 || ! TREE_VALUE (oldargs)
875 || ! TREE_VALUE (newargs)
876 || TYPE_MODE (TREE_VALUE (oldargs))
877 != TYPE_MODE (TREE_VALUE (newargs)))
878 return 0;
880 oldargs = TREE_CHAIN (oldargs);
881 newargs = TREE_CHAIN (newargs);
884 trytype = build_function_type (newrettype, tryargs);
885 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
888 /* Subroutine of diagnose_mismatched_decls. Check for function type
889 mismatch involving an empty arglist vs a nonempty one and give clearer
890 diagnostics. */
891 static void
892 diagnose_arglist_conflict (tree newdecl, tree olddecl,
893 tree newtype, tree oldtype)
895 tree t;
897 if (TREE_CODE (olddecl) != FUNCTION_DECL
898 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
899 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
901 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
902 return;
904 t = TYPE_ARG_TYPES (oldtype);
905 if (t == 0)
906 t = TYPE_ARG_TYPES (newtype);
907 for (; t; t = TREE_CHAIN (t))
909 tree type = TREE_VALUE (t);
911 if (TREE_CHAIN (t) == 0
912 && TYPE_MAIN_VARIANT (type) != void_type_node)
914 inform ("a parameter list with an ellipsis can't match "
915 "an empty parameter name list declaration");
916 break;
919 if (c_type_promotes_to (type) != type)
921 inform ("an argument type that has a default promotion can't match "
922 "an empty parameter name list declaration");
923 break;
928 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
929 old-style function definition, NEWDECL is a prototype declaration.
930 Diagnose inconsistencies in the argument list. Returns TRUE if
931 the prototype is compatible, FALSE if not. */
932 static bool
933 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
935 tree newargs, oldargs;
936 int i;
938 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
939 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
941 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
942 newargs = TYPE_ARG_TYPES (newtype);
943 i = 1;
945 for (;;)
947 tree oldargtype = TREE_VALUE (oldargs);
948 tree newargtype = TREE_VALUE (newargs);
950 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
951 break;
953 /* Reaching the end of just one list means the two decls don't
954 agree on the number of arguments. */
955 if (END_OF_ARGLIST (oldargtype))
957 error ("%Jprototype for '%D' declares more arguments "
958 "than previous old-style definition", newdecl, newdecl);
959 return false;
961 else if (END_OF_ARGLIST (newargtype))
963 error ("%Jprototype for '%D' declares fewer arguments "
964 "than previous old-style definition", newdecl, newdecl);
965 return false;
968 /* Type for passing arg must be consistent with that declared
969 for the arg. */
970 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
972 error ("%Jprototype for '%D' declares arg %d with incompatible type",
973 newdecl, newdecl, i);
974 return false;
977 oldargs = TREE_CHAIN (oldargs);
978 newargs = TREE_CHAIN (newargs);
979 i++;
982 /* If we get here, no errors were found, but do issue a warning
983 for this poor-style construct. */
984 warning ("%Jprototype for '%D' follows non-prototype definition",
985 newdecl, newdecl);
986 return true;
987 #undef END_OF_ARGLIST
990 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
991 first in a pair of mismatched declarations, using the diagnostic
992 function DIAG. */
993 static void
994 locate_old_decl (tree decl, void (*diag)(const char *, ...))
996 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
998 else if (DECL_INITIAL (decl))
999 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1000 else if (C_DECL_IMPLICIT (decl))
1001 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1002 else
1003 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1006 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1007 Returns true if the caller should proceed to merge the two, false
1008 if OLDDECL should simply be discarded. As a side effect, issues
1009 all necessary diagnostics for invalid or poor-style combinations.
1010 If it returns true, writes the types of NEWDECL and OLDDECL to
1011 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1012 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1014 static bool
1015 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1016 tree *newtypep, tree *oldtypep)
1018 tree newtype, oldtype;
1019 bool pedwarned = false;
1020 bool warned = false;
1022 /* If we have error_mark_node for either decl or type, just discard
1023 the previous decl - we're in an error cascade already. */
1024 if (olddecl == error_mark_node || newdecl == error_mark_node)
1025 return false;
1026 *oldtypep = oldtype = TREE_TYPE (olddecl);
1027 *newtypep = newtype = TREE_TYPE (newdecl);
1028 if (oldtype == error_mark_node || newtype == error_mark_node)
1029 return false;
1031 /* Two different categories of symbol altogether. This is an error
1032 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1033 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1035 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1036 && DECL_BUILT_IN (olddecl)
1037 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1039 error ("%J'%D' redeclared as different kind of symbol",
1040 newdecl, newdecl);
1041 locate_old_decl (olddecl, error);
1043 else if (TREE_PUBLIC (newdecl))
1044 warning ("%Jbuilt-in function '%D' declared as non-function",
1045 newdecl, newdecl);
1046 else if (warn_shadow)
1047 warning ("%Jdeclaration of '%D' shadows a built-in function",
1048 newdecl, newdecl);
1049 return false;
1052 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1054 if (TREE_CODE (olddecl) == FUNCTION_DECL
1055 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1057 /* Accept harmless mismatch in function types.
1058 This is for the ffs and fprintf builtins. */
1059 tree trytype = match_builtin_function_types (newtype, oldtype);
1061 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1062 *oldtypep = oldtype = trytype;
1063 else
1065 /* If types don't match for a built-in, throw away the
1066 built-in. No point in calling locate_old_decl here, it
1067 won't print anything. */
1068 warning ("%Jconflicting types for built-in function '%D'",
1069 newdecl, newdecl);
1070 return false;
1073 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1074 && DECL_SOURCE_LINE (olddecl) == 0)
1076 /* A conflicting function declaration for a predeclared
1077 function that isn't actually built in. Objective C uses
1078 these. The new declaration silently overrides everything
1079 but the volatility (i.e. noreturn) indication. See also
1080 below. FIXME: Make Objective C use normal builtins. */
1081 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1082 return false;
1084 /* Permit void foo (...) to match int foo (...) if the latter is
1085 the definition and implicit int was used. See
1086 c-torture/compile/920625-2.c. */
1087 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1088 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1089 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1090 && C_FUNCTION_IMPLICIT_INT (newdecl))
1092 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1093 /* Make sure we keep void as the return type. */
1094 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1095 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1096 pedwarned = true;
1098 else
1100 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1101 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1102 locate_old_decl (olddecl, error);
1103 return false;
1107 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1108 but silently ignore the redeclaration if either is in a system
1109 header. (Conflicting redeclarations were handled above.) */
1110 if (TREE_CODE (newdecl) == TYPE_DECL)
1112 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1113 return true; /* Allow OLDDECL to continue in use. */
1115 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1116 locate_old_decl (olddecl, error);
1117 return false;
1120 /* Function declarations can either be 'static' or 'extern' (no
1121 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1122 can never conflict with each other on account of linkage (6.2.2p4).
1123 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1124 two definitions if one is 'extern inline' and one is not. The non-
1125 extern-inline definition supersedes the extern-inline definition. */
1126 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1128 /* If you declare a built-in function name as static, or
1129 define the built-in with an old-style definition (so we
1130 can't validate the argument list) the built-in definition is
1131 overridden, but optionally warn this was a bad choice of name. */
1132 if (DECL_BUILT_IN (olddecl)
1133 && !C_DECL_DECLARED_BUILTIN (olddecl)
1134 && (!TREE_PUBLIC (newdecl)
1135 || (DECL_INITIAL (newdecl)
1136 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1138 if (warn_shadow)
1139 warning ("%Jdeclaration of '%D' shadows a built-in function",
1140 newdecl, newdecl);
1141 /* Discard the old built-in function. */
1142 return false;
1145 if (DECL_INITIAL (newdecl))
1147 if (DECL_INITIAL (olddecl)
1148 && !(DECL_DECLARED_INLINE_P (olddecl)
1149 && DECL_EXTERNAL (olddecl)
1150 && !(DECL_DECLARED_INLINE_P (newdecl)
1151 && DECL_EXTERNAL (newdecl))))
1153 error ("%Jredefinition of '%D'", newdecl, newdecl);
1154 locate_old_decl (olddecl, error);
1155 return false;
1158 /* If we have a prototype after an old-style function definition,
1159 the argument types must be checked specially. */
1160 else if (DECL_INITIAL (olddecl)
1161 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1162 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1163 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1165 locate_old_decl (olddecl, error);
1166 return false;
1168 /* Mismatched non-static and static is considered poor style.
1169 We only diagnose static then non-static if -Wtraditional,
1170 because it is the most convenient way to get some effects
1171 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1172 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1173 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1175 /* A static function declaration for a predeclared function
1176 that isn't actually built in, silently overrides the
1177 default. Objective C uses these. See also above.
1178 FIXME: Make Objective C use normal builtins. */
1179 if (TREE_CODE (olddecl) == FUNCTION_DECL
1180 && DECL_SOURCE_LINE (olddecl) == 0)
1181 return false;
1182 else
1184 warning ("%Jstatic declaration of '%D' follows "
1185 "non-static declaration", newdecl, newdecl);
1186 warned = true;
1189 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1190 && warn_traditional)
1192 warning ("%Jnon-static declaration of '%D' follows "
1193 "static declaration", newdecl, newdecl);
1194 warned = true;
1197 else if (TREE_CODE (newdecl) == VAR_DECL)
1199 /* Only variables can be thread-local, and all declarations must
1200 agree on this property. */
1201 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1203 if (DECL_THREAD_LOCAL (newdecl))
1204 error ("%Jthread-local declaration of '%D' follows "
1205 "non-thread-local declaration", newdecl, newdecl);
1206 else
1207 error ("%Jnon-thread-local declaration of '%D' follows "
1208 "thread-local declaration", newdecl, newdecl);
1210 locate_old_decl (olddecl, error);
1211 return false;
1214 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1215 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1217 error ("%Jredefinition of '%D'", newdecl, newdecl);
1218 locate_old_decl (olddecl, error);
1219 return false;
1222 /* Objects declared at file scope: if at least one is 'extern',
1223 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1224 if (DECL_FILE_SCOPE_P (newdecl))
1226 if (!DECL_EXTERNAL (newdecl)
1227 && !DECL_EXTERNAL (olddecl)
1228 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1230 if (TREE_PUBLIC (newdecl))
1231 error ("%Jnon-static declaration of '%D' follows "
1232 "static declaration", newdecl, newdecl);
1233 else
1234 error ("%Jstatic declaration of '%D' follows "
1235 "non-static declaration", newdecl, newdecl);
1237 locate_old_decl (olddecl, error);
1238 return false;
1241 /* Two objects with the same name declared at the same block
1242 scope must both be external references (6.7p3). */
1243 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1244 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1246 if (DECL_EXTERNAL (newdecl))
1247 error ("%Jextern declaration of '%D' follows "
1248 "declaration with no linkage", newdecl, newdecl);
1249 else if (DECL_EXTERNAL (olddecl))
1250 error ("%Jdeclaration of '%D' with no linkage follows "
1251 "extern declaration", newdecl, newdecl);
1252 else
1253 error ("%Jredeclaration of '%D' with no linkage",
1254 newdecl, newdecl);
1256 locate_old_decl (olddecl, error);
1257 return false;
1261 /* warnings */
1262 /* All decls must agree on a non-default visibility. */
1263 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1264 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1265 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1267 warning ("%Jredeclaration of '%D' with different visibility "
1268 "(old visibility preserved)", newdecl, newdecl);
1269 warned = true;
1272 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1274 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1275 if (DECL_DECLARED_INLINE_P (newdecl)
1276 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1278 warning ("%Jinline declaration of '%D' follows "
1279 "declaration with attribute noinline", newdecl, newdecl);
1280 warned = true;
1282 else if (DECL_DECLARED_INLINE_P (olddecl)
1283 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1285 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1286 "inline declaration ", newdecl, newdecl);
1287 warned = true;
1290 /* Inline declaration after use or definition.
1291 ??? Should we still warn about this now we have unit-at-a-time
1292 mode and can get it right? */
1293 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1295 if (TREE_USED (olddecl))
1297 warning ("%J'%D' declared inline after being called",
1298 olddecl, olddecl);
1299 warned = true;
1301 else if (DECL_INITIAL (olddecl))
1303 warning ("%J'%D' declared inline after its definition",
1304 olddecl, olddecl);
1305 warned = true;
1309 else /* PARM_DECL, VAR_DECL */
1311 /* Redeclaration of a parameter is a constraint violation (this is
1312 not explicitly stated, but follows from C99 6.7p3 [no more than
1313 one declaration of the same identifier with no linkage in the
1314 same scope, except type tags] and 6.2.2p6 [parameters have no
1315 linkage]). We must check for a forward parameter declaration,
1316 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1317 an extension, the mandatory diagnostic for which is handled by
1318 mark_forward_parm_decls. */
1320 if (TREE_CODE (newdecl) == PARM_DECL
1321 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1323 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1325 return false;
1328 /* These bits are only type qualifiers when applied to objects. */
1329 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1331 if (TREE_THIS_VOLATILE (newdecl))
1332 pedwarn ("%Jvolatile declaration of '%D' follows "
1333 "non-volatile declaration", newdecl, newdecl);
1334 else
1335 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1336 "volatile declaration", newdecl, newdecl);
1337 pedwarned = true;
1339 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1341 if (TREE_READONLY (newdecl))
1342 pedwarn ("%Jconst declaration of '%D' follows "
1343 "non-const declaration", newdecl, newdecl);
1344 else
1345 pedwarn ("%Jnon-const declaration of '%D' follows "
1346 "const declaration", newdecl, newdecl);
1347 pedwarned = true;
1351 /* Optional warning for completely redundant decls. */
1352 if (!warned && !pedwarned
1353 && warn_redundant_decls
1354 /* Don't warn about a function declaration followed by a
1355 definition. */
1356 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1357 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1358 /* Don't warn about an extern followed by a definition. */
1359 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1360 /* Don't warn about forward parameter decls. */
1361 && !(TREE_CODE (newdecl) == PARM_DECL
1362 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1364 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1365 warned = true;
1368 /* Report location of previous decl/defn in a consistent manner. */
1369 if (warned || pedwarned)
1370 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1372 return true;
1375 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1376 consistent with OLDDECL, but carries new information. Merge the
1377 new information into OLDDECL. This function issues no
1378 diagnostics. */
1380 static void
1381 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1383 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1384 && DECL_INITIAL (newdecl) != 0);
1386 /* For real parm decl following a forward decl, rechain the old decl
1387 in its new location and clear TREE_ASM_WRITTEN (it's not a
1388 forward decl anymore). */
1389 if (TREE_CODE (newdecl) == PARM_DECL
1390 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1392 struct c_binding *b, **here;
1394 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1395 if ((*here)->decl == olddecl)
1396 goto found;
1397 abort ();
1399 found:
1400 b = *here;
1401 *here = b->prev;
1402 b->prev = current_scope->bindings;
1403 current_scope->bindings = b;
1405 TREE_ASM_WRITTEN (olddecl) = 0;
1408 DECL_ATTRIBUTES (newdecl)
1409 = targetm.merge_decl_attributes (olddecl, newdecl);
1411 /* Merge the data types specified in the two decls. */
1412 TREE_TYPE (newdecl)
1413 = TREE_TYPE (olddecl)
1414 = common_type (newtype, oldtype);
1416 /* Lay the type out, unless already done. */
1417 if (oldtype != TREE_TYPE (newdecl))
1419 if (TREE_TYPE (newdecl) != error_mark_node)
1420 layout_type (TREE_TYPE (newdecl));
1421 if (TREE_CODE (newdecl) != FUNCTION_DECL
1422 && TREE_CODE (newdecl) != TYPE_DECL
1423 && TREE_CODE (newdecl) != CONST_DECL)
1424 layout_decl (newdecl, 0);
1426 else
1428 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1429 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1430 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1431 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1432 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1433 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1435 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1436 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1440 /* Keep the old rtl since we can safely use it. */
1441 COPY_DECL_RTL (olddecl, newdecl);
1443 /* Merge the type qualifiers. */
1444 if (TREE_READONLY (newdecl))
1445 TREE_READONLY (olddecl) = 1;
1447 if (TREE_THIS_VOLATILE (newdecl))
1449 TREE_THIS_VOLATILE (olddecl) = 1;
1450 if (TREE_CODE (newdecl) == VAR_DECL)
1451 make_var_volatile (newdecl);
1454 /* Keep source location of definition rather than declaration. */
1455 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1456 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1458 /* Merge the unused-warning information. */
1459 if (DECL_IN_SYSTEM_HEADER (olddecl))
1460 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1461 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1462 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1464 /* Merge the initialization information. */
1465 if (DECL_INITIAL (newdecl) == 0)
1466 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1468 /* Merge the section attribute.
1469 We want to issue an error if the sections conflict but that must be
1470 done later in decl_attributes since we are called before attributes
1471 are assigned. */
1472 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1473 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1475 /* Copy the assembler name.
1476 Currently, it can only be defined in the prototype. */
1477 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1479 /* If either declaration has a nondefault visibility, use it. */
1480 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1481 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1483 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1485 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1486 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1487 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1488 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1489 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1490 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1491 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1492 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1493 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1496 /* Merge the storage class information. */
1497 merge_weak (newdecl, olddecl);
1499 /* For functions, static overrides non-static. */
1500 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1502 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1503 /* This is since we don't automatically
1504 copy the attributes of NEWDECL into OLDDECL. */
1505 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1506 /* If this clears `static', clear it in the identifier too. */
1507 if (! TREE_PUBLIC (olddecl))
1508 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1510 if (DECL_EXTERNAL (newdecl))
1512 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1513 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1515 /* An extern decl does not override previous storage class. */
1516 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1517 if (! DECL_EXTERNAL (newdecl))
1519 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1520 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1523 else
1525 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1526 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1529 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1531 /* If we're redefining a function previously defined as extern
1532 inline, make sure we emit debug info for the inline before we
1533 throw it away, in case it was inlined into a function that hasn't
1534 been written out yet. */
1535 if (new_is_definition && DECL_INITIAL (olddecl))
1537 if (TREE_USED (olddecl)
1538 /* In unit-at-a-time mode we never inline re-defined extern
1539 inline functions. */
1540 && !flag_unit_at_a_time
1541 && cgraph_function_possibly_inlined_p (olddecl))
1542 (*debug_hooks->outlining_inline_function) (olddecl);
1544 /* The new defn must not be inline. */
1545 DECL_INLINE (newdecl) = 0;
1546 DECL_UNINLINABLE (newdecl) = 1;
1548 else
1550 /* If either decl says `inline', this fn is inline,
1551 unless its definition was passed already. */
1552 if (DECL_DECLARED_INLINE_P (newdecl)
1553 || DECL_DECLARED_INLINE_P (olddecl))
1554 DECL_DECLARED_INLINE_P (newdecl) = 1;
1556 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1557 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1560 if (DECL_BUILT_IN (olddecl))
1562 /* If redeclaring a builtin function, it stays built in.
1563 But it gets tagged as having been declared. */
1564 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1565 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1566 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1569 /* Also preserve various other info from the definition. */
1570 if (! new_is_definition)
1572 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1573 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1574 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1575 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1576 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1578 /* Set DECL_INLINE on the declaration if we've got a body
1579 from which to instantiate. */
1580 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1582 DECL_INLINE (newdecl) = 1;
1583 DECL_ABSTRACT_ORIGIN (newdecl)
1584 = DECL_ABSTRACT_ORIGIN (olddecl);
1587 else
1589 /* If a previous declaration said inline, mark the
1590 definition as inlinable. */
1591 if (DECL_DECLARED_INLINE_P (newdecl)
1592 && ! DECL_UNINLINABLE (newdecl))
1593 DECL_INLINE (newdecl) = 1;
1597 /* This bit must not get wiped out. */
1598 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1600 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1601 But preserve OLDDECL's DECL_UID. */
1603 unsigned olddecl_uid = DECL_UID (olddecl);
1605 memcpy ((char *) olddecl + sizeof (struct tree_common),
1606 (char *) newdecl + sizeof (struct tree_common),
1607 sizeof (struct tree_decl) - sizeof (struct tree_common));
1608 DECL_UID (olddecl) = olddecl_uid;
1611 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1612 so that encode_section_info has a chance to look at the new decl
1613 flags and attributes. */
1614 if (DECL_RTL_SET_P (olddecl)
1615 && (TREE_CODE (olddecl) == FUNCTION_DECL
1616 || (TREE_CODE (olddecl) == VAR_DECL
1617 && TREE_STATIC (olddecl))))
1618 make_decl_rtl (olddecl, NULL);
1621 /* Handle when a new declaration NEWDECL has the same name as an old
1622 one OLDDECL in the same binding contour. Prints an error message
1623 if appropriate.
1625 If safely possible, alter OLDDECL to look like NEWDECL, and return
1626 true. Otherwise, return false. */
1628 static bool
1629 duplicate_decls (tree newdecl, tree olddecl)
1631 tree newtype, oldtype;
1633 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1634 return false;
1636 merge_decls (newdecl, olddecl, newtype, oldtype);
1637 return true;
1641 /* Check whether decl-node NEW shadows an existing declaration. */
1642 static void
1643 warn_if_shadowing (tree new)
1645 struct c_binding *b;
1647 /* Shadow warnings wanted? */
1648 if (!warn_shadow
1649 /* No shadow warnings for internally generated vars. */
1650 || DECL_SOURCE_LINE (new) == 0
1651 /* No shadow warnings for vars made for inlining. */
1652 || DECL_FROM_INLINE (new)
1653 /* Don't warn about the parm names in function declarator
1654 within a function declarator. It would be nice to avoid
1655 warning in any function declarator in a declaration, as
1656 opposed to a definition, but there is no way to tell
1657 it's not a definition at this point. */
1658 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1659 return;
1661 /* Is anything being shadowed? Do not be confused by a second binding
1662 to the same decl in the externals scope. */
1663 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1664 if (b->decl && b->decl != new && b->contour != external_scope)
1666 tree old = b->decl;
1668 if (TREE_CODE (old) == PARM_DECL)
1669 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1670 else if (DECL_FILE_SCOPE_P (old))
1671 warning ("%Jdeclaration of '%D' shadows a global declaration",
1672 new, new);
1673 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1674 warning ("%Jdeclaration of '%D' shadows a built-in function",
1675 new, new);
1676 else
1677 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1679 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1680 warning ("%Jshadowed declaration is here", old);
1682 break;
1687 /* Subroutine of pushdecl.
1689 X is a TYPE_DECL for a typedef statement. Create a brand new
1690 ..._TYPE node (which will be just a variant of the existing
1691 ..._TYPE node with identical properties) and then install X
1692 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1694 The whole point here is to end up with a situation where each
1695 and every ..._TYPE node the compiler creates will be uniquely
1696 associated with AT MOST one node representing a typedef name.
1697 This way, even though the compiler substitutes corresponding
1698 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1699 early on, later parts of the compiler can always do the reverse
1700 translation and get back the corresponding typedef name. For
1701 example, given:
1703 typedef struct S MY_TYPE;
1704 MY_TYPE object;
1706 Later parts of the compiler might only know that `object' was of
1707 type `struct S' if it were not for code just below. With this
1708 code however, later parts of the compiler see something like:
1710 struct S' == struct S
1711 typedef struct S' MY_TYPE;
1712 struct S' object;
1714 And they can then deduce (from the node for type struct S') that
1715 the original object declaration was:
1717 MY_TYPE object;
1719 Being able to do this is important for proper support of protoize,
1720 and also for generating precise symbolic debugging information
1721 which takes full account of the programmer's (typedef) vocabulary.
1723 Obviously, we don't want to generate a duplicate ..._TYPE node if
1724 the TYPE_DECL node that we are now processing really represents a
1725 standard built-in type.
1727 Since all standard types are effectively declared at line zero
1728 in the source file, we can easily check to see if we are working
1729 on a standard type by checking the current value of lineno. */
1731 static void
1732 clone_underlying_type (tree x)
1734 if (DECL_SOURCE_LINE (x) == 0)
1736 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1737 TYPE_NAME (TREE_TYPE (x)) = x;
1739 else if (TREE_TYPE (x) != error_mark_node
1740 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1742 tree tt = TREE_TYPE (x);
1743 DECL_ORIGINAL_TYPE (x) = tt;
1744 tt = build_type_copy (tt);
1745 TYPE_NAME (tt) = x;
1746 TREE_USED (tt) = TREE_USED (x);
1747 TREE_TYPE (x) = tt;
1751 /* Record a decl-node X as belonging to the current lexical scope.
1752 Check for errors (such as an incompatible declaration for the same
1753 name already seen in the same scope).
1755 Returns either X or an old decl for the same name.
1756 If an old decl is returned, it may have been smashed
1757 to agree with what X says. */
1759 tree
1760 pushdecl (tree x)
1762 tree name = DECL_NAME (x);
1763 struct c_scope *scope = current_scope;
1764 struct c_binding *b;
1766 /* Functions need the lang_decl data. */
1767 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1768 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1770 /* A local extern declaration for a function doesn't constitute nesting.
1771 A local auto declaration does, since it's a forward decl
1772 for a nested function coming later. */
1773 if (current_function_decl == NULL
1774 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1775 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1776 DECL_CONTEXT (x) = current_file_decl;
1777 else
1778 DECL_CONTEXT (x) = current_function_decl;
1780 /* Anonymous decls are just inserted in the scope. */
1781 if (!name)
1783 bind (name, x, scope);
1784 return x;
1787 /* First, see if there is another declaration with the same name in
1788 the current scope. If there is, duplicate_decls may do all the
1789 work for us. If duplicate_decls returns false, that indicates
1790 two incompatible decls in the same scope; we are to silently
1791 replace the old one (duplicate_decls has issued all appropriate
1792 diagnostics). In particular, we should not consider possible
1793 duplicates in the external scope, or shadowing. */
1794 b = I_SYMBOL_BINDING (name);
1795 if (b && b->contour == scope)
1797 if (duplicate_decls (x, b->decl))
1798 return b->decl;
1799 else
1800 goto skip_external_and_shadow_checks;
1803 /* All declarations with external linkage, and all external
1804 references, go in the external scope, no matter what scope is
1805 current. However, the binding in that scope is ignored for
1806 purposes of normal name lookup. A separate binding structure is
1807 created in the requested scope; this governs the normal
1808 visibility of the symbol.
1810 The binding in the externals scope is used exclusively for
1811 detecting duplicate declarations of the same object, no matter
1812 what scope they are in; this is what we do here. (C99 6.2.7p2:
1813 All declarations that refer to the same object or function shall
1814 have compatible type; otherwise, the behavior is undefined.) */
1815 if (DECL_EXTERNAL (x) || scope == file_scope)
1817 if (warn_nested_externs
1818 && scope != file_scope
1819 && !DECL_IN_SYSTEM_HEADER (x))
1820 warning ("nested extern declaration of `%s'",
1821 IDENTIFIER_POINTER (name));
1823 while (b && b->contour != external_scope)
1824 b = b->shadowed;
1826 /* The point of the same_translation_unit_p check here is,
1827 we want to detect a duplicate decl for a construct like
1828 foo() { extern bar(); } ... static bar(); but not if
1829 they are in different translation units. In any case,
1830 the static does not go in the externals scope. */
1831 if (b
1832 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1833 || same_translation_unit_p (x, b->decl))
1834 && duplicate_decls (x, b->decl))
1836 bind (name, b->decl, scope);
1837 return b->decl;
1839 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1841 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1842 bind (name, x, external_scope);
1846 warn_if_shadowing (x);
1848 skip_external_and_shadow_checks:
1849 if (TREE_CODE (x) == TYPE_DECL)
1850 clone_underlying_type (x);
1852 bind (name, x, scope);
1854 /* If x's type is incomplete because it's based on a
1855 structure or union which has not yet been fully declared,
1856 attach it to that structure or union type, so we can go
1857 back and complete the variable declaration later, if the
1858 structure or union gets fully declared.
1860 If the input is erroneous, we can have error_mark in the type
1861 slot (e.g. "f(void a, ...)") - that doesn't count as an
1862 incomplete type. */
1863 if (TREE_TYPE (x) != error_mark_node
1864 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1866 tree element = TREE_TYPE (x);
1868 while (TREE_CODE (element) == ARRAY_TYPE)
1869 element = TREE_TYPE (element);
1870 element = TYPE_MAIN_VARIANT (element);
1872 if ((TREE_CODE (element) == RECORD_TYPE
1873 || TREE_CODE (element) == UNION_TYPE)
1874 && (TREE_CODE (x) != TYPE_DECL
1875 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1876 && !COMPLETE_TYPE_P (element))
1877 C_TYPE_INCOMPLETE_VARS (element)
1878 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1880 return x;
1883 /* Record X as belonging to file scope.
1884 This is used only internally by the Objective-C front end,
1885 and is limited to its needs. duplicate_decls is not called;
1886 if there is any preexisting decl for this identifier, it is an ICE. */
1888 tree
1889 pushdecl_top_level (tree x)
1891 tree name;
1893 if (TREE_CODE (x) != VAR_DECL)
1894 abort ();
1896 name = DECL_NAME (x);
1898 if (I_SYMBOL_BINDING (name))
1899 abort ();
1901 DECL_CONTEXT (x) = current_file_decl;
1902 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1904 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1905 bind (name, x, external_scope);
1907 if (file_scope)
1908 bind (name, x, file_scope);
1910 return x;
1913 static void
1914 implicit_decl_warning (tree id, tree olddecl)
1916 void (*diag) (const char *, ...);
1917 switch (mesg_implicit_function_declaration)
1919 case 0: return;
1920 case 1: diag = warning; break;
1921 case 2: diag = error; break;
1922 default: abort ();
1925 diag (N_("implicit declaration of function '%E'"), id);
1926 if (olddecl)
1927 locate_old_decl (olddecl, diag);
1930 /* Generate an implicit declaration for identifier FUNCTIONID as a
1931 function of type int (). */
1933 tree
1934 implicitly_declare (tree functionid)
1936 tree decl = lookup_name_in_scope (functionid, external_scope);
1938 if (decl)
1940 /* FIXME: Objective-C has weird not-really-builtin functions
1941 which are supposed to be visible automatically. They wind up
1942 in the external scope because they're pushed before the file
1943 scope gets created. Catch this here and rebind them into the
1944 file scope. */
1945 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1947 bind (functionid, decl, file_scope);
1948 return decl;
1950 else
1952 /* Implicit declaration of a function already declared
1953 (somehow) in a different scope, or as a built-in.
1954 If this is the first time this has happened, warn;
1955 then recycle the old declaration. */
1956 if (!C_DECL_IMPLICIT (decl))
1958 implicit_decl_warning (functionid, decl);
1959 C_DECL_IMPLICIT (decl) = 1;
1961 bind (functionid, decl, current_scope);
1962 return decl;
1966 /* Not seen before. */
1967 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1968 DECL_EXTERNAL (decl) = 1;
1969 TREE_PUBLIC (decl) = 1;
1970 C_DECL_IMPLICIT (decl) = 1;
1971 implicit_decl_warning (functionid, 0);
1973 /* C89 says implicit declarations are in the innermost block.
1974 So we record the decl in the standard fashion. */
1975 decl = pushdecl (decl);
1977 /* No need to call objc_check_decl here - it's a function type. */
1978 rest_of_decl_compilation (decl, NULL, 0, 0);
1980 /* Write a record describing this implicit function declaration
1981 to the prototypes file (if requested). */
1982 gen_aux_info_record (decl, 0, 1, 0);
1984 /* Possibly apply some default attributes to this implicit declaration. */
1985 decl_attributes (&decl, NULL_TREE, 0);
1987 return decl;
1990 /* Issue an error message for a reference to an undeclared variable
1991 ID, including a reference to a builtin outside of function-call
1992 context. Establish a binding of the identifier to error_mark_node
1993 in an appropriate scope, which will suppress further errors for the
1994 same identifier. */
1995 void
1996 undeclared_variable (tree id)
1998 static bool already = false;
1999 struct c_scope *scope;
2001 if (current_function_decl == 0)
2003 error ("'%E' undeclared here (not in a function)", id);
2004 scope = current_scope;
2006 else
2008 error ("'%E' undeclared (first use in this function)", id);
2010 if (! already)
2012 error ("(Each undeclared identifier is reported only once");
2013 error ("for each function it appears in.)");
2014 already = true;
2017 /* If we are parsing old-style parameter decls, current_function_decl
2018 will be nonnull but current_function_scope will be null. */
2019 scope = current_function_scope ? current_function_scope : current_scope;
2021 bind (id, error_mark_node, scope);
2024 /* Subroutine of lookup_label, declare_label, define_label: construct a
2025 LABEL_DECL with all the proper frills. */
2027 static tree
2028 make_label (tree name, location_t location)
2030 tree label = build_decl (LABEL_DECL, name, void_type_node);
2032 DECL_CONTEXT (label) = current_function_decl;
2033 DECL_MODE (label) = VOIDmode;
2034 DECL_SOURCE_LOCATION (label) = location;
2036 return label;
2039 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2040 Create one if none exists so far for the current function.
2041 This is called when a label is used in a goto expression or
2042 has its address taken. */
2044 tree
2045 lookup_label (tree name)
2047 tree label;
2049 if (current_function_decl == 0)
2051 error ("label %s referenced outside of any function",
2052 IDENTIFIER_POINTER (name));
2053 return 0;
2056 /* Use a label already defined or ref'd with this name, but not if
2057 it is inherited from a containing function and wasn't declared
2058 using __label__. */
2059 label = I_LABEL_DECL (name);
2060 if (label && (DECL_CONTEXT (label) == current_function_decl
2061 || C_DECLARED_LABEL_FLAG (label)))
2063 /* If the label has only been declared, update its apparent
2064 location to point here, for better diagnostics if it
2065 turns out not to have been defined. */
2066 if (!TREE_USED (label))
2067 DECL_SOURCE_LOCATION (label) = input_location;
2068 return label;
2071 /* No label binding for that identifier; make one. */
2072 label = make_label (name, input_location);
2074 /* Ordinary labels go in the current function scope. */
2075 bind (name, label, current_function_scope);
2076 return label;
2079 /* Make a label named NAME in the current function, shadowing silently
2080 any that may be inherited from containing functions or containing
2081 scopes. This is called for __label__ declarations. */
2083 /* Note that valid use, if the label being shadowed comes from another
2084 scope in the same function, requires calling declare_nonlocal_label
2085 right away. (Is this still true? -zw 2003-07-17) */
2087 tree
2088 declare_label (tree name)
2090 struct c_binding *b = I_LABEL_BINDING (name);
2091 tree label;
2093 /* Check to make sure that the label hasn't already been declared
2094 at this scope */
2095 if (b && b->contour == current_scope)
2097 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2098 locate_old_decl (b->decl, error);
2100 /* Just use the previous declaration. */
2101 return b->decl;
2104 label = make_label (name, input_location);
2105 C_DECLARED_LABEL_FLAG (label) = 1;
2107 /* Declared labels go in the current scope. */
2108 bind (name, label, current_scope);
2109 return label;
2112 /* Define a label, specifying the location in the source file.
2113 Return the LABEL_DECL node for the label, if the definition is valid.
2114 Otherwise return 0. */
2116 tree
2117 define_label (location_t location, tree name)
2119 /* Find any preexisting label with this name. It is an error
2120 if that label has already been defined in this function, or
2121 if there is a containing function with a declared label with
2122 the same name. */
2123 tree label = I_LABEL_DECL (name);
2125 if (label
2126 && ((DECL_CONTEXT (label) == current_function_decl
2127 && DECL_INITIAL (label) != 0)
2128 || (DECL_CONTEXT (label) != current_function_decl
2129 && C_DECLARED_LABEL_FLAG (label))))
2131 error ("%Hduplicate label `%D'", &location, label);
2132 locate_old_decl (label, error);
2133 return 0;
2135 else if (label && DECL_CONTEXT (label) == current_function_decl)
2137 /* The label has been used or declared already in this function,
2138 but not defined. Update its location to point to this
2139 definition. */
2140 DECL_SOURCE_LOCATION (label) = location;
2142 else
2144 /* No label binding for that identifier; make one. */
2145 label = make_label (name, location);
2147 /* Ordinary labels go in the current function scope. */
2148 bind (name, label, current_function_scope);
2151 if (warn_traditional && !in_system_header && lookup_name (name))
2152 warning ("%Htraditional C lacks a separate namespace for labels, "
2153 "identifier `%s' conflicts", &location,
2154 IDENTIFIER_POINTER (name));
2156 /* Mark label as having been defined. */
2157 DECL_INITIAL (label) = error_mark_node;
2158 return label;
2161 /* Given NAME, an IDENTIFIER_NODE,
2162 return the structure (or union or enum) definition for that name.
2163 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2164 CODE says which kind of type the caller wants;
2165 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2166 If the wrong kind of type is found, an error is reported. */
2168 static tree
2169 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2171 struct c_binding *b = I_TAG_BINDING (name);
2172 int thislevel = 0;
2174 if (!b || !b->decl)
2175 return 0;
2177 /* We only care about whether it's in this level if
2178 thislevel_only was set or it might be a type clash. */
2179 if (thislevel_only || TREE_CODE (b->decl) != code)
2181 /* For our purposes, a tag in the external scope is the same as
2182 a tag in the file scope. (Primarily relevant to Objective-C
2183 and its builtin structure tags, which get pushed before the
2184 file scope is created.) */
2185 if (b->contour == current_scope
2186 || (current_scope == file_scope && b->contour == external_scope))
2187 thislevel = 1;
2190 if (thislevel_only && !thislevel)
2191 return 0;
2193 if (TREE_CODE (b->decl) != code)
2195 /* Definition isn't the kind we were looking for. */
2196 pending_invalid_xref = name;
2197 pending_invalid_xref_location = input_location;
2199 /* If in the same binding level as a declaration as a tag
2200 of a different type, this must not be allowed to
2201 shadow that tag, so give the error immediately.
2202 (For example, "struct foo; union foo;" is invalid.) */
2203 if (thislevel)
2204 pending_xref_error ();
2206 return b->decl;
2209 /* Print an error message now
2210 for a recent invalid struct, union or enum cross reference.
2211 We don't print them immediately because they are not invalid
2212 when used in the `struct foo;' construct for shadowing. */
2214 void
2215 pending_xref_error (void)
2217 if (pending_invalid_xref != 0)
2218 error ("%H`%s' defined as wrong kind of tag",
2219 &pending_invalid_xref_location,
2220 IDENTIFIER_POINTER (pending_invalid_xref));
2221 pending_invalid_xref = 0;
2225 /* Look up NAME in the current scope and its superiors
2226 in the namespace of variables, functions and typedefs.
2227 Return a ..._DECL node of some kind representing its definition,
2228 or return 0 if it is undefined. */
2230 tree
2231 lookup_name (tree name)
2233 struct c_binding *b = I_SYMBOL_BINDING (name);
2234 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2235 return b->decl;
2236 return 0;
2239 /* Similar to `lookup_name' but look only at the indicated scope. */
2241 static tree
2242 lookup_name_in_scope (tree name, struct c_scope *scope)
2244 struct c_binding *b;
2246 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2247 if (b->contour == scope)
2248 return b->decl;
2249 return 0;
2252 /* Create the predefined scalar types of C,
2253 and some nodes representing standard constants (0, 1, (void *) 0).
2254 Initialize the global scope.
2255 Make definitions for built-in primitive functions. */
2257 void
2258 c_init_decl_processing (void)
2260 tree endlink;
2261 tree ptr_ftype_void, ptr_ftype_ptr;
2262 location_t save_loc = input_location;
2264 /* Adds some ggc roots, and reserved words for c-parse.in. */
2265 c_parse_init ();
2267 current_function_decl = 0;
2269 /* Make the externals scope. */
2270 push_scope ();
2271 external_scope = current_scope;
2273 /* Declarations from c_common_nodes_and_builtins must not be associated
2274 with this input file, lest we get differences between using and not
2275 using preprocessed headers. */
2276 input_location.file = "<internal>";
2277 input_location.line = 0;
2279 build_common_tree_nodes (flag_signed_char);
2281 c_common_nodes_and_builtins ();
2283 /* In C, comparisons and TRUTH_* expressions have type int. */
2284 truthvalue_type_node = integer_type_node;
2285 truthvalue_true_node = integer_one_node;
2286 truthvalue_false_node = integer_zero_node;
2288 /* Even in C99, which has a real boolean type. */
2289 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2290 boolean_type_node));
2292 endlink = void_list_node;
2293 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2294 ptr_ftype_ptr
2295 = build_function_type (ptr_type_node,
2296 tree_cons (NULL_TREE, ptr_type_node, endlink));
2298 input_location = save_loc;
2300 pedantic_lvalues = true;
2302 make_fname_decl = c_make_fname_decl;
2303 start_fname_decls ();
2306 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2307 decl, NAME is the initialization string and TYPE_DEP indicates whether
2308 NAME depended on the type of the function. As we don't yet implement
2309 delayed emission of static data, we mark the decl as emitted
2310 so it is not placed in the output. Anything using it must therefore pull
2311 out the STRING_CST initializer directly. FIXME. */
2313 static tree
2314 c_make_fname_decl (tree id, int type_dep)
2316 const char *name = fname_as_string (type_dep);
2317 tree decl, type, init;
2318 size_t length = strlen (name);
2320 type = build_array_type
2321 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2322 build_index_type (size_int (length)));
2324 decl = build_decl (VAR_DECL, id, type);
2326 TREE_STATIC (decl) = 1;
2327 TREE_READONLY (decl) = 1;
2328 DECL_ARTIFICIAL (decl) = 1;
2330 init = build_string (length + 1, name);
2331 TREE_TYPE (init) = type;
2332 DECL_INITIAL (decl) = init;
2334 TREE_USED (decl) = 1;
2336 if (current_function_decl)
2338 DECL_CONTEXT (decl) = current_function_decl;
2339 bind (id, decl, current_function_scope);
2342 finish_decl (decl, init, NULL_TREE);
2344 return decl;
2347 /* Return a definition for a builtin function named NAME and whose data type
2348 is TYPE. TYPE should be a function type with argument types.
2349 FUNCTION_CODE tells later passes how to compile calls to this function.
2350 See tree.h for its possible values.
2352 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2353 the name to be called if we can't opencode the function. If
2354 ATTRS is nonzero, use that for the function's attribute list. */
2356 tree
2357 builtin_function (const char *name, tree type, int function_code,
2358 enum built_in_class class, const char *library_name,
2359 tree attrs)
2361 tree id = get_identifier (name);
2362 tree decl = build_decl (FUNCTION_DECL, id, type);
2363 TREE_PUBLIC (decl) = 1;
2364 DECL_EXTERNAL (decl) = 1;
2365 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2366 DECL_BUILT_IN_CLASS (decl) = class;
2367 DECL_FUNCTION_CODE (decl) = function_code;
2368 if (library_name)
2369 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2370 make_decl_rtl (decl, NULL);
2372 /* Should never be called on a symbol with a preexisting meaning. */
2373 if (I_SYMBOL_BINDING (id))
2374 abort ();
2376 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2377 bind (id, decl, external_scope);
2379 /* Builtins in the implementation namespace are made visible without
2380 needing to be explicitly declared. See push_file_scope. */
2381 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2383 TREE_CHAIN (decl) = visible_builtins;
2384 visible_builtins = decl;
2387 /* Possibly apply some default attributes to this built-in function. */
2388 if (attrs)
2389 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2390 else
2391 decl_attributes (&decl, NULL_TREE, 0);
2393 return decl;
2396 /* Called when a declaration is seen that contains no names to declare.
2397 If its type is a reference to a structure, union or enum inherited
2398 from a containing scope, shadow that tag name for the current scope
2399 with a forward reference.
2400 If its type defines a new named structure or union
2401 or defines an enum, it is valid but we need not do anything here.
2402 Otherwise, it is an error. */
2404 void
2405 shadow_tag (tree declspecs)
2407 shadow_tag_warned (declspecs, 0);
2410 void
2411 shadow_tag_warned (tree declspecs, int warned)
2414 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2415 no pedwarn. */
2417 int found_tag = 0;
2418 tree link;
2419 tree specs, attrs;
2421 pending_invalid_xref = 0;
2423 /* Remove the attributes from declspecs, since they will confuse the
2424 following code. */
2425 split_specs_attrs (declspecs, &specs, &attrs);
2427 for (link = specs; link; link = TREE_CHAIN (link))
2429 tree value = TREE_VALUE (link);
2430 enum tree_code code = TREE_CODE (value);
2432 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2433 /* Used to test also that TYPE_SIZE (value) != 0.
2434 That caused warning for `struct foo;' at top level in the file. */
2436 tree name = TYPE_NAME (value);
2437 tree t;
2439 found_tag++;
2441 if (name == 0)
2443 if (warned != 1 && code != ENUMERAL_TYPE)
2444 /* Empty unnamed enum OK */
2446 pedwarn ("unnamed struct/union that defines no instances");
2447 warned = 1;
2450 else
2452 t = lookup_tag (code, name, 1);
2454 if (t == 0)
2456 t = make_node (code);
2457 pushtag (name, t);
2461 else
2463 if (!warned && ! in_system_header)
2465 warning ("useless keyword or type name in empty declaration");
2466 warned = 2;
2471 if (found_tag > 1)
2472 error ("two types specified in one empty declaration");
2474 if (warned != 1)
2476 if (found_tag == 0)
2477 pedwarn ("empty declaration");
2481 /* Construct an array declarator. EXPR is the expression inside [], or
2482 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2483 to the pointer to which a parameter array is converted). STATIC_P is
2484 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2485 is nonzero is the array is [*], a VLA of unspecified length which is
2486 nevertheless a complete type (not currently implemented by GCC),
2487 zero otherwise. The declarator is constructed as an ARRAY_REF
2488 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2489 left of the [] (filled by in set_array_declarator_type) and operand 1
2490 is the expression inside; whose TREE_TYPE is the type qualifiers and
2491 which has TREE_STATIC set if "static" is used. */
2493 tree
2494 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2496 tree decl;
2497 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2498 TREE_TYPE (decl) = quals;
2499 TREE_STATIC (decl) = (static_p ? 1 : 0);
2500 if (pedantic && !flag_isoc99)
2502 if (static_p || quals != NULL_TREE)
2503 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2504 if (vla_unspec_p)
2505 pedwarn ("ISO C90 does not support `[*]' array declarators");
2507 if (vla_unspec_p)
2508 warning ("GCC does not yet properly implement `[*]' array declarators");
2509 return decl;
2512 /* Set the type of an array declarator. DECL is the declarator, as
2513 constructed by build_array_declarator; TYPE is what appears on the left
2514 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2515 abstract declarator, zero otherwise; this is used to reject static and
2516 type qualifiers in abstract declarators, where they are not in the
2517 C99 grammar. */
2519 tree
2520 set_array_declarator_type (tree decl, tree type, int abstract_p)
2522 TREE_OPERAND (decl, 0) = type;
2523 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2524 error ("static or type qualifiers in abstract declarator");
2525 return decl;
2528 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2530 tree
2531 groktypename (tree typename)
2533 tree specs, attrs;
2535 if (TREE_CODE (typename) != TREE_LIST)
2536 return typename;
2538 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2540 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2541 NULL);
2543 /* Apply attributes. */
2544 decl_attributes (&typename, attrs, 0);
2546 return typename;
2549 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2551 tree
2552 groktypename_in_parm_context (tree typename)
2554 if (TREE_CODE (typename) != TREE_LIST)
2555 return typename;
2556 return grokdeclarator (TREE_VALUE (typename),
2557 TREE_PURPOSE (typename),
2558 PARM, 0, NULL);
2561 /* Decode a declarator in an ordinary declaration or data definition.
2562 This is called as soon as the type information and variable name
2563 have been parsed, before parsing the initializer if any.
2564 Here we create the ..._DECL node, fill in its type,
2565 and put it on the list of decls for the current context.
2566 The ..._DECL node is returned as the value.
2568 Exception: for arrays where the length is not specified,
2569 the type is left null, to be filled in by `finish_decl'.
2571 Function definitions do not come here; they go to start_function
2572 instead. However, external and forward declarations of functions
2573 do go through here. Structure field declarations are done by
2574 grokfield and not through here. */
2576 tree
2577 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2579 tree decl;
2580 tree tem;
2582 /* An object declared as __attribute__((deprecated)) suppresses
2583 warnings of uses of other deprecated items. */
2584 if (lookup_attribute ("deprecated", attributes))
2585 deprecated_state = DEPRECATED_SUPPRESS;
2587 decl = grokdeclarator (declarator, declspecs,
2588 NORMAL, initialized, NULL);
2590 deprecated_state = DEPRECATED_NORMAL;
2592 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2593 && MAIN_NAME_P (DECL_NAME (decl)))
2594 warning ("%J'%D' is usually a function", decl, decl);
2596 if (initialized)
2597 /* Is it valid for this decl to have an initializer at all?
2598 If not, set INITIALIZED to zero, which will indirectly
2599 tell 'finish_decl' to ignore the initializer once it is parsed. */
2600 switch (TREE_CODE (decl))
2602 case TYPE_DECL:
2603 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2604 initialized = 0;
2605 break;
2607 case FUNCTION_DECL:
2608 error ("function '%D' is initialized like a variable", decl);
2609 initialized = 0;
2610 break;
2612 case PARM_DECL:
2613 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2614 error ("parameter '%D' is initialized", decl);
2615 initialized = 0;
2616 break;
2618 default:
2619 /* Don't allow initializations for incomplete types except for
2620 arrays which might be completed by the initialization. */
2622 /* This can happen if the array size is an undefined macro.
2623 We already gave a warning, so we don't need another one. */
2624 if (TREE_TYPE (decl) == error_mark_node)
2625 initialized = 0;
2626 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2628 /* A complete type is ok if size is fixed. */
2630 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2631 || C_DECL_VARIABLE_SIZE (decl))
2633 error ("variable-sized object may not be initialized");
2634 initialized = 0;
2637 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2639 error ("variable '%D' has initializer but incomplete type", decl);
2640 initialized = 0;
2642 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2644 error ("elements of array '%D' have incomplete type", decl);
2645 initialized = 0;
2649 if (initialized)
2651 DECL_EXTERNAL (decl) = 0;
2652 if (current_scope == file_scope)
2653 TREE_STATIC (decl) = 1;
2655 /* Tell 'pushdecl' this is an initialized decl
2656 even though we don't yet have the initializer expression.
2657 Also tell 'finish_decl' it may store the real initializer. */
2658 DECL_INITIAL (decl) = error_mark_node;
2661 /* If this is a function declaration, write a record describing it to the
2662 prototypes file (if requested). */
2664 if (TREE_CODE (decl) == FUNCTION_DECL)
2665 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2667 /* ANSI specifies that a tentative definition which is not merged with
2668 a non-tentative definition behaves exactly like a definition with an
2669 initializer equal to zero. (Section 3.7.2)
2671 -fno-common gives strict ANSI behavior, though this tends to break
2672 a large body of code that grew up without this rule.
2674 Thread-local variables are never common, since there's no entrenched
2675 body of code to break, and it allows more efficient variable references
2676 in the presence of dynamic linking. */
2678 if (TREE_CODE (decl) == VAR_DECL
2679 && !initialized
2680 && TREE_PUBLIC (decl)
2681 && !DECL_THREAD_LOCAL (decl)
2682 && !flag_no_common)
2683 DECL_COMMON (decl) = 1;
2685 /* Set attributes here so if duplicate decl, will have proper attributes. */
2686 decl_attributes (&decl, attributes, 0);
2688 if (TREE_CODE (decl) == FUNCTION_DECL
2689 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2691 tree ce = declarator;
2693 if (TREE_CODE (ce) == INDIRECT_REF)
2694 ce = TREE_OPERAND (declarator, 0);
2695 if (TREE_CODE (ce) == CALL_EXPR)
2697 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2698 for (; args; args = TREE_CHAIN (args))
2700 tree type = TREE_TYPE (args);
2701 if (INTEGRAL_TYPE_P (type)
2702 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2703 DECL_ARG_TYPE (args) = integer_type_node;
2708 if (TREE_CODE (decl) == FUNCTION_DECL
2709 && DECL_DECLARED_INLINE_P (decl)
2710 && DECL_UNINLINABLE (decl)
2711 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2712 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2714 /* Add this decl to the current scope.
2715 TEM may equal DECL or it may be a previous decl of the same name. */
2716 tem = pushdecl (decl);
2718 return tem;
2721 /* Finish processing of a declaration;
2722 install its initial value.
2723 If the length of an array type is not known before,
2724 it must be determined now, from the initial value, or it is an error. */
2726 void
2727 finish_decl (tree decl, tree init, tree asmspec_tree)
2729 tree type = TREE_TYPE (decl);
2730 int was_incomplete = (DECL_SIZE (decl) == 0);
2731 const char *asmspec = 0;
2733 /* If a name was specified, get the string. */
2734 if (current_scope == file_scope)
2735 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2736 if (asmspec_tree)
2737 asmspec = TREE_STRING_POINTER (asmspec_tree);
2739 /* If `start_decl' didn't like having an initialization, ignore it now. */
2740 if (init != 0 && DECL_INITIAL (decl) == 0)
2741 init = 0;
2743 /* Don't crash if parm is initialized. */
2744 if (TREE_CODE (decl) == PARM_DECL)
2745 init = 0;
2747 if (init)
2748 store_init_value (decl, init);
2750 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2751 || TREE_CODE (decl) == FUNCTION_DECL
2752 || TREE_CODE (decl) == FIELD_DECL))
2753 objc_check_decl (decl);
2755 /* Deduce size of array from initialization, if not already known. */
2756 if (TREE_CODE (type) == ARRAY_TYPE
2757 && TYPE_DOMAIN (type) == 0
2758 && TREE_CODE (decl) != TYPE_DECL)
2760 int do_default
2761 = (TREE_STATIC (decl)
2762 /* Even if pedantic, an external linkage array
2763 may have incomplete type at first. */
2764 ? pedantic && !TREE_PUBLIC (decl)
2765 : !DECL_EXTERNAL (decl));
2766 int failure
2767 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2769 /* Get the completed type made by complete_array_type. */
2770 type = TREE_TYPE (decl);
2772 if (failure == 1)
2773 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2775 else if (failure == 2)
2777 if (do_default)
2778 error ("%Jarray size missing in '%D'", decl, decl);
2779 /* If a `static' var's size isn't known,
2780 make it extern as well as static, so it does not get
2781 allocated.
2782 If it is not `static', then do not mark extern;
2783 finish_incomplete_decl will give it a default size
2784 and it will get allocated. */
2785 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2786 DECL_EXTERNAL (decl) = 1;
2789 /* TYPE_MAX_VALUE is always one less than the number of elements
2790 in the array, because we start counting at zero. Therefore,
2791 warn only if the value is less than zero. */
2792 else if (pedantic && TYPE_DOMAIN (type) != 0
2793 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2794 error ("%Jzero or negative size array '%D'", decl, decl);
2796 layout_decl (decl, 0);
2799 if (TREE_CODE (decl) == VAR_DECL)
2801 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2802 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2803 layout_decl (decl, 0);
2805 if (DECL_SIZE (decl) == 0
2806 /* Don't give an error if we already gave one earlier. */
2807 && TREE_TYPE (decl) != error_mark_node
2808 && (TREE_STATIC (decl)
2810 /* A static variable with an incomplete type
2811 is an error if it is initialized.
2812 Also if it is not file scope.
2813 Otherwise, let it through, but if it is not `extern'
2814 then it may cause an error message later. */
2815 (DECL_INITIAL (decl) != 0
2816 || !DECL_FILE_SCOPE_P (decl))
2818 /* An automatic variable with an incomplete type
2819 is an error. */
2820 !DECL_EXTERNAL (decl)))
2822 error ("%Jstorage size of '%D' isn't known", decl, decl);
2823 TREE_TYPE (decl) = error_mark_node;
2826 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2827 && DECL_SIZE (decl) != 0)
2829 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2830 constant_expression_warning (DECL_SIZE (decl));
2831 else
2832 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2835 if (TREE_USED (type))
2836 TREE_USED (decl) = 1;
2839 /* If this is a function and an assembler name is specified, reset DECL_RTL
2840 so we can give it its new name. Also, update built_in_decls if it
2841 was a normal built-in. */
2842 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2844 /* ASMSPEC is given, and not the name of a register. Mark the
2845 name with a star so assemble_name won't munge it. */
2846 char *starred = alloca (strlen (asmspec) + 2);
2847 starred[0] = '*';
2848 strcpy (starred + 1, asmspec);
2850 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2852 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2853 SET_DECL_RTL (builtin, NULL_RTX);
2854 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2855 #ifdef TARGET_MEM_FUNCTIONS
2856 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2857 init_block_move_fn (starred);
2858 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2859 init_block_clear_fn (starred);
2860 #else
2861 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2862 init_block_move_fn (starred);
2863 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2864 init_block_clear_fn (starred);
2865 #endif
2867 SET_DECL_RTL (decl, NULL_RTX);
2868 change_decl_assembler_name (decl, get_identifier (starred));
2871 /* If #pragma weak was used, mark the decl weak now. */
2872 if (current_scope == file_scope)
2873 maybe_apply_pragma_weak (decl);
2875 /* Output the assembler code and/or RTL code for variables and functions,
2876 unless the type is an undefined structure or union.
2877 If not, it will get done when the type is completed. */
2879 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2881 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2882 if (c_dialect_objc ())
2883 objc_check_decl (decl);
2885 if (DECL_FILE_SCOPE_P (decl))
2887 if (DECL_INITIAL (decl) == NULL_TREE
2888 || DECL_INITIAL (decl) == error_mark_node)
2889 /* Don't output anything
2890 when a tentative file-scope definition is seen.
2891 But at end of compilation, do output code for them. */
2892 DECL_DEFER_OUTPUT (decl) = 1;
2893 rest_of_decl_compilation (decl, asmspec, true, 0);
2895 else
2897 /* This is a local variable. If there is an ASMSPEC, the
2898 user has requested that we handle it specially. */
2899 if (asmspec)
2901 /* In conjunction with an ASMSPEC, the `register'
2902 keyword indicates that we should place the variable
2903 in a particular register. */
2904 if (DECL_REGISTER (decl))
2905 DECL_C_HARD_REGISTER (decl) = 1;
2907 /* If this is not a static variable, issue a warning.
2908 It doesn't make any sense to give an ASMSPEC for an
2909 ordinary, non-register local variable. Historically,
2910 GCC has accepted -- but ignored -- the ASMSPEC in
2911 this case. */
2912 if (TREE_CODE (decl) == VAR_DECL
2913 && !DECL_REGISTER (decl)
2914 && !TREE_STATIC (decl))
2915 warning ("%Jignoring asm-specifier for non-static local "
2916 "variable '%D'", decl, decl);
2917 else
2918 change_decl_assembler_name (decl, get_identifier (asmspec));
2921 if (TREE_CODE (decl) != FUNCTION_DECL)
2922 add_decl_stmt (decl);
2925 if (!DECL_FILE_SCOPE_P (decl))
2927 /* Recompute the RTL of a local array now
2928 if it used to be an incomplete type. */
2929 if (was_incomplete
2930 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2932 /* If we used it already as memory, it must stay in memory. */
2933 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2934 /* If it's still incomplete now, no init will save it. */
2935 if (DECL_SIZE (decl) == 0)
2936 DECL_INITIAL (decl) = 0;
2941 /* If this was marked 'used', be sure it will be output. */
2942 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2943 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2945 if (TREE_CODE (decl) == TYPE_DECL)
2946 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2948 /* At the end of a declaration, throw away any variable type sizes
2949 of types defined inside that declaration. There is no use
2950 computing them in the following function definition. */
2951 if (current_scope == file_scope)
2952 get_pending_sizes ();
2954 /* Install a cleanup (aka destructor) if one was given. */
2955 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2957 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
2958 if (attr)
2960 static bool eh_initialized_p;
2962 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
2963 tree cleanup_decl = lookup_name (cleanup_id);
2964 tree cleanup;
2966 /* Build "cleanup(&decl)" for the destructor. */
2967 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
2968 cleanup = build_tree_list (NULL_TREE, cleanup);
2969 cleanup = build_function_call (cleanup_decl, cleanup);
2971 /* Don't warn about decl unused; the cleanup uses it. */
2972 TREE_USED (decl) = 1;
2974 /* Initialize EH, if we've been told to do so. */
2975 if (flag_exceptions && !eh_initialized_p)
2977 eh_initialized_p = true;
2978 eh_personality_libfunc
2979 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
2980 ? "__gcc_personality_sj0"
2981 : "__gcc_personality_v0");
2982 using_eh_for_cleanups ();
2985 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
2990 /* Given a parsed parameter declaration, decode it into a PARM_DECL
2991 and push that on the current scope. */
2993 void
2994 push_parm_decl (tree parm)
2996 tree decl;
2998 /* Don't attempt to expand sizes while parsing this decl.
2999 (We can get here with i_s_e 1 somehow from Objective-C.) */
3000 int save_immediate_size_expand = immediate_size_expand;
3001 immediate_size_expand = 0;
3003 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3004 TREE_PURPOSE (TREE_PURPOSE (parm)),
3005 PARM, 0, NULL);
3006 decl_attributes (&decl, TREE_VALUE (parm), 0);
3008 decl = pushdecl (decl);
3010 finish_decl (decl, NULL_TREE, NULL_TREE);
3012 immediate_size_expand = save_immediate_size_expand;
3015 /* Mark all the parameter declarations to date as forward decls.
3016 Also diagnose use of this extension. */
3018 void
3019 mark_forward_parm_decls (void)
3021 struct c_binding *b;
3023 if (pedantic && !current_scope->warned_forward_parm_decls)
3025 pedwarn ("ISO C forbids forward parameter declarations");
3026 current_scope->warned_forward_parm_decls = true;
3029 for (b = current_scope->bindings; b; b = b->prev)
3030 if (TREE_CODE (b->decl) == PARM_DECL)
3031 TREE_ASM_WRITTEN (b->decl) = 1;
3034 static GTY(()) int compound_literal_number;
3036 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3037 literal, which may be an incomplete array type completed by the
3038 initializer; INIT is a CONSTRUCTOR that initializes the compound
3039 literal. */
3041 tree
3042 build_compound_literal (tree type, tree init)
3044 /* We do not use start_decl here because we have a type, not a declarator;
3045 and do not use finish_decl because the decl should be stored inside
3046 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3047 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3048 tree complit;
3049 tree stmt;
3050 DECL_EXTERNAL (decl) = 0;
3051 TREE_PUBLIC (decl) = 0;
3052 TREE_STATIC (decl) = (current_scope == file_scope);
3053 DECL_CONTEXT (decl) = current_function_decl;
3054 TREE_USED (decl) = 1;
3055 TREE_TYPE (decl) = type;
3056 TREE_READONLY (decl) = TREE_READONLY (type);
3057 store_init_value (decl, init);
3059 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3061 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3062 if (failure)
3063 abort ();
3066 type = TREE_TYPE (decl);
3067 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3068 return error_mark_node;
3070 stmt = build_stmt (DECL_STMT, decl);
3071 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3072 TREE_SIDE_EFFECTS (complit) = 1;
3074 layout_decl (decl, 0);
3076 if (TREE_STATIC (decl))
3078 /* This decl needs a name for the assembler output. We also need
3079 a unique suffix to be added to the name. */
3080 char *name;
3082 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3083 compound_literal_number);
3084 compound_literal_number++;
3085 DECL_NAME (decl) = get_identifier (name);
3086 DECL_DEFER_OUTPUT (decl) = 1;
3087 DECL_COMDAT (decl) = 1;
3088 DECL_ARTIFICIAL (decl) = 1;
3089 pushdecl (decl);
3090 rest_of_decl_compilation (decl, NULL, 1, 0);
3093 return complit;
3096 /* Make TYPE a complete type based on INITIAL_VALUE.
3097 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3098 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3101 complete_array_type (tree type, tree initial_value, int do_default)
3103 tree maxindex = NULL_TREE;
3104 int value = 0;
3106 if (initial_value)
3108 /* Note MAXINDEX is really the maximum index,
3109 one less than the size. */
3110 if (TREE_CODE (initial_value) == STRING_CST)
3112 int eltsize
3113 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3114 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3115 / eltsize) - 1, 0);
3117 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3119 tree elts = CONSTRUCTOR_ELTS (initial_value);
3120 maxindex = build_int_2 (-1, -1);
3121 for (; elts; elts = TREE_CHAIN (elts))
3123 if (TREE_PURPOSE (elts))
3124 maxindex = TREE_PURPOSE (elts);
3125 else
3126 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3127 maxindex, integer_one_node));
3129 maxindex = copy_node (maxindex);
3131 else
3133 /* Make an error message unless that happened already. */
3134 if (initial_value != error_mark_node)
3135 value = 1;
3137 /* Prevent further error messages. */
3138 maxindex = build_int_2 (0, 0);
3142 if (!maxindex)
3144 if (do_default)
3145 maxindex = build_int_2 (0, 0);
3146 value = 2;
3149 if (maxindex)
3151 TYPE_DOMAIN (type) = build_index_type (maxindex);
3152 if (!TREE_TYPE (maxindex))
3153 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3156 /* Lay out the type now that we can get the real answer. */
3158 layout_type (type);
3160 return value;
3163 /* Determine whether TYPE is a structure with a flexible array member,
3164 or a union containing such a structure (possibly recursively). */
3166 static bool
3167 flexible_array_type_p (tree type)
3169 tree x;
3170 switch (TREE_CODE (type))
3172 case RECORD_TYPE:
3173 x = TYPE_FIELDS (type);
3174 if (x == NULL_TREE)
3175 return false;
3176 while (TREE_CHAIN (x) != NULL_TREE)
3177 x = TREE_CHAIN (x);
3178 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3179 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3180 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3181 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3182 return true;
3183 return false;
3184 case UNION_TYPE:
3185 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3187 if (flexible_array_type_p (TREE_TYPE (x)))
3188 return true;
3190 return false;
3191 default:
3192 return false;
3196 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3197 replacing with appropriate values if they are invalid. */
3198 static void
3199 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3201 tree type_mv;
3202 unsigned int max_width;
3203 unsigned HOST_WIDE_INT w;
3204 const char *name = orig_name ? orig_name: _("<anonymous>");
3206 /* Necessary? */
3207 STRIP_NOPS (*width);
3209 /* Detect and ignore out of range field width and process valid
3210 field widths. */
3211 if (TREE_CODE (*width) != INTEGER_CST)
3213 error ("bit-field `%s' width not an integer constant", name);
3214 *width = integer_one_node;
3216 else
3218 constant_expression_warning (*width);
3219 if (tree_int_cst_sgn (*width) < 0)
3221 error ("negative width in bit-field `%s'", name);
3222 *width = integer_one_node;
3224 else if (integer_zerop (*width) && orig_name)
3226 error ("zero width for bit-field `%s'", name);
3227 *width = integer_one_node;
3231 /* Detect invalid bit-field type. */
3232 if (TREE_CODE (*type) != INTEGER_TYPE
3233 && TREE_CODE (*type) != BOOLEAN_TYPE
3234 && TREE_CODE (*type) != ENUMERAL_TYPE)
3236 error ("bit-field `%s' has invalid type", name);
3237 *type = unsigned_type_node;
3240 type_mv = TYPE_MAIN_VARIANT (*type);
3241 if (pedantic
3242 && type_mv != integer_type_node
3243 && type_mv != unsigned_type_node
3244 && type_mv != boolean_type_node)
3245 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3247 if (type_mv == boolean_type_node)
3248 max_width = CHAR_TYPE_SIZE;
3249 else
3250 max_width = TYPE_PRECISION (*type);
3252 if (0 < compare_tree_int (*width, max_width))
3254 error ("width of `%s' exceeds its type", name);
3255 w = max_width;
3256 *width = build_int_2 (w, 0);
3258 else
3259 w = tree_low_cst (*width, 1);
3261 if (TREE_CODE (*type) == ENUMERAL_TYPE
3262 && (w < min_precision (TYPE_MIN_VALUE (*type), TREE_UNSIGNED (*type))
3263 || w < min_precision (TYPE_MAX_VALUE (*type), TREE_UNSIGNED (*type))))
3264 warning ("`%s' is narrower than values of its type", name);
3267 /* Given declspecs and a declarator,
3268 determine the name and type of the object declared
3269 and construct a ..._DECL node for it.
3270 (In one case we can return a ..._TYPE node instead.
3271 For invalid input we sometimes return 0.)
3273 DECLSPECS is a chain of tree_list nodes whose value fields
3274 are the storage classes and type specifiers.
3276 DECL_CONTEXT says which syntactic context this declaration is in:
3277 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3278 FUNCDEF for a function definition. Like NORMAL but a few different
3279 error messages in each case. Return value may be zero meaning
3280 this definition is too screwy to try to parse.
3281 PARM for a parameter declaration (either within a function prototype
3282 or before a function body). Make a PARM_DECL, or return void_type_node.
3283 TYPENAME if for a typename (in a cast or sizeof).
3284 Don't make a DECL node; just return the ..._TYPE node.
3285 FIELD for a struct or union field; make a FIELD_DECL.
3286 INITIALIZED is 1 if the decl has an initializer.
3287 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3288 representing the width of the bit-field.
3290 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3291 It may also be so in the PARM case, for a prototype where the
3292 argument type is specified but not the name.
3294 This function is where the complicated C meanings of `static'
3295 and `extern' are interpreted. */
3297 static tree
3298 grokdeclarator (tree declarator, tree declspecs,
3299 enum decl_context decl_context, int initialized, tree *width)
3301 int specbits = 0;
3302 tree spec;
3303 tree type = NULL_TREE;
3304 int longlong = 0;
3305 int constp;
3306 int restrictp;
3307 int volatilep;
3308 int type_quals = TYPE_UNQUALIFIED;
3309 int inlinep;
3310 int explicit_int = 0;
3311 int explicit_char = 0;
3312 int defaulted_int = 0;
3313 tree typedef_decl = 0;
3314 const char *name, *orig_name;
3315 tree typedef_type = 0;
3316 int funcdef_flag = 0;
3317 enum tree_code innermost_code = ERROR_MARK;
3318 int size_varies = 0;
3319 tree decl_attr = NULL_TREE;
3320 tree array_ptr_quals = NULL_TREE;
3321 int array_parm_static = 0;
3322 tree returned_attrs = NULL_TREE;
3323 bool bitfield = width != NULL;
3324 tree element_type;
3325 tree arg_info = NULL_TREE;
3327 if (decl_context == FUNCDEF)
3328 funcdef_flag = 1, decl_context = NORMAL;
3330 /* Look inside a declarator for the name being declared
3331 and get it as a string, for an error message. */
3333 tree decl = declarator;
3334 name = 0;
3336 while (decl)
3337 switch (TREE_CODE (decl))
3339 case ARRAY_REF:
3340 case INDIRECT_REF:
3341 case CALL_EXPR:
3342 innermost_code = TREE_CODE (decl);
3343 decl = TREE_OPERAND (decl, 0);
3344 break;
3346 case TREE_LIST:
3347 decl = TREE_VALUE (decl);
3348 break;
3350 case IDENTIFIER_NODE:
3351 name = IDENTIFIER_POINTER (decl);
3352 decl = 0;
3353 break;
3355 default:
3356 abort ();
3358 orig_name = name;
3359 if (name == 0)
3360 name = "type name";
3363 /* A function definition's declarator must have the form of
3364 a function declarator. */
3366 if (funcdef_flag && innermost_code != CALL_EXPR)
3367 return 0;
3369 /* If this looks like a function definition, make it one,
3370 even if it occurs where parms are expected.
3371 Then store_parm_decls will reject it and not use it as a parm. */
3372 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3373 decl_context = PARM;
3375 /* Look through the decl specs and record which ones appear.
3376 Some typespecs are defined as built-in typenames.
3377 Others, the ones that are modifiers of other types,
3378 are represented by bits in SPECBITS: set the bits for
3379 the modifiers that appear. Storage class keywords are also in SPECBITS.
3381 If there is a typedef name or a type, store the type in TYPE.
3382 This includes builtin typedefs such as `int'.
3384 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3385 and did not come from a user typedef.
3387 Set LONGLONG if `long' is mentioned twice. */
3389 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3391 tree id = TREE_VALUE (spec);
3393 /* If the entire declaration is itself tagged as deprecated then
3394 suppress reports of deprecated items. */
3395 if (id && TREE_DEPRECATED (id))
3397 if (deprecated_state != DEPRECATED_SUPPRESS)
3398 warn_deprecated_use (id);
3401 if (id == ridpointers[(int) RID_INT])
3402 explicit_int = 1;
3403 if (id == ridpointers[(int) RID_CHAR])
3404 explicit_char = 1;
3406 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3408 enum rid i = C_RID_CODE (id);
3409 if ((int) i <= (int) RID_LAST_MODIFIER)
3411 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3413 if (longlong)
3414 error ("`long long long' is too long for GCC");
3415 else
3417 if (pedantic && !flag_isoc99 && ! in_system_header
3418 && warn_long_long)
3419 pedwarn ("ISO C90 does not support `long long'");
3420 longlong = 1;
3423 else if (specbits & (1 << (int) i))
3425 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3427 if (pedantic && !flag_isoc99)
3428 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3430 else
3431 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3434 /* Diagnose "__thread extern". Recall that this list
3435 is in the reverse order seen in the text. */
3436 if (i == RID_THREAD
3437 && (specbits & (1 << (int) RID_EXTERN
3438 | 1 << (int) RID_STATIC)))
3440 if (specbits & 1 << (int) RID_EXTERN)
3441 error ("`__thread' before `extern'");
3442 else
3443 error ("`__thread' before `static'");
3446 specbits |= 1 << (int) i;
3447 goto found;
3450 if (type)
3451 error ("two or more data types in declaration of `%s'", name);
3452 /* Actual typedefs come to us as TYPE_DECL nodes. */
3453 else if (TREE_CODE (id) == TYPE_DECL)
3455 if (TREE_TYPE (id) == error_mark_node)
3456 ; /* Allow the type to default to int to avoid cascading errors. */
3457 else
3459 type = TREE_TYPE (id);
3460 decl_attr = DECL_ATTRIBUTES (id);
3461 typedef_decl = id;
3464 /* Built-in types come as identifiers. */
3465 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3467 tree t = lookup_name (id);
3468 if (!t || TREE_CODE (t) != TYPE_DECL)
3469 error ("`%s' fails to be a typedef or built in type",
3470 IDENTIFIER_POINTER (id));
3471 else if (TREE_TYPE (t) == error_mark_node)
3473 else
3475 type = TREE_TYPE (t);
3476 typedef_decl = t;
3479 else if (TREE_CODE (id) != ERROR_MARK)
3480 type = id;
3482 found:
3486 typedef_type = type;
3487 if (type)
3488 size_varies = C_TYPE_VARIABLE_SIZE (type);
3490 /* No type at all: default to `int', and set DEFAULTED_INT
3491 because it was not a user-defined typedef. */
3493 if (type == 0)
3495 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3496 | (1 << (int) RID_SIGNED)
3497 | (1 << (int) RID_UNSIGNED)
3498 | (1 << (int) RID_COMPLEX))))
3499 /* Don't warn about typedef foo = bar. */
3500 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3501 && ! in_system_header)
3503 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3504 and this is a function, or if -Wimplicit; prefer the former
3505 warning since it is more explicit. */
3506 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3507 && funcdef_flag)
3508 warn_about_return_type = 1;
3509 else if (warn_implicit_int || flag_isoc99)
3510 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3511 name);
3514 defaulted_int = 1;
3515 type = integer_type_node;
3518 /* Now process the modifiers that were specified
3519 and check for invalid combinations. */
3521 /* Long double is a special combination. */
3523 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3524 && TYPE_MAIN_VARIANT (type) == double_type_node)
3526 specbits &= ~(1 << (int) RID_LONG);
3527 type = long_double_type_node;
3530 /* Check all other uses of type modifiers. */
3532 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3533 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3535 int ok = 0;
3537 if ((specbits & 1 << (int) RID_LONG)
3538 && (specbits & 1 << (int) RID_SHORT))
3539 error ("both long and short specified for `%s'", name);
3540 else if (((specbits & 1 << (int) RID_LONG)
3541 || (specbits & 1 << (int) RID_SHORT))
3542 && explicit_char)
3543 error ("long or short specified with char for `%s'", name);
3544 else if (((specbits & 1 << (int) RID_LONG)
3545 || (specbits & 1 << (int) RID_SHORT))
3546 && TREE_CODE (type) == REAL_TYPE)
3548 static int already = 0;
3550 error ("long or short specified with floating type for `%s'", name);
3551 if (! already && ! pedantic)
3553 error ("the only valid combination is `long double'");
3554 already = 1;
3557 else if ((specbits & 1 << (int) RID_SIGNED)
3558 && (specbits & 1 << (int) RID_UNSIGNED))
3559 error ("both signed and unsigned specified for `%s'", name);
3560 else if (TREE_CODE (type) != INTEGER_TYPE)
3561 error ("long, short, signed or unsigned invalid for `%s'", name);
3562 else
3564 ok = 1;
3565 if (!explicit_int && !defaulted_int && !explicit_char)
3567 error ("long, short, signed or unsigned used invalidly for `%s'",
3568 name);
3569 ok = 0;
3573 /* Discard the type modifiers if they are invalid. */
3574 if (! ok)
3576 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3577 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3578 longlong = 0;
3582 if ((specbits & (1 << (int) RID_COMPLEX))
3583 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3585 error ("complex invalid for `%s'", name);
3586 specbits &= ~(1 << (int) RID_COMPLEX);
3589 /* Decide whether an integer type is signed or not.
3590 Optionally treat bit-fields as signed by default. */
3591 if (specbits & 1 << (int) RID_UNSIGNED
3592 || (bitfield && ! flag_signed_bitfields
3593 && (explicit_int || defaulted_int || explicit_char
3594 /* A typedef for plain `int' without `signed'
3595 can be controlled just like plain `int'. */
3596 || ! (typedef_decl != 0
3597 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3598 && TREE_CODE (type) != ENUMERAL_TYPE
3599 && !(specbits & 1 << (int) RID_SIGNED)))
3601 if (longlong)
3602 type = long_long_unsigned_type_node;
3603 else if (specbits & 1 << (int) RID_LONG)
3604 type = long_unsigned_type_node;
3605 else if (specbits & 1 << (int) RID_SHORT)
3606 type = short_unsigned_type_node;
3607 else if (type == char_type_node)
3608 type = unsigned_char_type_node;
3609 else if (typedef_decl)
3610 type = c_common_unsigned_type (type);
3611 else
3612 type = unsigned_type_node;
3614 else if ((specbits & 1 << (int) RID_SIGNED)
3615 && type == char_type_node)
3616 type = signed_char_type_node;
3617 else if (longlong)
3618 type = long_long_integer_type_node;
3619 else if (specbits & 1 << (int) RID_LONG)
3620 type = long_integer_type_node;
3621 else if (specbits & 1 << (int) RID_SHORT)
3622 type = short_integer_type_node;
3624 if (specbits & 1 << (int) RID_COMPLEX)
3626 if (pedantic && !flag_isoc99)
3627 pedwarn ("ISO C90 does not support complex types");
3628 /* If we just have "complex", it is equivalent to
3629 "complex double", but if any modifiers at all are specified it is
3630 the complex form of TYPE. E.g, "complex short" is
3631 "complex short int". */
3633 if (defaulted_int && ! longlong
3634 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3635 | (1 << (int) RID_SIGNED)
3636 | (1 << (int) RID_UNSIGNED))))
3638 if (pedantic)
3639 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3640 type = complex_double_type_node;
3642 else if (type == integer_type_node)
3644 if (pedantic)
3645 pedwarn ("ISO C does not support complex integer types");
3646 type = complex_integer_type_node;
3648 else if (type == float_type_node)
3649 type = complex_float_type_node;
3650 else if (type == double_type_node)
3651 type = complex_double_type_node;
3652 else if (type == long_double_type_node)
3653 type = complex_long_double_type_node;
3654 else
3656 if (pedantic)
3657 pedwarn ("ISO C does not support complex integer types");
3658 type = build_complex_type (type);
3662 /* Check the type and width of a bit-field. */
3663 if (bitfield)
3664 check_bitfield_type_and_width (&type, width, orig_name);
3666 /* Figure out the type qualifiers for the declaration. There are
3667 two ways a declaration can become qualified. One is something
3668 like `const int i' where the `const' is explicit. Another is
3669 something like `typedef const int CI; CI i' where the type of the
3670 declaration contains the `const'. A third possibility is that
3671 there is a type qualifier on the element type of a typedefed
3672 array type, in which case we should extract that qualifier so
3673 that c_apply_type_quals_to_decls receives the full list of
3674 qualifiers to work with (C90 is not entirely clear about whether
3675 duplicate qualifiers should be diagnosed in this case, but it
3676 seems most appropriate to do so). */
3677 element_type = strip_array_types (type);
3678 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3679 restrictp
3680 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3681 volatilep
3682 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3683 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3684 if (pedantic && !flag_isoc99)
3686 if (constp > 1)
3687 pedwarn ("duplicate `const'");
3688 if (restrictp > 1)
3689 pedwarn ("duplicate `restrict'");
3690 if (volatilep > 1)
3691 pedwarn ("duplicate `volatile'");
3693 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3694 type = TYPE_MAIN_VARIANT (type);
3695 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3696 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3697 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3699 /* Warn if two storage classes are given. Default to `auto'. */
3702 int nclasses = 0;
3704 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3705 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3706 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3707 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3708 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3710 /* "static __thread" and "extern __thread" are allowed. */
3711 if ((specbits & (1 << (int) RID_THREAD
3712 | 1 << (int) RID_STATIC
3713 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3714 nclasses++;
3716 /* Warn about storage classes that are invalid for certain
3717 kinds of declarations (parameters, typenames, etc.). */
3719 if (nclasses > 1)
3720 error ("multiple storage classes in declaration of `%s'", name);
3721 else if (funcdef_flag
3722 && (specbits
3723 & ((1 << (int) RID_REGISTER)
3724 | (1 << (int) RID_AUTO)
3725 | (1 << (int) RID_TYPEDEF)
3726 | (1 << (int) RID_THREAD))))
3728 if (specbits & 1 << (int) RID_AUTO
3729 && (pedantic || current_scope == file_scope))
3730 pedwarn ("function definition declared `auto'");
3731 if (specbits & 1 << (int) RID_REGISTER)
3732 error ("function definition declared `register'");
3733 if (specbits & 1 << (int) RID_TYPEDEF)
3734 error ("function definition declared `typedef'");
3735 if (specbits & 1 << (int) RID_THREAD)
3736 error ("function definition declared `__thread'");
3737 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3738 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3740 else if (decl_context != NORMAL && nclasses > 0)
3742 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3744 else
3746 switch (decl_context)
3748 case FIELD:
3749 error ("storage class specified for structure field `%s'",
3750 name);
3751 break;
3752 case PARM:
3753 error ("storage class specified for parameter `%s'", name);
3754 break;
3755 default:
3756 error ("storage class specified for typename");
3757 break;
3759 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3760 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3761 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3764 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3766 /* `extern' with initialization is invalid if not at file scope. */
3767 if (current_scope == file_scope)
3768 warning ("`%s' initialized and declared `extern'", name);
3769 else
3770 error ("`%s' has both `extern' and initializer", name);
3772 else if (current_scope == file_scope)
3774 if (specbits & 1 << (int) RID_AUTO)
3775 error ("file-scope declaration of `%s' specifies `auto'", name);
3777 else
3779 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3780 error ("nested function `%s' declared `extern'", name);
3781 else if ((specbits & (1 << (int) RID_THREAD
3782 | 1 << (int) RID_EXTERN
3783 | 1 << (int) RID_STATIC))
3784 == (1 << (int) RID_THREAD))
3786 error ("function-scope `%s' implicitly auto and declared `__thread'",
3787 name);
3788 specbits &= ~(1 << (int) RID_THREAD);
3793 /* Now figure out the structure of the declarator proper.
3794 Descend through it, creating more complex types, until we reach
3795 the declared identifier (or NULL_TREE, in an absolute declarator). */
3797 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3799 if (type == error_mark_node)
3801 declarator = TREE_OPERAND (declarator, 0);
3802 continue;
3805 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3806 an INDIRECT_REF (for *...),
3807 a CALL_EXPR (for ...(...)),
3808 a TREE_LIST (for nested attributes),
3809 an identifier (for the name being declared)
3810 or a null pointer (for the place in an absolute declarator
3811 where the name was omitted).
3812 For the last two cases, we have just exited the loop.
3814 At this point, TYPE is the type of elements of an array,
3815 or for a function to return, or for a pointer to point to.
3816 After this sequence of ifs, TYPE is the type of the
3817 array or function or pointer, and DECLARATOR has had its
3818 outermost layer removed. */
3820 if (array_ptr_quals != NULL_TREE || array_parm_static)
3822 /* Only the innermost declarator (making a parameter be of
3823 array type which is converted to pointer type)
3824 may have static or type qualifiers. */
3825 error ("static or type qualifiers in non-parameter array declarator");
3826 array_ptr_quals = NULL_TREE;
3827 array_parm_static = 0;
3830 if (TREE_CODE (declarator) == TREE_LIST)
3832 /* We encode a declarator with embedded attributes using
3833 a TREE_LIST. */
3834 tree attrs = TREE_PURPOSE (declarator);
3835 tree inner_decl;
3836 int attr_flags = 0;
3837 declarator = TREE_VALUE (declarator);
3838 inner_decl = declarator;
3839 while (inner_decl != NULL_TREE
3840 && TREE_CODE (inner_decl) == TREE_LIST)
3841 inner_decl = TREE_VALUE (inner_decl);
3842 if (inner_decl == NULL_TREE
3843 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3844 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3845 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3846 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3847 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3848 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3849 returned_attrs = decl_attributes (&type,
3850 chainon (returned_attrs, attrs),
3851 attr_flags);
3853 else if (TREE_CODE (declarator) == ARRAY_REF)
3855 tree itype = NULL_TREE;
3856 tree size = TREE_OPERAND (declarator, 1);
3857 /* The index is a signed object `sizetype' bits wide. */
3858 tree index_type = c_common_signed_type (sizetype);
3860 array_ptr_quals = TREE_TYPE (declarator);
3861 array_parm_static = TREE_STATIC (declarator);
3863 declarator = TREE_OPERAND (declarator, 0);
3865 /* Check for some types that there cannot be arrays of. */
3867 if (VOID_TYPE_P (type))
3869 error ("declaration of `%s' as array of voids", name);
3870 type = error_mark_node;
3873 if (TREE_CODE (type) == FUNCTION_TYPE)
3875 error ("declaration of `%s' as array of functions", name);
3876 type = error_mark_node;
3879 if (pedantic && flexible_array_type_p (type))
3880 pedwarn ("invalid use of structure with flexible array member");
3882 if (size == error_mark_node)
3883 type = error_mark_node;
3885 if (type == error_mark_node)
3886 continue;
3888 /* If size was specified, set ITYPE to a range-type for that size.
3889 Otherwise, ITYPE remains null. finish_decl may figure it out
3890 from an initial value. */
3892 if (size)
3894 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3895 STRIP_TYPE_NOPS (size);
3897 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3899 error ("size of array `%s' has non-integer type", name);
3900 size = integer_one_node;
3903 if (pedantic && integer_zerop (size))
3904 pedwarn ("ISO C forbids zero-size array `%s'", name);
3906 if (TREE_CODE (size) == INTEGER_CST)
3908 constant_expression_warning (size);
3909 if (tree_int_cst_sgn (size) < 0)
3911 error ("size of array `%s' is negative", name);
3912 size = integer_one_node;
3915 else
3917 /* Make sure the array size remains visibly nonconstant
3918 even if it is (eg) a const variable with known value. */
3919 size_varies = 1;
3921 if (!flag_isoc99 && pedantic)
3923 if (TREE_CONSTANT (size))
3924 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3925 name);
3926 else
3927 pedwarn ("ISO C90 forbids variable-size array `%s'",
3928 name);
3932 if (integer_zerop (size))
3934 /* A zero-length array cannot be represented with an
3935 unsigned index type, which is what we'll get with
3936 build_index_type. Create an open-ended range instead. */
3937 itype = build_range_type (sizetype, size, NULL_TREE);
3939 else
3941 /* Compute the maximum valid index, that is, size - 1.
3942 Do the calculation in index_type, so that if it is
3943 a variable the computations will be done in the
3944 proper mode. */
3945 itype = fold (build (MINUS_EXPR, index_type,
3946 convert (index_type, size),
3947 convert (index_type, size_one_node)));
3949 /* If that overflowed, the array is too big.
3950 ??? While a size of INT_MAX+1 technically shouldn't
3951 cause an overflow (because we subtract 1), the overflow
3952 is recorded during the conversion to index_type, before
3953 the subtraction. Handling this case seems like an
3954 unnecessary complication. */
3955 if (TREE_OVERFLOW (itype))
3957 error ("size of array `%s' is too large", name);
3958 type = error_mark_node;
3959 continue;
3962 if (size_varies)
3964 /* We must be able to distinguish the
3965 SAVE_EXPR_CONTEXT for the variably-sized type
3966 so that we can set it correctly in
3967 set_save_expr_context. The convention is
3968 that all SAVE_EXPRs that need to be reset
3969 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
3970 tree cfd = current_function_decl;
3971 if (decl_context == PARM)
3972 current_function_decl = NULL_TREE;
3973 itype = variable_size (itype);
3974 if (decl_context == PARM)
3975 current_function_decl = cfd;
3977 itype = build_index_type (itype);
3980 else if (decl_context == FIELD)
3982 if (pedantic && !flag_isoc99 && !in_system_header)
3983 pedwarn ("ISO C90 does not support flexible array members");
3985 /* ISO C99 Flexible array members are effectively identical
3986 to GCC's zero-length array extension. */
3987 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
3990 /* If pedantic, complain about arrays of incomplete types. */
3992 if (pedantic && !COMPLETE_TYPE_P (type))
3993 pedwarn ("array type has incomplete element type");
3995 /* Build the array type itself, then merge any constancy or
3996 volatility into the target type. We must do it in this order
3997 to ensure that the TYPE_MAIN_VARIANT field of the array type
3998 is set correctly. */
4000 type = build_array_type (type, itype);
4001 if (type_quals)
4002 type = c_build_qualified_type (type, type_quals);
4004 if (size_varies)
4005 C_TYPE_VARIABLE_SIZE (type) = 1;
4007 /* The GCC extension for zero-length arrays differs from
4008 ISO flexible array members in that sizeof yields zero. */
4009 if (size && integer_zerop (size))
4011 layout_type (type);
4012 TYPE_SIZE (type) = bitsize_zero_node;
4013 TYPE_SIZE_UNIT (type) = size_zero_node;
4015 if (decl_context != PARM
4016 && (array_ptr_quals != NULL_TREE || array_parm_static))
4018 error ("static or type qualifiers in non-parameter array declarator");
4019 array_ptr_quals = NULL_TREE;
4020 array_parm_static = 0;
4023 else if (TREE_CODE (declarator) == CALL_EXPR)
4025 /* Say it's a definition only for the CALL_EXPR closest to
4026 the identifier. */
4027 bool really_funcdef = (funcdef_flag
4028 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4029 == IDENTIFIER_NODE));
4030 tree arg_types;
4032 /* Declaring a function type.
4033 Make sure we have a valid type for the function to return. */
4034 if (type == error_mark_node)
4035 continue;
4037 size_varies = 0;
4039 /* Warn about some types functions can't return. */
4041 if (TREE_CODE (type) == FUNCTION_TYPE)
4043 error ("`%s' declared as function returning a function", name);
4044 type = integer_type_node;
4046 if (TREE_CODE (type) == ARRAY_TYPE)
4048 error ("`%s' declared as function returning an array", name);
4049 type = integer_type_node;
4052 /* Construct the function type and go to the next
4053 inner layer of declarator. */
4054 arg_info = TREE_OPERAND (declarator, 1);
4055 arg_types = grokparms (arg_info, really_funcdef);
4057 /* Type qualifiers before the return type of the function
4058 qualify the return type, not the function type. */
4059 if (type_quals)
4061 /* Type qualifiers on a function return type are normally
4062 permitted by the standard but have no effect, so give a
4063 warning at -Wextra. Qualifiers on a void return type have
4064 meaning as a GNU extension, and are banned on function
4065 definitions in ISO C. FIXME: strictly we shouldn't
4066 pedwarn for qualified void return types except on function
4067 definitions, but not doing so could lead to the undesirable
4068 state of a "volatile void" function return type not being
4069 warned about, and a use of the function being compiled
4070 with GNU semantics, with no diagnostics under -pedantic. */
4071 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4072 pedwarn ("ISO C forbids qualified void function return type");
4073 else if (extra_warnings
4074 && !(VOID_TYPE_P (type)
4075 && type_quals == TYPE_QUAL_VOLATILE))
4076 warning ("type qualifiers ignored on function return type");
4078 type = c_build_qualified_type (type, type_quals);
4080 type_quals = TYPE_UNQUALIFIED;
4082 type = build_function_type (type, arg_types);
4083 declarator = TREE_OPERAND (declarator, 0);
4085 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4086 the formal parameter list of this FUNCTION_TYPE to point to
4087 the FUNCTION_TYPE node itself. */
4090 tree link;
4092 for (link = ARG_INFO_TAGS (arg_info);
4093 link;
4094 link = TREE_CHAIN (link))
4095 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4098 else if (TREE_CODE (declarator) == INDIRECT_REF)
4100 /* Merge any constancy or volatility into the target type
4101 for the pointer. */
4103 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4104 && type_quals)
4105 pedwarn ("ISO C forbids qualified function types");
4106 if (type_quals)
4107 type = c_build_qualified_type (type, type_quals);
4108 type_quals = TYPE_UNQUALIFIED;
4109 size_varies = 0;
4111 type = build_pointer_type (type);
4113 /* Process a list of type modifier keywords
4114 (such as const or volatile) that were given inside the `*'. */
4116 if (TREE_TYPE (declarator))
4118 tree typemodlist;
4119 int erred = 0;
4121 constp = 0;
4122 volatilep = 0;
4123 restrictp = 0;
4124 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4125 typemodlist = TREE_CHAIN (typemodlist))
4127 tree qualifier = TREE_VALUE (typemodlist);
4129 if (C_IS_RESERVED_WORD (qualifier))
4131 if (C_RID_CODE (qualifier) == RID_CONST)
4132 constp++;
4133 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4134 volatilep++;
4135 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4136 restrictp++;
4137 else
4138 erred++;
4140 else
4141 erred++;
4144 if (erred)
4145 error ("invalid type modifier within pointer declarator");
4146 if (pedantic && !flag_isoc99)
4148 if (constp > 1)
4149 pedwarn ("duplicate `const'");
4150 if (volatilep > 1)
4151 pedwarn ("duplicate `volatile'");
4152 if (restrictp > 1)
4153 pedwarn ("duplicate `restrict'");
4156 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4157 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4158 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4161 declarator = TREE_OPERAND (declarator, 0);
4163 else
4164 abort ();
4168 /* Now TYPE has the actual type. */
4170 /* Did array size calculations overflow? */
4172 if (TREE_CODE (type) == ARRAY_TYPE
4173 && COMPLETE_TYPE_P (type)
4174 && TREE_OVERFLOW (TYPE_SIZE (type)))
4176 error ("size of array `%s' is too large", name);
4177 /* If we proceed with the array type as it is, we'll eventually
4178 crash in tree_low_cst(). */
4179 type = error_mark_node;
4182 /* If this is declaring a typedef name, return a TYPE_DECL. */
4184 if (specbits & (1 << (int) RID_TYPEDEF))
4186 tree decl;
4187 /* Note that the grammar rejects storage classes
4188 in typenames, fields or parameters */
4189 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4190 && type_quals)
4191 pedwarn ("ISO C forbids qualified function types");
4192 if (type_quals)
4193 type = c_build_qualified_type (type, type_quals);
4194 decl = build_decl (TYPE_DECL, declarator, type);
4195 if ((specbits & (1 << (int) RID_SIGNED))
4196 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4197 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4198 decl_attributes (&decl, returned_attrs, 0);
4199 return decl;
4202 /* Detect the case of an array type of unspecified size
4203 which came, as such, direct from a typedef name.
4204 We must copy the type, so that each identifier gets
4205 a distinct type, so that each identifier's size can be
4206 controlled separately by its own initializer. */
4208 if (type != 0 && typedef_type != 0
4209 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4210 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4212 type = build_array_type (TREE_TYPE (type), 0);
4213 if (size_varies)
4214 C_TYPE_VARIABLE_SIZE (type) = 1;
4217 /* If this is a type name (such as, in a cast or sizeof),
4218 compute the type and return it now. */
4220 if (decl_context == TYPENAME)
4222 /* Note that the grammar rejects storage classes
4223 in typenames, fields or parameters */
4224 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4225 && type_quals)
4226 pedwarn ("ISO C forbids const or volatile function types");
4227 if (type_quals)
4228 type = c_build_qualified_type (type, type_quals);
4229 decl_attributes (&type, returned_attrs, 0);
4230 return type;
4233 /* Aside from typedefs and type names (handle above),
4234 `void' at top level (not within pointer)
4235 is allowed only in public variables.
4236 We don't complain about parms either, but that is because
4237 a better error message can be made later. */
4239 if (VOID_TYPE_P (type) && decl_context != PARM
4240 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4241 && ((specbits & (1 << (int) RID_EXTERN))
4242 || (current_scope == file_scope
4243 && !(specbits
4244 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4246 error ("variable or field `%s' declared void", name);
4247 type = integer_type_node;
4250 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4251 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4254 tree decl;
4256 if (decl_context == PARM)
4258 tree type_as_written;
4259 tree promoted_type;
4261 /* A parameter declared as an array of T is really a pointer to T.
4262 One declared as a function is really a pointer to a function. */
4264 if (TREE_CODE (type) == ARRAY_TYPE)
4266 /* Transfer const-ness of array into that of type pointed to. */
4267 type = TREE_TYPE (type);
4268 if (type_quals)
4269 type = c_build_qualified_type (type, type_quals);
4270 type = build_pointer_type (type);
4271 type_quals = TYPE_UNQUALIFIED;
4272 if (array_ptr_quals)
4274 tree new_ptr_quals, new_ptr_attrs;
4275 int erred = 0;
4276 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4277 /* We don't yet implement attributes in this context. */
4278 if (new_ptr_attrs != NULL_TREE)
4279 warning ("attributes in parameter array declarator ignored");
4281 constp = 0;
4282 volatilep = 0;
4283 restrictp = 0;
4284 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4286 tree qualifier = TREE_VALUE (new_ptr_quals);
4288 if (C_IS_RESERVED_WORD (qualifier))
4290 if (C_RID_CODE (qualifier) == RID_CONST)
4291 constp++;
4292 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4293 volatilep++;
4294 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4295 restrictp++;
4296 else
4297 erred++;
4299 else
4300 erred++;
4303 if (erred)
4304 error ("invalid type modifier within array declarator");
4306 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4307 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4308 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4310 size_varies = 0;
4312 else if (TREE_CODE (type) == FUNCTION_TYPE)
4314 if (pedantic && type_quals)
4315 pedwarn ("ISO C forbids qualified function types");
4316 if (type_quals)
4317 type = c_build_qualified_type (type, type_quals);
4318 type = build_pointer_type (type);
4319 type_quals = TYPE_UNQUALIFIED;
4321 else if (type_quals)
4322 type = c_build_qualified_type (type, type_quals);
4324 type_as_written = type;
4326 decl = build_decl (PARM_DECL, declarator, type);
4327 if (size_varies)
4328 C_DECL_VARIABLE_SIZE (decl) = 1;
4330 /* Compute the type actually passed in the parmlist,
4331 for the case where there is no prototype.
4332 (For example, shorts and chars are passed as ints.)
4333 When there is a prototype, this is overridden later. */
4335 if (type == error_mark_node)
4336 promoted_type = type;
4337 else
4338 promoted_type = c_type_promotes_to (type);
4340 DECL_ARG_TYPE (decl) = promoted_type;
4341 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4343 else if (decl_context == FIELD)
4345 /* Structure field. It may not be a function. */
4347 if (TREE_CODE (type) == FUNCTION_TYPE)
4349 error ("field `%s' declared as a function", name);
4350 type = build_pointer_type (type);
4352 else if (TREE_CODE (type) != ERROR_MARK
4353 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4355 error ("field `%s' has incomplete type", name);
4356 type = error_mark_node;
4358 /* Move type qualifiers down to element of an array. */
4359 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4360 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4361 type_quals),
4362 TYPE_DOMAIN (type));
4363 decl = build_decl (FIELD_DECL, declarator, type);
4364 DECL_NONADDRESSABLE_P (decl) = bitfield;
4366 if (size_varies)
4367 C_DECL_VARIABLE_SIZE (decl) = 1;
4369 else if (TREE_CODE (type) == FUNCTION_TYPE)
4371 /* Every function declaration is "external"
4372 except for those which are inside a function body
4373 in which `auto' is used.
4374 That is a case not specified by ANSI C,
4375 and we use it for forward declarations for nested functions. */
4376 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4377 || current_scope == file_scope);
4379 if (specbits & (1 << (int) RID_AUTO)
4380 && (pedantic || current_scope == file_scope))
4381 pedwarn ("invalid storage class for function `%s'", name);
4382 if (specbits & (1 << (int) RID_REGISTER))
4383 error ("invalid storage class for function `%s'", name);
4384 if (specbits & (1 << (int) RID_THREAD))
4385 error ("invalid storage class for function `%s'", name);
4386 /* Function declaration not at file scope.
4387 Storage classes other than `extern' are not allowed
4388 and `extern' makes no difference. */
4389 if (current_scope != file_scope
4390 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4391 && pedantic)
4392 pedwarn ("invalid storage class for function `%s'", name);
4394 decl = build_decl (FUNCTION_DECL, declarator, type);
4395 decl = build_decl_attribute_variant (decl, decl_attr);
4397 DECL_LANG_SPECIFIC (decl)
4398 = ggc_alloc_cleared (sizeof (struct lang_decl));
4400 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4401 pedwarn ("ISO C forbids qualified function types");
4403 /* GNU C interprets a `volatile void' return type to indicate
4404 that the function does not return. */
4405 if ((type_quals & TYPE_QUAL_VOLATILE)
4406 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4407 warning ("`noreturn' function returns non-void value");
4409 if (extern_ref)
4410 DECL_EXTERNAL (decl) = 1;
4411 /* Record absence of global scope for `static' or `auto'. */
4412 TREE_PUBLIC (decl)
4413 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4415 /* For a function definition, record the argument information
4416 block in DECL_ARGUMENTS where store_parm_decls will look
4417 for it. */
4418 if (funcdef_flag)
4419 DECL_ARGUMENTS (decl) = arg_info;
4421 if (defaulted_int)
4422 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4424 /* Record presence of `inline', if it is reasonable. */
4425 if (MAIN_NAME_P (declarator))
4427 if (inlinep)
4428 warning ("cannot inline function `main'");
4430 else if (inlinep)
4432 /* Record that the function is declared `inline'. */
4433 DECL_DECLARED_INLINE_P (decl) = 1;
4435 /* Do not mark bare declarations as DECL_INLINE. Doing so
4436 in the presence of multiple declarations can result in
4437 the abstract origin pointing between the declarations,
4438 which will confuse dwarf2out. */
4439 if (initialized)
4441 DECL_INLINE (decl) = 1;
4442 if (specbits & (1 << (int) RID_EXTERN))
4443 current_extern_inline = 1;
4446 /* If -finline-functions, assume it can be inlined. This does
4447 two things: let the function be deferred until it is actually
4448 needed, and let dwarf2 know that the function is inlinable. */
4449 else if (flag_inline_trees == 2 && initialized)
4450 DECL_INLINE (decl) = 1;
4452 else
4454 /* It's a variable. */
4455 /* An uninitialized decl with `extern' is a reference. */
4456 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4458 /* Move type qualifiers down to element of an array. */
4459 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4461 int saved_align = TYPE_ALIGN(type);
4462 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4463 type_quals),
4464 TYPE_DOMAIN (type));
4465 TYPE_ALIGN (type) = saved_align;
4467 else if (type_quals)
4468 type = c_build_qualified_type (type, type_quals);
4470 /* C99 6.2.2p7: It is invalid (compile-time undefined
4471 behavior) to create an 'extern' declaration for a
4472 variable if there is a global declaration that is
4473 'static' and the global declaration is not visible.
4474 (If the static declaration _is_ currently visible,
4475 the 'extern' declaration is taken to refer to that decl.) */
4476 if (extern_ref && current_scope != file_scope)
4478 tree global_decl = identifier_global_value (declarator);
4479 tree visible_decl = lookup_name (declarator);
4481 if (global_decl
4482 && global_decl != visible_decl
4483 && TREE_CODE (global_decl) == VAR_DECL
4484 && !TREE_PUBLIC (global_decl))
4485 error ("variable previously declared 'static' redeclared "
4486 "'extern'");
4489 decl = build_decl (VAR_DECL, declarator, type);
4490 if (size_varies)
4491 C_DECL_VARIABLE_SIZE (decl) = 1;
4493 if (inlinep)
4494 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4496 DECL_EXTERNAL (decl) = extern_ref;
4498 /* At file scope, the presence of a `static' or `register' storage
4499 class specifier, or the absence of all storage class specifiers
4500 makes this declaration a definition (perhaps tentative). Also,
4501 the absence of both `static' and `register' makes it public. */
4502 if (current_scope == file_scope)
4504 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4505 | (1 << (int) RID_REGISTER)));
4506 TREE_STATIC (decl) = !extern_ref;
4508 /* Not at file scope, only `static' makes a static definition. */
4509 else
4511 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4512 TREE_PUBLIC (decl) = extern_ref;
4515 if (specbits & 1 << (int) RID_THREAD)
4517 if (targetm.have_tls)
4518 DECL_THREAD_LOCAL (decl) = 1;
4519 else
4520 /* A mere warning is sure to result in improper semantics
4521 at runtime. Don't bother to allow this to compile. */
4522 error ("thread-local storage not supported for this target");
4526 /* Record `register' declaration for warnings on &
4527 and in case doing stupid register allocation. */
4529 if (specbits & (1 << (int) RID_REGISTER))
4530 DECL_REGISTER (decl) = 1;
4532 /* Record constancy and volatility. */
4533 c_apply_type_quals_to_decl (type_quals, decl);
4535 /* If a type has volatile components, it should be stored in memory.
4536 Otherwise, the fact that those components are volatile
4537 will be ignored, and would even crash the compiler. */
4538 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4539 c_mark_addressable (decl);
4541 #ifdef ENABLE_CHECKING
4542 /* This is the earliest point at which we might know the assembler
4543 name of a variable. Thus, if it's known before this, die horribly. */
4544 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4545 abort ();
4546 #endif
4548 decl_attributes (&decl, returned_attrs, 0);
4550 return decl;
4554 /* Decode the parameter-list info for a function type or function definition.
4555 The argument is the value returned by `get_parm_info' (or made in parse.y
4556 if there is an identifier list instead of a parameter decl list).
4557 These two functions are separate because when a function returns
4558 or receives functions then each is called multiple times but the order
4559 of calls is different. The last call to `grokparms' is always the one
4560 that contains the formal parameter names of a function definition.
4562 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4564 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4565 a mere declaration. A nonempty identifier-list gets an error message
4566 when FUNCDEF_FLAG is zero. */
4568 static tree
4569 grokparms (tree arg_info, int funcdef_flag)
4571 tree arg_types = ARG_INFO_TYPES (arg_info);
4573 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4574 && !in_system_header)
4575 warning ("function declaration isn't a prototype");
4577 if (arg_types == error_mark_node)
4578 return 0; /* don't set TYPE_ARG_TYPES in this case */
4580 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4582 if (! funcdef_flag)
4583 pedwarn ("parameter names (without types) in function declaration");
4585 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4586 ARG_INFO_TYPES (arg_info) = 0;
4587 return 0;
4589 else
4591 tree parm, type, typelt;
4592 unsigned int parmno;
4594 /* If the arg types are incomplete in a declaration, they must
4595 include undefined tags. These tags can never be defined in
4596 the scope of the declaration, so the types can never be
4597 completed, and no call can be compiled successfully. */
4599 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4600 parm;
4601 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4603 type = TREE_VALUE (typelt);
4604 if (type == error_mark_node)
4605 continue;
4607 if (!COMPLETE_TYPE_P (type))
4609 if (funcdef_flag)
4611 if (DECL_NAME (parm))
4612 error ("%Jparameter %u ('%D') has incomplete type",
4613 parm, parmno, parm);
4614 else
4615 error ("%Jparameter %u has incomplete type",
4616 parm, parmno);
4618 TREE_VALUE (typelt) = error_mark_node;
4619 TREE_TYPE (parm) = error_mark_node;
4621 else
4623 if (DECL_NAME (parm))
4624 warning ("%Jparameter %u ('%D') has incomplete type",
4625 parm, parmno, parm);
4626 else
4627 warning ("%Jparameter %u has incomplete type",
4628 parm, parmno);
4632 return arg_types;
4636 /* Take apart the current scope and return a tree_list node with info
4637 on a parameter list just parsed. This tree_list node should be
4638 examined using the ARG_INFO_* macros, defined above:
4640 ARG_INFO_PARMS: a list of parameter decls.
4641 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4642 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4643 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4644 constants) defined with the parameters.
4646 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4648 ELLIPSIS being true means the argument list ended in '...' so don't
4649 append a sentinel (void_list_node) to the end of the type-list. */
4651 tree
4652 get_parm_info (bool ellipsis)
4654 struct c_binding *b = current_scope->bindings;
4655 tree arg_info = make_node (TREE_LIST);
4656 tree parms = 0;
4657 tree tags = 0;
4658 tree types = 0;
4659 tree others = 0;
4661 static bool explained_incomplete_types = false;
4662 bool gave_void_only_once_err = false;
4664 /* The bindings in this scope must not get put into a block.
4665 We will take care of deleting the binding nodes. */
4666 current_scope->bindings = 0;
4668 /* This function is only called if there was *something* on the
4669 parameter list. */
4670 #ifdef ENABLE_CHECKING
4671 if (b == 0)
4672 abort ();
4673 #endif
4675 /* A parameter list consisting solely of 'void' indicates that the
4676 function takes no arguments. But if the 'void' is qualified
4677 (by 'const' or 'volatile'), or has a storage class specifier
4678 ('register'), then the behavior is undefined; issue an error.
4679 Typedefs for 'void' are OK (see DR#157). */
4680 if (b->prev == 0 /* one binding */
4681 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4682 && !DECL_NAME (b->decl) /* anonymous */
4683 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4685 if (TREE_THIS_VOLATILE (b->decl)
4686 || TREE_READONLY (b->decl)
4687 || DECL_REGISTER (b->decl))
4688 error ("'void' as only parameter may not be qualified");
4690 /* There cannot be an ellipsis. */
4691 if (ellipsis)
4692 error ("'void' must be the only parameter");
4694 ARG_INFO_TYPES (arg_info) = void_list_node;
4695 return arg_info;
4698 if (!ellipsis)
4699 types = void_list_node;
4701 /* Break up the bindings list into parms, tags, types, and others;
4702 apply sanity checks; purge the name-to-decl bindings. */
4703 while (b)
4705 tree decl = b->decl;
4706 tree type = TREE_TYPE (decl);
4707 const char *keyword;
4709 switch (TREE_CODE (decl))
4711 case PARM_DECL:
4712 if (b->id)
4714 #ifdef ENABLE_CHECKING
4715 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4716 #endif
4717 I_SYMBOL_BINDING (b->id) = b->shadowed;
4720 /* Check for forward decls that never got their actual decl. */
4721 if (TREE_ASM_WRITTEN (decl))
4722 error ("%Jparameter '%D' has just a forward declaration",
4723 decl, decl);
4724 /* Check for (..., void, ...) and issue an error. */
4725 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4727 if (!gave_void_only_once_err)
4729 error ("'void' must be the only parameter");
4730 gave_void_only_once_err = true;
4733 else
4735 /* Valid parameter, add it to the list. */
4736 TREE_CHAIN (decl) = parms;
4737 parms = decl;
4739 /* Since there is a prototype, args are passed in their
4740 declared types. The back end may override this later. */
4741 DECL_ARG_TYPE (decl) = type;
4742 types = tree_cons (0, type, types);
4744 break;
4746 case ENUMERAL_TYPE: keyword = "struct"; goto tag;
4747 case UNION_TYPE: keyword = "union"; goto tag;
4748 case RECORD_TYPE: keyword = "enum"; goto tag;
4749 tag:
4750 /* Types may not have tag-names, in which case the type
4751 appears in the bindings list with b->id NULL. */
4752 if (b->id)
4754 #ifdef ENABLE_CHECKING
4755 if (I_TAG_BINDING (b->id) != b) abort ();
4756 #endif
4757 I_TAG_BINDING (b->id) = b->shadowed;
4760 /* Warn about any struct, union or enum tags defined in a
4761 parameter list. The scope of such types is limited to
4762 the parameter list, which is rarely if ever desirable
4763 (it's impossible to call such a function with type-
4764 correct arguments). An anonymous union parm type is
4765 meaningful as a GNU extension, so don't warn for that. */
4766 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4768 if (b->id)
4769 /* The %s will be one of 'struct', 'union', or 'enum'. */
4770 warning ("'%s %E' declared inside parameter list",
4771 keyword, b->id);
4772 else
4773 /* The %s will be one of 'struct', 'union', or 'enum'. */
4774 warning ("anonymous %s declared inside parameter list",
4775 keyword);
4777 if (! explained_incomplete_types)
4779 warning ("its scope is only this definition or declaration,"
4780 " which is probably not what you want");
4781 explained_incomplete_types = true;
4785 tags = tree_cons (b->id, decl, tags);
4786 break;
4788 case CONST_DECL:
4789 case TYPE_DECL:
4790 /* CONST_DECLs appear here when we have an embedded enum,
4791 and TYPE_DECLs appear here when we have an embedded struct
4792 or union. No warnings for this - we already warned about the
4793 type itself. */
4794 if (b->id)
4796 #ifdef ENABLE_CHECKING
4797 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4798 #endif
4799 I_SYMBOL_BINDING (b->id) = b->shadowed;
4802 TREE_CHAIN (decl) = others;
4803 others = decl;
4804 break;
4806 /* Other things that might be encountered. */
4807 case LABEL_DECL:
4808 case FUNCTION_DECL:
4809 case VAR_DECL:
4810 case ERROR_MARK:
4811 default:
4812 abort ();
4815 b = free_binding_and_advance (b);
4818 ARG_INFO_PARMS (arg_info) = parms;
4819 ARG_INFO_TAGS (arg_info) = tags;
4820 ARG_INFO_TYPES (arg_info) = types;
4821 ARG_INFO_OTHERS (arg_info) = others;
4822 return arg_info;
4825 /* Get the struct, enum or union (CODE says which) with tag NAME.
4826 Define the tag as a forward-reference if it is not defined. */
4828 tree
4829 xref_tag (enum tree_code code, tree name)
4831 /* If a cross reference is requested, look up the type
4832 already defined for this tag and return it. */
4834 tree ref = lookup_tag (code, name, 0);
4835 /* If this is the right type of tag, return what we found.
4836 (This reference will be shadowed by shadow_tag later if appropriate.)
4837 If this is the wrong type of tag, do not return it. If it was the
4838 wrong type in the same scope, we will have had an error
4839 message already; if in a different scope and declaring
4840 a name, pending_xref_error will give an error message; but if in a
4841 different scope and not declaring a name, this tag should
4842 shadow the previous declaration of a different type of tag, and
4843 this would not work properly if we return the reference found.
4844 (For example, with "struct foo" in an outer scope, "union foo;"
4845 must shadow that tag with a new one of union type.) */
4846 if (ref && TREE_CODE (ref) == code)
4847 return ref;
4849 /* If no such tag is yet defined, create a forward-reference node
4850 and record it as the "definition".
4851 When a real declaration of this type is found,
4852 the forward-reference will be altered into a real type. */
4854 ref = make_node (code);
4855 if (code == ENUMERAL_TYPE)
4857 /* Give the type a default layout like unsigned int
4858 to avoid crashing if it does not get defined. */
4859 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4860 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4861 TYPE_USER_ALIGN (ref) = 0;
4862 TREE_UNSIGNED (ref) = 1;
4863 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4864 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4865 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4868 pushtag (name, ref);
4870 return ref;
4873 /* Make sure that the tag NAME is defined *in the current scope*
4874 at least as a forward reference.
4875 CODE says which kind of tag NAME ought to be. */
4877 tree
4878 start_struct (enum tree_code code, tree name)
4880 /* If there is already a tag defined at this scope
4881 (as a forward reference), just return it. */
4883 tree ref = 0;
4885 if (name != 0)
4886 ref = lookup_tag (code, name, 1);
4887 if (ref && TREE_CODE (ref) == code)
4889 if (TYPE_FIELDS (ref))
4891 if (code == UNION_TYPE)
4892 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4893 else
4894 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4897 else
4899 /* Otherwise create a forward-reference just so the tag is in scope. */
4901 ref = make_node (code);
4902 pushtag (name, ref);
4905 C_TYPE_BEING_DEFINED (ref) = 1;
4906 TYPE_PACKED (ref) = flag_pack_struct;
4907 return ref;
4910 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4911 of a structure component, returning a FIELD_DECL node.
4912 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4914 This is done during the parsing of the struct declaration.
4915 The FIELD_DECL nodes are chained together and the lot of them
4916 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4918 tree
4919 grokfield (tree declarator, tree declspecs, tree width)
4921 tree value;
4923 if (declarator == NULL_TREE && width == NULL_TREE)
4925 /* This is an unnamed decl.
4927 If we have something of the form "union { list } ;" then this
4928 is the anonymous union extension. Similarly for struct.
4930 If this is something of the form "struct foo;", then
4931 If MS extensions are enabled, this is handled as an
4932 anonymous struct.
4933 Otherwise this is a forward declaration of a structure tag.
4935 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4936 If MS extensions are enabled and foo names a structure, then
4937 again this is an anonymous struct.
4938 Otherwise this is an error.
4940 Oh what a horrid tangled web we weave. I wonder if MS consciously
4941 took this from Plan 9 or if it was an accident of implementation
4942 that took root before someone noticed the bug... */
4944 tree type = TREE_VALUE (declspecs);
4946 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
4947 type = TREE_TYPE (type);
4948 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
4950 if (flag_ms_extensions)
4951 ; /* ok */
4952 else if (flag_iso)
4953 goto warn_unnamed_field;
4954 else if (TYPE_NAME (type) == NULL)
4955 ; /* ok */
4956 else
4957 goto warn_unnamed_field;
4959 else
4961 warn_unnamed_field:
4962 warning ("declaration does not declare anything");
4963 return NULL_TREE;
4967 value = grokdeclarator (declarator, declspecs, FIELD, 0,
4968 width ? &width : NULL);
4970 finish_decl (value, NULL_TREE, NULL_TREE);
4971 DECL_INITIAL (value) = width;
4973 return value;
4976 /* Generate an error for any duplicate field names in FIELDLIST. Munge
4977 the list such that this does not present a problem later. */
4979 static void
4980 detect_field_duplicates (tree fieldlist)
4982 tree x, y;
4983 int timeout = 10;
4985 /* First, see if there are more than "a few" fields.
4986 This is trivially true if there are zero or one fields. */
4987 if (!fieldlist)
4988 return;
4989 x = TREE_CHAIN (fieldlist);
4990 if (!x)
4991 return;
4992 do {
4993 timeout--;
4994 x = TREE_CHAIN (x);
4995 } while (timeout > 0 && x);
4997 /* If there were "few" fields, avoid the overhead of allocating
4998 a hash table. Instead just do the nested traversal thing. */
4999 if (timeout > 0)
5001 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5002 if (DECL_NAME (x))
5004 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5005 if (DECL_NAME (y) == DECL_NAME (x))
5007 error ("%Jduplicate member '%D'", x, x);
5008 DECL_NAME (x) = NULL_TREE;
5012 else
5014 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5015 void **slot;
5017 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5018 if ((y = DECL_NAME (x)) != 0)
5020 slot = htab_find_slot (htab, y, INSERT);
5021 if (*slot)
5023 error ("%Jduplicate member '%D'", x, x);
5024 DECL_NAME (x) = NULL_TREE;
5026 *slot = y;
5029 htab_delete (htab);
5033 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5034 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5035 ATTRIBUTES are attributes to be applied to the structure. */
5037 tree
5038 finish_struct (tree t, tree fieldlist, tree attributes)
5040 tree x;
5041 bool toplevel = file_scope == current_scope;
5042 int saw_named_field;
5044 /* If this type was previously laid out as a forward reference,
5045 make sure we lay it out again. */
5047 TYPE_SIZE (t) = 0;
5049 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5051 if (pedantic)
5053 for (x = fieldlist; x; x = TREE_CHAIN (x))
5054 if (DECL_NAME (x) != 0)
5055 break;
5057 if (x == 0)
5058 pedwarn ("%s has no %s",
5059 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5060 fieldlist ? _("named members") : _("members"));
5063 /* Install struct as DECL_CONTEXT of each field decl.
5064 Also process specified field sizes,m which is found in the DECL_INITIAL.
5065 Store 0 there, except for ": 0" fields (so we can find them
5066 and delete them, below). */
5068 saw_named_field = 0;
5069 for (x = fieldlist; x; x = TREE_CHAIN (x))
5071 DECL_CONTEXT (x) = t;
5072 DECL_PACKED (x) |= TYPE_PACKED (t);
5074 /* If any field is const, the structure type is pseudo-const. */
5075 if (TREE_READONLY (x))
5076 C_TYPE_FIELDS_READONLY (t) = 1;
5077 else
5079 /* A field that is pseudo-const makes the structure likewise. */
5080 tree t1 = TREE_TYPE (x);
5081 while (TREE_CODE (t1) == ARRAY_TYPE)
5082 t1 = TREE_TYPE (t1);
5083 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5084 && C_TYPE_FIELDS_READONLY (t1))
5085 C_TYPE_FIELDS_READONLY (t) = 1;
5088 /* Any field that is volatile means variables of this type must be
5089 treated in some ways as volatile. */
5090 if (TREE_THIS_VOLATILE (x))
5091 C_TYPE_FIELDS_VOLATILE (t) = 1;
5093 /* Any field of nominal variable size implies structure is too. */
5094 if (C_DECL_VARIABLE_SIZE (x))
5095 C_TYPE_VARIABLE_SIZE (t) = 1;
5097 /* Detect invalid nested redefinition. */
5098 if (TREE_TYPE (x) == t)
5099 error ("nested redefinition of `%s'",
5100 IDENTIFIER_POINTER (TYPE_NAME (t)));
5102 if (DECL_INITIAL (x))
5104 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5105 DECL_SIZE (x) = bitsize_int (width);
5106 DECL_BIT_FIELD (x) = 1;
5107 SET_DECL_C_BIT_FIELD (x);
5110 DECL_INITIAL (x) = 0;
5112 /* Detect flexible array member in an invalid context. */
5113 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5114 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5115 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5116 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5118 if (TREE_CODE (t) == UNION_TYPE)
5120 error ("%Jflexible array member in union", x);
5121 TREE_TYPE (x) = error_mark_node;
5123 else if (TREE_CHAIN (x) != NULL_TREE)
5125 error ("%Jflexible array member not at end of struct", x);
5126 TREE_TYPE (x) = error_mark_node;
5128 else if (! saw_named_field)
5130 error ("%Jflexible array member in otherwise empty struct", x);
5131 TREE_TYPE (x) = error_mark_node;
5135 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5136 && flexible_array_type_p (TREE_TYPE (x)))
5137 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5139 if (DECL_NAME (x))
5140 saw_named_field = 1;
5143 detect_field_duplicates (fieldlist);
5145 /* Now we have the nearly final fieldlist. Record it,
5146 then lay out the structure or union (including the fields). */
5148 TYPE_FIELDS (t) = fieldlist;
5150 layout_type (t);
5152 /* Delete all zero-width bit-fields from the fieldlist. */
5154 tree *fieldlistp = &fieldlist;
5155 while (*fieldlistp)
5156 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5157 *fieldlistp = TREE_CHAIN (*fieldlistp);
5158 else
5159 fieldlistp = &TREE_CHAIN (*fieldlistp);
5162 /* Now we have the truly final field list.
5163 Store it in this type and in the variants. */
5165 TYPE_FIELDS (t) = fieldlist;
5167 /* If there are lots of fields, sort so we can look through them fast.
5168 We arbitrarily consider 16 or more elts to be "a lot". */
5171 int len = 0;
5173 for (x = fieldlist; x; x = TREE_CHAIN (x))
5175 if (len > 15 || DECL_NAME (x) == NULL)
5176 break;
5177 len += 1;
5180 if (len > 15)
5182 tree *field_array;
5183 struct lang_type *space;
5184 struct sorted_fields_type *space2;
5186 len += list_length (x);
5188 /* Use the same allocation policy here that make_node uses, to
5189 ensure that this lives as long as the rest of the struct decl.
5190 All decls in an inline function need to be saved. */
5192 space = ggc_alloc (sizeof (struct lang_type));
5193 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5195 len = 0;
5196 space->s = space2;
5197 field_array = &space2->elts[0];
5198 for (x = fieldlist; x; x = TREE_CHAIN (x))
5200 field_array[len++] = x;
5202 /* If there is anonymous struct or union, break out of the loop. */
5203 if (DECL_NAME (x) == NULL)
5204 break;
5206 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5207 if (x == NULL)
5209 TYPE_LANG_SPECIFIC (t) = space;
5210 TYPE_LANG_SPECIFIC (t)->s->len = len;
5211 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5212 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5217 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5219 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5220 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5221 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5222 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5225 /* If this was supposed to be a transparent union, but we can't
5226 make it one, warn and turn off the flag. */
5227 if (TREE_CODE (t) == UNION_TYPE
5228 && TYPE_TRANSPARENT_UNION (t)
5229 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5231 TYPE_TRANSPARENT_UNION (t) = 0;
5232 warning ("union cannot be made transparent");
5235 /* If this structure or union completes the type of any previous
5236 variable declaration, lay it out and output its rtl. */
5237 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5239 x = TREE_CHAIN (x))
5241 tree decl = TREE_VALUE (x);
5242 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5243 layout_array_type (TREE_TYPE (decl));
5244 if (TREE_CODE (decl) != TYPE_DECL)
5246 layout_decl (decl, 0);
5247 if (c_dialect_objc ())
5248 objc_check_decl (decl);
5249 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5250 if (! toplevel)
5251 expand_decl (decl);
5254 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5256 /* Finish debugging output for this type. */
5257 rest_of_type_compilation (t, toplevel);
5259 return t;
5262 /* Lay out the type T, and its element type, and so on. */
5264 static void
5265 layout_array_type (tree t)
5267 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5268 layout_array_type (TREE_TYPE (t));
5269 layout_type (t);
5272 /* Begin compiling the definition of an enumeration type.
5273 NAME is its name (or null if anonymous).
5274 Returns the type object, as yet incomplete.
5275 Also records info about it so that build_enumerator
5276 may be used to declare the individual values as they are read. */
5278 tree
5279 start_enum (tree name)
5281 tree enumtype = 0;
5283 /* If this is the real definition for a previous forward reference,
5284 fill in the contents in the same object that used to be the
5285 forward reference. */
5287 if (name != 0)
5288 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5290 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5292 enumtype = make_node (ENUMERAL_TYPE);
5293 pushtag (name, enumtype);
5296 C_TYPE_BEING_DEFINED (enumtype) = 1;
5298 if (TYPE_VALUES (enumtype) != 0)
5300 /* This enum is a named one that has been declared already. */
5301 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5303 /* Completely replace its old definition.
5304 The old enumerators remain defined, however. */
5305 TYPE_VALUES (enumtype) = 0;
5308 enum_next_value = integer_zero_node;
5309 enum_overflow = 0;
5311 if (flag_short_enums)
5312 TYPE_PACKED (enumtype) = 1;
5314 return enumtype;
5317 /* After processing and defining all the values of an enumeration type,
5318 install their decls in the enumeration type and finish it off.
5319 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5320 and ATTRIBUTES are the specified attributes.
5321 Returns ENUMTYPE. */
5323 tree
5324 finish_enum (tree enumtype, tree values, tree attributes)
5326 tree pair, tem;
5327 tree minnode = 0, maxnode = 0, enum_value_type;
5328 int precision, unsign;
5329 bool toplevel = (file_scope == current_scope);
5331 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5333 /* Calculate the maximum value of any enumerator in this type. */
5335 if (values == error_mark_node)
5336 minnode = maxnode = integer_zero_node;
5337 else
5339 minnode = maxnode = TREE_VALUE (values);
5340 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5342 tree value = TREE_VALUE (pair);
5343 if (tree_int_cst_lt (maxnode, value))
5344 maxnode = value;
5345 if (tree_int_cst_lt (value, minnode))
5346 minnode = value;
5350 /* Construct the final type of this enumeration. It is the same
5351 as one of the integral types - the narrowest one that fits, except
5352 that normally we only go as narrow as int - and signed iff any of
5353 the values are negative. */
5354 unsign = (tree_int_cst_sgn (minnode) >= 0);
5355 precision = MAX (min_precision (minnode, unsign),
5356 min_precision (maxnode, unsign));
5357 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5359 tree narrowest = c_common_type_for_size (precision, unsign);
5360 if (narrowest == 0)
5362 warning ("enumeration values exceed range of largest integer");
5363 narrowest = long_long_integer_type_node;
5366 precision = TYPE_PRECISION (narrowest);
5368 else
5369 precision = TYPE_PRECISION (integer_type_node);
5371 if (precision == TYPE_PRECISION (integer_type_node))
5372 enum_value_type = c_common_type_for_size (precision, 0);
5373 else
5374 enum_value_type = enumtype;
5376 TYPE_MIN_VALUE (enumtype) = minnode;
5377 TYPE_MAX_VALUE (enumtype) = maxnode;
5378 TYPE_PRECISION (enumtype) = precision;
5379 TREE_UNSIGNED (enumtype) = unsign;
5380 TYPE_SIZE (enumtype) = 0;
5381 layout_type (enumtype);
5383 if (values != error_mark_node)
5385 /* Change the type of the enumerators to be the enum type. We
5386 need to do this irrespective of the size of the enum, for
5387 proper type checking. Replace the DECL_INITIALs of the
5388 enumerators, and the value slots of the list, with copies
5389 that have the enum type; they cannot be modified in place
5390 because they may be shared (e.g. integer_zero_node) Finally,
5391 change the purpose slots to point to the names of the decls. */
5392 for (pair = values; pair; pair = TREE_CHAIN (pair))
5394 tree enu = TREE_PURPOSE (pair);
5396 TREE_TYPE (enu) = enumtype;
5398 /* The ISO C Standard mandates enumerators to have type int,
5399 even though the underlying type of an enum type is
5400 unspecified. Here we convert any enumerators that fit in
5401 an int to type int, to avoid promotions to unsigned types
5402 when comparing integers with enumerators that fit in the
5403 int range. When -pedantic is given, build_enumerator()
5404 would have already taken care of those that don't fit. */
5405 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5406 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5407 else
5408 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5410 TREE_PURPOSE (pair) = DECL_NAME (enu);
5411 TREE_VALUE (pair) = DECL_INITIAL (enu);
5414 TYPE_VALUES (enumtype) = values;
5417 /* Fix up all variant types of this enum type. */
5418 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5420 if (tem == enumtype)
5421 continue;
5422 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5423 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5424 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5425 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5426 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5427 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5428 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5429 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5430 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5431 TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
5434 /* Finish debugging output for this type. */
5435 rest_of_type_compilation (enumtype, toplevel);
5437 return enumtype;
5440 /* Build and install a CONST_DECL for one value of the
5441 current enumeration type (one that was begun with start_enum).
5442 Return a tree-list containing the CONST_DECL and its value.
5443 Assignment of sequential values by default is handled here. */
5445 tree
5446 build_enumerator (tree name, tree value)
5448 tree decl, type;
5450 /* Validate and default VALUE. */
5452 /* Remove no-op casts from the value. */
5453 if (value)
5454 STRIP_TYPE_NOPS (value);
5456 if (value != 0)
5458 /* Don't issue more errors for error_mark_node (i.e. an
5459 undeclared identifier) - just ignore the value expression. */
5460 if (value == error_mark_node)
5461 value = 0;
5462 else if (TREE_CODE (value) != INTEGER_CST)
5464 error ("enumerator value for '%E' is not an integer constant", name);
5465 value = 0;
5467 else
5469 value = default_conversion (value);
5470 constant_expression_warning (value);
5474 /* Default based on previous value. */
5475 /* It should no longer be possible to have NON_LVALUE_EXPR
5476 in the default. */
5477 if (value == 0)
5479 value = enum_next_value;
5480 if (enum_overflow)
5481 error ("overflow in enumeration values");
5484 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5486 pedwarn ("ISO C restricts enumerator values to range of `int'");
5487 /* XXX This causes -pedantic to change the meaning of the program.
5488 Remove? -zw 2004-03-15 */
5489 value = convert (integer_type_node, value);
5492 /* Set basis for default for next value. */
5493 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5494 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5496 /* Now create a declaration for the enum value name. */
5498 type = TREE_TYPE (value);
5499 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5500 TYPE_PRECISION (integer_type_node)),
5501 (TYPE_PRECISION (type)
5502 >= TYPE_PRECISION (integer_type_node)
5503 && TREE_UNSIGNED (type)));
5505 decl = build_decl (CONST_DECL, name, type);
5506 DECL_INITIAL (decl) = convert (type, value);
5507 pushdecl (decl);
5509 return tree_cons (decl, value, NULL_TREE);
5513 /* Create the FUNCTION_DECL for a function definition.
5514 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5515 the declaration; they describe the function's name and the type it returns,
5516 but twisted together in a fashion that parallels the syntax of C.
5518 This function creates a binding context for the function body
5519 as well as setting up the FUNCTION_DECL in current_function_decl.
5521 Returns 1 on success. If the DECLARATOR is not suitable for a function
5522 (it defines a datum instead), we return 0, which tells
5523 yyparse to report a parse error. */
5526 start_function (tree declspecs, tree declarator, tree attributes)
5528 tree decl1, old_decl;
5529 tree restype;
5530 int old_immediate_size_expand = immediate_size_expand;
5532 current_function_returns_value = 0; /* Assume, until we see it does. */
5533 current_function_returns_null = 0;
5534 current_function_returns_abnormally = 0;
5535 warn_about_return_type = 0;
5536 current_extern_inline = 0;
5537 c_in_iteration_stmt = 0;
5538 c_in_case_stmt = 0;
5540 /* Don't expand any sizes in the return type of the function. */
5541 immediate_size_expand = 0;
5543 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5545 /* If the declarator is not suitable for a function definition,
5546 cause a syntax error. */
5547 if (decl1 == 0)
5549 immediate_size_expand = old_immediate_size_expand;
5550 return 0;
5553 decl_attributes (&decl1, attributes, 0);
5555 if (DECL_DECLARED_INLINE_P (decl1)
5556 && DECL_UNINLINABLE (decl1)
5557 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5558 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5560 announce_function (decl1);
5562 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5564 error ("return type is an incomplete type");
5565 /* Make it return void instead. */
5566 TREE_TYPE (decl1)
5567 = build_function_type (void_type_node,
5568 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5571 if (warn_about_return_type)
5572 pedwarn_c99 ("return type defaults to `int'");
5574 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5575 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5576 DECL_INITIAL (decl1) = error_mark_node;
5578 /* If this definition isn't a prototype and we had a prototype declaration
5579 before, copy the arg type info from that prototype.
5580 But not if what we had before was a builtin function. */
5581 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5582 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5583 && !DECL_BUILT_IN (old_decl)
5584 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5585 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5586 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5588 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5589 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5592 /* Optionally warn of old-fashioned def with no previous prototype. */
5593 if (warn_strict_prototypes
5594 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5595 && C_DECL_ISNT_PROTOTYPE (old_decl))
5596 warning ("function declaration isn't a prototype");
5597 /* Optionally warn of any global def with no previous prototype. */
5598 else if (warn_missing_prototypes
5599 && TREE_PUBLIC (decl1)
5600 && ! MAIN_NAME_P (DECL_NAME (decl1))
5601 && C_DECL_ISNT_PROTOTYPE (old_decl))
5602 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5603 /* Optionally warn of any def with no previous prototype
5604 if the function has already been used. */
5605 else if (warn_missing_prototypes
5606 && old_decl != 0 && TREE_USED (old_decl)
5607 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5608 warning ("%J'%D' was used with no prototype before its definition",
5609 decl1, decl1);
5610 /* Optionally warn of any global def with no previous declaration. */
5611 else if (warn_missing_declarations
5612 && TREE_PUBLIC (decl1)
5613 && old_decl == 0
5614 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5615 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5616 /* Optionally warn of any def with no previous declaration
5617 if the function has already been used. */
5618 else if (warn_missing_declarations
5619 && old_decl != 0 && TREE_USED (old_decl)
5620 && C_DECL_IMPLICIT (old_decl))
5621 warning ("%J`%D' was used with no declaration before its definition",
5622 decl1, decl1);
5624 /* This is a definition, not a reference.
5625 So normally clear DECL_EXTERNAL.
5626 However, `extern inline' acts like a declaration
5627 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5628 DECL_EXTERNAL (decl1) = current_extern_inline;
5630 /* This function exists in static storage.
5631 (This does not mean `static' in the C sense!) */
5632 TREE_STATIC (decl1) = 1;
5634 /* A nested function is not global. */
5635 if (current_function_decl != 0)
5636 TREE_PUBLIC (decl1) = 0;
5638 #ifdef ENABLE_CHECKING
5639 /* This is the earliest point at which we might know the assembler
5640 name of the function. Thus, if it's set before this, die horribly. */
5641 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5642 abort ();
5643 #endif
5645 /* If #pragma weak was used, mark the decl weak now. */
5646 if (current_scope == file_scope)
5647 maybe_apply_pragma_weak (decl1);
5649 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5650 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5652 tree args;
5653 int argct = 0;
5655 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5656 != integer_type_node)
5657 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5659 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5660 args = TREE_CHAIN (args))
5662 tree type = args ? TREE_VALUE (args) : 0;
5664 if (type == void_type_node)
5665 break;
5667 ++argct;
5668 switch (argct)
5670 case 1:
5671 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5672 pedwarn ("%Jfirst argument of '%D' should be `int'",
5673 decl1, decl1);
5674 break;
5676 case 2:
5677 if (TREE_CODE (type) != POINTER_TYPE
5678 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5679 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5680 != char_type_node))
5681 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5682 decl1, decl1);
5683 break;
5685 case 3:
5686 if (TREE_CODE (type) != POINTER_TYPE
5687 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5688 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5689 != char_type_node))
5690 pedwarn ("%Jthird argument of '%D' should probably be "
5691 "'char **'", decl1, decl1);
5692 break;
5696 /* It is intentional that this message does not mention the third
5697 argument because it's only mentioned in an appendix of the
5698 standard. */
5699 if (argct > 0 && (argct < 2 || argct > 3))
5700 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5702 if (! TREE_PUBLIC (decl1))
5703 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5706 /* Record the decl so that the function name is defined.
5707 If we already have a decl for this name, and it is a FUNCTION_DECL,
5708 use the old decl. */
5710 current_function_decl = pushdecl (decl1);
5712 push_scope ();
5713 declare_parm_level ();
5715 make_decl_rtl (current_function_decl, NULL);
5717 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5718 /* Promote the value to int before returning it. */
5719 if (c_promoting_integer_type_p (restype))
5721 /* It retains unsignedness if not really getting wider. */
5722 if (TREE_UNSIGNED (restype)
5723 && (TYPE_PRECISION (restype)
5724 == TYPE_PRECISION (integer_type_node)))
5725 restype = unsigned_type_node;
5726 else
5727 restype = integer_type_node;
5729 DECL_RESULT (current_function_decl)
5730 = build_decl (RESULT_DECL, NULL_TREE, restype);
5732 /* If this fcn was already referenced via a block-scope `extern' decl
5733 (or an implicit decl), propagate certain information about the usage. */
5734 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5735 TREE_ADDRESSABLE (current_function_decl) = 1;
5737 immediate_size_expand = old_immediate_size_expand;
5739 start_fname_decls ();
5741 return 1;
5744 /* Subroutine of store_parm_decls which handles new-style function
5745 definitions (prototype format). The parms already have decls, so we
5746 need only record them as in effect and complain if any redundant
5747 old-style parm decls were written. */
5748 static void
5749 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5751 tree decl;
5752 tree parms = ARG_INFO_PARMS (arg_info);
5753 tree tags = ARG_INFO_TAGS (arg_info);
5754 tree others = ARG_INFO_OTHERS (arg_info);
5756 if (current_scope->bindings)
5758 error ("%Jold-style parameter declarations in prototyped "
5759 "function definition", fndecl);
5761 /* Get rid of the old-style declarations. */
5762 pop_scope ();
5763 push_scope ();
5765 /* Don't issue this warning for nested functions, and don't issue this
5766 warning if we got here because ARG_INFO_TYPES was error_mark_node
5767 (this happens when a function definition has just an ellipsis in
5768 its parameter list). */
5769 else if (warn_traditional && !in_system_header
5770 && DECL_CONTEXT (fndecl) == current_file_decl
5771 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5772 warning ("%Jtraditional C rejects ISO C style function definitions",
5773 fndecl);
5775 /* Now make all the parameter declarations visible in the function body.
5776 We can bypass most of the grunt work of pushdecl. */
5777 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5779 DECL_CONTEXT (decl) = current_function_decl;
5780 if (DECL_NAME (decl))
5781 bind (DECL_NAME (decl), decl, current_scope);
5782 else
5783 error ("%Jparameter name omitted", decl);
5786 /* Record the parameter list in the function declaration. */
5787 DECL_ARGUMENTS (fndecl) = parms;
5789 /* Now make all the ancillary declarations visible, likewise. */
5790 for (decl = others; decl; decl = TREE_CHAIN (decl))
5792 DECL_CONTEXT (decl) = current_function_decl;
5793 if (DECL_NAME (decl))
5794 bind (DECL_NAME (decl), decl, current_scope);
5797 /* And all the tag declarations. */
5798 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5799 if (TREE_PURPOSE (decl))
5800 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope);
5803 /* Subroutine of store_parm_decls which handles old-style function
5804 definitions (separate parameter list and declarations). */
5806 static void
5807 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5809 struct c_binding *b;
5810 tree parm, decl, last;
5811 tree parmids = ARG_INFO_PARMS (arg_info);
5813 /* We use DECL_WEAK as a flag to show which parameters have been
5814 seen already, since it is not used on PARM_DECL. */
5815 #ifdef ENABLE_CHECKING
5816 for (b = current_scope->bindings; b; b = b->prev)
5817 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5818 abort ();
5819 #endif
5821 if (warn_old_style_definition && !in_system_header)
5822 warning ("%Jold-style function definition", fndecl);
5824 /* Match each formal parameter name with its declaration. Save each
5825 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5826 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5828 if (TREE_VALUE (parm) == 0)
5830 error ("%Jparameter name missing from parameter list", fndecl);
5831 TREE_PURPOSE (parm) = 0;
5832 continue;
5835 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5836 if (b && b->contour == current_scope)
5838 decl = b->decl;
5839 /* If we got something other than a PARM_DECL it is an error. */
5840 if (TREE_CODE (decl) != PARM_DECL)
5841 error ("%J'%D' declared as a non-parameter", decl, decl);
5842 /* If the declaration is already marked, we have a duplicate
5843 name. Complain and ignore the duplicate. */
5844 else if (DECL_WEAK (decl))
5846 error ("%Jmultiple parameters named '%D'", decl, decl);
5847 TREE_PURPOSE (parm) = 0;
5848 continue;
5850 /* If the declaration says "void", complain and turn it into
5851 an int. */
5852 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5854 error ("%Jparameter '%D' declared with void type", decl, decl);
5855 TREE_TYPE (decl) = integer_type_node;
5856 DECL_ARG_TYPE (decl) = integer_type_node;
5857 layout_decl (decl, 0);
5860 /* If no declaration found, default to int. */
5861 else
5863 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5864 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5865 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5866 pushdecl (decl);
5868 if (flag_isoc99)
5869 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5870 else if (extra_warnings)
5871 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5874 TREE_PURPOSE (parm) = decl;
5875 DECL_WEAK (decl) = 1;
5878 /* Now examine the parms chain for incomplete declarations
5879 and declarations with no corresponding names. */
5881 for (b = current_scope->bindings; b; b = b->prev)
5883 parm = b->decl;
5884 if (TREE_CODE (parm) != PARM_DECL)
5885 continue;
5887 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5889 error ("%Jparameter '%D' has incomplete type", parm, parm);
5890 TREE_TYPE (parm) = error_mark_node;
5893 if (! DECL_WEAK (parm))
5895 error ("%Jdeclaration for parameter '%D' but no such parameter",
5896 parm, parm);
5898 /* Pretend the parameter was not missing.
5899 This gets us to a standard state and minimizes
5900 further error messages. */
5901 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5905 /* Chain the declarations together in the order of the list of
5906 names. Store that chain in the function decl, replacing the
5907 list of names. Update the current scope to match. */
5908 DECL_ARGUMENTS (fndecl) = 0;
5910 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5911 if (TREE_PURPOSE (parm))
5912 break;
5913 if (parm && TREE_PURPOSE (parm))
5915 last = TREE_PURPOSE (parm);
5916 DECL_ARGUMENTS (fndecl) = last;
5917 DECL_WEAK (last) = 0;
5919 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5920 if (TREE_PURPOSE (parm))
5922 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5923 last = TREE_PURPOSE (parm);
5924 DECL_WEAK (last) = 0;
5926 TREE_CHAIN (last) = 0;
5929 /* If there was a previous prototype,
5930 set the DECL_ARG_TYPE of each argument according to
5931 the type previously specified, and report any mismatches. */
5933 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5935 tree type;
5936 for (parm = DECL_ARGUMENTS (fndecl),
5937 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5938 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5939 != void_type_node));
5940 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
5942 if (parm == 0 || type == 0
5943 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
5945 error ("number of arguments doesn't match prototype");
5946 error ("%Hprototype declaration",
5947 &current_function_prototype_locus);
5948 break;
5950 /* Type for passing arg must be consistent with that
5951 declared for the arg. ISO C says we take the unqualified
5952 type for parameters declared with qualified type. */
5953 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
5954 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
5955 COMPARE_STRICT))
5957 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
5958 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
5960 /* Adjust argument to match prototype. E.g. a previous
5961 `int foo(float);' prototype causes
5962 `int foo(x) float x; {...}' to be treated like
5963 `int foo(float x) {...}'. This is particularly
5964 useful for argument types like uid_t. */
5965 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
5967 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
5968 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
5969 && TYPE_PRECISION (TREE_TYPE (parm))
5970 < TYPE_PRECISION (integer_type_node))
5971 DECL_ARG_TYPE (parm) = integer_type_node;
5973 if (pedantic)
5975 pedwarn ("promoted argument '%D' "
5976 "doesn't match prototype", parm);
5977 pedwarn ("%Hprototype declaration",
5978 &current_function_prototype_locus);
5981 else
5983 error ("argument '%D' doesn't match prototype", parm);
5984 error ("%Hprototype declaration",
5985 &current_function_prototype_locus);
5989 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
5992 /* Otherwise, create a prototype that would match. */
5994 else
5996 tree actual = 0, last = 0, type;
5998 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6000 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6001 if (last)
6002 TREE_CHAIN (last) = type;
6003 else
6004 actual = type;
6005 last = type;
6007 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6008 if (last)
6009 TREE_CHAIN (last) = type;
6010 else
6011 actual = type;
6013 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6014 of the type of this function, but we need to avoid having this
6015 affect the types of other similarly-typed functions, so we must
6016 first force the generation of an identical (but separate) type
6017 node for the relevant function type. The new node we create
6018 will be a variant of the main variant of the original function
6019 type. */
6021 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6023 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6027 /* Store the parameter declarations into the current function declaration.
6028 This is called after parsing the parameter declarations, before
6029 digesting the body of the function.
6031 For an old-style definition, construct a prototype out of the old-style
6032 parameter declarations and inject it into the function's type. */
6034 void
6035 store_parm_decls (void)
6037 tree fndecl = current_function_decl;
6039 /* The function containing FNDECL, if any. */
6040 tree context = decl_function_context (fndecl);
6042 /* The argument information block for FNDECL. */
6043 tree arg_info = DECL_ARGUMENTS (fndecl);
6045 /* True if this definition is written with a prototype. Note:
6046 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6047 list in a function definition as equivalent to (void) -- an
6048 empty argument list specifies the function has no parameters,
6049 but only (void) sets up a prototype for future calls. */
6050 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6052 if (proto)
6053 store_parm_decls_newstyle (fndecl, arg_info);
6054 else
6055 store_parm_decls_oldstyle (fndecl, arg_info);
6057 /* The next call to push_scope will be a function body. */
6059 next_is_function_body = true;
6061 /* Write a record describing this function definition to the prototypes
6062 file (if requested). */
6064 gen_aux_info_record (fndecl, 1, 0, proto);
6066 /* Initialize the RTL code for the function. */
6067 allocate_struct_function (fndecl);
6069 /* Begin the statement tree for this function. */
6070 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6072 /* If this is a nested function, save away the sizes of any
6073 variable-size types so that we can expand them when generating
6074 RTL. */
6075 if (context)
6077 tree t;
6079 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6080 = nreverse (get_pending_sizes ());
6081 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6083 t = TREE_CHAIN (t))
6084 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6087 /* This function is being processed in whole-function mode. */
6088 cfun->x_whole_function_mode_p = 1;
6090 /* Even though we're inside a function body, we still don't want to
6091 call expand_expr to calculate the size of a variable-sized array.
6092 We haven't necessarily assigned RTL to all variables yet, so it's
6093 not safe to try to expand expressions involving them. */
6094 immediate_size_expand = 0;
6095 cfun->x_dont_save_pending_sizes_p = 1;
6098 /* Finish up a function declaration and compile that function
6099 all the way to assembler language output. The free the storage
6100 for the function definition.
6102 This is called after parsing the body of the function definition. */
6104 void
6105 finish_function (void)
6107 tree fndecl = current_function_decl;
6109 /* When a function declaration is totally empty, e.g.
6110 void foo(void) { }
6111 (the argument list is irrelevant) the compstmt rule will not
6112 bother calling push_scope/pop_scope, which means we get here with
6113 the scope stack out of sync. Detect this situation by noticing
6114 that current_scope is still as store_parm_decls left it, and do
6115 a dummy push/pop to get back to consistency.
6116 Note that the call to push_scope does not actually push another
6117 scope - see there for details. */
6119 if (current_scope->parm_flag && next_is_function_body)
6121 push_scope ();
6122 pop_scope ();
6125 if (TREE_CODE (fndecl) == FUNCTION_DECL
6126 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6128 tree args = DECL_ARGUMENTS (fndecl);
6129 for (; args; args = TREE_CHAIN (args))
6131 tree type = TREE_TYPE (args);
6132 if (INTEGRAL_TYPE_P (type)
6133 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6134 DECL_ARG_TYPE (args) = integer_type_node;
6138 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6139 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6141 /* Must mark the RESULT_DECL as being in this function. */
6143 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6144 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6146 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6148 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6149 != integer_type_node)
6151 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6152 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6153 if (!warn_main)
6154 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6156 else
6158 #ifdef DEFAULT_MAIN_RETURN
6159 /* Make it so that `main' always returns success by default. */
6160 DEFAULT_MAIN_RETURN;
6161 #else
6162 if (flag_isoc99)
6163 c_expand_return (integer_zero_node);
6164 #endif
6168 finish_fname_decls ();
6170 /* Tie off the statement tree for this function. */
6171 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6173 /* Complain if there's just no return statement. */
6174 if (warn_return_type
6175 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6176 && !current_function_returns_value && !current_function_returns_null
6177 /* Don't complain if we abort. */
6178 && !current_function_returns_abnormally
6179 /* Don't warn for main(). */
6180 && !MAIN_NAME_P (DECL_NAME (fndecl))
6181 /* Or if they didn't actually specify a return type. */
6182 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6183 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6184 inline function, as we might never be compiled separately. */
6185 && DECL_INLINE (fndecl))
6186 warning ("no return statement in function returning non-void");
6188 /* With just -Wextra, complain only if function returns both with
6189 and without a value. */
6190 if (extra_warnings
6191 && current_function_returns_value
6192 && current_function_returns_null)
6193 warning ("this function may return with or without a value");
6195 /* We're leaving the context of this function, so zap cfun.
6196 It's still in DECL_STRUCT_FUNCTION , and we'll restore it in
6197 tree_rest_of_compilation. */
6198 cfun = NULL;
6200 /* ??? Objc emits functions after finalizing the compilation unit.
6201 This should be cleaned up later and this conditional removed. */
6202 if (!cgraph_global_info_ready)
6203 cgraph_finalize_function (fndecl, false);
6204 else
6205 c_expand_body (fndecl);
6206 current_function_decl = NULL;
6209 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6210 then we are already in the process of generating RTL for another
6211 function. */
6213 static void
6214 c_expand_body_1 (tree fndecl, int nested_p)
6216 if (nested_p)
6218 /* Make sure that we will evaluate variable-sized types involved
6219 in our function's type. */
6220 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6222 /* Squirrel away our current state. */
6223 push_function_context ();
6226 tree_rest_of_compilation (fndecl, nested_p);
6228 if (nested_p)
6229 /* Return to the enclosing function. */
6230 pop_function_context ();
6232 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6234 if (targetm.have_ctors_dtors)
6235 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6236 DEFAULT_INIT_PRIORITY);
6237 else
6238 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6241 if (DECL_STATIC_DESTRUCTOR (fndecl))
6243 if (targetm.have_ctors_dtors)
6244 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6245 DEFAULT_INIT_PRIORITY);
6246 else
6247 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6251 /* Like c_expand_body_1 but only for unnested functions. */
6253 void
6254 c_expand_body (tree fndecl)
6257 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6258 c_expand_body_1 (fndecl, 0);
6261 /* Check the declarations given in a for-loop for satisfying the C99
6262 constraints. */
6263 void
6264 check_for_loop_decls (void)
6266 struct c_binding *b;
6268 if (!flag_isoc99)
6270 /* If we get here, declarations have been used in a for loop without
6271 the C99 for loop scope. This doesn't make much sense, so don't
6272 allow it. */
6273 error ("'for' loop initial declaration used outside C99 mode");
6274 return;
6276 /* C99 subclause 6.8.5 paragraph 3:
6278 [#3] The declaration part of a for statement shall only
6279 declare identifiers for objects having storage class auto or
6280 register.
6282 It isn't clear whether, in this sentence, "identifiers" binds to
6283 "shall only declare" or to "objects" - that is, whether all identifiers
6284 declared must be identifiers for objects, or whether the restriction
6285 only applies to those that are. (A question on this in comp.std.c
6286 in November 2000 received no answer.) We implement the strictest
6287 interpretation, to avoid creating an extension which later causes
6288 problems. */
6290 for (b = current_scope->bindings; b; b = b->prev)
6292 tree id = b->id;
6293 tree decl = b->decl;
6295 if (!id)
6296 continue;
6298 switch (TREE_CODE (decl))
6300 case VAR_DECL:
6301 if (TREE_STATIC (decl))
6302 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6303 "initial declaration", decl, decl);
6304 else if (DECL_EXTERNAL (decl))
6305 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6306 "initial declaration", decl, decl);
6307 break;
6309 case RECORD_TYPE:
6310 error ("'struct %E' declared in 'for' loop initial declaration", id);
6311 break;
6312 case UNION_TYPE:
6313 error ("'union %E' declared in 'for' loop initial declaration", id);
6314 break;
6315 case ENUMERAL_TYPE:
6316 error ("'enum %E' declared in 'for' loop initial declaration", id);
6317 break;
6318 default:
6319 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6320 "initial declaration", decl, decl);
6325 /* Save and reinitialize the variables
6326 used during compilation of a C function. */
6328 void
6329 c_push_function_context (struct function *f)
6331 struct language_function *p;
6332 p = ggc_alloc (sizeof (struct language_function));
6333 f->language = p;
6335 p->base.x_stmt_tree = c_stmt_tree;
6336 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6337 p->x_in_iteration_stmt = c_in_iteration_stmt;
6338 p->x_in_case_stmt = c_in_case_stmt;
6339 p->returns_value = current_function_returns_value;
6340 p->returns_null = current_function_returns_null;
6341 p->returns_abnormally = current_function_returns_abnormally;
6342 p->warn_about_return_type = warn_about_return_type;
6343 p->extern_inline = current_extern_inline;
6346 /* Restore the variables used during compilation of a C function. */
6348 void
6349 c_pop_function_context (struct function *f)
6351 struct language_function *p = f->language;
6353 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6354 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6356 /* Stop pointing to the local nodes about to be freed. */
6357 /* But DECL_INITIAL must remain nonzero so we know this
6358 was an actual function definition. */
6359 DECL_INITIAL (current_function_decl) = error_mark_node;
6360 DECL_ARGUMENTS (current_function_decl) = 0;
6363 c_stmt_tree = p->base.x_stmt_tree;
6364 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6365 c_in_iteration_stmt = p->x_in_iteration_stmt;
6366 c_in_case_stmt = p->x_in_case_stmt;
6367 current_function_returns_value = p->returns_value;
6368 current_function_returns_null = p->returns_null;
6369 current_function_returns_abnormally = p->returns_abnormally;
6370 warn_about_return_type = p->warn_about_return_type;
6371 current_extern_inline = p->extern_inline;
6373 f->language = NULL;
6376 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6378 void
6379 c_dup_lang_specific_decl (tree decl)
6381 struct lang_decl *ld;
6383 if (!DECL_LANG_SPECIFIC (decl))
6384 return;
6386 ld = ggc_alloc (sizeof (struct lang_decl));
6387 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6388 DECL_LANG_SPECIFIC (decl) = ld;
6391 /* The functions below are required for functionality of doing
6392 function at once processing in the C front end. Currently these
6393 functions are not called from anywhere in the C front end, but as
6394 these changes continue, that will change. */
6396 /* Returns nonzero if the current statement is a full expression,
6397 i.e. temporaries created during that statement should be destroyed
6398 at the end of the statement. */
6401 stmts_are_full_exprs_p (void)
6403 return 0;
6406 /* Returns the stmt_tree (if any) to which statements are currently
6407 being added. If there is no active statement-tree, NULL is
6408 returned. */
6410 stmt_tree
6411 current_stmt_tree (void)
6413 return &c_stmt_tree;
6416 /* Returns the stack of SCOPE_STMTs for the current function. */
6418 tree *
6419 current_scope_stmt_stack (void)
6421 return &c_scope_stmt_stack;
6424 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6425 C. */
6428 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6430 return 0;
6433 /* Dummy function in place of callback used by C++. */
6435 void
6436 extract_interface_info (void)
6440 /* Return a new COMPOUND_STMT, after adding it to the current
6441 statement tree. */
6443 tree
6444 c_begin_compound_stmt (void)
6446 tree stmt;
6448 /* Create the COMPOUND_STMT. */
6449 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6451 return stmt;
6454 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6455 common code. */
6457 void
6458 c_expand_decl_stmt (tree t)
6460 tree decl = DECL_STMT_DECL (t);
6462 /* Expand nested functions. */
6463 if (TREE_CODE (decl) == FUNCTION_DECL
6464 && DECL_CONTEXT (decl) == current_function_decl
6465 && DECL_SAVED_TREE (decl))
6466 c_expand_body_1 (decl, 1);
6469 /* Return the global value of T as a symbol. */
6471 tree
6472 identifier_global_value (tree t)
6474 struct c_binding *b;
6476 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6477 if (b->contour == file_scope || b->contour == external_scope)
6478 return b->decl;
6480 return 0;
6483 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6484 otherwise the name is found in ridpointers from RID_INDEX. */
6486 void
6487 record_builtin_type (enum rid rid_index, const char *name, tree type)
6489 tree id;
6490 if (name == 0)
6491 id = ridpointers[(int) rid_index];
6492 else
6493 id = get_identifier (name);
6494 pushdecl (build_decl (TYPE_DECL, id, type));
6497 /* Build the void_list_node (void_type_node having been created). */
6498 tree
6499 build_void_list_node (void)
6501 tree t = build_tree_list (NULL_TREE, void_type_node);
6502 return t;
6505 /* Return something to represent absolute declarators containing a *.
6506 TARGET is the absolute declarator that the * contains.
6507 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6508 to apply to the pointer type, represented as identifiers, possible mixed
6509 with attributes.
6511 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6512 if attributes are present) and whose type is the modifier list. */
6514 tree
6515 make_pointer_declarator (tree type_quals_attrs, tree target)
6517 tree quals, attrs;
6518 tree itarget = target;
6519 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6520 if (attrs != NULL_TREE)
6521 itarget = tree_cons (attrs, target, NULL_TREE);
6522 return build1 (INDIRECT_REF, quals, itarget);
6525 /* A wrapper around lhd_set_decl_assembler_name that gives static
6526 variables their C names if they are at file scope and only one
6527 translation unit is being compiled, for backwards compatibility
6528 with certain bizarre assembler hacks (like crtstuff.c). */
6530 void
6531 c_static_assembler_name (tree decl)
6533 if (num_in_fnames == 1
6534 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6535 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6536 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6537 else
6538 lhd_set_decl_assembler_name (decl);
6541 /* Perform final processing on file-scope data. */
6542 static void
6543 c_write_global_declarations_1 (tree globals)
6545 size_t len = list_length (globals);
6546 tree *vec = xmalloc (sizeof (tree) * len);
6547 size_t i;
6548 tree decl;
6550 /* Process the decls in the order they were written. */
6551 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6552 vec[i] = decl;
6554 wrapup_global_declarations (vec, len);
6555 check_global_declarations (vec, len);
6557 free (vec);
6560 void
6561 c_write_global_declarations (void)
6563 tree t;
6565 /* We don't want to do this if generating a PCH. */
6566 if (pch_file)
6567 return;
6569 /* Process all file scopes in this compilation. */
6570 for (t = current_file_decl; t; t = TREE_CHAIN (t))
6571 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6573 /* Now do the same for the externals scope. */
6574 t = pop_scope ();
6575 if (t)
6576 c_write_global_declarations_1 (BLOCK_VARS (t));
6579 #include "gt-c-decl.h"