Mark ChangeLog
[official-gcc.git] / gcc / c-decl.c
blobf67c58b323551aef645bdca451972651f5a478d7
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, 2005 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 "input.h"
33 #include "tm.h"
34 #include "intl.h"
35 #include "tree.h"
36 #include "tree-inline.h"
37 #include "rtl.h"
38 #include "flags.h"
39 #include "function.h"
40 #include "output.h"
41 #include "expr.h"
42 #include "c-tree.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "tm_p.h"
46 #include "cpplib.h"
47 #include "target.h"
48 #include "debug.h"
49 #include "opts.h"
50 #include "timevar.h"
51 #include "c-common.h"
52 #include "c-pragma.h"
53 #include "langhooks.h"
54 #include "tree-mudflap.h"
55 #include "tree-gimple.h"
56 #include "diagnostic.h"
57 #include "tree-dump.h"
58 #include "cgraph.h"
59 #include "hashtab.h"
60 #include "libfuncs.h"
61 #include "except.h"
62 #include "langhooks-def.h"
64 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
65 enum decl_context
66 { NORMAL, /* Ordinary declaration */
67 FUNCDEF, /* Function definition */
68 PARM, /* Declaration of parm before function body */
69 FIELD, /* Declaration inside struct or union */
70 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* Nonzero if we have seen an invalid cross reference
74 to a struct, union, or enum, but not yet printed the message. */
75 tree pending_invalid_xref;
77 /* File and line to appear in the eventual error message. */
78 location_t pending_invalid_xref_location;
80 /* True means we've initialized exception handling. */
81 bool c_eh_initialized_p;
83 /* While defining an enum type, this is 1 plus the last enumerator
84 constant value. Note that will do not have to save this or `enum_overflow'
85 around nested function definition since such a definition could only
86 occur in an enum value expression and we don't use these variables in
87 that case. */
89 static tree enum_next_value;
91 /* Nonzero means that there was overflow computing enum_next_value. */
93 static int enum_overflow;
95 /* The file and line that the prototype came from if this is an
96 old-style definition; used for diagnostics in
97 store_parm_decls_oldstyle. */
99 static location_t current_function_prototype_locus;
101 /* Whether this prototype was built-in. */
103 static bool current_function_prototype_built_in;
105 /* The argument type information of this prototype. */
107 static tree current_function_prototype_arg_types;
109 /* The argument information structure for the function currently being
110 defined. */
112 static struct c_arg_info *current_function_arg_info;
114 /* The obstack on which parser and related data structures, which are
115 not live beyond their top-level declaration or definition, are
116 allocated. */
117 struct obstack parser_obstack;
119 /* The current statement tree. */
121 static GTY(()) struct stmt_tree_s c_stmt_tree;
123 /* State saving variables. */
124 tree c_break_label;
125 tree c_cont_label;
127 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
128 included in this invocation. Note that the current translation
129 unit is not included in this list. */
131 static GTY(()) tree all_translation_units;
133 /* A list of decls to be made automatically visible in each file scope. */
134 static GTY(()) tree visible_builtins;
136 /* Set to 0 at beginning of a function definition, set to 1 if
137 a return statement that specifies a return value is seen. */
139 int current_function_returns_value;
141 /* Set to 0 at beginning of a function definition, set to 1 if
142 a return statement with no argument is seen. */
144 int current_function_returns_null;
146 /* Set to 0 at beginning of a function definition, set to 1 if
147 a call to a noreturn function is seen. */
149 int current_function_returns_abnormally;
151 /* Set to nonzero by `grokdeclarator' for a function
152 whose return type is defaulted, if warnings for this are desired. */
154 static int warn_about_return_type;
156 /* Nonzero when starting a function declared `extern inline'. */
158 static int current_extern_inline;
160 /* Nonzero when the current toplevel function contains a declaration
161 of a nested function which is never defined. */
163 static bool undef_nested_function;
165 /* True means global_bindings_p should return false even if the scope stack
166 says we are in file scope. */
167 bool c_override_global_bindings_to_false;
170 /* Each c_binding structure describes one binding of an identifier to
171 a decl. All the decls in a scope - irrespective of namespace - are
172 chained together by the ->prev field, which (as the name implies)
173 runs in reverse order. All the decls in a given namespace bound to
174 a given identifier are chained by the ->shadowed field, which runs
175 from inner to outer scopes.
177 The ->decl field usually points to a DECL node, but there are two
178 exceptions. In the namespace of type tags, the bound entity is a
179 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
180 identifier is encountered, it is bound to error_mark_node to
181 suppress further errors about that identifier in the current
182 function.
184 The ->type field stores the type of the declaration in this scope;
185 if NULL, the type is the type of the ->decl field. This is only of
186 relevance for objects with external or internal linkage which may
187 be redeclared in inner scopes, forming composite types that only
188 persist for the duration of those scopes. In the external scope,
189 this stores the composite of all the types declared for this
190 object, visible or not. The ->inner_comp field (used only at file
191 scope) stores whether an incomplete array type at file scope was
192 completed at an inner scope to an array size other than 1.
194 The depth field is copied from the scope structure that holds this
195 decl. It is used to preserve the proper ordering of the ->shadowed
196 field (see bind()) and also for a handful of special-case checks.
197 Finally, the invisible bit is true for a decl which should be
198 ignored for purposes of normal name lookup, and the nested bit is
199 true for a decl that's been bound a second time in an inner scope;
200 in all such cases, the binding in the outer scope will have its
201 invisible bit true. */
203 struct c_binding GTY((chain_next ("%h.prev")))
205 tree decl; /* the decl bound */
206 tree type; /* the type in this scope */
207 tree id; /* the identifier it's bound to */
208 struct c_binding *prev; /* the previous decl in this scope */
209 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
210 unsigned int depth : 28; /* depth of this scope */
211 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
212 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
213 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
214 /* one free bit */
216 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
217 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
218 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
219 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
221 #define I_SYMBOL_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
223 #define I_SYMBOL_DECL(node) \
224 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
226 #define I_TAG_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
228 #define I_TAG_DECL(node) \
229 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
231 #define I_LABEL_BINDING(node) \
232 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
233 #define I_LABEL_DECL(node) \
234 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
236 /* Each C symbol points to three linked lists of c_binding structures.
237 These describe the values of the identifier in the three different
238 namespaces defined by the language. */
240 struct lang_identifier GTY(())
242 struct c_common_identifier common_id;
243 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
244 struct c_binding *tag_binding; /* struct/union/enum tags */
245 struct c_binding *label_binding; /* labels */
248 /* Validate c-lang.c's assumptions. */
249 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
250 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
252 /* The resulting tree type. */
254 union lang_tree_node
255 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
256 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)")))
258 union tree_node GTY ((tag ("0"),
259 desc ("tree_node_structure (&%h)")))
260 generic;
261 struct lang_identifier GTY ((tag ("1"))) identifier;
264 /* Each c_scope structure describes the complete contents of one
265 scope. Four scopes are distinguished specially: the innermost or
266 current scope, the innermost function scope, the file scope (always
267 the second to outermost) and the outermost or external scope.
269 Most declarations are recorded in the current scope.
271 All normal label declarations are recorded in the innermost
272 function scope, as are bindings of undeclared identifiers to
273 error_mark_node. (GCC permits nested functions as an extension,
274 hence the 'innermost' qualifier.) Explicitly declared labels
275 (using the __label__ extension) appear in the current scope.
277 Being in the file scope (current_scope == file_scope) causes
278 special behavior in several places below. Also, under some
279 conditions the Objective-C front end records declarations in the
280 file scope even though that isn't the current scope.
282 All declarations with external linkage are recorded in the external
283 scope, even if they aren't visible there; this models the fact that
284 such declarations are visible to the entire program, and (with a
285 bit of cleverness, see pushdecl) allows diagnosis of some violations
286 of C99 6.2.2p7 and 6.2.7p2:
288 If, within the same translation unit, the same identifier appears
289 with both internal and external linkage, the behavior is
290 undefined.
292 All declarations that refer to the same object or function shall
293 have compatible type; otherwise, the behavior is undefined.
295 Initially only the built-in declarations, which describe compiler
296 intrinsic functions plus a subset of the standard library, are in
297 this scope.
299 The order of the blocks list matters, and it is frequently appended
300 to. To avoid having to walk all the way to the end of the list on
301 each insertion, or reverse the list later, we maintain a pointer to
302 the last list entry. (FIXME: It should be feasible to use a reversed
303 list here.)
305 The bindings list is strictly in reverse order of declarations;
306 pop_scope relies on this. */
309 struct c_scope GTY((chain_next ("%h.outer")))
311 /* The scope containing this one. */
312 struct c_scope *outer;
314 /* The next outermost function scope. */
315 struct c_scope *outer_function;
317 /* All bindings in this scope. */
318 struct c_binding *bindings;
320 /* For each scope (except the global one), a chain of BLOCK nodes
321 for all the scopes that were entered and exited one level down. */
322 tree blocks;
323 tree blocks_last;
325 /* The depth of this scope. Used to keep the ->shadowed chain of
326 bindings sorted innermost to outermost. */
327 unsigned int depth : 28;
329 /* True if we are currently filling this scope with parameter
330 declarations. */
331 BOOL_BITFIELD parm_flag : 1;
333 /* True if we already complained about forward parameter decls
334 in this scope. This prevents double warnings on
335 foo (int a; int b; ...) */
336 BOOL_BITFIELD warned_forward_parm_decls : 1;
338 /* True if this is the outermost block scope of a function body.
339 This scope contains the parameters, the local variables declared
340 in the outermost block, and all the labels (except those in
341 nested functions, or declared at block scope with __label__). */
342 BOOL_BITFIELD function_body : 1;
344 /* True means make a BLOCK for this scope no matter what. */
345 BOOL_BITFIELD keep : 1;
348 /* The scope currently in effect. */
350 static GTY(()) struct c_scope *current_scope;
352 /* The innermost function scope. Ordinary (not explicitly declared)
353 labels, bindings to error_mark_node, and the lazily-created
354 bindings of __func__ and its friends get this scope. */
356 static GTY(()) struct c_scope *current_function_scope;
358 /* The C file scope. This is reset for each input translation unit. */
360 static GTY(()) struct c_scope *file_scope;
362 /* The outermost scope. This is used for all declarations with
363 external linkage, and only these, hence the name. */
365 static GTY(()) struct c_scope *external_scope;
367 /* A chain of c_scope structures awaiting reuse. */
369 static GTY((deletable)) struct c_scope *scope_freelist;
371 /* A chain of c_binding structures awaiting reuse. */
373 static GTY((deletable)) struct c_binding *binding_freelist;
375 /* Append VAR to LIST in scope SCOPE. */
376 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
377 struct c_scope *s_ = (scope); \
378 tree d_ = (decl); \
379 if (s_->list##_last) \
380 TREE_CHAIN (s_->list##_last) = d_; \
381 else \
382 s_->list = d_; \
383 s_->list##_last = d_; \
384 } while (0)
386 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
387 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
388 struct c_scope *t_ = (tscope); \
389 struct c_scope *f_ = (fscope); \
390 if (t_->to##_last) \
391 TREE_CHAIN (t_->to##_last) = f_->from; \
392 else \
393 t_->to = f_->from; \
394 t_->to##_last = f_->from##_last; \
395 } while (0)
397 /* True means unconditionally make a BLOCK for the next scope pushed. */
399 static bool keep_next_level_flag;
401 /* True means the next call to push_scope will be the outermost scope
402 of a function body, so do not push a new scope, merely cease
403 expecting parameter decls. */
405 static bool next_is_function_body;
407 /* Functions called automatically at the beginning and end of execution. */
409 static GTY(()) tree static_ctors;
410 static GTY(()) tree static_dtors;
412 /* Forward declarations. */
413 static tree lookup_name_in_scope (tree, struct c_scope *);
414 static tree c_make_fname_decl (tree, int);
415 static tree grokdeclarator (const struct c_declarator *,
416 struct c_declspecs *,
417 enum decl_context, bool, tree *);
418 static tree grokparms (struct c_arg_info *, bool);
419 static void layout_array_type (tree);
421 /* States indicating how grokdeclarator() should handle declspecs marked
422 with __attribute__((deprecated)). An object declared as
423 __attribute__((deprecated)) suppresses warnings of uses of other
424 deprecated items. */
426 enum deprecated_states {
427 DEPRECATED_NORMAL,
428 DEPRECATED_SUPPRESS
431 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
433 void
434 c_print_identifier (FILE *file, tree node, int indent)
436 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
437 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
438 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
439 if (C_IS_RESERVED_WORD (node))
441 tree rid = ridpointers[C_RID_CODE (node)];
442 indent_to (file, indent + 4);
443 fprintf (file, "rid " HOST_PTR_PRINTF " \"%s\"",
444 (void *) rid, IDENTIFIER_POINTER (rid));
448 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
449 which may be any of several kinds of DECL or TYPE or error_mark_node,
450 in the scope SCOPE. */
451 static void
452 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
454 struct c_binding *b, **here;
456 if (binding_freelist)
458 b = binding_freelist;
459 binding_freelist = b->prev;
461 else
462 b = GGC_NEW (struct c_binding);
464 b->shadowed = 0;
465 b->decl = decl;
466 b->id = name;
467 b->depth = scope->depth;
468 b->invisible = invisible;
469 b->nested = nested;
470 b->inner_comp = 0;
472 b->type = 0;
474 b->prev = scope->bindings;
475 scope->bindings = b;
477 if (!name)
478 return;
480 switch (TREE_CODE (decl))
482 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
483 case ENUMERAL_TYPE:
484 case UNION_TYPE:
485 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
486 case VAR_DECL:
487 case FUNCTION_DECL:
488 case TYPE_DECL:
489 case CONST_DECL:
490 case PARM_DECL:
491 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
493 default:
494 gcc_unreachable ();
497 /* Locate the appropriate place in the chain of shadowed decls
498 to insert this binding. Normally, scope == current_scope and
499 this does nothing. */
500 while (*here && (*here)->depth > scope->depth)
501 here = &(*here)->shadowed;
503 b->shadowed = *here;
504 *here = b;
507 /* Clear the binding structure B, stick it on the binding_freelist,
508 and return the former value of b->prev. This is used by pop_scope
509 and get_parm_info to iterate destructively over all the bindings
510 from a given scope. */
511 static struct c_binding *
512 free_binding_and_advance (struct c_binding *b)
514 struct c_binding *prev = b->prev;
516 memset (b, 0, sizeof (struct c_binding));
517 b->prev = binding_freelist;
518 binding_freelist = b;
520 return prev;
524 /* Hook called at end of compilation to assume 1 elt
525 for a file-scope tentative array defn that wasn't complete before. */
527 void
528 c_finish_incomplete_decl (tree decl)
530 if (TREE_CODE (decl) == VAR_DECL)
532 tree type = TREE_TYPE (decl);
533 if (type != error_mark_node
534 && TREE_CODE (type) == ARRAY_TYPE
535 && !DECL_EXTERNAL (decl)
536 && TYPE_DOMAIN (type) == 0)
538 warning ("%Jarray %qD assumed to have one element", decl, decl);
540 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
542 layout_decl (decl, 0);
547 /* The Objective-C front-end often needs to determine the current scope. */
549 void *
550 objc_get_current_scope (void)
552 return current_scope;
555 /* The following function is used only by Objective-C. It needs to live here
556 because it accesses the innards of c_scope. */
558 void
559 objc_mark_locals_volatile (void *enclosing_blk)
561 struct c_scope *scope;
562 struct c_binding *b;
564 for (scope = current_scope;
565 scope && scope != enclosing_blk;
566 scope = scope->outer)
568 for (b = scope->bindings; b; b = b->prev)
570 if (TREE_CODE (b->decl) == VAR_DECL
571 || TREE_CODE (b->decl) == PARM_DECL)
573 C_DECL_REGISTER (b->decl) = 0;
574 DECL_REGISTER (b->decl) = 0;
575 TREE_THIS_VOLATILE (b->decl) = 1;
579 /* Do not climb up past the current function. */
580 if (scope->function_body)
581 break;
585 /* Nonzero if we are currently in file scope. */
588 global_bindings_p (void)
590 return current_scope == file_scope && !c_override_global_bindings_to_false;
593 void
594 keep_next_level (void)
596 keep_next_level_flag = true;
599 /* Identify this scope as currently being filled with parameters. */
601 void
602 declare_parm_level (void)
604 current_scope->parm_flag = true;
607 void
608 push_scope (void)
610 if (next_is_function_body)
612 /* This is the transition from the parameters to the top level
613 of the function body. These are the same scope
614 (C99 6.2.1p4,6) so we do not push another scope structure.
615 next_is_function_body is set only by store_parm_decls, which
616 in turn is called when and only when we are about to
617 encounter the opening curly brace for the function body.
619 The outermost block of a function always gets a BLOCK node,
620 because the debugging output routines expect that each
621 function has at least one BLOCK. */
622 current_scope->parm_flag = false;
623 current_scope->function_body = true;
624 current_scope->keep = true;
625 current_scope->outer_function = current_function_scope;
626 current_function_scope = current_scope;
628 keep_next_level_flag = false;
629 next_is_function_body = false;
631 else
633 struct c_scope *scope;
634 if (scope_freelist)
636 scope = scope_freelist;
637 scope_freelist = scope->outer;
639 else
640 scope = GGC_CNEW (struct c_scope);
642 scope->keep = keep_next_level_flag;
643 scope->outer = current_scope;
644 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
646 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
647 possible. */
648 if (current_scope && scope->depth == 0)
650 scope->depth--;
651 sorry ("GCC supports only %u nested scopes", scope->depth);
654 current_scope = scope;
655 keep_next_level_flag = false;
659 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
661 static void
662 set_type_context (tree type, tree context)
664 for (type = TYPE_MAIN_VARIANT (type); type;
665 type = TYPE_NEXT_VARIANT (type))
666 TYPE_CONTEXT (type) = context;
669 /* Exit a scope. Restore the state of the identifier-decl mappings
670 that were in effect when this scope was entered. Return a BLOCK
671 node containing all the DECLs in this scope that are of interest
672 to debug info generation. */
674 tree
675 pop_scope (void)
677 struct c_scope *scope = current_scope;
678 tree block, context, p;
679 struct c_binding *b;
681 bool functionbody = scope->function_body;
682 bool keep = functionbody || scope->keep || scope->bindings;
684 c_end_vm_scope (scope->depth);
686 /* If appropriate, create a BLOCK to record the decls for the life
687 of this function. */
688 block = 0;
689 if (keep)
691 block = make_node (BLOCK);
692 BLOCK_SUBBLOCKS (block) = scope->blocks;
693 TREE_USED (block) = 1;
695 /* In each subblock, record that this is its superior. */
696 for (p = scope->blocks; p; p = TREE_CHAIN (p))
697 BLOCK_SUPERCONTEXT (p) = block;
699 BLOCK_VARS (block) = 0;
702 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
703 scope must be set so that they point to the appropriate
704 construct, i.e. either to the current FUNCTION_DECL node, or
705 else to the BLOCK node we just constructed.
707 Note that for tagged types whose scope is just the formal
708 parameter list for some function type specification, we can't
709 properly set their TYPE_CONTEXTs here, because we don't have a
710 pointer to the appropriate FUNCTION_TYPE node readily available
711 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
712 type nodes get set in `grokdeclarator' as soon as we have created
713 the FUNCTION_TYPE node which will represent the "scope" for these
714 "parameter list local" tagged types. */
715 if (scope->function_body)
716 context = current_function_decl;
717 else if (scope == file_scope)
719 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
720 TREE_CHAIN (file_decl) = all_translation_units;
721 all_translation_units = file_decl;
722 context = file_decl;
724 else
725 context = block;
727 /* Clear all bindings in this scope. */
728 for (b = scope->bindings; b; b = free_binding_and_advance (b))
730 p = b->decl;
731 switch (TREE_CODE (p))
733 case LABEL_DECL:
734 /* Warnings for unused labels, errors for undefined labels. */
735 if (TREE_USED (p) && !DECL_INITIAL (p))
737 error ("%Jlabel %qD used but not defined", p, p);
738 DECL_INITIAL (p) = error_mark_node;
740 else if (!TREE_USED (p) && warn_unused_label)
742 if (DECL_INITIAL (p))
743 warning ("%Jlabel %qD defined but not used", p, p);
744 else
745 warning ("%Jlabel %qD declared but not defined", p, p);
747 /* Labels go in BLOCK_VARS. */
748 TREE_CHAIN (p) = BLOCK_VARS (block);
749 BLOCK_VARS (block) = p;
750 gcc_assert (I_LABEL_BINDING (b->id) == b);
751 I_LABEL_BINDING (b->id) = b->shadowed;
752 break;
754 case ENUMERAL_TYPE:
755 case UNION_TYPE:
756 case RECORD_TYPE:
757 set_type_context (p, context);
759 /* Types may not have tag-names, in which case the type
760 appears in the bindings list with b->id NULL. */
761 if (b->id)
763 gcc_assert (I_TAG_BINDING (b->id) == b);
764 I_TAG_BINDING (b->id) = b->shadowed;
766 break;
768 case FUNCTION_DECL:
769 /* Propagate TREE_ADDRESSABLE from nested functions to their
770 containing functions. */
771 if (!TREE_ASM_WRITTEN (p)
772 && DECL_INITIAL (p) != 0
773 && TREE_ADDRESSABLE (p)
774 && DECL_ABSTRACT_ORIGIN (p) != 0
775 && DECL_ABSTRACT_ORIGIN (p) != p)
776 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
777 if (!DECL_EXTERNAL (p)
778 && DECL_INITIAL (p) == 0)
780 error ("%Jnested function %qD declared but never defined", p, p);
781 undef_nested_function = true;
783 goto common_symbol;
785 case VAR_DECL:
786 /* Warnings for unused variables. */
787 if (warn_unused_variable
788 && !TREE_USED (p)
789 && !DECL_IN_SYSTEM_HEADER (p)
790 && DECL_NAME (p)
791 && !DECL_ARTIFICIAL (p)
792 && scope != file_scope
793 && scope != external_scope)
794 warning ("%Junused variable %qD", p, p);
796 if (b->inner_comp)
798 error ("%Jtype of array %qD completed incompatibly with"
799 " implicit initialization", p, p);
802 /* Fall through. */
803 case TYPE_DECL:
804 case CONST_DECL:
805 common_symbol:
806 /* All of these go in BLOCK_VARS, but only if this is the
807 binding in the home scope. */
808 if (!b->nested)
810 TREE_CHAIN (p) = BLOCK_VARS (block);
811 BLOCK_VARS (block) = p;
813 /* If this is the file scope, and we are processing more
814 than one translation unit in this compilation, set
815 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
816 This makes same_translation_unit_p work, and causes
817 static declarations to be given disambiguating suffixes. */
818 if (scope == file_scope && num_in_fnames > 1)
820 DECL_CONTEXT (p) = context;
821 if (TREE_CODE (p) == TYPE_DECL)
822 set_type_context (TREE_TYPE (p), context);
825 /* Fall through. */
826 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
827 already been put there by store_parm_decls. Unused-
828 parameter warnings are handled by function.c.
829 error_mark_node obviously does not go in BLOCK_VARS and
830 does not get unused-variable warnings. */
831 case PARM_DECL:
832 case ERROR_MARK:
833 /* It is possible for a decl not to have a name. We get
834 here with b->id NULL in this case. */
835 if (b->id)
837 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
838 I_SYMBOL_BINDING (b->id) = b->shadowed;
839 if (b->shadowed && b->shadowed->type)
840 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
842 break;
844 default:
845 gcc_unreachable ();
850 /* Dispose of the block that we just made inside some higher level. */
851 if ((scope->function_body || scope == file_scope) && context)
853 DECL_INITIAL (context) = block;
854 BLOCK_SUPERCONTEXT (block) = context;
856 else if (scope->outer)
858 if (block)
859 SCOPE_LIST_APPEND (scope->outer, blocks, block);
860 /* If we did not make a block for the scope just exited, any
861 blocks made for inner scopes must be carried forward so they
862 will later become subblocks of something else. */
863 else if (scope->blocks)
864 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
867 /* Pop the current scope, and free the structure for reuse. */
868 current_scope = scope->outer;
869 if (scope->function_body)
870 current_function_scope = scope->outer_function;
872 memset (scope, 0, sizeof (struct c_scope));
873 scope->outer = scope_freelist;
874 scope_freelist = scope;
876 return block;
879 void
880 push_file_scope (void)
882 tree decl;
884 if (file_scope)
885 return;
887 push_scope ();
888 file_scope = current_scope;
890 start_fname_decls ();
892 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
893 bind (DECL_NAME (decl), decl, file_scope,
894 /*invisible=*/false, /*nested=*/true);
897 void
898 pop_file_scope (void)
900 /* In case there were missing closebraces, get us back to the global
901 binding level. */
902 while (current_scope != file_scope)
903 pop_scope ();
905 /* __FUNCTION__ is defined at file scope (""). This
906 call may not be necessary as my tests indicate it
907 still works without it. */
908 finish_fname_decls ();
910 /* This is the point to write out a PCH if we're doing that.
911 In that case we do not want to do anything else. */
912 if (pch_file)
914 c_common_write_pch ();
915 return;
918 /* Pop off the file scope and close this translation unit. */
919 pop_scope ();
920 file_scope = 0;
922 maybe_apply_pending_pragma_weaks ();
923 cgraph_finalize_compilation_unit ();
926 /* Insert BLOCK at the end of the list of subblocks of the current
927 scope. This is used when a BIND_EXPR is expanded, to handle the
928 BLOCK node inside the BIND_EXPR. */
930 void
931 insert_block (tree block)
933 TREE_USED (block) = 1;
934 SCOPE_LIST_APPEND (current_scope, blocks, block);
937 /* Push a definition or a declaration of struct, union or enum tag "name".
938 "type" should be the type node.
939 We assume that the tag "name" is not already defined.
941 Note that the definition may really be just a forward reference.
942 In that case, the TYPE_SIZE will be zero. */
944 static void
945 pushtag (tree name, tree type)
947 /* Record the identifier as the type's name if it has none. */
948 if (name && !TYPE_NAME (type))
949 TYPE_NAME (type) = name;
950 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
952 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
953 tagged type we just added to the current scope. This fake
954 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
955 to output a representation of a tagged type, and it also gives
956 us a convenient place to record the "scope start" address for the
957 tagged type. */
959 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
961 /* An approximation for now, so we can tell this is a function-scope tag.
962 This will be updated in pop_scope. */
963 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
966 /* Subroutine of compare_decls. Allow harmless mismatches in return
967 and argument types provided that the type modes match. This function
968 return a unified type given a suitable match, and 0 otherwise. */
970 static tree
971 match_builtin_function_types (tree newtype, tree oldtype)
973 tree newrettype, oldrettype;
974 tree newargs, oldargs;
975 tree trytype, tryargs;
977 /* Accept the return type of the new declaration if same modes. */
978 oldrettype = TREE_TYPE (oldtype);
979 newrettype = TREE_TYPE (newtype);
981 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
982 return 0;
984 oldargs = TYPE_ARG_TYPES (oldtype);
985 newargs = TYPE_ARG_TYPES (newtype);
986 tryargs = newargs;
988 while (oldargs || newargs)
990 if (!oldargs
991 || !newargs
992 || !TREE_VALUE (oldargs)
993 || !TREE_VALUE (newargs)
994 || TYPE_MODE (TREE_VALUE (oldargs))
995 != TYPE_MODE (TREE_VALUE (newargs)))
996 return 0;
998 oldargs = TREE_CHAIN (oldargs);
999 newargs = TREE_CHAIN (newargs);
1002 trytype = build_function_type (newrettype, tryargs);
1003 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1006 /* Subroutine of diagnose_mismatched_decls. Check for function type
1007 mismatch involving an empty arglist vs a nonempty one and give clearer
1008 diagnostics. */
1009 static void
1010 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1011 tree newtype, tree oldtype)
1013 tree t;
1015 if (TREE_CODE (olddecl) != FUNCTION_DECL
1016 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1017 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1019 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1020 return;
1022 t = TYPE_ARG_TYPES (oldtype);
1023 if (t == 0)
1024 t = TYPE_ARG_TYPES (newtype);
1025 for (; t; t = TREE_CHAIN (t))
1027 tree type = TREE_VALUE (t);
1029 if (TREE_CHAIN (t) == 0
1030 && TYPE_MAIN_VARIANT (type) != void_type_node)
1032 inform ("a parameter list with an ellipsis can%'t match "
1033 "an empty parameter name list declaration");
1034 break;
1037 if (c_type_promotes_to (type) != type)
1039 inform ("an argument type that has a default promotion can%'t match "
1040 "an empty parameter name list declaration");
1041 break;
1046 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1047 old-style function definition, NEWDECL is a prototype declaration.
1048 Diagnose inconsistencies in the argument list. Returns TRUE if
1049 the prototype is compatible, FALSE if not. */
1050 static bool
1051 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1053 tree newargs, oldargs;
1054 int i;
1056 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1058 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1059 newargs = TYPE_ARG_TYPES (newtype);
1060 i = 1;
1062 for (;;)
1064 tree oldargtype = TYPE_MAIN_VARIANT (TREE_VALUE (oldargs));
1065 tree newargtype = TYPE_MAIN_VARIANT (TREE_VALUE (newargs));
1067 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1068 break;
1070 /* Reaching the end of just one list means the two decls don't
1071 agree on the number of arguments. */
1072 if (END_OF_ARGLIST (oldargtype))
1074 error ("%Jprototype for %qD declares more arguments "
1075 "than previous old-style definition", newdecl, newdecl);
1076 return false;
1078 else if (END_OF_ARGLIST (newargtype))
1080 error ("%Jprototype for %qD declares fewer arguments "
1081 "than previous old-style definition", newdecl, newdecl);
1082 return false;
1085 /* Type for passing arg must be consistent with that declared
1086 for the arg. */
1087 else if (!comptypes (oldargtype, newargtype))
1089 error ("%Jprototype for %qD declares argument %d"
1090 " with incompatible type",
1091 newdecl, newdecl, i);
1092 return false;
1095 oldargs = TREE_CHAIN (oldargs);
1096 newargs = TREE_CHAIN (newargs);
1097 i++;
1100 /* If we get here, no errors were found, but do issue a warning
1101 for this poor-style construct. */
1102 warning ("%Jprototype for %qD follows non-prototype definition",
1103 newdecl, newdecl);
1104 return true;
1105 #undef END_OF_ARGLIST
1108 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1109 first in a pair of mismatched declarations, using the diagnostic
1110 function DIAG. */
1111 static void
1112 locate_old_decl (tree decl, void (*diag)(const char *, ...))
1114 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1116 else if (DECL_INITIAL (decl))
1117 diag (G_("%Jprevious definition of %qD was here"), decl, decl);
1118 else if (C_DECL_IMPLICIT (decl))
1119 diag (G_("%Jprevious implicit declaration of %qD was here"), decl, decl);
1120 else
1121 diag (G_("%Jprevious declaration of %qD was here"), decl, decl);
1124 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1125 Returns true if the caller should proceed to merge the two, false
1126 if OLDDECL should simply be discarded. As a side effect, issues
1127 all necessary diagnostics for invalid or poor-style combinations.
1128 If it returns true, writes the types of NEWDECL and OLDDECL to
1129 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1130 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1132 static bool
1133 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1134 tree *newtypep, tree *oldtypep)
1136 tree newtype, oldtype;
1137 bool pedwarned = false;
1138 bool warned = false;
1139 bool retval = true;
1141 /* If we have error_mark_node for either decl or type, just discard
1142 the previous decl - we're in an error cascade already. */
1143 if (olddecl == error_mark_node || newdecl == error_mark_node)
1144 return false;
1145 *oldtypep = oldtype = TREE_TYPE (olddecl);
1146 *newtypep = newtype = TREE_TYPE (newdecl);
1147 if (oldtype == error_mark_node || newtype == error_mark_node)
1148 return false;
1150 /* Two different categories of symbol altogether. This is an error
1151 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1152 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1154 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1155 && DECL_BUILT_IN (olddecl)
1156 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1158 error ("%J%qD redeclared as different kind of symbol",
1159 newdecl, newdecl);
1160 locate_old_decl (olddecl, error);
1162 else if (TREE_PUBLIC (newdecl))
1163 warning ("%Jbuilt-in function %qD declared as non-function",
1164 newdecl, newdecl);
1165 else if (warn_shadow)
1166 warning ("%Jdeclaration of %qD shadows a built-in function",
1167 newdecl, newdecl);
1168 return false;
1171 /* Enumerators have no linkage, so may only be declared once in a
1172 given scope. */
1173 if (TREE_CODE (olddecl) == CONST_DECL)
1175 error ("%Jredeclaration of enumerator %qD", newdecl, newdecl);
1176 locate_old_decl (olddecl, error);
1177 return false;
1180 if (!comptypes (oldtype, newtype))
1182 if (TREE_CODE (olddecl) == FUNCTION_DECL
1183 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1185 /* Accept harmless mismatch in function types.
1186 This is for the ffs and fprintf builtins. */
1187 tree trytype = match_builtin_function_types (newtype, oldtype);
1189 if (trytype && comptypes (newtype, trytype))
1190 *oldtypep = oldtype = trytype;
1191 else
1193 /* If types don't match for a built-in, throw away the
1194 built-in. No point in calling locate_old_decl here, it
1195 won't print anything. */
1196 warning ("%Jconflicting types for built-in function %qD",
1197 newdecl, newdecl);
1198 return false;
1201 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1202 && DECL_IS_BUILTIN (olddecl))
1204 /* A conflicting function declaration for a predeclared
1205 function that isn't actually built in. Objective C uses
1206 these. The new declaration silently overrides everything
1207 but the volatility (i.e. noreturn) indication. See also
1208 below. FIXME: Make Objective C use normal builtins. */
1209 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1210 return false;
1212 /* Permit void foo (...) to match int foo (...) if the latter is
1213 the definition and implicit int was used. See
1214 c-torture/compile/920625-2.c. */
1215 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1216 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1217 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1218 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1220 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1221 /* Make sure we keep void as the return type. */
1222 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1223 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1224 pedwarned = true;
1226 /* Permit void foo (...) to match an earlier call to foo (...) with
1227 no declared type (thus, implicitly int). */
1228 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1229 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1230 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1231 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1233 pedwarn ("%Jconflicting types for %qD", newdecl, newdecl);
1234 /* Make sure we keep void as the return type. */
1235 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1236 pedwarned = true;
1238 else
1240 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1241 error ("%J conflicting type qualifiers for %qD", newdecl, newdecl);
1242 else
1243 error ("%Jconflicting types for %qD", newdecl, newdecl);
1244 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1245 locate_old_decl (olddecl, error);
1246 return false;
1250 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1251 but silently ignore the redeclaration if either is in a system
1252 header. (Conflicting redeclarations were handled above.) */
1253 if (TREE_CODE (newdecl) == TYPE_DECL)
1255 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1256 return true; /* Allow OLDDECL to continue in use. */
1258 error ("%Jredefinition of typedef %qD", newdecl, newdecl);
1259 locate_old_decl (olddecl, error);
1260 return false;
1263 /* Function declarations can either be 'static' or 'extern' (no
1264 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1265 can never conflict with each other on account of linkage (6.2.2p4).
1266 Multiple definitions are not allowed (6.9p3,5) but GCC permits
1267 two definitions if one is 'extern inline' and one is not. The non-
1268 extern-inline definition supersedes the extern-inline definition. */
1269 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1271 /* If you declare a built-in function name as static, or
1272 define the built-in with an old-style definition (so we
1273 can't validate the argument list) the built-in definition is
1274 overridden, but optionally warn this was a bad choice of name. */
1275 if (DECL_BUILT_IN (olddecl)
1276 && !C_DECL_DECLARED_BUILTIN (olddecl)
1277 && (!TREE_PUBLIC (newdecl)
1278 || (DECL_INITIAL (newdecl)
1279 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1281 if (warn_shadow)
1282 warning ("%Jdeclaration of %qD shadows a built-in function",
1283 newdecl, newdecl);
1284 /* Discard the old built-in function. */
1285 return false;
1288 if (DECL_INITIAL (newdecl))
1290 if (DECL_INITIAL (olddecl))
1292 /* If both decls have extern inline and are in the same TU,
1293 reject the new decl. */
1294 if (DECL_DECLARED_INLINE_P (olddecl)
1295 && DECL_EXTERNAL (olddecl)
1296 && DECL_DECLARED_INLINE_P (newdecl)
1297 && DECL_EXTERNAL (newdecl)
1298 && same_translation_unit_p (newdecl, olddecl))
1300 error ("%Jredefinition of %qD", newdecl, newdecl);
1301 locate_old_decl (olddecl, error);
1302 return false;
1304 /* If both decls have not extern inline, reject the new decl. */
1305 if (!(DECL_DECLARED_INLINE_P (olddecl)
1306 && DECL_EXTERNAL (olddecl))
1307 && !(DECL_DECLARED_INLINE_P (newdecl)
1308 && DECL_EXTERNAL (newdecl)))
1310 error ("%Jredefinition of %qD", newdecl, newdecl);
1311 locate_old_decl (olddecl, error);
1312 return false;
1314 /* If the new decl is declared as extern inline, error if they are
1315 in the same TU, otherwise retain the old decl. */
1316 if (!DECL_DECLARED_INLINE_P (olddecl)
1317 && !DECL_EXTERNAL (olddecl)
1318 && DECL_DECLARED_INLINE_P (newdecl)
1319 && DECL_EXTERNAL (newdecl))
1321 if (same_translation_unit_p (newdecl, olddecl))
1323 error ("%Jredefinition of %qD", newdecl, newdecl);
1324 locate_old_decl (olddecl, error);
1325 return false;
1327 else
1328 retval = false;
1332 /* If we have a prototype after an old-style function definition,
1333 the argument types must be checked specially. */
1334 else if (DECL_INITIAL (olddecl)
1335 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1336 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1337 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1339 locate_old_decl (olddecl, error);
1340 return false;
1342 /* A non-static declaration (even an "extern") followed by a
1343 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1344 The same is true for a static forward declaration at block
1345 scope followed by a non-static declaration/definition at file
1346 scope. Static followed by non-static at the same scope is
1347 not undefined behavior, and is the most convenient way to get
1348 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1349 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1350 we do diagnose it if -Wtraditional. */
1351 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1353 /* Two exceptions to the rule. If olddecl is an extern
1354 inline, or a predeclared function that isn't actually
1355 built in, newdecl silently overrides olddecl. The latter
1356 occur only in Objective C; see also above. (FIXME: Make
1357 Objective C use normal builtins.) */
1358 if (!DECL_IS_BUILTIN (olddecl)
1359 && !(DECL_EXTERNAL (olddecl)
1360 && DECL_DECLARED_INLINE_P (olddecl)))
1362 error ("%Jstatic declaration of %qD follows "
1363 "non-static declaration", newdecl, newdecl);
1364 locate_old_decl (olddecl, error);
1366 return false;
1368 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1370 if (DECL_CONTEXT (olddecl))
1372 error ("%Jnon-static declaration of %qD follows "
1373 "static declaration", newdecl, newdecl);
1374 locate_old_decl (olddecl, error);
1375 return false;
1377 else if (warn_traditional)
1379 warning ("%Jnon-static declaration of %qD follows "
1380 "static declaration", newdecl, newdecl);
1381 warned = true;
1385 else if (TREE_CODE (newdecl) == VAR_DECL)
1387 /* Only variables can be thread-local, and all declarations must
1388 agree on this property. */
1389 if (DECL_THREAD_LOCAL (newdecl) != DECL_THREAD_LOCAL (olddecl))
1391 if (DECL_THREAD_LOCAL (newdecl))
1392 error ("%Jthread-local declaration of %qD follows "
1393 "non-thread-local declaration", newdecl, newdecl);
1394 else
1395 error ("%Jnon-thread-local declaration of %qD follows "
1396 "thread-local declaration", newdecl, newdecl);
1398 locate_old_decl (olddecl, error);
1399 return false;
1402 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1403 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1405 error ("%Jredefinition of %qD", newdecl, newdecl);
1406 locate_old_decl (olddecl, error);
1407 return false;
1410 /* Objects declared at file scope: if the first declaration had
1411 external linkage (even if it was an external reference) the
1412 second must have external linkage as well, or the behavior is
1413 undefined. If the first declaration had internal linkage, then
1414 the second must too, or else be an external reference (in which
1415 case the composite declaration still has internal linkage).
1416 As for function declarations, we warn about the static-then-
1417 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1418 if (DECL_FILE_SCOPE_P (newdecl)
1419 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1421 if (DECL_EXTERNAL (newdecl))
1423 if (!DECL_FILE_SCOPE_P (olddecl))
1425 error ("%Jextern declaration of %qD follows "
1426 "declaration with no linkage", newdecl, newdecl);
1427 locate_old_decl (olddecl, error);
1428 return false;
1430 else if (warn_traditional)
1432 warning ("%Jnon-static declaration of %qD follows "
1433 "static declaration", newdecl, newdecl);
1434 warned = true;
1437 else
1439 if (TREE_PUBLIC (newdecl))
1440 error ("%Jnon-static declaration of %qD follows "
1441 "static declaration", newdecl, newdecl);
1442 else
1443 error ("%Jstatic declaration of %qD follows "
1444 "non-static declaration", newdecl, newdecl);
1446 locate_old_decl (olddecl, error);
1447 return false;
1450 /* Two objects with the same name declared at the same block
1451 scope must both be external references (6.7p3). */
1452 else if (!DECL_FILE_SCOPE_P (newdecl))
1454 if (DECL_EXTERNAL (newdecl))
1456 /* Extern with initializer at block scope, which will
1457 already have received an error. */
1459 else if (DECL_EXTERNAL (olddecl))
1461 error ("%Jdeclaration of %qD with no linkage follows "
1462 "extern declaration", newdecl, newdecl);
1463 locate_old_decl (olddecl, error);
1465 else
1467 error ("%Jredeclaration of %qD with no linkage",
1468 newdecl, newdecl);
1469 locate_old_decl (olddecl, error);
1472 return false;
1476 /* warnings */
1477 /* All decls must agree on a visibility. */
1478 if (DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1479 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1481 warning ("%Jredeclaration of %qD with different visibility "
1482 "(old visibility preserved)", newdecl, newdecl);
1483 warned = true;
1486 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1488 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1489 if (DECL_DECLARED_INLINE_P (newdecl)
1490 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1492 warning ("%Jinline declaration of %qD follows "
1493 "declaration with attribute noinline", newdecl, newdecl);
1494 warned = true;
1496 else if (DECL_DECLARED_INLINE_P (olddecl)
1497 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1499 warning ("%Jdeclaration of %qD with attribute noinline follows "
1500 "inline declaration ", newdecl, newdecl);
1501 warned = true;
1504 /* Inline declaration after use or definition.
1505 ??? Should we still warn about this now we have unit-at-a-time
1506 mode and can get it right?
1507 Definitely don't complain if the decls are in different translation
1508 units. */
1509 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1510 && same_translation_unit_p (olddecl, newdecl))
1512 if (TREE_USED (olddecl))
1514 warning ("%J%qD declared inline after being called",
1515 olddecl, olddecl);
1516 warned = true;
1518 else if (DECL_INITIAL (olddecl))
1520 warning ("%J%qD declared inline after its definition",
1521 olddecl, olddecl);
1522 warned = true;
1526 else /* PARM_DECL, VAR_DECL */
1528 /* Redeclaration of a parameter is a constraint violation (this is
1529 not explicitly stated, but follows from C99 6.7p3 [no more than
1530 one declaration of the same identifier with no linkage in the
1531 same scope, except type tags] and 6.2.2p6 [parameters have no
1532 linkage]). We must check for a forward parameter declaration,
1533 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1534 an extension, the mandatory diagnostic for which is handled by
1535 mark_forward_parm_decls. */
1537 if (TREE_CODE (newdecl) == PARM_DECL
1538 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1540 error ("%Jredefinition of parameter %qD", newdecl, newdecl);
1541 locate_old_decl (olddecl, error);
1542 return false;
1546 /* Optional warning for completely redundant decls. */
1547 if (!warned && !pedwarned
1548 && warn_redundant_decls
1549 /* Don't warn about a function declaration followed by a
1550 definition. */
1551 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1552 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1553 /* Don't warn about redundant redeclarations of builtins. */
1554 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1555 && !DECL_BUILT_IN (newdecl)
1556 && DECL_BUILT_IN (olddecl)
1557 && !C_DECL_DECLARED_BUILTIN (olddecl))
1558 /* Don't warn about an extern followed by a definition. */
1559 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1560 /* Don't warn about forward parameter decls. */
1561 && !(TREE_CODE (newdecl) == PARM_DECL
1562 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1563 /* Don't warn about a variable definition following a declaration. */
1564 && !(TREE_CODE (newdecl) == VAR_DECL
1565 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1567 warning ("%Jredundant redeclaration of %qD", newdecl, newdecl);
1568 warned = true;
1571 /* Report location of previous decl/defn in a consistent manner. */
1572 if (warned || pedwarned)
1573 locate_old_decl (olddecl, pedwarned ? pedwarn : warning);
1575 return retval;
1578 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1579 consistent with OLDDECL, but carries new information. Merge the
1580 new information into OLDDECL. This function issues no
1581 diagnostics. */
1583 static void
1584 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1586 int new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1587 && DECL_INITIAL (newdecl) != 0);
1588 int new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1589 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1590 int old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1591 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1593 /* For real parm decl following a forward decl, rechain the old decl
1594 in its new location and clear TREE_ASM_WRITTEN (it's not a
1595 forward decl anymore). */
1596 if (TREE_CODE (newdecl) == PARM_DECL
1597 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1599 struct c_binding *b, **here;
1601 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1602 if ((*here)->decl == olddecl)
1603 goto found;
1604 gcc_unreachable ();
1606 found:
1607 b = *here;
1608 *here = b->prev;
1609 b->prev = current_scope->bindings;
1610 current_scope->bindings = b;
1612 TREE_ASM_WRITTEN (olddecl) = 0;
1615 DECL_ATTRIBUTES (newdecl)
1616 = targetm.merge_decl_attributes (olddecl, newdecl);
1618 /* Merge the data types specified in the two decls. */
1619 TREE_TYPE (newdecl)
1620 = TREE_TYPE (olddecl)
1621 = composite_type (newtype, oldtype);
1623 /* Lay the type out, unless already done. */
1624 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1626 if (TREE_TYPE (newdecl) != error_mark_node)
1627 layout_type (TREE_TYPE (newdecl));
1628 if (TREE_CODE (newdecl) != FUNCTION_DECL
1629 && TREE_CODE (newdecl) != TYPE_DECL
1630 && TREE_CODE (newdecl) != CONST_DECL)
1631 layout_decl (newdecl, 0);
1633 else
1635 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1636 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1637 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1638 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1639 if (TREE_CODE (olddecl) != FUNCTION_DECL)
1640 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1642 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1643 DECL_USER_ALIGN (newdecl) |= DECL_ALIGN (olddecl);
1647 /* Keep the old rtl since we can safely use it. */
1648 COPY_DECL_RTL (olddecl, newdecl);
1650 /* Merge the type qualifiers. */
1651 if (TREE_READONLY (newdecl))
1652 TREE_READONLY (olddecl) = 1;
1654 if (TREE_THIS_VOLATILE (newdecl))
1656 TREE_THIS_VOLATILE (olddecl) = 1;
1657 if (TREE_CODE (newdecl) == VAR_DECL)
1658 make_var_volatile (newdecl);
1661 /* Merge deprecatedness. */
1662 if (TREE_DEPRECATED (newdecl))
1663 TREE_DEPRECATED (olddecl) = 1;
1665 /* Keep source location of definition rather than declaration and of
1666 prototype rather than non-prototype unless that prototype is
1667 built-in. */
1668 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1669 || (old_is_prototype && !new_is_prototype
1670 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1671 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1673 /* Merge the unused-warning information. */
1674 if (DECL_IN_SYSTEM_HEADER (olddecl))
1675 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1676 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1677 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1679 /* Merge the initialization information. */
1680 if (DECL_INITIAL (newdecl) == 0)
1681 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1683 /* Merge the section attribute.
1684 We want to issue an error if the sections conflict but that must be
1685 done later in decl_attributes since we are called before attributes
1686 are assigned. */
1687 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1688 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1690 /* Copy the assembler name.
1691 Currently, it can only be defined in the prototype. */
1692 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1694 /* Use visibility of whichever declaration had it specified */
1695 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1697 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1698 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1701 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1703 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1704 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1705 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1706 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1707 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1708 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1709 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1710 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1711 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1714 /* Merge the storage class information. */
1715 merge_weak (newdecl, olddecl);
1717 /* For functions, static overrides non-static. */
1718 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1720 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1721 /* This is since we don't automatically
1722 copy the attributes of NEWDECL into OLDDECL. */
1723 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1724 /* If this clears `static', clear it in the identifier too. */
1725 if (!TREE_PUBLIC (olddecl))
1726 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1728 if (DECL_EXTERNAL (newdecl))
1730 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1731 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1733 /* An extern decl does not override previous storage class. */
1734 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1735 if (!DECL_EXTERNAL (newdecl))
1737 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1738 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1741 else
1743 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1744 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1747 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1749 /* If we're redefining a function previously defined as extern
1750 inline, make sure we emit debug info for the inline before we
1751 throw it away, in case it was inlined into a function that hasn't
1752 been written out yet. */
1753 if (new_is_definition && DECL_INITIAL (olddecl))
1755 if (TREE_USED (olddecl)
1756 /* In unit-at-a-time mode we never inline re-defined extern
1757 inline functions. */
1758 && !flag_unit_at_a_time
1759 && cgraph_function_possibly_inlined_p (olddecl))
1760 (*debug_hooks->outlining_inline_function) (olddecl);
1762 /* The new defn must not be inline. */
1763 DECL_INLINE (newdecl) = 0;
1764 DECL_UNINLINABLE (newdecl) = 1;
1766 else
1768 /* If either decl says `inline', this fn is inline,
1769 unless its definition was passed already. */
1770 if (DECL_DECLARED_INLINE_P (newdecl)
1771 || DECL_DECLARED_INLINE_P (olddecl))
1772 DECL_DECLARED_INLINE_P (newdecl) = 1;
1774 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1775 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1778 if (DECL_BUILT_IN (olddecl))
1780 /* If redeclaring a builtin function, it stays built in.
1781 But it gets tagged as having been declared. */
1782 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1783 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1784 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1785 if (new_is_prototype)
1786 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1787 else
1788 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1789 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1792 /* Also preserve various other info from the definition. */
1793 if (!new_is_definition)
1795 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1796 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1797 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1798 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1799 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1801 /* Set DECL_INLINE on the declaration if we've got a body
1802 from which to instantiate. */
1803 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1805 DECL_INLINE (newdecl) = 1;
1806 DECL_ABSTRACT_ORIGIN (newdecl)
1807 = DECL_ABSTRACT_ORIGIN (olddecl);
1810 else
1812 /* If a previous declaration said inline, mark the
1813 definition as inlinable. */
1814 if (DECL_DECLARED_INLINE_P (newdecl)
1815 && !DECL_UNINLINABLE (newdecl))
1816 DECL_INLINE (newdecl) = 1;
1820 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1821 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1823 unsigned olddecl_uid = DECL_UID (olddecl);
1824 tree olddecl_context = DECL_CONTEXT (olddecl);
1826 memcpy ((char *) olddecl + sizeof (struct tree_common),
1827 (char *) newdecl + sizeof (struct tree_common),
1828 sizeof (struct tree_decl) - sizeof (struct tree_common));
1829 DECL_UID (olddecl) = olddecl_uid;
1830 DECL_CONTEXT (olddecl) = olddecl_context;
1833 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1834 so that encode_section_info has a chance to look at the new decl
1835 flags and attributes. */
1836 if (DECL_RTL_SET_P (olddecl)
1837 && (TREE_CODE (olddecl) == FUNCTION_DECL
1838 || (TREE_CODE (olddecl) == VAR_DECL
1839 && TREE_STATIC (olddecl))))
1840 make_decl_rtl (olddecl);
1843 /* Handle when a new declaration NEWDECL has the same name as an old
1844 one OLDDECL in the same binding contour. Prints an error message
1845 if appropriate.
1847 If safely possible, alter OLDDECL to look like NEWDECL, and return
1848 true. Otherwise, return false. */
1850 static bool
1851 duplicate_decls (tree newdecl, tree olddecl)
1853 tree newtype = NULL, oldtype = NULL;
1855 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1856 return false;
1858 merge_decls (newdecl, olddecl, newtype, oldtype);
1859 return true;
1863 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1864 static void
1865 warn_if_shadowing (tree new_decl)
1867 struct c_binding *b;
1869 /* Shadow warnings wanted? */
1870 if (!warn_shadow
1871 /* No shadow warnings for internally generated vars. */
1872 || DECL_IS_BUILTIN (new_decl)
1873 /* No shadow warnings for vars made for inlining. */
1874 || DECL_FROM_INLINE (new_decl)
1875 /* Don't warn about the parm names in function declarator
1876 within a function declarator. It would be nice to avoid
1877 warning in any function declarator in a declaration, as
1878 opposed to a definition, but there is no way to tell
1879 it's not a definition at this point. */
1880 || (TREE_CODE (new_decl) == PARM_DECL && current_scope->outer->parm_flag))
1881 return;
1883 /* Is anything being shadowed? Invisible decls do not count. */
1884 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1885 if (b->decl && b->decl != new_decl && !b->invisible)
1887 tree old_decl = b->decl;
1889 if (old_decl == error_mark_node)
1891 warning ("%Jdeclaration of %qD shadows previous non-variable",
1892 new_decl, new_decl);
1893 break;
1895 else if (TREE_CODE (old_decl) == PARM_DECL)
1896 warning ("%Jdeclaration of %qD shadows a parameter",
1897 new_decl, new_decl);
1898 else if (DECL_FILE_SCOPE_P (old_decl))
1899 warning ("%Jdeclaration of %qD shadows a global declaration",
1900 new_decl, new_decl);
1901 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1902 && DECL_BUILT_IN (old_decl))
1904 warning ("%Jdeclaration of %qD shadows a built-in function",
1905 new_decl, new_decl);
1906 break;
1908 else
1909 warning ("%Jdeclaration of %qD shadows a previous local",
1910 new_decl, new_decl);
1912 warning ("%Jshadowed declaration is here", old_decl);
1914 break;
1919 /* Subroutine of pushdecl.
1921 X is a TYPE_DECL for a typedef statement. Create a brand new
1922 ..._TYPE node (which will be just a variant of the existing
1923 ..._TYPE node with identical properties) and then install X
1924 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
1926 The whole point here is to end up with a situation where each
1927 and every ..._TYPE node the compiler creates will be uniquely
1928 associated with AT MOST one node representing a typedef name.
1929 This way, even though the compiler substitutes corresponding
1930 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
1931 early on, later parts of the compiler can always do the reverse
1932 translation and get back the corresponding typedef name. For
1933 example, given:
1935 typedef struct S MY_TYPE;
1936 MY_TYPE object;
1938 Later parts of the compiler might only know that `object' was of
1939 type `struct S' if it were not for code just below. With this
1940 code however, later parts of the compiler see something like:
1942 struct S' == struct S
1943 typedef struct S' MY_TYPE;
1944 struct S' object;
1946 And they can then deduce (from the node for type struct S') that
1947 the original object declaration was:
1949 MY_TYPE object;
1951 Being able to do this is important for proper support of protoize,
1952 and also for generating precise symbolic debugging information
1953 which takes full account of the programmer's (typedef) vocabulary.
1955 Obviously, we don't want to generate a duplicate ..._TYPE node if
1956 the TYPE_DECL node that we are now processing really represents a
1957 standard built-in type.
1959 Since all standard types are effectively declared at line zero
1960 in the source file, we can easily check to see if we are working
1961 on a standard type by checking the current value of lineno. */
1963 static void
1964 clone_underlying_type (tree x)
1966 if (DECL_IS_BUILTIN (x))
1968 if (TYPE_NAME (TREE_TYPE (x)) == 0)
1969 TYPE_NAME (TREE_TYPE (x)) = x;
1971 else if (TREE_TYPE (x) != error_mark_node
1972 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
1974 tree tt = TREE_TYPE (x);
1975 DECL_ORIGINAL_TYPE (x) = tt;
1976 tt = build_variant_type_copy (tt);
1977 TYPE_NAME (tt) = x;
1978 TREE_USED (tt) = TREE_USED (x);
1979 TREE_TYPE (x) = tt;
1983 /* Record a decl-node X as belonging to the current lexical scope.
1984 Check for errors (such as an incompatible declaration for the same
1985 name already seen in the same scope).
1987 Returns either X or an old decl for the same name.
1988 If an old decl is returned, it may have been smashed
1989 to agree with what X says. */
1991 tree
1992 pushdecl (tree x)
1994 tree name = DECL_NAME (x);
1995 struct c_scope *scope = current_scope;
1996 struct c_binding *b;
1997 bool nested = false;
1999 /* Functions need the lang_decl data. */
2000 if (TREE_CODE (x) == FUNCTION_DECL && !DECL_LANG_SPECIFIC (x))
2001 DECL_LANG_SPECIFIC (x) = GGC_CNEW (struct lang_decl);
2003 /* Must set DECL_CONTEXT for everything not at file scope or
2004 DECL_FILE_SCOPE_P won't work. Local externs don't count
2005 unless they have initializers (which generate code). */
2006 if (current_function_decl
2007 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2008 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2009 DECL_CONTEXT (x) = current_function_decl;
2011 /* If this is of variably modified type, prevent jumping into its
2012 scope. */
2013 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2014 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2015 c_begin_vm_scope (scope->depth);
2017 /* Anonymous decls are just inserted in the scope. */
2018 if (!name)
2020 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2021 return x;
2024 /* First, see if there is another declaration with the same name in
2025 the current scope. If there is, duplicate_decls may do all the
2026 work for us. If duplicate_decls returns false, that indicates
2027 two incompatible decls in the same scope; we are to silently
2028 replace the old one (duplicate_decls has issued all appropriate
2029 diagnostics). In particular, we should not consider possible
2030 duplicates in the external scope, or shadowing. */
2031 b = I_SYMBOL_BINDING (name);
2032 if (b && B_IN_SCOPE (b, scope))
2034 struct c_binding *b_ext, *b_use;
2035 tree type = TREE_TYPE (x);
2036 tree visdecl = b->decl;
2037 tree vistype = TREE_TYPE (visdecl);
2038 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2039 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2040 b->inner_comp = false;
2041 b_use = b;
2042 b_ext = b;
2043 /* If this is an external linkage declaration, we should check
2044 for compatibility with the type in the external scope before
2045 setting the type at this scope based on the visible
2046 information only. */
2047 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2049 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2050 b_ext = b_ext->shadowed;
2051 if (b_ext)
2053 b_use = b_ext;
2054 if (b_use->type)
2055 TREE_TYPE (b_use->decl) = b_use->type;
2058 if (duplicate_decls (x, b_use->decl))
2060 if (b_use != b)
2062 /* Save the updated type in the external scope and
2063 restore the proper type for this scope. */
2064 tree thistype;
2065 if (comptypes (vistype, type))
2066 thistype = composite_type (vistype, type);
2067 else
2068 thistype = TREE_TYPE (b_use->decl);
2069 b_use->type = TREE_TYPE (b_use->decl);
2070 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2071 && DECL_BUILT_IN (b_use->decl))
2072 thistype
2073 = build_type_attribute_variant (thistype,
2074 TYPE_ATTRIBUTES
2075 (b_use->type));
2076 TREE_TYPE (b_use->decl) = thistype;
2078 return b_use->decl;
2080 else
2081 goto skip_external_and_shadow_checks;
2084 /* All declarations with external linkage, and all external
2085 references, go in the external scope, no matter what scope is
2086 current. However, the binding in that scope is ignored for
2087 purposes of normal name lookup. A separate binding structure is
2088 created in the requested scope; this governs the normal
2089 visibility of the symbol.
2091 The binding in the externals scope is used exclusively for
2092 detecting duplicate declarations of the same object, no matter
2093 what scope they are in; this is what we do here. (C99 6.2.7p2:
2094 All declarations that refer to the same object or function shall
2095 have compatible type; otherwise, the behavior is undefined.) */
2096 if (DECL_EXTERNAL (x) || scope == file_scope)
2098 tree type = TREE_TYPE (x);
2099 tree vistype = 0;
2100 tree visdecl = 0;
2101 bool type_saved = false;
2102 if (b && !B_IN_EXTERNAL_SCOPE (b)
2103 && (TREE_CODE (b->decl) == FUNCTION_DECL
2104 || TREE_CODE (b->decl) == VAR_DECL)
2105 && DECL_FILE_SCOPE_P (b->decl))
2107 visdecl = b->decl;
2108 vistype = TREE_TYPE (visdecl);
2110 if (warn_nested_externs
2111 && scope != file_scope
2112 && !DECL_IN_SYSTEM_HEADER (x))
2113 warning ("nested extern declaration of %qD", x);
2115 while (b && !B_IN_EXTERNAL_SCOPE (b))
2117 /* If this decl might be modified, save its type. This is
2118 done here rather than when the decl is first bound
2119 because the type may change after first binding, through
2120 being completed or through attributes being added. If we
2121 encounter multiple such decls, only the first should have
2122 its type saved; the others will already have had their
2123 proper types saved and the types will not have changed as
2124 their scopes will not have been re-entered. */
2125 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2127 b->type = TREE_TYPE (b->decl);
2128 type_saved = true;
2130 if (B_IN_FILE_SCOPE (b)
2131 && TREE_CODE (b->decl) == VAR_DECL
2132 && TREE_STATIC (b->decl)
2133 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2134 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2135 && TREE_CODE (type) == ARRAY_TYPE
2136 && TYPE_DOMAIN (type)
2137 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2138 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2140 /* Array type completed in inner scope, which should be
2141 diagnosed if the completion does not have size 1 and
2142 it does not get completed in the file scope. */
2143 b->inner_comp = true;
2145 b = b->shadowed;
2148 /* If a matching external declaration has been found, set its
2149 type to the composite of all the types of that declaration.
2150 After the consistency checks, it will be reset to the
2151 composite of the visible types only. */
2152 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2153 && b->type)
2154 TREE_TYPE (b->decl) = b->type;
2156 /* The point of the same_translation_unit_p check here is,
2157 we want to detect a duplicate decl for a construct like
2158 foo() { extern bar(); } ... static bar(); but not if
2159 they are in different translation units. In any case,
2160 the static does not go in the externals scope. */
2161 if (b
2162 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2163 && duplicate_decls (x, b->decl))
2165 tree thistype;
2166 if (vistype)
2168 if (comptypes (vistype, type))
2169 thistype = composite_type (vistype, type);
2170 else
2171 thistype = TREE_TYPE (b->decl);
2173 else
2174 thistype = type;
2175 b->type = TREE_TYPE (b->decl);
2176 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2177 thistype
2178 = build_type_attribute_variant (thistype,
2179 TYPE_ATTRIBUTES (b->type));
2180 TREE_TYPE (b->decl) = thistype;
2181 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2182 return b->decl;
2184 else if (TREE_PUBLIC (x))
2186 if (visdecl && !b && duplicate_decls (x, visdecl))
2188 /* An external declaration at block scope referring to a
2189 visible entity with internal linkage. The composite
2190 type will already be correct for this scope, so we
2191 just need to fall through to make the declaration in
2192 this scope. */
2193 nested = true;
2194 x = visdecl;
2196 else
2198 bind (name, x, external_scope, /*invisible=*/true,
2199 /*nested=*/false);
2200 nested = true;
2205 warn_if_shadowing (x);
2207 skip_external_and_shadow_checks:
2208 if (TREE_CODE (x) == TYPE_DECL)
2209 clone_underlying_type (x);
2211 bind (name, x, scope, /*invisible=*/false, nested);
2213 /* If x's type is incomplete because it's based on a
2214 structure or union which has not yet been fully declared,
2215 attach it to that structure or union type, so we can go
2216 back and complete the variable declaration later, if the
2217 structure or union gets fully declared.
2219 If the input is erroneous, we can have error_mark in the type
2220 slot (e.g. "f(void a, ...)") - that doesn't count as an
2221 incomplete type. */
2222 if (TREE_TYPE (x) != error_mark_node
2223 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2225 tree element = TREE_TYPE (x);
2227 while (TREE_CODE (element) == ARRAY_TYPE)
2228 element = TREE_TYPE (element);
2229 element = TYPE_MAIN_VARIANT (element);
2231 if ((TREE_CODE (element) == RECORD_TYPE
2232 || TREE_CODE (element) == UNION_TYPE)
2233 && (TREE_CODE (x) != TYPE_DECL
2234 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2235 && !COMPLETE_TYPE_P (element))
2236 C_TYPE_INCOMPLETE_VARS (element)
2237 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2239 return x;
2242 /* Record X as belonging to file scope.
2243 This is used only internally by the Objective-C front end,
2244 and is limited to its needs. duplicate_decls is not called;
2245 if there is any preexisting decl for this identifier, it is an ICE. */
2247 tree
2248 pushdecl_top_level (tree x)
2250 tree name;
2251 bool nested = false;
2252 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2254 name = DECL_NAME (x);
2256 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2258 if (TREE_PUBLIC (x))
2260 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2261 nested = true;
2263 if (file_scope)
2264 bind (name, x, file_scope, /*invisible=*/false, nested);
2266 return x;
2269 static void
2270 implicit_decl_warning (tree id, tree olddecl)
2272 void (*diag) (const char *, ...);
2273 switch (mesg_implicit_function_declaration)
2275 case 0: return;
2276 case 1: diag = warning; break;
2277 case 2: diag = error; break;
2278 default: gcc_unreachable ();
2281 diag (G_("implicit declaration of function %qE"), id);
2282 if (olddecl)
2283 locate_old_decl (olddecl, diag);
2286 /* Generate an implicit declaration for identifier FUNCTIONID as a
2287 function of type int (). */
2289 tree
2290 implicitly_declare (tree functionid)
2292 struct c_binding *b;
2293 tree decl = 0;
2294 tree asmspec_tree;
2296 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2298 if (B_IN_SCOPE (b, external_scope))
2300 decl = b->decl;
2301 break;
2305 if (decl)
2307 if (decl == error_mark_node)
2308 return decl;
2310 /* FIXME: Objective-C has weird not-really-builtin functions
2311 which are supposed to be visible automatically. They wind up
2312 in the external scope because they're pushed before the file
2313 scope gets created. Catch this here and rebind them into the
2314 file scope. */
2315 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2317 bind (functionid, decl, file_scope,
2318 /*invisible=*/false, /*nested=*/true);
2319 return decl;
2321 else
2323 tree newtype = default_function_type;
2324 if (b->type)
2325 TREE_TYPE (decl) = b->type;
2326 /* Implicit declaration of a function already declared
2327 (somehow) in a different scope, or as a built-in.
2328 If this is the first time this has happened, warn;
2329 then recycle the old declaration but with the new type. */
2330 if (!C_DECL_IMPLICIT (decl))
2332 implicit_decl_warning (functionid, decl);
2333 C_DECL_IMPLICIT (decl) = 1;
2335 if (DECL_BUILT_IN (decl))
2337 newtype = build_type_attribute_variant (newtype,
2338 TYPE_ATTRIBUTES
2339 (TREE_TYPE (decl)));
2340 if (!comptypes (newtype, TREE_TYPE (decl)))
2342 warning ("incompatible implicit declaration of built-in"
2343 " function %qD", decl);
2344 newtype = TREE_TYPE (decl);
2347 else
2349 if (!comptypes (newtype, TREE_TYPE (decl)))
2351 error ("incompatible implicit declaration of function %qD",
2352 decl);
2353 locate_old_decl (decl, error);
2356 b->type = TREE_TYPE (decl);
2357 TREE_TYPE (decl) = newtype;
2358 bind (functionid, decl, current_scope,
2359 /*invisible=*/false, /*nested=*/true);
2360 return decl;
2364 /* Not seen before. */
2365 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2366 DECL_EXTERNAL (decl) = 1;
2367 TREE_PUBLIC (decl) = 1;
2368 C_DECL_IMPLICIT (decl) = 1;
2369 implicit_decl_warning (functionid, 0);
2370 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2371 if (asmspec_tree)
2372 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2374 /* C89 says implicit declarations are in the innermost block.
2375 So we record the decl in the standard fashion. */
2376 decl = pushdecl (decl);
2378 /* No need to call objc_check_decl here - it's a function type. */
2379 rest_of_decl_compilation (decl, 0, 0);
2381 /* Write a record describing this implicit function declaration
2382 to the prototypes file (if requested). */
2383 gen_aux_info_record (decl, 0, 1, 0);
2385 /* Possibly apply some default attributes to this implicit declaration. */
2386 decl_attributes (&decl, NULL_TREE, 0);
2388 return decl;
2391 /* Issue an error message for a reference to an undeclared variable
2392 ID, including a reference to a builtin outside of function-call
2393 context. Establish a binding of the identifier to error_mark_node
2394 in an appropriate scope, which will suppress further errors for the
2395 same identifier. */
2396 void
2397 undeclared_variable (tree id)
2399 static bool already = false;
2400 struct c_scope *scope;
2402 if (current_function_decl == 0)
2404 error ("%qE undeclared here (not in a function)", id);
2405 scope = current_scope;
2407 else
2409 error ("%qE undeclared (first use in this function)", id);
2411 if (!already)
2413 error ("(Each undeclared identifier is reported only once");
2414 error ("for each function it appears in.)");
2415 already = true;
2418 /* If we are parsing old-style parameter decls, current_function_decl
2419 will be nonnull but current_function_scope will be null. */
2420 scope = current_function_scope ? current_function_scope : current_scope;
2422 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2425 /* Subroutine of lookup_label, declare_label, define_label: construct a
2426 LABEL_DECL with all the proper frills. */
2428 static tree
2429 make_label (tree name, location_t location)
2431 tree label = build_decl (LABEL_DECL, name, void_type_node);
2433 DECL_CONTEXT (label) = current_function_decl;
2434 DECL_MODE (label) = VOIDmode;
2435 DECL_SOURCE_LOCATION (label) = location;
2437 return label;
2440 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2441 Create one if none exists so far for the current function.
2442 This is called when a label is used in a goto expression or
2443 has its address taken. */
2445 tree
2446 lookup_label (tree name)
2448 tree label;
2450 if (current_function_decl == 0)
2452 error ("label %qs referenced outside of any function",
2453 IDENTIFIER_POINTER (name));
2454 return 0;
2457 /* Use a label already defined or ref'd with this name, but not if
2458 it is inherited from a containing function and wasn't declared
2459 using __label__. */
2460 label = I_LABEL_DECL (name);
2461 if (label && (DECL_CONTEXT (label) == current_function_decl
2462 || C_DECLARED_LABEL_FLAG (label)))
2464 /* If the label has only been declared, update its apparent
2465 location to point here, for better diagnostics if it
2466 turns out not to have been defined. */
2467 if (!TREE_USED (label))
2468 DECL_SOURCE_LOCATION (label) = input_location;
2469 return label;
2472 /* No label binding for that identifier; make one. */
2473 label = make_label (name, input_location);
2475 /* Ordinary labels go in the current function scope. */
2476 bind (name, label, current_function_scope,
2477 /*invisible=*/false, /*nested=*/false);
2478 return label;
2481 /* Make a label named NAME in the current function, shadowing silently
2482 any that may be inherited from containing functions or containing
2483 scopes. This is called for __label__ declarations. */
2485 tree
2486 declare_label (tree name)
2488 struct c_binding *b = I_LABEL_BINDING (name);
2489 tree label;
2491 /* Check to make sure that the label hasn't already been declared
2492 at this scope */
2493 if (b && B_IN_CURRENT_SCOPE (b))
2495 error ("duplicate label declaration %qs", IDENTIFIER_POINTER (name));
2496 locate_old_decl (b->decl, error);
2498 /* Just use the previous declaration. */
2499 return b->decl;
2502 label = make_label (name, input_location);
2503 C_DECLARED_LABEL_FLAG (label) = 1;
2505 /* Declared labels go in the current scope. */
2506 bind (name, label, current_scope,
2507 /*invisible=*/false, /*nested=*/false);
2508 return label;
2511 /* Define a label, specifying the location in the source file.
2512 Return the LABEL_DECL node for the label, if the definition is valid.
2513 Otherwise return 0. */
2515 tree
2516 define_label (location_t location, tree name)
2518 /* Find any preexisting label with this name. It is an error
2519 if that label has already been defined in this function, or
2520 if there is a containing function with a declared label with
2521 the same name. */
2522 tree label = I_LABEL_DECL (name);
2523 struct c_label_list *nlist_se, *nlist_vm;
2525 if (label
2526 && ((DECL_CONTEXT (label) == current_function_decl
2527 && DECL_INITIAL (label) != 0)
2528 || (DECL_CONTEXT (label) != current_function_decl
2529 && C_DECLARED_LABEL_FLAG (label))))
2531 error ("%Hduplicate label %qD", &location, label);
2532 locate_old_decl (label, error);
2533 return 0;
2535 else if (label && DECL_CONTEXT (label) == current_function_decl)
2537 /* The label has been used or declared already in this function,
2538 but not defined. Update its location to point to this
2539 definition. */
2540 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2541 error ("%Jjump into statement expression", label);
2542 if (C_DECL_UNDEFINABLE_VM (label))
2543 error ("%Jjump into scope of identifier with variably modified type",
2544 label);
2545 DECL_SOURCE_LOCATION (label) = location;
2547 else
2549 /* No label binding for that identifier; make one. */
2550 label = make_label (name, location);
2552 /* Ordinary labels go in the current function scope. */
2553 bind (name, label, current_function_scope,
2554 /*invisible=*/false, /*nested=*/false);
2557 if (warn_traditional && !in_system_header && lookup_name (name))
2558 warning ("%Htraditional C lacks a separate namespace for labels, "
2559 "identifier %qs conflicts", &location,
2560 IDENTIFIER_POINTER (name));
2562 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2563 nlist_se->next = label_context_stack_se->labels_def;
2564 nlist_se->label = label;
2565 label_context_stack_se->labels_def = nlist_se;
2567 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2568 nlist_vm->next = label_context_stack_vm->labels_def;
2569 nlist_vm->label = label;
2570 label_context_stack_vm->labels_def = nlist_vm;
2572 /* Mark label as having been defined. */
2573 DECL_INITIAL (label) = error_mark_node;
2574 return label;
2577 /* Given NAME, an IDENTIFIER_NODE,
2578 return the structure (or union or enum) definition for that name.
2579 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2580 CODE says which kind of type the caller wants;
2581 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2582 If the wrong kind of type is found, an error is reported. */
2584 static tree
2585 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2587 struct c_binding *b = I_TAG_BINDING (name);
2588 int thislevel = 0;
2590 if (!b || !b->decl)
2591 return 0;
2593 /* We only care about whether it's in this level if
2594 thislevel_only was set or it might be a type clash. */
2595 if (thislevel_only || TREE_CODE (b->decl) != code)
2597 /* For our purposes, a tag in the external scope is the same as
2598 a tag in the file scope. (Primarily relevant to Objective-C
2599 and its builtin structure tags, which get pushed before the
2600 file scope is created.) */
2601 if (B_IN_CURRENT_SCOPE (b)
2602 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2603 thislevel = 1;
2606 if (thislevel_only && !thislevel)
2607 return 0;
2609 if (TREE_CODE (b->decl) != code)
2611 /* Definition isn't the kind we were looking for. */
2612 pending_invalid_xref = name;
2613 pending_invalid_xref_location = input_location;
2615 /* If in the same binding level as a declaration as a tag
2616 of a different type, this must not be allowed to
2617 shadow that tag, so give the error immediately.
2618 (For example, "struct foo; union foo;" is invalid.) */
2619 if (thislevel)
2620 pending_xref_error ();
2622 return b->decl;
2625 /* Print an error message now
2626 for a recent invalid struct, union or enum cross reference.
2627 We don't print them immediately because they are not invalid
2628 when used in the `struct foo;' construct for shadowing. */
2630 void
2631 pending_xref_error (void)
2633 if (pending_invalid_xref != 0)
2634 error ("%H%qs defined as wrong kind of tag",
2635 &pending_invalid_xref_location,
2636 IDENTIFIER_POINTER (pending_invalid_xref));
2637 pending_invalid_xref = 0;
2641 /* Look up NAME in the current scope and its superiors
2642 in the namespace of variables, functions and typedefs.
2643 Return a ..._DECL node of some kind representing its definition,
2644 or return 0 if it is undefined. */
2646 tree
2647 lookup_name (tree name)
2649 struct c_binding *b = I_SYMBOL_BINDING (name);
2650 if (b && !b->invisible)
2651 return b->decl;
2652 return 0;
2655 /* Similar to `lookup_name' but look only at the indicated scope. */
2657 static tree
2658 lookup_name_in_scope (tree name, struct c_scope *scope)
2660 struct c_binding *b;
2662 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2663 if (B_IN_SCOPE (b, scope))
2664 return b->decl;
2665 return 0;
2668 /* Create the predefined scalar types of C,
2669 and some nodes representing standard constants (0, 1, (void *) 0).
2670 Initialize the global scope.
2671 Make definitions for built-in primitive functions. */
2673 void
2674 c_init_decl_processing (void)
2676 tree endlink;
2677 tree ptr_ftype_void, ptr_ftype_ptr;
2678 location_t save_loc = input_location;
2680 /* Adds some ggc roots, and reserved words for c-parse.in. */
2681 c_parse_init ();
2683 current_function_decl = 0;
2685 gcc_obstack_init (&parser_obstack);
2687 /* Make the externals scope. */
2688 push_scope ();
2689 external_scope = current_scope;
2691 /* Declarations from c_common_nodes_and_builtins must not be associated
2692 with this input file, lest we get differences between using and not
2693 using preprocessed headers. */
2694 #ifdef USE_MAPPED_LOCATION
2695 input_location = BUILTINS_LOCATION;
2696 #else
2697 input_location.file = "<built-in>";
2698 input_location.line = 0;
2699 #endif
2701 build_common_tree_nodes (flag_signed_char, false);
2703 c_common_nodes_and_builtins ();
2705 /* In C, comparisons and TRUTH_* expressions have type int. */
2706 truthvalue_type_node = integer_type_node;
2707 truthvalue_true_node = integer_one_node;
2708 truthvalue_false_node = integer_zero_node;
2710 /* Even in C99, which has a real boolean type. */
2711 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2712 boolean_type_node));
2714 endlink = void_list_node;
2715 ptr_ftype_void = build_function_type (ptr_type_node, endlink);
2716 ptr_ftype_ptr
2717 = build_function_type (ptr_type_node,
2718 tree_cons (NULL_TREE, ptr_type_node, endlink));
2720 input_location = save_loc;
2722 pedantic_lvalues = true;
2724 make_fname_decl = c_make_fname_decl;
2725 start_fname_decls ();
2728 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2729 decl, NAME is the initialization string and TYPE_DEP indicates whether
2730 NAME depended on the type of the function. As we don't yet implement
2731 delayed emission of static data, we mark the decl as emitted
2732 so it is not placed in the output. Anything using it must therefore pull
2733 out the STRING_CST initializer directly. FIXME. */
2735 static tree
2736 c_make_fname_decl (tree id, int type_dep)
2738 const char *name = fname_as_string (type_dep);
2739 tree decl, type, init;
2740 size_t length = strlen (name);
2742 type = build_array_type (char_type_node,
2743 build_index_type (size_int (length)));
2744 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2746 decl = build_decl (VAR_DECL, id, type);
2748 TREE_STATIC (decl) = 1;
2749 TREE_READONLY (decl) = 1;
2750 DECL_ARTIFICIAL (decl) = 1;
2752 init = build_string (length + 1, name);
2753 free ((char *) name);
2754 TREE_TYPE (init) = type;
2755 DECL_INITIAL (decl) = init;
2757 TREE_USED (decl) = 1;
2759 if (current_function_decl)
2761 DECL_CONTEXT (decl) = current_function_decl;
2762 bind (id, decl, current_function_scope,
2763 /*invisible=*/false, /*nested=*/false);
2766 finish_decl (decl, init, NULL_TREE);
2768 return decl;
2771 /* Return a definition for a builtin function named NAME and whose data type
2772 is TYPE. TYPE should be a function type with argument types.
2773 FUNCTION_CODE tells later passes how to compile calls to this function.
2774 See tree.h for its possible values.
2776 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
2777 the name to be called if we can't opencode the function. If
2778 ATTRS is nonzero, use that for the function's attribute list. */
2780 tree
2781 builtin_function (const char *name, tree type, int function_code,
2782 enum built_in_class cl, const char *library_name,
2783 tree attrs)
2785 tree id = get_identifier (name);
2786 tree decl = build_decl (FUNCTION_DECL, id, type);
2787 TREE_PUBLIC (decl) = 1;
2788 DECL_EXTERNAL (decl) = 1;
2789 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
2790 DECL_BUILT_IN_CLASS (decl) = cl;
2791 DECL_FUNCTION_CODE (decl) = function_code;
2792 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2793 if (library_name)
2794 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
2796 /* Should never be called on a symbol with a preexisting meaning. */
2797 gcc_assert (!I_SYMBOL_BINDING (id));
2799 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2801 /* Builtins in the implementation namespace are made visible without
2802 needing to be explicitly declared. See push_file_scope. */
2803 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2805 TREE_CHAIN (decl) = visible_builtins;
2806 visible_builtins = decl;
2809 /* Possibly apply some default attributes to this built-in function. */
2810 if (attrs)
2811 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
2812 else
2813 decl_attributes (&decl, NULL_TREE, 0);
2815 return decl;
2818 /* Called when a declaration is seen that contains no names to declare.
2819 If its type is a reference to a structure, union or enum inherited
2820 from a containing scope, shadow that tag name for the current scope
2821 with a forward reference.
2822 If its type defines a new named structure or union
2823 or defines an enum, it is valid but we need not do anything here.
2824 Otherwise, it is an error. */
2826 void
2827 shadow_tag (const struct c_declspecs *declspecs)
2829 shadow_tag_warned (declspecs, 0);
2832 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2833 but no pedwarn. */
2834 void
2835 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2837 bool found_tag = false;
2839 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2841 tree value = declspecs->type;
2842 enum tree_code code = TREE_CODE (value);
2844 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2845 /* Used to test also that TYPE_SIZE (value) != 0.
2846 That caused warning for `struct foo;' at top level in the file. */
2848 tree name = TYPE_NAME (value);
2849 tree t;
2851 found_tag = true;
2853 if (name == 0)
2855 if (warned != 1 && code != ENUMERAL_TYPE)
2856 /* Empty unnamed enum OK */
2858 pedwarn ("unnamed struct/union that defines no instances");
2859 warned = 1;
2862 else if (!declspecs->tag_defined_p
2863 && declspecs->storage_class != csc_none)
2865 if (warned != 1)
2866 pedwarn ("empty declaration with storage class specifier "
2867 "does not redeclare tag");
2868 warned = 1;
2869 pending_xref_error ();
2871 else if (!declspecs->tag_defined_p
2872 && (declspecs->const_p
2873 || declspecs->volatile_p
2874 || declspecs->restrict_p))
2876 if (warned != 1)
2877 pedwarn ("empty declaration with type qualifier "
2878 "does not redeclare tag");
2879 warned = 1;
2880 pending_xref_error ();
2882 else
2884 pending_invalid_xref = 0;
2885 t = lookup_tag (code, name, 1);
2887 if (t == 0)
2889 t = make_node (code);
2890 pushtag (name, t);
2894 else
2896 if (warned != 1 && !in_system_header)
2898 pedwarn ("useless type name in empty declaration");
2899 warned = 1;
2903 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2905 pedwarn ("useless type name in empty declaration");
2906 warned = 1;
2909 pending_invalid_xref = 0;
2911 if (declspecs->inline_p)
2913 error ("%<inline%> in empty declaration");
2914 warned = 1;
2917 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2919 error ("%<auto%> in file-scope empty declaration");
2920 warned = 1;
2923 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2925 error ("%<register%> in file-scope empty declaration");
2926 warned = 1;
2929 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2931 warning ("useless storage class specifier in empty declaration");
2932 warned = 2;
2935 if (!warned && !in_system_header && declspecs->thread_p)
2937 warning ("useless %<__thread%> in empty declaration");
2938 warned = 2;
2941 if (!warned && !in_system_header && (declspecs->const_p
2942 || declspecs->volatile_p
2943 || declspecs->restrict_p))
2945 warning ("useless type qualifier in empty declaration");
2946 warned = 2;
2949 if (warned != 1)
2951 if (!found_tag)
2952 pedwarn ("empty declaration");
2957 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2958 bits. SPECS represents declaration specifiers that the grammar
2959 only permits to contain type qualifiers and attributes. */
2962 quals_from_declspecs (const struct c_declspecs *specs)
2964 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
2965 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
2966 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
2967 gcc_assert (!specs->type
2968 && !specs->decl_attr
2969 && specs->typespec_word == cts_none
2970 && specs->storage_class == csc_none
2971 && !specs->typedef_p
2972 && !specs->explicit_signed_p
2973 && !specs->deprecated_p
2974 && !specs->long_p
2975 && !specs->long_long_p
2976 && !specs->short_p
2977 && !specs->signed_p
2978 && !specs->unsigned_p
2979 && !specs->complex_p
2980 && !specs->inline_p
2981 && !specs->thread_p);
2982 return quals;
2985 /* Construct an array declarator. EXPR is the expression inside [], or
2986 NULL_TREE. QUALS are the type qualifiers inside the [] (to be applied
2987 to the pointer to which a parameter array is converted). STATIC_P is
2988 true if "static" is inside the [], false otherwise. VLA_UNSPEC_P
2989 is true if the array is [*], a VLA of unspecified length which is
2990 nevertheless a complete type (not currently implemented by GCC),
2991 false otherwise. The field for the contained declarator is left to be
2992 filled in by set_array_declarator_inner. */
2994 struct c_declarator *
2995 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
2996 bool vla_unspec_p)
2998 struct c_declarator *declarator = XOBNEW (&parser_obstack,
2999 struct c_declarator);
3000 declarator->kind = cdk_array;
3001 declarator->declarator = 0;
3002 declarator->u.array.dimen = expr;
3003 if (quals)
3005 declarator->u.array.attrs = quals->attrs;
3006 declarator->u.array.quals = quals_from_declspecs (quals);
3008 else
3010 declarator->u.array.attrs = NULL_TREE;
3011 declarator->u.array.quals = 0;
3013 declarator->u.array.static_p = static_p;
3014 declarator->u.array.vla_unspec_p = vla_unspec_p;
3015 if (pedantic && !flag_isoc99)
3017 if (static_p || quals != NULL)
3018 pedwarn ("ISO C90 does not support %<static%> or type "
3019 "qualifiers in parameter array declarators");
3020 if (vla_unspec_p)
3021 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3023 if (vla_unspec_p)
3024 warning ("GCC does not yet properly implement %<[*]%> array declarators");
3025 return declarator;
3028 /* Set the contained declarator of an array declarator. DECL is the
3029 declarator, as constructed by build_array_declarator; INNER is what
3030 appears on the left of the []. ABSTRACT_P is true if it is an
3031 abstract declarator, false otherwise; this is used to reject static
3032 and type qualifiers in abstract declarators, where they are not in
3033 the C99 grammar (subject to possible change in DR#289). */
3035 struct c_declarator *
3036 set_array_declarator_inner (struct c_declarator *decl,
3037 struct c_declarator *inner, bool abstract_p)
3039 decl->declarator = inner;
3040 if (abstract_p && (decl->u.array.quals != TYPE_UNQUALIFIED
3041 || decl->u.array.attrs != NULL_TREE
3042 || decl->u.array.static_p))
3043 error ("static or type qualifiers in abstract declarator");
3044 return decl;
3047 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3049 tree
3050 groktypename (struct c_type_name *type_name)
3052 tree type;
3053 tree attrs = type_name->specs->attrs;
3055 type_name->specs->attrs = NULL_TREE;
3057 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3058 false, NULL);
3060 /* Apply attributes. */
3061 decl_attributes (&type, attrs, 0);
3063 return type;
3066 /* Decode a declarator in an ordinary declaration or data definition.
3067 This is called as soon as the type information and variable name
3068 have been parsed, before parsing the initializer if any.
3069 Here we create the ..._DECL node, fill in its type,
3070 and put it on the list of decls for the current context.
3071 The ..._DECL node is returned as the value.
3073 Exception: for arrays where the length is not specified,
3074 the type is left null, to be filled in by `finish_decl'.
3076 Function definitions do not come here; they go to start_function
3077 instead. However, external and forward declarations of functions
3078 do go through here. Structure field declarations are done by
3079 grokfield and not through here. */
3081 tree
3082 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3083 bool initialized, tree attributes)
3085 tree decl;
3086 tree tem;
3088 /* An object declared as __attribute__((deprecated)) suppresses
3089 warnings of uses of other deprecated items. */
3090 if (lookup_attribute ("deprecated", attributes))
3091 deprecated_state = DEPRECATED_SUPPRESS;
3093 decl = grokdeclarator (declarator, declspecs,
3094 NORMAL, initialized, NULL);
3095 if (!decl)
3096 return 0;
3098 deprecated_state = DEPRECATED_NORMAL;
3100 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3101 && MAIN_NAME_P (DECL_NAME (decl)))
3102 warning ("%J%qD is usually a function", decl, decl);
3104 if (initialized)
3105 /* Is it valid for this decl to have an initializer at all?
3106 If not, set INITIALIZED to zero, which will indirectly
3107 tell 'finish_decl' to ignore the initializer once it is parsed. */
3108 switch (TREE_CODE (decl))
3110 case TYPE_DECL:
3111 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3112 initialized = 0;
3113 break;
3115 case FUNCTION_DECL:
3116 error ("function %qD is initialized like a variable", decl);
3117 initialized = 0;
3118 break;
3120 case PARM_DECL:
3121 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3122 error ("parameter %qD is initialized", decl);
3123 initialized = 0;
3124 break;
3126 default:
3127 /* Don't allow initializations for incomplete types except for
3128 arrays which might be completed by the initialization. */
3130 /* This can happen if the array size is an undefined macro.
3131 We already gave a warning, so we don't need another one. */
3132 if (TREE_TYPE (decl) == error_mark_node)
3133 initialized = 0;
3134 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3136 /* A complete type is ok if size is fixed. */
3138 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3139 || C_DECL_VARIABLE_SIZE (decl))
3141 error ("variable-sized object may not be initialized");
3142 initialized = 0;
3145 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3147 error ("variable %qD has initializer but incomplete type", decl);
3148 initialized = 0;
3150 else if (C_DECL_VARIABLE_SIZE (decl))
3152 /* Although C99 is unclear about whether incomplete arrays
3153 of VLAs themselves count as VLAs, it does not make
3154 sense to permit them to be initialized given that
3155 ordinary VLAs may not be initialized. */
3156 error ("variable-sized object may not be initialized");
3157 initialized = 0;
3161 if (initialized)
3163 if (current_scope == file_scope)
3164 TREE_STATIC (decl) = 1;
3166 /* Tell 'pushdecl' this is an initialized decl
3167 even though we don't yet have the initializer expression.
3168 Also tell 'finish_decl' it may store the real initializer. */
3169 DECL_INITIAL (decl) = error_mark_node;
3172 /* If this is a function declaration, write a record describing it to the
3173 prototypes file (if requested). */
3175 if (TREE_CODE (decl) == FUNCTION_DECL)
3176 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3178 /* ANSI specifies that a tentative definition which is not merged with
3179 a non-tentative definition behaves exactly like a definition with an
3180 initializer equal to zero. (Section 3.7.2)
3182 -fno-common gives strict ANSI behavior, though this tends to break
3183 a large body of code that grew up without this rule.
3185 Thread-local variables are never common, since there's no entrenched
3186 body of code to break, and it allows more efficient variable references
3187 in the presence of dynamic linking. */
3189 if (TREE_CODE (decl) == VAR_DECL
3190 && !initialized
3191 && TREE_PUBLIC (decl)
3192 && !DECL_THREAD_LOCAL (decl)
3193 && !flag_no_common)
3194 DECL_COMMON (decl) = 1;
3196 /* Set attributes here so if duplicate decl, will have proper attributes. */
3197 decl_attributes (&decl, attributes, 0);
3199 if (TREE_CODE (decl) == FUNCTION_DECL
3200 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3202 struct c_declarator *ce = declarator;
3204 if (ce->kind == cdk_pointer)
3205 ce = declarator->declarator;
3206 if (ce->kind == cdk_function)
3208 tree args = ce->u.arg_info->parms;
3209 for (; args; args = TREE_CHAIN (args))
3211 tree type = TREE_TYPE (args);
3212 if (type && INTEGRAL_TYPE_P (type)
3213 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3214 DECL_ARG_TYPE (args) = integer_type_node;
3219 if (TREE_CODE (decl) == FUNCTION_DECL
3220 && DECL_DECLARED_INLINE_P (decl)
3221 && DECL_UNINLINABLE (decl)
3222 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3223 warning ("%Jinline function %qD given attribute noinline", decl, decl);
3225 /* Add this decl to the current scope.
3226 TEM may equal DECL or it may be a previous decl of the same name. */
3227 tem = pushdecl (decl);
3229 if (initialized && DECL_EXTERNAL (tem))
3231 DECL_EXTERNAL (tem) = 0;
3232 TREE_STATIC (tem) = 1;
3235 return tem;
3238 /* Finish processing of a declaration;
3239 install its initial value.
3240 If the length of an array type is not known before,
3241 it must be determined now, from the initial value, or it is an error. */
3243 void
3244 finish_decl (tree decl, tree init, tree asmspec_tree)
3246 tree type = TREE_TYPE (decl);
3247 int was_incomplete = (DECL_SIZE (decl) == 0);
3248 const char *asmspec = 0;
3250 /* If a name was specified, get the string. */
3251 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3252 && DECL_FILE_SCOPE_P (decl))
3253 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3254 if (asmspec_tree)
3255 asmspec = TREE_STRING_POINTER (asmspec_tree);
3257 /* If `start_decl' didn't like having an initialization, ignore it now. */
3258 if (init != 0 && DECL_INITIAL (decl) == 0)
3259 init = 0;
3261 /* Don't crash if parm is initialized. */
3262 if (TREE_CODE (decl) == PARM_DECL)
3263 init = 0;
3265 if (init)
3266 store_init_value (decl, init);
3268 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3269 || TREE_CODE (decl) == FUNCTION_DECL
3270 || TREE_CODE (decl) == FIELD_DECL))
3271 objc_check_decl (decl);
3273 /* Deduce size of array from initialization, if not already known. */
3274 if (TREE_CODE (type) == ARRAY_TYPE
3275 && TYPE_DOMAIN (type) == 0
3276 && TREE_CODE (decl) != TYPE_DECL)
3278 bool do_default
3279 = (TREE_STATIC (decl)
3280 /* Even if pedantic, an external linkage array
3281 may have incomplete type at first. */
3282 ? pedantic && !TREE_PUBLIC (decl)
3283 : !DECL_EXTERNAL (decl));
3284 int failure
3285 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3286 do_default);
3288 /* Get the completed type made by complete_array_type. */
3289 type = TREE_TYPE (decl);
3291 switch (failure)
3293 case 1:
3294 error ("%Jinitializer fails to determine size of %qD", decl, decl);
3295 break;
3297 case 2:
3298 if (do_default)
3299 error ("%Jarray size missing in %qD", decl, decl);
3300 /* If a `static' var's size isn't known,
3301 make it extern as well as static, so it does not get
3302 allocated.
3303 If it is not `static', then do not mark extern;
3304 finish_incomplete_decl will give it a default size
3305 and it will get allocated. */
3306 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3307 DECL_EXTERNAL (decl) = 1;
3308 break;
3310 case 3:
3311 error ("%Jzero or negative size array %qD", decl, decl);
3312 break;
3314 case 0:
3315 /* For global variables, update the copy of the type that
3316 exists in the binding. */
3317 if (TREE_PUBLIC (decl))
3319 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3320 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3321 b_ext = b_ext->shadowed;
3322 if (b_ext)
3324 if (b_ext->type)
3325 b_ext->type = composite_type (b_ext->type, type);
3326 else
3327 b_ext->type = type;
3330 break;
3332 default:
3333 gcc_unreachable ();
3336 if (DECL_INITIAL (decl))
3337 TREE_TYPE (DECL_INITIAL (decl)) = type;
3339 layout_decl (decl, 0);
3342 if (TREE_CODE (decl) == VAR_DECL)
3344 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3345 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3346 layout_decl (decl, 0);
3348 if (DECL_SIZE (decl) == 0
3349 /* Don't give an error if we already gave one earlier. */
3350 && TREE_TYPE (decl) != error_mark_node
3351 && (TREE_STATIC (decl)
3352 /* A static variable with an incomplete type
3353 is an error if it is initialized.
3354 Also if it is not file scope.
3355 Otherwise, let it through, but if it is not `extern'
3356 then it may cause an error message later. */
3357 ? (DECL_INITIAL (decl) != 0
3358 || !DECL_FILE_SCOPE_P (decl))
3359 /* An automatic variable with an incomplete type
3360 is an error. */
3361 : !DECL_EXTERNAL (decl)))
3363 error ("%Jstorage size of %qD isn%'t known", decl, decl);
3364 TREE_TYPE (decl) = error_mark_node;
3367 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3368 && DECL_SIZE (decl) != 0)
3370 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3371 constant_expression_warning (DECL_SIZE (decl));
3372 else
3373 error ("%Jstorage size of %qD isn%'t constant", decl, decl);
3376 if (TREE_USED (type))
3377 TREE_USED (decl) = 1;
3380 /* If this is a function and an assembler name is specified, reset DECL_RTL
3381 so we can give it its new name. Also, update built_in_decls if it
3382 was a normal built-in. */
3383 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3385 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3386 set_builtin_user_assembler_name (decl, asmspec);
3387 set_user_assembler_name (decl, asmspec);
3390 /* If #pragma weak was used, mark the decl weak now. */
3391 maybe_apply_pragma_weak (decl);
3393 /* If this is a variable definition, determine its ELF visibility. */
3394 if (TREE_CODE (decl) == VAR_DECL
3395 && TREE_STATIC (decl)
3396 && !DECL_EXTERNAL (decl))
3397 c_determine_visibility (decl);
3399 /* Output the assembler code and/or RTL code for variables and functions,
3400 unless the type is an undefined structure or union.
3401 If not, it will get done when the type is completed. */
3403 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3405 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3406 if (c_dialect_objc ())
3407 objc_check_decl (decl);
3409 if (asmspec)
3411 /* If this is not a static variable, issue a warning.
3412 It doesn't make any sense to give an ASMSPEC for an
3413 ordinary, non-register local variable. Historically,
3414 GCC has accepted -- but ignored -- the ASMSPEC in
3415 this case. */
3416 if (!DECL_FILE_SCOPE_P (decl)
3417 && TREE_CODE (decl) == VAR_DECL
3418 && !C_DECL_REGISTER (decl)
3419 && !TREE_STATIC (decl))
3420 warning ("%Jignoring asm-specifier for non-static local "
3421 "variable %qD", decl, decl);
3422 else
3423 set_user_assembler_name (decl, asmspec);
3426 if (DECL_FILE_SCOPE_P (decl))
3428 if (DECL_INITIAL (decl) == NULL_TREE
3429 || DECL_INITIAL (decl) == error_mark_node)
3430 /* Don't output anything
3431 when a tentative file-scope definition is seen.
3432 But at end of compilation, do output code for them. */
3433 DECL_DEFER_OUTPUT (decl) = 1;
3434 rest_of_decl_compilation (decl, true, 0);
3436 else
3438 /* In conjunction with an ASMSPEC, the `register'
3439 keyword indicates that we should place the variable
3440 in a particular register. */
3441 if (asmspec && C_DECL_REGISTER (decl))
3443 DECL_HARD_REGISTER (decl) = 1;
3444 /* This cannot be done for a structure with volatile
3445 fields, on which DECL_REGISTER will have been
3446 reset. */
3447 if (!DECL_REGISTER (decl))
3448 error ("cannot put object with volatile field into register");
3451 if (TREE_CODE (decl) != FUNCTION_DECL)
3453 /* If we're building a variable sized type, and we might be
3454 reachable other than via the top of the current binding
3455 level, then create a new BIND_EXPR so that we deallocate
3456 the object at the right time. */
3457 /* Note that DECL_SIZE can be null due to errors. */
3458 if (DECL_SIZE (decl)
3459 && !TREE_CONSTANT (DECL_SIZE (decl))
3460 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3462 tree bind;
3463 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3464 TREE_SIDE_EFFECTS (bind) = 1;
3465 add_stmt (bind);
3466 BIND_EXPR_BODY (bind) = push_stmt_list ();
3468 add_stmt (build_stmt (DECL_EXPR, decl));
3473 if (!DECL_FILE_SCOPE_P (decl))
3475 /* Recompute the RTL of a local array now
3476 if it used to be an incomplete type. */
3477 if (was_incomplete
3478 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3480 /* If we used it already as memory, it must stay in memory. */
3481 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3482 /* If it's still incomplete now, no init will save it. */
3483 if (DECL_SIZE (decl) == 0)
3484 DECL_INITIAL (decl) = 0;
3489 /* If this was marked 'used', be sure it will be output. */
3490 if (lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3491 mark_decl_referenced (decl);
3493 if (TREE_CODE (decl) == TYPE_DECL)
3495 if (!DECL_FILE_SCOPE_P (decl)
3496 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3497 add_stmt (build_stmt (DECL_EXPR, decl));
3499 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3502 /* At the end of a declaration, throw away any variable type sizes
3503 of types defined inside that declaration. There is no use
3504 computing them in the following function definition. */
3505 if (current_scope == file_scope)
3506 get_pending_sizes ();
3508 /* Install a cleanup (aka destructor) if one was given. */
3509 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3511 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3512 if (attr)
3514 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3515 tree cleanup_decl = lookup_name (cleanup_id);
3516 tree cleanup;
3518 /* Build "cleanup(&decl)" for the destructor. */
3519 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3520 cleanup = build_tree_list (NULL_TREE, cleanup);
3521 cleanup = build_function_call (cleanup_decl, cleanup);
3523 /* Don't warn about decl unused; the cleanup uses it. */
3524 TREE_USED (decl) = 1;
3525 TREE_USED (cleanup_decl) = 1;
3527 /* Initialize EH, if we've been told to do so. */
3528 if (flag_exceptions && !c_eh_initialized_p)
3530 c_eh_initialized_p = true;
3531 eh_personality_libfunc
3532 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3533 ? "__gcc_personality_sj0"
3534 : "__gcc_personality_v0");
3535 using_eh_for_cleanups ();
3538 push_cleanup (decl, cleanup, false);
3543 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3545 tree
3546 grokparm (const struct c_parm *parm)
3548 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3549 NULL);
3551 decl_attributes (&decl, parm->attrs, 0);
3553 return decl;
3556 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3557 and push that on the current scope. */
3559 void
3560 push_parm_decl (const struct c_parm *parm)
3562 tree decl;
3564 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL);
3565 decl_attributes (&decl, parm->attrs, 0);
3567 decl = pushdecl (decl);
3569 finish_decl (decl, NULL_TREE, NULL_TREE);
3572 /* Mark all the parameter declarations to date as forward decls.
3573 Also diagnose use of this extension. */
3575 void
3576 mark_forward_parm_decls (void)
3578 struct c_binding *b;
3580 if (pedantic && !current_scope->warned_forward_parm_decls)
3582 pedwarn ("ISO C forbids forward parameter declarations");
3583 current_scope->warned_forward_parm_decls = true;
3586 for (b = current_scope->bindings; b; b = b->prev)
3587 if (TREE_CODE (b->decl) == PARM_DECL)
3588 TREE_ASM_WRITTEN (b->decl) = 1;
3591 static GTY(()) int compound_literal_number;
3593 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3594 literal, which may be an incomplete array type completed by the
3595 initializer; INIT is a CONSTRUCTOR that initializes the compound
3596 literal. */
3598 tree
3599 build_compound_literal (tree type, tree init)
3601 /* We do not use start_decl here because we have a type, not a declarator;
3602 and do not use finish_decl because the decl should be stored inside
3603 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3604 tree decl;
3605 tree complit;
3606 tree stmt;
3608 if (type == error_mark_node)
3609 return error_mark_node;
3611 decl = build_decl (VAR_DECL, NULL_TREE, type);
3612 DECL_EXTERNAL (decl) = 0;
3613 TREE_PUBLIC (decl) = 0;
3614 TREE_STATIC (decl) = (current_scope == file_scope);
3615 DECL_CONTEXT (decl) = current_function_decl;
3616 TREE_USED (decl) = 1;
3617 TREE_TYPE (decl) = type;
3618 TREE_READONLY (decl) = TYPE_READONLY (type);
3619 store_init_value (decl, init);
3621 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3623 int failure = complete_array_type (&TREE_TYPE (decl),
3624 DECL_INITIAL (decl), true);
3625 gcc_assert (!failure);
3627 type = TREE_TYPE (decl);
3628 TREE_TYPE (DECL_INITIAL (decl)) = type;
3631 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3632 return error_mark_node;
3634 stmt = build_stmt (DECL_EXPR, decl);
3635 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3636 TREE_SIDE_EFFECTS (complit) = 1;
3638 layout_decl (decl, 0);
3640 if (TREE_STATIC (decl))
3642 /* This decl needs a name for the assembler output. We also need
3643 a unique suffix to be added to the name. */
3644 char *name;
3646 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
3647 compound_literal_number);
3648 compound_literal_number++;
3649 DECL_NAME (decl) = get_identifier (name);
3650 DECL_DEFER_OUTPUT (decl) = 1;
3651 DECL_COMDAT (decl) = 1;
3652 DECL_ARTIFICIAL (decl) = 1;
3653 DECL_IGNORED_P (decl) = 1;
3654 pushdecl (decl);
3655 rest_of_decl_compilation (decl, 1, 0);
3658 return complit;
3661 /* Determine whether TYPE is a structure with a flexible array member,
3662 or a union containing such a structure (possibly recursively). */
3664 static bool
3665 flexible_array_type_p (tree type)
3667 tree x;
3668 switch (TREE_CODE (type))
3670 case RECORD_TYPE:
3671 x = TYPE_FIELDS (type);
3672 if (x == NULL_TREE)
3673 return false;
3674 while (TREE_CHAIN (x) != NULL_TREE)
3675 x = TREE_CHAIN (x);
3676 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3677 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3678 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3679 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3680 return true;
3681 return false;
3682 case UNION_TYPE:
3683 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3685 if (flexible_array_type_p (TREE_TYPE (x)))
3686 return true;
3688 return false;
3689 default:
3690 return false;
3694 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3695 replacing with appropriate values if they are invalid. */
3696 static void
3697 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3699 tree type_mv;
3700 unsigned int max_width;
3701 unsigned HOST_WIDE_INT w;
3702 const char *name = orig_name ? orig_name: _("<anonymous>");
3704 /* Necessary? */
3705 STRIP_NOPS (*width);
3707 /* Detect and ignore out of range field width and process valid
3708 field widths. */
3709 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3710 || TREE_CODE (*width) != INTEGER_CST)
3712 error ("bit-field %qs width not an integer constant", name);
3713 *width = integer_one_node;
3715 else
3717 constant_expression_warning (*width);
3718 if (tree_int_cst_sgn (*width) < 0)
3720 error ("negative width in bit-field %qs", name);
3721 *width = integer_one_node;
3723 else if (integer_zerop (*width) && orig_name)
3725 error ("zero width for bit-field %qs", name);
3726 *width = integer_one_node;
3730 /* Detect invalid bit-field type. */
3731 if (TREE_CODE (*type) != INTEGER_TYPE
3732 && TREE_CODE (*type) != BOOLEAN_TYPE
3733 && TREE_CODE (*type) != ENUMERAL_TYPE)
3735 error ("bit-field %qs has invalid type", name);
3736 *type = unsigned_type_node;
3739 type_mv = TYPE_MAIN_VARIANT (*type);
3740 if (pedantic
3741 && !in_system_header
3742 && type_mv != integer_type_node
3743 && type_mv != unsigned_type_node
3744 && type_mv != boolean_type_node)
3745 pedwarn ("type of bit-field %qs is a GCC extension", name);
3747 if (type_mv == boolean_type_node)
3748 max_width = CHAR_TYPE_SIZE;
3749 else
3750 max_width = TYPE_PRECISION (*type);
3752 if (0 < compare_tree_int (*width, max_width))
3754 error ("width of %qs exceeds its type", name);
3755 w = max_width;
3756 *width = build_int_cst (NULL_TREE, w);
3758 else
3759 w = tree_low_cst (*width, 1);
3761 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3763 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3764 if (!lt
3765 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3766 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3767 warning ("%qs is narrower than values of its type", name);
3771 /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3772 static tree
3773 c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3775 /* Extended integer types of the same width as a standard type have
3776 lesser rank, so those of the same width as int promote to int or
3777 unsigned int and are valid for printf formats expecting int or
3778 unsigned int. To avoid such special cases, avoid creating
3779 extended integer types for bit-fields if a standard integer type
3780 is available. */
3781 if (width == TYPE_PRECISION (integer_type_node))
3782 return unsignedp ? unsigned_type_node : integer_type_node;
3783 if (width == TYPE_PRECISION (signed_char_type_node))
3784 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3785 if (width == TYPE_PRECISION (short_integer_type_node))
3786 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3787 if (width == TYPE_PRECISION (long_integer_type_node))
3788 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3789 if (width == TYPE_PRECISION (long_long_integer_type_node))
3790 return (unsignedp ? long_long_unsigned_type_node
3791 : long_long_integer_type_node);
3792 return build_nonstandard_integer_type (width, unsignedp);
3795 /* Given declspecs and a declarator,
3796 determine the name and type of the object declared
3797 and construct a ..._DECL node for it.
3798 (In one case we can return a ..._TYPE node instead.
3799 For invalid input we sometimes return 0.)
3801 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3803 DECL_CONTEXT says which syntactic context this declaration is in:
3804 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3805 FUNCDEF for a function definition. Like NORMAL but a few different
3806 error messages in each case. Return value may be zero meaning
3807 this definition is too screwy to try to parse.
3808 PARM for a parameter declaration (either within a function prototype
3809 or before a function body). Make a PARM_DECL, or return void_type_node.
3810 TYPENAME if for a typename (in a cast or sizeof).
3811 Don't make a DECL node; just return the ..._TYPE node.
3812 FIELD for a struct or union field; make a FIELD_DECL.
3813 INITIALIZED is true if the decl has an initializer.
3814 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3815 representing the width of the bit-field.
3817 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3818 It may also be so in the PARM case, for a prototype where the
3819 argument type is specified but not the name.
3821 This function is where the complicated C meanings of `static'
3822 and `extern' are interpreted. */
3824 static tree
3825 grokdeclarator (const struct c_declarator *declarator,
3826 struct c_declspecs *declspecs,
3827 enum decl_context decl_context, bool initialized, tree *width)
3829 tree type = declspecs->type;
3830 bool threadp = declspecs->thread_p;
3831 enum c_storage_class storage_class = declspecs->storage_class;
3832 int constp;
3833 int restrictp;
3834 int volatilep;
3835 int type_quals = TYPE_UNQUALIFIED;
3836 const char *name, *orig_name;
3837 tree typedef_type = 0;
3838 int funcdef_flag = 0;
3839 bool funcdef_syntax = false;
3840 int size_varies = 0;
3841 tree decl_attr = declspecs->decl_attr;
3842 int array_ptr_quals = TYPE_UNQUALIFIED;
3843 tree array_ptr_attrs = NULL_TREE;
3844 int array_parm_static = 0;
3845 tree returned_attrs = NULL_TREE;
3846 bool bitfield = width != NULL;
3847 tree element_type;
3848 struct c_arg_info *arg_info = 0;
3850 if (decl_context == FUNCDEF)
3851 funcdef_flag = 1, decl_context = NORMAL;
3853 /* Look inside a declarator for the name being declared
3854 and get it as a string, for an error message. */
3856 const struct c_declarator *decl = declarator;
3857 name = 0;
3859 while (decl)
3860 switch (decl->kind)
3862 case cdk_function:
3863 case cdk_array:
3864 case cdk_pointer:
3865 funcdef_syntax = (decl->kind == cdk_function);
3866 decl = decl->declarator;
3867 break;
3869 case cdk_attrs:
3870 decl = decl->declarator;
3871 break;
3873 case cdk_id:
3874 if (decl->u.id)
3875 name = IDENTIFIER_POINTER (decl->u.id);
3876 decl = 0;
3877 break;
3879 default:
3880 gcc_unreachable ();
3882 orig_name = name;
3883 if (name == 0)
3884 name = "type name";
3887 /* A function definition's declarator must have the form of
3888 a function declarator. */
3890 if (funcdef_flag && !funcdef_syntax)
3891 return 0;
3893 /* If this looks like a function definition, make it one,
3894 even if it occurs where parms are expected.
3895 Then store_parm_decls will reject it and not use it as a parm. */
3896 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
3897 decl_context = PARM;
3899 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
3900 warn_deprecated_use (declspecs->type);
3902 if ((decl_context == NORMAL || decl_context == FIELD)
3903 && current_scope == file_scope
3904 && variably_modified_type_p (type, NULL_TREE))
3906 error ("variably modified %qs at file scope", name);
3907 type = integer_type_node;
3910 typedef_type = type;
3911 size_varies = C_TYPE_VARIABLE_SIZE (type);
3913 /* Diagnose defaulting to "int". */
3915 if (declspecs->default_int_p && !in_system_header)
3917 /* Issue a warning if this is an ISO C 99 program or if
3918 -Wreturn-type and this is a function, or if -Wimplicit;
3919 prefer the former warning since it is more explicit. */
3920 if ((warn_implicit_int || warn_return_type || flag_isoc99)
3921 && funcdef_flag)
3922 warn_about_return_type = 1;
3923 else if (warn_implicit_int || flag_isoc99)
3924 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
3927 /* Adjust the type if a bit-field is being declared,
3928 -funsigned-bitfields applied and the type is not explicitly
3929 "signed". */
3930 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
3931 && TREE_CODE (type) == INTEGER_TYPE)
3932 type = c_common_unsigned_type (type);
3934 /* Figure out the type qualifiers for the declaration. There are
3935 two ways a declaration can become qualified. One is something
3936 like `const int i' where the `const' is explicit. Another is
3937 something like `typedef const int CI; CI i' where the type of the
3938 declaration contains the `const'. A third possibility is that
3939 there is a type qualifier on the element type of a typedefed
3940 array type, in which case we should extract that qualifier so
3941 that c_apply_type_quals_to_decls receives the full list of
3942 qualifiers to work with (C90 is not entirely clear about whether
3943 duplicate qualifiers should be diagnosed in this case, but it
3944 seems most appropriate to do so). */
3945 element_type = strip_array_types (type);
3946 constp = declspecs->const_p + TYPE_READONLY (element_type);
3947 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
3948 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
3949 if (pedantic && !flag_isoc99)
3951 if (constp > 1)
3952 pedwarn ("duplicate %<const%>");
3953 if (restrictp > 1)
3954 pedwarn ("duplicate %<restrict%>");
3955 if (volatilep > 1)
3956 pedwarn ("duplicate %<volatile%>");
3958 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
3959 type = TYPE_MAIN_VARIANT (type);
3960 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
3961 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
3962 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
3964 /* Warn about storage classes that are invalid for certain
3965 kinds of declarations (parameters, typenames, etc.). */
3967 if (funcdef_flag
3968 && (threadp
3969 || storage_class == csc_auto
3970 || storage_class == csc_register
3971 || storage_class == csc_typedef))
3973 if (storage_class == csc_auto
3974 && (pedantic || current_scope == file_scope))
3975 pedwarn ("function definition declared %<auto%>");
3976 if (storage_class == csc_register)
3977 error ("function definition declared %<register%>");
3978 if (storage_class == csc_typedef)
3979 error ("function definition declared %<typedef%>");
3980 if (threadp)
3981 error ("function definition declared %<__thread%>");
3982 threadp = false;
3983 if (storage_class == csc_auto
3984 || storage_class == csc_register
3985 || storage_class == csc_typedef)
3986 storage_class = csc_none;
3988 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
3990 if (decl_context == PARM && storage_class == csc_register)
3992 else
3994 switch (decl_context)
3996 case FIELD:
3997 error ("storage class specified for structure field %qs",
3998 name);
3999 break;
4000 case PARM:
4001 error ("storage class specified for parameter %qs", name);
4002 break;
4003 default:
4004 error ("storage class specified for typename");
4005 break;
4007 storage_class = csc_none;
4008 threadp = false;
4011 else if (storage_class == csc_extern
4012 && initialized
4013 && !funcdef_flag)
4015 /* 'extern' with initialization is invalid if not at file scope. */
4016 if (current_scope == file_scope)
4017 warning ("%qs initialized and declared %<extern%>", name);
4018 else
4019 error ("%qs has both %<extern%> and initializer", name);
4021 else if (current_scope == file_scope)
4023 if (storage_class == csc_auto)
4024 error ("file-scope declaration of %qs specifies %<auto%>", name);
4025 if (pedantic && storage_class == csc_register)
4026 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4028 else
4030 if (storage_class == csc_extern && funcdef_flag)
4031 error ("nested function %qs declared %<extern%>", name);
4032 else if (threadp && storage_class == csc_none)
4034 error ("function-scope %qs implicitly auto and declared "
4035 "%<__thread%>",
4036 name);
4037 threadp = false;
4041 /* Now figure out the structure of the declarator proper.
4042 Descend through it, creating more complex types, until we reach
4043 the declared identifier (or NULL_TREE, in an absolute declarator).
4044 At each stage we maintain an unqualified version of the type
4045 together with any qualifiers that should be applied to it with
4046 c_build_qualified_type; this way, array types including
4047 multidimensional array types are first built up in unqualified
4048 form and then the qualified form is created with
4049 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4051 while (declarator && declarator->kind != cdk_id)
4053 if (type == error_mark_node)
4055 declarator = declarator->declarator;
4056 continue;
4059 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4060 a cdk_pointer (for *...),
4061 a cdk_function (for ...(...)),
4062 a cdk_attrs (for nested attributes),
4063 or a cdk_id (for the name being declared
4064 or the place in an absolute declarator
4065 where the name was omitted).
4066 For the last case, we have just exited the loop.
4068 At this point, TYPE is the type of elements of an array,
4069 or for a function to return, or for a pointer to point to.
4070 After this sequence of ifs, TYPE is the type of the
4071 array or function or pointer, and DECLARATOR has had its
4072 outermost layer removed. */
4074 if (array_ptr_quals != TYPE_UNQUALIFIED
4075 || array_ptr_attrs != NULL_TREE
4076 || array_parm_static)
4078 /* Only the innermost declarator (making a parameter be of
4079 array type which is converted to pointer type)
4080 may have static or type qualifiers. */
4081 error ("static or type qualifiers in non-parameter array declarator");
4082 array_ptr_quals = TYPE_UNQUALIFIED;
4083 array_ptr_attrs = NULL_TREE;
4084 array_parm_static = 0;
4087 switch (declarator->kind)
4089 case cdk_attrs:
4091 /* A declarator with embedded attributes. */
4092 tree attrs = declarator->u.attrs;
4093 const struct c_declarator *inner_decl;
4094 int attr_flags = 0;
4095 declarator = declarator->declarator;
4096 inner_decl = declarator;
4097 while (inner_decl->kind == cdk_attrs)
4098 inner_decl = inner_decl->declarator;
4099 if (inner_decl->kind == cdk_id)
4100 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4101 else if (inner_decl->kind == cdk_function)
4102 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4103 else if (inner_decl->kind == cdk_array)
4104 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4105 returned_attrs = decl_attributes (&type,
4106 chainon (returned_attrs, attrs),
4107 attr_flags);
4108 break;
4110 case cdk_array:
4112 tree itype = NULL_TREE;
4113 tree size = declarator->u.array.dimen;
4114 /* The index is a signed object `sizetype' bits wide. */
4115 tree index_type = c_common_signed_type (sizetype);
4117 array_ptr_quals = declarator->u.array.quals;
4118 array_ptr_attrs = declarator->u.array.attrs;
4119 array_parm_static = declarator->u.array.static_p;
4121 declarator = declarator->declarator;
4123 /* Check for some types that there cannot be arrays of. */
4125 if (VOID_TYPE_P (type))
4127 error ("declaration of %qs as array of voids", name);
4128 type = error_mark_node;
4131 if (TREE_CODE (type) == FUNCTION_TYPE)
4133 error ("declaration of %qs as array of functions", name);
4134 type = error_mark_node;
4137 if (pedantic && !in_system_header && flexible_array_type_p (type))
4138 pedwarn ("invalid use of structure with flexible array member");
4140 if (size == error_mark_node)
4141 type = error_mark_node;
4143 if (type == error_mark_node)
4144 continue;
4146 /* If size was specified, set ITYPE to a range-type for
4147 that size. Otherwise, ITYPE remains null. finish_decl
4148 may figure it out from an initial value. */
4150 if (size)
4152 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4153 lvalue. */
4154 STRIP_TYPE_NOPS (size);
4156 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4158 error ("size of array %qs has non-integer type", name);
4159 size = integer_one_node;
4162 if (pedantic && integer_zerop (size))
4163 pedwarn ("ISO C forbids zero-size array %qs", name);
4165 if (TREE_CODE (size) == INTEGER_CST)
4167 constant_expression_warning (size);
4168 if (tree_int_cst_sgn (size) < 0)
4170 error ("size of array %qs is negative", name);
4171 size = integer_one_node;
4174 else if ((decl_context == NORMAL || decl_context == FIELD)
4175 && current_scope == file_scope)
4177 error ("variably modified %qs at file scope", name);
4178 size = integer_one_node;
4180 else
4182 /* Make sure the array size remains visibly
4183 nonconstant even if it is (eg) a const variable
4184 with known value. */
4185 size_varies = 1;
4187 if (!flag_isoc99 && pedantic)
4189 if (TREE_CONSTANT (size))
4190 pedwarn ("ISO C90 forbids array %qs whose size "
4191 "can%'t be evaluated",
4192 name);
4193 else
4194 pedwarn ("ISO C90 forbids variable-size array %qs",
4195 name);
4199 if (integer_zerop (size))
4201 /* A zero-length array cannot be represented with
4202 an unsigned index type, which is what we'll
4203 get with build_index_type. Create an
4204 open-ended range instead. */
4205 itype = build_range_type (sizetype, size, NULL_TREE);
4207 else
4209 /* Arrange for the SAVE_EXPR on the inside of the
4210 MINUS_EXPR, which allows the -1 to get folded
4211 with the +1 that happens when building TYPE_SIZE. */
4212 if (size_varies)
4213 size = variable_size (size);
4215 /* Compute the maximum valid index, that is, size
4216 - 1. Do the calculation in index_type, so that
4217 if it is a variable the computations will be
4218 done in the proper mode. */
4219 itype = fold (build2 (MINUS_EXPR, index_type,
4220 convert (index_type, size),
4221 convert (index_type,
4222 size_one_node)));
4224 /* If that overflowed, the array is too big. ???
4225 While a size of INT_MAX+1 technically shouldn't
4226 cause an overflow (because we subtract 1), the
4227 overflow is recorded during the conversion to
4228 index_type, before the subtraction. Handling
4229 this case seems like an unnecessary
4230 complication. */
4231 if (TREE_OVERFLOW (itype))
4233 error ("size of array %qs is too large", name);
4234 type = error_mark_node;
4235 continue;
4238 itype = build_index_type (itype);
4241 else if (decl_context == FIELD)
4243 if (pedantic && !flag_isoc99 && !in_system_header)
4244 pedwarn ("ISO C90 does not support flexible array members");
4246 /* ISO C99 Flexible array members are effectively
4247 identical to GCC's zero-length array extension. */
4248 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4251 /* Complain about arrays of incomplete types. */
4252 if (!COMPLETE_TYPE_P (type))
4254 error ("array type has incomplete element type");
4255 type = error_mark_node;
4257 else
4258 type = build_array_type (type, itype);
4260 if (type != error_mark_node)
4262 if (size_varies)
4264 if (size && TREE_CODE (size) == INTEGER_CST)
4265 type
4266 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4267 C_TYPE_VARIABLE_SIZE (type) = 1;
4270 /* The GCC extension for zero-length arrays differs from
4271 ISO flexible array members in that sizeof yields
4272 zero. */
4273 if (size && integer_zerop (size))
4275 TYPE_SIZE (type) = bitsize_zero_node;
4276 TYPE_SIZE_UNIT (type) = size_zero_node;
4280 if (decl_context != PARM
4281 && (array_ptr_quals != TYPE_UNQUALIFIED
4282 || array_ptr_attrs != NULL_TREE
4283 || array_parm_static))
4285 error ("static or type qualifiers in non-parameter array declarator");
4286 array_ptr_quals = TYPE_UNQUALIFIED;
4287 array_ptr_attrs = NULL_TREE;
4288 array_parm_static = 0;
4290 break;
4292 case cdk_function:
4294 /* Say it's a definition only for the declarator closest
4295 to the identifier, apart possibly from some
4296 attributes. */
4297 bool really_funcdef = false;
4298 tree arg_types;
4299 if (funcdef_flag)
4301 const struct c_declarator *t = declarator->declarator;
4302 while (t->kind == cdk_attrs)
4303 t = t->declarator;
4304 really_funcdef = (t->kind == cdk_id);
4307 /* Declaring a function type. Make sure we have a valid
4308 type for the function to return. */
4309 if (type == error_mark_node)
4310 continue;
4312 size_varies = 0;
4314 /* Warn about some types functions can't return. */
4315 if (TREE_CODE (type) == FUNCTION_TYPE)
4317 error ("%qs declared as function returning a function", name);
4318 type = integer_type_node;
4320 if (TREE_CODE (type) == ARRAY_TYPE)
4322 error ("%qs declared as function returning an array", name);
4323 type = integer_type_node;
4326 /* Construct the function type and go to the next
4327 inner layer of declarator. */
4328 arg_info = declarator->u.arg_info;
4329 arg_types = grokparms (arg_info, really_funcdef);
4331 /* Type qualifiers before the return type of the function
4332 qualify the return type, not the function type. */
4333 if (type_quals)
4335 /* Type qualifiers on a function return type are
4336 normally permitted by the standard but have no
4337 effect, so give a warning at -Wreturn-type.
4338 Qualifiers on a void return type are banned on
4339 function definitions in ISO C; GCC used to used
4340 them for noreturn functions. */
4341 if (VOID_TYPE_P (type) && really_funcdef)
4342 pedwarn ("function definition has qualified void return type");
4343 else if (warn_return_type)
4344 warning ("type qualifiers ignored on function return type");
4346 type = c_build_qualified_type (type, type_quals);
4348 type_quals = TYPE_UNQUALIFIED;
4350 type = build_function_type (type, arg_types);
4351 declarator = declarator->declarator;
4353 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4354 the formal parameter list of this FUNCTION_TYPE to point to
4355 the FUNCTION_TYPE node itself. */
4357 tree link;
4359 for (link = arg_info->tags;
4360 link;
4361 link = TREE_CHAIN (link))
4362 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4364 break;
4366 case cdk_pointer:
4368 /* Merge any constancy or volatility into the target type
4369 for the pointer. */
4371 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4372 && type_quals)
4373 pedwarn ("ISO C forbids qualified function types");
4374 if (type_quals)
4375 type = c_build_qualified_type (type, type_quals);
4376 size_varies = 0;
4378 type = build_pointer_type (type);
4380 /* Process type qualifiers (such as const or volatile)
4381 that were given inside the `*'. */
4382 type_quals = declarator->u.pointer_quals;
4384 declarator = declarator->declarator;
4385 break;
4387 default:
4388 gcc_unreachable ();
4392 /* Now TYPE has the actual type, apart from any qualifiers in
4393 TYPE_QUALS. */
4395 /* Check the type and width of a bit-field. */
4396 if (bitfield)
4397 check_bitfield_type_and_width (&type, width, orig_name);
4399 /* Did array size calculations overflow? */
4401 if (TREE_CODE (type) == ARRAY_TYPE
4402 && COMPLETE_TYPE_P (type)
4403 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4405 error ("size of array %qs is too large", name);
4406 /* If we proceed with the array type as it is, we'll eventually
4407 crash in tree_low_cst(). */
4408 type = error_mark_node;
4411 /* If this is declaring a typedef name, return a TYPE_DECL. */
4413 if (storage_class == csc_typedef)
4415 tree decl;
4416 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4417 && type_quals)
4418 pedwarn ("ISO C forbids qualified function types");
4419 if (type_quals)
4420 type = c_build_qualified_type (type, type_quals);
4421 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4422 if (declspecs->explicit_signed_p)
4423 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4424 decl_attributes (&decl, returned_attrs, 0);
4425 if (declspecs->inline_p)
4426 pedwarn ("%Jtypedef %qD declared %<inline%>", decl, decl);
4427 return decl;
4430 /* Detect the case of an array type of unspecified size
4431 which came, as such, direct from a typedef name.
4432 We must copy the type, so that each identifier gets
4433 a distinct type, so that each identifier's size can be
4434 controlled separately by its own initializer. */
4436 if (type != 0 && typedef_type != 0
4437 && TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == 0
4438 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
4440 type = build_array_type (TREE_TYPE (type), 0);
4441 if (size_varies)
4442 C_TYPE_VARIABLE_SIZE (type) = 1;
4445 /* If this is a type name (such as, in a cast or sizeof),
4446 compute the type and return it now. */
4448 if (decl_context == TYPENAME)
4450 /* Note that the grammar rejects storage classes in typenames
4451 and fields. */
4452 gcc_assert (storage_class == csc_none && !threadp
4453 && !declspecs->inline_p);
4454 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4455 && type_quals)
4456 pedwarn ("ISO C forbids const or volatile function types");
4457 if (type_quals)
4458 type = c_build_qualified_type (type, type_quals);
4459 decl_attributes (&type, returned_attrs, 0);
4460 return type;
4463 /* Aside from typedefs and type names (handle above),
4464 `void' at top level (not within pointer)
4465 is allowed only in public variables.
4466 We don't complain about parms either, but that is because
4467 a better error message can be made later. */
4469 if (VOID_TYPE_P (type) && decl_context != PARM
4470 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4471 && (storage_class == csc_extern
4472 || (current_scope == file_scope
4473 && !(storage_class == csc_static
4474 || storage_class == csc_register)))))
4476 error ("variable or field %qs declared void", name);
4477 type = integer_type_node;
4480 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4481 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4484 tree decl;
4486 if (decl_context == PARM)
4488 tree type_as_written;
4489 tree promoted_type;
4491 /* A parameter declared as an array of T is really a pointer to T.
4492 One declared as a function is really a pointer to a function. */
4494 if (TREE_CODE (type) == ARRAY_TYPE)
4496 /* Transfer const-ness of array into that of type pointed to. */
4497 type = TREE_TYPE (type);
4498 if (type_quals)
4499 type = c_build_qualified_type (type, type_quals);
4500 type = build_pointer_type (type);
4501 type_quals = array_ptr_quals;
4503 /* We don't yet implement attributes in this context. */
4504 if (array_ptr_attrs != NULL_TREE)
4505 warning ("attributes in parameter array declarator ignored");
4507 size_varies = 0;
4509 else if (TREE_CODE (type) == FUNCTION_TYPE)
4511 if (pedantic && type_quals)
4512 pedwarn ("ISO C forbids qualified function types");
4513 if (type_quals)
4514 type = c_build_qualified_type (type, type_quals);
4515 type = build_pointer_type (type);
4516 type_quals = TYPE_UNQUALIFIED;
4518 else if (type_quals)
4519 type = c_build_qualified_type (type, type_quals);
4521 type_as_written = type;
4523 decl = build_decl (PARM_DECL, declarator->u.id, type);
4524 if (size_varies)
4525 C_DECL_VARIABLE_SIZE (decl) = 1;
4527 /* Compute the type actually passed in the parmlist,
4528 for the case where there is no prototype.
4529 (For example, shorts and chars are passed as ints.)
4530 When there is a prototype, this is overridden later. */
4532 if (type == error_mark_node)
4533 promoted_type = type;
4534 else
4535 promoted_type = c_type_promotes_to (type);
4537 DECL_ARG_TYPE (decl) = promoted_type;
4538 DECL_ARG_TYPE_AS_WRITTEN (decl) = type_as_written;
4539 if (declspecs->inline_p)
4540 pedwarn ("%Jparameter %qD declared %<inline%>", decl, decl);
4542 else if (decl_context == FIELD)
4544 /* Note that the grammar rejects storage classes in typenames
4545 and fields. */
4546 gcc_assert (storage_class == csc_none && !threadp
4547 && !declspecs->inline_p);
4549 /* Structure field. It may not be a function. */
4551 if (TREE_CODE (type) == FUNCTION_TYPE)
4553 error ("field %qs declared as a function", name);
4554 type = build_pointer_type (type);
4556 else if (TREE_CODE (type) != ERROR_MARK
4557 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4559 error ("field %qs has incomplete type", name);
4560 type = error_mark_node;
4562 type = c_build_qualified_type (type, type_quals);
4563 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4564 DECL_NONADDRESSABLE_P (decl) = bitfield;
4566 if (size_varies)
4567 C_DECL_VARIABLE_SIZE (decl) = 1;
4569 else if (TREE_CODE (type) == FUNCTION_TYPE)
4571 if (storage_class == csc_register || threadp)
4573 error ("invalid storage class for function %qs", name);
4575 else if (current_scope != file_scope)
4577 /* Function declaration not at file scope. Storage
4578 classes other than `extern' are not allowed, C99
4579 6.7.1p5, and `extern' makes no difference. However,
4580 GCC allows 'auto', perhaps with 'inline', to support
4581 nested functions. */
4582 if (storage_class == csc_auto)
4584 if (pedantic)
4585 pedwarn ("invalid storage class for function %qs", name);
4587 else if (storage_class == csc_static)
4589 error ("invalid storage class for function %qs", name);
4590 if (funcdef_flag)
4591 storage_class = declspecs->storage_class = csc_none;
4592 else
4593 return 0;
4597 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4598 decl = build_decl_attribute_variant (decl, decl_attr);
4600 DECL_LANG_SPECIFIC (decl) = GGC_CNEW (struct lang_decl);
4602 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4603 pedwarn ("ISO C forbids qualified function types");
4605 /* GNU C interprets a volatile-qualified function type to indicate
4606 that the function does not return. */
4607 if ((type_quals & TYPE_QUAL_VOLATILE)
4608 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4609 warning ("%<noreturn%> function returns non-void value");
4611 /* Every function declaration is an external reference
4612 (DECL_EXTERNAL) except for those which are not at file
4613 scope and are explicitly declared "auto". This is
4614 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4615 GCC to signify a forward declaration of a nested function. */
4616 if (storage_class == csc_auto && current_scope != file_scope)
4617 DECL_EXTERNAL (decl) = 0;
4618 else
4619 DECL_EXTERNAL (decl) = 1;
4621 /* Record absence of global scope for `static' or `auto'. */
4622 TREE_PUBLIC (decl)
4623 = !(storage_class == csc_static || storage_class == csc_auto);
4625 /* For a function definition, record the argument information
4626 block where store_parm_decls will look for it. */
4627 if (funcdef_flag)
4628 current_function_arg_info = arg_info;
4630 if (declspecs->default_int_p)
4631 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4633 /* Record presence of `inline', if it is reasonable. */
4634 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4636 if (declspecs->inline_p)
4637 pedwarn ("cannot inline function %<main%>");
4639 else if (declspecs->inline_p)
4641 /* Record that the function is declared `inline'. */
4642 DECL_DECLARED_INLINE_P (decl) = 1;
4644 /* Do not mark bare declarations as DECL_INLINE. Doing so
4645 in the presence of multiple declarations can result in
4646 the abstract origin pointing between the declarations,
4647 which will confuse dwarf2out. */
4648 if (initialized)
4650 DECL_INLINE (decl) = 1;
4651 if (storage_class == csc_extern)
4652 current_extern_inline = 1;
4655 /* If -finline-functions, assume it can be inlined. This does
4656 two things: let the function be deferred until it is actually
4657 needed, and let dwarf2 know that the function is inlinable. */
4658 else if (flag_inline_trees == 2 && initialized)
4659 DECL_INLINE (decl) = 1;
4661 else
4663 /* It's a variable. */
4664 /* An uninitialized decl with `extern' is a reference. */
4665 int extern_ref = !initialized && storage_class == csc_extern;
4667 type = c_build_qualified_type (type, type_quals);
4669 /* C99 6.2.2p7: It is invalid (compile-time undefined
4670 behavior) to create an 'extern' declaration for a
4671 variable if there is a global declaration that is
4672 'static' and the global declaration is not visible.
4673 (If the static declaration _is_ currently visible,
4674 the 'extern' declaration is taken to refer to that decl.) */
4675 if (extern_ref && current_scope != file_scope)
4677 tree global_decl = identifier_global_value (declarator->u.id);
4678 tree visible_decl = lookup_name (declarator->u.id);
4680 if (global_decl
4681 && global_decl != visible_decl
4682 && TREE_CODE (global_decl) == VAR_DECL
4683 && !TREE_PUBLIC (global_decl))
4684 error ("variable previously declared %<static%> redeclared "
4685 "%<extern%>");
4688 decl = build_decl (VAR_DECL, declarator->u.id, type);
4689 if (size_varies)
4690 C_DECL_VARIABLE_SIZE (decl) = 1;
4692 if (declspecs->inline_p)
4693 pedwarn ("%Jvariable %qD declared %<inline%>", decl, decl);
4695 /* At file scope, an initialized extern declaration may follow
4696 a static declaration. In that case, DECL_EXTERNAL will be
4697 reset later in start_decl. */
4698 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4700 /* At file scope, the presence of a `static' or `register' storage
4701 class specifier, or the absence of all storage class specifiers
4702 makes this declaration a definition (perhaps tentative). Also,
4703 the absence of both `static' and `register' makes it public. */
4704 if (current_scope == file_scope)
4706 TREE_PUBLIC (decl) = !(storage_class == csc_static
4707 || storage_class == csc_register);
4708 TREE_STATIC (decl) = !extern_ref;
4710 /* Not at file scope, only `static' makes a static definition. */
4711 else
4713 TREE_STATIC (decl) = (storage_class == csc_static);
4714 TREE_PUBLIC (decl) = extern_ref;
4717 if (threadp)
4719 if (targetm.have_tls)
4720 DECL_THREAD_LOCAL (decl) = 1;
4721 else
4722 /* A mere warning is sure to result in improper semantics
4723 at runtime. Don't bother to allow this to compile. */
4724 error ("thread-local storage not supported for this target");
4728 /* Record `register' declaration for warnings on &
4729 and in case doing stupid register allocation. */
4731 if (storage_class == csc_register)
4733 C_DECL_REGISTER (decl) = 1;
4734 DECL_REGISTER (decl) = 1;
4737 /* Record constancy and volatility. */
4738 c_apply_type_quals_to_decl (type_quals, decl);
4740 /* If a type has volatile components, it should be stored in memory.
4741 Otherwise, the fact that those components are volatile
4742 will be ignored, and would even crash the compiler. */
4743 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl)))
4745 /* It is not an error for a structure with volatile fields to
4746 be declared register, but reset DECL_REGISTER since it
4747 cannot actually go in a register. */
4748 int was_reg = C_DECL_REGISTER (decl);
4749 C_DECL_REGISTER (decl) = 0;
4750 DECL_REGISTER (decl) = 0;
4751 c_mark_addressable (decl);
4752 C_DECL_REGISTER (decl) = was_reg;
4755 /* This is the earliest point at which we might know the assembler
4756 name of a variable. Thus, if it's known before this, die horribly. */
4757 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4759 decl_attributes (&decl, returned_attrs, 0);
4761 return decl;
4765 /* Decode the parameter-list info for a function type or function definition.
4766 The argument is the value returned by `get_parm_info' (or made in parse.y
4767 if there is an identifier list instead of a parameter decl list).
4768 These two functions are separate because when a function returns
4769 or receives functions then each is called multiple times but the order
4770 of calls is different. The last call to `grokparms' is always the one
4771 that contains the formal parameter names of a function definition.
4773 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4775 FUNCDEF_FLAG is true for a function definition, false for
4776 a mere declaration. A nonempty identifier-list gets an error message
4777 when FUNCDEF_FLAG is false. */
4779 static tree
4780 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4782 tree arg_types = arg_info->types;
4784 if (warn_strict_prototypes && arg_types == 0 && !funcdef_flag
4785 && !in_system_header)
4786 warning ("function declaration isn%'t a prototype");
4788 if (arg_types == error_mark_node)
4789 return 0; /* don't set TYPE_ARG_TYPES in this case */
4791 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
4793 if (!funcdef_flag)
4794 pedwarn ("parameter names (without types) in function declaration");
4796 arg_info->parms = arg_info->types;
4797 arg_info->types = 0;
4798 return 0;
4800 else
4802 tree parm, type, typelt;
4803 unsigned int parmno;
4805 /* If there is a parameter of incomplete type in a definition,
4806 this is an error. In a declaration this is valid, and a
4807 struct or union type may be completed later, before any calls
4808 or definition of the function. In the case where the tag was
4809 first declared within the parameter list, a warning has
4810 already been given. If a parameter has void type, then
4811 however the function cannot be defined or called, so
4812 warn. */
4814 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
4815 parm;
4816 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
4818 type = TREE_VALUE (typelt);
4819 if (type == error_mark_node)
4820 continue;
4822 if (!COMPLETE_TYPE_P (type))
4824 if (funcdef_flag)
4826 if (DECL_NAME (parm))
4827 error ("%Jparameter %u (%qD) has incomplete type",
4828 parm, parmno, parm);
4829 else
4830 error ("%Jparameter %u has incomplete type",
4831 parm, parmno);
4833 TREE_VALUE (typelt) = error_mark_node;
4834 TREE_TYPE (parm) = error_mark_node;
4836 else if (VOID_TYPE_P (type))
4838 if (DECL_NAME (parm))
4839 warning ("%Jparameter %u (%qD) has void type",
4840 parm, parmno, parm);
4841 else
4842 warning ("%Jparameter %u has void type",
4843 parm, parmno);
4847 return arg_types;
4851 /* Take apart the current scope and return a c_arg_info structure with
4852 info on a parameter list just parsed.
4854 This structure is later fed to 'grokparms' and 'store_parm_decls'.
4856 ELLIPSIS being true means the argument list ended in '...' so don't
4857 append a sentinel (void_list_node) to the end of the type-list. */
4859 struct c_arg_info *
4860 get_parm_info (bool ellipsis)
4862 struct c_binding *b = current_scope->bindings;
4863 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
4864 struct c_arg_info);
4865 tree parms = 0;
4866 tree tags = 0;
4867 tree types = 0;
4868 tree others = 0;
4870 static bool explained_incomplete_types = false;
4871 bool gave_void_only_once_err = false;
4873 arg_info->parms = 0;
4874 arg_info->tags = 0;
4875 arg_info->types = 0;
4876 arg_info->others = 0;
4878 /* The bindings in this scope must not get put into a block.
4879 We will take care of deleting the binding nodes. */
4880 current_scope->bindings = 0;
4882 /* This function is only called if there was *something* on the
4883 parameter list. */
4884 gcc_assert (b);
4886 /* A parameter list consisting solely of 'void' indicates that the
4887 function takes no arguments. But if the 'void' is qualified
4888 (by 'const' or 'volatile'), or has a storage class specifier
4889 ('register'), then the behavior is undefined; issue an error.
4890 Typedefs for 'void' are OK (see DR#157). */
4891 if (b->prev == 0 /* one binding */
4892 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
4893 && !DECL_NAME (b->decl) /* anonymous */
4894 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
4896 if (TREE_THIS_VOLATILE (b->decl)
4897 || TREE_READONLY (b->decl)
4898 || C_DECL_REGISTER (b->decl))
4899 error ("%<void%> as only parameter may not be qualified");
4901 /* There cannot be an ellipsis. */
4902 if (ellipsis)
4903 error ("%<void%> must be the only parameter");
4905 arg_info->types = void_list_node;
4906 return arg_info;
4909 if (!ellipsis)
4910 types = void_list_node;
4912 /* Break up the bindings list into parms, tags, types, and others;
4913 apply sanity checks; purge the name-to-decl bindings. */
4914 while (b)
4916 tree decl = b->decl;
4917 tree type = TREE_TYPE (decl);
4918 const char *keyword;
4920 switch (TREE_CODE (decl))
4922 case PARM_DECL:
4923 if (b->id)
4925 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
4926 I_SYMBOL_BINDING (b->id) = b->shadowed;
4929 /* Check for forward decls that never got their actual decl. */
4930 if (TREE_ASM_WRITTEN (decl))
4931 error ("%Jparameter %qD has just a forward declaration",
4932 decl, decl);
4933 /* Check for (..., void, ...) and issue an error. */
4934 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
4936 if (!gave_void_only_once_err)
4938 error ("%<void%> must be the only parameter");
4939 gave_void_only_once_err = true;
4942 else
4944 /* Valid parameter, add it to the list. */
4945 TREE_CHAIN (decl) = parms;
4946 parms = decl;
4948 /* Since there is a prototype, args are passed in their
4949 declared types. The back end may override this later. */
4950 DECL_ARG_TYPE (decl) = type;
4951 types = tree_cons (0, type, types);
4953 break;
4955 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
4956 case UNION_TYPE: keyword = "union"; goto tag;
4957 case RECORD_TYPE: keyword = "struct"; goto tag;
4958 tag:
4959 /* Types may not have tag-names, in which case the type
4960 appears in the bindings list with b->id NULL. */
4961 if (b->id)
4963 gcc_assert (I_TAG_BINDING (b->id) == b);
4964 I_TAG_BINDING (b->id) = b->shadowed;
4967 /* Warn about any struct, union or enum tags defined in a
4968 parameter list. The scope of such types is limited to
4969 the parameter list, which is rarely if ever desirable
4970 (it's impossible to call such a function with type-
4971 correct arguments). An anonymous union parm type is
4972 meaningful as a GNU extension, so don't warn for that. */
4973 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
4975 if (b->id)
4976 /* The %s will be one of 'struct', 'union', or 'enum'. */
4977 warning ("%<%s %E%> declared inside parameter list",
4978 keyword, b->id);
4979 else
4980 /* The %s will be one of 'struct', 'union', or 'enum'. */
4981 warning ("anonymous %s declared inside parameter list",
4982 keyword);
4984 if (!explained_incomplete_types)
4986 warning ("its scope is only this definition or declaration,"
4987 " which is probably not what you want");
4988 explained_incomplete_types = true;
4992 tags = tree_cons (b->id, decl, tags);
4993 break;
4995 case CONST_DECL:
4996 case TYPE_DECL:
4997 case FUNCTION_DECL:
4998 /* CONST_DECLs appear here when we have an embedded enum,
4999 and TYPE_DECLs appear here when we have an embedded struct
5000 or union. No warnings for this - we already warned about the
5001 type itself. FUNCTION_DECLs appear when there is an implicit
5002 function declaration in the parameter list. */
5004 TREE_CHAIN (decl) = others;
5005 others = decl;
5006 /* fall through */
5008 case ERROR_MARK:
5009 /* error_mark_node appears here when we have an undeclared
5010 variable. Just throw it away. */
5011 if (b->id)
5013 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5014 I_SYMBOL_BINDING (b->id) = b->shadowed;
5016 break;
5018 /* Other things that might be encountered. */
5019 case LABEL_DECL:
5020 case VAR_DECL:
5021 default:
5022 gcc_unreachable ();
5025 b = free_binding_and_advance (b);
5028 arg_info->parms = parms;
5029 arg_info->tags = tags;
5030 arg_info->types = types;
5031 arg_info->others = others;
5032 return arg_info;
5035 /* Get the struct, enum or union (CODE says which) with tag NAME.
5036 Define the tag as a forward-reference if it is not defined.
5037 Return a c_typespec structure for the type specifier. */
5039 struct c_typespec
5040 parser_xref_tag (enum tree_code code, tree name)
5042 struct c_typespec ret;
5043 /* If a cross reference is requested, look up the type
5044 already defined for this tag and return it. */
5046 tree ref = lookup_tag (code, name, 0);
5047 /* If this is the right type of tag, return what we found.
5048 (This reference will be shadowed by shadow_tag later if appropriate.)
5049 If this is the wrong type of tag, do not return it. If it was the
5050 wrong type in the same scope, we will have had an error
5051 message already; if in a different scope and declaring
5052 a name, pending_xref_error will give an error message; but if in a
5053 different scope and not declaring a name, this tag should
5054 shadow the previous declaration of a different type of tag, and
5055 this would not work properly if we return the reference found.
5056 (For example, with "struct foo" in an outer scope, "union foo;"
5057 must shadow that tag with a new one of union type.) */
5058 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5059 if (ref && TREE_CODE (ref) == code)
5061 ret.spec = ref;
5062 return ret;
5065 /* If no such tag is yet defined, create a forward-reference node
5066 and record it as the "definition".
5067 When a real declaration of this type is found,
5068 the forward-reference will be altered into a real type. */
5070 ref = make_node (code);
5071 if (code == ENUMERAL_TYPE)
5073 /* Give the type a default layout like unsigned int
5074 to avoid crashing if it does not get defined. */
5075 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5076 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5077 TYPE_USER_ALIGN (ref) = 0;
5078 TYPE_UNSIGNED (ref) = 1;
5079 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5080 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5081 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5084 pushtag (name, ref);
5086 ret.spec = ref;
5087 return ret;
5090 /* Get the struct, enum or union (CODE says which) with tag NAME.
5091 Define the tag as a forward-reference if it is not defined.
5092 Return a tree for the type. */
5094 tree
5095 xref_tag (enum tree_code code, tree name)
5097 return parser_xref_tag (code, name).spec;
5100 /* Make sure that the tag NAME is defined *in the current scope*
5101 at least as a forward reference.
5102 CODE says which kind of tag NAME ought to be. */
5104 tree
5105 start_struct (enum tree_code code, tree name)
5107 /* If there is already a tag defined at this scope
5108 (as a forward reference), just return it. */
5110 tree ref = 0;
5112 if (name != 0)
5113 ref = lookup_tag (code, name, 1);
5114 if (ref && TREE_CODE (ref) == code)
5116 if (TYPE_SIZE (ref))
5118 if (code == UNION_TYPE)
5119 error ("redefinition of %<union %s%>", IDENTIFIER_POINTER (name));
5120 else
5121 error ("redefinition of %<struct %s%>", IDENTIFIER_POINTER (name));
5123 else if (C_TYPE_BEING_DEFINED (ref))
5125 if (code == UNION_TYPE)
5126 error ("nested redefinition of %<union %s%>",
5127 IDENTIFIER_POINTER (name));
5128 else
5129 error ("nested redefinition of %<struct %s%>",
5130 IDENTIFIER_POINTER (name));
5133 else
5135 /* Otherwise create a forward-reference just so the tag is in scope. */
5137 ref = make_node (code);
5138 pushtag (name, ref);
5141 C_TYPE_BEING_DEFINED (ref) = 1;
5142 TYPE_PACKED (ref) = flag_pack_struct;
5143 return ref;
5146 /* Process the specs, declarator and width (NULL if omitted)
5147 of a structure component, returning a FIELD_DECL node.
5148 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5150 This is done during the parsing of the struct declaration.
5151 The FIELD_DECL nodes are chained together and the lot of them
5152 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5154 tree
5155 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5156 tree width)
5158 tree value;
5160 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5161 && width == NULL_TREE)
5163 /* This is an unnamed decl.
5165 If we have something of the form "union { list } ;" then this
5166 is the anonymous union extension. Similarly for struct.
5168 If this is something of the form "struct foo;", then
5169 If MS extensions are enabled, this is handled as an
5170 anonymous struct.
5171 Otherwise this is a forward declaration of a structure tag.
5173 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5174 If MS extensions are enabled and foo names a structure, then
5175 again this is an anonymous struct.
5176 Otherwise this is an error.
5178 Oh what a horrid tangled web we weave. I wonder if MS consciously
5179 took this from Plan 9 or if it was an accident of implementation
5180 that took root before someone noticed the bug... */
5182 tree type = declspecs->type;
5183 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5184 || TREE_CODE (type) == UNION_TYPE);
5185 bool ok = false;
5187 if (type_ok
5188 && (flag_ms_extensions || !declspecs->typedef_p))
5190 if (flag_ms_extensions)
5191 ok = true;
5192 else if (flag_iso)
5193 ok = false;
5194 else if (TYPE_NAME (type) == NULL)
5195 ok = true;
5196 else
5197 ok = false;
5199 if (!ok)
5201 pedwarn ("declaration does not declare anything");
5202 return NULL_TREE;
5204 if (pedantic)
5205 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5208 value = grokdeclarator (declarator, declspecs, FIELD, false,
5209 width ? &width : NULL);
5211 finish_decl (value, NULL_TREE, NULL_TREE);
5212 DECL_INITIAL (value) = width;
5214 return value;
5217 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5218 the list such that this does not present a problem later. */
5220 static void
5221 detect_field_duplicates (tree fieldlist)
5223 tree x, y;
5224 int timeout = 10;
5226 /* First, see if there are more than "a few" fields.
5227 This is trivially true if there are zero or one fields. */
5228 if (!fieldlist)
5229 return;
5230 x = TREE_CHAIN (fieldlist);
5231 if (!x)
5232 return;
5233 do {
5234 timeout--;
5235 x = TREE_CHAIN (x);
5236 } while (timeout > 0 && x);
5238 /* If there were "few" fields, avoid the overhead of allocating
5239 a hash table. Instead just do the nested traversal thing. */
5240 if (timeout > 0)
5242 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5243 if (DECL_NAME (x))
5245 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5246 if (DECL_NAME (y) == DECL_NAME (x))
5248 error ("%Jduplicate member %qD", x, x);
5249 DECL_NAME (x) = NULL_TREE;
5253 else
5255 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5256 void **slot;
5258 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5259 if ((y = DECL_NAME (x)) != 0)
5261 slot = htab_find_slot (htab, y, INSERT);
5262 if (*slot)
5264 error ("%Jduplicate member %qD", x, x);
5265 DECL_NAME (x) = NULL_TREE;
5267 *slot = y;
5270 htab_delete (htab);
5274 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5275 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5276 ATTRIBUTES are attributes to be applied to the structure. */
5278 tree
5279 finish_struct (tree t, tree fieldlist, tree attributes)
5281 tree x;
5282 bool toplevel = file_scope == current_scope;
5283 int saw_named_field;
5285 /* If this type was previously laid out as a forward reference,
5286 make sure we lay it out again. */
5288 TYPE_SIZE (t) = 0;
5290 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5292 if (pedantic)
5294 for (x = fieldlist; x; x = TREE_CHAIN (x))
5295 if (DECL_NAME (x) != 0)
5296 break;
5298 if (x == 0)
5300 if (TREE_CODE (t) == UNION_TYPE)
5302 if (fieldlist)
5303 pedwarn ("union has no named members");
5304 else
5305 pedwarn ("union has no members");
5307 else
5309 if (fieldlist)
5310 pedwarn ("struct has no named members");
5311 else
5312 pedwarn ("struct has no members");
5317 /* Install struct as DECL_CONTEXT of each field decl.
5318 Also process specified field sizes, found in the DECL_INITIAL,
5319 storing 0 there after the type has been changed to precision equal
5320 to its width, rather than the precision of the specified standard
5321 type. (Correct layout requires the original type to have been preserved
5322 until now.) */
5324 saw_named_field = 0;
5325 for (x = fieldlist; x; x = TREE_CHAIN (x))
5327 DECL_CONTEXT (x) = t;
5328 DECL_PACKED (x) |= TYPE_PACKED (t);
5330 /* If any field is const, the structure type is pseudo-const. */
5331 if (TREE_READONLY (x))
5332 C_TYPE_FIELDS_READONLY (t) = 1;
5333 else
5335 /* A field that is pseudo-const makes the structure likewise. */
5336 tree t1 = TREE_TYPE (x);
5337 while (TREE_CODE (t1) == ARRAY_TYPE)
5338 t1 = TREE_TYPE (t1);
5339 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5340 && C_TYPE_FIELDS_READONLY (t1))
5341 C_TYPE_FIELDS_READONLY (t) = 1;
5344 /* Any field that is volatile means variables of this type must be
5345 treated in some ways as volatile. */
5346 if (TREE_THIS_VOLATILE (x))
5347 C_TYPE_FIELDS_VOLATILE (t) = 1;
5349 /* Any field of nominal variable size implies structure is too. */
5350 if (C_DECL_VARIABLE_SIZE (x))
5351 C_TYPE_VARIABLE_SIZE (t) = 1;
5353 if (DECL_INITIAL (x))
5355 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5356 DECL_SIZE (x) = bitsize_int (width);
5357 DECL_BIT_FIELD (x) = 1;
5358 SET_DECL_C_BIT_FIELD (x);
5361 /* Detect flexible array member in an invalid context. */
5362 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5363 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5364 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5365 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5367 if (TREE_CODE (t) == UNION_TYPE)
5369 error ("%Jflexible array member in union", x);
5370 TREE_TYPE (x) = error_mark_node;
5372 else if (TREE_CHAIN (x) != NULL_TREE)
5374 error ("%Jflexible array member not at end of struct", x);
5375 TREE_TYPE (x) = error_mark_node;
5377 else if (!saw_named_field)
5379 error ("%Jflexible array member in otherwise empty struct", x);
5380 TREE_TYPE (x) = error_mark_node;
5384 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5385 && flexible_array_type_p (TREE_TYPE (x)))
5386 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5388 if (DECL_NAME (x))
5389 saw_named_field = 1;
5392 detect_field_duplicates (fieldlist);
5394 /* Now we have the nearly final fieldlist. Record it,
5395 then lay out the structure or union (including the fields). */
5397 TYPE_FIELDS (t) = fieldlist;
5399 layout_type (t);
5401 /* Give bit-fields their proper types. */
5403 tree *fieldlistp = &fieldlist;
5404 while (*fieldlistp)
5405 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5406 && TREE_TYPE (*fieldlistp) != error_mark_node)
5408 unsigned HOST_WIDE_INT width
5409 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5410 tree type = TREE_TYPE (*fieldlistp);
5411 if (width != TYPE_PRECISION (type))
5413 TREE_TYPE (*fieldlistp)
5414 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5415 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5417 DECL_INITIAL (*fieldlistp) = 0;
5419 else
5420 fieldlistp = &TREE_CHAIN (*fieldlistp);
5423 /* Now we have the truly final field list.
5424 Store it in this type and in the variants. */
5426 TYPE_FIELDS (t) = fieldlist;
5428 /* If there are lots of fields, sort so we can look through them fast.
5429 We arbitrarily consider 16 or more elts to be "a lot". */
5432 int len = 0;
5434 for (x = fieldlist; x; x = TREE_CHAIN (x))
5436 if (len > 15 || DECL_NAME (x) == NULL)
5437 break;
5438 len += 1;
5441 if (len > 15)
5443 tree *field_array;
5444 struct lang_type *space;
5445 struct sorted_fields_type *space2;
5447 len += list_length (x);
5449 /* Use the same allocation policy here that make_node uses, to
5450 ensure that this lives as long as the rest of the struct decl.
5451 All decls in an inline function need to be saved. */
5453 space = GGC_CNEW (struct lang_type);
5454 space2 = GGC_NEWVAR (struct sorted_fields_type,
5455 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5457 len = 0;
5458 space->s = space2;
5459 field_array = &space2->elts[0];
5460 for (x = fieldlist; x; x = TREE_CHAIN (x))
5462 field_array[len++] = x;
5464 /* If there is anonymous struct or union, break out of the loop. */
5465 if (DECL_NAME (x) == NULL)
5466 break;
5468 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5469 if (x == NULL)
5471 TYPE_LANG_SPECIFIC (t) = space;
5472 TYPE_LANG_SPECIFIC (t)->s->len = len;
5473 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5474 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5479 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5481 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5482 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5483 TYPE_ALIGN (x) = TYPE_ALIGN (t);
5484 TYPE_USER_ALIGN (x) = TYPE_USER_ALIGN (t);
5485 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5486 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5487 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5490 /* If this was supposed to be a transparent union, but we can't
5491 make it one, warn and turn off the flag. */
5492 if (TREE_CODE (t) == UNION_TYPE
5493 && TYPE_TRANSPARENT_UNION (t)
5494 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5496 TYPE_TRANSPARENT_UNION (t) = 0;
5497 warning ("union cannot be made transparent");
5500 /* If this structure or union completes the type of any previous
5501 variable declaration, lay it out and output its rtl. */
5502 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5504 x = TREE_CHAIN (x))
5506 tree decl = TREE_VALUE (x);
5507 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5508 layout_array_type (TREE_TYPE (decl));
5509 if (TREE_CODE (decl) != TYPE_DECL)
5511 layout_decl (decl, 0);
5512 if (c_dialect_objc ())
5513 objc_check_decl (decl);
5514 rest_of_decl_compilation (decl, toplevel, 0);
5515 if (!toplevel)
5516 expand_decl (decl);
5519 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5521 /* Finish debugging output for this type. */
5522 rest_of_type_compilation (t, toplevel);
5524 /* If we're inside a function proper, i.e. not file-scope and not still
5525 parsing parameters, then arrange for the size of a variable sized type
5526 to be bound now. */
5527 if (cur_stmt_list && variably_modified_type_p (t, NULL))
5528 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5530 return t;
5533 /* Lay out the type T, and its element type, and so on. */
5535 static void
5536 layout_array_type (tree t)
5538 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5539 layout_array_type (TREE_TYPE (t));
5540 layout_type (t);
5543 /* Begin compiling the definition of an enumeration type.
5544 NAME is its name (or null if anonymous).
5545 Returns the type object, as yet incomplete.
5546 Also records info about it so that build_enumerator
5547 may be used to declare the individual values as they are read. */
5549 tree
5550 start_enum (tree name)
5552 tree enumtype = 0;
5554 /* If this is the real definition for a previous forward reference,
5555 fill in the contents in the same object that used to be the
5556 forward reference. */
5558 if (name != 0)
5559 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5561 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5563 enumtype = make_node (ENUMERAL_TYPE);
5564 pushtag (name, enumtype);
5567 if (C_TYPE_BEING_DEFINED (enumtype))
5568 error ("nested redefinition of %<enum %s%>", IDENTIFIER_POINTER (name));
5570 C_TYPE_BEING_DEFINED (enumtype) = 1;
5572 if (TYPE_VALUES (enumtype) != 0)
5574 /* This enum is a named one that has been declared already. */
5575 error ("redeclaration of %<enum %s%>", IDENTIFIER_POINTER (name));
5577 /* Completely replace its old definition.
5578 The old enumerators remain defined, however. */
5579 TYPE_VALUES (enumtype) = 0;
5582 enum_next_value = integer_zero_node;
5583 enum_overflow = 0;
5585 if (flag_short_enums)
5586 TYPE_PACKED (enumtype) = 1;
5588 return enumtype;
5591 /* After processing and defining all the values of an enumeration type,
5592 install their decls in the enumeration type and finish it off.
5593 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5594 and ATTRIBUTES are the specified attributes.
5595 Returns ENUMTYPE. */
5597 tree
5598 finish_enum (tree enumtype, tree values, tree attributes)
5600 tree pair, tem;
5601 tree minnode = 0, maxnode = 0;
5602 int precision, unsign;
5603 bool toplevel = (file_scope == current_scope);
5604 struct lang_type *lt;
5606 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5608 /* Calculate the maximum value of any enumerator in this type. */
5610 if (values == error_mark_node)
5611 minnode = maxnode = integer_zero_node;
5612 else
5614 minnode = maxnode = TREE_VALUE (values);
5615 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5617 tree value = TREE_VALUE (pair);
5618 if (tree_int_cst_lt (maxnode, value))
5619 maxnode = value;
5620 if (tree_int_cst_lt (value, minnode))
5621 minnode = value;
5625 /* Construct the final type of this enumeration. It is the same
5626 as one of the integral types - the narrowest one that fits, except
5627 that normally we only go as narrow as int - and signed iff any of
5628 the values are negative. */
5629 unsign = (tree_int_cst_sgn (minnode) >= 0);
5630 precision = MAX (min_precision (minnode, unsign),
5631 min_precision (maxnode, unsign));
5633 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5635 tem = c_common_type_for_size (precision, unsign);
5636 if (tem == NULL)
5638 warning ("enumeration values exceed range of largest integer");
5639 tem = long_long_integer_type_node;
5642 else
5643 tem = unsign ? unsigned_type_node : integer_type_node;
5645 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5646 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5647 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5648 TYPE_SIZE (enumtype) = 0;
5650 /* If the precision of the type was specific with an attribute and it
5651 was too small, give an error. Otherwise, use it. */
5652 if (TYPE_PRECISION (enumtype))
5654 if (precision > TYPE_PRECISION (enumtype))
5655 error ("specified mode too small for enumeral values");
5657 else
5658 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5660 layout_type (enumtype);
5662 if (values != error_mark_node)
5664 /* Change the type of the enumerators to be the enum type. We
5665 need to do this irrespective of the size of the enum, for
5666 proper type checking. Replace the DECL_INITIALs of the
5667 enumerators, and the value slots of the list, with copies
5668 that have the enum type; they cannot be modified in place
5669 because they may be shared (e.g. integer_zero_node) Finally,
5670 change the purpose slots to point to the names of the decls. */
5671 for (pair = values; pair; pair = TREE_CHAIN (pair))
5673 tree enu = TREE_PURPOSE (pair);
5674 tree ini = DECL_INITIAL (enu);
5676 TREE_TYPE (enu) = enumtype;
5678 /* The ISO C Standard mandates enumerators to have type int,
5679 even though the underlying type of an enum type is
5680 unspecified. Here we convert any enumerators that fit in
5681 an int to type int, to avoid promotions to unsigned types
5682 when comparing integers with enumerators that fit in the
5683 int range. When -pedantic is given, build_enumerator()
5684 would have already taken care of those that don't fit. */
5685 if (int_fits_type_p (ini, integer_type_node))
5686 tem = integer_type_node;
5687 else
5688 tem = enumtype;
5689 ini = convert (tem, ini);
5691 DECL_INITIAL (enu) = ini;
5692 TREE_PURPOSE (pair) = DECL_NAME (enu);
5693 TREE_VALUE (pair) = ini;
5696 TYPE_VALUES (enumtype) = values;
5699 /* Record the min/max values so that we can warn about bit-field
5700 enumerations that are too small for the values. */
5701 lt = GGC_CNEW (struct lang_type);
5702 lt->enum_min = minnode;
5703 lt->enum_max = maxnode;
5704 TYPE_LANG_SPECIFIC (enumtype) = lt;
5706 /* Fix up all variant types of this enum type. */
5707 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5709 if (tem == enumtype)
5710 continue;
5711 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5712 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5713 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5714 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5715 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5716 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5717 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5718 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5719 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5720 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5721 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5724 /* Finish debugging output for this type. */
5725 rest_of_type_compilation (enumtype, toplevel);
5727 return enumtype;
5730 /* Build and install a CONST_DECL for one value of the
5731 current enumeration type (one that was begun with start_enum).
5732 Return a tree-list containing the CONST_DECL and its value.
5733 Assignment of sequential values by default is handled here. */
5735 tree
5736 build_enumerator (tree name, tree value)
5738 tree decl, type;
5740 /* Validate and default VALUE. */
5742 /* Remove no-op casts from the value. */
5743 if (value)
5744 STRIP_TYPE_NOPS (value);
5746 if (value != 0)
5748 /* Don't issue more errors for error_mark_node (i.e. an
5749 undeclared identifier) - just ignore the value expression. */
5750 if (value == error_mark_node)
5751 value = 0;
5752 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5753 || TREE_CODE (value) != INTEGER_CST)
5755 error ("enumerator value for %qE is not an integer constant", name);
5756 value = 0;
5758 else
5760 value = default_conversion (value);
5761 constant_expression_warning (value);
5765 /* Default based on previous value. */
5766 /* It should no longer be possible to have NON_LVALUE_EXPR
5767 in the default. */
5768 if (value == 0)
5770 value = enum_next_value;
5771 if (enum_overflow)
5772 error ("overflow in enumeration values");
5775 if (pedantic && !int_fits_type_p (value, integer_type_node))
5777 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5778 /* XXX This causes -pedantic to change the meaning of the program.
5779 Remove? -zw 2004-03-15 */
5780 value = convert (integer_type_node, value);
5783 /* Set basis for default for next value. */
5784 enum_next_value = build_binary_op (PLUS_EXPR, value, integer_one_node, 0);
5785 enum_overflow = tree_int_cst_lt (enum_next_value, value);
5787 /* Now create a declaration for the enum value name. */
5789 type = TREE_TYPE (value);
5790 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
5791 TYPE_PRECISION (integer_type_node)),
5792 (TYPE_PRECISION (type)
5793 >= TYPE_PRECISION (integer_type_node)
5794 && TYPE_UNSIGNED (type)));
5796 decl = build_decl (CONST_DECL, name, type);
5797 DECL_INITIAL (decl) = convert (type, value);
5798 pushdecl (decl);
5800 return tree_cons (decl, value, NULL_TREE);
5804 /* Create the FUNCTION_DECL for a function definition.
5805 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
5806 the declaration; they describe the function's name and the type it returns,
5807 but twisted together in a fashion that parallels the syntax of C.
5809 This function creates a binding context for the function body
5810 as well as setting up the FUNCTION_DECL in current_function_decl.
5812 Returns 1 on success. If the DECLARATOR is not suitable for a function
5813 (it defines a datum instead), we return 0, which tells
5814 yyparse to report a parse error. */
5817 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
5818 tree attributes)
5820 tree decl1, old_decl;
5821 tree restype, resdecl;
5822 struct c_label_context_se *nstack_se;
5823 struct c_label_context_vm *nstack_vm;
5825 current_function_returns_value = 0; /* Assume, until we see it does. */
5826 current_function_returns_null = 0;
5827 current_function_returns_abnormally = 0;
5828 warn_about_return_type = 0;
5829 current_extern_inline = 0;
5830 c_switch_stack = NULL;
5832 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
5833 nstack_se->labels_def = NULL;
5834 nstack_se->labels_used = NULL;
5835 nstack_se->next = label_context_stack_se;
5836 label_context_stack_se = nstack_se;
5838 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
5839 nstack_vm->labels_def = NULL;
5840 nstack_vm->labels_used = NULL;
5841 nstack_vm->scope = 0;
5842 nstack_vm->next = label_context_stack_vm;
5843 label_context_stack_vm = nstack_vm;
5845 /* Indicate no valid break/continue context by setting these variables
5846 to some non-null, non-label value. We'll notice and emit the proper
5847 error message in c_finish_bc_stmt. */
5848 c_break_label = c_cont_label = size_zero_node;
5850 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL);
5852 /* If the declarator is not suitable for a function definition,
5853 cause a syntax error. */
5854 if (decl1 == 0)
5856 label_context_stack_se = label_context_stack_se->next;
5857 label_context_stack_vm = label_context_stack_vm->next;
5858 return 0;
5861 decl_attributes (&decl1, attributes, 0);
5863 if (DECL_DECLARED_INLINE_P (decl1)
5864 && DECL_UNINLINABLE (decl1)
5865 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
5866 warning ("%Jinline function %qD given attribute noinline", decl1, decl1);
5868 announce_function (decl1);
5870 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
5872 error ("return type is an incomplete type");
5873 /* Make it return void instead. */
5874 TREE_TYPE (decl1)
5875 = build_function_type (void_type_node,
5876 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
5879 if (warn_about_return_type)
5880 pedwarn_c99 ("return type defaults to %<int%>");
5882 /* Make the init_value nonzero so pushdecl knows this is not tentative.
5883 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
5884 DECL_INITIAL (decl1) = error_mark_node;
5886 /* If this definition isn't a prototype and we had a prototype declaration
5887 before, copy the arg type info from that prototype. */
5888 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
5889 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
5890 old_decl = 0;
5891 current_function_prototype_locus = UNKNOWN_LOCATION;
5892 current_function_prototype_built_in = false;
5893 current_function_prototype_arg_types = NULL_TREE;
5894 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
5896 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
5897 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5898 TREE_TYPE (TREE_TYPE (old_decl))))
5900 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
5901 TREE_TYPE (decl1));
5902 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
5903 current_function_prototype_built_in
5904 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
5905 current_function_prototype_arg_types
5906 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
5908 if (TREE_PUBLIC (decl1))
5910 /* If there is an external prototype declaration of this
5911 function, record its location but do not copy information
5912 to this decl. This may be an invisible declaration
5913 (built-in or in a scope which has finished) or simply
5914 have more refined argument types than any declaration
5915 found above. */
5916 struct c_binding *b;
5917 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
5918 if (B_IN_SCOPE (b, external_scope))
5919 break;
5920 if (b)
5922 tree ext_decl, ext_type;
5923 ext_decl = b->decl;
5924 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
5925 if (TREE_CODE (ext_type) == FUNCTION_TYPE
5926 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
5927 TREE_TYPE (ext_type)))
5929 current_function_prototype_locus
5930 = DECL_SOURCE_LOCATION (ext_decl);
5931 current_function_prototype_built_in
5932 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
5933 current_function_prototype_arg_types
5934 = TYPE_ARG_TYPES (ext_type);
5940 /* Optionally warn of old-fashioned def with no previous prototype. */
5941 if (warn_strict_prototypes
5942 && old_decl != error_mark_node
5943 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
5944 && C_DECL_ISNT_PROTOTYPE (old_decl))
5945 warning ("function declaration isn%'t a prototype");
5946 /* Optionally warn of any global def with no previous prototype. */
5947 else if (warn_missing_prototypes
5948 && old_decl != error_mark_node
5949 && TREE_PUBLIC (decl1)
5950 && !MAIN_NAME_P (DECL_NAME (decl1))
5951 && C_DECL_ISNT_PROTOTYPE (old_decl))
5952 warning ("%Jno previous prototype for %qD", decl1, decl1);
5953 /* Optionally warn of any def with no previous prototype
5954 if the function has already been used. */
5955 else if (warn_missing_prototypes
5956 && old_decl != 0
5957 && old_decl != error_mark_node
5958 && TREE_USED (old_decl)
5959 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
5960 warning ("%J%qD was used with no prototype before its definition",
5961 decl1, decl1);
5962 /* Optionally warn of any global def with no previous declaration. */
5963 else if (warn_missing_declarations
5964 && TREE_PUBLIC (decl1)
5965 && old_decl == 0
5966 && !MAIN_NAME_P (DECL_NAME (decl1)))
5967 warning ("%Jno previous declaration for %qD", decl1, decl1);
5968 /* Optionally warn of any def with no previous declaration
5969 if the function has already been used. */
5970 else if (warn_missing_declarations
5971 && old_decl != 0
5972 && old_decl != error_mark_node
5973 && TREE_USED (old_decl)
5974 && C_DECL_IMPLICIT (old_decl))
5975 warning ("%J%qD was used with no declaration before its definition",
5976 decl1, decl1);
5978 /* This is a definition, not a reference.
5979 So normally clear DECL_EXTERNAL.
5980 However, `extern inline' acts like a declaration
5981 except for defining how to inline. So set DECL_EXTERNAL in that case. */
5982 DECL_EXTERNAL (decl1) = current_extern_inline;
5984 /* This function exists in static storage.
5985 (This does not mean `static' in the C sense!) */
5986 TREE_STATIC (decl1) = 1;
5988 /* A nested function is not global. */
5989 if (current_function_decl != 0)
5990 TREE_PUBLIC (decl1) = 0;
5992 /* This is the earliest point at which we might know the assembler
5993 name of the function. Thus, if it's set before this, die horribly. */
5994 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
5996 /* If #pragma weak was used, mark the decl weak now. */
5997 if (current_scope == file_scope)
5998 maybe_apply_pragma_weak (decl1);
6000 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6001 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6003 tree args;
6004 int argct = 0;
6006 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6007 != integer_type_node)
6008 pedwarn ("%Jreturn type of %qD is not %<int%>", decl1, decl1);
6010 for (args = TYPE_ARG_TYPES (TREE_TYPE (decl1)); args;
6011 args = TREE_CHAIN (args))
6013 tree type = args ? TREE_VALUE (args) : 0;
6015 if (type == void_type_node)
6016 break;
6018 ++argct;
6019 switch (argct)
6021 case 1:
6022 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
6023 pedwarn ("%Jfirst argument of %qD should be %<int%>",
6024 decl1, decl1);
6025 break;
6027 case 2:
6028 if (TREE_CODE (type) != POINTER_TYPE
6029 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6030 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6031 != char_type_node))
6032 pedwarn ("%Jsecond argument of %qD should be %<char **%>",
6033 decl1, decl1);
6034 break;
6036 case 3:
6037 if (TREE_CODE (type) != POINTER_TYPE
6038 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
6039 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
6040 != char_type_node))
6041 pedwarn ("%Jthird argument of %qD should probably be "
6042 "%<char **%>", decl1, decl1);
6043 break;
6047 /* It is intentional that this message does not mention the third
6048 argument because it's only mentioned in an appendix of the
6049 standard. */
6050 if (argct > 0 && (argct < 2 || argct > 3))
6051 pedwarn ("%J%qD takes only zero or two arguments", decl1, decl1);
6053 if (!TREE_PUBLIC (decl1))
6054 pedwarn ("%J%qD is normally a non-static function", decl1, decl1);
6057 /* Record the decl so that the function name is defined.
6058 If we already have a decl for this name, and it is a FUNCTION_DECL,
6059 use the old decl. */
6061 current_function_decl = pushdecl (decl1);
6063 push_scope ();
6064 declare_parm_level ();
6066 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6067 /* Promote the value to int before returning it. */
6068 if (c_promoting_integer_type_p (restype))
6070 /* It retains unsignedness if not really getting wider. */
6071 if (TYPE_UNSIGNED (restype)
6072 && (TYPE_PRECISION (restype)
6073 == TYPE_PRECISION (integer_type_node)))
6074 restype = unsigned_type_node;
6075 else
6076 restype = integer_type_node;
6079 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6080 DECL_ARTIFICIAL (resdecl) = 1;
6081 DECL_IGNORED_P (resdecl) = 1;
6082 DECL_RESULT (current_function_decl) = resdecl;
6084 start_fname_decls ();
6086 return 1;
6089 /* Subroutine of store_parm_decls which handles new-style function
6090 definitions (prototype format). The parms already have decls, so we
6091 need only record them as in effect and complain if any redundant
6092 old-style parm decls were written. */
6093 static void
6094 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6096 tree decl;
6098 if (current_scope->bindings)
6100 error ("%Jold-style parameter declarations in prototyped "
6101 "function definition", fndecl);
6103 /* Get rid of the old-style declarations. */
6104 pop_scope ();
6105 push_scope ();
6107 /* Don't issue this warning for nested functions, and don't issue this
6108 warning if we got here because ARG_INFO_TYPES was error_mark_node
6109 (this happens when a function definition has just an ellipsis in
6110 its parameter list). */
6111 else if (warn_traditional && !in_system_header && !current_function_scope
6112 && arg_info->types != error_mark_node)
6113 warning ("%Jtraditional C rejects ISO C style function definitions",
6114 fndecl);
6116 /* Now make all the parameter declarations visible in the function body.
6117 We can bypass most of the grunt work of pushdecl. */
6118 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6120 DECL_CONTEXT (decl) = current_function_decl;
6121 if (DECL_NAME (decl))
6122 bind (DECL_NAME (decl), decl, current_scope,
6123 /*invisible=*/false, /*nested=*/false);
6124 else
6125 error ("%Jparameter name omitted", decl);
6128 /* Record the parameter list in the function declaration. */
6129 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6131 /* Now make all the ancillary declarations visible, likewise. */
6132 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6134 DECL_CONTEXT (decl) = current_function_decl;
6135 if (DECL_NAME (decl))
6136 bind (DECL_NAME (decl), decl, current_scope,
6137 /*invisible=*/false, /*nested=*/false);
6140 /* And all the tag declarations. */
6141 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6142 if (TREE_PURPOSE (decl))
6143 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6144 /*invisible=*/false, /*nested=*/false);
6147 /* Subroutine of store_parm_decls which handles old-style function
6148 definitions (separate parameter list and declarations). */
6150 static void
6151 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6153 struct c_binding *b;
6154 tree parm, decl, last;
6155 tree parmids = arg_info->parms;
6157 /* We use DECL_WEAK as a flag to show which parameters have been
6158 seen already, since it is not used on PARM_DECL. */
6159 #ifdef ENABLE_CHECKING
6160 for (b = current_scope->bindings; b; b = b->prev)
6161 gcc_assert (TREE_CODE (b->decl) != PARM_DECL || !DECL_WEAK (b->decl));
6162 #endif
6164 if (warn_old_style_definition && !in_system_header)
6165 warning ("%Jold-style function definition", fndecl);
6167 /* Match each formal parameter name with its declaration. Save each
6168 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6169 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6171 if (TREE_VALUE (parm) == 0)
6173 error ("%Jparameter name missing from parameter list", fndecl);
6174 TREE_PURPOSE (parm) = 0;
6175 continue;
6178 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6179 if (b && B_IN_CURRENT_SCOPE (b))
6181 decl = b->decl;
6182 /* If we got something other than a PARM_DECL it is an error. */
6183 if (TREE_CODE (decl) != PARM_DECL)
6184 error ("%J%qD declared as a non-parameter", decl, decl);
6185 /* If the declaration is already marked, we have a duplicate
6186 name. Complain and ignore the duplicate. */
6187 else if (DECL_WEAK (decl))
6189 error ("%Jmultiple parameters named %qD", decl, decl);
6190 TREE_PURPOSE (parm) = 0;
6191 continue;
6193 /* If the declaration says "void", complain and turn it into
6194 an int. */
6195 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6197 error ("%Jparameter %qD declared with void type", decl, decl);
6198 TREE_TYPE (decl) = integer_type_node;
6199 DECL_ARG_TYPE (decl) = integer_type_node;
6200 layout_decl (decl, 0);
6203 /* If no declaration found, default to int. */
6204 else
6206 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6207 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6208 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6209 pushdecl (decl);
6211 if (flag_isoc99)
6212 pedwarn ("%Jtype of %qD defaults to %<int%>", decl, decl);
6213 else if (extra_warnings)
6214 warning ("%Jtype of %qD defaults to %<int%>", decl, decl);
6217 TREE_PURPOSE (parm) = decl;
6218 DECL_WEAK (decl) = 1;
6221 /* Now examine the parms chain for incomplete declarations
6222 and declarations with no corresponding names. */
6224 for (b = current_scope->bindings; b; b = b->prev)
6226 parm = b->decl;
6227 if (TREE_CODE (parm) != PARM_DECL)
6228 continue;
6230 if (TREE_TYPE (parm) != error_mark_node
6231 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6233 error ("%Jparameter %qD has incomplete type", parm, parm);
6234 TREE_TYPE (parm) = error_mark_node;
6237 if (!DECL_WEAK (parm))
6239 error ("%Jdeclaration for parameter %qD but no such parameter",
6240 parm, parm);
6242 /* Pretend the parameter was not missing.
6243 This gets us to a standard state and minimizes
6244 further error messages. */
6245 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6249 /* Chain the declarations together in the order of the list of
6250 names. Store that chain in the function decl, replacing the
6251 list of names. Update the current scope to match. */
6252 DECL_ARGUMENTS (fndecl) = 0;
6254 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6255 if (TREE_PURPOSE (parm))
6256 break;
6257 if (parm && TREE_PURPOSE (parm))
6259 last = TREE_PURPOSE (parm);
6260 DECL_ARGUMENTS (fndecl) = last;
6261 DECL_WEAK (last) = 0;
6263 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6264 if (TREE_PURPOSE (parm))
6266 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6267 last = TREE_PURPOSE (parm);
6268 DECL_WEAK (last) = 0;
6270 TREE_CHAIN (last) = 0;
6273 /* If there was a previous prototype,
6274 set the DECL_ARG_TYPE of each argument according to
6275 the type previously specified, and report any mismatches. */
6277 if (current_function_prototype_arg_types)
6279 tree type;
6280 for (parm = DECL_ARGUMENTS (fndecl),
6281 type = current_function_prototype_arg_types;
6282 parm || (type && (TYPE_MAIN_VARIANT (TREE_VALUE (type))
6283 != void_type_node));
6284 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6286 if (parm == 0 || type == 0
6287 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6289 if (current_function_prototype_built_in)
6290 warning ("number of arguments doesn%'t match "
6291 "built-in prototype");
6292 else
6294 error ("number of arguments doesn%'t match prototype");
6295 error ("%Hprototype declaration",
6296 &current_function_prototype_locus);
6298 break;
6300 /* Type for passing arg must be consistent with that
6301 declared for the arg. ISO C says we take the unqualified
6302 type for parameters declared with qualified type. */
6303 if (!comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6304 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6306 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6307 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6309 /* Adjust argument to match prototype. E.g. a previous
6310 `int foo(float);' prototype causes
6311 `int foo(x) float x; {...}' to be treated like
6312 `int foo(float x) {...}'. This is particularly
6313 useful for argument types like uid_t. */
6314 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6316 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6317 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6318 && TYPE_PRECISION (TREE_TYPE (parm))
6319 < TYPE_PRECISION (integer_type_node))
6320 DECL_ARG_TYPE (parm) = integer_type_node;
6322 if (pedantic)
6324 /* ??? Is it possible to get here with a
6325 built-in prototype or will it always have
6326 been diagnosed as conflicting with an
6327 old-style definition and discarded? */
6328 if (current_function_prototype_built_in)
6329 warning ("promoted argument %qD "
6330 "doesn%'t match built-in prototype", parm);
6331 else
6333 pedwarn ("promoted argument %qD "
6334 "doesn%'t match prototype", parm);
6335 pedwarn ("%Hprototype declaration",
6336 &current_function_prototype_locus);
6340 else
6342 if (current_function_prototype_built_in)
6343 warning ("argument %qD doesn%'t match "
6344 "built-in prototype", parm);
6345 else
6347 error ("argument %qD doesn%'t match prototype", parm);
6348 error ("%Hprototype declaration",
6349 &current_function_prototype_locus);
6354 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6357 /* Otherwise, create a prototype that would match. */
6359 else
6361 tree actual = 0, last = 0, type;
6363 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6365 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6366 if (last)
6367 TREE_CHAIN (last) = type;
6368 else
6369 actual = type;
6370 last = type;
6372 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6373 if (last)
6374 TREE_CHAIN (last) = type;
6375 else
6376 actual = type;
6378 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6379 of the type of this function, but we need to avoid having this
6380 affect the types of other similarly-typed functions, so we must
6381 first force the generation of an identical (but separate) type
6382 node for the relevant function type. The new node we create
6383 will be a variant of the main variant of the original function
6384 type. */
6386 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6388 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6392 /* Store parameter declarations passed in ARG_INFO into the current
6393 function declaration. */
6395 void
6396 store_parm_decls_from (struct c_arg_info *arg_info)
6398 current_function_arg_info = arg_info;
6399 store_parm_decls ();
6402 /* Store the parameter declarations into the current function declaration.
6403 This is called after parsing the parameter declarations, before
6404 digesting the body of the function.
6406 For an old-style definition, construct a prototype out of the old-style
6407 parameter declarations and inject it into the function's type. */
6409 void
6410 store_parm_decls (void)
6412 tree fndecl = current_function_decl;
6413 bool proto;
6415 /* The argument information block for FNDECL. */
6416 struct c_arg_info *arg_info = current_function_arg_info;
6417 current_function_arg_info = 0;
6419 /* True if this definition is written with a prototype. Note:
6420 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6421 list in a function definition as equivalent to (void) -- an
6422 empty argument list specifies the function has no parameters,
6423 but only (void) sets up a prototype for future calls. */
6424 proto = arg_info->types != 0;
6426 if (proto)
6427 store_parm_decls_newstyle (fndecl, arg_info);
6428 else
6429 store_parm_decls_oldstyle (fndecl, arg_info);
6431 /* The next call to push_scope will be a function body. */
6433 next_is_function_body = true;
6435 /* Write a record describing this function definition to the prototypes
6436 file (if requested). */
6438 gen_aux_info_record (fndecl, 1, 0, proto);
6440 /* Initialize the RTL code for the function. */
6441 allocate_struct_function (fndecl);
6443 /* Begin the statement tree for this function. */
6444 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6446 /* ??? Insert the contents of the pending sizes list into the function
6447 to be evaluated. The only reason left to have this is
6448 void foo(int n, int array[n++])
6449 because we throw away the array type in favor of a pointer type, and
6450 thus won't naturally see the SAVE_EXPR containing the increment. All
6451 other pending sizes would be handled by gimplify_parameters. */
6453 tree t;
6454 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6455 add_stmt (TREE_VALUE (t));
6458 /* Even though we're inside a function body, we still don't want to
6459 call expand_expr to calculate the size of a variable-sized array.
6460 We haven't necessarily assigned RTL to all variables yet, so it's
6461 not safe to try to expand expressions involving them. */
6462 cfun->x_dont_save_pending_sizes_p = 1;
6465 /* Handle attribute((warn_unused_result)) on FNDECL and all its nested
6466 functions. */
6468 static void
6469 c_warn_unused_result_recursively (tree fndecl)
6471 struct cgraph_node *cgn;
6473 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6474 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6476 /* Finalize all nested functions now. */
6477 cgn = cgraph_node (fndecl);
6478 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6479 c_warn_unused_result_recursively (cgn->decl);
6482 /* Finish up a function declaration and compile that function
6483 all the way to assembler language output. The free the storage
6484 for the function definition.
6486 This is called after parsing the body of the function definition. */
6488 void
6489 finish_function (void)
6491 tree fndecl = current_function_decl;
6493 label_context_stack_se = label_context_stack_se->next;
6494 label_context_stack_vm = label_context_stack_vm->next;
6496 if (TREE_CODE (fndecl) == FUNCTION_DECL
6497 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6499 tree args = DECL_ARGUMENTS (fndecl);
6500 for (; args; args = TREE_CHAIN (args))
6502 tree type = TREE_TYPE (args);
6503 if (INTEGRAL_TYPE_P (type)
6504 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6505 DECL_ARG_TYPE (args) = integer_type_node;
6509 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6510 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6512 /* Must mark the RESULT_DECL as being in this function. */
6514 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6515 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6517 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6519 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6520 != integer_type_node)
6522 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6523 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6524 if (!warn_main)
6525 pedwarn ("%Jreturn type of %qD is not %<int%>", fndecl, fndecl);
6527 else
6529 if (flag_isoc99)
6531 tree stmt = c_finish_return (integer_zero_node);
6532 /* Hack. We don't want the middle-end to warn that this
6533 return is unreachable, so put the statement on the
6534 special line 0. */
6535 annotate_with_file_line (stmt, input_filename, 0);
6540 /* Tie off the statement tree for this function. */
6541 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6543 finish_fname_decls ();
6545 /* Complain if there's just no return statement. */
6546 if (warn_return_type
6547 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6548 && !current_function_returns_value && !current_function_returns_null
6549 /* Don't complain if we abort. */
6550 && !current_function_returns_abnormally
6551 /* Don't warn for main(). */
6552 && !MAIN_NAME_P (DECL_NAME (fndecl))
6553 /* Or if they didn't actually specify a return type. */
6554 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6555 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6556 inline function, as we might never be compiled separately. */
6557 && DECL_INLINE (fndecl))
6558 warning ("no return statement in function returning non-void");
6560 /* With just -Wextra, complain only if function returns both with
6561 and without a value. */
6562 if (extra_warnings
6563 && current_function_returns_value
6564 && current_function_returns_null)
6565 warning ("this function may return with or without a value");
6567 /* Store the end of the function, so that we get good line number
6568 info for the epilogue. */
6569 cfun->function_end_locus = input_location;
6571 /* If we don't have ctors/dtors sections, and this is a static
6572 constructor or destructor, it must be recorded now. */
6573 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6574 && !targetm.have_ctors_dtors)
6575 static_ctors = tree_cons (NULL_TREE, fndecl, static_ctors);
6576 if (DECL_STATIC_DESTRUCTOR (fndecl)
6577 && !targetm.have_ctors_dtors)
6578 static_dtors = tree_cons (NULL_TREE, fndecl, static_dtors);
6580 /* Finalize the ELF visibility for the function. */
6581 c_determine_visibility (fndecl);
6583 /* Genericize before inlining. Delay genericizing nested functions
6584 until their parent function is genericized. Since finalizing
6585 requires GENERIC, delay that as well. */
6587 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6588 && !undef_nested_function)
6590 if (!decl_function_context (fndecl))
6592 c_genericize (fndecl);
6593 c_warn_unused_result_recursively (fndecl);
6595 /* ??? Objc emits functions after finalizing the compilation unit.
6596 This should be cleaned up later and this conditional removed. */
6597 if (cgraph_global_info_ready)
6599 c_expand_body (fndecl);
6600 return;
6603 cgraph_finalize_function (fndecl, false);
6605 else
6607 /* Register this function with cgraph just far enough to get it
6608 added to our parent's nested function list. Handy, since the
6609 C front end doesn't have such a list. */
6610 (void) cgraph_node (fndecl);
6614 if (!decl_function_context (fndecl))
6615 undef_nested_function = false;
6617 /* We're leaving the context of this function, so zap cfun.
6618 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6619 tree_rest_of_compilation. */
6620 cfun = NULL;
6621 current_function_decl = NULL;
6624 /* Generate the RTL for the body of FNDECL. */
6626 void
6627 c_expand_body (tree fndecl)
6630 if (!DECL_INITIAL (fndecl)
6631 || DECL_INITIAL (fndecl) == error_mark_node)
6632 return;
6634 tree_rest_of_compilation (fndecl);
6636 if (DECL_STATIC_CONSTRUCTOR (fndecl)
6637 && targetm.have_ctors_dtors)
6638 targetm.asm_out.constructor (XEXP (DECL_RTL (fndecl), 0),
6639 DEFAULT_INIT_PRIORITY);
6640 if (DECL_STATIC_DESTRUCTOR (fndecl)
6641 && targetm.have_ctors_dtors)
6642 targetm.asm_out.destructor (XEXP (DECL_RTL (fndecl), 0),
6643 DEFAULT_INIT_PRIORITY);
6646 /* Check the declarations given in a for-loop for satisfying the C99
6647 constraints. */
6648 void
6649 check_for_loop_decls (void)
6651 struct c_binding *b;
6653 if (!flag_isoc99)
6655 /* If we get here, declarations have been used in a for loop without
6656 the C99 for loop scope. This doesn't make much sense, so don't
6657 allow it. */
6658 error ("%<for%> loop initial declaration used outside C99 mode");
6659 return;
6661 /* C99 subclause 6.8.5 paragraph 3:
6663 [#3] The declaration part of a for statement shall only
6664 declare identifiers for objects having storage class auto or
6665 register.
6667 It isn't clear whether, in this sentence, "identifiers" binds to
6668 "shall only declare" or to "objects" - that is, whether all identifiers
6669 declared must be identifiers for objects, or whether the restriction
6670 only applies to those that are. (A question on this in comp.std.c
6671 in November 2000 received no answer.) We implement the strictest
6672 interpretation, to avoid creating an extension which later causes
6673 problems. */
6675 for (b = current_scope->bindings; b; b = b->prev)
6677 tree id = b->id;
6678 tree decl = b->decl;
6680 if (!id)
6681 continue;
6683 switch (TREE_CODE (decl))
6685 case VAR_DECL:
6686 if (TREE_STATIC (decl))
6687 error ("%Jdeclaration of static variable %qD in %<for%> loop "
6688 "initial declaration", decl, decl);
6689 else if (DECL_EXTERNAL (decl))
6690 error ("%Jdeclaration of %<extern%> variable %qD in %<for%> loop "
6691 "initial declaration", decl, decl);
6692 break;
6694 case RECORD_TYPE:
6695 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6696 id);
6697 break;
6698 case UNION_TYPE:
6699 error ("%<union %E%> declared in %<for%> loop initial declaration",
6700 id);
6701 break;
6702 case ENUMERAL_TYPE:
6703 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6704 id);
6705 break;
6706 default:
6707 error ("%Jdeclaration of non-variable %qD in %<for%> loop "
6708 "initial declaration", decl, decl);
6713 /* Save and reinitialize the variables
6714 used during compilation of a C function. */
6716 void
6717 c_push_function_context (struct function *f)
6719 struct language_function *p;
6720 p = GGC_NEW (struct language_function);
6721 f->language = p;
6723 p->base.x_stmt_tree = c_stmt_tree;
6724 p->x_break_label = c_break_label;
6725 p->x_cont_label = c_cont_label;
6726 p->x_switch_stack = c_switch_stack;
6727 p->arg_info = current_function_arg_info;
6728 p->returns_value = current_function_returns_value;
6729 p->returns_null = current_function_returns_null;
6730 p->returns_abnormally = current_function_returns_abnormally;
6731 p->warn_about_return_type = warn_about_return_type;
6732 p->extern_inline = current_extern_inline;
6735 /* Restore the variables used during compilation of a C function. */
6737 void
6738 c_pop_function_context (struct function *f)
6740 struct language_function *p = f->language;
6742 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6743 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6745 /* Stop pointing to the local nodes about to be freed. */
6746 /* But DECL_INITIAL must remain nonzero so we know this
6747 was an actual function definition. */
6748 DECL_INITIAL (current_function_decl) = error_mark_node;
6749 DECL_ARGUMENTS (current_function_decl) = 0;
6752 c_stmt_tree = p->base.x_stmt_tree;
6753 c_break_label = p->x_break_label;
6754 c_cont_label = p->x_cont_label;
6755 c_switch_stack = p->x_switch_stack;
6756 current_function_arg_info = p->arg_info;
6757 current_function_returns_value = p->returns_value;
6758 current_function_returns_null = p->returns_null;
6759 current_function_returns_abnormally = p->returns_abnormally;
6760 warn_about_return_type = p->warn_about_return_type;
6761 current_extern_inline = p->extern_inline;
6763 f->language = NULL;
6766 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6768 void
6769 c_dup_lang_specific_decl (tree decl)
6771 struct lang_decl *ld;
6773 if (!DECL_LANG_SPECIFIC (decl))
6774 return;
6776 ld = GGC_NEW (struct lang_decl);
6777 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6778 DECL_LANG_SPECIFIC (decl) = ld;
6781 /* The functions below are required for functionality of doing
6782 function at once processing in the C front end. Currently these
6783 functions are not called from anywhere in the C front end, but as
6784 these changes continue, that will change. */
6786 /* Returns nonzero if the current statement is a full expression,
6787 i.e. temporaries created during that statement should be destroyed
6788 at the end of the statement. */
6791 stmts_are_full_exprs_p (void)
6793 return 0;
6796 /* Returns the stmt_tree (if any) to which statements are currently
6797 being added. If there is no active statement-tree, NULL is
6798 returned. */
6800 stmt_tree
6801 current_stmt_tree (void)
6803 return &c_stmt_tree;
6806 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6807 C. */
6810 anon_aggr_type_p (tree ARG_UNUSED (node))
6812 return 0;
6815 /* Return the global value of T as a symbol. */
6817 tree
6818 identifier_global_value (tree t)
6820 struct c_binding *b;
6822 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6823 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6824 return b->decl;
6826 return 0;
6829 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6830 otherwise the name is found in ridpointers from RID_INDEX. */
6832 void
6833 record_builtin_type (enum rid rid_index, const char *name, tree type)
6835 tree id, decl;
6836 if (name == 0)
6837 id = ridpointers[(int) rid_index];
6838 else
6839 id = get_identifier (name);
6840 decl = build_decl (TYPE_DECL, id, type);
6841 pushdecl (decl);
6842 if (debug_hooks->type_decl)
6843 debug_hooks->type_decl (decl, false);
6846 /* Build the void_list_node (void_type_node having been created). */
6847 tree
6848 build_void_list_node (void)
6850 tree t = build_tree_list (NULL_TREE, void_type_node);
6851 return t;
6854 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
6856 struct c_parm *
6857 build_c_parm (struct c_declspecs *specs, tree attrs,
6858 struct c_declarator *declarator)
6860 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
6861 ret->specs = specs;
6862 ret->attrs = attrs;
6863 ret->declarator = declarator;
6864 return ret;
6867 /* Return a declarator with nested attributes. TARGET is the inner
6868 declarator to which these attributes apply. ATTRS are the
6869 attributes. */
6871 struct c_declarator *
6872 build_attrs_declarator (tree attrs, struct c_declarator *target)
6874 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6875 ret->kind = cdk_attrs;
6876 ret->declarator = target;
6877 ret->u.attrs = attrs;
6878 return ret;
6881 /* Return a declarator for a function with arguments specified by ARGS
6882 and return type specified by TARGET. */
6884 struct c_declarator *
6885 build_function_declarator (struct c_arg_info *args,
6886 struct c_declarator *target)
6888 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6889 ret->kind = cdk_function;
6890 ret->declarator = target;
6891 ret->u.arg_info = args;
6892 return ret;
6895 /* Return a declarator for the identifier IDENT (which may be
6896 NULL_TREE for an abstract declarator). */
6898 struct c_declarator *
6899 build_id_declarator (tree ident)
6901 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6902 ret->kind = cdk_id;
6903 ret->declarator = 0;
6904 ret->u.id = ident;
6905 return ret;
6908 /* Return something to represent absolute declarators containing a *.
6909 TARGET is the absolute declarator that the * contains.
6910 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
6911 to apply to the pointer type. */
6913 struct c_declarator *
6914 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
6915 struct c_declarator *target)
6917 tree attrs;
6918 int quals = 0;
6919 struct c_declarator *itarget = target;
6920 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
6921 if (type_quals_attrs)
6923 attrs = type_quals_attrs->attrs;
6924 quals = quals_from_declspecs (type_quals_attrs);
6925 if (attrs != NULL_TREE)
6926 itarget = build_attrs_declarator (attrs, target);
6928 ret->kind = cdk_pointer;
6929 ret->declarator = itarget;
6930 ret->u.pointer_quals = quals;
6931 return ret;
6934 /* Return a pointer to a structure for an empty list of declaration
6935 specifiers. */
6937 struct c_declspecs *
6938 build_null_declspecs (void)
6940 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
6941 ret->type = 0;
6942 ret->decl_attr = 0;
6943 ret->attrs = 0;
6944 ret->typespec_word = cts_none;
6945 ret->storage_class = csc_none;
6946 ret->non_sc_seen_p = false;
6947 ret->typedef_p = false;
6948 ret->tag_defined_p = false;
6949 ret->explicit_signed_p = false;
6950 ret->deprecated_p = false;
6951 ret->default_int_p = false;
6952 ret->long_p = false;
6953 ret->long_long_p = false;
6954 ret->short_p = false;
6955 ret->signed_p = false;
6956 ret->unsigned_p = false;
6957 ret->complex_p = false;
6958 ret->inline_p = false;
6959 ret->thread_p = false;
6960 ret->const_p = false;
6961 ret->volatile_p = false;
6962 ret->restrict_p = false;
6963 return ret;
6966 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
6967 returning SPECS. */
6969 struct c_declspecs *
6970 declspecs_add_qual (struct c_declspecs *specs, tree qual)
6972 enum rid i;
6973 bool dupe = false;
6974 specs->non_sc_seen_p = true;
6975 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
6976 && C_IS_RESERVED_WORD (qual));
6977 i = C_RID_CODE (qual);
6978 switch (i)
6980 case RID_CONST:
6981 dupe = specs->const_p;
6982 specs->const_p = true;
6983 break;
6984 case RID_VOLATILE:
6985 dupe = specs->volatile_p;
6986 specs->volatile_p = true;
6987 break;
6988 case RID_RESTRICT:
6989 dupe = specs->restrict_p;
6990 specs->restrict_p = true;
6991 break;
6992 default:
6993 gcc_unreachable ();
6995 if (dupe && pedantic && !flag_isoc99)
6996 pedwarn ("duplicate %qs", IDENTIFIER_POINTER (qual));
6997 return specs;
7000 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7001 returning SPECS. */
7003 struct c_declspecs *
7004 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7006 tree type = spec.spec;
7007 specs->non_sc_seen_p = true;
7008 if (TREE_DEPRECATED (type))
7009 specs->deprecated_p = true;
7011 /* Handle type specifier keywords. */
7012 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7014 enum rid i = C_RID_CODE (type);
7015 if (specs->type)
7017 error ("two or more data types in declaration specifiers");
7018 return specs;
7020 if ((int) i <= (int) RID_LAST_MODIFIER)
7022 /* "long", "short", "signed", "unsigned" or "_Complex". */
7023 bool dupe = false;
7024 switch (i)
7026 case RID_LONG:
7027 if (specs->long_long_p)
7029 error ("%<long long long%> is too long for GCC");
7030 break;
7032 if (specs->long_p)
7034 if (specs->typespec_word == cts_double)
7036 error ("both %<long long%> and %<double%> in "
7037 "declaration specifiers");
7038 break;
7040 if (pedantic && !flag_isoc99 && !in_system_header
7041 && warn_long_long)
7042 pedwarn ("ISO C90 does not support %<long long%>");
7043 specs->long_long_p = 1;
7044 break;
7046 if (specs->short_p)
7047 error ("both %<long%> and %<short%> in "
7048 "declaration specifiers");
7049 else if (specs->typespec_word == cts_void)
7050 error ("both %<long%> and %<void%> in "
7051 "declaration specifiers");
7052 else if (specs->typespec_word == cts_bool)
7053 error ("both %<long%> and %<_Bool%> in "
7054 "declaration specifiers");
7055 else if (specs->typespec_word == cts_char)
7056 error ("both %<long%> and %<char%> in "
7057 "declaration specifiers");
7058 else if (specs->typespec_word == cts_float)
7059 error ("both %<long%> and %<float%> in "
7060 "declaration specifiers");
7061 else
7062 specs->long_p = true;
7063 break;
7064 case RID_SHORT:
7065 dupe = specs->short_p;
7066 if (specs->long_p)
7067 error ("both %<long%> and %<short%> in "
7068 "declaration specifiers");
7069 else if (specs->typespec_word == cts_void)
7070 error ("both %<short%> and %<void%> in "
7071 "declaration specifiers");
7072 else if (specs->typespec_word == cts_bool)
7073 error ("both %<short%> and %<_Bool%> in "
7074 "declaration specifiers");
7075 else if (specs->typespec_word == cts_char)
7076 error ("both %<short%> and %<char%> in "
7077 "declaration specifiers");
7078 else if (specs->typespec_word == cts_float)
7079 error ("both %<short%> and %<float%> in "
7080 "declaration specifiers");
7081 else if (specs->typespec_word == cts_double)
7082 error ("both %<short%> and %<double%> in "
7083 "declaration specifiers");
7084 else
7085 specs->short_p = true;
7086 break;
7087 case RID_SIGNED:
7088 dupe = specs->signed_p;
7089 if (specs->unsigned_p)
7090 error ("both %<signed%> and %<unsigned%> in "
7091 "declaration specifiers");
7092 else if (specs->typespec_word == cts_void)
7093 error ("both %<signed%> and %<void%> in "
7094 "declaration specifiers");
7095 else if (specs->typespec_word == cts_bool)
7096 error ("both %<signed%> and %<_Bool%> in "
7097 "declaration specifiers");
7098 else if (specs->typespec_word == cts_float)
7099 error ("both %<signed%> and %<float%> in "
7100 "declaration specifiers");
7101 else if (specs->typespec_word == cts_double)
7102 error ("both %<signed%> and %<double%> in "
7103 "declaration specifiers");
7104 else
7105 specs->signed_p = true;
7106 break;
7107 case RID_UNSIGNED:
7108 dupe = specs->unsigned_p;
7109 if (specs->signed_p)
7110 error ("both %<signed%> and %<unsigned%> in "
7111 "declaration specifiers");
7112 else if (specs->typespec_word == cts_void)
7113 error ("both %<unsigned%> and %<void%> in "
7114 "declaration specifiers");
7115 else if (specs->typespec_word == cts_bool)
7116 error ("both %<unsigned%> and %<_Bool%> in "
7117 "declaration specifiers");
7118 else if (specs->typespec_word == cts_float)
7119 error ("both %<unsigned%> and %<float%> in "
7120 "declaration specifiers");
7121 else if (specs->typespec_word == cts_double)
7122 error ("both %<unsigned%> and %<double%> in "
7123 "declaration specifiers");
7124 else
7125 specs->unsigned_p = true;
7126 break;
7127 case RID_COMPLEX:
7128 dupe = specs->complex_p;
7129 if (pedantic && !flag_isoc99 && !in_system_header)
7130 pedwarn ("ISO C90 does not support complex types");
7131 if (specs->typespec_word == cts_void)
7132 error ("both %<complex%> and %<void%> in "
7133 "declaration specifiers");
7134 else if (specs->typespec_word == cts_bool)
7135 error ("both %<complex%> and %<_Bool%> in "
7136 "declaration specifiers");
7137 else
7138 specs->complex_p = true;
7139 break;
7140 default:
7141 gcc_unreachable ();
7144 if (dupe)
7145 error ("duplicate %qs", IDENTIFIER_POINTER (type));
7147 return specs;
7149 else
7151 /* "void", "_Bool", "char", "int", "float" or "double". */
7152 if (specs->typespec_word != cts_none)
7154 error ("two or more data types in declaration specifiers");
7155 return specs;
7157 switch (i)
7159 case RID_VOID:
7160 if (specs->long_p)
7161 error ("both %<long%> and %<void%> in "
7162 "declaration specifiers");
7163 else if (specs->short_p)
7164 error ("both %<short%> and %<void%> in "
7165 "declaration specifiers");
7166 else if (specs->signed_p)
7167 error ("both %<signed%> and %<void%> in "
7168 "declaration specifiers");
7169 else if (specs->unsigned_p)
7170 error ("both %<unsigned%> and %<void%> in "
7171 "declaration specifiers");
7172 else if (specs->complex_p)
7173 error ("both %<complex%> and %<void%> in "
7174 "declaration specifiers");
7175 else
7176 specs->typespec_word = cts_void;
7177 return specs;
7178 case RID_BOOL:
7179 if (specs->long_p)
7180 error ("both %<long%> and %<_Bool%> in "
7181 "declaration specifiers");
7182 else if (specs->short_p)
7183 error ("both %<short%> and %<_Bool%> in "
7184 "declaration specifiers");
7185 else if (specs->signed_p)
7186 error ("both %<signed%> and %<_Bool%> in "
7187 "declaration specifiers");
7188 else if (specs->unsigned_p)
7189 error ("both %<unsigned%> and %<_Bool%> in "
7190 "declaration specifiers");
7191 else if (specs->complex_p)
7192 error ("both %<complex%> and %<_Bool%> in "
7193 "declaration specifiers");
7194 else
7195 specs->typespec_word = cts_bool;
7196 return specs;
7197 case RID_CHAR:
7198 if (specs->long_p)
7199 error ("both %<long%> and %<char%> in "
7200 "declaration specifiers");
7201 else if (specs->short_p)
7202 error ("both %<short%> and %<char%> in "
7203 "declaration specifiers");
7204 else
7205 specs->typespec_word = cts_char;
7206 return specs;
7207 case RID_INT:
7208 specs->typespec_word = cts_int;
7209 return specs;
7210 case RID_FLOAT:
7211 if (specs->long_p)
7212 error ("both %<long%> and %<float%> in "
7213 "declaration specifiers");
7214 else if (specs->short_p)
7215 error ("both %<short%> and %<float%> in "
7216 "declaration specifiers");
7217 else if (specs->signed_p)
7218 error ("both %<signed%> and %<float%> in "
7219 "declaration specifiers");
7220 else if (specs->unsigned_p)
7221 error ("both %<unsigned%> and %<float%> in "
7222 "declaration specifiers");
7223 else
7224 specs->typespec_word = cts_float;
7225 return specs;
7226 case RID_DOUBLE:
7227 if (specs->long_long_p)
7228 error ("both %<long long%> and %<double%> in "
7229 "declaration specifiers");
7230 else if (specs->short_p)
7231 error ("both %<short%> and %<double%> in "
7232 "declaration specifiers");
7233 else if (specs->signed_p)
7234 error ("both %<signed%> and %<double%> in "
7235 "declaration specifiers");
7236 else if (specs->unsigned_p)
7237 error ("both %<unsigned%> and %<double%> in "
7238 "declaration specifiers");
7239 else
7240 specs->typespec_word = cts_double;
7241 return specs;
7242 default:
7243 /* ObjC reserved word "id", handled below. */
7244 break;
7249 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7250 form of ObjC type, cases such as "int" and "long" being handled
7251 above), a TYPE (struct, union, enum and typeof specifiers) or an
7252 ERROR_MARK. In none of these cases may there have previously
7253 been any type specifiers. */
7254 if (specs->type || specs->typespec_word != cts_none
7255 || specs->long_p || specs->short_p || specs->signed_p
7256 || specs->unsigned_p || specs->complex_p)
7257 error ("two or more data types in declaration specifiers");
7258 else if (TREE_CODE (type) == TYPE_DECL)
7260 if (TREE_TYPE (type) == error_mark_node)
7261 ; /* Allow the type to default to int to avoid cascading errors. */
7262 else
7264 specs->type = TREE_TYPE (type);
7265 specs->decl_attr = DECL_ATTRIBUTES (type);
7266 specs->typedef_p = true;
7267 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7270 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7272 tree t = lookup_name (type);
7273 if (!t || TREE_CODE (t) != TYPE_DECL)
7274 error ("%qs fails to be a typedef or built in type",
7275 IDENTIFIER_POINTER (type));
7276 else if (TREE_TYPE (t) == error_mark_node)
7278 else
7279 specs->type = TREE_TYPE (t);
7281 else if (TREE_CODE (type) != ERROR_MARK)
7283 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7284 specs->tag_defined_p = true;
7285 if (spec.kind == ctsk_typeof)
7286 specs->typedef_p = true;
7287 specs->type = type;
7290 return specs;
7293 /* Add the storage class specifier or function specifier SCSPEC to the
7294 declaration specifiers SPECS, returning SPECS. */
7296 struct c_declspecs *
7297 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7299 enum rid i;
7300 enum c_storage_class n = csc_none;
7301 bool dupe = false;
7302 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7303 && C_IS_RESERVED_WORD (scspec));
7304 i = C_RID_CODE (scspec);
7305 if (extra_warnings && specs->non_sc_seen_p)
7306 warning ("%qs is not at beginning of declaration",
7307 IDENTIFIER_POINTER (scspec));
7308 switch (i)
7310 case RID_INLINE:
7311 /* C99 permits duplicate inline. Although of doubtful utility,
7312 it seems simplest to permit it in gnu89 mode as well, as
7313 there is also little utility in maintaining this as a
7314 difference between gnu89 and C99 inline. */
7315 dupe = false;
7316 specs->inline_p = true;
7317 break;
7318 case RID_THREAD:
7319 dupe = specs->thread_p;
7320 if (specs->storage_class == csc_auto)
7321 error ("%<__thread%> used with %<auto%>");
7322 else if (specs->storage_class == csc_register)
7323 error ("%<__thread%> used with %<register%>");
7324 else if (specs->storage_class == csc_typedef)
7325 error ("%<__thread%> used with %<typedef%>");
7326 else
7327 specs->thread_p = true;
7328 break;
7329 case RID_AUTO:
7330 n = csc_auto;
7331 break;
7332 case RID_EXTERN:
7333 n = csc_extern;
7334 /* Diagnose "__thread extern". */
7335 if (specs->thread_p)
7336 error ("%<__thread%> before %<extern%>");
7337 break;
7338 case RID_REGISTER:
7339 n = csc_register;
7340 break;
7341 case RID_STATIC:
7342 n = csc_static;
7343 /* Diagnose "__thread static". */
7344 if (specs->thread_p)
7345 error ("%<__thread%> before %<static%>");
7346 break;
7347 case RID_TYPEDEF:
7348 n = csc_typedef;
7349 break;
7350 default:
7351 gcc_unreachable ();
7353 if (n != csc_none && n == specs->storage_class)
7354 dupe = true;
7355 if (dupe)
7356 error ("duplicate %qs", IDENTIFIER_POINTER (scspec));
7357 if (n != csc_none)
7359 if (specs->storage_class != csc_none && n != specs->storage_class)
7361 error ("multiple storage classes in declaration specifiers");
7363 else
7365 specs->storage_class = n;
7366 if (n != csc_extern && n != csc_static && specs->thread_p)
7368 error ("%<__thread%> used with %qs",
7369 IDENTIFIER_POINTER (scspec));
7370 specs->thread_p = false;
7374 return specs;
7377 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7378 returning SPECS. */
7380 struct c_declspecs *
7381 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7383 specs->attrs = chainon (attrs, specs->attrs);
7384 return specs;
7387 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7388 specifiers with any other type specifier to determine the resulting
7389 type. This is where ISO C checks on complex types are made, since
7390 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7391 double". */
7393 struct c_declspecs *
7394 finish_declspecs (struct c_declspecs *specs)
7396 /* If a type was specified as a whole, we have no modifiers and are
7397 done. */
7398 if (specs->type != NULL_TREE)
7400 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7401 && !specs->signed_p && !specs->unsigned_p
7402 && !specs->complex_p);
7403 return specs;
7406 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7407 has been specified, treat it as "int" unless "_Complex" is
7408 present and there are no other specifiers. If we just have
7409 "_Complex", it is equivalent to "_Complex double", but e.g.
7410 "_Complex short" is equivalent to "_Complex short int". */
7411 if (specs->typespec_word == cts_none)
7413 if (specs->long_p || specs->short_p
7414 || specs->signed_p || specs->unsigned_p)
7416 specs->typespec_word = cts_int;
7418 else if (specs->complex_p)
7420 specs->typespec_word = cts_double;
7421 if (pedantic)
7422 pedwarn ("ISO C does not support plain %<complex%> meaning "
7423 "%<double complex%>");
7425 else
7427 specs->typespec_word = cts_int;
7428 specs->default_int_p = true;
7429 /* We don't diagnose this here because grokdeclarator will
7430 give more specific diagnostics according to whether it is
7431 a function definition. */
7435 /* If "signed" was specified, record this to distinguish "int" and
7436 "signed int" in the case of a bit-field with
7437 -funsigned-bitfields. */
7438 specs->explicit_signed_p = specs->signed_p;
7440 /* Now compute the actual type. */
7441 switch (specs->typespec_word)
7443 case cts_void:
7444 gcc_assert (!specs->long_p && !specs->short_p
7445 && !specs->signed_p && !specs->unsigned_p
7446 && !specs->complex_p);
7447 specs->type = void_type_node;
7448 break;
7449 case cts_bool:
7450 gcc_assert (!specs->long_p && !specs->short_p
7451 && !specs->signed_p && !specs->unsigned_p
7452 && !specs->complex_p);
7453 specs->type = boolean_type_node;
7454 break;
7455 case cts_char:
7456 gcc_assert (!specs->long_p && !specs->short_p);
7457 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7458 if (specs->signed_p)
7459 specs->type = signed_char_type_node;
7460 else if (specs->unsigned_p)
7461 specs->type = unsigned_char_type_node;
7462 else
7463 specs->type = char_type_node;
7464 if (specs->complex_p)
7466 if (pedantic)
7467 pedwarn ("ISO C does not support complex integer types");
7468 specs->type = build_complex_type (specs->type);
7470 break;
7471 case cts_int:
7472 gcc_assert (!(specs->long_p && specs->short_p));
7473 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7474 if (specs->long_long_p)
7475 specs->type = (specs->unsigned_p
7476 ? long_long_unsigned_type_node
7477 : long_long_integer_type_node);
7478 else if (specs->long_p)
7479 specs->type = (specs->unsigned_p
7480 ? long_unsigned_type_node
7481 : long_integer_type_node);
7482 else if (specs->short_p)
7483 specs->type = (specs->unsigned_p
7484 ? short_unsigned_type_node
7485 : short_integer_type_node);
7486 else
7487 specs->type = (specs->unsigned_p
7488 ? unsigned_type_node
7489 : integer_type_node);
7490 if (specs->complex_p)
7492 if (pedantic)
7493 pedwarn ("ISO C does not support complex integer types");
7494 specs->type = build_complex_type (specs->type);
7496 break;
7497 case cts_float:
7498 gcc_assert (!specs->long_p && !specs->short_p
7499 && !specs->signed_p && !specs->unsigned_p);
7500 specs->type = (specs->complex_p
7501 ? complex_float_type_node
7502 : float_type_node);
7503 break;
7504 case cts_double:
7505 gcc_assert (!specs->long_long_p && !specs->short_p
7506 && !specs->signed_p && !specs->unsigned_p);
7507 if (specs->long_p)
7509 specs->type = (specs->complex_p
7510 ? complex_long_double_type_node
7511 : long_double_type_node);
7513 else
7515 specs->type = (specs->complex_p
7516 ? complex_double_type_node
7517 : double_type_node);
7519 break;
7520 default:
7521 gcc_unreachable ();
7524 return specs;
7527 /* Synthesize a function which calls all the global ctors or global
7528 dtors in this file. This is only used for targets which do not
7529 support .ctors/.dtors sections. FIXME: Migrate into cgraph. */
7530 static void
7531 build_cdtor (int method_type, tree cdtors)
7533 tree body = 0;
7535 if (!cdtors)
7536 return;
7538 for (; cdtors; cdtors = TREE_CHAIN (cdtors))
7539 append_to_statement_list (build_function_call (TREE_VALUE (cdtors), 0),
7540 &body);
7542 cgraph_build_static_cdtor (method_type, body, DEFAULT_INIT_PRIORITY);
7545 /* A subroutine of c_write_global_declarations. Perform final processing
7546 on one file scope's declarations (or the external scope's declarations),
7547 GLOBALS. */
7549 static void
7550 c_write_global_declarations_1 (tree globals)
7552 tree decl;
7553 bool reconsider;
7555 /* Process the decls in the order they were written. */
7556 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7558 /* Check for used but undefined static functions using the C
7559 standard's definition of "used", and set TREE_NO_WARNING so
7560 that check_global_declarations doesn't repeat the check. */
7561 if (TREE_CODE (decl) == FUNCTION_DECL
7562 && DECL_INITIAL (decl) == 0
7563 && DECL_EXTERNAL (decl)
7564 && !TREE_PUBLIC (decl)
7565 && C_DECL_USED (decl))
7567 pedwarn ("%J%qF used but never defined", decl, decl);
7568 TREE_NO_WARNING (decl) = 1;
7571 wrapup_global_declaration_1 (decl);
7576 reconsider = false;
7577 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7578 reconsider |= wrapup_global_declaration_2 (decl);
7580 while (reconsider);
7582 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7583 check_global_declaration_1 (decl);
7586 /* A subroutine of c_write_global_declarations Emit debug information for each
7587 of the declarations in GLOBALS. */
7589 static void
7590 c_write_global_declarations_2 (tree globals)
7592 tree decl;
7594 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
7595 debug_hooks->global_decl (decl);
7598 /* Preserve the external declarations scope across a garbage collect. */
7599 static GTY(()) tree ext_block;
7601 void
7602 c_write_global_declarations (void)
7604 tree t;
7606 /* We don't want to do this if generating a PCH. */
7607 if (pch_file)
7608 return;
7610 /* Don't waste time on further processing if -fsyntax-only or we've
7611 encountered errors. */
7612 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
7613 return;
7615 /* Close the external scope. */
7616 ext_block = pop_scope ();
7617 external_scope = 0;
7618 gcc_assert (!current_scope);
7620 /* Process all file scopes in this compilation, and the external_scope,
7621 through wrapup_global_declarations and check_global_declarations. */
7622 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7623 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
7624 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
7626 /* Generate functions to call static constructors and destructors
7627 for targets that do not support .ctors/.dtors sections. These
7628 functions have magic names which are detected by collect2. */
7629 build_cdtor ('I', static_ctors); static_ctors = 0;
7630 build_cdtor ('D', static_dtors); static_dtors = 0;
7632 /* We're done parsing; proceed to optimize and emit assembly.
7633 FIXME: shouldn't be the front end's responsibility to call this. */
7634 cgraph_optimize ();
7636 /* After cgraph has had a chance to emit everything that's going to
7637 be emitted, output debug information for globals. */
7638 if (errorcount == 0 && sorrycount == 0)
7640 timevar_push (TV_SYMOUT);
7641 for (t = all_translation_units; t; t = TREE_CHAIN (t))
7642 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
7643 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
7644 timevar_pop (TV_SYMOUT);
7647 ext_block = NULL;
7650 #include "gt-c-decl.h"