* tree.h (SET_ARRAY_OR_VECTOR_CHECK): Rename to SET_OR_ARRAY_CHECK
[official-gcc.git] / gcc / c-decl.c
blobecf1198f5c52deaf408e90df05dbd632937d93a0
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "cgraph.h"
53 #include "hashtab.h"
54 #include "libfuncs.h"
55 #include "except.h"
56 #include "langhooks-def.h"
58 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
59 enum decl_context
60 { NORMAL, /* Ordinary declaration */
61 FUNCDEF, /* Function definition */
62 PARM, /* Declaration of parm before function body */
63 FIELD, /* Declaration inside struct or union */
64 TYPENAME}; /* Typename (inside cast or sizeof) */
67 /* Nonzero if we have seen an invalid cross reference
68 to a struct, union, or enum, but not yet printed the message. */
70 tree pending_invalid_xref;
71 /* File and line to appear in the eventual error message. */
72 location_t pending_invalid_xref_location;
74 /* While defining an enum type, this is 1 plus the last enumerator
75 constant value. Note that will do not have to save this or `enum_overflow'
76 around nested function definition since such a definition could only
77 occur in an enum value expression and we don't use these variables in
78 that case. */
80 static tree enum_next_value;
82 /* Nonzero means that there was overflow computing enum_next_value. */
84 static int enum_overflow;
86 /* These #defines are for clarity in working with the information block
87 returned by get_parm_info. */
88 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
89 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
90 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
91 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
93 /* The file and line that the prototype came from if this is an
94 old-style definition; used for diagnostics in
95 store_parm_decls_oldstyle. */
97 static location_t current_function_prototype_locus;
99 /* The current statement tree. */
101 static GTY(()) struct stmt_tree_s c_stmt_tree;
103 /* The current scope statement stack. */
105 static GTY(()) tree c_scope_stmt_stack;
107 /* State saving variables. */
108 int c_in_iteration_stmt;
109 int c_in_case_stmt;
111 /* A DECL for the current file-scope context. */
113 static GTY(()) tree current_file_decl;
115 /* A list of decls to be made automatically visible in each file scope. */
116 static GTY(()) tree visible_builtins;
118 /* Set to 0 at beginning of a function definition, set to 1 if
119 a return statement that specifies a return value is seen. */
121 int current_function_returns_value;
123 /* Set to 0 at beginning of a function definition, set to 1 if
124 a return statement with no argument is seen. */
126 int current_function_returns_null;
128 /* Set to 0 at beginning of a function definition, set to 1 if
129 a call to a noreturn function is seen. */
131 int current_function_returns_abnormally;
133 /* Set to nonzero by `grokdeclarator' for a function
134 whose return type is defaulted, if warnings for this are desired. */
136 static int warn_about_return_type;
138 /* Nonzero when starting a function declared `extern inline'. */
140 static int current_extern_inline;
142 /* True means global_bindings_p should return false even if the scope stack
143 says we are in file scope. */
144 bool c_override_global_bindings_to_false;
147 /* Each c_binding structure describes one binding of an identifier to
148 a decl. All the decls in a scope - irrespective of namespace - are
149 chained together by the ->prev field, which (as the name implies)
150 runs in reverse order. All the decls in a given namespace bound to
151 a given identifier are chained by the ->shadowed field, which runs
152 from inner to outer scopes. Finally, the ->contour field points
153 back to the relevant scope structure; this is mainly used to make
154 decls in the externals scope invisible (see below).
156 The ->decl field usually points to a DECL node, but there are two
157 exceptions. In the namespace of type tags, the bound entity is a
158 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
159 identifier is encountered, it is bound to error_mark_node to
160 suppress further errors about that identifier in the current
161 function. */
163 struct c_binding GTY((chain_next ("%h.prev")))
165 tree decl; /* the decl bound */
166 tree id; /* the identifier it's bound to */
167 struct c_binding *prev; /* the previous decl in this scope */
168 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
169 struct c_scope *contour; /* the scope in which this decl is bound */
172 #define I_SYMBOL_BINDING(node) \
173 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
174 #define I_SYMBOL_DECL(node) \
175 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
177 #define I_TAG_BINDING(node) \
178 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
179 #define I_TAG_DECL(node) \
180 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
182 #define I_LABEL_BINDING(node) \
183 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
184 #define I_LABEL_DECL(node) \
185 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
187 /* Each C symbol points to three linked lists of c_binding structures.
188 These describe the values of the identifier in the three different
189 namespaces defined by the language. */
191 struct lang_identifier GTY(())
193 struct c_common_identifier common_id;
194 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
195 struct c_binding *tag_binding; /* struct/union/enum tags */
196 struct c_binding *label_binding; /* labels */
199 /* Validate c-lang.c's assumptions. */
200 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
201 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
203 /* The resulting tree type. */
205 union lang_tree_node
206 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
207 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)")))
209 union tree_node GTY ((tag ("0"),
210 desc ("tree_node_structure (&%h)")))
211 generic;
212 struct lang_identifier GTY ((tag ("1"))) identifier;
215 /* Each c_scope structure describes the complete contents of one
216 scope. Four scopes are distinguished specially: the innermost or
217 current scope, the innermost function scope, the file scope (always
218 the second to outermost) and the outermost or external scope.
220 Most declarations are recorded in the current scope.
222 All normal label declarations are recorded in the innermost
223 function scope, as are bindings of undeclared identifiers to
224 error_mark_node. (GCC permits nested functions as an extension,
225 hence the 'innermost' qualifier.) Explicitly declared labels
226 (using the __label__ extension) appear in the current scope.
228 Being in the file scope (current_scope == file_scope) causes
229 special behavior in several places below. Also, under some
230 conditions the Objective-C front end records declarations in the
231 file scope even though that isn't the current scope.
233 All declarations with external linkage are recorded in the external
234 scope, even if they aren't visible there; this models the fact that
235 such declarations are visible to the entire program, and (with a
236 bit of cleverness, see pushdecl) allows diagnosis of some violations
237 of C99 6.2.2p7 and 6.2.7p2:
239 If, within the same translation unit, the same identifier appears
240 with both internal and external linkage, the behavior is
241 undefined.
243 All declarations that refer to the same object or function shall
244 have compatible type; otherwise, the behavior is undefined.
246 Initially only the built-in declarations, which describe compiler
247 intrinsic functions plus a subset of the standard library, are in
248 this scope.
250 The order of the blocks list matters, and it is frequently appended
251 to. To avoid having to walk all the way to the end of the list on
252 each insertion, or reverse the list later, we maintain a pointer to
253 the last list entry. (FIXME: It should be feasible to use a reversed
254 list here.)
256 The bindings list is strictly in reverse order of declarations;
257 pop_scope relies on this. */
260 struct c_scope GTY((chain_next ("%h.outer")))
262 /* The scope containing this one. */
263 struct c_scope *outer;
265 /* The next outermost function scope. */
266 struct c_scope *outer_function;
268 /* All bindings in this scope. */
269 struct c_binding *bindings;
271 /* For each scope (except the global one), a chain of BLOCK nodes
272 for all the scopes that were entered and exited one level down. */
273 tree blocks;
274 tree blocks_last;
276 /* The depth of this scope. Used to keep the ->shadowed chain of
277 bindings sorted innermost to outermost. */
278 unsigned int depth : 28;
280 /* True if we are currently filling this scope with parameter
281 declarations. */
282 BOOL_BITFIELD parm_flag : 1;
284 /* True if we already complained about forward parameter decls
285 in this scope. This prevents double warnings on
286 foo (int a; int b; ...) */
287 BOOL_BITFIELD warned_forward_parm_decls : 1;
289 /* True if this is the outermost block scope of a function body.
290 This scope contains the parameters, the local variables declared
291 in the outermost block, and all the labels (except those in
292 nested functions, or declared at block scope with __label__). */
293 BOOL_BITFIELD function_body : 1;
295 /* True means make a BLOCK for this scope no matter what. */
296 BOOL_BITFIELD keep : 1;
299 /* The scope currently in effect. */
301 static GTY(()) struct c_scope *current_scope;
303 /* The innermost function scope. Ordinary (not explicitly declared)
304 labels, bindings to error_mark_node, and the lazily-created
305 bindings of __func__ and its friends get this scope. */
307 static GTY(()) struct c_scope *current_function_scope;
309 /* The C file scope. This is reset for each input translation unit. */
311 static GTY(()) struct c_scope *file_scope;
313 /* The outermost scope. This is used for all declarations with
314 external linkage, and only these, hence the name. */
316 static GTY(()) struct c_scope *external_scope;
318 /* A chain of c_scope structures awaiting reuse. */
320 static GTY((deletable)) struct c_scope *scope_freelist;
322 /* A chain of c_binding structures awaiting reuse. */
324 static GTY((deletable)) struct c_binding *binding_freelist;
326 /* Append VAR to LIST in scope SCOPE. */
327 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
328 struct c_scope *s_ = (scope); \
329 tree d_ = (decl); \
330 if (s_->list##_last) \
331 TREE_CHAIN (s_->list##_last) = d_; \
332 else \
333 s_->list = d_; \
334 s_->list##_last = d_; \
335 } while (0)
337 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
338 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
339 struct c_scope *t_ = (tscope); \
340 struct c_scope *f_ = (fscope); \
341 if (t_->to##_last) \
342 TREE_CHAIN (t_->to##_last) = f_->from; \
343 else \
344 t_->to = f_->from; \
345 t_->to##_last = f_->from##_last; \
346 } while (0)
348 /* True means unconditionally make a BLOCK for the next scope pushed. */
350 static bool keep_next_level_flag;
352 /* True means the next call to push_scope will be the outermost scope
353 of a function body, so do not push a new scope, merely cease
354 expecting parameter decls. */
356 static bool next_is_function_body;
358 /* Functions called automatically at the beginning and end of execution. */
360 tree static_ctors, static_dtors;
362 /* Forward declarations. */
363 static tree lookup_name_in_scope (tree, struct c_scope *);
364 static tree c_make_fname_decl (tree, int);
365 static tree grokdeclarator (tree, tree, enum decl_context, int, tree *);
366 static tree grokparms (tree, int);
367 static void layout_array_type (tree);
369 /* States indicating how grokdeclarator() should handle declspecs marked
370 with __attribute__((deprecated)). An object declared as
371 __attribute__((deprecated)) suppresses warnings of uses of other
372 deprecated items. */
374 enum deprecated_states {
375 DEPRECATED_NORMAL,
376 DEPRECATED_SUPPRESS
379 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
381 void
382 c_print_identifier (FILE *file, tree node, int indent)
384 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
385 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
386 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
387 if (C_IS_RESERVED_WORD (node))
389 tree rid = ridpointers[C_RID_CODE (node)];
390 indent_to (file, indent + 4);
391 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
392 (void *) rid, IDENTIFIER_POINTER (rid));
396 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
397 which may be any of several kinds of DECL or TYPE or error_mark_node,
398 in the scope SCOPE. */
399 static void
400 bind (tree name, tree decl, struct c_scope *scope)
402 struct c_binding *b, **here;
404 if (binding_freelist)
406 b = binding_freelist;
407 binding_freelist = b->prev;
409 else
410 b = ggc_alloc (sizeof (struct c_binding));
412 b->shadowed = 0;
413 b->decl = decl;
414 b->id = name;
415 b->contour = scope;
417 b->prev = scope->bindings;
418 scope->bindings = b;
420 if (!name)
421 return;
423 switch (TREE_CODE (decl))
425 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
426 case ENUMERAL_TYPE:
427 case UNION_TYPE:
428 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
429 case VAR_DECL:
430 case FUNCTION_DECL:
431 case TYPE_DECL:
432 case CONST_DECL:
433 case PARM_DECL:
434 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
436 default:
437 abort ();
440 /* Locate the appropriate place in the chain of shadowed decls
441 to insert this binding. Normally, scope == current_scope and
442 this does nothing. */
443 while (*here && (*here)->contour->depth > scope->depth)
444 here = &(*here)->shadowed;
446 b->shadowed = *here;
447 *here = b;
450 /* Clear the binding structure B, stick it on the binding_freelist,
451 and return the former value of b->prev. This is used by pop_scope
452 and get_parm_info to iterate destructively over all the bindings
453 from a given scope. */
454 static struct c_binding *
455 free_binding_and_advance (struct c_binding *b)
457 struct c_binding *prev = b->prev;
459 b->id = 0;
460 b->decl = 0;
461 b->contour = 0;
462 b->shadowed = 0;
463 b->prev = binding_freelist;
464 binding_freelist = b;
466 return prev;
470 /* Hook called at end of compilation to assume 1 elt
471 for a file-scope tentative array defn that wasn't complete before. */
473 void
474 c_finish_incomplete_decl (tree decl)
476 if (TREE_CODE (decl) == VAR_DECL)
478 tree type = TREE_TYPE (decl);
479 if (type != error_mark_node
480 && TREE_CODE (type) == ARRAY_TYPE
481 && ! DECL_EXTERNAL (decl)
482 && TYPE_DOMAIN (type) == 0)
484 warning ("%Jarray '%D' assumed to have one element", decl, decl);
486 complete_array_type (type, NULL_TREE, 1);
488 layout_decl (decl, 0);
493 /* The Objective-C front-end often needs to determine the current scope. */
495 void *
496 get_current_scope (void)
498 return current_scope;
501 /* The following function is used only by Objective-C. It needs to live here
502 because it accesses the innards of c_scope. */
504 void
505 objc_mark_locals_volatile (void *enclosing_blk)
507 struct c_scope *scope;
508 struct c_binding *b;
510 for (scope = current_scope;
511 scope && scope != enclosing_blk;
512 scope = scope->outer)
514 for (b = scope->bindings; b; b = b->prev)
516 if (TREE_CODE (b->decl) == VAR_DECL
517 || TREE_CODE (b->decl) == PARM_DECL)
519 C_DECL_REGISTER (b->decl) = 0;
520 DECL_REGISTER (b->decl) = 0;
521 TREE_THIS_VOLATILE (b->decl) = 1;
525 /* Do not climb up past the current function. */
526 if (scope->function_body)
527 break;
531 /* Nonzero if we are currently in file scope. */
534 global_bindings_p (void)
536 return current_scope == file_scope && !c_override_global_bindings_to_false;
539 void
540 keep_next_level (void)
542 keep_next_level_flag = true;
545 /* Identify this scope as currently being filled with parameters. */
547 void
548 declare_parm_level (void)
550 current_scope->parm_flag = true;
553 void
554 push_scope (void)
556 if (next_is_function_body)
558 /* This is the transition from the parameters to the top level
559 of the function body. These are the same scope
560 (C99 6.2.1p4,6) so we do not push another scope structure.
561 next_is_function_body is set only by store_parm_decls, which
562 in turn is called when and only when we are about to
563 encounter the opening curly brace for the function body.
565 The outermost block of a function always gets a BLOCK node,
566 because the debugging output routines expect that each
567 function has at least one BLOCK. */
568 current_scope->parm_flag = false;
569 current_scope->function_body = true;
570 current_scope->keep = true;
571 current_scope->outer_function = current_function_scope;
572 current_function_scope = current_scope;
574 keep_next_level_flag = false;
575 next_is_function_body = false;
577 else
579 struct c_scope *scope;
580 if (scope_freelist)
582 scope = scope_freelist;
583 scope_freelist = scope->outer;
585 else
586 scope = ggc_alloc_cleared (sizeof (struct c_scope));
588 scope->keep = keep_next_level_flag;
589 scope->outer = current_scope;
590 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
592 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
593 possible. */
594 if (current_scope && scope->depth == 0)
596 scope->depth--;
597 sorry ("GCC supports only %u nested scopes\n", scope->depth);
600 current_scope = scope;
601 keep_next_level_flag = false;
605 /* Exit a scope. Restore the state of the identifier-decl mappings
606 that were in effect when this scope was entered. Return a BLOCK
607 node containing all the DECLs in this scope that are of interest
608 to debug info generation. */
610 tree
611 pop_scope (void)
613 struct c_scope *scope = current_scope;
614 tree block, context, p;
615 struct c_binding *b;
617 bool functionbody = scope->function_body;
618 bool keep = functionbody || scope->keep || scope->bindings;
620 /* If appropriate, create a BLOCK to record the decls for the life
621 of this function. */
622 block = 0;
623 if (keep)
625 block = make_node (BLOCK);
626 BLOCK_SUBBLOCKS (block) = scope->blocks;
627 TREE_USED (block) = 1;
629 /* In each subblock, record that this is its superior. */
630 for (p = scope->blocks; p; p = TREE_CHAIN (p))
631 BLOCK_SUPERCONTEXT (p) = block;
633 BLOCK_VARS (block) = 0;
636 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
637 scope must be set so that they point to the appropriate
638 construct, i.e. either to the current FUNCTION_DECL node, or
639 else to the BLOCK node we just constructed.
641 Note that for tagged types whose scope is just the formal
642 parameter list for some function type specification, we can't
643 properly set their TYPE_CONTEXTs here, because we don't have a
644 pointer to the appropriate FUNCTION_TYPE node readily available
645 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
646 type nodes get set in `grokdeclarator' as soon as we have created
647 the FUNCTION_TYPE node which will represent the "scope" for these
648 "parameter list local" tagged types. */
649 if (scope->function_body)
650 context = current_function_decl;
651 else if (scope == file_scope)
652 context = current_file_decl;
653 else
654 context = block;
656 /* Clear all bindings in this scope. */
657 for (b = scope->bindings; b; b = free_binding_and_advance (b))
659 p = b->decl;
660 switch (TREE_CODE (p))
662 case LABEL_DECL:
663 /* Warnings for unused labels, errors for undefined labels. */
664 if (TREE_USED (p) && !DECL_INITIAL (p))
666 error ("%Jlabel `%D' used but not defined", p, p);
667 DECL_INITIAL (p) = error_mark_node;
669 else if (!TREE_USED (p) && warn_unused_label)
671 if (DECL_INITIAL (p))
672 warning ("%Jlabel `%D' defined but not used", p, p);
673 else
674 warning ("%Jlabel `%D' declared but not defined", p, p);
676 /* Labels go in BLOCK_VARS. */
677 TREE_CHAIN (p) = BLOCK_VARS (block);
678 BLOCK_VARS (block) = p;
680 #ifdef ENABLE_CHECKING
681 if (I_LABEL_BINDING (b->id) != b) abort ();
682 #endif
683 I_LABEL_BINDING (b->id) = b->shadowed;
684 break;
686 case ENUMERAL_TYPE:
687 case UNION_TYPE:
688 case RECORD_TYPE:
689 TYPE_CONTEXT (p) = context;
691 /* Types may not have tag-names, in which case the type
692 appears in the bindings list with b->id NULL. */
693 if (b->id)
695 #ifdef ENABLE_CHECKING
696 if (I_TAG_BINDING (b->id) != b) abort ();
697 #endif
698 I_TAG_BINDING (b->id) = b->shadowed;
700 break;
702 case FUNCTION_DECL:
703 /* Propagate TREE_ADDRESSABLE from nested functions to their
704 containing functions. */
705 if (! TREE_ASM_WRITTEN (p)
706 && DECL_INITIAL (p) != 0
707 && TREE_ADDRESSABLE (p)
708 && DECL_ABSTRACT_ORIGIN (p) != 0
709 && DECL_ABSTRACT_ORIGIN (p) != p)
710 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
711 goto common_symbol;
713 case VAR_DECL:
714 /* Warnings for unused variables. Keep this in sync with
715 stmt.c:warn_about_unused_variables, which we cannot use
716 since it expects a different data structure. */
717 if (warn_unused_variable
718 && !TREE_USED (p)
719 && !DECL_IN_SYSTEM_HEADER (p)
720 && DECL_NAME (p)
721 && !DECL_ARTIFICIAL (p)
722 && (scope != file_scope
723 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
724 && !TREE_THIS_VOLATILE (p)))
725 && scope != external_scope)
726 warning ("%Junused variable `%D'", p, p);
728 /* Fall through. */
729 case TYPE_DECL:
730 case CONST_DECL:
731 common_symbol:
732 /* All of these go in BLOCK_VARS, but only if this is the
733 binding in the home scope. */
734 if (!C_DECL_IN_EXTERNAL_SCOPE (p) || scope == external_scope)
736 TREE_CHAIN (p) = BLOCK_VARS (block);
737 BLOCK_VARS (block) = p;
740 /* Fall through. */
741 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
742 already been put there by store_parm_decls. Unused-
743 parameter warnings are handled by function.c.
744 error_mark_node obviously does not go in BLOCK_VARS and
745 does not get unused-variable warnings. */
746 case PARM_DECL:
747 case ERROR_MARK:
748 /* It is possible for a decl not to have a name. We get
749 here with b->id NULL in this case. */
750 if (b->id)
752 #ifdef ENABLE_CHECKING
753 if (I_SYMBOL_BINDING (b->id) != b) abort ();
754 #endif
755 I_SYMBOL_BINDING (b->id) = b->shadowed;
757 break;
759 default:
760 abort ();
765 /* Dispose of the block that we just made inside some higher level. */
766 if ((scope->function_body || scope == file_scope) && context)
768 DECL_INITIAL (context) = block;
769 BLOCK_SUPERCONTEXT (block) = context;
771 else if (scope->outer)
773 if (block)
774 SCOPE_LIST_APPEND (scope->outer, blocks, block);
775 /* If we did not make a block for the scope just exited, any
776 blocks made for inner scopes must be carried forward so they
777 will later become subblocks of something else. */
778 else if (scope->blocks)
779 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
782 /* Pop the current scope, and free the structure for reuse. */
783 current_scope = scope->outer;
784 if (scope->function_body)
785 current_function_scope = scope->outer_function;
787 memset (scope, 0, sizeof (struct c_scope));
788 scope->outer = scope_freelist;
789 scope_freelist = scope;
791 return block;
794 void
795 push_file_scope (void)
797 tree decl;
798 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
799 TREE_CHAIN (file_decl) = current_file_decl;
800 current_file_decl = file_decl;
802 push_scope ();
803 file_scope = current_scope;
805 start_fname_decls ();
807 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
808 bind (DECL_NAME (decl), decl, file_scope);
811 void
812 pop_file_scope (void)
814 /* In case there were missing closebraces, get us back to the global
815 binding level. */
816 while (current_scope != file_scope)
817 pop_scope ();
819 /* __FUNCTION__ is defined at file scope (""). This
820 call may not be necessary as my tests indicate it
821 still works without it. */
822 finish_fname_decls ();
824 /* Kludge: don't actually pop the file scope if generating a
825 precompiled header, so that macros and local symbols are still
826 visible to the PCH generator. */
827 if (pch_file)
828 return;
830 /* And pop off the file scope. */
831 pop_scope ();
832 file_scope = 0;
834 cpp_undef_all (parse_in);
837 /* Insert BLOCK at the end of the list of subblocks of the current
838 scope. This is used when a BIND_EXPR is expanded, to handle the
839 BLOCK node inside the BIND_EXPR. */
841 void
842 insert_block (tree block)
844 TREE_USED (block) = 1;
845 SCOPE_LIST_APPEND (current_scope, blocks, block);
848 /* Push a definition or a declaration of struct, union or enum tag "name".
849 "type" should be the type node.
850 We assume that the tag "name" is not already defined.
852 Note that the definition may really be just a forward reference.
853 In that case, the TYPE_SIZE will be zero. */
855 static void
856 pushtag (tree name, tree type)
858 /* Record the identifier as the type's name if it has none. */
859 if (name && !TYPE_NAME (type))
860 TYPE_NAME (type) = name;
861 bind (name, type, current_scope);
863 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
864 tagged type we just added to the current scope. This fake
865 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
866 to output a representation of a tagged type, and it also gives
867 us a convenient place to record the "scope start" address for the
868 tagged type. */
870 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
872 /* An approximation for now, so we can tell this is a function-scope tag.
873 This will be updated in pop_scope. */
874 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
877 /* Subroutine of compare_decls. Allow harmless mismatches in return
878 and argument types provided that the type modes match. This function
879 return a unified type given a suitable match, and 0 otherwise. */
881 static tree
882 match_builtin_function_types (tree newtype, tree oldtype)
884 tree newrettype, oldrettype;
885 tree newargs, oldargs;
886 tree trytype, tryargs;
888 /* Accept the return type of the new declaration if same modes. */
889 oldrettype = TREE_TYPE (oldtype);
890 newrettype = TREE_TYPE (newtype);
892 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
893 return 0;
895 oldargs = TYPE_ARG_TYPES (oldtype);
896 newargs = TYPE_ARG_TYPES (newtype);
897 tryargs = newargs;
899 while (oldargs || newargs)
901 if (! oldargs
902 || ! newargs
903 || ! TREE_VALUE (oldargs)
904 || ! TREE_VALUE (newargs)
905 || TYPE_MODE (TREE_VALUE (oldargs))
906 != TYPE_MODE (TREE_VALUE (newargs)))
907 return 0;
909 oldargs = TREE_CHAIN (oldargs);
910 newargs = TREE_CHAIN (newargs);
913 trytype = build_function_type (newrettype, tryargs);
914 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
917 /* Subroutine of diagnose_mismatched_decls. Check for function type
918 mismatch involving an empty arglist vs a nonempty one and give clearer
919 diagnostics. */
920 static void
921 diagnose_arglist_conflict (tree newdecl, tree olddecl,
922 tree newtype, tree oldtype)
924 tree t;
926 if (TREE_CODE (olddecl) != FUNCTION_DECL
927 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype), COMPARE_STRICT)
928 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
930 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
931 return;
933 t = TYPE_ARG_TYPES (oldtype);
934 if (t == 0)
935 t = TYPE_ARG_TYPES (newtype);
936 for (; t; t = TREE_CHAIN (t))
938 tree type = TREE_VALUE (t);
940 if (TREE_CHAIN (t) == 0
941 && TYPE_MAIN_VARIANT (type) != void_type_node)
943 inform ("a parameter list with an ellipsis can't match "
944 "an empty parameter name list declaration");
945 break;
948 if (c_type_promotes_to (type) != type)
950 inform ("an argument type that has a default promotion can't match "
951 "an empty parameter name list declaration");
952 break;
957 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
958 old-style function definition, NEWDECL is a prototype declaration.
959 Diagnose inconsistencies in the argument list. Returns TRUE if
960 the prototype is compatible, FALSE if not. */
961 static bool
962 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
964 tree newargs, oldargs;
965 int i;
967 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
968 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
970 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
971 newargs = TYPE_ARG_TYPES (newtype);
972 i = 1;
974 for (;;)
976 tree oldargtype = TREE_VALUE (oldargs);
977 tree newargtype = TREE_VALUE (newargs);
979 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
980 break;
982 /* Reaching the end of just one list means the two decls don't
983 agree on the number of arguments. */
984 if (END_OF_ARGLIST (oldargtype))
986 error ("%Jprototype for '%D' declares more arguments "
987 "than previous old-style definition", newdecl, newdecl);
988 return false;
990 else if (END_OF_ARGLIST (newargtype))
992 error ("%Jprototype for '%D' declares fewer arguments "
993 "than previous old-style definition", newdecl, newdecl);
994 return false;
997 /* Type for passing arg must be consistent with that declared
998 for the arg. */
999 else if (! comptypes (oldargtype, newargtype, COMPARE_STRICT))
1001 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1002 newdecl, newdecl, i);
1003 return false;
1006 oldargs = TREE_CHAIN (oldargs);
1007 newargs = TREE_CHAIN (newargs);
1008 i++;
1011 /* If we get here, no errors were found, but do issue a warning
1012 for this poor-style construct. */
1013 warning ("%Jprototype for '%D' follows non-prototype definition",
1014 newdecl, newdecl);
1015 return true;
1016 #undef END_OF_ARGLIST
1019 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1020 first in a pair of mismatched declarations, using the diagnostic
1021 function DIAG. */
1022 static void
1023 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1025 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1027 else if (DECL_INITIAL (decl))
1028 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1029 else if (C_DECL_IMPLICIT (decl))
1030 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1031 else
1032 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1035 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1036 Returns true if the caller should proceed to merge the two, false
1037 if OLDDECL should simply be discarded. As a side effect, issues
1038 all necessary diagnostics for invalid or poor-style combinations.
1039 If it returns true, writes the types of NEWDECL and OLDDECL to
1040 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1041 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1043 static bool
1044 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1045 tree *newtypep, tree *oldtypep)
1047 tree newtype, oldtype;
1048 bool pedwarned = false;
1049 bool warned = false;
1051 /* If we have error_mark_node for either decl or type, just discard
1052 the previous decl - we're in an error cascade already. */
1053 if (olddecl == error_mark_node || newdecl == error_mark_node)
1054 return false;
1055 *oldtypep = oldtype = TREE_TYPE (olddecl);
1056 *newtypep = newtype = TREE_TYPE (newdecl);
1057 if (oldtype == error_mark_node || newtype == error_mark_node)
1058 return false;
1060 /* Two different categories of symbol altogether. This is an error
1061 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1062 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1064 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1065 && DECL_BUILT_IN (olddecl)
1066 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1068 error ("%J'%D' redeclared as different kind of symbol",
1069 newdecl, newdecl);
1070 locate_old_decl (olddecl, error);
1072 else if (TREE_PUBLIC (newdecl))
1073 warning ("%Jbuilt-in function '%D' declared as non-function",
1074 newdecl, newdecl);
1075 else if (warn_shadow)
1076 warning ("%Jdeclaration of '%D' shadows a built-in function",
1077 newdecl, newdecl);
1078 return false;
1081 if (!comptypes (oldtype, newtype, COMPARE_STRICT))
1083 if (TREE_CODE (olddecl) == FUNCTION_DECL
1084 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1086 /* Accept harmless mismatch in function types.
1087 This is for the ffs and fprintf builtins. */
1088 tree trytype = match_builtin_function_types (newtype, oldtype);
1090 if (trytype && comptypes (newtype, trytype, COMPARE_STRICT))
1091 *oldtypep = oldtype = trytype;
1092 else
1094 /* If types don't match for a built-in, throw away the
1095 built-in. No point in calling locate_old_decl here, it
1096 won't print anything. */
1097 warning ("%Jconflicting types for built-in function '%D'",
1098 newdecl, newdecl);
1099 return false;
1102 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1103 && DECL_SOURCE_LINE (olddecl) == 0)
1105 /* A conflicting function declaration for a predeclared
1106 function that isn't actually built in. Objective C uses
1107 these. The new declaration silently overrides everything
1108 but the volatility (i.e. noreturn) indication. See also
1109 below. FIXME: Make Objective C use normal builtins. */
1110 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1111 return false;
1113 /* Permit void foo (...) to match int foo (...) if the latter is
1114 the definition and implicit int was used. See
1115 c-torture/compile/920625-2.c. */
1116 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1117 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1118 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1119 && C_FUNCTION_IMPLICIT_INT (newdecl))
1121 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1122 /* Make sure we keep void as the return type. */
1123 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1124 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1125 pedwarned = true;
1127 else
1129 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1130 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1131 locate_old_decl (olddecl, error);
1132 return false;
1136 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1137 but silently ignore the redeclaration if either is in a system
1138 header. (Conflicting redeclarations were handled above.) */
1139 if (TREE_CODE (newdecl) == TYPE_DECL)
1141 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1142 return true; /* Allow OLDDECL to continue in use. */
1144 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1145 locate_old_decl (olddecl, error);
1146 return false;
1149 /* Function declarations can either be 'static' or 'extern' (no
1150 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1151 can never conflict with each other on account of linkage (6.2.2p4).
1152 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1153 two definitions if one is 'extern inline' and one is not. The non-
1154 extern-inline definition supersedes the extern-inline definition. */
1155 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1157 /* If you declare a built-in function name as static, or
1158 define the built-in with an old-style definition (so we
1159 can't validate the argument list) the built-in definition is
1160 overridden, but optionally warn this was a bad choice of name. */
1161 if (DECL_BUILT_IN (olddecl)
1162 && !C_DECL_DECLARED_BUILTIN (olddecl)
1163 && (!TREE_PUBLIC (newdecl)
1164 || (DECL_INITIAL (newdecl)
1165 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1167 if (warn_shadow)
1168 warning ("%Jdeclaration of '%D' shadows a built-in function",
1169 newdecl, newdecl);
1170 /* Discard the old built-in function. */
1171 return false;
1174 if (DECL_INITIAL (newdecl))
1176 if (DECL_INITIAL (olddecl)
1177 && !(DECL_DECLARED_INLINE_P (olddecl)
1178 && DECL_EXTERNAL (olddecl)
1179 && !(DECL_DECLARED_INLINE_P (newdecl)
1180 && DECL_EXTERNAL (newdecl))))
1182 error ("%Jredefinition of '%D'", newdecl, newdecl);
1183 locate_old_decl (olddecl, error);
1184 return false;
1187 /* If we have a prototype after an old-style function definition,
1188 the argument types must be checked specially. */
1189 else if (DECL_INITIAL (olddecl)
1190 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1191 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1192 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1194 locate_old_decl (olddecl, error);
1195 return false;
1197 /* Mismatched non-static and static is considered poor style.
1198 We only diagnose static then non-static if -Wtraditional,
1199 because it is the most convenient way to get some effects
1200 (see e.g. what unwind-dw2-fde-glibc.c does to the definition
1201 of _Unwind_Find_FDE in unwind-dw2-fde.c). Revisit? */
1202 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1204 /* A static function declaration for a predeclared function
1205 that isn't actually built in, silently overrides the
1206 default. Objective C uses these. See also above.
1207 FIXME: Make Objective C use normal builtins. */
1208 if (TREE_CODE (olddecl) == FUNCTION_DECL
1209 && DECL_SOURCE_LINE (olddecl) == 0)
1210 return false;
1211 else
1213 warning ("%Jstatic declaration of '%D' follows "
1214 "non-static declaration", newdecl, newdecl);
1215 warned = true;
1218 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl)
1219 && warn_traditional)
1221 warning ("%Jnon-static declaration of '%D' follows "
1222 "static declaration", newdecl, newdecl);
1223 warned = true;
1226 else if (TREE_CODE (newdecl) == VAR_DECL)
1228 /* Only variables can be thread-local, and all declarations must
1229 agree on this property. */
1230 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1232 if (DECL_THREAD_LOCAL (newdecl))
1233 error ("%Jthread-local declaration of '%D' follows "
1234 "non-thread-local declaration", newdecl, newdecl);
1235 else
1236 error ("%Jnon-thread-local declaration of '%D' follows "
1237 "thread-local declaration", newdecl, newdecl);
1239 locate_old_decl (olddecl, error);
1240 return false;
1243 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1244 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1246 error ("%Jredefinition of '%D'", newdecl, newdecl);
1247 locate_old_decl (olddecl, error);
1248 return false;
1251 /* Objects declared at file scope: if at least one is 'extern',
1252 it's fine (6.2.2p4); otherwise the linkage must agree (6.2.2p7). */
1253 if (DECL_FILE_SCOPE_P (newdecl))
1255 if (!DECL_EXTERNAL (newdecl)
1256 && !DECL_EXTERNAL (olddecl)
1257 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1259 if (TREE_PUBLIC (newdecl))
1260 error ("%Jnon-static declaration of '%D' follows "
1261 "static declaration", newdecl, newdecl);
1262 else
1263 error ("%Jstatic declaration of '%D' follows "
1264 "non-static declaration", newdecl, newdecl);
1266 locate_old_decl (olddecl, error);
1267 return false;
1270 /* Two objects with the same name declared at the same block
1271 scope must both be external references (6.7p3). */
1272 else if (DECL_CONTEXT (newdecl) == DECL_CONTEXT (olddecl)
1273 && (!DECL_EXTERNAL (newdecl) || !DECL_EXTERNAL (olddecl)))
1275 if (DECL_EXTERNAL (newdecl))
1276 error ("%Jextern declaration of '%D' follows "
1277 "declaration with no linkage", newdecl, newdecl);
1278 else if (DECL_EXTERNAL (olddecl))
1279 error ("%Jdeclaration of '%D' with no linkage follows "
1280 "extern declaration", newdecl, newdecl);
1281 else
1282 error ("%Jredeclaration of '%D' with no linkage",
1283 newdecl, newdecl);
1285 locate_old_decl (olddecl, error);
1286 return false;
1290 /* warnings */
1291 /* All decls must agree on a non-default visibility. */
1292 if (DECL_VISIBILITY (newdecl) != VISIBILITY_DEFAULT
1293 && DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT
1294 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1296 warning ("%Jredeclaration of '%D' with different visibility "
1297 "(old visibility preserved)", newdecl, newdecl);
1298 warned = true;
1301 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1303 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1304 if (DECL_DECLARED_INLINE_P (newdecl)
1305 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1307 warning ("%Jinline declaration of '%D' follows "
1308 "declaration with attribute noinline", newdecl, newdecl);
1309 warned = true;
1311 else if (DECL_DECLARED_INLINE_P (olddecl)
1312 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1314 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1315 "inline declaration ", newdecl, newdecl);
1316 warned = true;
1319 /* Inline declaration after use or definition.
1320 ??? Should we still warn about this now we have unit-at-a-time
1321 mode and can get it right? */
1322 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl))
1324 if (TREE_USED (olddecl))
1326 warning ("%J'%D' declared inline after being called",
1327 olddecl, olddecl);
1328 warned = true;
1330 else if (DECL_INITIAL (olddecl))
1332 warning ("%J'%D' declared inline after its definition",
1333 olddecl, olddecl);
1334 warned = true;
1338 else /* PARM_DECL, VAR_DECL */
1340 /* Redeclaration of a parameter is a constraint violation (this is
1341 not explicitly stated, but follows from C99 6.7p3 [no more than
1342 one declaration of the same identifier with no linkage in the
1343 same scope, except type tags] and 6.2.2p6 [parameters have no
1344 linkage]). We must check for a forward parameter declaration,
1345 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1346 an extension, the mandatory diagnostic for which is handled by
1347 mark_forward_parm_decls. */
1349 if (TREE_CODE (newdecl) == PARM_DECL
1350 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1352 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1353 locate_old_decl (olddecl, error);
1354 return false;
1357 /* These bits are only type qualifiers when applied to objects. */
1358 if (TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
1360 if (TREE_THIS_VOLATILE (newdecl))
1361 pedwarn ("%Jvolatile declaration of '%D' follows "
1362 "non-volatile declaration", newdecl, newdecl);
1363 else
1364 pedwarn ("%Jnon-volatile declaration of '%D' follows "
1365 "volatile declaration", newdecl, newdecl);
1366 pedwarned = true;
1368 if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl))
1370 if (TREE_READONLY (newdecl))
1371 pedwarn ("%Jconst declaration of '%D' follows "
1372 "non-const declaration", newdecl, newdecl);
1373 else
1374 pedwarn ("%Jnon-const declaration of '%D' follows "
1375 "const declaration", newdecl, newdecl);
1376 pedwarned = true;
1380 /* Optional warning for completely redundant decls. */
1381 if (!warned && !pedwarned
1382 && warn_redundant_decls
1383 /* Don't warn about a function declaration followed by a
1384 definition. */
1385 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1386 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1387 /* Don't warn about an extern followed by a definition. */
1388 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1389 /* Don't warn about forward parameter decls. */
1390 && !(TREE_CODE (newdecl) == PARM_DECL
1391 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1393 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1394 warned = true;
1397 /* Report location of previous decl/defn in a consistent manner. */
1398 if (warned || pedwarned)
1399 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1401 return true;
1404 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1405 consistent with OLDDECL, but carries new information. Merge the
1406 new information into OLDDECL. This function issues no
1407 diagnostics. */
1409 static void
1410 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1412 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1413 && DECL_INITIAL (newdecl) != 0);
1415 /* For real parm decl following a forward decl, rechain the old decl
1416 in its new location and clear TREE_ASM_WRITTEN (it's not a
1417 forward decl anymore). */
1418 if (TREE_CODE (newdecl) == PARM_DECL
1419 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1421 struct c_binding *b, **here;
1423 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1424 if ((*here)->decl == olddecl)
1425 goto found;
1426 abort ();
1428 found:
1429 b = *here;
1430 *here = b->prev;
1431 b->prev = current_scope->bindings;
1432 current_scope->bindings = b;
1434 TREE_ASM_WRITTEN (olddecl) = 0;
1437 DECL_ATTRIBUTES (newdecl)
1438 = targetm.merge_decl_attributes (olddecl, newdecl);
1440 /* Merge the data types specified in the two decls. */
1441 TREE_TYPE (newdecl)
1442 = TREE_TYPE (olddecl)
1443 = common_type (newtype, oldtype);
1445 /* Lay the type out, unless already done. */
1446 if (oldtype != TREE_TYPE (newdecl))
1448 if (TREE_TYPE (newdecl) != error_mark_node)
1449 layout_type (TREE_TYPE (newdecl));
1450 if (TREE_CODE (newdecl) != FUNCTION_DECL
1451 && TREE_CODE (newdecl) != TYPE_DECL
1452 && TREE_CODE (newdecl) != CONST_DECL)
1453 layout_decl (newdecl, 0);
1455 else
1457 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1458 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1459 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1460 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1461 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1462 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1464 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1465 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1469 /* Keep the old rtl since we can safely use it. */
1470 COPY_DECL_RTL (olddecl, newdecl);
1472 /* Merge the type qualifiers. */
1473 if (TREE_READONLY (newdecl))
1474 TREE_READONLY (olddecl) = 1;
1476 if (TREE_THIS_VOLATILE (newdecl))
1478 TREE_THIS_VOLATILE (olddecl) = 1;
1479 if (TREE_CODE (newdecl) == VAR_DECL)
1480 make_var_volatile (newdecl);
1483 /* Keep source location of definition rather than declaration. */
1484 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1485 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1487 /* Merge the unused-warning information. */
1488 if (DECL_IN_SYSTEM_HEADER (olddecl))
1489 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1490 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1491 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1493 /* Merge the initialization information. */
1494 if (DECL_INITIAL (newdecl) == 0)
1495 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1497 /* Merge the section attribute.
1498 We want to issue an error if the sections conflict but that must be
1499 done later in decl_attributes since we are called before attributes
1500 are assigned. */
1501 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1502 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1504 /* Copy the assembler name.
1505 Currently, it can only be defined in the prototype. */
1506 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1508 /* If either declaration has a nondefault visibility, use it. */
1509 if (DECL_VISIBILITY (olddecl) != VISIBILITY_DEFAULT)
1510 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1512 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1514 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1515 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1516 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1517 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1518 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1519 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1520 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1521 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1522 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1525 /* Merge the storage class information. */
1526 merge_weak (newdecl, olddecl);
1528 /* For functions, static overrides non-static. */
1529 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1531 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1532 /* This is since we don't automatically
1533 copy the attributes of NEWDECL into OLDDECL. */
1534 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1535 /* If this clears `static', clear it in the identifier too. */
1536 if (! TREE_PUBLIC (olddecl))
1537 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1539 if (DECL_EXTERNAL (newdecl))
1541 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1542 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1544 /* An extern decl does not override previous storage class. */
1545 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1546 if (! DECL_EXTERNAL (newdecl))
1548 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1549 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1552 else
1554 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1555 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1558 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1560 /* If we're redefining a function previously defined as extern
1561 inline, make sure we emit debug info for the inline before we
1562 throw it away, in case it was inlined into a function that hasn't
1563 been written out yet. */
1564 if (new_is_definition && DECL_INITIAL (olddecl))
1566 if (TREE_USED (olddecl)
1567 /* In unit-at-a-time mode we never inline re-defined extern
1568 inline functions. */
1569 && !flag_unit_at_a_time
1570 && cgraph_function_possibly_inlined_p (olddecl))
1571 (*debug_hooks->outlining_inline_function) (olddecl);
1573 /* The new defn must not be inline. */
1574 DECL_INLINE (newdecl) = 0;
1575 DECL_UNINLINABLE (newdecl) = 1;
1577 else
1579 /* If either decl says `inline', this fn is inline,
1580 unless its definition was passed already. */
1581 if (DECL_DECLARED_INLINE_P (newdecl)
1582 || DECL_DECLARED_INLINE_P (olddecl))
1583 DECL_DECLARED_INLINE_P (newdecl) = 1;
1585 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1586 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1589 if (DECL_BUILT_IN (olddecl))
1591 /* If redeclaring a builtin function, it stays built in.
1592 But it gets tagged as having been declared. */
1593 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1594 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1595 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1598 /* Also preserve various other info from the definition. */
1599 if (! new_is_definition)
1601 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1602 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1603 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1604 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1605 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1607 /* Set DECL_INLINE on the declaration if we've got a body
1608 from which to instantiate. */
1609 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1611 DECL_INLINE (newdecl) = 1;
1612 DECL_ABSTRACT_ORIGIN (newdecl)
1613 = DECL_ABSTRACT_ORIGIN (olddecl);
1616 else
1618 /* If a previous declaration said inline, mark the
1619 definition as inlinable. */
1620 if (DECL_DECLARED_INLINE_P (newdecl)
1621 && ! DECL_UNINLINABLE (newdecl))
1622 DECL_INLINE (newdecl) = 1;
1626 /* This bit must not get wiped out. */
1627 C_DECL_IN_EXTERNAL_SCOPE (newdecl) |= C_DECL_IN_EXTERNAL_SCOPE (olddecl);
1629 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1630 But preserve OLDDECL's DECL_UID. */
1632 unsigned olddecl_uid = DECL_UID (olddecl);
1634 memcpy ((char *) olddecl + sizeof (struct tree_common),
1635 (char *) newdecl + sizeof (struct tree_common),
1636 sizeof (struct tree_decl) - sizeof (struct tree_common));
1637 DECL_UID (olddecl) = olddecl_uid;
1640 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1641 so that encode_section_info has a chance to look at the new decl
1642 flags and attributes. */
1643 if (DECL_RTL_SET_P (olddecl)
1644 && (TREE_CODE (olddecl) == FUNCTION_DECL
1645 || (TREE_CODE (olddecl) == VAR_DECL
1646 && TREE_STATIC (olddecl))))
1647 make_decl_rtl (olddecl, NULL);
1650 /* Handle when a new declaration NEWDECL has the same name as an old
1651 one OLDDECL in the same binding contour. Prints an error message
1652 if appropriate.
1654 If safely possible, alter OLDDECL to look like NEWDECL, and return
1655 true. Otherwise, return false. */
1657 static bool
1658 duplicate_decls (tree newdecl, tree olddecl)
1660 tree newtype, oldtype;
1662 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1663 return false;
1665 merge_decls (newdecl, olddecl, newtype, oldtype);
1666 return true;
1670 /* Check whether decl-node NEW shadows an existing declaration. */
1671 static void
1672 warn_if_shadowing (tree new)
1674 struct c_binding *b;
1676 /* Shadow warnings wanted? */
1677 if (!warn_shadow
1678 /* No shadow warnings for internally generated vars. */
1679 || DECL_SOURCE_LINE (new) == 0
1680 /* No shadow warnings for vars made for inlining. */
1681 || DECL_FROM_INLINE (new)
1682 /* Don't warn about the parm names in function declarator
1683 within a function declarator. It would be nice to avoid
1684 warning in any function declarator in a declaration, as
1685 opposed to a definition, but there is no way to tell
1686 it's not a definition at this point. */
1687 || (TREE_CODE (new) == PARM_DECL && current_scope->outer->parm_flag))
1688 return;
1690 /* Is anything being shadowed? Do not be confused by a second binding
1691 to the same decl in the externals scope. */
1692 for (b = I_SYMBOL_BINDING (DECL_NAME (new)); b; b = b->shadowed)
1693 if (b->decl && b->decl != new && b->contour != external_scope)
1695 tree old = b->decl;
1697 if (TREE_CODE (old) == PARM_DECL)
1698 warning ("%Jdeclaration of '%D' shadows a parameter", new, new);
1699 else if (DECL_FILE_SCOPE_P (old))
1700 warning ("%Jdeclaration of '%D' shadows a global declaration",
1701 new, new);
1702 else if (TREE_CODE (old) == FUNCTION_DECL && DECL_BUILT_IN (old))
1703 warning ("%Jdeclaration of '%D' shadows a built-in function",
1704 new, new);
1705 else
1706 warning ("%Jdeclaration of '%D' shadows a previous local", new, new);
1708 if (TREE_CODE (old) != FUNCTION_DECL || !DECL_BUILT_IN (old))
1709 warning ("%Jshadowed declaration is here", old);
1711 break;
1716 /* Subroutine of pushdecl.
1718 X is a TYPE_DECL for a typedef statement. Create a brand new
1719 ..._TYPE node (which will be just a variant of the existing
1720 ..._TYPE node with identical properties) and then install X
1721 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1723 The whole point here is to end up with a situation where each
1724 and every ..._TYPE node the compiler creates will be uniquely
1725 associated with AT MOST one node representing a typedef name.
1726 This way, even though the compiler substitutes corresponding
1727 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1728 early on, later parts of the compiler can always do the reverse
1729 translation and get back the corresponding typedef name. For
1730 example, given:
1732 typedef struct S MY_TYPE;
1733 MY_TYPE object;
1735 Later parts of the compiler might only know that `object' was of
1736 type `struct S' if it were not for code just below. With this
1737 code however, later parts of the compiler see something like:
1739 struct S' == struct S
1740 typedef struct S' MY_TYPE;
1741 struct S' object;
1743 And they can then deduce (from the node for type struct S') that
1744 the original object declaration was:
1746 MY_TYPE object;
1748 Being able to do this is important for proper support of protoize,
1749 and also for generating precise symbolic debugging information
1750 which takes full account of the programmer's (typedef) vocabulary.
1752 Obviously, we don't want to generate a duplicate ..._TYPE node if
1753 the TYPE_DECL node that we are now processing really represents a
1754 standard built-in type.
1756 Since all standard types are effectively declared at line zero
1757 in the source file, we can easily check to see if we are working
1758 on a standard type by checking the current value of lineno. */
1760 static void
1761 clone_underlying_type (tree x)
1763 if (DECL_SOURCE_LINE (x) == 0)
1765 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1766 TYPE_NAME (TREE_TYPE (x)) = x;
1768 else if (TREE_TYPE (x) != error_mark_node
1769 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1771 tree tt = TREE_TYPE (x);
1772 DECL_ORIGINAL_TYPE (x) = tt;
1773 tt = build_type_copy (tt);
1774 TYPE_NAME (tt) = x;
1775 TREE_USED (tt) = TREE_USED (x);
1776 TREE_TYPE (x) = tt;
1780 /* Record a decl-node X as belonging to the current lexical scope.
1781 Check for errors (such as an incompatible declaration for the same
1782 name already seen in the same scope).
1784 Returns either X or an old decl for the same name.
1785 If an old decl is returned, it may have been smashed
1786 to agree with what X says. */
1788 tree
1789 pushdecl (tree x)
1791 tree name = DECL_NAME (x);
1792 struct c_scope *scope = current_scope;
1793 struct c_binding *b;
1795 /* Functions need the lang_decl data. */
1796 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1797 DECL_LANG_SPECIFIC (x) = ggc_alloc_cleared (sizeof (struct lang_decl));
1799 /* A local extern declaration for a function doesn't constitute nesting.
1800 A local auto declaration does, since it's a forward decl
1801 for a nested function coming later. */
1802 if (current_function_decl == NULL
1803 || ((TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
1804 && DECL_INITIAL (x) == 0 && DECL_EXTERNAL (x)))
1805 DECL_CONTEXT (x) = current_file_decl;
1806 else
1807 DECL_CONTEXT (x) = current_function_decl;
1809 /* Anonymous decls are just inserted in the scope. */
1810 if (!name)
1812 bind (name, x, scope);
1813 return x;
1816 /* First, see if there is another declaration with the same name in
1817 the current scope. If there is, duplicate_decls may do all the
1818 work for us. If duplicate_decls returns false, that indicates
1819 two incompatible decls in the same scope; we are to silently
1820 replace the old one (duplicate_decls has issued all appropriate
1821 diagnostics). In particular, we should not consider possible
1822 duplicates in the external scope, or shadowing. */
1823 b = I_SYMBOL_BINDING (name);
1824 if (b && b->contour == scope)
1826 if (duplicate_decls (x, b->decl))
1827 return b->decl;
1828 else
1829 goto skip_external_and_shadow_checks;
1832 /* All declarations with external linkage, and all external
1833 references, go in the external scope, no matter what scope is
1834 current. However, the binding in that scope is ignored for
1835 purposes of normal name lookup. A separate binding structure is
1836 created in the requested scope; this governs the normal
1837 visibility of the symbol.
1839 The binding in the externals scope is used exclusively for
1840 detecting duplicate declarations of the same object, no matter
1841 what scope they are in; this is what we do here. (C99 6.2.7p2:
1842 All declarations that refer to the same object or function shall
1843 have compatible type; otherwise, the behavior is undefined.) */
1844 if (DECL_EXTERNAL (x) || scope == file_scope)
1846 if (warn_nested_externs
1847 && scope != file_scope
1848 && !DECL_IN_SYSTEM_HEADER (x))
1849 warning ("nested extern declaration of `%s'",
1850 IDENTIFIER_POINTER (name));
1852 while (b && b->contour != external_scope)
1853 b = b->shadowed;
1855 /* The point of the same_translation_unit_p check here is,
1856 we want to detect a duplicate decl for a construct like
1857 foo() { extern bar(); } ... static bar(); but not if
1858 they are in different translation units. In any case,
1859 the static does not go in the externals scope. */
1860 if (b
1861 && (DECL_EXTERNAL (x) || TREE_PUBLIC (x)
1862 || same_translation_unit_p (x, b->decl))
1863 && duplicate_decls (x, b->decl))
1865 bind (name, b->decl, scope);
1866 return b->decl;
1868 else if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1870 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1871 bind (name, x, external_scope);
1875 warn_if_shadowing (x);
1877 skip_external_and_shadow_checks:
1878 if (TREE_CODE (x) == TYPE_DECL)
1879 clone_underlying_type (x);
1881 bind (name, x, scope);
1883 /* If x's type is incomplete because it's based on a
1884 structure or union which has not yet been fully declared,
1885 attach it to that structure or union type, so we can go
1886 back and complete the variable declaration later, if the
1887 structure or union gets fully declared.
1889 If the input is erroneous, we can have error_mark in the type
1890 slot (e.g. "f(void a, ...)") - that doesn't count as an
1891 incomplete type. */
1892 if (TREE_TYPE (x) != error_mark_node
1893 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
1895 tree element = TREE_TYPE (x);
1897 while (TREE_CODE (element) == ARRAY_TYPE)
1898 element = TREE_TYPE (element);
1899 element = TYPE_MAIN_VARIANT (element);
1901 if ((TREE_CODE (element) == RECORD_TYPE
1902 || TREE_CODE (element) == UNION_TYPE)
1903 && (TREE_CODE (x) != TYPE_DECL
1904 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
1905 && !COMPLETE_TYPE_P (element))
1906 C_TYPE_INCOMPLETE_VARS (element)
1907 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
1909 return x;
1912 /* Record X as belonging to file scope.
1913 This is used only internally by the Objective-C front end,
1914 and is limited to its needs. duplicate_decls is not called;
1915 if there is any preexisting decl for this identifier, it is an ICE. */
1917 tree
1918 pushdecl_top_level (tree x)
1920 tree name;
1922 if (TREE_CODE (x) != VAR_DECL)
1923 abort ();
1925 name = DECL_NAME (x);
1927 if (I_SYMBOL_BINDING (name))
1928 abort ();
1930 DECL_CONTEXT (x) = current_file_decl;
1931 if (DECL_EXTERNAL (x) || TREE_PUBLIC (x))
1933 C_DECL_IN_EXTERNAL_SCOPE (x) = 1;
1934 bind (name, x, external_scope);
1936 if (file_scope)
1937 bind (name, x, file_scope);
1939 return x;
1942 static void
1943 implicit_decl_warning (tree id, tree olddecl)
1945 void (*diag) (const char *, ...);
1946 switch (mesg_implicit_function_declaration)
1948 case 0: return;
1949 case 1: diag = warning; break;
1950 case 2: diag = error; break;
1951 default: abort ();
1954 diag (N_("implicit declaration of function '%E'"), id);
1955 if (olddecl)
1956 locate_old_decl (olddecl, diag);
1959 /* Generate an implicit declaration for identifier FUNCTIONID as a
1960 function of type int (). */
1962 tree
1963 implicitly_declare (tree functionid)
1965 tree decl = lookup_name_in_scope (functionid, external_scope);
1967 if (decl)
1969 /* FIXME: Objective-C has weird not-really-builtin functions
1970 which are supposed to be visible automatically. They wind up
1971 in the external scope because they're pushed before the file
1972 scope gets created. Catch this here and rebind them into the
1973 file scope. */
1974 if (!DECL_BUILT_IN (decl) && DECL_SOURCE_LINE (decl) == 0)
1976 bind (functionid, decl, file_scope);
1977 return decl;
1979 else
1981 /* Implicit declaration of a function already declared
1982 (somehow) in a different scope, or as a built-in.
1983 If this is the first time this has happened, warn;
1984 then recycle the old declaration. */
1985 if (!C_DECL_IMPLICIT (decl))
1987 implicit_decl_warning (functionid, decl);
1988 C_DECL_IMPLICIT (decl) = 1;
1990 bind (functionid, decl, current_scope);
1991 return decl;
1995 /* Not seen before. */
1996 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
1997 DECL_EXTERNAL (decl) = 1;
1998 TREE_PUBLIC (decl) = 1;
1999 C_DECL_IMPLICIT (decl) = 1;
2000 implicit_decl_warning (functionid, 0);
2002 /* C89 says implicit declarations are in the innermost block.
2003 So we record the decl in the standard fashion. */
2004 decl = pushdecl (decl);
2006 /* No need to call objc_check_decl here - it's a function type. */
2007 rest_of_decl_compilation (decl, NULL, 0, 0);
2009 /* Write a record describing this implicit function declaration
2010 to the prototypes file (if requested). */
2011 gen_aux_info_record (decl, 0, 1, 0);
2013 /* Possibly apply some default attributes to this implicit declaration. */
2014 decl_attributes (&decl, NULL_TREE, 0);
2016 return decl;
2019 /* Issue an error message for a reference to an undeclared variable
2020 ID, including a reference to a builtin outside of function-call
2021 context. Establish a binding of the identifier to error_mark_node
2022 in an appropriate scope, which will suppress further errors for the
2023 same identifier. */
2024 void
2025 undeclared_variable (tree id)
2027 static bool already = false;
2028 struct c_scope *scope;
2030 if (current_function_decl == 0)
2032 error ("'%E' undeclared here (not in a function)", id);
2033 scope = current_scope;
2035 else
2037 error ("'%E' undeclared (first use in this function)", id);
2039 if (! already)
2041 error ("(Each undeclared identifier is reported only once");
2042 error ("for each function it appears in.)");
2043 already = true;
2046 /* If we are parsing old-style parameter decls, current_function_decl
2047 will be nonnull but current_function_scope will be null. */
2048 scope = current_function_scope ? current_function_scope : current_scope;
2050 bind (id, error_mark_node, scope);
2053 /* Subroutine of lookup_label, declare_label, define_label: construct a
2054 LABEL_DECL with all the proper frills. */
2056 static tree
2057 make_label (tree name, location_t location)
2059 tree label = build_decl (LABEL_DECL, name, void_type_node);
2061 DECL_CONTEXT (label) = current_function_decl;
2062 DECL_MODE (label) = VOIDmode;
2063 DECL_SOURCE_LOCATION (label) = location;
2065 return label;
2068 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2069 Create one if none exists so far for the current function.
2070 This is called when a label is used in a goto expression or
2071 has its address taken. */
2073 tree
2074 lookup_label (tree name)
2076 tree label;
2078 if (current_function_decl == 0)
2080 error ("label %s referenced outside of any function",
2081 IDENTIFIER_POINTER (name));
2082 return 0;
2085 /* Use a label already defined or ref'd with this name, but not if
2086 it is inherited from a containing function and wasn't declared
2087 using __label__. */
2088 label = I_LABEL_DECL (name);
2089 if (label && (DECL_CONTEXT (label) == current_function_decl
2090 || C_DECLARED_LABEL_FLAG (label)))
2092 /* If the label has only been declared, update its apparent
2093 location to point here, for better diagnostics if it
2094 turns out not to have been defined. */
2095 if (!TREE_USED (label))
2096 DECL_SOURCE_LOCATION (label) = input_location;
2097 return label;
2100 /* No label binding for that identifier; make one. */
2101 label = make_label (name, input_location);
2103 /* Ordinary labels go in the current function scope. */
2104 bind (name, label, current_function_scope);
2105 return label;
2108 /* Make a label named NAME in the current function, shadowing silently
2109 any that may be inherited from containing functions or containing
2110 scopes. This is called for __label__ declarations. */
2112 /* Note that valid use, if the label being shadowed comes from another
2113 scope in the same function, requires calling declare_nonlocal_label
2114 right away. (Is this still true? -zw 2003-07-17) */
2116 tree
2117 declare_label (tree name)
2119 struct c_binding *b = I_LABEL_BINDING (name);
2120 tree label;
2122 /* Check to make sure that the label hasn't already been declared
2123 at this scope */
2124 if (b && b->contour == current_scope)
2126 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2127 locate_old_decl (b->decl, error);
2129 /* Just use the previous declaration. */
2130 return b->decl;
2133 label = make_label (name, input_location);
2134 C_DECLARED_LABEL_FLAG (label) = 1;
2136 /* Declared labels go in the current scope. */
2137 bind (name, label, current_scope);
2138 return label;
2141 /* Define a label, specifying the location in the source file.
2142 Return the LABEL_DECL node for the label, if the definition is valid.
2143 Otherwise return 0. */
2145 tree
2146 define_label (location_t location, tree name)
2148 /* Find any preexisting label with this name. It is an error
2149 if that label has already been defined in this function, or
2150 if there is a containing function with a declared label with
2151 the same name. */
2152 tree label = I_LABEL_DECL (name);
2154 if (label
2155 && ((DECL_CONTEXT (label) == current_function_decl
2156 && DECL_INITIAL (label) != 0)
2157 || (DECL_CONTEXT (label) != current_function_decl
2158 && C_DECLARED_LABEL_FLAG (label))))
2160 error ("%Hduplicate label `%D'", &location, label);
2161 locate_old_decl (label, error);
2162 return 0;
2164 else if (label && DECL_CONTEXT (label) == current_function_decl)
2166 /* The label has been used or declared already in this function,
2167 but not defined. Update its location to point to this
2168 definition. */
2169 DECL_SOURCE_LOCATION (label) = location;
2171 else
2173 /* No label binding for that identifier; make one. */
2174 label = make_label (name, location);
2176 /* Ordinary labels go in the current function scope. */
2177 bind (name, label, current_function_scope);
2180 if (warn_traditional && !in_system_header && lookup_name (name))
2181 warning ("%Htraditional C lacks a separate namespace for labels, "
2182 "identifier `%s' conflicts", &location,
2183 IDENTIFIER_POINTER (name));
2185 /* Mark label as having been defined. */
2186 DECL_INITIAL (label) = error_mark_node;
2187 return label;
2190 /* Given NAME, an IDENTIFIER_NODE,
2191 return the structure (or union or enum) definition for that name.
2192 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2193 CODE says which kind of type the caller wants;
2194 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2195 If the wrong kind of type is found, an error is reported. */
2197 static tree
2198 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2200 struct c_binding *b = I_TAG_BINDING (name);
2201 int thislevel = 0;
2203 if (!b || !b->decl)
2204 return 0;
2206 /* We only care about whether it's in this level if
2207 thislevel_only was set or it might be a type clash. */
2208 if (thislevel_only || TREE_CODE (b->decl) != code)
2210 /* For our purposes, a tag in the external scope is the same as
2211 a tag in the file scope. (Primarily relevant to Objective-C
2212 and its builtin structure tags, which get pushed before the
2213 file scope is created.) */
2214 if (b->contour == current_scope
2215 || (current_scope == file_scope && b->contour == external_scope))
2216 thislevel = 1;
2219 if (thislevel_only && !thislevel)
2220 return 0;
2222 if (TREE_CODE (b->decl) != code)
2224 /* Definition isn't the kind we were looking for. */
2225 pending_invalid_xref = name;
2226 pending_invalid_xref_location = input_location;
2228 /* If in the same binding level as a declaration as a tag
2229 of a different type, this must not be allowed to
2230 shadow that tag, so give the error immediately.
2231 (For example, "struct foo; union foo;" is invalid.) */
2232 if (thislevel)
2233 pending_xref_error ();
2235 return b->decl;
2238 /* Print an error message now
2239 for a recent invalid struct, union or enum cross reference.
2240 We don't print them immediately because they are not invalid
2241 when used in the `struct foo;' construct for shadowing. */
2243 void
2244 pending_xref_error (void)
2246 if (pending_invalid_xref != 0)
2247 error ("%H`%s' defined as wrong kind of tag",
2248 &pending_invalid_xref_location,
2249 IDENTIFIER_POINTER (pending_invalid_xref));
2250 pending_invalid_xref = 0;
2254 /* Look up NAME in the current scope and its superiors
2255 in the namespace of variables, functions and typedefs.
2256 Return a ..._DECL node of some kind representing its definition,
2257 or return 0 if it is undefined. */
2259 tree
2260 lookup_name (tree name)
2262 struct c_binding *b = I_SYMBOL_BINDING (name);
2263 if (b && (b->contour != external_scope || TREE_CODE (b->decl) == TYPE_DECL))
2264 return b->decl;
2265 return 0;
2268 /* Similar to `lookup_name' but look only at the indicated scope. */
2270 static tree
2271 lookup_name_in_scope (tree name, struct c_scope *scope)
2273 struct c_binding *b;
2275 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2276 if (b->contour == scope)
2277 return b->decl;
2278 return 0;
2281 /* Create the predefined scalar types of C,
2282 and some nodes representing standard constants (0, 1, (void *) 0).
2283 Initialize the global scope.
2284 Make definitions for built-in primitive functions. */
2286 void
2287 c_init_decl_processing (void)
2289 tree endlink;
2290 tree ptr_ftype_void, ptr_ftype_ptr;
2291 location_t save_loc = input_location;
2293 /* Adds some ggc roots, and reserved words for c-parse.in. */
2294 c_parse_init ();
2296 current_function_decl = 0;
2298 /* Make the externals scope. */
2299 push_scope ();
2300 external_scope = current_scope;
2302 /* Declarations from c_common_nodes_and_builtins must not be associated
2303 with this input file, lest we get differences between using and not
2304 using preprocessed headers. */
2305 input_location.file = "<internal>";
2306 input_location.line = 0;
2308 build_common_tree_nodes (flag_signed_char);
2310 c_common_nodes_and_builtins ();
2312 /* In C, comparisons and TRUTH_* expressions have type int. */
2313 truthvalue_type_node = integer_type_node;
2314 truthvalue_true_node = integer_one_node;
2315 truthvalue_false_node = integer_zero_node;
2317 /* Even in C99, which has a real boolean type. */
2318 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2319 boolean_type_node));
2321 endlink = void_list_node;
2322 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2323 ptr_ftype_ptr
2324 = build_function_type (ptr_type_node,
2325 tree_cons (NULL_TREE, ptr_type_node, endlink));
2327 input_location = save_loc;
2329 pedantic_lvalues = true;
2331 make_fname_decl = c_make_fname_decl;
2332 start_fname_decls ();
2335 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2336 decl, NAME is the initialization string and TYPE_DEP indicates whether
2337 NAME depended on the type of the function. As we don't yet implement
2338 delayed emission of static data, we mark the decl as emitted
2339 so it is not placed in the output. Anything using it must therefore pull
2340 out the STRING_CST initializer directly. FIXME. */
2342 static tree
2343 c_make_fname_decl (tree id, int type_dep)
2345 const char *name = fname_as_string (type_dep);
2346 tree decl, type, init;
2347 size_t length = strlen (name);
2349 type = build_array_type
2350 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2351 build_index_type (size_int (length)));
2353 decl = build_decl (VAR_DECL, id, type);
2355 TREE_STATIC (decl) = 1;
2356 TREE_READONLY (decl) = 1;
2357 DECL_ARTIFICIAL (decl) = 1;
2359 init = build_string (length + 1, name);
2360 TREE_TYPE (init) = type;
2361 DECL_INITIAL (decl) = init;
2363 TREE_USED (decl) = 1;
2365 if (current_function_decl)
2367 DECL_CONTEXT (decl) = current_function_decl;
2368 bind (id, decl, current_function_scope);
2371 finish_decl (decl, init, NULL_TREE);
2373 return decl;
2376 /* Return a definition for a builtin function named NAME and whose data type
2377 is TYPE. TYPE should be a function type with argument types.
2378 FUNCTION_CODE tells later passes how to compile calls to this function.
2379 See tree.h for its possible values.
2381 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2382 the name to be called if we can't opencode the function. If
2383 ATTRS is nonzero, use that for the function's attribute list. */
2385 tree
2386 builtin_function (const char *name, tree type, int function_code,
2387 enum built_in_class class, const char *library_name,
2388 tree attrs)
2390 tree id = get_identifier (name);
2391 tree decl = build_decl (FUNCTION_DECL, id, type);
2392 TREE_PUBLIC (decl) = 1;
2393 DECL_EXTERNAL (decl) = 1;
2394 DECL_LANG_SPECIFIC (decl) = ggc_alloc_cleared (sizeof (struct lang_decl));
2395 DECL_BUILT_IN_CLASS (decl) = class;
2396 DECL_FUNCTION_CODE (decl) = function_code;
2397 if (library_name)
2398 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2399 make_decl_rtl (decl, NULL);
2401 /* Should never be called on a symbol with a preexisting meaning. */
2402 if (I_SYMBOL_BINDING (id))
2403 abort ();
2405 C_DECL_IN_EXTERNAL_SCOPE (decl) = 1;
2406 bind (id, decl, external_scope);
2408 /* Builtins in the implementation namespace are made visible without
2409 needing to be explicitly declared. See push_file_scope. */
2410 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2412 TREE_CHAIN (decl) = visible_builtins;
2413 visible_builtins = decl;
2416 /* Possibly apply some default attributes to this built-in function. */
2417 if (attrs)
2418 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2419 else
2420 decl_attributes (&decl, NULL_TREE, 0);
2422 return decl;
2425 /* Called when a declaration is seen that contains no names to declare.
2426 If its type is a reference to a structure, union or enum inherited
2427 from a containing scope, shadow that tag name for the current scope
2428 with a forward reference.
2429 If its type defines a new named structure or union
2430 or defines an enum, it is valid but we need not do anything here.
2431 Otherwise, it is an error. */
2433 void
2434 shadow_tag (tree declspecs)
2436 shadow_tag_warned (declspecs, 0);
2439 void
2440 shadow_tag_warned (tree declspecs, int warned)
2443 /* 1 => we have done a pedwarn. 2 => we have done a warning, but
2444 no pedwarn. */
2446 int found_tag = 0;
2447 tree link;
2448 tree specs, attrs;
2450 pending_invalid_xref = 0;
2452 /* Remove the attributes from declspecs, since they will confuse the
2453 following code. */
2454 split_specs_attrs (declspecs, &specs, &attrs);
2456 for (link = specs; link; link = TREE_CHAIN (link))
2458 tree value = TREE_VALUE (link);
2459 enum tree_code code = TREE_CODE (value);
2461 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2462 /* Used to test also that TYPE_SIZE (value) != 0.
2463 That caused warning for `struct foo;' at top level in the file. */
2465 tree name = TYPE_NAME (value);
2466 tree t;
2468 found_tag++;
2470 if (name == 0)
2472 if (warned != 1 && code != ENUMERAL_TYPE)
2473 /* Empty unnamed enum OK */
2475 pedwarn ("unnamed struct/union that defines no instances");
2476 warned = 1;
2479 else
2481 t = lookup_tag (code, name, 1);
2483 if (t == 0)
2485 t = make_node (code);
2486 pushtag (name, t);
2490 else
2492 if (!warned && ! in_system_header)
2494 warning ("useless keyword or type name in empty declaration");
2495 warned = 2;
2500 if (found_tag > 1)
2501 error ("two types specified in one empty declaration");
2503 if (warned != 1)
2505 if (found_tag == 0)
2506 pedwarn ("empty declaration");
2510 /* Construct an array declarator. EXPR is the expression inside [], or
2511 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2512 to the pointer to which a parameter array is converted). STATIC_P is
2513 nonzero if "static" is inside the [], zero otherwise. VLA_UNSPEC_P
2514 is nonzero is the array is [*], a VLA of unspecified length which is
2515 nevertheless a complete type (not currently implemented by GCC),
2516 zero otherwise. The declarator is constructed as an ARRAY_REF
2517 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2518 left of the [] (filled by in set_array_declarator_type) and operand 1
2519 is the expression inside; whose TREE_TYPE is the type qualifiers and
2520 which has TREE_STATIC set if "static" is used. */
2522 tree
2523 build_array_declarator (tree expr, tree quals, int static_p, int vla_unspec_p)
2525 tree decl;
2526 decl = build_nt (ARRAY_REF, NULL_TREE, expr);
2527 TREE_TYPE (decl) = quals;
2528 TREE_STATIC (decl) = (static_p ? 1 : 0);
2529 if (pedantic && !flag_isoc99)
2531 if (static_p || quals != NULL_TREE)
2532 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2533 if (vla_unspec_p)
2534 pedwarn ("ISO C90 does not support `[*]' array declarators");
2536 if (vla_unspec_p)
2537 warning ("GCC does not yet properly implement `[*]' array declarators");
2538 return decl;
2541 /* Set the type of an array declarator. DECL is the declarator, as
2542 constructed by build_array_declarator; TYPE is what appears on the left
2543 of the [] and goes in operand 0. ABSTRACT_P is nonzero if it is an
2544 abstract declarator, zero otherwise; this is used to reject static and
2545 type qualifiers in abstract declarators, where they are not in the
2546 C99 grammar. */
2548 tree
2549 set_array_declarator_type (tree decl, tree type, int abstract_p)
2551 TREE_OPERAND (decl, 0) = type;
2552 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2553 error ("static or type qualifiers in abstract declarator");
2554 return decl;
2557 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2559 tree
2560 groktypename (tree typename)
2562 tree specs, attrs;
2564 if (TREE_CODE (typename) != TREE_LIST)
2565 return typename;
2567 split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
2569 typename = grokdeclarator (TREE_VALUE (typename), specs, TYPENAME, 0,
2570 NULL);
2572 /* Apply attributes. */
2573 decl_attributes (&typename, attrs, 0);
2575 return typename;
2578 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2580 tree
2581 groktypename_in_parm_context (tree typename)
2583 if (TREE_CODE (typename) != TREE_LIST)
2584 return typename;
2585 return grokdeclarator (TREE_VALUE (typename),
2586 TREE_PURPOSE (typename),
2587 PARM, 0, NULL);
2590 /* Decode a declarator in an ordinary declaration or data definition.
2591 This is called as soon as the type information and variable name
2592 have been parsed, before parsing the initializer if any.
2593 Here we create the ..._DECL node, fill in its type,
2594 and put it on the list of decls for the current context.
2595 The ..._DECL node is returned as the value.
2597 Exception: for arrays where the length is not specified,
2598 the type is left null, to be filled in by `finish_decl'.
2600 Function definitions do not come here; they go to start_function
2601 instead. However, external and forward declarations of functions
2602 do go through here. Structure field declarations are done by
2603 grokfield and not through here. */
2605 tree
2606 start_decl (tree declarator, tree declspecs, int initialized, tree attributes)
2608 tree decl;
2609 tree tem;
2611 /* An object declared as __attribute__((deprecated)) suppresses
2612 warnings of uses of other deprecated items. */
2613 if (lookup_attribute ("deprecated", attributes))
2614 deprecated_state = DEPRECATED_SUPPRESS;
2616 decl = grokdeclarator (declarator, declspecs,
2617 NORMAL, initialized, NULL);
2619 deprecated_state = DEPRECATED_NORMAL;
2621 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2622 && MAIN_NAME_P (DECL_NAME (decl)))
2623 warning ("%J'%D' is usually a function", decl, decl);
2625 if (initialized)
2626 /* Is it valid for this decl to have an initializer at all?
2627 If not, set INITIALIZED to zero, which will indirectly
2628 tell 'finish_decl' to ignore the initializer once it is parsed. */
2629 switch (TREE_CODE (decl))
2631 case TYPE_DECL:
2632 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2633 initialized = 0;
2634 break;
2636 case FUNCTION_DECL:
2637 error ("function '%D' is initialized like a variable", decl);
2638 initialized = 0;
2639 break;
2641 case PARM_DECL:
2642 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2643 error ("parameter '%D' is initialized", decl);
2644 initialized = 0;
2645 break;
2647 default:
2648 /* Don't allow initializations for incomplete types except for
2649 arrays which might be completed by the initialization. */
2651 /* This can happen if the array size is an undefined macro.
2652 We already gave a warning, so we don't need another one. */
2653 if (TREE_TYPE (decl) == error_mark_node)
2654 initialized = 0;
2655 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2657 /* A complete type is ok if size is fixed. */
2659 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2660 || C_DECL_VARIABLE_SIZE (decl))
2662 error ("variable-sized object may not be initialized");
2663 initialized = 0;
2666 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2668 error ("variable '%D' has initializer but incomplete type", decl);
2669 initialized = 0;
2671 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2673 error ("elements of array '%D' have incomplete type", decl);
2674 initialized = 0;
2678 if (initialized)
2680 DECL_EXTERNAL (decl) = 0;
2681 if (current_scope == file_scope)
2682 TREE_STATIC (decl) = 1;
2684 /* Tell 'pushdecl' this is an initialized decl
2685 even though we don't yet have the initializer expression.
2686 Also tell 'finish_decl' it may store the real initializer. */
2687 DECL_INITIAL (decl) = error_mark_node;
2690 /* If this is a function declaration, write a record describing it to the
2691 prototypes file (if requested). */
2693 if (TREE_CODE (decl) == FUNCTION_DECL)
2694 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2696 /* ANSI specifies that a tentative definition which is not merged with
2697 a non-tentative definition behaves exactly like a definition with an
2698 initializer equal to zero. (Section 3.7.2)
2700 -fno-common gives strict ANSI behavior, though this tends to break
2701 a large body of code that grew up without this rule.
2703 Thread-local variables are never common, since there's no entrenched
2704 body of code to break, and it allows more efficient variable references
2705 in the presence of dynamic linking. */
2707 if (TREE_CODE (decl) == VAR_DECL
2708 && !initialized
2709 && TREE_PUBLIC (decl)
2710 && !DECL_THREAD_LOCAL (decl)
2711 && !flag_no_common)
2712 DECL_COMMON (decl) = 1;
2714 /* Set attributes here so if duplicate decl, will have proper attributes. */
2715 decl_attributes (&decl, attributes, 0);
2717 if (TREE_CODE (decl) == FUNCTION_DECL
2718 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2720 tree ce = declarator;
2722 if (TREE_CODE (ce) == INDIRECT_REF)
2723 ce = TREE_OPERAND (declarator, 0);
2724 if (TREE_CODE (ce) == CALL_EXPR)
2726 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2727 for (; args; args = TREE_CHAIN (args))
2729 tree type = TREE_TYPE (args);
2730 if (INTEGRAL_TYPE_P (type)
2731 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2732 DECL_ARG_TYPE (args) = integer_type_node;
2737 if (TREE_CODE (decl) == FUNCTION_DECL
2738 && DECL_DECLARED_INLINE_P (decl)
2739 && DECL_UNINLINABLE (decl)
2740 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2741 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2743 /* Add this decl to the current scope.
2744 TEM may equal DECL or it may be a previous decl of the same name. */
2745 tem = pushdecl (decl);
2747 return tem;
2750 /* Finish processing of a declaration;
2751 install its initial value.
2752 If the length of an array type is not known before,
2753 it must be determined now, from the initial value, or it is an error. */
2755 void
2756 finish_decl (tree decl, tree init, tree asmspec_tree)
2758 tree type = TREE_TYPE (decl);
2759 int was_incomplete = (DECL_SIZE (decl) == 0);
2760 const char *asmspec = 0;
2762 /* If a name was specified, get the string. */
2763 if (current_scope == file_scope)
2764 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
2765 if (asmspec_tree)
2766 asmspec = TREE_STRING_POINTER (asmspec_tree);
2768 /* If `start_decl' didn't like having an initialization, ignore it now. */
2769 if (init != 0 && DECL_INITIAL (decl) == 0)
2770 init = 0;
2772 /* Don't crash if parm is initialized. */
2773 if (TREE_CODE (decl) == PARM_DECL)
2774 init = 0;
2776 if (init)
2777 store_init_value (decl, init);
2779 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
2780 || TREE_CODE (decl) == FUNCTION_DECL
2781 || TREE_CODE (decl) == FIELD_DECL))
2782 objc_check_decl (decl);
2784 /* Deduce size of array from initialization, if not already known. */
2785 if (TREE_CODE (type) == ARRAY_TYPE
2786 && TYPE_DOMAIN (type) == 0
2787 && TREE_CODE (decl) != TYPE_DECL)
2789 int do_default
2790 = (TREE_STATIC (decl)
2791 /* Even if pedantic, an external linkage array
2792 may have incomplete type at first. */
2793 ? pedantic && !TREE_PUBLIC (decl)
2794 : !DECL_EXTERNAL (decl));
2795 int failure
2796 = complete_array_type (type, DECL_INITIAL (decl), do_default);
2798 /* Get the completed type made by complete_array_type. */
2799 type = TREE_TYPE (decl);
2801 if (failure == 1)
2802 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
2804 else if (failure == 2)
2806 if (do_default)
2807 error ("%Jarray size missing in '%D'", decl, decl);
2808 /* If a `static' var's size isn't known,
2809 make it extern as well as static, so it does not get
2810 allocated.
2811 If it is not `static', then do not mark extern;
2812 finish_incomplete_decl will give it a default size
2813 and it will get allocated. */
2814 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
2815 DECL_EXTERNAL (decl) = 1;
2818 /* TYPE_MAX_VALUE is always one less than the number of elements
2819 in the array, because we start counting at zero. Therefore,
2820 warn only if the value is less than zero. */
2821 else if (pedantic && TYPE_DOMAIN (type) != 0
2822 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
2823 error ("%Jzero or negative size array '%D'", decl, decl);
2825 layout_decl (decl, 0);
2828 if (TREE_CODE (decl) == VAR_DECL)
2830 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
2831 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
2832 layout_decl (decl, 0);
2834 if (DECL_SIZE (decl) == 0
2835 /* Don't give an error if we already gave one earlier. */
2836 && TREE_TYPE (decl) != error_mark_node
2837 && (TREE_STATIC (decl)
2839 /* A static variable with an incomplete type
2840 is an error if it is initialized.
2841 Also if it is not file scope.
2842 Otherwise, let it through, but if it is not `extern'
2843 then it may cause an error message later. */
2844 (DECL_INITIAL (decl) != 0
2845 || !DECL_FILE_SCOPE_P (decl))
2847 /* An automatic variable with an incomplete type
2848 is an error. */
2849 !DECL_EXTERNAL (decl)))
2851 error ("%Jstorage size of '%D' isn't known", decl, decl);
2852 TREE_TYPE (decl) = error_mark_node;
2855 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
2856 && DECL_SIZE (decl) != 0)
2858 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
2859 constant_expression_warning (DECL_SIZE (decl));
2860 else
2861 error ("%Jstorage size of '%D' isn't constant", decl, decl);
2864 if (TREE_USED (type))
2865 TREE_USED (decl) = 1;
2868 /* If this is a function and an assembler name is specified, reset DECL_RTL
2869 so we can give it its new name. Also, update built_in_decls if it
2870 was a normal built-in. */
2871 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
2873 /* ASMSPEC is given, and not the name of a register. Mark the
2874 name with a star so assemble_name won't munge it. */
2875 char *starred = alloca (strlen (asmspec) + 2);
2876 starred[0] = '*';
2877 strcpy (starred + 1, asmspec);
2879 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
2881 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
2882 SET_DECL_RTL (builtin, NULL_RTX);
2883 SET_DECL_ASSEMBLER_NAME (builtin, get_identifier (starred));
2884 #ifdef TARGET_MEM_FUNCTIONS
2885 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
2886 init_block_move_fn (starred);
2887 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
2888 init_block_clear_fn (starred);
2889 #else
2890 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BCOPY)
2891 init_block_move_fn (starred);
2892 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_BZERO)
2893 init_block_clear_fn (starred);
2894 #endif
2896 SET_DECL_RTL (decl, NULL_RTX);
2897 change_decl_assembler_name (decl, get_identifier (starred));
2900 /* If #pragma weak was used, mark the decl weak now. */
2901 if (current_scope == file_scope)
2902 maybe_apply_pragma_weak (decl);
2904 /* Output the assembler code and/or RTL code for variables and functions,
2905 unless the type is an undefined structure or union.
2906 If not, it will get done when the type is completed. */
2908 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
2910 /* This is a no-op in c-lang.c or something real in objc-act.c. */
2911 if (c_dialect_objc ())
2912 objc_check_decl (decl);
2914 if (DECL_FILE_SCOPE_P (decl))
2916 if (DECL_INITIAL (decl) == NULL_TREE
2917 || DECL_INITIAL (decl) == error_mark_node)
2918 /* Don't output anything
2919 when a tentative file-scope definition is seen.
2920 But at end of compilation, do output code for them. */
2921 DECL_DEFER_OUTPUT (decl) = 1;
2922 rest_of_decl_compilation (decl, asmspec, true, 0);
2924 else
2926 /* This is a local variable. If there is an ASMSPEC, the
2927 user has requested that we handle it specially. */
2928 if (asmspec)
2930 /* In conjunction with an ASMSPEC, the `register'
2931 keyword indicates that we should place the variable
2932 in a particular register. */
2933 if (C_DECL_REGISTER (decl))
2935 DECL_C_HARD_REGISTER (decl) = 1;
2936 /* This cannot be done for a structure with volatile
2937 fields, on which DECL_REGISTER will have been
2938 reset. */
2939 if (!DECL_REGISTER (decl))
2940 error ("cannot put object with volatile field into register");
2943 /* If this is not a static variable, issue a warning.
2944 It doesn't make any sense to give an ASMSPEC for an
2945 ordinary, non-register local variable. Historically,
2946 GCC has accepted -- but ignored -- the ASMSPEC in
2947 this case. */
2948 if (TREE_CODE (decl) == VAR_DECL
2949 && !C_DECL_REGISTER (decl)
2950 && !TREE_STATIC (decl))
2951 warning ("%Jignoring asm-specifier for non-static local "
2952 "variable '%D'", decl, decl);
2953 else
2954 change_decl_assembler_name (decl, get_identifier (asmspec));
2957 if (TREE_CODE (decl) != FUNCTION_DECL)
2958 add_decl_stmt (decl);
2961 if (!DECL_FILE_SCOPE_P (decl))
2963 /* Recompute the RTL of a local array now
2964 if it used to be an incomplete type. */
2965 if (was_incomplete
2966 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
2968 /* If we used it already as memory, it must stay in memory. */
2969 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
2970 /* If it's still incomplete now, no init will save it. */
2971 if (DECL_SIZE (decl) == 0)
2972 DECL_INITIAL (decl) = 0;
2977 /* If this was marked 'used', be sure it will be output. */
2978 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
2979 mark_referenced (DECL_ASSEMBLER_NAME (decl));
2981 if (TREE_CODE (decl) == TYPE_DECL)
2983 if (!DECL_FILE_SCOPE_P (decl)
2984 && variably_modified_type_p (TREE_TYPE (decl)))
2985 add_decl_stmt (decl);
2987 rest_of_decl_compilation (decl, NULL, DECL_FILE_SCOPE_P (decl), 0);
2990 /* At the end of a declaration, throw away any variable type sizes
2991 of types defined inside that declaration. There is no use
2992 computing them in the following function definition. */
2993 if (current_scope == file_scope)
2994 get_pending_sizes ();
2996 /* Install a cleanup (aka destructor) if one was given. */
2997 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
2999 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3000 if (attr)
3002 static bool eh_initialized_p;
3004 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3005 tree cleanup_decl = lookup_name (cleanup_id);
3006 tree cleanup;
3008 /* Build "cleanup(&decl)" for the destructor. */
3009 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3010 cleanup = build_tree_list (NULL_TREE, cleanup);
3011 cleanup = build_function_call (cleanup_decl, cleanup);
3013 /* Don't warn about decl unused; the cleanup uses it. */
3014 TREE_USED (decl) = 1;
3016 /* Initialize EH, if we've been told to do so. */
3017 if (flag_exceptions && !eh_initialized_p)
3019 eh_initialized_p = true;
3020 eh_personality_libfunc
3021 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3022 ? "__gcc_personality_sj0"
3023 : "__gcc_personality_v0");
3024 using_eh_for_cleanups ();
3027 add_stmt (build_stmt (CLEANUP_STMT, decl, cleanup));
3032 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3033 and push that on the current scope. */
3035 void
3036 push_parm_decl (tree parm)
3038 tree decl;
3040 /* Don't attempt to expand sizes while parsing this decl.
3041 (We can get here with i_s_e 1 somehow from Objective-C.) */
3042 int save_immediate_size_expand = immediate_size_expand;
3043 immediate_size_expand = 0;
3045 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3046 TREE_PURPOSE (TREE_PURPOSE (parm)),
3047 PARM, 0, NULL);
3048 decl_attributes (&decl, TREE_VALUE (parm), 0);
3050 decl = pushdecl (decl);
3052 finish_decl (decl, NULL_TREE, NULL_TREE);
3054 immediate_size_expand = save_immediate_size_expand;
3057 /* Mark all the parameter declarations to date as forward decls.
3058 Also diagnose use of this extension. */
3060 void
3061 mark_forward_parm_decls (void)
3063 struct c_binding *b;
3065 if (pedantic && !current_scope->warned_forward_parm_decls)
3067 pedwarn ("ISO C forbids forward parameter declarations");
3068 current_scope->warned_forward_parm_decls = true;
3071 for (b = current_scope->bindings; b; b = b->prev)
3072 if (TREE_CODE (b->decl) == PARM_DECL)
3073 TREE_ASM_WRITTEN (b->decl) = 1;
3076 static GTY(()) int compound_literal_number;
3078 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3079 literal, which may be an incomplete array type completed by the
3080 initializer; INIT is a CONSTRUCTOR that initializes the compound
3081 literal. */
3083 tree
3084 build_compound_literal (tree type, tree init)
3086 /* We do not use start_decl here because we have a type, not a declarator;
3087 and do not use finish_decl because the decl should be stored inside
3088 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_STMT. */
3089 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3090 tree complit;
3091 tree stmt;
3092 DECL_EXTERNAL (decl) = 0;
3093 TREE_PUBLIC (decl) = 0;
3094 TREE_STATIC (decl) = (current_scope == file_scope);
3095 DECL_CONTEXT (decl) = current_function_decl;
3096 TREE_USED (decl) = 1;
3097 TREE_TYPE (decl) = type;
3098 TREE_READONLY (decl) = TYPE_READONLY (type);
3099 store_init_value (decl, init);
3101 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3103 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3104 if (failure)
3105 abort ();
3108 type = TREE_TYPE (decl);
3109 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3110 return error_mark_node;
3112 stmt = build_stmt (DECL_STMT, decl);
3113 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3114 TREE_SIDE_EFFECTS (complit) = 1;
3116 layout_decl (decl, 0);
3118 if (TREE_STATIC (decl))
3120 /* This decl needs a name for the assembler output. We also need
3121 a unique suffix to be added to the name. */
3122 char *name;
3124 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3125 compound_literal_number);
3126 compound_literal_number++;
3127 DECL_NAME (decl) = get_identifier (name);
3128 DECL_DEFER_OUTPUT (decl) = 1;
3129 DECL_COMDAT (decl) = 1;
3130 DECL_ARTIFICIAL (decl) = 1;
3131 pushdecl (decl);
3132 rest_of_decl_compilation (decl, NULL, 1, 0);
3135 return complit;
3138 /* Make TYPE a complete type based on INITIAL_VALUE.
3139 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3140 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3143 complete_array_type (tree type, tree initial_value, int do_default)
3145 tree maxindex = NULL_TREE;
3146 int value = 0;
3148 if (initial_value)
3150 /* Note MAXINDEX is really the maximum index,
3151 one less than the size. */
3152 if (TREE_CODE (initial_value) == STRING_CST)
3154 int eltsize
3155 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3156 maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
3157 / eltsize) - 1, 0);
3159 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3161 tree elts = CONSTRUCTOR_ELTS (initial_value);
3162 maxindex = build_int_2 (-1, -1);
3163 for (; elts; elts = TREE_CHAIN (elts))
3165 if (TREE_PURPOSE (elts))
3166 maxindex = TREE_PURPOSE (elts);
3167 else
3168 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3169 maxindex, integer_one_node));
3171 maxindex = copy_node (maxindex);
3173 else
3175 /* Make an error message unless that happened already. */
3176 if (initial_value != error_mark_node)
3177 value = 1;
3179 /* Prevent further error messages. */
3180 maxindex = build_int_2 (0, 0);
3184 if (!maxindex)
3186 if (do_default)
3187 maxindex = build_int_2 (0, 0);
3188 value = 2;
3191 if (maxindex)
3193 TYPE_DOMAIN (type) = build_index_type (maxindex);
3194 if (!TREE_TYPE (maxindex))
3195 TREE_TYPE (maxindex) = TYPE_DOMAIN (type);
3198 /* Lay out the type now that we can get the real answer. */
3200 layout_type (type);
3202 return value;
3205 /* Determine whether TYPE is a structure with a flexible array member,
3206 or a union containing such a structure (possibly recursively). */
3208 static bool
3209 flexible_array_type_p (tree type)
3211 tree x;
3212 switch (TREE_CODE (type))
3214 case RECORD_TYPE:
3215 x = TYPE_FIELDS (type);
3216 if (x == NULL_TREE)
3217 return false;
3218 while (TREE_CHAIN (x) != NULL_TREE)
3219 x = TREE_CHAIN (x);
3220 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3221 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3222 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3223 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3224 return true;
3225 return false;
3226 case UNION_TYPE:
3227 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3229 if (flexible_array_type_p (TREE_TYPE (x)))
3230 return true;
3232 return false;
3233 default:
3234 return false;
3238 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3239 replacing with appropriate values if they are invalid. */
3240 static void
3241 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3243 tree type_mv;
3244 unsigned int max_width;
3245 unsigned HOST_WIDE_INT w;
3246 const char *name = orig_name ? orig_name: _("<anonymous>");
3248 /* Necessary? */
3249 STRIP_NOPS (*width);
3251 /* Detect and ignore out of range field width and process valid
3252 field widths. */
3253 if (TREE_CODE (*width) != INTEGER_CST)
3255 error ("bit-field `%s' width not an integer constant", name);
3256 *width = integer_one_node;
3258 else
3260 constant_expression_warning (*width);
3261 if (tree_int_cst_sgn (*width) < 0)
3263 error ("negative width in bit-field `%s'", name);
3264 *width = integer_one_node;
3266 else if (integer_zerop (*width) && orig_name)
3268 error ("zero width for bit-field `%s'", name);
3269 *width = integer_one_node;
3273 /* Detect invalid bit-field type. */
3274 if (TREE_CODE (*type) != INTEGER_TYPE
3275 && TREE_CODE (*type) != BOOLEAN_TYPE
3276 && TREE_CODE (*type) != ENUMERAL_TYPE)
3278 error ("bit-field `%s' has invalid type", name);
3279 *type = unsigned_type_node;
3282 type_mv = TYPE_MAIN_VARIANT (*type);
3283 if (pedantic
3284 && type_mv != integer_type_node
3285 && type_mv != unsigned_type_node
3286 && type_mv != boolean_type_node)
3287 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3289 if (type_mv == boolean_type_node)
3290 max_width = CHAR_TYPE_SIZE;
3291 else
3292 max_width = TYPE_PRECISION (*type);
3294 if (0 < compare_tree_int (*width, max_width))
3296 error ("width of `%s' exceeds its type", name);
3297 w = max_width;
3298 *width = build_int_2 (w, 0);
3300 else
3301 w = tree_low_cst (*width, 1);
3303 if (TREE_CODE (*type) == ENUMERAL_TYPE
3304 && (w < min_precision (TYPE_MIN_VALUE (*type), TYPE_UNSIGNED (*type))
3305 || w < min_precision (TYPE_MAX_VALUE (*type),
3306 TYPE_UNSIGNED (*type))))
3307 warning ("`%s' is narrower than values of its type", name);
3310 /* Given declspecs and a declarator,
3311 determine the name and type of the object declared
3312 and construct a ..._DECL node for it.
3313 (In one case we can return a ..._TYPE node instead.
3314 For invalid input we sometimes return 0.)
3316 DECLSPECS is a chain of tree_list nodes whose value fields
3317 are the storage classes and type specifiers.
3319 DECL_CONTEXT says which syntactic context this declaration is in:
3320 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3321 FUNCDEF for a function definition. Like NORMAL but a few different
3322 error messages in each case. Return value may be zero meaning
3323 this definition is too screwy to try to parse.
3324 PARM for a parameter declaration (either within a function prototype
3325 or before a function body). Make a PARM_DECL, or return void_type_node.
3326 TYPENAME if for a typename (in a cast or sizeof).
3327 Don't make a DECL node; just return the ..._TYPE node.
3328 FIELD for a struct or union field; make a FIELD_DECL.
3329 INITIALIZED is 1 if the decl has an initializer.
3330 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3331 representing the width of the bit-field.
3333 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3334 It may also be so in the PARM case, for a prototype where the
3335 argument type is specified but not the name.
3337 This function is where the complicated C meanings of `static'
3338 and `extern' are interpreted. */
3340 static tree
3341 grokdeclarator (tree declarator, tree declspecs,
3342 enum decl_context decl_context, int initialized, tree *width)
3344 int specbits = 0;
3345 tree spec;
3346 tree type = NULL_TREE;
3347 int longlong = 0;
3348 int constp;
3349 int restrictp;
3350 int volatilep;
3351 int type_quals = TYPE_UNQUALIFIED;
3352 int inlinep;
3353 int explicit_int = 0;
3354 int explicit_char = 0;
3355 int defaulted_int = 0;
3356 tree typedef_decl = 0;
3357 const char *name, *orig_name;
3358 tree typedef_type = 0;
3359 int funcdef_flag = 0;
3360 enum tree_code innermost_code = ERROR_MARK;
3361 int size_varies = 0;
3362 tree decl_attr = NULL_TREE;
3363 tree array_ptr_quals = NULL_TREE;
3364 int array_parm_static = 0;
3365 tree returned_attrs = NULL_TREE;
3366 bool bitfield = width != NULL;
3367 tree element_type;
3368 tree arg_info = NULL_TREE;
3370 if (decl_context == FUNCDEF)
3371 funcdef_flag = 1, decl_context = NORMAL;
3373 /* Look inside a declarator for the name being declared
3374 and get it as a string, for an error message. */
3376 tree decl = declarator;
3377 name = 0;
3379 while (decl)
3380 switch (TREE_CODE (decl))
3382 case ARRAY_REF:
3383 case INDIRECT_REF:
3384 case CALL_EXPR:
3385 innermost_code = TREE_CODE (decl);
3386 decl = TREE_OPERAND (decl, 0);
3387 break;
3389 case TREE_LIST:
3390 decl = TREE_VALUE (decl);
3391 break;
3393 case IDENTIFIER_NODE:
3394 name = IDENTIFIER_POINTER (decl);
3395 decl = 0;
3396 break;
3398 default:
3399 abort ();
3401 orig_name = name;
3402 if (name == 0)
3403 name = "type name";
3406 /* A function definition's declarator must have the form of
3407 a function declarator. */
3409 if (funcdef_flag && innermost_code != CALL_EXPR)
3410 return 0;
3412 /* If this looks like a function definition, make it one,
3413 even if it occurs where parms are expected.
3414 Then store_parm_decls will reject it and not use it as a parm. */
3415 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3416 decl_context = PARM;
3418 /* Look through the decl specs and record which ones appear.
3419 Some typespecs are defined as built-in typenames.
3420 Others, the ones that are modifiers of other types,
3421 are represented by bits in SPECBITS: set the bits for
3422 the modifiers that appear. Storage class keywords are also in SPECBITS.
3424 If there is a typedef name or a type, store the type in TYPE.
3425 This includes builtin typedefs such as `int'.
3427 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3428 and did not come from a user typedef.
3430 Set LONGLONG if `long' is mentioned twice. */
3432 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3434 tree id = TREE_VALUE (spec);
3436 /* If the entire declaration is itself tagged as deprecated then
3437 suppress reports of deprecated items. */
3438 if (id && TREE_DEPRECATED (id))
3440 if (deprecated_state != DEPRECATED_SUPPRESS)
3441 warn_deprecated_use (id);
3444 if (id == ridpointers[(int) RID_INT])
3445 explicit_int = 1;
3446 if (id == ridpointers[(int) RID_CHAR])
3447 explicit_char = 1;
3449 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3451 enum rid i = C_RID_CODE (id);
3452 if ((int) i <= (int) RID_LAST_MODIFIER)
3454 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3456 if (longlong)
3457 error ("`long long long' is too long for GCC");
3458 else
3460 if (pedantic && !flag_isoc99 && ! in_system_header
3461 && warn_long_long)
3462 pedwarn ("ISO C90 does not support `long long'");
3463 longlong = 1;
3466 else if (specbits & (1 << (int) i))
3468 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3470 if (pedantic && !flag_isoc99)
3471 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3473 else
3474 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3477 /* Diagnose "__thread extern". Recall that this list
3478 is in the reverse order seen in the text. */
3479 if (i == RID_THREAD
3480 && (specbits & (1 << (int) RID_EXTERN
3481 | 1 << (int) RID_STATIC)))
3483 if (specbits & 1 << (int) RID_EXTERN)
3484 error ("`__thread' before `extern'");
3485 else
3486 error ("`__thread' before `static'");
3489 specbits |= 1 << (int) i;
3490 goto found;
3493 if (type)
3494 error ("two or more data types in declaration of `%s'", name);
3495 /* Actual typedefs come to us as TYPE_DECL nodes. */
3496 else if (TREE_CODE (id) == TYPE_DECL)
3498 if (TREE_TYPE (id) == error_mark_node)
3499 ; /* Allow the type to default to int to avoid cascading errors. */
3500 else
3502 type = TREE_TYPE (id);
3503 decl_attr = DECL_ATTRIBUTES (id);
3504 typedef_decl = id;
3507 /* Built-in types come as identifiers. */
3508 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3510 tree t = lookup_name (id);
3511 if (!t || TREE_CODE (t) != TYPE_DECL)
3512 error ("`%s' fails to be a typedef or built in type",
3513 IDENTIFIER_POINTER (id));
3514 else if (TREE_TYPE (t) == error_mark_node)
3516 else
3518 type = TREE_TYPE (t);
3519 typedef_decl = t;
3522 else if (TREE_CODE (id) != ERROR_MARK)
3523 type = id;
3525 found:
3529 typedef_type = type;
3530 if (type)
3531 size_varies = C_TYPE_VARIABLE_SIZE (type);
3533 /* No type at all: default to `int', and set DEFAULTED_INT
3534 because it was not a user-defined typedef. */
3536 if (type == 0)
3538 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3539 | (1 << (int) RID_SIGNED)
3540 | (1 << (int) RID_UNSIGNED)
3541 | (1 << (int) RID_COMPLEX))))
3542 /* Don't warn about typedef foo = bar. */
3543 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3544 && ! in_system_header)
3546 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3547 and this is a function, or if -Wimplicit; prefer the former
3548 warning since it is more explicit. */
3549 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3550 && funcdef_flag)
3551 warn_about_return_type = 1;
3552 else if (warn_implicit_int || flag_isoc99)
3553 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3554 name);
3557 defaulted_int = 1;
3558 type = integer_type_node;
3561 /* Now process the modifiers that were specified
3562 and check for invalid combinations. */
3564 /* Long double is a special combination. */
3566 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3567 && TYPE_MAIN_VARIANT (type) == double_type_node)
3569 specbits &= ~(1 << (int) RID_LONG);
3570 type = long_double_type_node;
3573 /* Check all other uses of type modifiers. */
3575 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3576 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3578 int ok = 0;
3580 if ((specbits & 1 << (int) RID_LONG)
3581 && (specbits & 1 << (int) RID_SHORT))
3582 error ("both long and short specified for `%s'", name);
3583 else if (((specbits & 1 << (int) RID_LONG)
3584 || (specbits & 1 << (int) RID_SHORT))
3585 && explicit_char)
3586 error ("long or short specified with char for `%s'", name);
3587 else if (((specbits & 1 << (int) RID_LONG)
3588 || (specbits & 1 << (int) RID_SHORT))
3589 && TREE_CODE (type) == REAL_TYPE)
3591 static int already = 0;
3593 error ("long or short specified with floating type for `%s'", name);
3594 if (! already && ! pedantic)
3596 error ("the only valid combination is `long double'");
3597 already = 1;
3600 else if ((specbits & 1 << (int) RID_SIGNED)
3601 && (specbits & 1 << (int) RID_UNSIGNED))
3602 error ("both signed and unsigned specified for `%s'", name);
3603 else if (TREE_CODE (type) != INTEGER_TYPE)
3604 error ("long, short, signed or unsigned invalid for `%s'", name);
3605 else
3607 ok = 1;
3608 if (!explicit_int && !defaulted_int && !explicit_char)
3610 error ("long, short, signed or unsigned used invalidly for `%s'",
3611 name);
3612 ok = 0;
3616 /* Discard the type modifiers if they are invalid. */
3617 if (! ok)
3619 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3620 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3621 longlong = 0;
3625 if ((specbits & (1 << (int) RID_COMPLEX))
3626 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3628 error ("complex invalid for `%s'", name);
3629 specbits &= ~(1 << (int) RID_COMPLEX);
3632 /* Decide whether an integer type is signed or not.
3633 Optionally treat bit-fields as signed by default. */
3634 if (specbits & 1 << (int) RID_UNSIGNED
3635 || (bitfield && ! flag_signed_bitfields
3636 && (explicit_int || defaulted_int || explicit_char
3637 /* A typedef for plain `int' without `signed'
3638 can be controlled just like plain `int'. */
3639 || ! (typedef_decl != 0
3640 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3641 && TREE_CODE (type) != ENUMERAL_TYPE
3642 && !(specbits & 1 << (int) RID_SIGNED)))
3644 if (longlong)
3645 type = long_long_unsigned_type_node;
3646 else if (specbits & 1 << (int) RID_LONG)
3647 type = long_unsigned_type_node;
3648 else if (specbits & 1 << (int) RID_SHORT)
3649 type = short_unsigned_type_node;
3650 else if (type == char_type_node)
3651 type = unsigned_char_type_node;
3652 else if (typedef_decl)
3653 type = c_common_unsigned_type (type);
3654 else
3655 type = unsigned_type_node;
3657 else if ((specbits & 1 << (int) RID_SIGNED)
3658 && type == char_type_node)
3659 type = signed_char_type_node;
3660 else if (longlong)
3661 type = long_long_integer_type_node;
3662 else if (specbits & 1 << (int) RID_LONG)
3663 type = long_integer_type_node;
3664 else if (specbits & 1 << (int) RID_SHORT)
3665 type = short_integer_type_node;
3667 if (specbits & 1 << (int) RID_COMPLEX)
3669 if (pedantic && !flag_isoc99)
3670 pedwarn ("ISO C90 does not support complex types");
3671 /* If we just have "complex", it is equivalent to
3672 "complex double", but if any modifiers at all are specified it is
3673 the complex form of TYPE. E.g, "complex short" is
3674 "complex short int". */
3676 if (defaulted_int && ! longlong
3677 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3678 | (1 << (int) RID_SIGNED)
3679 | (1 << (int) RID_UNSIGNED))))
3681 if (pedantic)
3682 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3683 type = complex_double_type_node;
3685 else if (type == integer_type_node)
3687 if (pedantic)
3688 pedwarn ("ISO C does not support complex integer types");
3689 type = complex_integer_type_node;
3691 else if (type == float_type_node)
3692 type = complex_float_type_node;
3693 else if (type == double_type_node)
3694 type = complex_double_type_node;
3695 else if (type == long_double_type_node)
3696 type = complex_long_double_type_node;
3697 else
3699 if (pedantic)
3700 pedwarn ("ISO C does not support complex integer types");
3701 type = build_complex_type (type);
3705 /* Check the type and width of a bit-field. */
3706 if (bitfield)
3707 check_bitfield_type_and_width (&type, width, orig_name);
3709 /* Figure out the type qualifiers for the declaration. There are
3710 two ways a declaration can become qualified. One is something
3711 like `const int i' where the `const' is explicit. Another is
3712 something like `typedef const int CI; CI i' where the type of the
3713 declaration contains the `const'. A third possibility is that
3714 there is a type qualifier on the element type of a typedefed
3715 array type, in which case we should extract that qualifier so
3716 that c_apply_type_quals_to_decls receives the full list of
3717 qualifiers to work with (C90 is not entirely clear about whether
3718 duplicate qualifiers should be diagnosed in this case, but it
3719 seems most appropriate to do so). */
3720 element_type = strip_array_types (type);
3721 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3722 restrictp
3723 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3724 volatilep
3725 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3726 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3727 if (pedantic && !flag_isoc99)
3729 if (constp > 1)
3730 pedwarn ("duplicate `const'");
3731 if (restrictp > 1)
3732 pedwarn ("duplicate `restrict'");
3733 if (volatilep > 1)
3734 pedwarn ("duplicate `volatile'");
3736 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3737 type = TYPE_MAIN_VARIANT (type);
3738 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3739 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3740 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3742 /* Warn if two storage classes are given. Default to `auto'. */
3745 int nclasses = 0;
3747 if (specbits & 1 << (int) RID_AUTO) nclasses++;
3748 if (specbits & 1 << (int) RID_STATIC) nclasses++;
3749 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
3750 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
3751 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
3753 /* "static __thread" and "extern __thread" are allowed. */
3754 if ((specbits & (1 << (int) RID_THREAD
3755 | 1 << (int) RID_STATIC
3756 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
3757 nclasses++;
3759 /* Warn about storage classes that are invalid for certain
3760 kinds of declarations (parameters, typenames, etc.). */
3762 if (nclasses > 1)
3763 error ("multiple storage classes in declaration of `%s'", name);
3764 else if (funcdef_flag
3765 && (specbits
3766 & ((1 << (int) RID_REGISTER)
3767 | (1 << (int) RID_AUTO)
3768 | (1 << (int) RID_TYPEDEF)
3769 | (1 << (int) RID_THREAD))))
3771 if (specbits & 1 << (int) RID_AUTO
3772 && (pedantic || current_scope == file_scope))
3773 pedwarn ("function definition declared `auto'");
3774 if (specbits & 1 << (int) RID_REGISTER)
3775 error ("function definition declared `register'");
3776 if (specbits & 1 << (int) RID_TYPEDEF)
3777 error ("function definition declared `typedef'");
3778 if (specbits & 1 << (int) RID_THREAD)
3779 error ("function definition declared `__thread'");
3780 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3781 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
3783 else if (decl_context != NORMAL && nclasses > 0)
3785 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
3787 else
3789 switch (decl_context)
3791 case FIELD:
3792 error ("storage class specified for structure field `%s'",
3793 name);
3794 break;
3795 case PARM:
3796 error ("storage class specified for parameter `%s'", name);
3797 break;
3798 default:
3799 error ("storage class specified for typename");
3800 break;
3802 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
3803 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
3804 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
3807 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
3809 /* `extern' with initialization is invalid if not at file scope. */
3810 if (current_scope == file_scope)
3811 warning ("`%s' initialized and declared `extern'", name);
3812 else
3813 error ("`%s' has both `extern' and initializer", name);
3815 else if (current_scope == file_scope)
3817 if (specbits & 1 << (int) RID_AUTO)
3818 error ("file-scope declaration of `%s' specifies `auto'", name);
3820 else
3822 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
3823 error ("nested function `%s' declared `extern'", name);
3824 else if ((specbits & (1 << (int) RID_THREAD
3825 | 1 << (int) RID_EXTERN
3826 | 1 << (int) RID_STATIC))
3827 == (1 << (int) RID_THREAD))
3829 error ("function-scope `%s' implicitly auto and declared `__thread'",
3830 name);
3831 specbits &= ~(1 << (int) RID_THREAD);
3836 /* Now figure out the structure of the declarator proper.
3837 Descend through it, creating more complex types, until we reach
3838 the declared identifier (or NULL_TREE, in an absolute declarator). */
3840 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
3842 if (type == error_mark_node)
3844 declarator = TREE_OPERAND (declarator, 0);
3845 continue;
3848 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
3849 an INDIRECT_REF (for *...),
3850 a CALL_EXPR (for ...(...)),
3851 a TREE_LIST (for nested attributes),
3852 an identifier (for the name being declared)
3853 or a null pointer (for the place in an absolute declarator
3854 where the name was omitted).
3855 For the last two cases, we have just exited the loop.
3857 At this point, TYPE is the type of elements of an array,
3858 or for a function to return, or for a pointer to point to.
3859 After this sequence of ifs, TYPE is the type of the
3860 array or function or pointer, and DECLARATOR has had its
3861 outermost layer removed. */
3863 if (array_ptr_quals != NULL_TREE || array_parm_static)
3865 /* Only the innermost declarator (making a parameter be of
3866 array type which is converted to pointer type)
3867 may have static or type qualifiers. */
3868 error ("static or type qualifiers in non-parameter array declarator");
3869 array_ptr_quals = NULL_TREE;
3870 array_parm_static = 0;
3873 if (TREE_CODE (declarator) == TREE_LIST)
3875 /* We encode a declarator with embedded attributes using
3876 a TREE_LIST. */
3877 tree attrs = TREE_PURPOSE (declarator);
3878 tree inner_decl;
3879 int attr_flags = 0;
3880 declarator = TREE_VALUE (declarator);
3881 inner_decl = declarator;
3882 while (inner_decl != NULL_TREE
3883 && TREE_CODE (inner_decl) == TREE_LIST)
3884 inner_decl = TREE_VALUE (inner_decl);
3885 if (inner_decl == NULL_TREE
3886 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
3887 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
3888 else if (TREE_CODE (inner_decl) == CALL_EXPR)
3889 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
3890 else if (TREE_CODE (inner_decl) == ARRAY_REF)
3891 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
3892 returned_attrs = decl_attributes (&type,
3893 chainon (returned_attrs, attrs),
3894 attr_flags);
3896 else if (TREE_CODE (declarator) == ARRAY_REF)
3898 tree itype = NULL_TREE;
3899 tree size = TREE_OPERAND (declarator, 1);
3900 /* The index is a signed object `sizetype' bits wide. */
3901 tree index_type = c_common_signed_type (sizetype);
3903 array_ptr_quals = TREE_TYPE (declarator);
3904 array_parm_static = TREE_STATIC (declarator);
3906 declarator = TREE_OPERAND (declarator, 0);
3908 /* Check for some types that there cannot be arrays of. */
3910 if (VOID_TYPE_P (type))
3912 error ("declaration of `%s' as array of voids", name);
3913 type = error_mark_node;
3916 if (TREE_CODE (type) == FUNCTION_TYPE)
3918 error ("declaration of `%s' as array of functions", name);
3919 type = error_mark_node;
3922 if (pedantic && flexible_array_type_p (type))
3923 pedwarn ("invalid use of structure with flexible array member");
3925 if (size == error_mark_node)
3926 type = error_mark_node;
3928 if (type == error_mark_node)
3929 continue;
3931 /* If size was specified, set ITYPE to a range-type for that size.
3932 Otherwise, ITYPE remains null. finish_decl may figure it out
3933 from an initial value. */
3935 if (size)
3937 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
3938 STRIP_TYPE_NOPS (size);
3940 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
3942 error ("size of array `%s' has non-integer type", name);
3943 size = integer_one_node;
3946 if (pedantic && integer_zerop (size))
3947 pedwarn ("ISO C forbids zero-size array `%s'", name);
3949 if (TREE_CODE (size) == INTEGER_CST)
3951 constant_expression_warning (size);
3952 if (tree_int_cst_sgn (size) < 0)
3954 error ("size of array `%s' is negative", name);
3955 size = integer_one_node;
3958 else
3960 /* Make sure the array size remains visibly nonconstant
3961 even if it is (eg) a const variable with known value. */
3962 size_varies = 1;
3964 if (!flag_isoc99 && pedantic)
3966 if (TREE_CONSTANT (size))
3967 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
3968 name);
3969 else
3970 pedwarn ("ISO C90 forbids variable-size array `%s'",
3971 name);
3975 if (integer_zerop (size))
3977 /* A zero-length array cannot be represented with an
3978 unsigned index type, which is what we'll get with
3979 build_index_type. Create an open-ended range instead. */
3980 itype = build_range_type (sizetype, size, NULL_TREE);
3982 else
3984 /* Compute the maximum valid index, that is, size - 1.
3985 Do the calculation in index_type, so that if it is
3986 a variable the computations will be done in the
3987 proper mode. */
3988 itype = fold (build (MINUS_EXPR, index_type,
3989 convert (index_type, size),
3990 convert (index_type, size_one_node)));
3992 /* If that overflowed, the array is too big.
3993 ??? While a size of INT_MAX+1 technically shouldn't
3994 cause an overflow (because we subtract 1), the overflow
3995 is recorded during the conversion to index_type, before
3996 the subtraction. Handling this case seems like an
3997 unnecessary complication. */
3998 if (TREE_OVERFLOW (itype))
4000 error ("size of array `%s' is too large", name);
4001 type = error_mark_node;
4002 continue;
4005 if (size_varies)
4007 /* We must be able to distinguish the
4008 SAVE_EXPR_CONTEXT for the variably-sized type
4009 so that we can set it correctly in
4010 set_save_expr_context. The convention is
4011 that all SAVE_EXPRs that need to be reset
4012 have NULL_TREE for their SAVE_EXPR_CONTEXT. */
4013 tree cfd = current_function_decl;
4014 if (decl_context == PARM)
4015 current_function_decl = NULL_TREE;
4016 itype = variable_size (itype);
4017 if (decl_context == PARM)
4018 current_function_decl = cfd;
4020 itype = build_index_type (itype);
4023 else if (decl_context == FIELD)
4025 if (pedantic && !flag_isoc99 && !in_system_header)
4026 pedwarn ("ISO C90 does not support flexible array members");
4028 /* ISO C99 Flexible array members are effectively identical
4029 to GCC's zero-length array extension. */
4030 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4033 /* If pedantic, complain about arrays of incomplete types. */
4035 if (pedantic && !COMPLETE_TYPE_P (type))
4036 pedwarn ("array type has incomplete element type");
4038 /* Build the array type itself, then merge any constancy or
4039 volatility into the target type. We must do it in this order
4040 to ensure that the TYPE_MAIN_VARIANT field of the array type
4041 is set correctly. */
4043 type = build_array_type (type, itype);
4044 if (type_quals)
4045 type = c_build_qualified_type (type, type_quals);
4047 if (size_varies)
4048 C_TYPE_VARIABLE_SIZE (type) = 1;
4050 /* The GCC extension for zero-length arrays differs from
4051 ISO flexible array members in that sizeof yields zero. */
4052 if (size && integer_zerop (size))
4054 layout_type (type);
4055 TYPE_SIZE (type) = bitsize_zero_node;
4056 TYPE_SIZE_UNIT (type) = size_zero_node;
4058 if (decl_context != PARM
4059 && (array_ptr_quals != NULL_TREE || array_parm_static))
4061 error ("static or type qualifiers in non-parameter array declarator");
4062 array_ptr_quals = NULL_TREE;
4063 array_parm_static = 0;
4066 else if (TREE_CODE (declarator) == CALL_EXPR)
4068 /* Say it's a definition only for the CALL_EXPR closest to
4069 the identifier. */
4070 bool really_funcdef = (funcdef_flag
4071 && (TREE_CODE (TREE_OPERAND (declarator, 0))
4072 == IDENTIFIER_NODE));
4073 tree arg_types;
4075 /* Declaring a function type.
4076 Make sure we have a valid type for the function to return. */
4077 if (type == error_mark_node)
4078 continue;
4080 size_varies = 0;
4082 /* Warn about some types functions can't return. */
4084 if (TREE_CODE (type) == FUNCTION_TYPE)
4086 error ("`%s' declared as function returning a function", name);
4087 type = integer_type_node;
4089 if (TREE_CODE (type) == ARRAY_TYPE)
4091 error ("`%s' declared as function returning an array", name);
4092 type = integer_type_node;
4095 /* Construct the function type and go to the next
4096 inner layer of declarator. */
4097 arg_info = TREE_OPERAND (declarator, 1);
4098 arg_types = grokparms (arg_info, really_funcdef);
4100 /* Type qualifiers before the return type of the function
4101 qualify the return type, not the function type. */
4102 if (type_quals)
4104 /* Type qualifiers on a function return type are normally
4105 permitted by the standard but have no effect, so give a
4106 warning at -Wextra. Qualifiers on a void return type have
4107 meaning as a GNU extension, and are banned on function
4108 definitions in ISO C. FIXME: strictly we shouldn't
4109 pedwarn for qualified void return types except on function
4110 definitions, but not doing so could lead to the undesirable
4111 state of a "volatile void" function return type not being
4112 warned about, and a use of the function being compiled
4113 with GNU semantics, with no diagnostics under -pedantic. */
4114 if (VOID_TYPE_P (type) && pedantic && !in_system_header)
4115 pedwarn ("ISO C forbids qualified void function return type");
4116 else if (extra_warnings
4117 && !(VOID_TYPE_P (type)
4118 && type_quals == TYPE_QUAL_VOLATILE))
4119 warning ("type qualifiers ignored on function return type");
4121 type = c_build_qualified_type (type, type_quals);
4123 type_quals = TYPE_UNQUALIFIED;
4125 type = build_function_type (type, arg_types);
4126 declarator = TREE_OPERAND (declarator, 0);
4128 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4129 the formal parameter list of this FUNCTION_TYPE to point to
4130 the FUNCTION_TYPE node itself. */
4133 tree link;
4135 for (link = ARG_INFO_TAGS (arg_info);
4136 link;
4137 link = TREE_CHAIN (link))
4138 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4141 else if (TREE_CODE (declarator) == INDIRECT_REF)
4143 /* Merge any constancy or volatility into the target type
4144 for the pointer. */
4146 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4147 && type_quals)
4148 pedwarn ("ISO C forbids qualified function types");
4149 if (type_quals)
4150 type = c_build_qualified_type (type, type_quals);
4151 type_quals = TYPE_UNQUALIFIED;
4152 size_varies = 0;
4154 type = build_pointer_type (type);
4156 /* Process a list of type modifier keywords
4157 (such as const or volatile) that were given inside the `*'. */
4159 if (TREE_TYPE (declarator))
4161 tree typemodlist;
4162 int erred = 0;
4164 constp = 0;
4165 volatilep = 0;
4166 restrictp = 0;
4167 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4168 typemodlist = TREE_CHAIN (typemodlist))
4170 tree qualifier = TREE_VALUE (typemodlist);
4172 if (C_IS_RESERVED_WORD (qualifier))
4174 if (C_RID_CODE (qualifier) == RID_CONST)
4175 constp++;
4176 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4177 volatilep++;
4178 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4179 restrictp++;
4180 else
4181 erred++;
4183 else
4184 erred++;
4187 if (erred)
4188 error ("invalid type modifier within pointer declarator");
4189 if (pedantic && !flag_isoc99)
4191 if (constp > 1)
4192 pedwarn ("duplicate `const'");
4193 if (volatilep > 1)
4194 pedwarn ("duplicate `volatile'");
4195 if (restrictp > 1)
4196 pedwarn ("duplicate `restrict'");
4199 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4200 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4201 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4204 declarator = TREE_OPERAND (declarator, 0);
4206 else
4207 abort ();
4211 /* Now TYPE has the actual type. */
4213 /* Did array size calculations overflow? */
4215 if (TREE_CODE (type) == ARRAY_TYPE
4216 && COMPLETE_TYPE_P (type)
4217 && TREE_OVERFLOW (TYPE_SIZE (type)))
4219 error ("size of array `%s' is too large", name);
4220 /* If we proceed with the array type as it is, we'll eventually
4221 crash in tree_low_cst(). */
4222 type = error_mark_node;
4225 /* If this is declaring a typedef name, return a TYPE_DECL. */
4227 if (specbits & (1 << (int) RID_TYPEDEF))
4229 tree decl;
4230 /* Note that the grammar rejects storage classes
4231 in typenames, fields or parameters */
4232 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4233 && type_quals)
4234 pedwarn ("ISO C forbids qualified function types");
4235 if (type_quals)
4236 type = c_build_qualified_type (type, type_quals);
4237 decl = build_decl (TYPE_DECL, declarator, type);
4238 if ((specbits & (1 << (int) RID_SIGNED))
4239 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4240 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4241 decl_attributes (&decl, returned_attrs, 0);
4242 return decl;
4245 /* Detect the case of an array type of unspecified size
4246 which came, as such, direct from a typedef name.
4247 We must copy the type, so that each identifier gets
4248 a distinct type, so that each identifier's size can be
4249 controlled separately by its own initializer. */
4251 if (type != 0 && typedef_type != 0
4252 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4253 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4255 type = build_array_type (TREE_TYPE (type), 0);
4256 if (size_varies)
4257 C_TYPE_VARIABLE_SIZE (type) = 1;
4260 /* If this is a type name (such as, in a cast or sizeof),
4261 compute the type and return it now. */
4263 if (decl_context == TYPENAME)
4265 /* Note that the grammar rejects storage classes
4266 in typenames, fields or parameters */
4267 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4268 && type_quals)
4269 pedwarn ("ISO C forbids const or volatile function types");
4270 if (type_quals)
4271 type = c_build_qualified_type (type, type_quals);
4272 decl_attributes (&type, returned_attrs, 0);
4273 return type;
4276 /* Aside from typedefs and type names (handle above),
4277 `void' at top level (not within pointer)
4278 is allowed only in public variables.
4279 We don't complain about parms either, but that is because
4280 a better error message can be made later. */
4282 if (VOID_TYPE_P (type) && decl_context != PARM
4283 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4284 && ((specbits & (1 << (int) RID_EXTERN))
4285 || (current_scope == file_scope
4286 && !(specbits
4287 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4289 error ("variable or field `%s' declared void", name);
4290 type = integer_type_node;
4293 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4294 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4297 tree decl;
4299 if (decl_context == PARM)
4301 tree type_as_written;
4302 tree promoted_type;
4304 /* A parameter declared as an array of T is really a pointer to T.
4305 One declared as a function is really a pointer to a function. */
4307 if (TREE_CODE (type) == ARRAY_TYPE)
4309 /* Transfer const-ness of array into that of type pointed to. */
4310 type = TREE_TYPE (type);
4311 if (type_quals)
4312 type = c_build_qualified_type (type, type_quals);
4313 type = build_pointer_type (type);
4314 type_quals = TYPE_UNQUALIFIED;
4315 if (array_ptr_quals)
4317 tree new_ptr_quals, new_ptr_attrs;
4318 int erred = 0;
4319 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4320 /* We don't yet implement attributes in this context. */
4321 if (new_ptr_attrs != NULL_TREE)
4322 warning ("attributes in parameter array declarator ignored");
4324 constp = 0;
4325 volatilep = 0;
4326 restrictp = 0;
4327 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4329 tree qualifier = TREE_VALUE (new_ptr_quals);
4331 if (C_IS_RESERVED_WORD (qualifier))
4333 if (C_RID_CODE (qualifier) == RID_CONST)
4334 constp++;
4335 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4336 volatilep++;
4337 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4338 restrictp++;
4339 else
4340 erred++;
4342 else
4343 erred++;
4346 if (erred)
4347 error ("invalid type modifier within array declarator");
4349 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4350 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4351 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4353 size_varies = 0;
4355 else if (TREE_CODE (type) == FUNCTION_TYPE)
4357 if (pedantic && type_quals)
4358 pedwarn ("ISO C forbids qualified function types");
4359 if (type_quals)
4360 type = c_build_qualified_type (type, type_quals);
4361 type = build_pointer_type (type);
4362 type_quals = TYPE_UNQUALIFIED;
4364 else if (type_quals)
4365 type = c_build_qualified_type (type, type_quals);
4367 type_as_written = type;
4369 decl = build_decl (PARM_DECL, declarator, type);
4370 if (size_varies)
4371 C_DECL_VARIABLE_SIZE (decl) = 1;
4373 /* Compute the type actually passed in the parmlist,
4374 for the case where there is no prototype.
4375 (For example, shorts and chars are passed as ints.)
4376 When there is a prototype, this is overridden later. */
4378 if (type == error_mark_node)
4379 promoted_type = type;
4380 else
4381 promoted_type = c_type_promotes_to (type);
4383 DECL_ARG_TYPE (decl) = promoted_type;
4384 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4386 else if (decl_context == FIELD)
4388 /* Structure field. It may not be a function. */
4390 if (TREE_CODE (type) == FUNCTION_TYPE)
4392 error ("field `%s' declared as a function", name);
4393 type = build_pointer_type (type);
4395 else if (TREE_CODE (type) != ERROR_MARK
4396 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4398 error ("field `%s' has incomplete type", name);
4399 type = error_mark_node;
4401 /* Move type qualifiers down to element of an array. */
4402 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4403 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4404 type_quals),
4405 TYPE_DOMAIN (type));
4406 decl = build_decl (FIELD_DECL, declarator, type);
4407 DECL_NONADDRESSABLE_P (decl) = bitfield;
4409 if (size_varies)
4410 C_DECL_VARIABLE_SIZE (decl) = 1;
4412 else if (TREE_CODE (type) == FUNCTION_TYPE)
4414 /* Every function declaration is "external"
4415 except for those which are inside a function body
4416 in which `auto' is used.
4417 That is a case not specified by ANSI C,
4418 and we use it for forward declarations for nested functions. */
4419 int extern_ref = (!(specbits & (1 << (int) RID_AUTO))
4420 || current_scope == file_scope);
4422 if (specbits & (1 << (int) RID_AUTO)
4423 && (pedantic || current_scope == file_scope))
4424 pedwarn ("invalid storage class for function `%s'", name);
4425 if (specbits & (1 << (int) RID_REGISTER))
4426 error ("invalid storage class for function `%s'", name);
4427 if (specbits & (1 << (int) RID_THREAD))
4428 error ("invalid storage class for function `%s'", name);
4429 /* Function declaration not at file scope.
4430 Storage classes other than `extern' are not allowed
4431 and `extern' makes no difference. */
4432 if (current_scope != file_scope
4433 && (specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_INLINE)))
4434 && pedantic)
4435 pedwarn ("invalid storage class for function `%s'", name);
4437 decl = build_decl (FUNCTION_DECL, declarator, type);
4438 decl = build_decl_attribute_variant (decl, decl_attr);
4440 DECL_LANG_SPECIFIC (decl)
4441 = ggc_alloc_cleared (sizeof (struct lang_decl));
4443 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4444 pedwarn ("ISO C forbids qualified function types");
4446 /* GNU C interprets a `volatile void' return type to indicate
4447 that the function does not return. */
4448 if ((type_quals & TYPE_QUAL_VOLATILE)
4449 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4450 warning ("`noreturn' function returns non-void value");
4452 if (extern_ref)
4453 DECL_EXTERNAL (decl) = 1;
4454 /* Record absence of global scope for `static' or `auto'. */
4455 TREE_PUBLIC (decl)
4456 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4458 /* For a function definition, record the argument information
4459 block in DECL_ARGUMENTS where store_parm_decls will look
4460 for it. */
4461 if (funcdef_flag)
4462 DECL_ARGUMENTS (decl) = arg_info;
4464 if (defaulted_int)
4465 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4467 /* Record presence of `inline', if it is reasonable. */
4468 if (MAIN_NAME_P (declarator))
4470 if (inlinep)
4471 warning ("cannot inline function `main'");
4473 else if (inlinep)
4475 /* Record that the function is declared `inline'. */
4476 DECL_DECLARED_INLINE_P (decl) = 1;
4478 /* Do not mark bare declarations as DECL_INLINE. Doing so
4479 in the presence of multiple declarations can result in
4480 the abstract origin pointing between the declarations,
4481 which will confuse dwarf2out. */
4482 if (initialized)
4484 DECL_INLINE (decl) = 1;
4485 if (specbits & (1 << (int) RID_EXTERN))
4486 current_extern_inline = 1;
4489 /* If -finline-functions, assume it can be inlined. This does
4490 two things: let the function be deferred until it is actually
4491 needed, and let dwarf2 know that the function is inlinable. */
4492 else if (flag_inline_trees == 2 && initialized)
4493 DECL_INLINE (decl) = 1;
4495 else
4497 /* It's a variable. */
4498 /* An uninitialized decl with `extern' is a reference. */
4499 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4501 /* Move type qualifiers down to element of an array. */
4502 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4504 int saved_align = TYPE_ALIGN(type);
4505 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4506 type_quals),
4507 TYPE_DOMAIN (type));
4508 TYPE_ALIGN (type) = saved_align;
4510 else if (type_quals)
4511 type = c_build_qualified_type (type, type_quals);
4513 /* C99 6.2.2p7: It is invalid (compile-time undefined
4514 behavior) to create an 'extern' declaration for a
4515 variable if there is a global declaration that is
4516 'static' and the global declaration is not visible.
4517 (If the static declaration _is_ currently visible,
4518 the 'extern' declaration is taken to refer to that decl.) */
4519 if (extern_ref && current_scope != file_scope)
4521 tree global_decl = identifier_global_value (declarator);
4522 tree visible_decl = lookup_name (declarator);
4524 if (global_decl
4525 && global_decl != visible_decl
4526 && TREE_CODE (global_decl) == VAR_DECL
4527 && !TREE_PUBLIC (global_decl))
4528 error ("variable previously declared 'static' redeclared "
4529 "'extern'");
4532 decl = build_decl (VAR_DECL, declarator, type);
4533 if (size_varies)
4534 C_DECL_VARIABLE_SIZE (decl) = 1;
4536 if (inlinep)
4537 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4539 DECL_EXTERNAL (decl) = extern_ref;
4541 /* At file scope, the presence of a `static' or `register' storage
4542 class specifier, or the absence of all storage class specifiers
4543 makes this declaration a definition (perhaps tentative). Also,
4544 the absence of both `static' and `register' makes it public. */
4545 if (current_scope == file_scope)
4547 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4548 | (1 << (int) RID_REGISTER)));
4549 TREE_STATIC (decl) = !extern_ref;
4551 /* Not at file scope, only `static' makes a static definition. */
4552 else
4554 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4555 TREE_PUBLIC (decl) = extern_ref;
4558 if (specbits & 1 << (int) RID_THREAD)
4560 if (targetm.have_tls)
4561 DECL_THREAD_LOCAL (decl) = 1;
4562 else
4563 /* A mere warning is sure to result in improper semantics
4564 at runtime. Don't bother to allow this to compile. */
4565 error ("thread-local storage not supported for this target");
4569 /* Record `register' declaration for warnings on &
4570 and in case doing stupid register allocation. */
4572 if (specbits & (1 << (int) RID_REGISTER))
4574 C_DECL_REGISTER (decl) = 1;
4575 DECL_REGISTER (decl) = 1;
4578 /* Record constancy and volatility. */
4579 c_apply_type_quals_to_decl (type_quals, decl);
4581 /* If a type has volatile components, it should be stored in memory.
4582 Otherwise, the fact that those components are volatile
4583 will be ignored, and would even crash the compiler. */
4584 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4586 /* It is not an error for a structure with volatile fields to
4587 be declared register, but reset DECL_REGISTER since it
4588 cannot actually go in a register. */
4589 int was_reg = C_DECL_REGISTER (decl);
4590 C_DECL_REGISTER (decl) = 0;
4591 DECL_REGISTER (decl) = 0;
4592 c_mark_addressable (decl);
4593 C_DECL_REGISTER (decl) = was_reg;
4596 #ifdef ENABLE_CHECKING
4597 /* This is the earliest point at which we might know the assembler
4598 name of a variable. Thus, if it's known before this, die horribly. */
4599 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4600 abort ();
4601 #endif
4603 decl_attributes (&decl, returned_attrs, 0);
4605 return decl;
4609 /* Decode the parameter-list info for a function type or function definition.
4610 The argument is the value returned by `get_parm_info' (or made in parse.y
4611 if there is an identifier list instead of a parameter decl list).
4612 These two functions are separate because when a function returns
4613 or receives functions then each is called multiple times but the order
4614 of calls is different. The last call to `grokparms' is always the one
4615 that contains the formal parameter names of a function definition.
4617 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4619 FUNCDEF_FLAG is nonzero for a function definition, 0 for
4620 a mere declaration. A nonempty identifier-list gets an error message
4621 when FUNCDEF_FLAG is zero. */
4623 static tree
4624 grokparms (tree arg_info, int funcdef_flag)
4626 tree arg_types = ARG_INFO_TYPES (arg_info);
4628 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4629 && !in_system_header)
4630 warning ("function declaration isn't a prototype");
4632 if (arg_types == error_mark_node)
4633 return 0; /* don't set TYPE_ARG_TYPES in this case */
4635 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4637 if (! funcdef_flag)
4638 pedwarn ("parameter names (without types) in function declaration");
4640 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4641 ARG_INFO_TYPES (arg_info) = 0;
4642 return 0;
4644 else
4646 tree parm, type, typelt;
4647 unsigned int parmno;
4649 /* If the arg types are incomplete in a declaration, they must
4650 include undefined tags. These tags can never be defined in
4651 the scope of the declaration, so the types can never be
4652 completed, and no call can be compiled successfully. */
4654 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4655 parm;
4656 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4658 type = TREE_VALUE (typelt);
4659 if (type == error_mark_node)
4660 continue;
4662 if (!COMPLETE_TYPE_P (type))
4664 if (funcdef_flag)
4666 if (DECL_NAME (parm))
4667 error ("%Jparameter %u ('%D') has incomplete type",
4668 parm, parmno, parm);
4669 else
4670 error ("%Jparameter %u has incomplete type",
4671 parm, parmno);
4673 TREE_VALUE (typelt) = error_mark_node;
4674 TREE_TYPE (parm) = error_mark_node;
4676 else
4678 if (DECL_NAME (parm))
4679 warning ("%Jparameter %u ('%D') has incomplete type",
4680 parm, parmno, parm);
4681 else
4682 warning ("%Jparameter %u has incomplete type",
4683 parm, parmno);
4687 return arg_types;
4691 /* Take apart the current scope and return a tree_list node with info
4692 on a parameter list just parsed. This tree_list node should be
4693 examined using the ARG_INFO_* macros, defined above:
4695 ARG_INFO_PARMS: a list of parameter decls.
4696 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4697 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4698 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4699 constants) defined with the parameters.
4701 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4703 ELLIPSIS being true means the argument list ended in '...' so don't
4704 append a sentinel (void_list_node) to the end of the type-list. */
4706 tree
4707 get_parm_info (bool ellipsis)
4709 struct c_binding *b = current_scope->bindings;
4710 tree arg_info = make_node (TREE_LIST);
4711 tree parms = 0;
4712 tree tags = 0;
4713 tree types = 0;
4714 tree others = 0;
4716 static bool explained_incomplete_types = false;
4717 bool gave_void_only_once_err = false;
4719 /* The bindings in this scope must not get put into a block.
4720 We will take care of deleting the binding nodes. */
4721 current_scope->bindings = 0;
4723 /* This function is only called if there was *something* on the
4724 parameter list. */
4725 #ifdef ENABLE_CHECKING
4726 if (b == 0)
4727 abort ();
4728 #endif
4730 /* A parameter list consisting solely of 'void' indicates that the
4731 function takes no arguments. But if the 'void' is qualified
4732 (by 'const' or 'volatile'), or has a storage class specifier
4733 ('register'), then the behavior is undefined; issue an error.
4734 Typedefs for 'void' are OK (see DR#157). */
4735 if (b->prev == 0 /* one binding */
4736 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4737 && !DECL_NAME (b->decl) /* anonymous */
4738 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4740 if (TREE_THIS_VOLATILE (b->decl)
4741 || TREE_READONLY (b->decl)
4742 || C_DECL_REGISTER (b->decl))
4743 error ("'void' as only parameter may not be qualified");
4745 /* There cannot be an ellipsis. */
4746 if (ellipsis)
4747 error ("'void' must be the only parameter");
4749 ARG_INFO_TYPES (arg_info) = void_list_node;
4750 return arg_info;
4753 if (!ellipsis)
4754 types = void_list_node;
4756 /* Break up the bindings list into parms, tags, types, and others;
4757 apply sanity checks; purge the name-to-decl bindings. */
4758 while (b)
4760 tree decl = b->decl;
4761 tree type = TREE_TYPE (decl);
4762 const char *keyword;
4764 switch (TREE_CODE (decl))
4766 case PARM_DECL:
4767 if (b->id)
4769 #ifdef ENABLE_CHECKING
4770 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4771 #endif
4772 I_SYMBOL_BINDING (b->id) = b->shadowed;
4775 /* Check for forward decls that never got their actual decl. */
4776 if (TREE_ASM_WRITTEN (decl))
4777 error ("%Jparameter '%D' has just a forward declaration",
4778 decl, decl);
4779 /* Check for (..., void, ...) and issue an error. */
4780 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4782 if (!gave_void_only_once_err)
4784 error ("'void' must be the only parameter");
4785 gave_void_only_once_err = true;
4788 else
4790 /* Valid parameter, add it to the list. */
4791 TREE_CHAIN (decl) = parms;
4792 parms = decl;
4794 /* Since there is a prototype, args are passed in their
4795 declared types. The back end may override this later. */
4796 DECL_ARG_TYPE (decl) = type;
4797 types = tree_cons (0, type, types);
4799 break;
4801 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4802 case UNION_TYPE: keyword = "union"; goto tag;
4803 case RECORD_TYPE: keyword = "struct"; goto tag;
4804 tag:
4805 /* Types may not have tag-names, in which case the type
4806 appears in the bindings list with b->id NULL. */
4807 if (b->id)
4809 #ifdef ENABLE_CHECKING
4810 if (I_TAG_BINDING (b->id) != b) abort ();
4811 #endif
4812 I_TAG_BINDING (b->id) = b->shadowed;
4815 /* Warn about any struct, union or enum tags defined in a
4816 parameter list. The scope of such types is limited to
4817 the parameter list, which is rarely if ever desirable
4818 (it's impossible to call such a function with type-
4819 correct arguments). An anonymous union parm type is
4820 meaningful as a GNU extension, so don't warn for that. */
4821 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4823 if (b->id)
4824 /* The %s will be one of 'struct', 'union', or 'enum'. */
4825 warning ("'%s %E' declared inside parameter list",
4826 keyword, b->id);
4827 else
4828 /* The %s will be one of 'struct', 'union', or 'enum'. */
4829 warning ("anonymous %s declared inside parameter list",
4830 keyword);
4832 if (! explained_incomplete_types)
4834 warning ("its scope is only this definition or declaration,"
4835 " which is probably not what you want");
4836 explained_incomplete_types = true;
4840 tags = tree_cons (b->id, decl, tags);
4841 break;
4843 case CONST_DECL:
4844 case TYPE_DECL:
4845 /* CONST_DECLs appear here when we have an embedded enum,
4846 and TYPE_DECLs appear here when we have an embedded struct
4847 or union. No warnings for this - we already warned about the
4848 type itself. */
4849 TREE_CHAIN (decl) = others;
4850 others = decl;
4851 /* fall through */
4853 case ERROR_MARK:
4854 /* error_mark_node appears here when we have an undeclared
4855 variable. Just throw it away. */
4856 if (b->id)
4858 #ifdef ENABLE_CHECKING
4859 if (I_SYMBOL_BINDING (b->id) != b) abort ();
4860 #endif
4861 I_SYMBOL_BINDING (b->id) = b->shadowed;
4863 break;
4865 /* Other things that might be encountered. */
4866 case LABEL_DECL:
4867 case FUNCTION_DECL:
4868 case VAR_DECL:
4869 default:
4870 abort ();
4873 b = free_binding_and_advance (b);
4876 ARG_INFO_PARMS (arg_info) = parms;
4877 ARG_INFO_TAGS (arg_info) = tags;
4878 ARG_INFO_TYPES (arg_info) = types;
4879 ARG_INFO_OTHERS (arg_info) = others;
4880 return arg_info;
4883 /* Get the struct, enum or union (CODE says which) with tag NAME.
4884 Define the tag as a forward-reference if it is not defined. */
4886 tree
4887 xref_tag (enum tree_code code, tree name)
4889 /* If a cross reference is requested, look up the type
4890 already defined for this tag and return it. */
4892 tree ref = lookup_tag (code, name, 0);
4893 /* If this is the right type of tag, return what we found.
4894 (This reference will be shadowed by shadow_tag later if appropriate.)
4895 If this is the wrong type of tag, do not return it. If it was the
4896 wrong type in the same scope, we will have had an error
4897 message already; if in a different scope and declaring
4898 a name, pending_xref_error will give an error message; but if in a
4899 different scope and not declaring a name, this tag should
4900 shadow the previous declaration of a different type of tag, and
4901 this would not work properly if we return the reference found.
4902 (For example, with "struct foo" in an outer scope, "union foo;"
4903 must shadow that tag with a new one of union type.) */
4904 if (ref && TREE_CODE (ref) == code)
4905 return ref;
4907 /* If no such tag is yet defined, create a forward-reference node
4908 and record it as the "definition".
4909 When a real declaration of this type is found,
4910 the forward-reference will be altered into a real type. */
4912 ref = make_node (code);
4913 if (code == ENUMERAL_TYPE)
4915 /* Give the type a default layout like unsigned int
4916 to avoid crashing if it does not get defined. */
4917 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
4918 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
4919 TYPE_USER_ALIGN (ref) = 0;
4920 TYPE_UNSIGNED (ref) = 1;
4921 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
4922 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
4923 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
4926 pushtag (name, ref);
4928 return ref;
4931 /* Make sure that the tag NAME is defined *in the current scope*
4932 at least as a forward reference.
4933 CODE says which kind of tag NAME ought to be. */
4935 tree
4936 start_struct (enum tree_code code, tree name)
4938 /* If there is already a tag defined at this scope
4939 (as a forward reference), just return it. */
4941 tree ref = 0;
4943 if (name != 0)
4944 ref = lookup_tag (code, name, 1);
4945 if (ref && TREE_CODE (ref) == code)
4947 if (TYPE_FIELDS (ref))
4949 if (code == UNION_TYPE)
4950 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
4951 else
4952 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
4955 else
4957 /* Otherwise create a forward-reference just so the tag is in scope. */
4959 ref = make_node (code);
4960 pushtag (name, ref);
4963 C_TYPE_BEING_DEFINED (ref) = 1;
4964 TYPE_PACKED (ref) = flag_pack_struct;
4965 return ref;
4968 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
4969 of a structure component, returning a FIELD_DECL node.
4970 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
4972 This is done during the parsing of the struct declaration.
4973 The FIELD_DECL nodes are chained together and the lot of them
4974 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
4976 tree
4977 grokfield (tree declarator, tree declspecs, tree width)
4979 tree value;
4981 if (declarator == NULL_TREE && width == NULL_TREE)
4983 /* This is an unnamed decl.
4985 If we have something of the form "union { list } ;" then this
4986 is the anonymous union extension. Similarly for struct.
4988 If this is something of the form "struct foo;", then
4989 If MS extensions are enabled, this is handled as an
4990 anonymous struct.
4991 Otherwise this is a forward declaration of a structure tag.
4993 If this is something of the form "foo;" and foo is a TYPE_DECL, then
4994 If MS extensions are enabled and foo names a structure, then
4995 again this is an anonymous struct.
4996 Otherwise this is an error.
4998 Oh what a horrid tangled web we weave. I wonder if MS consciously
4999 took this from Plan 9 or if it was an accident of implementation
5000 that took root before someone noticed the bug... */
5002 tree type = TREE_VALUE (declspecs);
5004 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5005 type = TREE_TYPE (type);
5006 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5008 if (flag_ms_extensions)
5009 ; /* ok */
5010 else if (flag_iso)
5011 goto warn_unnamed_field;
5012 else if (TYPE_NAME (type) == NULL)
5013 ; /* ok */
5014 else
5015 goto warn_unnamed_field;
5017 else
5019 warn_unnamed_field:
5020 warning ("declaration does not declare anything");
5021 return NULL_TREE;
5025 value = grokdeclarator (declarator, declspecs, FIELD, 0,
5026 width ? &width : NULL);
5028 finish_decl (value, NULL_TREE, NULL_TREE);
5029 DECL_INITIAL (value) = width;
5031 return value;
5034 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5035 the list such that this does not present a problem later. */
5037 static void
5038 detect_field_duplicates (tree fieldlist)
5040 tree x, y;
5041 int timeout = 10;
5043 /* First, see if there are more than "a few" fields.
5044 This is trivially true if there are zero or one fields. */
5045 if (!fieldlist)
5046 return;
5047 x = TREE_CHAIN (fieldlist);
5048 if (!x)
5049 return;
5050 do {
5051 timeout--;
5052 x = TREE_CHAIN (x);
5053 } while (timeout > 0 && x);
5055 /* If there were "few" fields, avoid the overhead of allocating
5056 a hash table. Instead just do the nested traversal thing. */
5057 if (timeout > 0)
5059 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5060 if (DECL_NAME (x))
5062 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5063 if (DECL_NAME (y) == DECL_NAME (x))
5065 error ("%Jduplicate member '%D'", x, x);
5066 DECL_NAME (x) = NULL_TREE;
5070 else
5072 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5073 void **slot;
5075 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5076 if ((y = DECL_NAME (x)) != 0)
5078 slot = htab_find_slot (htab, y, INSERT);
5079 if (*slot)
5081 error ("%Jduplicate member '%D'", x, x);
5082 DECL_NAME (x) = NULL_TREE;
5084 *slot = y;
5087 htab_delete (htab);
5091 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5092 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5093 ATTRIBUTES are attributes to be applied to the structure. */
5095 tree
5096 finish_struct (tree t, tree fieldlist, tree attributes)
5098 tree x;
5099 bool toplevel = file_scope == current_scope;
5100 int saw_named_field;
5102 /* If this type was previously laid out as a forward reference,
5103 make sure we lay it out again. */
5105 TYPE_SIZE (t) = 0;
5107 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5109 if (pedantic)
5111 for (x = fieldlist; x; x = TREE_CHAIN (x))
5112 if (DECL_NAME (x) != 0)
5113 break;
5115 if (x == 0)
5116 pedwarn ("%s has no %s",
5117 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5118 fieldlist ? _("named members") : _("members"));
5121 /* Install struct as DECL_CONTEXT of each field decl.
5122 Also process specified field sizes,m which is found in the DECL_INITIAL.
5123 Store 0 there, except for ": 0" fields (so we can find them
5124 and delete them, below). */
5126 saw_named_field = 0;
5127 for (x = fieldlist; x; x = TREE_CHAIN (x))
5129 DECL_CONTEXT (x) = t;
5130 DECL_PACKED (x) |= TYPE_PACKED (t);
5132 /* If any field is const, the structure type is pseudo-const. */
5133 if (TREE_READONLY (x))
5134 C_TYPE_FIELDS_READONLY (t) = 1;
5135 else
5137 /* A field that is pseudo-const makes the structure likewise. */
5138 tree t1 = TREE_TYPE (x);
5139 while (TREE_CODE (t1) == ARRAY_TYPE)
5140 t1 = TREE_TYPE (t1);
5141 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5142 && C_TYPE_FIELDS_READONLY (t1))
5143 C_TYPE_FIELDS_READONLY (t) = 1;
5146 /* Any field that is volatile means variables of this type must be
5147 treated in some ways as volatile. */
5148 if (TREE_THIS_VOLATILE (x))
5149 C_TYPE_FIELDS_VOLATILE (t) = 1;
5151 /* Any field of nominal variable size implies structure is too. */
5152 if (C_DECL_VARIABLE_SIZE (x))
5153 C_TYPE_VARIABLE_SIZE (t) = 1;
5155 /* Detect invalid nested redefinition. */
5156 if (TREE_TYPE (x) == t)
5157 error ("nested redefinition of `%s'",
5158 IDENTIFIER_POINTER (TYPE_NAME (t)));
5160 if (DECL_INITIAL (x))
5162 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5163 DECL_SIZE (x) = bitsize_int (width);
5164 DECL_BIT_FIELD (x) = 1;
5165 SET_DECL_C_BIT_FIELD (x);
5168 DECL_INITIAL (x) = 0;
5170 /* Detect flexible array member in an invalid context. */
5171 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5172 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5173 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5174 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5176 if (TREE_CODE (t) == UNION_TYPE)
5178 error ("%Jflexible array member in union", x);
5179 TREE_TYPE (x) = error_mark_node;
5181 else if (TREE_CHAIN (x) != NULL_TREE)
5183 error ("%Jflexible array member not at end of struct", x);
5184 TREE_TYPE (x) = error_mark_node;
5186 else if (! saw_named_field)
5188 error ("%Jflexible array member in otherwise empty struct", x);
5189 TREE_TYPE (x) = error_mark_node;
5193 if (pedantic && TREE_CODE (t) == RECORD_TYPE
5194 && flexible_array_type_p (TREE_TYPE (x)))
5195 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5197 if (DECL_NAME (x))
5198 saw_named_field = 1;
5201 detect_field_duplicates (fieldlist);
5203 /* Now we have the nearly final fieldlist. Record it,
5204 then lay out the structure or union (including the fields). */
5206 TYPE_FIELDS (t) = fieldlist;
5208 layout_type (t);
5210 /* Delete all zero-width bit-fields from the fieldlist. */
5212 tree *fieldlistp = &fieldlist;
5213 while (*fieldlistp)
5214 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp))
5215 *fieldlistp = TREE_CHAIN (*fieldlistp);
5216 else
5217 fieldlistp = &TREE_CHAIN (*fieldlistp);
5220 /* Now we have the truly final field list.
5221 Store it in this type and in the variants. */
5223 TYPE_FIELDS (t) = fieldlist;
5225 /* If there are lots of fields, sort so we can look through them fast.
5226 We arbitrarily consider 16 or more elts to be "a lot". */
5229 int len = 0;
5231 for (x = fieldlist; x; x = TREE_CHAIN (x))
5233 if (len > 15 || DECL_NAME (x) == NULL)
5234 break;
5235 len += 1;
5238 if (len > 15)
5240 tree *field_array;
5241 struct lang_type *space;
5242 struct sorted_fields_type *space2;
5244 len += list_length (x);
5246 /* Use the same allocation policy here that make_node uses, to
5247 ensure that this lives as long as the rest of the struct decl.
5248 All decls in an inline function need to be saved. */
5250 space = ggc_alloc (sizeof (struct lang_type));
5251 space2 = ggc_alloc (sizeof (struct sorted_fields_type) + len * sizeof (tree));
5253 len = 0;
5254 space->s = space2;
5255 field_array = &space2->elts[0];
5256 for (x = fieldlist; x; x = TREE_CHAIN (x))
5258 field_array[len++] = x;
5260 /* If there is anonymous struct or union, break out of the loop. */
5261 if (DECL_NAME (x) == NULL)
5262 break;
5264 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5265 if (x == NULL)
5267 TYPE_LANG_SPECIFIC (t) = space;
5268 TYPE_LANG_SPECIFIC (t)->s->len = len;
5269 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5270 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5275 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5277 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5278 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5279 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5280 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5283 /* If this was supposed to be a transparent union, but we can't
5284 make it one, warn and turn off the flag. */
5285 if (TREE_CODE (t) == UNION_TYPE
5286 && TYPE_TRANSPARENT_UNION (t)
5287 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5289 TYPE_TRANSPARENT_UNION (t) = 0;
5290 warning ("union cannot be made transparent");
5293 /* If this structure or union completes the type of any previous
5294 variable declaration, lay it out and output its rtl. */
5295 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5297 x = TREE_CHAIN (x))
5299 tree decl = TREE_VALUE (x);
5300 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5301 layout_array_type (TREE_TYPE (decl));
5302 if (TREE_CODE (decl) != TYPE_DECL)
5304 layout_decl (decl, 0);
5305 if (c_dialect_objc ())
5306 objc_check_decl (decl);
5307 rest_of_decl_compilation (decl, NULL, toplevel, 0);
5308 if (! toplevel)
5309 expand_decl (decl);
5312 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5314 /* Finish debugging output for this type. */
5315 rest_of_type_compilation (t, toplevel);
5317 return t;
5320 /* Lay out the type T, and its element type, and so on. */
5322 static void
5323 layout_array_type (tree t)
5325 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5326 layout_array_type (TREE_TYPE (t));
5327 layout_type (t);
5330 /* Begin compiling the definition of an enumeration type.
5331 NAME is its name (or null if anonymous).
5332 Returns the type object, as yet incomplete.
5333 Also records info about it so that build_enumerator
5334 may be used to declare the individual values as they are read. */
5336 tree
5337 start_enum (tree name)
5339 tree enumtype = 0;
5341 /* If this is the real definition for a previous forward reference,
5342 fill in the contents in the same object that used to be the
5343 forward reference. */
5345 if (name != 0)
5346 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5348 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5350 enumtype = make_node (ENUMERAL_TYPE);
5351 pushtag (name, enumtype);
5354 C_TYPE_BEING_DEFINED (enumtype) = 1;
5356 if (TYPE_VALUES (enumtype) != 0)
5358 /* This enum is a named one that has been declared already. */
5359 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5361 /* Completely replace its old definition.
5362 The old enumerators remain defined, however. */
5363 TYPE_VALUES (enumtype) = 0;
5366 enum_next_value = integer_zero_node;
5367 enum_overflow = 0;
5369 if (flag_short_enums)
5370 TYPE_PACKED (enumtype) = 1;
5372 return enumtype;
5375 /* After processing and defining all the values of an enumeration type,
5376 install their decls in the enumeration type and finish it off.
5377 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5378 and ATTRIBUTES are the specified attributes.
5379 Returns ENUMTYPE. */
5381 tree
5382 finish_enum (tree enumtype, tree values, tree attributes)
5384 tree pair, tem;
5385 tree minnode = 0, maxnode = 0, enum_value_type;
5386 int precision, unsign;
5387 bool toplevel = (file_scope == current_scope);
5389 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5391 /* Calculate the maximum value of any enumerator in this type. */
5393 if (values == error_mark_node)
5394 minnode = maxnode = integer_zero_node;
5395 else
5397 minnode = maxnode = TREE_VALUE (values);
5398 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5400 tree value = TREE_VALUE (pair);
5401 if (tree_int_cst_lt (maxnode, value))
5402 maxnode = value;
5403 if (tree_int_cst_lt (value, minnode))
5404 minnode = value;
5408 /* Construct the final type of this enumeration. It is the same
5409 as one of the integral types - the narrowest one that fits, except
5410 that normally we only go as narrow as int - and signed iff any of
5411 the values are negative. */
5412 unsign = (tree_int_cst_sgn (minnode) >= 0);
5413 precision = MAX (min_precision (minnode, unsign),
5414 min_precision (maxnode, unsign));
5415 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5417 tree narrowest = c_common_type_for_size (precision, unsign);
5418 if (narrowest == 0)
5420 warning ("enumeration values exceed range of largest integer");
5421 narrowest = long_long_integer_type_node;
5424 precision = TYPE_PRECISION (narrowest);
5426 else
5427 precision = TYPE_PRECISION (integer_type_node);
5429 if (precision == TYPE_PRECISION (integer_type_node))
5430 enum_value_type = c_common_type_for_size (precision, 0);
5431 else
5432 enum_value_type = enumtype;
5434 TYPE_MIN_VALUE (enumtype) = minnode;
5435 TYPE_MAX_VALUE (enumtype) = maxnode;
5436 TYPE_PRECISION (enumtype) = precision;
5437 TYPE_UNSIGNED (enumtype) = unsign;
5438 TYPE_SIZE (enumtype) = 0;
5439 layout_type (enumtype);
5441 if (values != error_mark_node)
5443 /* Change the type of the enumerators to be the enum type. We
5444 need to do this irrespective of the size of the enum, for
5445 proper type checking. Replace the DECL_INITIALs of the
5446 enumerators, and the value slots of the list, with copies
5447 that have the enum type; they cannot be modified in place
5448 because they may be shared (e.g. integer_zero_node) Finally,
5449 change the purpose slots to point to the names of the decls. */
5450 for (pair = values; pair; pair = TREE_CHAIN (pair))
5452 tree enu = TREE_PURPOSE (pair);
5454 TREE_TYPE (enu) = enumtype;
5456 /* The ISO C Standard mandates enumerators to have type int,
5457 even though the underlying type of an enum type is
5458 unspecified. Here we convert any enumerators that fit in
5459 an int to type int, to avoid promotions to unsigned types
5460 when comparing integers with enumerators that fit in the
5461 int range. When -pedantic is given, build_enumerator()
5462 would have already taken care of those that don't fit. */
5463 if (int_fits_type_p (DECL_INITIAL (enu), enum_value_type))
5464 DECL_INITIAL (enu) = convert (enum_value_type, DECL_INITIAL (enu));
5465 else
5466 DECL_INITIAL (enu) = convert (enumtype, DECL_INITIAL (enu));
5468 TREE_PURPOSE (pair) = DECL_NAME (enu);
5469 TREE_VALUE (pair) = DECL_INITIAL (enu);
5472 TYPE_VALUES (enumtype) = values;
5475 /* Fix up all variant types of this enum type. */
5476 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5478 if (tem == enumtype)
5479 continue;
5480 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5481 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5482 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5483 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5484 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5485 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5486 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5487 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5488 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5489 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5492 /* Finish debugging output for this type. */
5493 rest_of_type_compilation (enumtype, toplevel);
5495 return enumtype;
5498 /* Build and install a CONST_DECL for one value of the
5499 current enumeration type (one that was begun with start_enum).
5500 Return a tree-list containing the CONST_DECL and its value.
5501 Assignment of sequential values by default is handled here. */
5503 tree
5504 build_enumerator (tree name, tree value)
5506 tree decl, type;
5508 /* Validate and default VALUE. */
5510 /* Remove no-op casts from the value. */
5511 if (value)
5512 STRIP_TYPE_NOPS (value);
5514 if (value != 0)
5516 /* Don't issue more errors for error_mark_node (i.e. an
5517 undeclared identifier) - just ignore the value expression. */
5518 if (value == error_mark_node)
5519 value = 0;
5520 else if (TREE_CODE (value) != INTEGER_CST)
5522 error ("enumerator value for '%E' is not an integer constant", name);
5523 value = 0;
5525 else
5527 value = default_conversion (value);
5528 constant_expression_warning (value);
5532 /* Default based on previous value. */
5533 /* It should no longer be possible to have NON_LVALUE_EXPR
5534 in the default. */
5535 if (value == 0)
5537 value = enum_next_value;
5538 if (enum_overflow)
5539 error ("overflow in enumeration values");
5542 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5544 pedwarn ("ISO C restricts enumerator values to range of `int'");
5545 /* XXX This causes -pedantic to change the meaning of the program.
5546 Remove? -zw 2004-03-15 */
5547 value = convert (integer_type_node, value);
5550 /* Set basis for default for next value. */
5551 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5552 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5554 /* Now create a declaration for the enum value name. */
5556 type = TREE_TYPE (value);
5557 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5558 TYPE_PRECISION (integer_type_node)),
5559 (TYPE_PRECISION (type)
5560 >= TYPE_PRECISION (integer_type_node)
5561 && TYPE_UNSIGNED (type)));
5563 decl = build_decl (CONST_DECL, name, type);
5564 DECL_INITIAL (decl) = convert (type, value);
5565 pushdecl (decl);
5567 return tree_cons (decl, value, NULL_TREE);
5571 /* Create the FUNCTION_DECL for a function definition.
5572 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5573 the declaration; they describe the function's name and the type it returns,
5574 but twisted together in a fashion that parallels the syntax of C.
5576 This function creates a binding context for the function body
5577 as well as setting up the FUNCTION_DECL in current_function_decl.
5579 Returns 1 on success. If the DECLARATOR is not suitable for a function
5580 (it defines a datum instead), we return 0, which tells
5581 yyparse to report a parse error. */
5584 start_function (tree declspecs, tree declarator, tree attributes)
5586 tree decl1, old_decl;
5587 tree restype;
5588 int old_immediate_size_expand = immediate_size_expand;
5590 current_function_returns_value = 0; /* Assume, until we see it does. */
5591 current_function_returns_null = 0;
5592 current_function_returns_abnormally = 0;
5593 warn_about_return_type = 0;
5594 current_extern_inline = 0;
5595 c_in_iteration_stmt = 0;
5596 c_in_case_stmt = 0;
5598 /* Don't expand any sizes in the return type of the function. */
5599 immediate_size_expand = 0;
5601 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
5603 /* If the declarator is not suitable for a function definition,
5604 cause a syntax error. */
5605 if (decl1 == 0)
5607 immediate_size_expand = old_immediate_size_expand;
5608 return 0;
5611 decl_attributes (&decl1, attributes, 0);
5613 if (DECL_DECLARED_INLINE_P (decl1)
5614 && DECL_UNINLINABLE (decl1)
5615 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5616 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5618 announce_function (decl1);
5620 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5622 error ("return type is an incomplete type");
5623 /* Make it return void instead. */
5624 TREE_TYPE (decl1)
5625 = build_function_type (void_type_node,
5626 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5629 if (warn_about_return_type)
5630 pedwarn_c99 ("return type defaults to `int'");
5632 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5633 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5634 DECL_INITIAL (decl1) = error_mark_node;
5636 /* If this definition isn't a prototype and we had a prototype declaration
5637 before, copy the arg type info from that prototype.
5638 But not if what we had before was a builtin function. */
5639 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5640 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5641 && !DECL_BUILT_IN (old_decl)
5642 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5643 == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (old_decl))))
5644 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5646 TREE_TYPE (decl1) = TREE_TYPE (old_decl);
5647 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5650 /* Optionally warn of old-fashioned def with no previous prototype. */
5651 if (warn_strict_prototypes
5652 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5653 && C_DECL_ISNT_PROTOTYPE (old_decl))
5654 warning ("function declaration isn't a prototype");
5655 /* Optionally warn of any global def with no previous prototype. */
5656 else if (warn_missing_prototypes
5657 && TREE_PUBLIC (decl1)
5658 && ! MAIN_NAME_P (DECL_NAME (decl1))
5659 && C_DECL_ISNT_PROTOTYPE (old_decl))
5660 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5661 /* Optionally warn of any def with no previous prototype
5662 if the function has already been used. */
5663 else if (warn_missing_prototypes
5664 && old_decl != 0 && TREE_USED (old_decl)
5665 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5666 warning ("%J'%D' was used with no prototype before its definition",
5667 decl1, decl1);
5668 /* Optionally warn of any global def with no previous declaration. */
5669 else if (warn_missing_declarations
5670 && TREE_PUBLIC (decl1)
5671 && old_decl == 0
5672 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5673 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5674 /* Optionally warn of any def with no previous declaration
5675 if the function has already been used. */
5676 else if (warn_missing_declarations
5677 && old_decl != 0 && TREE_USED (old_decl)
5678 && C_DECL_IMPLICIT (old_decl))
5679 warning ("%J`%D' was used with no declaration before its definition",
5680 decl1, decl1);
5682 /* This is a definition, not a reference.
5683 So normally clear DECL_EXTERNAL.
5684 However, `extern inline' acts like a declaration
5685 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5686 DECL_EXTERNAL (decl1) = current_extern_inline;
5688 /* This function exists in static storage.
5689 (This does not mean `static' in the C sense!) */
5690 TREE_STATIC (decl1) = 1;
5692 /* A nested function is not global. */
5693 if (current_function_decl != 0)
5694 TREE_PUBLIC (decl1) = 0;
5696 #ifdef ENABLE_CHECKING
5697 /* This is the earliest point at which we might know the assembler
5698 name of the function. Thus, if it's set before this, die horribly. */
5699 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5700 abort ();
5701 #endif
5703 /* If #pragma weak was used, mark the decl weak now. */
5704 if (current_scope == file_scope)
5705 maybe_apply_pragma_weak (decl1);
5707 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5708 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5710 tree args;
5711 int argct = 0;
5713 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5714 != integer_type_node)
5715 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5717 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5718 args = TREE_CHAIN (args))
5720 tree type = args ? TREE_VALUE (args) : 0;
5722 if (type == void_type_node)
5723 break;
5725 ++argct;
5726 switch (argct)
5728 case 1:
5729 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
5730 pedwarn ("%Jfirst argument of '%D' should be `int'",
5731 decl1, decl1);
5732 break;
5734 case 2:
5735 if (TREE_CODE (type) != POINTER_TYPE
5736 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5737 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5738 != char_type_node))
5739 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
5740 decl1, decl1);
5741 break;
5743 case 3:
5744 if (TREE_CODE (type) != POINTER_TYPE
5745 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
5746 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
5747 != char_type_node))
5748 pedwarn ("%Jthird argument of '%D' should probably be "
5749 "'char **'", decl1, decl1);
5750 break;
5754 /* It is intentional that this message does not mention the third
5755 argument because it's only mentioned in an appendix of the
5756 standard. */
5757 if (argct > 0 && (argct < 2 || argct > 3))
5758 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
5760 if (! TREE_PUBLIC (decl1))
5761 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
5764 /* Record the decl so that the function name is defined.
5765 If we already have a decl for this name, and it is a FUNCTION_DECL,
5766 use the old decl. */
5768 current_function_decl = pushdecl (decl1);
5770 push_scope ();
5771 declare_parm_level ();
5773 make_decl_rtl (current_function_decl, NULL);
5775 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
5776 /* Promote the value to int before returning it. */
5777 if (c_promoting_integer_type_p (restype))
5779 /* It retains unsignedness if not really getting wider. */
5780 if (TYPE_UNSIGNED (restype)
5781 && (TYPE_PRECISION (restype)
5782 == TYPE_PRECISION (integer_type_node)))
5783 restype = unsigned_type_node;
5784 else
5785 restype = integer_type_node;
5787 DECL_RESULT (current_function_decl)
5788 = build_decl (RESULT_DECL, NULL_TREE, restype);
5790 /* If this fcn was already referenced via a block-scope `extern' decl
5791 (or an implicit decl), propagate certain information about the usage. */
5792 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
5793 TREE_ADDRESSABLE (current_function_decl) = 1;
5795 immediate_size_expand = old_immediate_size_expand;
5797 start_fname_decls ();
5799 return 1;
5802 /* Subroutine of store_parm_decls which handles new-style function
5803 definitions (prototype format). The parms already have decls, so we
5804 need only record them as in effect and complain if any redundant
5805 old-style parm decls were written. */
5806 static void
5807 store_parm_decls_newstyle (tree fndecl, tree arg_info)
5809 tree decl;
5810 tree parms = ARG_INFO_PARMS (arg_info);
5811 tree tags = ARG_INFO_TAGS (arg_info);
5812 tree others = ARG_INFO_OTHERS (arg_info);
5814 if (current_scope->bindings)
5816 error ("%Jold-style parameter declarations in prototyped "
5817 "function definition", fndecl);
5819 /* Get rid of the old-style declarations. */
5820 pop_scope ();
5821 push_scope ();
5823 /* Don't issue this warning for nested functions, and don't issue this
5824 warning if we got here because ARG_INFO_TYPES was error_mark_node
5825 (this happens when a function definition has just an ellipsis in
5826 its parameter list). */
5827 else if (warn_traditional && !in_system_header
5828 && DECL_CONTEXT (fndecl) == current_file_decl
5829 && ARG_INFO_TYPES (arg_info) != error_mark_node)
5830 warning ("%Jtraditional C rejects ISO C style function definitions",
5831 fndecl);
5833 /* Now make all the parameter declarations visible in the function body.
5834 We can bypass most of the grunt work of pushdecl. */
5835 for (decl = parms; decl; decl = TREE_CHAIN (decl))
5837 DECL_CONTEXT (decl) = current_function_decl;
5838 if (DECL_NAME (decl))
5839 bind (DECL_NAME (decl), decl, current_scope);
5840 else
5841 error ("%Jparameter name omitted", decl);
5844 /* Record the parameter list in the function declaration. */
5845 DECL_ARGUMENTS (fndecl) = parms;
5847 /* Now make all the ancillary declarations visible, likewise. */
5848 for (decl = others; decl; decl = TREE_CHAIN (decl))
5850 DECL_CONTEXT (decl) = current_function_decl;
5851 if (DECL_NAME (decl))
5852 bind (DECL_NAME (decl), decl, current_scope);
5855 /* And all the tag declarations. */
5856 for (decl = tags; decl; decl = TREE_CHAIN (decl))
5857 if (TREE_PURPOSE (decl))
5858 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope);
5861 /* Subroutine of store_parm_decls which handles old-style function
5862 definitions (separate parameter list and declarations). */
5864 static void
5865 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
5867 struct c_binding *b;
5868 tree parm, decl, last;
5869 tree parmids = ARG_INFO_PARMS (arg_info);
5871 /* We use DECL_WEAK as a flag to show which parameters have been
5872 seen already, since it is not used on PARM_DECL. */
5873 #ifdef ENABLE_CHECKING
5874 for (b = current_scope->bindings; b; b = b->prev)
5875 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
5876 abort ();
5877 #endif
5879 if (warn_old_style_definition && !in_system_header)
5880 warning ("%Jold-style function definition", fndecl);
5882 /* Match each formal parameter name with its declaration. Save each
5883 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
5884 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5886 if (TREE_VALUE (parm) == 0)
5888 error ("%Jparameter name missing from parameter list", fndecl);
5889 TREE_PURPOSE (parm) = 0;
5890 continue;
5893 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
5894 if (b && b->contour == current_scope)
5896 decl = b->decl;
5897 /* If we got something other than a PARM_DECL it is an error. */
5898 if (TREE_CODE (decl) != PARM_DECL)
5899 error ("%J'%D' declared as a non-parameter", decl, decl);
5900 /* If the declaration is already marked, we have a duplicate
5901 name. Complain and ignore the duplicate. */
5902 else if (DECL_WEAK (decl))
5904 error ("%Jmultiple parameters named '%D'", decl, decl);
5905 TREE_PURPOSE (parm) = 0;
5906 continue;
5908 /* If the declaration says "void", complain and turn it into
5909 an int. */
5910 else if (VOID_TYPE_P (TREE_TYPE (decl)))
5912 error ("%Jparameter '%D' declared with void type", decl, decl);
5913 TREE_TYPE (decl) = integer_type_node;
5914 DECL_ARG_TYPE (decl) = integer_type_node;
5915 layout_decl (decl, 0);
5918 /* If no declaration found, default to int. */
5919 else
5921 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
5922 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
5923 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
5924 pushdecl (decl);
5926 if (flag_isoc99)
5927 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
5928 else if (extra_warnings)
5929 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
5932 TREE_PURPOSE (parm) = decl;
5933 DECL_WEAK (decl) = 1;
5936 /* Now examine the parms chain for incomplete declarations
5937 and declarations with no corresponding names. */
5939 for (b = current_scope->bindings; b; b = b->prev)
5941 parm = b->decl;
5942 if (TREE_CODE (parm) != PARM_DECL)
5943 continue;
5945 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
5947 error ("%Jparameter '%D' has incomplete type", parm, parm);
5948 TREE_TYPE (parm) = error_mark_node;
5951 if (! DECL_WEAK (parm))
5953 error ("%Jdeclaration for parameter '%D' but no such parameter",
5954 parm, parm);
5956 /* Pretend the parameter was not missing.
5957 This gets us to a standard state and minimizes
5958 further error messages. */
5959 parmids = chainon (parmids, tree_cons (parm, 0, 0));
5963 /* Chain the declarations together in the order of the list of
5964 names. Store that chain in the function decl, replacing the
5965 list of names. Update the current scope to match. */
5966 DECL_ARGUMENTS (fndecl) = 0;
5968 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
5969 if (TREE_PURPOSE (parm))
5970 break;
5971 if (parm && TREE_PURPOSE (parm))
5973 last = TREE_PURPOSE (parm);
5974 DECL_ARGUMENTS (fndecl) = last;
5975 DECL_WEAK (last) = 0;
5977 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
5978 if (TREE_PURPOSE (parm))
5980 TREE_CHAIN (last) = TREE_PURPOSE (parm);
5981 last = TREE_PURPOSE (parm);
5982 DECL_WEAK (last) = 0;
5984 TREE_CHAIN (last) = 0;
5987 /* If there was a previous prototype,
5988 set the DECL_ARG_TYPE of each argument according to
5989 the type previously specified, and report any mismatches. */
5991 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
5993 tree type;
5994 for (parm = DECL_ARGUMENTS (fndecl),
5995 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
5996 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
5997 != void_type_node));
5998 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6000 if (parm == 0 || type == 0
6001 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6003 error ("number of arguments doesn't match prototype");
6004 error ("%Hprototype declaration",
6005 &current_function_prototype_locus);
6006 break;
6008 /* Type for passing arg must be consistent with that
6009 declared for the arg. ISO C says we take the unqualified
6010 type for parameters declared with qualified type. */
6011 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6012 TYPE_MAIN_VARIANT (TREE_VALUE (type)),
6013 COMPARE_STRICT))
6015 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6016 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6018 /* Adjust argument to match prototype. E.g. a previous
6019 `int foo(float);' prototype causes
6020 `int foo(x) float x; {...}' to be treated like
6021 `int foo(float x) {...}'. This is particularly
6022 useful for argument types like uid_t. */
6023 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6025 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6026 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6027 && TYPE_PRECISION (TREE_TYPE (parm))
6028 < TYPE_PRECISION (integer_type_node))
6029 DECL_ARG_TYPE (parm) = integer_type_node;
6031 if (pedantic)
6033 pedwarn ("promoted argument '%D' "
6034 "doesn't match prototype", parm);
6035 pedwarn ("%Hprototype declaration",
6036 &current_function_prototype_locus);
6039 else
6041 error ("argument '%D' doesn't match prototype", parm);
6042 error ("%Hprototype declaration",
6043 &current_function_prototype_locus);
6047 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6050 /* Otherwise, create a prototype that would match. */
6052 else
6054 tree actual = 0, last = 0, type;
6056 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6058 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6059 if (last)
6060 TREE_CHAIN (last) = type;
6061 else
6062 actual = type;
6063 last = type;
6065 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6066 if (last)
6067 TREE_CHAIN (last) = type;
6068 else
6069 actual = type;
6071 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6072 of the type of this function, but we need to avoid having this
6073 affect the types of other similarly-typed functions, so we must
6074 first force the generation of an identical (but separate) type
6075 node for the relevant function type. The new node we create
6076 will be a variant of the main variant of the original function
6077 type. */
6079 TREE_TYPE (fndecl) = build_type_copy (TREE_TYPE (fndecl));
6081 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6085 /* Store the parameter declarations into the current function declaration.
6086 This is called after parsing the parameter declarations, before
6087 digesting the body of the function.
6089 For an old-style definition, construct a prototype out of the old-style
6090 parameter declarations and inject it into the function's type. */
6092 void
6093 store_parm_decls (void)
6095 tree fndecl = current_function_decl;
6097 /* The function containing FNDECL, if any. */
6098 tree context = decl_function_context (fndecl);
6100 /* The argument information block for FNDECL. */
6101 tree arg_info = DECL_ARGUMENTS (fndecl);
6103 /* True if this definition is written with a prototype. Note:
6104 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6105 list in a function definition as equivalent to (void) -- an
6106 empty argument list specifies the function has no parameters,
6107 but only (void) sets up a prototype for future calls. */
6108 bool proto = ARG_INFO_TYPES (arg_info) != 0;
6110 if (proto)
6111 store_parm_decls_newstyle (fndecl, arg_info);
6112 else
6113 store_parm_decls_oldstyle (fndecl, arg_info);
6115 /* The next call to push_scope will be a function body. */
6117 next_is_function_body = true;
6119 /* Write a record describing this function definition to the prototypes
6120 file (if requested). */
6122 gen_aux_info_record (fndecl, 1, 0, proto);
6124 /* Initialize the RTL code for the function. */
6125 allocate_struct_function (fndecl);
6127 /* Begin the statement tree for this function. */
6128 begin_stmt_tree (&DECL_SAVED_TREE (fndecl));
6130 /* If this is a nested function, save away the sizes of any
6131 variable-size types so that we can expand them when generating
6132 RTL. */
6133 if (context)
6135 tree t;
6137 DECL_LANG_SPECIFIC (fndecl)->pending_sizes
6138 = nreverse (get_pending_sizes ());
6139 for (t = DECL_LANG_SPECIFIC (fndecl)->pending_sizes;
6141 t = TREE_CHAIN (t))
6142 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = context;
6145 /* This function is being processed in whole-function mode. */
6146 cfun->x_whole_function_mode_p = 1;
6148 /* Even though we're inside a function body, we still don't want to
6149 call expand_expr to calculate the size of a variable-sized array.
6150 We haven't necessarily assigned RTL to all variables yet, so it's
6151 not safe to try to expand expressions involving them. */
6152 immediate_size_expand = 0;
6153 cfun->x_dont_save_pending_sizes_p = 1;
6156 /* Finish up a function declaration and compile that function
6157 all the way to assembler language output. The free the storage
6158 for the function definition.
6160 This is called after parsing the body of the function definition. */
6162 void
6163 finish_function (void)
6165 tree fndecl = current_function_decl;
6167 /* When a function declaration is totally empty, e.g.
6168 void foo(void) { }
6169 (the argument list is irrelevant) the compstmt rule will not
6170 bother calling push_scope/pop_scope, which means we get here with
6171 the scope stack out of sync. Detect this situation by noticing
6172 that current_scope is still as store_parm_decls left it, and do
6173 a dummy push/pop to get back to consistency.
6174 Note that the call to push_scope does not actually push another
6175 scope - see there for details. */
6177 if (current_scope->parm_flag && next_is_function_body)
6179 push_scope ();
6180 pop_scope ();
6183 if (TREE_CODE (fndecl) == FUNCTION_DECL
6184 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6186 tree args = DECL_ARGUMENTS (fndecl);
6187 for (; args; args = TREE_CHAIN (args))
6189 tree type = TREE_TYPE (args);
6190 if (INTEGRAL_TYPE_P (type)
6191 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6192 DECL_ARG_TYPE (args) = integer_type_node;
6196 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6197 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6199 /* Must mark the RESULT_DECL as being in this function. */
6201 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6202 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6204 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6206 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6207 != integer_type_node)
6209 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6210 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6211 if (!warn_main)
6212 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6214 else
6216 #ifdef DEFAULT_MAIN_RETURN
6217 /* Make it so that `main' always returns success by default. */
6218 DEFAULT_MAIN_RETURN;
6219 #else
6220 if (flag_isoc99)
6221 c_expand_return (integer_zero_node);
6222 #endif
6226 finish_fname_decls ();
6228 /* Tie off the statement tree for this function. */
6229 finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
6231 /* Complain if there's just no return statement. */
6232 if (warn_return_type
6233 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6234 && !current_function_returns_value && !current_function_returns_null
6235 /* Don't complain if we abort. */
6236 && !current_function_returns_abnormally
6237 /* Don't warn for main(). */
6238 && !MAIN_NAME_P (DECL_NAME (fndecl))
6239 /* Or if they didn't actually specify a return type. */
6240 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6241 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6242 inline function, as we might never be compiled separately. */
6243 && DECL_INLINE (fndecl))
6244 warning ("no return statement in function returning non-void");
6246 /* With just -Wextra, complain only if function returns both with
6247 and without a value. */
6248 if (extra_warnings
6249 && current_function_returns_value
6250 && current_function_returns_null)
6251 warning ("this function may return with or without a value");
6253 /* We're leaving the context of this function, so zap cfun.
6254 It's still in DECL_STRUCT_FUNCTION , and we'll restore it in
6255 tree_rest_of_compilation. */
6256 cfun = NULL;
6258 /* ??? Objc emits functions after finalizing the compilation unit.
6259 This should be cleaned up later and this conditional removed. */
6260 if (!cgraph_global_info_ready)
6261 cgraph_finalize_function (fndecl, false);
6262 else
6263 c_expand_body (fndecl);
6264 current_function_decl = NULL;
6267 /* Generate the RTL for the body of FNDECL. If NESTED_P is nonzero,
6268 then we are already in the process of generating RTL for another
6269 function. */
6271 static void
6272 c_expand_body_1 (tree fndecl, int nested_p)
6274 if (nested_p)
6276 /* Make sure that we will evaluate variable-sized types involved
6277 in our function's type. */
6278 expand_pending_sizes (DECL_LANG_SPECIFIC (fndecl)->pending_sizes);
6280 /* Squirrel away our current state. */
6281 push_function_context ();
6284 tree_rest_of_compilation (fndecl, nested_p);
6286 if (nested_p)
6287 /* Return to the enclosing function. */
6288 pop_function_context ();
6290 if (DECL_STATIC_CONSTRUCTOR (fndecl))
6292 if (targetm.have_ctors_dtors)
6293 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6294 DEFAULT_INIT_PRIORITY);
6295 else
6296 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6299 if (DECL_STATIC_DESTRUCTOR (fndecl))
6301 if (targetm.have_ctors_dtors)
6302 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6303 DEFAULT_INIT_PRIORITY);
6304 else
6305 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6309 /* Like c_expand_body_1 but only for unnested functions. */
6311 void
6312 c_expand_body (tree fndecl)
6315 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6316 c_expand_body_1 (fndecl, 0);
6319 /* Check the declarations given in a for-loop for satisfying the C99
6320 constraints. */
6321 void
6322 check_for_loop_decls (void)
6324 struct c_binding *b;
6326 if (!flag_isoc99)
6328 /* If we get here, declarations have been used in a for loop without
6329 the C99 for loop scope. This doesn't make much sense, so don't
6330 allow it. */
6331 error ("'for' loop initial declaration used outside C99 mode");
6332 return;
6334 /* C99 subclause 6.8.5 paragraph 3:
6336 [#3] The declaration part of a for statement shall only
6337 declare identifiers for objects having storage class auto or
6338 register.
6340 It isn't clear whether, in this sentence, "identifiers" binds to
6341 "shall only declare" or to "objects" - that is, whether all identifiers
6342 declared must be identifiers for objects, or whether the restriction
6343 only applies to those that are. (A question on this in comp.std.c
6344 in November 2000 received no answer.) We implement the strictest
6345 interpretation, to avoid creating an extension which later causes
6346 problems. */
6348 for (b = current_scope->bindings; b; b = b->prev)
6350 tree id = b->id;
6351 tree decl = b->decl;
6353 if (!id)
6354 continue;
6356 switch (TREE_CODE (decl))
6358 case VAR_DECL:
6359 if (TREE_STATIC (decl))
6360 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6361 "initial declaration", decl, decl);
6362 else if (DECL_EXTERNAL (decl))
6363 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6364 "initial declaration", decl, decl);
6365 break;
6367 case RECORD_TYPE:
6368 error ("'struct %E' declared in 'for' loop initial declaration", id);
6369 break;
6370 case UNION_TYPE:
6371 error ("'union %E' declared in 'for' loop initial declaration", id);
6372 break;
6373 case ENUMERAL_TYPE:
6374 error ("'enum %E' declared in 'for' loop initial declaration", id);
6375 break;
6376 default:
6377 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6378 "initial declaration", decl, decl);
6383 /* Save and reinitialize the variables
6384 used during compilation of a C function. */
6386 void
6387 c_push_function_context (struct function *f)
6389 struct language_function *p;
6390 p = ggc_alloc (sizeof (struct language_function));
6391 f->language = p;
6393 p->base.x_stmt_tree = c_stmt_tree;
6394 p->base.x_scope_stmt_stack = c_scope_stmt_stack;
6395 p->x_in_iteration_stmt = c_in_iteration_stmt;
6396 p->x_in_case_stmt = c_in_case_stmt;
6397 p->returns_value = current_function_returns_value;
6398 p->returns_null = current_function_returns_null;
6399 p->returns_abnormally = current_function_returns_abnormally;
6400 p->warn_about_return_type = warn_about_return_type;
6401 p->extern_inline = current_extern_inline;
6404 /* Restore the variables used during compilation of a C function. */
6406 void
6407 c_pop_function_context (struct function *f)
6409 struct language_function *p = f->language;
6411 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6412 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6414 /* Stop pointing to the local nodes about to be freed. */
6415 /* But DECL_INITIAL must remain nonzero so we know this
6416 was an actual function definition. */
6417 DECL_INITIAL (current_function_decl) = error_mark_node;
6418 DECL_ARGUMENTS (current_function_decl) = 0;
6421 c_stmt_tree = p->base.x_stmt_tree;
6422 c_scope_stmt_stack = p->base.x_scope_stmt_stack;
6423 c_in_iteration_stmt = p->x_in_iteration_stmt;
6424 c_in_case_stmt = p->x_in_case_stmt;
6425 current_function_returns_value = p->returns_value;
6426 current_function_returns_null = p->returns_null;
6427 current_function_returns_abnormally = p->returns_abnormally;
6428 warn_about_return_type = p->warn_about_return_type;
6429 current_extern_inline = p->extern_inline;
6431 f->language = NULL;
6434 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6436 void
6437 c_dup_lang_specific_decl (tree decl)
6439 struct lang_decl *ld;
6441 if (!DECL_LANG_SPECIFIC (decl))
6442 return;
6444 ld = ggc_alloc (sizeof (struct lang_decl));
6445 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6446 DECL_LANG_SPECIFIC (decl) = ld;
6449 /* The functions below are required for functionality of doing
6450 function at once processing in the C front end. Currently these
6451 functions are not called from anywhere in the C front end, but as
6452 these changes continue, that will change. */
6454 /* Returns nonzero if the current statement is a full expression,
6455 i.e. temporaries created during that statement should be destroyed
6456 at the end of the statement. */
6459 stmts_are_full_exprs_p (void)
6461 return 0;
6464 /* Returns the stmt_tree (if any) to which statements are currently
6465 being added. If there is no active statement-tree, NULL is
6466 returned. */
6468 stmt_tree
6469 current_stmt_tree (void)
6471 return &c_stmt_tree;
6474 /* Returns the stack of SCOPE_STMTs for the current function. */
6476 tree *
6477 current_scope_stmt_stack (void)
6479 return &c_scope_stmt_stack;
6482 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6483 C. */
6486 anon_aggr_type_p (tree node ATTRIBUTE_UNUSED)
6488 return 0;
6491 /* Dummy function in place of callback used by C++. */
6493 void
6494 extract_interface_info (void)
6498 /* Return a new COMPOUND_STMT, after adding it to the current
6499 statement tree. */
6501 tree
6502 c_begin_compound_stmt (void)
6504 tree stmt;
6506 /* Create the COMPOUND_STMT. */
6507 stmt = add_stmt (build_stmt (COMPOUND_STMT, NULL_TREE));
6509 return stmt;
6512 /* Expand T (a DECL_STMT) if it declares an entity not handled by the
6513 common code. */
6515 void
6516 c_expand_decl_stmt (tree t)
6518 tree decl = DECL_STMT_DECL (t);
6520 /* Expand nested functions. */
6521 if (TREE_CODE (decl) == FUNCTION_DECL
6522 && DECL_CONTEXT (decl) == current_function_decl
6523 && DECL_SAVED_TREE (decl))
6524 c_expand_body_1 (decl, 1);
6527 /* Return the global value of T as a symbol. */
6529 tree
6530 identifier_global_value (tree t)
6532 struct c_binding *b;
6534 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6535 if (b->contour == file_scope || b->contour == external_scope)
6536 return b->decl;
6538 return 0;
6541 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6542 otherwise the name is found in ridpointers from RID_INDEX. */
6544 void
6545 record_builtin_type (enum rid rid_index, const char *name, tree type)
6547 tree id;
6548 if (name == 0)
6549 id = ridpointers[(int) rid_index];
6550 else
6551 id = get_identifier (name);
6552 pushdecl (build_decl (TYPE_DECL, id, type));
6555 /* Build the void_list_node (void_type_node having been created). */
6556 tree
6557 build_void_list_node (void)
6559 tree t = build_tree_list (NULL_TREE, void_type_node);
6560 return t;
6563 /* Return something to represent absolute declarators containing a *.
6564 TARGET is the absolute declarator that the * contains.
6565 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6566 to apply to the pointer type, represented as identifiers, possible mixed
6567 with attributes.
6569 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6570 if attributes are present) and whose type is the modifier list. */
6572 tree
6573 make_pointer_declarator (tree type_quals_attrs, tree target)
6575 tree quals, attrs;
6576 tree itarget = target;
6577 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6578 if (attrs != NULL_TREE)
6579 itarget = tree_cons (attrs, target, NULL_TREE);
6580 return build1 (INDIRECT_REF, quals, itarget);
6583 /* A wrapper around lhd_set_decl_assembler_name that gives static
6584 variables their C names if they are at file scope and only one
6585 translation unit is being compiled, for backwards compatibility
6586 with certain bizarre assembler hacks (like crtstuff.c). */
6588 void
6589 c_static_assembler_name (tree decl)
6591 if (num_in_fnames == 1
6592 && !TREE_PUBLIC (decl) && DECL_CONTEXT (decl)
6593 && TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
6594 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
6595 else
6596 lhd_set_decl_assembler_name (decl);
6599 /* Perform final processing on file-scope data. */
6600 static void
6601 c_write_global_declarations_1 (tree globals)
6603 size_t len = list_length (globals);
6604 tree *vec = xmalloc (sizeof (tree) * len);
6605 size_t i;
6606 tree decl;
6608 /* Process the decls in the order they were written. */
6609 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6610 vec[i] = decl;
6612 wrapup_global_declarations (vec, len);
6613 check_global_declarations (vec, len);
6615 free (vec);
6618 void
6619 c_write_global_declarations (void)
6621 tree t;
6623 /* We don't want to do this if generating a PCH. */
6624 if (pch_file)
6625 return;
6627 /* Process all file scopes in this compilation. */
6628 for (t = current_file_decl; t; t = TREE_CHAIN (t))
6629 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6631 /* Now do the same for the externals scope. */
6632 t = pop_scope ();
6633 if (t)
6634 c_write_global_declarations_1 (BLOCK_VARS (t));
6637 #include "gt-c-decl.h"