2004-08-30 Geoffrey Keating <geoffk@apple.com>
[official-gcc.git] / gcc / c-decl.c
blobb2f7bbe42d24e046f25855bebe89ccfce73ad7dd
1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* Process declarations and symbol lookup for C front end.
23 Also constructs types; the standard scalar types at initialization,
24 and structure, union, array and enum types when they are declared. */
26 /* ??? not all decl nodes are given the most useful possible
27 line numbers. For example, the CONST_DECLs for enum values. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "intl.h"
34 #include "tree.h"
35 #include "tree-inline.h"
36 #include "rtl.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "output.h"
40 #include "expr.h"
41 #include "c-tree.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "tm_p.h"
45 #include "cpplib.h"
46 #include "target.h"
47 #include "debug.h"
48 #include "opts.h"
49 #include "timevar.h"
50 #include "c-common.h"
51 #include "c-pragma.h"
52 #include "langhooks.h"
53 #include "tree-mudflap.h"
54 #include "tree-gimple.h"
55 #include "diagnostic.h"
56 #include "tree-dump.h"
57 #include "cgraph.h"
58 #include "hashtab.h"
59 #include "libfuncs.h"
60 #include "except.h"
61 #include "langhooks-def.h"
63 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
64 enum decl_context
65 { NORMAL, /* Ordinary declaration */
66 FUNCDEF, /* Function definition */
67 PARM, /* Declaration of parm before function body */
68 FIELD, /* Declaration inside struct or union */
69 TYPENAME}; /* Typename (inside cast or sizeof) */
72 /* Nonzero if we have seen an invalid cross reference
73 to a struct, union, or enum, but not yet printed the message. */
74 tree pending_invalid_xref;
76 /* File and line to appear in the eventual error message. */
77 location_t pending_invalid_xref_location;
79 /* True means we've initialized exception handling. */
80 bool c_eh_initialized_p;
82 /* While defining an enum type, this is 1 plus the last enumerator
83 constant value. Note that will do not have to save this or `enum_overflow'
84 around nested function definition since such a definition could only
85 occur in an enum value expression and we don't use these variables in
86 that case. */
88 static tree enum_next_value;
90 /* Nonzero means that there was overflow computing enum_next_value. */
92 static int enum_overflow;
94 /* These #defines are for clarity in working with the information block
95 returned by get_parm_info. */
96 #define ARG_INFO_PARMS(args) TREE_PURPOSE(args)
97 #define ARG_INFO_TAGS(args) TREE_VALUE(args)
98 #define ARG_INFO_TYPES(args) TREE_CHAIN(args)
99 #define ARG_INFO_OTHERS(args) TREE_TYPE(args)
101 /* The file and line that the prototype came from if this is an
102 old-style definition; used for diagnostics in
103 store_parm_decls_oldstyle. */
105 static location_t current_function_prototype_locus;
107 /* The argument information structure for the function currently being
108 defined. */
110 static GTY(()) tree current_function_arg_info;
112 /* The current statement tree. */
114 static GTY(()) struct stmt_tree_s c_stmt_tree;
116 /* State saving variables. */
117 tree c_break_label;
118 tree c_cont_label;
120 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
121 included in this invocation. Note that the current translation
122 unit is not included in this list. */
124 static GTY(()) tree all_translation_units;
126 /* A list of decls to be made automatically visible in each file scope. */
127 static GTY(()) tree visible_builtins;
129 /* Set to 0 at beginning of a function definition, set to 1 if
130 a return statement that specifies a return value is seen. */
132 int current_function_returns_value;
134 /* Set to 0 at beginning of a function definition, set to 1 if
135 a return statement with no argument is seen. */
137 int current_function_returns_null;
139 /* Set to 0 at beginning of a function definition, set to 1 if
140 a call to a noreturn function is seen. */
142 int current_function_returns_abnormally;
144 /* Set to nonzero by `grokdeclarator' for a function
145 whose return type is defaulted, if warnings for this are desired. */
147 static int warn_about_return_type;
149 /* Nonzero when starting a function declared `extern inline'. */
151 static int current_extern_inline;
153 /* True means global_bindings_p should return false even if the scope stack
154 says we are in file scope. */
155 bool c_override_global_bindings_to_false;
158 /* Each c_binding structure describes one binding of an identifier to
159 a decl. All the decls in a scope - irrespective of namespace - are
160 chained together by the ->prev field, which (as the name implies)
161 runs in reverse order. All the decls in a given namespace bound to
162 a given identifier are chained by the ->shadowed field, which runs
163 from inner to outer scopes.
165 The ->decl field usually points to a DECL node, but there are two
166 exceptions. In the namespace of type tags, the bound entity is a
167 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
168 identifier is encountered, it is bound to error_mark_node to
169 suppress further errors about that identifier in the current
170 function.
172 The ->type field stores the type of the declaration in this scope;
173 if NULL, the type is the type of the ->decl field. This is only of
174 relevance for objects with external or internal linkage which may
175 be redeclared in inner scopes, forming composite types that only
176 persist for the duration of those scopes. In the external scope,
177 this stores the composite of all the types declared for this
178 object, visible or not. The ->inner_comp field (used only at file
179 scope) stores whether an incomplete array type at file scope was
180 completed at an inner scope to an array size other than 1.
182 The depth field is copied from the scope structure that holds this
183 decl. It is used to preserve the proper ordering of the ->shadowed
184 field (see bind()) and also for a handful of special-case checks.
185 Finally, the invisible bit is true for a decl which should be
186 ignored for purposes of normal name lookup, and the nested bit is
187 true for a decl that's been bound a second time in an inner scope;
188 in all such cases, the binding in the outer scope will have its
189 invisible bit true. */
191 struct c_binding GTY((chain_next ("%h.prev")))
193 tree decl; /* the decl bound */
194 tree type; /* the type in this scope */
195 tree id; /* the identifier it's bound to */
196 struct c_binding *prev; /* the previous decl in this scope */
197 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
198 unsigned int depth : 28; /* depth of this scope */
199 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
200 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
201 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
202 /* one free bit */
204 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
205 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
206 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
207 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
209 #define I_SYMBOL_BINDING(node) \
210 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->symbol_binding)
211 #define I_SYMBOL_DECL(node) \
212 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
214 #define I_TAG_BINDING(node) \
215 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->tag_binding)
216 #define I_TAG_DECL(node) \
217 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
219 #define I_LABEL_BINDING(node) \
220 (((struct lang_identifier *)IDENTIFIER_NODE_CHECK(node))->label_binding)
221 #define I_LABEL_DECL(node) \
222 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
224 /* Each C symbol points to three linked lists of c_binding structures.
225 These describe the values of the identifier in the three different
226 namespaces defined by the language. */
228 struct lang_identifier GTY(())
230 struct c_common_identifier common_id;
231 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
232 struct c_binding *tag_binding; /* struct/union/enum tags */
233 struct c_binding *label_binding; /* labels */
236 /* Validate c-lang.c's assumptions. */
237 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
238 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
240 /* The resulting tree type. */
242 union lang_tree_node
243 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
244 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)")))
246 union tree_node GTY ((tag ("0"),
247 desc ("tree_node_structure (&%h)")))
248 generic;
249 struct lang_identifier GTY ((tag ("1"))) identifier;
252 /* Each c_scope structure describes the complete contents of one
253 scope. Four scopes are distinguished specially: the innermost or
254 current scope, the innermost function scope, the file scope (always
255 the second to outermost) and the outermost or external scope.
257 Most declarations are recorded in the current scope.
259 All normal label declarations are recorded in the innermost
260 function scope, as are bindings of undeclared identifiers to
261 error_mark_node. (GCC permits nested functions as an extension,
262 hence the 'innermost' qualifier.) Explicitly declared labels
263 (using the __label__ extension) appear in the current scope.
265 Being in the file scope (current_scope == file_scope) causes
266 special behavior in several places below. Also, under some
267 conditions the Objective-C front end records declarations in the
268 file scope even though that isn't the current scope.
270 All declarations with external linkage are recorded in the external
271 scope, even if they aren't visible there; this models the fact that
272 such declarations are visible to the entire program, and (with a
273 bit of cleverness, see pushdecl) allows diagnosis of some violations
274 of C99 6.2.2p7 and 6.2.7p2:
276 If, within the same translation unit, the same identifier appears
277 with both internal and external linkage, the behavior is
278 undefined.
280 All declarations that refer to the same object or function shall
281 have compatible type; otherwise, the behavior is undefined.
283 Initially only the built-in declarations, which describe compiler
284 intrinsic functions plus a subset of the standard library, are in
285 this scope.
287 The order of the blocks list matters, and it is frequently appended
288 to. To avoid having to walk all the way to the end of the list on
289 each insertion, or reverse the list later, we maintain a pointer to
290 the last list entry. (FIXME: It should be feasible to use a reversed
291 list here.)
293 The bindings list is strictly in reverse order of declarations;
294 pop_scope relies on this. */
297 struct c_scope GTY((chain_next ("%h.outer")))
299 /* The scope containing this one. */
300 struct c_scope *outer;
302 /* The next outermost function scope. */
303 struct c_scope *outer_function;
305 /* All bindings in this scope. */
306 struct c_binding *bindings;
308 /* For each scope (except the global one), a chain of BLOCK nodes
309 for all the scopes that were entered and exited one level down. */
310 tree blocks;
311 tree blocks_last;
313 /* The depth of this scope. Used to keep the ->shadowed chain of
314 bindings sorted innermost to outermost. */
315 unsigned int depth : 28;
317 /* True if we are currently filling this scope with parameter
318 declarations. */
319 BOOL_BITFIELD parm_flag : 1;
321 /* True if we already complained about forward parameter decls
322 in this scope. This prevents double warnings on
323 foo (int a; int b; ...) */
324 BOOL_BITFIELD warned_forward_parm_decls : 1;
326 /* True if this is the outermost block scope of a function body.
327 This scope contains the parameters, the local variables declared
328 in the outermost block, and all the labels (except those in
329 nested functions, or declared at block scope with __label__). */
330 BOOL_BITFIELD function_body : 1;
332 /* True means make a BLOCK for this scope no matter what. */
333 BOOL_BITFIELD keep : 1;
336 /* The scope currently in effect. */
338 static GTY(()) struct c_scope *current_scope;
340 /* The innermost function scope. Ordinary (not explicitly declared)
341 labels, bindings to error_mark_node, and the lazily-created
342 bindings of __func__ and its friends get this scope. */
344 static GTY(()) struct c_scope *current_function_scope;
346 /* The C file scope. This is reset for each input translation unit. */
348 static GTY(()) struct c_scope *file_scope;
350 /* The outermost scope. This is used for all declarations with
351 external linkage, and only these, hence the name. */
353 static GTY(()) struct c_scope *external_scope;
355 /* A chain of c_scope structures awaiting reuse. */
357 static GTY((deletable)) struct c_scope *scope_freelist;
359 /* A chain of c_binding structures awaiting reuse. */
361 static GTY((deletable)) struct c_binding *binding_freelist;
363 /* Append VAR to LIST in scope SCOPE. */
364 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
365 struct c_scope *s_ = (scope); \
366 tree d_ = (decl); \
367 if (s_->list##_last) \
368 TREE_CHAIN (s_->list##_last) = d_; \
369 else \
370 s_->list = d_; \
371 s_->list##_last = d_; \
372 } while (0)
374 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
375 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
376 struct c_scope *t_ = (tscope); \
377 struct c_scope *f_ = (fscope); \
378 if (t_->to##_last) \
379 TREE_CHAIN (t_->to##_last) = f_->from; \
380 else \
381 t_->to = f_->from; \
382 t_->to##_last = f_->from##_last; \
383 } while (0)
385 /* True means unconditionally make a BLOCK for the next scope pushed. */
387 static bool keep_next_level_flag;
389 /* True means the next call to push_scope will be the outermost scope
390 of a function body, so do not push a new scope, merely cease
391 expecting parameter decls. */
393 static bool next_is_function_body;
395 /* Functions called automatically at the beginning and end of execution. */
397 static GTY(()) tree static_ctors;
398 static GTY(()) tree static_dtors;
400 /* Forward declarations. */
401 static tree lookup_name_in_scope (tree, struct c_scope *);
402 static tree c_make_fname_decl (tree, int);
403 static tree grokdeclarator (tree, tree, enum decl_context, bool, tree *);
404 static tree grokparms (tree, bool);
405 static void layout_array_type (tree);
407 /* States indicating how grokdeclarator() should handle declspecs marked
408 with __attribute__((deprecated)). An object declared as
409 __attribute__((deprecated)) suppresses warnings of uses of other
410 deprecated items. */
412 enum deprecated_states {
413 DEPRECATED_NORMAL,
414 DEPRECATED_SUPPRESS
417 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
419 void
420 c_print_identifier (FILE *file, tree node, int indent)
422 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
423 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
424 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
425 if (C_IS_RESERVED_WORD (node))
427 tree rid = ridpointers[C_RID_CODE (node)];
428 indent_to (file, indent + 4);
429 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
430 (void *) rid, IDENTIFIER_POINTER (rid));
434 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
435 which may be any of several kinds of DECL or TYPE or error_mark_node,
436 in the scope SCOPE. */
437 static void
438 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
440 struct c_binding *b, **here;
442 if (binding_freelist)
444 b = binding_freelist;
445 binding_freelist = b->prev;
447 else
448 b = GGC_NEW (struct c_binding);
450 b->shadowed = 0;
451 b->decl = decl;
452 b->id = name;
453 b->depth = scope->depth;
454 b->invisible = invisible;
455 b->nested = nested;
456 b->inner_comp = 0;
458 b->type = 0;
460 b->prev = scope->bindings;
461 scope->bindings = b;
463 if (!name)
464 return;
466 switch (TREE_CODE (decl))
468 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
469 case ENUMERAL_TYPE:
470 case UNION_TYPE:
471 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
472 case VAR_DECL:
473 case FUNCTION_DECL:
474 case TYPE_DECL:
475 case CONST_DECL:
476 case PARM_DECL:
477 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
479 default:
480 abort ();
483 /* Locate the appropriate place in the chain of shadowed decls
484 to insert this binding. Normally, scope == current_scope and
485 this does nothing. */
486 while (*here && (*here)->depth > scope->depth)
487 here = &(*here)->shadowed;
489 b->shadowed = *here;
490 *here = b;
493 /* Clear the binding structure B, stick it on the binding_freelist,
494 and return the former value of b->prev. This is used by pop_scope
495 and get_parm_info to iterate destructively over all the bindings
496 from a given scope. */
497 static struct c_binding *
498 free_binding_and_advance (struct c_binding *b)
500 struct c_binding *prev = b->prev;
502 memset (b, 0, sizeof (struct c_binding));
503 b->prev = binding_freelist;
504 binding_freelist = b;
506 return prev;
510 /* Hook called at end of compilation to assume 1 elt
511 for a file-scope tentative array defn that wasn't complete before. */
513 void
514 c_finish_incomplete_decl (tree decl)
516 if (TREE_CODE (decl) == VAR_DECL)
518 tree type = TREE_TYPE (decl);
519 if (type != error_mark_node
520 && TREE_CODE (type) == ARRAY_TYPE
521 && ! DECL_EXTERNAL (decl)
522 && TYPE_DOMAIN (type) == 0)
524 warning ("%Jarray '%D' assumed to have one element", decl, decl);
526 complete_array_type (type, NULL_TREE, 1);
528 layout_decl (decl, 0);
533 /* The Objective-C front-end often needs to determine the current scope. */
535 void *
536 get_current_scope (void)
538 return current_scope;
541 /* The following function is used only by Objective-C. It needs to live here
542 because it accesses the innards of c_scope. */
544 void
545 objc_mark_locals_volatile (void *enclosing_blk)
547 struct c_scope *scope;
548 struct c_binding *b;
550 for (scope = current_scope;
551 scope && scope != enclosing_blk;
552 scope = scope->outer)
554 for (b = scope->bindings; b; b = b->prev)
556 if (TREE_CODE (b->decl) == VAR_DECL
557 || TREE_CODE (b->decl) == PARM_DECL)
559 C_DECL_REGISTER (b->decl) = 0;
560 DECL_REGISTER (b->decl) = 0;
561 TREE_THIS_VOLATILE (b->decl) = 1;
565 /* Do not climb up past the current function. */
566 if (scope->function_body)
567 break;
571 /* Nonzero if we are currently in file scope. */
574 global_bindings_p (void)
576 return current_scope == file_scope && !c_override_global_bindings_to_false;
579 void
580 keep_next_level (void)
582 keep_next_level_flag = true;
585 /* Identify this scope as currently being filled with parameters. */
587 void
588 declare_parm_level (void)
590 current_scope->parm_flag = true;
593 void
594 push_scope (void)
596 if (next_is_function_body)
598 /* This is the transition from the parameters to the top level
599 of the function body. These are the same scope
600 (C99 6.2.1p4,6) so we do not push another scope structure.
601 next_is_function_body is set only by store_parm_decls, which
602 in turn is called when and only when we are about to
603 encounter the opening curly brace for the function body.
605 The outermost block of a function always gets a BLOCK node,
606 because the debugging output routines expect that each
607 function has at least one BLOCK. */
608 current_scope->parm_flag = false;
609 current_scope->function_body = true;
610 current_scope->keep = true;
611 current_scope->outer_function = current_function_scope;
612 current_function_scope = current_scope;
614 keep_next_level_flag = false;
615 next_is_function_body = false;
617 else
619 struct c_scope *scope;
620 if (scope_freelist)
622 scope = scope_freelist;
623 scope_freelist = scope->outer;
625 else
626 scope = GGC_CNEW (struct c_scope);
628 scope->keep = keep_next_level_flag;
629 scope->outer = current_scope;
630 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
632 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
633 possible. */
634 if (current_scope && scope->depth == 0)
636 scope->depth--;
637 sorry ("GCC supports only %u nested scopes\n", scope->depth);
640 current_scope = scope;
641 keep_next_level_flag = false;
645 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
647 static void
648 set_type_context (tree type, tree context)
650 for (type = TYPE_MAIN_VARIANT (type); type;
651 type = TYPE_NEXT_VARIANT (type))
652 TYPE_CONTEXT (type) = context;
655 /* Exit a scope. Restore the state of the identifier-decl mappings
656 that were in effect when this scope was entered. Return a BLOCK
657 node containing all the DECLs in this scope that are of interest
658 to debug info generation. */
660 tree
661 pop_scope (void)
663 struct c_scope *scope = current_scope;
664 tree block, context, p;
665 struct c_binding *b;
667 bool functionbody = scope->function_body;
668 bool keep = functionbody || scope->keep || scope->bindings;
670 /* If appropriate, create a BLOCK to record the decls for the life
671 of this function. */
672 block = 0;
673 if (keep)
675 block = make_node (BLOCK);
676 BLOCK_SUBBLOCKS (block) = scope->blocks;
677 TREE_USED (block) = 1;
679 /* In each subblock, record that this is its superior. */
680 for (p = scope->blocks; p; p = TREE_CHAIN (p))
681 BLOCK_SUPERCONTEXT (p) = block;
683 BLOCK_VARS (block) = 0;
686 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
687 scope must be set so that they point to the appropriate
688 construct, i.e. either to the current FUNCTION_DECL node, or
689 else to the BLOCK node we just constructed.
691 Note that for tagged types whose scope is just the formal
692 parameter list for some function type specification, we can't
693 properly set their TYPE_CONTEXTs here, because we don't have a
694 pointer to the appropriate FUNCTION_TYPE node readily available
695 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
696 type nodes get set in `grokdeclarator' as soon as we have created
697 the FUNCTION_TYPE node which will represent the "scope" for these
698 "parameter list local" tagged types. */
699 if (scope->function_body)
700 context = current_function_decl;
701 else if (scope == file_scope)
703 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
704 TREE_CHAIN (file_decl) = all_translation_units;
705 all_translation_units = file_decl;
706 context = file_decl;
708 else
709 context = block;
711 /* Clear all bindings in this scope. */
712 for (b = scope->bindings; b; b = free_binding_and_advance (b))
714 p = b->decl;
715 switch (TREE_CODE (p))
717 case LABEL_DECL:
718 /* Warnings for unused labels, errors for undefined labels. */
719 if (TREE_USED (p) && !DECL_INITIAL (p))
721 error ("%Jlabel `%D' used but not defined", p, p);
722 DECL_INITIAL (p) = error_mark_node;
724 else if (!TREE_USED (p) && warn_unused_label)
726 if (DECL_INITIAL (p))
727 warning ("%Jlabel `%D' defined but not used", p, p);
728 else
729 warning ("%Jlabel `%D' declared but not defined", p, p);
731 /* Labels go in BLOCK_VARS. */
732 TREE_CHAIN (p) = BLOCK_VARS (block);
733 BLOCK_VARS (block) = p;
735 #ifdef ENABLE_CHECKING
736 if (I_LABEL_BINDING (b->id) != b) abort ();
737 #endif
738 I_LABEL_BINDING (b->id) = b->shadowed;
739 break;
741 case ENUMERAL_TYPE:
742 case UNION_TYPE:
743 case RECORD_TYPE:
744 set_type_context (p, context);
746 /* Types may not have tag-names, in which case the type
747 appears in the bindings list with b->id NULL. */
748 if (b->id)
750 #ifdef ENABLE_CHECKING
751 if (I_TAG_BINDING (b->id) != b) abort ();
752 #endif
753 I_TAG_BINDING (b->id) = b->shadowed;
755 break;
757 case FUNCTION_DECL:
758 /* Propagate TREE_ADDRESSABLE from nested functions to their
759 containing functions. */
760 if (! TREE_ASM_WRITTEN (p)
761 && DECL_INITIAL (p) != 0
762 && TREE_ADDRESSABLE (p)
763 && DECL_ABSTRACT_ORIGIN (p) != 0
764 && DECL_ABSTRACT_ORIGIN (p) != p)
765 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
766 goto common_symbol;
768 case VAR_DECL:
769 /* Warnings for unused variables. */
770 if (warn_unused_variable
771 && !TREE_USED (p)
772 && !DECL_IN_SYSTEM_HEADER (p)
773 && DECL_NAME (p)
774 && !DECL_ARTIFICIAL (p)
775 && (scope != file_scope
776 || (TREE_STATIC (p) && !TREE_PUBLIC (p)
777 && !TREE_THIS_VOLATILE (p)))
778 && scope != external_scope)
779 warning ("%Junused variable `%D'", p, p);
781 if (b->inner_comp)
783 error ("%Jtype of array %qD completed incompatibly with"
784 " implicit initialization", p, p);
787 /* Fall through. */
788 case TYPE_DECL:
789 case CONST_DECL:
790 common_symbol:
791 /* All of these go in BLOCK_VARS, but only if this is the
792 binding in the home scope. */
793 if (!b->nested)
795 TREE_CHAIN (p) = BLOCK_VARS (block);
796 BLOCK_VARS (block) = p;
798 /* If this is the file scope, and we are processing more
799 than one translation unit in this compilation, set
800 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
801 This makes same_translation_unit_p work, and causes
802 static declarations to be given disambiguating suffixes. */
803 if (scope == file_scope && num_in_fnames > 1)
805 DECL_CONTEXT (p) = context;
806 if (TREE_CODE (p) == TYPE_DECL)
807 set_type_context (TREE_TYPE (p), context);
810 /* Fall through. */
811 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
812 already been put there by store_parm_decls. Unused-
813 parameter warnings are handled by function.c.
814 error_mark_node obviously does not go in BLOCK_VARS and
815 does not get unused-variable warnings. */
816 case PARM_DECL:
817 case ERROR_MARK:
818 /* It is possible for a decl not to have a name. We get
819 here with b->id NULL in this case. */
820 if (b->id)
822 #ifdef ENABLE_CHECKING
823 if (I_SYMBOL_BINDING (b->id) != b) abort ();
824 #endif
825 I_SYMBOL_BINDING (b->id) = b->shadowed;
826 if (b->shadowed && b->shadowed->type)
827 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
829 break;
831 default:
832 abort ();
837 /* Dispose of the block that we just made inside some higher level. */
838 if ((scope->function_body || scope == file_scope) && context)
840 DECL_INITIAL (context) = block;
841 BLOCK_SUPERCONTEXT (block) = context;
843 else if (scope->outer)
845 if (block)
846 SCOPE_LIST_APPEND (scope->outer, blocks, block);
847 /* If we did not make a block for the scope just exited, any
848 blocks made for inner scopes must be carried forward so they
849 will later become subblocks of something else. */
850 else if (scope->blocks)
851 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
854 /* Pop the current scope, and free the structure for reuse. */
855 current_scope = scope->outer;
856 if (scope->function_body)
857 current_function_scope = scope->outer_function;
859 memset (scope, 0, sizeof (struct c_scope));
860 scope->outer = scope_freelist;
861 scope_freelist = scope;
863 return block;
866 void
867 push_file_scope (void)
869 tree decl;
871 if (file_scope)
872 return;
874 push_scope ();
875 file_scope = current_scope;
877 start_fname_decls ();
879 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
880 bind (DECL_NAME (decl), decl, file_scope,
881 /*invisible=*/false, /*nested=*/true);
884 void
885 pop_file_scope (void)
887 /* In case there were missing closebraces, get us back to the global
888 binding level. */
889 while (current_scope != file_scope)
890 pop_scope ();
892 /* __FUNCTION__ is defined at file scope (""). This
893 call may not be necessary as my tests indicate it
894 still works without it. */
895 finish_fname_decls ();
897 /* This is the point to write out a PCH if we're doing that.
898 In that case we do not want to do anything else. */
899 if (pch_file)
901 c_common_write_pch ();
902 return;
905 /* Pop off the file scope and close this translation unit. */
906 pop_scope ();
907 file_scope = 0;
908 cgraph_finalize_compilation_unit ();
911 /* Insert BLOCK at the end of the list of subblocks of the current
912 scope. This is used when a BIND_EXPR is expanded, to handle the
913 BLOCK node inside the BIND_EXPR. */
915 void
916 insert_block (tree block)
918 TREE_USED (block) = 1;
919 SCOPE_LIST_APPEND (current_scope, blocks, block);
922 /* Push a definition or a declaration of struct, union or enum tag "name".
923 "type" should be the type node.
924 We assume that the tag "name" is not already defined.
926 Note that the definition may really be just a forward reference.
927 In that case, the TYPE_SIZE will be zero. */
929 static void
930 pushtag (tree name, tree type)
932 /* Record the identifier as the type's name if it has none. */
933 if (name && !TYPE_NAME (type))
934 TYPE_NAME (type) = name;
935 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
937 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
938 tagged type we just added to the current scope. This fake
939 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
940 to output a representation of a tagged type, and it also gives
941 us a convenient place to record the "scope start" address for the
942 tagged type. */
944 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
946 /* An approximation for now, so we can tell this is a function-scope tag.
947 This will be updated in pop_scope. */
948 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
951 /* Subroutine of compare_decls. Allow harmless mismatches in return
952 and argument types provided that the type modes match. This function
953 return a unified type given a suitable match, and 0 otherwise. */
955 static tree
956 match_builtin_function_types (tree newtype, tree oldtype)
958 tree newrettype, oldrettype;
959 tree newargs, oldargs;
960 tree trytype, tryargs;
962 /* Accept the return type of the new declaration if same modes. */
963 oldrettype = TREE_TYPE (oldtype);
964 newrettype = TREE_TYPE (newtype);
966 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
967 return 0;
969 oldargs = TYPE_ARG_TYPES (oldtype);
970 newargs = TYPE_ARG_TYPES (newtype);
971 tryargs = newargs;
973 while (oldargs || newargs)
975 if (! oldargs
976 || ! newargs
977 || ! TREE_VALUE (oldargs)
978 || ! TREE_VALUE (newargs)
979 || TYPE_MODE (TREE_VALUE (oldargs))
980 != TYPE_MODE (TREE_VALUE (newargs)))
981 return 0;
983 oldargs = TREE_CHAIN (oldargs);
984 newargs = TREE_CHAIN (newargs);
987 trytype = build_function_type (newrettype, tryargs);
988 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
991 /* Subroutine of diagnose_mismatched_decls. Check for function type
992 mismatch involving an empty arglist vs a nonempty one and give clearer
993 diagnostics. */
994 static void
995 diagnose_arglist_conflict (tree newdecl, tree olddecl,
996 tree newtype, tree oldtype)
998 tree t;
1000 if (TREE_CODE (olddecl) != FUNCTION_DECL
1001 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1002 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1004 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1005 return;
1007 t = TYPE_ARG_TYPES (oldtype);
1008 if (t == 0)
1009 t = TYPE_ARG_TYPES (newtype);
1010 for (; t; t = TREE_CHAIN (t))
1012 tree type = TREE_VALUE (t);
1014 if (TREE_CHAIN (t) == 0
1015 && TYPE_MAIN_VARIANT (type) != void_type_node)
1017 inform ("a parameter list with an ellipsis can't match "
1018 "an empty parameter name list declaration");
1019 break;
1022 if (c_type_promotes_to (type) != type)
1024 inform ("an argument type that has a default promotion can't match "
1025 "an empty parameter name list declaration");
1026 break;
1031 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1032 old-style function definition, NEWDECL is a prototype declaration.
1033 Diagnose inconsistencies in the argument list. Returns TRUE if
1034 the prototype is compatible, FALSE if not. */
1035 static bool
1036 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1038 tree newargs, oldargs;
1039 int i;
1041 /* ??? Elsewhere TYPE_MAIN_VARIANT is not used in this context. */
1042 #define END_OF_ARGLIST(t) (TYPE_MAIN_VARIANT (t) == void_type_node)
1044 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1045 newargs = TYPE_ARG_TYPES (newtype);
1046 i = 1;
1048 for (;;)
1050 tree oldargtype = TREE_VALUE (oldargs);
1051 tree newargtype = TREE_VALUE (newargs);
1053 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1054 break;
1056 /* Reaching the end of just one list means the two decls don't
1057 agree on the number of arguments. */
1058 if (END_OF_ARGLIST (oldargtype))
1060 error ("%Jprototype for '%D' declares more arguments "
1061 "than previous old-style definition", newdecl, newdecl);
1062 return false;
1064 else if (END_OF_ARGLIST (newargtype))
1066 error ("%Jprototype for '%D' declares fewer arguments "
1067 "than previous old-style definition", newdecl, newdecl);
1068 return false;
1071 /* Type for passing arg must be consistent with that declared
1072 for the arg. */
1073 else if (! comptypes (oldargtype, newargtype))
1075 error ("%Jprototype for '%D' declares arg %d with incompatible type",
1076 newdecl, newdecl, i);
1077 return false;
1080 oldargs = TREE_CHAIN (oldargs);
1081 newargs = TREE_CHAIN (newargs);
1082 i++;
1085 /* If we get here, no errors were found, but do issue a warning
1086 for this poor-style construct. */
1087 warning ("%Jprototype for '%D' follows non-prototype definition",
1088 newdecl, newdecl);
1089 return true;
1090 #undef END_OF_ARGLIST
1093 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1094 first in a pair of mismatched declarations, using the diagnostic
1095 function DIAG. */
1096 static void
1097 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1099 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1101 else if (DECL_INITIAL (decl))
1102 diag (N_("%Jprevious definition of '%D' was here"), decl, decl);
1103 else if (C_DECL_IMPLICIT (decl))
1104 diag (N_("%Jprevious implicit declaration of '%D' was here"), decl, decl);
1105 else
1106 diag (N_("%Jprevious declaration of '%D' was here"), decl, decl);
1109 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1110 Returns true if the caller should proceed to merge the two, false
1111 if OLDDECL should simply be discarded. As a side effect, issues
1112 all necessary diagnostics for invalid or poor-style combinations.
1113 If it returns true, writes the types of NEWDECL and OLDDECL to
1114 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1115 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1117 static bool
1118 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1119 tree *newtypep, tree *oldtypep)
1121 tree newtype, oldtype;
1122 bool pedwarned = false;
1123 bool warned = false;
1125 /* If we have error_mark_node for either decl or type, just discard
1126 the previous decl - we're in an error cascade already. */
1127 if (olddecl == error_mark_node || newdecl == error_mark_node)
1128 return false;
1129 *oldtypep = oldtype = TREE_TYPE (olddecl);
1130 *newtypep = newtype = TREE_TYPE (newdecl);
1131 if (oldtype == error_mark_node || newtype == error_mark_node)
1132 return false;
1134 /* Two different categories of symbol altogether. This is an error
1135 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1136 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1138 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1139 && DECL_BUILT_IN (olddecl)
1140 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1142 error ("%J'%D' redeclared as different kind of symbol",
1143 newdecl, newdecl);
1144 locate_old_decl (olddecl, error);
1146 else if (TREE_PUBLIC (newdecl))
1147 warning ("%Jbuilt-in function '%D' declared as non-function",
1148 newdecl, newdecl);
1149 else if (warn_shadow)
1150 warning ("%Jdeclaration of '%D' shadows a built-in function",
1151 newdecl, newdecl);
1152 return false;
1155 if (!comptypes (oldtype, newtype))
1157 if (TREE_CODE (olddecl) == FUNCTION_DECL
1158 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1160 /* Accept harmless mismatch in function types.
1161 This is for the ffs and fprintf builtins. */
1162 tree trytype = match_builtin_function_types (newtype, oldtype);
1164 if (trytype && comptypes (newtype, trytype))
1165 *oldtypep = oldtype = trytype;
1166 else
1168 /* If types don't match for a built-in, throw away the
1169 built-in. No point in calling locate_old_decl here, it
1170 won't print anything. */
1171 warning ("%Jconflicting types for built-in function '%D'",
1172 newdecl, newdecl);
1173 return false;
1176 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1177 && DECL_IS_BUILTIN (olddecl))
1179 /* A conflicting function declaration for a predeclared
1180 function that isn't actually built in. Objective C uses
1181 these. The new declaration silently overrides everything
1182 but the volatility (i.e. noreturn) indication. See also
1183 below. FIXME: Make Objective C use normal builtins. */
1184 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1185 return false;
1187 /* Permit void foo (...) to match int foo (...) if the latter is
1188 the definition and implicit int was used. See
1189 c-torture/compile/920625-2.c. */
1190 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1191 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1192 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1193 && C_FUNCTION_IMPLICIT_INT (newdecl))
1195 pedwarn ("%Jconflicting types for '%D'", newdecl, newdecl);
1196 /* Make sure we keep void as the return type. */
1197 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1198 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1199 pedwarned = true;
1201 else
1203 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1204 error ("%J conflicting type qualifiers for '%D'", newdecl, newdecl);
1205 else
1206 error ("%Jconflicting types for '%D'", newdecl, newdecl);
1207 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1208 locate_old_decl (olddecl, error);
1209 return false;
1213 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1214 but silently ignore the redeclaration if either is in a system
1215 header. (Conflicting redeclarations were handled above.) */
1216 if (TREE_CODE (newdecl) == TYPE_DECL)
1218 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1219 return true; /* Allow OLDDECL to continue in use. */
1221 error ("%Jredefinition of typedef '%D'", newdecl, newdecl);
1222 locate_old_decl (olddecl, error);
1223 return false;
1226 /* Function declarations can either be 'static' or 'extern' (no
1227 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1228 can never conflict with each other on account of linkage (6.2.2p4).
1229 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1230 two definitions if one is 'extern inline' and one is not. The non-
1231 extern-inline definition supersedes the extern-inline definition. */
1232 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1234 /* If you declare a built-in function name as static, or
1235 define the built-in with an old-style definition (so we
1236 can't validate the argument list) the built-in definition is
1237 overridden, but optionally warn this was a bad choice of name. */
1238 if (DECL_BUILT_IN (olddecl)
1239 && !C_DECL_DECLARED_BUILTIN (olddecl)
1240 && (!TREE_PUBLIC (newdecl)
1241 || (DECL_INITIAL (newdecl)
1242 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1244 if (warn_shadow)
1245 warning ("%Jdeclaration of '%D' shadows a built-in function",
1246 newdecl, newdecl);
1247 /* Discard the old built-in function. */
1248 return false;
1251 if (DECL_INITIAL (newdecl))
1253 if (DECL_INITIAL (olddecl)
1254 && !(DECL_DECLARED_INLINE_P (olddecl)
1255 && DECL_EXTERNAL (olddecl)
1256 && !(DECL_DECLARED_INLINE_P (newdecl)
1257 && DECL_EXTERNAL (newdecl)
1258 && same_translation_unit_p (olddecl, newdecl))))
1260 error ("%Jredefinition of '%D'", newdecl, newdecl);
1261 locate_old_decl (olddecl, error);
1262 return false;
1265 /* If we have a prototype after an old-style function definition,
1266 the argument types must be checked specially. */
1267 else if (DECL_INITIAL (olddecl)
1268 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1269 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1270 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1272 locate_old_decl (olddecl, error);
1273 return false;
1275 /* A non-static declaration (even an "extern") followed by a
1276 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1277 The same is true for a static forward declaration at block
1278 scope followed by a non-static declaration/definition at file
1279 scope. Static followed by non-static at the same scope is
1280 not undefined behavior, and is the most convenient way to get
1281 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1282 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1283 we do diagnose it if -Wtraditional. */
1284 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1286 /* Two exceptions to the rule. If olddecl is an extern
1287 inline, or a predeclared function that isn't actually
1288 built in, newdecl silently overrides olddecl. The latter
1289 occur only in Objective C; see also above. (FIXME: Make
1290 Objective C use normal builtins.) */
1291 if (!DECL_IS_BUILTIN (olddecl)
1292 && !(DECL_EXTERNAL (olddecl)
1293 && DECL_DECLARED_INLINE_P (olddecl)))
1295 error ("%Jstatic declaration of '%D' follows "
1296 "non-static declaration", newdecl, newdecl);
1297 locate_old_decl (olddecl, error);
1299 return false;
1301 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1303 if (DECL_CONTEXT (olddecl))
1305 error ("%Jnon-static declaration of '%D' follows "
1306 "static declaration", newdecl, newdecl);
1307 locate_old_decl (olddecl, error);
1308 return false;
1310 else if (warn_traditional)
1312 warning ("%Jnon-static declaration of '%D' follows "
1313 "static declaration", newdecl, newdecl);
1314 warned = true;
1318 else if (TREE_CODE (newdecl) == VAR_DECL)
1320 /* Only variables can be thread-local, and all declarations must
1321 agree on this property. */
1322 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1324 if (DECL_THREAD_LOCAL (newdecl))
1325 error ("%Jthread-local declaration of '%D' follows "
1326 "non-thread-local declaration", newdecl, newdecl);
1327 else
1328 error ("%Jnon-thread-local declaration of '%D' follows "
1329 "thread-local declaration", newdecl, newdecl);
1331 locate_old_decl (olddecl, error);
1332 return false;
1335 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1336 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1338 error ("%Jredefinition of '%D'", newdecl, newdecl);
1339 locate_old_decl (olddecl, error);
1340 return false;
1343 /* Objects declared at file scope: if the first declaration had
1344 external linkage (even if it was an external reference) the
1345 second must have external linkage as well, or the behavior is
1346 undefined. If the first declaration had internal linkage, then
1347 the second must too, or else be an external reference (in which
1348 case the composite declaration still has internal linkage).
1349 As for function declarations, we warn about the static-then-
1350 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1351 if (DECL_FILE_SCOPE_P (newdecl)
1352 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1354 if (DECL_EXTERNAL (newdecl))
1356 if (!DECL_FILE_SCOPE_P (olddecl))
1358 error ("%Jextern declaration of %qD follows "
1359 "declaration with no linkage", newdecl, newdecl);
1360 locate_old_decl (olddecl, error);
1361 return false;
1363 else if (warn_traditional)
1365 warning ("%Jnon-static declaration of '%D' follows "
1366 "static declaration", newdecl, newdecl);
1367 warned = true;
1370 else
1372 if (TREE_PUBLIC (newdecl))
1373 error ("%Jnon-static declaration of '%D' follows "
1374 "static declaration", newdecl, newdecl);
1375 else
1376 error ("%Jstatic declaration of '%D' follows "
1377 "non-static declaration", newdecl, newdecl);
1379 locate_old_decl (olddecl, error);
1380 return false;
1383 /* Two objects with the same name declared at the same block
1384 scope must both be external references (6.7p3). */
1385 else if (!DECL_FILE_SCOPE_P (newdecl))
1387 if (DECL_EXTERNAL (newdecl))
1389 /* Extern with initializer at block scope, which will
1390 already have received an error. */
1392 else if (DECL_EXTERNAL (olddecl))
1394 error ("%Jdeclaration of '%D' with no linkage follows "
1395 "extern declaration", newdecl, newdecl);
1396 locate_old_decl (olddecl, error);
1398 else
1400 error ("%Jredeclaration of '%D' with no linkage",
1401 newdecl, newdecl);
1402 locate_old_decl (olddecl, error);
1405 return false;
1409 /* warnings */
1410 /* All decls must agree on a visibility. */
1411 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1412 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1414 warning ("%Jredeclaration of '%D' with different visibility "
1415 "(old visibility preserved)", newdecl, newdecl);
1416 warned = true;
1419 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1421 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1422 if (DECL_DECLARED_INLINE_P (newdecl)
1423 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1425 warning ("%Jinline declaration of '%D' follows "
1426 "declaration with attribute noinline", newdecl, newdecl);
1427 warned = true;
1429 else if (DECL_DECLARED_INLINE_P (olddecl)
1430 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1432 warning ("%Jdeclaration of '%D' with attribute noinline follows "
1433 "inline declaration ", newdecl, newdecl);
1434 warned = true;
1437 /* Inline declaration after use or definition.
1438 ??? Should we still warn about this now we have unit-at-a-time
1439 mode and can get it right?
1440 Definitely don't complain if the decls are in different translation
1441 units. */
1442 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1443 && same_translation_unit_p (olddecl, newdecl))
1445 if (TREE_USED (olddecl))
1447 warning ("%J'%D' declared inline after being called",
1448 olddecl, olddecl);
1449 warned = true;
1451 else if (DECL_INITIAL (olddecl))
1453 warning ("%J'%D' declared inline after its definition",
1454 olddecl, olddecl);
1455 warned = true;
1459 else /* PARM_DECL, VAR_DECL */
1461 /* Redeclaration of a parameter is a constraint violation (this is
1462 not explicitly stated, but follows from C99 6.7p3 [no more than
1463 one declaration of the same identifier with no linkage in the
1464 same scope, except type tags] and 6.2.2p6 [parameters have no
1465 linkage]). We must check for a forward parameter declaration,
1466 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1467 an extension, the mandatory diagnostic for which is handled by
1468 mark_forward_parm_decls. */
1470 if (TREE_CODE (newdecl) == PARM_DECL
1471 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1473 error ("%Jredefinition of parameter '%D'", newdecl, newdecl);
1474 locate_old_decl (olddecl, error);
1475 return false;
1479 /* Optional warning for completely redundant decls. */
1480 if (!warned && !pedwarned
1481 && warn_redundant_decls
1482 /* Don't warn about a function declaration followed by a
1483 definition. */
1484 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1485 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1486 /* Don't warn about redundant redeclarations of builtins. */
1487 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1488 && !DECL_BUILT_IN (newdecl)
1489 && DECL_BUILT_IN (olddecl)
1490 && !C_DECL_DECLARED_BUILTIN (olddecl))
1491 /* Don't warn about an extern followed by a definition. */
1492 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1493 /* Don't warn about forward parameter decls. */
1494 && !(TREE_CODE (newdecl) == PARM_DECL
1495 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl)))
1497 warning ("%Jredundant redeclaration of '%D'", newdecl, newdecl);
1498 warned = true;
1501 /* Report location of previous decl/defn in a consistent manner. */
1502 if (warned || pedwarned)
1503 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1505 return true;
1508 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1509 consistent with OLDDECL, but carries new information. Merge the
1510 new information into OLDDECL. This function issues no
1511 diagnostics. */
1513 static void
1514 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1516 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1517 && DECL_INITIAL (newdecl) != 0);
1519 /* For real parm decl following a forward decl, rechain the old decl
1520 in its new location and clear TREE_ASM_WRITTEN (it's not a
1521 forward decl anymore). */
1522 if (TREE_CODE (newdecl) == PARM_DECL
1523 && TREE_ASM_WRITTEN (olddecl) && ! TREE_ASM_WRITTEN (newdecl))
1525 struct c_binding *b, **here;
1527 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1528 if ((*here)->decl == olddecl)
1529 goto found;
1530 abort ();
1532 found:
1533 b = *here;
1534 *here = b->prev;
1535 b->prev = current_scope->bindings;
1536 current_scope->bindings = b;
1538 TREE_ASM_WRITTEN (olddecl) = 0;
1541 DECL_ATTRIBUTES (newdecl)
1542 = targetm.merge_decl_attributes (olddecl, newdecl);
1544 /* Merge the data types specified in the two decls. */
1545 TREE_TYPE (newdecl)
1546 = TREE_TYPE (olddecl)
1547 = composite_type (newtype, oldtype);
1549 /* Lay the type out, unless already done. */
1550 if (oldtype != TREE_TYPE (newdecl))
1552 if (TREE_TYPE (newdecl) != error_mark_node)
1553 layout_type (TREE_TYPE (newdecl));
1554 if (TREE_CODE (newdecl) != FUNCTION_DECL
1555 && TREE_CODE (newdecl) != TYPE_DECL
1556 && TREE_CODE (newdecl) != CONST_DECL)
1557 layout_decl (newdecl, 0);
1559 else
1561 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1562 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1563 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1564 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1565 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1566 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1568 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1569 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1573 /* Keep the old rtl since we can safely use it. */
1574 COPY_DECL_RTL (olddecl, newdecl);
1576 /* Merge the type qualifiers. */
1577 if (TREE_READONLY (newdecl))
1578 TREE_READONLY (olddecl) = 1;
1580 if (TREE_THIS_VOLATILE (newdecl))
1582 TREE_THIS_VOLATILE (olddecl) = 1;
1583 if (TREE_CODE (newdecl) == VAR_DECL)
1584 make_var_volatile (newdecl);
1587 /* Keep source location of definition rather than declaration. */
1588 if (DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1589 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1591 /* Merge the unused-warning information. */
1592 if (DECL_IN_SYSTEM_HEADER (olddecl))
1593 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1594 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1595 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1597 /* Merge the initialization information. */
1598 if (DECL_INITIAL (newdecl) == 0)
1599 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1601 /* Merge the section attribute.
1602 We want to issue an error if the sections conflict but that must be
1603 done later in decl_attributes since we are called before attributes
1604 are assigned. */
1605 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1606 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1608 /* Copy the assembler name.
1609 Currently, it can only be defined in the prototype. */
1610 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1612 /* Use visibility of whichever declaration had it specified */
1613 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1615 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1616 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1619 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1621 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1622 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1623 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1624 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1625 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1626 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1627 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1628 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1629 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1632 /* Merge the storage class information. */
1633 merge_weak (newdecl, olddecl);
1635 /* For functions, static overrides non-static. */
1636 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1638 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1639 /* This is since we don't automatically
1640 copy the attributes of NEWDECL into OLDDECL. */
1641 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1642 /* If this clears `static', clear it in the identifier too. */
1643 if (! TREE_PUBLIC (olddecl))
1644 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1646 if (DECL_EXTERNAL (newdecl))
1648 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1649 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1651 /* An extern decl does not override previous storage class. */
1652 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1653 if (! DECL_EXTERNAL (newdecl))
1655 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1656 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1659 else
1661 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1662 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1665 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1667 /* If we're redefining a function previously defined as extern
1668 inline, make sure we emit debug info for the inline before we
1669 throw it away, in case it was inlined into a function that hasn't
1670 been written out yet. */
1671 if (new_is_definition && DECL_INITIAL (olddecl))
1673 if (TREE_USED (olddecl)
1674 /* In unit-at-a-time mode we never inline re-defined extern
1675 inline functions. */
1676 && !flag_unit_at_a_time
1677 && cgraph_function_possibly_inlined_p (olddecl))
1678 (*debug_hooks->outlining_inline_function) (olddecl);
1680 /* The new defn must not be inline. */
1681 DECL_INLINE (newdecl) = 0;
1682 DECL_UNINLINABLE (newdecl) = 1;
1684 else
1686 /* If either decl says `inline', this fn is inline,
1687 unless its definition was passed already. */
1688 if (DECL_DECLARED_INLINE_P (newdecl)
1689 || DECL_DECLARED_INLINE_P (olddecl))
1690 DECL_DECLARED_INLINE_P (newdecl) = 1;
1692 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1693 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1696 if (DECL_BUILT_IN (olddecl))
1698 /* If redeclaring a builtin function, it stays built in.
1699 But it gets tagged as having been declared. */
1700 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1701 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1702 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1705 /* Also preserve various other info from the definition. */
1706 if (! new_is_definition)
1708 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1709 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1710 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1711 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1712 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1714 /* Set DECL_INLINE on the declaration if we've got a body
1715 from which to instantiate. */
1716 if (DECL_INLINE (olddecl) && ! DECL_UNINLINABLE (newdecl))
1718 DECL_INLINE (newdecl) = 1;
1719 DECL_ABSTRACT_ORIGIN (newdecl)
1720 = DECL_ABSTRACT_ORIGIN (olddecl);
1723 else
1725 /* If a previous declaration said inline, mark the
1726 definition as inlinable. */
1727 if (DECL_DECLARED_INLINE_P (newdecl)
1728 && ! DECL_UNINLINABLE (newdecl))
1729 DECL_INLINE (newdecl) = 1;
1733 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1734 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1736 unsigned olddecl_uid = DECL_UID (olddecl);
1737 tree olddecl_context = DECL_CONTEXT (olddecl);
1739 memcpy ((char *) olddecl + sizeof (struct tree_common),
1740 (char *) newdecl + sizeof (struct tree_common),
1741 sizeof (struct tree_decl) - sizeof (struct tree_common));
1742 DECL_UID (olddecl) = olddecl_uid;
1743 DECL_CONTEXT (olddecl) = olddecl_context;
1746 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1747 so that encode_section_info has a chance to look at the new decl
1748 flags and attributes. */
1749 if (DECL_RTL_SET_P (olddecl)
1750 && (TREE_CODE (olddecl) == FUNCTION_DECL
1751 || (TREE_CODE (olddecl) == VAR_DECL
1752 && TREE_STATIC (olddecl))))
1753 make_decl_rtl (olddecl);
1756 /* Handle when a new declaration NEWDECL has the same name as an old
1757 one OLDDECL in the same binding contour. Prints an error message
1758 if appropriate.
1760 If safely possible, alter OLDDECL to look like NEWDECL, and return
1761 true. Otherwise, return false. */
1763 static bool
1764 duplicate_decls (tree newdecl, tree olddecl)
1766 tree newtype = NULL, oldtype = NULL;
1768 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1769 return false;
1771 merge_decls (newdecl, olddecl, newtype, oldtype);
1772 return true;
1776 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1777 static void
1778 warn_if_shadowing (tree new_decl)
1780 struct c_binding *b;
1782 /* Shadow warnings wanted? */
1783 if (!warn_shadow
1784 /* No shadow warnings for internally generated vars. */
1785 || DECL_IS_BUILTIN (new_decl)
1786 /* No shadow warnings for vars made for inlining. */
1787 || DECL_FROM_INLINE (new_decl)
1788 /* Don't warn about the parm names in function declarator
1789 within a function declarator. It would be nice to avoid
1790 warning in any function declarator in a declaration, as
1791 opposed to a definition, but there is no way to tell
1792 it's not a definition at this point. */
1793 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1794 return;
1796 /* Is anything being shadowed? Invisible decls do not count. */
1797 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1798 if (b->decl && b->decl != new_decl && !b->invisible)
1800 tree old_decl = b->decl;
1802 if (TREE_CODE (old_decl) == PARM_DECL)
1803 warning ("%Jdeclaration of '%D' shadows a parameter",
1804 new_decl, new_decl);
1805 else if (DECL_FILE_SCOPE_P (old_decl))
1806 warning ("%Jdeclaration of '%D' shadows a global declaration",
1807 new_decl, new_decl);
1808 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1809 && DECL_BUILT_IN (old_decl))
1810 warning ("%Jdeclaration of '%D' shadows a built-in function",
1811 new_decl, new_decl);
1812 else
1813 warning ("%Jdeclaration of '%D' shadows a previous local",
1814 new_decl, new_decl);
1816 if (TREE_CODE (old_decl) != FUNCTION_DECL
1817 || ! DECL_BUILT_IN (old_decl))
1818 warning ("%Jshadowed declaration is here", old_decl);
1820 break;
1825 /* Subroutine of pushdecl.
1827 X is a TYPE_DECL for a typedef statement. Create a brand new
1828 ..._TYPE node (which will be just a variant of the existing
1829 ..._TYPE node with identical properties) and then install X
1830 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1832 The whole point here is to end up with a situation where each
1833 and every ..._TYPE node the compiler creates will be uniquely
1834 associated with AT MOST one node representing a typedef name.
1835 This way, even though the compiler substitutes corresponding
1836 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1837 early on, later parts of the compiler can always do the reverse
1838 translation and get back the corresponding typedef name. For
1839 example, given:
1841 typedef struct S MY_TYPE;
1842 MY_TYPE object;
1844 Later parts of the compiler might only know that `object' was of
1845 type `struct S' if it were not for code just below. With this
1846 code however, later parts of the compiler see something like:
1848 struct S' == struct S
1849 typedef struct S' MY_TYPE;
1850 struct S' object;
1852 And they can then deduce (from the node for type struct S') that
1853 the original object declaration was:
1855 MY_TYPE object;
1857 Being able to do this is important for proper support of protoize,
1858 and also for generating precise symbolic debugging information
1859 which takes full account of the programmer's (typedef) vocabulary.
1861 Obviously, we don't want to generate a duplicate ..._TYPE node if
1862 the TYPE_DECL node that we are now processing really represents a
1863 standard built-in type.
1865 Since all standard types are effectively declared at line zero
1866 in the source file, we can easily check to see if we are working
1867 on a standard type by checking the current value of lineno. */
1869 static void
1870 clone_underlying_type (tree x)
1872 if (DECL_IS_BUILTIN (x))
1874 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1875 TYPE_NAME (TREE_TYPE (x)) = x;
1877 else if (TREE_TYPE (x) != error_mark_node
1878 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1880 tree tt = TREE_TYPE (x);
1881 DECL_ORIGINAL_TYPE (x) = tt;
1882 tt = build_variant_type_copy (tt);
1883 TYPE_NAME (tt) = x;
1884 TREE_USED (tt) = TREE_USED (x);
1885 TREE_TYPE (x) = tt;
1889 /* Record a decl-node X as belonging to the current lexical scope.
1890 Check for errors (such as an incompatible declaration for the same
1891 name already seen in the same scope).
1893 Returns either X or an old decl for the same name.
1894 If an old decl is returned, it may have been smashed
1895 to agree with what X says. */
1897 tree
1898 pushdecl (tree x)
1900 tree name = DECL_NAME (x);
1901 struct c_scope *scope = current_scope;
1902 struct c_binding *b;
1903 bool nested = false;
1905 /* Functions need the lang_decl data. */
1906 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_LANG_SPECIFIC (x))
1907 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
1909 /* Must set DECL_CONTEXT for everything not at file scope or
1910 DECL_FILE_SCOPE_P won't work. Local externs don't count
1911 unless they have initializers (which generate code). */
1912 if (current_function_decl
1913 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
1914 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
1915 DECL_CONTEXT (x) = current_function_decl;
1917 /* Anonymous decls are just inserted in the scope. */
1918 if (!name)
1920 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
1921 return x;
1924 /* First, see if there is another declaration with the same name in
1925 the current scope. If there is, duplicate_decls may do all the
1926 work for us. If duplicate_decls returns false, that indicates
1927 two incompatible decls in the same scope; we are to silently
1928 replace the old one (duplicate_decls has issued all appropriate
1929 diagnostics). In particular, we should not consider possible
1930 duplicates in the external scope, or shadowing. */
1931 b = I_SYMBOL_BINDING (name);
1932 if (b && B_IN_SCOPE (b, scope))
1934 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
1935 && COMPLETE_TYPE_P (TREE_TYPE (x)))
1936 b->inner_comp = false;
1937 if (duplicate_decls (x, b->decl))
1938 return b->decl;
1939 else
1940 goto skip_external_and_shadow_checks;
1943 /* All declarations with external linkage, and all external
1944 references, go in the external scope, no matter what scope is
1945 current. However, the binding in that scope is ignored for
1946 purposes of normal name lookup. A separate binding structure is
1947 created in the requested scope; this governs the normal
1948 visibility of the symbol.
1950 The binding in the externals scope is used exclusively for
1951 detecting duplicate declarations of the same object, no matter
1952 what scope they are in; this is what we do here. (C99 6.2.7p2:
1953 All declarations that refer to the same object or function shall
1954 have compatible type; otherwise, the behavior is undefined.) */
1955 if (DECL_EXTERNAL (x) || scope == file_scope)
1957 tree type = TREE_TYPE (x);
1958 tree vistype = 0;
1959 tree visdecl = 0;
1960 bool type_saved = false;
1961 if (b && !B_IN_EXTERNAL_SCOPE (b)
1962 && (TREE_CODE (b->decl) == FUNCTION_DECL
1963 || TREE_CODE (b->decl) == VAR_DECL)
1964 && DECL_FILE_SCOPE_P (b->decl))
1966 visdecl = b->decl;
1967 vistype = TREE_TYPE (visdecl);
1969 if (warn_nested_externs
1970 && scope != file_scope
1971 && !DECL_IN_SYSTEM_HEADER (x))
1972 warning ("nested extern declaration of '%D'", x);
1974 while (b && !B_IN_EXTERNAL_SCOPE (b))
1976 /* If this decl might be modified, save its type. This is
1977 done here rather than when the decl is first bound
1978 because the type may change after first binding, through
1979 being completed or through attributes being added. If we
1980 encounter multiple such decls, only the first should have
1981 its type saved; the others will already have had their
1982 proper types saved and the types will not have changed as
1983 their scopes will not have been re-entered. */
1984 if (DECL_FILE_SCOPE_P (b->decl) && !type_saved)
1986 b->type = TREE_TYPE (b->decl);
1987 type_saved = true;
1989 if (B_IN_FILE_SCOPE (b)
1990 && TREE_CODE (b->decl) == VAR_DECL
1991 && TREE_STATIC (b->decl)
1992 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
1993 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
1994 && TREE_CODE (type) == ARRAY_TYPE
1995 && TYPE_DOMAIN (type)
1996 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
1997 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
1999 /* Array type completed in inner scope, which should be
2000 diagnosed if the completion does not have size 1 and
2001 it does not get completed in the file scope. */
2002 b->inner_comp = true;
2004 b = b->shadowed;
2007 /* If a matching external declaration has been found, set its
2008 type to the composite of all the types of that declaration.
2009 After the consistency checks, it will be reset to the
2010 composite of the visible types only. */
2011 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2012 && b->type)
2013 TREE_TYPE (b->decl) = b->type;
2015 /* The point of the same_translation_unit_p check here is,
2016 we want to detect a duplicate decl for a construct like
2017 foo() { extern bar(); } ... static bar(); but not if
2018 they are in different translation units. In any case,
2019 the static does not go in the externals scope. */
2020 if (b
2021 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2022 && duplicate_decls (x, b->decl))
2024 tree thistype;
2025 thistype = (vistype ? composite_type (vistype, type) : type);
2026 b->type = TREE_TYPE (b->decl);
2027 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2028 thistype
2029 = build_type_attribute_variant (thistype,
2030 TYPE_ATTRIBUTES (b->type));
2031 TREE_TYPE (b->decl) = thistype;
2032 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2033 return b->decl;
2035 else if (TREE_PUBLIC (x))
2037 if (visdecl && !b && duplicate_decls (x, visdecl))
2039 /* An external declaration at block scope referring to a
2040 visible entity with internal linkage. The composite
2041 type will already be correct for this scope, so we
2042 just need to fall through to make the declaration in
2043 this scope. */
2044 nested = true;
2046 else
2048 bind (name, x, external_scope, /*invisible=*/true,
2049 /*nested=*/false);
2050 nested = true;
2054 /* Similarly, a declaration of a function with static linkage at
2055 block scope must be checked against any existing declaration
2056 of that function at file scope. */
2057 else if (TREE_CODE (x) == FUNCTION_DECL && scope != file_scope
2058 && !TREE_PUBLIC (x) && !DECL_INITIAL (x))
2060 if (warn_nested_externs && !DECL_IN_SYSTEM_HEADER (x))
2061 warning ("nested static declaration of '%D'", x);
2063 while (b && !B_IN_FILE_SCOPE (b))
2064 b = b->shadowed;
2066 if (b && same_translation_unit_p (x, b->decl)
2067 && duplicate_decls (x, b->decl))
2069 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2070 return b->decl;
2072 else
2074 bind (name, x, file_scope, /*invisible=*/true, /*nested=*/false);
2075 nested = true;
2079 warn_if_shadowing (x);
2081 skip_external_and_shadow_checks:
2082 if (TREE_CODE (x) == TYPE_DECL)
2083 clone_underlying_type (x);
2085 bind (name, x, scope, /*invisible=*/false, nested);
2087 /* If x's type is incomplete because it's based on a
2088 structure or union which has not yet been fully declared,
2089 attach it to that structure or union type, so we can go
2090 back and complete the variable declaration later, if the
2091 structure or union gets fully declared.
2093 If the input is erroneous, we can have error_mark in the type
2094 slot (e.g. "f(void a, ...)") - that doesn't count as an
2095 incomplete type. */
2096 if (TREE_TYPE (x) != error_mark_node
2097 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2099 tree element = TREE_TYPE (x);
2101 while (TREE_CODE (element) == ARRAY_TYPE)
2102 element = TREE_TYPE (element);
2103 element = TYPE_MAIN_VARIANT (element);
2105 if ((TREE_CODE (element) == RECORD_TYPE
2106 || TREE_CODE (element) == UNION_TYPE)
2107 && (TREE_CODE (x) != TYPE_DECL
2108 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2109 && !COMPLETE_TYPE_P (element))
2110 C_TYPE_INCOMPLETE_VARS (element)
2111 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2113 return x;
2116 /* Record X as belonging to file scope.
2117 This is used only internally by the Objective-C front end,
2118 and is limited to its needs. duplicate_decls is not called;
2119 if there is any preexisting decl for this identifier, it is an ICE. */
2121 tree
2122 pushdecl_top_level (tree x)
2124 tree name;
2125 bool nested = false;
2127 if (TREE_CODE (x) != VAR_DECL)
2128 abort ();
2130 name = DECL_NAME (x);
2132 if (I_SYMBOL_BINDING (name))
2133 abort ();
2135 if (TREE_PUBLIC (x))
2137 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2138 nested = true;
2140 if (file_scope)
2141 bind (name, x, file_scope, /*invisible=*/false, nested);
2143 return x;
2146 static void
2147 implicit_decl_warning (tree id, tree olddecl)
2149 void (*diag) (const char *, ...);
2150 switch (mesg_implicit_function_declaration)
2152 case 0: return;
2153 case 1: diag = warning; break;
2154 case 2: diag = error; break;
2155 default: abort ();
2158 diag (N_("implicit declaration of function '%E'"), id);
2159 if (olddecl)
2160 locate_old_decl (olddecl, diag);
2163 /* Generate an implicit declaration for identifier FUNCTIONID as a
2164 function of type int (). */
2166 tree
2167 implicitly_declare (tree functionid)
2169 struct c_binding *b;
2170 tree decl = 0;
2171 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2173 if (B_IN_SCOPE (b, external_scope))
2175 decl = b->decl;
2176 break;
2180 if (decl)
2182 /* FIXME: Objective-C has weird not-really-builtin functions
2183 which are supposed to be visible automatically. They wind up
2184 in the external scope because they're pushed before the file
2185 scope gets created. Catch this here and rebind them into the
2186 file scope. */
2187 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2189 bind (functionid, decl, file_scope,
2190 /*invisible=*/false, /*nested=*/true);
2191 return decl;
2193 else
2195 tree newtype = default_function_type;
2196 if (b->type)
2197 TREE_TYPE (decl) = b->type;
2198 /* Implicit declaration of a function already declared
2199 (somehow) in a different scope, or as a built-in.
2200 If this is the first time this has happened, warn;
2201 then recycle the old declaration but with the new type. */
2202 if (!C_DECL_IMPLICIT (decl))
2204 implicit_decl_warning (functionid, decl);
2205 C_DECL_IMPLICIT (decl) = 1;
2207 if (DECL_BUILT_IN (decl))
2209 newtype = build_type_attribute_variant (newtype,
2210 TYPE_ATTRIBUTES
2211 (TREE_TYPE (decl)));
2212 if (!comptypes (newtype, TREE_TYPE (decl)))
2214 warning ("incompatible implicit declaration of built-in"
2215 " function %qD", decl);
2216 newtype = TREE_TYPE (decl);
2219 else
2221 if (!comptypes (newtype, TREE_TYPE (decl)))
2223 error ("incompatible implicit declaration of function %qD",
2224 decl);
2225 locate_old_decl (decl, error);
2228 b->type = TREE_TYPE (decl);
2229 TREE_TYPE (decl) = newtype;
2230 bind (functionid, decl, current_scope,
2231 /*invisible=*/false, /*nested=*/true);
2232 return decl;
2236 /* Not seen before. */
2237 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2238 DECL_EXTERNAL (decl) = 1;
2239 TREE_PUBLIC (decl) = 1;
2240 C_DECL_IMPLICIT (decl) = 1;
2241 implicit_decl_warning (functionid, 0);
2243 /* C89 says implicit declarations are in the innermost block.
2244 So we record the decl in the standard fashion. */
2245 decl = pushdecl (decl);
2247 /* No need to call objc_check_decl here - it's a function type. */
2248 rest_of_decl_compilation (decl, 0, 0);
2250 /* Write a record describing this implicit function declaration
2251 to the prototypes file (if requested). */
2252 gen_aux_info_record (decl, 0, 1, 0);
2254 /* Possibly apply some default attributes to this implicit declaration. */
2255 decl_attributes (&decl, NULL_TREE, 0);
2257 return decl;
2260 /* Issue an error message for a reference to an undeclared variable
2261 ID, including a reference to a builtin outside of function-call
2262 context. Establish a binding of the identifier to error_mark_node
2263 in an appropriate scope, which will suppress further errors for the
2264 same identifier. */
2265 void
2266 undeclared_variable (tree id)
2268 static bool already = false;
2269 struct c_scope *scope;
2271 if (current_function_decl == 0)
2273 error ("'%E' undeclared here (not in a function)", id);
2274 scope = current_scope;
2276 else
2278 error ("'%E' undeclared (first use in this function)", id);
2280 if (! already)
2282 error ("(Each undeclared identifier is reported only once");
2283 error ("for each function it appears in.)");
2284 already = true;
2287 /* If we are parsing old-style parameter decls, current_function_decl
2288 will be nonnull but current_function_scope will be null. */
2289 scope = current_function_scope ? current_function_scope : current_scope;
2291 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2294 /* Subroutine of lookup_label, declare_label, define_label: construct a
2295 LABEL_DECL with all the proper frills. */
2297 static tree
2298 make_label (tree name, location_t location)
2300 tree label = build_decl (LABEL_DECL, name, void_type_node);
2302 DECL_CONTEXT (label) = current_function_decl;
2303 DECL_MODE (label) = VOIDmode;
2304 DECL_SOURCE_LOCATION (label) = location;
2306 return label;
2309 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2310 Create one if none exists so far for the current function.
2311 This is called when a label is used in a goto expression or
2312 has its address taken. */
2314 tree
2315 lookup_label (tree name)
2317 tree label;
2319 if (current_function_decl == 0)
2321 error ("label %s referenced outside of any function",
2322 IDENTIFIER_POINTER (name));
2323 return 0;
2326 /* Use a label already defined or ref'd with this name, but not if
2327 it is inherited from a containing function and wasn't declared
2328 using __label__. */
2329 label = I_LABEL_DECL (name);
2330 if (label && (DECL_CONTEXT (label) == current_function_decl
2331 || C_DECLARED_LABEL_FLAG (label)))
2333 /* If the label has only been declared, update its apparent
2334 location to point here, for better diagnostics if it
2335 turns out not to have been defined. */
2336 if (!TREE_USED (label))
2337 DECL_SOURCE_LOCATION (label) = input_location;
2338 return label;
2341 /* No label binding for that identifier; make one. */
2342 label = make_label (name, input_location);
2344 /* Ordinary labels go in the current function scope. */
2345 bind (name, label, current_function_scope,
2346 /*invisible=*/false, /*nested=*/false);
2347 return label;
2350 /* Make a label named NAME in the current function, shadowing silently
2351 any that may be inherited from containing functions or containing
2352 scopes. This is called for __label__ declarations. */
2354 tree
2355 declare_label (tree name)
2357 struct c_binding *b = I_LABEL_BINDING (name);
2358 tree label;
2360 /* Check to make sure that the label hasn't already been declared
2361 at this scope */
2362 if (b && B_IN_CURRENT_SCOPE (b))
2364 error ("duplicate label declaration `%s'", IDENTIFIER_POINTER (name));
2365 locate_old_decl (b->decl, error);
2367 /* Just use the previous declaration. */
2368 return b->decl;
2371 label = make_label (name, input_location);
2372 C_DECLARED_LABEL_FLAG (label) = 1;
2374 /* Declared labels go in the current scope. */
2375 bind (name, label, current_scope,
2376 /*invisible=*/false, /*nested=*/false);
2377 return label;
2380 /* Define a label, specifying the location in the source file.
2381 Return the LABEL_DECL node for the label, if the definition is valid.
2382 Otherwise return 0. */
2384 tree
2385 define_label (location_t location, tree name)
2387 /* Find any preexisting label with this name. It is an error
2388 if that label has already been defined in this function, or
2389 if there is a containing function with a declared label with
2390 the same name. */
2391 tree label = I_LABEL_DECL (name);
2393 if (label
2394 && ((DECL_CONTEXT (label) == current_function_decl
2395 && DECL_INITIAL (label) != 0)
2396 || (DECL_CONTEXT (label) != current_function_decl
2397 && C_DECLARED_LABEL_FLAG (label))))
2399 error ("%Hduplicate label `%D'", &location, label);
2400 locate_old_decl (label, error);
2401 return 0;
2403 else if (label && DECL_CONTEXT (label) == current_function_decl)
2405 /* The label has been used or declared already in this function,
2406 but not defined. Update its location to point to this
2407 definition. */
2408 DECL_SOURCE_LOCATION (label) = location;
2410 else
2412 /* No label binding for that identifier; make one. */
2413 label = make_label (name, location);
2415 /* Ordinary labels go in the current function scope. */
2416 bind (name, label, current_function_scope,
2417 /*invisible=*/false, /*nested=*/false);
2420 if (warn_traditional && !in_system_header && lookup_name (name))
2421 warning ("%Htraditional C lacks a separate namespace for labels, "
2422 "identifier `%s' conflicts", &location,
2423 IDENTIFIER_POINTER (name));
2425 /* Mark label as having been defined. */
2426 DECL_INITIAL (label) = error_mark_node;
2427 return label;
2430 /* Given NAME, an IDENTIFIER_NODE,
2431 return the structure (or union or enum) definition for that name.
2432 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2433 CODE says which kind of type the caller wants;
2434 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2435 If the wrong kind of type is found, an error is reported. */
2437 static tree
2438 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2440 struct c_binding *b = I_TAG_BINDING (name);
2441 int thislevel = 0;
2443 if (!b || !b->decl)
2444 return 0;
2446 /* We only care about whether it's in this level if
2447 thislevel_only was set or it might be a type clash. */
2448 if (thislevel_only || TREE_CODE (b->decl) != code)
2450 /* For our purposes, a tag in the external scope is the same as
2451 a tag in the file scope. (Primarily relevant to Objective-C
2452 and its builtin structure tags, which get pushed before the
2453 file scope is created.) */
2454 if (B_IN_CURRENT_SCOPE (b)
2455 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2456 thislevel = 1;
2459 if (thislevel_only && !thislevel)
2460 return 0;
2462 if (TREE_CODE (b->decl) != code)
2464 /* Definition isn't the kind we were looking for. */
2465 pending_invalid_xref = name;
2466 pending_invalid_xref_location = input_location;
2468 /* If in the same binding level as a declaration as a tag
2469 of a different type, this must not be allowed to
2470 shadow that tag, so give the error immediately.
2471 (For example, "struct foo; union foo;" is invalid.) */
2472 if (thislevel)
2473 pending_xref_error ();
2475 return b->decl;
2478 /* Print an error message now
2479 for a recent invalid struct, union or enum cross reference.
2480 We don't print them immediately because they are not invalid
2481 when used in the `struct foo;' construct for shadowing. */
2483 void
2484 pending_xref_error (void)
2486 if (pending_invalid_xref != 0)
2487 error ("%H`%s' defined as wrong kind of tag",
2488 &pending_invalid_xref_location,
2489 IDENTIFIER_POINTER (pending_invalid_xref));
2490 pending_invalid_xref = 0;
2494 /* Look up NAME in the current scope and its superiors
2495 in the namespace of variables, functions and typedefs.
2496 Return a ..._DECL node of some kind representing its definition,
2497 or return 0 if it is undefined. */
2499 tree
2500 lookup_name (tree name)
2502 struct c_binding *b = I_SYMBOL_BINDING (name);
2503 if (b && !b->invisible)
2504 return b->decl;
2505 return 0;
2508 /* Similar to `lookup_name' but look only at the indicated scope. */
2510 static tree
2511 lookup_name_in_scope (tree name, struct c_scope *scope)
2513 struct c_binding *b;
2515 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2516 if (B_IN_SCOPE (b, scope))
2517 return b->decl;
2518 return 0;
2521 /* Create the predefined scalar types of C,
2522 and some nodes representing standard constants (0, 1, (void *) 0).
2523 Initialize the global scope.
2524 Make definitions for built-in primitive functions. */
2526 void
2527 c_init_decl_processing (void)
2529 tree endlink;
2530 tree ptr_ftype_void, ptr_ftype_ptr;
2531 location_t save_loc = input_location;
2533 /* Adds some ggc roots, and reserved words for c-parse.in. */
2534 c_parse_init ();
2536 current_function_decl = 0;
2538 /* Make the externals scope. */
2539 push_scope ();
2540 external_scope = current_scope;
2542 /* Declarations from c_common_nodes_and_builtins must not be associated
2543 with this input file, lest we get differences between using and not
2544 using preprocessed headers. */
2545 #ifdef USE_MAPPED_LOCATION
2546 input_location = BUILTINS_LOCATION;
2547 #else
2548 input_location.file = "<built-in>";
2549 input_location.line = 0;
2550 #endif
2552 build_common_tree_nodes (flag_signed_char, false);
2554 c_common_nodes_and_builtins ();
2556 /* In C, comparisons and TRUTH_* expressions have type int. */
2557 truthvalue_type_node = integer_type_node;
2558 truthvalue_true_node = integer_one_node;
2559 truthvalue_false_node = integer_zero_node;
2561 /* Even in C99, which has a real boolean type. */
2562 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2563 boolean_type_node));
2565 endlink = void_list_node;
2566 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2567 ptr_ftype_ptr
2568 = build_function_type (ptr_type_node,
2569 tree_cons (NULL_TREE, ptr_type_node, endlink));
2571 input_location = save_loc;
2573 pedantic_lvalues = true;
2575 make_fname_decl = c_make_fname_decl;
2576 start_fname_decls ();
2579 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2580 decl, NAME is the initialization string and TYPE_DEP indicates whether
2581 NAME depended on the type of the function. As we don't yet implement
2582 delayed emission of static data, we mark the decl as emitted
2583 so it is not placed in the output. Anything using it must therefore pull
2584 out the STRING_CST initializer directly. FIXME. */
2586 static tree
2587 c_make_fname_decl (tree id, int type_dep)
2589 const char *name = fname_as_string (type_dep);
2590 tree decl, type, init;
2591 size_t length = strlen (name);
2593 type = build_array_type
2594 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
2595 build_index_type (size_int (length)));
2597 decl = build_decl (VAR_DECL, id, type);
2599 TREE_STATIC (decl) = 1;
2600 TREE_READONLY (decl) = 1;
2601 DECL_ARTIFICIAL (decl) = 1;
2603 init = build_string (length + 1, name);
2604 free ((char *) name);
2605 TREE_TYPE (init) = type;
2606 DECL_INITIAL (decl) = init;
2608 TREE_USED (decl) = 1;
2610 if (current_function_decl)
2612 DECL_CONTEXT (decl) = current_function_decl;
2613 bind (id, decl, current_function_scope,
2614 /*invisible=*/false, /*nested=*/false);
2617 finish_decl (decl, init, NULL_TREE);
2619 return decl;
2622 /* Return a definition for a builtin function named NAME and whose data type
2623 is TYPE. TYPE should be a function type with argument types.
2624 FUNCTION_CODE tells later passes how to compile calls to this function.
2625 See tree.h for its possible values.
2627 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2628 the name to be called if we can't opencode the function. If
2629 ATTRS is nonzero, use that for the function's attribute list. */
2631 tree
2632 builtin_function (const char *name, tree type, int function_code,
2633 enum built_in_class cl, const char *library_name,
2634 tree attrs)
2636 tree id = get_identifier (name);
2637 tree decl = build_decl (FUNCTION_DECL, id, type);
2638 TREE_PUBLIC (decl) = 1;
2639 DECL_EXTERNAL (decl) = 1;
2640 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2641 DECL_BUILT_IN_CLASS (decl) = cl;
2642 DECL_FUNCTION_CODE (decl) = function_code;
2643 if (library_name)
2644 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2646 /* Should never be called on a symbol with a preexisting meaning. */
2647 if (I_SYMBOL_BINDING (id))
2648 abort ();
2650 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2652 /* Builtins in the implementation namespace are made visible without
2653 needing to be explicitly declared. See push_file_scope. */
2654 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2656 TREE_CHAIN (decl) = visible_builtins;
2657 visible_builtins = decl;
2660 /* Possibly apply some default attributes to this built-in function. */
2661 if (attrs)
2662 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2663 else
2664 decl_attributes (&decl, NULL_TREE, 0);
2666 return decl;
2669 /* Called when a declaration is seen that contains no names to declare.
2670 If its type is a reference to a structure, union or enum inherited
2671 from a containing scope, shadow that tag name for the current scope
2672 with a forward reference.
2673 If its type defines a new named structure or union
2674 or defines an enum, it is valid but we need not do anything here.
2675 Otherwise, it is an error. */
2677 void
2678 shadow_tag (tree declspecs)
2680 shadow_tag_warned (declspecs, 0);
2683 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2684 but no pedwarn. */
2685 void
2686 shadow_tag_warned (tree declspecs, int warned)
2688 int found_tag = 0;
2689 tree link;
2690 tree specs, attrs;
2692 pending_invalid_xref = 0;
2694 /* Remove the attributes from declspecs, since they will confuse the
2695 following code. */
2696 split_specs_attrs (declspecs, &specs, &attrs);
2698 for (link = specs; link; link = TREE_CHAIN (link))
2700 tree value = TREE_VALUE (link);
2701 enum tree_code code = TREE_CODE (value);
2703 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2704 /* Used to test also that TYPE_SIZE (value) != 0.
2705 That caused warning for `struct foo;' at top level in the file. */
2707 tree name = TYPE_NAME (value);
2708 tree t;
2710 found_tag++;
2712 if (name == 0)
2714 if (warned != 1 && code != ENUMERAL_TYPE)
2715 /* Empty unnamed enum OK */
2717 pedwarn ("unnamed struct/union that defines no instances");
2718 warned = 1;
2721 else
2723 t = lookup_tag (code, name, 1);
2725 if (t == 0)
2727 t = make_node (code);
2728 pushtag (name, t);
2732 else
2734 if (!warned && ! in_system_header)
2736 warning ("useless keyword or type name in empty declaration");
2737 warned = 2;
2742 if (found_tag > 1)
2743 error ("two types specified in one empty declaration");
2745 if (warned != 1)
2747 if (found_tag == 0)
2748 pedwarn ("empty declaration");
2752 /* Construct an array declarator. EXPR is the expression inside [], or
2753 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2754 to the pointer to which a parameter array is converted). STATIC_P is
2755 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2756 is true if the array is [*], a VLA of unspecified length which is
2757 nevertheless a complete type (not currently implemented by GCC),
2758 false otherwise. The declarator is constructed as an ARRAY_REF
2759 (to be decoded by grokdeclarator), whose operand 0 is what's on the
2760 left of the [] (filled by in set_array_declarator_inner) and operand 1
2761 is the expression inside; whose TREE_TYPE is the type qualifiers and
2762 which has TREE_STATIC set if "static" is used. */
2764 tree
2765 build_array_declarator (tree expr, tree quals, bool static_p,
2766 bool vla_unspec_p)
2768 tree decl;
2769 decl = build_nt (ARRAY_REF, NULL_TREE, expr, NULL_TREE, NULL_TREE);
2770 TREE_TYPE (decl) = quals;
2771 TREE_STATIC (decl) = (static_p ? 1 : 0);
2772 if (pedantic && !flag_isoc99)
2774 if (static_p || quals != NULL_TREE)
2775 pedwarn ("ISO C90 does not support `static' or type qualifiers in parameter array declarators");
2776 if (vla_unspec_p)
2777 pedwarn ("ISO C90 does not support `[*]' array declarators");
2779 if (vla_unspec_p)
2780 warning ("GCC does not yet properly implement `[*]' array declarators");
2781 return decl;
2784 /* Set the type of an array declarator. DECL is the declarator, as
2785 constructed by build_array_declarator; TYPE is what appears on the left
2786 of the [] and goes in operand 0. ABSTRACT_P is true if it is an
2787 abstract declarator, false otherwise; this is used to reject static and
2788 type qualifiers in abstract declarators, where they are not in the
2789 C99 grammar. */
2791 tree
2792 set_array_declarator_inner (tree decl, tree type, bool abstract_p)
2794 TREE_OPERAND (decl, 0) = type;
2795 if (abstract_p && (TREE_TYPE (decl) != NULL_TREE || TREE_STATIC (decl)))
2796 error ("static or type qualifiers in abstract declarator");
2797 return decl;
2800 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
2802 tree
2803 groktypename (tree type_name)
2805 tree specs, attrs;
2807 if (TREE_CODE (type_name) != TREE_LIST)
2808 return type_name;
2810 split_specs_attrs (TREE_PURPOSE (type_name), &specs, &attrs);
2812 type_name = grokdeclarator (TREE_VALUE (type_name), specs, TYPENAME, false,
2813 NULL);
2815 /* Apply attributes. */
2816 decl_attributes (&type_name, attrs, 0);
2818 return type_name;
2821 /* Return a PARM_DECL node for a given pair of specs and declarator. */
2823 tree
2824 groktypename_in_parm_context (tree type_name)
2826 if (TREE_CODE (type_name) != TREE_LIST)
2827 return type_name;
2828 return grokdeclarator (TREE_VALUE (type_name),
2829 TREE_PURPOSE (type_name),
2830 PARM, false, NULL);
2833 /* Decode a declarator in an ordinary declaration or data definition.
2834 This is called as soon as the type information and variable name
2835 have been parsed, before parsing the initializer if any.
2836 Here we create the ..._DECL node, fill in its type,
2837 and put it on the list of decls for the current context.
2838 The ..._DECL node is returned as the value.
2840 Exception: for arrays where the length is not specified,
2841 the type is left null, to be filled in by `finish_decl'.
2843 Function definitions do not come here; they go to start_function
2844 instead. However, external and forward declarations of functions
2845 do go through here. Structure field declarations are done by
2846 grokfield and not through here. */
2848 tree
2849 start_decl (tree declarator, tree declspecs, bool initialized, tree attributes)
2851 tree decl;
2852 tree tem;
2854 /* An object declared as __attribute__((deprecated)) suppresses
2855 warnings of uses of other deprecated items. */
2856 if (lookup_attribute ("deprecated", attributes))
2857 deprecated_state = DEPRECATED_SUPPRESS;
2859 decl = grokdeclarator (declarator, declspecs,
2860 NORMAL, initialized, NULL);
2862 deprecated_state = DEPRECATED_NORMAL;
2864 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
2865 && MAIN_NAME_P (DECL_NAME (decl)))
2866 warning ("%J'%D' is usually a function", decl, decl);
2868 if (initialized)
2869 /* Is it valid for this decl to have an initializer at all?
2870 If not, set INITIALIZED to zero, which will indirectly
2871 tell 'finish_decl' to ignore the initializer once it is parsed. */
2872 switch (TREE_CODE (decl))
2874 case TYPE_DECL:
2875 error ("typedef '%D' is initialized (use __typeof__ instead)", decl);
2876 initialized = 0;
2877 break;
2879 case FUNCTION_DECL:
2880 error ("function '%D' is initialized like a variable", decl);
2881 initialized = 0;
2882 break;
2884 case PARM_DECL:
2885 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
2886 error ("parameter '%D' is initialized", decl);
2887 initialized = 0;
2888 break;
2890 default:
2891 /* Don't allow initializations for incomplete types except for
2892 arrays which might be completed by the initialization. */
2894 /* This can happen if the array size is an undefined macro.
2895 We already gave a warning, so we don't need another one. */
2896 if (TREE_TYPE (decl) == error_mark_node)
2897 initialized = 0;
2898 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
2900 /* A complete type is ok if size is fixed. */
2902 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
2903 || C_DECL_VARIABLE_SIZE (decl))
2905 error ("variable-sized object may not be initialized");
2906 initialized = 0;
2909 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
2911 error ("variable '%D' has initializer but incomplete type", decl);
2912 initialized = 0;
2914 else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
2916 error ("elements of array '%D' have incomplete type", decl);
2917 initialized = 0;
2921 if (initialized)
2923 if (current_scope == file_scope)
2924 TREE_STATIC (decl) = 1;
2926 /* Tell 'pushdecl' this is an initialized decl
2927 even though we don't yet have the initializer expression.
2928 Also tell 'finish_decl' it may store the real initializer. */
2929 DECL_INITIAL (decl) = error_mark_node;
2932 /* If this is a function declaration, write a record describing it to the
2933 prototypes file (if requested). */
2935 if (TREE_CODE (decl) == FUNCTION_DECL)
2936 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
2938 /* ANSI specifies that a tentative definition which is not merged with
2939 a non-tentative definition behaves exactly like a definition with an
2940 initializer equal to zero. (Section 3.7.2)
2942 -fno-common gives strict ANSI behavior, though this tends to break
2943 a large body of code that grew up without this rule.
2945 Thread-local variables are never common, since there's no entrenched
2946 body of code to break, and it allows more efficient variable references
2947 in the presence of dynamic linking. */
2949 if (TREE_CODE (decl) == VAR_DECL
2950 && !initialized
2951 && TREE_PUBLIC (decl)
2952 && !DECL_THREAD_LOCAL (decl)
2953 && !flag_no_common)
2954 DECL_COMMON (decl) = 1;
2956 /* Set attributes here so if duplicate decl, will have proper attributes. */
2957 decl_attributes (&decl, attributes, 0);
2959 if (TREE_CODE (decl) == FUNCTION_DECL
2960 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
2962 tree ce = declarator;
2964 if (TREE_CODE (ce) == INDIRECT_REF)
2965 ce = TREE_OPERAND (declarator, 0);
2966 if (TREE_CODE (ce) == CALL_EXPR)
2968 tree args = TREE_PURPOSE (TREE_OPERAND (ce, 1));
2969 for (; args; args = TREE_CHAIN (args))
2971 tree type = TREE_TYPE (args);
2972 if (type && INTEGRAL_TYPE_P (type)
2973 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2974 DECL_ARG_TYPE (args) = integer_type_node;
2979 if (TREE_CODE (decl) == FUNCTION_DECL
2980 && DECL_DECLARED_INLINE_P (decl)
2981 && DECL_UNINLINABLE (decl)
2982 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
2983 warning ("%Jinline function '%D' given attribute noinline", decl, decl);
2985 /* Add this decl to the current scope.
2986 TEM may equal DECL or it may be a previous decl of the same name. */
2987 tem = pushdecl (decl);
2989 if (initialized)
2990 DECL_EXTERNAL (tem) = 0;
2992 return tem;
2995 /* Finish processing of a declaration;
2996 install its initial value.
2997 If the length of an array type is not known before,
2998 it must be determined now, from the initial value, or it is an error. */
3000 void
3001 finish_decl (tree decl, tree init, tree asmspec_tree)
3003 tree type = TREE_TYPE (decl);
3004 int was_incomplete = (DECL_SIZE (decl) == 0);
3005 const char *asmspec = 0;
3007 /* If a name was specified, get the string. */
3008 if (current_scope == file_scope)
3009 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3010 if (asmspec_tree)
3011 asmspec = TREE_STRING_POINTER (asmspec_tree);
3013 /* If `start_decl' didn't like having an initialization, ignore it now. */
3014 if (init != 0 && DECL_INITIAL (decl) == 0)
3015 init = 0;
3017 /* Don't crash if parm is initialized. */
3018 if (TREE_CODE (decl) == PARM_DECL)
3019 init = 0;
3021 if (init)
3022 store_init_value (decl, init);
3024 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3025 || TREE_CODE (decl) == FUNCTION_DECL
3026 || TREE_CODE (decl) == FIELD_DECL))
3027 objc_check_decl (decl);
3029 /* Deduce size of array from initialization, if not already known. */
3030 if (TREE_CODE (type) == ARRAY_TYPE
3031 && TYPE_DOMAIN (type) == 0
3032 && TREE_CODE (decl) != TYPE_DECL)
3034 int do_default
3035 = (TREE_STATIC (decl)
3036 /* Even if pedantic, an external linkage array
3037 may have incomplete type at first. */
3038 ? pedantic && !TREE_PUBLIC (decl)
3039 : !DECL_EXTERNAL (decl));
3040 int failure
3041 = complete_array_type (type, DECL_INITIAL (decl), do_default);
3043 /* Get the completed type made by complete_array_type. */
3044 type = TREE_TYPE (decl);
3046 if (failure == 1)
3047 error ("%Jinitializer fails to determine size of '%D'", decl, decl);
3049 else if (failure == 2)
3051 if (do_default)
3052 error ("%Jarray size missing in '%D'", decl, decl);
3053 /* If a `static' var's size isn't known,
3054 make it extern as well as static, so it does not get
3055 allocated.
3056 If it is not `static', then do not mark extern;
3057 finish_incomplete_decl will give it a default size
3058 and it will get allocated. */
3059 else if (!pedantic && TREE_STATIC (decl) && ! TREE_PUBLIC (decl))
3060 DECL_EXTERNAL (decl) = 1;
3063 /* TYPE_MAX_VALUE is always one less than the number of elements
3064 in the array, because we start counting at zero. Therefore,
3065 warn only if the value is less than zero. */
3066 else if (pedantic && TYPE_DOMAIN (type) != 0
3067 && tree_int_cst_sgn (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < 0)
3068 error ("%Jzero or negative size array '%D'", decl, decl);
3070 layout_decl (decl, 0);
3073 if (TREE_CODE (decl) == VAR_DECL)
3075 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3076 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3077 layout_decl (decl, 0);
3079 if (DECL_SIZE (decl) == 0
3080 /* Don't give an error if we already gave one earlier. */
3081 && TREE_TYPE (decl) != error_mark_node
3082 && (TREE_STATIC (decl)
3083 /* A static variable with an incomplete type
3084 is an error if it is initialized.
3085 Also if it is not file scope.
3086 Otherwise, let it through, but if it is not `extern'
3087 then it may cause an error message later. */
3088 ? (DECL_INITIAL (decl) != 0
3089 || !DECL_FILE_SCOPE_P (decl))
3090 /* An automatic variable with an incomplete type
3091 is an error. */
3092 : !DECL_EXTERNAL (decl)))
3094 error ("%Jstorage size of '%D' isn't known", decl, decl);
3095 TREE_TYPE (decl) = error_mark_node;
3098 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3099 && DECL_SIZE (decl) != 0)
3101 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3102 constant_expression_warning (DECL_SIZE (decl));
3103 else
3104 error ("%Jstorage size of '%D' isn't constant", decl, decl);
3107 if (TREE_USED (type))
3108 TREE_USED (decl) = 1;
3111 /* If this is a function and an assembler name is specified, reset DECL_RTL
3112 so we can give it its new name. Also, update built_in_decls if it
3113 was a normal built-in. */
3114 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3116 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3118 tree builtin = built_in_decls [DECL_FUNCTION_CODE (decl)];
3119 set_user_assembler_name (builtin, asmspec);
3120 if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMCPY)
3121 init_block_move_fn (asmspec);
3122 else if (DECL_FUNCTION_CODE (decl) == BUILT_IN_MEMSET)
3123 init_block_clear_fn (asmspec);
3125 set_user_assembler_name (decl, asmspec);
3128 /* If #pragma weak was used, mark the decl weak now. */
3129 if (current_scope == file_scope)
3130 maybe_apply_pragma_weak (decl);
3132 /* If this is a variable definition, determine its ELF visibility. */
3133 if (TREE_CODE (decl) == VAR_DECL
3134 && TREE_STATIC (decl)
3135 && !DECL_EXTERNAL (decl))
3136 c_determine_visibility (decl);
3138 /* Output the assembler code and/or RTL code for variables and functions,
3139 unless the type is an undefined structure or union.
3140 If not, it will get done when the type is completed. */
3142 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3144 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3145 if (c_dialect_objc ())
3146 objc_check_decl (decl);
3148 if (asmspec)
3150 /* If this is not a static variable, issue a warning.
3151 It doesn't make any sense to give an ASMSPEC for an
3152 ordinary, non-register local variable. Historically,
3153 GCC has accepted -- but ignored -- the ASMSPEC in
3154 this case. */
3155 if (! DECL_FILE_SCOPE_P (decl)
3156 && TREE_CODE (decl) == VAR_DECL
3157 && !C_DECL_REGISTER (decl)
3158 && !TREE_STATIC (decl))
3159 warning ("%Jignoring asm-specifier for non-static local "
3160 "variable '%D'", decl, decl);
3161 else if (C_DECL_REGISTER (decl))
3162 change_decl_assembler_name (decl, get_identifier (asmspec));
3163 else
3164 set_user_assembler_name (decl, asmspec);
3167 if (DECL_FILE_SCOPE_P (decl))
3169 if (DECL_INITIAL (decl) == NULL_TREE
3170 || DECL_INITIAL (decl) == error_mark_node)
3171 /* Don't output anything
3172 when a tentative file-scope definition is seen.
3173 But at end of compilation, do output code for them. */
3174 DECL_DEFER_OUTPUT (decl) = 1;
3175 rest_of_decl_compilation (decl, true, 0);
3177 else
3179 /* In conjunction with an ASMSPEC, the `register'
3180 keyword indicates that we should place the variable
3181 in a particular register. */
3182 if (asmspec && C_DECL_REGISTER (decl))
3184 DECL_HARD_REGISTER (decl) = 1;
3185 /* This cannot be done for a structure with volatile
3186 fields, on which DECL_REGISTER will have been
3187 reset. */
3188 if (!DECL_REGISTER (decl))
3189 error ("cannot put object with volatile field into register");
3192 if (TREE_CODE (decl) != FUNCTION_DECL)
3194 /* If we're building a variable sized type, and we might be
3195 reachable other than via the top of the current binding
3196 level, then create a new BIND_EXPR so that we deallocate
3197 the object at the right time. */
3198 /* Note that DECL_SIZE can be null due to errors. */
3199 if (DECL_SIZE (decl)
3200 && !TREE_CONSTANT (DECL_SIZE (decl))
3201 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3203 tree bind;
3204 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3205 TREE_SIDE_EFFECTS (bind) = 1;
3206 add_stmt (bind);
3207 BIND_EXPR_BODY (bind) = push_stmt_list ();
3209 add_stmt (build_stmt (DECL_EXPR, decl));
3214 if (!DECL_FILE_SCOPE_P (decl))
3216 /* Recompute the RTL of a local array now
3217 if it used to be an incomplete type. */
3218 if (was_incomplete
3219 && ! TREE_STATIC (decl) && ! DECL_EXTERNAL (decl))
3221 /* If we used it already as memory, it must stay in memory. */
3222 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3223 /* If it's still incomplete now, no init will save it. */
3224 if (DECL_SIZE (decl) == 0)
3225 DECL_INITIAL (decl) = 0;
3230 /* If this was marked 'used', be sure it will be output. */
3231 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3232 mark_decl_referenced (decl);
3234 if (TREE_CODE (decl) == TYPE_DECL)
3236 if (!DECL_FILE_SCOPE_P (decl)
3237 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3238 add_stmt (build_stmt (DECL_EXPR, decl));
3240 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3243 /* At the end of a declaration, throw away any variable type sizes
3244 of types defined inside that declaration. There is no use
3245 computing them in the following function definition. */
3246 if (current_scope == file_scope)
3247 get_pending_sizes ();
3249 /* Install a cleanup (aka destructor) if one was given. */
3250 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3252 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3253 if (attr)
3255 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3256 tree cleanup_decl = lookup_name (cleanup_id);
3257 tree cleanup;
3259 /* Build "cleanup(&decl)" for the destructor. */
3260 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3261 cleanup = build_tree_list (NULL_TREE, cleanup);
3262 cleanup = build_function_call (cleanup_decl, cleanup);
3264 /* Don't warn about decl unused; the cleanup uses it. */
3265 TREE_USED (decl) = 1;
3266 TREE_USED (cleanup_decl) = 1;
3268 /* Initialize EH, if we've been told to do so. */
3269 if (flag_exceptions && !c_eh_initialized_p)
3271 c_eh_initialized_p = true;
3272 eh_personality_libfunc
3273 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3274 ? "__gcc_personality_sj0"
3275 : "__gcc_personality_v0");
3276 using_eh_for_cleanups ();
3279 push_cleanup (decl, cleanup, false);
3284 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3286 tree
3287 grokparm (tree parm)
3289 tree decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3290 TREE_PURPOSE (TREE_PURPOSE (parm)),
3291 PARM, false, NULL);
3293 decl_attributes (&decl, TREE_VALUE (parm), 0);
3295 return decl;
3298 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3299 and push that on the current scope. */
3301 void
3302 push_parm_decl (tree parm)
3304 tree decl;
3306 decl = grokdeclarator (TREE_VALUE (TREE_PURPOSE (parm)),
3307 TREE_PURPOSE (TREE_PURPOSE (parm)),
3308 PARM, false, NULL);
3309 decl_attributes (&decl, TREE_VALUE (parm), 0);
3311 decl = pushdecl (decl);
3313 finish_decl (decl, NULL_TREE, NULL_TREE);
3316 /* Mark all the parameter declarations to date as forward decls.
3317 Also diagnose use of this extension. */
3319 void
3320 mark_forward_parm_decls (void)
3322 struct c_binding *b;
3324 if (pedantic && !current_scope->warned_forward_parm_decls)
3326 pedwarn ("ISO C forbids forward parameter declarations");
3327 current_scope->warned_forward_parm_decls = true;
3330 for (b = current_scope->bindings; b; b = b->prev)
3331 if (TREE_CODE (b->decl) == PARM_DECL)
3332 TREE_ASM_WRITTEN (b->decl) = 1;
3335 static GTY(()) int compound_literal_number;
3337 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3338 literal, which may be an incomplete array type completed by the
3339 initializer; INIT is a CONSTRUCTOR that initializes the compound
3340 literal. */
3342 tree
3343 build_compound_literal (tree type, tree init)
3345 /* We do not use start_decl here because we have a type, not a declarator;
3346 and do not use finish_decl because the decl should be stored inside
3347 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3348 tree decl = build_decl (VAR_DECL, NULL_TREE, type);
3349 tree complit;
3350 tree stmt;
3351 DECL_EXTERNAL (decl) = 0;
3352 TREE_PUBLIC (decl) = 0;
3353 TREE_STATIC (decl) = (current_scope == file_scope);
3354 DECL_CONTEXT (decl) = current_function_decl;
3355 TREE_USED (decl) = 1;
3356 TREE_TYPE (decl) = type;
3357 TREE_READONLY (decl) = TYPE_READONLY (type);
3358 store_init_value (decl, init);
3360 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3362 int failure = complete_array_type (type, DECL_INITIAL (decl), 1);
3363 if (failure)
3364 abort ();
3367 type = TREE_TYPE (decl);
3368 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3369 return error_mark_node;
3371 stmt = build_stmt (DECL_EXPR, decl);
3372 complit = build1 (COMPOUND_LITERAL_EXPR, TREE_TYPE (decl), stmt);
3373 TREE_SIDE_EFFECTS (complit) = 1;
3375 layout_decl (decl, 0);
3377 if (TREE_STATIC (decl))
3379 /* This decl needs a name for the assembler output. We also need
3380 a unique suffix to be added to the name. */
3381 char *name;
3383 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3384 compound_literal_number);
3385 compound_literal_number++;
3386 DECL_NAME (decl) = get_identifier (name);
3387 DECL_DEFER_OUTPUT (decl) = 1;
3388 DECL_COMDAT (decl) = 1;
3389 DECL_ARTIFICIAL (decl) = 1;
3390 pushdecl (decl);
3391 rest_of_decl_compilation (decl, 1, 0);
3394 return complit;
3397 /* Make TYPE a complete type based on INITIAL_VALUE.
3398 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
3399 2 if there was no information (in which case assume 1 if DO_DEFAULT). */
3402 complete_array_type (tree type, tree initial_value, int do_default)
3404 tree maxindex = NULL_TREE;
3405 int value = 0;
3407 if (initial_value)
3409 /* Note MAXINDEX is really the maximum index,
3410 one less than the size. */
3411 if (TREE_CODE (initial_value) == STRING_CST)
3413 int eltsize
3414 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
3415 maxindex = build_int_cst (NULL_TREE,
3416 (TREE_STRING_LENGTH (initial_value)
3417 / eltsize) - 1);
3419 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
3421 tree elts = CONSTRUCTOR_ELTS (initial_value);
3422 maxindex = build_int_cst (NULL_TREE, -1);
3423 for (; elts; elts = TREE_CHAIN (elts))
3425 if (TREE_PURPOSE (elts))
3426 maxindex = TREE_PURPOSE (elts);
3427 else
3428 maxindex = fold (build2 (PLUS_EXPR, integer_type_node,
3429 maxindex, integer_one_node));
3432 else
3434 /* Make an error message unless that happened already. */
3435 if (initial_value != error_mark_node)
3436 value = 1;
3438 /* Prevent further error messages. */
3439 maxindex = build_int_cst (NULL_TREE, 0);
3443 if (!maxindex)
3445 if (do_default)
3446 maxindex = build_int_cst (NULL_TREE, 0);
3447 value = 2;
3450 if (maxindex)
3452 TYPE_DOMAIN (type) = build_index_type (maxindex);
3453 if (!TREE_TYPE (maxindex))
3454 abort ();
3457 /* Lay out the type now that we can get the real answer. */
3459 layout_type (type);
3461 return value;
3464 /* Determine whether TYPE is a structure with a flexible array member,
3465 or a union containing such a structure (possibly recursively). */
3467 static bool
3468 flexible_array_type_p (tree type)
3470 tree x;
3471 switch (TREE_CODE (type))
3473 case RECORD_TYPE:
3474 x = TYPE_FIELDS (type);
3475 if (x == NULL_TREE)
3476 return false;
3477 while (TREE_CHAIN (x) != NULL_TREE)
3478 x = TREE_CHAIN (x);
3479 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3480 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3481 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3482 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3483 return true;
3484 return false;
3485 case UNION_TYPE:
3486 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3488 if (flexible_array_type_p (TREE_TYPE (x)))
3489 return true;
3491 return false;
3492 default:
3493 return false;
3497 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3498 replacing with appropriate values if they are invalid. */
3499 static void
3500 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3502 tree type_mv;
3503 unsigned int max_width;
3504 unsigned HOST_WIDE_INT w;
3505 const char *name = orig_name ? orig_name: _("<anonymous>");
3507 /* Necessary? */
3508 STRIP_NOPS (*width);
3510 /* Detect and ignore out of range field width and process valid
3511 field widths. */
3512 if (TREE_CODE (*width) != INTEGER_CST)
3514 error ("bit-field `%s' width not an integer constant", name);
3515 *width = integer_one_node;
3517 else
3519 constant_expression_warning (*width);
3520 if (tree_int_cst_sgn (*width) < 0)
3522 error ("negative width in bit-field `%s'", name);
3523 *width = integer_one_node;
3525 else if (integer_zerop (*width) && orig_name)
3527 error ("zero width for bit-field `%s'", name);
3528 *width = integer_one_node;
3532 /* Detect invalid bit-field type. */
3533 if (TREE_CODE (*type) != INTEGER_TYPE
3534 && TREE_CODE (*type) != BOOLEAN_TYPE
3535 && TREE_CODE (*type) != ENUMERAL_TYPE)
3537 error ("bit-field `%s' has invalid type", name);
3538 *type = unsigned_type_node;
3541 type_mv = TYPE_MAIN_VARIANT (*type);
3542 if (pedantic
3543 && type_mv != integer_type_node
3544 && type_mv != unsigned_type_node
3545 && type_mv != boolean_type_node)
3546 pedwarn ("type of bit-field `%s' is a GCC extension", name);
3548 if (type_mv == boolean_type_node)
3549 max_width = CHAR_TYPE_SIZE;
3550 else
3551 max_width = TYPE_PRECISION (*type);
3553 if (0 < compare_tree_int (*width, max_width))
3555 error ("width of `%s' exceeds its type", name);
3556 w = max_width;
3557 *width = build_int_cst (NULL_TREE, w);
3559 else
3560 w = tree_low_cst (*width, 1);
3562 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3564 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3565 if (!lt
3566 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3567 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3568 warning ("`%s' is narrower than values of its type", name);
3572 /* Given declspecs and a declarator,
3573 determine the name and type of the object declared
3574 and construct a ..._DECL node for it.
3575 (In one case we can return a ..._TYPE node instead.
3576 For invalid input we sometimes return 0.)
3578 DECLSPECS is a chain of tree_list nodes whose value fields
3579 are the storage classes and type specifiers.
3581 DECL_CONTEXT says which syntactic context this declaration is in:
3582 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3583 FUNCDEF for a function definition. Like NORMAL but a few different
3584 error messages in each case. Return value may be zero meaning
3585 this definition is too screwy to try to parse.
3586 PARM for a parameter declaration (either within a function prototype
3587 or before a function body). Make a PARM_DECL, or return void_type_node.
3588 TYPENAME if for a typename (in a cast or sizeof).
3589 Don't make a DECL node; just return the ..._TYPE node.
3590 FIELD for a struct or union field; make a FIELD_DECL.
3591 INITIALIZED is true if the decl has an initializer.
3592 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3593 representing the width of the bit-field.
3595 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3596 It may also be so in the PARM case, for a prototype where the
3597 argument type is specified but not the name.
3599 This function is where the complicated C meanings of `static'
3600 and `extern' are interpreted. */
3602 static tree
3603 grokdeclarator (tree declarator, tree declspecs,
3604 enum decl_context decl_context, bool initialized, tree *width)
3606 int specbits = 0;
3607 tree spec;
3608 tree type = NULL_TREE;
3609 int longlong = 0;
3610 int constp;
3611 int restrictp;
3612 int volatilep;
3613 int type_quals = TYPE_UNQUALIFIED;
3614 int inlinep;
3615 int explicit_int = 0;
3616 int explicit_char = 0;
3617 int defaulted_int = 0;
3618 tree typedef_decl = 0;
3619 const char *name, *orig_name;
3620 tree typedef_type = 0;
3621 int funcdef_flag = 0;
3622 enum tree_code innermost_code = ERROR_MARK;
3623 int size_varies = 0;
3624 tree decl_attr = NULL_TREE;
3625 tree array_ptr_quals = NULL_TREE;
3626 int array_parm_static = 0;
3627 tree returned_attrs = NULL_TREE;
3628 bool bitfield = width != NULL;
3629 tree element_type;
3630 tree arg_info = NULL_TREE;
3632 if (decl_context == FUNCDEF)
3633 funcdef_flag = 1, decl_context = NORMAL;
3635 /* Look inside a declarator for the name being declared
3636 and get it as a string, for an error message. */
3638 tree decl = declarator;
3639 name = 0;
3641 while (decl)
3642 switch (TREE_CODE (decl))
3644 case ARRAY_REF:
3645 case INDIRECT_REF:
3646 case CALL_EXPR:
3647 innermost_code = TREE_CODE (decl);
3648 decl = TREE_OPERAND (decl, 0);
3649 break;
3651 case TREE_LIST:
3652 decl = TREE_VALUE (decl);
3653 break;
3655 case IDENTIFIER_NODE:
3656 name = IDENTIFIER_POINTER (decl);
3657 decl = 0;
3658 break;
3660 default:
3661 abort ();
3663 orig_name = name;
3664 if (name == 0)
3665 name = "type name";
3668 /* A function definition's declarator must have the form of
3669 a function declarator. */
3671 if (funcdef_flag && innermost_code != CALL_EXPR)
3672 return 0;
3674 /* If this looks like a function definition, make it one,
3675 even if it occurs where parms are expected.
3676 Then store_parm_decls will reject it and not use it as a parm. */
3677 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3678 decl_context = PARM;
3680 /* Look through the decl specs and record which ones appear.
3681 Some typespecs are defined as built-in typenames.
3682 Others, the ones that are modifiers of other types,
3683 are represented by bits in SPECBITS: set the bits for
3684 the modifiers that appear. Storage class keywords are also in SPECBITS.
3686 If there is a typedef name or a type, store the type in TYPE.
3687 This includes builtin typedefs such as `int'.
3689 Set EXPLICIT_INT or EXPLICIT_CHAR if the type is `int' or `char'
3690 and did not come from a user typedef.
3692 Set LONGLONG if `long' is mentioned twice. */
3694 for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
3696 tree id = TREE_VALUE (spec);
3698 /* If the entire declaration is itself tagged as deprecated then
3699 suppress reports of deprecated items. */
3700 if (id && TREE_DEPRECATED (id))
3702 if (deprecated_state != DEPRECATED_SUPPRESS)
3703 warn_deprecated_use (id);
3706 if (id == ridpointers[(int) RID_INT])
3707 explicit_int = 1;
3708 if (id == ridpointers[(int) RID_CHAR])
3709 explicit_char = 1;
3711 if (TREE_CODE (id) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (id))
3713 enum rid i = C_RID_CODE (id);
3714 if ((int) i <= (int) RID_LAST_MODIFIER)
3716 if (i == RID_LONG && (specbits & (1 << (int) RID_LONG)))
3718 if (longlong)
3719 error ("`long long long' is too long for GCC");
3720 else
3722 if (pedantic && !flag_isoc99 && ! in_system_header
3723 && warn_long_long)
3724 pedwarn ("ISO C90 does not support `long long'");
3725 longlong = 1;
3728 else if (specbits & (1 << (int) i))
3730 if (i == RID_CONST || i == RID_VOLATILE || i == RID_RESTRICT)
3732 if (pedantic && !flag_isoc99)
3733 pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
3735 else
3736 error ("duplicate `%s'", IDENTIFIER_POINTER (id));
3739 /* Diagnose "__thread extern". Recall that this list
3740 is in the reverse order seen in the text. */
3741 if (i == RID_THREAD
3742 && (specbits & (1 << (int) RID_EXTERN
3743 | 1 << (int) RID_STATIC)))
3745 if (specbits & 1 << (int) RID_EXTERN)
3746 error ("`__thread' before `extern'");
3747 else
3748 error ("`__thread' before `static'");
3751 specbits |= 1 << (int) i;
3752 goto found;
3755 if (type)
3756 error ("two or more data types in declaration of `%s'", name);
3757 /* Actual typedefs come to us as TYPE_DECL nodes. */
3758 else if (TREE_CODE (id) == TYPE_DECL)
3760 if (TREE_TYPE (id) == error_mark_node)
3761 ; /* Allow the type to default to int to avoid cascading errors. */
3762 else
3764 type = TREE_TYPE (id);
3765 decl_attr = DECL_ATTRIBUTES (id);
3766 typedef_decl = id;
3769 /* Built-in types come as identifiers. */
3770 else if (TREE_CODE (id) == IDENTIFIER_NODE)
3772 tree t = lookup_name (id);
3773 if (!t || TREE_CODE (t) != TYPE_DECL)
3774 error ("`%s' fails to be a typedef or built in type",
3775 IDENTIFIER_POINTER (id));
3776 else if (TREE_TYPE (t) == error_mark_node)
3778 else
3780 type = TREE_TYPE (t);
3781 typedef_decl = t;
3784 else if (TREE_CODE (id) != ERROR_MARK)
3785 type = id;
3787 found:
3791 typedef_type = type;
3792 if (type)
3793 size_varies = C_TYPE_VARIABLE_SIZE (type);
3795 /* No type at all: default to `int', and set DEFAULTED_INT
3796 because it was not a user-defined typedef. */
3798 if (type == 0)
3800 if ((! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3801 | (1 << (int) RID_SIGNED)
3802 | (1 << (int) RID_UNSIGNED)
3803 | (1 << (int) RID_COMPLEX))))
3804 /* Don't warn about typedef foo = bar. */
3805 && ! (specbits & (1 << (int) RID_TYPEDEF) && initialized)
3806 && ! in_system_header)
3808 /* Issue a warning if this is an ISO C 99 program or if -Wreturn-type
3809 and this is a function, or if -Wimplicit; prefer the former
3810 warning since it is more explicit. */
3811 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3812 && funcdef_flag)
3813 warn_about_return_type = 1;
3814 else if (warn_implicit_int || flag_isoc99)
3815 pedwarn_c99 ("type defaults to `int' in declaration of `%s'",
3816 name);
3819 defaulted_int = 1;
3820 type = integer_type_node;
3823 /* Now process the modifiers that were specified
3824 and check for invalid combinations. */
3826 /* Long double is a special combination. */
3828 if ((specbits & 1 << (int) RID_LONG) && ! longlong
3829 && TYPE_MAIN_VARIANT (type) == double_type_node)
3831 specbits &= ~(1 << (int) RID_LONG);
3832 type = long_double_type_node;
3835 /* Check all other uses of type modifiers. */
3837 if (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3838 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED)))
3840 int ok = 0;
3842 if ((specbits & 1 << (int) RID_LONG)
3843 && (specbits & 1 << (int) RID_SHORT))
3844 error ("both long and short specified for `%s'", name);
3845 else if (((specbits & 1 << (int) RID_LONG)
3846 || (specbits & 1 << (int) RID_SHORT))
3847 && explicit_char)
3848 error ("long or short specified with char for `%s'", name);
3849 else if (((specbits & 1 << (int) RID_LONG)
3850 || (specbits & 1 << (int) RID_SHORT))
3851 && TREE_CODE (type) == REAL_TYPE)
3853 static int already = 0;
3855 error ("long or short specified with floating type for `%s'", name);
3856 if (! already && ! pedantic)
3858 error ("the only valid combination is `long double'");
3859 already = 1;
3862 else if ((specbits & 1 << (int) RID_SIGNED)
3863 && (specbits & 1 << (int) RID_UNSIGNED))
3864 error ("both signed and unsigned specified for `%s'", name);
3865 else if (TREE_CODE (type) != INTEGER_TYPE)
3866 error ("long, short, signed or unsigned invalid for `%s'", name);
3867 else
3869 ok = 1;
3870 if (!explicit_int && !defaulted_int && !explicit_char)
3872 error ("long, short, signed or unsigned used invalidly for `%s'",
3873 name);
3874 ok = 0;
3878 /* Discard the type modifiers if they are invalid. */
3879 if (! ok)
3881 specbits &= ~((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3882 | (1 << (int) RID_UNSIGNED) | (1 << (int) RID_SIGNED));
3883 longlong = 0;
3887 if ((specbits & (1 << (int) RID_COMPLEX))
3888 && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
3890 error ("complex invalid for `%s'", name);
3891 specbits &= ~(1 << (int) RID_COMPLEX);
3894 /* Decide whether an integer type is signed or not.
3895 Optionally treat bit-fields as signed by default. */
3896 if (specbits & 1 << (int) RID_UNSIGNED
3897 || (bitfield && ! flag_signed_bitfields
3898 && (explicit_int || defaulted_int || explicit_char
3899 /* A typedef for plain `int' without `signed'
3900 can be controlled just like plain `int'. */
3901 || ! (typedef_decl != 0
3902 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
3903 && TREE_CODE (type) != ENUMERAL_TYPE
3904 && !(specbits & 1 << (int) RID_SIGNED)))
3906 if (longlong)
3907 type = long_long_unsigned_type_node;
3908 else if (specbits & 1 << (int) RID_LONG)
3909 type = long_unsigned_type_node;
3910 else if (specbits & 1 << (int) RID_SHORT)
3911 type = short_unsigned_type_node;
3912 else if (type == char_type_node)
3913 type = unsigned_char_type_node;
3914 else if (typedef_decl)
3915 type = c_common_unsigned_type (type);
3916 else
3917 type = unsigned_type_node;
3919 else if ((specbits & 1 << (int) RID_SIGNED)
3920 && type == char_type_node)
3921 type = signed_char_type_node;
3922 else if (longlong)
3923 type = long_long_integer_type_node;
3924 else if (specbits & 1 << (int) RID_LONG)
3925 type = long_integer_type_node;
3926 else if (specbits & 1 << (int) RID_SHORT)
3927 type = short_integer_type_node;
3929 if (specbits & 1 << (int) RID_COMPLEX)
3931 if (pedantic && !flag_isoc99)
3932 pedwarn ("ISO C90 does not support complex types");
3933 /* If we just have "complex", it is equivalent to
3934 "complex double", but if any modifiers at all are specified it is
3935 the complex form of TYPE. E.g, "complex short" is
3936 "complex short int". */
3938 if (defaulted_int && ! longlong
3939 && ! (specbits & ((1 << (int) RID_LONG) | (1 << (int) RID_SHORT)
3940 | (1 << (int) RID_SIGNED)
3941 | (1 << (int) RID_UNSIGNED))))
3943 if (pedantic)
3944 pedwarn ("ISO C does not support plain `complex' meaning `double complex'");
3945 type = complex_double_type_node;
3947 else if (type == integer_type_node)
3949 if (pedantic)
3950 pedwarn ("ISO C does not support complex integer types");
3951 type = complex_integer_type_node;
3953 else if (type == float_type_node)
3954 type = complex_float_type_node;
3955 else if (type == double_type_node)
3956 type = complex_double_type_node;
3957 else if (type == long_double_type_node)
3958 type = complex_long_double_type_node;
3959 else
3961 if (pedantic)
3962 pedwarn ("ISO C does not support complex integer types");
3963 type = build_complex_type (type);
3967 /* Check the type and width of a bit-field. */
3968 if (bitfield)
3969 check_bitfield_type_and_width (&type, width, orig_name);
3971 /* Figure out the type qualifiers for the declaration. There are
3972 two ways a declaration can become qualified. One is something
3973 like `const int i' where the `const' is explicit. Another is
3974 something like `typedef const int CI; CI i' where the type of the
3975 declaration contains the `const'. A third possibility is that
3976 there is a type qualifier on the element type of a typedefed
3977 array type, in which case we should extract that qualifier so
3978 that c_apply_type_quals_to_decls receives the full list of
3979 qualifiers to work with (C90 is not entirely clear about whether
3980 duplicate qualifiers should be diagnosed in this case, but it
3981 seems most appropriate to do so). */
3982 element_type = strip_array_types (type);
3983 constp = !! (specbits & 1 << (int) RID_CONST) + TYPE_READONLY (element_type);
3984 restrictp
3985 = !! (specbits & 1 << (int) RID_RESTRICT) + TYPE_RESTRICT (element_type);
3986 volatilep
3987 = !! (specbits & 1 << (int) RID_VOLATILE) + TYPE_VOLATILE (element_type);
3988 inlinep = !! (specbits & (1 << (int) RID_INLINE));
3989 if (pedantic && !flag_isoc99)
3991 if (constp > 1)
3992 pedwarn ("duplicate `const'");
3993 if (restrictp > 1)
3994 pedwarn ("duplicate `restrict'");
3995 if (volatilep > 1)
3996 pedwarn ("duplicate `volatile'");
3998 if (! flag_gen_aux_info && (TYPE_QUALS (type)))
3999 type = TYPE_MAIN_VARIANT (type);
4000 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4001 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4002 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4004 /* Warn if two storage classes are given. Default to `auto'. */
4007 int nclasses = 0;
4009 if (specbits & 1 << (int) RID_AUTO) nclasses++;
4010 if (specbits & 1 << (int) RID_STATIC) nclasses++;
4011 if (specbits & 1 << (int) RID_EXTERN) nclasses++;
4012 if (specbits & 1 << (int) RID_REGISTER) nclasses++;
4013 if (specbits & 1 << (int) RID_TYPEDEF) nclasses++;
4015 /* "static __thread" and "extern __thread" are allowed. */
4016 if ((specbits & (1 << (int) RID_THREAD
4017 | 1 << (int) RID_STATIC
4018 | 1 << (int) RID_EXTERN)) == (1 << (int) RID_THREAD))
4019 nclasses++;
4021 /* Warn about storage classes that are invalid for certain
4022 kinds of declarations (parameters, typenames, etc.). */
4024 if (nclasses > 1)
4025 error ("multiple storage classes in declaration of `%s'", name);
4026 else if (funcdef_flag
4027 && (specbits
4028 & ((1 << (int) RID_REGISTER)
4029 | (1 << (int) RID_AUTO)
4030 | (1 << (int) RID_TYPEDEF)
4031 | (1 << (int) RID_THREAD))))
4033 if (specbits & 1 << (int) RID_AUTO
4034 && (pedantic || current_scope == file_scope))
4035 pedwarn ("function definition declared `auto'");
4036 if (specbits & 1 << (int) RID_REGISTER)
4037 error ("function definition declared `register'");
4038 if (specbits & 1 << (int) RID_TYPEDEF)
4039 error ("function definition declared `typedef'");
4040 if (specbits & 1 << (int) RID_THREAD)
4041 error ("function definition declared `__thread'");
4042 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4043 | (1 << (int) RID_AUTO) | (1 << (int) RID_THREAD));
4045 else if (decl_context != NORMAL && nclasses > 0)
4047 if (decl_context == PARM && specbits & 1 << (int) RID_REGISTER)
4049 else
4051 switch (decl_context)
4053 case FIELD:
4054 error ("storage class specified for structure field `%s'",
4055 name);
4056 break;
4057 case PARM:
4058 error ("storage class specified for parameter `%s'", name);
4059 break;
4060 default:
4061 error ("storage class specified for typename");
4062 break;
4064 specbits &= ~((1 << (int) RID_TYPEDEF) | (1 << (int) RID_REGISTER)
4065 | (1 << (int) RID_AUTO) | (1 << (int) RID_STATIC)
4066 | (1 << (int) RID_EXTERN) | (1 << (int) RID_THREAD));
4069 else if (specbits & 1 << (int) RID_EXTERN && initialized && ! funcdef_flag)
4071 /* `extern' with initialization is invalid if not at file scope. */
4072 if (current_scope == file_scope)
4073 warning ("`%s' initialized and declared `extern'", name);
4074 else
4075 error ("`%s' has both `extern' and initializer", name);
4077 else if (current_scope == file_scope)
4079 if (specbits & 1 << (int) RID_AUTO)
4080 error ("file-scope declaration of `%s' specifies `auto'", name);
4082 else
4084 if (specbits & 1 << (int) RID_EXTERN && funcdef_flag)
4085 error ("nested function `%s' declared `extern'", name);
4086 else if ((specbits & (1 << (int) RID_THREAD
4087 | 1 << (int) RID_EXTERN
4088 | 1 << (int) RID_STATIC))
4089 == (1 << (int) RID_THREAD))
4091 error ("function-scope `%s' implicitly auto and declared `__thread'",
4092 name);
4093 specbits &= ~(1 << (int) RID_THREAD);
4098 /* Now figure out the structure of the declarator proper.
4099 Descend through it, creating more complex types, until we reach
4100 the declared identifier (or NULL_TREE, in an absolute declarator). */
4102 while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE)
4104 if (type == error_mark_node)
4106 declarator = TREE_OPERAND (declarator, 0);
4107 continue;
4110 /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
4111 an INDIRECT_REF (for *...),
4112 a CALL_EXPR (for ...(...)),
4113 a TREE_LIST (for nested attributes),
4114 an identifier (for the name being declared)
4115 or a null pointer (for the place in an absolute declarator
4116 where the name was omitted).
4117 For the last two cases, we have just exited the loop.
4119 At this point, TYPE is the type of elements of an array,
4120 or for a function to return, or for a pointer to point to.
4121 After this sequence of ifs, TYPE is the type of the
4122 array or function or pointer, and DECLARATOR has had its
4123 outermost layer removed. */
4125 if (array_ptr_quals != NULL_TREE || array_parm_static)
4127 /* Only the innermost declarator (making a parameter be of
4128 array type which is converted to pointer type)
4129 may have static or type qualifiers. */
4130 error ("static or type qualifiers in non-parameter array declarator");
4131 array_ptr_quals = NULL_TREE;
4132 array_parm_static = 0;
4135 if (TREE_CODE (declarator) == TREE_LIST)
4137 /* We encode a declarator with embedded attributes using
4138 a TREE_LIST. */
4139 tree attrs = TREE_PURPOSE (declarator);
4140 tree inner_decl;
4141 int attr_flags = 0;
4142 declarator = TREE_VALUE (declarator);
4143 inner_decl = declarator;
4144 while (inner_decl != NULL_TREE
4145 && TREE_CODE (inner_decl) == TREE_LIST)
4146 inner_decl = TREE_VALUE (inner_decl);
4147 if (inner_decl == NULL_TREE
4148 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
4149 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4150 else if (TREE_CODE (inner_decl) == CALL_EXPR)
4151 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4152 else if (TREE_CODE (inner_decl) == ARRAY_REF)
4153 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4154 returned_attrs = decl_attributes (&type,
4155 chainon (returned_attrs, attrs),
4156 attr_flags);
4158 else if (TREE_CODE (declarator) == ARRAY_REF)
4160 tree itype = NULL_TREE;
4161 tree size = TREE_OPERAND (declarator, 1);
4162 /* The index is a signed object `sizetype' bits wide. */
4163 tree index_type = c_common_signed_type (sizetype);
4165 array_ptr_quals = TREE_TYPE (declarator);
4166 array_parm_static = TREE_STATIC (declarator);
4168 declarator = TREE_OPERAND (declarator, 0);
4170 /* Check for some types that there cannot be arrays of. */
4172 if (VOID_TYPE_P (type))
4174 error ("declaration of `%s' as array of voids", name);
4175 type = error_mark_node;
4178 if (TREE_CODE (type) == FUNCTION_TYPE)
4180 error ("declaration of `%s' as array of functions", name);
4181 type = error_mark_node;
4184 if (pedantic && !in_system_header && flexible_array_type_p (type))
4185 pedwarn ("invalid use of structure with flexible array member");
4187 if (size == error_mark_node)
4188 type = error_mark_node;
4190 if (type == error_mark_node)
4191 continue;
4193 /* If size was specified, set ITYPE to a range-type for that size.
4194 Otherwise, ITYPE remains null. finish_decl may figure it out
4195 from an initial value. */
4197 if (size)
4199 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
4200 STRIP_TYPE_NOPS (size);
4202 if (! INTEGRAL_TYPE_P (TREE_TYPE (size)))
4204 error ("size of array `%s' has non-integer type", name);
4205 size = integer_one_node;
4208 if (pedantic && integer_zerop (size))
4209 pedwarn ("ISO C forbids zero-size array `%s'", name);
4211 if (TREE_CODE (size) == INTEGER_CST)
4213 constant_expression_warning (size);
4214 if (tree_int_cst_sgn (size) < 0)
4216 error ("size of array `%s' is negative", name);
4217 size = integer_one_node;
4220 else
4222 /* Make sure the array size remains visibly nonconstant
4223 even if it is (eg) a const variable with known value. */
4224 size_varies = 1;
4226 if (!flag_isoc99 && pedantic)
4228 if (TREE_CONSTANT (size))
4229 pedwarn ("ISO C90 forbids array `%s' whose size can't be evaluated",
4230 name);
4231 else
4232 pedwarn ("ISO C90 forbids variable-size array `%s'",
4233 name);
4237 if (integer_zerop (size))
4239 /* A zero-length array cannot be represented with an
4240 unsigned index type, which is what we'll get with
4241 build_index_type. Create an open-ended range instead. */
4242 itype = build_range_type (sizetype, size, NULL_TREE);
4244 else
4246 /* Compute the maximum valid index, that is, size - 1.
4247 Do the calculation in index_type, so that if it is
4248 a variable the computations will be done in the
4249 proper mode. */
4250 itype = fold (build2 (MINUS_EXPR, index_type,
4251 convert (index_type, size),
4252 convert (index_type, size_one_node)));
4254 /* If that overflowed, the array is too big.
4255 ??? While a size of INT_MAX+1 technically shouldn't
4256 cause an overflow (because we subtract 1), the overflow
4257 is recorded during the conversion to index_type, before
4258 the subtraction. Handling this case seems like an
4259 unnecessary complication. */
4260 if (TREE_OVERFLOW (itype))
4262 error ("size of array `%s' is too large", name);
4263 type = error_mark_node;
4264 continue;
4267 if (size_varies)
4268 itype = variable_size (itype);
4269 itype = build_index_type (itype);
4272 else if (decl_context == FIELD)
4274 if (pedantic && !flag_isoc99 && !in_system_header)
4275 pedwarn ("ISO C90 does not support flexible array members");
4277 /* ISO C99 Flexible array members are effectively identical
4278 to GCC's zero-length array extension. */
4279 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4282 /* If pedantic, complain about arrays of incomplete types. */
4284 if (pedantic && !COMPLETE_TYPE_P (type))
4285 pedwarn ("array type has incomplete element type");
4287 /* Build the array type itself, then merge any constancy or
4288 volatility into the target type. We must do it in this order
4289 to ensure that the TYPE_MAIN_VARIANT field of the array type
4290 is set correctly. */
4292 type = build_array_type (type, itype);
4293 if (type_quals)
4294 type = c_build_qualified_type (type, type_quals);
4296 if (size_varies)
4297 C_TYPE_VARIABLE_SIZE (type) = 1;
4299 /* The GCC extension for zero-length arrays differs from
4300 ISO flexible array members in that sizeof yields zero. */
4301 if (size && integer_zerop (size))
4303 layout_type (type);
4304 TYPE_SIZE (type) = bitsize_zero_node;
4305 TYPE_SIZE_UNIT (type) = size_zero_node;
4307 else if (declarator && TREE_CODE (declarator) == INDIRECT_REF)
4308 /* We can never complete an array type which is the target of a
4309 pointer, so go ahead and lay it out. */
4310 layout_type (type);
4312 if (decl_context != PARM
4313 && (array_ptr_quals != NULL_TREE || array_parm_static))
4315 error ("static or type qualifiers in non-parameter array declarator");
4316 array_ptr_quals = NULL_TREE;
4317 array_parm_static = 0;
4320 else if (TREE_CODE (declarator) == CALL_EXPR)
4322 /* Say it's a definition only for the declarator closest to
4323 the identifier, apart possibly from some attributes. */
4324 bool really_funcdef = false;
4325 tree arg_types;
4326 if (funcdef_flag)
4328 tree t = TREE_OPERAND (declarator, 0);
4329 while (TREE_CODE (t) == TREE_LIST)
4330 t = TREE_VALUE (t);
4331 really_funcdef = (TREE_CODE (t) == IDENTIFIER_NODE);
4334 /* Declaring a function type.
4335 Make sure we have a valid type for the function to return. */
4336 if (type == error_mark_node)
4337 continue;
4339 size_varies = 0;
4341 /* Warn about some types functions can't return. */
4343 if (TREE_CODE (type) == FUNCTION_TYPE)
4345 error ("`%s' declared as function returning a function", name);
4346 type = integer_type_node;
4348 if (TREE_CODE (type) == ARRAY_TYPE)
4350 error ("`%s' declared as function returning an array", name);
4351 type = integer_type_node;
4354 /* Construct the function type and go to the next
4355 inner layer of declarator. */
4356 arg_info = TREE_OPERAND (declarator, 1);
4357 arg_types = grokparms (arg_info, really_funcdef);
4359 /* Type qualifiers before the return type of the function
4360 qualify the return type, not the function type. */
4361 if (type_quals)
4363 /* Type qualifiers on a function return type are
4364 normally permitted by the standard but have no
4365 effect, so give a warning at -Wreturn-type.
4366 Qualifiers on a void return type are banned on
4367 function definitions in ISO C; GCC used to used them
4368 for noreturn functions. */
4369 if (VOID_TYPE_P (type) && really_funcdef)
4370 pedwarn ("function definition has qualified void return type");
4371 else if (warn_return_type)
4372 warning ("type qualifiers ignored on function return type");
4374 type = c_build_qualified_type (type, type_quals);
4376 type_quals = TYPE_UNQUALIFIED;
4378 type = build_function_type (type, arg_types);
4379 declarator = TREE_OPERAND (declarator, 0);
4381 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4382 the formal parameter list of this FUNCTION_TYPE to point to
4383 the FUNCTION_TYPE node itself. */
4386 tree link;
4388 for (link = ARG_INFO_TAGS (arg_info);
4389 link;
4390 link = TREE_CHAIN (link))
4391 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4394 else if (TREE_CODE (declarator) == INDIRECT_REF)
4396 /* Merge any constancy or volatility into the target type
4397 for the pointer. */
4399 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4400 && type_quals)
4401 pedwarn ("ISO C forbids qualified function types");
4402 if (type_quals)
4403 type = c_build_qualified_type (type, type_quals);
4404 type_quals = TYPE_UNQUALIFIED;
4405 size_varies = 0;
4407 type = build_pointer_type (type);
4409 /* Process a list of type modifier keywords
4410 (such as const or volatile) that were given inside the `*'. */
4412 if (TREE_TYPE (declarator))
4414 tree typemodlist;
4415 int erred = 0;
4417 constp = 0;
4418 volatilep = 0;
4419 restrictp = 0;
4420 for (typemodlist = TREE_TYPE (declarator); typemodlist;
4421 typemodlist = TREE_CHAIN (typemodlist))
4423 tree qualifier = TREE_VALUE (typemodlist);
4425 if (C_IS_RESERVED_WORD (qualifier))
4427 if (C_RID_CODE (qualifier) == RID_CONST)
4428 constp++;
4429 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4430 volatilep++;
4431 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4432 restrictp++;
4433 else
4434 erred++;
4436 else
4437 erred++;
4440 if (erred)
4441 error ("invalid type modifier within pointer declarator");
4442 if (pedantic && !flag_isoc99)
4444 if (constp > 1)
4445 pedwarn ("duplicate `const'");
4446 if (volatilep > 1)
4447 pedwarn ("duplicate `volatile'");
4448 if (restrictp > 1)
4449 pedwarn ("duplicate `restrict'");
4452 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4453 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4454 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4457 declarator = TREE_OPERAND (declarator, 0);
4459 else
4460 abort ();
4464 /* Now TYPE has the actual type. */
4466 /* Did array size calculations overflow? */
4468 if (TREE_CODE (type) == ARRAY_TYPE
4469 && COMPLETE_TYPE_P (type)
4470 && TREE_OVERFLOW (TYPE_SIZE (type)))
4472 error ("size of array `%s' is too large", name);
4473 /* If we proceed with the array type as it is, we'll eventually
4474 crash in tree_low_cst(). */
4475 type = error_mark_node;
4478 /* If this is declaring a typedef name, return a TYPE_DECL. */
4480 if (specbits & (1 << (int) RID_TYPEDEF))
4482 tree decl;
4483 /* Note that the grammar rejects storage classes
4484 in typenames, fields or parameters */
4485 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4486 && type_quals)
4487 pedwarn ("ISO C forbids qualified function types");
4488 if (type_quals)
4489 type = c_build_qualified_type (type, type_quals);
4490 decl = build_decl (TYPE_DECL, declarator, type);
4491 if ((specbits & (1 << (int) RID_SIGNED))
4492 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
4493 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4494 decl_attributes (&decl, returned_attrs, 0);
4495 return decl;
4498 /* Detect the case of an array type of unspecified size
4499 which came, as such, direct from a typedef name.
4500 We must copy the type, so that each identifier gets
4501 a distinct type, so that each identifier's size can be
4502 controlled separately by its own initializer. */
4504 if (type != 0 && typedef_type != 0
4505 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4506 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4508 type = build_array_type (TREE_TYPE (type), 0);
4509 if (size_varies)
4510 C_TYPE_VARIABLE_SIZE (type) = 1;
4513 /* If this is a type name (such as, in a cast or sizeof),
4514 compute the type and return it now. */
4516 if (decl_context == TYPENAME)
4518 /* Note that the grammar rejects storage classes
4519 in typenames, fields or parameters */
4520 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4521 && type_quals)
4522 pedwarn ("ISO C forbids const or volatile function types");
4523 if (type_quals)
4524 type = c_build_qualified_type (type, type_quals);
4525 decl_attributes (&type, returned_attrs, 0);
4526 return type;
4529 /* Aside from typedefs and type names (handle above),
4530 `void' at top level (not within pointer)
4531 is allowed only in public variables.
4532 We don't complain about parms either, but that is because
4533 a better error message can be made later. */
4535 if (VOID_TYPE_P (type) && decl_context != PARM
4536 && ! ((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4537 && ((specbits & (1 << (int) RID_EXTERN))
4538 || (current_scope == file_scope
4539 && !(specbits
4540 & ((1 << (int) RID_STATIC) | (1 << (int) RID_REGISTER)))))))
4542 error ("variable or field `%s' declared void", name);
4543 type = integer_type_node;
4546 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4547 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4550 tree decl;
4552 if (decl_context == PARM)
4554 tree type_as_written;
4555 tree promoted_type;
4557 /* A parameter declared as an array of T is really a pointer to T.
4558 One declared as a function is really a pointer to a function. */
4560 if (TREE_CODE (type) == ARRAY_TYPE)
4562 /* Transfer const-ness of array into that of type pointed to. */
4563 type = TREE_TYPE (type);
4564 if (type_quals)
4565 type = c_build_qualified_type (type, type_quals);
4566 type = build_pointer_type (type);
4567 type_quals = TYPE_UNQUALIFIED;
4568 if (array_ptr_quals)
4570 tree new_ptr_quals, new_ptr_attrs;
4571 int erred = 0;
4572 split_specs_attrs (array_ptr_quals, &new_ptr_quals, &new_ptr_attrs);
4573 /* We don't yet implement attributes in this context. */
4574 if (new_ptr_attrs != NULL_TREE)
4575 warning ("attributes in parameter array declarator ignored");
4577 constp = 0;
4578 volatilep = 0;
4579 restrictp = 0;
4580 for (; new_ptr_quals; new_ptr_quals = TREE_CHAIN (new_ptr_quals))
4582 tree qualifier = TREE_VALUE (new_ptr_quals);
4584 if (C_IS_RESERVED_WORD (qualifier))
4586 if (C_RID_CODE (qualifier) == RID_CONST)
4587 constp++;
4588 else if (C_RID_CODE (qualifier) == RID_VOLATILE)
4589 volatilep++;
4590 else if (C_RID_CODE (qualifier) == RID_RESTRICT)
4591 restrictp++;
4592 else
4593 erred++;
4595 else
4596 erred++;
4599 if (erred)
4600 error ("invalid type modifier within array declarator");
4602 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4603 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4604 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4606 size_varies = 0;
4608 else if (TREE_CODE (type) == FUNCTION_TYPE)
4610 if (pedantic && type_quals)
4611 pedwarn ("ISO C forbids qualified function types");
4612 if (type_quals)
4613 type = c_build_qualified_type (type, type_quals);
4614 type = build_pointer_type (type);
4615 type_quals = TYPE_UNQUALIFIED;
4617 else if (type_quals)
4618 type = c_build_qualified_type (type, type_quals);
4620 type_as_written = type;
4622 decl = build_decl (PARM_DECL, declarator, type);
4623 if (size_varies)
4624 C_DECL_VARIABLE_SIZE (decl) = 1;
4626 /* Compute the type actually passed in the parmlist,
4627 for the case where there is no prototype.
4628 (For example, shorts and chars are passed as ints.)
4629 When there is a prototype, this is overridden later. */
4631 if (type == error_mark_node)
4632 promoted_type = type;
4633 else
4634 promoted_type = c_type_promotes_to (type);
4636 DECL_ARG_TYPE (decl) = promoted_type;
4637 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4639 else if (decl_context == FIELD)
4641 /* Structure field. It may not be a function. */
4643 if (TREE_CODE (type) == FUNCTION_TYPE)
4645 error ("field `%s' declared as a function", name);
4646 type = build_pointer_type (type);
4648 else if (TREE_CODE (type) != ERROR_MARK
4649 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4651 error ("field `%s' has incomplete type", name);
4652 type = error_mark_node;
4654 /* Move type qualifiers down to element of an array. */
4655 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4656 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4657 type_quals),
4658 TYPE_DOMAIN (type));
4659 decl = build_decl (FIELD_DECL, declarator, type);
4660 DECL_NONADDRESSABLE_P (decl) = bitfield;
4662 if (size_varies)
4663 C_DECL_VARIABLE_SIZE (decl) = 1;
4665 else if (TREE_CODE (type) == FUNCTION_TYPE)
4667 if (specbits & (1 << (int) RID_REGISTER)
4668 || specbits & (1 << (int) RID_THREAD))
4669 error ("invalid storage class for function `%s'", name);
4670 else if (current_scope != file_scope)
4672 /* Function declaration not at file scope. Storage
4673 classes other than `extern' are not allowed, C99
4674 6.7.1p5, and `extern' makes no difference. However,
4675 GCC allows 'auto', perhaps with 'inline', to support
4676 nested functions. */
4677 if (specbits & (1 << (int) RID_AUTO))
4679 if (pedantic)
4680 pedwarn ("invalid storage class for function `%s'", name);
4682 if (specbits & (1 << (int) RID_STATIC))
4683 error ("invalid storage class for function `%s'", name);
4686 decl = build_decl (FUNCTION_DECL, declarator, type);
4687 decl = build_decl_attribute_variant (decl, decl_attr);
4689 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4691 if (pedantic && type_quals && ! DECL_IN_SYSTEM_HEADER (decl))
4692 pedwarn ("ISO C forbids qualified function types");
4694 /* GNU C interprets a volatile-qualified function type to indicate
4695 that the function does not return. */
4696 if ((type_quals & TYPE_QUAL_VOLATILE)
4697 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4698 warning ("`noreturn' function returns non-void value");
4700 /* Every function declaration is an external reference
4701 (DECL_EXTERNAL) except for those which are not at file
4702 scope and are explicitly declared "auto". This is
4703 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4704 GCC to signify a forward declaration of a nested function. */
4705 if ((specbits & (1 << RID_AUTO)) && current_scope != file_scope)
4706 DECL_EXTERNAL (decl) = 0;
4707 else
4708 DECL_EXTERNAL (decl) = 1;
4710 /* Record absence of global scope for `static' or `auto'. */
4711 TREE_PUBLIC (decl)
4712 = !(specbits & ((1 << (int) RID_STATIC) | (1 << (int) RID_AUTO)));
4714 /* For a function definition, record the argument information
4715 block where store_parm_decls will look for it. */
4716 if (funcdef_flag)
4717 current_function_arg_info = arg_info;
4719 if (defaulted_int)
4720 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4722 /* Record presence of `inline', if it is reasonable. */
4723 if (MAIN_NAME_P (declarator))
4725 if (inlinep)
4726 warning ("cannot inline function `main'");
4728 else if (inlinep)
4730 /* Record that the function is declared `inline'. */
4731 DECL_DECLARED_INLINE_P (decl) = 1;
4733 /* Do not mark bare declarations as DECL_INLINE. Doing so
4734 in the presence of multiple declarations can result in
4735 the abstract origin pointing between the declarations,
4736 which will confuse dwarf2out. */
4737 if (initialized)
4739 DECL_INLINE (decl) = 1;
4740 if (specbits & (1 << (int) RID_EXTERN))
4741 current_extern_inline = 1;
4744 /* If -finline-functions, assume it can be inlined. This does
4745 two things: let the function be deferred until it is actually
4746 needed, and let dwarf2 know that the function is inlinable. */
4747 else if (flag_inline_trees == 2 && initialized)
4748 DECL_INLINE (decl) = 1;
4750 else
4752 /* It's a variable. */
4753 /* An uninitialized decl with `extern' is a reference. */
4754 int extern_ref = !initialized && (specbits & (1 << (int) RID_EXTERN));
4756 /* Move type qualifiers down to element of an array. */
4757 if (TREE_CODE (type) == ARRAY_TYPE && type_quals)
4759 int saved_align = TYPE_ALIGN(type);
4760 type = build_array_type (c_build_qualified_type (TREE_TYPE (type),
4761 type_quals),
4762 TYPE_DOMAIN (type));
4763 TYPE_ALIGN (type) = saved_align;
4765 else if (type_quals)
4766 type = c_build_qualified_type (type, type_quals);
4768 /* C99 6.2.2p7: It is invalid (compile-time undefined
4769 behavior) to create an 'extern' declaration for a
4770 variable if there is a global declaration that is
4771 'static' and the global declaration is not visible.
4772 (If the static declaration _is_ currently visible,
4773 the 'extern' declaration is taken to refer to that decl.) */
4774 if (extern_ref && current_scope != file_scope)
4776 tree global_decl = identifier_global_value (declarator);
4777 tree visible_decl = lookup_name (declarator);
4779 if (global_decl
4780 && global_decl != visible_decl
4781 && TREE_CODE (global_decl) == VAR_DECL
4782 && !TREE_PUBLIC (global_decl))
4783 error ("variable previously declared 'static' redeclared "
4784 "'extern'");
4787 decl = build_decl (VAR_DECL, declarator, type);
4788 if (size_varies)
4789 C_DECL_VARIABLE_SIZE (decl) = 1;
4791 if (inlinep)
4792 pedwarn ("%Jvariable '%D' declared `inline'", decl, decl);
4794 /* At file scope, an initialized extern declaration may follow
4795 a static declaration. In that case, DECL_EXTERNAL will be
4796 reset later in start_decl. */
4797 DECL_EXTERNAL (decl) = !!(specbits & (1 << (int) RID_EXTERN));
4799 /* At file scope, the presence of a `static' or `register' storage
4800 class specifier, or the absence of all storage class specifiers
4801 makes this declaration a definition (perhaps tentative). Also,
4802 the absence of both `static' and `register' makes it public. */
4803 if (current_scope == file_scope)
4805 TREE_PUBLIC (decl) = !(specbits & ((1 << (int) RID_STATIC)
4806 | (1 << (int) RID_REGISTER)));
4807 TREE_STATIC (decl) = !extern_ref;
4809 /* Not at file scope, only `static' makes a static definition. */
4810 else
4812 TREE_STATIC (decl) = (specbits & (1 << (int) RID_STATIC)) != 0;
4813 TREE_PUBLIC (decl) = extern_ref;
4816 if (specbits & 1 << (int) RID_THREAD)
4818 if (targetm.have_tls)
4819 DECL_THREAD_LOCAL (decl) = 1;
4820 else
4821 /* A mere warning is sure to result in improper semantics
4822 at runtime. Don't bother to allow this to compile. */
4823 error ("thread-local storage not supported for this target");
4827 /* Record `register' declaration for warnings on &
4828 and in case doing stupid register allocation. */
4830 if (specbits & (1 << (int) RID_REGISTER))
4832 C_DECL_REGISTER (decl) = 1;
4833 DECL_REGISTER (decl) = 1;
4836 /* Record constancy and volatility. */
4837 c_apply_type_quals_to_decl (type_quals, decl);
4839 /* If a type has volatile components, it should be stored in memory.
4840 Otherwise, the fact that those components are volatile
4841 will be ignored, and would even crash the compiler. */
4842 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4844 /* It is not an error for a structure with volatile fields to
4845 be declared register, but reset DECL_REGISTER since it
4846 cannot actually go in a register. */
4847 int was_reg = C_DECL_REGISTER (decl);
4848 C_DECL_REGISTER (decl) = 0;
4849 DECL_REGISTER (decl) = 0;
4850 c_mark_addressable (decl);
4851 C_DECL_REGISTER (decl) = was_reg;
4854 #ifdef ENABLE_CHECKING
4855 /* This is the earliest point at which we might know the assembler
4856 name of a variable. Thus, if it's known before this, die horribly. */
4857 if (DECL_ASSEMBLER_NAME_SET_P (decl))
4858 abort ();
4859 #endif
4861 decl_attributes (&decl, returned_attrs, 0);
4863 return decl;
4867 /* Decode the parameter-list info for a function type or function definition.
4868 The argument is the value returned by `get_parm_info' (or made in parse.y
4869 if there is an identifier list instead of a parameter decl list).
4870 These two functions are separate because when a function returns
4871 or receives functions then each is called multiple times but the order
4872 of calls is different. The last call to `grokparms' is always the one
4873 that contains the formal parameter names of a function definition.
4875 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4877 FUNCDEF_FLAG is true for a function definition, false for
4878 a mere declaration. A nonempty identifier-list gets an error message
4879 when FUNCDEF_FLAG is false. */
4881 static tree
4882 grokparms (tree arg_info, bool funcdef_flag)
4884 tree arg_types = ARG_INFO_TYPES (arg_info);
4886 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4887 && !in_system_header)
4888 warning ("function declaration isn't a prototype");
4890 if (arg_types == error_mark_node)
4891 return 0; /* don't set TYPE_ARG_TYPES in this case */
4893 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4895 if (! funcdef_flag)
4896 pedwarn ("parameter names (without types) in function declaration");
4898 ARG_INFO_PARMS (arg_info) = ARG_INFO_TYPES (arg_info);
4899 ARG_INFO_TYPES (arg_info) = 0;
4900 return 0;
4902 else
4904 tree parm, type, typelt;
4905 unsigned int parmno;
4907 /* If the arg types are incomplete in a declaration, they must
4908 include undefined tags. These tags can never be defined in
4909 the scope of the declaration, so the types can never be
4910 completed, and no call can be compiled successfully. */
4912 for (parm = ARG_INFO_PARMS (arg_info), typelt = arg_types, parmno = 1;
4913 parm;
4914 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4916 type = TREE_VALUE (typelt);
4917 if (type == error_mark_node)
4918 continue;
4920 if (!COMPLETE_TYPE_P (type))
4922 if (funcdef_flag)
4924 if (DECL_NAME (parm))
4925 error ("%Jparameter %u ('%D') has incomplete type",
4926 parm, parmno, parm);
4927 else
4928 error ("%Jparameter %u has incomplete type",
4929 parm, parmno);
4931 TREE_VALUE (typelt) = error_mark_node;
4932 TREE_TYPE (parm) = error_mark_node;
4934 else
4936 if (DECL_NAME (parm))
4937 warning ("%Jparameter %u ('%D') has incomplete type",
4938 parm, parmno, parm);
4939 else
4940 warning ("%Jparameter %u has incomplete type",
4941 parm, parmno);
4945 return arg_types;
4949 /* Take apart the current scope and return a tree_list node with info
4950 on a parameter list just parsed. This tree_list node should be
4951 examined using the ARG_INFO_* macros, defined above:
4953 ARG_INFO_PARMS: a list of parameter decls.
4954 ARG_INFO_TAGS: a list of structure, union and enum tags defined.
4955 ARG_INFO_TYPES: a list of argument types to go in the FUNCTION_TYPE.
4956 ARG_INFO_OTHERS: a list of non-parameter decls (notably enumeration
4957 constants) defined with the parameters.
4959 This tree_list node is later fed to 'grokparms' and 'store_parm_decls'.
4961 ELLIPSIS being true means the argument list ended in '...' so don't
4962 append a sentinel (void_list_node) to the end of the type-list. */
4964 tree
4965 get_parm_info (bool ellipsis)
4967 struct c_binding *b = current_scope->bindings;
4968 tree arg_info = make_node (TREE_LIST);
4969 tree parms = 0;
4970 tree tags = 0;
4971 tree types = 0;
4972 tree others = 0;
4974 static bool explained_incomplete_types = false;
4975 bool gave_void_only_once_err = false;
4977 /* The bindings in this scope must not get put into a block.
4978 We will take care of deleting the binding nodes. */
4979 current_scope->bindings = 0;
4981 /* This function is only called if there was *something* on the
4982 parameter list. */
4983 #ifdef ENABLE_CHECKING
4984 if (b == 0)
4985 abort ();
4986 #endif
4988 /* A parameter list consisting solely of 'void' indicates that the
4989 function takes no arguments. But if the 'void' is qualified
4990 (by 'const' or 'volatile'), or has a storage class specifier
4991 ('register'), then the behavior is undefined; issue an error.
4992 Typedefs for 'void' are OK (see DR#157). */
4993 if (b->prev == 0 /* one binding */
4994 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4995 && !DECL_NAME (b->decl) /* anonymous */
4996 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4998 if (TREE_THIS_VOLATILE (b->decl)
4999 || TREE_READONLY (b->decl)
5000 || C_DECL_REGISTER (b->decl))
5001 error ("'void' as only parameter may not be qualified");
5003 /* There cannot be an ellipsis. */
5004 if (ellipsis)
5005 error ("'void' must be the only parameter");
5007 ARG_INFO_TYPES (arg_info) = void_list_node;
5008 return arg_info;
5011 if (!ellipsis)
5012 types = void_list_node;
5014 /* Break up the bindings list into parms, tags, types, and others;
5015 apply sanity checks; purge the name-to-decl bindings. */
5016 while (b)
5018 tree decl = b->decl;
5019 tree type = TREE_TYPE (decl);
5020 const char *keyword;
5022 switch (TREE_CODE (decl))
5024 case PARM_DECL:
5025 if (b->id)
5027 #ifdef ENABLE_CHECKING
5028 if (I_SYMBOL_BINDING (b->id) != b) abort ();
5029 #endif
5030 I_SYMBOL_BINDING (b->id) = b->shadowed;
5033 /* Check for forward decls that never got their actual decl. */
5034 if (TREE_ASM_WRITTEN (decl))
5035 error ("%Jparameter '%D' has just a forward declaration",
5036 decl, decl);
5037 /* Check for (..., void, ...) and issue an error. */
5038 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5040 if (!gave_void_only_once_err)
5042 error ("'void' must be the only parameter");
5043 gave_void_only_once_err = true;
5046 else
5048 /* Valid parameter, add it to the list. */
5049 TREE_CHAIN (decl) = parms;
5050 parms = decl;
5052 /* Since there is a prototype, args are passed in their
5053 declared types. The back end may override this later. */
5054 DECL_ARG_TYPE (decl) = type;
5055 types = tree_cons (0, type, types);
5057 break;
5059 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5060 case UNION_TYPE: keyword = "union"; goto tag;
5061 case RECORD_TYPE: keyword = "struct"; goto tag;
5062 tag:
5063 /* Types may not have tag-names, in which case the type
5064 appears in the bindings list with b->id NULL. */
5065 if (b->id)
5067 #ifdef ENABLE_CHECKING
5068 if (I_TAG_BINDING (b->id) != b) abort ();
5069 #endif
5070 I_TAG_BINDING (b->id) = b->shadowed;
5073 /* Warn about any struct, union or enum tags defined in a
5074 parameter list. The scope of such types is limited to
5075 the parameter list, which is rarely if ever desirable
5076 (it's impossible to call such a function with type-
5077 correct arguments). An anonymous union parm type is
5078 meaningful as a GNU extension, so don't warn for that. */
5079 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5081 if (b->id)
5082 /* The %s will be one of 'struct', 'union', or 'enum'. */
5083 warning ("'%s %E' declared inside parameter list",
5084 keyword, b->id);
5085 else
5086 /* The %s will be one of 'struct', 'union', or 'enum'. */
5087 warning ("anonymous %s declared inside parameter list",
5088 keyword);
5090 if (! explained_incomplete_types)
5092 warning ("its scope is only this definition or declaration,"
5093 " which is probably not what you want");
5094 explained_incomplete_types = true;
5098 tags = tree_cons (b->id, decl, tags);
5099 break;
5101 case CONST_DECL:
5102 case TYPE_DECL:
5103 /* CONST_DECLs appear here when we have an embedded enum,
5104 and TYPE_DECLs appear here when we have an embedded struct
5105 or union. No warnings for this - we already warned about the
5106 type itself. */
5107 TREE_CHAIN (decl) = others;
5108 others = decl;
5109 /* fall through */
5111 case ERROR_MARK:
5112 /* error_mark_node appears here when we have an undeclared
5113 variable. Just throw it away. */
5114 if (b->id)
5116 #ifdef ENABLE_CHECKING
5117 if (I_SYMBOL_BINDING (b->id) != b) abort ();
5118 #endif
5119 I_SYMBOL_BINDING (b->id) = b->shadowed;
5121 break;
5123 /* Other things that might be encountered. */
5124 case LABEL_DECL:
5125 case FUNCTION_DECL:
5126 case VAR_DECL:
5127 default:
5128 abort ();
5131 b = free_binding_and_advance (b);
5134 ARG_INFO_PARMS (arg_info) = parms;
5135 ARG_INFO_TAGS (arg_info) = tags;
5136 ARG_INFO_TYPES (arg_info) = types;
5137 ARG_INFO_OTHERS (arg_info) = others;
5138 return arg_info;
5141 /* Get the struct, enum or union (CODE says which) with tag NAME.
5142 Define the tag as a forward-reference if it is not defined. */
5144 tree
5145 xref_tag (enum tree_code code, tree name)
5147 /* If a cross reference is requested, look up the type
5148 already defined for this tag and return it. */
5150 tree ref = lookup_tag (code, name, 0);
5151 /* If this is the right type of tag, return what we found.
5152 (This reference will be shadowed by shadow_tag later if appropriate.)
5153 If this is the wrong type of tag, do not return it. If it was the
5154 wrong type in the same scope, we will have had an error
5155 message already; if in a different scope and declaring
5156 a name, pending_xref_error will give an error message; but if in a
5157 different scope and not declaring a name, this tag should
5158 shadow the previous declaration of a different type of tag, and
5159 this would not work properly if we return the reference found.
5160 (For example, with "struct foo" in an outer scope, "union foo;"
5161 must shadow that tag with a new one of union type.) */
5162 if (ref && TREE_CODE (ref) == code)
5163 return ref;
5165 /* If no such tag is yet defined, create a forward-reference node
5166 and record it as the "definition".
5167 When a real declaration of this type is found,
5168 the forward-reference will be altered into a real type. */
5170 ref = make_node (code);
5171 if (code == ENUMERAL_TYPE)
5173 /* Give the type a default layout like unsigned int
5174 to avoid crashing if it does not get defined. */
5175 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5176 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5177 TYPE_USER_ALIGN (ref) = 0;
5178 TYPE_UNSIGNED (ref) = 1;
5179 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5180 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5181 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5184 pushtag (name, ref);
5186 return ref;
5189 /* Make sure that the tag NAME is defined *in the current scope*
5190 at least as a forward reference.
5191 CODE says which kind of tag NAME ought to be. */
5193 tree
5194 start_struct (enum tree_code code, tree name)
5196 /* If there is already a tag defined at this scope
5197 (as a forward reference), just return it. */
5199 tree ref = 0;
5201 if (name != 0)
5202 ref = lookup_tag (code, name, 1);
5203 if (ref && TREE_CODE (ref) == code)
5205 if (TYPE_FIELDS (ref))
5207 if (code == UNION_TYPE)
5208 error ("redefinition of `union %s'", IDENTIFIER_POINTER (name));
5209 else
5210 error ("redefinition of `struct %s'", IDENTIFIER_POINTER (name));
5213 else
5215 /* Otherwise create a forward-reference just so the tag is in scope. */
5217 ref = make_node (code);
5218 pushtag (name, ref);
5221 C_TYPE_BEING_DEFINED (ref) = 1;
5222 TYPE_PACKED (ref) = flag_pack_struct;
5223 return ref;
5226 /* Process the specs, declarator (NULL if omitted) and width (NULL if omitted)
5227 of a structure component, returning a FIELD_DECL node.
5228 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5230 This is done during the parsing of the struct declaration.
5231 The FIELD_DECL nodes are chained together and the lot of them
5232 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5234 tree
5235 grokfield (tree declarator, tree declspecs, tree width)
5237 tree value;
5239 if (declarator == NULL_TREE && width == NULL_TREE)
5241 /* This is an unnamed decl.
5243 If we have something of the form "union { list } ;" then this
5244 is the anonymous union extension. Similarly for struct.
5246 If this is something of the form "struct foo;", then
5247 If MS extensions are enabled, this is handled as an
5248 anonymous struct.
5249 Otherwise this is a forward declaration of a structure tag.
5251 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5252 If MS extensions are enabled and foo names a structure, then
5253 again this is an anonymous struct.
5254 Otherwise this is an error.
5256 Oh what a horrid tangled web we weave. I wonder if MS consciously
5257 took this from Plan 9 or if it was an accident of implementation
5258 that took root before someone noticed the bug... */
5260 tree type = TREE_VALUE (declspecs);
5262 if (flag_ms_extensions && TREE_CODE (type) == TYPE_DECL)
5263 type = TREE_TYPE (type);
5264 if (TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE)
5266 if (flag_ms_extensions)
5267 ; /* ok */
5268 else if (flag_iso)
5269 goto warn_unnamed_field;
5270 else if (TYPE_NAME (type) == NULL)
5271 ; /* ok */
5272 else
5273 goto warn_unnamed_field;
5275 else
5277 warn_unnamed_field:
5278 warning ("declaration does not declare anything");
5279 return NULL_TREE;
5283 value = grokdeclarator (declarator, declspecs, FIELD, false,
5284 width ? &width : NULL);
5286 finish_decl (value, NULL_TREE, NULL_TREE);
5287 DECL_INITIAL (value) = width;
5289 return value;
5292 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5293 the list such that this does not present a problem later. */
5295 static void
5296 detect_field_duplicates (tree fieldlist)
5298 tree x, y;
5299 int timeout = 10;
5301 /* First, see if there are more than "a few" fields.
5302 This is trivially true if there are zero or one fields. */
5303 if (!fieldlist)
5304 return;
5305 x = TREE_CHAIN (fieldlist);
5306 if (!x)
5307 return;
5308 do {
5309 timeout--;
5310 x = TREE_CHAIN (x);
5311 } while (timeout > 0 && x);
5313 /* If there were "few" fields, avoid the overhead of allocating
5314 a hash table. Instead just do the nested traversal thing. */
5315 if (timeout > 0)
5317 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5318 if (DECL_NAME (x))
5320 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5321 if (DECL_NAME (y) == DECL_NAME (x))
5323 error ("%Jduplicate member '%D'", x, x);
5324 DECL_NAME (x) = NULL_TREE;
5328 else
5330 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5331 void **slot;
5333 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5334 if ((y = DECL_NAME (x)) != 0)
5336 slot = htab_find_slot (htab, y, INSERT);
5337 if (*slot)
5339 error ("%Jduplicate member '%D'", x, x);
5340 DECL_NAME (x) = NULL_TREE;
5342 *slot = y;
5345 htab_delete (htab);
5349 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5350 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5351 ATTRIBUTES are attributes to be applied to the structure. */
5353 tree
5354 finish_struct (tree t, tree fieldlist, tree attributes)
5356 tree x;
5357 bool toplevel = file_scope == current_scope;
5358 int saw_named_field;
5360 /* If this type was previously laid out as a forward reference,
5361 make sure we lay it out again. */
5363 TYPE_SIZE (t) = 0;
5365 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5367 if (pedantic)
5369 for (x = fieldlist; x; x = TREE_CHAIN (x))
5370 if (DECL_NAME (x) != 0)
5371 break;
5373 if (x == 0)
5374 pedwarn ("%s has no %s",
5375 TREE_CODE (t) == UNION_TYPE ? _("union") : _("struct"),
5376 fieldlist ? _("named members") : _("members"));
5379 /* Install struct as DECL_CONTEXT of each field decl.
5380 Also process specified field sizes, found in the DECL_INITIAL,
5381 storing 0 there after the type has been changed to precision equal
5382 to its width, rather than the precision of the specified standard
5383 type. (Correct layout requires the original type to have been preserved
5384 until now.) */
5386 saw_named_field = 0;
5387 for (x = fieldlist; x; x = TREE_CHAIN (x))
5389 DECL_CONTEXT (x) = t;
5390 DECL_PACKED (x) |= TYPE_PACKED (t);
5392 /* If any field is const, the structure type is pseudo-const. */
5393 if (TREE_READONLY (x))
5394 C_TYPE_FIELDS_READONLY (t) = 1;
5395 else
5397 /* A field that is pseudo-const makes the structure likewise. */
5398 tree t1 = TREE_TYPE (x);
5399 while (TREE_CODE (t1) == ARRAY_TYPE)
5400 t1 = TREE_TYPE (t1);
5401 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5402 && C_TYPE_FIELDS_READONLY (t1))
5403 C_TYPE_FIELDS_READONLY (t) = 1;
5406 /* Any field that is volatile means variables of this type must be
5407 treated in some ways as volatile. */
5408 if (TREE_THIS_VOLATILE (x))
5409 C_TYPE_FIELDS_VOLATILE (t) = 1;
5411 /* Any field of nominal variable size implies structure is too. */
5412 if (C_DECL_VARIABLE_SIZE (x))
5413 C_TYPE_VARIABLE_SIZE (t) = 1;
5415 /* Detect invalid nested redefinition. */
5416 if (TREE_TYPE (x) == t)
5417 error ("nested redefinition of `%s'",
5418 IDENTIFIER_POINTER (TYPE_NAME (t)));
5420 if (DECL_INITIAL (x))
5422 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5423 DECL_SIZE (x) = bitsize_int (width);
5424 DECL_BIT_FIELD (x) = 1;
5425 SET_DECL_C_BIT_FIELD (x);
5428 /* Detect flexible array member in an invalid context. */
5429 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5430 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5431 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5432 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5434 if (TREE_CODE (t) == UNION_TYPE)
5436 error ("%Jflexible array member in union", x);
5437 TREE_TYPE (x) = error_mark_node;
5439 else if (TREE_CHAIN (x) != NULL_TREE)
5441 error ("%Jflexible array member not at end of struct", x);
5442 TREE_TYPE (x) = error_mark_node;
5444 else if (! saw_named_field)
5446 error ("%Jflexible array member in otherwise empty struct", x);
5447 TREE_TYPE (x) = error_mark_node;
5451 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5452 && flexible_array_type_p (TREE_TYPE (x)))
5453 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5455 if (DECL_NAME (x))
5456 saw_named_field = 1;
5459 detect_field_duplicates (fieldlist);
5461 /* Now we have the nearly final fieldlist. Record it,
5462 then lay out the structure or union (including the fields). */
5464 TYPE_FIELDS (t) = fieldlist;
5466 layout_type (t);
5468 /* Give bit-fields their proper types. */
5470 tree *fieldlistp = &fieldlist;
5471 while (*fieldlistp)
5472 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5473 && TREE_TYPE (*fieldlistp) != error_mark_node)
5475 unsigned HOST_WIDE_INT width
5476 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5477 tree type = TREE_TYPE (*fieldlistp);
5478 if (width != TYPE_PRECISION (type))
5479 TREE_TYPE (*fieldlistp)
5480 = build_nonstandard_integer_type (width, TYPE_UNSIGNED (type));
5481 DECL_INITIAL (*fieldlistp) = 0;
5483 else
5484 fieldlistp = &TREE_CHAIN (*fieldlistp);
5487 /* Now we have the truly final field list.
5488 Store it in this type and in the variants. */
5490 TYPE_FIELDS (t) = fieldlist;
5492 /* If there are lots of fields, sort so we can look through them fast.
5493 We arbitrarily consider 16 or more elts to be "a lot". */
5496 int len = 0;
5498 for (x = fieldlist; x; x = TREE_CHAIN (x))
5500 if (len > 15 || DECL_NAME (x) == NULL)
5501 break;
5502 len += 1;
5505 if (len > 15)
5507 tree *field_array;
5508 struct lang_type *space;
5509 struct sorted_fields_type *space2;
5511 len += list_length (x);
5513 /* Use the same allocation policy here that make_node uses, to
5514 ensure that this lives as long as the rest of the struct decl.
5515 All decls in an inline function need to be saved. */
5517 space = GGC_CNEW (struct lang_type);
5518 space2 = GGC_NEWVAR (struct sorted_fields_type,
5519 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5521 len = 0;
5522 space->s = space2;
5523 field_array = &space2->elts[0];
5524 for (x = fieldlist; x; x = TREE_CHAIN (x))
5526 field_array[len++] = x;
5528 /* If there is anonymous struct or union, break out of the loop. */
5529 if (DECL_NAME (x) == NULL)
5530 break;
5532 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5533 if (x == NULL)
5535 TYPE_LANG_SPECIFIC (t) = space;
5536 TYPE_LANG_SPECIFIC (t)->s->len = len;
5537 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5538 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5543 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5545 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5546 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5547 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5548 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5551 /* If this was supposed to be a transparent union, but we can't
5552 make it one, warn and turn off the flag. */
5553 if (TREE_CODE (t) == UNION_TYPE
5554 && TYPE_TRANSPARENT_UNION (t)
5555 && TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t)))
5557 TYPE_TRANSPARENT_UNION (t) = 0;
5558 warning ("union cannot be made transparent");
5561 /* If this structure or union completes the type of any previous
5562 variable declaration, lay it out and output its rtl. */
5563 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5565 x = TREE_CHAIN (x))
5567 tree decl = TREE_VALUE (x);
5568 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5569 layout_array_type (TREE_TYPE (decl));
5570 if (TREE_CODE (decl) != TYPE_DECL)
5572 layout_decl (decl, 0);
5573 if (c_dialect_objc ())
5574 objc_check_decl (decl);
5575 rest_of_decl_compilation (decl, toplevel, 0);
5576 if (! toplevel)
5577 expand_decl (decl);
5580 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5582 /* Finish debugging output for this type. */
5583 rest_of_type_compilation (t, toplevel);
5585 return t;
5588 /* Lay out the type T, and its element type, and so on. */
5590 static void
5591 layout_array_type (tree t)
5593 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5594 layout_array_type (TREE_TYPE (t));
5595 layout_type (t);
5598 /* Begin compiling the definition of an enumeration type.
5599 NAME is its name (or null if anonymous).
5600 Returns the type object, as yet incomplete.
5601 Also records info about it so that build_enumerator
5602 may be used to declare the individual values as they are read. */
5604 tree
5605 start_enum (tree name)
5607 tree enumtype = 0;
5609 /* If this is the real definition for a previous forward reference,
5610 fill in the contents in the same object that used to be the
5611 forward reference. */
5613 if (name != 0)
5614 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5616 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5618 enumtype = make_node (ENUMERAL_TYPE);
5619 pushtag (name, enumtype);
5622 C_TYPE_BEING_DEFINED (enumtype) = 1;
5624 if (TYPE_VALUES (enumtype) != 0)
5626 /* This enum is a named one that has been declared already. */
5627 error ("redeclaration of `enum %s'", IDENTIFIER_POINTER (name));
5629 /* Completely replace its old definition.
5630 The old enumerators remain defined, however. */
5631 TYPE_VALUES (enumtype) = 0;
5634 enum_next_value = integer_zero_node;
5635 enum_overflow = 0;
5637 if (flag_short_enums)
5638 TYPE_PACKED (enumtype) = 1;
5640 return enumtype;
5643 /* After processing and defining all the values of an enumeration type,
5644 install their decls in the enumeration type and finish it off.
5645 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5646 and ATTRIBUTES are the specified attributes.
5647 Returns ENUMTYPE. */
5649 tree
5650 finish_enum (tree enumtype, tree values, tree attributes)
5652 tree pair, tem;
5653 tree minnode = 0, maxnode = 0;
5654 int precision, unsign;
5655 bool toplevel = (file_scope == current_scope);
5656 struct lang_type *lt;
5658 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5660 /* Calculate the maximum value of any enumerator in this type. */
5662 if (values == error_mark_node)
5663 minnode = maxnode = integer_zero_node;
5664 else
5666 minnode = maxnode = TREE_VALUE (values);
5667 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5669 tree value = TREE_VALUE (pair);
5670 if (tree_int_cst_lt (maxnode, value))
5671 maxnode = value;
5672 if (tree_int_cst_lt (value, minnode))
5673 minnode = value;
5677 /* Construct the final type of this enumeration. It is the same
5678 as one of the integral types - the narrowest one that fits, except
5679 that normally we only go as narrow as int - and signed iff any of
5680 the values are negative. */
5681 unsign = (tree_int_cst_sgn (minnode) >= 0);
5682 precision = MAX (min_precision (minnode, unsign),
5683 min_precision (maxnode, unsign));
5684 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5686 tem = c_common_type_for_size (precision, unsign);
5687 if (tem == NULL)
5689 warning ("enumeration values exceed range of largest integer");
5690 tem = long_long_integer_type_node;
5693 else
5694 tem = unsign ? unsigned_type_node : integer_type_node;
5696 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5697 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5698 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5699 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5700 TYPE_SIZE (enumtype) = 0;
5701 layout_type (enumtype);
5703 if (values != error_mark_node)
5705 /* Change the type of the enumerators to be the enum type. We
5706 need to do this irrespective of the size of the enum, for
5707 proper type checking. Replace the DECL_INITIALs of the
5708 enumerators, and the value slots of the list, with copies
5709 that have the enum type; they cannot be modified in place
5710 because they may be shared (e.g. integer_zero_node) Finally,
5711 change the purpose slots to point to the names of the decls. */
5712 for (pair = values; pair; pair = TREE_CHAIN (pair))
5714 tree enu = TREE_PURPOSE (pair);
5715 tree ini = DECL_INITIAL (enu);
5717 TREE_TYPE (enu) = enumtype;
5719 /* The ISO C Standard mandates enumerators to have type int,
5720 even though the underlying type of an enum type is
5721 unspecified. Here we convert any enumerators that fit in
5722 an int to type int, to avoid promotions to unsigned types
5723 when comparing integers with enumerators that fit in the
5724 int range. When -pedantic is given, build_enumerator()
5725 would have already taken care of those that don't fit. */
5726 if (int_fits_type_p (ini, integer_type_node))
5727 tem = integer_type_node;
5728 else
5729 tem = enumtype;
5730 ini = convert (tem, ini);
5732 DECL_INITIAL (enu) = ini;
5733 TREE_PURPOSE (pair) = DECL_NAME (enu);
5734 TREE_VALUE (pair) = ini;
5737 TYPE_VALUES (enumtype) = values;
5740 /* Record the min/max values so that we can warn about bit-field
5741 enumerations that are too small for the values. */
5742 lt = GGC_CNEW (struct lang_type);
5743 lt->enum_min = minnode;
5744 lt->enum_max = maxnode;
5745 TYPE_LANG_SPECIFIC (enumtype) = lt;
5747 /* Fix up all variant types of this enum type. */
5748 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5750 if (tem == enumtype)
5751 continue;
5752 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5753 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5754 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5755 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5756 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5757 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5758 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5759 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5760 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5761 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5762 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5765 /* Finish debugging output for this type. */
5766 rest_of_type_compilation (enumtype, toplevel);
5768 return enumtype;
5771 /* Build and install a CONST_DECL for one value of the
5772 current enumeration type (one that was begun with start_enum).
5773 Return a tree-list containing the CONST_DECL and its value.
5774 Assignment of sequential values by default is handled here. */
5776 tree
5777 build_enumerator (tree name, tree value)
5779 tree decl, type;
5781 /* Validate and default VALUE. */
5783 /* Remove no-op casts from the value. */
5784 if (value)
5785 STRIP_TYPE_NOPS (value);
5787 if (value != 0)
5789 /* Don't issue more errors for error_mark_node (i.e. an
5790 undeclared identifier) - just ignore the value expression. */
5791 if (value == error_mark_node)
5792 value = 0;
5793 else if (TREE_CODE (value) != INTEGER_CST)
5795 error ("enumerator value for '%E' is not an integer constant", name);
5796 value = 0;
5798 else
5800 value = default_conversion (value);
5801 constant_expression_warning (value);
5805 /* Default based on previous value. */
5806 /* It should no longer be possible to have NON_LVALUE_EXPR
5807 in the default. */
5808 if (value == 0)
5810 value = enum_next_value;
5811 if (enum_overflow)
5812 error ("overflow in enumeration values");
5815 if (pedantic && ! int_fits_type_p (value, integer_type_node))
5817 pedwarn ("ISO C restricts enumerator values to range of `int'");
5818 /* XXX This causes -pedantic to change the meaning of the program.
5819 Remove? -zw 2004-03-15 */
5820 value = convert (integer_type_node, value);
5823 /* Set basis for default for next value. */
5824 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5825 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5827 /* Now create a declaration for the enum value name. */
5829 type = TREE_TYPE (value);
5830 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5831 TYPE_PRECISION (integer_type_node)),
5832 (TYPE_PRECISION (type)
5833 >= TYPE_PRECISION (integer_type_node)
5834 && TYPE_UNSIGNED (type)));
5836 decl = build_decl (CONST_DECL, name, type);
5837 DECL_INITIAL (decl) = convert (type, value);
5838 pushdecl (decl);
5840 return tree_cons (decl, value, NULL_TREE);
5844 /* Create the FUNCTION_DECL for a function definition.
5845 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5846 the declaration; they describe the function's name and the type it returns,
5847 but twisted together in a fashion that parallels the syntax of C.
5849 This function creates a binding context for the function body
5850 as well as setting up the FUNCTION_DECL in current_function_decl.
5852 Returns 1 on success. If the DECLARATOR is not suitable for a function
5853 (it defines a datum instead), we return 0, which tells
5854 yyparse to report a parse error. */
5857 start_function (tree declspecs, tree declarator, tree attributes)
5859 tree decl1, old_decl;
5860 tree restype, resdecl;
5862 current_function_returns_value = 0; /* Assume, until we see it does. */
5863 current_function_returns_null = 0;
5864 current_function_returns_abnormally = 0;
5865 warn_about_return_type = 0;
5866 current_extern_inline = 0;
5867 c_switch_stack = NULL;
5869 /* Indicate no valid break/continue context by setting these variables
5870 to some non-null, non-label value. We'll notice and emit the proper
5871 error message in c_finish_bc_stmt. */
5872 c_break_label = c_cont_label = size_zero_node;
5874 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5876 /* If the declarator is not suitable for a function definition,
5877 cause a syntax error. */
5878 if (decl1 == 0)
5879 return 0;
5881 decl_attributes (&decl1, attributes, 0);
5883 if (DECL_DECLARED_INLINE_P (decl1)
5884 && DECL_UNINLINABLE (decl1)
5885 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5886 warning ("%Jinline function '%D' given attribute noinline", decl1, decl1);
5888 announce_function (decl1);
5890 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5892 error ("return type is an incomplete type");
5893 /* Make it return void instead. */
5894 TREE_TYPE (decl1)
5895 = build_function_type (void_type_node,
5896 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5899 if (warn_about_return_type)
5900 pedwarn_c99 ("return type defaults to `int'");
5902 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5903 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5904 DECL_INITIAL (decl1) = error_mark_node;
5906 /* If this definition isn't a prototype and we had a prototype declaration
5907 before, copy the arg type info from that prototype.
5908 But not if what we had before was a builtin function. */
5909 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5910 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5911 && !DECL_BUILT_IN (old_decl)
5912 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5913 TREE_TYPE (TREE_TYPE (old_decl)))
5914 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5916 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5917 TREE_TYPE (decl1));
5918 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5921 /* Optionally warn of old-fashioned def with no previous prototype. */
5922 if (warn_strict_prototypes
5923 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5924 && C_DECL_ISNT_PROTOTYPE (old_decl))
5925 warning ("function declaration isn't a prototype");
5926 /* Optionally warn of any global def with no previous prototype. */
5927 else if (warn_missing_prototypes
5928 && TREE_PUBLIC (decl1)
5929 && ! MAIN_NAME_P (DECL_NAME (decl1))
5930 && C_DECL_ISNT_PROTOTYPE (old_decl))
5931 warning ("%Jno previous prototype for '%D'", decl1, decl1);
5932 /* Optionally warn of any def with no previous prototype
5933 if the function has already been used. */
5934 else if (warn_missing_prototypes
5935 && old_decl != 0 && TREE_USED (old_decl)
5936 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5937 warning ("%J'%D' was used with no prototype before its definition",
5938 decl1, decl1);
5939 /* Optionally warn of any global def with no previous declaration. */
5940 else if (warn_missing_declarations
5941 && TREE_PUBLIC (decl1)
5942 && old_decl == 0
5943 && ! MAIN_NAME_P (DECL_NAME (decl1)))
5944 warning ("%Jno previous declaration for '%D'", decl1, decl1);
5945 /* Optionally warn of any def with no previous declaration
5946 if the function has already been used. */
5947 else if (warn_missing_declarations
5948 && old_decl != 0 && TREE_USED (old_decl)
5949 && C_DECL_IMPLICIT (old_decl))
5950 warning ("%J`%D' was used with no declaration before its definition",
5951 decl1, decl1);
5953 /* This is a definition, not a reference.
5954 So normally clear DECL_EXTERNAL.
5955 However, `extern inline' acts like a declaration
5956 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5957 DECL_EXTERNAL (decl1) = current_extern_inline;
5959 /* This function exists in static storage.
5960 (This does not mean `static' in the C sense!) */
5961 TREE_STATIC (decl1) = 1;
5963 /* A nested function is not global. */
5964 if (current_function_decl != 0)
5965 TREE_PUBLIC (decl1) = 0;
5967 #ifdef ENABLE_CHECKING
5968 /* This is the earliest point at which we might know the assembler
5969 name of the function. Thus, if it's set before this, die horribly. */
5970 if (DECL_ASSEMBLER_NAME_SET_P (decl1))
5971 abort ();
5972 #endif
5974 /* If #pragma weak was used, mark the decl weak now. */
5975 if (current_scope == file_scope)
5976 maybe_apply_pragma_weak (decl1);
5978 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
5979 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
5981 tree args;
5982 int argct = 0;
5984 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
5985 != integer_type_node)
5986 pedwarn ("%Jreturn type of '%D' is not `int'", decl1, decl1);
5988 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
5989 args = TREE_CHAIN (args))
5991 tree type = args ? TREE_VALUE (args) : 0;
5993 if (type == void_type_node)
5994 break;
5996 ++argct;
5997 switch (argct)
5999 case 1:
6000 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6001 pedwarn ("%Jfirst argument of '%D' should be `int'",
6002 decl1, decl1);
6003 break;
6005 case 2:
6006 if (TREE_CODE (type) != POINTER_TYPE
6007 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6008 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6009 != char_type_node))
6010 pedwarn ("%Jsecond argument of '%D' should be 'char **'",
6011 decl1, decl1);
6012 break;
6014 case 3:
6015 if (TREE_CODE (type) != POINTER_TYPE
6016 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6017 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6018 != char_type_node))
6019 pedwarn ("%Jthird argument of '%D' should probably be "
6020 "'char **'", decl1, decl1);
6021 break;
6025 /* It is intentional that this message does not mention the third
6026 argument because it's only mentioned in an appendix of the
6027 standard. */
6028 if (argct > 0 && (argct < 2 || argct > 3))
6029 pedwarn ("%J'%D' takes only zero or two arguments", decl1, decl1);
6031 if (! TREE_PUBLIC (decl1))
6032 pedwarn ("%J'%D' is normally a non-static function", decl1, decl1);
6035 /* Record the decl so that the function name is defined.
6036 If we already have a decl for this name, and it is a FUNCTION_DECL,
6037 use the old decl. */
6039 current_function_decl = pushdecl (decl1);
6041 push_scope ();
6042 declare_parm_level ();
6044 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6045 /* Promote the value to int before returning it. */
6046 if (c_promoting_integer_type_p (restype))
6048 /* It retains unsignedness if not really getting wider. */
6049 if (TYPE_UNSIGNED (restype)
6050 && (TYPE_PRECISION (restype)
6051 == TYPE_PRECISION (integer_type_node)))
6052 restype = unsigned_type_node;
6053 else
6054 restype = integer_type_node;
6057 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6058 DECL_ARTIFICIAL (resdecl) = 1;
6059 DECL_IGNORED_P (resdecl) = 1;
6060 DECL_RESULT (current_function_decl) = resdecl;
6062 start_fname_decls ();
6064 return 1;
6067 /* Subroutine of store_parm_decls which handles new-style function
6068 definitions (prototype format). The parms already have decls, so we
6069 need only record them as in effect and complain if any redundant
6070 old-style parm decls were written. */
6071 static void
6072 store_parm_decls_newstyle (tree fndecl, tree arg_info)
6074 tree decl;
6075 tree parms = ARG_INFO_PARMS (arg_info);
6076 tree tags = ARG_INFO_TAGS (arg_info);
6077 tree others = ARG_INFO_OTHERS (arg_info);
6079 if (current_scope->bindings)
6081 error ("%Jold-style parameter declarations in prototyped "
6082 "function definition", fndecl);
6084 /* Get rid of the old-style declarations. */
6085 pop_scope ();
6086 push_scope ();
6088 /* Don't issue this warning for nested functions, and don't issue this
6089 warning if we got here because ARG_INFO_TYPES was error_mark_node
6090 (this happens when a function definition has just an ellipsis in
6091 its parameter list). */
6092 else if (warn_traditional && !in_system_header && !current_function_scope
6093 && ARG_INFO_TYPES (arg_info) != error_mark_node)
6094 warning ("%Jtraditional C rejects ISO C style function definitions",
6095 fndecl);
6097 /* Now make all the parameter declarations visible in the function body.
6098 We can bypass most of the grunt work of pushdecl. */
6099 for (decl = parms; decl; decl = TREE_CHAIN (decl))
6101 DECL_CONTEXT (decl) = current_function_decl;
6102 if (DECL_NAME (decl))
6103 bind (DECL_NAME (decl), decl, current_scope,
6104 /*invisible=*/false, /*nested=*/false);
6105 else
6106 error ("%Jparameter name omitted", decl);
6109 /* Record the parameter list in the function declaration. */
6110 DECL_ARGUMENTS (fndecl) = parms;
6112 /* Now make all the ancillary declarations visible, likewise. */
6113 for (decl = others; decl; decl = TREE_CHAIN (decl))
6115 DECL_CONTEXT (decl) = current_function_decl;
6116 if (DECL_NAME (decl))
6117 bind (DECL_NAME (decl), decl, current_scope,
6118 /*invisible=*/false, /*nested=*/false);
6121 /* And all the tag declarations. */
6122 for (decl = tags; decl; decl = TREE_CHAIN (decl))
6123 if (TREE_PURPOSE (decl))
6124 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6125 /*invisible=*/false, /*nested=*/false);
6128 /* Subroutine of store_parm_decls which handles old-style function
6129 definitions (separate parameter list and declarations). */
6131 static void
6132 store_parm_decls_oldstyle (tree fndecl, tree arg_info)
6134 struct c_binding *b;
6135 tree parm, decl, last;
6136 tree parmids = ARG_INFO_PARMS (arg_info);
6138 /* We use DECL_WEAK as a flag to show which parameters have been
6139 seen already, since it is not used on PARM_DECL. */
6140 #ifdef ENABLE_CHECKING
6141 for (b = current_scope->bindings; b; b = b->prev)
6142 if (TREE_CODE (b->decl) == PARM_DECL && DECL_WEAK (b->decl))
6143 abort ();
6144 #endif
6146 if (warn_old_style_definition && !in_system_header)
6147 warning ("%Jold-style function definition", fndecl);
6149 /* Match each formal parameter name with its declaration. Save each
6150 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6151 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6153 if (TREE_VALUE (parm) == 0)
6155 error ("%Jparameter name missing from parameter list", fndecl);
6156 TREE_PURPOSE (parm) = 0;
6157 continue;
6160 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6161 if (b && B_IN_CURRENT_SCOPE (b))
6163 decl = b->decl;
6164 /* If we got something other than a PARM_DECL it is an error. */
6165 if (TREE_CODE (decl) != PARM_DECL)
6166 error ("%J'%D' declared as a non-parameter", decl, decl);
6167 /* If the declaration is already marked, we have a duplicate
6168 name. Complain and ignore the duplicate. */
6169 else if (DECL_WEAK (decl))
6171 error ("%Jmultiple parameters named '%D'", decl, decl);
6172 TREE_PURPOSE (parm) = 0;
6173 continue;
6175 /* If the declaration says "void", complain and turn it into
6176 an int. */
6177 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6179 error ("%Jparameter '%D' declared with void type", decl, decl);
6180 TREE_TYPE (decl) = integer_type_node;
6181 DECL_ARG_TYPE (decl) = integer_type_node;
6182 layout_decl (decl, 0);
6185 /* If no declaration found, default to int. */
6186 else
6188 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6189 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6190 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6191 pushdecl (decl);
6193 if (flag_isoc99)
6194 pedwarn ("%Jtype of '%D' defaults to 'int'", decl, decl);
6195 else if (extra_warnings)
6196 warning ("%Jtype of '%D' defaults to 'int'", decl, decl);
6199 TREE_PURPOSE (parm) = decl;
6200 DECL_WEAK (decl) = 1;
6203 /* Now examine the parms chain for incomplete declarations
6204 and declarations with no corresponding names. */
6206 for (b = current_scope->bindings; b; b = b->prev)
6208 parm = b->decl;
6209 if (TREE_CODE (parm) != PARM_DECL)
6210 continue;
6212 if (!COMPLETE_TYPE_P (TREE_TYPE (parm)))
6214 error ("%Jparameter '%D' has incomplete type", parm, parm);
6215 TREE_TYPE (parm) = error_mark_node;
6218 if (! DECL_WEAK (parm))
6220 error ("%Jdeclaration for parameter '%D' but no such parameter",
6221 parm, parm);
6223 /* Pretend the parameter was not missing.
6224 This gets us to a standard state and minimizes
6225 further error messages. */
6226 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6230 /* Chain the declarations together in the order of the list of
6231 names. Store that chain in the function decl, replacing the
6232 list of names. Update the current scope to match. */
6233 DECL_ARGUMENTS (fndecl) = 0;
6235 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6236 if (TREE_PURPOSE (parm))
6237 break;
6238 if (parm && TREE_PURPOSE (parm))
6240 last = TREE_PURPOSE (parm);
6241 DECL_ARGUMENTS (fndecl) = last;
6242 DECL_WEAK (last) = 0;
6244 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6245 if (TREE_PURPOSE (parm))
6247 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6248 last = TREE_PURPOSE (parm);
6249 DECL_WEAK (last) = 0;
6251 TREE_CHAIN (last) = 0;
6254 /* If there was a previous prototype,
6255 set the DECL_ARG_TYPE of each argument according to
6256 the type previously specified, and report any mismatches. */
6258 if (TYPE_ARG_TYPES (TREE_TYPE (fndecl)))
6260 tree type;
6261 for (parm = DECL_ARGUMENTS (fndecl),
6262 type = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
6263 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6264 != void_type_node));
6265 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6267 if (parm == 0 || type == 0
6268 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6270 error ("number of arguments doesn't match prototype");
6271 error ("%Hprototype declaration",
6272 &current_function_prototype_locus);
6273 break;
6275 /* Type for passing arg must be consistent with that
6276 declared for the arg. ISO C says we take the unqualified
6277 type for parameters declared with qualified type. */
6278 if (! comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6279 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6281 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6282 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6284 /* Adjust argument to match prototype. E.g. a previous
6285 `int foo(float);' prototype causes
6286 `int foo(x) float x; {...}' to be treated like
6287 `int foo(float x) {...}'. This is particularly
6288 useful for argument types like uid_t. */
6289 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6291 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6292 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6293 && TYPE_PRECISION (TREE_TYPE (parm))
6294 < TYPE_PRECISION (integer_type_node))
6295 DECL_ARG_TYPE (parm) = integer_type_node;
6297 if (pedantic)
6299 pedwarn ("promoted argument '%D' "
6300 "doesn't match prototype", parm);
6301 pedwarn ("%Hprototype declaration",
6302 &current_function_prototype_locus);
6305 else
6307 error ("argument '%D' doesn't match prototype", parm);
6308 error ("%Hprototype declaration",
6309 &current_function_prototype_locus);
6313 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6316 /* Otherwise, create a prototype that would match. */
6318 else
6320 tree actual = 0, last = 0, type;
6322 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6324 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6325 if (last)
6326 TREE_CHAIN (last) = type;
6327 else
6328 actual = type;
6329 last = type;
6331 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6332 if (last)
6333 TREE_CHAIN (last) = type;
6334 else
6335 actual = type;
6337 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6338 of the type of this function, but we need to avoid having this
6339 affect the types of other similarly-typed functions, so we must
6340 first force the generation of an identical (but separate) type
6341 node for the relevant function type. The new node we create
6342 will be a variant of the main variant of the original function
6343 type. */
6345 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6347 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6351 /* Store the parameter declarations into the current function declaration.
6352 This is called after parsing the parameter declarations, before
6353 digesting the body of the function.
6355 For an old-style definition, construct a prototype out of the old-style
6356 parameter declarations and inject it into the function's type. */
6358 void
6359 store_parm_decls (void)
6361 tree fndecl = current_function_decl;
6362 bool proto;
6364 /* The argument information block for FNDECL. */
6365 tree arg_info = current_function_arg_info;
6366 current_function_arg_info = 0;
6368 /* True if this definition is written with a prototype. Note:
6369 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6370 list in a function definition as equivalent to (void) -- an
6371 empty argument list specifies the function has no parameters,
6372 but only (void) sets up a prototype for future calls. */
6373 proto = ARG_INFO_TYPES (arg_info) != 0;
6375 if (proto)
6376 store_parm_decls_newstyle (fndecl, arg_info);
6377 else
6378 store_parm_decls_oldstyle (fndecl, arg_info);
6380 /* The next call to push_scope will be a function body. */
6382 next_is_function_body = true;
6384 /* Write a record describing this function definition to the prototypes
6385 file (if requested). */
6387 gen_aux_info_record (fndecl, 1, 0, proto);
6389 /* Initialize the RTL code for the function. */
6390 allocate_struct_function (fndecl);
6392 /* Begin the statement tree for this function. */
6393 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6395 /* ??? Insert the contents of the pending sizes list into the function
6396 to be evaluated. This just changes mis-behaviour until assign_parms
6397 phase ordering problems are resolved. */
6399 tree t;
6400 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6401 add_stmt (TREE_VALUE (t));
6404 /* Even though we're inside a function body, we still don't want to
6405 call expand_expr to calculate the size of a variable-sized array.
6406 We haven't necessarily assigned RTL to all variables yet, so it's
6407 not safe to try to expand expressions involving them. */
6408 cfun->x_dont_save_pending_sizes_p = 1;
6411 /* Give FNDECL and all its nested functions to cgraph for compilation. */
6413 static void
6414 c_finalize (tree fndecl)
6416 struct cgraph_node *cgn;
6418 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6419 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6421 /* ??? Objc emits functions after finalizing the compilation unit.
6422 This should be cleaned up later and this conditional removed. */
6423 if (cgraph_global_info_ready)
6425 c_expand_body (fndecl);
6426 return;
6429 /* Finalize all nested functions now. */
6430 cgn = cgraph_node (fndecl);
6431 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6432 c_finalize (cgn->decl);
6434 cgraph_finalize_function (fndecl, false);
6437 /* Finish up a function declaration and compile that function
6438 all the way to assembler language output. The free the storage
6439 for the function definition.
6441 This is called after parsing the body of the function definition. */
6443 void
6444 finish_function (void)
6446 tree fndecl = current_function_decl;
6448 if (TREE_CODE (fndecl) == FUNCTION_DECL
6449 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6451 tree args = DECL_ARGUMENTS (fndecl);
6452 for (; args; args = TREE_CHAIN (args))
6454 tree type = TREE_TYPE (args);
6455 if (INTEGRAL_TYPE_P (type)
6456 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6457 DECL_ARG_TYPE (args) = integer_type_node;
6461 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6462 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6464 /* Must mark the RESULT_DECL as being in this function. */
6466 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6467 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6469 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6471 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6472 != integer_type_node)
6474 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6475 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6476 if (!warn_main)
6477 pedwarn ("%Jreturn type of '%D' is not `int'", fndecl, fndecl);
6479 else
6481 if (flag_isoc99)
6482 c_finish_return (integer_zero_node);
6486 /* Tie off the statement tree for this function. */
6487 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6489 finish_fname_decls ();
6491 /* Complain if there's just no return statement. */
6492 if (warn_return_type
6493 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6494 && !current_function_returns_value && !current_function_returns_null
6495 /* Don't complain if we abort. */
6496 && !current_function_returns_abnormally
6497 /* Don't warn for main(). */
6498 && !MAIN_NAME_P (DECL_NAME (fndecl))
6499 /* Or if they didn't actually specify a return type. */
6500 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6501 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6502 inline function, as we might never be compiled separately. */
6503 && DECL_INLINE (fndecl))
6504 warning ("no return statement in function returning non-void");
6506 /* With just -Wextra, complain only if function returns both with
6507 and without a value. */
6508 if (extra_warnings
6509 && current_function_returns_value
6510 && current_function_returns_null)
6511 warning ("this function may return with or without a value");
6513 /* Store the end of the function, so that we get good line number
6514 info for the epilogue. */
6515 cfun->function_end_locus = input_location;
6517 /* If we don't have ctors/dtors sections, and this is a static
6518 constructor or destructor, it must be recorded now. */
6519 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6520 && !targetm.have_ctors_dtors)
6521 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6522 if (DECL_STATIC_DESTRUCTOR (fndecl)
6523 && !targetm.have_ctors_dtors)
6524 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6526 /* Finalize the ELF visibility for the function. */
6527 c_determine_visibility (fndecl);
6529 /* Genericize before inlining. Delay genericizing nested functions
6530 until their parent function is genericized. Since finalizing
6531 requires GENERIC, delay that as well. */
6533 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6535 if (!decl_function_context (fndecl))
6537 c_genericize (fndecl);
6538 lower_nested_functions (fndecl);
6539 c_finalize (fndecl);
6541 else
6543 /* Register this function with cgraph just far enough to get it
6544 added to our parent's nested function list. Handy, since the
6545 C front end doesn't have such a list. */
6546 (void) cgraph_node (fndecl);
6550 /* We're leaving the context of this function, so zap cfun.
6551 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6552 tree_rest_of_compilation. */
6553 cfun = NULL;
6554 current_function_decl = NULL;
6557 /* Generate the RTL for the body of FNDECL. */
6559 void
6560 c_expand_body (tree fndecl)
6563 if (!DECL_INITIAL (fndecl)
6564 || DECL_INITIAL (fndecl) == error_mark_node)
6565 return;
6567 tree_rest_of_compilation (fndecl, false);
6569 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6570 && targetm.have_ctors_dtors)
6571 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6572 DEFAULT_INIT_PRIORITY);
6573 if (DECL_STATIC_DESTRUCTOR (fndecl)
6574 && targetm.have_ctors_dtors)
6575 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6576 DEFAULT_INIT_PRIORITY);
6579 /* Check the declarations given in a for-loop for satisfying the C99
6580 constraints. */
6581 void
6582 check_for_loop_decls (void)
6584 struct c_binding *b;
6586 if (!flag_isoc99)
6588 /* If we get here, declarations have been used in a for loop without
6589 the C99 for loop scope. This doesn't make much sense, so don't
6590 allow it. */
6591 error ("'for' loop initial declaration used outside C99 mode");
6592 return;
6594 /* C99 subclause 6.8.5 paragraph 3:
6596 [#3] The declaration part of a for statement shall only
6597 declare identifiers for objects having storage class auto or
6598 register.
6600 It isn't clear whether, in this sentence, "identifiers" binds to
6601 "shall only declare" or to "objects" - that is, whether all identifiers
6602 declared must be identifiers for objects, or whether the restriction
6603 only applies to those that are. (A question on this in comp.std.c
6604 in November 2000 received no answer.) We implement the strictest
6605 interpretation, to avoid creating an extension which later causes
6606 problems. */
6608 for (b = current_scope->bindings; b; b = b->prev)
6610 tree id = b->id;
6611 tree decl = b->decl;
6613 if (!id)
6614 continue;
6616 switch (TREE_CODE (decl))
6618 case VAR_DECL:
6619 if (TREE_STATIC (decl))
6620 error ("%Jdeclaration of static variable '%D' in 'for' loop "
6621 "initial declaration", decl, decl);
6622 else if (DECL_EXTERNAL (decl))
6623 error ("%Jdeclaration of 'extern' variable '%D' in 'for' loop "
6624 "initial declaration", decl, decl);
6625 break;
6627 case RECORD_TYPE:
6628 error ("'struct %E' declared in 'for' loop initial declaration", id);
6629 break;
6630 case UNION_TYPE:
6631 error ("'union %E' declared in 'for' loop initial declaration", id);
6632 break;
6633 case ENUMERAL_TYPE:
6634 error ("'enum %E' declared in 'for' loop initial declaration", id);
6635 break;
6636 default:
6637 error ("%Jdeclaration of non-variable '%D' in 'for' loop "
6638 "initial declaration", decl, decl);
6643 /* Save and reinitialize the variables
6644 used during compilation of a C function. */
6646 void
6647 c_push_function_context (struct function *f)
6649 struct language_function *p;
6650 p = GGC_NEW (struct language_function);
6651 f->language = p;
6653 p->base.x_stmt_tree = c_stmt_tree;
6654 p->x_break_label = c_break_label;
6655 p->x_cont_label = c_cont_label;
6656 p->x_switch_stack = c_switch_stack;
6657 p->arg_info = current_function_arg_info;
6658 p->returns_value = current_function_returns_value;
6659 p->returns_null = current_function_returns_null;
6660 p->returns_abnormally = current_function_returns_abnormally;
6661 p->warn_about_return_type = warn_about_return_type;
6662 p->extern_inline = current_extern_inline;
6665 /* Restore the variables used during compilation of a C function. */
6667 void
6668 c_pop_function_context (struct function *f)
6670 struct language_function *p = f->language;
6672 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6673 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6675 /* Stop pointing to the local nodes about to be freed. */
6676 /* But DECL_INITIAL must remain nonzero so we know this
6677 was an actual function definition. */
6678 DECL_INITIAL (current_function_decl) = error_mark_node;
6679 DECL_ARGUMENTS (current_function_decl) = 0;
6682 c_stmt_tree = p->base.x_stmt_tree;
6683 c_break_label = p->x_break_label;
6684 c_cont_label = p->x_cont_label;
6685 c_switch_stack = p->x_switch_stack;
6686 current_function_arg_info = p->arg_info;
6687 current_function_returns_value = p->returns_value;
6688 current_function_returns_null = p->returns_null;
6689 current_function_returns_abnormally = p->returns_abnormally;
6690 warn_about_return_type = p->warn_about_return_type;
6691 current_extern_inline = p->extern_inline;
6693 f->language = NULL;
6696 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6698 void
6699 c_dup_lang_specific_decl (tree decl)
6701 struct lang_decl *ld;
6703 if (!DECL_LANG_SPECIFIC (decl))
6704 return;
6706 ld = GGC_NEW (struct lang_decl);
6707 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6708 DECL_LANG_SPECIFIC (decl) = ld;
6711 /* The functions below are required for functionality of doing
6712 function at once processing in the C front end. Currently these
6713 functions are not called from anywhere in the C front end, but as
6714 these changes continue, that will change. */
6716 /* Returns nonzero if the current statement is a full expression,
6717 i.e. temporaries created during that statement should be destroyed
6718 at the end of the statement. */
6721 stmts_are_full_exprs_p (void)
6723 return 0;
6726 /* Returns the stmt_tree (if any) to which statements are currently
6727 being added. If there is no active statement-tree, NULL is
6728 returned. */
6730 stmt_tree
6731 current_stmt_tree (void)
6733 return &c_stmt_tree;
6736 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6737 C. */
6740 anon_aggr_type_p (tree ARG_UNUSED (node))
6742 return 0;
6745 /* Dummy function in place of callback used by C++. */
6747 void
6748 extract_interface_info (void)
6752 /* Return the global value of T as a symbol. */
6754 tree
6755 identifier_global_value (tree t)
6757 struct c_binding *b;
6759 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6760 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6761 return b->decl;
6763 return 0;
6766 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6767 otherwise the name is found in ridpointers from RID_INDEX. */
6769 void
6770 record_builtin_type (enum rid rid_index, const char *name, tree type)
6772 tree id, decl;
6773 if (name == 0)
6774 id = ridpointers[(int) rid_index];
6775 else
6776 id = get_identifier (name);
6777 decl = build_decl (TYPE_DECL, id, type);
6778 pushdecl (decl);
6779 if (debug_hooks->type_decl)
6780 debug_hooks->type_decl (decl, false);
6783 /* Build the void_list_node (void_type_node having been created). */
6784 tree
6785 build_void_list_node (void)
6787 tree t = build_tree_list (NULL_TREE, void_type_node);
6788 return t;
6791 /* Return a structure for a parameter with the given SPECS, ATTRS and
6792 DECLARATOR. */
6794 tree
6795 build_c_parm (tree specs, tree attrs, tree declarator)
6797 return build_tree_list (build_tree_list (specs, declarator), attrs);
6800 /* Return a declarator with nested attributes. TARGET is the inner
6801 declarator to which these attributes apply. ATTRS are the
6802 attributes. */
6804 tree
6805 build_attrs_declarator (tree attrs, tree target)
6807 return tree_cons (attrs, target, NULL_TREE);
6810 /* Return a declarator for a function with arguments specified by ARGS
6811 and return type specified by TARGET. */
6813 tree
6814 build_function_declarator (tree args, tree target)
6816 return build_nt (CALL_EXPR, target, args, NULL_TREE);
6819 /* Return something to represent absolute declarators containing a *.
6820 TARGET is the absolute declarator that the * contains.
6821 TYPE_QUALS_ATTRS is a list of modifiers such as const or volatile
6822 to apply to the pointer type, represented as identifiers, possible mixed
6823 with attributes.
6825 We return an INDIRECT_REF whose "contents" are TARGET (inside a TREE_LIST,
6826 if attributes are present) and whose type is the modifier list. */
6828 tree
6829 make_pointer_declarator (tree type_quals_attrs, tree target)
6831 tree quals, attrs;
6832 tree itarget = target;
6833 split_specs_attrs (type_quals_attrs, &quals, &attrs);
6834 if (attrs != NULL_TREE)
6835 itarget = build_attrs_declarator (attrs, target);
6836 return build1 (INDIRECT_REF, quals, itarget);
6839 /* Synthesize a function which calls all the global ctors or global
6840 dtors in this file. This is only used for targets which do not
6841 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
6842 static void
6843 build_cdtor (int method_type, tree cdtors)
6845 tree body = 0;
6847 if (!cdtors)
6848 return;
6850 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
6851 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
6852 &body);
6854 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
6857 /* Perform final processing on one file scope's declarations (or the
6858 external scope's declarations), GLOBALS. */
6859 static void
6860 c_write_global_declarations_1 (tree globals)
6862 size_t len = list_length (globals);
6863 tree *vec = XNEWVEC (tree, len);
6864 size_t i;
6865 tree decl;
6867 /* Process the decls in the order they were written. */
6868 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
6869 vec[i] = decl;
6871 wrapup_global_declarations (vec, len);
6872 check_global_declarations (vec, len);
6874 free (vec);
6877 void
6878 c_write_global_declarations (void)
6880 tree ext_block, t;
6882 /* We don't want to do this if generating a PCH. */
6883 if (pch_file)
6884 return;
6886 /* Don't waste time on further processing if -fsyntax-only or we've
6887 encountered errors. */
6888 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
6889 return;
6891 /* Close the external scope. */
6892 ext_block = pop_scope ();
6893 external_scope = 0;
6894 if (current_scope)
6895 abort ();
6897 /* Process all file scopes in this compilation, and the external_scope,
6898 through wrapup_global_declarations and check_global_declarations. */
6899 for (t = all_translation_units; t; t = TREE_CHAIN (t))
6900 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
6901 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
6903 /* Generate functions to call static constructors and destructors
6904 for targets that do not support .ctors/.dtors sections. These
6905 functions have magic names which are detected by collect2. */
6906 build_cdtor ('I', static_ctors); static_ctors = 0;
6907 build_cdtor ('D', static_dtors); static_dtors = 0;
6909 /* We're done parsing; proceed to optimize and emit assembly.
6910 FIXME: shouldn't be the front end's responsibility to call this. */
6911 cgraph_optimize ();
6913 /* Presently this has to happen after cgraph_optimize.
6914 FIXME: shouldn't be the front end's responsibility to call this. */
6915 if (flag_mudflap)
6916 mudflap_finish_file ();
6919 #include "gt-c-decl.h"