* options.c (gfc_handle_module_path_options): Fix buffer overrun.
[official-gcc.git] / gcc / c-decl.c
blob8ca5b086ca2416799a30cd41dbb6e535a72b63a8
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 "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "libfuncs.h"
60 #include "except.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 enum decl_context
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* While defining an enum type, this is 1 plus the last enumerator
80 constant value. Note that will do not have to save this or `enum_overflow'
81 around nested function definition since such a definition could only
82 occur in an enum value expression and we don't use these variables in
83 that case. */
85 static tree enum_next_value;
87 /* Nonzero means that there was overflow computing enum_next_value. */
89 static int enum_overflow;
91 /* These #defines are for clarity in working with the information block
92 returned by get_parm_info. */
93 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
94 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
95 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
96 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
98 /* The file and line that the prototype came from if this is an
99 old-style definition; used for diagnostics in
100 store_parm_decls_oldstyle. */
102 static location_t current_function_prototype_locus;
104 /* The current statement tree. */
106 static GTY(()) struct stmt_tree_s c_stmt_tree;
108 /* The current scope statement stack. */
110 static GTY(()) tree c_scope_stmt_stack;
112 /* State saving variables. */
113 int c_in_iteration_stmt;
114 int c_in_case_stmt;
116 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
117 included in this invocation. Note that the current translation
118 unit is not included in this list. */
120 static GTY(()) tree all_translation_units;
122 /* A list of decls to be made automatically visible in each file scope. */
123 static GTY(()) tree visible_builtins;
125 /* Set to 0 at beginning of a function definition, set to 1 if
126 a return statement that specifies a return value is seen. */
128 int current_function_returns_value;
130 /* Set to 0 at beginning of a function definition, set to 1 if
131 a return statement with no argument is seen. */
133 int current_function_returns_null;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a call to a noreturn function is seen. */
138 int current_function_returns_abnormally;
140 /* Set to nonzero by `grokdeclarator' for a function
141 whose return type is defaulted, if warnings for this are desired. */
143 static int warn_about_return_type;
145 /* Nonzero when starting a function declared `extern inline'. */
147 static int current_extern_inline;
149 /* True means global_bindings_p should return false even if the scope stack
150 says we are in file scope. */
151 bool c_override_global_bindings_to_false;
154 /* Each c_binding structure describes one binding of an identifier to
155 a decl. All the decls in a scope - irrespective of namespace - are
156 chained together by the ->prev field, which (as the name implies)
157 runs in reverse order. All the decls in a given namespace bound to
158 a given identifier are chained by the ->shadowed field, which runs
159 from inner to outer scopes. Finally, the ->contour field points
160 back to the relevant scope structure; this is mainly used to make
161 decls in the externals scope invisible (see below).
163 The ->decl field usually points to a DECL node, but there are two
164 exceptions. In the namespace of type tags, the bound entity is a
165 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
166 identifier is encountered, it is bound to error_mark_node to
167 suppress further errors about that identifier in the current
168 function. */
170 struct c_binding GTY((chain_next ("%h.prev")))
172 tree decl; /* the decl bound */
173 tree id; /* the identifier it's bound to */
174 struct c_binding *prev; /* the previous decl in this scope */
175 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
176 struct c_scope *contour; /* the scope in which this decl is bound */
179 #define I_SYMBOL_BINDING(node) \
180 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
181 #define I_SYMBOL_DECL(node) \
182 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
184 #define I_TAG_BINDING(node) \
185 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
186 #define I_TAG_DECL(node) \
187 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
189 #define I_LABEL_BINDING(node) \
190 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
191 #define I_LABEL_DECL(node) \
192 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
194 /* Each C symbol points to three linked lists of c_binding structures.
195 These describe the values of the identifier in the three different
196 namespaces defined by the language. */
198 struct lang_identifier GTY(())
200 struct c_common_identifier common_id;
201 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
202 struct c_binding *tag_binding; /* struct/union/enum tags */
203 struct c_binding *label_binding; /* labels */
206 /* Validate c-lang.c's assumptions. */
207 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
208 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
210 /* The resulting tree type. */
212 union lang_tree_node
213 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
214 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *)TYPE_NEXT_VARIANT (&%h.generic) : (union lang_tree_node *)TREE_CHAIN (&%h.generic)")))
216 union tree_node GTY ((tag ("0"),
217 desc ("tree_node_structure (&%h)")))
218 generic;
219 struct lang_identifier GTY ((tag ("1"))) identifier;
222 /* Each c_scope structure describes the complete contents of one
223 scope. Four scopes are distinguished specially: the innermost or
224 current scope, the innermost function scope, the file scope (always
225 the second to outermost) and the outermost or external scope.
227 Most declarations are recorded in the current scope.
229 All normal label declarations are recorded in the innermost
230 function scope, as are bindings of undeclared identifiers to
231 error_mark_node. (GCC permits nested functions as an extension,
232 hence the 'innermost' qualifier.) Explicitly declared labels
233 (using the __label__ extension) appear in the current scope.
235 Being in the file scope (current_scope == file_scope) causes
236 special behavior in several places below. Also, under some
237 conditions the Objective-C front end records declarations in the
238 file scope even though that isn't the current scope.
240 All declarations with external linkage are recorded in the external
241 scope, even if they aren't visible there; this models the fact that
242 such declarations are visible to the entire program, and (with a
243 bit of cleverness, see pushdecl) allows diagnosis of some violations
244 of C99 6.2.2p7 and 6.2.7p2:
246 If, within the same translation unit, the same identifier appears
247 with both internal and external linkage, the behavior is
248 undefined.
250 All declarations that refer to the same object or function shall
251 have compatible type; otherwise, the behavior is undefined.
253 Initially only the built-in declarations, which describe compiler
254 intrinsic functions plus a subset of the standard library, are in
255 this scope.
257 The order of the blocks list matters, and it is frequently appended
258 to. To avoid having to walk all the way to the end of the list on
259 each insertion, or reverse the list later, we maintain a pointer to
260 the last list entry. (FIXME: It should be feasible to use a reversed
261 list here.)
263 The bindings list is strictly in reverse order of declarations;
264 pop_scope relies on this. */
267 struct c_scope GTY((chain_next ("%h.outer")))
269 /* The scope containing this one. */
270 struct c_scope *outer;
272 /* The next outermost function scope. */
273 struct c_scope *outer_function;
275 /* All bindings in this scope. */
276 struct c_binding *bindings;
278 /* For each scope (except the global one), a chain of BLOCK nodes
279 for all the scopes that were entered and exited one level down. */
280 tree blocks;
281 tree blocks_last;
283 /* The depth of this scope. Used to keep the ->shadowed chain of
284 bindings sorted innermost to outermost. */
285 unsigned int depth : 28;
287 /* True if we are currently filling this scope with parameter
288 declarations. */
289 BOOL_BITFIELD parm_flag : 1;
291 /* True if we already complained about forward parameter decls
292 in this scope. This prevents double warnings on
293 foo (int a; int b; ...) */
294 BOOL_BITFIELD warned_forward_parm_decls : 1;
296 /* True if this is the outermost block scope of a function body.
297 This scope contains the parameters, the local variables declared
298 in the outermost block, and all the labels (except those in
299 nested functions, or declared at block scope with __label__). */
300 BOOL_BITFIELD function_body : 1;
302 /* True means make a BLOCK for this scope no matter what. */
303 BOOL_BITFIELD keep : 1;
306 /* The scope currently in effect. */
308 static GTY(()) struct c_scope *current_scope;
310 /* The innermost function scope. Ordinary (not explicitly declared)
311 labels, bindings to error_mark_node, and the lazily-created
312 bindings of __func__ and its friends get this scope. */
314 static GTY(()) struct c_scope *current_function_scope;
316 /* The C file scope. This is reset for each input translation unit. */
318 static GTY(()) struct c_scope *file_scope;
320 /* The outermost scope. This is used for all declarations with
321 external linkage, and only these, hence the name. */
323 static GTY(()) struct c_scope *external_scope;
325 /* A chain of c_scope structures awaiting reuse. */
327 static GTY((deletable)) struct c_scope *scope_freelist;
329 /* A chain of c_binding structures awaiting reuse. */
331 static GTY((deletable)) struct c_binding *binding_freelist;
333 /* Append VAR to LIST in scope SCOPE. */
334 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
335 struct c_scope *s_ = (scope); \
336 tree d_ = (decl); \
337 if (s_->list##_last) \
338 TREE_CHAIN (s_->list##_last) = d_; \
339 else \
340 s_->list = d_; \
341 s_->list##_last = d_; \
342 } while (0)
344 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
345 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
346 struct c_scope *t_ = (tscope); \
347 struct c_scope *f_ = (fscope); \
348 if (t_->to##_last) \
349 TREE_CHAIN (t_->to##_last) = f_->from; \
350 else \
351 t_->to = f_->from; \
352 t_->to##_last = f_->from##_last; \
353 } while (0)
355 /* True means unconditionally make a BLOCK for the next scope pushed. */
357 static bool keep_next_level_flag;
359 /* True means the next call to push_scope will be the outermost scope
360 of a function body, so do not push a new scope, merely cease
361 expecting parameter decls. */
363 static bool next_is_function_body;
365 /* Functions called automatically at the beginning and end of execution. */
367 tree static_ctors, static_dtors;
369 /* Forward declarations. */
370 static tree lookup_name_in_scope (tree, struct c_scope *);
371 static tree c_make_fname_decl (tree, int);
372 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
373 static tree grokparms (tree, int);
374 static void layout_array_type (tree);
376 /* States indicating how grokdeclarator() should handle declspecs marked
377 with __attribute__((deprecated)). An object declared as
378 __attribute__((deprecated)) suppresses warnings of uses of other
379 deprecated items. */
381 enum deprecated_states {
382 DEPRECATED_NORMAL,
383 DEPRECATED_SUPPRESS
386 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
388 void
389 c_print_identifier (FILE *file, tree node, int indent)
391 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
392 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
393 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
394 if (C_IS_RESERVED_WORD (node))
396 tree rid = ridpointers[C_RID_CODE (node)];
397 indent_to (file, indent + 4);
398 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
399 (void *) rid, IDENTIFIER_POINTER (rid));
403 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
404 which may be any of several kinds of DECL or TYPE or error_mark_node,
405 in the scope SCOPE. */
406 static void
407 bind (tree name, tree decl, struct c_scope *scope)
409 struct c_binding *b, **here;
411 if (binding_freelist)
413 b = binding_freelist;
414 binding_freelist = b->prev;
416 else
417 b = ggc_alloc (sizeof (struct c_binding));
419 b->shadowed = 0;
420 b->decl = decl;
421 b->id = name;
422 b->contour = scope;
424 b->prev = scope->bindings;
425 scope->bindings = b;
427 if (!name)
428 return;
430 switch (TREE_CODE (decl))
432 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
433 case ENUMERAL_TYPE:
434 case UNION_TYPE:
435 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
436 case VAR_DECL:
437 case FUNCTION_DECL:
438 case TYPE_DECL:
439 case CONST_DECL:
440 case PARM_DECL:
441 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
443 default:
444 abort ();
447 /* Locate the appropriate place in the chain of shadowed decls
448 to insert this binding. Normally, scope == current_scope and
449 this does nothing. */
450 while (*here && (*here)->contour->depth > scope->depth)
451 here = &(*here)->shadowed;
453 b->shadowed = *here;
454 *here = b;
457 /* Clear the binding structure B, stick it on the binding_freelist,
458 and return the former value of b->prev. This is used by pop_scope
459 and get_parm_info to iterate destructively over all the bindings
460 from a given scope. */
461 static struct c_binding *
462 free_binding_and_advance (struct c_binding *b)
464 struct c_binding *prev = b->prev;
466 b->id = 0;
467 b->decl = 0;
468 b->contour = 0;
469 b->shadowed = 0;
470 b->prev = binding_freelist;
471 binding_freelist = b;
473 return prev;
477 /* Hook called at end of compilation to assume 1 elt
478 for a file-scope tentative array defn that wasn't complete before. */
480 void
481 c_finish_incomplete_decl (tree decl)
483 if (TREE_CODE (decl) == VAR_DECL)
485 tree type = TREE_TYPE (decl);
486 if (type != error_mark_node
487 && TREE_CODE (type) == ARRAY_TYPE
488 && ! DECL_EXTERNAL (decl)
489 && TYPE_DOMAIN (type) == 0)
491 warning ("%Jarray '%D' assumed to have one element", decl, decl);
493 complete_array_type (type, NULL_TREE, 1);
495 layout_decl (decl, 0);
500 /* The Objective-C front-end often needs to determine the current scope. */
502 void *
503 get_current_scope (void)
505 return current_scope;
508 /* The following function is used only by Objective-C. It needs to live here
509 because it accesses the innards of c_scope. */
511 void
512 objc_mark_locals_volatile (void *enclosing_blk)
514 struct c_scope *scope;
515 struct c_binding *b;
517 for (scope = current_scope;
518 scope && scope != enclosing_blk;
519 scope = scope->outer)
521 for (b = scope->bindings; b; b = b->prev)
523 if (TREE_CODE (b->decl) == VAR_DECL
524 || TREE_CODE (b->decl) == PARM_DECL)
526 C_DECL_REGISTER (b->decl) = 0;
527 DECL_REGISTER (b->decl) = 0;
528 TREE_THIS_VOLATILE (b->decl) = 1;
532 /* Do not climb up past the current function. */
533 if (scope->function_body)
534 break;
538 /* Nonzero if we are currently in file scope. */
541 global_bindings_p (void)
543 return current_scope == file_scope && !c_override_global_bindings_to_false;
546 void
547 keep_next_level (void)
549 keep_next_level_flag = true;
552 /* Identify this scope as currently being filled with parameters. */
554 void
555 declare_parm_level (void)
557 current_scope->parm_flag = true;
560 void
561 push_scope (void)
563 if (next_is_function_body)
565 /* This is the transition from the parameters to the top level
566 of the function body. These are the same scope
567 (C99 6.2.1p4,6) so we do not push another scope structure.
568 next_is_function_body is set only by store_parm_decls, which
569 in turn is called when and only when we are about to
570 encounter the opening curly brace for the function body.
572 The outermost block of a function always gets a BLOCK node,
573 because the debugging output routines expect that each
574 function has at least one BLOCK. */
575 current_scope->parm_flag = false;
576 current_scope->function_body = true;
577 current_scope->keep = true;
578 current_scope->outer_function = current_function_scope;
579 current_function_scope = current_scope;
581 keep_next_level_flag = false;
582 next_is_function_body = false;
584 else
586 struct c_scope *scope;
587 if (scope_freelist)
589 scope = scope_freelist;
590 scope_freelist = scope->outer;
592 else
593 scope = ggc_alloc_cleared (sizeof (struct c_scope));
595 scope->keep = keep_next_level_flag;
596 scope->outer = current_scope;
597 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
599 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
600 possible. */
601 if (current_scope && scope->depth == 0)
603 scope->depth--;
604 sorry ("GCC supports only %u nested scopes\n", scope->depth);
607 current_scope = scope;
608 keep_next_level_flag = false;
612 /* Exit a scope. Restore the state of the identifier-decl mappings
613 that were in effect when this scope was entered. Return a BLOCK
614 node containing all the DECLs in this scope that are of interest
615 to debug info generation. */
617 tree
618 pop_scope (void)
620 struct c_scope *scope = current_scope;
621 tree block, context, p;
622 struct c_binding *b;
624 bool functionbody = scope->function_body;
625 bool keep = functionbody || scope->keep || scope->bindings;
627 /* If appropriate, create a BLOCK to record the decls for the life
628 of this function. */
629 block = 0;
630 if (keep)
632 block = make_node (BLOCK);
633 BLOCK_SUBBLOCKS (block) = scope->blocks;
634 TREE_USED (block) = 1;
636 /* In each subblock, record that this is its superior. */
637 for (p = scope->blocks; p; p = TREE_CHAIN (p))
638 BLOCK_SUPERCONTEXT (p) = block;
640 BLOCK_VARS (block) = 0;
643 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
644 scope must be set so that they point to the appropriate
645 construct, i.e. either to the current FUNCTION_DECL node, or
646 else to the BLOCK node we just constructed.
648 Note that for tagged types whose scope is just the formal
649 parameter list for some function type specification, we can't
650 properly set their TYPE_CONTEXTs here, because we don't have a
651 pointer to the appropriate FUNCTION_TYPE node readily available
652 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
653 type nodes get set in `grokdeclarator' as soon as we have created
654 the FUNCTION_TYPE node which will represent the "scope" for these
655 "parameter list local" tagged types. */
656 if (scope->function_body)
657 context = current_function_decl;
658 else if (scope == file_scope)
660 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
661 TREE_CHAIN (file_decl) = all_translation_units;
662 all_translation_units = file_decl;
663 context = file_decl;
665 else
666 context = block;
668 /* Clear all bindings in this scope. */
669 for (b = scope->bindings; b; b = free_binding_and_advance (b))
671 p = b->decl;
672 switch (TREE_CODE (p))
674 case LABEL_DECL:
675 /* Warnings for unused labels, errors for undefined labels. */
676 if (TREE_USED (p) && !DECL_INITIAL (p))
678 error ("%Jlabel `%D' used but not defined", p, p);
679 DECL_INITIAL (p) = error_mark_node;
681 else if (!TREE_USED (p) && warn_unused_label)
683 if (DECL_INITIAL (p))
684 warning ("%Jlabel `%D' defined but not used", p, p);
685 else
686 warning ("%Jlabel `%D' declared but not defined", p, p);
688 /* Labels go in BLOCK_VARS. */
689 TREE_CHAIN (p) = BLOCK_VARS (block);
690 BLOCK_VARS (block) = p;
692 #ifdef ENABLE_CHECKING
693 if (I_LABEL_BINDING (b->id) != b) abort ();
694 #endif
695 I_LABEL_BINDING (b->id) = b->shadowed;
696 break;
698 case ENUMERAL_TYPE:
699 case UNION_TYPE:
700 case RECORD_TYPE:
701 TYPE_CONTEXT (p) = context;
703 /* Types may not have tag-names, in which case the type
704 appears in the bindings list with b->id NULL. */
705 if (b->id)
707 #ifdef ENABLE_CHECKING
708 if (I_TAG_BINDING (b->id) != b) abort ();
709 #endif
710 I_TAG_BINDING (b->id) = b->shadowed;
712 break;
714 case FUNCTION_DECL:
715 /* Propagate TREE_ADDRESSABLE from nested functions to their
716 containing functions. */
717 if (! TREE_ASM_WRITTEN (p)
718 && DECL_INITIAL (p) != 0
719 && TREE_ADDRESSABLE (p)
720 && DECL_ABSTRACT_ORIGIN (p) != 0
721 && DECL_ABSTRACT_ORIGIN (p) != p)
722 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
723 goto common_symbol;
725 case VAR_DECL:
726 /* Warnings for unused variables. Keep this in sync with
727 stmt.c:warn_about_unused_variables, which we cannot use
728 since it expects a different data structure. */
729 if (warn_unused_variable
730 && !TREE_USED (p)
731 && !DECL_IN_SYSTEM_HEADER (p)
732 && DECL_NAME (p)
733 && !DECL_ARTIFICIAL (p)
734 && (scope != file_scope
735 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
736 && !TREE_THIS_VOLATILE (p)))
737 && scope != external_scope)
738 warning ("%Junused variable `%D'", p, p);
740 /* Fall through. */
741 case TYPE_DECL:
742 case CONST_DECL:
743 common_symbol:
744 /* All of these go in BLOCK_VARS, but only if this is the
745 binding in the home scope. */
746 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
751 /* If this is the file scope, must set DECL_CONTEXT on these. */
752 if (!C_DECL_IN_EXTERNAL_SCOPE (p) && scope == file_scope)
753 DECL_CONTEXT (p) = context;
755 /* Fall through. */
756 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
757 already been put there by store_parm_decls. Unused-
758 parameter warnings are handled by function.c.
759 error_mark_node obviously does not go in BLOCK_VARS and
760 does not get unused-variable warnings. */
761 case PARM_DECL:
762 case ERROR_MARK:
763 /* It is possible for a decl not to have a name. We get
764 here with b->id NULL in this case. */
765 if (b->id)
767 #ifdef ENABLE_CHECKING
768 if (I_SYMBOL_BINDING (b->id) != b) abort ();
769 #endif
770 I_SYMBOL_BINDING (b->id) = b->shadowed;
772 break;
774 default:
775 abort ();
780 /* Dispose of the block that we just made inside some higher level. */
781 if ((scope->function_body || scope == file_scope) && context)
783 DECL_INITIAL (context) = block;
784 BLOCK_SUPERCONTEXT (block) = context;
786 else if (scope->outer)
788 if (block)
789 SCOPE_LIST_APPEND (scope->outer, blocks, block);
790 /* If we did not make a block for the scope just exited, any
791 blocks made for inner scopes must be carried forward so they
792 will later become subblocks of something else. */
793 else if (scope->blocks)
794 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
797 /* Pop the current scope, and free the structure for reuse. */
798 current_scope = scope->outer;
799 if (scope->function_body)
800 current_function_scope = scope->outer_function;
802 memset (scope, 0, sizeof (struct c_scope));
803 scope->outer = scope_freelist;
804 scope_freelist = scope;
806 return block;
809 void
810 push_file_scope (void)
812 tree decl;
814 push_scope ();
815 file_scope = current_scope;
817 start_fname_decls ();
819 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
820 bind (DECL_NAME (decl), decl, file_scope);
823 void
824 pop_file_scope (void)
826 /* In case there were missing closebraces, get us back to the global
827 binding level. */
828 while (current_scope != file_scope)
829 pop_scope ();
831 /* __FUNCTION__ is defined at file scope (""). This
832 call may not be necessary as my tests indicate it
833 still works without it. */
834 finish_fname_decls ();
836 /* Kludge: don't actually pop the file scope if generating a
837 precompiled header, so that macros and local symbols are still
838 visible to the PCH generator. */
839 if (pch_file)
840 return;
842 /* And pop off the file scope. */
843 pop_scope ();
844 file_scope = 0;
846 cpp_undef_all (parse_in);
849 /* Insert BLOCK at the end of the list of subblocks of the current
850 scope. This is used when a BIND_EXPR is expanded, to handle the
851 BLOCK node inside the BIND_EXPR. */
853 void
854 insert_block (tree block)
856 TREE_USED (block) = 1;
857 SCOPE_LIST_APPEND (current_scope, blocks, block);
860 /* Push a definition or a declaration of struct, union or enum tag "name".
861 "type" should be the type node.
862 We assume that the tag "name" is not already defined.
864 Note that the definition may really be just a forward reference.
865 In that case, the TYPE_SIZE will be zero. */
867 static void
868 pushtag (tree name, tree type)
870 /* Record the identifier as the type's name if it has none. */
871 if (name && !TYPE_NAME (type))
872 TYPE_NAME (type) = name;
873 bind (name, type, current_scope);
875 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
876 tagged type we just added to the current scope. This fake
877 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
878 to output a representation of a tagged type, and it also gives
879 us a convenient place to record the "scope start" address for the
880 tagged type. */
882 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
884 /* An approximation for now, so we can tell this is a function-scope tag.
885 This will be updated in pop_scope. */
886 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
889 /* Subroutine of compare_decls. Allow harmless mismatches in return
890 and argument types provided that the type modes match. This function
891 return a unified type given a suitable match, and 0 otherwise. */
893 static tree
894 match_builtin_function_types (tree newtype, tree oldtype)
896 tree newrettype, oldrettype;
897 tree newargs, oldargs;
898 tree trytype, tryargs;
900 /* Accept the return type of the new declaration if same modes. */
901 oldrettype = TREE_TYPE (oldtype);
902 newrettype = TREE_TYPE (newtype);
904 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
905 return 0;
907 oldargs = TYPE_ARG_TYPES (oldtype);
908 newargs = TYPE_ARG_TYPES (newtype);
909 tryargs = newargs;
911 while (oldargs || newargs)
913 if (! oldargs
914 || ! newargs
915 || ! TREE_VALUE (oldargs)
916 || ! TREE_VALUE (newargs)
917 || TYPE_MODE (TREE_VALUE (oldargs))
918 != TYPE_MODE (TREE_VALUE (newargs)))
919 return 0;
921 oldargs = TREE_CHAIN (oldargs);
922 newargs = TREE_CHAIN (newargs);
925 trytype = build_function_type (newrettype, tryargs);
926 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
929 /* Subroutine of diagnose_mismatched_decls. Check for function type
930 mismatch involving an empty arglist vs a nonempty one and give clearer
931 diagnostics. */
932 static void
933 diagnose_arglist_conflict (tree newdecl, tree olddecl,
934 tree newtype, tree oldtype)
936 tree t;
938 if (TREE_CODE (olddecl) != FUNCTION_DECL
939 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
940 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
942 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
943 return;
945 t = TYPE_ARG_TYPES (oldtype);
946 if (t == 0)
947 t = TYPE_ARG_TYPES (newtype);
948 for (; t; t = TREE_CHAIN (t))
950 tree type = TREE_VALUE (t);
952 if (TREE_CHAIN (t) == 0
953 && TYPE_MAIN_VARIANT (type) != void_type_node)
955 inform ("a parameter list with an ellipsis can't match "
956 "an empty parameter name list declaration");
957 break;
960 if (c_type_promotes_to (type) != type)
962 inform ("an argument type that has a default promotion can't match "
963 "an empty parameter name list declaration");
964 break;
969 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
970 old-style function definition, NEWDECL is a prototype declaration.
971 Diagnose inconsistencies in the argument list. Returns TRUE if
972 the prototype is compatible, FALSE if not. */
973 static bool
974 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
976 tree newargs, oldargs;
977 int i;
979 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
980 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
982 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
983 newargs = TYPE_ARG_TYPES (newtype);
984 i = 1;
986 for (;;)
988 tree oldargtype = TREE_VALUE (oldargs);
989 tree newargtype = TREE_VALUE (newargs);
991 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
992 break;
994 /* Reaching the end of just one list means the two decls don't
995 agree on the number of arguments. */
996 if (END_OF_ARGLIST (oldargtype))
998 error ("%Jprototype for '%D' declares more arguments "
999 "than previous old-style definition", newdecl, newdecl);
1000 return false;
1002 else if (END_OF_ARGLIST (newargtype))
1004 error ("%Jprototype for '%D' declares fewer arguments "
1005 "than previous old-style definition", newdecl, newdecl);
1006 return false;
1009 /* Type for passing arg must be consistent with that declared
1010 for the arg. */
1011 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
1013 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1014 newdecl, newdecl, i);
1015 return false;
1018 oldargs = TREE_CHAIN (oldargs);
1019 newargs = TREE_CHAIN (newargs);
1020 i++;
1023 /* If we get here, no errors were found, but do issue a warning
1024 for this poor-style construct. */
1025 warning ("%Jprototype for '%D' follows non-prototype definition",
1026 newdecl, newdecl);
1027 return true;
1028 #undef END_OF_ARGLIST
1031 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1032 first in a pair of mismatched declarations, using the diagnostic
1033 function DIAG. */
1034 static void
1035 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1037 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1039 else if (DECL_INITIAL (decl))
1040 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1041 else if (C_DECL_IMPLICIT (decl))
1042 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1043 else
1044 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1047 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1048 Returns true if the caller should proceed to merge the two, false
1049 if OLDDECL should simply be discarded. As a side effect, issues
1050 all necessary diagnostics for invalid or poor-style combinations.
1051 If it returns true, writes the types of NEWDECL and OLDDECL to
1052 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1053 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1055 static bool
1056 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1057 tree *newtypep, tree *oldtypep)
1059 tree newtype, oldtype;
1060 bool pedwarned = false;
1061 bool warned = false;
1063 /* If we have error_mark_node for either decl or type, just discard
1064 the previous decl - we're in an error cascade already. */
1065 if (olddecl == error_mark_node || newdecl == error_mark_node)
1066 return false;
1067 *oldtypep = oldtype = TREE_TYPE (olddecl);
1068 *newtypep = newtype = TREE_TYPE (newdecl);
1069 if (oldtype == error_mark_node || newtype == error_mark_node)
1070 return false;
1072 /* Two different categories of symbol altogether. This is an error
1073 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1074 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1076 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1077 && DECL_BUILT_IN (olddecl)
1078 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1080 error ("%J'%D' redeclared as different kind of symbol",
1081 newdecl, newdecl);
1082 locate_old_decl (olddecl, error);
1084 else if (TREE_PUBLIC (newdecl))
1085 warning ("%Jbuilt-in function '%D' declared as non-function",
1086 newdecl, newdecl);
1087 else if (warn_shadow)
1088 warning ("%Jdeclaration of '%D' shadows a built-in function",
1089 newdecl, newdecl);
1090 return false;
1093 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1095 if (TREE_CODE (olddecl) == FUNCTION_DECL
1096 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1098 /* Accept harmless mismatch in function types.
1099 This is for the ffs and fprintf builtins. */
1100 tree trytype = match_builtin_function_types (newtype, oldtype);
1102 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1103 *oldtypep = oldtype = trytype;
1104 else
1106 /* If types don't match for a built-in, throw away the
1107 built-in. No point in calling locate_old_decl here, it
1108 won't print anything. */
1109 warning ("%Jconflicting types for built-in function '%D'",
1110 newdecl, newdecl);
1111 return false;
1114 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1115 && DECL_SOURCE_LINE (olddecl) == 0)
1117 /* A conflicting function declaration for a predeclared
1118 function that isn't actually built in. Objective C uses
1119 these. The new declaration silently overrides everything
1120 but the volatility (i.e. noreturn) indication. See also
1121 below. FIXME: Make Objective C use normal builtins. */
1122 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1123 return false;
1125 /* Permit void foo (...) to match int foo (...) if the latter is
1126 the definition and implicit int was used. See
1127 c-torture/compile/920625-2.c. */
1128 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1129 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1130 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1131 && C_FUNCTION_IMPLICIT_INT (newdecl))
1133 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1134 /* Make sure we keep void as the return type. */
1135 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1136 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1137 pedwarned = true;
1139 else
1141 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1142 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1143 locate_old_decl (olddecl, error);
1144 return false;
1148 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1149 but silently ignore the redeclaration if either is in a system
1150 header. (Conflicting redeclarations were handled above.) */
1151 if (TREE_CODE (newdecl) == TYPE_DECL)
1153 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1154 return true; /* Allow OLDDECL to continue in use. */
1156 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1157 locate_old_decl (olddecl, error);
1158 return false;
1161 /* Function declarations can either be 'static' or 'extern' (no
1162 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1163 can never conflict with each other on account of linkage (6.2.2p4).
1164 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1165 two definitions if one is 'extern inline' and one is not. The non-
1166 extern-inline definition supersedes the extern-inline definition. */
1167 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1169 /* If you declare a built-in function name as static, or
1170 define the built-in with an old-style definition (so we
1171 can't validate the argument list) the built-in definition is
1172 overridden, but optionally warn this was a bad choice of name. */
1173 if (DECL_BUILT_IN (olddecl)
1174 && !C_DECL_DECLARED_BUILTIN (olddecl)
1175 && (!TREE_PUBLIC (newdecl)
1176 || (DECL_INITIAL (newdecl)
1177 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1179 if (warn_shadow)
1180 warning ("%Jdeclaration of '%D' shadows a built-in function",
1181 newdecl, newdecl);
1182 /* Discard the old built-in function. */
1183 return false;
1186 if (DECL_INITIAL (newdecl))
1188 if (DECL_INITIAL (olddecl)
1189 && !(DECL_DECLARED_INLINE_P (olddecl)
1190 && DECL_EXTERNAL (olddecl)
1191 && !(DECL_DECLARED_INLINE_P (newdecl)
1192 && DECL_EXTERNAL (newdecl))))
1194 error ("%Jredefinition of '%D'", newdecl, newdecl);
1195 locate_old_decl (olddecl, error);
1196 return false;
1199 /* If we have a prototype after an old-style function definition,
1200 the argument types must be checked specially. */
1201 else if (DECL_INITIAL (olddecl)
1202 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1203 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1204 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1206 locate_old_decl (olddecl, error);
1207 return false;
1209 /* Mismatched non-static and static is considered poor style.
1210 We only diagnose static then non-static if -Wtraditional,
1211 because it is the most convenient way to get some effects
1212 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1213 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1214 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1216 /* A static function declaration for a predeclared function
1217 that isn't actually built in, silently overrides the
1218 default. Objective C uses these. See also above.
1219 FIXME: Make Objective C use normal builtins. */
1220 if (TREE_CODE (olddecl) == FUNCTION_DECL
1221 && DECL_SOURCE_LINE (olddecl) == 0)
1222 return false;
1223 else
1225 warning ("%Jstatic declaration of '%D' follows "
1226 "non-static declaration", newdecl, newdecl);
1227 warned = true;
1230 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1231 && warn_traditional)
1233 warning ("%Jnon-static declaration of '%D' follows "
1234 "static declaration", newdecl, newdecl);
1235 warned = true;
1238 else if (TREE_CODE (newdecl) == VAR_DECL)
1240 /* Only variables can be thread-local, and all declarations must
1241 agree on this property. */
1242 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1244 if (DECL_THREAD_LOCAL (newdecl))
1245 error ("%Jthread-local declaration of '%D' follows "
1246 "non-thread-local declaration", newdecl, newdecl);
1247 else
1248 error ("%Jnon-thread-local declaration of '%D' follows "
1249 "thread-local declaration", newdecl, newdecl);
1251 locate_old_decl (olddecl, error);
1252 return false;
1255 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1256 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1258 error ("%Jredefinition of '%D'", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1260 return false;
1263 /* Objects declared at file scope: if at least one is 'extern',
1264 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1265 if (DECL_FILE_SCOPE_P (newdecl))
1267 if (!DECL_EXTERNAL (newdecl)
1268 && !DECL_EXTERNAL (olddecl)
1269 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1271 if (TREE_PUBLIC (newdecl))
1272 error ("%Jnon-static declaration of '%D' follows "
1273 "static declaration", newdecl, newdecl);
1274 else
1275 error ("%Jstatic declaration of '%D' follows "
1276 "non-static declaration", newdecl, newdecl);
1278 locate_old_decl (olddecl, error);
1279 return false;
1282 /* Two objects with the same name declared at the same block
1283 scope must both be external references (6.7p3). */
1284 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1285 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1287 if (DECL_EXTERNAL (newdecl))
1288 error ("%Jextern declaration of '%D' follows "
1289 "declaration with no linkage", newdecl, newdecl);
1290 else if (DECL_EXTERNAL (olddecl))
1291 error ("%Jdeclaration of '%D' with no linkage follows "
1292 "extern declaration", newdecl, newdecl);
1293 else
1294 error ("%Jredeclaration of '%D' with no linkage",
1295 newdecl, newdecl);
1297 locate_old_decl (olddecl, error);
1298 return false;
1302 /* warnings */
1303 /* All decls must agree on a non-default visibility. */
1304 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1305 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1306 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1308 warning ("%Jredeclaration of '%D' with different visibility "
1309 "(old visibility preserved)", newdecl, newdecl);
1310 warned = true;
1313 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1315 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1316 if (DECL_DECLARED_INLINE_P (newdecl)
1317 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1319 warning ("%Jinline declaration of '%D' follows "
1320 "declaration with attribute noinline", newdecl, newdecl);
1321 warned = true;
1323 else if (DECL_DECLARED_INLINE_P (olddecl)
1324 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1326 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1327 "inline declaration ", newdecl, newdecl);
1328 warned = true;
1331 /* Inline declaration after use or definition.
1332 ??? Should we still warn about this now we have unit-at-a-time
1333 mode and can get it right? */
1334 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1336 if (TREE_USED (olddecl))
1338 warning ("%J'%D' declared inline after being called",
1339 olddecl, olddecl);
1340 warned = true;
1342 else if (DECL_INITIAL (olddecl))
1344 warning ("%J'%D' declared inline after its definition",
1345 olddecl, olddecl);
1346 warned = true;
1350 else /* PARM_DECL, VAR_DECL */
1352 /* Redeclaration of a parameter is a constraint violation (this is
1353 not explicitly stated, but follows from C99 6.7p3 [no more than
1354 one declaration of the same identifier with no linkage in the
1355 same scope, except type tags] and 6.2.2p6 [parameters have no
1356 linkage]). We must check for a forward parameter declaration,
1357 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1358 an extension, the mandatory diagnostic for which is handled by
1359 mark_forward_parm_decls. */
1361 if (TREE_CODE (newdecl) == PARM_DECL
1362 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1364 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1365 locate_old_decl (olddecl, error);
1366 return false;
1369 /* These bits are only type qualifiers when applied to objects. */
1370 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1372 if (TREE_THIS_VOLATILE (newdecl))
1373 pedwarn ("%Jvolatile declaration of '%D' follows "
1374 "non-volatile declaration", newdecl, newdecl);
1375 else
1376 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1377 "volatile declaration", newdecl, newdecl);
1378 pedwarned = true;
1380 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1382 if (TREE_READONLY (newdecl))
1383 pedwarn ("%Jconst declaration of '%D' follows "
1384 "non-const declaration", newdecl, newdecl);
1385 else
1386 pedwarn ("%Jnon-const declaration of '%D' follows "
1387 "const declaration", newdecl, newdecl);
1388 pedwarned = true;
1392 /* Optional warning for completely redundant decls. */
1393 if (!warned && !pedwarned
1394 && warn_redundant_decls
1395 /* Don't warn about a function declaration followed by a
1396 definition. */
1397 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1398 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1399 /* Don't warn about an extern followed by a definition. */
1400 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1401 /* Don't warn about forward parameter decls. */
1402 && !(TREE_CODE (newdecl) == PARM_DECL
1403 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1405 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1406 warned = true;
1409 /* Report location of previous decl/defn in a consistent manner. */
1410 if (warned || pedwarned)
1411 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1413 return true;
1416 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1417 consistent with OLDDECL, but carries new information. Merge the
1418 new information into OLDDECL. This function issues no
1419 diagnostics. */
1421 static void
1422 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1424 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1425 && DECL_INITIAL (newdecl) != 0);
1427 /* For real parm decl following a forward decl, rechain the old decl
1428 in its new location and clear TREE_ASM_WRITTEN (it's not a
1429 forward decl anymore). */
1430 if (TREE_CODE (newdecl) == PARM_DECL
1431 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1433 struct c_binding *b, **here;
1435 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1436 if ((*here)->decl == olddecl)
1437 goto found;
1438 abort ();
1440 found:
1441 b = *here;
1442 *here = b->prev;
1443 b->prev = current_scope->bindings;
1444 current_scope->bindings = b;
1446 TREE_ASM_WRITTEN (olddecl) = 0;
1449 DECL_ATTRIBUTES (newdecl)
1450 = targetm.merge_decl_attributes (olddecl, newdecl);
1452 /* Merge the data types specified in the two decls. */
1453 TREE_TYPE (newdecl)
1454 = TREE_TYPE (olddecl)
1455 = common_type (newtype, oldtype);
1457 /* Lay the type out, unless already done. */
1458 if (oldtype != TREE_TYPE (newdecl))
1460 if (TREE_TYPE (newdecl) != error_mark_node)
1461 layout_type (TREE_TYPE (newdecl));
1462 if (TREE_CODE (newdecl) != FUNCTION_DECL
1463 && TREE_CODE (newdecl) != TYPE_DECL
1464 && TREE_CODE (newdecl) != CONST_DECL)
1465 layout_decl (newdecl, 0);
1467 else
1469 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1470 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1471 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1472 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1473 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1474 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1476 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1477 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1481 /* Keep the old rtl since we can safely use it. */
1482 COPY_DECL_RTL (olddecl, newdecl);
1484 /* Merge the type qualifiers. */
1485 if (TREE_READONLY (newdecl))
1486 TREE_READONLY (olddecl) = 1;
1488 if (TREE_THIS_VOLATILE (newdecl))
1490 TREE_THIS_VOLATILE (olddecl) = 1;
1491 if (TREE_CODE (newdecl) == VAR_DECL)
1492 make_var_volatile (newdecl);
1495 /* Keep source location of definition rather than declaration. */
1496 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1497 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1499 /* Merge the unused-warning information. */
1500 if (DECL_IN_SYSTEM_HEADER (olddecl))
1501 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1502 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1503 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1505 /* Merge the initialization information. */
1506 if (DECL_INITIAL (newdecl) == 0)
1507 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1509 /* Merge the section attribute.
1510 We want to issue an error if the sections conflict but that must be
1511 done later in decl_attributes since we are called before attributes
1512 are assigned. */
1513 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1514 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1516 /* Copy the assembler name.
1517 Currently, it can only be defined in the prototype. */
1518 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1520 /* If either declaration has a nondefault visibility, use it. */
1521 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1522 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1524 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1526 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1527 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1528 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1529 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1530 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1531 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1532 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1533 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1534 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1537 /* Merge the storage class information. */
1538 merge_weak (newdecl, olddecl);
1540 /* For functions, static overrides non-static. */
1541 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1543 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1544 /* This is since we don't automatically
1545 copy the attributes of NEWDECL into OLDDECL. */
1546 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1547 /* If this clears `static', clear it in the identifier too. */
1548 if (! TREE_PUBLIC (olddecl))
1549 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1551 if (DECL_EXTERNAL (newdecl))
1553 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1554 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1556 /* An extern decl does not override previous storage class. */
1557 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1558 if (! DECL_EXTERNAL (newdecl))
1560 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1561 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1564 else
1566 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1567 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1570 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1572 /* If we're redefining a function previously defined as extern
1573 inline, make sure we emit debug info for the inline before we
1574 throw it away, in case it was inlined into a function that hasn't
1575 been written out yet. */
1576 if (new_is_definition && DECL_INITIAL (olddecl))
1578 if (TREE_USED (olddecl)
1579 /* In unit-at-a-time mode we never inline re-defined extern
1580 inline functions. */
1581 && !flag_unit_at_a_time
1582 && cgraph_function_possibly_inlined_p (olddecl))
1583 (*debug_hooks->outlining_inline_function) (olddecl);
1585 /* The new defn must not be inline. */
1586 DECL_INLINE (newdecl) = 0;
1587 DECL_UNINLINABLE (newdecl) = 1;
1589 else
1591 /* If either decl says `inline', this fn is inline,
1592 unless its definition was passed already. */
1593 if (DECL_DECLARED_INLINE_P (newdecl)
1594 || DECL_DECLARED_INLINE_P (olddecl))
1595 DECL_DECLARED_INLINE_P (newdecl) = 1;
1597 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1598 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1601 if (DECL_BUILT_IN (olddecl))
1603 /* If redeclaring a builtin function, it stays built in.
1604 But it gets tagged as having been declared. */
1605 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1606 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1607 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1610 /* Also preserve various other info from the definition. */
1611 if (! new_is_definition)
1613 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1614 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1615 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1616 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1617 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1619 /* Set DECL_INLINE on the declaration if we've got a body
1620 from which to instantiate. */
1621 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1623 DECL_INLINE (newdecl) = 1;
1624 DECL_ABSTRACT_ORIGIN (newdecl)
1625 = DECL_ABSTRACT_ORIGIN (olddecl);
1628 else
1630 /* If a previous declaration said inline, mark the
1631 definition as inlinable. */
1632 if (DECL_DECLARED_INLINE_P (newdecl)
1633 && ! DECL_UNINLINABLE (newdecl))
1634 DECL_INLINE (newdecl) = 1;
1638 /* This bit must not get wiped out. */
1639 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1641 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1642 But preserve OLDDECL's DECL_UID. */
1644 unsigned olddecl_uid = DECL_UID (olddecl);
1646 memcpy ((char *) olddecl + sizeof (struct tree_common),
1647 (char *) newdecl + sizeof (struct tree_common),
1648 sizeof (struct tree_decl) - sizeof (struct tree_common));
1649 DECL_UID (olddecl) = olddecl_uid;
1652 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1653 so that encode_section_info has a chance to look at the new decl
1654 flags and attributes. */
1655 if (DECL_RTL_SET_P (olddecl)
1656 && (TREE_CODE (olddecl) == FUNCTION_DECL
1657 || (TREE_CODE (olddecl) == VAR_DECL
1658 && TREE_STATIC (olddecl))))
1659 make_decl_rtl (olddecl, NULL);
1662 /* Handle when a new declaration NEWDECL has the same name as an old
1663 one OLDDECL in the same binding contour. Prints an error message
1664 if appropriate.
1666 If safely possible, alter OLDDECL to look like NEWDECL, and return
1667 true. Otherwise, return false. */
1669 static bool
1670 duplicate_decls (tree newdecl, tree olddecl)
1672 tree newtype = NULL, oldtype = NULL;
1674 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1675 return false;
1677 merge_decls (newdecl, olddecl, newtype, oldtype);
1678 return true;
1682 /* Check whether decl-node NEW shadows an existing declaration. */
1683 static void
1684 warn_if_shadowing (tree new)
1686 struct c_binding *b;
1688 /* Shadow warnings wanted? */
1689 if (!warn_shadow
1690 /* No shadow warnings for internally generated vars. */
1691 || DECL_SOURCE_LINE (new) == 0
1692 /* No shadow warnings for vars made for inlining. */
1693 || DECL_FROM_INLINE (new)
1694 /* Don't warn about the parm names in function declarator
1695 within a function declarator. It would be nice to avoid
1696 warning in any function declarator in a declaration, as
1697 opposed to a definition, but there is no way to tell
1698 it's not a definition at this point. */
1699 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1700 return;
1702 /* Is anything being shadowed? Do not be confused by a second binding
1703 to the same decl in the externals scope. */
1704 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1705 if (b->decl && b->decl != new && b->contour != external_scope)
1707 tree old = b->decl;
1709 if (TREE_CODE (old) == PARM_DECL)
1710 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1711 else if (DECL_FILE_SCOPE_P (old))
1712 warning ("%Jdeclaration of '%D' shadows a global declaration",
1713 new, new);
1714 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1715 warning ("%Jdeclaration of '%D' shadows a built-in function",
1716 new, new);
1717 else
1718 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1720 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1721 warning ("%Jshadowed declaration is here", old);
1723 break;
1728 /* Subroutine of pushdecl.
1730 X is a TYPE_DECL for a typedef statement. Create a brand new
1731 ..._TYPE node (which will be just a variant of the existing
1732 ..._TYPE node with identical properties) and then install X
1733 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1735 The whole point here is to end up with a situation where each
1736 and every ..._TYPE node the compiler creates will be uniquely
1737 associated with AT MOST one node representing a typedef name.
1738 This way, even though the compiler substitutes corresponding
1739 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1740 early on, later parts of the compiler can always do the reverse
1741 translation and get back the corresponding typedef name. For
1742 example, given:
1744 typedef struct S MY_TYPE;
1745 MY_TYPE object;
1747 Later parts of the compiler might only know that `object' was of
1748 type `struct S' if it were not for code just below. With this
1749 code however, later parts of the compiler see something like:
1751 struct S' == struct S
1752 typedef struct S' MY_TYPE;
1753 struct S' object;
1755 And they can then deduce (from the node for type struct S') that
1756 the original object declaration was:
1758 MY_TYPE object;
1760 Being able to do this is important for proper support of protoize,
1761 and also for generating precise symbolic debugging information
1762 which takes full account of the programmer's (typedef) vocabulary.
1764 Obviously, we don't want to generate a duplicate ..._TYPE node if
1765 the TYPE_DECL node that we are now processing really represents a
1766 standard built-in type.
1768 Since all standard types are effectively declared at line zero
1769 in the source file, we can easily check to see if we are working
1770 on a standard type by checking the current value of lineno. */
1772 static void
1773 clone_underlying_type (tree x)
1775 if (DECL_SOURCE_LINE (x) == 0)
1777 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1778 TYPE_NAME (TREE_TYPE (x)) = x;
1780 else if (TREE_TYPE (x) != error_mark_node
1781 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1783 tree tt = TREE_TYPE (x);
1784 DECL_ORIGINAL_TYPE (x) = tt;
1785 tt = build_type_copy (tt);
1786 TYPE_NAME (tt) = x;
1787 TREE_USED (tt) = TREE_USED (x);
1788 TREE_TYPE (x) = tt;
1792 /* Record a decl-node X as belonging to the current lexical scope.
1793 Check for errors (such as an incompatible declaration for the same
1794 name already seen in the same scope).
1796 Returns either X or an old decl for the same name.
1797 If an old decl is returned, it may have been smashed
1798 to agree with what X says. */
1800 tree
1801 pushdecl (tree x)
1803 tree name = DECL_NAME (x);
1804 struct c_scope *scope = current_scope;
1805 struct c_binding *b;
1807 /* Functions need the lang_decl data. */
1808 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1809 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1811 /* Must set DECL_CONTEXT for everything not at file scope or
1812 DECL_FILE_SCOPE_P won't work. Local externs don't count
1813 unless they have initializers (which generate code). */
1814 if (current_function_decl
1815 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1816 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1817 DECL_CONTEXT (x) = current_function_decl;
1819 /* Anonymous decls are just inserted in the scope. */
1820 if (!name)
1822 bind (name, x, scope);
1823 return x;
1826 /* First, see if there is another declaration with the same name in
1827 the current scope. If there is, duplicate_decls may do all the
1828 work for us. If duplicate_decls returns false, that indicates
1829 two incompatible decls in the same scope; we are to silently
1830 replace the old one (duplicate_decls has issued all appropriate
1831 diagnostics). In particular, we should not consider possible
1832 duplicates in the external scope, or shadowing. */
1833 b = I_SYMBOL_BINDING (name);
1834 if (b && b->contour == scope)
1836 if (duplicate_decls (x, b->decl))
1837 return b->decl;
1838 else
1839 goto skip_external_and_shadow_checks;
1842 /* All declarations with external linkage, and all external
1843 references, go in the external scope, no matter what scope is
1844 current. However, the binding in that scope is ignored for
1845 purposes of normal name lookup. A separate binding structure is
1846 created in the requested scope; this governs the normal
1847 visibility of the symbol.
1849 The binding in the externals scope is used exclusively for
1850 detecting duplicate declarations of the same object, no matter
1851 what scope they are in; this is what we do here. (C99 6.2.7p2:
1852 All declarations that refer to the same object or function shall
1853 have compatible type; otherwise, the behavior is undefined.) */
1854 if (DECL_EXTERNAL (x) || scope == file_scope)
1856 if (warn_nested_externs
1857 && scope != file_scope
1858 && !DECL_IN_SYSTEM_HEADER (x))
1859 warning ("nested extern declaration of `%s'",
1860 IDENTIFIER_POINTER (name));
1862 while (b && b->contour != external_scope)
1863 b = b->shadowed;
1865 /* The point of the same_translation_unit_p check here is,
1866 we want to detect a duplicate decl for a construct like
1867 foo() { extern bar(); } ... static bar(); but not if
1868 they are in different translation units. In any case,
1869 the static does not go in the externals scope. */
1870 if (b
1871 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1872 || same_translation_unit_p (x, b->decl))
1873 && duplicate_decls (x, b->decl))
1875 bind (name, b->decl, scope);
1876 return b->decl;
1878 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1880 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1881 bind (name, x, external_scope);
1885 warn_if_shadowing (x);
1887 skip_external_and_shadow_checks:
1888 if (TREE_CODE (x) == TYPE_DECL)
1889 clone_underlying_type (x);
1891 bind (name, x, scope);
1893 /* If x's type is incomplete because it's based on a
1894 structure or union which has not yet been fully declared,
1895 attach it to that structure or union type, so we can go
1896 back and complete the variable declaration later, if the
1897 structure or union gets fully declared.
1899 If the input is erroneous, we can have error_mark in the type
1900 slot (e.g. "f(void a, ...)") - that doesn't count as an
1901 incomplete type. */
1902 if (TREE_TYPE (x) != error_mark_node
1903 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1905 tree element = TREE_TYPE (x);
1907 while (TREE_CODE (element) == ARRAY_TYPE)
1908 element = TREE_TYPE (element);
1909 element = TYPE_MAIN_VARIANT (element);
1911 if ((TREE_CODE (element) == RECORD_TYPE
1912 || TREE_CODE (element) == UNION_TYPE)
1913 && (TREE_CODE (x) != TYPE_DECL
1914 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1915 && !COMPLETE_TYPE_P (element))
1916 C_TYPE_INCOMPLETE_VARS (element)
1917 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1919 return x;
1922 /* Record X as belonging to file scope.
1923 This is used only internally by the Objective-C front end,
1924 and is limited to its needs. duplicate_decls is not called;
1925 if there is any preexisting decl for this identifier, it is an ICE. */
1927 tree
1928 pushdecl_top_level (tree x)
1930 tree name;
1932 if (TREE_CODE (x) != VAR_DECL)
1933 abort ();
1935 name = DECL_NAME (x);
1937 if (I_SYMBOL_BINDING (name))
1938 abort ();
1940 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1942 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1943 bind (name, x, external_scope);
1945 if (file_scope)
1946 bind (name, x, file_scope);
1948 return x;
1951 static void
1952 implicit_decl_warning (tree id, tree olddecl)
1954 void (*diag) (const char *, ...);
1955 switch (mesg_implicit_function_declaration)
1957 case 0: return;
1958 case 1: diag = warning; break;
1959 case 2: diag = error; break;
1960 default: abort ();
1963 diag (N_("implicit declaration of function '%E'"), id);
1964 if (olddecl)
1965 locate_old_decl (olddecl, diag);
1968 /* Generate an implicit declaration for identifier FUNCTIONID as a
1969 function of type int (). */
1971 tree
1972 implicitly_declare (tree functionid)
1974 tree decl = lookup_name_in_scope (functionid, external_scope);
1976 if (decl)
1978 /* FIXME: Objective-C has weird not-really-builtin functions
1979 which are supposed to be visible automatically. They wind up
1980 in the external scope because they're pushed before the file
1981 scope gets created. Catch this here and rebind them into the
1982 file scope. */
1983 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1985 bind (functionid, decl, file_scope);
1986 return decl;
1988 else
1990 /* Implicit declaration of a function already declared
1991 (somehow) in a different scope, or as a built-in.
1992 If this is the first time this has happened, warn;
1993 then recycle the old declaration. */
1994 if (!C_DECL_IMPLICIT (decl))
1996 implicit_decl_warning (functionid, decl);
1997 C_DECL_IMPLICIT (decl) = 1;
1999 bind (functionid, decl, current_scope);
2000 return decl;
2004 /* Not seen before. */
2005 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2006 DECL_EXTERNAL (decl) = 1;
2007 TREE_PUBLIC (decl) = 1;
2008 C_DECL_IMPLICIT (decl) = 1;
2009 implicit_decl_warning (functionid, 0);
2011 /* C89 says implicit declarations are in the innermost block.
2012 So we record the decl in the standard fashion. */
2013 decl = pushdecl (decl);
2015 /* No need to call objc_check_decl here - it's a function type. */
2016 rest_of_decl_compilation (decl, NULL, 0, 0);
2018 /* Write a record describing this implicit function declaration
2019 to the prototypes file (if requested). */
2020 gen_aux_info_record (decl, 0, 1, 0);
2022 /* Possibly apply some default attributes to this implicit declaration. */
2023 decl_attributes (&decl, NULL_TREE, 0);
2025 return decl;
2028 /* Issue an error message for a reference to an undeclared variable
2029 ID, including a reference to a builtin outside of function-call
2030 context. Establish a binding of the identifier to error_mark_node
2031 in an appropriate scope, which will suppress further errors for the
2032 same identifier. */
2033 void
2034 undeclared_variable (tree id)
2036 static bool already = false;
2037 struct c_scope *scope;
2039 if (current_function_decl == 0)
2041 error ("'%E' undeclared here (not in a function)", id);
2042 scope = current_scope;
2044 else
2046 error ("'%E' undeclared (first use in this function)", id);
2048 if (! already)
2050 error ("(Each undeclared identifier is reported only once");
2051 error ("for each function it appears in.)");
2052 already = true;
2055 /* If we are parsing old-style parameter decls, current_function_decl
2056 will be nonnull but current_function_scope will be null. */
2057 scope = current_function_scope ? current_function_scope : current_scope;
2059 bind (id, error_mark_node, scope);
2062 /* Subroutine of lookup_label, declare_label, define_label: construct a
2063 LABEL_DECL with all the proper frills. */
2065 static tree
2066 make_label (tree name, location_t location)
2068 tree label = build_decl (LABEL_DECL, name, void_type_node);
2070 DECL_CONTEXT (label) = current_function_decl;
2071 DECL_MODE (label) = VOIDmode;
2072 DECL_SOURCE_LOCATION (label) = location;
2074 return label;
2077 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2078 Create one if none exists so far for the current function.
2079 This is called when a label is used in a goto expression or
2080 has its address taken. */
2082 tree
2083 lookup_label (tree name)
2085 tree label;
2087 if (current_function_decl == 0)
2089 error ("label %s referenced outside of any function",
2090 IDENTIFIER_POINTER (name));
2091 return 0;
2094 /* Use a label already defined or ref'd with this name, but not if
2095 it is inherited from a containing function and wasn't declared
2096 using __label__. */
2097 label = I_LABEL_DECL (name);
2098 if (label && (DECL_CONTEXT (label) == current_function_decl
2099 || C_DECLARED_LABEL_FLAG (label)))
2101 /* If the label has only been declared, update its apparent
2102 location to point here, for better diagnostics if it
2103 turns out not to have been defined. */
2104 if (!TREE_USED (label))
2105 DECL_SOURCE_LOCATION (label) = input_location;
2106 return label;
2109 /* No label binding for that identifier; make one. */
2110 label = make_label (name, input_location);
2112 /* Ordinary labels go in the current function scope. */
2113 bind (name, label, current_function_scope);
2114 return label;
2117 /* Make a label named NAME in the current function, shadowing silently
2118 any that may be inherited from containing functions or containing
2119 scopes. This is called for __label__ declarations. */
2121 tree
2122 declare_label (tree name)
2124 struct c_binding *b = I_LABEL_BINDING (name);
2125 tree label;
2127 /* Check to make sure that the label hasn't already been declared
2128 at this scope */
2129 if (b && b->contour == current_scope)
2131 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2132 locate_old_decl (b->decl, error);
2134 /* Just use the previous declaration. */
2135 return b->decl;
2138 label = make_label (name, input_location);
2139 C_DECLARED_LABEL_FLAG (label) = 1;
2141 /* Declared labels go in the current scope. */
2142 bind (name, label, current_scope);
2143 return label;
2146 /* Define a label, specifying the location in the source file.
2147 Return the LABEL_DECL node for the label, if the definition is valid.
2148 Otherwise return 0. */
2150 tree
2151 define_label (location_t location, tree name)
2153 /* Find any preexisting label with this name. It is an error
2154 if that label has already been defined in this function, or
2155 if there is a containing function with a declared label with
2156 the same name. */
2157 tree label = I_LABEL_DECL (name);
2159 if (label
2160 && ((DECL_CONTEXT (label) == current_function_decl
2161 && DECL_INITIAL (label) != 0)
2162 || (DECL_CONTEXT (label) != current_function_decl
2163 && C_DECLARED_LABEL_FLAG (label))))
2165 error ("%Hduplicate label `%D'", &location, label);
2166 locate_old_decl (label, error);
2167 return 0;
2169 else if (label && DECL_CONTEXT (label) == current_function_decl)
2171 /* The label has been used or declared already in this function,
2172 but not defined. Update its location to point to this
2173 definition. */
2174 DECL_SOURCE_LOCATION (label) = location;
2176 else
2178 /* No label binding for that identifier; make one. */
2179 label = make_label (name, location);
2181 /* Ordinary labels go in the current function scope. */
2182 bind (name, label, current_function_scope);
2185 if (warn_traditional && !in_system_header && lookup_name (name))
2186 warning ("%Htraditional C lacks a separate namespace for labels, "
2187 "identifier `%s' conflicts", &location,
2188 IDENTIFIER_POINTER (name));
2190 /* Mark label as having been defined. */
2191 DECL_INITIAL (label) = error_mark_node;
2192 return label;
2195 /* Given NAME, an IDENTIFIER_NODE,
2196 return the structure (or union or enum) definition for that name.
2197 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2198 CODE says which kind of type the caller wants;
2199 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2200 If the wrong kind of type is found, an error is reported. */
2202 static tree
2203 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2205 struct c_binding *b = I_TAG_BINDING (name);
2206 int thislevel = 0;
2208 if (!b || !b->decl)
2209 return 0;
2211 /* We only care about whether it's in this level if
2212 thislevel_only was set or it might be a type clash. */
2213 if (thislevel_only || TREE_CODE (b->decl) != code)
2215 /* For our purposes, a tag in the external scope is the same as
2216 a tag in the file scope. (Primarily relevant to Objective-C
2217 and its builtin structure tags, which get pushed before the
2218 file scope is created.) */
2219 if (b->contour == current_scope
2220 || (current_scope == file_scope && b->contour == external_scope))
2221 thislevel = 1;
2224 if (thislevel_only && !thislevel)
2225 return 0;
2227 if (TREE_CODE (b->decl) != code)
2229 /* Definition isn't the kind we were looking for. */
2230 pending_invalid_xref = name;
2231 pending_invalid_xref_location = input_location;
2233 /* If in the same binding level as a declaration as a tag
2234 of a different type, this must not be allowed to
2235 shadow that tag, so give the error immediately.
2236 (For example, "struct foo; union foo;" is invalid.) */
2237 if (thislevel)
2238 pending_xref_error ();
2240 return b->decl;
2243 /* Print an error message now
2244 for a recent invalid struct, union or enum cross reference.
2245 We don't print them immediately because they are not invalid
2246 when used in the `struct foo;' construct for shadowing. */
2248 void
2249 pending_xref_error (void)
2251 if (pending_invalid_xref != 0)
2252 error ("%H`%s' defined as wrong kind of tag",
2253 &pending_invalid_xref_location,
2254 IDENTIFIER_POINTER (pending_invalid_xref));
2255 pending_invalid_xref = 0;
2259 /* Look up NAME in the current scope and its superiors
2260 in the namespace of variables, functions and typedefs.
2261 Return a ..._DECL node of some kind representing its definition,
2262 or return 0 if it is undefined. */
2264 tree
2265 lookup_name (tree name)
2267 struct c_binding *b = I_SYMBOL_BINDING (name);
2268 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2269 return b->decl;
2270 return 0;
2273 /* Similar to `lookup_name' but look only at the indicated scope. */
2275 static tree
2276 lookup_name_in_scope (tree name, struct c_scope *scope)
2278 struct c_binding *b;
2280 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2281 if (b->contour == scope)
2282 return b->decl;
2283 return 0;
2286 /* Create the predefined scalar types of C,
2287 and some nodes representing standard constants (0, 1, (void *) 0).
2288 Initialize the global scope.
2289 Make definitions for built-in primitive functions. */
2291 void
2292 c_init_decl_processing (void)
2294 tree endlink;
2295 tree ptr_ftype_void, ptr_ftype_ptr;
2296 location_t save_loc = input_location;
2298 /* Adds some ggc roots, and reserved words for c-parse.in. */
2299 c_parse_init ();
2301 current_function_decl = 0;
2303 /* Make the externals scope. */
2304 push_scope ();
2305 external_scope = current_scope;
2307 /* Declarations from c_common_nodes_and_builtins must not be associated
2308 with this input file, lest we get differences between using and not
2309 using preprocessed headers. */
2310 input_location.file = "<internal>";
2311 input_location.line = 0;
2313 build_common_tree_nodes (flag_signed_char);
2315 c_common_nodes_and_builtins ();
2317 /* In C, comparisons and TRUTH_* expressions have type int. */
2318 truthvalue_type_node = integer_type_node;
2319 truthvalue_true_node = integer_one_node;
2320 truthvalue_false_node = integer_zero_node;
2322 /* Even in C99, which has a real boolean type. */
2323 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2324 boolean_type_node));
2326 endlink = void_list_node;
2327 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2328 ptr_ftype_ptr
2329 = build_function_type (ptr_type_node,
2330 tree_cons (NULL_TREE, ptr_type_node, endlink));
2332 input_location = save_loc;
2334 pedantic_lvalues = true;
2336 make_fname_decl = c_make_fname_decl;
2337 start_fname_decls ();
2340 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2341 decl, NAME is the initialization string and TYPE_DEP indicates whether
2342 NAME depended on the type of the function. As we don't yet implement
2343 delayed emission of static data, we mark the decl as emitted
2344 so it is not placed in the output. Anything using it must therefore pull
2345 out the STRING_CST initializer directly. FIXME. */
2347 static tree
2348 c_make_fname_decl (tree id, int type_dep)
2350 const char *name = fname_as_string (type_dep);
2351 tree decl, type, init;
2352 size_t length = strlen (name);
2354 type = build_array_type
2355 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2356 build_index_type (size_int (length)));
2358 decl = build_decl (VAR_DECL, id, type);
2360 TREE_STATIC (decl) = 1;
2361 TREE_READONLY (decl) = 1;
2362 DECL_ARTIFICIAL (decl) = 1;
2364 init = build_string (length + 1, name);
2365 TREE_TYPE (init) = type;
2366 DECL_INITIAL (decl) = init;
2368 TREE_USED (decl) = 1;
2370 if (current_function_decl)
2372 DECL_CONTEXT (decl) = current_function_decl;
2373 bind (id, decl, current_function_scope);
2376 finish_decl (decl, init, NULL_TREE);
2378 return decl;
2381 /* Return a definition for a builtin function named NAME and whose data type
2382 is TYPE. TYPE should be a function type with argument types.
2383 FUNCTION_CODE tells later passes how to compile calls to this function.
2384 See tree.h for its possible values.
2386 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2387 the name to be called if we can't opencode the function. If
2388 ATTRS is nonzero, use that for the function's attribute list. */
2390 tree
2391 builtin_function (const char *name, tree type, int function_code,
2392 enum built_in_class class, const char *library_name,
2393 tree attrs)
2395 tree id = get_identifier (name);
2396 tree decl = build_decl (FUNCTION_DECL, id, type);
2397 TREE_PUBLIC (decl) = 1;
2398 DECL_EXTERNAL (decl) = 1;
2399 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2400 DECL_BUILT_IN_CLASS (decl) = class;
2401 DECL_FUNCTION_CODE (decl) = function_code;
2402 if (library_name)
2403 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2404 make_decl_rtl (decl, NULL);
2406 /* Should never be called on a symbol with a preexisting meaning. */
2407 if (I_SYMBOL_BINDING (id))
2408 abort ();
2410 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2411 bind (id, decl, external_scope);
2413 /* Builtins in the implementation namespace are made visible without
2414 needing to be explicitly declared. See push_file_scope. */
2415 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2417 TREE_CHAIN (decl) = visible_builtins;
2418 visible_builtins = decl;
2421 /* Possibly apply some default attributes to this built-in function. */
2422 if (attrs)
2423 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2424 else
2425 decl_attributes (&decl, NULL_TREE, 0);
2427 return decl;
2430 /* Called when a declaration is seen that contains no names to declare.
2431 If its type is a reference to a structure, union or enum inherited
2432 from a containing scope, shadow that tag name for the current scope
2433 with a forward reference.
2434 If its type defines a new named structure or union
2435 or defines an enum, it is valid but we need not do anything here.
2436 Otherwise, it is an error. */
2438 void
2439 shadow_tag (tree declspecs)
2441 shadow_tag_warned (declspecs, 0);
2444 void
2445 shadow_tag_warned (tree declspecs, int warned)
2448 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2449 no pedwarn. */
2451 int found_tag = 0;
2452 tree link;
2453 tree specs, attrs;
2455 pending_invalid_xref = 0;
2457 /* Remove the attributes from declspecs, since they will confuse the
2458 following code. */
2459 split_specs_attrs (declspecs, &specs, &attrs);
2461 for (link = specs; link; link = TREE_CHAIN (link))
2463 tree value = TREE_VALUE (link);
2464 enum tree_code code = TREE_CODE (value);
2466 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2467 /* Used to test also that TYPE_SIZE (value) != 0.
2468 That caused warning for `struct foo;' at top level in the file. */
2470 tree name = TYPE_NAME (value);
2471 tree t;
2473 found_tag++;
2475 if (name == 0)
2477 if (warned != 1 && code != ENUMERAL_TYPE)
2478 /* Empty unnamed enum OK */
2480 pedwarn ("unnamed struct/union that defines no instances");
2481 warned = 1;
2484 else
2486 t = lookup_tag (code, name, 1);
2488 if (t == 0)
2490 t = make_node (code);
2491 pushtag (name, t);
2495 else
2497 if (!warned && ! in_system_header)
2499 warning ("useless keyword or type name in empty declaration");
2500 warned = 2;
2505 if (found_tag > 1)
2506 error ("two types specified in one empty declaration");
2508 if (warned != 1)
2510 if (found_tag == 0)
2511 pedwarn ("empty declaration");
2515 /* Construct an array declarator. EXPR is the expression inside [], or
2516 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2517 to the pointer to which a parameter array is converted). STATIC_P is
2518 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2519 is nonzero is the array is [*], a VLA of unspecified length which is
2520 nevertheless a complete type (not currently implemented by GCC),
2521 zero otherwise. The declarator is constructed as an ARRAY_REF
2522 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2523 left of the [] (filled by in set_array_declarator_type) and operand 1
2524 is the expression inside; whose TREE_TYPE is the type qualifiers and
2525 which has TREE_STATIC set if "static" is used. */
2527 tree
2528 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2530 tree decl;
2531 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2532 TREE_TYPE (decl) = quals;
2533 TREE_STATIC (decl) = (static_p ? 1 : 0);
2534 if (pedantic && !flag_isoc99)
2536 if (static_p || quals != NULL_TREE)
2537 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2538 if (vla_unspec_p)
2539 pedwarn ("ISO C90 does not support `[*]' array declarators");
2541 if (vla_unspec_p)
2542 warning ("GCC does not yet properly implement `[*]' array declarators");
2543 return decl;
2546 /* Set the type of an array declarator. DECL is the declarator, as
2547 constructed by build_array_declarator; TYPE is what appears on the left
2548 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2549 abstract declarator, zero otherwise; this is used to reject static and
2550 type qualifiers in abstract declarators, where they are not in the
2551 C99 grammar. */
2553 tree
2554 set_array_declarator_type (tree decl, tree type, int abstract_p)
2556 TREE_OPERAND (decl, 0) = type;
2557 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2558 error ("static or type qualifiers in abstract declarator");
2559 return decl;
2562 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2564 tree
2565 groktypename (tree typename)
2567 tree specs, attrs;
2569 if (TREE_CODE (typename) != TREE_LIST)
2570 return typename;
2572 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2574 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2575 NULL);
2577 /* Apply attributes. */
2578 decl_attributes (&typename, attrs, 0);
2580 return typename;
2583 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2585 tree
2586 groktypename_in_parm_context (tree typename)
2588 if (TREE_CODE (typename) != TREE_LIST)
2589 return typename;
2590 return grokdeclarator (TREE_VALUE (typename),
2591 TREE_PURPOSE (typename),
2592 PARM, 0, NULL);
2595 /* Decode a declarator in an ordinary declaration or data definition.
2596 This is called as soon as the type information and variable name
2597 have been parsed, before parsing the initializer if any.
2598 Here we create the ..._DECL node, fill in its type,
2599 and put it on the list of decls for the current context.
2600 The ..._DECL node is returned as the value.
2602 Exception: for arrays where the length is not specified,
2603 the type is left null, to be filled in by `finish_decl'.
2605 Function definitions do not come here; they go to start_function
2606 instead. However, external and forward declarations of functions
2607 do go through here. Structure field declarations are done by
2608 grokfield and not through here. */
2610 tree
2611 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2613 tree decl;
2614 tree tem;
2616 /* An object declared as __attribute__((deprecated)) suppresses
2617 warnings of uses of other deprecated items. */
2618 if (lookup_attribute ("deprecated", attributes))
2619 deprecated_state = DEPRECATED_SUPPRESS;
2621 decl = grokdeclarator (declarator, declspecs,
2622 NORMAL, initialized, NULL);
2624 deprecated_state = DEPRECATED_NORMAL;
2626 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2627 && MAIN_NAME_P (DECL_NAME (decl)))
2628 warning ("%J'%D' is usually a function", decl, decl);
2630 if (initialized)
2631 /* Is it valid for this decl to have an initializer at all?
2632 If not, set INITIALIZED to zero, which will indirectly
2633 tell 'finish_decl' to ignore the initializer once it is parsed. */
2634 switch (TREE_CODE (decl))
2636 case TYPE_DECL:
2637 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2638 initialized = 0;
2639 break;
2641 case FUNCTION_DECL:
2642 error ("function '%D' is initialized like a variable", decl);
2643 initialized = 0;
2644 break;
2646 case PARM_DECL:
2647 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2648 error ("parameter '%D' is initialized", decl);
2649 initialized = 0;
2650 break;
2652 default:
2653 /* Don't allow initializations for incomplete types except for
2654 arrays which might be completed by the initialization. */
2656 /* This can happen if the array size is an undefined macro.
2657 We already gave a warning, so we don't need another one. */
2658 if (TREE_TYPE (decl) == error_mark_node)
2659 initialized = 0;
2660 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2662 /* A complete type is ok if size is fixed. */
2664 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2665 || C_DECL_VARIABLE_SIZE (decl))
2667 error ("variable-sized object may not be initialized");
2668 initialized = 0;
2671 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2673 error ("variable '%D' has initializer but incomplete type", decl);
2674 initialized = 0;
2676 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2678 error ("elements of array '%D' have incomplete type", decl);
2679 initialized = 0;
2683 if (initialized)
2685 DECL_EXTERNAL (decl) = 0;
2686 if (current_scope == file_scope)
2687 TREE_STATIC (decl) = 1;
2689 /* Tell 'pushdecl' this is an initialized decl
2690 even though we don't yet have the initializer expression.
2691 Also tell 'finish_decl' it may store the real initializer. */
2692 DECL_INITIAL (decl) = error_mark_node;
2695 /* If this is a function declaration, write a record describing it to the
2696 prototypes file (if requested). */
2698 if (TREE_CODE (decl) == FUNCTION_DECL)
2699 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2701 /* ANSI specifies that a tentative definition which is not merged with
2702 a non-tentative definition behaves exactly like a definition with an
2703 initializer equal to zero. (Section 3.7.2)
2705 -fno-common gives strict ANSI behavior, though this tends to break
2706 a large body of code that grew up without this rule.
2708 Thread-local variables are never common, since there's no entrenched
2709 body of code to break, and it allows more efficient variable references
2710 in the presence of dynamic linking. */
2712 if (TREE_CODE (decl) == VAR_DECL
2713 && !initialized
2714 && TREE_PUBLIC (decl)
2715 && !DECL_THREAD_LOCAL (decl)
2716 && !flag_no_common)
2717 DECL_COMMON (decl) = 1;
2719 /* Set attributes here so if duplicate decl, will have proper attributes. */
2720 decl_attributes (&decl, attributes, 0);
2722 if (TREE_CODE (decl) == FUNCTION_DECL
2723 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2725 tree ce = declarator;
2727 if (TREE_CODE (ce) == INDIRECT_REF)
2728 ce = TREE_OPERAND (declarator, 0);
2729 if (TREE_CODE (ce) == CALL_EXPR)
2731 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2732 for (; args; args = TREE_CHAIN (args))
2734 tree type = TREE_TYPE (args);
2735 if (INTEGRAL_TYPE_P (type)
2736 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2737 DECL_ARG_TYPE (args) = integer_type_node;
2742 if (TREE_CODE (decl) == FUNCTION_DECL
2743 && DECL_DECLARED_INLINE_P (decl)
2744 && DECL_UNINLINABLE (decl)
2745 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2746 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2748 /* Add this decl to the current scope.
2749 TEM may equal DECL or it may be a previous decl of the same name. */
2750 tem = pushdecl (decl);
2752 return tem;
2755 /* Finish processing of a declaration;
2756 install its initial value.
2757 If the length of an array type is not known before,
2758 it must be determined now, from the initial value, or it is an error. */
2760 void
2761 finish_decl (tree decl, tree init, tree asmspec_tree)
2763 tree type = TREE_TYPE (decl);
2764 int was_incomplete = (DECL_SIZE (decl) == 0);
2765 const char *asmspec = 0;
2767 /* If a name was specified, get the string. */
2768 if (current_scope == file_scope)
2769 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2770 if (asmspec_tree)
2771 asmspec = TREE_STRING_POINTER (asmspec_tree);
2773 /* If `start_decl' didn't like having an initialization, ignore it now. */
2774 if (init != 0 && DECL_INITIAL (decl) == 0)
2775 init = 0;
2777 /* Don't crash if parm is initialized. */
2778 if (TREE_CODE (decl) == PARM_DECL)
2779 init = 0;
2781 if (init)
2782 store_init_value (decl, init);
2784 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2785 || TREE_CODE (decl) == FUNCTION_DECL
2786 || TREE_CODE (decl) == FIELD_DECL))
2787 objc_check_decl (decl);
2789 /* Deduce size of array from initialization, if not already known. */
2790 if (TREE_CODE (type) == ARRAY_TYPE
2791 && TYPE_DOMAIN (type) == 0
2792 && TREE_CODE (decl) != TYPE_DECL)
2794 int do_default
2795 = (TREE_STATIC (decl)
2796 /* Even if pedantic, an external linkage array
2797 may have incomplete type at first. */
2798 ? pedantic && !TREE_PUBLIC (decl)
2799 : !DECL_EXTERNAL (decl));
2800 int failure
2801 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2803 /* Get the completed type made by complete_array_type. */
2804 type = TREE_TYPE (decl);
2806 if (failure == 1)
2807 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2809 else if (failure == 2)
2811 if (do_default)
2812 error ("%Jarray size missing in '%D'", decl, decl);
2813 /* If a `static' var's size isn't known,
2814 make it extern as well as static, so it does not get
2815 allocated.
2816 If it is not `static', then do not mark extern;
2817 finish_incomplete_decl will give it a default size
2818 and it will get allocated. */
2819 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2820 DECL_EXTERNAL (decl) = 1;
2823 /* TYPE_MAX_VALUE is always one less than the number of elements
2824 in the array, because we start counting at zero. Therefore,
2825 warn only if the value is less than zero. */
2826 else if (pedantic && TYPE_DOMAIN (type) != 0
2827 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2828 error ("%Jzero or negative size array '%D'", decl, decl);
2830 layout_decl (decl, 0);
2833 if (TREE_CODE (decl) == VAR_DECL)
2835 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2836 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2837 layout_decl (decl, 0);
2839 if (DECL_SIZE (decl) == 0
2840 /* Don't give an error if we already gave one earlier. */
2841 && TREE_TYPE (decl) != error_mark_node
2842 && (TREE_STATIC (decl)
2844 /* A static variable with an incomplete type
2845 is an error if it is initialized.
2846 Also if it is not file scope.
2847 Otherwise, let it through, but if it is not `extern'
2848 then it may cause an error message later. */
2849 (DECL_INITIAL (decl) != 0
2850 || !DECL_FILE_SCOPE_P (decl))
2852 /* An automatic variable with an incomplete type
2853 is an error. */
2854 !DECL_EXTERNAL (decl)))
2856 error ("%Jstorage size of '%D' isn't known", decl, decl);
2857 TREE_TYPE (decl) = error_mark_node;
2860 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2861 && DECL_SIZE (decl) != 0)
2863 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2864 constant_expression_warning (DECL_SIZE (decl));
2865 else
2866 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2869 if (TREE_USED (type))
2870 TREE_USED (decl) = 1;
2873 /* If this is a function and an assembler name is specified, reset DECL_RTL
2874 so we can give it its new name. Also, update built_in_decls if it
2875 was a normal built-in. */
2876 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2878 /* ASMSPEC is given, and not the name of a register. Mark the
2879 name with a star so assemble_name won't munge it. */
2880 char *starred = alloca (strlen (asmspec) + 2);
2881 starred[0] = '*';
2882 strcpy (starred + 1, asmspec);
2884 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2886 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2887 SET_DECL_RTL (builtin, NULL_RTX);
2888 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2889 #ifdef TARGET_MEM_FUNCTIONS
2890 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2891 init_block_move_fn (starred);
2892 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2893 init_block_clear_fn (starred);
2894 #else
2895 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2896 init_block_move_fn (starred);
2897 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2898 init_block_clear_fn (starred);
2899 #endif
2901 SET_DECL_RTL (decl, NULL_RTX);
2902 change_decl_assembler_name (decl, get_identifier (starred));
2905 /* If #pragma weak was used, mark the decl weak now. */
2906 if (current_scope == file_scope)
2907 maybe_apply_pragma_weak (decl);
2909 /* Output the assembler code and/or RTL code for variables and functions,
2910 unless the type is an undefined structure or union.
2911 If not, it will get done when the type is completed. */
2913 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2915 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2916 if (c_dialect_objc ())
2917 objc_check_decl (decl);
2919 if (DECL_FILE_SCOPE_P (decl))
2921 if (DECL_INITIAL (decl) == NULL_TREE
2922 || DECL_INITIAL (decl) == error_mark_node)
2923 /* Don't output anything
2924 when a tentative file-scope definition is seen.
2925 But at end of compilation, do output code for them. */
2926 DECL_DEFER_OUTPUT (decl) = 1;
2927 rest_of_decl_compilation (decl, asmspec, true, 0);
2929 else
2931 /* This is a local variable. If there is an ASMSPEC, the
2932 user has requested that we handle it specially. */
2933 if (asmspec)
2935 /* In conjunction with an ASMSPEC, the `register'
2936 keyword indicates that we should place the variable
2937 in a particular register. */
2938 if (C_DECL_REGISTER (decl))
2940 DECL_HARD_REGISTER (decl) = 1;
2941 /* This cannot be done for a structure with volatile
2942 fields, on which DECL_REGISTER will have been
2943 reset. */
2944 if (!DECL_REGISTER (decl))
2945 error ("cannot put object with volatile field into register");
2948 /* If this is not a static variable, issue a warning.
2949 It doesn't make any sense to give an ASMSPEC for an
2950 ordinary, non-register local variable. Historically,
2951 GCC has accepted -- but ignored -- the ASMSPEC in
2952 this case. */
2953 if (TREE_CODE (decl) == VAR_DECL
2954 && !C_DECL_REGISTER (decl)
2955 && !TREE_STATIC (decl))
2956 warning ("%Jignoring asm-specifier for non-static local "
2957 "variable '%D'", decl, decl);
2958 else
2959 change_decl_assembler_name (decl, get_identifier (asmspec));
2962 if (TREE_CODE (decl) != FUNCTION_DECL)
2963 add_decl_stmt (decl);
2966 if (!DECL_FILE_SCOPE_P (decl))
2968 /* Recompute the RTL of a local array now
2969 if it used to be an incomplete type. */
2970 if (was_incomplete
2971 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2973 /* If we used it already as memory, it must stay in memory. */
2974 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2975 /* If it's still incomplete now, no init will save it. */
2976 if (DECL_SIZE (decl) == 0)
2977 DECL_INITIAL (decl) = 0;
2982 /* If this was marked 'used', be sure it will be output. */
2983 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2984 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2986 if (TREE_CODE (decl) == TYPE_DECL)
2988 if (!DECL_FILE_SCOPE_P (decl)
2989 && variably_modified_type_p (TREE_TYPE (decl)))
2990 add_decl_stmt (decl);
2992 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2995 /* At the end of a declaration, throw away any variable type sizes
2996 of types defined inside that declaration. There is no use
2997 computing them in the following function definition. */
2998 if (current_scope == file_scope)
2999 get_pending_sizes ();
3001 /* Install a cleanup (aka destructor) if one was given. */
3002 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3004 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3005 if (attr)
3007 static bool eh_initialized_p;
3009 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3010 tree cleanup_decl = lookup_name (cleanup_id);
3011 tree cleanup;
3013 /* Build "cleanup(&decl)" for the destructor. */
3014 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3015 cleanup = build_tree_list (NULL_TREE, cleanup);
3016 cleanup = build_function_call (cleanup_decl, cleanup);
3018 /* Don't warn about decl unused; the cleanup uses it. */
3019 TREE_USED (decl) = 1;
3021 /* Initialize EH, if we've been told to do so. */
3022 if (flag_exceptions && !eh_initialized_p)
3024 eh_initialized_p = true;
3025 eh_personality_libfunc
3026 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3027 ? "__gcc_personality_sj0"
3028 : "__gcc_personality_v0");
3029 using_eh_for_cleanups ();
3032 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3037 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3038 and push that on the current scope. */
3040 void
3041 push_parm_decl (tree parm)
3043 tree decl;
3045 /* Don't attempt to expand sizes while parsing this decl.
3046 (We can get here with i_s_e 1 somehow from Objective-C.) */
3047 int save_immediate_size_expand = immediate_size_expand;
3048 immediate_size_expand = 0;
3050 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3051 TREE_PURPOSE (TREE_PURPOSE (parm)),
3052 PARM, 0, NULL);
3053 decl_attributes (&decl, TREE_VALUE (parm), 0);
3055 decl = pushdecl (decl);
3057 finish_decl (decl, NULL_TREE, NULL_TREE);
3059 immediate_size_expand = save_immediate_size_expand;
3062 /* Mark all the parameter declarations to date as forward decls.
3063 Also diagnose use of this extension. */
3065 void
3066 mark_forward_parm_decls (void)
3068 struct c_binding *b;
3070 if (pedantic && !current_scope->warned_forward_parm_decls)
3072 pedwarn ("ISO C forbids forward parameter declarations");
3073 current_scope->warned_forward_parm_decls = true;
3076 for (b = current_scope->bindings; b; b = b->prev)
3077 if (TREE_CODE (b->decl) == PARM_DECL)
3078 TREE_ASM_WRITTEN (b->decl) = 1;
3081 static GTY(()) int compound_literal_number;
3083 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3084 literal, which may be an incomplete array type completed by the
3085 initializer; INIT is a CONSTRUCTOR that initializes the compound
3086 literal. */
3088 tree
3089 build_compound_literal (tree type, tree init)
3091 /* We do not use start_decl here because we have a type, not a declarator;
3092 and do not use finish_decl because the decl should be stored inside
3093 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3094 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3095 tree complit;
3096 tree stmt;
3097 DECL_EXTERNAL (decl) = 0;
3098 TREE_PUBLIC (decl) = 0;
3099 TREE_STATIC (decl) = (current_scope == file_scope);
3100 DECL_CONTEXT (decl) = current_function_decl;
3101 TREE_USED (decl) = 1;
3102 TREE_TYPE (decl) = type;
3103 TREE_READONLY (decl) = TYPE_READONLY (type);
3104 store_init_value (decl, init);
3106 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3108 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3109 if (failure)
3110 abort ();
3113 type = TREE_TYPE (decl);
3114 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3115 return error_mark_node;
3117 stmt = build_stmt (DECL_STMT, decl);
3118 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3119 TREE_SIDE_EFFECTS (complit) = 1;
3121 layout_decl (decl, 0);
3123 if (TREE_STATIC (decl))
3125 /* This decl needs a name for the assembler output. We also need
3126 a unique suffix to be added to the name. */
3127 char *name;
3129 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3130 compound_literal_number);
3131 compound_literal_number++;
3132 DECL_NAME (decl) = get_identifier (name);
3133 DECL_DEFER_OUTPUT (decl) = 1;
3134 DECL_COMDAT (decl) = 1;
3135 DECL_ARTIFICIAL (decl) = 1;
3136 pushdecl (decl);
3137 rest_of_decl_compilation (decl, NULL, 1, 0);
3140 return complit;
3143 /* Make TYPE a complete type based on INITIAL_VALUE.
3144 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3145 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3148 complete_array_type (tree type, tree initial_value, int do_default)
3150 tree maxindex = NULL_TREE;
3151 int value = 0;
3153 if (initial_value)
3155 /* Note MAXINDEX is really the maximum index,
3156 one less than the size. */
3157 if (TREE_CODE (initial_value) == STRING_CST)
3159 int eltsize
3160 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3161 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3162 / eltsize) - 1, 0);
3164 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3166 tree elts = CONSTRUCTOR_ELTS (initial_value);
3167 maxindex = build_int_2 (-1, -1);
3168 for (; elts; elts = TREE_CHAIN (elts))
3170 if (TREE_PURPOSE (elts))
3171 maxindex = TREE_PURPOSE (elts);
3172 else
3173 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3174 maxindex, integer_one_node));
3176 maxindex = copy_node (maxindex);
3178 else
3180 /* Make an error message unless that happened already. */
3181 if (initial_value != error_mark_node)
3182 value = 1;
3184 /* Prevent further error messages. */
3185 maxindex = build_int_2 (0, 0);
3189 if (!maxindex)
3191 if (do_default)
3192 maxindex = build_int_2 (0, 0);
3193 value = 2;
3196 if (maxindex)
3198 TYPE_DOMAIN (type) = build_index_type (maxindex);
3199 if (!TREE_TYPE (maxindex))
3200 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3203 /* Lay out the type now that we can get the real answer. */
3205 layout_type (type);
3207 return value;
3210 /* Determine whether TYPE is a structure with a flexible array member,
3211 or a union containing such a structure (possibly recursively). */
3213 static bool
3214 flexible_array_type_p (tree type)
3216 tree x;
3217 switch (TREE_CODE (type))
3219 case RECORD_TYPE:
3220 x = TYPE_FIELDS (type);
3221 if (x == NULL_TREE)
3222 return false;
3223 while (TREE_CHAIN (x) != NULL_TREE)
3224 x = TREE_CHAIN (x);
3225 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3226 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3227 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3228 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3229 return true;
3230 return false;
3231 case UNION_TYPE:
3232 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3234 if (flexible_array_type_p (TREE_TYPE (x)))
3235 return true;
3237 return false;
3238 default:
3239 return false;
3243 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3244 replacing with appropriate values if they are invalid. */
3245 static void
3246 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3248 tree type_mv;
3249 unsigned int max_width;
3250 unsigned HOST_WIDE_INT w;
3251 const char *name = orig_name ? orig_name: _("<anonymous>");
3253 /* Necessary? */
3254 STRIP_NOPS (*width);
3256 /* Detect and ignore out of range field width and process valid
3257 field widths. */
3258 if (TREE_CODE (*width) != INTEGER_CST)
3260 error ("bit-field `%s' width not an integer constant", name);
3261 *width = integer_one_node;
3263 else
3265 constant_expression_warning (*width);
3266 if (tree_int_cst_sgn (*width) < 0)
3268 error ("negative width in bit-field `%s'", name);
3269 *width = integer_one_node;
3271 else if (integer_zerop (*width) && orig_name)
3273 error ("zero width for bit-field `%s'", name);
3274 *width = integer_one_node;
3278 /* Detect invalid bit-field type. */
3279 if (TREE_CODE (*type) != INTEGER_TYPE
3280 && TREE_CODE (*type) != BOOLEAN_TYPE
3281 && TREE_CODE (*type) != ENUMERAL_TYPE)
3283 error ("bit-field `%s' has invalid type", name);
3284 *type = unsigned_type_node;
3287 type_mv = TYPE_MAIN_VARIANT (*type);
3288 if (pedantic
3289 && type_mv != integer_type_node
3290 && type_mv != unsigned_type_node
3291 && type_mv != boolean_type_node)
3292 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3294 if (type_mv == boolean_type_node)
3295 max_width = CHAR_TYPE_SIZE;
3296 else
3297 max_width = TYPE_PRECISION (*type);
3299 if (0 < compare_tree_int (*width, max_width))
3301 error ("width of `%s' exceeds its type", name);
3302 w = max_width;
3303 *width = build_int_2 (w, 0);
3305 else
3306 w = tree_low_cst (*width, 1);
3308 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3310 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3311 if (!lt
3312 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3313 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3314 warning ("`%s' is narrower than values of its type", name);
3318 /* Given declspecs and a declarator,
3319 determine the name and type of the object declared
3320 and construct a ..._DECL node for it.
3321 (In one case we can return a ..._TYPE node instead.
3322 For invalid input we sometimes return 0.)
3324 DECLSPECS is a chain of tree_list nodes whose value fields
3325 are the storage classes and type specifiers.
3327 DECL_CONTEXT says which syntactic context this declaration is in:
3328 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3329 FUNCDEF for a function definition. Like NORMAL but a few different
3330 error messages in each case. Return value may be zero meaning
3331 this definition is too screwy to try to parse.
3332 PARM for a parameter declaration (either within a function prototype
3333 or before a function body). Make a PARM_DECL, or return void_type_node.
3334 TYPENAME if for a typename (in a cast or sizeof).
3335 Don't make a DECL node; just return the ..._TYPE node.
3336 FIELD for a struct or union field; make a FIELD_DECL.
3337 INITIALIZED is 1 if the decl has an initializer.
3338 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3339 representing the width of the bit-field.
3341 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3342 It may also be so in the PARM case, for a prototype where the
3343 argument type is specified but not the name.
3345 This function is where the complicated C meanings of `static'
3346 and `extern' are interpreted. */
3348 static tree
3349 grokdeclarator (tree declarator, tree declspecs,
3350 enum decl_context decl_context, int initialized, tree *width)
3352 int specbits = 0;
3353 tree spec;
3354 tree type = NULL_TREE;
3355 int longlong = 0;
3356 int constp;
3357 int restrictp;
3358 int volatilep;
3359 int type_quals = TYPE_UNQUALIFIED;
3360 int inlinep;
3361 int explicit_int = 0;
3362 int explicit_char = 0;
3363 int defaulted_int = 0;
3364 tree typedef_decl = 0;
3365 const char *name, *orig_name;
3366 tree typedef_type = 0;
3367 int funcdef_flag = 0;
3368 enum tree_code innermost_code = ERROR_MARK;
3369 int size_varies = 0;
3370 tree decl_attr = NULL_TREE;
3371 tree array_ptr_quals = NULL_TREE;
3372 int array_parm_static = 0;
3373 tree returned_attrs = NULL_TREE;
3374 bool bitfield = width != NULL;
3375 tree element_type;
3376 tree arg_info = NULL_TREE;
3378 if (decl_context == FUNCDEF)
3379 funcdef_flag = 1, decl_context = NORMAL;
3381 /* Look inside a declarator for the name being declared
3382 and get it as a string, for an error message. */
3384 tree decl = declarator;
3385 name = 0;
3387 while (decl)
3388 switch (TREE_CODE (decl))
3390 case ARRAY_REF:
3391 case INDIRECT_REF:
3392 case CALL_EXPR:
3393 innermost_code = TREE_CODE (decl);
3394 decl = TREE_OPERAND (decl, 0);
3395 break;
3397 case TREE_LIST:
3398 decl = TREE_VALUE (decl);
3399 break;
3401 case IDENTIFIER_NODE:
3402 name = IDENTIFIER_POINTER (decl);
3403 decl = 0;
3404 break;
3406 default:
3407 abort ();
3409 orig_name = name;
3410 if (name == 0)
3411 name = "type name";
3414 /* A function definition's declarator must have the form of
3415 a function declarator. */
3417 if (funcdef_flag && innermost_code != CALL_EXPR)
3418 return 0;
3420 /* If this looks like a function definition, make it one,
3421 even if it occurs where parms are expected.
3422 Then store_parm_decls will reject it and not use it as a parm. */
3423 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3424 decl_context = PARM;
3426 /* Look through the decl specs and record which ones appear.
3427 Some typespecs are defined as built-in typenames.
3428 Others, the ones that are modifiers of other types,
3429 are represented by bits in SPECBITS: set the bits for
3430 the modifiers that appear. Storage class keywords are also in SPECBITS.
3432 If there is a typedef name or a type, store the type in TYPE.
3433 This includes builtin typedefs such as `int'.
3435 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3436 and did not come from a user typedef.
3438 Set LONGLONG if `long' is mentioned twice. */
3440 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3442 tree id = TREE_VALUE (spec);
3444 /* If the entire declaration is itself tagged as deprecated then
3445 suppress reports of deprecated items. */
3446 if (id && TREE_DEPRECATED (id))
3448 if (deprecated_state != DEPRECATED_SUPPRESS)
3449 warn_deprecated_use (id);
3452 if (id == ridpointers[(int) RID_INT])
3453 explicit_int = 1;
3454 if (id == ridpointers[(int) RID_CHAR])
3455 explicit_char = 1;
3457 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3459 enum rid i = C_RID_CODE (id);
3460 if ((int) i <= (int) RID_LAST_MODIFIER)
3462 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3464 if (longlong)
3465 error ("`long long long' is too long for GCC");
3466 else
3468 if (pedantic && !flag_isoc99 && ! in_system_header
3469 && warn_long_long)
3470 pedwarn ("ISO C90 does not support `long long'");
3471 longlong = 1;
3474 else if (specbits & (1 << (int) i))
3476 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3478 if (pedantic && !flag_isoc99)
3479 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3481 else
3482 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3485 /* Diagnose "__thread extern". Recall that this list
3486 is in the reverse order seen in the text. */
3487 if (i == RID_THREAD
3488 && (specbits & (1 << (int) RID_EXTERN
3489 | 1 << (int) RID_STATIC)))
3491 if (specbits & 1 << (int) RID_EXTERN)
3492 error ("`__thread' before `extern'");
3493 else
3494 error ("`__thread' before `static'");
3497 specbits |= 1 << (int) i;
3498 goto found;
3501 if (type)
3502 error ("two or more data types in declaration of `%s'", name);
3503 /* Actual typedefs come to us as TYPE_DECL nodes. */
3504 else if (TREE_CODE (id) == TYPE_DECL)
3506 if (TREE_TYPE (id) == error_mark_node)
3507 ; /* Allow the type to default to int to avoid cascading errors. */
3508 else
3510 type = TREE_TYPE (id);
3511 decl_attr = DECL_ATTRIBUTES (id);
3512 typedef_decl = id;
3515 /* Built-in types come as identifiers. */
3516 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3518 tree t = lookup_name (id);
3519 if (!t || TREE_CODE (t) != TYPE_DECL)
3520 error ("`%s' fails to be a typedef or built in type",
3521 IDENTIFIER_POINTER (id));
3522 else if (TREE_TYPE (t) == error_mark_node)
3524 else
3526 type = TREE_TYPE (t);
3527 typedef_decl = t;
3530 else if (TREE_CODE (id) != ERROR_MARK)
3531 type = id;
3533 found:
3537 typedef_type = type;
3538 if (type)
3539 size_varies = C_TYPE_VARIABLE_SIZE (type);
3541 /* No type at all: default to `int', and set DEFAULTED_INT
3542 because it was not a user-defined typedef. */
3544 if (type == 0)
3546 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3547 | (1 << (int) RID_SIGNED)
3548 | (1 << (int) RID_UNSIGNED)
3549 | (1 << (int) RID_COMPLEX))))
3550 /* Don't warn about typedef foo = bar. */
3551 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3552 && ! in_system_header)
3554 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3555 and this is a function, or if -Wimplicit; prefer the former
3556 warning since it is more explicit. */
3557 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3558 && funcdef_flag)
3559 warn_about_return_type = 1;
3560 else if (warn_implicit_int || flag_isoc99)
3561 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3562 name);
3565 defaulted_int = 1;
3566 type = integer_type_node;
3569 /* Now process the modifiers that were specified
3570 and check for invalid combinations. */
3572 /* Long double is a special combination. */
3574 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3575 && TYPE_MAIN_VARIANT (type) == double_type_node)
3577 specbits &= ~(1 << (int) RID_LONG);
3578 type = long_double_type_node;
3581 /* Check all other uses of type modifiers. */
3583 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3584 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3586 int ok = 0;
3588 if ((specbits & 1 << (int) RID_LONG)
3589 && (specbits & 1 << (int) RID_SHORT))
3590 error ("both long and short specified for `%s'", name);
3591 else if (((specbits & 1 << (int) RID_LONG)
3592 || (specbits & 1 << (int) RID_SHORT))
3593 && explicit_char)
3594 error ("long or short specified with char for `%s'", name);
3595 else if (((specbits & 1 << (int) RID_LONG)
3596 || (specbits & 1 << (int) RID_SHORT))
3597 && TREE_CODE (type) == REAL_TYPE)
3599 static int already = 0;
3601 error ("long or short specified with floating type for `%s'", name);
3602 if (! already && ! pedantic)
3604 error ("the only valid combination is `long double'");
3605 already = 1;
3608 else if ((specbits & 1 << (int) RID_SIGNED)
3609 && (specbits & 1 << (int) RID_UNSIGNED))
3610 error ("both signed and unsigned specified for `%s'", name);
3611 else if (TREE_CODE (type) != INTEGER_TYPE)
3612 error ("long, short, signed or unsigned invalid for `%s'", name);
3613 else
3615 ok = 1;
3616 if (!explicit_int && !defaulted_int && !explicit_char)
3618 error ("long, short, signed or unsigned used invalidly for `%s'",
3619 name);
3620 ok = 0;
3624 /* Discard the type modifiers if they are invalid. */
3625 if (! ok)
3627 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3628 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3629 longlong = 0;
3633 if ((specbits & (1 << (int) RID_COMPLEX))
3634 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3636 error ("complex invalid for `%s'", name);
3637 specbits &= ~(1 << (int) RID_COMPLEX);
3640 /* Decide whether an integer type is signed or not.
3641 Optionally treat bit-fields as signed by default. */
3642 if (specbits & 1 << (int) RID_UNSIGNED
3643 || (bitfield && ! flag_signed_bitfields
3644 && (explicit_int || defaulted_int || explicit_char
3645 /* A typedef for plain `int' without `signed'
3646 can be controlled just like plain `int'. */
3647 || ! (typedef_decl != 0
3648 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3649 && TREE_CODE (type) != ENUMERAL_TYPE
3650 && !(specbits & 1 << (int) RID_SIGNED)))
3652 if (longlong)
3653 type = long_long_unsigned_type_node;
3654 else if (specbits & 1 << (int) RID_LONG)
3655 type = long_unsigned_type_node;
3656 else if (specbits & 1 << (int) RID_SHORT)
3657 type = short_unsigned_type_node;
3658 else if (type == char_type_node)
3659 type = unsigned_char_type_node;
3660 else if (typedef_decl)
3661 type = c_common_unsigned_type (type);
3662 else
3663 type = unsigned_type_node;
3665 else if ((specbits & 1 << (int) RID_SIGNED)
3666 && type == char_type_node)
3667 type = signed_char_type_node;
3668 else if (longlong)
3669 type = long_long_integer_type_node;
3670 else if (specbits & 1 << (int) RID_LONG)
3671 type = long_integer_type_node;
3672 else if (specbits & 1 << (int) RID_SHORT)
3673 type = short_integer_type_node;
3675 if (specbits & 1 << (int) RID_COMPLEX)
3677 if (pedantic && !flag_isoc99)
3678 pedwarn ("ISO C90 does not support complex types");
3679 /* If we just have "complex", it is equivalent to
3680 "complex double", but if any modifiers at all are specified it is
3681 the complex form of TYPE. E.g, "complex short" is
3682 "complex short int". */
3684 if (defaulted_int && ! longlong
3685 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3686 | (1 << (int) RID_SIGNED)
3687 | (1 << (int) RID_UNSIGNED))))
3689 if (pedantic)
3690 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3691 type = complex_double_type_node;
3693 else if (type == integer_type_node)
3695 if (pedantic)
3696 pedwarn ("ISO C does not support complex integer types");
3697 type = complex_integer_type_node;
3699 else if (type == float_type_node)
3700 type = complex_float_type_node;
3701 else if (type == double_type_node)
3702 type = complex_double_type_node;
3703 else if (type == long_double_type_node)
3704 type = complex_long_double_type_node;
3705 else
3707 if (pedantic)
3708 pedwarn ("ISO C does not support complex integer types");
3709 type = build_complex_type (type);
3713 /* Check the type and width of a bit-field. */
3714 if (bitfield)
3715 check_bitfield_type_and_width (&type, width, orig_name);
3717 /* Figure out the type qualifiers for the declaration. There are
3718 two ways a declaration can become qualified. One is something
3719 like `const int i' where the `const' is explicit. Another is
3720 something like `typedef const int CI; CI i' where the type of the
3721 declaration contains the `const'. A third possibility is that
3722 there is a type qualifier on the element type of a typedefed
3723 array type, in which case we should extract that qualifier so
3724 that c_apply_type_quals_to_decls receives the full list of
3725 qualifiers to work with (C90 is not entirely clear about whether
3726 duplicate qualifiers should be diagnosed in this case, but it
3727 seems most appropriate to do so). */
3728 element_type = strip_array_types (type);
3729 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3730 restrictp
3731 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3732 volatilep
3733 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3734 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3735 if (pedantic && !flag_isoc99)
3737 if (constp > 1)
3738 pedwarn ("duplicate `const'");
3739 if (restrictp > 1)
3740 pedwarn ("duplicate `restrict'");
3741 if (volatilep > 1)
3742 pedwarn ("duplicate `volatile'");
3744 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3745 type = TYPE_MAIN_VARIANT (type);
3746 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3747 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3748 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3750 /* Warn if two storage classes are given. Default to `auto'. */
3753 int nclasses = 0;
3755 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3756 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3757 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3758 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3759 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3761 /* "static __thread" and "extern __thread" are allowed. */
3762 if ((specbits & (1 << (int) RID_THREAD
3763 | 1 << (int) RID_STATIC
3764 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3765 nclasses++;
3767 /* Warn about storage classes that are invalid for certain
3768 kinds of declarations (parameters, typenames, etc.). */
3770 if (nclasses > 1)
3771 error ("multiple storage classes in declaration of `%s'", name);
3772 else if (funcdef_flag
3773 && (specbits
3774 & ((1 << (int) RID_REGISTER)
3775 | (1 << (int) RID_AUTO)
3776 | (1 << (int) RID_TYPEDEF)
3777 | (1 << (int) RID_THREAD))))
3779 if (specbits & 1 << (int) RID_AUTO
3780 && (pedantic || current_scope == file_scope))
3781 pedwarn ("function definition declared `auto'");
3782 if (specbits & 1 << (int) RID_REGISTER)
3783 error ("function definition declared `register'");
3784 if (specbits & 1 << (int) RID_TYPEDEF)
3785 error ("function definition declared `typedef'");
3786 if (specbits & 1 << (int) RID_THREAD)
3787 error ("function definition declared `__thread'");
3788 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3789 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3791 else if (decl_context != NORMAL && nclasses > 0)
3793 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3795 else
3797 switch (decl_context)
3799 case FIELD:
3800 error ("storage class specified for structure field `%s'",
3801 name);
3802 break;
3803 case PARM:
3804 error ("storage class specified for parameter `%s'", name);
3805 break;
3806 default:
3807 error ("storage class specified for typename");
3808 break;
3810 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3811 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3812 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3815 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3817 /* `extern' with initialization is invalid if not at file scope. */
3818 if (current_scope == file_scope)
3819 warning ("`%s' initialized and declared `extern'", name);
3820 else
3821 error ("`%s' has both `extern' and initializer", name);
3823 else if (current_scope == file_scope)
3825 if (specbits & 1 << (int) RID_AUTO)
3826 error ("file-scope declaration of `%s' specifies `auto'", name);
3828 else
3830 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3831 error ("nested function `%s' declared `extern'", name);
3832 else if ((specbits & (1 << (int) RID_THREAD
3833 | 1 << (int) RID_EXTERN
3834 | 1 << (int) RID_STATIC))
3835 == (1 << (int) RID_THREAD))
3837 error ("function-scope `%s' implicitly auto and declared `__thread'",
3838 name);
3839 specbits &= ~(1 << (int) RID_THREAD);
3844 /* Now figure out the structure of the declarator proper.
3845 Descend through it, creating more complex types, until we reach
3846 the declared identifier (or NULL_TREE, in an absolute declarator). */
3848 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3850 if (type == error_mark_node)
3852 declarator = TREE_OPERAND (declarator, 0);
3853 continue;
3856 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3857 an INDIRECT_REF (for *...),
3858 a CALL_EXPR (for ...(...)),
3859 a TREE_LIST (for nested attributes),
3860 an identifier (for the name being declared)
3861 or a null pointer (for the place in an absolute declarator
3862 where the name was omitted).
3863 For the last two cases, we have just exited the loop.
3865 At this point, TYPE is the type of elements of an array,
3866 or for a function to return, or for a pointer to point to.
3867 After this sequence of ifs, TYPE is the type of the
3868 array or function or pointer, and DECLARATOR has had its
3869 outermost layer removed. */
3871 if (array_ptr_quals != NULL_TREE || array_parm_static)
3873 /* Only the innermost declarator (making a parameter be of
3874 array type which is converted to pointer type)
3875 may have static or type qualifiers. */
3876 error ("static or type qualifiers in non-parameter array declarator");
3877 array_ptr_quals = NULL_TREE;
3878 array_parm_static = 0;
3881 if (TREE_CODE (declarator) == TREE_LIST)
3883 /* We encode a declarator with embedded attributes using
3884 a TREE_LIST. */
3885 tree attrs = TREE_PURPOSE (declarator);
3886 tree inner_decl;
3887 int attr_flags = 0;
3888 declarator = TREE_VALUE (declarator);
3889 inner_decl = declarator;
3890 while (inner_decl != NULL_TREE
3891 && TREE_CODE (inner_decl) == TREE_LIST)
3892 inner_decl = TREE_VALUE (inner_decl);
3893 if (inner_decl == NULL_TREE
3894 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3895 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3896 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3897 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3898 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3899 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3900 returned_attrs = decl_attributes (&type,
3901 chainon (returned_attrs, attrs),
3902 attr_flags);
3904 else if (TREE_CODE (declarator) == ARRAY_REF)
3906 tree itype = NULL_TREE;
3907 tree size = TREE_OPERAND (declarator, 1);
3908 /* The index is a signed object `sizetype' bits wide. */
3909 tree index_type = c_common_signed_type (sizetype);
3911 array_ptr_quals = TREE_TYPE (declarator);
3912 array_parm_static = TREE_STATIC (declarator);
3914 declarator = TREE_OPERAND (declarator, 0);
3916 /* Check for some types that there cannot be arrays of. */
3918 if (VOID_TYPE_P (type))
3920 error ("declaration of `%s' as array of voids", name);
3921 type = error_mark_node;
3924 if (TREE_CODE (type) == FUNCTION_TYPE)
3926 error ("declaration of `%s' as array of functions", name);
3927 type = error_mark_node;
3930 if (pedantic && flexible_array_type_p (type))
3931 pedwarn ("invalid use of structure with flexible array member");
3933 if (size == error_mark_node)
3934 type = error_mark_node;
3936 if (type == error_mark_node)
3937 continue;
3939 /* If size was specified, set ITYPE to a range-type for that size.
3940 Otherwise, ITYPE remains null. finish_decl may figure it out
3941 from an initial value. */
3943 if (size)
3945 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3946 STRIP_TYPE_NOPS (size);
3948 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3950 error ("size of array `%s' has non-integer type", name);
3951 size = integer_one_node;
3954 if (pedantic && integer_zerop (size))
3955 pedwarn ("ISO C forbids zero-size array `%s'", name);
3957 if (TREE_CODE (size) == INTEGER_CST)
3959 constant_expression_warning (size);
3960 if (tree_int_cst_sgn (size) < 0)
3962 error ("size of array `%s' is negative", name);
3963 size = integer_one_node;
3966 else
3968 /* Make sure the array size remains visibly nonconstant
3969 even if it is (eg) a const variable with known value. */
3970 size_varies = 1;
3972 if (!flag_isoc99 && pedantic)
3974 if (TREE_CONSTANT (size))
3975 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3976 name);
3977 else
3978 pedwarn ("ISO C90 forbids variable-size array `%s'",
3979 name);
3983 if (integer_zerop (size))
3985 /* A zero-length array cannot be represented with an
3986 unsigned index type, which is what we'll get with
3987 build_index_type. Create an open-ended range instead. */
3988 itype = build_range_type (sizetype, size, NULL_TREE);
3990 else
3992 /* Compute the maximum valid index, that is, size - 1.
3993 Do the calculation in index_type, so that if it is
3994 a variable the computations will be done in the
3995 proper mode. */
3996 itype = fold (build (MINUS_EXPR, index_type,
3997 convert (index_type, size),
3998 convert (index_type, size_one_node)));
4000 /* If that overflowed, the array is too big.
4001 ??? While a size of INT_MAX+1 technically shouldn't
4002 cause an overflow (because we subtract 1), the overflow
4003 is recorded during the conversion to index_type, before
4004 the subtraction. Handling this case seems like an
4005 unnecessary complication. */
4006 if (TREE_OVERFLOW (itype))
4008 error ("size of array `%s' is too large", name);
4009 type = error_mark_node;
4010 continue;
4013 if (size_varies)
4015 /* We must be able to distinguish the
4016 SAVE_EXPR_CONTEXT for the variably-sized type
4017 so that we can set it correctly in
4018 set_save_expr_context. The convention is
4019 that all SAVE_EXPRs that need to be reset
4020 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4021 tree cfd = current_function_decl;
4022 if (decl_context == PARM)
4023 current_function_decl = NULL_TREE;
4024 itype = variable_size (itype);
4025 if (decl_context == PARM)
4026 current_function_decl = cfd;
4028 itype = build_index_type (itype);
4031 else if (decl_context == FIELD)
4033 if (pedantic && !flag_isoc99 && !in_system_header)
4034 pedwarn ("ISO C90 does not support flexible array members");
4036 /* ISO C99 Flexible array members are effectively identical
4037 to GCC's zero-length array extension. */
4038 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4041 /* If pedantic, complain about arrays of incomplete types. */
4043 if (pedantic && !COMPLETE_TYPE_P (type))
4044 pedwarn ("array type has incomplete element type");
4046 /* Build the array type itself, then merge any constancy or
4047 volatility into the target type. We must do it in this order
4048 to ensure that the TYPE_MAIN_VARIANT field of the array type
4049 is set correctly. */
4051 type = build_array_type (type, itype);
4052 if (type_quals)
4053 type = c_build_qualified_type (type, type_quals);
4055 if (size_varies)
4056 C_TYPE_VARIABLE_SIZE (type) = 1;
4058 /* The GCC extension for zero-length arrays differs from
4059 ISO flexible array members in that sizeof yields zero. */
4060 if (size && integer_zerop (size))
4062 layout_type (type);
4063 TYPE_SIZE (type) = bitsize_zero_node;
4064 TYPE_SIZE_UNIT (type) = size_zero_node;
4066 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4067 /* We can never complete an array type which is the target of a
4068 pointer, so go ahead and lay it out. */
4069 layout_type (type);
4071 if (decl_context != PARM
4072 && (array_ptr_quals != NULL_TREE || array_parm_static))
4074 error ("static or type qualifiers in non-parameter array declarator");
4075 array_ptr_quals = NULL_TREE;
4076 array_parm_static = 0;
4079 else if (TREE_CODE (declarator) == CALL_EXPR)
4081 /* Say it's a definition only for the CALL_EXPR closest to
4082 the identifier. */
4083 bool really_funcdef = (funcdef_flag
4084 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4085 == IDENTIFIER_NODE));
4086 tree arg_types;
4088 /* Declaring a function type.
4089 Make sure we have a valid type for the function to return. */
4090 if (type == error_mark_node)
4091 continue;
4093 size_varies = 0;
4095 /* Warn about some types functions can't return. */
4097 if (TREE_CODE (type) == FUNCTION_TYPE)
4099 error ("`%s' declared as function returning a function", name);
4100 type = integer_type_node;
4102 if (TREE_CODE (type) == ARRAY_TYPE)
4104 error ("`%s' declared as function returning an array", name);
4105 type = integer_type_node;
4108 /* Construct the function type and go to the next
4109 inner layer of declarator. */
4110 arg_info = TREE_OPERAND (declarator, 1);
4111 arg_types = grokparms (arg_info, really_funcdef);
4113 /* Type qualifiers before the return type of the function
4114 qualify the return type, not the function type. */
4115 if (type_quals)
4117 /* Type qualifiers on a function return type are normally
4118 permitted by the standard but have no effect, so give a
4119 warning at -Wextra. Qualifiers on a void return type have
4120 meaning as a GNU extension, and are banned on function
4121 definitions in ISO C. FIXME: strictly we shouldn't
4122 pedwarn for qualified void return types except on function
4123 definitions, but not doing so could lead to the undesirable
4124 state of a "volatile void" function return type not being
4125 warned about, and a use of the function being compiled
4126 with GNU semantics, with no diagnostics under -pedantic. */
4127 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4128 pedwarn ("ISO C forbids qualified void function return type");
4129 else if (extra_warnings
4130 && !(VOID_TYPE_P (type)
4131 && type_quals == TYPE_QUAL_VOLATILE))
4132 warning ("type qualifiers ignored on function return type");
4134 type = c_build_qualified_type (type, type_quals);
4136 type_quals = TYPE_UNQUALIFIED;
4138 type = build_function_type (type, arg_types);
4139 declarator = TREE_OPERAND (declarator, 0);
4141 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4142 the formal parameter list of this FUNCTION_TYPE to point to
4143 the FUNCTION_TYPE node itself. */
4146 tree link;
4148 for (link = ARG_INFO_TAGS (arg_info);
4149 link;
4150 link = TREE_CHAIN (link))
4151 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4154 else if (TREE_CODE (declarator) == INDIRECT_REF)
4156 /* Merge any constancy or volatility into the target type
4157 for the pointer. */
4159 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4160 && type_quals)
4161 pedwarn ("ISO C forbids qualified function types");
4162 if (type_quals)
4163 type = c_build_qualified_type (type, type_quals);
4164 type_quals = TYPE_UNQUALIFIED;
4165 size_varies = 0;
4167 type = build_pointer_type (type);
4169 /* Process a list of type modifier keywords
4170 (such as const or volatile) that were given inside the `*'. */
4172 if (TREE_TYPE (declarator))
4174 tree typemodlist;
4175 int erred = 0;
4177 constp = 0;
4178 volatilep = 0;
4179 restrictp = 0;
4180 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4181 typemodlist = TREE_CHAIN (typemodlist))
4183 tree qualifier = TREE_VALUE (typemodlist);
4185 if (C_IS_RESERVED_WORD (qualifier))
4187 if (C_RID_CODE (qualifier) == RID_CONST)
4188 constp++;
4189 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4190 volatilep++;
4191 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4192 restrictp++;
4193 else
4194 erred++;
4196 else
4197 erred++;
4200 if (erred)
4201 error ("invalid type modifier within pointer declarator");
4202 if (pedantic && !flag_isoc99)
4204 if (constp > 1)
4205 pedwarn ("duplicate `const'");
4206 if (volatilep > 1)
4207 pedwarn ("duplicate `volatile'");
4208 if (restrictp > 1)
4209 pedwarn ("duplicate `restrict'");
4212 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4213 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4214 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4217 declarator = TREE_OPERAND (declarator, 0);
4219 else
4220 abort ();
4224 /* Now TYPE has the actual type. */
4226 /* Did array size calculations overflow? */
4228 if (TREE_CODE (type) == ARRAY_TYPE
4229 && COMPLETE_TYPE_P (type)
4230 && TREE_OVERFLOW (TYPE_SIZE (type)))
4232 error ("size of array `%s' is too large", name);
4233 /* If we proceed with the array type as it is, we'll eventually
4234 crash in tree_low_cst(). */
4235 type = error_mark_node;
4238 /* If this is declaring a typedef name, return a TYPE_DECL. */
4240 if (specbits & (1 << (int) RID_TYPEDEF))
4242 tree decl;
4243 /* Note that the grammar rejects storage classes
4244 in typenames, fields or parameters */
4245 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4246 && type_quals)
4247 pedwarn ("ISO C forbids qualified function types");
4248 if (type_quals)
4249 type = c_build_qualified_type (type, type_quals);
4250 decl = build_decl (TYPE_DECL, declarator, type);
4251 if ((specbits & (1 << (int) RID_SIGNED))
4252 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4253 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4254 decl_attributes (&decl, returned_attrs, 0);
4255 return decl;
4258 /* Detect the case of an array type of unspecified size
4259 which came, as such, direct from a typedef name.
4260 We must copy the type, so that each identifier gets
4261 a distinct type, so that each identifier's size can be
4262 controlled separately by its own initializer. */
4264 if (type != 0 && typedef_type != 0
4265 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4266 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4268 type = build_array_type (TREE_TYPE (type), 0);
4269 if (size_varies)
4270 C_TYPE_VARIABLE_SIZE (type) = 1;
4273 /* If this is a type name (such as, in a cast or sizeof),
4274 compute the type and return it now. */
4276 if (decl_context == TYPENAME)
4278 /* Note that the grammar rejects storage classes
4279 in typenames, fields or parameters */
4280 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4281 && type_quals)
4282 pedwarn ("ISO C forbids const or volatile function types");
4283 if (type_quals)
4284 type = c_build_qualified_type (type, type_quals);
4285 decl_attributes (&type, returned_attrs, 0);
4286 return type;
4289 /* Aside from typedefs and type names (handle above),
4290 `void' at top level (not within pointer)
4291 is allowed only in public variables.
4292 We don't complain about parms either, but that is because
4293 a better error message can be made later. */
4295 if (VOID_TYPE_P (type) && decl_context != PARM
4296 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4297 && ((specbits & (1 << (int) RID_EXTERN))
4298 || (current_scope == file_scope
4299 && !(specbits
4300 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4302 error ("variable or field `%s' declared void", name);
4303 type = integer_type_node;
4306 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4307 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4310 tree decl;
4312 if (decl_context == PARM)
4314 tree type_as_written;
4315 tree promoted_type;
4317 /* A parameter declared as an array of T is really a pointer to T.
4318 One declared as a function is really a pointer to a function. */
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4322 /* Transfer const-ness of array into that of type pointed to. */
4323 type = TREE_TYPE (type);
4324 if (type_quals)
4325 type = c_build_qualified_type (type, type_quals);
4326 type = build_pointer_type (type);
4327 type_quals = TYPE_UNQUALIFIED;
4328 if (array_ptr_quals)
4330 tree new_ptr_quals, new_ptr_attrs;
4331 int erred = 0;
4332 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4333 /* We don't yet implement attributes in this context. */
4334 if (new_ptr_attrs != NULL_TREE)
4335 warning ("attributes in parameter array declarator ignored");
4337 constp = 0;
4338 volatilep = 0;
4339 restrictp = 0;
4340 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4342 tree qualifier = TREE_VALUE (new_ptr_quals);
4344 if (C_IS_RESERVED_WORD (qualifier))
4346 if (C_RID_CODE (qualifier) == RID_CONST)
4347 constp++;
4348 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4349 volatilep++;
4350 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4351 restrictp++;
4352 else
4353 erred++;
4355 else
4356 erred++;
4359 if (erred)
4360 error ("invalid type modifier within array declarator");
4362 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4363 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4364 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4366 size_varies = 0;
4368 else if (TREE_CODE (type) == FUNCTION_TYPE)
4370 if (pedantic && type_quals)
4371 pedwarn ("ISO C forbids qualified function types");
4372 if (type_quals)
4373 type = c_build_qualified_type (type, type_quals);
4374 type = build_pointer_type (type);
4375 type_quals = TYPE_UNQUALIFIED;
4377 else if (type_quals)
4378 type = c_build_qualified_type (type, type_quals);
4380 type_as_written = type;
4382 decl = build_decl (PARM_DECL, declarator, type);
4383 if (size_varies)
4384 C_DECL_VARIABLE_SIZE (decl) = 1;
4386 /* Compute the type actually passed in the parmlist,
4387 for the case where there is no prototype.
4388 (For example, shorts and chars are passed as ints.)
4389 When there is a prototype, this is overridden later. */
4391 if (type == error_mark_node)
4392 promoted_type = type;
4393 else
4394 promoted_type = c_type_promotes_to (type);
4396 DECL_ARG_TYPE (decl) = promoted_type;
4397 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4399 else if (decl_context == FIELD)
4401 /* Structure field. It may not be a function. */
4403 if (TREE_CODE (type) == FUNCTION_TYPE)
4405 error ("field `%s' declared as a function", name);
4406 type = build_pointer_type (type);
4408 else if (TREE_CODE (type) != ERROR_MARK
4409 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4411 error ("field `%s' has incomplete type", name);
4412 type = error_mark_node;
4414 /* Move type qualifiers down to element of an array. */
4415 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4416 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4417 type_quals),
4418 TYPE_DOMAIN (type));
4419 decl = build_decl (FIELD_DECL, declarator, type);
4420 DECL_NONADDRESSABLE_P (decl) = bitfield;
4422 if (size_varies)
4423 C_DECL_VARIABLE_SIZE (decl) = 1;
4425 else if (TREE_CODE (type) == FUNCTION_TYPE)
4427 /* Every function declaration is "external"
4428 except for those which are inside a function body
4429 in which `auto' is used.
4430 That is a case not specified by ANSI C,
4431 and we use it for forward declarations for nested functions. */
4432 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4433 || current_scope == file_scope);
4435 if (specbits & (1 << (int) RID_AUTO)
4436 && (pedantic || current_scope == file_scope))
4437 pedwarn ("invalid storage class for function `%s'", name);
4438 if (specbits & (1 << (int) RID_REGISTER))
4439 error ("invalid storage class for function `%s'", name);
4440 if (specbits & (1 << (int) RID_THREAD))
4441 error ("invalid storage class for function `%s'", name);
4442 /* Function declaration not at file scope.
4443 Storage classes other than `extern' are not allowed
4444 and `extern' makes no difference. */
4445 if (current_scope != file_scope
4446 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4447 && pedantic)
4448 pedwarn ("invalid storage class for function `%s'", name);
4450 decl = build_decl (FUNCTION_DECL, declarator, type);
4451 decl = build_decl_attribute_variant (decl, decl_attr);
4453 DECL_LANG_SPECIFIC (decl)
4454 = ggc_alloc_cleared (sizeof (struct lang_decl));
4456 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4457 pedwarn ("ISO C forbids qualified function types");
4459 /* GNU C interprets a `volatile void' return type to indicate
4460 that the function does not return. */
4461 if ((type_quals & TYPE_QUAL_VOLATILE)
4462 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4463 warning ("`noreturn' function returns non-void value");
4465 if (extern_ref)
4466 DECL_EXTERNAL (decl) = 1;
4467 /* Record absence of global scope for `static' or `auto'. */
4468 TREE_PUBLIC (decl)
4469 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4471 /* For a function definition, record the argument information
4472 block in DECL_ARGUMENTS where store_parm_decls will look
4473 for it. */
4474 if (funcdef_flag)
4475 DECL_ARGUMENTS (decl) = arg_info;
4477 if (defaulted_int)
4478 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4480 /* Record presence of `inline', if it is reasonable. */
4481 if (MAIN_NAME_P (declarator))
4483 if (inlinep)
4484 warning ("cannot inline function `main'");
4486 else if (inlinep)
4488 /* Record that the function is declared `inline'. */
4489 DECL_DECLARED_INLINE_P (decl) = 1;
4491 /* Do not mark bare declarations as DECL_INLINE. Doing so
4492 in the presence of multiple declarations can result in
4493 the abstract origin pointing between the declarations,
4494 which will confuse dwarf2out. */
4495 if (initialized)
4497 DECL_INLINE (decl) = 1;
4498 if (specbits & (1 << (int) RID_EXTERN))
4499 current_extern_inline = 1;
4502 /* If -finline-functions, assume it can be inlined. This does
4503 two things: let the function be deferred until it is actually
4504 needed, and let dwarf2 know that the function is inlinable. */
4505 else if (flag_inline_trees == 2 && initialized)
4506 DECL_INLINE (decl) = 1;
4508 else
4510 /* It's a variable. */
4511 /* An uninitialized decl with `extern' is a reference. */
4512 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4514 /* Move type qualifiers down to element of an array. */
4515 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4517 int saved_align = TYPE_ALIGN(type);
4518 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4519 type_quals),
4520 TYPE_DOMAIN (type));
4521 TYPE_ALIGN (type) = saved_align;
4523 else if (type_quals)
4524 type = c_build_qualified_type (type, type_quals);
4526 /* C99 6.2.2p7: It is invalid (compile-time undefined
4527 behavior) to create an 'extern' declaration for a
4528 variable if there is a global declaration that is
4529 'static' and the global declaration is not visible.
4530 (If the static declaration _is_ currently visible,
4531 the 'extern' declaration is taken to refer to that decl.) */
4532 if (extern_ref && current_scope != file_scope)
4534 tree global_decl = identifier_global_value (declarator);
4535 tree visible_decl = lookup_name (declarator);
4537 if (global_decl
4538 && global_decl != visible_decl
4539 && TREE_CODE (global_decl) == VAR_DECL
4540 && !TREE_PUBLIC (global_decl))
4541 error ("variable previously declared 'static' redeclared "
4542 "'extern'");
4545 decl = build_decl (VAR_DECL, declarator, type);
4546 if (size_varies)
4547 C_DECL_VARIABLE_SIZE (decl) = 1;
4549 if (inlinep)
4550 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4552 DECL_EXTERNAL (decl) = extern_ref;
4554 /* At file scope, the presence of a `static' or `register' storage
4555 class specifier, or the absence of all storage class specifiers
4556 makes this declaration a definition (perhaps tentative). Also,
4557 the absence of both `static' and `register' makes it public. */
4558 if (current_scope == file_scope)
4560 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4561 | (1 << (int) RID_REGISTER)));
4562 TREE_STATIC (decl) = !extern_ref;
4564 /* Not at file scope, only `static' makes a static definition. */
4565 else
4567 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4568 TREE_PUBLIC (decl) = extern_ref;
4571 if (specbits & 1 << (int) RID_THREAD)
4573 if (targetm.have_tls)
4574 DECL_THREAD_LOCAL (decl) = 1;
4575 else
4576 /* A mere warning is sure to result in improper semantics
4577 at runtime. Don't bother to allow this to compile. */
4578 error ("thread-local storage not supported for this target");
4582 /* Record `register' declaration for warnings on &
4583 and in case doing stupid register allocation. */
4585 if (specbits & (1 << (int) RID_REGISTER))
4587 C_DECL_REGISTER (decl) = 1;
4588 DECL_REGISTER (decl) = 1;
4591 /* Record constancy and volatility. */
4592 c_apply_type_quals_to_decl (type_quals, decl);
4594 /* If a type has volatile components, it should be stored in memory.
4595 Otherwise, the fact that those components are volatile
4596 will be ignored, and would even crash the compiler. */
4597 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4599 /* It is not an error for a structure with volatile fields to
4600 be declared register, but reset DECL_REGISTER since it
4601 cannot actually go in a register. */
4602 int was_reg = C_DECL_REGISTER (decl);
4603 C_DECL_REGISTER (decl) = 0;
4604 DECL_REGISTER (decl) = 0;
4605 c_mark_addressable (decl);
4606 C_DECL_REGISTER (decl) = was_reg;
4609 #ifdef ENABLE_CHECKING
4610 /* This is the earliest point at which we might know the assembler
4611 name of a variable. Thus, if it's known before this, die horribly. */
4612 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4613 abort ();
4614 #endif
4616 decl_attributes (&decl, returned_attrs, 0);
4618 return decl;
4622 /* Decode the parameter-list info for a function type or function definition.
4623 The argument is the value returned by `get_parm_info' (or made in parse.y
4624 if there is an identifier list instead of a parameter decl list).
4625 These two functions are separate because when a function returns
4626 or receives functions then each is called multiple times but the order
4627 of calls is different. The last call to `grokparms' is always the one
4628 that contains the formal parameter names of a function definition.
4630 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4632 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4633 a mere declaration. A nonempty identifier-list gets an error message
4634 when FUNCDEF_FLAG is zero. */
4636 static tree
4637 grokparms (tree arg_info, int funcdef_flag)
4639 tree arg_types = ARG_INFO_TYPES (arg_info);
4641 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4642 && !in_system_header)
4643 warning ("function declaration isn't a prototype");
4645 if (arg_types == error_mark_node)
4646 return 0; /* don't set TYPE_ARG_TYPES in this case */
4648 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4650 if (! funcdef_flag)
4651 pedwarn ("parameter names (without types) in function declaration");
4653 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4654 ARG_INFO_TYPES (arg_info) = 0;
4655 return 0;
4657 else
4659 tree parm, type, typelt;
4660 unsigned int parmno;
4662 /* If the arg types are incomplete in a declaration, they must
4663 include undefined tags. These tags can never be defined in
4664 the scope of the declaration, so the types can never be
4665 completed, and no call can be compiled successfully. */
4667 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4668 parm;
4669 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4671 type = TREE_VALUE (typelt);
4672 if (type == error_mark_node)
4673 continue;
4675 if (!COMPLETE_TYPE_P (type))
4677 if (funcdef_flag)
4679 if (DECL_NAME (parm))
4680 error ("%Jparameter %u ('%D') has incomplete type",
4681 parm, parmno, parm);
4682 else
4683 error ("%Jparameter %u has incomplete type",
4684 parm, parmno);
4686 TREE_VALUE (typelt) = error_mark_node;
4687 TREE_TYPE (parm) = error_mark_node;
4689 else
4691 if (DECL_NAME (parm))
4692 warning ("%Jparameter %u ('%D') has incomplete type",
4693 parm, parmno, parm);
4694 else
4695 warning ("%Jparameter %u has incomplete type",
4696 parm, parmno);
4700 return arg_types;
4704 /* Take apart the current scope and return a tree_list node with info
4705 on a parameter list just parsed. This tree_list node should be
4706 examined using the ARG_INFO_* macros, defined above:
4708 ARG_INFO_PARMS: a list of parameter decls.
4709 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4710 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4711 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4712 constants) defined with the parameters.
4714 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4716 ELLIPSIS being true means the argument list ended in '...' so don't
4717 append a sentinel (void_list_node) to the end of the type-list. */
4719 tree
4720 get_parm_info (bool ellipsis)
4722 struct c_binding *b = current_scope->bindings;
4723 tree arg_info = make_node (TREE_LIST);
4724 tree parms = 0;
4725 tree tags = 0;
4726 tree types = 0;
4727 tree others = 0;
4729 static bool explained_incomplete_types = false;
4730 bool gave_void_only_once_err = false;
4732 /* The bindings in this scope must not get put into a block.
4733 We will take care of deleting the binding nodes. */
4734 current_scope->bindings = 0;
4736 /* This function is only called if there was *something* on the
4737 parameter list. */
4738 #ifdef ENABLE_CHECKING
4739 if (b == 0)
4740 abort ();
4741 #endif
4743 /* A parameter list consisting solely of 'void' indicates that the
4744 function takes no arguments. But if the 'void' is qualified
4745 (by 'const' or 'volatile'), or has a storage class specifier
4746 ('register'), then the behavior is undefined; issue an error.
4747 Typedefs for 'void' are OK (see DR#157). */
4748 if (b->prev == 0 /* one binding */
4749 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4750 && !DECL_NAME (b->decl) /* anonymous */
4751 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4753 if (TREE_THIS_VOLATILE (b->decl)
4754 || TREE_READONLY (b->decl)
4755 || C_DECL_REGISTER (b->decl))
4756 error ("'void' as only parameter may not be qualified");
4758 /* There cannot be an ellipsis. */
4759 if (ellipsis)
4760 error ("'void' must be the only parameter");
4762 ARG_INFO_TYPES (arg_info) = void_list_node;
4763 return arg_info;
4766 if (!ellipsis)
4767 types = void_list_node;
4769 /* Break up the bindings list into parms, tags, types, and others;
4770 apply sanity checks; purge the name-to-decl bindings. */
4771 while (b)
4773 tree decl = b->decl;
4774 tree type = TREE_TYPE (decl);
4775 const char *keyword;
4777 switch (TREE_CODE (decl))
4779 case PARM_DECL:
4780 if (b->id)
4782 #ifdef ENABLE_CHECKING
4783 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4784 #endif
4785 I_SYMBOL_BINDING (b->id) = b->shadowed;
4788 /* Check for forward decls that never got their actual decl. */
4789 if (TREE_ASM_WRITTEN (decl))
4790 error ("%Jparameter '%D' has just a forward declaration",
4791 decl, decl);
4792 /* Check for (..., void, ...) and issue an error. */
4793 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4795 if (!gave_void_only_once_err)
4797 error ("'void' must be the only parameter");
4798 gave_void_only_once_err = true;
4801 else
4803 /* Valid parameter, add it to the list. */
4804 TREE_CHAIN (decl) = parms;
4805 parms = decl;
4807 /* Since there is a prototype, args are passed in their
4808 declared types. The back end may override this later. */
4809 DECL_ARG_TYPE (decl) = type;
4810 types = tree_cons (0, type, types);
4812 break;
4814 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4815 case UNION_TYPE: keyword = "union"; goto tag;
4816 case RECORD_TYPE: keyword = "struct"; goto tag;
4817 tag:
4818 /* Types may not have tag-names, in which case the type
4819 appears in the bindings list with b->id NULL. */
4820 if (b->id)
4822 #ifdef ENABLE_CHECKING
4823 if (I_TAG_BINDING (b->id) != b) abort ();
4824 #endif
4825 I_TAG_BINDING (b->id) = b->shadowed;
4828 /* Warn about any struct, union or enum tags defined in a
4829 parameter list. The scope of such types is limited to
4830 the parameter list, which is rarely if ever desirable
4831 (it's impossible to call such a function with type-
4832 correct arguments). An anonymous union parm type is
4833 meaningful as a GNU extension, so don't warn for that. */
4834 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4836 if (b->id)
4837 /* The %s will be one of 'struct', 'union', or 'enum'. */
4838 warning ("'%s %E' declared inside parameter list",
4839 keyword, b->id);
4840 else
4841 /* The %s will be one of 'struct', 'union', or 'enum'. */
4842 warning ("anonymous %s declared inside parameter list",
4843 keyword);
4845 if (! explained_incomplete_types)
4847 warning ("its scope is only this definition or declaration,"
4848 " which is probably not what you want");
4849 explained_incomplete_types = true;
4853 tags = tree_cons (b->id, decl, tags);
4854 break;
4856 case CONST_DECL:
4857 case TYPE_DECL:
4858 /* CONST_DECLs appear here when we have an embedded enum,
4859 and TYPE_DECLs appear here when we have an embedded struct
4860 or union. No warnings for this - we already warned about the
4861 type itself. */
4862 TREE_CHAIN (decl) = others;
4863 others = decl;
4864 /* fall through */
4866 case ERROR_MARK:
4867 /* error_mark_node appears here when we have an undeclared
4868 variable. Just throw it away. */
4869 if (b->id)
4871 #ifdef ENABLE_CHECKING
4872 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4873 #endif
4874 I_SYMBOL_BINDING (b->id) = b->shadowed;
4876 break;
4878 /* Other things that might be encountered. */
4879 case LABEL_DECL:
4880 case FUNCTION_DECL:
4881 case VAR_DECL:
4882 default:
4883 abort ();
4886 b = free_binding_and_advance (b);
4889 ARG_INFO_PARMS (arg_info) = parms;
4890 ARG_INFO_TAGS (arg_info) = tags;
4891 ARG_INFO_TYPES (arg_info) = types;
4892 ARG_INFO_OTHERS (arg_info) = others;
4893 return arg_info;
4896 /* Get the struct, enum or union (CODE says which) with tag NAME.
4897 Define the tag as a forward-reference if it is not defined. */
4899 tree
4900 xref_tag (enum tree_code code, tree name)
4902 /* If a cross reference is requested, look up the type
4903 already defined for this tag and return it. */
4905 tree ref = lookup_tag (code, name, 0);
4906 /* If this is the right type of tag, return what we found.
4907 (This reference will be shadowed by shadow_tag later if appropriate.)
4908 If this is the wrong type of tag, do not return it. If it was the
4909 wrong type in the same scope, we will have had an error
4910 message already; if in a different scope and declaring
4911 a name, pending_xref_error will give an error message; but if in a
4912 different scope and not declaring a name, this tag should
4913 shadow the previous declaration of a different type of tag, and
4914 this would not work properly if we return the reference found.
4915 (For example, with "struct foo" in an outer scope, "union foo;"
4916 must shadow that tag with a new one of union type.) */
4917 if (ref && TREE_CODE (ref) == code)
4918 return ref;
4920 /* If no such tag is yet defined, create a forward-reference node
4921 and record it as the "definition".
4922 When a real declaration of this type is found,
4923 the forward-reference will be altered into a real type. */
4925 ref = make_node (code);
4926 if (code == ENUMERAL_TYPE)
4928 /* Give the type a default layout like unsigned int
4929 to avoid crashing if it does not get defined. */
4930 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4931 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4932 TYPE_USER_ALIGN (ref) = 0;
4933 TYPE_UNSIGNED (ref) = 1;
4934 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4935 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4936 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4939 pushtag (name, ref);
4941 return ref;
4944 /* Make sure that the tag NAME is defined *in the current scope*
4945 at least as a forward reference.
4946 CODE says which kind of tag NAME ought to be. */
4948 tree
4949 start_struct (enum tree_code code, tree name)
4951 /* If there is already a tag defined at this scope
4952 (as a forward reference), just return it. */
4954 tree ref = 0;
4956 if (name != 0)
4957 ref = lookup_tag (code, name, 1);
4958 if (ref && TREE_CODE (ref) == code)
4960 if (TYPE_FIELDS (ref))
4962 if (code == UNION_TYPE)
4963 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4964 else
4965 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4968 else
4970 /* Otherwise create a forward-reference just so the tag is in scope. */
4972 ref = make_node (code);
4973 pushtag (name, ref);
4976 C_TYPE_BEING_DEFINED (ref) = 1;
4977 TYPE_PACKED (ref) = flag_pack_struct;
4978 return ref;
4981 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4982 of a structure component, returning a FIELD_DECL node.
4983 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4985 This is done during the parsing of the struct declaration.
4986 The FIELD_DECL nodes are chained together and the lot of them
4987 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4989 tree
4990 grokfield (tree declarator, tree declspecs, tree width)
4992 tree value;
4994 if (declarator == NULL_TREE && width == NULL_TREE)
4996 /* This is an unnamed decl.
4998 If we have something of the form "union { list } ;" then this
4999 is the anonymous union extension. Similarly for struct.
5001 If this is something of the form "struct foo;", then
5002 If MS extensions are enabled, this is handled as an
5003 anonymous struct.
5004 Otherwise this is a forward declaration of a structure tag.
5006 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5007 If MS extensions are enabled and foo names a structure, then
5008 again this is an anonymous struct.
5009 Otherwise this is an error.
5011 Oh what a horrid tangled web we weave. I wonder if MS consciously
5012 took this from Plan 9 or if it was an accident of implementation
5013 that took root before someone noticed the bug... */
5015 tree type = TREE_VALUE (declspecs);
5017 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5018 type = TREE_TYPE (type);
5019 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5021 if (flag_ms_extensions)
5022 ; /* ok */
5023 else if (flag_iso)
5024 goto warn_unnamed_field;
5025 else if (TYPE_NAME (type) == NULL)
5026 ; /* ok */
5027 else
5028 goto warn_unnamed_field;
5030 else
5032 warn_unnamed_field:
5033 warning ("declaration does not declare anything");
5034 return NULL_TREE;
5038 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5039 width ? &width : NULL);
5041 finish_decl (value, NULL_TREE, NULL_TREE);
5042 DECL_INITIAL (value) = width;
5044 return value;
5047 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5048 the list such that this does not present a problem later. */
5050 static void
5051 detect_field_duplicates (tree fieldlist)
5053 tree x, y;
5054 int timeout = 10;
5056 /* First, see if there are more than "a few" fields.
5057 This is trivially true if there are zero or one fields. */
5058 if (!fieldlist)
5059 return;
5060 x = TREE_CHAIN (fieldlist);
5061 if (!x)
5062 return;
5063 do {
5064 timeout--;
5065 x = TREE_CHAIN (x);
5066 } while (timeout > 0 && x);
5068 /* If there were "few" fields, avoid the overhead of allocating
5069 a hash table. Instead just do the nested traversal thing. */
5070 if (timeout > 0)
5072 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5073 if (DECL_NAME (x))
5075 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5076 if (DECL_NAME (y) == DECL_NAME (x))
5078 error ("%Jduplicate member '%D'", x, x);
5079 DECL_NAME (x) = NULL_TREE;
5083 else
5085 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5086 void **slot;
5088 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5089 if ((y = DECL_NAME (x)) != 0)
5091 slot = htab_find_slot (htab, y, INSERT);
5092 if (*slot)
5094 error ("%Jduplicate member '%D'", x, x);
5095 DECL_NAME (x) = NULL_TREE;
5097 *slot = y;
5100 htab_delete (htab);
5104 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5105 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5106 ATTRIBUTES are attributes to be applied to the structure. */
5108 tree
5109 finish_struct (tree t, tree fieldlist, tree attributes)
5111 tree x;
5112 bool toplevel = file_scope == current_scope;
5113 int saw_named_field;
5115 /* If this type was previously laid out as a forward reference,
5116 make sure we lay it out again. */
5118 TYPE_SIZE (t) = 0;
5120 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5122 if (pedantic)
5124 for (x = fieldlist; x; x = TREE_CHAIN (x))
5125 if (DECL_NAME (x) != 0)
5126 break;
5128 if (x == 0)
5129 pedwarn ("%s has no %s",
5130 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5131 fieldlist ? _("named members") : _("members"));
5134 /* Install struct as DECL_CONTEXT of each field decl.
5135 Also process specified field sizes,m which is found in the DECL_INITIAL.
5136 Store 0 there, except for ": 0" fields (so we can find them
5137 and delete them, below). */
5139 saw_named_field = 0;
5140 for (x = fieldlist; x; x = TREE_CHAIN (x))
5142 DECL_CONTEXT (x) = t;
5143 DECL_PACKED (x) |= TYPE_PACKED (t);
5145 /* If any field is const, the structure type is pseudo-const. */
5146 if (TREE_READONLY (x))
5147 C_TYPE_FIELDS_READONLY (t) = 1;
5148 else
5150 /* A field that is pseudo-const makes the structure likewise. */
5151 tree t1 = TREE_TYPE (x);
5152 while (TREE_CODE (t1) == ARRAY_TYPE)
5153 t1 = TREE_TYPE (t1);
5154 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5155 && C_TYPE_FIELDS_READONLY (t1))
5156 C_TYPE_FIELDS_READONLY (t) = 1;
5159 /* Any field that is volatile means variables of this type must be
5160 treated in some ways as volatile. */
5161 if (TREE_THIS_VOLATILE (x))
5162 C_TYPE_FIELDS_VOLATILE (t) = 1;
5164 /* Any field of nominal variable size implies structure is too. */
5165 if (C_DECL_VARIABLE_SIZE (x))
5166 C_TYPE_VARIABLE_SIZE (t) = 1;
5168 /* Detect invalid nested redefinition. */
5169 if (TREE_TYPE (x) == t)
5170 error ("nested redefinition of `%s'",
5171 IDENTIFIER_POINTER (TYPE_NAME (t)));
5173 if (DECL_INITIAL (x))
5175 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5176 DECL_SIZE (x) = bitsize_int (width);
5177 DECL_BIT_FIELD (x) = 1;
5178 SET_DECL_C_BIT_FIELD (x);
5181 DECL_INITIAL (x) = 0;
5183 /* Detect flexible array member in an invalid context. */
5184 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5185 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5186 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5187 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5189 if (TREE_CODE (t) == UNION_TYPE)
5191 error ("%Jflexible array member in union", x);
5192 TREE_TYPE (x) = error_mark_node;
5194 else if (TREE_CHAIN (x) != NULL_TREE)
5196 error ("%Jflexible array member not at end of struct", x);
5197 TREE_TYPE (x) = error_mark_node;
5199 else if (! saw_named_field)
5201 error ("%Jflexible array member in otherwise empty struct", x);
5202 TREE_TYPE (x) = error_mark_node;
5206 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5207 && flexible_array_type_p (TREE_TYPE (x)))
5208 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5210 if (DECL_NAME (x))
5211 saw_named_field = 1;
5214 detect_field_duplicates (fieldlist);
5216 /* Now we have the nearly final fieldlist. Record it,
5217 then lay out the structure or union (including the fields). */
5219 TYPE_FIELDS (t) = fieldlist;
5221 layout_type (t);
5223 /* Delete all zero-width bit-fields from the fieldlist. */
5225 tree *fieldlistp = &fieldlist;
5226 while (*fieldlistp)
5227 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5228 *fieldlistp = TREE_CHAIN (*fieldlistp);
5229 else
5230 fieldlistp = &TREE_CHAIN (*fieldlistp);
5233 /* Now we have the truly final field list.
5234 Store it in this type and in the variants. */
5236 TYPE_FIELDS (t) = fieldlist;
5238 /* If there are lots of fields, sort so we can look through them fast.
5239 We arbitrarily consider 16 or more elts to be "a lot". */
5242 int len = 0;
5244 for (x = fieldlist; x; x = TREE_CHAIN (x))
5246 if (len > 15 || DECL_NAME (x) == NULL)
5247 break;
5248 len += 1;
5251 if (len > 15)
5253 tree *field_array;
5254 struct lang_type *space;
5255 struct sorted_fields_type *space2;
5257 len += list_length (x);
5259 /* Use the same allocation policy here that make_node uses, to
5260 ensure that this lives as long as the rest of the struct decl.
5261 All decls in an inline function need to be saved. */
5263 space = ggc_alloc_cleared (sizeof (struct lang_type));
5264 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5266 len = 0;
5267 space->s = space2;
5268 field_array = &space2->elts[0];
5269 for (x = fieldlist; x; x = TREE_CHAIN (x))
5271 field_array[len++] = x;
5273 /* If there is anonymous struct or union, break out of the loop. */
5274 if (DECL_NAME (x) == NULL)
5275 break;
5277 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5278 if (x == NULL)
5280 TYPE_LANG_SPECIFIC (t) = space;
5281 TYPE_LANG_SPECIFIC (t)->s->len = len;
5282 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5283 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5288 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5290 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5291 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5292 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5293 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5296 /* If this was supposed to be a transparent union, but we can't
5297 make it one, warn and turn off the flag. */
5298 if (TREE_CODE (t) == UNION_TYPE
5299 && TYPE_TRANSPARENT_UNION (t)
5300 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5302 TYPE_TRANSPARENT_UNION (t) = 0;
5303 warning ("union cannot be made transparent");
5306 /* If this structure or union completes the type of any previous
5307 variable declaration, lay it out and output its rtl. */
5308 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5310 x = TREE_CHAIN (x))
5312 tree decl = TREE_VALUE (x);
5313 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5314 layout_array_type (TREE_TYPE (decl));
5315 if (TREE_CODE (decl) != TYPE_DECL)
5317 layout_decl (decl, 0);
5318 if (c_dialect_objc ())
5319 objc_check_decl (decl);
5320 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5321 if (! toplevel)
5322 expand_decl (decl);
5325 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5327 /* Finish debugging output for this type. */
5328 rest_of_type_compilation (t, toplevel);
5330 return t;
5333 /* Lay out the type T, and its element type, and so on. */
5335 static void
5336 layout_array_type (tree t)
5338 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5339 layout_array_type (TREE_TYPE (t));
5340 layout_type (t);
5343 /* Begin compiling the definition of an enumeration type.
5344 NAME is its name (or null if anonymous).
5345 Returns the type object, as yet incomplete.
5346 Also records info about it so that build_enumerator
5347 may be used to declare the individual values as they are read. */
5349 tree
5350 start_enum (tree name)
5352 tree enumtype = 0;
5354 /* If this is the real definition for a previous forward reference,
5355 fill in the contents in the same object that used to be the
5356 forward reference. */
5358 if (name != 0)
5359 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5361 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5363 enumtype = make_node (ENUMERAL_TYPE);
5364 pushtag (name, enumtype);
5367 C_TYPE_BEING_DEFINED (enumtype) = 1;
5369 if (TYPE_VALUES (enumtype) != 0)
5371 /* This enum is a named one that has been declared already. */
5372 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5374 /* Completely replace its old definition.
5375 The old enumerators remain defined, however. */
5376 TYPE_VALUES (enumtype) = 0;
5379 enum_next_value = integer_zero_node;
5380 enum_overflow = 0;
5382 if (flag_short_enums)
5383 TYPE_PACKED (enumtype) = 1;
5385 return enumtype;
5388 /* After processing and defining all the values of an enumeration type,
5389 install their decls in the enumeration type and finish it off.
5390 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5391 and ATTRIBUTES are the specified attributes.
5392 Returns ENUMTYPE. */
5394 tree
5395 finish_enum (tree enumtype, tree values, tree attributes)
5397 tree pair, tem;
5398 tree minnode = 0, maxnode = 0;
5399 int precision, unsign;
5400 bool toplevel = (file_scope == current_scope);
5401 struct lang_type *lt;
5403 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5405 /* Calculate the maximum value of any enumerator in this type. */
5407 if (values == error_mark_node)
5408 minnode = maxnode = integer_zero_node;
5409 else
5411 minnode = maxnode = TREE_VALUE (values);
5412 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5414 tree value = TREE_VALUE (pair);
5415 if (tree_int_cst_lt (maxnode, value))
5416 maxnode = value;
5417 if (tree_int_cst_lt (value, minnode))
5418 minnode = value;
5422 /* Construct the final type of this enumeration. It is the same
5423 as one of the integral types - the narrowest one that fits, except
5424 that normally we only go as narrow as int - and signed iff any of
5425 the values are negative. */
5426 unsign = (tree_int_cst_sgn (minnode) >= 0);
5427 precision = MAX (min_precision (minnode, unsign),
5428 min_precision (maxnode, unsign));
5429 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5431 tem = c_common_type_for_size (precision, unsign);
5432 if (tem == NULL)
5434 warning ("enumeration values exceed range of largest integer");
5435 tem = long_long_integer_type_node;
5438 else
5439 tem = unsign ? unsigned_type_node : integer_type_node;
5441 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5442 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5443 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5444 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5445 TYPE_SIZE (enumtype) = 0;
5446 layout_type (enumtype);
5448 if (values != error_mark_node)
5450 /* Change the type of the enumerators to be the enum type. We
5451 need to do this irrespective of the size of the enum, for
5452 proper type checking. Replace the DECL_INITIALs of the
5453 enumerators, and the value slots of the list, with copies
5454 that have the enum type; they cannot be modified in place
5455 because they may be shared (e.g. integer_zero_node) Finally,
5456 change the purpose slots to point to the names of the decls. */
5457 for (pair = values; pair; pair = TREE_CHAIN (pair))
5459 tree enu = TREE_PURPOSE (pair);
5460 tree ini = DECL_INITIAL (enu);
5462 TREE_TYPE (enu) = enumtype;
5464 /* The ISO C Standard mandates enumerators to have type int,
5465 even though the underlying type of an enum type is
5466 unspecified. Here we convert any enumerators that fit in
5467 an int to type int, to avoid promotions to unsigned types
5468 when comparing integers with enumerators that fit in the
5469 int range. When -pedantic is given, build_enumerator()
5470 would have already taken care of those that don't fit. */
5471 if (int_fits_type_p (ini, integer_type_node))
5472 tem = integer_type_node;
5473 else
5474 tem = enumtype;
5475 ini = convert (tem, ini);
5477 DECL_INITIAL (enu) = ini;
5478 TREE_PURPOSE (pair) = DECL_NAME (enu);
5479 TREE_VALUE (pair) = ini;
5482 TYPE_VALUES (enumtype) = values;
5485 /* Record the min/max values so that we can warn about bit-field
5486 enumerations that are too small for the values. */
5487 lt = ggc_alloc_cleared (sizeof (struct lang_type));
5488 lt->enum_min = minnode;
5489 lt->enum_max = maxnode;
5490 TYPE_LANG_SPECIFIC (enumtype) = lt;
5492 /* Fix up all variant types of this enum type. */
5493 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5495 if (tem == enumtype)
5496 continue;
5497 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5498 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5499 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5500 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5501 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5502 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5503 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5504 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5505 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5506 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5507 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5510 /* Finish debugging output for this type. */
5511 rest_of_type_compilation (enumtype, toplevel);
5513 return enumtype;
5516 /* Build and install a CONST_DECL for one value of the
5517 current enumeration type (one that was begun with start_enum).
5518 Return a tree-list containing the CONST_DECL and its value.
5519 Assignment of sequential values by default is handled here. */
5521 tree
5522 build_enumerator (tree name, tree value)
5524 tree decl, type;
5526 /* Validate and default VALUE. */
5528 /* Remove no-op casts from the value. */
5529 if (value)
5530 STRIP_TYPE_NOPS (value);
5532 if (value != 0)
5534 /* Don't issue more errors for error_mark_node (i.e. an
5535 undeclared identifier) - just ignore the value expression. */
5536 if (value == error_mark_node)
5537 value = 0;
5538 else if (TREE_CODE (value) != INTEGER_CST)
5540 error ("enumerator value for '%E' is not an integer constant", name);
5541 value = 0;
5543 else
5545 value = default_conversion (value);
5546 constant_expression_warning (value);
5550 /* Default based on previous value. */
5551 /* It should no longer be possible to have NON_LVALUE_EXPR
5552 in the default. */
5553 if (value == 0)
5555 value = enum_next_value;
5556 if (enum_overflow)
5557 error ("overflow in enumeration values");
5560 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5562 pedwarn ("ISO C restricts enumerator values to range of `int'");
5563 /* XXX This causes -pedantic to change the meaning of the program.
5564 Remove? -zw 2004-03-15 */
5565 value = convert (integer_type_node, value);
5568 /* Set basis for default for next value. */
5569 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5570 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5572 /* Now create a declaration for the enum value name. */
5574 type = TREE_TYPE (value);
5575 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5576 TYPE_PRECISION (integer_type_node)),
5577 (TYPE_PRECISION (type)
5578 >= TYPE_PRECISION (integer_type_node)
5579 && TYPE_UNSIGNED (type)));
5581 decl = build_decl (CONST_DECL, name, type);
5582 DECL_INITIAL (decl) = convert (type, value);
5583 pushdecl (decl);
5585 return tree_cons (decl, value, NULL_TREE);
5589 /* Create the FUNCTION_DECL for a function definition.
5590 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5591 the declaration; they describe the function's name and the type it returns,
5592 but twisted together in a fashion that parallels the syntax of C.
5594 This function creates a binding context for the function body
5595 as well as setting up the FUNCTION_DECL in current_function_decl.
5597 Returns 1 on success. If the DECLARATOR is not suitable for a function
5598 (it defines a datum instead), we return 0, which tells
5599 yyparse to report a parse error. */
5602 start_function (tree declspecs, tree declarator, tree attributes)
5604 tree decl1, old_decl;
5605 tree restype;
5606 int old_immediate_size_expand = immediate_size_expand;
5608 current_function_returns_value = 0; /* Assume, until we see it does. */
5609 current_function_returns_null = 0;
5610 current_function_returns_abnormally = 0;
5611 warn_about_return_type = 0;
5612 current_extern_inline = 0;
5613 c_in_iteration_stmt = 0;
5614 c_in_case_stmt = 0;
5616 /* Don't expand any sizes in the return type of the function. */
5617 immediate_size_expand = 0;
5619 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5621 /* If the declarator is not suitable for a function definition,
5622 cause a syntax error. */
5623 if (decl1 == 0)
5625 immediate_size_expand = old_immediate_size_expand;
5626 return 0;
5629 decl_attributes (&decl1, attributes, 0);
5631 if (DECL_DECLARED_INLINE_P (decl1)
5632 && DECL_UNINLINABLE (decl1)
5633 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5634 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5636 announce_function (decl1);
5638 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5640 error ("return type is an incomplete type");
5641 /* Make it return void instead. */
5642 TREE_TYPE (decl1)
5643 = build_function_type (void_type_node,
5644 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5647 if (warn_about_return_type)
5648 pedwarn_c99 ("return type defaults to `int'");
5650 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5651 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5652 DECL_INITIAL (decl1) = error_mark_node;
5654 /* If this definition isn't a prototype and we had a prototype declaration
5655 before, copy the arg type info from that prototype.
5656 But not if what we had before was a builtin function. */
5657 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5658 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5659 && !DECL_BUILT_IN (old_decl)
5660 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5661 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5662 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5664 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5665 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5668 /* Optionally warn of old-fashioned def with no previous prototype. */
5669 if (warn_strict_prototypes
5670 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5671 && C_DECL_ISNT_PROTOTYPE (old_decl))
5672 warning ("function declaration isn't a prototype");
5673 /* Optionally warn of any global def with no previous prototype. */
5674 else if (warn_missing_prototypes
5675 && TREE_PUBLIC (decl1)
5676 && ! MAIN_NAME_P (DECL_NAME (decl1))
5677 && C_DECL_ISNT_PROTOTYPE (old_decl))
5678 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5679 /* Optionally warn of any def with no previous prototype
5680 if the function has already been used. */
5681 else if (warn_missing_prototypes
5682 && old_decl != 0 && TREE_USED (old_decl)
5683 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5684 warning ("%J'%D' was used with no prototype before its definition",
5685 decl1, decl1);
5686 /* Optionally warn of any global def with no previous declaration. */
5687 else if (warn_missing_declarations
5688 && TREE_PUBLIC (decl1)
5689 && old_decl == 0
5690 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5691 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5692 /* Optionally warn of any def with no previous declaration
5693 if the function has already been used. */
5694 else if (warn_missing_declarations
5695 && old_decl != 0 && TREE_USED (old_decl)
5696 && C_DECL_IMPLICIT (old_decl))
5697 warning ("%J`%D' was used with no declaration before its definition",
5698 decl1, decl1);
5700 /* This is a definition, not a reference.
5701 So normally clear DECL_EXTERNAL.
5702 However, `extern inline' acts like a declaration
5703 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5704 DECL_EXTERNAL (decl1) = current_extern_inline;
5706 /* This function exists in static storage.
5707 (This does not mean `static' in the C sense!) */
5708 TREE_STATIC (decl1) = 1;
5710 /* A nested function is not global. */
5711 if (current_function_decl != 0)
5712 TREE_PUBLIC (decl1) = 0;
5714 #ifdef ENABLE_CHECKING
5715 /* This is the earliest point at which we might know the assembler
5716 name of the function. Thus, if it's set before this, die horribly. */
5717 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5718 abort ();
5719 #endif
5721 /* If #pragma weak was used, mark the decl weak now. */
5722 if (current_scope == file_scope)
5723 maybe_apply_pragma_weak (decl1);
5725 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5726 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5728 tree args;
5729 int argct = 0;
5731 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5732 != integer_type_node)
5733 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5735 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5736 args = TREE_CHAIN (args))
5738 tree type = args ? TREE_VALUE (args) : 0;
5740 if (type == void_type_node)
5741 break;
5743 ++argct;
5744 switch (argct)
5746 case 1:
5747 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5748 pedwarn ("%Jfirst argument of '%D' should be `int'",
5749 decl1, decl1);
5750 break;
5752 case 2:
5753 if (TREE_CODE (type) != POINTER_TYPE
5754 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5755 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5756 != char_type_node))
5757 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5758 decl1, decl1);
5759 break;
5761 case 3:
5762 if (TREE_CODE (type) != POINTER_TYPE
5763 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5764 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5765 != char_type_node))
5766 pedwarn ("%Jthird argument of '%D' should probably be "
5767 "'char **'", decl1, decl1);
5768 break;
5772 /* It is intentional that this message does not mention the third
5773 argument because it's only mentioned in an appendix of the
5774 standard. */
5775 if (argct > 0 && (argct < 2 || argct > 3))
5776 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5778 if (! TREE_PUBLIC (decl1))
5779 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5782 /* Record the decl so that the function name is defined.
5783 If we already have a decl for this name, and it is a FUNCTION_DECL,
5784 use the old decl. */
5786 current_function_decl = pushdecl (decl1);
5788 push_scope ();
5789 declare_parm_level ();
5791 make_decl_rtl (current_function_decl, NULL);
5793 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5794 /* Promote the value to int before returning it. */
5795 if (c_promoting_integer_type_p (restype))
5797 /* It retains unsignedness if not really getting wider. */
5798 if (TYPE_UNSIGNED (restype)
5799 && (TYPE_PRECISION (restype)
5800 == TYPE_PRECISION (integer_type_node)))
5801 restype = unsigned_type_node;
5802 else
5803 restype = integer_type_node;
5805 DECL_RESULT (current_function_decl)
5806 = build_decl (RESULT_DECL, NULL_TREE, restype);
5808 /* If this fcn was already referenced via a block-scope `extern' decl
5809 (or an implicit decl), propagate certain information about the usage. */
5810 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5811 TREE_ADDRESSABLE (current_function_decl) = 1;
5813 immediate_size_expand = old_immediate_size_expand;
5815 start_fname_decls ();
5817 return 1;
5820 /* Subroutine of store_parm_decls which handles new-style function
5821 definitions (prototype format). The parms already have decls, so we
5822 need only record them as in effect and complain if any redundant
5823 old-style parm decls were written. */
5824 static void
5825 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5827 tree decl;
5828 tree parms = ARG_INFO_PARMS (arg_info);
5829 tree tags = ARG_INFO_TAGS (arg_info);
5830 tree others = ARG_INFO_OTHERS (arg_info);
5832 if (current_scope->bindings)
5834 error ("%Jold-style parameter declarations in prototyped "
5835 "function definition", fndecl);
5837 /* Get rid of the old-style declarations. */
5838 pop_scope ();
5839 push_scope ();
5841 /* Don't issue this warning for nested functions, and don't issue this
5842 warning if we got here because ARG_INFO_TYPES was error_mark_node
5843 (this happens when a function definition has just an ellipsis in
5844 its parameter list). */
5845 else if (warn_traditional && !in_system_header && !current_function_scope
5846 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5847 warning ("%Jtraditional C rejects ISO C style function definitions",
5848 fndecl);
5850 /* Now make all the parameter declarations visible in the function body.
5851 We can bypass most of the grunt work of pushdecl. */
5852 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5854 DECL_CONTEXT (decl) = current_function_decl;
5855 if (DECL_NAME (decl))
5856 bind (DECL_NAME (decl), decl, current_scope);
5857 else
5858 error ("%Jparameter name omitted", decl);
5861 /* Record the parameter list in the function declaration. */
5862 DECL_ARGUMENTS (fndecl) = parms;
5864 /* Now make all the ancillary declarations visible, likewise. */
5865 for (decl = others; decl; decl = TREE_CHAIN (decl))
5867 DECL_CONTEXT (decl) = current_function_decl;
5868 if (DECL_NAME (decl))
5869 bind (DECL_NAME (decl), decl, current_scope);
5872 /* And all the tag declarations. */
5873 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5874 if (TREE_PURPOSE (decl))
5875 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope);
5878 /* Subroutine of store_parm_decls which handles old-style function
5879 definitions (separate parameter list and declarations). */
5881 static void
5882 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5884 struct c_binding *b;
5885 tree parm, decl, last;
5886 tree parmids = ARG_INFO_PARMS (arg_info);
5888 /* We use DECL_WEAK as a flag to show which parameters have been
5889 seen already, since it is not used on PARM_DECL. */
5890 #ifdef ENABLE_CHECKING
5891 for (b = current_scope->bindings; b; b = b->prev)
5892 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5893 abort ();
5894 #endif
5896 if (warn_old_style_definition && !in_system_header)
5897 warning ("%Jold-style function definition", fndecl);
5899 /* Match each formal parameter name with its declaration. Save each
5900 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5901 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5903 if (TREE_VALUE (parm) == 0)
5905 error ("%Jparameter name missing from parameter list", fndecl);
5906 TREE_PURPOSE (parm) = 0;
5907 continue;
5910 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5911 if (b && b->contour == current_scope)
5913 decl = b->decl;
5914 /* If we got something other than a PARM_DECL it is an error. */
5915 if (TREE_CODE (decl) != PARM_DECL)
5916 error ("%J'%D' declared as a non-parameter", decl, decl);
5917 /* If the declaration is already marked, we have a duplicate
5918 name. Complain and ignore the duplicate. */
5919 else if (DECL_WEAK (decl))
5921 error ("%Jmultiple parameters named '%D'", decl, decl);
5922 TREE_PURPOSE (parm) = 0;
5923 continue;
5925 /* If the declaration says "void", complain and turn it into
5926 an int. */
5927 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5929 error ("%Jparameter '%D' declared with void type", decl, decl);
5930 TREE_TYPE (decl) = integer_type_node;
5931 DECL_ARG_TYPE (decl) = integer_type_node;
5932 layout_decl (decl, 0);
5935 /* If no declaration found, default to int. */
5936 else
5938 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5939 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5940 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5941 pushdecl (decl);
5943 if (flag_isoc99)
5944 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5945 else if (extra_warnings)
5946 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5949 TREE_PURPOSE (parm) = decl;
5950 DECL_WEAK (decl) = 1;
5953 /* Now examine the parms chain for incomplete declarations
5954 and declarations with no corresponding names. */
5956 for (b = current_scope->bindings; b; b = b->prev)
5958 parm = b->decl;
5959 if (TREE_CODE (parm) != PARM_DECL)
5960 continue;
5962 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5964 error ("%Jparameter '%D' has incomplete type", parm, parm);
5965 TREE_TYPE (parm) = error_mark_node;
5968 if (! DECL_WEAK (parm))
5970 error ("%Jdeclaration for parameter '%D' but no such parameter",
5971 parm, parm);
5973 /* Pretend the parameter was not missing.
5974 This gets us to a standard state and minimizes
5975 further error messages. */
5976 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5980 /* Chain the declarations together in the order of the list of
5981 names. Store that chain in the function decl, replacing the
5982 list of names. Update the current scope to match. */
5983 DECL_ARGUMENTS (fndecl) = 0;
5985 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5986 if (TREE_PURPOSE (parm))
5987 break;
5988 if (parm && TREE_PURPOSE (parm))
5990 last = TREE_PURPOSE (parm);
5991 DECL_ARGUMENTS (fndecl) = last;
5992 DECL_WEAK (last) = 0;
5994 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5995 if (TREE_PURPOSE (parm))
5997 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5998 last = TREE_PURPOSE (parm);
5999 DECL_WEAK (last) = 0;
6001 TREE_CHAIN (last) = 0;
6004 /* If there was a previous prototype,
6005 set the DECL_ARG_TYPE of each argument according to
6006 the type previously specified, and report any mismatches. */
6008 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6010 tree type;
6011 for (parm = DECL_ARGUMENTS (fndecl),
6012 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6013 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6014 != void_type_node));
6015 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6017 if (parm == 0 || type == 0
6018 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6020 error ("number of arguments doesn't match prototype");
6021 error ("%Hprototype declaration",
6022 &current_function_prototype_locus);
6023 break;
6025 /* Type for passing arg must be consistent with that
6026 declared for the arg. ISO C says we take the unqualified
6027 type for parameters declared with qualified type. */
6028 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6029 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6030 COMPARE_STRICT))
6032 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6033 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6035 /* Adjust argument to match prototype. E.g. a previous
6036 `int foo(float);' prototype causes
6037 `int foo(x) float x; {...}' to be treated like
6038 `int foo(float x) {...}'. This is particularly
6039 useful for argument types like uid_t. */
6040 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6042 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6043 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6044 && TYPE_PRECISION (TREE_TYPE (parm))
6045 < TYPE_PRECISION (integer_type_node))
6046 DECL_ARG_TYPE (parm) = integer_type_node;
6048 if (pedantic)
6050 pedwarn ("promoted argument '%D' "
6051 "doesn't match prototype", parm);
6052 pedwarn ("%Hprototype declaration",
6053 &current_function_prototype_locus);
6056 else
6058 error ("argument '%D' doesn't match prototype", parm);
6059 error ("%Hprototype declaration",
6060 &current_function_prototype_locus);
6064 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6067 /* Otherwise, create a prototype that would match. */
6069 else
6071 tree actual = 0, last = 0, type;
6073 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6075 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6076 if (last)
6077 TREE_CHAIN (last) = type;
6078 else
6079 actual = type;
6080 last = type;
6082 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6083 if (last)
6084 TREE_CHAIN (last) = type;
6085 else
6086 actual = type;
6088 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6089 of the type of this function, but we need to avoid having this
6090 affect the types of other similarly-typed functions, so we must
6091 first force the generation of an identical (but separate) type
6092 node for the relevant function type. The new node we create
6093 will be a variant of the main variant of the original function
6094 type. */
6096 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6098 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6102 /* A subroutine of store_parm_decls called via walk_tree. Mark all
6103 decls non-local. */
6105 static tree
6106 set_decl_nonlocal (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
6108 tree t = *tp;
6110 if (DECL_P (t))
6112 DECL_NONLOCAL (t) = 1;
6113 *walk_subtrees = 0;
6115 else if (TYPE_P (t))
6116 *walk_subtrees = 0;
6118 return NULL;
6121 /* Store the parameter declarations into the current function declaration.
6122 This is called after parsing the parameter declarations, before
6123 digesting the body of the function.
6125 For an old-style definition, construct a prototype out of the old-style
6126 parameter declarations and inject it into the function's type. */
6128 void
6129 store_parm_decls (void)
6131 tree fndecl = current_function_decl;
6133 /* The function containing FNDECL, if any. */
6134 tree context = decl_function_context (fndecl);
6136 /* The argument information block for FNDECL. */
6137 tree arg_info = DECL_ARGUMENTS (fndecl);
6139 /* True if this definition is written with a prototype. Note:
6140 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6141 list in a function definition as equivalent to (void) -- an
6142 empty argument list specifies the function has no parameters,
6143 but only (void) sets up a prototype for future calls. */
6144 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6146 if (proto)
6147 store_parm_decls_newstyle (fndecl, arg_info);
6148 else
6149 store_parm_decls_oldstyle (fndecl, arg_info);
6151 /* The next call to push_scope will be a function body. */
6153 next_is_function_body = true;
6155 /* Write a record describing this function definition to the prototypes
6156 file (if requested). */
6158 gen_aux_info_record (fndecl, 1, 0, proto);
6160 /* Initialize the RTL code for the function. */
6161 allocate_struct_function (fndecl);
6163 /* Begin the statement tree for this function. */
6164 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6166 /* If this is a nested function, save away the sizes of any
6167 variable-size types so that we can expand them when generating
6168 RTL. */
6169 if (context)
6171 tree t;
6173 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6174 = nreverse (get_pending_sizes ());
6175 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6177 t = TREE_CHAIN (t))
6179 /* We will have a nonlocal use of whatever variables are
6180 buried inside here. */
6181 walk_tree (&TREE_OPERAND (TREE_VALUE (t), 0),
6182 set_decl_nonlocal, NULL, NULL);
6184 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6188 /* This function is being processed in whole-function mode. */
6189 cfun->x_whole_function_mode_p = 1;
6191 /* Even though we're inside a function body, we still don't want to
6192 call expand_expr to calculate the size of a variable-sized array.
6193 We haven't necessarily assigned RTL to all variables yet, so it's
6194 not safe to try to expand expressions involving them. */
6195 immediate_size_expand = 0;
6196 cfun->x_dont_save_pending_sizes_p = 1;
6199 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6201 static void
6202 c_finalize (tree fndecl)
6204 struct cgraph_node *cgn;
6206 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6207 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6209 /* ??? Objc emits functions after finalizing the compilation unit.
6210 This should be cleaned up later and this conditional removed. */
6211 if (cgraph_global_info_ready)
6213 c_expand_body (fndecl);
6214 return;
6217 /* Finalize all nested functions now. */
6218 cgn = cgraph_node (fndecl);
6219 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6220 c_finalize (cgn->decl);
6222 cgraph_finalize_function (fndecl, false);
6225 /* Finish up a function declaration and compile that function
6226 all the way to assembler language output. The free the storage
6227 for the function definition.
6229 This is called after parsing the body of the function definition. */
6231 void
6232 finish_function (void)
6234 tree fndecl = current_function_decl;
6236 /* When a function declaration is totally empty, e.g.
6237 void foo(void) { }
6238 (the argument list is irrelevant) the compstmt rule will not
6239 bother calling push_scope/pop_scope, which means we get here with
6240 the scope stack out of sync. Detect this situation by noticing
6241 that current_scope is still as store_parm_decls left it, and do
6242 a dummy push/pop to get back to consistency.
6243 Note that the call to push_scope does not actually push another
6244 scope - see there for details. */
6246 if (current_scope->parm_flag && next_is_function_body)
6248 push_scope ();
6249 pop_scope ();
6252 if (TREE_CODE (fndecl) == FUNCTION_DECL
6253 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6255 tree args = DECL_ARGUMENTS (fndecl);
6256 for (; args; args = TREE_CHAIN (args))
6258 tree type = TREE_TYPE (args);
6259 if (INTEGRAL_TYPE_P (type)
6260 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6261 DECL_ARG_TYPE (args) = integer_type_node;
6265 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6266 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6268 /* Must mark the RESULT_DECL as being in this function. */
6270 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6271 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6273 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6275 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6276 != integer_type_node)
6278 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6279 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6280 if (!warn_main)
6281 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6283 else
6285 #ifdef DEFAULT_MAIN_RETURN
6286 /* Make it so that `main' always returns success by default. */
6287 DEFAULT_MAIN_RETURN;
6288 #else
6289 if (flag_isoc99)
6290 c_expand_return (integer_zero_node);
6291 #endif
6295 finish_fname_decls ();
6297 /* Tie off the statement tree for this function. */
6298 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6300 /* Complain if there's just no return statement. */
6301 if (warn_return_type
6302 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6303 && !current_function_returns_value && !current_function_returns_null
6304 /* Don't complain if we abort. */
6305 && !current_function_returns_abnormally
6306 /* Don't warn for main(). */
6307 && !MAIN_NAME_P (DECL_NAME (fndecl))
6308 /* Or if they didn't actually specify a return type. */
6309 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6310 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6311 inline function, as we might never be compiled separately. */
6312 && DECL_INLINE (fndecl))
6313 warning ("no return statement in function returning non-void");
6315 /* With just -Wextra, complain only if function returns both with
6316 and without a value. */
6317 if (extra_warnings
6318 && current_function_returns_value
6319 && current_function_returns_null)
6320 warning ("this function may return with or without a value");
6322 /* Store the end of the function, so that we get good line number
6323 info for the epilogue. */
6324 cfun->function_end_locus = input_location;
6326 /* If we don't have ctors/dtors sections, and this is a static
6327 constructor or destructor, it must be recorded now. */
6328 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6329 && !targetm.have_ctors_dtors)
6330 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6331 if (DECL_STATIC_DESTRUCTOR (fndecl)
6332 && !targetm.have_ctors_dtors)
6333 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6335 /* Genericize before inlining. Delay genericizing nested functions
6336 until their parent function is genericized. Since finalizing
6337 requires GENERIC, delay that as well. */
6339 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6341 if (!decl_function_context (fndecl))
6343 c_genericize (fndecl);
6344 lower_nested_functions (fndecl);
6345 c_finalize (fndecl);
6347 else
6349 /* Register this function with cgraph just far enough to get it
6350 added to our parent's nested function list. Handy, since the
6351 C front end doesn't have such a list. */
6352 (void) cgraph_node (fndecl);
6356 /* We're leaving the context of this function, so zap cfun.
6357 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6358 tree_rest_of_compilation. */
6359 cfun = NULL;
6360 current_function_decl = NULL;
6363 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6364 then we are already in the process of generating RTL for another
6365 function. */
6367 static void
6368 c_expand_body_1 (tree fndecl, int nested_p)
6370 if (nested_p)
6372 /* Make sure that we will evaluate variable-sized types involved
6373 in our function's type. */
6374 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6376 /* Squirrel away our current state. */
6377 push_function_context ();
6380 tree_rest_of_compilation (fndecl, nested_p);
6382 if (nested_p)
6383 /* Return to the enclosing function. */
6384 pop_function_context ();
6386 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6387 && targetm.have_ctors_dtors)
6388 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6389 DEFAULT_INIT_PRIORITY);
6390 if (DECL_STATIC_DESTRUCTOR (fndecl)
6391 && targetm.have_ctors_dtors)
6392 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6393 DEFAULT_INIT_PRIORITY);
6396 /* Like c_expand_body_1 but only for unnested functions. */
6398 void
6399 c_expand_body (tree fndecl)
6402 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6403 c_expand_body_1 (fndecl, 0);
6406 /* Check the declarations given in a for-loop for satisfying the C99
6407 constraints. */
6408 void
6409 check_for_loop_decls (void)
6411 struct c_binding *b;
6413 if (!flag_isoc99)
6415 /* If we get here, declarations have been used in a for loop without
6416 the C99 for loop scope. This doesn't make much sense, so don't
6417 allow it. */
6418 error ("'for' loop initial declaration used outside C99 mode");
6419 return;
6421 /* C99 subclause 6.8.5 paragraph 3:
6423 [#3] The declaration part of a for statement shall only
6424 declare identifiers for objects having storage class auto or
6425 register.
6427 It isn't clear whether, in this sentence, "identifiers" binds to
6428 "shall only declare" or to "objects" - that is, whether all identifiers
6429 declared must be identifiers for objects, or whether the restriction
6430 only applies to those that are. (A question on this in comp.std.c
6431 in November 2000 received no answer.) We implement the strictest
6432 interpretation, to avoid creating an extension which later causes
6433 problems. */
6435 for (b = current_scope->bindings; b; b = b->prev)
6437 tree id = b->id;
6438 tree decl = b->decl;
6440 if (!id)
6441 continue;
6443 switch (TREE_CODE (decl))
6445 case VAR_DECL:
6446 if (TREE_STATIC (decl))
6447 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6448 "initial declaration", decl, decl);
6449 else if (DECL_EXTERNAL (decl))
6450 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6451 "initial declaration", decl, decl);
6452 break;
6454 case RECORD_TYPE:
6455 error ("'struct %E' declared in 'for' loop initial declaration", id);
6456 break;
6457 case UNION_TYPE:
6458 error ("'union %E' declared in 'for' loop initial declaration", id);
6459 break;
6460 case ENUMERAL_TYPE:
6461 error ("'enum %E' declared in 'for' loop initial declaration", id);
6462 break;
6463 default:
6464 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6465 "initial declaration", decl, decl);
6470 /* Save and reinitialize the variables
6471 used during compilation of a C function. */
6473 void
6474 c_push_function_context (struct function *f)
6476 struct language_function *p;
6477 p = ggc_alloc (sizeof (struct language_function));
6478 f->language = p;
6480 p->base.x_stmt_tree = c_stmt_tree;
6481 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6482 p->x_in_iteration_stmt = c_in_iteration_stmt;
6483 p->x_in_case_stmt = c_in_case_stmt;
6484 p->returns_value = current_function_returns_value;
6485 p->returns_null = current_function_returns_null;
6486 p->returns_abnormally = current_function_returns_abnormally;
6487 p->warn_about_return_type = warn_about_return_type;
6488 p->extern_inline = current_extern_inline;
6491 /* Restore the variables used during compilation of a C function. */
6493 void
6494 c_pop_function_context (struct function *f)
6496 struct language_function *p = f->language;
6498 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6499 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6501 /* Stop pointing to the local nodes about to be freed. */
6502 /* But DECL_INITIAL must remain nonzero so we know this
6503 was an actual function definition. */
6504 DECL_INITIAL (current_function_decl) = error_mark_node;
6505 DECL_ARGUMENTS (current_function_decl) = 0;
6508 c_stmt_tree = p->base.x_stmt_tree;
6509 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6510 c_in_iteration_stmt = p->x_in_iteration_stmt;
6511 c_in_case_stmt = p->x_in_case_stmt;
6512 current_function_returns_value = p->returns_value;
6513 current_function_returns_null = p->returns_null;
6514 current_function_returns_abnormally = p->returns_abnormally;
6515 warn_about_return_type = p->warn_about_return_type;
6516 current_extern_inline = p->extern_inline;
6518 f->language = NULL;
6521 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6523 void
6524 c_dup_lang_specific_decl (tree decl)
6526 struct lang_decl *ld;
6528 if (!DECL_LANG_SPECIFIC (decl))
6529 return;
6531 ld = ggc_alloc (sizeof (struct lang_decl));
6532 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6533 DECL_LANG_SPECIFIC (decl) = ld;
6536 /* The functions below are required for functionality of doing
6537 function at once processing in the C front end. Currently these
6538 functions are not called from anywhere in the C front end, but as
6539 these changes continue, that will change. */
6541 /* Returns nonzero if the current statement is a full expression,
6542 i.e. temporaries created during that statement should be destroyed
6543 at the end of the statement. */
6546 stmts_are_full_exprs_p (void)
6548 return 0;
6551 /* Returns the stmt_tree (if any) to which statements are currently
6552 being added. If there is no active statement-tree, NULL is
6553 returned. */
6555 stmt_tree
6556 current_stmt_tree (void)
6558 return &c_stmt_tree;
6561 /* Returns the stack of SCOPE_STMTs for the current function. */
6563 tree *
6564 current_scope_stmt_stack (void)
6566 return &c_scope_stmt_stack;
6569 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6570 C. */
6573 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6575 return 0;
6578 /* Dummy function in place of callback used by C++. */
6580 void
6581 extract_interface_info (void)
6585 /* Return a new COMPOUND_STMT, after adding it to the current
6586 statement tree. */
6588 tree
6589 c_begin_compound_stmt (void)
6591 tree stmt;
6593 /* Create the COMPOUND_STMT. */
6594 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6596 return stmt;
6599 /* Expand DECL if it declares an entity not handled by the
6600 common code. */
6603 c_expand_decl (tree decl)
6605 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
6607 /* Let the back-end know about this variable. */
6608 if (!anon_aggr_type_p (TREE_TYPE (decl)))
6609 emit_local_var (decl);
6610 else
6611 expand_anon_union_decl (decl, NULL_TREE,
6612 DECL_ANON_UNION_ELEMS (decl));
6614 else if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
6615 make_rtl_for_local_static (decl);
6616 /* Expand nested functions. */
6617 else if (TREE_CODE (decl) == FUNCTION_DECL
6618 && DECL_CONTEXT (decl) == current_function_decl
6619 && DECL_SAVED_TREE (decl))
6620 c_expand_body_1 (decl, 1);
6621 else
6622 return 0;
6624 return 1;
6627 /* Return the global value of T as a symbol. */
6629 tree
6630 identifier_global_value (tree t)
6632 struct c_binding *b;
6634 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6635 if (b->contour == file_scope || b->contour == external_scope)
6636 return b->decl;
6638 return 0;
6641 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6642 otherwise the name is found in ridpointers from RID_INDEX. */
6644 void
6645 record_builtin_type (enum rid rid_index, const char *name, tree type)
6647 tree id;
6648 if (name == 0)
6649 id = ridpointers[(int) rid_index];
6650 else
6651 id = get_identifier (name);
6652 pushdecl (build_decl (TYPE_DECL, id, type));
6655 /* Build the void_list_node (void_type_node having been created). */
6656 tree
6657 build_void_list_node (void)
6659 tree t = build_tree_list (NULL_TREE, void_type_node);
6660 return t;
6663 /* Return something to represent absolute declarators containing a *.
6664 TARGET is the absolute declarator that the * contains.
6665 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6666 to apply to the pointer type, represented as identifiers, possible mixed
6667 with attributes.
6669 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6670 if attributes are present) and whose type is the modifier list. */
6672 tree
6673 make_pointer_declarator (tree type_quals_attrs, tree target)
6675 tree quals, attrs;
6676 tree itarget = target;
6677 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6678 if (attrs != NULL_TREE)
6679 itarget = tree_cons (attrs, target, NULL_TREE);
6680 return build1 (INDIRECT_REF, quals, itarget);
6683 /* A wrapper around lhd_set_decl_assembler_name that gives static
6684 variables their C names if they are at file scope and only one
6685 translation unit is being compiled, for backwards compatibility
6686 with certain bizarre assembler hacks (like crtstuff.c). */
6688 void
6689 c_static_assembler_name (tree decl)
6691 if (num_in_fnames == 1
6692 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6693 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6694 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6695 else
6696 lhd_set_decl_assembler_name (decl);
6699 /* Perform final processing on file-scope data. */
6700 static void
6701 c_write_global_declarations_1 (tree globals)
6703 size_t len = list_length (globals);
6704 tree *vec = xmalloc (sizeof (tree) * len);
6705 size_t i;
6706 tree decl;
6708 /* Process the decls in the order they were written. */
6709 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6710 vec[i] = decl;
6712 wrapup_global_declarations (vec, len);
6713 check_global_declarations (vec, len);
6715 free (vec);
6718 void
6719 c_write_global_declarations (void)
6721 tree t;
6723 /* We don't want to do this if generating a PCH. */
6724 if (pch_file)
6725 return;
6727 /* Process all file scopes in this compilation. */
6728 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6729 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6731 /* Now do the same for the externals scope. */
6732 t = pop_scope ();
6733 if (t)
6734 c_write_global_declarations_1 (BLOCK_VARS (t));
6737 #include "gt-c-decl.h"