Daily bump.
[official-gcc.git] / gcc / c-decl.c
blob22491aaec6aa1c5cba18061472a1a472f39f12b9
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, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
63 #include "pointer-set.h"
65 /* In grokdeclarator, distinguish syntactic contexts of declarators. */
66 enum decl_context
67 { NORMAL, /* Ordinary declaration */
68 FUNCDEF, /* Function definition */
69 PARM, /* Declaration of parm before function body */
70 FIELD, /* Declaration inside struct or union */
71 TYPENAME}; /* Typename (inside cast or sizeof) */
73 /* States indicating how grokdeclarator() should handle declspecs marked
74 with __attribute__((deprecated)). An object declared as
75 __attribute__((deprecated)) suppresses warnings of uses of other
76 deprecated items. */
78 enum deprecated_states {
79 DEPRECATED_NORMAL,
80 DEPRECATED_SUPPRESS
84 /* Nonzero if we have seen an invalid cross reference
85 to a struct, union, or enum, but not yet printed the message. */
86 tree pending_invalid_xref;
88 /* File and line to appear in the eventual error message. */
89 location_t pending_invalid_xref_location;
91 /* True means we've initialized exception handling. */
92 bool c_eh_initialized_p;
94 /* The file and line that the prototype came from if this is an
95 old-style definition; used for diagnostics in
96 store_parm_decls_oldstyle. */
98 static location_t current_function_prototype_locus;
100 /* Whether this prototype was built-in. */
102 static bool current_function_prototype_built_in;
104 /* The argument type information of this prototype. */
106 static tree current_function_prototype_arg_types;
108 /* The argument information structure for the function currently being
109 defined. */
111 static struct c_arg_info *current_function_arg_info;
113 /* The obstack on which parser and related data structures, which are
114 not live beyond their top-level declaration or definition, are
115 allocated. */
116 struct obstack parser_obstack;
118 /* The current statement tree. */
120 static GTY(()) struct stmt_tree_s c_stmt_tree;
122 /* State saving variables. */
123 tree c_break_label;
124 tree c_cont_label;
126 /* Linked list of TRANSLATION_UNIT_DECLS for the translation units
127 included in this invocation. Note that the current translation
128 unit is not included in this list. */
130 static GTY(()) tree all_translation_units;
132 /* A list of decls to be made automatically visible in each file scope. */
133 static GTY(()) tree visible_builtins;
135 /* Set to 0 at beginning of a function definition, set to 1 if
136 a return statement that specifies a return value is seen. */
138 int current_function_returns_value;
140 /* Set to 0 at beginning of a function definition, set to 1 if
141 a return statement with no argument is seen. */
143 int current_function_returns_null;
145 /* Set to 0 at beginning of a function definition, set to 1 if
146 a call to a noreturn function is seen. */
148 int current_function_returns_abnormally;
150 /* Set to nonzero by `grokdeclarator' for a function
151 whose return type is defaulted, if warnings for this are desired. */
153 static int warn_about_return_type;
155 /* Nonzero when the current toplevel function contains a declaration
156 of a nested function which is never defined. */
158 static bool undef_nested_function;
160 /* True means global_bindings_p should return false even if the scope stack
161 says we are in file scope. */
162 bool c_override_global_bindings_to_false;
165 /* Each c_binding structure describes one binding of an identifier to
166 a decl. All the decls in a scope - irrespective of namespace - are
167 chained together by the ->prev field, which (as the name implies)
168 runs in reverse order. All the decls in a given namespace bound to
169 a given identifier are chained by the ->shadowed field, which runs
170 from inner to outer scopes.
172 The ->decl field usually points to a DECL node, but there are two
173 exceptions. In the namespace of type tags, the bound entity is a
174 RECORD_TYPE, UNION_TYPE, or ENUMERAL_TYPE node. If an undeclared
175 identifier is encountered, it is bound to error_mark_node to
176 suppress further errors about that identifier in the current
177 function.
179 The ->type field stores the type of the declaration in this scope;
180 if NULL, the type is the type of the ->decl field. This is only of
181 relevance for objects with external or internal linkage which may
182 be redeclared in inner scopes, forming composite types that only
183 persist for the duration of those scopes. In the external scope,
184 this stores the composite of all the types declared for this
185 object, visible or not. The ->inner_comp field (used only at file
186 scope) stores whether an incomplete array type at file scope was
187 completed at an inner scope to an array size other than 1.
189 The depth field is copied from the scope structure that holds this
190 decl. It is used to preserve the proper ordering of the ->shadowed
191 field (see bind()) and also for a handful of special-case checks.
192 Finally, the invisible bit is true for a decl which should be
193 ignored for purposes of normal name lookup, and the nested bit is
194 true for a decl that's been bound a second time in an inner scope;
195 in all such cases, the binding in the outer scope will have its
196 invisible bit true. */
198 struct c_binding GTY((chain_next ("%h.prev")))
200 tree decl; /* the decl bound */
201 tree type; /* the type in this scope */
202 tree id; /* the identifier it's bound to */
203 struct c_binding *prev; /* the previous decl in this scope */
204 struct c_binding *shadowed; /* the innermost decl shadowed by this one */
205 unsigned int depth : 28; /* depth of this scope */
206 BOOL_BITFIELD invisible : 1; /* normal lookup should ignore this binding */
207 BOOL_BITFIELD nested : 1; /* do not set DECL_CONTEXT when popping */
208 BOOL_BITFIELD inner_comp : 1; /* incomplete array completed in inner scope */
209 /* one free bit */
211 #define B_IN_SCOPE(b1, b2) ((b1)->depth == (b2)->depth)
212 #define B_IN_CURRENT_SCOPE(b) ((b)->depth == current_scope->depth)
213 #define B_IN_FILE_SCOPE(b) ((b)->depth == 1 /*file_scope->depth*/)
214 #define B_IN_EXTERNAL_SCOPE(b) ((b)->depth == 0 /*external_scope->depth*/)
216 #define I_SYMBOL_BINDING(node) \
217 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->symbol_binding)
218 #define I_SYMBOL_DECL(node) \
219 (I_SYMBOL_BINDING(node) ? I_SYMBOL_BINDING(node)->decl : 0)
221 #define I_TAG_BINDING(node) \
222 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->tag_binding)
223 #define I_TAG_DECL(node) \
224 (I_TAG_BINDING(node) ? I_TAG_BINDING(node)->decl : 0)
226 #define I_LABEL_BINDING(node) \
227 (((struct lang_identifier *) IDENTIFIER_NODE_CHECK(node))->label_binding)
228 #define I_LABEL_DECL(node) \
229 (I_LABEL_BINDING(node) ? I_LABEL_BINDING(node)->decl : 0)
231 /* Each C symbol points to three linked lists of c_binding structures.
232 These describe the values of the identifier in the three different
233 namespaces defined by the language. */
235 struct lang_identifier GTY(())
237 struct c_common_identifier common_id;
238 struct c_binding *symbol_binding; /* vars, funcs, constants, typedefs */
239 struct c_binding *tag_binding; /* struct/union/enum tags */
240 struct c_binding *label_binding; /* labels */
243 /* Validate c-lang.c's assumptions. */
244 extern char C_SIZEOF_STRUCT_LANG_IDENTIFIER_isnt_accurate
245 [(sizeof(struct lang_identifier) == C_SIZEOF_STRUCT_LANG_IDENTIFIER) ? 1 : -1];
247 /* The resulting tree type. */
249 union lang_tree_node
250 GTY((desc ("TREE_CODE (&%h.generic) == IDENTIFIER_NODE"),
251 chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE ? (union lang_tree_node *) TYPE_NEXT_VARIANT (&%h.generic) : ((union lang_tree_node *) GENERIC_NEXT (&%h.generic))")))
253 union tree_node GTY ((tag ("0"),
254 desc ("tree_node_structure (&%h)")))
255 generic;
256 struct lang_identifier GTY ((tag ("1"))) identifier;
259 /* Each c_scope structure describes the complete contents of one
260 scope. Four scopes are distinguished specially: the innermost or
261 current scope, the innermost function scope, the file scope (always
262 the second to outermost) and the outermost or external scope.
264 Most declarations are recorded in the current scope.
266 All normal label declarations are recorded in the innermost
267 function scope, as are bindings of undeclared identifiers to
268 error_mark_node. (GCC permits nested functions as an extension,
269 hence the 'innermost' qualifier.) Explicitly declared labels
270 (using the __label__ extension) appear in the current scope.
272 Being in the file scope (current_scope == file_scope) causes
273 special behavior in several places below. Also, under some
274 conditions the Objective-C front end records declarations in the
275 file scope even though that isn't the current scope.
277 All declarations with external linkage are recorded in the external
278 scope, even if they aren't visible there; this models the fact that
279 such declarations are visible to the entire program, and (with a
280 bit of cleverness, see pushdecl) allows diagnosis of some violations
281 of C99 6.2.2p7 and 6.2.7p2:
283 If, within the same translation unit, the same identifier appears
284 with both internal and external linkage, the behavior is
285 undefined.
287 All declarations that refer to the same object or function shall
288 have compatible type; otherwise, the behavior is undefined.
290 Initially only the built-in declarations, which describe compiler
291 intrinsic functions plus a subset of the standard library, are in
292 this scope.
294 The order of the blocks list matters, and it is frequently appended
295 to. To avoid having to walk all the way to the end of the list on
296 each insertion, or reverse the list later, we maintain a pointer to
297 the last list entry. (FIXME: It should be feasible to use a reversed
298 list here.)
300 The bindings list is strictly in reverse order of declarations;
301 pop_scope relies on this. */
304 struct c_scope GTY((chain_next ("%h.outer")))
306 /* The scope containing this one. */
307 struct c_scope *outer;
309 /* The next outermost function scope. */
310 struct c_scope *outer_function;
312 /* All bindings in this scope. */
313 struct c_binding *bindings;
315 /* For each scope (except the global one), a chain of BLOCK nodes
316 for all the scopes that were entered and exited one level down. */
317 tree blocks;
318 tree blocks_last;
320 /* The depth of this scope. Used to keep the ->shadowed chain of
321 bindings sorted innermost to outermost. */
322 unsigned int depth : 28;
324 /* True if we are currently filling this scope with parameter
325 declarations. */
326 BOOL_BITFIELD parm_flag : 1;
328 /* True if we saw [*] in this scope. Used to give an error messages
329 if these appears in a function definition. */
330 BOOL_BITFIELD had_vla_unspec : 1;
332 /* True if we already complained about forward parameter decls
333 in this scope. This prevents double warnings on
334 foo (int a; int b; ...) */
335 BOOL_BITFIELD warned_forward_parm_decls : 1;
337 /* True if this is the outermost block scope of a function body.
338 This scope contains the parameters, the local variables declared
339 in the outermost block, and all the labels (except those in
340 nested functions, or declared at block scope with __label__). */
341 BOOL_BITFIELD function_body : 1;
343 /* True means make a BLOCK for this scope no matter what. */
344 BOOL_BITFIELD keep : 1;
347 /* The scope currently in effect. */
349 static GTY(()) struct c_scope *current_scope;
351 /* The innermost function scope. Ordinary (not explicitly declared)
352 labels, bindings to error_mark_node, and the lazily-created
353 bindings of __func__ and its friends get this scope. */
355 static GTY(()) struct c_scope *current_function_scope;
357 /* The C file scope. This is reset for each input translation unit. */
359 static GTY(()) struct c_scope *file_scope;
361 /* The outermost scope. This is used for all declarations with
362 external linkage, and only these, hence the name. */
364 static GTY(()) struct c_scope *external_scope;
366 /* A chain of c_scope structures awaiting reuse. */
368 static GTY((deletable)) struct c_scope *scope_freelist;
370 /* A chain of c_binding structures awaiting reuse. */
372 static GTY((deletable)) struct c_binding *binding_freelist;
374 /* Append VAR to LIST in scope SCOPE. */
375 #define SCOPE_LIST_APPEND(scope, list, decl) do { \
376 struct c_scope *s_ = (scope); \
377 tree d_ = (decl); \
378 if (s_->list##_last) \
379 BLOCK_CHAIN (s_->list##_last) = d_; \
380 else \
381 s_->list = d_; \
382 s_->list##_last = d_; \
383 } while (0)
385 /* Concatenate FROM in scope FSCOPE onto TO in scope TSCOPE. */
386 #define SCOPE_LIST_CONCAT(tscope, to, fscope, from) do { \
387 struct c_scope *t_ = (tscope); \
388 struct c_scope *f_ = (fscope); \
389 if (t_->to##_last) \
390 BLOCK_CHAIN (t_->to##_last) = f_->from; \
391 else \
392 t_->to = f_->from; \
393 t_->to##_last = f_->from##_last; \
394 } while (0)
396 /* True means unconditionally make a BLOCK for the next scope pushed. */
398 static bool keep_next_level_flag;
400 /* True means the next call to push_scope will be the outermost scope
401 of a function body, so do not push a new scope, merely cease
402 expecting parameter decls. */
404 static bool next_is_function_body;
406 /* Forward declarations. */
407 static tree lookup_name_in_scope (tree, struct c_scope *);
408 static tree c_make_fname_decl (tree, int);
409 static tree grokdeclarator (const struct c_declarator *,
410 struct c_declspecs *,
411 enum decl_context, bool, tree *, tree *,
412 enum deprecated_states);
413 static tree grokparms (struct c_arg_info *, bool);
414 static void layout_array_type (tree);
416 /* T is a statement. Add it to the statement-tree. This is the
417 C/ObjC version--C++ has a slightly different version of this
418 function. */
420 tree
421 add_stmt (tree t)
423 enum tree_code code = TREE_CODE (t);
425 if (CAN_HAVE_LOCATION_P (t) && code != LABEL_EXPR)
427 if (!EXPR_HAS_LOCATION (t))
428 SET_EXPR_LOCATION (t, input_location);
431 if (code == LABEL_EXPR || code == CASE_LABEL_EXPR)
432 STATEMENT_LIST_HAS_LABEL (cur_stmt_list) = 1;
434 /* Add T to the statement-tree. Non-side-effect statements need to be
435 recorded during statement expressions. */
436 append_to_statement_list_force (t, &cur_stmt_list);
438 return t;
442 void
443 c_print_identifier (FILE *file, tree node, int indent)
445 print_node (file, "symbol", I_SYMBOL_DECL (node), indent + 4);
446 print_node (file, "tag", I_TAG_DECL (node), indent + 4);
447 print_node (file, "label", I_LABEL_DECL (node), indent + 4);
448 if (C_IS_RESERVED_WORD (node))
450 tree rid = ridpointers[C_RID_CODE (node)];
451 indent_to (file, indent + 4);
452 fprintf (file, "rid %p \"%s\"",
453 (void *) rid, IDENTIFIER_POINTER (rid));
457 /* Establish a binding between NAME, an IDENTIFIER_NODE, and DECL,
458 which may be any of several kinds of DECL or TYPE or error_mark_node,
459 in the scope SCOPE. */
460 static void
461 bind (tree name, tree decl, struct c_scope *scope, bool invisible, bool nested)
463 struct c_binding *b, **here;
465 if (binding_freelist)
467 b = binding_freelist;
468 binding_freelist = b->prev;
470 else
471 b = GGC_NEW (struct c_binding);
473 b->shadowed = 0;
474 b->decl = decl;
475 b->id = name;
476 b->depth = scope->depth;
477 b->invisible = invisible;
478 b->nested = nested;
479 b->inner_comp = 0;
481 b->type = 0;
483 b->prev = scope->bindings;
484 scope->bindings = b;
486 if (!name)
487 return;
489 switch (TREE_CODE (decl))
491 case LABEL_DECL: here = &I_LABEL_BINDING (name); break;
492 case ENUMERAL_TYPE:
493 case UNION_TYPE:
494 case RECORD_TYPE: here = &I_TAG_BINDING (name); break;
495 case VAR_DECL:
496 case FUNCTION_DECL:
497 case TYPE_DECL:
498 case CONST_DECL:
499 case PARM_DECL:
500 case ERROR_MARK: here = &I_SYMBOL_BINDING (name); break;
502 default:
503 gcc_unreachable ();
506 /* Locate the appropriate place in the chain of shadowed decls
507 to insert this binding. Normally, scope == current_scope and
508 this does nothing. */
509 while (*here && (*here)->depth > scope->depth)
510 here = &(*here)->shadowed;
512 b->shadowed = *here;
513 *here = b;
516 /* Clear the binding structure B, stick it on the binding_freelist,
517 and return the former value of b->prev. This is used by pop_scope
518 and get_parm_info to iterate destructively over all the bindings
519 from a given scope. */
520 static struct c_binding *
521 free_binding_and_advance (struct c_binding *b)
523 struct c_binding *prev = b->prev;
525 memset (b, 0, sizeof (struct c_binding));
526 b->prev = binding_freelist;
527 binding_freelist = b;
529 return prev;
533 /* Hook called at end of compilation to assume 1 elt
534 for a file-scope tentative array defn that wasn't complete before. */
536 void
537 c_finish_incomplete_decl (tree decl)
539 if (TREE_CODE (decl) == VAR_DECL)
541 tree type = TREE_TYPE (decl);
542 if (type != error_mark_node
543 && TREE_CODE (type) == ARRAY_TYPE
544 && !DECL_EXTERNAL (decl)
545 && TYPE_DOMAIN (type) == 0)
547 warning (0, "array %q+D assumed to have one element", decl);
549 complete_array_type (&TREE_TYPE (decl), NULL_TREE, true);
551 layout_decl (decl, 0);
556 /* The Objective-C front-end often needs to determine the current scope. */
558 void *
559 objc_get_current_scope (void)
561 return current_scope;
564 /* The following function is used only by Objective-C. It needs to live here
565 because it accesses the innards of c_scope. */
567 void
568 objc_mark_locals_volatile (void *enclosing_blk)
570 struct c_scope *scope;
571 struct c_binding *b;
573 for (scope = current_scope;
574 scope && scope != enclosing_blk;
575 scope = scope->outer)
577 for (b = scope->bindings; b; b = b->prev)
578 objc_volatilize_decl (b->decl);
580 /* Do not climb up past the current function. */
581 if (scope->function_body)
582 break;
586 /* Nonzero if we are currently in file scope. */
589 global_bindings_p (void)
591 return current_scope == file_scope && !c_override_global_bindings_to_false;
594 void
595 keep_next_level (void)
597 keep_next_level_flag = true;
600 /* Identify this scope as currently being filled with parameters. */
602 void
603 declare_parm_level (void)
605 current_scope->parm_flag = true;
608 void
609 push_scope (void)
611 if (next_is_function_body)
613 /* This is the transition from the parameters to the top level
614 of the function body. These are the same scope
615 (C99 6.2.1p4,6) so we do not push another scope structure.
616 next_is_function_body is set only by store_parm_decls, which
617 in turn is called when and only when we are about to
618 encounter the opening curly brace for the function body.
620 The outermost block of a function always gets a BLOCK node,
621 because the debugging output routines expect that each
622 function has at least one BLOCK. */
623 current_scope->parm_flag = false;
624 current_scope->function_body = true;
625 current_scope->keep = true;
626 current_scope->outer_function = current_function_scope;
627 current_function_scope = current_scope;
629 keep_next_level_flag = false;
630 next_is_function_body = false;
632 else
634 struct c_scope *scope;
635 if (scope_freelist)
637 scope = scope_freelist;
638 scope_freelist = scope->outer;
640 else
641 scope = GGC_CNEW (struct c_scope);
643 scope->keep = keep_next_level_flag;
644 scope->outer = current_scope;
645 scope->depth = current_scope ? (current_scope->depth + 1) : 0;
647 /* Check for scope depth overflow. Unlikely (2^28 == 268,435,456) but
648 possible. */
649 if (current_scope && scope->depth == 0)
651 scope->depth--;
652 sorry ("GCC supports only %u nested scopes", scope->depth);
655 current_scope = scope;
656 keep_next_level_flag = false;
660 /* Set the TYPE_CONTEXT of all of TYPE's variants to CONTEXT. */
662 static void
663 set_type_context (tree type, tree context)
665 for (type = TYPE_MAIN_VARIANT (type); type;
666 type = TYPE_NEXT_VARIANT (type))
667 TYPE_CONTEXT (type) = context;
670 /* Exit a scope. Restore the state of the identifier-decl mappings
671 that were in effect when this scope was entered. Return a BLOCK
672 node containing all the DECLs in this scope that are of interest
673 to debug info generation. */
675 tree
676 pop_scope (void)
678 struct c_scope *scope = current_scope;
679 tree block, context, p;
680 struct c_binding *b;
682 bool functionbody = scope->function_body;
683 bool keep = functionbody || scope->keep || scope->bindings;
685 c_end_vm_scope (scope->depth);
687 /* If appropriate, create a BLOCK to record the decls for the life
688 of this function. */
689 block = 0;
690 if (keep)
692 block = make_node (BLOCK);
693 BLOCK_SUBBLOCKS (block) = scope->blocks;
694 TREE_USED (block) = 1;
696 /* In each subblock, record that this is its superior. */
697 for (p = scope->blocks; p; p = BLOCK_CHAIN (p))
698 BLOCK_SUPERCONTEXT (p) = block;
700 BLOCK_VARS (block) = 0;
703 /* The TYPE_CONTEXTs for all of the tagged types belonging to this
704 scope must be set so that they point to the appropriate
705 construct, i.e. either to the current FUNCTION_DECL node, or
706 else to the BLOCK node we just constructed.
708 Note that for tagged types whose scope is just the formal
709 parameter list for some function type specification, we can't
710 properly set their TYPE_CONTEXTs here, because we don't have a
711 pointer to the appropriate FUNCTION_TYPE node readily available
712 to us. For those cases, the TYPE_CONTEXTs of the relevant tagged
713 type nodes get set in `grokdeclarator' as soon as we have created
714 the FUNCTION_TYPE node which will represent the "scope" for these
715 "parameter list local" tagged types. */
716 if (scope->function_body)
717 context = current_function_decl;
718 else if (scope == file_scope)
720 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, 0, 0);
721 TREE_CHAIN (file_decl) = all_translation_units;
722 all_translation_units = file_decl;
723 context = file_decl;
725 else
726 context = block;
728 /* Clear all bindings in this scope. */
729 for (b = scope->bindings; b; b = free_binding_and_advance (b))
731 p = b->decl;
732 switch (TREE_CODE (p))
734 case LABEL_DECL:
735 /* Warnings for unused labels, errors for undefined labels. */
736 if (TREE_USED (p) && !DECL_INITIAL (p))
738 error ("label %q+D used but not defined", p);
739 DECL_INITIAL (p) = error_mark_node;
741 else
742 warn_for_unused_label (p);
744 /* Labels go in BLOCK_VARS. */
745 TREE_CHAIN (p) = BLOCK_VARS (block);
746 BLOCK_VARS (block) = p;
747 gcc_assert (I_LABEL_BINDING (b->id) == b);
748 I_LABEL_BINDING (b->id) = b->shadowed;
749 break;
751 case ENUMERAL_TYPE:
752 case UNION_TYPE:
753 case RECORD_TYPE:
754 set_type_context (p, context);
756 /* Types may not have tag-names, in which case the type
757 appears in the bindings list with b->id NULL. */
758 if (b->id)
760 gcc_assert (I_TAG_BINDING (b->id) == b);
761 I_TAG_BINDING (b->id) = b->shadowed;
763 break;
765 case FUNCTION_DECL:
766 /* Propagate TREE_ADDRESSABLE from nested functions to their
767 containing functions. */
768 if (!TREE_ASM_WRITTEN (p)
769 && DECL_INITIAL (p) != 0
770 && TREE_ADDRESSABLE (p)
771 && DECL_ABSTRACT_ORIGIN (p) != 0
772 && DECL_ABSTRACT_ORIGIN (p) != p)
773 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (p)) = 1;
774 if (!DECL_EXTERNAL (p)
775 && !DECL_INITIAL (p)
776 && scope != file_scope
777 && scope != external_scope)
779 error ("nested function %q+D declared but never defined", p);
780 undef_nested_function = true;
782 /* C99 6.7.4p6: "a function with external linkage... declared
783 with an inline function specifier ... shall also be defined in the
784 same translation unit." */
785 else if (DECL_DECLARED_INLINE_P (p)
786 && TREE_PUBLIC (p)
787 && !DECL_INITIAL (p)
788 && !flag_gnu89_inline)
789 pedwarn ("inline function %q+D declared but never defined", p);
791 goto common_symbol;
793 case VAR_DECL:
794 /* Warnings for unused variables. */
795 if (!TREE_USED (p)
796 && !TREE_NO_WARNING (p)
797 && !DECL_IN_SYSTEM_HEADER (p)
798 && DECL_NAME (p)
799 && !DECL_ARTIFICIAL (p)
800 && scope != file_scope
801 && scope != external_scope)
802 warning (OPT_Wunused_variable, "unused variable %q+D", p);
804 if (b->inner_comp)
806 error ("type of array %q+D completed incompatibly with"
807 " implicit initialization", p);
810 /* Fall through. */
811 case TYPE_DECL:
812 case CONST_DECL:
813 common_symbol:
814 /* All of these go in BLOCK_VARS, but only if this is the
815 binding in the home scope. */
816 if (!b->nested)
818 TREE_CHAIN (p) = BLOCK_VARS (block);
819 BLOCK_VARS (block) = p;
821 /* If this is the file scope, and we are processing more
822 than one translation unit in this compilation, set
823 DECL_CONTEXT of each decl to the TRANSLATION_UNIT_DECL.
824 This makes same_translation_unit_p work, and causes
825 static declarations to be given disambiguating suffixes. */
826 if (scope == file_scope && num_in_fnames > 1)
828 DECL_CONTEXT (p) = context;
829 if (TREE_CODE (p) == TYPE_DECL)
830 set_type_context (TREE_TYPE (p), context);
833 /* Fall through. */
834 /* Parameters go in DECL_ARGUMENTS, not BLOCK_VARS, and have
835 already been put there by store_parm_decls. Unused-
836 parameter warnings are handled by function.c.
837 error_mark_node obviously does not go in BLOCK_VARS and
838 does not get unused-variable warnings. */
839 case PARM_DECL:
840 case ERROR_MARK:
841 /* It is possible for a decl not to have a name. We get
842 here with b->id NULL in this case. */
843 if (b->id)
845 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
846 I_SYMBOL_BINDING (b->id) = b->shadowed;
847 if (b->shadowed && b->shadowed->type)
848 TREE_TYPE (b->shadowed->decl) = b->shadowed->type;
850 break;
852 default:
853 gcc_unreachable ();
858 /* Dispose of the block that we just made inside some higher level. */
859 if ((scope->function_body || scope == file_scope) && context)
861 DECL_INITIAL (context) = block;
862 BLOCK_SUPERCONTEXT (block) = context;
864 else if (scope->outer)
866 if (block)
867 SCOPE_LIST_APPEND (scope->outer, blocks, block);
868 /* If we did not make a block for the scope just exited, any
869 blocks made for inner scopes must be carried forward so they
870 will later become subblocks of something else. */
871 else if (scope->blocks)
872 SCOPE_LIST_CONCAT (scope->outer, blocks, scope, blocks);
875 /* Pop the current scope, and free the structure for reuse. */
876 current_scope = scope->outer;
877 if (scope->function_body)
878 current_function_scope = scope->outer_function;
880 memset (scope, 0, sizeof (struct c_scope));
881 scope->outer = scope_freelist;
882 scope_freelist = scope;
884 return block;
887 void
888 push_file_scope (void)
890 tree decl;
892 if (file_scope)
893 return;
895 push_scope ();
896 file_scope = current_scope;
898 start_fname_decls ();
900 for (decl = visible_builtins; decl; decl = TREE_CHAIN (decl))
901 bind (DECL_NAME (decl), decl, file_scope,
902 /*invisible=*/false, /*nested=*/true);
905 void
906 pop_file_scope (void)
908 /* In case there were missing closebraces, get us back to the global
909 binding level. */
910 while (current_scope != file_scope)
911 pop_scope ();
913 /* __FUNCTION__ is defined at file scope (""). This
914 call may not be necessary as my tests indicate it
915 still works without it. */
916 finish_fname_decls ();
918 /* This is the point to write out a PCH if we're doing that.
919 In that case we do not want to do anything else. */
920 if (pch_file)
922 c_common_write_pch ();
923 return;
926 /* Pop off the file scope and close this translation unit. */
927 pop_scope ();
928 file_scope = 0;
930 maybe_apply_pending_pragma_weaks ();
931 cgraph_finalize_compilation_unit ();
935 /* Push a definition or a declaration of struct, union or enum tag "name".
936 "type" should be the type node.
937 We assume that the tag "name" is not already defined.
939 Note that the definition may really be just a forward reference.
940 In that case, the TYPE_SIZE will be zero. */
942 static void
943 pushtag (tree name, tree type)
945 /* Record the identifier as the type's name if it has none. */
946 if (name && !TYPE_NAME (type))
947 TYPE_NAME (type) = name;
948 bind (name, type, current_scope, /*invisible=*/false, /*nested=*/false);
950 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE will be the
951 tagged type we just added to the current scope. This fake
952 NULL-named TYPE_DECL node helps dwarfout.c to know when it needs
953 to output a representation of a tagged type, and it also gives
954 us a convenient place to record the "scope start" address for the
955 tagged type. */
957 TYPE_STUB_DECL (type) = pushdecl (build_decl (TYPE_DECL, NULL_TREE, type));
959 /* An approximation for now, so we can tell this is a function-scope tag.
960 This will be updated in pop_scope. */
961 TYPE_CONTEXT (type) = DECL_CONTEXT (TYPE_STUB_DECL (type));
964 /* Subroutine of compare_decls. Allow harmless mismatches in return
965 and argument types provided that the type modes match. This function
966 return a unified type given a suitable match, and 0 otherwise. */
968 static tree
969 match_builtin_function_types (tree newtype, tree oldtype)
971 tree newrettype, oldrettype;
972 tree newargs, oldargs;
973 tree trytype, tryargs;
975 /* Accept the return type of the new declaration if same modes. */
976 oldrettype = TREE_TYPE (oldtype);
977 newrettype = TREE_TYPE (newtype);
979 if (TYPE_MODE (oldrettype) != TYPE_MODE (newrettype))
980 return 0;
982 oldargs = TYPE_ARG_TYPES (oldtype);
983 newargs = TYPE_ARG_TYPES (newtype);
984 tryargs = newargs;
986 while (oldargs || newargs)
988 if (!oldargs
989 || !newargs
990 || !TREE_VALUE (oldargs)
991 || !TREE_VALUE (newargs)
992 || TYPE_MODE (TREE_VALUE (oldargs))
993 != TYPE_MODE (TREE_VALUE (newargs)))
994 return 0;
996 oldargs = TREE_CHAIN (oldargs);
997 newargs = TREE_CHAIN (newargs);
1000 trytype = build_function_type (newrettype, tryargs);
1001 return build_type_attribute_variant (trytype, TYPE_ATTRIBUTES (oldtype));
1004 /* Subroutine of diagnose_mismatched_decls. Check for function type
1005 mismatch involving an empty arglist vs a nonempty one and give clearer
1006 diagnostics. */
1007 static void
1008 diagnose_arglist_conflict (tree newdecl, tree olddecl,
1009 tree newtype, tree oldtype)
1011 tree t;
1013 if (TREE_CODE (olddecl) != FUNCTION_DECL
1014 || !comptypes (TREE_TYPE (oldtype), TREE_TYPE (newtype))
1015 || !((TYPE_ARG_TYPES (oldtype) == 0 && DECL_INITIAL (olddecl) == 0)
1017 (TYPE_ARG_TYPES (newtype) == 0 && DECL_INITIAL (newdecl) == 0)))
1018 return;
1020 t = TYPE_ARG_TYPES (oldtype);
1021 if (t == 0)
1022 t = TYPE_ARG_TYPES (newtype);
1023 for (; t; t = TREE_CHAIN (t))
1025 tree type = TREE_VALUE (t);
1027 if (TREE_CHAIN (t) == 0
1028 && TYPE_MAIN_VARIANT (type) != void_type_node)
1030 inform ("a parameter list with an ellipsis can%'t match "
1031 "an empty parameter name list declaration");
1032 break;
1035 if (c_type_promotes_to (type) != type)
1037 inform ("an argument type that has a default promotion can%'t match "
1038 "an empty parameter name list declaration");
1039 break;
1044 /* Another subroutine of diagnose_mismatched_decls. OLDDECL is an
1045 old-style function definition, NEWDECL is a prototype declaration.
1046 Diagnose inconsistencies in the argument list. Returns TRUE if
1047 the prototype is compatible, FALSE if not. */
1048 static bool
1049 validate_proto_after_old_defn (tree newdecl, tree newtype, tree oldtype)
1051 tree newargs, oldargs;
1052 int i;
1054 #define END_OF_ARGLIST(t) ((t) == void_type_node)
1056 oldargs = TYPE_ACTUAL_ARG_TYPES (oldtype);
1057 newargs = TYPE_ARG_TYPES (newtype);
1058 i = 1;
1060 for (;;)
1062 tree oldargtype = TREE_VALUE (oldargs);
1063 tree newargtype = TREE_VALUE (newargs);
1065 if (oldargtype == error_mark_node || newargtype == error_mark_node)
1066 return false;
1068 oldargtype = TYPE_MAIN_VARIANT (oldargtype);
1069 newargtype = TYPE_MAIN_VARIANT (newargtype);
1071 if (END_OF_ARGLIST (oldargtype) && END_OF_ARGLIST (newargtype))
1072 break;
1074 /* Reaching the end of just one list means the two decls don't
1075 agree on the number of arguments. */
1076 if (END_OF_ARGLIST (oldargtype))
1078 error ("prototype for %q+D declares more arguments "
1079 "than previous old-style definition", newdecl);
1080 return false;
1082 else if (END_OF_ARGLIST (newargtype))
1084 error ("prototype for %q+D declares fewer arguments "
1085 "than previous old-style definition", newdecl);
1086 return false;
1089 /* Type for passing arg must be consistent with that declared
1090 for the arg. */
1091 else if (!comptypes (oldargtype, newargtype))
1093 error ("prototype for %q+D declares argument %d"
1094 " with incompatible type",
1095 newdecl, i);
1096 return false;
1099 oldargs = TREE_CHAIN (oldargs);
1100 newargs = TREE_CHAIN (newargs);
1101 i++;
1104 /* If we get here, no errors were found, but do issue a warning
1105 for this poor-style construct. */
1106 warning (0, "prototype for %q+D follows non-prototype definition",
1107 newdecl);
1108 return true;
1109 #undef END_OF_ARGLIST
1112 /* Subroutine of diagnose_mismatched_decls. Report the location of DECL,
1113 first in a pair of mismatched declarations, using the diagnostic
1114 function DIAG. */
1115 static void
1116 locate_old_decl (tree decl, void (*diag)(const char *, ...) ATTRIBUTE_GCC_CDIAG(1,2))
1118 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1120 else if (DECL_INITIAL (decl))
1121 diag (G_("previous definition of %q+D was here"), decl);
1122 else if (C_DECL_IMPLICIT (decl))
1123 diag (G_("previous implicit declaration of %q+D was here"), decl);
1124 else
1125 diag (G_("previous declaration of %q+D was here"), decl);
1128 /* Subroutine of duplicate_decls. Compare NEWDECL to OLDDECL.
1129 Returns true if the caller should proceed to merge the two, false
1130 if OLDDECL should simply be discarded. As a side effect, issues
1131 all necessary diagnostics for invalid or poor-style combinations.
1132 If it returns true, writes the types of NEWDECL and OLDDECL to
1133 *NEWTYPEP and *OLDTYPEP - these may have been adjusted from
1134 TREE_TYPE (NEWDECL, OLDDECL) respectively. */
1136 static bool
1137 diagnose_mismatched_decls (tree newdecl, tree olddecl,
1138 tree *newtypep, tree *oldtypep)
1140 tree newtype, oldtype;
1141 bool pedwarned = false;
1142 bool warned = false;
1143 bool retval = true;
1145 #define DECL_EXTERN_INLINE(DECL) (DECL_DECLARED_INLINE_P (DECL) \
1146 && DECL_EXTERNAL (DECL))
1148 /* If we have error_mark_node for either decl or type, just discard
1149 the previous decl - we're in an error cascade already. */
1150 if (olddecl == error_mark_node || newdecl == error_mark_node)
1151 return false;
1152 *oldtypep = oldtype = TREE_TYPE (olddecl);
1153 *newtypep = newtype = TREE_TYPE (newdecl);
1154 if (oldtype == error_mark_node || newtype == error_mark_node)
1155 return false;
1157 /* Two different categories of symbol altogether. This is an error
1158 unless OLDDECL is a builtin. OLDDECL will be discarded in any case. */
1159 if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1161 if (!(TREE_CODE (olddecl) == FUNCTION_DECL
1162 && DECL_BUILT_IN (olddecl)
1163 && !C_DECL_DECLARED_BUILTIN (olddecl)))
1165 error ("%q+D redeclared as different kind of symbol", newdecl);
1166 locate_old_decl (olddecl, error);
1168 else if (TREE_PUBLIC (newdecl))
1169 warning (0, "built-in function %q+D declared as non-function",
1170 newdecl);
1171 else
1172 warning (OPT_Wshadow, "declaration of %q+D shadows "
1173 "a built-in function", newdecl);
1174 return false;
1177 /* Enumerators have no linkage, so may only be declared once in a
1178 given scope. */
1179 if (TREE_CODE (olddecl) == CONST_DECL)
1181 error ("redeclaration of enumerator %q+D", newdecl);
1182 locate_old_decl (olddecl, error);
1183 return false;
1186 if (!comptypes (oldtype, newtype))
1188 if (TREE_CODE (olddecl) == FUNCTION_DECL
1189 && DECL_BUILT_IN (olddecl) && !C_DECL_DECLARED_BUILTIN (olddecl))
1191 /* Accept harmless mismatch in function types.
1192 This is for the ffs and fprintf builtins. */
1193 tree trytype = match_builtin_function_types (newtype, oldtype);
1195 if (trytype && comptypes (newtype, trytype))
1196 *oldtypep = oldtype = trytype;
1197 else
1199 /* If types don't match for a built-in, throw away the
1200 built-in. No point in calling locate_old_decl here, it
1201 won't print anything. */
1202 warning (0, "conflicting types for built-in function %q+D",
1203 newdecl);
1204 return false;
1207 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1208 && DECL_IS_BUILTIN (olddecl))
1210 /* A conflicting function declaration for a predeclared
1211 function that isn't actually built in. Objective C uses
1212 these. The new declaration silently overrides everything
1213 but the volatility (i.e. noreturn) indication. See also
1214 below. FIXME: Make Objective C use normal builtins. */
1215 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1216 return false;
1218 /* Permit void foo (...) to match int foo (...) if the latter is
1219 the definition and implicit int was used. See
1220 c-torture/compile/920625-2.c. */
1221 else if (TREE_CODE (newdecl) == FUNCTION_DECL && DECL_INITIAL (newdecl)
1222 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == void_type_node
1223 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == integer_type_node
1224 && C_FUNCTION_IMPLICIT_INT (newdecl) && !DECL_INITIAL (olddecl))
1226 pedwarn ("conflicting types for %q+D", newdecl);
1227 /* Make sure we keep void as the return type. */
1228 TREE_TYPE (newdecl) = *newtypep = newtype = oldtype;
1229 C_FUNCTION_IMPLICIT_INT (newdecl) = 0;
1230 pedwarned = true;
1232 /* Permit void foo (...) to match an earlier call to foo (...) with
1233 no declared type (thus, implicitly int). */
1234 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1235 && TYPE_MAIN_VARIANT (TREE_TYPE (newtype)) == void_type_node
1236 && TYPE_MAIN_VARIANT (TREE_TYPE (oldtype)) == integer_type_node
1237 && C_DECL_IMPLICIT (olddecl) && !DECL_INITIAL (olddecl))
1239 pedwarn ("conflicting types for %q+D", newdecl);
1240 /* Make sure we keep void as the return type. */
1241 TREE_TYPE (olddecl) = *oldtypep = oldtype = newtype;
1242 pedwarned = true;
1244 else
1246 if (TYPE_QUALS (newtype) != TYPE_QUALS (oldtype))
1247 error ("conflicting type qualifiers for %q+D", newdecl);
1248 else
1249 error ("conflicting types for %q+D", newdecl);
1250 diagnose_arglist_conflict (newdecl, olddecl, newtype, oldtype);
1251 locate_old_decl (olddecl, error);
1252 return false;
1256 /* Redeclaration of a type is a constraint violation (6.7.2.3p1),
1257 but silently ignore the redeclaration if either is in a system
1258 header. (Conflicting redeclarations were handled above.) */
1259 if (TREE_CODE (newdecl) == TYPE_DECL)
1261 if (DECL_IN_SYSTEM_HEADER (newdecl) || DECL_IN_SYSTEM_HEADER (olddecl))
1262 return true; /* Allow OLDDECL to continue in use. */
1264 error ("redefinition of typedef %q+D", newdecl);
1265 locate_old_decl (olddecl, error);
1266 return false;
1269 /* Function declarations can either be 'static' or 'extern' (no
1270 qualifier is equivalent to 'extern' - C99 6.2.2p5) and therefore
1271 can never conflict with each other on account of linkage
1272 (6.2.2p4). Multiple definitions are not allowed (6.9p3,5) but
1273 gnu89 mode permits two definitions if one is 'extern inline' and
1274 one is not. The non- extern-inline definition supersedes the
1275 extern-inline definition. */
1277 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1279 /* If you declare a built-in function name as static, or
1280 define the built-in with an old-style definition (so we
1281 can't validate the argument list) the built-in definition is
1282 overridden, but optionally warn this was a bad choice of name. */
1283 if (DECL_BUILT_IN (olddecl)
1284 && !C_DECL_DECLARED_BUILTIN (olddecl)
1285 && (!TREE_PUBLIC (newdecl)
1286 || (DECL_INITIAL (newdecl)
1287 && !TYPE_ARG_TYPES (TREE_TYPE (newdecl)))))
1289 warning (OPT_Wshadow, "declaration of %q+D shadows "
1290 "a built-in function", newdecl);
1291 /* Discard the old built-in function. */
1292 return false;
1295 if (DECL_INITIAL (newdecl))
1297 if (DECL_INITIAL (olddecl))
1299 /* If both decls are in the same TU and the new declaration
1300 isn't overriding an extern inline reject the new decl.
1301 In c99, no overriding is allowed in the same translation
1302 unit. */
1303 if ((!DECL_EXTERN_INLINE (olddecl)
1304 || DECL_EXTERN_INLINE (newdecl)
1305 || (!flag_gnu89_inline
1306 && (!DECL_DECLARED_INLINE_P (olddecl)
1307 || !lookup_attribute ("gnu_inline",
1308 DECL_ATTRIBUTES (olddecl)))
1309 && (!DECL_DECLARED_INLINE_P (newdecl)
1310 || !lookup_attribute ("gnu_inline",
1311 DECL_ATTRIBUTES (newdecl))))
1313 && same_translation_unit_p (newdecl, olddecl))
1315 error ("redefinition of %q+D", newdecl);
1316 locate_old_decl (olddecl, error);
1317 return false;
1321 /* If we have a prototype after an old-style function definition,
1322 the argument types must be checked specially. */
1323 else if (DECL_INITIAL (olddecl)
1324 && !TYPE_ARG_TYPES (oldtype) && TYPE_ARG_TYPES (newtype)
1325 && TYPE_ACTUAL_ARG_TYPES (oldtype)
1326 && !validate_proto_after_old_defn (newdecl, newtype, oldtype))
1328 locate_old_decl (olddecl, error);
1329 return false;
1331 /* A non-static declaration (even an "extern") followed by a
1332 static declaration is undefined behavior per C99 6.2.2p3-5,7.
1333 The same is true for a static forward declaration at block
1334 scope followed by a non-static declaration/definition at file
1335 scope. Static followed by non-static at the same scope is
1336 not undefined behavior, and is the most convenient way to get
1337 some effects (see e.g. what unwind-dw2-fde-glibc.c does to
1338 the definition of _Unwind_Find_FDE in unwind-dw2-fde.c), but
1339 we do diagnose it if -Wtraditional. */
1340 if (TREE_PUBLIC (olddecl) && !TREE_PUBLIC (newdecl))
1342 /* Two exceptions to the rule. If olddecl is an extern
1343 inline, or a predeclared function that isn't actually
1344 built in, newdecl silently overrides olddecl. The latter
1345 occur only in Objective C; see also above. (FIXME: Make
1346 Objective C use normal builtins.) */
1347 if (!DECL_IS_BUILTIN (olddecl)
1348 && !DECL_EXTERN_INLINE (olddecl))
1350 error ("static declaration of %q+D follows "
1351 "non-static declaration", newdecl);
1352 locate_old_decl (olddecl, error);
1354 return false;
1356 else if (TREE_PUBLIC (newdecl) && !TREE_PUBLIC (olddecl))
1358 if (DECL_CONTEXT (olddecl))
1360 error ("non-static declaration of %q+D follows "
1361 "static declaration", newdecl);
1362 locate_old_decl (olddecl, error);
1363 return false;
1365 else if (warn_traditional)
1367 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1368 "follows static declaration", newdecl);
1369 warned = true;
1373 /* Make sure gnu_inline attribute is either not present, or
1374 present on all inline decls. */
1375 if (DECL_DECLARED_INLINE_P (olddecl)
1376 && DECL_DECLARED_INLINE_P (newdecl))
1378 bool newa = lookup_attribute ("gnu_inline",
1379 DECL_ATTRIBUTES (newdecl)) != NULL;
1380 bool olda = lookup_attribute ("gnu_inline",
1381 DECL_ATTRIBUTES (olddecl)) != NULL;
1382 if (newa != olda)
1384 error ("%<gnu_inline%> attribute present on %q+D",
1385 newa ? newdecl : olddecl);
1386 error ("%Jbut not here", newa ? olddecl : newdecl);
1390 else if (TREE_CODE (newdecl) == VAR_DECL)
1392 /* Only variables can be thread-local, and all declarations must
1393 agree on this property. */
1394 if (C_DECL_THREADPRIVATE_P (olddecl) && !DECL_THREAD_LOCAL_P (newdecl))
1396 /* Nothing to check. Since OLDDECL is marked threadprivate
1397 and NEWDECL does not have a thread-local attribute, we
1398 will merge the threadprivate attribute into NEWDECL. */
1401 else if (DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl))
1403 if (DECL_THREAD_LOCAL_P (newdecl))
1404 error ("thread-local declaration of %q+D follows "
1405 "non-thread-local declaration", newdecl);
1406 else
1407 error ("non-thread-local declaration of %q+D follows "
1408 "thread-local declaration", newdecl);
1410 locate_old_decl (olddecl, error);
1411 return false;
1414 /* Multiple initialized definitions are not allowed (6.9p3,5). */
1415 if (DECL_INITIAL (newdecl) && DECL_INITIAL (olddecl))
1417 error ("redefinition of %q+D", newdecl);
1418 locate_old_decl (olddecl, error);
1419 return false;
1422 /* Objects declared at file scope: if the first declaration had
1423 external linkage (even if it was an external reference) the
1424 second must have external linkage as well, or the behavior is
1425 undefined. If the first declaration had internal linkage, then
1426 the second must too, or else be an external reference (in which
1427 case the composite declaration still has internal linkage).
1428 As for function declarations, we warn about the static-then-
1429 extern case only for -Wtraditional. See generally 6.2.2p3-5,7. */
1430 if (DECL_FILE_SCOPE_P (newdecl)
1431 && TREE_PUBLIC (newdecl) != TREE_PUBLIC (olddecl))
1433 if (DECL_EXTERNAL (newdecl))
1435 if (!DECL_FILE_SCOPE_P (olddecl))
1437 error ("extern declaration of %q+D follows "
1438 "declaration with no linkage", newdecl);
1439 locate_old_decl (olddecl, error);
1440 return false;
1442 else if (warn_traditional)
1444 warning (OPT_Wtraditional, "non-static declaration of %q+D "
1445 "follows static declaration", newdecl);
1446 warned = true;
1449 else
1451 if (TREE_PUBLIC (newdecl))
1452 error ("non-static declaration of %q+D follows "
1453 "static declaration", newdecl);
1454 else
1455 error ("static declaration of %q+D follows "
1456 "non-static declaration", newdecl);
1458 locate_old_decl (olddecl, error);
1459 return false;
1462 /* Two objects with the same name declared at the same block
1463 scope must both be external references (6.7p3). */
1464 else if (!DECL_FILE_SCOPE_P (newdecl))
1466 if (DECL_EXTERNAL (newdecl))
1468 /* Extern with initializer at block scope, which will
1469 already have received an error. */
1471 else if (DECL_EXTERNAL (olddecl))
1473 error ("declaration of %q+D with no linkage follows "
1474 "extern declaration", newdecl);
1475 locate_old_decl (olddecl, error);
1477 else
1479 error ("redeclaration of %q+D with no linkage", newdecl);
1480 locate_old_decl (olddecl, error);
1483 return false;
1487 /* warnings */
1488 /* All decls must agree on a visibility. */
1489 if (CODE_CONTAINS_STRUCT (TREE_CODE (newdecl), TS_DECL_WITH_VIS)
1490 && DECL_VISIBILITY_SPECIFIED (newdecl) && DECL_VISIBILITY_SPECIFIED (olddecl)
1491 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1493 warning (0, "redeclaration of %q+D with different visibility "
1494 "(old visibility preserved)", newdecl);
1495 warned = true;
1498 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1500 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
1501 if (DECL_DECLARED_INLINE_P (newdecl)
1502 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1504 warning (OPT_Wattributes, "inline declaration of %qD follows "
1505 "declaration with attribute noinline", newdecl);
1506 warned = true;
1508 else if (DECL_DECLARED_INLINE_P (olddecl)
1509 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1511 warning (OPT_Wattributes, "declaration of %q+D with attribute "
1512 "noinline follows inline declaration ", newdecl);
1513 warned = true;
1516 /* Inline declaration after use or definition.
1517 ??? Should we still warn about this now we have unit-at-a-time
1518 mode and can get it right?
1519 Definitely don't complain if the decls are in different translation
1520 units.
1521 C99 permits this, so don't warn in that case. (The function
1522 may not be inlined everywhere in function-at-a-time mode, but
1523 we still shouldn't warn.) */
1524 if (DECL_DECLARED_INLINE_P (newdecl) && !DECL_DECLARED_INLINE_P (olddecl)
1525 && same_translation_unit_p (olddecl, newdecl)
1526 && flag_gnu89_inline)
1528 if (TREE_USED (olddecl))
1530 warning (0, "%q+D declared inline after being called", olddecl);
1531 warned = true;
1533 else if (DECL_INITIAL (olddecl))
1535 warning (0, "%q+D declared inline after its definition", olddecl);
1536 warned = true;
1540 else /* PARM_DECL, VAR_DECL */
1542 /* Redeclaration of a parameter is a constraint violation (this is
1543 not explicitly stated, but follows from C99 6.7p3 [no more than
1544 one declaration of the same identifier with no linkage in the
1545 same scope, except type tags] and 6.2.2p6 [parameters have no
1546 linkage]). We must check for a forward parameter declaration,
1547 indicated by TREE_ASM_WRITTEN on the old declaration - this is
1548 an extension, the mandatory diagnostic for which is handled by
1549 mark_forward_parm_decls. */
1551 if (TREE_CODE (newdecl) == PARM_DECL
1552 && (!TREE_ASM_WRITTEN (olddecl) || TREE_ASM_WRITTEN (newdecl)))
1554 error ("redefinition of parameter %q+D", newdecl);
1555 locate_old_decl (olddecl, error);
1556 return false;
1560 /* Optional warning for completely redundant decls. */
1561 if (!warned && !pedwarned
1562 && warn_redundant_decls
1563 /* Don't warn about a function declaration followed by a
1564 definition. */
1565 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1566 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl))
1567 /* Don't warn about redundant redeclarations of builtins. */
1568 && !(TREE_CODE (newdecl) == FUNCTION_DECL
1569 && !DECL_BUILT_IN (newdecl)
1570 && DECL_BUILT_IN (olddecl)
1571 && !C_DECL_DECLARED_BUILTIN (olddecl))
1572 /* Don't warn about an extern followed by a definition. */
1573 && !(DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl))
1574 /* Don't warn about forward parameter decls. */
1575 && !(TREE_CODE (newdecl) == PARM_DECL
1576 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1577 /* Don't warn about a variable definition following a declaration. */
1578 && !(TREE_CODE (newdecl) == VAR_DECL
1579 && DECL_INITIAL (newdecl) && !DECL_INITIAL (olddecl)))
1581 warning (OPT_Wredundant_decls, "redundant redeclaration of %q+D",
1582 newdecl);
1583 warned = true;
1586 /* Report location of previous decl/defn in a consistent manner. */
1587 if (warned || pedwarned)
1588 locate_old_decl (olddecl, pedwarned ? pedwarn : warning0);
1590 #undef DECL_EXTERN_INLINE
1592 return retval;
1595 /* Subroutine of duplicate_decls. NEWDECL has been found to be
1596 consistent with OLDDECL, but carries new information. Merge the
1597 new information into OLDDECL. This function issues no
1598 diagnostics. */
1600 static void
1601 merge_decls (tree newdecl, tree olddecl, tree newtype, tree oldtype)
1603 bool new_is_definition = (TREE_CODE (newdecl) == FUNCTION_DECL
1604 && DECL_INITIAL (newdecl) != 0);
1605 bool new_is_prototype = (TREE_CODE (newdecl) == FUNCTION_DECL
1606 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != 0);
1607 bool old_is_prototype = (TREE_CODE (olddecl) == FUNCTION_DECL
1608 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) != 0);
1609 bool extern_changed = false;
1611 /* For real parm decl following a forward decl, rechain the old decl
1612 in its new location and clear TREE_ASM_WRITTEN (it's not a
1613 forward decl anymore). */
1614 if (TREE_CODE (newdecl) == PARM_DECL
1615 && TREE_ASM_WRITTEN (olddecl) && !TREE_ASM_WRITTEN (newdecl))
1617 struct c_binding *b, **here;
1619 for (here = &current_scope->bindings; *here; here = &(*here)->prev)
1620 if ((*here)->decl == olddecl)
1621 goto found;
1622 gcc_unreachable ();
1624 found:
1625 b = *here;
1626 *here = b->prev;
1627 b->prev = current_scope->bindings;
1628 current_scope->bindings = b;
1630 TREE_ASM_WRITTEN (olddecl) = 0;
1633 DECL_ATTRIBUTES (newdecl)
1634 = targetm.merge_decl_attributes (olddecl, newdecl);
1636 /* Merge the data types specified in the two decls. */
1637 TREE_TYPE (newdecl)
1638 = TREE_TYPE (olddecl)
1639 = composite_type (newtype, oldtype);
1641 /* Lay the type out, unless already done. */
1642 if (!comptypes (oldtype, TREE_TYPE (newdecl)))
1644 if (TREE_TYPE (newdecl) != error_mark_node)
1645 layout_type (TREE_TYPE (newdecl));
1646 if (TREE_CODE (newdecl) != FUNCTION_DECL
1647 && TREE_CODE (newdecl) != TYPE_DECL
1648 && TREE_CODE (newdecl) != CONST_DECL)
1649 layout_decl (newdecl, 0);
1651 else
1653 /* Since the type is OLDDECL's, make OLDDECL's size go with. */
1654 DECL_SIZE (newdecl) = DECL_SIZE (olddecl);
1655 DECL_SIZE_UNIT (newdecl) = DECL_SIZE_UNIT (olddecl);
1656 DECL_MODE (newdecl) = DECL_MODE (olddecl);
1657 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
1659 DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
1660 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
1664 /* Keep the old rtl since we can safely use it. */
1665 if (HAS_RTL_P (olddecl))
1666 COPY_DECL_RTL (olddecl, newdecl);
1668 /* Merge the type qualifiers. */
1669 if (TREE_READONLY (newdecl))
1670 TREE_READONLY (olddecl) = 1;
1672 if (TREE_THIS_VOLATILE (newdecl))
1673 TREE_THIS_VOLATILE (olddecl) = 1;
1675 /* Merge deprecatedness. */
1676 if (TREE_DEPRECATED (newdecl))
1677 TREE_DEPRECATED (olddecl) = 1;
1679 /* Keep source location of definition rather than declaration and of
1680 prototype rather than non-prototype unless that prototype is
1681 built-in. */
1682 if ((DECL_INITIAL (newdecl) == 0 && DECL_INITIAL (olddecl) != 0)
1683 || (old_is_prototype && !new_is_prototype
1684 && !C_DECL_BUILTIN_PROTOTYPE (olddecl)))
1685 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1687 /* Merge the initialization information. */
1688 if (DECL_INITIAL (newdecl) == 0)
1689 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1691 /* Merge the threadprivate attribute. */
1692 if (TREE_CODE (olddecl) == VAR_DECL && C_DECL_THREADPRIVATE_P (olddecl))
1694 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1695 C_DECL_THREADPRIVATE_P (newdecl) = 1;
1698 if (CODE_CONTAINS_STRUCT (TREE_CODE (olddecl), TS_DECL_WITH_VIS))
1700 /* Merge the unused-warning information. */
1701 if (DECL_IN_SYSTEM_HEADER (olddecl))
1702 DECL_IN_SYSTEM_HEADER (newdecl) = 1;
1703 else if (DECL_IN_SYSTEM_HEADER (newdecl))
1704 DECL_IN_SYSTEM_HEADER (olddecl) = 1;
1706 /* Merge the section attribute.
1707 We want to issue an error if the sections conflict but that
1708 must be done later in decl_attributes since we are called
1709 before attributes are assigned. */
1710 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1711 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1713 /* Copy the assembler name.
1714 Currently, it can only be defined in the prototype. */
1715 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1717 /* Use visibility of whichever declaration had it specified */
1718 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1720 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1721 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1724 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1726 DECL_STATIC_CONSTRUCTOR(newdecl) |= DECL_STATIC_CONSTRUCTOR(olddecl);
1727 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1728 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1729 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1730 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1731 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1732 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1733 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1734 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1735 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1736 DECL_IS_NOVOPS (newdecl) |= DECL_IS_NOVOPS (olddecl);
1739 /* Merge the storage class information. */
1740 merge_weak (newdecl, olddecl);
1742 /* For functions, static overrides non-static. */
1743 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1745 TREE_PUBLIC (newdecl) &= TREE_PUBLIC (olddecl);
1746 /* This is since we don't automatically
1747 copy the attributes of NEWDECL into OLDDECL. */
1748 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1749 /* If this clears `static', clear it in the identifier too. */
1750 if (!TREE_PUBLIC (olddecl))
1751 TREE_PUBLIC (DECL_NAME (olddecl)) = 0;
1755 /* In c99, 'extern' declaration before (or after) 'inline' means this
1756 function is not DECL_EXTERNAL, unless 'gnu_inline' attribute
1757 is present. */
1758 if (TREE_CODE (newdecl) == FUNCTION_DECL
1759 && !flag_gnu89_inline
1760 && (DECL_DECLARED_INLINE_P (newdecl)
1761 || DECL_DECLARED_INLINE_P (olddecl))
1762 && (!DECL_DECLARED_INLINE_P (newdecl)
1763 || !DECL_DECLARED_INLINE_P (olddecl)
1764 || !DECL_EXTERNAL (olddecl))
1765 && DECL_EXTERNAL (newdecl)
1766 && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (newdecl)))
1767 DECL_EXTERNAL (newdecl) = 0;
1769 if (DECL_EXTERNAL (newdecl))
1771 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1772 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1774 /* An extern decl does not override previous storage class. */
1775 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1776 if (!DECL_EXTERNAL (newdecl))
1778 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1779 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1782 else
1784 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1785 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1788 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1790 /* If we're redefining a function previously defined as extern
1791 inline, make sure we emit debug info for the inline before we
1792 throw it away, in case it was inlined into a function that
1793 hasn't been written out yet. */
1794 if (new_is_definition && DECL_INITIAL (olddecl))
1796 if (TREE_USED (olddecl)
1797 /* In unit-at-a-time mode we never inline re-defined extern
1798 inline functions. */
1799 && !flag_unit_at_a_time
1800 && cgraph_function_possibly_inlined_p (olddecl))
1801 (*debug_hooks->outlining_inline_function) (olddecl);
1803 /* The new defn must not be inline. */
1804 DECL_INLINE (newdecl) = 0;
1805 DECL_UNINLINABLE (newdecl) = 1;
1807 else
1809 /* If either decl says `inline', this fn is inline, unless
1810 its definition was passed already. */
1811 if (DECL_DECLARED_INLINE_P (newdecl)
1812 || DECL_DECLARED_INLINE_P (olddecl))
1813 DECL_DECLARED_INLINE_P (newdecl) = 1;
1815 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1816 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1818 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1819 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1820 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1821 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1824 if (DECL_BUILT_IN (olddecl))
1826 /* If redeclaring a builtin function, it stays built in.
1827 But it gets tagged as having been declared. */
1828 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1829 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1830 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1831 if (new_is_prototype)
1832 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1833 else
1834 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1835 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1838 /* Also preserve various other info from the definition. */
1839 if (!new_is_definition)
1841 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1842 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1843 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1844 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1845 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1847 /* Set DECL_INLINE on the declaration if we've got a body
1848 from which to instantiate. */
1849 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1851 DECL_INLINE (newdecl) = 1;
1852 DECL_ABSTRACT_ORIGIN (newdecl)
1853 = DECL_ABSTRACT_ORIGIN (olddecl);
1856 else
1858 /* If a previous declaration said inline, mark the
1859 definition as inlinable. */
1860 if (DECL_DECLARED_INLINE_P (newdecl)
1861 && !DECL_UNINLINABLE (newdecl))
1862 DECL_INLINE (newdecl) = 1;
1866 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1868 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1869 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1871 unsigned olddecl_uid = DECL_UID (olddecl);
1872 tree olddecl_context = DECL_CONTEXT (olddecl);
1874 memcpy ((char *) olddecl + sizeof (struct tree_common),
1875 (char *) newdecl + sizeof (struct tree_common),
1876 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1877 switch (TREE_CODE (olddecl))
1879 case FIELD_DECL:
1880 case VAR_DECL:
1881 case PARM_DECL:
1882 case LABEL_DECL:
1883 case RESULT_DECL:
1884 case CONST_DECL:
1885 case TYPE_DECL:
1886 case FUNCTION_DECL:
1887 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1888 (char *) newdecl + sizeof (struct tree_decl_common),
1889 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1890 break;
1892 default:
1894 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1895 (char *) newdecl + sizeof (struct tree_decl_common),
1896 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1898 DECL_UID (olddecl) = olddecl_uid;
1899 DECL_CONTEXT (olddecl) = olddecl_context;
1902 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1903 so that encode_section_info has a chance to look at the new decl
1904 flags and attributes. */
1905 if (DECL_RTL_SET_P (olddecl)
1906 && (TREE_CODE (olddecl) == FUNCTION_DECL
1907 || (TREE_CODE (olddecl) == VAR_DECL
1908 && TREE_STATIC (olddecl))))
1909 make_decl_rtl (olddecl);
1911 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1912 and the definition is coming from the old version, cgraph needs
1913 to be called again. */
1914 if (extern_changed && !new_is_definition
1915 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1916 cgraph_finalize_function (olddecl, false);
1919 /* Handle when a new declaration NEWDECL has the same name as an old
1920 one OLDDECL in the same binding contour. Prints an error message
1921 if appropriate.
1923 If safely possible, alter OLDDECL to look like NEWDECL, and return
1924 true. Otherwise, return false. */
1926 static bool
1927 duplicate_decls (tree newdecl, tree olddecl)
1929 tree newtype = NULL, oldtype = NULL;
1931 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1933 /* Avoid `unused variable' and other warnings for OLDDECL. */
1934 TREE_NO_WARNING (olddecl) = 1;
1935 return false;
1938 merge_decls (newdecl, olddecl, newtype, oldtype);
1939 return true;
1943 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1944 static void
1945 warn_if_shadowing (tree new_decl)
1947 struct c_binding *b;
1949 /* Shadow warnings wanted? */
1950 if (!warn_shadow
1951 /* No shadow warnings for internally generated vars. */
1952 || DECL_IS_BUILTIN (new_decl)
1953 /* No shadow warnings for vars made for inlining. */
1954 || DECL_FROM_INLINE (new_decl))
1955 return;
1957 /* Is anything being shadowed? Invisible decls do not count. */
1958 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1959 if (b->decl && b->decl != new_decl && !b->invisible)
1961 tree old_decl = b->decl;
1963 if (old_decl == error_mark_node)
1965 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1966 "non-variable", new_decl);
1967 break;
1969 else if (TREE_CODE (old_decl) == PARM_DECL)
1970 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1971 new_decl);
1972 else if (DECL_FILE_SCOPE_P (old_decl))
1973 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1974 "declaration", new_decl);
1975 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1976 && DECL_BUILT_IN (old_decl))
1978 warning (OPT_Wshadow, "declaration of %q+D shadows "
1979 "a built-in function", new_decl);
1980 break;
1982 else
1983 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1984 new_decl);
1986 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1988 break;
1993 /* Subroutine of pushdecl.
1995 X is a TYPE_DECL for a typedef statement. Create a brand new
1996 ..._TYPE node (which will be just a variant of the existing
1997 ..._TYPE node with identical properties) and then install X
1998 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2000 The whole point here is to end up with a situation where each
2001 and every ..._TYPE node the compiler creates will be uniquely
2002 associated with AT MOST one node representing a typedef name.
2003 This way, even though the compiler substitutes corresponding
2004 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2005 early on, later parts of the compiler can always do the reverse
2006 translation and get back the corresponding typedef name. For
2007 example, given:
2009 typedef struct S MY_TYPE;
2010 MY_TYPE object;
2012 Later parts of the compiler might only know that `object' was of
2013 type `struct S' if it were not for code just below. With this
2014 code however, later parts of the compiler see something like:
2016 struct S' == struct S
2017 typedef struct S' MY_TYPE;
2018 struct S' object;
2020 And they can then deduce (from the node for type struct S') that
2021 the original object declaration was:
2023 MY_TYPE object;
2025 Being able to do this is important for proper support of protoize,
2026 and also for generating precise symbolic debugging information
2027 which takes full account of the programmer's (typedef) vocabulary.
2029 Obviously, we don't want to generate a duplicate ..._TYPE node if
2030 the TYPE_DECL node that we are now processing really represents a
2031 standard built-in type. */
2033 static void
2034 clone_underlying_type (tree x)
2036 if (DECL_IS_BUILTIN (x))
2038 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2039 TYPE_NAME (TREE_TYPE (x)) = x;
2041 else if (TREE_TYPE (x) != error_mark_node
2042 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2044 tree tt = TREE_TYPE (x);
2045 DECL_ORIGINAL_TYPE (x) = tt;
2046 tt = build_variant_type_copy (tt);
2047 TYPE_NAME (tt) = x;
2048 TREE_USED (tt) = TREE_USED (x);
2049 TREE_TYPE (x) = tt;
2053 /* Record a decl-node X as belonging to the current lexical scope.
2054 Check for errors (such as an incompatible declaration for the same
2055 name already seen in the same scope).
2057 Returns either X or an old decl for the same name.
2058 If an old decl is returned, it may have been smashed
2059 to agree with what X says. */
2061 tree
2062 pushdecl (tree x)
2064 tree name = DECL_NAME (x);
2065 struct c_scope *scope = current_scope;
2066 struct c_binding *b;
2067 bool nested = false;
2069 /* Must set DECL_CONTEXT for everything not at file scope or
2070 DECL_FILE_SCOPE_P won't work. Local externs don't count
2071 unless they have initializers (which generate code). */
2072 if (current_function_decl
2073 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2074 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2075 DECL_CONTEXT (x) = current_function_decl;
2077 /* If this is of variably modified type, prevent jumping into its
2078 scope. */
2079 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2080 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2081 c_begin_vm_scope (scope->depth);
2083 /* Anonymous decls are just inserted in the scope. */
2084 if (!name)
2086 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2087 return x;
2090 /* First, see if there is another declaration with the same name in
2091 the current scope. If there is, duplicate_decls may do all the
2092 work for us. If duplicate_decls returns false, that indicates
2093 two incompatible decls in the same scope; we are to silently
2094 replace the old one (duplicate_decls has issued all appropriate
2095 diagnostics). In particular, we should not consider possible
2096 duplicates in the external scope, or shadowing. */
2097 b = I_SYMBOL_BINDING (name);
2098 if (b && B_IN_SCOPE (b, scope))
2100 struct c_binding *b_ext, *b_use;
2101 tree type = TREE_TYPE (x);
2102 tree visdecl = b->decl;
2103 tree vistype = TREE_TYPE (visdecl);
2104 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2105 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2106 b->inner_comp = false;
2107 b_use = b;
2108 b_ext = b;
2109 /* If this is an external linkage declaration, we should check
2110 for compatibility with the type in the external scope before
2111 setting the type at this scope based on the visible
2112 information only. */
2113 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2115 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2116 b_ext = b_ext->shadowed;
2117 if (b_ext)
2119 b_use = b_ext;
2120 if (b_use->type)
2121 TREE_TYPE (b_use->decl) = b_use->type;
2124 if (duplicate_decls (x, b_use->decl))
2126 if (b_use != b)
2128 /* Save the updated type in the external scope and
2129 restore the proper type for this scope. */
2130 tree thistype;
2131 if (comptypes (vistype, type))
2132 thistype = composite_type (vistype, type);
2133 else
2134 thistype = TREE_TYPE (b_use->decl);
2135 b_use->type = TREE_TYPE (b_use->decl);
2136 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2137 && DECL_BUILT_IN (b_use->decl))
2138 thistype
2139 = build_type_attribute_variant (thistype,
2140 TYPE_ATTRIBUTES
2141 (b_use->type));
2142 TREE_TYPE (b_use->decl) = thistype;
2144 return b_use->decl;
2146 else
2147 goto skip_external_and_shadow_checks;
2150 /* All declarations with external linkage, and all external
2151 references, go in the external scope, no matter what scope is
2152 current. However, the binding in that scope is ignored for
2153 purposes of normal name lookup. A separate binding structure is
2154 created in the requested scope; this governs the normal
2155 visibility of the symbol.
2157 The binding in the externals scope is used exclusively for
2158 detecting duplicate declarations of the same object, no matter
2159 what scope they are in; this is what we do here. (C99 6.2.7p2:
2160 All declarations that refer to the same object or function shall
2161 have compatible type; otherwise, the behavior is undefined.) */
2162 if (DECL_EXTERNAL (x) || scope == file_scope)
2164 tree type = TREE_TYPE (x);
2165 tree vistype = 0;
2166 tree visdecl = 0;
2167 bool type_saved = false;
2168 if (b && !B_IN_EXTERNAL_SCOPE (b)
2169 && (TREE_CODE (b->decl) == FUNCTION_DECL
2170 || TREE_CODE (b->decl) == VAR_DECL)
2171 && DECL_FILE_SCOPE_P (b->decl))
2173 visdecl = b->decl;
2174 vistype = TREE_TYPE (visdecl);
2176 if (scope != file_scope
2177 && !DECL_IN_SYSTEM_HEADER (x))
2178 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2180 while (b && !B_IN_EXTERNAL_SCOPE (b))
2182 /* If this decl might be modified, save its type. This is
2183 done here rather than when the decl is first bound
2184 because the type may change after first binding, through
2185 being completed or through attributes being added. If we
2186 encounter multiple such decls, only the first should have
2187 its type saved; the others will already have had their
2188 proper types saved and the types will not have changed as
2189 their scopes will not have been re-entered. */
2190 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2192 b->type = TREE_TYPE (b->decl);
2193 type_saved = true;
2195 if (B_IN_FILE_SCOPE (b)
2196 && TREE_CODE (b->decl) == VAR_DECL
2197 && TREE_STATIC (b->decl)
2198 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2199 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2200 && TREE_CODE (type) == ARRAY_TYPE
2201 && TYPE_DOMAIN (type)
2202 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2203 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2205 /* Array type completed in inner scope, which should be
2206 diagnosed if the completion does not have size 1 and
2207 it does not get completed in the file scope. */
2208 b->inner_comp = true;
2210 b = b->shadowed;
2213 /* If a matching external declaration has been found, set its
2214 type to the composite of all the types of that declaration.
2215 After the consistency checks, it will be reset to the
2216 composite of the visible types only. */
2217 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2218 && b->type)
2219 TREE_TYPE (b->decl) = b->type;
2221 /* The point of the same_translation_unit_p check here is,
2222 we want to detect a duplicate decl for a construct like
2223 foo() { extern bar(); } ... static bar(); but not if
2224 they are in different translation units. In any case,
2225 the static does not go in the externals scope. */
2226 if (b
2227 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2228 && duplicate_decls (x, b->decl))
2230 tree thistype;
2231 if (vistype)
2233 if (comptypes (vistype, type))
2234 thistype = composite_type (vistype, type);
2235 else
2236 thistype = TREE_TYPE (b->decl);
2238 else
2239 thistype = type;
2240 b->type = TREE_TYPE (b->decl);
2241 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2242 thistype
2243 = build_type_attribute_variant (thistype,
2244 TYPE_ATTRIBUTES (b->type));
2245 TREE_TYPE (b->decl) = thistype;
2246 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2247 return b->decl;
2249 else if (TREE_PUBLIC (x))
2251 if (visdecl && !b && duplicate_decls (x, visdecl))
2253 /* An external declaration at block scope referring to a
2254 visible entity with internal linkage. The composite
2255 type will already be correct for this scope, so we
2256 just need to fall through to make the declaration in
2257 this scope. */
2258 nested = true;
2259 x = visdecl;
2261 else
2263 bind (name, x, external_scope, /*invisible=*/true,
2264 /*nested=*/false);
2265 nested = true;
2270 if (TREE_CODE (x) != PARM_DECL)
2271 warn_if_shadowing (x);
2273 skip_external_and_shadow_checks:
2274 if (TREE_CODE (x) == TYPE_DECL)
2275 clone_underlying_type (x);
2277 bind (name, x, scope, /*invisible=*/false, nested);
2279 /* If x's type is incomplete because it's based on a
2280 structure or union which has not yet been fully declared,
2281 attach it to that structure or union type, so we can go
2282 back and complete the variable declaration later, if the
2283 structure or union gets fully declared.
2285 If the input is erroneous, we can have error_mark in the type
2286 slot (e.g. "f(void a, ...)") - that doesn't count as an
2287 incomplete type. */
2288 if (TREE_TYPE (x) != error_mark_node
2289 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2291 tree element = TREE_TYPE (x);
2293 while (TREE_CODE (element) == ARRAY_TYPE)
2294 element = TREE_TYPE (element);
2295 element = TYPE_MAIN_VARIANT (element);
2297 if ((TREE_CODE (element) == RECORD_TYPE
2298 || TREE_CODE (element) == UNION_TYPE)
2299 && (TREE_CODE (x) != TYPE_DECL
2300 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2301 && !COMPLETE_TYPE_P (element))
2302 C_TYPE_INCOMPLETE_VARS (element)
2303 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2305 return x;
2308 /* Record X as belonging to file scope.
2309 This is used only internally by the Objective-C front end,
2310 and is limited to its needs. duplicate_decls is not called;
2311 if there is any preexisting decl for this identifier, it is an ICE. */
2313 tree
2314 pushdecl_top_level (tree x)
2316 tree name;
2317 bool nested = false;
2318 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2320 name = DECL_NAME (x);
2322 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2324 if (TREE_PUBLIC (x))
2326 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2327 nested = true;
2329 if (file_scope)
2330 bind (name, x, file_scope, /*invisible=*/false, nested);
2332 return x;
2335 static void
2336 implicit_decl_warning (tree id, tree olddecl)
2338 if (warn_implicit_function_declaration)
2340 if (flag_isoc99)
2341 pedwarn (G_("implicit declaration of function %qE"), id);
2342 else
2343 warning (OPT_Wimplicit_function_declaration,
2344 G_("implicit declaration of function %qE"), id);
2345 if (olddecl)
2346 locate_old_decl (olddecl, inform);
2350 /* Generate an implicit declaration for identifier FUNCTIONID as a
2351 function of type int (). */
2353 tree
2354 implicitly_declare (tree functionid)
2356 struct c_binding *b;
2357 tree decl = 0;
2358 tree asmspec_tree;
2360 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2362 if (B_IN_SCOPE (b, external_scope))
2364 decl = b->decl;
2365 break;
2369 if (decl)
2371 if (decl == error_mark_node)
2372 return decl;
2374 /* FIXME: Objective-C has weird not-really-builtin functions
2375 which are supposed to be visible automatically. They wind up
2376 in the external scope because they're pushed before the file
2377 scope gets created. Catch this here and rebind them into the
2378 file scope. */
2379 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2381 bind (functionid, decl, file_scope,
2382 /*invisible=*/false, /*nested=*/true);
2383 return decl;
2385 else
2387 tree newtype = default_function_type;
2388 if (b->type)
2389 TREE_TYPE (decl) = b->type;
2390 /* Implicit declaration of a function already declared
2391 (somehow) in a different scope, or as a built-in.
2392 If this is the first time this has happened, warn;
2393 then recycle the old declaration but with the new type. */
2394 if (!C_DECL_IMPLICIT (decl))
2396 implicit_decl_warning (functionid, decl);
2397 C_DECL_IMPLICIT (decl) = 1;
2399 if (DECL_BUILT_IN (decl))
2401 newtype = build_type_attribute_variant (newtype,
2402 TYPE_ATTRIBUTES
2403 (TREE_TYPE (decl)));
2404 if (!comptypes (newtype, TREE_TYPE (decl)))
2406 warning (0, "incompatible implicit declaration of built-in"
2407 " function %qD", decl);
2408 newtype = TREE_TYPE (decl);
2411 else
2413 if (!comptypes (newtype, TREE_TYPE (decl)))
2415 error ("incompatible implicit declaration of function %qD",
2416 decl);
2417 locate_old_decl (decl, error);
2420 b->type = TREE_TYPE (decl);
2421 TREE_TYPE (decl) = newtype;
2422 bind (functionid, decl, current_scope,
2423 /*invisible=*/false, /*nested=*/true);
2424 return decl;
2428 /* Not seen before. */
2429 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2430 DECL_EXTERNAL (decl) = 1;
2431 TREE_PUBLIC (decl) = 1;
2432 C_DECL_IMPLICIT (decl) = 1;
2433 implicit_decl_warning (functionid, 0);
2434 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2435 if (asmspec_tree)
2436 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2438 /* C89 says implicit declarations are in the innermost block.
2439 So we record the decl in the standard fashion. */
2440 decl = pushdecl (decl);
2442 /* No need to call objc_check_decl here - it's a function type. */
2443 rest_of_decl_compilation (decl, 0, 0);
2445 /* Write a record describing this implicit function declaration
2446 to the prototypes file (if requested). */
2447 gen_aux_info_record (decl, 0, 1, 0);
2449 /* Possibly apply some default attributes to this implicit declaration. */
2450 decl_attributes (&decl, NULL_TREE, 0);
2452 return decl;
2455 /* Issue an error message for a reference to an undeclared variable
2456 ID, including a reference to a builtin outside of function-call
2457 context. Establish a binding of the identifier to error_mark_node
2458 in an appropriate scope, which will suppress further errors for the
2459 same identifier. The error message should be given location LOC. */
2460 void
2461 undeclared_variable (tree id, location_t loc)
2463 static bool already = false;
2464 struct c_scope *scope;
2466 if (current_function_decl == 0)
2468 error ("%H%qE undeclared here (not in a function)", &loc, id);
2469 scope = current_scope;
2471 else
2473 error ("%H%qE undeclared (first use in this function)", &loc, id);
2475 if (!already)
2477 error ("%H(Each undeclared identifier is reported only once", &loc);
2478 error ("%Hfor each function it appears in.)", &loc);
2479 already = true;
2482 /* If we are parsing old-style parameter decls, current_function_decl
2483 will be nonnull but current_function_scope will be null. */
2484 scope = current_function_scope ? current_function_scope : current_scope;
2486 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2489 /* Subroutine of lookup_label, declare_label, define_label: construct a
2490 LABEL_DECL with all the proper frills. */
2492 static tree
2493 make_label (tree name, location_t location)
2495 tree label = build_decl (LABEL_DECL, name, void_type_node);
2497 DECL_CONTEXT (label) = current_function_decl;
2498 DECL_MODE (label) = VOIDmode;
2499 DECL_SOURCE_LOCATION (label) = location;
2501 return label;
2504 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2505 Create one if none exists so far for the current function.
2506 This is called when a label is used in a goto expression or
2507 has its address taken. */
2509 tree
2510 lookup_label (tree name)
2512 tree label;
2514 if (current_function_decl == 0)
2516 error ("label %qE referenced outside of any function", name);
2517 return 0;
2520 /* Use a label already defined or ref'd with this name, but not if
2521 it is inherited from a containing function and wasn't declared
2522 using __label__. */
2523 label = I_LABEL_DECL (name);
2524 if (label && (DECL_CONTEXT (label) == current_function_decl
2525 || C_DECLARED_LABEL_FLAG (label)))
2527 /* If the label has only been declared, update its apparent
2528 location to point here, for better diagnostics if it
2529 turns out not to have been defined. */
2530 if (!TREE_USED (label))
2531 DECL_SOURCE_LOCATION (label) = input_location;
2532 return label;
2535 /* No label binding for that identifier; make one. */
2536 label = make_label (name, input_location);
2538 /* Ordinary labels go in the current function scope. */
2539 bind (name, label, current_function_scope,
2540 /*invisible=*/false, /*nested=*/false);
2541 return label;
2544 /* Make a label named NAME in the current function, shadowing silently
2545 any that may be inherited from containing functions or containing
2546 scopes. This is called for __label__ declarations. */
2548 tree
2549 declare_label (tree name)
2551 struct c_binding *b = I_LABEL_BINDING (name);
2552 tree label;
2554 /* Check to make sure that the label hasn't already been declared
2555 at this scope */
2556 if (b && B_IN_CURRENT_SCOPE (b))
2558 error ("duplicate label declaration %qE", name);
2559 locate_old_decl (b->decl, error);
2561 /* Just use the previous declaration. */
2562 return b->decl;
2565 label = make_label (name, input_location);
2566 C_DECLARED_LABEL_FLAG (label) = 1;
2568 /* Declared labels go in the current scope. */
2569 bind (name, label, current_scope,
2570 /*invisible=*/false, /*nested=*/false);
2571 return label;
2574 /* Define a label, specifying the location in the source file.
2575 Return the LABEL_DECL node for the label, if the definition is valid.
2576 Otherwise return 0. */
2578 tree
2579 define_label (location_t location, tree name)
2581 /* Find any preexisting label with this name. It is an error
2582 if that label has already been defined in this function, or
2583 if there is a containing function with a declared label with
2584 the same name. */
2585 tree label = I_LABEL_DECL (name);
2586 struct c_label_list *nlist_se, *nlist_vm;
2588 if (label
2589 && ((DECL_CONTEXT (label) == current_function_decl
2590 && DECL_INITIAL (label) != 0)
2591 || (DECL_CONTEXT (label) != current_function_decl
2592 && C_DECLARED_LABEL_FLAG (label))))
2594 error ("%Hduplicate label %qD", &location, label);
2595 locate_old_decl (label, error);
2596 return 0;
2598 else if (label && DECL_CONTEXT (label) == current_function_decl)
2600 /* The label has been used or declared already in this function,
2601 but not defined. Update its location to point to this
2602 definition. */
2603 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2604 error ("%Jjump into statement expression", label);
2605 if (C_DECL_UNDEFINABLE_VM (label))
2606 error ("%Jjump into scope of identifier with variably modified type",
2607 label);
2608 DECL_SOURCE_LOCATION (label) = location;
2610 else
2612 /* No label binding for that identifier; make one. */
2613 label = make_label (name, location);
2615 /* Ordinary labels go in the current function scope. */
2616 bind (name, label, current_function_scope,
2617 /*invisible=*/false, /*nested=*/false);
2620 if (!in_system_header && lookup_name (name))
2621 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2622 "for labels, identifier %qE conflicts", &location, name);
2624 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2625 nlist_se->next = label_context_stack_se->labels_def;
2626 nlist_se->label = label;
2627 label_context_stack_se->labels_def = nlist_se;
2629 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2630 nlist_vm->next = label_context_stack_vm->labels_def;
2631 nlist_vm->label = label;
2632 label_context_stack_vm->labels_def = nlist_vm;
2634 /* Mark label as having been defined. */
2635 DECL_INITIAL (label) = error_mark_node;
2636 return label;
2639 /* Given NAME, an IDENTIFIER_NODE,
2640 return the structure (or union or enum) definition for that name.
2641 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2642 CODE says which kind of type the caller wants;
2643 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2644 If the wrong kind of type is found, an error is reported. */
2646 static tree
2647 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2649 struct c_binding *b = I_TAG_BINDING (name);
2650 int thislevel = 0;
2652 if (!b || !b->decl)
2653 return 0;
2655 /* We only care about whether it's in this level if
2656 thislevel_only was set or it might be a type clash. */
2657 if (thislevel_only || TREE_CODE (b->decl) != code)
2659 /* For our purposes, a tag in the external scope is the same as
2660 a tag in the file scope. (Primarily relevant to Objective-C
2661 and its builtin structure tags, which get pushed before the
2662 file scope is created.) */
2663 if (B_IN_CURRENT_SCOPE (b)
2664 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2665 thislevel = 1;
2668 if (thislevel_only && !thislevel)
2669 return 0;
2671 if (TREE_CODE (b->decl) != code)
2673 /* Definition isn't the kind we were looking for. */
2674 pending_invalid_xref = name;
2675 pending_invalid_xref_location = input_location;
2677 /* If in the same binding level as a declaration as a tag
2678 of a different type, this must not be allowed to
2679 shadow that tag, so give the error immediately.
2680 (For example, "struct foo; union foo;" is invalid.) */
2681 if (thislevel)
2682 pending_xref_error ();
2684 return b->decl;
2687 /* Print an error message now
2688 for a recent invalid struct, union or enum cross reference.
2689 We don't print them immediately because they are not invalid
2690 when used in the `struct foo;' construct for shadowing. */
2692 void
2693 pending_xref_error (void)
2695 if (pending_invalid_xref != 0)
2696 error ("%H%qE defined as wrong kind of tag",
2697 &pending_invalid_xref_location, pending_invalid_xref);
2698 pending_invalid_xref = 0;
2702 /* Look up NAME in the current scope and its superiors
2703 in the namespace of variables, functions and typedefs.
2704 Return a ..._DECL node of some kind representing its definition,
2705 or return 0 if it is undefined. */
2707 tree
2708 lookup_name (tree name)
2710 struct c_binding *b = I_SYMBOL_BINDING (name);
2711 if (b && !b->invisible)
2712 return b->decl;
2713 return 0;
2716 /* Similar to `lookup_name' but look only at the indicated scope. */
2718 static tree
2719 lookup_name_in_scope (tree name, struct c_scope *scope)
2721 struct c_binding *b;
2723 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2724 if (B_IN_SCOPE (b, scope))
2725 return b->decl;
2726 return 0;
2729 /* Create the predefined scalar types of C,
2730 and some nodes representing standard constants (0, 1, (void *) 0).
2731 Initialize the global scope.
2732 Make definitions for built-in primitive functions. */
2734 void
2735 c_init_decl_processing (void)
2737 location_t save_loc = input_location;
2739 /* Initialize reserved words for parser. */
2740 c_parse_init ();
2742 current_function_decl = 0;
2744 gcc_obstack_init (&parser_obstack);
2746 /* Make the externals scope. */
2747 push_scope ();
2748 external_scope = current_scope;
2750 /* Declarations from c_common_nodes_and_builtins must not be associated
2751 with this input file, lest we get differences between using and not
2752 using preprocessed headers. */
2753 input_location = BUILTINS_LOCATION;
2755 build_common_tree_nodes (flag_signed_char, false);
2757 c_common_nodes_and_builtins ();
2759 /* In C, comparisons and TRUTH_* expressions have type int. */
2760 truthvalue_type_node = integer_type_node;
2761 truthvalue_true_node = integer_one_node;
2762 truthvalue_false_node = integer_zero_node;
2764 /* Even in C99, which has a real boolean type. */
2765 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2766 boolean_type_node));
2768 input_location = save_loc;
2770 pedantic_lvalues = true;
2772 make_fname_decl = c_make_fname_decl;
2773 start_fname_decls ();
2776 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2777 decl, NAME is the initialization string and TYPE_DEP indicates whether
2778 NAME depended on the type of the function. As we don't yet implement
2779 delayed emission of static data, we mark the decl as emitted
2780 so it is not placed in the output. Anything using it must therefore pull
2781 out the STRING_CST initializer directly. FIXME. */
2783 static tree
2784 c_make_fname_decl (tree id, int type_dep)
2786 const char *name = fname_as_string (type_dep);
2787 tree decl, type, init;
2788 size_t length = strlen (name);
2790 type = build_array_type (char_type_node,
2791 build_index_type (size_int (length)));
2792 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2794 decl = build_decl (VAR_DECL, id, type);
2796 TREE_STATIC (decl) = 1;
2797 TREE_READONLY (decl) = 1;
2798 DECL_ARTIFICIAL (decl) = 1;
2800 init = build_string (length + 1, name);
2801 free (CONST_CAST (char *, name));
2802 TREE_TYPE (init) = type;
2803 DECL_INITIAL (decl) = init;
2805 TREE_USED (decl) = 1;
2807 if (current_function_decl
2808 /* For invalid programs like this:
2810 void foo()
2811 const char* p = __FUNCTION__;
2813 the __FUNCTION__ is believed to appear in K&R style function
2814 parameter declarator. In that case we still don't have
2815 function_scope. */
2816 && (!errorcount || current_function_scope))
2818 DECL_CONTEXT (decl) = current_function_decl;
2819 bind (id, decl, current_function_scope,
2820 /*invisible=*/false, /*nested=*/false);
2823 finish_decl (decl, init, NULL_TREE);
2825 return decl;
2828 tree
2829 c_builtin_function (tree decl)
2831 tree type = TREE_TYPE (decl);
2832 tree id = DECL_NAME (decl);
2834 const char *name = IDENTIFIER_POINTER (id);
2835 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2837 /* Should never be called on a symbol with a preexisting meaning. */
2838 gcc_assert (!I_SYMBOL_BINDING (id));
2840 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2842 /* Builtins in the implementation namespace are made visible without
2843 needing to be explicitly declared. See push_file_scope. */
2844 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2846 TREE_CHAIN (decl) = visible_builtins;
2847 visible_builtins = decl;
2850 return decl;
2853 /* Called when a declaration is seen that contains no names to declare.
2854 If its type is a reference to a structure, union or enum inherited
2855 from a containing scope, shadow that tag name for the current scope
2856 with a forward reference.
2857 If its type defines a new named structure or union
2858 or defines an enum, it is valid but we need not do anything here.
2859 Otherwise, it is an error. */
2861 void
2862 shadow_tag (const struct c_declspecs *declspecs)
2864 shadow_tag_warned (declspecs, 0);
2867 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2868 but no pedwarn. */
2869 void
2870 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2872 bool found_tag = false;
2874 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2876 tree value = declspecs->type;
2877 enum tree_code code = TREE_CODE (value);
2879 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2880 /* Used to test also that TYPE_SIZE (value) != 0.
2881 That caused warning for `struct foo;' at top level in the file. */
2883 tree name = TYPE_NAME (value);
2884 tree t;
2886 found_tag = true;
2888 if (name == 0)
2890 if (warned != 1 && code != ENUMERAL_TYPE)
2891 /* Empty unnamed enum OK */
2893 pedwarn ("unnamed struct/union that defines no instances");
2894 warned = 1;
2897 else if (!declspecs->tag_defined_p
2898 && declspecs->storage_class != csc_none)
2900 if (warned != 1)
2901 pedwarn ("empty declaration with storage class specifier "
2902 "does not redeclare tag");
2903 warned = 1;
2904 pending_xref_error ();
2906 else if (!declspecs->tag_defined_p
2907 && (declspecs->const_p
2908 || declspecs->volatile_p
2909 || declspecs->restrict_p))
2911 if (warned != 1)
2912 pedwarn ("empty declaration with type qualifier "
2913 "does not redeclare tag");
2914 warned = 1;
2915 pending_xref_error ();
2917 else
2919 pending_invalid_xref = 0;
2920 t = lookup_tag (code, name, 1);
2922 if (t == 0)
2924 t = make_node (code);
2925 pushtag (name, t);
2929 else
2931 if (warned != 1 && !in_system_header)
2933 pedwarn ("useless type name in empty declaration");
2934 warned = 1;
2938 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2940 pedwarn ("useless type name in empty declaration");
2941 warned = 1;
2944 pending_invalid_xref = 0;
2946 if (declspecs->inline_p)
2948 error ("%<inline%> in empty declaration");
2949 warned = 1;
2952 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2954 error ("%<auto%> in file-scope empty declaration");
2955 warned = 1;
2958 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2960 error ("%<register%> in file-scope empty declaration");
2961 warned = 1;
2964 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2966 warning (0, "useless storage class specifier in empty declaration");
2967 warned = 2;
2970 if (!warned && !in_system_header && declspecs->thread_p)
2972 warning (0, "useless %<__thread%> in empty declaration");
2973 warned = 2;
2976 if (!warned && !in_system_header && (declspecs->const_p
2977 || declspecs->volatile_p
2978 || declspecs->restrict_p))
2980 warning (0, "useless type qualifier in empty declaration");
2981 warned = 2;
2984 if (warned != 1)
2986 if (!found_tag)
2987 pedwarn ("empty declaration");
2992 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2993 bits. SPECS represents declaration specifiers that the grammar
2994 only permits to contain type qualifiers and attributes. */
2997 quals_from_declspecs (const struct c_declspecs *specs)
2999 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3000 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3001 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3002 gcc_assert (!specs->type
3003 && !specs->decl_attr
3004 && specs->typespec_word == cts_none
3005 && specs->storage_class == csc_none
3006 && !specs->typedef_p
3007 && !specs->explicit_signed_p
3008 && !specs->deprecated_p
3009 && !specs->long_p
3010 && !specs->long_long_p
3011 && !specs->short_p
3012 && !specs->signed_p
3013 && !specs->unsigned_p
3014 && !specs->complex_p
3015 && !specs->inline_p
3016 && !specs->thread_p);
3017 return quals;
3020 /* Construct an array declarator. EXPR is the expression inside [],
3021 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3022 applied to the pointer to which a parameter array is converted).
3023 STATIC_P is true if "static" is inside the [], false otherwise.
3024 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3025 length which is nevertheless a complete type, false otherwise. The
3026 field for the contained declarator is left to be filled in by
3027 set_array_declarator_inner. */
3029 struct c_declarator *
3030 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3031 bool vla_unspec_p)
3033 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3034 struct c_declarator);
3035 declarator->kind = cdk_array;
3036 declarator->declarator = 0;
3037 declarator->u.array.dimen = expr;
3038 if (quals)
3040 declarator->u.array.attrs = quals->attrs;
3041 declarator->u.array.quals = quals_from_declspecs (quals);
3043 else
3045 declarator->u.array.attrs = NULL_TREE;
3046 declarator->u.array.quals = 0;
3048 declarator->u.array.static_p = static_p;
3049 declarator->u.array.vla_unspec_p = vla_unspec_p;
3050 if (pedantic && !flag_isoc99)
3052 if (static_p || quals != NULL)
3053 pedwarn ("ISO C90 does not support %<static%> or type "
3054 "qualifiers in parameter array declarators");
3055 if (vla_unspec_p)
3056 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3058 if (vla_unspec_p)
3060 if (!current_scope->parm_flag)
3062 /* C99 6.7.5.2p4 */
3063 error ("%<[*]%> not allowed in other than function prototype scope");
3064 declarator->u.array.vla_unspec_p = false;
3065 return NULL;
3067 current_scope->had_vla_unspec = true;
3069 return declarator;
3072 /* Set the contained declarator of an array declarator. DECL is the
3073 declarator, as constructed by build_array_declarator; INNER is what
3074 appears on the left of the []. */
3076 struct c_declarator *
3077 set_array_declarator_inner (struct c_declarator *decl,
3078 struct c_declarator *inner)
3080 decl->declarator = inner;
3081 return decl;
3084 /* INIT is a constructor that forms DECL's initializer. If the final
3085 element initializes a flexible array field, add the size of that
3086 initializer to DECL's size. */
3088 static void
3089 add_flexible_array_elts_to_size (tree decl, tree init)
3091 tree elt, type;
3093 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3094 return;
3096 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3097 type = TREE_TYPE (elt);
3098 if (TREE_CODE (type) == ARRAY_TYPE
3099 && TYPE_SIZE (type) == NULL_TREE
3100 && TYPE_DOMAIN (type) != NULL_TREE
3101 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3103 complete_array_type (&type, elt, false);
3104 DECL_SIZE (decl)
3105 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3106 DECL_SIZE_UNIT (decl)
3107 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3111 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3113 tree
3114 groktypename (struct c_type_name *type_name)
3116 tree type;
3117 tree attrs = type_name->specs->attrs;
3119 type_name->specs->attrs = NULL_TREE;
3121 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3122 false, NULL, &attrs, DEPRECATED_NORMAL);
3124 /* Apply attributes. */
3125 decl_attributes (&type, attrs, 0);
3127 return type;
3130 /* Decode a declarator in an ordinary declaration or data definition.
3131 This is called as soon as the type information and variable name
3132 have been parsed, before parsing the initializer if any.
3133 Here we create the ..._DECL node, fill in its type,
3134 and put it on the list of decls for the current context.
3135 The ..._DECL node is returned as the value.
3137 Exception: for arrays where the length is not specified,
3138 the type is left null, to be filled in by `finish_decl'.
3140 Function definitions do not come here; they go to start_function
3141 instead. However, external and forward declarations of functions
3142 do go through here. Structure field declarations are done by
3143 grokfield and not through here. */
3145 tree
3146 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3147 bool initialized, tree attributes)
3149 tree decl;
3150 tree tem;
3151 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3153 /* An object declared as __attribute__((deprecated)) suppresses
3154 warnings of uses of other deprecated items. */
3155 if (lookup_attribute ("deprecated", attributes))
3156 deprecated_state = DEPRECATED_SUPPRESS;
3158 decl = grokdeclarator (declarator, declspecs,
3159 NORMAL, initialized, NULL, &attributes,
3160 deprecated_state);
3161 if (!decl)
3162 return 0;
3164 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3165 && MAIN_NAME_P (DECL_NAME (decl)))
3166 warning (OPT_Wmain, "%q+D is usually a function", decl);
3168 if (initialized)
3169 /* Is it valid for this decl to have an initializer at all?
3170 If not, set INITIALIZED to zero, which will indirectly
3171 tell 'finish_decl' to ignore the initializer once it is parsed. */
3172 switch (TREE_CODE (decl))
3174 case TYPE_DECL:
3175 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3176 initialized = 0;
3177 break;
3179 case FUNCTION_DECL:
3180 error ("function %qD is initialized like a variable", decl);
3181 initialized = 0;
3182 break;
3184 case PARM_DECL:
3185 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3186 error ("parameter %qD is initialized", decl);
3187 initialized = 0;
3188 break;
3190 default:
3191 /* Don't allow initializations for incomplete types except for
3192 arrays which might be completed by the initialization. */
3194 /* This can happen if the array size is an undefined macro.
3195 We already gave a warning, so we don't need another one. */
3196 if (TREE_TYPE (decl) == error_mark_node)
3197 initialized = 0;
3198 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3200 /* A complete type is ok if size is fixed. */
3202 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3203 || C_DECL_VARIABLE_SIZE (decl))
3205 error ("variable-sized object may not be initialized");
3206 initialized = 0;
3209 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3211 error ("variable %qD has initializer but incomplete type", decl);
3212 initialized = 0;
3214 else if (C_DECL_VARIABLE_SIZE (decl))
3216 /* Although C99 is unclear about whether incomplete arrays
3217 of VLAs themselves count as VLAs, it does not make
3218 sense to permit them to be initialized given that
3219 ordinary VLAs may not be initialized. */
3220 error ("variable-sized object may not be initialized");
3221 initialized = 0;
3225 if (initialized)
3227 if (current_scope == file_scope)
3228 TREE_STATIC (decl) = 1;
3230 /* Tell 'pushdecl' this is an initialized decl
3231 even though we don't yet have the initializer expression.
3232 Also tell 'finish_decl' it may store the real initializer. */
3233 DECL_INITIAL (decl) = error_mark_node;
3236 /* If this is a function declaration, write a record describing it to the
3237 prototypes file (if requested). */
3239 if (TREE_CODE (decl) == FUNCTION_DECL)
3240 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3242 /* ANSI specifies that a tentative definition which is not merged with
3243 a non-tentative definition behaves exactly like a definition with an
3244 initializer equal to zero. (Section 3.7.2)
3246 -fno-common gives strict ANSI behavior, though this tends to break
3247 a large body of code that grew up without this rule.
3249 Thread-local variables are never common, since there's no entrenched
3250 body of code to break, and it allows more efficient variable references
3251 in the presence of dynamic linking. */
3253 if (TREE_CODE (decl) == VAR_DECL
3254 && !initialized
3255 && TREE_PUBLIC (decl)
3256 && !DECL_THREAD_LOCAL_P (decl)
3257 && !flag_no_common)
3258 DECL_COMMON (decl) = 1;
3260 /* Set attributes here so if duplicate decl, will have proper attributes. */
3261 decl_attributes (&decl, attributes, 0);
3263 /* Handle gnu_inline attribute. */
3264 if (declspecs->inline_p
3265 && !flag_gnu89_inline
3266 && TREE_CODE (decl) == FUNCTION_DECL
3267 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3269 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3271 else if (declspecs->storage_class != csc_static)
3272 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3275 if (TREE_CODE (decl) == FUNCTION_DECL
3276 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3278 struct c_declarator *ce = declarator;
3280 if (ce->kind == cdk_pointer)
3281 ce = declarator->declarator;
3282 if (ce->kind == cdk_function)
3284 tree args = ce->u.arg_info->parms;
3285 for (; args; args = TREE_CHAIN (args))
3287 tree type = TREE_TYPE (args);
3288 if (type && INTEGRAL_TYPE_P (type)
3289 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3290 DECL_ARG_TYPE (args) = integer_type_node;
3295 if (TREE_CODE (decl) == FUNCTION_DECL
3296 && DECL_DECLARED_INLINE_P (decl)
3297 && DECL_UNINLINABLE (decl)
3298 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3299 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3300 decl);
3302 /* C99 6.7.4p3: An inline definition of a function with external
3303 linkage shall not contain a definition of a modifiable object
3304 with static storage duration... */
3305 if (TREE_CODE (decl) == VAR_DECL
3306 && current_scope != file_scope
3307 && TREE_STATIC (decl)
3308 && !TREE_READONLY (decl)
3309 && DECL_DECLARED_INLINE_P (current_function_decl)
3310 && DECL_EXTERNAL (current_function_decl))
3311 pedwarn ("%q+D is static but declared in inline function %qD "
3312 "which is not static", decl, current_function_decl);
3314 /* Add this decl to the current scope.
3315 TEM may equal DECL or it may be a previous decl of the same name. */
3316 tem = pushdecl (decl);
3318 if (initialized && DECL_EXTERNAL (tem))
3320 DECL_EXTERNAL (tem) = 0;
3321 TREE_STATIC (tem) = 1;
3324 return tem;
3327 /* Initialize EH if not initialized yet and exceptions are enabled. */
3329 void
3330 c_maybe_initialize_eh (void)
3332 if (!flag_exceptions || c_eh_initialized_p)
3333 return;
3335 c_eh_initialized_p = true;
3336 eh_personality_libfunc
3337 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3338 ? "__gcc_personality_sj0"
3339 : "__gcc_personality_v0");
3340 default_init_unwind_resume_libfunc ();
3341 using_eh_for_cleanups ();
3344 /* Finish processing of a declaration;
3345 install its initial value.
3346 If the length of an array type is not known before,
3347 it must be determined now, from the initial value, or it is an error. */
3349 void
3350 finish_decl (tree decl, tree init, tree asmspec_tree)
3352 tree type;
3353 int was_incomplete = (DECL_SIZE (decl) == 0);
3354 const char *asmspec = 0;
3356 /* If a name was specified, get the string. */
3357 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3358 && DECL_FILE_SCOPE_P (decl))
3359 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3360 if (asmspec_tree)
3361 asmspec = TREE_STRING_POINTER (asmspec_tree);
3363 /* If `start_decl' didn't like having an initialization, ignore it now. */
3364 if (init != 0 && DECL_INITIAL (decl) == 0)
3365 init = 0;
3367 /* Don't crash if parm is initialized. */
3368 if (TREE_CODE (decl) == PARM_DECL)
3369 init = 0;
3371 if (init)
3372 store_init_value (decl, init);
3374 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3375 || TREE_CODE (decl) == FUNCTION_DECL
3376 || TREE_CODE (decl) == FIELD_DECL))
3377 objc_check_decl (decl);
3379 type = TREE_TYPE (decl);
3381 /* Deduce size of array from initialization, if not already known. */
3382 if (TREE_CODE (type) == ARRAY_TYPE
3383 && TYPE_DOMAIN (type) == 0
3384 && TREE_CODE (decl) != TYPE_DECL)
3386 bool do_default
3387 = (TREE_STATIC (decl)
3388 /* Even if pedantic, an external linkage array
3389 may have incomplete type at first. */
3390 ? pedantic && !TREE_PUBLIC (decl)
3391 : !DECL_EXTERNAL (decl));
3392 int failure
3393 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3394 do_default);
3396 /* Get the completed type made by complete_array_type. */
3397 type = TREE_TYPE (decl);
3399 switch (failure)
3401 case 1:
3402 error ("initializer fails to determine size of %q+D", decl);
3403 break;
3405 case 2:
3406 if (do_default)
3407 error ("array size missing in %q+D", decl);
3408 /* If a `static' var's size isn't known,
3409 make it extern as well as static, so it does not get
3410 allocated.
3411 If it is not `static', then do not mark extern;
3412 finish_incomplete_decl will give it a default size
3413 and it will get allocated. */
3414 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3415 DECL_EXTERNAL (decl) = 1;
3416 break;
3418 case 3:
3419 error ("zero or negative size array %q+D", decl);
3420 break;
3422 case 0:
3423 /* For global variables, update the copy of the type that
3424 exists in the binding. */
3425 if (TREE_PUBLIC (decl))
3427 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3428 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3429 b_ext = b_ext->shadowed;
3430 if (b_ext)
3432 if (b_ext->type)
3433 b_ext->type = composite_type (b_ext->type, type);
3434 else
3435 b_ext->type = type;
3438 break;
3440 default:
3441 gcc_unreachable ();
3444 if (DECL_INITIAL (decl))
3445 TREE_TYPE (DECL_INITIAL (decl)) = type;
3447 layout_decl (decl, 0);
3450 if (TREE_CODE (decl) == VAR_DECL)
3452 if (init && TREE_CODE (init) == CONSTRUCTOR)
3453 add_flexible_array_elts_to_size (decl, init);
3455 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3456 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3457 layout_decl (decl, 0);
3459 if (DECL_SIZE (decl) == 0
3460 /* Don't give an error if we already gave one earlier. */
3461 && TREE_TYPE (decl) != error_mark_node
3462 && (TREE_STATIC (decl)
3463 /* A static variable with an incomplete type
3464 is an error if it is initialized.
3465 Also if it is not file scope.
3466 Otherwise, let it through, but if it is not `extern'
3467 then it may cause an error message later. */
3468 ? (DECL_INITIAL (decl) != 0
3469 || !DECL_FILE_SCOPE_P (decl))
3470 /* An automatic variable with an incomplete type
3471 is an error. */
3472 : !DECL_EXTERNAL (decl)))
3474 error ("storage size of %q+D isn%'t known", decl);
3475 TREE_TYPE (decl) = error_mark_node;
3478 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3479 && DECL_SIZE (decl) != 0)
3481 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3482 constant_expression_warning (DECL_SIZE (decl));
3483 else
3484 error ("storage size of %q+D isn%'t constant", decl);
3487 if (TREE_USED (type))
3488 TREE_USED (decl) = 1;
3491 /* If this is a function and an assembler name is specified, reset DECL_RTL
3492 so we can give it its new name. Also, update built_in_decls if it
3493 was a normal built-in. */
3494 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3496 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3497 set_builtin_user_assembler_name (decl, asmspec);
3498 set_user_assembler_name (decl, asmspec);
3501 /* If #pragma weak was used, mark the decl weak now. */
3502 maybe_apply_pragma_weak (decl);
3504 /* Output the assembler code and/or RTL code for variables and functions,
3505 unless the type is an undefined structure or union.
3506 If not, it will get done when the type is completed. */
3508 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3510 /* Determine the ELF visibility. */
3511 if (TREE_PUBLIC (decl))
3512 c_determine_visibility (decl);
3514 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3515 if (c_dialect_objc ())
3516 objc_check_decl (decl);
3518 if (asmspec)
3520 /* If this is not a static variable, issue a warning.
3521 It doesn't make any sense to give an ASMSPEC for an
3522 ordinary, non-register local variable. Historically,
3523 GCC has accepted -- but ignored -- the ASMSPEC in
3524 this case. */
3525 if (!DECL_FILE_SCOPE_P (decl)
3526 && TREE_CODE (decl) == VAR_DECL
3527 && !C_DECL_REGISTER (decl)
3528 && !TREE_STATIC (decl))
3529 warning (0, "ignoring asm-specifier for non-static local "
3530 "variable %q+D", decl);
3531 else
3532 set_user_assembler_name (decl, asmspec);
3535 if (DECL_FILE_SCOPE_P (decl))
3537 if (DECL_INITIAL (decl) == NULL_TREE
3538 || DECL_INITIAL (decl) == error_mark_node)
3539 /* Don't output anything
3540 when a tentative file-scope definition is seen.
3541 But at end of compilation, do output code for them. */
3542 DECL_DEFER_OUTPUT (decl) = 1;
3543 rest_of_decl_compilation (decl, true, 0);
3545 else
3547 /* In conjunction with an ASMSPEC, the `register'
3548 keyword indicates that we should place the variable
3549 in a particular register. */
3550 if (asmspec && C_DECL_REGISTER (decl))
3552 DECL_HARD_REGISTER (decl) = 1;
3553 /* This cannot be done for a structure with volatile
3554 fields, on which DECL_REGISTER will have been
3555 reset. */
3556 if (!DECL_REGISTER (decl))
3557 error ("cannot put object with volatile field into register");
3560 if (TREE_CODE (decl) != FUNCTION_DECL)
3562 /* If we're building a variable sized type, and we might be
3563 reachable other than via the top of the current binding
3564 level, then create a new BIND_EXPR so that we deallocate
3565 the object at the right time. */
3566 /* Note that DECL_SIZE can be null due to errors. */
3567 if (DECL_SIZE (decl)
3568 && !TREE_CONSTANT (DECL_SIZE (decl))
3569 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3571 tree bind;
3572 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3573 TREE_SIDE_EFFECTS (bind) = 1;
3574 add_stmt (bind);
3575 BIND_EXPR_BODY (bind) = push_stmt_list ();
3577 add_stmt (build_stmt (DECL_EXPR, decl));
3582 if (!DECL_FILE_SCOPE_P (decl))
3584 /* Recompute the RTL of a local array now
3585 if it used to be an incomplete type. */
3586 if (was_incomplete
3587 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3589 /* If we used it already as memory, it must stay in memory. */
3590 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3591 /* If it's still incomplete now, no init will save it. */
3592 if (DECL_SIZE (decl) == 0)
3593 DECL_INITIAL (decl) = 0;
3598 /* If this was marked 'used', be sure it will be output. */
3599 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3600 mark_decl_referenced (decl);
3602 if (TREE_CODE (decl) == TYPE_DECL)
3604 if (!DECL_FILE_SCOPE_P (decl)
3605 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3606 add_stmt (build_stmt (DECL_EXPR, decl));
3608 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3611 /* At the end of a declaration, throw away any variable type sizes
3612 of types defined inside that declaration. There is no use
3613 computing them in the following function definition. */
3614 if (current_scope == file_scope)
3615 get_pending_sizes ();
3617 /* Install a cleanup (aka destructor) if one was given. */
3618 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3620 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3621 if (attr)
3623 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3624 tree cleanup_decl = lookup_name (cleanup_id);
3625 tree cleanup;
3627 /* Build "cleanup(&decl)" for the destructor. */
3628 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3629 cleanup = build_tree_list (NULL_TREE, cleanup);
3630 cleanup = build_function_call (cleanup_decl, cleanup);
3632 /* Don't warn about decl unused; the cleanup uses it. */
3633 TREE_USED (decl) = 1;
3634 TREE_USED (cleanup_decl) = 1;
3636 /* Initialize EH, if we've been told to do so. */
3637 c_maybe_initialize_eh ();
3639 push_cleanup (decl, cleanup, false);
3644 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3646 tree
3647 grokparm (const struct c_parm *parm)
3649 tree attrs = parm->attrs;
3650 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3651 NULL, &attrs, DEPRECATED_NORMAL);
3653 decl_attributes (&decl, attrs, 0);
3655 return decl;
3658 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3659 and push that on the current scope. */
3661 void
3662 push_parm_decl (const struct c_parm *parm)
3664 tree attrs = parm->attrs;
3665 tree decl;
3667 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3668 &attrs, DEPRECATED_NORMAL);
3669 decl_attributes (&decl, attrs, 0);
3671 decl = pushdecl (decl);
3673 finish_decl (decl, NULL_TREE, NULL_TREE);
3676 /* Mark all the parameter declarations to date as forward decls.
3677 Also diagnose use of this extension. */
3679 void
3680 mark_forward_parm_decls (void)
3682 struct c_binding *b;
3684 if (pedantic && !current_scope->warned_forward_parm_decls)
3686 pedwarn ("ISO C forbids forward parameter declarations");
3687 current_scope->warned_forward_parm_decls = true;
3690 for (b = current_scope->bindings; b; b = b->prev)
3691 if (TREE_CODE (b->decl) == PARM_DECL)
3692 TREE_ASM_WRITTEN (b->decl) = 1;
3695 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3696 literal, which may be an incomplete array type completed by the
3697 initializer; INIT is a CONSTRUCTOR that initializes the compound
3698 literal. */
3700 tree
3701 build_compound_literal (tree type, tree init)
3703 /* We do not use start_decl here because we have a type, not a declarator;
3704 and do not use finish_decl because the decl should be stored inside
3705 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3706 tree decl;
3707 tree complit;
3708 tree stmt;
3710 if (type == error_mark_node)
3711 return error_mark_node;
3713 decl = build_decl (VAR_DECL, NULL_TREE, type);
3714 DECL_EXTERNAL (decl) = 0;
3715 TREE_PUBLIC (decl) = 0;
3716 TREE_STATIC (decl) = (current_scope == file_scope);
3717 DECL_CONTEXT (decl) = current_function_decl;
3718 TREE_USED (decl) = 1;
3719 TREE_TYPE (decl) = type;
3720 TREE_READONLY (decl) = TYPE_READONLY (type);
3721 store_init_value (decl, init);
3723 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3725 int failure = complete_array_type (&TREE_TYPE (decl),
3726 DECL_INITIAL (decl), true);
3727 gcc_assert (!failure);
3729 type = TREE_TYPE (decl);
3730 TREE_TYPE (DECL_INITIAL (decl)) = type;
3733 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3734 return error_mark_node;
3736 stmt = build_stmt (DECL_EXPR, decl);
3737 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3738 TREE_SIDE_EFFECTS (complit) = 1;
3740 layout_decl (decl, 0);
3742 if (TREE_STATIC (decl))
3744 /* This decl needs a name for the assembler output. */
3745 set_compound_literal_name (decl);
3746 DECL_DEFER_OUTPUT (decl) = 1;
3747 DECL_COMDAT (decl) = 1;
3748 DECL_ARTIFICIAL (decl) = 1;
3749 DECL_IGNORED_P (decl) = 1;
3750 pushdecl (decl);
3751 rest_of_decl_compilation (decl, 1, 0);
3754 return complit;
3757 /* Determine whether TYPE is a structure with a flexible array member,
3758 or a union containing such a structure (possibly recursively). */
3760 static bool
3761 flexible_array_type_p (tree type)
3763 tree x;
3764 switch (TREE_CODE (type))
3766 case RECORD_TYPE:
3767 x = TYPE_FIELDS (type);
3768 if (x == NULL_TREE)
3769 return false;
3770 while (TREE_CHAIN (x) != NULL_TREE)
3771 x = TREE_CHAIN (x);
3772 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3773 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3774 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3775 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3776 return true;
3777 return false;
3778 case UNION_TYPE:
3779 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3781 if (flexible_array_type_p (TREE_TYPE (x)))
3782 return true;
3784 return false;
3785 default:
3786 return false;
3790 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3791 replacing with appropriate values if they are invalid. */
3792 static void
3793 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3795 tree type_mv;
3796 unsigned int max_width;
3797 unsigned HOST_WIDE_INT w;
3798 const char *name = orig_name ? orig_name: _("<anonymous>");
3800 /* Detect and ignore out of range field width and process valid
3801 field widths. */
3802 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3803 || TREE_CODE (*width) != INTEGER_CST)
3805 error ("bit-field %qs width not an integer constant", name);
3806 *width = integer_one_node;
3808 else
3810 constant_expression_warning (*width);
3811 if (tree_int_cst_sgn (*width) < 0)
3813 error ("negative width in bit-field %qs", name);
3814 *width = integer_one_node;
3816 else if (integer_zerop (*width) && orig_name)
3818 error ("zero width for bit-field %qs", name);
3819 *width = integer_one_node;
3823 /* Detect invalid bit-field type. */
3824 if (TREE_CODE (*type) != INTEGER_TYPE
3825 && TREE_CODE (*type) != BOOLEAN_TYPE
3826 && TREE_CODE (*type) != ENUMERAL_TYPE)
3828 error ("bit-field %qs has invalid type", name);
3829 *type = unsigned_type_node;
3832 type_mv = TYPE_MAIN_VARIANT (*type);
3833 if (pedantic
3834 && !in_system_header
3835 && type_mv != integer_type_node
3836 && type_mv != unsigned_type_node
3837 && type_mv != boolean_type_node)
3838 pedwarn ("type of bit-field %qs is a GCC extension", name);
3840 max_width = TYPE_PRECISION (*type);
3842 if (0 < compare_tree_int (*width, max_width))
3844 error ("width of %qs exceeds its type", name);
3845 w = max_width;
3846 *width = build_int_cst (NULL_TREE, w);
3848 else
3849 w = tree_low_cst (*width, 1);
3851 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3853 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3854 if (!lt
3855 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3856 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3857 warning (0, "%qs is narrower than values of its type", name);
3863 /* Print warning about variable length array if necessary. */
3865 static void
3866 warn_variable_length_array (const char *name, tree size)
3868 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3869 int const_size = TREE_CONSTANT (size);
3871 if (ped)
3873 if (const_size)
3875 if (name)
3876 pedwarn ("ISO C90 forbids array %qs whose size "
3877 "can%'t be evaluated",
3878 name);
3879 else
3880 pedwarn ("ISO C90 forbids array whose size "
3881 "can%'t be evaluated");
3883 else
3885 if (name)
3886 pedwarn ("ISO C90 forbids variable length array %qs",
3887 name);
3888 else
3889 pedwarn ("ISO C90 forbids variable length array");
3892 else if (warn_vla > 0)
3894 if (const_size)
3896 if (name)
3897 warning (OPT_Wvla,
3898 "the size of array %qs can"
3899 "%'t be evaluated", name);
3900 else
3901 warning (OPT_Wvla,
3902 "the size of array can %'t be evaluated");
3904 else
3906 if (name)
3907 warning (OPT_Wvla,
3908 "variable length array %qs is used",
3909 name);
3910 else
3911 warning (OPT_Wvla,
3912 "variable length array is used");
3917 /* Given declspecs and a declarator,
3918 determine the name and type of the object declared
3919 and construct a ..._DECL node for it.
3920 (In one case we can return a ..._TYPE node instead.
3921 For invalid input we sometimes return 0.)
3923 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3925 DECL_CONTEXT says which syntactic context this declaration is in:
3926 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3927 FUNCDEF for a function definition. Like NORMAL but a few different
3928 error messages in each case. Return value may be zero meaning
3929 this definition is too screwy to try to parse.
3930 PARM for a parameter declaration (either within a function prototype
3931 or before a function body). Make a PARM_DECL, or return void_type_node.
3932 TYPENAME if for a typename (in a cast or sizeof).
3933 Don't make a DECL node; just return the ..._TYPE node.
3934 FIELD for a struct or union field; make a FIELD_DECL.
3935 INITIALIZED is true if the decl has an initializer.
3936 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3937 representing the width of the bit-field.
3938 DECL_ATTRS points to the list of attributes that should be added to this
3939 decl. Any nested attributes that belong on the decl itself will be
3940 added to this list.
3941 DEPRECATED_STATE is a deprecated_states value indicating whether
3942 deprecation warnings should be suppressed.
3944 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3945 It may also be so in the PARM case, for a prototype where the
3946 argument type is specified but not the name.
3948 This function is where the complicated C meanings of `static'
3949 and `extern' are interpreted. */
3951 static tree
3952 grokdeclarator (const struct c_declarator *declarator,
3953 struct c_declspecs *declspecs,
3954 enum decl_context decl_context, bool initialized, tree *width,
3955 tree *decl_attrs, enum deprecated_states deprecated_state)
3957 tree type = declspecs->type;
3958 bool threadp = declspecs->thread_p;
3959 enum c_storage_class storage_class = declspecs->storage_class;
3960 int constp;
3961 int restrictp;
3962 int volatilep;
3963 int type_quals = TYPE_UNQUALIFIED;
3964 const char *name, *orig_name;
3965 bool funcdef_flag = false;
3966 bool funcdef_syntax = false;
3967 int size_varies = 0;
3968 tree decl_attr = declspecs->decl_attr;
3969 int array_ptr_quals = TYPE_UNQUALIFIED;
3970 tree array_ptr_attrs = NULL_TREE;
3971 int array_parm_static = 0;
3972 bool array_parm_vla_unspec_p = false;
3973 tree returned_attrs = NULL_TREE;
3974 bool bitfield = width != NULL;
3975 tree element_type;
3976 struct c_arg_info *arg_info = 0;
3978 if (decl_context == FUNCDEF)
3979 funcdef_flag = true, decl_context = NORMAL;
3981 /* Look inside a declarator for the name being declared
3982 and get it as a string, for an error message. */
3984 const struct c_declarator *decl = declarator;
3985 name = 0;
3987 while (decl)
3988 switch (decl->kind)
3990 case cdk_function:
3991 case cdk_array:
3992 case cdk_pointer:
3993 funcdef_syntax = (decl->kind == cdk_function);
3994 decl = decl->declarator;
3995 break;
3997 case cdk_attrs:
3998 decl = decl->declarator;
3999 break;
4001 case cdk_id:
4002 if (decl->u.id)
4003 name = IDENTIFIER_POINTER (decl->u.id);
4004 decl = 0;
4005 break;
4007 default:
4008 gcc_unreachable ();
4010 orig_name = name;
4011 if (name == 0)
4012 name = "type name";
4015 /* A function definition's declarator must have the form of
4016 a function declarator. */
4018 if (funcdef_flag && !funcdef_syntax)
4019 return 0;
4021 /* If this looks like a function definition, make it one,
4022 even if it occurs where parms are expected.
4023 Then store_parm_decls will reject it and not use it as a parm. */
4024 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4025 decl_context = PARM;
4027 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4028 warn_deprecated_use (declspecs->type);
4030 if ((decl_context == NORMAL || decl_context == FIELD)
4031 && current_scope == file_scope
4032 && variably_modified_type_p (type, NULL_TREE))
4034 error ("variably modified %qs at file scope", name);
4035 type = integer_type_node;
4038 size_varies = C_TYPE_VARIABLE_SIZE (type);
4040 /* Diagnose defaulting to "int". */
4042 if (declspecs->default_int_p && !in_system_header)
4044 /* Issue a warning if this is an ISO C 99 program or if
4045 -Wreturn-type and this is a function, or if -Wimplicit;
4046 prefer the former warning since it is more explicit. */
4047 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4048 && funcdef_flag)
4049 warn_about_return_type = 1;
4050 else if (warn_implicit_int || flag_isoc99)
4051 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4054 /* Adjust the type if a bit-field is being declared,
4055 -funsigned-bitfields applied and the type is not explicitly
4056 "signed". */
4057 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4058 && TREE_CODE (type) == INTEGER_TYPE)
4059 type = c_common_unsigned_type (type);
4061 /* Figure out the type qualifiers for the declaration. There are
4062 two ways a declaration can become qualified. One is something
4063 like `const int i' where the `const' is explicit. Another is
4064 something like `typedef const int CI; CI i' where the type of the
4065 declaration contains the `const'. A third possibility is that
4066 there is a type qualifier on the element type of a typedefed
4067 array type, in which case we should extract that qualifier so
4068 that c_apply_type_quals_to_decls receives the full list of
4069 qualifiers to work with (C90 is not entirely clear about whether
4070 duplicate qualifiers should be diagnosed in this case, but it
4071 seems most appropriate to do so). */
4072 element_type = strip_array_types (type);
4073 constp = declspecs->const_p + TYPE_READONLY (element_type);
4074 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4075 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4076 if (pedantic && !flag_isoc99)
4078 if (constp > 1)
4079 pedwarn ("duplicate %<const%>");
4080 if (restrictp > 1)
4081 pedwarn ("duplicate %<restrict%>");
4082 if (volatilep > 1)
4083 pedwarn ("duplicate %<volatile%>");
4085 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4086 type = TYPE_MAIN_VARIANT (type);
4087 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4088 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4089 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4091 /* Warn about storage classes that are invalid for certain
4092 kinds of declarations (parameters, typenames, etc.). */
4094 if (funcdef_flag
4095 && (threadp
4096 || storage_class == csc_auto
4097 || storage_class == csc_register
4098 || storage_class == csc_typedef))
4100 if (storage_class == csc_auto
4101 && (pedantic || current_scope == file_scope))
4102 pedwarn ("function definition declared %<auto%>");
4103 if (storage_class == csc_register)
4104 error ("function definition declared %<register%>");
4105 if (storage_class == csc_typedef)
4106 error ("function definition declared %<typedef%>");
4107 if (threadp)
4108 error ("function definition declared %<__thread%>");
4109 threadp = false;
4110 if (storage_class == csc_auto
4111 || storage_class == csc_register
4112 || storage_class == csc_typedef)
4113 storage_class = csc_none;
4115 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4117 if (decl_context == PARM && storage_class == csc_register)
4119 else
4121 switch (decl_context)
4123 case FIELD:
4124 error ("storage class specified for structure field %qs",
4125 name);
4126 break;
4127 case PARM:
4128 error ("storage class specified for parameter %qs", name);
4129 break;
4130 default:
4131 error ("storage class specified for typename");
4132 break;
4134 storage_class = csc_none;
4135 threadp = false;
4138 else if (storage_class == csc_extern
4139 && initialized
4140 && !funcdef_flag)
4142 /* 'extern' with initialization is invalid if not at file scope. */
4143 if (current_scope == file_scope)
4145 /* It is fine to have 'extern const' when compiling at C
4146 and C++ intersection. */
4147 if (!(warn_cxx_compat && constp))
4148 warning (0, "%qs initialized and declared %<extern%>", name);
4150 else
4151 error ("%qs has both %<extern%> and initializer", name);
4153 else if (current_scope == file_scope)
4155 if (storage_class == csc_auto)
4156 error ("file-scope declaration of %qs specifies %<auto%>", name);
4157 if (pedantic && storage_class == csc_register)
4158 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4160 else
4162 if (storage_class == csc_extern && funcdef_flag)
4163 error ("nested function %qs declared %<extern%>", name);
4164 else if (threadp && storage_class == csc_none)
4166 error ("function-scope %qs implicitly auto and declared "
4167 "%<__thread%>",
4168 name);
4169 threadp = false;
4173 /* Now figure out the structure of the declarator proper.
4174 Descend through it, creating more complex types, until we reach
4175 the declared identifier (or NULL_TREE, in an absolute declarator).
4176 At each stage we maintain an unqualified version of the type
4177 together with any qualifiers that should be applied to it with
4178 c_build_qualified_type; this way, array types including
4179 multidimensional array types are first built up in unqualified
4180 form and then the qualified form is created with
4181 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4183 while (declarator && declarator->kind != cdk_id)
4185 if (type == error_mark_node)
4187 declarator = declarator->declarator;
4188 continue;
4191 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4192 a cdk_pointer (for *...),
4193 a cdk_function (for ...(...)),
4194 a cdk_attrs (for nested attributes),
4195 or a cdk_id (for the name being declared
4196 or the place in an absolute declarator
4197 where the name was omitted).
4198 For the last case, we have just exited the loop.
4200 At this point, TYPE is the type of elements of an array,
4201 or for a function to return, or for a pointer to point to.
4202 After this sequence of ifs, TYPE is the type of the
4203 array or function or pointer, and DECLARATOR has had its
4204 outermost layer removed. */
4206 if (array_ptr_quals != TYPE_UNQUALIFIED
4207 || array_ptr_attrs != NULL_TREE
4208 || array_parm_static)
4210 /* Only the innermost declarator (making a parameter be of
4211 array type which is converted to pointer type)
4212 may have static or type qualifiers. */
4213 error ("static or type qualifiers in non-parameter array declarator");
4214 array_ptr_quals = TYPE_UNQUALIFIED;
4215 array_ptr_attrs = NULL_TREE;
4216 array_parm_static = 0;
4219 switch (declarator->kind)
4221 case cdk_attrs:
4223 /* A declarator with embedded attributes. */
4224 tree attrs = declarator->u.attrs;
4225 const struct c_declarator *inner_decl;
4226 int attr_flags = 0;
4227 declarator = declarator->declarator;
4228 inner_decl = declarator;
4229 while (inner_decl->kind == cdk_attrs)
4230 inner_decl = inner_decl->declarator;
4231 if (inner_decl->kind == cdk_id)
4232 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4233 else if (inner_decl->kind == cdk_function)
4234 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4235 else if (inner_decl->kind == cdk_array)
4236 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4237 returned_attrs = decl_attributes (&type,
4238 chainon (returned_attrs, attrs),
4239 attr_flags);
4240 break;
4242 case cdk_array:
4244 tree itype = NULL_TREE;
4245 tree size = declarator->u.array.dimen;
4246 /* The index is a signed object `sizetype' bits wide. */
4247 tree index_type = c_common_signed_type (sizetype);
4249 array_ptr_quals = declarator->u.array.quals;
4250 array_ptr_attrs = declarator->u.array.attrs;
4251 array_parm_static = declarator->u.array.static_p;
4252 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4254 declarator = declarator->declarator;
4256 /* Check for some types that there cannot be arrays of. */
4258 if (VOID_TYPE_P (type))
4260 error ("declaration of %qs as array of voids", name);
4261 type = error_mark_node;
4264 if (TREE_CODE (type) == FUNCTION_TYPE)
4266 error ("declaration of %qs as array of functions", name);
4267 type = error_mark_node;
4270 if (pedantic && !in_system_header && flexible_array_type_p (type))
4271 pedwarn ("invalid use of structure with flexible array member");
4273 if (size == error_mark_node)
4274 type = error_mark_node;
4276 if (type == error_mark_node)
4277 continue;
4279 /* If size was specified, set ITYPE to a range-type for
4280 that size. Otherwise, ITYPE remains null. finish_decl
4281 may figure it out from an initial value. */
4283 if (size)
4285 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4286 lvalue. */
4287 STRIP_TYPE_NOPS (size);
4289 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4291 error ("size of array %qs has non-integer type", name);
4292 size = integer_one_node;
4295 if (pedantic && integer_zerop (size))
4296 pedwarn ("ISO C forbids zero-size array %qs", name);
4298 if (TREE_CODE (size) == INTEGER_CST)
4300 constant_expression_warning (size);
4301 if (tree_int_cst_sgn (size) < 0)
4303 error ("size of array %qs is negative", name);
4304 size = integer_one_node;
4307 else if ((decl_context == NORMAL || decl_context == FIELD)
4308 && current_scope == file_scope)
4310 error ("variably modified %qs at file scope", name);
4311 size = integer_one_node;
4313 else
4315 /* Make sure the array size remains visibly
4316 nonconstant even if it is (eg) a const variable
4317 with known value. */
4318 size_varies = 1;
4319 warn_variable_length_array (orig_name, size);
4322 if (integer_zerop (size))
4324 /* A zero-length array cannot be represented with
4325 an unsigned index type, which is what we'll
4326 get with build_index_type. Create an
4327 open-ended range instead. */
4328 itype = build_range_type (sizetype, size, NULL_TREE);
4330 else
4332 /* Arrange for the SAVE_EXPR on the inside of the
4333 MINUS_EXPR, which allows the -1 to get folded
4334 with the +1 that happens when building TYPE_SIZE. */
4335 if (size_varies)
4336 size = variable_size (size);
4338 /* Compute the maximum valid index, that is, size
4339 - 1. Do the calculation in index_type, so that
4340 if it is a variable the computations will be
4341 done in the proper mode. */
4342 itype = fold_build2 (MINUS_EXPR, index_type,
4343 convert (index_type, size),
4344 convert (index_type,
4345 size_one_node));
4347 /* If that overflowed, the array is too big. ???
4348 While a size of INT_MAX+1 technically shouldn't
4349 cause an overflow (because we subtract 1), the
4350 overflow is recorded during the conversion to
4351 index_type, before the subtraction. Handling
4352 this case seems like an unnecessary
4353 complication. */
4354 if (TREE_CODE (itype) == INTEGER_CST
4355 && TREE_OVERFLOW (itype))
4357 error ("size of array %qs is too large", name);
4358 type = error_mark_node;
4359 continue;
4362 itype = build_index_type (itype);
4365 else if (decl_context == FIELD)
4367 if (pedantic && !flag_isoc99 && !in_system_header)
4368 pedwarn ("ISO C90 does not support flexible array members");
4370 /* ISO C99 Flexible array members are effectively
4371 identical to GCC's zero-length array extension. */
4372 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4374 else if (decl_context == PARM)
4376 if (array_parm_vla_unspec_p)
4378 if (! orig_name)
4380 /* C99 6.7.5.2p4 */
4381 error ("%<[*]%> not allowed in other than a declaration");
4384 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4385 size_varies = 1;
4388 else if (decl_context == TYPENAME)
4390 if (array_parm_vla_unspec_p)
4392 /* The error is printed elsewhere. We use this to
4393 avoid messing up with incomplete array types of
4394 the same type, that would otherwise be modified
4395 below. */
4396 itype = build_range_type (sizetype, size_zero_node,
4397 NULL_TREE);
4401 /* Complain about arrays of incomplete types. */
4402 if (!COMPLETE_TYPE_P (type))
4404 error ("array type has incomplete element type");
4405 type = error_mark_node;
4407 else
4408 /* When itype is NULL, a shared incomplete array type is
4409 returned for all array of a given type. Elsewhere we
4410 make sure we don't complete that type before copying
4411 it, but here we want to make sure we don't ever
4412 modify the shared type, so we gcc_assert (itype)
4413 below. */
4414 type = build_array_type (type, itype);
4416 if (type != error_mark_node)
4418 if (size_varies)
4420 /* It is ok to modify type here even if itype is
4421 NULL: if size_varies, we're in a
4422 multi-dimensional array and the inner type has
4423 variable size, so the enclosing shared array type
4424 must too. */
4425 if (size && TREE_CODE (size) == INTEGER_CST)
4426 type
4427 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4428 C_TYPE_VARIABLE_SIZE (type) = 1;
4431 /* The GCC extension for zero-length arrays differs from
4432 ISO flexible array members in that sizeof yields
4433 zero. */
4434 if (size && integer_zerop (size))
4436 gcc_assert (itype);
4437 TYPE_SIZE (type) = bitsize_zero_node;
4438 TYPE_SIZE_UNIT (type) = size_zero_node;
4440 if (array_parm_vla_unspec_p)
4442 gcc_assert (itype);
4443 /* The type is complete. C99 6.7.5.2p4 */
4444 TYPE_SIZE (type) = bitsize_zero_node;
4445 TYPE_SIZE_UNIT (type) = size_zero_node;
4449 if (decl_context != PARM
4450 && (array_ptr_quals != TYPE_UNQUALIFIED
4451 || array_ptr_attrs != NULL_TREE
4452 || array_parm_static))
4454 error ("static or type qualifiers in non-parameter array declarator");
4455 array_ptr_quals = TYPE_UNQUALIFIED;
4456 array_ptr_attrs = NULL_TREE;
4457 array_parm_static = 0;
4459 break;
4461 case cdk_function:
4463 /* Say it's a definition only for the declarator closest
4464 to the identifier, apart possibly from some
4465 attributes. */
4466 bool really_funcdef = false;
4467 tree arg_types;
4468 if (funcdef_flag)
4470 const struct c_declarator *t = declarator->declarator;
4471 while (t->kind == cdk_attrs)
4472 t = t->declarator;
4473 really_funcdef = (t->kind == cdk_id);
4476 /* Declaring a function type. Make sure we have a valid
4477 type for the function to return. */
4478 if (type == error_mark_node)
4479 continue;
4481 size_varies = 0;
4483 /* Warn about some types functions can't return. */
4484 if (TREE_CODE (type) == FUNCTION_TYPE)
4486 error ("%qs declared as function returning a function", name);
4487 type = integer_type_node;
4489 if (TREE_CODE (type) == ARRAY_TYPE)
4491 error ("%qs declared as function returning an array", name);
4492 type = integer_type_node;
4495 /* Construct the function type and go to the next
4496 inner layer of declarator. */
4497 arg_info = declarator->u.arg_info;
4498 arg_types = grokparms (arg_info, really_funcdef);
4499 if (really_funcdef)
4500 put_pending_sizes (arg_info->pending_sizes);
4502 /* Type qualifiers before the return type of the function
4503 qualify the return type, not the function type. */
4504 if (type_quals)
4506 /* Type qualifiers on a function return type are
4507 normally permitted by the standard but have no
4508 effect, so give a warning at -Wreturn-type.
4509 Qualifiers on a void return type are banned on
4510 function definitions in ISO C; GCC used to used
4511 them for noreturn functions. */
4512 if (VOID_TYPE_P (type) && really_funcdef)
4513 pedwarn ("function definition has qualified void return type");
4514 else
4515 warning (OPT_Wignored_qualifiers,
4516 "type qualifiers ignored on function return type");
4518 type = c_build_qualified_type (type, type_quals);
4520 type_quals = TYPE_UNQUALIFIED;
4522 type = build_function_type (type, arg_types);
4523 declarator = declarator->declarator;
4525 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4526 the formal parameter list of this FUNCTION_TYPE to point to
4527 the FUNCTION_TYPE node itself. */
4529 tree link;
4531 for (link = arg_info->tags;
4532 link;
4533 link = TREE_CHAIN (link))
4534 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4536 break;
4538 case cdk_pointer:
4540 /* Merge any constancy or volatility into the target type
4541 for the pointer. */
4543 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4544 && type_quals)
4545 pedwarn ("ISO C forbids qualified function types");
4546 if (type_quals)
4547 type = c_build_qualified_type (type, type_quals);
4548 size_varies = 0;
4550 /* When the pointed-to type involves components of variable size,
4551 care must be taken to ensure that the size evaluation code is
4552 emitted early enough to dominate all the possible later uses
4553 and late enough for the variables on which it depends to have
4554 been assigned.
4556 This is expected to happen automatically when the pointed-to
4557 type has a name/declaration of it's own, but special attention
4558 is required if the type is anonymous.
4560 We handle the NORMAL and FIELD contexts here by attaching an
4561 artificial TYPE_DECL to such pointed-to type. This forces the
4562 sizes evaluation at a safe point and ensures it is not deferred
4563 until e.g. within a deeper conditional context.
4565 We expect nothing to be needed here for PARM or TYPENAME.
4566 Pushing a TYPE_DECL at this point for TYPENAME would actually
4567 be incorrect, as we might be in the middle of an expression
4568 with side effects on the pointed-to type size "arguments" prior
4569 to the pointer declaration point and the fake TYPE_DECL in the
4570 enclosing context would force the size evaluation prior to the
4571 side effects. */
4573 if (!TYPE_NAME (type)
4574 && (decl_context == NORMAL || decl_context == FIELD)
4575 && variably_modified_type_p (type, NULL_TREE))
4577 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4578 DECL_ARTIFICIAL (decl) = 1;
4579 pushdecl (decl);
4580 finish_decl (decl, NULL_TREE, NULL_TREE);
4581 TYPE_NAME (type) = decl;
4584 type = build_pointer_type (type);
4586 /* Process type qualifiers (such as const or volatile)
4587 that were given inside the `*'. */
4588 type_quals = declarator->u.pointer_quals;
4590 declarator = declarator->declarator;
4591 break;
4593 default:
4594 gcc_unreachable ();
4597 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4599 /* Now TYPE has the actual type, apart from any qualifiers in
4600 TYPE_QUALS. */
4602 /* Check the type and width of a bit-field. */
4603 if (bitfield)
4604 check_bitfield_type_and_width (&type, width, orig_name);
4606 /* Did array size calculations overflow? */
4608 if (TREE_CODE (type) == ARRAY_TYPE
4609 && COMPLETE_TYPE_P (type)
4610 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4611 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4613 error ("size of array %qs is too large", name);
4614 /* If we proceed with the array type as it is, we'll eventually
4615 crash in tree_low_cst(). */
4616 type = error_mark_node;
4619 /* If this is declaring a typedef name, return a TYPE_DECL. */
4621 if (storage_class == csc_typedef)
4623 tree decl;
4624 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4625 && type_quals)
4626 pedwarn ("ISO C forbids qualified function types");
4627 if (type_quals)
4628 type = c_build_qualified_type (type, type_quals);
4629 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4630 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4631 if (declspecs->explicit_signed_p)
4632 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4633 if (declspecs->inline_p)
4634 pedwarn ("typedef %q+D declared %<inline%>", decl);
4635 return decl;
4638 /* If this is a type name (such as, in a cast or sizeof),
4639 compute the type and return it now. */
4641 if (decl_context == TYPENAME)
4643 /* Note that the grammar rejects storage classes in typenames
4644 and fields. */
4645 gcc_assert (storage_class == csc_none && !threadp
4646 && !declspecs->inline_p);
4647 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4648 && type_quals)
4649 pedwarn ("ISO C forbids const or volatile function types");
4650 if (type_quals)
4651 type = c_build_qualified_type (type, type_quals);
4652 return type;
4655 if (pedantic && decl_context == FIELD
4656 && variably_modified_type_p (type, NULL_TREE))
4658 /* C99 6.7.2.1p8 */
4659 pedwarn ("a member of a structure or union cannot have a variably modified type");
4662 /* Aside from typedefs and type names (handle above),
4663 `void' at top level (not within pointer)
4664 is allowed only in public variables.
4665 We don't complain about parms either, but that is because
4666 a better error message can be made later. */
4668 if (VOID_TYPE_P (type) && decl_context != PARM
4669 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4670 && (storage_class == csc_extern
4671 || (current_scope == file_scope
4672 && !(storage_class == csc_static
4673 || storage_class == csc_register)))))
4675 error ("variable or field %qs declared void", name);
4676 type = integer_type_node;
4679 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4680 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4683 tree decl;
4685 if (decl_context == PARM)
4687 tree promoted_type;
4689 /* A parameter declared as an array of T is really a pointer to T.
4690 One declared as a function is really a pointer to a function. */
4692 if (TREE_CODE (type) == ARRAY_TYPE)
4694 /* Transfer const-ness of array into that of type pointed to. */
4695 type = TREE_TYPE (type);
4696 if (type_quals)
4697 type = c_build_qualified_type (type, type_quals);
4698 type = build_pointer_type (type);
4699 type_quals = array_ptr_quals;
4700 if (type_quals)
4701 type = c_build_qualified_type (type, type_quals);
4703 /* We don't yet implement attributes in this context. */
4704 if (array_ptr_attrs != NULL_TREE)
4705 warning (OPT_Wattributes,
4706 "attributes in parameter array declarator ignored");
4708 size_varies = 0;
4710 else if (TREE_CODE (type) == FUNCTION_TYPE)
4712 if (pedantic && type_quals)
4713 pedwarn ("ISO C forbids qualified function types");
4714 if (type_quals)
4715 type = c_build_qualified_type (type, type_quals);
4716 type = build_pointer_type (type);
4717 type_quals = TYPE_UNQUALIFIED;
4719 else if (type_quals)
4720 type = c_build_qualified_type (type, type_quals);
4722 decl = build_decl (PARM_DECL, declarator->u.id, type);
4723 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4724 if (size_varies)
4725 C_DECL_VARIABLE_SIZE (decl) = 1;
4727 /* Compute the type actually passed in the parmlist,
4728 for the case where there is no prototype.
4729 (For example, shorts and chars are passed as ints.)
4730 When there is a prototype, this is overridden later. */
4732 if (type == error_mark_node)
4733 promoted_type = type;
4734 else
4735 promoted_type = c_type_promotes_to (type);
4737 DECL_ARG_TYPE (decl) = promoted_type;
4738 if (declspecs->inline_p)
4739 pedwarn ("parameter %q+D declared %<inline%>", decl);
4741 else if (decl_context == FIELD)
4743 /* Note that the grammar rejects storage classes in typenames
4744 and fields. */
4745 gcc_assert (storage_class == csc_none && !threadp
4746 && !declspecs->inline_p);
4748 /* Structure field. It may not be a function. */
4750 if (TREE_CODE (type) == FUNCTION_TYPE)
4752 error ("field %qs declared as a function", name);
4753 type = build_pointer_type (type);
4755 else if (TREE_CODE (type) != ERROR_MARK
4756 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4758 error ("field %qs has incomplete type", name);
4759 type = error_mark_node;
4761 type = c_build_qualified_type (type, type_quals);
4762 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4763 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4764 DECL_NONADDRESSABLE_P (decl) = bitfield;
4765 if (bitfield && !declarator->u.id)
4766 TREE_NO_WARNING (decl) = 1;
4768 if (size_varies)
4769 C_DECL_VARIABLE_SIZE (decl) = 1;
4771 else if (TREE_CODE (type) == FUNCTION_TYPE)
4773 if (storage_class == csc_register || threadp)
4775 error ("invalid storage class for function %qs", name);
4777 else if (current_scope != file_scope)
4779 /* Function declaration not at file scope. Storage
4780 classes other than `extern' are not allowed, C99
4781 6.7.1p5, and `extern' makes no difference. However,
4782 GCC allows 'auto', perhaps with 'inline', to support
4783 nested functions. */
4784 if (storage_class == csc_auto)
4786 if (pedantic)
4787 pedwarn ("invalid storage class for function %qs", name);
4789 else if (storage_class == csc_static)
4791 error ("invalid storage class for function %qs", name);
4792 if (funcdef_flag)
4793 storage_class = declspecs->storage_class = csc_none;
4794 else
4795 return 0;
4799 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4800 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4801 decl = build_decl_attribute_variant (decl, decl_attr);
4803 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4804 pedwarn ("ISO C forbids qualified function types");
4806 /* GNU C interprets a volatile-qualified function type to indicate
4807 that the function does not return. */
4808 if ((type_quals & TYPE_QUAL_VOLATILE)
4809 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4810 warning (0, "%<noreturn%> function returns non-void value");
4812 /* Every function declaration is an external reference
4813 (DECL_EXTERNAL) except for those which are not at file
4814 scope and are explicitly declared "auto". This is
4815 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4816 GCC to signify a forward declaration of a nested function. */
4817 if (storage_class == csc_auto && current_scope != file_scope)
4818 DECL_EXTERNAL (decl) = 0;
4819 /* In C99, a function which is declared 'inline' with 'extern'
4820 is not an external reference (which is confusing). It
4821 means that the later definition of the function must be output
4822 in this file, C99 6.7.4p6. In GNU C89, a function declared
4823 'extern inline' is an external reference. */
4824 else if (declspecs->inline_p && storage_class != csc_static)
4825 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4826 == flag_gnu89_inline);
4827 else
4828 DECL_EXTERNAL (decl) = !initialized;
4830 /* Record absence of global scope for `static' or `auto'. */
4831 TREE_PUBLIC (decl)
4832 = !(storage_class == csc_static || storage_class == csc_auto);
4834 /* For a function definition, record the argument information
4835 block where store_parm_decls will look for it. */
4836 if (funcdef_flag)
4837 current_function_arg_info = arg_info;
4839 if (declspecs->default_int_p)
4840 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4842 /* Record presence of `inline', if it is reasonable. */
4843 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4845 if (declspecs->inline_p)
4846 pedwarn ("cannot inline function %<main%>");
4848 else if (declspecs->inline_p)
4850 /* Record that the function is declared `inline'. */
4851 DECL_DECLARED_INLINE_P (decl) = 1;
4853 /* Do not mark bare declarations as DECL_INLINE. Doing so
4854 in the presence of multiple declarations can result in
4855 the abstract origin pointing between the declarations,
4856 which will confuse dwarf2out. */
4857 if (initialized)
4858 DECL_INLINE (decl) = 1;
4860 /* If -finline-functions, assume it can be inlined. This does
4861 two things: let the function be deferred until it is actually
4862 needed, and let dwarf2 know that the function is inlinable. */
4863 else if (flag_inline_trees == 2 && initialized)
4864 DECL_INLINE (decl) = 1;
4866 else
4868 /* It's a variable. */
4869 /* An uninitialized decl with `extern' is a reference. */
4870 int extern_ref = !initialized && storage_class == csc_extern;
4872 type = c_build_qualified_type (type, type_quals);
4874 /* C99 6.2.2p7: It is invalid (compile-time undefined
4875 behavior) to create an 'extern' declaration for a
4876 variable if there is a global declaration that is
4877 'static' and the global declaration is not visible.
4878 (If the static declaration _is_ currently visible,
4879 the 'extern' declaration is taken to refer to that decl.) */
4880 if (extern_ref && current_scope != file_scope)
4882 tree global_decl = identifier_global_value (declarator->u.id);
4883 tree visible_decl = lookup_name (declarator->u.id);
4885 if (global_decl
4886 && global_decl != visible_decl
4887 && TREE_CODE (global_decl) == VAR_DECL
4888 && !TREE_PUBLIC (global_decl))
4889 error ("variable previously declared %<static%> redeclared "
4890 "%<extern%>");
4893 decl = build_decl (VAR_DECL, declarator->u.id, type);
4894 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4895 if (size_varies)
4896 C_DECL_VARIABLE_SIZE (decl) = 1;
4898 if (declspecs->inline_p)
4899 pedwarn ("variable %q+D declared %<inline%>", decl);
4901 /* At file scope, an initialized extern declaration may follow
4902 a static declaration. In that case, DECL_EXTERNAL will be
4903 reset later in start_decl. */
4904 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4906 /* At file scope, the presence of a `static' or `register' storage
4907 class specifier, or the absence of all storage class specifiers
4908 makes this declaration a definition (perhaps tentative). Also,
4909 the absence of `static' makes it public. */
4910 if (current_scope == file_scope)
4912 TREE_PUBLIC (decl) = storage_class != csc_static;
4913 TREE_STATIC (decl) = !extern_ref;
4915 /* Not at file scope, only `static' makes a static definition. */
4916 else
4918 TREE_STATIC (decl) = (storage_class == csc_static);
4919 TREE_PUBLIC (decl) = extern_ref;
4922 if (threadp)
4923 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4926 if (storage_class == csc_extern
4927 && variably_modified_type_p (type, NULL_TREE))
4929 /* C99 6.7.5.2p2 */
4930 error ("object with variably modified type must have no linkage");
4933 /* Record `register' declaration for warnings on &
4934 and in case doing stupid register allocation. */
4936 if (storage_class == csc_register)
4938 C_DECL_REGISTER (decl) = 1;
4939 DECL_REGISTER (decl) = 1;
4942 /* Record constancy and volatility. */
4943 c_apply_type_quals_to_decl (type_quals, decl);
4945 /* If a type has volatile components, it should be stored in memory.
4946 Otherwise, the fact that those components are volatile
4947 will be ignored, and would even crash the compiler.
4948 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4949 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4950 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4951 || TREE_CODE (decl) == RESULT_DECL))
4953 /* It is not an error for a structure with volatile fields to
4954 be declared register, but reset DECL_REGISTER since it
4955 cannot actually go in a register. */
4956 int was_reg = C_DECL_REGISTER (decl);
4957 C_DECL_REGISTER (decl) = 0;
4958 DECL_REGISTER (decl) = 0;
4959 c_mark_addressable (decl);
4960 C_DECL_REGISTER (decl) = was_reg;
4963 /* This is the earliest point at which we might know the assembler
4964 name of a variable. Thus, if it's known before this, die horribly. */
4965 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4967 return decl;
4971 /* Decode the parameter-list info for a function type or function definition.
4972 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4973 if there is an identifier list instead of a parameter decl list).
4974 These two functions are separate because when a function returns
4975 or receives functions then each is called multiple times but the order
4976 of calls is different. The last call to `grokparms' is always the one
4977 that contains the formal parameter names of a function definition.
4979 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4981 FUNCDEF_FLAG is true for a function definition, false for
4982 a mere declaration. A nonempty identifier-list gets an error message
4983 when FUNCDEF_FLAG is false. */
4985 static tree
4986 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4988 tree arg_types = arg_info->types;
4990 if (funcdef_flag && arg_info->had_vla_unspec)
4992 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4993 /* C99 6.7.5.2p4 */
4994 error ("%<[*]%> not allowed in other than function prototype scope");
4997 if (arg_types == 0 && !funcdef_flag && !in_system_header)
4998 warning (OPT_Wstrict_prototypes,
4999 "function declaration isn%'t a prototype");
5001 if (arg_types == error_mark_node)
5002 return 0; /* don't set TYPE_ARG_TYPES in this case */
5004 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5006 if (!funcdef_flag)
5007 pedwarn ("parameter names (without types) in function declaration");
5009 arg_info->parms = arg_info->types;
5010 arg_info->types = 0;
5011 return 0;
5013 else
5015 tree parm, type, typelt;
5016 unsigned int parmno;
5018 /* If there is a parameter of incomplete type in a definition,
5019 this is an error. In a declaration this is valid, and a
5020 struct or union type may be completed later, before any calls
5021 or definition of the function. In the case where the tag was
5022 first declared within the parameter list, a warning has
5023 already been given. If a parameter has void type, then
5024 however the function cannot be defined or called, so
5025 warn. */
5027 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5028 parm;
5029 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5031 type = TREE_VALUE (typelt);
5032 if (type == error_mark_node)
5033 continue;
5035 if (!COMPLETE_TYPE_P (type))
5037 if (funcdef_flag)
5039 if (DECL_NAME (parm))
5040 error ("parameter %u (%q+D) has incomplete type",
5041 parmno, parm);
5042 else
5043 error ("%Jparameter %u has incomplete type",
5044 parm, parmno);
5046 TREE_VALUE (typelt) = error_mark_node;
5047 TREE_TYPE (parm) = error_mark_node;
5049 else if (VOID_TYPE_P (type))
5051 if (DECL_NAME (parm))
5052 warning (0, "parameter %u (%q+D) has void type",
5053 parmno, parm);
5054 else
5055 warning (0, "%Jparameter %u has void type",
5056 parm, parmno);
5060 if (DECL_NAME (parm) && TREE_USED (parm))
5061 warn_if_shadowing (parm);
5063 return arg_types;
5067 /* Take apart the current scope and return a c_arg_info structure with
5068 info on a parameter list just parsed.
5070 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5072 ELLIPSIS being true means the argument list ended in '...' so don't
5073 append a sentinel (void_list_node) to the end of the type-list. */
5075 struct c_arg_info *
5076 get_parm_info (bool ellipsis)
5078 struct c_binding *b = current_scope->bindings;
5079 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5080 struct c_arg_info);
5081 tree parms = 0;
5082 tree tags = 0;
5083 tree types = 0;
5084 tree others = 0;
5086 static bool explained_incomplete_types = false;
5087 bool gave_void_only_once_err = false;
5089 arg_info->parms = 0;
5090 arg_info->tags = 0;
5091 arg_info->types = 0;
5092 arg_info->others = 0;
5093 arg_info->pending_sizes = 0;
5094 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5096 /* The bindings in this scope must not get put into a block.
5097 We will take care of deleting the binding nodes. */
5098 current_scope->bindings = 0;
5100 /* This function is only called if there was *something* on the
5101 parameter list. */
5102 gcc_assert (b);
5104 /* A parameter list consisting solely of 'void' indicates that the
5105 function takes no arguments. But if the 'void' is qualified
5106 (by 'const' or 'volatile'), or has a storage class specifier
5107 ('register'), then the behavior is undefined; issue an error.
5108 Typedefs for 'void' are OK (see DR#157). */
5109 if (b->prev == 0 /* one binding */
5110 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5111 && !DECL_NAME (b->decl) /* anonymous */
5112 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5114 if (TREE_THIS_VOLATILE (b->decl)
5115 || TREE_READONLY (b->decl)
5116 || C_DECL_REGISTER (b->decl))
5117 error ("%<void%> as only parameter may not be qualified");
5119 /* There cannot be an ellipsis. */
5120 if (ellipsis)
5121 error ("%<void%> must be the only parameter");
5123 arg_info->types = void_list_node;
5124 return arg_info;
5127 if (!ellipsis)
5128 types = void_list_node;
5130 /* Break up the bindings list into parms, tags, types, and others;
5131 apply sanity checks; purge the name-to-decl bindings. */
5132 while (b)
5134 tree decl = b->decl;
5135 tree type = TREE_TYPE (decl);
5136 const char *keyword;
5138 switch (TREE_CODE (decl))
5140 case PARM_DECL:
5141 if (b->id)
5143 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5144 I_SYMBOL_BINDING (b->id) = b->shadowed;
5147 /* Check for forward decls that never got their actual decl. */
5148 if (TREE_ASM_WRITTEN (decl))
5149 error ("parameter %q+D has just a forward declaration", decl);
5150 /* Check for (..., void, ...) and issue an error. */
5151 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5153 if (!gave_void_only_once_err)
5155 error ("%<void%> must be the only parameter");
5156 gave_void_only_once_err = true;
5159 else
5161 /* Valid parameter, add it to the list. */
5162 TREE_CHAIN (decl) = parms;
5163 parms = decl;
5165 /* Since there is a prototype, args are passed in their
5166 declared types. The back end may override this later. */
5167 DECL_ARG_TYPE (decl) = type;
5168 types = tree_cons (0, type, types);
5170 break;
5172 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5173 case UNION_TYPE: keyword = "union"; goto tag;
5174 case RECORD_TYPE: keyword = "struct"; goto tag;
5175 tag:
5176 /* Types may not have tag-names, in which case the type
5177 appears in the bindings list with b->id NULL. */
5178 if (b->id)
5180 gcc_assert (I_TAG_BINDING (b->id) == b);
5181 I_TAG_BINDING (b->id) = b->shadowed;
5184 /* Warn about any struct, union or enum tags defined in a
5185 parameter list. The scope of such types is limited to
5186 the parameter list, which is rarely if ever desirable
5187 (it's impossible to call such a function with type-
5188 correct arguments). An anonymous union parm type is
5189 meaningful as a GNU extension, so don't warn for that. */
5190 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5192 if (b->id)
5193 /* The %s will be one of 'struct', 'union', or 'enum'. */
5194 warning (0, "%<%s %E%> declared inside parameter list",
5195 keyword, b->id);
5196 else
5197 /* The %s will be one of 'struct', 'union', or 'enum'. */
5198 warning (0, "anonymous %s declared inside parameter list",
5199 keyword);
5201 if (!explained_incomplete_types)
5203 warning (0, "its scope is only this definition or declaration,"
5204 " which is probably not what you want");
5205 explained_incomplete_types = true;
5209 tags = tree_cons (b->id, decl, tags);
5210 break;
5212 case CONST_DECL:
5213 case TYPE_DECL:
5214 case FUNCTION_DECL:
5215 /* CONST_DECLs appear here when we have an embedded enum,
5216 and TYPE_DECLs appear here when we have an embedded struct
5217 or union. No warnings for this - we already warned about the
5218 type itself. FUNCTION_DECLs appear when there is an implicit
5219 function declaration in the parameter list. */
5221 TREE_CHAIN (decl) = others;
5222 others = decl;
5223 /* fall through */
5225 case ERROR_MARK:
5226 /* error_mark_node appears here when we have an undeclared
5227 variable. Just throw it away. */
5228 if (b->id)
5230 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5231 I_SYMBOL_BINDING (b->id) = b->shadowed;
5233 break;
5235 /* Other things that might be encountered. */
5236 case LABEL_DECL:
5237 case VAR_DECL:
5238 default:
5239 gcc_unreachable ();
5242 b = free_binding_and_advance (b);
5245 arg_info->parms = parms;
5246 arg_info->tags = tags;
5247 arg_info->types = types;
5248 arg_info->others = others;
5249 arg_info->pending_sizes = get_pending_sizes ();
5250 return arg_info;
5253 /* Get the struct, enum or union (CODE says which) with tag NAME.
5254 Define the tag as a forward-reference if it is not defined.
5255 Return a c_typespec structure for the type specifier. */
5257 struct c_typespec
5258 parser_xref_tag (enum tree_code code, tree name)
5260 struct c_typespec ret;
5261 /* If a cross reference is requested, look up the type
5262 already defined for this tag and return it. */
5264 tree ref = lookup_tag (code, name, 0);
5265 /* If this is the right type of tag, return what we found.
5266 (This reference will be shadowed by shadow_tag later if appropriate.)
5267 If this is the wrong type of tag, do not return it. If it was the
5268 wrong type in the same scope, we will have had an error
5269 message already; if in a different scope and declaring
5270 a name, pending_xref_error will give an error message; but if in a
5271 different scope and not declaring a name, this tag should
5272 shadow the previous declaration of a different type of tag, and
5273 this would not work properly if we return the reference found.
5274 (For example, with "struct foo" in an outer scope, "union foo;"
5275 must shadow that tag with a new one of union type.) */
5276 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5277 if (ref && TREE_CODE (ref) == code)
5279 ret.spec = ref;
5280 return ret;
5283 /* If no such tag is yet defined, create a forward-reference node
5284 and record it as the "definition".
5285 When a real declaration of this type is found,
5286 the forward-reference will be altered into a real type. */
5288 ref = make_node (code);
5289 if (code == ENUMERAL_TYPE)
5291 /* Give the type a default layout like unsigned int
5292 to avoid crashing if it does not get defined. */
5293 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5294 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5295 TYPE_USER_ALIGN (ref) = 0;
5296 TYPE_UNSIGNED (ref) = 1;
5297 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5298 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5299 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5302 pushtag (name, ref);
5304 ret.spec = ref;
5305 return ret;
5308 /* Get the struct, enum or union (CODE says which) with tag NAME.
5309 Define the tag as a forward-reference if it is not defined.
5310 Return a tree for the type. */
5312 tree
5313 xref_tag (enum tree_code code, tree name)
5315 return parser_xref_tag (code, name).spec;
5318 /* Make sure that the tag NAME is defined *in the current scope*
5319 at least as a forward reference.
5320 CODE says which kind of tag NAME ought to be. */
5322 tree
5323 start_struct (enum tree_code code, tree name)
5325 /* If there is already a tag defined at this scope
5326 (as a forward reference), just return it. */
5328 tree ref = 0;
5330 if (name != 0)
5331 ref = lookup_tag (code, name, 1);
5332 if (ref && TREE_CODE (ref) == code)
5334 if (TYPE_SIZE (ref))
5336 if (code == UNION_TYPE)
5337 error ("redefinition of %<union %E%>", name);
5338 else
5339 error ("redefinition of %<struct %E%>", name);
5341 else if (C_TYPE_BEING_DEFINED (ref))
5343 if (code == UNION_TYPE)
5344 error ("nested redefinition of %<union %E%>", name);
5345 else
5346 error ("nested redefinition of %<struct %E%>", name);
5347 /* Don't create structures that contain themselves. */
5348 ref = NULL_TREE;
5352 /* Otherwise create a forward-reference just so the tag is in scope. */
5354 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5356 ref = make_node (code);
5357 pushtag (name, ref);
5360 C_TYPE_BEING_DEFINED (ref) = 1;
5361 TYPE_PACKED (ref) = flag_pack_struct;
5362 return ref;
5365 /* Process the specs, declarator and width (NULL if omitted)
5366 of a structure component, returning a FIELD_DECL node.
5367 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5368 DECL_ATTRS is as for grokdeclarator.
5370 This is done during the parsing of the struct declaration.
5371 The FIELD_DECL nodes are chained together and the lot of them
5372 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5374 tree
5375 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5376 tree width, tree *decl_attrs)
5378 tree value;
5380 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5381 && width == NULL_TREE)
5383 /* This is an unnamed decl.
5385 If we have something of the form "union { list } ;" then this
5386 is the anonymous union extension. Similarly for struct.
5388 If this is something of the form "struct foo;", then
5389 If MS extensions are enabled, this is handled as an
5390 anonymous struct.
5391 Otherwise this is a forward declaration of a structure tag.
5393 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5394 If MS extensions are enabled and foo names a structure, then
5395 again this is an anonymous struct.
5396 Otherwise this is an error.
5398 Oh what a horrid tangled web we weave. I wonder if MS consciously
5399 took this from Plan 9 or if it was an accident of implementation
5400 that took root before someone noticed the bug... */
5402 tree type = declspecs->type;
5403 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5404 || TREE_CODE (type) == UNION_TYPE);
5405 bool ok = false;
5407 if (type_ok
5408 && (flag_ms_extensions || !declspecs->typedef_p))
5410 if (flag_ms_extensions)
5411 ok = true;
5412 else if (flag_iso)
5413 ok = false;
5414 else if (TYPE_NAME (type) == NULL)
5415 ok = true;
5416 else
5417 ok = false;
5419 if (!ok)
5421 pedwarn ("declaration does not declare anything");
5422 return NULL_TREE;
5424 if (pedantic)
5425 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5428 value = grokdeclarator (declarator, declspecs, FIELD, false,
5429 width ? &width : NULL, decl_attrs,
5430 DEPRECATED_NORMAL);
5432 finish_decl (value, NULL_TREE, NULL_TREE);
5433 DECL_INITIAL (value) = width;
5435 return value;
5438 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5439 the list such that this does not present a problem later. */
5441 static void
5442 detect_field_duplicates (tree fieldlist)
5444 tree x, y;
5445 int timeout = 10;
5447 /* First, see if there are more than "a few" fields.
5448 This is trivially true if there are zero or one fields. */
5449 if (!fieldlist)
5450 return;
5451 x = TREE_CHAIN (fieldlist);
5452 if (!x)
5453 return;
5454 do {
5455 timeout--;
5456 x = TREE_CHAIN (x);
5457 } while (timeout > 0 && x);
5459 /* If there were "few" fields, avoid the overhead of allocating
5460 a hash table. Instead just do the nested traversal thing. */
5461 if (timeout > 0)
5463 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5464 if (DECL_NAME (x))
5466 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5467 if (DECL_NAME (y) == DECL_NAME (x))
5469 error ("duplicate member %q+D", x);
5470 DECL_NAME (x) = NULL_TREE;
5474 else
5476 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5477 void **slot;
5479 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5480 if ((y = DECL_NAME (x)) != 0)
5482 slot = htab_find_slot (htab, y, INSERT);
5483 if (*slot)
5485 error ("duplicate member %q+D", x);
5486 DECL_NAME (x) = NULL_TREE;
5488 *slot = y;
5491 htab_delete (htab);
5495 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5496 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5497 ATTRIBUTES are attributes to be applied to the structure. */
5499 tree
5500 finish_struct (tree t, tree fieldlist, tree attributes)
5502 tree x;
5503 bool toplevel = file_scope == current_scope;
5504 int saw_named_field;
5506 /* If this type was previously laid out as a forward reference,
5507 make sure we lay it out again. */
5509 TYPE_SIZE (t) = 0;
5511 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5513 if (pedantic)
5515 for (x = fieldlist; x; x = TREE_CHAIN (x))
5516 if (DECL_NAME (x) != 0)
5517 break;
5519 if (x == 0)
5521 if (TREE_CODE (t) == UNION_TYPE)
5523 if (fieldlist)
5524 pedwarn ("union has no named members");
5525 else
5526 pedwarn ("union has no members");
5528 else
5530 if (fieldlist)
5531 pedwarn ("struct has no named members");
5532 else
5533 pedwarn ("struct has no members");
5538 /* Install struct as DECL_CONTEXT of each field decl.
5539 Also process specified field sizes, found in the DECL_INITIAL,
5540 storing 0 there after the type has been changed to precision equal
5541 to its width, rather than the precision of the specified standard
5542 type. (Correct layout requires the original type to have been preserved
5543 until now.) */
5545 saw_named_field = 0;
5546 for (x = fieldlist; x; x = TREE_CHAIN (x))
5548 if (TREE_TYPE (x) == error_mark_node)
5549 continue;
5551 DECL_CONTEXT (x) = t;
5553 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5554 DECL_PACKED (x) = 1;
5556 /* If any field is const, the structure type is pseudo-const. */
5557 if (TREE_READONLY (x))
5558 C_TYPE_FIELDS_READONLY (t) = 1;
5559 else
5561 /* A field that is pseudo-const makes the structure likewise. */
5562 tree t1 = TREE_TYPE (x);
5563 while (TREE_CODE (t1) == ARRAY_TYPE)
5564 t1 = TREE_TYPE (t1);
5565 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5566 && C_TYPE_FIELDS_READONLY (t1))
5567 C_TYPE_FIELDS_READONLY (t) = 1;
5570 /* Any field that is volatile means variables of this type must be
5571 treated in some ways as volatile. */
5572 if (TREE_THIS_VOLATILE (x))
5573 C_TYPE_FIELDS_VOLATILE (t) = 1;
5575 /* Any field of nominal variable size implies structure is too. */
5576 if (C_DECL_VARIABLE_SIZE (x))
5577 C_TYPE_VARIABLE_SIZE (t) = 1;
5579 if (DECL_INITIAL (x))
5581 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5582 DECL_SIZE (x) = bitsize_int (width);
5583 DECL_BIT_FIELD (x) = 1;
5584 SET_DECL_C_BIT_FIELD (x);
5587 /* Detect flexible array member in an invalid context. */
5588 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5589 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5590 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5591 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5593 if (TREE_CODE (t) == UNION_TYPE)
5595 error ("%Jflexible array member in union", x);
5596 TREE_TYPE (x) = error_mark_node;
5598 else if (TREE_CHAIN (x) != NULL_TREE)
5600 error ("%Jflexible array member not at end of struct", x);
5601 TREE_TYPE (x) = error_mark_node;
5603 else if (!saw_named_field)
5605 error ("%Jflexible array member in otherwise empty struct", x);
5606 TREE_TYPE (x) = error_mark_node;
5610 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5611 && flexible_array_type_p (TREE_TYPE (x)))
5612 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5614 if (DECL_NAME (x))
5615 saw_named_field = 1;
5618 detect_field_duplicates (fieldlist);
5620 /* Now we have the nearly final fieldlist. Record it,
5621 then lay out the structure or union (including the fields). */
5623 TYPE_FIELDS (t) = fieldlist;
5625 layout_type (t);
5627 /* Give bit-fields their proper types. */
5629 tree *fieldlistp = &fieldlist;
5630 while (*fieldlistp)
5631 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5632 && TREE_TYPE (*fieldlistp) != error_mark_node)
5634 unsigned HOST_WIDE_INT width
5635 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5636 tree type = TREE_TYPE (*fieldlistp);
5637 if (width != TYPE_PRECISION (type))
5639 TREE_TYPE (*fieldlistp)
5640 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5641 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5643 DECL_INITIAL (*fieldlistp) = 0;
5645 else
5646 fieldlistp = &TREE_CHAIN (*fieldlistp);
5649 /* Now we have the truly final field list.
5650 Store it in this type and in the variants. */
5652 TYPE_FIELDS (t) = fieldlist;
5654 /* If there are lots of fields, sort so we can look through them fast.
5655 We arbitrarily consider 16 or more elts to be "a lot". */
5658 int len = 0;
5660 for (x = fieldlist; x; x = TREE_CHAIN (x))
5662 if (len > 15 || DECL_NAME (x) == NULL)
5663 break;
5664 len += 1;
5667 if (len > 15)
5669 tree *field_array;
5670 struct lang_type *space;
5671 struct sorted_fields_type *space2;
5673 len += list_length (x);
5675 /* Use the same allocation policy here that make_node uses, to
5676 ensure that this lives as long as the rest of the struct decl.
5677 All decls in an inline function need to be saved. */
5679 space = GGC_CNEW (struct lang_type);
5680 space2 = GGC_NEWVAR (struct sorted_fields_type,
5681 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5683 len = 0;
5684 space->s = space2;
5685 field_array = &space2->elts[0];
5686 for (x = fieldlist; x; x = TREE_CHAIN (x))
5688 field_array[len++] = x;
5690 /* If there is anonymous struct or union, break out of the loop. */
5691 if (DECL_NAME (x) == NULL)
5692 break;
5694 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5695 if (x == NULL)
5697 TYPE_LANG_SPECIFIC (t) = space;
5698 TYPE_LANG_SPECIFIC (t)->s->len = len;
5699 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5700 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5705 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5707 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5708 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5709 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5710 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5711 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5714 /* If this was supposed to be a transparent union, but we can't
5715 make it one, warn and turn off the flag. */
5716 if (TREE_CODE (t) == UNION_TYPE
5717 && TYPE_TRANSPARENT_UNION (t)
5718 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5720 TYPE_TRANSPARENT_UNION (t) = 0;
5721 warning (0, "union cannot be made transparent");
5724 /* If this structure or union completes the type of any previous
5725 variable declaration, lay it out and output its rtl. */
5726 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5728 x = TREE_CHAIN (x))
5730 tree decl = TREE_VALUE (x);
5731 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5732 layout_array_type (TREE_TYPE (decl));
5733 if (TREE_CODE (decl) != TYPE_DECL)
5735 layout_decl (decl, 0);
5736 if (c_dialect_objc ())
5737 objc_check_decl (decl);
5738 rest_of_decl_compilation (decl, toplevel, 0);
5739 if (!toplevel)
5740 expand_decl (decl);
5743 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5745 /* Finish debugging output for this type. */
5746 rest_of_type_compilation (t, toplevel);
5748 /* If we're inside a function proper, i.e. not file-scope and not still
5749 parsing parameters, then arrange for the size of a variable sized type
5750 to be bound now. */
5751 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5752 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5754 return t;
5757 /* Lay out the type T, and its element type, and so on. */
5759 static void
5760 layout_array_type (tree t)
5762 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5763 layout_array_type (TREE_TYPE (t));
5764 layout_type (t);
5767 /* Begin compiling the definition of an enumeration type.
5768 NAME is its name (or null if anonymous).
5769 Returns the type object, as yet incomplete.
5770 Also records info about it so that build_enumerator
5771 may be used to declare the individual values as they are read. */
5773 tree
5774 start_enum (struct c_enum_contents *the_enum, tree name)
5776 tree enumtype = 0;
5778 /* If this is the real definition for a previous forward reference,
5779 fill in the contents in the same object that used to be the
5780 forward reference. */
5782 if (name != 0)
5783 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5785 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5787 enumtype = make_node (ENUMERAL_TYPE);
5788 pushtag (name, enumtype);
5791 if (C_TYPE_BEING_DEFINED (enumtype))
5792 error ("nested redefinition of %<enum %E%>", name);
5794 C_TYPE_BEING_DEFINED (enumtype) = 1;
5796 if (TYPE_VALUES (enumtype) != 0)
5798 /* This enum is a named one that has been declared already. */
5799 error ("redeclaration of %<enum %E%>", name);
5801 /* Completely replace its old definition.
5802 The old enumerators remain defined, however. */
5803 TYPE_VALUES (enumtype) = 0;
5806 the_enum->enum_next_value = integer_zero_node;
5807 the_enum->enum_overflow = 0;
5809 if (flag_short_enums)
5810 TYPE_PACKED (enumtype) = 1;
5812 return enumtype;
5815 /* After processing and defining all the values of an enumeration type,
5816 install their decls in the enumeration type and finish it off.
5817 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5818 and ATTRIBUTES are the specified attributes.
5819 Returns ENUMTYPE. */
5821 tree
5822 finish_enum (tree enumtype, tree values, tree attributes)
5824 tree pair, tem;
5825 tree minnode = 0, maxnode = 0;
5826 int precision, unsign;
5827 bool toplevel = (file_scope == current_scope);
5828 struct lang_type *lt;
5830 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5832 /* Calculate the maximum value of any enumerator in this type. */
5834 if (values == error_mark_node)
5835 minnode = maxnode = integer_zero_node;
5836 else
5838 minnode = maxnode = TREE_VALUE (values);
5839 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5841 tree value = TREE_VALUE (pair);
5842 if (tree_int_cst_lt (maxnode, value))
5843 maxnode = value;
5844 if (tree_int_cst_lt (value, minnode))
5845 minnode = value;
5849 /* Construct the final type of this enumeration. It is the same
5850 as one of the integral types - the narrowest one that fits, except
5851 that normally we only go as narrow as int - and signed iff any of
5852 the values are negative. */
5853 unsign = (tree_int_cst_sgn (minnode) >= 0);
5854 precision = MAX (min_precision (minnode, unsign),
5855 min_precision (maxnode, unsign));
5857 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5859 tem = c_common_type_for_size (precision, unsign);
5860 if (tem == NULL)
5862 warning (0, "enumeration values exceed range of largest integer");
5863 tem = long_long_integer_type_node;
5866 else
5867 tem = unsign ? unsigned_type_node : integer_type_node;
5869 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5870 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5871 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5872 TYPE_SIZE (enumtype) = 0;
5874 /* If the precision of the type was specific with an attribute and it
5875 was too small, give an error. Otherwise, use it. */
5876 if (TYPE_PRECISION (enumtype))
5878 if (precision > TYPE_PRECISION (enumtype))
5879 error ("specified mode too small for enumeral values");
5881 else
5882 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5884 layout_type (enumtype);
5886 if (values != error_mark_node)
5888 /* Change the type of the enumerators to be the enum type. We
5889 need to do this irrespective of the size of the enum, for
5890 proper type checking. Replace the DECL_INITIALs of the
5891 enumerators, and the value slots of the list, with copies
5892 that have the enum type; they cannot be modified in place
5893 because they may be shared (e.g. integer_zero_node) Finally,
5894 change the purpose slots to point to the names of the decls. */
5895 for (pair = values; pair; pair = TREE_CHAIN (pair))
5897 tree enu = TREE_PURPOSE (pair);
5898 tree ini = DECL_INITIAL (enu);
5900 TREE_TYPE (enu) = enumtype;
5902 /* The ISO C Standard mandates enumerators to have type int,
5903 even though the underlying type of an enum type is
5904 unspecified. Here we convert any enumerators that fit in
5905 an int to type int, to avoid promotions to unsigned types
5906 when comparing integers with enumerators that fit in the
5907 int range. When -pedantic is given, build_enumerator()
5908 would have already taken care of those that don't fit. */
5909 if (int_fits_type_p (ini, integer_type_node))
5910 tem = integer_type_node;
5911 else
5912 tem = enumtype;
5913 ini = convert (tem, ini);
5915 DECL_INITIAL (enu) = ini;
5916 TREE_PURPOSE (pair) = DECL_NAME (enu);
5917 TREE_VALUE (pair) = ini;
5920 TYPE_VALUES (enumtype) = values;
5923 /* Record the min/max values so that we can warn about bit-field
5924 enumerations that are too small for the values. */
5925 lt = GGC_CNEW (struct lang_type);
5926 lt->enum_min = minnode;
5927 lt->enum_max = maxnode;
5928 TYPE_LANG_SPECIFIC (enumtype) = lt;
5930 /* Fix up all variant types of this enum type. */
5931 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5933 if (tem == enumtype)
5934 continue;
5935 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5936 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5937 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5938 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5939 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5940 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5941 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5942 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5943 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5944 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5945 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5948 /* Finish debugging output for this type. */
5949 rest_of_type_compilation (enumtype, toplevel);
5951 return enumtype;
5954 /* Build and install a CONST_DECL for one value of the
5955 current enumeration type (one that was begun with start_enum).
5956 Return a tree-list containing the CONST_DECL and its value.
5957 Assignment of sequential values by default is handled here. */
5959 tree
5960 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value)
5962 tree decl, type;
5964 /* Validate and default VALUE. */
5966 if (value != 0)
5968 /* Don't issue more errors for error_mark_node (i.e. an
5969 undeclared identifier) - just ignore the value expression. */
5970 if (value == error_mark_node)
5971 value = 0;
5972 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5973 || TREE_CODE (value) != INTEGER_CST)
5975 error ("enumerator value for %qE is not an integer constant", name);
5976 value = 0;
5978 else
5980 value = default_conversion (value);
5981 constant_expression_warning (value);
5985 /* Default based on previous value. */
5986 /* It should no longer be possible to have NON_LVALUE_EXPR
5987 in the default. */
5988 if (value == 0)
5990 value = the_enum->enum_next_value;
5991 if (the_enum->enum_overflow)
5992 error ("overflow in enumeration values");
5995 if (pedantic && !int_fits_type_p (value, integer_type_node))
5997 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
5998 /* XXX This causes -pedantic to change the meaning of the program.
5999 Remove? -zw 2004-03-15 */
6000 value = convert (integer_type_node, value);
6003 /* Set basis for default for next value. */
6004 the_enum->enum_next_value = build_binary_op (PLUS_EXPR, value,
6005 integer_one_node, 0);
6006 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6008 /* Now create a declaration for the enum value name. */
6010 type = TREE_TYPE (value);
6011 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6012 TYPE_PRECISION (integer_type_node)),
6013 (TYPE_PRECISION (type)
6014 >= TYPE_PRECISION (integer_type_node)
6015 && TYPE_UNSIGNED (type)));
6017 decl = build_decl (CONST_DECL, name, type);
6018 DECL_INITIAL (decl) = convert (type, value);
6019 pushdecl (decl);
6021 return tree_cons (decl, value, NULL_TREE);
6025 /* Create the FUNCTION_DECL for a function definition.
6026 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6027 the declaration; they describe the function's name and the type it returns,
6028 but twisted together in a fashion that parallels the syntax of C.
6030 This function creates a binding context for the function body
6031 as well as setting up the FUNCTION_DECL in current_function_decl.
6033 Returns 1 on success. If the DECLARATOR is not suitable for a function
6034 (it defines a datum instead), we return 0, which tells
6035 yyparse to report a parse error. */
6038 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6039 tree attributes)
6041 tree decl1, old_decl;
6042 tree restype, resdecl;
6043 struct c_label_context_se *nstack_se;
6044 struct c_label_context_vm *nstack_vm;
6046 current_function_returns_value = 0; /* Assume, until we see it does. */
6047 current_function_returns_null = 0;
6048 current_function_returns_abnormally = 0;
6049 warn_about_return_type = 0;
6050 c_switch_stack = NULL;
6052 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6053 nstack_se->labels_def = NULL;
6054 nstack_se->labels_used = NULL;
6055 nstack_se->next = label_context_stack_se;
6056 label_context_stack_se = nstack_se;
6058 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6059 nstack_vm->labels_def = NULL;
6060 nstack_vm->labels_used = NULL;
6061 nstack_vm->scope = 0;
6062 nstack_vm->next = label_context_stack_vm;
6063 label_context_stack_vm = nstack_vm;
6065 /* Indicate no valid break/continue context by setting these variables
6066 to some non-null, non-label value. We'll notice and emit the proper
6067 error message in c_finish_bc_stmt. */
6068 c_break_label = c_cont_label = size_zero_node;
6070 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6071 &attributes, DEPRECATED_NORMAL);
6073 /* If the declarator is not suitable for a function definition,
6074 cause a syntax error. */
6075 if (decl1 == 0)
6077 label_context_stack_se = label_context_stack_se->next;
6078 label_context_stack_vm = label_context_stack_vm->next;
6079 return 0;
6082 decl_attributes (&decl1, attributes, 0);
6084 if (DECL_DECLARED_INLINE_P (decl1)
6085 && DECL_UNINLINABLE (decl1)
6086 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6087 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6088 decl1);
6090 /* Handle gnu_inline attribute. */
6091 if (declspecs->inline_p
6092 && !flag_gnu89_inline
6093 && TREE_CODE (decl1) == FUNCTION_DECL
6094 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6096 if (declspecs->storage_class != csc_static)
6097 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6100 announce_function (decl1);
6102 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6104 error ("return type is an incomplete type");
6105 /* Make it return void instead. */
6106 TREE_TYPE (decl1)
6107 = build_function_type (void_type_node,
6108 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6111 if (warn_about_return_type)
6112 pedwarn_c99 ("return type defaults to %<int%>");
6114 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6115 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6116 DECL_INITIAL (decl1) = error_mark_node;
6118 /* If this definition isn't a prototype and we had a prototype declaration
6119 before, copy the arg type info from that prototype. */
6120 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6121 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6122 old_decl = 0;
6123 current_function_prototype_locus = UNKNOWN_LOCATION;
6124 current_function_prototype_built_in = false;
6125 current_function_prototype_arg_types = NULL_TREE;
6126 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6128 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6129 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6130 TREE_TYPE (TREE_TYPE (old_decl))))
6132 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6133 TREE_TYPE (decl1));
6134 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6135 current_function_prototype_built_in
6136 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6137 current_function_prototype_arg_types
6138 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6140 if (TREE_PUBLIC (decl1))
6142 /* If there is an external prototype declaration of this
6143 function, record its location but do not copy information
6144 to this decl. This may be an invisible declaration
6145 (built-in or in a scope which has finished) or simply
6146 have more refined argument types than any declaration
6147 found above. */
6148 struct c_binding *b;
6149 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6150 if (B_IN_SCOPE (b, external_scope))
6151 break;
6152 if (b)
6154 tree ext_decl, ext_type;
6155 ext_decl = b->decl;
6156 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6157 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6158 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6159 TREE_TYPE (ext_type)))
6161 current_function_prototype_locus
6162 = DECL_SOURCE_LOCATION (ext_decl);
6163 current_function_prototype_built_in
6164 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6165 current_function_prototype_arg_types
6166 = TYPE_ARG_TYPES (ext_type);
6172 /* Optionally warn of old-fashioned def with no previous prototype. */
6173 if (warn_strict_prototypes
6174 && old_decl != error_mark_node
6175 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6176 && C_DECL_ISNT_PROTOTYPE (old_decl))
6177 warning (OPT_Wstrict_prototypes,
6178 "function declaration isn%'t a prototype");
6179 /* Optionally warn of any global def with no previous prototype. */
6180 else if (warn_missing_prototypes
6181 && old_decl != error_mark_node
6182 && TREE_PUBLIC (decl1)
6183 && !MAIN_NAME_P (DECL_NAME (decl1))
6184 && C_DECL_ISNT_PROTOTYPE (old_decl))
6185 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6186 /* Optionally warn of any def with no previous prototype
6187 if the function has already been used. */
6188 else if (warn_missing_prototypes
6189 && old_decl != 0
6190 && old_decl != error_mark_node
6191 && TREE_USED (old_decl)
6192 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6193 warning (OPT_Wmissing_prototypes,
6194 "%q+D was used with no prototype before its definition", decl1);
6195 /* Optionally warn of any global def with no previous declaration. */
6196 else if (warn_missing_declarations
6197 && TREE_PUBLIC (decl1)
6198 && old_decl == 0
6199 && !MAIN_NAME_P (DECL_NAME (decl1)))
6200 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6201 decl1);
6202 /* Optionally warn of any def with no previous declaration
6203 if the function has already been used. */
6204 else if (warn_missing_declarations
6205 && old_decl != 0
6206 && old_decl != error_mark_node
6207 && TREE_USED (old_decl)
6208 && C_DECL_IMPLICIT (old_decl))
6209 warning (OPT_Wmissing_declarations,
6210 "%q+D was used with no declaration before its definition", decl1);
6212 /* This function exists in static storage.
6213 (This does not mean `static' in the C sense!) */
6214 TREE_STATIC (decl1) = 1;
6216 /* A nested function is not global. */
6217 if (current_function_decl != 0)
6218 TREE_PUBLIC (decl1) = 0;
6220 /* This is the earliest point at which we might know the assembler
6221 name of the function. Thus, if it's set before this, die horribly. */
6222 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6224 /* If #pragma weak was used, mark the decl weak now. */
6225 if (current_scope == file_scope)
6226 maybe_apply_pragma_weak (decl1);
6228 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6229 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6231 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6232 != integer_type_node)
6233 pedwarn ("return type of %q+D is not %<int%>", decl1);
6235 check_main_parameter_types(decl1);
6237 if (!TREE_PUBLIC (decl1))
6238 pedwarn ("%q+D is normally a non-static function", decl1);
6241 /* Record the decl so that the function name is defined.
6242 If we already have a decl for this name, and it is a FUNCTION_DECL,
6243 use the old decl. */
6245 current_function_decl = pushdecl (decl1);
6247 push_scope ();
6248 declare_parm_level ();
6250 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6251 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6252 DECL_ARTIFICIAL (resdecl) = 1;
6253 DECL_IGNORED_P (resdecl) = 1;
6254 DECL_RESULT (current_function_decl) = resdecl;
6256 start_fname_decls ();
6258 return 1;
6261 /* Subroutine of store_parm_decls which handles new-style function
6262 definitions (prototype format). The parms already have decls, so we
6263 need only record them as in effect and complain if any redundant
6264 old-style parm decls were written. */
6265 static void
6266 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6268 tree decl;
6270 if (current_scope->bindings)
6272 error ("%Jold-style parameter declarations in prototyped "
6273 "function definition", fndecl);
6275 /* Get rid of the old-style declarations. */
6276 pop_scope ();
6277 push_scope ();
6279 /* Don't issue this warning for nested functions, and don't issue this
6280 warning if we got here because ARG_INFO_TYPES was error_mark_node
6281 (this happens when a function definition has just an ellipsis in
6282 its parameter list). */
6283 else if (!in_system_header && !current_function_scope
6284 && arg_info->types != error_mark_node)
6285 warning (OPT_Wtraditional,
6286 "%Jtraditional C rejects ISO C style function definitions",
6287 fndecl);
6289 /* Now make all the parameter declarations visible in the function body.
6290 We can bypass most of the grunt work of pushdecl. */
6291 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6293 DECL_CONTEXT (decl) = current_function_decl;
6294 if (DECL_NAME (decl))
6296 bind (DECL_NAME (decl), decl, current_scope,
6297 /*invisible=*/false, /*nested=*/false);
6298 if (!TREE_USED (decl))
6299 warn_if_shadowing (decl);
6301 else
6302 error ("%Jparameter name omitted", decl);
6305 /* Record the parameter list in the function declaration. */
6306 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6308 /* Now make all the ancillary declarations visible, likewise. */
6309 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6311 DECL_CONTEXT (decl) = current_function_decl;
6312 if (DECL_NAME (decl))
6313 bind (DECL_NAME (decl), decl, current_scope,
6314 /*invisible=*/false, /*nested=*/false);
6317 /* And all the tag declarations. */
6318 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6319 if (TREE_PURPOSE (decl))
6320 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6321 /*invisible=*/false, /*nested=*/false);
6324 /* Subroutine of store_parm_decls which handles old-style function
6325 definitions (separate parameter list and declarations). */
6327 static void
6328 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6330 struct c_binding *b;
6331 tree parm, decl, last;
6332 tree parmids = arg_info->parms;
6333 struct pointer_set_t *seen_args = pointer_set_create ();
6335 if (!in_system_header)
6336 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6337 fndecl);
6339 /* Match each formal parameter name with its declaration. Save each
6340 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6341 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6343 if (TREE_VALUE (parm) == 0)
6345 error ("%Jparameter name missing from parameter list", fndecl);
6346 TREE_PURPOSE (parm) = 0;
6347 continue;
6350 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6351 if (b && B_IN_CURRENT_SCOPE (b))
6353 decl = b->decl;
6354 /* If we got something other than a PARM_DECL it is an error. */
6355 if (TREE_CODE (decl) != PARM_DECL)
6356 error ("%q+D declared as a non-parameter", decl);
6357 /* If the declaration is already marked, we have a duplicate
6358 name. Complain and ignore the duplicate. */
6359 else if (pointer_set_contains (seen_args, decl))
6361 error ("multiple parameters named %q+D", decl);
6362 TREE_PURPOSE (parm) = 0;
6363 continue;
6365 /* If the declaration says "void", complain and turn it into
6366 an int. */
6367 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6369 error ("parameter %q+D declared with void type", decl);
6370 TREE_TYPE (decl) = integer_type_node;
6371 DECL_ARG_TYPE (decl) = integer_type_node;
6372 layout_decl (decl, 0);
6374 warn_if_shadowing (decl);
6376 /* If no declaration found, default to int. */
6377 else
6379 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6380 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6381 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6382 pushdecl (decl);
6383 warn_if_shadowing (decl);
6385 if (flag_isoc99)
6386 pedwarn ("type of %q+D defaults to %<int%>", decl);
6387 else
6388 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6391 TREE_PURPOSE (parm) = decl;
6392 pointer_set_insert (seen_args, decl);
6395 /* Now examine the parms chain for incomplete declarations
6396 and declarations with no corresponding names. */
6398 for (b = current_scope->bindings; b; b = b->prev)
6400 parm = b->decl;
6401 if (TREE_CODE (parm) != PARM_DECL)
6402 continue;
6404 if (TREE_TYPE (parm) != error_mark_node
6405 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6407 error ("parameter %q+D has incomplete type", parm);
6408 TREE_TYPE (parm) = error_mark_node;
6411 if (!pointer_set_contains (seen_args, parm))
6413 error ("declaration for parameter %q+D but no such parameter", parm);
6415 /* Pretend the parameter was not missing.
6416 This gets us to a standard state and minimizes
6417 further error messages. */
6418 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6422 /* Chain the declarations together in the order of the list of
6423 names. Store that chain in the function decl, replacing the
6424 list of names. Update the current scope to match. */
6425 DECL_ARGUMENTS (fndecl) = 0;
6427 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6428 if (TREE_PURPOSE (parm))
6429 break;
6430 if (parm && TREE_PURPOSE (parm))
6432 last = TREE_PURPOSE (parm);
6433 DECL_ARGUMENTS (fndecl) = last;
6435 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6436 if (TREE_PURPOSE (parm))
6438 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6439 last = TREE_PURPOSE (parm);
6441 TREE_CHAIN (last) = 0;
6444 pointer_set_destroy (seen_args);
6446 /* If there was a previous prototype,
6447 set the DECL_ARG_TYPE of each argument according to
6448 the type previously specified, and report any mismatches. */
6450 if (current_function_prototype_arg_types)
6452 tree type;
6453 for (parm = DECL_ARGUMENTS (fndecl),
6454 type = current_function_prototype_arg_types;
6455 parm || (type && TREE_VALUE (type) != error_mark_node
6456 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6457 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6459 if (parm == 0 || type == 0
6460 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6462 if (current_function_prototype_built_in)
6463 warning (0, "number of arguments doesn%'t match "
6464 "built-in prototype");
6465 else
6467 error ("number of arguments doesn%'t match prototype");
6468 error ("%Hprototype declaration",
6469 &current_function_prototype_locus);
6471 break;
6473 /* Type for passing arg must be consistent with that
6474 declared for the arg. ISO C says we take the unqualified
6475 type for parameters declared with qualified type. */
6476 if (TREE_TYPE (parm) != error_mark_node
6477 && TREE_TYPE (type) != error_mark_node
6478 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6479 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6481 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6482 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6484 /* Adjust argument to match prototype. E.g. a previous
6485 `int foo(float);' prototype causes
6486 `int foo(x) float x; {...}' to be treated like
6487 `int foo(float x) {...}'. This is particularly
6488 useful for argument types like uid_t. */
6489 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6491 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6492 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6493 && TYPE_PRECISION (TREE_TYPE (parm))
6494 < TYPE_PRECISION (integer_type_node))
6495 DECL_ARG_TYPE (parm) = integer_type_node;
6497 if (pedantic)
6499 /* ??? Is it possible to get here with a
6500 built-in prototype or will it always have
6501 been diagnosed as conflicting with an
6502 old-style definition and discarded? */
6503 if (current_function_prototype_built_in)
6504 warning (0, "promoted argument %qD "
6505 "doesn%'t match built-in prototype", parm);
6506 else
6508 pedwarn ("promoted argument %qD "
6509 "doesn%'t match prototype", parm);
6510 pedwarn ("%Hprototype declaration",
6511 &current_function_prototype_locus);
6515 else
6517 if (current_function_prototype_built_in)
6518 warning (0, "argument %qD doesn%'t match "
6519 "built-in prototype", parm);
6520 else
6522 error ("argument %qD doesn%'t match prototype", parm);
6523 error ("%Hprototype declaration",
6524 &current_function_prototype_locus);
6529 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6532 /* Otherwise, create a prototype that would match. */
6534 else
6536 tree actual = 0, last = 0, type;
6538 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6540 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6541 if (last)
6542 TREE_CHAIN (last) = type;
6543 else
6544 actual = type;
6545 last = type;
6547 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6548 if (last)
6549 TREE_CHAIN (last) = type;
6550 else
6551 actual = type;
6553 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6554 of the type of this function, but we need to avoid having this
6555 affect the types of other similarly-typed functions, so we must
6556 first force the generation of an identical (but separate) type
6557 node for the relevant function type. The new node we create
6558 will be a variant of the main variant of the original function
6559 type. */
6561 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6563 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6567 /* Store parameter declarations passed in ARG_INFO into the current
6568 function declaration. */
6570 void
6571 store_parm_decls_from (struct c_arg_info *arg_info)
6573 current_function_arg_info = arg_info;
6574 store_parm_decls ();
6577 /* Store the parameter declarations into the current function declaration.
6578 This is called after parsing the parameter declarations, before
6579 digesting the body of the function.
6581 For an old-style definition, construct a prototype out of the old-style
6582 parameter declarations and inject it into the function's type. */
6584 void
6585 store_parm_decls (void)
6587 tree fndecl = current_function_decl;
6588 bool proto;
6590 /* The argument information block for FNDECL. */
6591 struct c_arg_info *arg_info = current_function_arg_info;
6592 current_function_arg_info = 0;
6594 /* True if this definition is written with a prototype. Note:
6595 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6596 list in a function definition as equivalent to (void) -- an
6597 empty argument list specifies the function has no parameters,
6598 but only (void) sets up a prototype for future calls. */
6599 proto = arg_info->types != 0;
6601 if (proto)
6602 store_parm_decls_newstyle (fndecl, arg_info);
6603 else
6604 store_parm_decls_oldstyle (fndecl, arg_info);
6606 /* The next call to push_scope will be a function body. */
6608 next_is_function_body = true;
6610 /* Write a record describing this function definition to the prototypes
6611 file (if requested). */
6613 gen_aux_info_record (fndecl, 1, 0, proto);
6615 /* Initialize the RTL code for the function. */
6616 allocate_struct_function (fndecl, false);
6618 /* Begin the statement tree for this function. */
6619 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6621 /* ??? Insert the contents of the pending sizes list into the function
6622 to be evaluated. The only reason left to have this is
6623 void foo(int n, int array[n++])
6624 because we throw away the array type in favor of a pointer type, and
6625 thus won't naturally see the SAVE_EXPR containing the increment. All
6626 other pending sizes would be handled by gimplify_parameters. */
6628 tree t;
6629 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6630 add_stmt (TREE_VALUE (t));
6633 /* Even though we're inside a function body, we still don't want to
6634 call expand_expr to calculate the size of a variable-sized array.
6635 We haven't necessarily assigned RTL to all variables yet, so it's
6636 not safe to try to expand expressions involving them. */
6637 cfun->x_dont_save_pending_sizes_p = 1;
6640 /* Emit diagnostics that require gimple input for detection. Operate on
6641 FNDECL and all its nested functions. */
6643 static void
6644 c_gimple_diagnostics_recursively (tree fndecl)
6646 struct cgraph_node *cgn;
6648 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6649 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6651 /* Notice when OpenMP structured block constraints are violated. */
6652 if (flag_openmp)
6653 diagnose_omp_structured_block_errors (fndecl);
6655 /* Finalize all nested functions now. */
6656 cgn = cgraph_node (fndecl);
6657 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6658 c_gimple_diagnostics_recursively (cgn->decl);
6661 /* Finish up a function declaration and compile that function
6662 all the way to assembler language output. The free the storage
6663 for the function definition.
6665 This is called after parsing the body of the function definition. */
6667 void
6668 finish_function (void)
6670 tree fndecl = current_function_decl;
6672 label_context_stack_se = label_context_stack_se->next;
6673 label_context_stack_vm = label_context_stack_vm->next;
6675 if (TREE_CODE (fndecl) == FUNCTION_DECL
6676 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6678 tree args = DECL_ARGUMENTS (fndecl);
6679 for (; args; args = TREE_CHAIN (args))
6681 tree type = TREE_TYPE (args);
6682 if (INTEGRAL_TYPE_P (type)
6683 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6684 DECL_ARG_TYPE (args) = integer_type_node;
6688 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6689 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6691 /* Must mark the RESULT_DECL as being in this function. */
6693 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6694 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6696 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6698 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6699 != integer_type_node)
6701 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6702 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6703 if (!warn_main)
6704 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6706 else
6708 if (flag_isoc99)
6710 tree stmt = c_finish_return (integer_zero_node);
6711 /* Hack. We don't want the middle-end to warn that this return
6712 is unreachable, so we mark its location as special. Using
6713 UNKNOWN_LOCATION has the problem that it gets clobbered in
6714 annotate_one_with_locus. A cleaner solution might be to
6715 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6717 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6722 /* Tie off the statement tree for this function. */
6723 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6725 finish_fname_decls ();
6727 /* Complain if there's just no return statement. */
6728 if (warn_return_type
6729 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6730 && !current_function_returns_value && !current_function_returns_null
6731 /* Don't complain if we are no-return. */
6732 && !current_function_returns_abnormally
6733 /* Don't warn for main(). */
6734 && !MAIN_NAME_P (DECL_NAME (fndecl))
6735 /* Or if they didn't actually specify a return type. */
6736 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6737 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6738 inline function, as we might never be compiled separately. */
6739 && DECL_INLINE (fndecl))
6741 warning (OPT_Wreturn_type,
6742 "no return statement in function returning non-void");
6743 TREE_NO_WARNING (fndecl) = 1;
6746 /* Store the end of the function, so that we get good line number
6747 info for the epilogue. */
6748 cfun->function_end_locus = input_location;
6750 /* Finalize the ELF visibility for the function. */
6751 c_determine_visibility (fndecl);
6753 /* For GNU C extern inline functions disregard inline limits. */
6754 if (DECL_EXTERNAL (fndecl)
6755 && DECL_DECLARED_INLINE_P (fndecl))
6756 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6758 /* Genericize before inlining. Delay genericizing nested functions
6759 until their parent function is genericized. Since finalizing
6760 requires GENERIC, delay that as well. */
6762 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6763 && !undef_nested_function)
6765 if (!decl_function_context (fndecl))
6767 c_genericize (fndecl);
6768 c_gimple_diagnostics_recursively (fndecl);
6770 /* ??? Objc emits functions after finalizing the compilation unit.
6771 This should be cleaned up later and this conditional removed. */
6772 if (cgraph_global_info_ready)
6774 cgraph_add_new_function (fndecl, false);
6775 return;
6778 cgraph_finalize_function (fndecl, false);
6780 else
6782 /* Register this function with cgraph just far enough to get it
6783 added to our parent's nested function list. Handy, since the
6784 C front end doesn't have such a list. */
6785 (void) cgraph_node (fndecl);
6789 if (!decl_function_context (fndecl))
6790 undef_nested_function = false;
6792 /* We're leaving the context of this function, so zap cfun.
6793 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6794 tree_rest_of_compilation. */
6795 set_cfun (NULL);
6796 current_function_decl = NULL;
6799 /* Check the declarations given in a for-loop for satisfying the C99
6800 constraints. If exactly one such decl is found, return it. */
6802 tree
6803 check_for_loop_decls (void)
6805 struct c_binding *b;
6806 tree one_decl = NULL_TREE;
6807 int n_decls = 0;
6810 if (!flag_isoc99)
6812 /* If we get here, declarations have been used in a for loop without
6813 the C99 for loop scope. This doesn't make much sense, so don't
6814 allow it. */
6815 error ("%<for%> loop initial declaration used outside C99 mode");
6816 return NULL_TREE;
6818 /* C99 subclause 6.8.5 paragraph 3:
6820 [#3] The declaration part of a for statement shall only
6821 declare identifiers for objects having storage class auto or
6822 register.
6824 It isn't clear whether, in this sentence, "identifiers" binds to
6825 "shall only declare" or to "objects" - that is, whether all identifiers
6826 declared must be identifiers for objects, or whether the restriction
6827 only applies to those that are. (A question on this in comp.std.c
6828 in November 2000 received no answer.) We implement the strictest
6829 interpretation, to avoid creating an extension which later causes
6830 problems. */
6832 for (b = current_scope->bindings; b; b = b->prev)
6834 tree id = b->id;
6835 tree decl = b->decl;
6837 if (!id)
6838 continue;
6840 switch (TREE_CODE (decl))
6842 case VAR_DECL:
6843 if (TREE_STATIC (decl))
6844 error ("declaration of static variable %q+D in %<for%> loop "
6845 "initial declaration", decl);
6846 else if (DECL_EXTERNAL (decl))
6847 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6848 "initial declaration", decl);
6849 break;
6851 case RECORD_TYPE:
6852 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6853 id);
6854 break;
6855 case UNION_TYPE:
6856 error ("%<union %E%> declared in %<for%> loop initial declaration",
6857 id);
6858 break;
6859 case ENUMERAL_TYPE:
6860 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6861 id);
6862 break;
6863 default:
6864 error ("declaration of non-variable %q+D in %<for%> loop "
6865 "initial declaration", decl);
6868 n_decls++;
6869 one_decl = decl;
6872 return n_decls == 1 ? one_decl : NULL_TREE;
6875 /* Save and reinitialize the variables
6876 used during compilation of a C function. */
6878 void
6879 c_push_function_context (void)
6881 struct language_function *p;
6882 p = GGC_NEW (struct language_function);
6883 cfun->language = p;
6885 p->base.x_stmt_tree = c_stmt_tree;
6886 p->x_break_label = c_break_label;
6887 p->x_cont_label = c_cont_label;
6888 p->x_switch_stack = c_switch_stack;
6889 p->arg_info = current_function_arg_info;
6890 p->returns_value = current_function_returns_value;
6891 p->returns_null = current_function_returns_null;
6892 p->returns_abnormally = current_function_returns_abnormally;
6893 p->warn_about_return_type = warn_about_return_type;
6895 push_function_context ();
6898 /* Restore the variables used during compilation of a C function. */
6900 void
6901 c_pop_function_context (void)
6903 struct language_function *p;
6905 pop_function_context ();
6906 p = cfun->language;
6907 cfun->language = NULL;
6909 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6910 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6912 /* Stop pointing to the local nodes about to be freed. */
6913 /* But DECL_INITIAL must remain nonzero so we know this
6914 was an actual function definition. */
6915 DECL_INITIAL (current_function_decl) = error_mark_node;
6916 DECL_ARGUMENTS (current_function_decl) = 0;
6919 c_stmt_tree = p->base.x_stmt_tree;
6920 c_break_label = p->x_break_label;
6921 c_cont_label = p->x_cont_label;
6922 c_switch_stack = p->x_switch_stack;
6923 current_function_arg_info = p->arg_info;
6924 current_function_returns_value = p->returns_value;
6925 current_function_returns_null = p->returns_null;
6926 current_function_returns_abnormally = p->returns_abnormally;
6927 warn_about_return_type = p->warn_about_return_type;
6930 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6932 void
6933 c_dup_lang_specific_decl (tree decl)
6935 struct lang_decl *ld;
6937 if (!DECL_LANG_SPECIFIC (decl))
6938 return;
6940 ld = GGC_NEW (struct lang_decl);
6941 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6942 DECL_LANG_SPECIFIC (decl) = ld;
6945 /* The functions below are required for functionality of doing
6946 function at once processing in the C front end. Currently these
6947 functions are not called from anywhere in the C front end, but as
6948 these changes continue, that will change. */
6950 /* Returns the stmt_tree (if any) to which statements are currently
6951 being added. If there is no active statement-tree, NULL is
6952 returned. */
6954 stmt_tree
6955 current_stmt_tree (void)
6957 return &c_stmt_tree;
6960 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6961 C. */
6964 anon_aggr_type_p (const_tree ARG_UNUSED (node))
6966 return 0;
6969 /* Return the global value of T as a symbol. */
6971 tree
6972 identifier_global_value (tree t)
6974 struct c_binding *b;
6976 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6977 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6978 return b->decl;
6980 return 0;
6983 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6984 otherwise the name is found in ridpointers from RID_INDEX. */
6986 void
6987 record_builtin_type (enum rid rid_index, const char *name, tree type)
6989 tree id, decl;
6990 if (name == 0)
6991 id = ridpointers[(int) rid_index];
6992 else
6993 id = get_identifier (name);
6994 decl = build_decl (TYPE_DECL, id, type);
6995 pushdecl (decl);
6996 if (debug_hooks->type_decl)
6997 debug_hooks->type_decl (decl, false);
7000 /* Build the void_list_node (void_type_node having been created). */
7001 tree
7002 build_void_list_node (void)
7004 tree t = build_tree_list (NULL_TREE, void_type_node);
7005 return t;
7008 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7010 struct c_parm *
7011 build_c_parm (struct c_declspecs *specs, tree attrs,
7012 struct c_declarator *declarator)
7014 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7015 ret->specs = specs;
7016 ret->attrs = attrs;
7017 ret->declarator = declarator;
7018 return ret;
7021 /* Return a declarator with nested attributes. TARGET is the inner
7022 declarator to which these attributes apply. ATTRS are the
7023 attributes. */
7025 struct c_declarator *
7026 build_attrs_declarator (tree attrs, struct c_declarator *target)
7028 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7029 ret->kind = cdk_attrs;
7030 ret->declarator = target;
7031 ret->u.attrs = attrs;
7032 return ret;
7035 /* Return a declarator for a function with arguments specified by ARGS
7036 and return type specified by TARGET. */
7038 struct c_declarator *
7039 build_function_declarator (struct c_arg_info *args,
7040 struct c_declarator *target)
7042 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7043 ret->kind = cdk_function;
7044 ret->declarator = target;
7045 ret->u.arg_info = args;
7046 return ret;
7049 /* Return a declarator for the identifier IDENT (which may be
7050 NULL_TREE for an abstract declarator). */
7052 struct c_declarator *
7053 build_id_declarator (tree ident)
7055 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7056 ret->kind = cdk_id;
7057 ret->declarator = 0;
7058 ret->u.id = ident;
7059 /* Default value - may get reset to a more precise location. */
7060 ret->id_loc = input_location;
7061 return ret;
7064 /* Return something to represent absolute declarators containing a *.
7065 TARGET is the absolute declarator that the * contains.
7066 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7067 to apply to the pointer type. */
7069 struct c_declarator *
7070 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7071 struct c_declarator *target)
7073 tree attrs;
7074 int quals = 0;
7075 struct c_declarator *itarget = target;
7076 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7077 if (type_quals_attrs)
7079 attrs = type_quals_attrs->attrs;
7080 quals = quals_from_declspecs (type_quals_attrs);
7081 if (attrs != NULL_TREE)
7082 itarget = build_attrs_declarator (attrs, target);
7084 ret->kind = cdk_pointer;
7085 ret->declarator = itarget;
7086 ret->u.pointer_quals = quals;
7087 return ret;
7090 /* Return a pointer to a structure for an empty list of declaration
7091 specifiers. */
7093 struct c_declspecs *
7094 build_null_declspecs (void)
7096 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7097 ret->type = 0;
7098 ret->decl_attr = 0;
7099 ret->attrs = 0;
7100 ret->typespec_word = cts_none;
7101 ret->storage_class = csc_none;
7102 ret->declspecs_seen_p = false;
7103 ret->type_seen_p = false;
7104 ret->non_sc_seen_p = false;
7105 ret->typedef_p = false;
7106 ret->tag_defined_p = false;
7107 ret->explicit_signed_p = false;
7108 ret->deprecated_p = false;
7109 ret->default_int_p = false;
7110 ret->long_p = false;
7111 ret->long_long_p = false;
7112 ret->short_p = false;
7113 ret->signed_p = false;
7114 ret->unsigned_p = false;
7115 ret->complex_p = false;
7116 ret->inline_p = false;
7117 ret->thread_p = false;
7118 ret->const_p = false;
7119 ret->volatile_p = false;
7120 ret->restrict_p = false;
7121 ret->saturating_p = false;
7122 return ret;
7125 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7126 returning SPECS. */
7128 struct c_declspecs *
7129 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7131 enum rid i;
7132 bool dupe = false;
7133 specs->non_sc_seen_p = true;
7134 specs->declspecs_seen_p = true;
7135 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7136 && C_IS_RESERVED_WORD (qual));
7137 i = C_RID_CODE (qual);
7138 switch (i)
7140 case RID_CONST:
7141 dupe = specs->const_p;
7142 specs->const_p = true;
7143 break;
7144 case RID_VOLATILE:
7145 dupe = specs->volatile_p;
7146 specs->volatile_p = true;
7147 break;
7148 case RID_RESTRICT:
7149 dupe = specs->restrict_p;
7150 specs->restrict_p = true;
7151 break;
7152 default:
7153 gcc_unreachable ();
7155 if (dupe && pedantic && !flag_isoc99)
7156 pedwarn ("duplicate %qE", qual);
7157 return specs;
7160 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7161 returning SPECS. */
7163 struct c_declspecs *
7164 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7166 tree type = spec.spec;
7167 specs->non_sc_seen_p = true;
7168 specs->declspecs_seen_p = true;
7169 specs->type_seen_p = true;
7170 if (TREE_DEPRECATED (type))
7171 specs->deprecated_p = true;
7173 /* Handle type specifier keywords. */
7174 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7176 enum rid i = C_RID_CODE (type);
7177 if (specs->type)
7179 error ("two or more data types in declaration specifiers");
7180 return specs;
7182 if ((int) i <= (int) RID_LAST_MODIFIER)
7184 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7185 bool dupe = false;
7186 switch (i)
7188 case RID_LONG:
7189 if (specs->long_long_p)
7191 error ("%<long long long%> is too long for GCC");
7192 break;
7194 if (specs->long_p)
7196 if (specs->typespec_word == cts_double)
7198 error ("both %<long long%> and %<double%> in "
7199 "declaration specifiers");
7200 break;
7202 if (pedantic && !flag_isoc99 && !in_system_header
7203 && warn_long_long)
7204 pedwarn ("ISO C90 does not support %<long long%>");
7205 specs->long_long_p = 1;
7206 break;
7208 if (specs->short_p)
7209 error ("both %<long%> and %<short%> in "
7210 "declaration specifiers");
7211 else if (specs->typespec_word == cts_void)
7212 error ("both %<long%> and %<void%> in "
7213 "declaration specifiers");
7214 else if (specs->typespec_word == cts_bool)
7215 error ("both %<long%> and %<_Bool%> in "
7216 "declaration specifiers");
7217 else if (specs->typespec_word == cts_char)
7218 error ("both %<long%> and %<char%> in "
7219 "declaration specifiers");
7220 else if (specs->typespec_word == cts_float)
7221 error ("both %<long%> and %<float%> in "
7222 "declaration specifiers");
7223 else if (specs->typespec_word == cts_dfloat32)
7224 error ("both %<long%> and %<_Decimal32%> in "
7225 "declaration specifiers");
7226 else if (specs->typespec_word == cts_dfloat64)
7227 error ("both %<long%> and %<_Decimal64%> in "
7228 "declaration specifiers");
7229 else if (specs->typespec_word == cts_dfloat128)
7230 error ("both %<long%> and %<_Decimal128%> in "
7231 "declaration specifiers");
7232 else
7233 specs->long_p = true;
7234 break;
7235 case RID_SHORT:
7236 dupe = specs->short_p;
7237 if (specs->long_p)
7238 error ("both %<long%> and %<short%> in "
7239 "declaration specifiers");
7240 else if (specs->typespec_word == cts_void)
7241 error ("both %<short%> and %<void%> in "
7242 "declaration specifiers");
7243 else if (specs->typespec_word == cts_bool)
7244 error ("both %<short%> and %<_Bool%> in "
7245 "declaration specifiers");
7246 else if (specs->typespec_word == cts_char)
7247 error ("both %<short%> and %<char%> in "
7248 "declaration specifiers");
7249 else if (specs->typespec_word == cts_float)
7250 error ("both %<short%> and %<float%> in "
7251 "declaration specifiers");
7252 else if (specs->typespec_word == cts_double)
7253 error ("both %<short%> and %<double%> in "
7254 "declaration specifiers");
7255 else if (specs->typespec_word == cts_dfloat32)
7256 error ("both %<short%> and %<_Decimal32%> in "
7257 "declaration specifiers");
7258 else if (specs->typespec_word == cts_dfloat64)
7259 error ("both %<short%> and %<_Decimal64%> in "
7260 "declaration specifiers");
7261 else if (specs->typespec_word == cts_dfloat128)
7262 error ("both %<short%> and %<_Decimal128%> in "
7263 "declaration specifiers");
7264 else
7265 specs->short_p = true;
7266 break;
7267 case RID_SIGNED:
7268 dupe = specs->signed_p;
7269 if (specs->unsigned_p)
7270 error ("both %<signed%> and %<unsigned%> in "
7271 "declaration specifiers");
7272 else if (specs->typespec_word == cts_void)
7273 error ("both %<signed%> and %<void%> in "
7274 "declaration specifiers");
7275 else if (specs->typespec_word == cts_bool)
7276 error ("both %<signed%> and %<_Bool%> in "
7277 "declaration specifiers");
7278 else if (specs->typespec_word == cts_float)
7279 error ("both %<signed%> and %<float%> in "
7280 "declaration specifiers");
7281 else if (specs->typespec_word == cts_double)
7282 error ("both %<signed%> and %<double%> in "
7283 "declaration specifiers");
7284 else if (specs->typespec_word == cts_dfloat32)
7285 error ("both %<signed%> and %<_Decimal32%> in "
7286 "declaration specifiers");
7287 else if (specs->typespec_word == cts_dfloat64)
7288 error ("both %<signed%> and %<_Decimal64%> in "
7289 "declaration specifiers");
7290 else if (specs->typespec_word == cts_dfloat128)
7291 error ("both %<signed%> and %<_Decimal128%> in "
7292 "declaration specifiers");
7293 else
7294 specs->signed_p = true;
7295 break;
7296 case RID_UNSIGNED:
7297 dupe = specs->unsigned_p;
7298 if (specs->signed_p)
7299 error ("both %<signed%> and %<unsigned%> in "
7300 "declaration specifiers");
7301 else if (specs->typespec_word == cts_void)
7302 error ("both %<unsigned%> and %<void%> in "
7303 "declaration specifiers");
7304 else if (specs->typespec_word == cts_bool)
7305 error ("both %<unsigned%> and %<_Bool%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_float)
7308 error ("both %<unsigned%> and %<float%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_double)
7311 error ("both %<unsigned%> and %<double%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_dfloat32)
7314 error ("both %<unsigned%> and %<_Decimal32%> in "
7315 "declaration specifiers");
7316 else if (specs->typespec_word == cts_dfloat64)
7317 error ("both %<unsigned%> and %<_Decimal64%> in "
7318 "declaration specifiers");
7319 else if (specs->typespec_word == cts_dfloat128)
7320 error ("both %<unsigned%> and %<_Decimal128%> in "
7321 "declaration specifiers");
7322 else
7323 specs->unsigned_p = true;
7324 break;
7325 case RID_COMPLEX:
7326 dupe = specs->complex_p;
7327 if (pedantic && !flag_isoc99 && !in_system_header)
7328 pedwarn ("ISO C90 does not support complex types");
7329 if (specs->typespec_word == cts_void)
7330 error ("both %<complex%> and %<void%> in "
7331 "declaration specifiers");
7332 else if (specs->typespec_word == cts_bool)
7333 error ("both %<complex%> and %<_Bool%> in "
7334 "declaration specifiers");
7335 else if (specs->typespec_word == cts_dfloat32)
7336 error ("both %<complex%> and %<_Decimal32%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_dfloat64)
7339 error ("both %<complex%> and %<_Decimal64%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_dfloat128)
7342 error ("both %<complex%> and %<_Decimal128%> in "
7343 "declaration specifiers");
7344 else if (specs->typespec_word == cts_fract)
7345 error ("both %<complex%> and %<_Fract%> in "
7346 "declaration specifiers");
7347 else if (specs->typespec_word == cts_accum)
7348 error ("both %<complex%> and %<_Accum%> in "
7349 "declaration specifiers");
7350 else if (specs->saturating_p)
7351 error ("both %<complex%> and %<_Sat%> in "
7352 "declaration specifiers");
7353 else
7354 specs->complex_p = true;
7355 break;
7356 case RID_SAT:
7357 dupe = specs->saturating_p;
7358 if (pedantic)
7359 pedwarn ("ISO C does not support saturating types");
7360 if (specs->typespec_word == cts_void)
7361 error ("both %<_Sat%> and %<void%> in "
7362 "declaration specifiers");
7363 else if (specs->typespec_word == cts_bool)
7364 error ("both %<_Sat%> and %<_Bool%> in "
7365 "declaration specifiers");
7366 else if (specs->typespec_word == cts_char)
7367 error ("both %<_Sat%> and %<char%> in "
7368 "declaration specifiers");
7369 else if (specs->typespec_word == cts_int)
7370 error ("both %<_Sat%> and %<int%> in "
7371 "declaration specifiers");
7372 else if (specs->typespec_word == cts_float)
7373 error ("both %<_Sat%> and %<float%> in "
7374 "declaration specifiers");
7375 else if (specs->typespec_word == cts_double)
7376 error ("both %<_Sat%> and %<double%> in "
7377 "declaration specifiers");
7378 else if (specs->typespec_word == cts_dfloat32)
7379 error ("both %<_Sat%> and %<_Decimal32%> in "
7380 "declaration specifiers");
7381 else if (specs->typespec_word == cts_dfloat64)
7382 error ("both %<_Sat%> and %<_Decimal64%> in "
7383 "declaration specifiers");
7384 else if (specs->typespec_word == cts_dfloat128)
7385 error ("both %<_Sat%> and %<_Decimal128%> in "
7386 "declaration specifiers");
7387 else if (specs->complex_p)
7388 error ("both %<_Sat%> and %<complex%> in "
7389 "declaration specifiers");
7390 else
7391 specs->saturating_p = true;
7392 break;
7393 default:
7394 gcc_unreachable ();
7397 if (dupe)
7398 error ("duplicate %qE", type);
7400 return specs;
7402 else
7404 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7405 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7406 if (specs->typespec_word != cts_none)
7408 error ("two or more data types in declaration specifiers");
7409 return specs;
7411 switch (i)
7413 case RID_VOID:
7414 if (specs->long_p)
7415 error ("both %<long%> and %<void%> in "
7416 "declaration specifiers");
7417 else if (specs->short_p)
7418 error ("both %<short%> and %<void%> in "
7419 "declaration specifiers");
7420 else if (specs->signed_p)
7421 error ("both %<signed%> and %<void%> in "
7422 "declaration specifiers");
7423 else if (specs->unsigned_p)
7424 error ("both %<unsigned%> and %<void%> in "
7425 "declaration specifiers");
7426 else if (specs->complex_p)
7427 error ("both %<complex%> and %<void%> in "
7428 "declaration specifiers");
7429 else if (specs->saturating_p)
7430 error ("both %<_Sat%> and %<void%> in "
7431 "declaration specifiers");
7432 else
7433 specs->typespec_word = cts_void;
7434 return specs;
7435 case RID_BOOL:
7436 if (specs->long_p)
7437 error ("both %<long%> and %<_Bool%> in "
7438 "declaration specifiers");
7439 else if (specs->short_p)
7440 error ("both %<short%> and %<_Bool%> in "
7441 "declaration specifiers");
7442 else if (specs->signed_p)
7443 error ("both %<signed%> and %<_Bool%> in "
7444 "declaration specifiers");
7445 else if (specs->unsigned_p)
7446 error ("both %<unsigned%> and %<_Bool%> in "
7447 "declaration specifiers");
7448 else if (specs->complex_p)
7449 error ("both %<complex%> and %<_Bool%> in "
7450 "declaration specifiers");
7451 else if (specs->saturating_p)
7452 error ("both %<_Sat%> and %<_Bool%> in "
7453 "declaration specifiers");
7454 else
7455 specs->typespec_word = cts_bool;
7456 return specs;
7457 case RID_CHAR:
7458 if (specs->long_p)
7459 error ("both %<long%> and %<char%> in "
7460 "declaration specifiers");
7461 else if (specs->short_p)
7462 error ("both %<short%> and %<char%> in "
7463 "declaration specifiers");
7464 else if (specs->saturating_p)
7465 error ("both %<_Sat%> and %<char%> in "
7466 "declaration specifiers");
7467 else
7468 specs->typespec_word = cts_char;
7469 return specs;
7470 case RID_INT:
7471 if (specs->saturating_p)
7472 error ("both %<_Sat%> and %<int%> in "
7473 "declaration specifiers");
7474 else
7475 specs->typespec_word = cts_int;
7476 return specs;
7477 case RID_FLOAT:
7478 if (specs->long_p)
7479 error ("both %<long%> and %<float%> in "
7480 "declaration specifiers");
7481 else if (specs->short_p)
7482 error ("both %<short%> and %<float%> in "
7483 "declaration specifiers");
7484 else if (specs->signed_p)
7485 error ("both %<signed%> and %<float%> in "
7486 "declaration specifiers");
7487 else if (specs->unsigned_p)
7488 error ("both %<unsigned%> and %<float%> in "
7489 "declaration specifiers");
7490 else if (specs->saturating_p)
7491 error ("both %<_Sat%> and %<float%> in "
7492 "declaration specifiers");
7493 else
7494 specs->typespec_word = cts_float;
7495 return specs;
7496 case RID_DOUBLE:
7497 if (specs->long_long_p)
7498 error ("both %<long long%> and %<double%> in "
7499 "declaration specifiers");
7500 else if (specs->short_p)
7501 error ("both %<short%> and %<double%> in "
7502 "declaration specifiers");
7503 else if (specs->signed_p)
7504 error ("both %<signed%> and %<double%> in "
7505 "declaration specifiers");
7506 else if (specs->unsigned_p)
7507 error ("both %<unsigned%> and %<double%> in "
7508 "declaration specifiers");
7509 else if (specs->saturating_p)
7510 error ("both %<_Sat%> and %<double%> in "
7511 "declaration specifiers");
7512 else
7513 specs->typespec_word = cts_double;
7514 return specs;
7515 case RID_DFLOAT32:
7516 case RID_DFLOAT64:
7517 case RID_DFLOAT128:
7519 const char *str;
7520 if (i == RID_DFLOAT32)
7521 str = "_Decimal32";
7522 else if (i == RID_DFLOAT64)
7523 str = "_Decimal64";
7524 else
7525 str = "_Decimal128";
7526 if (specs->long_long_p)
7527 error ("both %<long long%> and %<%s%> in "
7528 "declaration specifiers", str);
7529 if (specs->long_p)
7530 error ("both %<long%> and %<%s%> in "
7531 "declaration specifiers", str);
7532 else if (specs->short_p)
7533 error ("both %<short%> and %<%s%> in "
7534 "declaration specifiers", str);
7535 else if (specs->signed_p)
7536 error ("both %<signed%> and %<%s%> in "
7537 "declaration specifiers", str);
7538 else if (specs->unsigned_p)
7539 error ("both %<unsigned%> and %<%s%> in "
7540 "declaration specifiers", str);
7541 else if (specs->complex_p)
7542 error ("both %<complex%> and %<%s%> in "
7543 "declaration specifiers", str);
7544 else if (specs->saturating_p)
7545 error ("both %<_Sat%> and %<%s%> in "
7546 "declaration specifiers", str);
7547 else if (i == RID_DFLOAT32)
7548 specs->typespec_word = cts_dfloat32;
7549 else if (i == RID_DFLOAT64)
7550 specs->typespec_word = cts_dfloat64;
7551 else
7552 specs->typespec_word = cts_dfloat128;
7554 if (!targetm.decimal_float_supported_p ())
7555 error ("decimal floating point not supported for this target");
7556 if (pedantic)
7557 pedwarn ("ISO C does not support decimal floating point");
7558 return specs;
7559 case RID_FRACT:
7560 case RID_ACCUM:
7562 const char *str;
7563 if (i == RID_FRACT)
7564 str = "_Fract";
7565 else
7566 str = "_Accum";
7567 if (specs->complex_p)
7568 error ("both %<complex%> and %<%s%> in "
7569 "declaration specifiers", str);
7570 else if (i == RID_FRACT)
7571 specs->typespec_word = cts_fract;
7572 else
7573 specs->typespec_word = cts_accum;
7575 if (!targetm.fixed_point_supported_p ())
7576 error ("fixed-point types not supported for this target");
7577 if (pedantic)
7578 pedwarn ("ISO C does not support fixed-point types");
7579 return specs;
7580 default:
7581 /* ObjC reserved word "id", handled below. */
7582 break;
7587 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7588 form of ObjC type, cases such as "int" and "long" being handled
7589 above), a TYPE (struct, union, enum and typeof specifiers) or an
7590 ERROR_MARK. In none of these cases may there have previously
7591 been any type specifiers. */
7592 if (specs->type || specs->typespec_word != cts_none
7593 || specs->long_p || specs->short_p || specs->signed_p
7594 || specs->unsigned_p || specs->complex_p)
7595 error ("two or more data types in declaration specifiers");
7596 else if (TREE_CODE (type) == TYPE_DECL)
7598 if (TREE_TYPE (type) == error_mark_node)
7599 ; /* Allow the type to default to int to avoid cascading errors. */
7600 else
7602 specs->type = TREE_TYPE (type);
7603 specs->decl_attr = DECL_ATTRIBUTES (type);
7604 specs->typedef_p = true;
7605 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7608 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7610 tree t = lookup_name (type);
7611 if (!t || TREE_CODE (t) != TYPE_DECL)
7612 error ("%qE fails to be a typedef or built in type", type);
7613 else if (TREE_TYPE (t) == error_mark_node)
7615 else
7616 specs->type = TREE_TYPE (t);
7618 else if (TREE_CODE (type) != ERROR_MARK)
7620 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7621 specs->tag_defined_p = true;
7622 if (spec.kind == ctsk_typeof)
7623 specs->typedef_p = true;
7624 specs->type = type;
7627 return specs;
7630 /* Add the storage class specifier or function specifier SCSPEC to the
7631 declaration specifiers SPECS, returning SPECS. */
7633 struct c_declspecs *
7634 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7636 enum rid i;
7637 enum c_storage_class n = csc_none;
7638 bool dupe = false;
7639 specs->declspecs_seen_p = true;
7640 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7641 && C_IS_RESERVED_WORD (scspec));
7642 i = C_RID_CODE (scspec);
7643 if (specs->non_sc_seen_p)
7644 warning (OPT_Wold_style_declaration,
7645 "%qE is not at beginning of declaration", scspec);
7646 switch (i)
7648 case RID_INLINE:
7649 /* C99 permits duplicate inline. Although of doubtful utility,
7650 it seems simplest to permit it in gnu89 mode as well, as
7651 there is also little utility in maintaining this as a
7652 difference between gnu89 and C99 inline. */
7653 dupe = false;
7654 specs->inline_p = true;
7655 break;
7656 case RID_THREAD:
7657 dupe = specs->thread_p;
7658 if (specs->storage_class == csc_auto)
7659 error ("%<__thread%> used with %<auto%>");
7660 else if (specs->storage_class == csc_register)
7661 error ("%<__thread%> used with %<register%>");
7662 else if (specs->storage_class == csc_typedef)
7663 error ("%<__thread%> used with %<typedef%>");
7664 else
7665 specs->thread_p = true;
7666 break;
7667 case RID_AUTO:
7668 n = csc_auto;
7669 break;
7670 case RID_EXTERN:
7671 n = csc_extern;
7672 /* Diagnose "__thread extern". */
7673 if (specs->thread_p)
7674 error ("%<__thread%> before %<extern%>");
7675 break;
7676 case RID_REGISTER:
7677 n = csc_register;
7678 break;
7679 case RID_STATIC:
7680 n = csc_static;
7681 /* Diagnose "__thread static". */
7682 if (specs->thread_p)
7683 error ("%<__thread%> before %<static%>");
7684 break;
7685 case RID_TYPEDEF:
7686 n = csc_typedef;
7687 break;
7688 default:
7689 gcc_unreachable ();
7691 if (n != csc_none && n == specs->storage_class)
7692 dupe = true;
7693 if (dupe)
7694 error ("duplicate %qE", scspec);
7695 if (n != csc_none)
7697 if (specs->storage_class != csc_none && n != specs->storage_class)
7699 error ("multiple storage classes in declaration specifiers");
7701 else
7703 specs->storage_class = n;
7704 if (n != csc_extern && n != csc_static && specs->thread_p)
7706 error ("%<__thread%> used with %qE", scspec);
7707 specs->thread_p = false;
7711 return specs;
7714 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7715 returning SPECS. */
7717 struct c_declspecs *
7718 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7720 specs->attrs = chainon (attrs, specs->attrs);
7721 specs->declspecs_seen_p = true;
7722 return specs;
7725 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7726 specifiers with any other type specifier to determine the resulting
7727 type. This is where ISO C checks on complex types are made, since
7728 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7729 double". */
7731 struct c_declspecs *
7732 finish_declspecs (struct c_declspecs *specs)
7734 /* If a type was specified as a whole, we have no modifiers and are
7735 done. */
7736 if (specs->type != NULL_TREE)
7738 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7739 && !specs->signed_p && !specs->unsigned_p
7740 && !specs->complex_p);
7741 return specs;
7744 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7745 has been specified, treat it as "int" unless "_Complex" is
7746 present and there are no other specifiers. If we just have
7747 "_Complex", it is equivalent to "_Complex double", but e.g.
7748 "_Complex short" is equivalent to "_Complex short int". */
7749 if (specs->typespec_word == cts_none)
7751 if (specs->saturating_p)
7753 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7754 specs->typespec_word = cts_fract;
7756 else if (specs->long_p || specs->short_p
7757 || specs->signed_p || specs->unsigned_p)
7759 specs->typespec_word = cts_int;
7761 else if (specs->complex_p)
7763 specs->typespec_word = cts_double;
7764 if (pedantic)
7765 pedwarn ("ISO C does not support plain %<complex%> meaning "
7766 "%<double complex%>");
7768 else
7770 specs->typespec_word = cts_int;
7771 specs->default_int_p = true;
7772 /* We don't diagnose this here because grokdeclarator will
7773 give more specific diagnostics according to whether it is
7774 a function definition. */
7778 /* If "signed" was specified, record this to distinguish "int" and
7779 "signed int" in the case of a bit-field with
7780 -funsigned-bitfields. */
7781 specs->explicit_signed_p = specs->signed_p;
7783 /* Now compute the actual type. */
7784 switch (specs->typespec_word)
7786 case cts_void:
7787 gcc_assert (!specs->long_p && !specs->short_p
7788 && !specs->signed_p && !specs->unsigned_p
7789 && !specs->complex_p);
7790 specs->type = void_type_node;
7791 break;
7792 case cts_bool:
7793 gcc_assert (!specs->long_p && !specs->short_p
7794 && !specs->signed_p && !specs->unsigned_p
7795 && !specs->complex_p);
7796 specs->type = boolean_type_node;
7797 break;
7798 case cts_char:
7799 gcc_assert (!specs->long_p && !specs->short_p);
7800 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7801 if (specs->signed_p)
7802 specs->type = signed_char_type_node;
7803 else if (specs->unsigned_p)
7804 specs->type = unsigned_char_type_node;
7805 else
7806 specs->type = char_type_node;
7807 if (specs->complex_p)
7809 if (pedantic)
7810 pedwarn ("ISO C does not support complex integer types");
7811 specs->type = build_complex_type (specs->type);
7813 break;
7814 case cts_int:
7815 gcc_assert (!(specs->long_p && specs->short_p));
7816 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7817 if (specs->long_long_p)
7818 specs->type = (specs->unsigned_p
7819 ? long_long_unsigned_type_node
7820 : long_long_integer_type_node);
7821 else if (specs->long_p)
7822 specs->type = (specs->unsigned_p
7823 ? long_unsigned_type_node
7824 : long_integer_type_node);
7825 else if (specs->short_p)
7826 specs->type = (specs->unsigned_p
7827 ? short_unsigned_type_node
7828 : short_integer_type_node);
7829 else
7830 specs->type = (specs->unsigned_p
7831 ? unsigned_type_node
7832 : integer_type_node);
7833 if (specs->complex_p)
7835 if (pedantic)
7836 pedwarn ("ISO C does not support complex integer types");
7837 specs->type = build_complex_type (specs->type);
7839 break;
7840 case cts_float:
7841 gcc_assert (!specs->long_p && !specs->short_p
7842 && !specs->signed_p && !specs->unsigned_p);
7843 specs->type = (specs->complex_p
7844 ? complex_float_type_node
7845 : float_type_node);
7846 break;
7847 case cts_double:
7848 gcc_assert (!specs->long_long_p && !specs->short_p
7849 && !specs->signed_p && !specs->unsigned_p);
7850 if (specs->long_p)
7852 specs->type = (specs->complex_p
7853 ? complex_long_double_type_node
7854 : long_double_type_node);
7856 else
7858 specs->type = (specs->complex_p
7859 ? complex_double_type_node
7860 : double_type_node);
7862 break;
7863 case cts_dfloat32:
7864 case cts_dfloat64:
7865 case cts_dfloat128:
7866 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7867 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7868 if (specs->typespec_word == cts_dfloat32)
7869 specs->type = dfloat32_type_node;
7870 else if (specs->typespec_word == cts_dfloat64)
7871 specs->type = dfloat64_type_node;
7872 else
7873 specs->type = dfloat128_type_node;
7874 break;
7875 case cts_fract:
7876 gcc_assert (!specs->complex_p);
7877 if (specs->saturating_p)
7879 if (specs->long_long_p)
7880 specs->type = specs->unsigned_p
7881 ? sat_unsigned_long_long_fract_type_node
7882 : sat_long_long_fract_type_node;
7883 else if (specs->long_p)
7884 specs->type = specs->unsigned_p
7885 ? sat_unsigned_long_fract_type_node
7886 : sat_long_fract_type_node;
7887 else if (specs->short_p)
7888 specs->type = specs->unsigned_p
7889 ? sat_unsigned_short_fract_type_node
7890 : sat_short_fract_type_node;
7891 else
7892 specs->type = specs->unsigned_p
7893 ? sat_unsigned_fract_type_node
7894 : sat_fract_type_node;
7896 else
7898 if (specs->long_long_p)
7899 specs->type = specs->unsigned_p
7900 ? unsigned_long_long_fract_type_node
7901 : long_long_fract_type_node;
7902 else if (specs->long_p)
7903 specs->type = specs->unsigned_p
7904 ? unsigned_long_fract_type_node
7905 : long_fract_type_node;
7906 else if (specs->short_p)
7907 specs->type = specs->unsigned_p
7908 ? unsigned_short_fract_type_node
7909 : short_fract_type_node;
7910 else
7911 specs->type = specs->unsigned_p
7912 ? unsigned_fract_type_node
7913 : fract_type_node;
7915 break;
7916 case cts_accum:
7917 gcc_assert (!specs->complex_p);
7918 if (specs->saturating_p)
7920 if (specs->long_long_p)
7921 specs->type = specs->unsigned_p
7922 ? sat_unsigned_long_long_accum_type_node
7923 : sat_long_long_accum_type_node;
7924 else if (specs->long_p)
7925 specs->type = specs->unsigned_p
7926 ? sat_unsigned_long_accum_type_node
7927 : sat_long_accum_type_node;
7928 else if (specs->short_p)
7929 specs->type = specs->unsigned_p
7930 ? sat_unsigned_short_accum_type_node
7931 : sat_short_accum_type_node;
7932 else
7933 specs->type = specs->unsigned_p
7934 ? sat_unsigned_accum_type_node
7935 : sat_accum_type_node;
7937 else
7939 if (specs->long_long_p)
7940 specs->type = specs->unsigned_p
7941 ? unsigned_long_long_accum_type_node
7942 : long_long_accum_type_node;
7943 else if (specs->long_p)
7944 specs->type = specs->unsigned_p
7945 ? unsigned_long_accum_type_node
7946 : long_accum_type_node;
7947 else if (specs->short_p)
7948 specs->type = specs->unsigned_p
7949 ? unsigned_short_accum_type_node
7950 : short_accum_type_node;
7951 else
7952 specs->type = specs->unsigned_p
7953 ? unsigned_accum_type_node
7954 : accum_type_node;
7956 break;
7957 default:
7958 gcc_unreachable ();
7961 return specs;
7964 /* A subroutine of c_write_global_declarations. Perform final processing
7965 on one file scope's declarations (or the external scope's declarations),
7966 GLOBALS. */
7968 static void
7969 c_write_global_declarations_1 (tree globals)
7971 tree decl;
7972 bool reconsider;
7974 /* Process the decls in the order they were written. */
7975 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7977 /* Check for used but undefined static functions using the C
7978 standard's definition of "used", and set TREE_NO_WARNING so
7979 that check_global_declarations doesn't repeat the check. */
7980 if (TREE_CODE (decl) == FUNCTION_DECL
7981 && DECL_INITIAL (decl) == 0
7982 && DECL_EXTERNAL (decl)
7983 && !TREE_PUBLIC (decl)
7984 && C_DECL_USED (decl))
7986 pedwarn ("%q+F used but never defined", decl);
7987 TREE_NO_WARNING (decl) = 1;
7990 wrapup_global_declaration_1 (decl);
7995 reconsider = false;
7996 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7997 reconsider |= wrapup_global_declaration_2 (decl);
7999 while (reconsider);
8001 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8002 check_global_declaration_1 (decl);
8005 /* A subroutine of c_write_global_declarations Emit debug information for each
8006 of the declarations in GLOBALS. */
8008 static void
8009 c_write_global_declarations_2 (tree globals)
8011 tree decl;
8013 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8014 debug_hooks->global_decl (decl);
8017 /* Preserve the external declarations scope across a garbage collect. */
8018 static GTY(()) tree ext_block;
8020 void
8021 c_write_global_declarations (void)
8023 tree t;
8025 /* We don't want to do this if generating a PCH. */
8026 if (pch_file)
8027 return;
8029 /* Don't waste time on further processing if -fsyntax-only or we've
8030 encountered errors. */
8031 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8032 return;
8034 /* Close the external scope. */
8035 ext_block = pop_scope ();
8036 external_scope = 0;
8037 gcc_assert (!current_scope);
8039 if (ext_block)
8041 tree tmp = BLOCK_VARS (ext_block);
8042 int flags;
8043 FILE * stream = dump_begin (TDI_tu, &flags);
8044 if (stream && tmp)
8046 dump_node (tmp, flags & ~TDF_SLIM, stream);
8047 dump_end (TDI_tu, stream);
8051 /* Process all file scopes in this compilation, and the external_scope,
8052 through wrapup_global_declarations and check_global_declarations. */
8053 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8054 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8055 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8057 /* We're done parsing; proceed to optimize and emit assembly.
8058 FIXME: shouldn't be the front end's responsibility to call this. */
8059 cgraph_optimize ();
8061 /* After cgraph has had a chance to emit everything that's going to
8062 be emitted, output debug information for globals. */
8063 if (errorcount == 0 && sorrycount == 0)
8065 timevar_push (TV_SYMOUT);
8066 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8067 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8068 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8069 timevar_pop (TV_SYMOUT);
8072 ext_block = NULL;
8075 #include "gt-c-decl.h"