2008-07-07 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / c-decl.c
blob18ad11901f97962da9b9b42e50121cd51ed607ca
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 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1733 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1734 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1735 DECL_PURE_P (newdecl) |= DECL_PURE_P (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 && !current_function_decl)
1768 DECL_EXTERNAL (newdecl) = 0;
1770 if (DECL_EXTERNAL (newdecl))
1772 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1773 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1775 /* An extern decl does not override previous storage class. */
1776 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1777 if (!DECL_EXTERNAL (newdecl))
1779 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1780 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1783 else
1785 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1786 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1789 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1791 /* If we're redefining a function previously defined as extern
1792 inline, make sure we emit debug info for the inline before we
1793 throw it away, in case it was inlined into a function that
1794 hasn't been written out yet. */
1795 if (new_is_definition && DECL_INITIAL (olddecl))
1797 if (TREE_USED (olddecl)
1798 /* In unit-at-a-time mode we never inline re-defined extern
1799 inline functions. */
1800 && !flag_unit_at_a_time
1801 && cgraph_function_possibly_inlined_p (olddecl))
1802 (*debug_hooks->outlining_inline_function) (olddecl);
1804 /* The new defn must not be inline. */
1805 DECL_INLINE (newdecl) = 0;
1806 DECL_UNINLINABLE (newdecl) = 1;
1808 else
1810 /* If either decl says `inline', this fn is inline, unless
1811 its definition was passed already. */
1812 if (DECL_DECLARED_INLINE_P (newdecl)
1813 || DECL_DECLARED_INLINE_P (olddecl))
1814 DECL_DECLARED_INLINE_P (newdecl) = 1;
1816 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1817 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1819 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1820 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1821 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1822 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1825 if (DECL_BUILT_IN (olddecl))
1827 /* If redeclaring a builtin function, it stays built in.
1828 But it gets tagged as having been declared. */
1829 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1830 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1831 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1832 if (new_is_prototype)
1833 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1834 else
1835 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1836 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1839 /* Also preserve various other info from the definition. */
1840 if (!new_is_definition)
1842 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1843 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1844 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1845 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1846 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1848 /* Set DECL_INLINE on the declaration if we've got a body
1849 from which to instantiate. */
1850 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1852 DECL_INLINE (newdecl) = 1;
1853 DECL_ABSTRACT_ORIGIN (newdecl)
1854 = DECL_ABSTRACT_ORIGIN (olddecl);
1857 else
1859 /* If a previous declaration said inline, mark the
1860 definition as inlinable. */
1861 if (DECL_DECLARED_INLINE_P (newdecl)
1862 && !DECL_UNINLINABLE (newdecl))
1863 DECL_INLINE (newdecl) = 1;
1867 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1869 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1870 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1872 unsigned olddecl_uid = DECL_UID (olddecl);
1873 tree olddecl_context = DECL_CONTEXT (olddecl);
1875 memcpy ((char *) olddecl + sizeof (struct tree_common),
1876 (char *) newdecl + sizeof (struct tree_common),
1877 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1878 switch (TREE_CODE (olddecl))
1880 case FIELD_DECL:
1881 case VAR_DECL:
1882 case PARM_DECL:
1883 case LABEL_DECL:
1884 case RESULT_DECL:
1885 case CONST_DECL:
1886 case TYPE_DECL:
1887 case FUNCTION_DECL:
1888 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1889 (char *) newdecl + sizeof (struct tree_decl_common),
1890 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1891 break;
1893 default:
1895 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1896 (char *) newdecl + sizeof (struct tree_decl_common),
1897 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1899 DECL_UID (olddecl) = olddecl_uid;
1900 DECL_CONTEXT (olddecl) = olddecl_context;
1903 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1904 so that encode_section_info has a chance to look at the new decl
1905 flags and attributes. */
1906 if (DECL_RTL_SET_P (olddecl)
1907 && (TREE_CODE (olddecl) == FUNCTION_DECL
1908 || (TREE_CODE (olddecl) == VAR_DECL
1909 && TREE_STATIC (olddecl))))
1910 make_decl_rtl (olddecl);
1912 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1913 and the definition is coming from the old version, cgraph needs
1914 to be called again. */
1915 if (extern_changed && !new_is_definition
1916 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1917 cgraph_mark_if_needed (olddecl);
1920 /* Handle when a new declaration NEWDECL has the same name as an old
1921 one OLDDECL in the same binding contour. Prints an error message
1922 if appropriate.
1924 If safely possible, alter OLDDECL to look like NEWDECL, and return
1925 true. Otherwise, return false. */
1927 static bool
1928 duplicate_decls (tree newdecl, tree olddecl)
1930 tree newtype = NULL, oldtype = NULL;
1932 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1934 /* Avoid `unused variable' and other warnings for OLDDECL. */
1935 TREE_NO_WARNING (olddecl) = 1;
1936 return false;
1939 merge_decls (newdecl, olddecl, newtype, oldtype);
1940 return true;
1944 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1945 static void
1946 warn_if_shadowing (tree new_decl)
1948 struct c_binding *b;
1950 /* Shadow warnings wanted? */
1951 if (!warn_shadow
1952 /* No shadow warnings for internally generated vars. */
1953 || DECL_IS_BUILTIN (new_decl)
1954 /* No shadow warnings for vars made for inlining. */
1955 || DECL_FROM_INLINE (new_decl))
1956 return;
1958 /* Is anything being shadowed? Invisible decls do not count. */
1959 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1960 if (b->decl && b->decl != new_decl && !b->invisible)
1962 tree old_decl = b->decl;
1964 if (old_decl == error_mark_node)
1966 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1967 "non-variable", new_decl);
1968 break;
1970 else if (TREE_CODE (old_decl) == PARM_DECL)
1971 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1972 new_decl);
1973 else if (DECL_FILE_SCOPE_P (old_decl))
1974 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1975 "declaration", new_decl);
1976 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1977 && DECL_BUILT_IN (old_decl))
1979 warning (OPT_Wshadow, "declaration of %q+D shadows "
1980 "a built-in function", new_decl);
1981 break;
1983 else
1984 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1985 new_decl);
1987 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1989 break;
1994 /* Subroutine of pushdecl.
1996 X is a TYPE_DECL for a typedef statement. Create a brand new
1997 ..._TYPE node (which will be just a variant of the existing
1998 ..._TYPE node with identical properties) and then install X
1999 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2001 The whole point here is to end up with a situation where each
2002 and every ..._TYPE node the compiler creates will be uniquely
2003 associated with AT MOST one node representing a typedef name.
2004 This way, even though the compiler substitutes corresponding
2005 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2006 early on, later parts of the compiler can always do the reverse
2007 translation and get back the corresponding typedef name. For
2008 example, given:
2010 typedef struct S MY_TYPE;
2011 MY_TYPE object;
2013 Later parts of the compiler might only know that `object' was of
2014 type `struct S' if it were not for code just below. With this
2015 code however, later parts of the compiler see something like:
2017 struct S' == struct S
2018 typedef struct S' MY_TYPE;
2019 struct S' object;
2021 And they can then deduce (from the node for type struct S') that
2022 the original object declaration was:
2024 MY_TYPE object;
2026 Being able to do this is important for proper support of protoize,
2027 and also for generating precise symbolic debugging information
2028 which takes full account of the programmer's (typedef) vocabulary.
2030 Obviously, we don't want to generate a duplicate ..._TYPE node if
2031 the TYPE_DECL node that we are now processing really represents a
2032 standard built-in type. */
2034 static void
2035 clone_underlying_type (tree x)
2037 if (DECL_IS_BUILTIN (x))
2039 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2040 TYPE_NAME (TREE_TYPE (x)) = x;
2042 else if (TREE_TYPE (x) != error_mark_node
2043 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2045 tree tt = TREE_TYPE (x);
2046 DECL_ORIGINAL_TYPE (x) = tt;
2047 tt = build_variant_type_copy (tt);
2048 TYPE_NAME (tt) = x;
2049 TREE_USED (tt) = TREE_USED (x);
2050 TREE_TYPE (x) = tt;
2054 /* Record a decl-node X as belonging to the current lexical scope.
2055 Check for errors (such as an incompatible declaration for the same
2056 name already seen in the same scope).
2058 Returns either X or an old decl for the same name.
2059 If an old decl is returned, it may have been smashed
2060 to agree with what X says. */
2062 tree
2063 pushdecl (tree x)
2065 tree name = DECL_NAME (x);
2066 struct c_scope *scope = current_scope;
2067 struct c_binding *b;
2068 bool nested = false;
2070 /* Must set DECL_CONTEXT for everything not at file scope or
2071 DECL_FILE_SCOPE_P won't work. Local externs don't count
2072 unless they have initializers (which generate code). */
2073 if (current_function_decl
2074 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2075 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2076 DECL_CONTEXT (x) = current_function_decl;
2078 /* If this is of variably modified type, prevent jumping into its
2079 scope. */
2080 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2081 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2082 c_begin_vm_scope (scope->depth);
2084 /* Anonymous decls are just inserted in the scope. */
2085 if (!name)
2087 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2088 return x;
2091 /* First, see if there is another declaration with the same name in
2092 the current scope. If there is, duplicate_decls may do all the
2093 work for us. If duplicate_decls returns false, that indicates
2094 two incompatible decls in the same scope; we are to silently
2095 replace the old one (duplicate_decls has issued all appropriate
2096 diagnostics). In particular, we should not consider possible
2097 duplicates in the external scope, or shadowing. */
2098 b = I_SYMBOL_BINDING (name);
2099 if (b && B_IN_SCOPE (b, scope))
2101 struct c_binding *b_ext, *b_use;
2102 tree type = TREE_TYPE (x);
2103 tree visdecl = b->decl;
2104 tree vistype = TREE_TYPE (visdecl);
2105 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2106 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2107 b->inner_comp = false;
2108 b_use = b;
2109 b_ext = b;
2110 /* If this is an external linkage declaration, we should check
2111 for compatibility with the type in the external scope before
2112 setting the type at this scope based on the visible
2113 information only. */
2114 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2116 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2117 b_ext = b_ext->shadowed;
2118 if (b_ext)
2120 b_use = b_ext;
2121 if (b_use->type)
2122 TREE_TYPE (b_use->decl) = b_use->type;
2125 if (duplicate_decls (x, b_use->decl))
2127 if (b_use != b)
2129 /* Save the updated type in the external scope and
2130 restore the proper type for this scope. */
2131 tree thistype;
2132 if (comptypes (vistype, type))
2133 thistype = composite_type (vistype, type);
2134 else
2135 thistype = TREE_TYPE (b_use->decl);
2136 b_use->type = TREE_TYPE (b_use->decl);
2137 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2138 && DECL_BUILT_IN (b_use->decl))
2139 thistype
2140 = build_type_attribute_variant (thistype,
2141 TYPE_ATTRIBUTES
2142 (b_use->type));
2143 TREE_TYPE (b_use->decl) = thistype;
2145 return b_use->decl;
2147 else
2148 goto skip_external_and_shadow_checks;
2151 /* All declarations with external linkage, and all external
2152 references, go in the external scope, no matter what scope is
2153 current. However, the binding in that scope is ignored for
2154 purposes of normal name lookup. A separate binding structure is
2155 created in the requested scope; this governs the normal
2156 visibility of the symbol.
2158 The binding in the externals scope is used exclusively for
2159 detecting duplicate declarations of the same object, no matter
2160 what scope they are in; this is what we do here. (C99 6.2.7p2:
2161 All declarations that refer to the same object or function shall
2162 have compatible type; otherwise, the behavior is undefined.) */
2163 if (DECL_EXTERNAL (x) || scope == file_scope)
2165 tree type = TREE_TYPE (x);
2166 tree vistype = 0;
2167 tree visdecl = 0;
2168 bool type_saved = false;
2169 if (b && !B_IN_EXTERNAL_SCOPE (b)
2170 && (TREE_CODE (b->decl) == FUNCTION_DECL
2171 || TREE_CODE (b->decl) == VAR_DECL)
2172 && DECL_FILE_SCOPE_P (b->decl))
2174 visdecl = b->decl;
2175 vistype = TREE_TYPE (visdecl);
2177 if (scope != file_scope
2178 && !DECL_IN_SYSTEM_HEADER (x))
2179 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2181 while (b && !B_IN_EXTERNAL_SCOPE (b))
2183 /* If this decl might be modified, save its type. This is
2184 done here rather than when the decl is first bound
2185 because the type may change after first binding, through
2186 being completed or through attributes being added. If we
2187 encounter multiple such decls, only the first should have
2188 its type saved; the others will already have had their
2189 proper types saved and the types will not have changed as
2190 their scopes will not have been re-entered. */
2191 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2193 b->type = TREE_TYPE (b->decl);
2194 type_saved = true;
2196 if (B_IN_FILE_SCOPE (b)
2197 && TREE_CODE (b->decl) == VAR_DECL
2198 && TREE_STATIC (b->decl)
2199 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2200 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2201 && TREE_CODE (type) == ARRAY_TYPE
2202 && TYPE_DOMAIN (type)
2203 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2204 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2206 /* Array type completed in inner scope, which should be
2207 diagnosed if the completion does not have size 1 and
2208 it does not get completed in the file scope. */
2209 b->inner_comp = true;
2211 b = b->shadowed;
2214 /* If a matching external declaration has been found, set its
2215 type to the composite of all the types of that declaration.
2216 After the consistency checks, it will be reset to the
2217 composite of the visible types only. */
2218 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2219 && b->type)
2220 TREE_TYPE (b->decl) = b->type;
2222 /* The point of the same_translation_unit_p check here is,
2223 we want to detect a duplicate decl for a construct like
2224 foo() { extern bar(); } ... static bar(); but not if
2225 they are in different translation units. In any case,
2226 the static does not go in the externals scope. */
2227 if (b
2228 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2229 && duplicate_decls (x, b->decl))
2231 tree thistype;
2232 if (vistype)
2234 if (comptypes (vistype, type))
2235 thistype = composite_type (vistype, type);
2236 else
2237 thistype = TREE_TYPE (b->decl);
2239 else
2240 thistype = type;
2241 b->type = TREE_TYPE (b->decl);
2242 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2243 thistype
2244 = build_type_attribute_variant (thistype,
2245 TYPE_ATTRIBUTES (b->type));
2246 TREE_TYPE (b->decl) = thistype;
2247 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2248 return b->decl;
2250 else if (TREE_PUBLIC (x))
2252 if (visdecl && !b && duplicate_decls (x, visdecl))
2254 /* An external declaration at block scope referring to a
2255 visible entity with internal linkage. The composite
2256 type will already be correct for this scope, so we
2257 just need to fall through to make the declaration in
2258 this scope. */
2259 nested = true;
2260 x = visdecl;
2262 else
2264 bind (name, x, external_scope, /*invisible=*/true,
2265 /*nested=*/false);
2266 nested = true;
2271 if (TREE_CODE (x) != PARM_DECL)
2272 warn_if_shadowing (x);
2274 skip_external_and_shadow_checks:
2275 if (TREE_CODE (x) == TYPE_DECL)
2276 clone_underlying_type (x);
2278 bind (name, x, scope, /*invisible=*/false, nested);
2280 /* If x's type is incomplete because it's based on a
2281 structure or union which has not yet been fully declared,
2282 attach it to that structure or union type, so we can go
2283 back and complete the variable declaration later, if the
2284 structure or union gets fully declared.
2286 If the input is erroneous, we can have error_mark in the type
2287 slot (e.g. "f(void a, ...)") - that doesn't count as an
2288 incomplete type. */
2289 if (TREE_TYPE (x) != error_mark_node
2290 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2292 tree element = TREE_TYPE (x);
2294 while (TREE_CODE (element) == ARRAY_TYPE)
2295 element = TREE_TYPE (element);
2296 element = TYPE_MAIN_VARIANT (element);
2298 if ((TREE_CODE (element) == RECORD_TYPE
2299 || TREE_CODE (element) == UNION_TYPE)
2300 && (TREE_CODE (x) != TYPE_DECL
2301 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2302 && !COMPLETE_TYPE_P (element))
2303 C_TYPE_INCOMPLETE_VARS (element)
2304 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2306 return x;
2309 /* Record X as belonging to file scope.
2310 This is used only internally by the Objective-C front end,
2311 and is limited to its needs. duplicate_decls is not called;
2312 if there is any preexisting decl for this identifier, it is an ICE. */
2314 tree
2315 pushdecl_top_level (tree x)
2317 tree name;
2318 bool nested = false;
2319 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2321 name = DECL_NAME (x);
2323 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2325 if (TREE_PUBLIC (x))
2327 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2328 nested = true;
2330 if (file_scope)
2331 bind (name, x, file_scope, /*invisible=*/false, nested);
2333 return x;
2336 static void
2337 implicit_decl_warning (tree id, tree olddecl)
2339 if (warn_implicit_function_declaration)
2341 if (flag_isoc99)
2342 pedwarn (G_("implicit declaration of function %qE"), id);
2343 else
2344 warning (OPT_Wimplicit_function_declaration,
2345 G_("implicit declaration of function %qE"), id);
2346 if (olddecl)
2347 locate_old_decl (olddecl, inform);
2351 /* Generate an implicit declaration for identifier FUNCTIONID as a
2352 function of type int (). */
2354 tree
2355 implicitly_declare (tree functionid)
2357 struct c_binding *b;
2358 tree decl = 0;
2359 tree asmspec_tree;
2361 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2363 if (B_IN_SCOPE (b, external_scope))
2365 decl = b->decl;
2366 break;
2370 if (decl)
2372 if (decl == error_mark_node)
2373 return decl;
2375 /* FIXME: Objective-C has weird not-really-builtin functions
2376 which are supposed to be visible automatically. They wind up
2377 in the external scope because they're pushed before the file
2378 scope gets created. Catch this here and rebind them into the
2379 file scope. */
2380 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2382 bind (functionid, decl, file_scope,
2383 /*invisible=*/false, /*nested=*/true);
2384 return decl;
2386 else
2388 tree newtype = default_function_type;
2389 if (b->type)
2390 TREE_TYPE (decl) = b->type;
2391 /* Implicit declaration of a function already declared
2392 (somehow) in a different scope, or as a built-in.
2393 If this is the first time this has happened, warn;
2394 then recycle the old declaration but with the new type. */
2395 if (!C_DECL_IMPLICIT (decl))
2397 implicit_decl_warning (functionid, decl);
2398 C_DECL_IMPLICIT (decl) = 1;
2400 if (DECL_BUILT_IN (decl))
2402 newtype = build_type_attribute_variant (newtype,
2403 TYPE_ATTRIBUTES
2404 (TREE_TYPE (decl)));
2405 if (!comptypes (newtype, TREE_TYPE (decl)))
2407 warning (0, "incompatible implicit declaration of built-in"
2408 " function %qD", decl);
2409 newtype = TREE_TYPE (decl);
2412 else
2414 if (!comptypes (newtype, TREE_TYPE (decl)))
2416 error ("incompatible implicit declaration of function %qD",
2417 decl);
2418 locate_old_decl (decl, error);
2421 b->type = TREE_TYPE (decl);
2422 TREE_TYPE (decl) = newtype;
2423 bind (functionid, decl, current_scope,
2424 /*invisible=*/false, /*nested=*/true);
2425 return decl;
2429 /* Not seen before. */
2430 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2431 DECL_EXTERNAL (decl) = 1;
2432 TREE_PUBLIC (decl) = 1;
2433 C_DECL_IMPLICIT (decl) = 1;
2434 implicit_decl_warning (functionid, 0);
2435 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2436 if (asmspec_tree)
2437 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2439 /* C89 says implicit declarations are in the innermost block.
2440 So we record the decl in the standard fashion. */
2441 decl = pushdecl (decl);
2443 /* No need to call objc_check_decl here - it's a function type. */
2444 rest_of_decl_compilation (decl, 0, 0);
2446 /* Write a record describing this implicit function declaration
2447 to the prototypes file (if requested). */
2448 gen_aux_info_record (decl, 0, 1, 0);
2450 /* Possibly apply some default attributes to this implicit declaration. */
2451 decl_attributes (&decl, NULL_TREE, 0);
2453 return decl;
2456 /* Issue an error message for a reference to an undeclared variable
2457 ID, including a reference to a builtin outside of function-call
2458 context. Establish a binding of the identifier to error_mark_node
2459 in an appropriate scope, which will suppress further errors for the
2460 same identifier. The error message should be given location LOC. */
2461 void
2462 undeclared_variable (tree id, location_t loc)
2464 static bool already = false;
2465 struct c_scope *scope;
2467 if (current_function_decl == 0)
2469 error ("%H%qE undeclared here (not in a function)", &loc, id);
2470 scope = current_scope;
2472 else
2474 error ("%H%qE undeclared (first use in this function)", &loc, id);
2476 if (!already)
2478 error ("%H(Each undeclared identifier is reported only once", &loc);
2479 error ("%Hfor each function it appears in.)", &loc);
2480 already = true;
2483 /* If we are parsing old-style parameter decls, current_function_decl
2484 will be nonnull but current_function_scope will be null. */
2485 scope = current_function_scope ? current_function_scope : current_scope;
2487 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2490 /* Subroutine of lookup_label, declare_label, define_label: construct a
2491 LABEL_DECL with all the proper frills. */
2493 static tree
2494 make_label (tree name, location_t location)
2496 tree label = build_decl (LABEL_DECL, name, void_type_node);
2498 DECL_CONTEXT (label) = current_function_decl;
2499 DECL_MODE (label) = VOIDmode;
2500 DECL_SOURCE_LOCATION (label) = location;
2502 return label;
2505 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2506 Create one if none exists so far for the current function.
2507 This is called when a label is used in a goto expression or
2508 has its address taken. */
2510 tree
2511 lookup_label (tree name)
2513 tree label;
2515 if (current_function_decl == 0)
2517 error ("label %qE referenced outside of any function", name);
2518 return 0;
2521 /* Use a label already defined or ref'd with this name, but not if
2522 it is inherited from a containing function and wasn't declared
2523 using __label__. */
2524 label = I_LABEL_DECL (name);
2525 if (label && (DECL_CONTEXT (label) == current_function_decl
2526 || C_DECLARED_LABEL_FLAG (label)))
2528 /* If the label has only been declared, update its apparent
2529 location to point here, for better diagnostics if it
2530 turns out not to have been defined. */
2531 if (!TREE_USED (label))
2532 DECL_SOURCE_LOCATION (label) = input_location;
2533 return label;
2536 /* No label binding for that identifier; make one. */
2537 label = make_label (name, input_location);
2539 /* Ordinary labels go in the current function scope. */
2540 bind (name, label, current_function_scope,
2541 /*invisible=*/false, /*nested=*/false);
2542 return label;
2545 /* Make a label named NAME in the current function, shadowing silently
2546 any that may be inherited from containing functions or containing
2547 scopes. This is called for __label__ declarations. */
2549 tree
2550 declare_label (tree name)
2552 struct c_binding *b = I_LABEL_BINDING (name);
2553 tree label;
2555 /* Check to make sure that the label hasn't already been declared
2556 at this scope */
2557 if (b && B_IN_CURRENT_SCOPE (b))
2559 error ("duplicate label declaration %qE", name);
2560 locate_old_decl (b->decl, error);
2562 /* Just use the previous declaration. */
2563 return b->decl;
2566 label = make_label (name, input_location);
2567 C_DECLARED_LABEL_FLAG (label) = 1;
2569 /* Declared labels go in the current scope. */
2570 bind (name, label, current_scope,
2571 /*invisible=*/false, /*nested=*/false);
2572 return label;
2575 /* Define a label, specifying the location in the source file.
2576 Return the LABEL_DECL node for the label, if the definition is valid.
2577 Otherwise return 0. */
2579 tree
2580 define_label (location_t location, tree name)
2582 /* Find any preexisting label with this name. It is an error
2583 if that label has already been defined in this function, or
2584 if there is a containing function with a declared label with
2585 the same name. */
2586 tree label = I_LABEL_DECL (name);
2587 struct c_label_list *nlist_se, *nlist_vm;
2589 if (label
2590 && ((DECL_CONTEXT (label) == current_function_decl
2591 && DECL_INITIAL (label) != 0)
2592 || (DECL_CONTEXT (label) != current_function_decl
2593 && C_DECLARED_LABEL_FLAG (label))))
2595 error ("%Hduplicate label %qD", &location, label);
2596 locate_old_decl (label, error);
2597 return 0;
2599 else if (label && DECL_CONTEXT (label) == current_function_decl)
2601 /* The label has been used or declared already in this function,
2602 but not defined. Update its location to point to this
2603 definition. */
2604 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2605 error ("%Jjump into statement expression", label);
2606 if (C_DECL_UNDEFINABLE_VM (label))
2607 error ("%Jjump into scope of identifier with variably modified type",
2608 label);
2609 DECL_SOURCE_LOCATION (label) = location;
2611 else
2613 /* No label binding for that identifier; make one. */
2614 label = make_label (name, location);
2616 /* Ordinary labels go in the current function scope. */
2617 bind (name, label, current_function_scope,
2618 /*invisible=*/false, /*nested=*/false);
2621 if (!in_system_header && lookup_name (name))
2622 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2623 "for labels, identifier %qE conflicts", &location, name);
2625 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2626 nlist_se->next = label_context_stack_se->labels_def;
2627 nlist_se->label = label;
2628 label_context_stack_se->labels_def = nlist_se;
2630 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2631 nlist_vm->next = label_context_stack_vm->labels_def;
2632 nlist_vm->label = label;
2633 label_context_stack_vm->labels_def = nlist_vm;
2635 /* Mark label as having been defined. */
2636 DECL_INITIAL (label) = error_mark_node;
2637 return label;
2640 /* Given NAME, an IDENTIFIER_NODE,
2641 return the structure (or union or enum) definition for that name.
2642 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2643 CODE says which kind of type the caller wants;
2644 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2645 If the wrong kind of type is found, an error is reported. */
2647 static tree
2648 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2650 struct c_binding *b = I_TAG_BINDING (name);
2651 int thislevel = 0;
2653 if (!b || !b->decl)
2654 return 0;
2656 /* We only care about whether it's in this level if
2657 thislevel_only was set or it might be a type clash. */
2658 if (thislevel_only || TREE_CODE (b->decl) != code)
2660 /* For our purposes, a tag in the external scope is the same as
2661 a tag in the file scope. (Primarily relevant to Objective-C
2662 and its builtin structure tags, which get pushed before the
2663 file scope is created.) */
2664 if (B_IN_CURRENT_SCOPE (b)
2665 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2666 thislevel = 1;
2669 if (thislevel_only && !thislevel)
2670 return 0;
2672 if (TREE_CODE (b->decl) != code)
2674 /* Definition isn't the kind we were looking for. */
2675 pending_invalid_xref = name;
2676 pending_invalid_xref_location = input_location;
2678 /* If in the same binding level as a declaration as a tag
2679 of a different type, this must not be allowed to
2680 shadow that tag, so give the error immediately.
2681 (For example, "struct foo; union foo;" is invalid.) */
2682 if (thislevel)
2683 pending_xref_error ();
2685 return b->decl;
2688 /* Print an error message now
2689 for a recent invalid struct, union or enum cross reference.
2690 We don't print them immediately because they are not invalid
2691 when used in the `struct foo;' construct for shadowing. */
2693 void
2694 pending_xref_error (void)
2696 if (pending_invalid_xref != 0)
2697 error ("%H%qE defined as wrong kind of tag",
2698 &pending_invalid_xref_location, pending_invalid_xref);
2699 pending_invalid_xref = 0;
2703 /* Look up NAME in the current scope and its superiors
2704 in the namespace of variables, functions and typedefs.
2705 Return a ..._DECL node of some kind representing its definition,
2706 or return 0 if it is undefined. */
2708 tree
2709 lookup_name (tree name)
2711 struct c_binding *b = I_SYMBOL_BINDING (name);
2712 if (b && !b->invisible)
2713 return b->decl;
2714 return 0;
2717 /* Similar to `lookup_name' but look only at the indicated scope. */
2719 static tree
2720 lookup_name_in_scope (tree name, struct c_scope *scope)
2722 struct c_binding *b;
2724 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2725 if (B_IN_SCOPE (b, scope))
2726 return b->decl;
2727 return 0;
2730 /* Create the predefined scalar types of C,
2731 and some nodes representing standard constants (0, 1, (void *) 0).
2732 Initialize the global scope.
2733 Make definitions for built-in primitive functions. */
2735 void
2736 c_init_decl_processing (void)
2738 location_t save_loc = input_location;
2740 /* Initialize reserved words for parser. */
2741 c_parse_init ();
2743 current_function_decl = 0;
2745 gcc_obstack_init (&parser_obstack);
2747 /* Make the externals scope. */
2748 push_scope ();
2749 external_scope = current_scope;
2751 /* Declarations from c_common_nodes_and_builtins must not be associated
2752 with this input file, lest we get differences between using and not
2753 using preprocessed headers. */
2754 input_location = BUILTINS_LOCATION;
2756 build_common_tree_nodes (flag_signed_char, false);
2758 c_common_nodes_and_builtins ();
2760 /* In C, comparisons and TRUTH_* expressions have type int. */
2761 truthvalue_type_node = integer_type_node;
2762 truthvalue_true_node = integer_one_node;
2763 truthvalue_false_node = integer_zero_node;
2765 /* Even in C99, which has a real boolean type. */
2766 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2767 boolean_type_node));
2769 input_location = save_loc;
2771 pedantic_lvalues = true;
2773 make_fname_decl = c_make_fname_decl;
2774 start_fname_decls ();
2777 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2778 decl, NAME is the initialization string and TYPE_DEP indicates whether
2779 NAME depended on the type of the function. As we don't yet implement
2780 delayed emission of static data, we mark the decl as emitted
2781 so it is not placed in the output. Anything using it must therefore pull
2782 out the STRING_CST initializer directly. FIXME. */
2784 static tree
2785 c_make_fname_decl (tree id, int type_dep)
2787 const char *name = fname_as_string (type_dep);
2788 tree decl, type, init;
2789 size_t length = strlen (name);
2791 type = build_array_type (char_type_node,
2792 build_index_type (size_int (length)));
2793 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2795 decl = build_decl (VAR_DECL, id, type);
2797 TREE_STATIC (decl) = 1;
2798 TREE_READONLY (decl) = 1;
2799 DECL_ARTIFICIAL (decl) = 1;
2801 init = build_string (length + 1, name);
2802 free (CONST_CAST (char *, name));
2803 TREE_TYPE (init) = type;
2804 DECL_INITIAL (decl) = init;
2806 TREE_USED (decl) = 1;
2808 if (current_function_decl
2809 /* For invalid programs like this:
2811 void foo()
2812 const char* p = __FUNCTION__;
2814 the __FUNCTION__ is believed to appear in K&R style function
2815 parameter declarator. In that case we still don't have
2816 function_scope. */
2817 && (!errorcount || current_function_scope))
2819 DECL_CONTEXT (decl) = current_function_decl;
2820 bind (id, decl, current_function_scope,
2821 /*invisible=*/false, /*nested=*/false);
2824 finish_decl (decl, init, NULL_TREE);
2826 return decl;
2829 tree
2830 c_builtin_function (tree decl)
2832 tree type = TREE_TYPE (decl);
2833 tree id = DECL_NAME (decl);
2835 const char *name = IDENTIFIER_POINTER (id);
2836 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2838 /* Should never be called on a symbol with a preexisting meaning. */
2839 gcc_assert (!I_SYMBOL_BINDING (id));
2841 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2843 /* Builtins in the implementation namespace are made visible without
2844 needing to be explicitly declared. See push_file_scope. */
2845 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2847 TREE_CHAIN (decl) = visible_builtins;
2848 visible_builtins = decl;
2851 return decl;
2854 /* Called when a declaration is seen that contains no names to declare.
2855 If its type is a reference to a structure, union or enum inherited
2856 from a containing scope, shadow that tag name for the current scope
2857 with a forward reference.
2858 If its type defines a new named structure or union
2859 or defines an enum, it is valid but we need not do anything here.
2860 Otherwise, it is an error. */
2862 void
2863 shadow_tag (const struct c_declspecs *declspecs)
2865 shadow_tag_warned (declspecs, 0);
2868 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2869 but no pedwarn. */
2870 void
2871 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2873 bool found_tag = false;
2875 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2877 tree value = declspecs->type;
2878 enum tree_code code = TREE_CODE (value);
2880 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2881 /* Used to test also that TYPE_SIZE (value) != 0.
2882 That caused warning for `struct foo;' at top level in the file. */
2884 tree name = TYPE_NAME (value);
2885 tree t;
2887 found_tag = true;
2889 if (name == 0)
2891 if (warned != 1 && code != ENUMERAL_TYPE)
2892 /* Empty unnamed enum OK */
2894 pedwarn ("unnamed struct/union that defines no instances");
2895 warned = 1;
2898 else if (!declspecs->tag_defined_p
2899 && declspecs->storage_class != csc_none)
2901 if (warned != 1)
2902 pedwarn ("empty declaration with storage class specifier "
2903 "does not redeclare tag");
2904 warned = 1;
2905 pending_xref_error ();
2907 else if (!declspecs->tag_defined_p
2908 && (declspecs->const_p
2909 || declspecs->volatile_p
2910 || declspecs->restrict_p))
2912 if (warned != 1)
2913 pedwarn ("empty declaration with type qualifier "
2914 "does not redeclare tag");
2915 warned = 1;
2916 pending_xref_error ();
2918 else
2920 pending_invalid_xref = 0;
2921 t = lookup_tag (code, name, 1);
2923 if (t == 0)
2925 t = make_node (code);
2926 pushtag (name, t);
2930 else
2932 if (warned != 1 && !in_system_header)
2934 pedwarn ("useless type name in empty declaration");
2935 warned = 1;
2939 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2941 pedwarn ("useless type name in empty declaration");
2942 warned = 1;
2945 pending_invalid_xref = 0;
2947 if (declspecs->inline_p)
2949 error ("%<inline%> in empty declaration");
2950 warned = 1;
2953 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2955 error ("%<auto%> in file-scope empty declaration");
2956 warned = 1;
2959 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2961 error ("%<register%> in file-scope empty declaration");
2962 warned = 1;
2965 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2967 warning (0, "useless storage class specifier in empty declaration");
2968 warned = 2;
2971 if (!warned && !in_system_header && declspecs->thread_p)
2973 warning (0, "useless %<__thread%> in empty declaration");
2974 warned = 2;
2977 if (!warned && !in_system_header && (declspecs->const_p
2978 || declspecs->volatile_p
2979 || declspecs->restrict_p))
2981 warning (0, "useless type qualifier in empty declaration");
2982 warned = 2;
2985 if (warned != 1)
2987 if (!found_tag)
2988 pedwarn ("empty declaration");
2993 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2994 bits. SPECS represents declaration specifiers that the grammar
2995 only permits to contain type qualifiers and attributes. */
2998 quals_from_declspecs (const struct c_declspecs *specs)
3000 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3001 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3002 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3003 gcc_assert (!specs->type
3004 && !specs->decl_attr
3005 && specs->typespec_word == cts_none
3006 && specs->storage_class == csc_none
3007 && !specs->typedef_p
3008 && !specs->explicit_signed_p
3009 && !specs->deprecated_p
3010 && !specs->long_p
3011 && !specs->long_long_p
3012 && !specs->short_p
3013 && !specs->signed_p
3014 && !specs->unsigned_p
3015 && !specs->complex_p
3016 && !specs->inline_p
3017 && !specs->thread_p);
3018 return quals;
3021 /* Construct an array declarator. EXPR is the expression inside [],
3022 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3023 applied to the pointer to which a parameter array is converted).
3024 STATIC_P is true if "static" is inside the [], false otherwise.
3025 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3026 length which is nevertheless a complete type, false otherwise. The
3027 field for the contained declarator is left to be filled in by
3028 set_array_declarator_inner. */
3030 struct c_declarator *
3031 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3032 bool vla_unspec_p)
3034 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3035 struct c_declarator);
3036 declarator->kind = cdk_array;
3037 declarator->declarator = 0;
3038 declarator->u.array.dimen = expr;
3039 if (quals)
3041 declarator->u.array.attrs = quals->attrs;
3042 declarator->u.array.quals = quals_from_declspecs (quals);
3044 else
3046 declarator->u.array.attrs = NULL_TREE;
3047 declarator->u.array.quals = 0;
3049 declarator->u.array.static_p = static_p;
3050 declarator->u.array.vla_unspec_p = vla_unspec_p;
3051 if (pedantic && !flag_isoc99)
3053 if (static_p || quals != NULL)
3054 pedwarn ("ISO C90 does not support %<static%> or type "
3055 "qualifiers in parameter array declarators");
3056 if (vla_unspec_p)
3057 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3059 if (vla_unspec_p)
3061 if (!current_scope->parm_flag)
3063 /* C99 6.7.5.2p4 */
3064 error ("%<[*]%> not allowed in other than function prototype scope");
3065 declarator->u.array.vla_unspec_p = false;
3066 return NULL;
3068 current_scope->had_vla_unspec = true;
3070 return declarator;
3073 /* Set the contained declarator of an array declarator. DECL is the
3074 declarator, as constructed by build_array_declarator; INNER is what
3075 appears on the left of the []. */
3077 struct c_declarator *
3078 set_array_declarator_inner (struct c_declarator *decl,
3079 struct c_declarator *inner)
3081 decl->declarator = inner;
3082 return decl;
3085 /* INIT is a constructor that forms DECL's initializer. If the final
3086 element initializes a flexible array field, add the size of that
3087 initializer to DECL's size. */
3089 static void
3090 add_flexible_array_elts_to_size (tree decl, tree init)
3092 tree elt, type;
3094 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3095 return;
3097 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3098 type = TREE_TYPE (elt);
3099 if (TREE_CODE (type) == ARRAY_TYPE
3100 && TYPE_SIZE (type) == NULL_TREE
3101 && TYPE_DOMAIN (type) != NULL_TREE
3102 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3104 complete_array_type (&type, elt, false);
3105 DECL_SIZE (decl)
3106 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3107 DECL_SIZE_UNIT (decl)
3108 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3112 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3114 tree
3115 groktypename (struct c_type_name *type_name)
3117 tree type;
3118 tree attrs = type_name->specs->attrs;
3120 type_name->specs->attrs = NULL_TREE;
3122 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3123 false, NULL, &attrs, DEPRECATED_NORMAL);
3125 /* Apply attributes. */
3126 decl_attributes (&type, attrs, 0);
3128 return type;
3131 /* Decode a declarator in an ordinary declaration or data definition.
3132 This is called as soon as the type information and variable name
3133 have been parsed, before parsing the initializer if any.
3134 Here we create the ..._DECL node, fill in its type,
3135 and put it on the list of decls for the current context.
3136 The ..._DECL node is returned as the value.
3138 Exception: for arrays where the length is not specified,
3139 the type is left null, to be filled in by `finish_decl'.
3141 Function definitions do not come here; they go to start_function
3142 instead. However, external and forward declarations of functions
3143 do go through here. Structure field declarations are done by
3144 grokfield and not through here. */
3146 tree
3147 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3148 bool initialized, tree attributes)
3150 tree decl;
3151 tree tem;
3152 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3154 /* An object declared as __attribute__((deprecated)) suppresses
3155 warnings of uses of other deprecated items. */
3156 if (lookup_attribute ("deprecated", attributes))
3157 deprecated_state = DEPRECATED_SUPPRESS;
3159 decl = grokdeclarator (declarator, declspecs,
3160 NORMAL, initialized, NULL, &attributes,
3161 deprecated_state);
3162 if (!decl)
3163 return 0;
3165 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3166 && MAIN_NAME_P (DECL_NAME (decl)))
3167 warning (OPT_Wmain, "%q+D is usually a function", decl);
3169 if (initialized)
3170 /* Is it valid for this decl to have an initializer at all?
3171 If not, set INITIALIZED to zero, which will indirectly
3172 tell 'finish_decl' to ignore the initializer once it is parsed. */
3173 switch (TREE_CODE (decl))
3175 case TYPE_DECL:
3176 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3177 initialized = 0;
3178 break;
3180 case FUNCTION_DECL:
3181 error ("function %qD is initialized like a variable", decl);
3182 initialized = 0;
3183 break;
3185 case PARM_DECL:
3186 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3187 error ("parameter %qD is initialized", decl);
3188 initialized = 0;
3189 break;
3191 default:
3192 /* Don't allow initializations for incomplete types except for
3193 arrays which might be completed by the initialization. */
3195 /* This can happen if the array size is an undefined macro.
3196 We already gave a warning, so we don't need another one. */
3197 if (TREE_TYPE (decl) == error_mark_node)
3198 initialized = 0;
3199 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3201 /* A complete type is ok if size is fixed. */
3203 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3204 || C_DECL_VARIABLE_SIZE (decl))
3206 error ("variable-sized object may not be initialized");
3207 initialized = 0;
3210 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3212 error ("variable %qD has initializer but incomplete type", decl);
3213 initialized = 0;
3215 else if (C_DECL_VARIABLE_SIZE (decl))
3217 /* Although C99 is unclear about whether incomplete arrays
3218 of VLAs themselves count as VLAs, it does not make
3219 sense to permit them to be initialized given that
3220 ordinary VLAs may not be initialized. */
3221 error ("variable-sized object may not be initialized");
3222 initialized = 0;
3226 if (initialized)
3228 if (current_scope == file_scope)
3229 TREE_STATIC (decl) = 1;
3231 /* Tell 'pushdecl' this is an initialized decl
3232 even though we don't yet have the initializer expression.
3233 Also tell 'finish_decl' it may store the real initializer. */
3234 DECL_INITIAL (decl) = error_mark_node;
3237 /* If this is a function declaration, write a record describing it to the
3238 prototypes file (if requested). */
3240 if (TREE_CODE (decl) == FUNCTION_DECL)
3241 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3243 /* ANSI specifies that a tentative definition which is not merged with
3244 a non-tentative definition behaves exactly like a definition with an
3245 initializer equal to zero. (Section 3.7.2)
3247 -fno-common gives strict ANSI behavior, though this tends to break
3248 a large body of code that grew up without this rule.
3250 Thread-local variables are never common, since there's no entrenched
3251 body of code to break, and it allows more efficient variable references
3252 in the presence of dynamic linking. */
3254 if (TREE_CODE (decl) == VAR_DECL
3255 && !initialized
3256 && TREE_PUBLIC (decl)
3257 && !DECL_THREAD_LOCAL_P (decl)
3258 && !flag_no_common)
3259 DECL_COMMON (decl) = 1;
3261 /* Set attributes here so if duplicate decl, will have proper attributes. */
3262 decl_attributes (&decl, attributes, 0);
3264 /* Handle gnu_inline attribute. */
3265 if (declspecs->inline_p
3266 && !flag_gnu89_inline
3267 && TREE_CODE (decl) == FUNCTION_DECL
3268 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl))
3269 || current_function_decl))
3271 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3273 else if (declspecs->storage_class != csc_static)
3274 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3277 if (TREE_CODE (decl) == FUNCTION_DECL
3278 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3280 struct c_declarator *ce = declarator;
3282 if (ce->kind == cdk_pointer)
3283 ce = declarator->declarator;
3284 if (ce->kind == cdk_function)
3286 tree args = ce->u.arg_info->parms;
3287 for (; args; args = TREE_CHAIN (args))
3289 tree type = TREE_TYPE (args);
3290 if (type && INTEGRAL_TYPE_P (type)
3291 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3292 DECL_ARG_TYPE (args) = integer_type_node;
3297 if (TREE_CODE (decl) == FUNCTION_DECL
3298 && DECL_DECLARED_INLINE_P (decl)
3299 && DECL_UNINLINABLE (decl)
3300 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3301 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3302 decl);
3304 /* C99 6.7.4p3: An inline definition of a function with external
3305 linkage shall not contain a definition of a modifiable object
3306 with static storage duration... */
3307 if (TREE_CODE (decl) == VAR_DECL
3308 && current_scope != file_scope
3309 && TREE_STATIC (decl)
3310 && !TREE_READONLY (decl)
3311 && DECL_DECLARED_INLINE_P (current_function_decl)
3312 && DECL_EXTERNAL (current_function_decl))
3313 pedwarn ("%q+D is static but declared in inline function %qD "
3314 "which is not static", decl, current_function_decl);
3316 /* Add this decl to the current scope.
3317 TEM may equal DECL or it may be a previous decl of the same name. */
3318 tem = pushdecl (decl);
3320 if (initialized && DECL_EXTERNAL (tem))
3322 DECL_EXTERNAL (tem) = 0;
3323 TREE_STATIC (tem) = 1;
3326 return tem;
3329 /* Initialize EH if not initialized yet and exceptions are enabled. */
3331 void
3332 c_maybe_initialize_eh (void)
3334 if (!flag_exceptions || c_eh_initialized_p)
3335 return;
3337 c_eh_initialized_p = true;
3338 eh_personality_libfunc
3339 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3340 ? "__gcc_personality_sj0"
3341 : "__gcc_personality_v0");
3342 default_init_unwind_resume_libfunc ();
3343 using_eh_for_cleanups ();
3346 /* Finish processing of a declaration;
3347 install its initial value.
3348 If the length of an array type is not known before,
3349 it must be determined now, from the initial value, or it is an error. */
3351 void
3352 finish_decl (tree decl, tree init, tree asmspec_tree)
3354 tree type;
3355 int was_incomplete = (DECL_SIZE (decl) == 0);
3356 const char *asmspec = 0;
3358 /* If a name was specified, get the string. */
3359 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3360 && DECL_FILE_SCOPE_P (decl))
3361 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3362 if (asmspec_tree)
3363 asmspec = TREE_STRING_POINTER (asmspec_tree);
3365 /* If `start_decl' didn't like having an initialization, ignore it now. */
3366 if (init != 0 && DECL_INITIAL (decl) == 0)
3367 init = 0;
3369 /* Don't crash if parm is initialized. */
3370 if (TREE_CODE (decl) == PARM_DECL)
3371 init = 0;
3373 if (init)
3374 store_init_value (decl, init);
3376 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3377 || TREE_CODE (decl) == FUNCTION_DECL
3378 || TREE_CODE (decl) == FIELD_DECL))
3379 objc_check_decl (decl);
3381 type = TREE_TYPE (decl);
3383 /* Deduce size of array from initialization, if not already known. */
3384 if (TREE_CODE (type) == ARRAY_TYPE
3385 && TYPE_DOMAIN (type) == 0
3386 && TREE_CODE (decl) != TYPE_DECL)
3388 bool do_default
3389 = (TREE_STATIC (decl)
3390 /* Even if pedantic, an external linkage array
3391 may have incomplete type at first. */
3392 ? pedantic && !TREE_PUBLIC (decl)
3393 : !DECL_EXTERNAL (decl));
3394 int failure
3395 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3396 do_default);
3398 /* Get the completed type made by complete_array_type. */
3399 type = TREE_TYPE (decl);
3401 switch (failure)
3403 case 1:
3404 error ("initializer fails to determine size of %q+D", decl);
3405 break;
3407 case 2:
3408 if (do_default)
3409 error ("array size missing in %q+D", decl);
3410 /* If a `static' var's size isn't known,
3411 make it extern as well as static, so it does not get
3412 allocated.
3413 If it is not `static', then do not mark extern;
3414 finish_incomplete_decl will give it a default size
3415 and it will get allocated. */
3416 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3417 DECL_EXTERNAL (decl) = 1;
3418 break;
3420 case 3:
3421 error ("zero or negative size array %q+D", decl);
3422 break;
3424 case 0:
3425 /* For global variables, update the copy of the type that
3426 exists in the binding. */
3427 if (TREE_PUBLIC (decl))
3429 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3430 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3431 b_ext = b_ext->shadowed;
3432 if (b_ext)
3434 if (b_ext->type)
3435 b_ext->type = composite_type (b_ext->type, type);
3436 else
3437 b_ext->type = type;
3440 break;
3442 default:
3443 gcc_unreachable ();
3446 if (DECL_INITIAL (decl))
3447 TREE_TYPE (DECL_INITIAL (decl)) = type;
3449 layout_decl (decl, 0);
3452 if (TREE_CODE (decl) == VAR_DECL)
3454 if (init && TREE_CODE (init) == CONSTRUCTOR)
3455 add_flexible_array_elts_to_size (decl, init);
3457 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3458 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3459 layout_decl (decl, 0);
3461 if (DECL_SIZE (decl) == 0
3462 /* Don't give an error if we already gave one earlier. */
3463 && TREE_TYPE (decl) != error_mark_node
3464 && (TREE_STATIC (decl)
3465 /* A static variable with an incomplete type
3466 is an error if it is initialized.
3467 Also if it is not file scope.
3468 Otherwise, let it through, but if it is not `extern'
3469 then it may cause an error message later. */
3470 ? (DECL_INITIAL (decl) != 0
3471 || !DECL_FILE_SCOPE_P (decl))
3472 /* An automatic variable with an incomplete type
3473 is an error. */
3474 : !DECL_EXTERNAL (decl)))
3476 error ("storage size of %q+D isn%'t known", decl);
3477 TREE_TYPE (decl) = error_mark_node;
3480 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3481 && DECL_SIZE (decl) != 0)
3483 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3484 constant_expression_warning (DECL_SIZE (decl));
3485 else
3487 error ("storage size of %q+D isn%'t constant", decl);
3488 TREE_TYPE (decl) = error_mark_node;
3492 if (TREE_USED (type))
3493 TREE_USED (decl) = 1;
3496 /* If this is a function and an assembler name is specified, reset DECL_RTL
3497 so we can give it its new name. Also, update built_in_decls if it
3498 was a normal built-in. */
3499 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3501 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3502 set_builtin_user_assembler_name (decl, asmspec);
3503 set_user_assembler_name (decl, asmspec);
3506 /* If #pragma weak was used, mark the decl weak now. */
3507 maybe_apply_pragma_weak (decl);
3509 /* Output the assembler code and/or RTL code for variables and functions,
3510 unless the type is an undefined structure or union.
3511 If not, it will get done when the type is completed. */
3513 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3515 /* Determine the ELF visibility. */
3516 if (TREE_PUBLIC (decl))
3517 c_determine_visibility (decl);
3519 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3520 if (c_dialect_objc ())
3521 objc_check_decl (decl);
3523 if (asmspec)
3525 /* If this is not a static variable, issue a warning.
3526 It doesn't make any sense to give an ASMSPEC for an
3527 ordinary, non-register local variable. Historically,
3528 GCC has accepted -- but ignored -- the ASMSPEC in
3529 this case. */
3530 if (!DECL_FILE_SCOPE_P (decl)
3531 && TREE_CODE (decl) == VAR_DECL
3532 && !C_DECL_REGISTER (decl)
3533 && !TREE_STATIC (decl))
3534 warning (0, "ignoring asm-specifier for non-static local "
3535 "variable %q+D", decl);
3536 else
3537 set_user_assembler_name (decl, asmspec);
3540 if (DECL_FILE_SCOPE_P (decl))
3542 if (DECL_INITIAL (decl) == NULL_TREE
3543 || DECL_INITIAL (decl) == error_mark_node)
3544 /* Don't output anything
3545 when a tentative file-scope definition is seen.
3546 But at end of compilation, do output code for them. */
3547 DECL_DEFER_OUTPUT (decl) = 1;
3548 rest_of_decl_compilation (decl, true, 0);
3550 else
3552 /* In conjunction with an ASMSPEC, the `register'
3553 keyword indicates that we should place the variable
3554 in a particular register. */
3555 if (asmspec && C_DECL_REGISTER (decl))
3557 DECL_HARD_REGISTER (decl) = 1;
3558 /* This cannot be done for a structure with volatile
3559 fields, on which DECL_REGISTER will have been
3560 reset. */
3561 if (!DECL_REGISTER (decl))
3562 error ("cannot put object with volatile field into register");
3565 if (TREE_CODE (decl) != FUNCTION_DECL)
3567 /* If we're building a variable sized type, and we might be
3568 reachable other than via the top of the current binding
3569 level, then create a new BIND_EXPR so that we deallocate
3570 the object at the right time. */
3571 /* Note that DECL_SIZE can be null due to errors. */
3572 if (DECL_SIZE (decl)
3573 && !TREE_CONSTANT (DECL_SIZE (decl))
3574 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3576 tree bind;
3577 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3578 TREE_SIDE_EFFECTS (bind) = 1;
3579 add_stmt (bind);
3580 BIND_EXPR_BODY (bind) = push_stmt_list ();
3582 add_stmt (build_stmt (DECL_EXPR, decl));
3587 if (!DECL_FILE_SCOPE_P (decl))
3589 /* Recompute the RTL of a local array now
3590 if it used to be an incomplete type. */
3591 if (was_incomplete
3592 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3594 /* If we used it already as memory, it must stay in memory. */
3595 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3596 /* If it's still incomplete now, no init will save it. */
3597 if (DECL_SIZE (decl) == 0)
3598 DECL_INITIAL (decl) = 0;
3603 /* If this was marked 'used', be sure it will be output. */
3604 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3605 mark_decl_referenced (decl);
3607 if (TREE_CODE (decl) == TYPE_DECL)
3609 if (!DECL_FILE_SCOPE_P (decl)
3610 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3611 add_stmt (build_stmt (DECL_EXPR, decl));
3613 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3616 /* At the end of a declaration, throw away any variable type sizes
3617 of types defined inside that declaration. There is no use
3618 computing them in the following function definition. */
3619 if (current_scope == file_scope)
3620 get_pending_sizes ();
3622 /* Install a cleanup (aka destructor) if one was given. */
3623 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3625 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3626 if (attr)
3628 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3629 tree cleanup_decl = lookup_name (cleanup_id);
3630 tree cleanup;
3632 /* Build "cleanup(&decl)" for the destructor. */
3633 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3634 cleanup = build_tree_list (NULL_TREE, cleanup);
3635 cleanup = build_function_call (cleanup_decl, cleanup);
3637 /* Don't warn about decl unused; the cleanup uses it. */
3638 TREE_USED (decl) = 1;
3639 TREE_USED (cleanup_decl) = 1;
3641 /* Initialize EH, if we've been told to do so. */
3642 c_maybe_initialize_eh ();
3644 push_cleanup (decl, cleanup, false);
3649 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3651 tree
3652 grokparm (const struct c_parm *parm)
3654 tree attrs = parm->attrs;
3655 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3656 NULL, &attrs, DEPRECATED_NORMAL);
3658 decl_attributes (&decl, attrs, 0);
3660 return decl;
3663 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3664 and push that on the current scope. */
3666 void
3667 push_parm_decl (const struct c_parm *parm)
3669 tree attrs = parm->attrs;
3670 tree decl;
3672 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3673 &attrs, DEPRECATED_NORMAL);
3674 decl_attributes (&decl, attrs, 0);
3676 decl = pushdecl (decl);
3678 finish_decl (decl, NULL_TREE, NULL_TREE);
3681 /* Mark all the parameter declarations to date as forward decls.
3682 Also diagnose use of this extension. */
3684 void
3685 mark_forward_parm_decls (void)
3687 struct c_binding *b;
3689 if (pedantic && !current_scope->warned_forward_parm_decls)
3691 pedwarn ("ISO C forbids forward parameter declarations");
3692 current_scope->warned_forward_parm_decls = true;
3695 for (b = current_scope->bindings; b; b = b->prev)
3696 if (TREE_CODE (b->decl) == PARM_DECL)
3697 TREE_ASM_WRITTEN (b->decl) = 1;
3700 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3701 literal, which may be an incomplete array type completed by the
3702 initializer; INIT is a CONSTRUCTOR that initializes the compound
3703 literal. */
3705 tree
3706 build_compound_literal (tree type, tree init)
3708 /* We do not use start_decl here because we have a type, not a declarator;
3709 and do not use finish_decl because the decl should be stored inside
3710 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3711 tree decl;
3712 tree complit;
3713 tree stmt;
3715 if (type == error_mark_node)
3716 return error_mark_node;
3718 decl = build_decl (VAR_DECL, NULL_TREE, type);
3719 DECL_EXTERNAL (decl) = 0;
3720 TREE_PUBLIC (decl) = 0;
3721 TREE_STATIC (decl) = (current_scope == file_scope);
3722 DECL_CONTEXT (decl) = current_function_decl;
3723 TREE_USED (decl) = 1;
3724 TREE_TYPE (decl) = type;
3725 TREE_READONLY (decl) = TYPE_READONLY (type);
3726 store_init_value (decl, init);
3728 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3730 int failure = complete_array_type (&TREE_TYPE (decl),
3731 DECL_INITIAL (decl), true);
3732 gcc_assert (!failure);
3734 type = TREE_TYPE (decl);
3735 TREE_TYPE (DECL_INITIAL (decl)) = type;
3738 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3739 return error_mark_node;
3741 stmt = build_stmt (DECL_EXPR, decl);
3742 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3743 TREE_SIDE_EFFECTS (complit) = 1;
3745 layout_decl (decl, 0);
3747 if (TREE_STATIC (decl))
3749 /* This decl needs a name for the assembler output. */
3750 set_compound_literal_name (decl);
3751 DECL_DEFER_OUTPUT (decl) = 1;
3752 DECL_COMDAT (decl) = 1;
3753 DECL_ARTIFICIAL (decl) = 1;
3754 DECL_IGNORED_P (decl) = 1;
3755 pushdecl (decl);
3756 rest_of_decl_compilation (decl, 1, 0);
3759 return complit;
3762 /* Determine whether TYPE is a structure with a flexible array member,
3763 or a union containing such a structure (possibly recursively). */
3765 static bool
3766 flexible_array_type_p (tree type)
3768 tree x;
3769 switch (TREE_CODE (type))
3771 case RECORD_TYPE:
3772 x = TYPE_FIELDS (type);
3773 if (x == NULL_TREE)
3774 return false;
3775 while (TREE_CHAIN (x) != NULL_TREE)
3776 x = TREE_CHAIN (x);
3777 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3778 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3779 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3780 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3781 return true;
3782 return false;
3783 case UNION_TYPE:
3784 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3786 if (flexible_array_type_p (TREE_TYPE (x)))
3787 return true;
3789 return false;
3790 default:
3791 return false;
3795 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3796 replacing with appropriate values if they are invalid. */
3797 static void
3798 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3800 tree type_mv;
3801 unsigned int max_width;
3802 unsigned HOST_WIDE_INT w;
3803 const char *name = orig_name ? orig_name: _("<anonymous>");
3805 /* Detect and ignore out of range field width and process valid
3806 field widths. */
3807 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3808 || TREE_CODE (*width) != INTEGER_CST)
3810 error ("bit-field %qs width not an integer constant", name);
3811 *width = integer_one_node;
3813 else
3815 constant_expression_warning (*width);
3816 if (tree_int_cst_sgn (*width) < 0)
3818 error ("negative width in bit-field %qs", name);
3819 *width = integer_one_node;
3821 else if (integer_zerop (*width) && orig_name)
3823 error ("zero width for bit-field %qs", name);
3824 *width = integer_one_node;
3828 /* Detect invalid bit-field type. */
3829 if (TREE_CODE (*type) != INTEGER_TYPE
3830 && TREE_CODE (*type) != BOOLEAN_TYPE
3831 && TREE_CODE (*type) != ENUMERAL_TYPE)
3833 error ("bit-field %qs has invalid type", name);
3834 *type = unsigned_type_node;
3837 type_mv = TYPE_MAIN_VARIANT (*type);
3838 if (pedantic
3839 && !in_system_header
3840 && type_mv != integer_type_node
3841 && type_mv != unsigned_type_node
3842 && type_mv != boolean_type_node)
3843 pedwarn ("type of bit-field %qs is a GCC extension", name);
3845 max_width = TYPE_PRECISION (*type);
3847 if (0 < compare_tree_int (*width, max_width))
3849 error ("width of %qs exceeds its type", name);
3850 w = max_width;
3851 *width = build_int_cst (NULL_TREE, w);
3853 else
3854 w = tree_low_cst (*width, 1);
3856 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3858 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3859 if (!lt
3860 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3861 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3862 warning (0, "%qs is narrower than values of its type", name);
3868 /* Print warning about variable length array if necessary. */
3870 static void
3871 warn_variable_length_array (const char *name, tree size)
3873 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3874 int const_size = TREE_CONSTANT (size);
3876 if (ped)
3878 if (const_size)
3880 if (name)
3881 pedwarn ("ISO C90 forbids array %qs whose size "
3882 "can%'t be evaluated",
3883 name);
3884 else
3885 pedwarn ("ISO C90 forbids array whose size "
3886 "can%'t be evaluated");
3888 else
3890 if (name)
3891 pedwarn ("ISO C90 forbids variable length array %qs",
3892 name);
3893 else
3894 pedwarn ("ISO C90 forbids variable length array");
3897 else if (warn_vla > 0)
3899 if (const_size)
3901 if (name)
3902 warning (OPT_Wvla,
3903 "the size of array %qs can"
3904 "%'t be evaluated", name);
3905 else
3906 warning (OPT_Wvla,
3907 "the size of array can %'t be evaluated");
3909 else
3911 if (name)
3912 warning (OPT_Wvla,
3913 "variable length array %qs is used",
3914 name);
3915 else
3916 warning (OPT_Wvla,
3917 "variable length array is used");
3922 /* Given declspecs and a declarator,
3923 determine the name and type of the object declared
3924 and construct a ..._DECL node for it.
3925 (In one case we can return a ..._TYPE node instead.
3926 For invalid input we sometimes return 0.)
3928 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3930 DECL_CONTEXT says which syntactic context this declaration is in:
3931 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3932 FUNCDEF for a function definition. Like NORMAL but a few different
3933 error messages in each case. Return value may be zero meaning
3934 this definition is too screwy to try to parse.
3935 PARM for a parameter declaration (either within a function prototype
3936 or before a function body). Make a PARM_DECL, or return void_type_node.
3937 TYPENAME if for a typename (in a cast or sizeof).
3938 Don't make a DECL node; just return the ..._TYPE node.
3939 FIELD for a struct or union field; make a FIELD_DECL.
3940 INITIALIZED is true if the decl has an initializer.
3941 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3942 representing the width of the bit-field.
3943 DECL_ATTRS points to the list of attributes that should be added to this
3944 decl. Any nested attributes that belong on the decl itself will be
3945 added to this list.
3946 DEPRECATED_STATE is a deprecated_states value indicating whether
3947 deprecation warnings should be suppressed.
3949 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3950 It may also be so in the PARM case, for a prototype where the
3951 argument type is specified but not the name.
3953 This function is where the complicated C meanings of `static'
3954 and `extern' are interpreted. */
3956 static tree
3957 grokdeclarator (const struct c_declarator *declarator,
3958 struct c_declspecs *declspecs,
3959 enum decl_context decl_context, bool initialized, tree *width,
3960 tree *decl_attrs, enum deprecated_states deprecated_state)
3962 tree type = declspecs->type;
3963 bool threadp = declspecs->thread_p;
3964 enum c_storage_class storage_class = declspecs->storage_class;
3965 int constp;
3966 int restrictp;
3967 int volatilep;
3968 int type_quals = TYPE_UNQUALIFIED;
3969 const char *name, *orig_name;
3970 bool funcdef_flag = false;
3971 bool funcdef_syntax = false;
3972 int size_varies = 0;
3973 tree decl_attr = declspecs->decl_attr;
3974 int array_ptr_quals = TYPE_UNQUALIFIED;
3975 tree array_ptr_attrs = NULL_TREE;
3976 int array_parm_static = 0;
3977 bool array_parm_vla_unspec_p = false;
3978 tree returned_attrs = NULL_TREE;
3979 bool bitfield = width != NULL;
3980 tree element_type;
3981 struct c_arg_info *arg_info = 0;
3983 if (decl_context == FUNCDEF)
3984 funcdef_flag = true, decl_context = NORMAL;
3986 /* Look inside a declarator for the name being declared
3987 and get it as a string, for an error message. */
3989 const struct c_declarator *decl = declarator;
3990 name = 0;
3992 while (decl)
3993 switch (decl->kind)
3995 case cdk_function:
3996 case cdk_array:
3997 case cdk_pointer:
3998 funcdef_syntax = (decl->kind == cdk_function);
3999 decl = decl->declarator;
4000 break;
4002 case cdk_attrs:
4003 decl = decl->declarator;
4004 break;
4006 case cdk_id:
4007 if (decl->u.id)
4008 name = IDENTIFIER_POINTER (decl->u.id);
4009 decl = 0;
4010 break;
4012 default:
4013 gcc_unreachable ();
4015 orig_name = name;
4016 if (name == 0)
4017 name = "type name";
4020 /* A function definition's declarator must have the form of
4021 a function declarator. */
4023 if (funcdef_flag && !funcdef_syntax)
4024 return 0;
4026 /* If this looks like a function definition, make it one,
4027 even if it occurs where parms are expected.
4028 Then store_parm_decls will reject it and not use it as a parm. */
4029 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4030 decl_context = PARM;
4032 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4033 warn_deprecated_use (declspecs->type);
4035 if ((decl_context == NORMAL || decl_context == FIELD)
4036 && current_scope == file_scope
4037 && variably_modified_type_p (type, NULL_TREE))
4039 error ("variably modified %qs at file scope", name);
4040 type = integer_type_node;
4043 size_varies = C_TYPE_VARIABLE_SIZE (type);
4045 /* Diagnose defaulting to "int". */
4047 if (declspecs->default_int_p && !in_system_header)
4049 /* Issue a warning if this is an ISO C 99 program or if
4050 -Wreturn-type and this is a function, or if -Wimplicit;
4051 prefer the former warning since it is more explicit. */
4052 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4053 && funcdef_flag)
4054 warn_about_return_type = 1;
4055 else if (warn_implicit_int || flag_isoc99)
4056 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4059 /* Adjust the type if a bit-field is being declared,
4060 -funsigned-bitfields applied and the type is not explicitly
4061 "signed". */
4062 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4063 && TREE_CODE (type) == INTEGER_TYPE)
4064 type = c_common_unsigned_type (type);
4066 /* Figure out the type qualifiers for the declaration. There are
4067 two ways a declaration can become qualified. One is something
4068 like `const int i' where the `const' is explicit. Another is
4069 something like `typedef const int CI; CI i' where the type of the
4070 declaration contains the `const'. A third possibility is that
4071 there is a type qualifier on the element type of a typedefed
4072 array type, in which case we should extract that qualifier so
4073 that c_apply_type_quals_to_decl receives the full list of
4074 qualifiers to work with (C90 is not entirely clear about whether
4075 duplicate qualifiers should be diagnosed in this case, but it
4076 seems most appropriate to do so). */
4077 element_type = strip_array_types (type);
4078 constp = declspecs->const_p + TYPE_READONLY (element_type);
4079 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4080 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4081 if (pedantic && !flag_isoc99)
4083 if (constp > 1)
4084 pedwarn ("duplicate %<const%>");
4085 if (restrictp > 1)
4086 pedwarn ("duplicate %<restrict%>");
4087 if (volatilep > 1)
4088 pedwarn ("duplicate %<volatile%>");
4090 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4091 type = TYPE_MAIN_VARIANT (type);
4092 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4093 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4094 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4096 /* Warn about storage classes that are invalid for certain
4097 kinds of declarations (parameters, typenames, etc.). */
4099 if (funcdef_flag
4100 && (threadp
4101 || storage_class == csc_auto
4102 || storage_class == csc_register
4103 || storage_class == csc_typedef))
4105 if (storage_class == csc_auto
4106 && (pedantic || current_scope == file_scope))
4107 pedwarn ("function definition declared %<auto%>");
4108 if (storage_class == csc_register)
4109 error ("function definition declared %<register%>");
4110 if (storage_class == csc_typedef)
4111 error ("function definition declared %<typedef%>");
4112 if (threadp)
4113 error ("function definition declared %<__thread%>");
4114 threadp = false;
4115 if (storage_class == csc_auto
4116 || storage_class == csc_register
4117 || storage_class == csc_typedef)
4118 storage_class = csc_none;
4120 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4122 if (decl_context == PARM && storage_class == csc_register)
4124 else
4126 switch (decl_context)
4128 case FIELD:
4129 error ("storage class specified for structure field %qs",
4130 name);
4131 break;
4132 case PARM:
4133 error ("storage class specified for parameter %qs", name);
4134 break;
4135 default:
4136 error ("storage class specified for typename");
4137 break;
4139 storage_class = csc_none;
4140 threadp = false;
4143 else if (storage_class == csc_extern
4144 && initialized
4145 && !funcdef_flag)
4147 /* 'extern' with initialization is invalid if not at file scope. */
4148 if (current_scope == file_scope)
4150 /* It is fine to have 'extern const' when compiling at C
4151 and C++ intersection. */
4152 if (!(warn_cxx_compat && constp))
4153 warning (0, "%qs initialized and declared %<extern%>", name);
4155 else
4156 error ("%qs has both %<extern%> and initializer", name);
4158 else if (current_scope == file_scope)
4160 if (storage_class == csc_auto)
4161 error ("file-scope declaration of %qs specifies %<auto%>", name);
4162 if (pedantic && storage_class == csc_register)
4163 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4165 else
4167 if (storage_class == csc_extern && funcdef_flag)
4168 error ("nested function %qs declared %<extern%>", name);
4169 else if (threadp && storage_class == csc_none)
4171 error ("function-scope %qs implicitly auto and declared "
4172 "%<__thread%>",
4173 name);
4174 threadp = false;
4178 /* Now figure out the structure of the declarator proper.
4179 Descend through it, creating more complex types, until we reach
4180 the declared identifier (or NULL_TREE, in an absolute declarator).
4181 At each stage we maintain an unqualified version of the type
4182 together with any qualifiers that should be applied to it with
4183 c_build_qualified_type; this way, array types including
4184 multidimensional array types are first built up in unqualified
4185 form and then the qualified form is created with
4186 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4188 while (declarator && declarator->kind != cdk_id)
4190 if (type == error_mark_node)
4192 declarator = declarator->declarator;
4193 continue;
4196 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4197 a cdk_pointer (for *...),
4198 a cdk_function (for ...(...)),
4199 a cdk_attrs (for nested attributes),
4200 or a cdk_id (for the name being declared
4201 or the place in an absolute declarator
4202 where the name was omitted).
4203 For the last case, we have just exited the loop.
4205 At this point, TYPE is the type of elements of an array,
4206 or for a function to return, or for a pointer to point to.
4207 After this sequence of ifs, TYPE is the type of the
4208 array or function or pointer, and DECLARATOR has had its
4209 outermost layer removed. */
4211 if (array_ptr_quals != TYPE_UNQUALIFIED
4212 || array_ptr_attrs != NULL_TREE
4213 || array_parm_static)
4215 /* Only the innermost declarator (making a parameter be of
4216 array type which is converted to pointer type)
4217 may have static or type qualifiers. */
4218 error ("static or type qualifiers in non-parameter array declarator");
4219 array_ptr_quals = TYPE_UNQUALIFIED;
4220 array_ptr_attrs = NULL_TREE;
4221 array_parm_static = 0;
4224 switch (declarator->kind)
4226 case cdk_attrs:
4228 /* A declarator with embedded attributes. */
4229 tree attrs = declarator->u.attrs;
4230 const struct c_declarator *inner_decl;
4231 int attr_flags = 0;
4232 declarator = declarator->declarator;
4233 inner_decl = declarator;
4234 while (inner_decl->kind == cdk_attrs)
4235 inner_decl = inner_decl->declarator;
4236 if (inner_decl->kind == cdk_id)
4237 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4238 else if (inner_decl->kind == cdk_function)
4239 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4240 else if (inner_decl->kind == cdk_array)
4241 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4242 returned_attrs = decl_attributes (&type,
4243 chainon (returned_attrs, attrs),
4244 attr_flags);
4245 break;
4247 case cdk_array:
4249 tree itype = NULL_TREE;
4250 tree size = declarator->u.array.dimen;
4251 /* The index is a signed object `sizetype' bits wide. */
4252 tree index_type = c_common_signed_type (sizetype);
4254 array_ptr_quals = declarator->u.array.quals;
4255 array_ptr_attrs = declarator->u.array.attrs;
4256 array_parm_static = declarator->u.array.static_p;
4257 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4259 declarator = declarator->declarator;
4261 /* Check for some types that there cannot be arrays of. */
4263 if (VOID_TYPE_P (type))
4265 error ("declaration of %qs as array of voids", name);
4266 type = error_mark_node;
4269 if (TREE_CODE (type) == FUNCTION_TYPE)
4271 error ("declaration of %qs as array of functions", name);
4272 type = error_mark_node;
4275 if (pedantic && !in_system_header && flexible_array_type_p (type))
4276 pedwarn ("invalid use of structure with flexible array member");
4278 if (size == error_mark_node)
4279 type = error_mark_node;
4281 if (type == error_mark_node)
4282 continue;
4284 /* If size was specified, set ITYPE to a range-type for
4285 that size. Otherwise, ITYPE remains null. finish_decl
4286 may figure it out from an initial value. */
4288 if (size)
4290 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4291 lvalue. */
4292 STRIP_TYPE_NOPS (size);
4294 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4296 error ("size of array %qs has non-integer type", name);
4297 size = integer_one_node;
4300 if (pedantic && integer_zerop (size))
4301 pedwarn ("ISO C forbids zero-size array %qs", name);
4303 if (TREE_CODE (size) == INTEGER_CST)
4305 constant_expression_warning (size);
4306 if (tree_int_cst_sgn (size) < 0)
4308 error ("size of array %qs is negative", name);
4309 size = integer_one_node;
4312 else if ((decl_context == NORMAL || decl_context == FIELD)
4313 && current_scope == file_scope)
4315 error ("variably modified %qs at file scope", name);
4316 size = integer_one_node;
4318 else
4320 /* Make sure the array size remains visibly
4321 nonconstant even if it is (eg) a const variable
4322 with known value. */
4323 size_varies = 1;
4324 warn_variable_length_array (orig_name, size);
4327 if (integer_zerop (size))
4329 /* A zero-length array cannot be represented with
4330 an unsigned index type, which is what we'll
4331 get with build_index_type. Create an
4332 open-ended range instead. */
4333 itype = build_range_type (sizetype, size, NULL_TREE);
4335 else
4337 /* Arrange for the SAVE_EXPR on the inside of the
4338 MINUS_EXPR, which allows the -1 to get folded
4339 with the +1 that happens when building TYPE_SIZE. */
4340 if (size_varies)
4341 size = variable_size (size);
4343 /* Compute the maximum valid index, that is, size
4344 - 1. Do the calculation in index_type, so that
4345 if it is a variable the computations will be
4346 done in the proper mode. */
4347 itype = fold_build2 (MINUS_EXPR, index_type,
4348 convert (index_type, size),
4349 convert (index_type,
4350 size_one_node));
4352 /* If that overflowed, the array is too big. ???
4353 While a size of INT_MAX+1 technically shouldn't
4354 cause an overflow (because we subtract 1), the
4355 overflow is recorded during the conversion to
4356 index_type, before the subtraction. Handling
4357 this case seems like an unnecessary
4358 complication. */
4359 if (TREE_CODE (itype) == INTEGER_CST
4360 && TREE_OVERFLOW (itype))
4362 error ("size of array %qs is too large", name);
4363 type = error_mark_node;
4364 continue;
4367 itype = build_index_type (itype);
4370 else if (decl_context == FIELD)
4372 if (pedantic && !flag_isoc99 && !in_system_header)
4373 pedwarn ("ISO C90 does not support flexible array members");
4375 /* ISO C99 Flexible array members are effectively
4376 identical to GCC's zero-length array extension. */
4377 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4379 else if (decl_context == PARM)
4381 if (array_parm_vla_unspec_p)
4383 if (! orig_name)
4385 /* C99 6.7.5.2p4 */
4386 error ("%<[*]%> not allowed in other than a declaration");
4389 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4390 size_varies = 1;
4393 else if (decl_context == TYPENAME)
4395 if (array_parm_vla_unspec_p)
4397 /* The error is printed elsewhere. We use this to
4398 avoid messing up with incomplete array types of
4399 the same type, that would otherwise be modified
4400 below. */
4401 itype = build_range_type (sizetype, size_zero_node,
4402 NULL_TREE);
4406 /* Complain about arrays of incomplete types. */
4407 if (!COMPLETE_TYPE_P (type))
4409 error ("array type has incomplete element type");
4410 type = error_mark_node;
4412 else
4413 /* When itype is NULL, a shared incomplete array type is
4414 returned for all array of a given type. Elsewhere we
4415 make sure we don't complete that type before copying
4416 it, but here we want to make sure we don't ever
4417 modify the shared type, so we gcc_assert (itype)
4418 below. */
4419 type = build_array_type (type, itype);
4421 if (type != error_mark_node)
4423 if (size_varies)
4425 /* It is ok to modify type here even if itype is
4426 NULL: if size_varies, we're in a
4427 multi-dimensional array and the inner type has
4428 variable size, so the enclosing shared array type
4429 must too. */
4430 if (size && TREE_CODE (size) == INTEGER_CST)
4431 type
4432 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4433 C_TYPE_VARIABLE_SIZE (type) = 1;
4436 /* The GCC extension for zero-length arrays differs from
4437 ISO flexible array members in that sizeof yields
4438 zero. */
4439 if (size && integer_zerop (size))
4441 gcc_assert (itype);
4442 TYPE_SIZE (type) = bitsize_zero_node;
4443 TYPE_SIZE_UNIT (type) = size_zero_node;
4445 if (array_parm_vla_unspec_p)
4447 gcc_assert (itype);
4448 /* The type is complete. C99 6.7.5.2p4 */
4449 TYPE_SIZE (type) = bitsize_zero_node;
4450 TYPE_SIZE_UNIT (type) = size_zero_node;
4454 if (decl_context != PARM
4455 && (array_ptr_quals != TYPE_UNQUALIFIED
4456 || array_ptr_attrs != NULL_TREE
4457 || array_parm_static))
4459 error ("static or type qualifiers in non-parameter array declarator");
4460 array_ptr_quals = TYPE_UNQUALIFIED;
4461 array_ptr_attrs = NULL_TREE;
4462 array_parm_static = 0;
4464 break;
4466 case cdk_function:
4468 /* Say it's a definition only for the declarator closest
4469 to the identifier, apart possibly from some
4470 attributes. */
4471 bool really_funcdef = false;
4472 tree arg_types;
4473 if (funcdef_flag)
4475 const struct c_declarator *t = declarator->declarator;
4476 while (t->kind == cdk_attrs)
4477 t = t->declarator;
4478 really_funcdef = (t->kind == cdk_id);
4481 /* Declaring a function type. Make sure we have a valid
4482 type for the function to return. */
4483 if (type == error_mark_node)
4484 continue;
4486 size_varies = 0;
4488 /* Warn about some types functions can't return. */
4489 if (TREE_CODE (type) == FUNCTION_TYPE)
4491 error ("%qs declared as function returning a function", name);
4492 type = integer_type_node;
4494 if (TREE_CODE (type) == ARRAY_TYPE)
4496 error ("%qs declared as function returning an array", name);
4497 type = integer_type_node;
4500 /* Construct the function type and go to the next
4501 inner layer of declarator. */
4502 arg_info = declarator->u.arg_info;
4503 arg_types = grokparms (arg_info, really_funcdef);
4504 if (really_funcdef)
4505 put_pending_sizes (arg_info->pending_sizes);
4507 /* Type qualifiers before the return type of the function
4508 qualify the return type, not the function type. */
4509 if (type_quals)
4511 /* Type qualifiers on a function return type are
4512 normally permitted by the standard but have no
4513 effect, so give a warning at -Wreturn-type.
4514 Qualifiers on a void return type are banned on
4515 function definitions in ISO C; GCC used to used
4516 them for noreturn functions. */
4517 if (VOID_TYPE_P (type) && really_funcdef)
4518 pedwarn ("function definition has qualified void return type");
4519 else
4520 warning (OPT_Wignored_qualifiers,
4521 "type qualifiers ignored on function return type");
4523 type = c_build_qualified_type (type, type_quals);
4525 type_quals = TYPE_UNQUALIFIED;
4527 type = build_function_type (type, arg_types);
4528 declarator = declarator->declarator;
4530 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4531 the formal parameter list of this FUNCTION_TYPE to point to
4532 the FUNCTION_TYPE node itself. */
4534 tree link;
4536 for (link = arg_info->tags;
4537 link;
4538 link = TREE_CHAIN (link))
4539 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4541 break;
4543 case cdk_pointer:
4545 /* Merge any constancy or volatility into the target type
4546 for the pointer. */
4548 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4549 && type_quals)
4550 pedwarn ("ISO C forbids qualified function types");
4551 if (type_quals)
4552 type = c_build_qualified_type (type, type_quals);
4553 size_varies = 0;
4555 /* When the pointed-to type involves components of variable size,
4556 care must be taken to ensure that the size evaluation code is
4557 emitted early enough to dominate all the possible later uses
4558 and late enough for the variables on which it depends to have
4559 been assigned.
4561 This is expected to happen automatically when the pointed-to
4562 type has a name/declaration of it's own, but special attention
4563 is required if the type is anonymous.
4565 We handle the NORMAL and FIELD contexts here by attaching an
4566 artificial TYPE_DECL to such pointed-to type. This forces the
4567 sizes evaluation at a safe point and ensures it is not deferred
4568 until e.g. within a deeper conditional context.
4570 We expect nothing to be needed here for PARM or TYPENAME.
4571 Pushing a TYPE_DECL at this point for TYPENAME would actually
4572 be incorrect, as we might be in the middle of an expression
4573 with side effects on the pointed-to type size "arguments" prior
4574 to the pointer declaration point and the fake TYPE_DECL in the
4575 enclosing context would force the size evaluation prior to the
4576 side effects. */
4578 if (!TYPE_NAME (type)
4579 && (decl_context == NORMAL || decl_context == FIELD)
4580 && variably_modified_type_p (type, NULL_TREE))
4582 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4583 DECL_ARTIFICIAL (decl) = 1;
4584 pushdecl (decl);
4585 finish_decl (decl, NULL_TREE, NULL_TREE);
4586 TYPE_NAME (type) = decl;
4589 type = build_pointer_type (type);
4591 /* Process type qualifiers (such as const or volatile)
4592 that were given inside the `*'. */
4593 type_quals = declarator->u.pointer_quals;
4595 declarator = declarator->declarator;
4596 break;
4598 default:
4599 gcc_unreachable ();
4602 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4604 /* Now TYPE has the actual type, apart from any qualifiers in
4605 TYPE_QUALS. */
4607 /* Check the type and width of a bit-field. */
4608 if (bitfield)
4609 check_bitfield_type_and_width (&type, width, orig_name);
4611 /* Did array size calculations overflow? */
4613 if (TREE_CODE (type) == ARRAY_TYPE
4614 && COMPLETE_TYPE_P (type)
4615 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4616 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4618 error ("size of array %qs is too large", name);
4619 /* If we proceed with the array type as it is, we'll eventually
4620 crash in tree_low_cst(). */
4621 type = error_mark_node;
4624 /* If this is declaring a typedef name, return a TYPE_DECL. */
4626 if (storage_class == csc_typedef)
4628 tree decl;
4629 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4630 && type_quals)
4631 pedwarn ("ISO C forbids qualified function types");
4632 if (type_quals)
4633 type = c_build_qualified_type (type, type_quals);
4634 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4635 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4636 if (declspecs->explicit_signed_p)
4637 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4638 if (declspecs->inline_p)
4639 pedwarn ("typedef %q+D declared %<inline%>", decl);
4640 return decl;
4643 /* If this is a type name (such as, in a cast or sizeof),
4644 compute the type and return it now. */
4646 if (decl_context == TYPENAME)
4648 /* Note that the grammar rejects storage classes in typenames
4649 and fields. */
4650 gcc_assert (storage_class == csc_none && !threadp
4651 && !declspecs->inline_p);
4652 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4653 && type_quals)
4654 pedwarn ("ISO C forbids const or volatile function types");
4655 if (type_quals)
4656 type = c_build_qualified_type (type, type_quals);
4657 return type;
4660 if (pedantic && decl_context == FIELD
4661 && variably_modified_type_p (type, NULL_TREE))
4663 /* C99 6.7.2.1p8 */
4664 pedwarn ("a member of a structure or union cannot have a variably modified type");
4667 /* Aside from typedefs and type names (handle above),
4668 `void' at top level (not within pointer)
4669 is allowed only in public variables.
4670 We don't complain about parms either, but that is because
4671 a better error message can be made later. */
4673 if (VOID_TYPE_P (type) && decl_context != PARM
4674 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4675 && (storage_class == csc_extern
4676 || (current_scope == file_scope
4677 && !(storage_class == csc_static
4678 || storage_class == csc_register)))))
4680 error ("variable or field %qs declared void", name);
4681 type = integer_type_node;
4684 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4685 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4688 tree decl;
4690 if (decl_context == PARM)
4692 tree promoted_type;
4694 /* A parameter declared as an array of T is really a pointer to T.
4695 One declared as a function is really a pointer to a function. */
4697 if (TREE_CODE (type) == ARRAY_TYPE)
4699 /* Transfer const-ness of array into that of type pointed to. */
4700 type = TREE_TYPE (type);
4701 if (type_quals)
4702 type = c_build_qualified_type (type, type_quals);
4703 type = build_pointer_type (type);
4704 type_quals = array_ptr_quals;
4705 if (type_quals)
4706 type = c_build_qualified_type (type, type_quals);
4708 /* We don't yet implement attributes in this context. */
4709 if (array_ptr_attrs != NULL_TREE)
4710 warning (OPT_Wattributes,
4711 "attributes in parameter array declarator ignored");
4713 size_varies = 0;
4715 else if (TREE_CODE (type) == FUNCTION_TYPE)
4717 if (pedantic && type_quals)
4718 pedwarn ("ISO C forbids qualified function types");
4719 if (type_quals)
4720 type = c_build_qualified_type (type, type_quals);
4721 type = build_pointer_type (type);
4722 type_quals = TYPE_UNQUALIFIED;
4724 else if (type_quals)
4725 type = c_build_qualified_type (type, type_quals);
4727 decl = build_decl (PARM_DECL, declarator->u.id, type);
4728 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4729 if (size_varies)
4730 C_DECL_VARIABLE_SIZE (decl) = 1;
4732 /* Compute the type actually passed in the parmlist,
4733 for the case where there is no prototype.
4734 (For example, shorts and chars are passed as ints.)
4735 When there is a prototype, this is overridden later. */
4737 if (type == error_mark_node)
4738 promoted_type = type;
4739 else
4740 promoted_type = c_type_promotes_to (type);
4742 DECL_ARG_TYPE (decl) = promoted_type;
4743 if (declspecs->inline_p)
4744 pedwarn ("parameter %q+D declared %<inline%>", decl);
4746 else if (decl_context == FIELD)
4748 /* Note that the grammar rejects storage classes in typenames
4749 and fields. */
4750 gcc_assert (storage_class == csc_none && !threadp
4751 && !declspecs->inline_p);
4753 /* Structure field. It may not be a function. */
4755 if (TREE_CODE (type) == FUNCTION_TYPE)
4757 error ("field %qs declared as a function", name);
4758 type = build_pointer_type (type);
4760 else if (TREE_CODE (type) != ERROR_MARK
4761 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4763 error ("field %qs has incomplete type", name);
4764 type = error_mark_node;
4766 type = c_build_qualified_type (type, type_quals);
4767 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4768 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4769 DECL_NONADDRESSABLE_P (decl) = bitfield;
4770 if (bitfield && !declarator->u.id)
4771 TREE_NO_WARNING (decl) = 1;
4773 if (size_varies)
4774 C_DECL_VARIABLE_SIZE (decl) = 1;
4776 else if (TREE_CODE (type) == FUNCTION_TYPE)
4778 if (storage_class == csc_register || threadp)
4780 error ("invalid storage class for function %qs", name);
4782 else if (current_scope != file_scope)
4784 /* Function declaration not at file scope. Storage
4785 classes other than `extern' are not allowed, C99
4786 6.7.1p5, and `extern' makes no difference. However,
4787 GCC allows 'auto', perhaps with 'inline', to support
4788 nested functions. */
4789 if (storage_class == csc_auto)
4791 if (pedantic)
4792 pedwarn ("invalid storage class for function %qs", name);
4794 else if (storage_class == csc_static)
4796 error ("invalid storage class for function %qs", name);
4797 if (funcdef_flag)
4798 storage_class = declspecs->storage_class = csc_none;
4799 else
4800 return 0;
4804 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4805 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4806 decl = build_decl_attribute_variant (decl, decl_attr);
4808 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4809 pedwarn ("ISO C forbids qualified function types");
4811 /* GNU C interprets a volatile-qualified function type to indicate
4812 that the function does not return. */
4813 if ((type_quals & TYPE_QUAL_VOLATILE)
4814 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4815 warning (0, "%<noreturn%> function returns non-void value");
4817 /* Every function declaration is an external reference
4818 (DECL_EXTERNAL) except for those which are not at file
4819 scope and are explicitly declared "auto". This is
4820 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4821 GCC to signify a forward declaration of a nested function. */
4822 if (storage_class == csc_auto && current_scope != file_scope)
4823 DECL_EXTERNAL (decl) = 0;
4824 /* In C99, a function which is declared 'inline' with 'extern'
4825 is not an external reference (which is confusing). It
4826 means that the later definition of the function must be output
4827 in this file, C99 6.7.4p6. In GNU C89, a function declared
4828 'extern inline' is an external reference. */
4829 else if (declspecs->inline_p && storage_class != csc_static)
4830 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4831 == flag_gnu89_inline);
4832 else
4833 DECL_EXTERNAL (decl) = !initialized;
4835 /* Record absence of global scope for `static' or `auto'. */
4836 TREE_PUBLIC (decl)
4837 = !(storage_class == csc_static || storage_class == csc_auto);
4839 /* For a function definition, record the argument information
4840 block where store_parm_decls will look for it. */
4841 if (funcdef_flag)
4842 current_function_arg_info = arg_info;
4844 if (declspecs->default_int_p)
4845 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4847 /* Record presence of `inline', if it is reasonable. */
4848 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4850 if (declspecs->inline_p)
4851 pedwarn ("cannot inline function %<main%>");
4853 else if (declspecs->inline_p)
4855 /* Record that the function is declared `inline'. */
4856 DECL_DECLARED_INLINE_P (decl) = 1;
4858 /* Do not mark bare declarations as DECL_INLINE. Doing so
4859 in the presence of multiple declarations can result in
4860 the abstract origin pointing between the declarations,
4861 which will confuse dwarf2out. */
4862 if (initialized)
4863 DECL_INLINE (decl) = 1;
4865 /* If -finline-functions, assume it can be inlined. This does
4866 two things: let the function be deferred until it is actually
4867 needed, and let dwarf2 know that the function is inlinable. */
4868 else if (flag_inline_trees == 2 && initialized)
4869 DECL_INLINE (decl) = 1;
4871 else
4873 /* It's a variable. */
4874 /* An uninitialized decl with `extern' is a reference. */
4875 int extern_ref = !initialized && storage_class == csc_extern;
4877 type = c_build_qualified_type (type, type_quals);
4879 /* C99 6.2.2p7: It is invalid (compile-time undefined
4880 behavior) to create an 'extern' declaration for a
4881 variable if there is a global declaration that is
4882 'static' and the global declaration is not visible.
4883 (If the static declaration _is_ currently visible,
4884 the 'extern' declaration is taken to refer to that decl.) */
4885 if (extern_ref && current_scope != file_scope)
4887 tree global_decl = identifier_global_value (declarator->u.id);
4888 tree visible_decl = lookup_name (declarator->u.id);
4890 if (global_decl
4891 && global_decl != visible_decl
4892 && TREE_CODE (global_decl) == VAR_DECL
4893 && !TREE_PUBLIC (global_decl))
4894 error ("variable previously declared %<static%> redeclared "
4895 "%<extern%>");
4898 decl = build_decl (VAR_DECL, declarator->u.id, type);
4899 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4900 if (size_varies)
4901 C_DECL_VARIABLE_SIZE (decl) = 1;
4903 if (declspecs->inline_p)
4904 pedwarn ("variable %q+D declared %<inline%>", decl);
4906 /* At file scope, an initialized extern declaration may follow
4907 a static declaration. In that case, DECL_EXTERNAL will be
4908 reset later in start_decl. */
4909 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4911 /* At file scope, the presence of a `static' or `register' storage
4912 class specifier, or the absence of all storage class specifiers
4913 makes this declaration a definition (perhaps tentative). Also,
4914 the absence of `static' makes it public. */
4915 if (current_scope == file_scope)
4917 TREE_PUBLIC (decl) = storage_class != csc_static;
4918 TREE_STATIC (decl) = !extern_ref;
4920 /* Not at file scope, only `static' makes a static definition. */
4921 else
4923 TREE_STATIC (decl) = (storage_class == csc_static);
4924 TREE_PUBLIC (decl) = extern_ref;
4927 if (threadp)
4928 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4931 if (storage_class == csc_extern
4932 && variably_modified_type_p (type, NULL_TREE))
4934 /* C99 6.7.5.2p2 */
4935 error ("object with variably modified type must have no linkage");
4938 /* Record `register' declaration for warnings on &
4939 and in case doing stupid register allocation. */
4941 if (storage_class == csc_register)
4943 C_DECL_REGISTER (decl) = 1;
4944 DECL_REGISTER (decl) = 1;
4947 /* Record constancy and volatility. */
4948 c_apply_type_quals_to_decl (type_quals, decl);
4950 /* If a type has volatile components, it should be stored in memory.
4951 Otherwise, the fact that those components are volatile
4952 will be ignored, and would even crash the compiler.
4953 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4954 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4955 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4956 || TREE_CODE (decl) == RESULT_DECL))
4958 /* It is not an error for a structure with volatile fields to
4959 be declared register, but reset DECL_REGISTER since it
4960 cannot actually go in a register. */
4961 int was_reg = C_DECL_REGISTER (decl);
4962 C_DECL_REGISTER (decl) = 0;
4963 DECL_REGISTER (decl) = 0;
4964 c_mark_addressable (decl);
4965 C_DECL_REGISTER (decl) = was_reg;
4968 /* This is the earliest point at which we might know the assembler
4969 name of a variable. Thus, if it's known before this, die horribly. */
4970 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4972 return decl;
4976 /* Decode the parameter-list info for a function type or function definition.
4977 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4978 if there is an identifier list instead of a parameter decl list).
4979 These two functions are separate because when a function returns
4980 or receives functions then each is called multiple times but the order
4981 of calls is different. The last call to `grokparms' is always the one
4982 that contains the formal parameter names of a function definition.
4984 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4986 FUNCDEF_FLAG is true for a function definition, false for
4987 a mere declaration. A nonempty identifier-list gets an error message
4988 when FUNCDEF_FLAG is false. */
4990 static tree
4991 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4993 tree arg_types = arg_info->types;
4995 if (funcdef_flag && arg_info->had_vla_unspec)
4997 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4998 /* C99 6.7.5.2p4 */
4999 error ("%<[*]%> not allowed in other than function prototype scope");
5002 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5003 warning (OPT_Wstrict_prototypes,
5004 "function declaration isn%'t a prototype");
5006 if (arg_types == error_mark_node)
5007 return 0; /* don't set TYPE_ARG_TYPES in this case */
5009 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5011 if (!funcdef_flag)
5012 pedwarn ("parameter names (without types) in function declaration");
5014 arg_info->parms = arg_info->types;
5015 arg_info->types = 0;
5016 return 0;
5018 else
5020 tree parm, type, typelt;
5021 unsigned int parmno;
5023 /* If there is a parameter of incomplete type in a definition,
5024 this is an error. In a declaration this is valid, and a
5025 struct or union type may be completed later, before any calls
5026 or definition of the function. In the case where the tag was
5027 first declared within the parameter list, a warning has
5028 already been given. If a parameter has void type, then
5029 however the function cannot be defined or called, so
5030 warn. */
5032 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5033 parm;
5034 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5036 type = TREE_VALUE (typelt);
5037 if (type == error_mark_node)
5038 continue;
5040 if (!COMPLETE_TYPE_P (type))
5042 if (funcdef_flag)
5044 if (DECL_NAME (parm))
5045 error ("parameter %u (%q+D) has incomplete type",
5046 parmno, parm);
5047 else
5048 error ("%Jparameter %u has incomplete type",
5049 parm, parmno);
5051 TREE_VALUE (typelt) = error_mark_node;
5052 TREE_TYPE (parm) = error_mark_node;
5054 else if (VOID_TYPE_P (type))
5056 if (DECL_NAME (parm))
5057 warning (0, "parameter %u (%q+D) has void type",
5058 parmno, parm);
5059 else
5060 warning (0, "%Jparameter %u has void type",
5061 parm, parmno);
5065 if (DECL_NAME (parm) && TREE_USED (parm))
5066 warn_if_shadowing (parm);
5068 return arg_types;
5072 /* Take apart the current scope and return a c_arg_info structure with
5073 info on a parameter list just parsed.
5075 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5077 ELLIPSIS being true means the argument list ended in '...' so don't
5078 append a sentinel (void_list_node) to the end of the type-list. */
5080 struct c_arg_info *
5081 get_parm_info (bool ellipsis)
5083 struct c_binding *b = current_scope->bindings;
5084 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5085 struct c_arg_info);
5086 tree parms = 0;
5087 tree tags = 0;
5088 tree types = 0;
5089 tree others = 0;
5091 static bool explained_incomplete_types = false;
5092 bool gave_void_only_once_err = false;
5094 arg_info->parms = 0;
5095 arg_info->tags = 0;
5096 arg_info->types = 0;
5097 arg_info->others = 0;
5098 arg_info->pending_sizes = 0;
5099 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5101 /* The bindings in this scope must not get put into a block.
5102 We will take care of deleting the binding nodes. */
5103 current_scope->bindings = 0;
5105 /* This function is only called if there was *something* on the
5106 parameter list. */
5107 gcc_assert (b);
5109 /* A parameter list consisting solely of 'void' indicates that the
5110 function takes no arguments. But if the 'void' is qualified
5111 (by 'const' or 'volatile'), or has a storage class specifier
5112 ('register'), then the behavior is undefined; issue an error.
5113 Typedefs for 'void' are OK (see DR#157). */
5114 if (b->prev == 0 /* one binding */
5115 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5116 && !DECL_NAME (b->decl) /* anonymous */
5117 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5119 if (TREE_THIS_VOLATILE (b->decl)
5120 || TREE_READONLY (b->decl)
5121 || C_DECL_REGISTER (b->decl))
5122 error ("%<void%> as only parameter may not be qualified");
5124 /* There cannot be an ellipsis. */
5125 if (ellipsis)
5126 error ("%<void%> must be the only parameter");
5128 arg_info->types = void_list_node;
5129 return arg_info;
5132 if (!ellipsis)
5133 types = void_list_node;
5135 /* Break up the bindings list into parms, tags, types, and others;
5136 apply sanity checks; purge the name-to-decl bindings. */
5137 while (b)
5139 tree decl = b->decl;
5140 tree type = TREE_TYPE (decl);
5141 const char *keyword;
5143 switch (TREE_CODE (decl))
5145 case PARM_DECL:
5146 if (b->id)
5148 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5149 I_SYMBOL_BINDING (b->id) = b->shadowed;
5152 /* Check for forward decls that never got their actual decl. */
5153 if (TREE_ASM_WRITTEN (decl))
5154 error ("parameter %q+D has just a forward declaration", decl);
5155 /* Check for (..., void, ...) and issue an error. */
5156 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5158 if (!gave_void_only_once_err)
5160 error ("%<void%> must be the only parameter");
5161 gave_void_only_once_err = true;
5164 else
5166 /* Valid parameter, add it to the list. */
5167 TREE_CHAIN (decl) = parms;
5168 parms = decl;
5170 /* Since there is a prototype, args are passed in their
5171 declared types. The back end may override this later. */
5172 DECL_ARG_TYPE (decl) = type;
5173 types = tree_cons (0, type, types);
5175 break;
5177 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5178 case UNION_TYPE: keyword = "union"; goto tag;
5179 case RECORD_TYPE: keyword = "struct"; goto tag;
5180 tag:
5181 /* Types may not have tag-names, in which case the type
5182 appears in the bindings list with b->id NULL. */
5183 if (b->id)
5185 gcc_assert (I_TAG_BINDING (b->id) == b);
5186 I_TAG_BINDING (b->id) = b->shadowed;
5189 /* Warn about any struct, union or enum tags defined in a
5190 parameter list. The scope of such types is limited to
5191 the parameter list, which is rarely if ever desirable
5192 (it's impossible to call such a function with type-
5193 correct arguments). An anonymous union parm type is
5194 meaningful as a GNU extension, so don't warn for that. */
5195 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5197 if (b->id)
5198 /* The %s will be one of 'struct', 'union', or 'enum'. */
5199 warning (0, "%<%s %E%> declared inside parameter list",
5200 keyword, b->id);
5201 else
5202 /* The %s will be one of 'struct', 'union', or 'enum'. */
5203 warning (0, "anonymous %s declared inside parameter list",
5204 keyword);
5206 if (!explained_incomplete_types)
5208 warning (0, "its scope is only this definition or declaration,"
5209 " which is probably not what you want");
5210 explained_incomplete_types = true;
5214 tags = tree_cons (b->id, decl, tags);
5215 break;
5217 case CONST_DECL:
5218 case TYPE_DECL:
5219 case FUNCTION_DECL:
5220 /* CONST_DECLs appear here when we have an embedded enum,
5221 and TYPE_DECLs appear here when we have an embedded struct
5222 or union. No warnings for this - we already warned about the
5223 type itself. FUNCTION_DECLs appear when there is an implicit
5224 function declaration in the parameter list. */
5226 TREE_CHAIN (decl) = others;
5227 others = decl;
5228 /* fall through */
5230 case ERROR_MARK:
5231 /* error_mark_node appears here when we have an undeclared
5232 variable. Just throw it away. */
5233 if (b->id)
5235 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5236 I_SYMBOL_BINDING (b->id) = b->shadowed;
5238 break;
5240 /* Other things that might be encountered. */
5241 case LABEL_DECL:
5242 case VAR_DECL:
5243 default:
5244 gcc_unreachable ();
5247 b = free_binding_and_advance (b);
5250 arg_info->parms = parms;
5251 arg_info->tags = tags;
5252 arg_info->types = types;
5253 arg_info->others = others;
5254 arg_info->pending_sizes = get_pending_sizes ();
5255 return arg_info;
5258 /* Get the struct, enum or union (CODE says which) with tag NAME.
5259 Define the tag as a forward-reference if it is not defined.
5260 Return a c_typespec structure for the type specifier. */
5262 struct c_typespec
5263 parser_xref_tag (enum tree_code code, tree name)
5265 struct c_typespec ret;
5266 /* If a cross reference is requested, look up the type
5267 already defined for this tag and return it. */
5269 tree ref = lookup_tag (code, name, 0);
5270 /* If this is the right type of tag, return what we found.
5271 (This reference will be shadowed by shadow_tag later if appropriate.)
5272 If this is the wrong type of tag, do not return it. If it was the
5273 wrong type in the same scope, we will have had an error
5274 message already; if in a different scope and declaring
5275 a name, pending_xref_error will give an error message; but if in a
5276 different scope and not declaring a name, this tag should
5277 shadow the previous declaration of a different type of tag, and
5278 this would not work properly if we return the reference found.
5279 (For example, with "struct foo" in an outer scope, "union foo;"
5280 must shadow that tag with a new one of union type.) */
5281 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5282 if (ref && TREE_CODE (ref) == code)
5284 ret.spec = ref;
5285 return ret;
5288 /* If no such tag is yet defined, create a forward-reference node
5289 and record it as the "definition".
5290 When a real declaration of this type is found,
5291 the forward-reference will be altered into a real type. */
5293 ref = make_node (code);
5294 if (code == ENUMERAL_TYPE)
5296 /* Give the type a default layout like unsigned int
5297 to avoid crashing if it does not get defined. */
5298 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5299 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5300 TYPE_USER_ALIGN (ref) = 0;
5301 TYPE_UNSIGNED (ref) = 1;
5302 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5303 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5304 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5307 pushtag (name, ref);
5309 ret.spec = ref;
5310 return ret;
5313 /* Get the struct, enum or union (CODE says which) with tag NAME.
5314 Define the tag as a forward-reference if it is not defined.
5315 Return a tree for the type. */
5317 tree
5318 xref_tag (enum tree_code code, tree name)
5320 return parser_xref_tag (code, name).spec;
5323 /* Make sure that the tag NAME is defined *in the current scope*
5324 at least as a forward reference.
5325 CODE says which kind of tag NAME ought to be. */
5327 tree
5328 start_struct (enum tree_code code, tree name)
5330 /* If there is already a tag defined at this scope
5331 (as a forward reference), just return it. */
5333 tree ref = 0;
5335 if (name != 0)
5336 ref = lookup_tag (code, name, 1);
5337 if (ref && TREE_CODE (ref) == code)
5339 if (TYPE_SIZE (ref))
5341 if (code == UNION_TYPE)
5342 error ("redefinition of %<union %E%>", name);
5343 else
5344 error ("redefinition of %<struct %E%>", name);
5346 else if (C_TYPE_BEING_DEFINED (ref))
5348 if (code == UNION_TYPE)
5349 error ("nested redefinition of %<union %E%>", name);
5350 else
5351 error ("nested redefinition of %<struct %E%>", name);
5352 /* Don't create structures that contain themselves. */
5353 ref = NULL_TREE;
5357 /* Otherwise create a forward-reference just so the tag is in scope. */
5359 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5361 ref = make_node (code);
5362 pushtag (name, ref);
5365 C_TYPE_BEING_DEFINED (ref) = 1;
5366 TYPE_PACKED (ref) = flag_pack_struct;
5367 return ref;
5370 /* Process the specs, declarator and width (NULL if omitted)
5371 of a structure component, returning a FIELD_DECL node.
5372 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5373 DECL_ATTRS is as for grokdeclarator.
5375 This is done during the parsing of the struct declaration.
5376 The FIELD_DECL nodes are chained together and the lot of them
5377 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5379 tree
5380 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5381 tree width, tree *decl_attrs)
5383 tree value;
5385 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5386 && width == NULL_TREE)
5388 /* This is an unnamed decl.
5390 If we have something of the form "union { list } ;" then this
5391 is the anonymous union extension. Similarly for struct.
5393 If this is something of the form "struct foo;", then
5394 If MS extensions are enabled, this is handled as an
5395 anonymous struct.
5396 Otherwise this is a forward declaration of a structure tag.
5398 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5399 If MS extensions are enabled and foo names a structure, then
5400 again this is an anonymous struct.
5401 Otherwise this is an error.
5403 Oh what a horrid tangled web we weave. I wonder if MS consciously
5404 took this from Plan 9 or if it was an accident of implementation
5405 that took root before someone noticed the bug... */
5407 tree type = declspecs->type;
5408 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5409 || TREE_CODE (type) == UNION_TYPE);
5410 bool ok = false;
5412 if (type_ok
5413 && (flag_ms_extensions || !declspecs->typedef_p))
5415 if (flag_ms_extensions)
5416 ok = true;
5417 else if (flag_iso)
5418 ok = false;
5419 else if (TYPE_NAME (type) == NULL)
5420 ok = true;
5421 else
5422 ok = false;
5424 if (!ok)
5426 pedwarn ("declaration does not declare anything");
5427 return NULL_TREE;
5429 if (pedantic)
5430 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5433 value = grokdeclarator (declarator, declspecs, FIELD, false,
5434 width ? &width : NULL, decl_attrs,
5435 DEPRECATED_NORMAL);
5437 finish_decl (value, NULL_TREE, NULL_TREE);
5438 DECL_INITIAL (value) = width;
5440 return value;
5443 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5444 the list such that this does not present a problem later. */
5446 static void
5447 detect_field_duplicates (tree fieldlist)
5449 tree x, y;
5450 int timeout = 10;
5452 /* First, see if there are more than "a few" fields.
5453 This is trivially true if there are zero or one fields. */
5454 if (!fieldlist)
5455 return;
5456 x = TREE_CHAIN (fieldlist);
5457 if (!x)
5458 return;
5459 do {
5460 timeout--;
5461 x = TREE_CHAIN (x);
5462 } while (timeout > 0 && x);
5464 /* If there were "few" fields, avoid the overhead of allocating
5465 a hash table. Instead just do the nested traversal thing. */
5466 if (timeout > 0)
5468 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5469 if (DECL_NAME (x))
5471 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5472 if (DECL_NAME (y) == DECL_NAME (x))
5474 error ("duplicate member %q+D", x);
5475 DECL_NAME (x) = NULL_TREE;
5479 else
5481 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5482 void **slot;
5484 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5485 if ((y = DECL_NAME (x)) != 0)
5487 slot = htab_find_slot (htab, y, INSERT);
5488 if (*slot)
5490 error ("duplicate member %q+D", x);
5491 DECL_NAME (x) = NULL_TREE;
5493 *slot = y;
5496 htab_delete (htab);
5500 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5501 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5502 ATTRIBUTES are attributes to be applied to the structure. */
5504 tree
5505 finish_struct (tree t, tree fieldlist, tree attributes)
5507 tree x;
5508 bool toplevel = file_scope == current_scope;
5509 int saw_named_field;
5511 /* If this type was previously laid out as a forward reference,
5512 make sure we lay it out again. */
5514 TYPE_SIZE (t) = 0;
5516 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5518 if (pedantic)
5520 for (x = fieldlist; x; x = TREE_CHAIN (x))
5521 if (DECL_NAME (x) != 0)
5522 break;
5524 if (x == 0)
5526 if (TREE_CODE (t) == UNION_TYPE)
5528 if (fieldlist)
5529 pedwarn ("union has no named members");
5530 else
5531 pedwarn ("union has no members");
5533 else
5535 if (fieldlist)
5536 pedwarn ("struct has no named members");
5537 else
5538 pedwarn ("struct has no members");
5543 /* Install struct as DECL_CONTEXT of each field decl.
5544 Also process specified field sizes, found in the DECL_INITIAL,
5545 storing 0 there after the type has been changed to precision equal
5546 to its width, rather than the precision of the specified standard
5547 type. (Correct layout requires the original type to have been preserved
5548 until now.) */
5550 saw_named_field = 0;
5551 for (x = fieldlist; x; x = TREE_CHAIN (x))
5553 if (TREE_TYPE (x) == error_mark_node)
5554 continue;
5556 DECL_CONTEXT (x) = t;
5558 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5559 DECL_PACKED (x) = 1;
5561 /* If any field is const, the structure type is pseudo-const. */
5562 if (TREE_READONLY (x))
5563 C_TYPE_FIELDS_READONLY (t) = 1;
5564 else
5566 /* A field that is pseudo-const makes the structure likewise. */
5567 tree t1 = TREE_TYPE (x);
5568 while (TREE_CODE (t1) == ARRAY_TYPE)
5569 t1 = TREE_TYPE (t1);
5570 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5571 && C_TYPE_FIELDS_READONLY (t1))
5572 C_TYPE_FIELDS_READONLY (t) = 1;
5575 /* Any field that is volatile means variables of this type must be
5576 treated in some ways as volatile. */
5577 if (TREE_THIS_VOLATILE (x))
5578 C_TYPE_FIELDS_VOLATILE (t) = 1;
5580 /* Any field of nominal variable size implies structure is too. */
5581 if (C_DECL_VARIABLE_SIZE (x))
5582 C_TYPE_VARIABLE_SIZE (t) = 1;
5584 if (DECL_INITIAL (x))
5586 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5587 DECL_SIZE (x) = bitsize_int (width);
5588 DECL_BIT_FIELD (x) = 1;
5589 SET_DECL_C_BIT_FIELD (x);
5592 /* Detect flexible array member in an invalid context. */
5593 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5594 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5595 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5596 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5598 if (TREE_CODE (t) == UNION_TYPE)
5600 error ("%Jflexible array member in union", x);
5601 TREE_TYPE (x) = error_mark_node;
5603 else if (TREE_CHAIN (x) != NULL_TREE)
5605 error ("%Jflexible array member not at end of struct", x);
5606 TREE_TYPE (x) = error_mark_node;
5608 else if (!saw_named_field)
5610 error ("%Jflexible array member in otherwise empty struct", x);
5611 TREE_TYPE (x) = error_mark_node;
5615 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5616 && flexible_array_type_p (TREE_TYPE (x)))
5617 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5619 if (DECL_NAME (x))
5620 saw_named_field = 1;
5623 detect_field_duplicates (fieldlist);
5625 /* Now we have the nearly final fieldlist. Record it,
5626 then lay out the structure or union (including the fields). */
5628 TYPE_FIELDS (t) = fieldlist;
5630 layout_type (t);
5632 /* Give bit-fields their proper types. */
5634 tree *fieldlistp = &fieldlist;
5635 while (*fieldlistp)
5636 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5637 && TREE_TYPE (*fieldlistp) != error_mark_node)
5639 unsigned HOST_WIDE_INT width
5640 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5641 tree type = TREE_TYPE (*fieldlistp);
5642 if (width != TYPE_PRECISION (type))
5644 TREE_TYPE (*fieldlistp)
5645 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5646 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5648 DECL_INITIAL (*fieldlistp) = 0;
5650 else
5651 fieldlistp = &TREE_CHAIN (*fieldlistp);
5654 /* Now we have the truly final field list.
5655 Store it in this type and in the variants. */
5657 TYPE_FIELDS (t) = fieldlist;
5659 /* If there are lots of fields, sort so we can look through them fast.
5660 We arbitrarily consider 16 or more elts to be "a lot". */
5663 int len = 0;
5665 for (x = fieldlist; x; x = TREE_CHAIN (x))
5667 if (len > 15 || DECL_NAME (x) == NULL)
5668 break;
5669 len += 1;
5672 if (len > 15)
5674 tree *field_array;
5675 struct lang_type *space;
5676 struct sorted_fields_type *space2;
5678 len += list_length (x);
5680 /* Use the same allocation policy here that make_node uses, to
5681 ensure that this lives as long as the rest of the struct decl.
5682 All decls in an inline function need to be saved. */
5684 space = GGC_CNEW (struct lang_type);
5685 space2 = GGC_NEWVAR (struct sorted_fields_type,
5686 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5688 len = 0;
5689 space->s = space2;
5690 field_array = &space2->elts[0];
5691 for (x = fieldlist; x; x = TREE_CHAIN (x))
5693 field_array[len++] = x;
5695 /* If there is anonymous struct or union, break out of the loop. */
5696 if (DECL_NAME (x) == NULL)
5697 break;
5699 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5700 if (x == NULL)
5702 TYPE_LANG_SPECIFIC (t) = space;
5703 TYPE_LANG_SPECIFIC (t)->s->len = len;
5704 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5705 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5710 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5712 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5713 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5714 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5715 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5716 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5719 /* If this was supposed to be a transparent union, but we can't
5720 make it one, warn and turn off the flag. */
5721 if (TREE_CODE (t) == UNION_TYPE
5722 && TYPE_TRANSPARENT_UNION (t)
5723 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5725 TYPE_TRANSPARENT_UNION (t) = 0;
5726 warning (0, "union cannot be made transparent");
5729 /* If this structure or union completes the type of any previous
5730 variable declaration, lay it out and output its rtl. */
5731 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5733 x = TREE_CHAIN (x))
5735 tree decl = TREE_VALUE (x);
5736 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5737 layout_array_type (TREE_TYPE (decl));
5738 if (TREE_CODE (decl) != TYPE_DECL)
5740 layout_decl (decl, 0);
5741 if (c_dialect_objc ())
5742 objc_check_decl (decl);
5743 rest_of_decl_compilation (decl, toplevel, 0);
5744 if (!toplevel)
5745 expand_decl (decl);
5748 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5750 /* Finish debugging output for this type. */
5751 rest_of_type_compilation (t, toplevel);
5753 /* If we're inside a function proper, i.e. not file-scope and not still
5754 parsing parameters, then arrange for the size of a variable sized type
5755 to be bound now. */
5756 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5757 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5759 return t;
5762 /* Lay out the type T, and its element type, and so on. */
5764 static void
5765 layout_array_type (tree t)
5767 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5768 layout_array_type (TREE_TYPE (t));
5769 layout_type (t);
5772 /* Begin compiling the definition of an enumeration type.
5773 NAME is its name (or null if anonymous).
5774 Returns the type object, as yet incomplete.
5775 Also records info about it so that build_enumerator
5776 may be used to declare the individual values as they are read. */
5778 tree
5779 start_enum (struct c_enum_contents *the_enum, tree name)
5781 tree enumtype = 0;
5783 /* If this is the real definition for a previous forward reference,
5784 fill in the contents in the same object that used to be the
5785 forward reference. */
5787 if (name != 0)
5788 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5790 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5792 enumtype = make_node (ENUMERAL_TYPE);
5793 pushtag (name, enumtype);
5796 if (C_TYPE_BEING_DEFINED (enumtype))
5797 error ("nested redefinition of %<enum %E%>", name);
5799 C_TYPE_BEING_DEFINED (enumtype) = 1;
5801 if (TYPE_VALUES (enumtype) != 0)
5803 /* This enum is a named one that has been declared already. */
5804 error ("redeclaration of %<enum %E%>", name);
5806 /* Completely replace its old definition.
5807 The old enumerators remain defined, however. */
5808 TYPE_VALUES (enumtype) = 0;
5811 the_enum->enum_next_value = integer_zero_node;
5812 the_enum->enum_overflow = 0;
5814 if (flag_short_enums)
5815 TYPE_PACKED (enumtype) = 1;
5817 return enumtype;
5820 /* After processing and defining all the values of an enumeration type,
5821 install their decls in the enumeration type and finish it off.
5822 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5823 and ATTRIBUTES are the specified attributes.
5824 Returns ENUMTYPE. */
5826 tree
5827 finish_enum (tree enumtype, tree values, tree attributes)
5829 tree pair, tem;
5830 tree minnode = 0, maxnode = 0;
5831 int precision, unsign;
5832 bool toplevel = (file_scope == current_scope);
5833 struct lang_type *lt;
5835 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5837 /* Calculate the maximum value of any enumerator in this type. */
5839 if (values == error_mark_node)
5840 minnode = maxnode = integer_zero_node;
5841 else
5843 minnode = maxnode = TREE_VALUE (values);
5844 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5846 tree value = TREE_VALUE (pair);
5847 if (tree_int_cst_lt (maxnode, value))
5848 maxnode = value;
5849 if (tree_int_cst_lt (value, minnode))
5850 minnode = value;
5854 /* Construct the final type of this enumeration. It is the same
5855 as one of the integral types - the narrowest one that fits, except
5856 that normally we only go as narrow as int - and signed iff any of
5857 the values are negative. */
5858 unsign = (tree_int_cst_sgn (minnode) >= 0);
5859 precision = MAX (min_precision (minnode, unsign),
5860 min_precision (maxnode, unsign));
5862 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5864 tem = c_common_type_for_size (precision, unsign);
5865 if (tem == NULL)
5867 warning (0, "enumeration values exceed range of largest integer");
5868 tem = long_long_integer_type_node;
5871 else
5872 tem = unsign ? unsigned_type_node : integer_type_node;
5874 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5875 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5876 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5877 TYPE_SIZE (enumtype) = 0;
5879 /* If the precision of the type was specific with an attribute and it
5880 was too small, give an error. Otherwise, use it. */
5881 if (TYPE_PRECISION (enumtype))
5883 if (precision > TYPE_PRECISION (enumtype))
5884 error ("specified mode too small for enumeral values");
5886 else
5887 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5889 layout_type (enumtype);
5891 if (values != error_mark_node)
5893 /* Change the type of the enumerators to be the enum type. We
5894 need to do this irrespective of the size of the enum, for
5895 proper type checking. Replace the DECL_INITIALs of the
5896 enumerators, and the value slots of the list, with copies
5897 that have the enum type; they cannot be modified in place
5898 because they may be shared (e.g. integer_zero_node) Finally,
5899 change the purpose slots to point to the names of the decls. */
5900 for (pair = values; pair; pair = TREE_CHAIN (pair))
5902 tree enu = TREE_PURPOSE (pair);
5903 tree ini = DECL_INITIAL (enu);
5905 TREE_TYPE (enu) = enumtype;
5907 /* The ISO C Standard mandates enumerators to have type int,
5908 even though the underlying type of an enum type is
5909 unspecified. Here we convert any enumerators that fit in
5910 an int to type int, to avoid promotions to unsigned types
5911 when comparing integers with enumerators that fit in the
5912 int range. When -pedantic is given, build_enumerator()
5913 would have already taken care of those that don't fit. */
5914 if (int_fits_type_p (ini, integer_type_node))
5915 tem = integer_type_node;
5916 else
5917 tem = enumtype;
5918 ini = convert (tem, ini);
5920 DECL_INITIAL (enu) = ini;
5921 TREE_PURPOSE (pair) = DECL_NAME (enu);
5922 TREE_VALUE (pair) = ini;
5925 TYPE_VALUES (enumtype) = values;
5928 /* Record the min/max values so that we can warn about bit-field
5929 enumerations that are too small for the values. */
5930 lt = GGC_CNEW (struct lang_type);
5931 lt->enum_min = minnode;
5932 lt->enum_max = maxnode;
5933 TYPE_LANG_SPECIFIC (enumtype) = lt;
5935 /* Fix up all variant types of this enum type. */
5936 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5938 if (tem == enumtype)
5939 continue;
5940 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5941 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5942 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5943 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5944 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5945 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5946 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5947 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5948 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5949 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5950 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5953 /* Finish debugging output for this type. */
5954 rest_of_type_compilation (enumtype, toplevel);
5956 return enumtype;
5959 /* Build and install a CONST_DECL for one value of the
5960 current enumeration type (one that was begun with start_enum).
5961 Return a tree-list containing the CONST_DECL and its value.
5962 Assignment of sequential values by default is handled here. */
5964 tree
5965 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value)
5967 tree decl, type;
5969 /* Validate and default VALUE. */
5971 if (value != 0)
5973 /* Don't issue more errors for error_mark_node (i.e. an
5974 undeclared identifier) - just ignore the value expression. */
5975 if (value == error_mark_node)
5976 value = 0;
5977 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5978 || TREE_CODE (value) != INTEGER_CST)
5980 error ("enumerator value for %qE is not an integer constant", name);
5981 value = 0;
5983 else
5985 value = default_conversion (value);
5986 constant_expression_warning (value);
5990 /* Default based on previous value. */
5991 /* It should no longer be possible to have NON_LVALUE_EXPR
5992 in the default. */
5993 if (value == 0)
5995 value = the_enum->enum_next_value;
5996 if (the_enum->enum_overflow)
5997 error ("overflow in enumeration values");
6000 if (pedantic && !int_fits_type_p (value, integer_type_node))
6002 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6003 /* XXX This causes -pedantic to change the meaning of the program.
6004 Remove? -zw 2004-03-15 */
6005 value = convert (integer_type_node, value);
6008 /* Set basis for default for next value. */
6009 the_enum->enum_next_value = build_binary_op (PLUS_EXPR, value,
6010 integer_one_node, 0);
6011 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6013 /* Now create a declaration for the enum value name. */
6015 type = TREE_TYPE (value);
6016 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6017 TYPE_PRECISION (integer_type_node)),
6018 (TYPE_PRECISION (type)
6019 >= TYPE_PRECISION (integer_type_node)
6020 && TYPE_UNSIGNED (type)));
6022 decl = build_decl (CONST_DECL, name, type);
6023 DECL_INITIAL (decl) = convert (type, value);
6024 pushdecl (decl);
6026 return tree_cons (decl, value, NULL_TREE);
6030 /* Create the FUNCTION_DECL for a function definition.
6031 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6032 the declaration; they describe the function's name and the type it returns,
6033 but twisted together in a fashion that parallels the syntax of C.
6035 This function creates a binding context for the function body
6036 as well as setting up the FUNCTION_DECL in current_function_decl.
6038 Returns 1 on success. If the DECLARATOR is not suitable for a function
6039 (it defines a datum instead), we return 0, which tells
6040 yyparse to report a parse error. */
6043 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6044 tree attributes)
6046 tree decl1, old_decl;
6047 tree restype, resdecl;
6048 struct c_label_context_se *nstack_se;
6049 struct c_label_context_vm *nstack_vm;
6051 current_function_returns_value = 0; /* Assume, until we see it does. */
6052 current_function_returns_null = 0;
6053 current_function_returns_abnormally = 0;
6054 warn_about_return_type = 0;
6055 c_switch_stack = NULL;
6057 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6058 nstack_se->labels_def = NULL;
6059 nstack_se->labels_used = NULL;
6060 nstack_se->next = label_context_stack_se;
6061 label_context_stack_se = nstack_se;
6063 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6064 nstack_vm->labels_def = NULL;
6065 nstack_vm->labels_used = NULL;
6066 nstack_vm->scope = 0;
6067 nstack_vm->next = label_context_stack_vm;
6068 label_context_stack_vm = nstack_vm;
6070 /* Indicate no valid break/continue context by setting these variables
6071 to some non-null, non-label value. We'll notice and emit the proper
6072 error message in c_finish_bc_stmt. */
6073 c_break_label = c_cont_label = size_zero_node;
6075 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6076 &attributes, DEPRECATED_NORMAL);
6078 /* If the declarator is not suitable for a function definition,
6079 cause a syntax error. */
6080 if (decl1 == 0)
6082 label_context_stack_se = label_context_stack_se->next;
6083 label_context_stack_vm = label_context_stack_vm->next;
6084 return 0;
6087 decl_attributes (&decl1, attributes, 0);
6089 if (DECL_DECLARED_INLINE_P (decl1)
6090 && DECL_UNINLINABLE (decl1)
6091 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6092 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6093 decl1);
6095 /* Handle gnu_inline attribute. */
6096 if (declspecs->inline_p
6097 && !flag_gnu89_inline
6098 && TREE_CODE (decl1) == FUNCTION_DECL
6099 && (lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1))
6100 || current_function_decl))
6102 if (declspecs->storage_class != csc_static)
6103 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6106 announce_function (decl1);
6108 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6110 error ("return type is an incomplete type");
6111 /* Make it return void instead. */
6112 TREE_TYPE (decl1)
6113 = build_function_type (void_type_node,
6114 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6117 if (warn_about_return_type)
6118 pedwarn_c99 ("return type defaults to %<int%>");
6120 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6121 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6122 DECL_INITIAL (decl1) = error_mark_node;
6124 /* If this definition isn't a prototype and we had a prototype declaration
6125 before, copy the arg type info from that prototype. */
6126 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6127 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6128 old_decl = 0;
6129 current_function_prototype_locus = UNKNOWN_LOCATION;
6130 current_function_prototype_built_in = false;
6131 current_function_prototype_arg_types = NULL_TREE;
6132 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6134 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6135 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6136 TREE_TYPE (TREE_TYPE (old_decl))))
6138 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6139 TREE_TYPE (decl1));
6140 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6141 current_function_prototype_built_in
6142 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6143 current_function_prototype_arg_types
6144 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6146 if (TREE_PUBLIC (decl1))
6148 /* If there is an external prototype declaration of this
6149 function, record its location but do not copy information
6150 to this decl. This may be an invisible declaration
6151 (built-in or in a scope which has finished) or simply
6152 have more refined argument types than any declaration
6153 found above. */
6154 struct c_binding *b;
6155 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6156 if (B_IN_SCOPE (b, external_scope))
6157 break;
6158 if (b)
6160 tree ext_decl, ext_type;
6161 ext_decl = b->decl;
6162 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6163 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6164 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6165 TREE_TYPE (ext_type)))
6167 current_function_prototype_locus
6168 = DECL_SOURCE_LOCATION (ext_decl);
6169 current_function_prototype_built_in
6170 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6171 current_function_prototype_arg_types
6172 = TYPE_ARG_TYPES (ext_type);
6178 /* Optionally warn of old-fashioned def with no previous prototype. */
6179 if (warn_strict_prototypes
6180 && old_decl != error_mark_node
6181 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6182 && C_DECL_ISNT_PROTOTYPE (old_decl))
6183 warning (OPT_Wstrict_prototypes,
6184 "function declaration isn%'t a prototype");
6185 /* Optionally warn of any global def with no previous prototype. */
6186 else if (warn_missing_prototypes
6187 && old_decl != error_mark_node
6188 && TREE_PUBLIC (decl1)
6189 && !MAIN_NAME_P (DECL_NAME (decl1))
6190 && C_DECL_ISNT_PROTOTYPE (old_decl))
6191 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6192 /* Optionally warn of any def with no previous prototype
6193 if the function has already been used. */
6194 else if (warn_missing_prototypes
6195 && old_decl != 0
6196 && old_decl != error_mark_node
6197 && TREE_USED (old_decl)
6198 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6199 warning (OPT_Wmissing_prototypes,
6200 "%q+D was used with no prototype before its definition", decl1);
6201 /* Optionally warn of any global def with no previous declaration. */
6202 else if (warn_missing_declarations
6203 && TREE_PUBLIC (decl1)
6204 && old_decl == 0
6205 && !MAIN_NAME_P (DECL_NAME (decl1)))
6206 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6207 decl1);
6208 /* Optionally warn of any def with no previous declaration
6209 if the function has already been used. */
6210 else if (warn_missing_declarations
6211 && old_decl != 0
6212 && old_decl != error_mark_node
6213 && TREE_USED (old_decl)
6214 && C_DECL_IMPLICIT (old_decl))
6215 warning (OPT_Wmissing_declarations,
6216 "%q+D was used with no declaration before its definition", decl1);
6218 /* This function exists in static storage.
6219 (This does not mean `static' in the C sense!) */
6220 TREE_STATIC (decl1) = 1;
6222 /* A nested function is not global. */
6223 if (current_function_decl != 0)
6224 TREE_PUBLIC (decl1) = 0;
6226 /* This is the earliest point at which we might know the assembler
6227 name of the function. Thus, if it's set before this, die horribly. */
6228 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6230 /* If #pragma weak was used, mark the decl weak now. */
6231 if (current_scope == file_scope)
6232 maybe_apply_pragma_weak (decl1);
6234 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6235 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6237 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6238 != integer_type_node)
6239 pedwarn ("return type of %q+D is not %<int%>", decl1);
6241 check_main_parameter_types(decl1);
6243 if (!TREE_PUBLIC (decl1))
6244 pedwarn ("%q+D is normally a non-static function", decl1);
6247 /* Record the decl so that the function name is defined.
6248 If we already have a decl for this name, and it is a FUNCTION_DECL,
6249 use the old decl. */
6251 current_function_decl = pushdecl (decl1);
6253 push_scope ();
6254 declare_parm_level ();
6256 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6257 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6258 DECL_ARTIFICIAL (resdecl) = 1;
6259 DECL_IGNORED_P (resdecl) = 1;
6260 DECL_RESULT (current_function_decl) = resdecl;
6262 start_fname_decls ();
6264 return 1;
6267 /* Subroutine of store_parm_decls which handles new-style function
6268 definitions (prototype format). The parms already have decls, so we
6269 need only record them as in effect and complain if any redundant
6270 old-style parm decls were written. */
6271 static void
6272 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6274 tree decl;
6276 if (current_scope->bindings)
6278 error ("%Jold-style parameter declarations in prototyped "
6279 "function definition", fndecl);
6281 /* Get rid of the old-style declarations. */
6282 pop_scope ();
6283 push_scope ();
6285 /* Don't issue this warning for nested functions, and don't issue this
6286 warning if we got here because ARG_INFO_TYPES was error_mark_node
6287 (this happens when a function definition has just an ellipsis in
6288 its parameter list). */
6289 else if (!in_system_header && !current_function_scope
6290 && arg_info->types != error_mark_node)
6291 warning (OPT_Wtraditional,
6292 "%Jtraditional C rejects ISO C style function definitions",
6293 fndecl);
6295 /* Now make all the parameter declarations visible in the function body.
6296 We can bypass most of the grunt work of pushdecl. */
6297 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6299 DECL_CONTEXT (decl) = current_function_decl;
6300 if (DECL_NAME (decl))
6302 bind (DECL_NAME (decl), decl, current_scope,
6303 /*invisible=*/false, /*nested=*/false);
6304 if (!TREE_USED (decl))
6305 warn_if_shadowing (decl);
6307 else
6308 error ("%Jparameter name omitted", decl);
6311 /* Record the parameter list in the function declaration. */
6312 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6314 /* Now make all the ancillary declarations visible, likewise. */
6315 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6317 DECL_CONTEXT (decl) = current_function_decl;
6318 if (DECL_NAME (decl))
6319 bind (DECL_NAME (decl), decl, current_scope,
6320 /*invisible=*/false, /*nested=*/false);
6323 /* And all the tag declarations. */
6324 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6325 if (TREE_PURPOSE (decl))
6326 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6327 /*invisible=*/false, /*nested=*/false);
6330 /* Subroutine of store_parm_decls which handles old-style function
6331 definitions (separate parameter list and declarations). */
6333 static void
6334 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6336 struct c_binding *b;
6337 tree parm, decl, last;
6338 tree parmids = arg_info->parms;
6339 struct pointer_set_t *seen_args = pointer_set_create ();
6341 if (!in_system_header)
6342 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6343 fndecl);
6345 /* Match each formal parameter name with its declaration. Save each
6346 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6347 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6349 if (TREE_VALUE (parm) == 0)
6351 error ("%Jparameter name missing from parameter list", fndecl);
6352 TREE_PURPOSE (parm) = 0;
6353 continue;
6356 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6357 if (b && B_IN_CURRENT_SCOPE (b))
6359 decl = b->decl;
6360 /* If we got something other than a PARM_DECL it is an error. */
6361 if (TREE_CODE (decl) != PARM_DECL)
6362 error ("%q+D declared as a non-parameter", decl);
6363 /* If the declaration is already marked, we have a duplicate
6364 name. Complain and ignore the duplicate. */
6365 else if (pointer_set_contains (seen_args, decl))
6367 error ("multiple parameters named %q+D", decl);
6368 TREE_PURPOSE (parm) = 0;
6369 continue;
6371 /* If the declaration says "void", complain and turn it into
6372 an int. */
6373 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6375 error ("parameter %q+D declared with void type", decl);
6376 TREE_TYPE (decl) = integer_type_node;
6377 DECL_ARG_TYPE (decl) = integer_type_node;
6378 layout_decl (decl, 0);
6380 warn_if_shadowing (decl);
6382 /* If no declaration found, default to int. */
6383 else
6385 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6386 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6387 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6388 pushdecl (decl);
6389 warn_if_shadowing (decl);
6391 if (flag_isoc99)
6392 pedwarn ("type of %q+D defaults to %<int%>", decl);
6393 else
6394 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6397 TREE_PURPOSE (parm) = decl;
6398 pointer_set_insert (seen_args, decl);
6401 /* Now examine the parms chain for incomplete declarations
6402 and declarations with no corresponding names. */
6404 for (b = current_scope->bindings; b; b = b->prev)
6406 parm = b->decl;
6407 if (TREE_CODE (parm) != PARM_DECL)
6408 continue;
6410 if (TREE_TYPE (parm) != error_mark_node
6411 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6413 error ("parameter %q+D has incomplete type", parm);
6414 TREE_TYPE (parm) = error_mark_node;
6417 if (!pointer_set_contains (seen_args, parm))
6419 error ("declaration for parameter %q+D but no such parameter", parm);
6421 /* Pretend the parameter was not missing.
6422 This gets us to a standard state and minimizes
6423 further error messages. */
6424 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6428 /* Chain the declarations together in the order of the list of
6429 names. Store that chain in the function decl, replacing the
6430 list of names. Update the current scope to match. */
6431 DECL_ARGUMENTS (fndecl) = 0;
6433 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6434 if (TREE_PURPOSE (parm))
6435 break;
6436 if (parm && TREE_PURPOSE (parm))
6438 last = TREE_PURPOSE (parm);
6439 DECL_ARGUMENTS (fndecl) = last;
6441 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6442 if (TREE_PURPOSE (parm))
6444 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6445 last = TREE_PURPOSE (parm);
6447 TREE_CHAIN (last) = 0;
6450 pointer_set_destroy (seen_args);
6452 /* If there was a previous prototype,
6453 set the DECL_ARG_TYPE of each argument according to
6454 the type previously specified, and report any mismatches. */
6456 if (current_function_prototype_arg_types)
6458 tree type;
6459 for (parm = DECL_ARGUMENTS (fndecl),
6460 type = current_function_prototype_arg_types;
6461 parm || (type && TREE_VALUE (type) != error_mark_node
6462 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6463 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6465 if (parm == 0 || type == 0
6466 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6468 if (current_function_prototype_built_in)
6469 warning (0, "number of arguments doesn%'t match "
6470 "built-in prototype");
6471 else
6473 error ("number of arguments doesn%'t match prototype");
6474 error ("%Hprototype declaration",
6475 &current_function_prototype_locus);
6477 break;
6479 /* Type for passing arg must be consistent with that
6480 declared for the arg. ISO C says we take the unqualified
6481 type for parameters declared with qualified type. */
6482 if (TREE_TYPE (parm) != error_mark_node
6483 && TREE_TYPE (type) != error_mark_node
6484 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6485 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6487 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6488 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6490 /* Adjust argument to match prototype. E.g. a previous
6491 `int foo(float);' prototype causes
6492 `int foo(x) float x; {...}' to be treated like
6493 `int foo(float x) {...}'. This is particularly
6494 useful for argument types like uid_t. */
6495 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6497 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6498 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6499 && TYPE_PRECISION (TREE_TYPE (parm))
6500 < TYPE_PRECISION (integer_type_node))
6501 DECL_ARG_TYPE (parm) = integer_type_node;
6503 if (pedantic)
6505 /* ??? Is it possible to get here with a
6506 built-in prototype or will it always have
6507 been diagnosed as conflicting with an
6508 old-style definition and discarded? */
6509 if (current_function_prototype_built_in)
6510 warning (0, "promoted argument %qD "
6511 "doesn%'t match built-in prototype", parm);
6512 else
6514 pedwarn ("promoted argument %qD "
6515 "doesn%'t match prototype", parm);
6516 pedwarn ("%Hprototype declaration",
6517 &current_function_prototype_locus);
6521 else
6523 if (current_function_prototype_built_in)
6524 warning (0, "argument %qD doesn%'t match "
6525 "built-in prototype", parm);
6526 else
6528 error ("argument %qD doesn%'t match prototype", parm);
6529 error ("%Hprototype declaration",
6530 &current_function_prototype_locus);
6535 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6538 /* Otherwise, create a prototype that would match. */
6540 else
6542 tree actual = 0, last = 0, type;
6544 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6546 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6547 if (last)
6548 TREE_CHAIN (last) = type;
6549 else
6550 actual = type;
6551 last = type;
6553 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6554 if (last)
6555 TREE_CHAIN (last) = type;
6556 else
6557 actual = type;
6559 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6560 of the type of this function, but we need to avoid having this
6561 affect the types of other similarly-typed functions, so we must
6562 first force the generation of an identical (but separate) type
6563 node for the relevant function type. The new node we create
6564 will be a variant of the main variant of the original function
6565 type. */
6567 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6569 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6573 /* Store parameter declarations passed in ARG_INFO into the current
6574 function declaration. */
6576 void
6577 store_parm_decls_from (struct c_arg_info *arg_info)
6579 current_function_arg_info = arg_info;
6580 store_parm_decls ();
6583 /* Store the parameter declarations into the current function declaration.
6584 This is called after parsing the parameter declarations, before
6585 digesting the body of the function.
6587 For an old-style definition, construct a prototype out of the old-style
6588 parameter declarations and inject it into the function's type. */
6590 void
6591 store_parm_decls (void)
6593 tree fndecl = current_function_decl;
6594 bool proto;
6596 /* The argument information block for FNDECL. */
6597 struct c_arg_info *arg_info = current_function_arg_info;
6598 current_function_arg_info = 0;
6600 /* True if this definition is written with a prototype. Note:
6601 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6602 list in a function definition as equivalent to (void) -- an
6603 empty argument list specifies the function has no parameters,
6604 but only (void) sets up a prototype for future calls. */
6605 proto = arg_info->types != 0;
6607 if (proto)
6608 store_parm_decls_newstyle (fndecl, arg_info);
6609 else
6610 store_parm_decls_oldstyle (fndecl, arg_info);
6612 /* The next call to push_scope will be a function body. */
6614 next_is_function_body = true;
6616 /* Write a record describing this function definition to the prototypes
6617 file (if requested). */
6619 gen_aux_info_record (fndecl, 1, 0, proto);
6621 /* Initialize the RTL code for the function. */
6622 allocate_struct_function (fndecl, false);
6624 /* Begin the statement tree for this function. */
6625 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6627 /* ??? Insert the contents of the pending sizes list into the function
6628 to be evaluated. The only reason left to have this is
6629 void foo(int n, int array[n++])
6630 because we throw away the array type in favor of a pointer type, and
6631 thus won't naturally see the SAVE_EXPR containing the increment. All
6632 other pending sizes would be handled by gimplify_parameters. */
6634 tree t;
6635 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6636 add_stmt (TREE_VALUE (t));
6639 /* Even though we're inside a function body, we still don't want to
6640 call expand_expr to calculate the size of a variable-sized array.
6641 We haven't necessarily assigned RTL to all variables yet, so it's
6642 not safe to try to expand expressions involving them. */
6643 cfun->dont_save_pending_sizes_p = 1;
6646 /* Emit diagnostics that require gimple input for detection. Operate on
6647 FNDECL and all its nested functions. */
6649 static void
6650 c_gimple_diagnostics_recursively (tree fndecl)
6652 struct cgraph_node *cgn;
6654 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6655 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6657 /* Notice when OpenMP structured block constraints are violated. */
6658 if (flag_openmp)
6659 diagnose_omp_structured_block_errors (fndecl);
6661 /* Finalize all nested functions now. */
6662 cgn = cgraph_node (fndecl);
6663 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6664 c_gimple_diagnostics_recursively (cgn->decl);
6667 /* Finish up a function declaration and compile that function
6668 all the way to assembler language output. The free the storage
6669 for the function definition.
6671 This is called after parsing the body of the function definition. */
6673 void
6674 finish_function (void)
6676 tree fndecl = current_function_decl;
6678 label_context_stack_se = label_context_stack_se->next;
6679 label_context_stack_vm = label_context_stack_vm->next;
6681 if (TREE_CODE (fndecl) == FUNCTION_DECL
6682 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6684 tree args = DECL_ARGUMENTS (fndecl);
6685 for (; args; args = TREE_CHAIN (args))
6687 tree type = TREE_TYPE (args);
6688 if (INTEGRAL_TYPE_P (type)
6689 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6690 DECL_ARG_TYPE (args) = integer_type_node;
6694 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6695 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6697 /* Must mark the RESULT_DECL as being in this function. */
6699 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6700 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6702 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6704 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6705 != integer_type_node)
6707 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6708 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6709 if (!warn_main)
6710 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6712 else
6714 if (flag_isoc99)
6716 tree stmt = c_finish_return (integer_zero_node);
6717 /* Hack. We don't want the middle-end to warn that this return
6718 is unreachable, so we mark its location as special. Using
6719 UNKNOWN_LOCATION has the problem that it gets clobbered in
6720 annotate_one_with_locus. A cleaner solution might be to
6721 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6723 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6728 /* Tie off the statement tree for this function. */
6729 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6731 finish_fname_decls ();
6733 /* Complain if there's just no return statement. */
6734 if (warn_return_type
6735 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6736 && !current_function_returns_value && !current_function_returns_null
6737 /* Don't complain if we are no-return. */
6738 && !current_function_returns_abnormally
6739 /* Don't warn for main(). */
6740 && !MAIN_NAME_P (DECL_NAME (fndecl))
6741 /* Or if they didn't actually specify a return type. */
6742 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6743 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6744 inline function, as we might never be compiled separately. */
6745 && DECL_INLINE (fndecl))
6747 warning (OPT_Wreturn_type,
6748 "no return statement in function returning non-void");
6749 TREE_NO_WARNING (fndecl) = 1;
6752 /* Store the end of the function, so that we get good line number
6753 info for the epilogue. */
6754 cfun->function_end_locus = input_location;
6756 /* Finalize the ELF visibility for the function. */
6757 c_determine_visibility (fndecl);
6759 /* For GNU C extern inline functions disregard inline limits. */
6760 if (DECL_EXTERNAL (fndecl)
6761 && DECL_DECLARED_INLINE_P (fndecl))
6762 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6764 /* Genericize before inlining. Delay genericizing nested functions
6765 until their parent function is genericized. Since finalizing
6766 requires GENERIC, delay that as well. */
6768 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6769 && !undef_nested_function)
6771 if (!decl_function_context (fndecl))
6773 c_genericize (fndecl);
6774 c_gimple_diagnostics_recursively (fndecl);
6776 /* ??? Objc emits functions after finalizing the compilation unit.
6777 This should be cleaned up later and this conditional removed. */
6778 if (cgraph_global_info_ready)
6780 cgraph_add_new_function (fndecl, false);
6781 return;
6784 cgraph_finalize_function (fndecl, false);
6786 else
6788 /* Register this function with cgraph just far enough to get it
6789 added to our parent's nested function list. Handy, since the
6790 C front end doesn't have such a list. */
6791 (void) cgraph_node (fndecl);
6795 if (!decl_function_context (fndecl))
6796 undef_nested_function = false;
6798 /* We're leaving the context of this function, so zap cfun.
6799 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6800 tree_rest_of_compilation. */
6801 set_cfun (NULL);
6802 current_function_decl = NULL;
6805 /* Check the declarations given in a for-loop for satisfying the C99
6806 constraints. If exactly one such decl is found, return it. */
6808 tree
6809 check_for_loop_decls (void)
6811 struct c_binding *b;
6812 tree one_decl = NULL_TREE;
6813 int n_decls = 0;
6816 if (!flag_isoc99)
6818 /* If we get here, declarations have been used in a for loop without
6819 the C99 for loop scope. This doesn't make much sense, so don't
6820 allow it. */
6821 error ("%<for%> loop initial declaration used outside C99 mode");
6822 return NULL_TREE;
6824 /* C99 subclause 6.8.5 paragraph 3:
6826 [#3] The declaration part of a for statement shall only
6827 declare identifiers for objects having storage class auto or
6828 register.
6830 It isn't clear whether, in this sentence, "identifiers" binds to
6831 "shall only declare" or to "objects" - that is, whether all identifiers
6832 declared must be identifiers for objects, or whether the restriction
6833 only applies to those that are. (A question on this in comp.std.c
6834 in November 2000 received no answer.) We implement the strictest
6835 interpretation, to avoid creating an extension which later causes
6836 problems. */
6838 for (b = current_scope->bindings; b; b = b->prev)
6840 tree id = b->id;
6841 tree decl = b->decl;
6843 if (!id)
6844 continue;
6846 switch (TREE_CODE (decl))
6848 case VAR_DECL:
6849 if (TREE_STATIC (decl))
6850 error ("declaration of static variable %q+D in %<for%> loop "
6851 "initial declaration", decl);
6852 else if (DECL_EXTERNAL (decl))
6853 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6854 "initial declaration", decl);
6855 break;
6857 case RECORD_TYPE:
6858 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6859 id);
6860 break;
6861 case UNION_TYPE:
6862 error ("%<union %E%> declared in %<for%> loop initial declaration",
6863 id);
6864 break;
6865 case ENUMERAL_TYPE:
6866 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6867 id);
6868 break;
6869 default:
6870 error ("declaration of non-variable %q+D in %<for%> loop "
6871 "initial declaration", decl);
6874 n_decls++;
6875 one_decl = decl;
6878 return n_decls == 1 ? one_decl : NULL_TREE;
6881 /* Save and reinitialize the variables
6882 used during compilation of a C function. */
6884 void
6885 c_push_function_context (void)
6887 struct language_function *p;
6888 p = GGC_NEW (struct language_function);
6889 cfun->language = p;
6891 p->base.x_stmt_tree = c_stmt_tree;
6892 p->x_break_label = c_break_label;
6893 p->x_cont_label = c_cont_label;
6894 p->x_switch_stack = c_switch_stack;
6895 p->arg_info = current_function_arg_info;
6896 p->returns_value = current_function_returns_value;
6897 p->returns_null = current_function_returns_null;
6898 p->returns_abnormally = current_function_returns_abnormally;
6899 p->warn_about_return_type = warn_about_return_type;
6901 push_function_context ();
6904 /* Restore the variables used during compilation of a C function. */
6906 void
6907 c_pop_function_context (void)
6909 struct language_function *p;
6911 pop_function_context ();
6912 p = cfun->language;
6913 cfun->language = NULL;
6915 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6916 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6918 /* Stop pointing to the local nodes about to be freed. */
6919 /* But DECL_INITIAL must remain nonzero so we know this
6920 was an actual function definition. */
6921 DECL_INITIAL (current_function_decl) = error_mark_node;
6922 DECL_ARGUMENTS (current_function_decl) = 0;
6925 c_stmt_tree = p->base.x_stmt_tree;
6926 c_break_label = p->x_break_label;
6927 c_cont_label = p->x_cont_label;
6928 c_switch_stack = p->x_switch_stack;
6929 current_function_arg_info = p->arg_info;
6930 current_function_returns_value = p->returns_value;
6931 current_function_returns_null = p->returns_null;
6932 current_function_returns_abnormally = p->returns_abnormally;
6933 warn_about_return_type = p->warn_about_return_type;
6936 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6938 void
6939 c_dup_lang_specific_decl (tree decl)
6941 struct lang_decl *ld;
6943 if (!DECL_LANG_SPECIFIC (decl))
6944 return;
6946 ld = GGC_NEW (struct lang_decl);
6947 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6948 DECL_LANG_SPECIFIC (decl) = ld;
6951 /* The functions below are required for functionality of doing
6952 function at once processing in the C front end. Currently these
6953 functions are not called from anywhere in the C front end, but as
6954 these changes continue, that will change. */
6956 /* Returns the stmt_tree (if any) to which statements are currently
6957 being added. If there is no active statement-tree, NULL is
6958 returned. */
6960 stmt_tree
6961 current_stmt_tree (void)
6963 return &c_stmt_tree;
6966 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6967 C. */
6970 anon_aggr_type_p (const_tree ARG_UNUSED (node))
6972 return 0;
6975 /* Return the global value of T as a symbol. */
6977 tree
6978 identifier_global_value (tree t)
6980 struct c_binding *b;
6982 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6983 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6984 return b->decl;
6986 return 0;
6989 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6990 otherwise the name is found in ridpointers from RID_INDEX. */
6992 void
6993 record_builtin_type (enum rid rid_index, const char *name, tree type)
6995 tree id, decl;
6996 if (name == 0)
6997 id = ridpointers[(int) rid_index];
6998 else
6999 id = get_identifier (name);
7000 decl = build_decl (TYPE_DECL, id, type);
7001 pushdecl (decl);
7002 if (debug_hooks->type_decl)
7003 debug_hooks->type_decl (decl, false);
7006 /* Build the void_list_node (void_type_node having been created). */
7007 tree
7008 build_void_list_node (void)
7010 tree t = build_tree_list (NULL_TREE, void_type_node);
7011 return t;
7014 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7016 struct c_parm *
7017 build_c_parm (struct c_declspecs *specs, tree attrs,
7018 struct c_declarator *declarator)
7020 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7021 ret->specs = specs;
7022 ret->attrs = attrs;
7023 ret->declarator = declarator;
7024 return ret;
7027 /* Return a declarator with nested attributes. TARGET is the inner
7028 declarator to which these attributes apply. ATTRS are the
7029 attributes. */
7031 struct c_declarator *
7032 build_attrs_declarator (tree attrs, struct c_declarator *target)
7034 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7035 ret->kind = cdk_attrs;
7036 ret->declarator = target;
7037 ret->u.attrs = attrs;
7038 return ret;
7041 /* Return a declarator for a function with arguments specified by ARGS
7042 and return type specified by TARGET. */
7044 struct c_declarator *
7045 build_function_declarator (struct c_arg_info *args,
7046 struct c_declarator *target)
7048 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7049 ret->kind = cdk_function;
7050 ret->declarator = target;
7051 ret->u.arg_info = args;
7052 return ret;
7055 /* Return a declarator for the identifier IDENT (which may be
7056 NULL_TREE for an abstract declarator). */
7058 struct c_declarator *
7059 build_id_declarator (tree ident)
7061 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7062 ret->kind = cdk_id;
7063 ret->declarator = 0;
7064 ret->u.id = ident;
7065 /* Default value - may get reset to a more precise location. */
7066 ret->id_loc = input_location;
7067 return ret;
7070 /* Return something to represent absolute declarators containing a *.
7071 TARGET is the absolute declarator that the * contains.
7072 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7073 to apply to the pointer type. */
7075 struct c_declarator *
7076 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7077 struct c_declarator *target)
7079 tree attrs;
7080 int quals = 0;
7081 struct c_declarator *itarget = target;
7082 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7083 if (type_quals_attrs)
7085 attrs = type_quals_attrs->attrs;
7086 quals = quals_from_declspecs (type_quals_attrs);
7087 if (attrs != NULL_TREE)
7088 itarget = build_attrs_declarator (attrs, target);
7090 ret->kind = cdk_pointer;
7091 ret->declarator = itarget;
7092 ret->u.pointer_quals = quals;
7093 return ret;
7096 /* Return a pointer to a structure for an empty list of declaration
7097 specifiers. */
7099 struct c_declspecs *
7100 build_null_declspecs (void)
7102 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7103 ret->type = 0;
7104 ret->decl_attr = 0;
7105 ret->attrs = 0;
7106 ret->typespec_word = cts_none;
7107 ret->storage_class = csc_none;
7108 ret->declspecs_seen_p = false;
7109 ret->type_seen_p = false;
7110 ret->non_sc_seen_p = false;
7111 ret->typedef_p = false;
7112 ret->tag_defined_p = false;
7113 ret->explicit_signed_p = false;
7114 ret->deprecated_p = false;
7115 ret->default_int_p = false;
7116 ret->long_p = false;
7117 ret->long_long_p = false;
7118 ret->short_p = false;
7119 ret->signed_p = false;
7120 ret->unsigned_p = false;
7121 ret->complex_p = false;
7122 ret->inline_p = false;
7123 ret->thread_p = false;
7124 ret->const_p = false;
7125 ret->volatile_p = false;
7126 ret->restrict_p = false;
7127 ret->saturating_p = false;
7128 return ret;
7131 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7132 returning SPECS. */
7134 struct c_declspecs *
7135 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7137 enum rid i;
7138 bool dupe = false;
7139 specs->non_sc_seen_p = true;
7140 specs->declspecs_seen_p = true;
7141 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7142 && C_IS_RESERVED_WORD (qual));
7143 i = C_RID_CODE (qual);
7144 switch (i)
7146 case RID_CONST:
7147 dupe = specs->const_p;
7148 specs->const_p = true;
7149 break;
7150 case RID_VOLATILE:
7151 dupe = specs->volatile_p;
7152 specs->volatile_p = true;
7153 break;
7154 case RID_RESTRICT:
7155 dupe = specs->restrict_p;
7156 specs->restrict_p = true;
7157 break;
7158 default:
7159 gcc_unreachable ();
7161 if (dupe && pedantic && !flag_isoc99)
7162 pedwarn ("duplicate %qE", qual);
7163 return specs;
7166 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7167 returning SPECS. */
7169 struct c_declspecs *
7170 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7172 tree type = spec.spec;
7173 specs->non_sc_seen_p = true;
7174 specs->declspecs_seen_p = true;
7175 specs->type_seen_p = true;
7176 if (TREE_DEPRECATED (type))
7177 specs->deprecated_p = true;
7179 /* Handle type specifier keywords. */
7180 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7182 enum rid i = C_RID_CODE (type);
7183 if (specs->type)
7185 error ("two or more data types in declaration specifiers");
7186 return specs;
7188 if ((int) i <= (int) RID_LAST_MODIFIER)
7190 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7191 bool dupe = false;
7192 switch (i)
7194 case RID_LONG:
7195 if (specs->long_long_p)
7197 error ("%<long long long%> is too long for GCC");
7198 break;
7200 if (specs->long_p)
7202 if (specs->typespec_word == cts_double)
7204 error ("both %<long long%> and %<double%> in "
7205 "declaration specifiers");
7206 break;
7208 if (pedantic && !flag_isoc99 && !in_system_header
7209 && warn_long_long)
7210 pedwarn ("ISO C90 does not support %<long long%>");
7211 specs->long_long_p = 1;
7212 break;
7214 if (specs->short_p)
7215 error ("both %<long%> and %<short%> in "
7216 "declaration specifiers");
7217 else if (specs->typespec_word == cts_void)
7218 error ("both %<long%> and %<void%> in "
7219 "declaration specifiers");
7220 else if (specs->typespec_word == cts_bool)
7221 error ("both %<long%> and %<_Bool%> in "
7222 "declaration specifiers");
7223 else if (specs->typespec_word == cts_char)
7224 error ("both %<long%> and %<char%> in "
7225 "declaration specifiers");
7226 else if (specs->typespec_word == cts_float)
7227 error ("both %<long%> and %<float%> in "
7228 "declaration specifiers");
7229 else if (specs->typespec_word == cts_dfloat32)
7230 error ("both %<long%> and %<_Decimal32%> in "
7231 "declaration specifiers");
7232 else if (specs->typespec_word == cts_dfloat64)
7233 error ("both %<long%> and %<_Decimal64%> in "
7234 "declaration specifiers");
7235 else if (specs->typespec_word == cts_dfloat128)
7236 error ("both %<long%> and %<_Decimal128%> in "
7237 "declaration specifiers");
7238 else
7239 specs->long_p = true;
7240 break;
7241 case RID_SHORT:
7242 dupe = specs->short_p;
7243 if (specs->long_p)
7244 error ("both %<long%> and %<short%> in "
7245 "declaration specifiers");
7246 else if (specs->typespec_word == cts_void)
7247 error ("both %<short%> and %<void%> in "
7248 "declaration specifiers");
7249 else if (specs->typespec_word == cts_bool)
7250 error ("both %<short%> and %<_Bool%> in "
7251 "declaration specifiers");
7252 else if (specs->typespec_word == cts_char)
7253 error ("both %<short%> and %<char%> in "
7254 "declaration specifiers");
7255 else if (specs->typespec_word == cts_float)
7256 error ("both %<short%> and %<float%> in "
7257 "declaration specifiers");
7258 else if (specs->typespec_word == cts_double)
7259 error ("both %<short%> and %<double%> in "
7260 "declaration specifiers");
7261 else if (specs->typespec_word == cts_dfloat32)
7262 error ("both %<short%> and %<_Decimal32%> in "
7263 "declaration specifiers");
7264 else if (specs->typespec_word == cts_dfloat64)
7265 error ("both %<short%> and %<_Decimal64%> in "
7266 "declaration specifiers");
7267 else if (specs->typespec_word == cts_dfloat128)
7268 error ("both %<short%> and %<_Decimal128%> in "
7269 "declaration specifiers");
7270 else
7271 specs->short_p = true;
7272 break;
7273 case RID_SIGNED:
7274 dupe = specs->signed_p;
7275 if (specs->unsigned_p)
7276 error ("both %<signed%> and %<unsigned%> in "
7277 "declaration specifiers");
7278 else if (specs->typespec_word == cts_void)
7279 error ("both %<signed%> and %<void%> in "
7280 "declaration specifiers");
7281 else if (specs->typespec_word == cts_bool)
7282 error ("both %<signed%> and %<_Bool%> in "
7283 "declaration specifiers");
7284 else if (specs->typespec_word == cts_float)
7285 error ("both %<signed%> and %<float%> in "
7286 "declaration specifiers");
7287 else if (specs->typespec_word == cts_double)
7288 error ("both %<signed%> and %<double%> in "
7289 "declaration specifiers");
7290 else if (specs->typespec_word == cts_dfloat32)
7291 error ("both %<signed%> and %<_Decimal32%> in "
7292 "declaration specifiers");
7293 else if (specs->typespec_word == cts_dfloat64)
7294 error ("both %<signed%> and %<_Decimal64%> in "
7295 "declaration specifiers");
7296 else if (specs->typespec_word == cts_dfloat128)
7297 error ("both %<signed%> and %<_Decimal128%> in "
7298 "declaration specifiers");
7299 else
7300 specs->signed_p = true;
7301 break;
7302 case RID_UNSIGNED:
7303 dupe = specs->unsigned_p;
7304 if (specs->signed_p)
7305 error ("both %<signed%> and %<unsigned%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_void)
7308 error ("both %<unsigned%> and %<void%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_bool)
7311 error ("both %<unsigned%> and %<_Bool%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_float)
7314 error ("both %<unsigned%> and %<float%> in "
7315 "declaration specifiers");
7316 else if (specs->typespec_word == cts_double)
7317 error ("both %<unsigned%> and %<double%> in "
7318 "declaration specifiers");
7319 else if (specs->typespec_word == cts_dfloat32)
7320 error ("both %<unsigned%> and %<_Decimal32%> in "
7321 "declaration specifiers");
7322 else if (specs->typespec_word == cts_dfloat64)
7323 error ("both %<unsigned%> and %<_Decimal64%> in "
7324 "declaration specifiers");
7325 else if (specs->typespec_word == cts_dfloat128)
7326 error ("both %<unsigned%> and %<_Decimal128%> in "
7327 "declaration specifiers");
7328 else
7329 specs->unsigned_p = true;
7330 break;
7331 case RID_COMPLEX:
7332 dupe = specs->complex_p;
7333 if (pedantic && !flag_isoc99 && !in_system_header)
7334 pedwarn ("ISO C90 does not support complex types");
7335 if (specs->typespec_word == cts_void)
7336 error ("both %<complex%> and %<void%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_bool)
7339 error ("both %<complex%> and %<_Bool%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_dfloat32)
7342 error ("both %<complex%> and %<_Decimal32%> in "
7343 "declaration specifiers");
7344 else if (specs->typespec_word == cts_dfloat64)
7345 error ("both %<complex%> and %<_Decimal64%> in "
7346 "declaration specifiers");
7347 else if (specs->typespec_word == cts_dfloat128)
7348 error ("both %<complex%> and %<_Decimal128%> in "
7349 "declaration specifiers");
7350 else if (specs->typespec_word == cts_fract)
7351 error ("both %<complex%> and %<_Fract%> in "
7352 "declaration specifiers");
7353 else if (specs->typespec_word == cts_accum)
7354 error ("both %<complex%> and %<_Accum%> in "
7355 "declaration specifiers");
7356 else if (specs->saturating_p)
7357 error ("both %<complex%> and %<_Sat%> in "
7358 "declaration specifiers");
7359 else
7360 specs->complex_p = true;
7361 break;
7362 case RID_SAT:
7363 dupe = specs->saturating_p;
7364 if (pedantic)
7365 pedwarn ("ISO C does not support saturating types");
7366 if (specs->typespec_word == cts_void)
7367 error ("both %<_Sat%> and %<void%> in "
7368 "declaration specifiers");
7369 else if (specs->typespec_word == cts_bool)
7370 error ("both %<_Sat%> and %<_Bool%> in "
7371 "declaration specifiers");
7372 else if (specs->typespec_word == cts_char)
7373 error ("both %<_Sat%> and %<char%> in "
7374 "declaration specifiers");
7375 else if (specs->typespec_word == cts_int)
7376 error ("both %<_Sat%> and %<int%> in "
7377 "declaration specifiers");
7378 else if (specs->typespec_word == cts_float)
7379 error ("both %<_Sat%> and %<float%> in "
7380 "declaration specifiers");
7381 else if (specs->typespec_word == cts_double)
7382 error ("both %<_Sat%> and %<double%> in "
7383 "declaration specifiers");
7384 else if (specs->typespec_word == cts_dfloat32)
7385 error ("both %<_Sat%> and %<_Decimal32%> in "
7386 "declaration specifiers");
7387 else if (specs->typespec_word == cts_dfloat64)
7388 error ("both %<_Sat%> and %<_Decimal64%> in "
7389 "declaration specifiers");
7390 else if (specs->typespec_word == cts_dfloat128)
7391 error ("both %<_Sat%> and %<_Decimal128%> in "
7392 "declaration specifiers");
7393 else if (specs->complex_p)
7394 error ("both %<_Sat%> and %<complex%> in "
7395 "declaration specifiers");
7396 else
7397 specs->saturating_p = true;
7398 break;
7399 default:
7400 gcc_unreachable ();
7403 if (dupe)
7404 error ("duplicate %qE", type);
7406 return specs;
7408 else
7410 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7411 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7412 if (specs->typespec_word != cts_none)
7414 error ("two or more data types in declaration specifiers");
7415 return specs;
7417 switch (i)
7419 case RID_VOID:
7420 if (specs->long_p)
7421 error ("both %<long%> and %<void%> in "
7422 "declaration specifiers");
7423 else if (specs->short_p)
7424 error ("both %<short%> and %<void%> in "
7425 "declaration specifiers");
7426 else if (specs->signed_p)
7427 error ("both %<signed%> and %<void%> in "
7428 "declaration specifiers");
7429 else if (specs->unsigned_p)
7430 error ("both %<unsigned%> and %<void%> in "
7431 "declaration specifiers");
7432 else if (specs->complex_p)
7433 error ("both %<complex%> and %<void%> in "
7434 "declaration specifiers");
7435 else if (specs->saturating_p)
7436 error ("both %<_Sat%> and %<void%> in "
7437 "declaration specifiers");
7438 else
7439 specs->typespec_word = cts_void;
7440 return specs;
7441 case RID_BOOL:
7442 if (specs->long_p)
7443 error ("both %<long%> and %<_Bool%> in "
7444 "declaration specifiers");
7445 else if (specs->short_p)
7446 error ("both %<short%> and %<_Bool%> in "
7447 "declaration specifiers");
7448 else if (specs->signed_p)
7449 error ("both %<signed%> and %<_Bool%> in "
7450 "declaration specifiers");
7451 else if (specs->unsigned_p)
7452 error ("both %<unsigned%> and %<_Bool%> in "
7453 "declaration specifiers");
7454 else if (specs->complex_p)
7455 error ("both %<complex%> and %<_Bool%> in "
7456 "declaration specifiers");
7457 else if (specs->saturating_p)
7458 error ("both %<_Sat%> and %<_Bool%> in "
7459 "declaration specifiers");
7460 else
7461 specs->typespec_word = cts_bool;
7462 return specs;
7463 case RID_CHAR:
7464 if (specs->long_p)
7465 error ("both %<long%> and %<char%> in "
7466 "declaration specifiers");
7467 else if (specs->short_p)
7468 error ("both %<short%> and %<char%> in "
7469 "declaration specifiers");
7470 else if (specs->saturating_p)
7471 error ("both %<_Sat%> and %<char%> in "
7472 "declaration specifiers");
7473 else
7474 specs->typespec_word = cts_char;
7475 return specs;
7476 case RID_INT:
7477 if (specs->saturating_p)
7478 error ("both %<_Sat%> and %<int%> in "
7479 "declaration specifiers");
7480 else
7481 specs->typespec_word = cts_int;
7482 return specs;
7483 case RID_FLOAT:
7484 if (specs->long_p)
7485 error ("both %<long%> and %<float%> in "
7486 "declaration specifiers");
7487 else if (specs->short_p)
7488 error ("both %<short%> and %<float%> in "
7489 "declaration specifiers");
7490 else if (specs->signed_p)
7491 error ("both %<signed%> and %<float%> in "
7492 "declaration specifiers");
7493 else if (specs->unsigned_p)
7494 error ("both %<unsigned%> and %<float%> in "
7495 "declaration specifiers");
7496 else if (specs->saturating_p)
7497 error ("both %<_Sat%> and %<float%> in "
7498 "declaration specifiers");
7499 else
7500 specs->typespec_word = cts_float;
7501 return specs;
7502 case RID_DOUBLE:
7503 if (specs->long_long_p)
7504 error ("both %<long long%> and %<double%> in "
7505 "declaration specifiers");
7506 else if (specs->short_p)
7507 error ("both %<short%> and %<double%> in "
7508 "declaration specifiers");
7509 else if (specs->signed_p)
7510 error ("both %<signed%> and %<double%> in "
7511 "declaration specifiers");
7512 else if (specs->unsigned_p)
7513 error ("both %<unsigned%> and %<double%> in "
7514 "declaration specifiers");
7515 else if (specs->saturating_p)
7516 error ("both %<_Sat%> and %<double%> in "
7517 "declaration specifiers");
7518 else
7519 specs->typespec_word = cts_double;
7520 return specs;
7521 case RID_DFLOAT32:
7522 case RID_DFLOAT64:
7523 case RID_DFLOAT128:
7525 const char *str;
7526 if (i == RID_DFLOAT32)
7527 str = "_Decimal32";
7528 else if (i == RID_DFLOAT64)
7529 str = "_Decimal64";
7530 else
7531 str = "_Decimal128";
7532 if (specs->long_long_p)
7533 error ("both %<long long%> and %<%s%> in "
7534 "declaration specifiers", str);
7535 if (specs->long_p)
7536 error ("both %<long%> and %<%s%> in "
7537 "declaration specifiers", str);
7538 else if (specs->short_p)
7539 error ("both %<short%> and %<%s%> in "
7540 "declaration specifiers", str);
7541 else if (specs->signed_p)
7542 error ("both %<signed%> and %<%s%> in "
7543 "declaration specifiers", str);
7544 else if (specs->unsigned_p)
7545 error ("both %<unsigned%> and %<%s%> in "
7546 "declaration specifiers", str);
7547 else if (specs->complex_p)
7548 error ("both %<complex%> and %<%s%> in "
7549 "declaration specifiers", str);
7550 else if (specs->saturating_p)
7551 error ("both %<_Sat%> and %<%s%> in "
7552 "declaration specifiers", str);
7553 else if (i == RID_DFLOAT32)
7554 specs->typespec_word = cts_dfloat32;
7555 else if (i == RID_DFLOAT64)
7556 specs->typespec_word = cts_dfloat64;
7557 else
7558 specs->typespec_word = cts_dfloat128;
7560 if (!targetm.decimal_float_supported_p ())
7561 error ("decimal floating point not supported for this target");
7562 if (pedantic)
7563 pedwarn ("ISO C does not support decimal floating point");
7564 return specs;
7565 case RID_FRACT:
7566 case RID_ACCUM:
7568 const char *str;
7569 if (i == RID_FRACT)
7570 str = "_Fract";
7571 else
7572 str = "_Accum";
7573 if (specs->complex_p)
7574 error ("both %<complex%> and %<%s%> in "
7575 "declaration specifiers", str);
7576 else if (i == RID_FRACT)
7577 specs->typespec_word = cts_fract;
7578 else
7579 specs->typespec_word = cts_accum;
7581 if (!targetm.fixed_point_supported_p ())
7582 error ("fixed-point types not supported for this target");
7583 if (pedantic)
7584 pedwarn ("ISO C does not support fixed-point types");
7585 return specs;
7586 default:
7587 /* ObjC reserved word "id", handled below. */
7588 break;
7593 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7594 form of ObjC type, cases such as "int" and "long" being handled
7595 above), a TYPE (struct, union, enum and typeof specifiers) or an
7596 ERROR_MARK. In none of these cases may there have previously
7597 been any type specifiers. */
7598 if (specs->type || specs->typespec_word != cts_none
7599 || specs->long_p || specs->short_p || specs->signed_p
7600 || specs->unsigned_p || specs->complex_p)
7601 error ("two or more data types in declaration specifiers");
7602 else if (TREE_CODE (type) == TYPE_DECL)
7604 if (TREE_TYPE (type) == error_mark_node)
7605 ; /* Allow the type to default to int to avoid cascading errors. */
7606 else
7608 specs->type = TREE_TYPE (type);
7609 specs->decl_attr = DECL_ATTRIBUTES (type);
7610 specs->typedef_p = true;
7611 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7614 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7616 tree t = lookup_name (type);
7617 if (!t || TREE_CODE (t) != TYPE_DECL)
7618 error ("%qE fails to be a typedef or built in type", type);
7619 else if (TREE_TYPE (t) == error_mark_node)
7621 else
7622 specs->type = TREE_TYPE (t);
7624 else if (TREE_CODE (type) != ERROR_MARK)
7626 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7627 specs->tag_defined_p = true;
7628 if (spec.kind == ctsk_typeof)
7629 specs->typedef_p = true;
7630 specs->type = type;
7633 return specs;
7636 /* Add the storage class specifier or function specifier SCSPEC to the
7637 declaration specifiers SPECS, returning SPECS. */
7639 struct c_declspecs *
7640 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7642 enum rid i;
7643 enum c_storage_class n = csc_none;
7644 bool dupe = false;
7645 specs->declspecs_seen_p = true;
7646 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7647 && C_IS_RESERVED_WORD (scspec));
7648 i = C_RID_CODE (scspec);
7649 if (specs->non_sc_seen_p)
7650 warning (OPT_Wold_style_declaration,
7651 "%qE is not at beginning of declaration", scspec);
7652 switch (i)
7654 case RID_INLINE:
7655 /* C99 permits duplicate inline. Although of doubtful utility,
7656 it seems simplest to permit it in gnu89 mode as well, as
7657 there is also little utility in maintaining this as a
7658 difference between gnu89 and C99 inline. */
7659 dupe = false;
7660 specs->inline_p = true;
7661 break;
7662 case RID_THREAD:
7663 dupe = specs->thread_p;
7664 if (specs->storage_class == csc_auto)
7665 error ("%<__thread%> used with %<auto%>");
7666 else if (specs->storage_class == csc_register)
7667 error ("%<__thread%> used with %<register%>");
7668 else if (specs->storage_class == csc_typedef)
7669 error ("%<__thread%> used with %<typedef%>");
7670 else
7671 specs->thread_p = true;
7672 break;
7673 case RID_AUTO:
7674 n = csc_auto;
7675 break;
7676 case RID_EXTERN:
7677 n = csc_extern;
7678 /* Diagnose "__thread extern". */
7679 if (specs->thread_p)
7680 error ("%<__thread%> before %<extern%>");
7681 break;
7682 case RID_REGISTER:
7683 n = csc_register;
7684 break;
7685 case RID_STATIC:
7686 n = csc_static;
7687 /* Diagnose "__thread static". */
7688 if (specs->thread_p)
7689 error ("%<__thread%> before %<static%>");
7690 break;
7691 case RID_TYPEDEF:
7692 n = csc_typedef;
7693 break;
7694 default:
7695 gcc_unreachable ();
7697 if (n != csc_none && n == specs->storage_class)
7698 dupe = true;
7699 if (dupe)
7700 error ("duplicate %qE", scspec);
7701 if (n != csc_none)
7703 if (specs->storage_class != csc_none && n != specs->storage_class)
7705 error ("multiple storage classes in declaration specifiers");
7707 else
7709 specs->storage_class = n;
7710 if (n != csc_extern && n != csc_static && specs->thread_p)
7712 error ("%<__thread%> used with %qE", scspec);
7713 specs->thread_p = false;
7717 return specs;
7720 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7721 returning SPECS. */
7723 struct c_declspecs *
7724 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7726 specs->attrs = chainon (attrs, specs->attrs);
7727 specs->declspecs_seen_p = true;
7728 return specs;
7731 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7732 specifiers with any other type specifier to determine the resulting
7733 type. This is where ISO C checks on complex types are made, since
7734 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7735 double". */
7737 struct c_declspecs *
7738 finish_declspecs (struct c_declspecs *specs)
7740 /* If a type was specified as a whole, we have no modifiers and are
7741 done. */
7742 if (specs->type != NULL_TREE)
7744 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7745 && !specs->signed_p && !specs->unsigned_p
7746 && !specs->complex_p);
7747 return specs;
7750 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7751 has been specified, treat it as "int" unless "_Complex" is
7752 present and there are no other specifiers. If we just have
7753 "_Complex", it is equivalent to "_Complex double", but e.g.
7754 "_Complex short" is equivalent to "_Complex short int". */
7755 if (specs->typespec_word == cts_none)
7757 if (specs->saturating_p)
7759 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7760 specs->typespec_word = cts_fract;
7762 else if (specs->long_p || specs->short_p
7763 || specs->signed_p || specs->unsigned_p)
7765 specs->typespec_word = cts_int;
7767 else if (specs->complex_p)
7769 specs->typespec_word = cts_double;
7770 if (pedantic)
7771 pedwarn ("ISO C does not support plain %<complex%> meaning "
7772 "%<double complex%>");
7774 else
7776 specs->typespec_word = cts_int;
7777 specs->default_int_p = true;
7778 /* We don't diagnose this here because grokdeclarator will
7779 give more specific diagnostics according to whether it is
7780 a function definition. */
7784 /* If "signed" was specified, record this to distinguish "int" and
7785 "signed int" in the case of a bit-field with
7786 -funsigned-bitfields. */
7787 specs->explicit_signed_p = specs->signed_p;
7789 /* Now compute the actual type. */
7790 switch (specs->typespec_word)
7792 case cts_void:
7793 gcc_assert (!specs->long_p && !specs->short_p
7794 && !specs->signed_p && !specs->unsigned_p
7795 && !specs->complex_p);
7796 specs->type = void_type_node;
7797 break;
7798 case cts_bool:
7799 gcc_assert (!specs->long_p && !specs->short_p
7800 && !specs->signed_p && !specs->unsigned_p
7801 && !specs->complex_p);
7802 specs->type = boolean_type_node;
7803 break;
7804 case cts_char:
7805 gcc_assert (!specs->long_p && !specs->short_p);
7806 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7807 if (specs->signed_p)
7808 specs->type = signed_char_type_node;
7809 else if (specs->unsigned_p)
7810 specs->type = unsigned_char_type_node;
7811 else
7812 specs->type = char_type_node;
7813 if (specs->complex_p)
7815 if (pedantic)
7816 pedwarn ("ISO C does not support complex integer types");
7817 specs->type = build_complex_type (specs->type);
7819 break;
7820 case cts_int:
7821 gcc_assert (!(specs->long_p && specs->short_p));
7822 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7823 if (specs->long_long_p)
7824 specs->type = (specs->unsigned_p
7825 ? long_long_unsigned_type_node
7826 : long_long_integer_type_node);
7827 else if (specs->long_p)
7828 specs->type = (specs->unsigned_p
7829 ? long_unsigned_type_node
7830 : long_integer_type_node);
7831 else if (specs->short_p)
7832 specs->type = (specs->unsigned_p
7833 ? short_unsigned_type_node
7834 : short_integer_type_node);
7835 else
7836 specs->type = (specs->unsigned_p
7837 ? unsigned_type_node
7838 : integer_type_node);
7839 if (specs->complex_p)
7841 if (pedantic)
7842 pedwarn ("ISO C does not support complex integer types");
7843 specs->type = build_complex_type (specs->type);
7845 break;
7846 case cts_float:
7847 gcc_assert (!specs->long_p && !specs->short_p
7848 && !specs->signed_p && !specs->unsigned_p);
7849 specs->type = (specs->complex_p
7850 ? complex_float_type_node
7851 : float_type_node);
7852 break;
7853 case cts_double:
7854 gcc_assert (!specs->long_long_p && !specs->short_p
7855 && !specs->signed_p && !specs->unsigned_p);
7856 if (specs->long_p)
7858 specs->type = (specs->complex_p
7859 ? complex_long_double_type_node
7860 : long_double_type_node);
7862 else
7864 specs->type = (specs->complex_p
7865 ? complex_double_type_node
7866 : double_type_node);
7868 break;
7869 case cts_dfloat32:
7870 case cts_dfloat64:
7871 case cts_dfloat128:
7872 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7873 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7874 if (specs->typespec_word == cts_dfloat32)
7875 specs->type = dfloat32_type_node;
7876 else if (specs->typespec_word == cts_dfloat64)
7877 specs->type = dfloat64_type_node;
7878 else
7879 specs->type = dfloat128_type_node;
7880 break;
7881 case cts_fract:
7882 gcc_assert (!specs->complex_p);
7883 if (specs->saturating_p)
7885 if (specs->long_long_p)
7886 specs->type = specs->unsigned_p
7887 ? sat_unsigned_long_long_fract_type_node
7888 : sat_long_long_fract_type_node;
7889 else if (specs->long_p)
7890 specs->type = specs->unsigned_p
7891 ? sat_unsigned_long_fract_type_node
7892 : sat_long_fract_type_node;
7893 else if (specs->short_p)
7894 specs->type = specs->unsigned_p
7895 ? sat_unsigned_short_fract_type_node
7896 : sat_short_fract_type_node;
7897 else
7898 specs->type = specs->unsigned_p
7899 ? sat_unsigned_fract_type_node
7900 : sat_fract_type_node;
7902 else
7904 if (specs->long_long_p)
7905 specs->type = specs->unsigned_p
7906 ? unsigned_long_long_fract_type_node
7907 : long_long_fract_type_node;
7908 else if (specs->long_p)
7909 specs->type = specs->unsigned_p
7910 ? unsigned_long_fract_type_node
7911 : long_fract_type_node;
7912 else if (specs->short_p)
7913 specs->type = specs->unsigned_p
7914 ? unsigned_short_fract_type_node
7915 : short_fract_type_node;
7916 else
7917 specs->type = specs->unsigned_p
7918 ? unsigned_fract_type_node
7919 : fract_type_node;
7921 break;
7922 case cts_accum:
7923 gcc_assert (!specs->complex_p);
7924 if (specs->saturating_p)
7926 if (specs->long_long_p)
7927 specs->type = specs->unsigned_p
7928 ? sat_unsigned_long_long_accum_type_node
7929 : sat_long_long_accum_type_node;
7930 else if (specs->long_p)
7931 specs->type = specs->unsigned_p
7932 ? sat_unsigned_long_accum_type_node
7933 : sat_long_accum_type_node;
7934 else if (specs->short_p)
7935 specs->type = specs->unsigned_p
7936 ? sat_unsigned_short_accum_type_node
7937 : sat_short_accum_type_node;
7938 else
7939 specs->type = specs->unsigned_p
7940 ? sat_unsigned_accum_type_node
7941 : sat_accum_type_node;
7943 else
7945 if (specs->long_long_p)
7946 specs->type = specs->unsigned_p
7947 ? unsigned_long_long_accum_type_node
7948 : long_long_accum_type_node;
7949 else if (specs->long_p)
7950 specs->type = specs->unsigned_p
7951 ? unsigned_long_accum_type_node
7952 : long_accum_type_node;
7953 else if (specs->short_p)
7954 specs->type = specs->unsigned_p
7955 ? unsigned_short_accum_type_node
7956 : short_accum_type_node;
7957 else
7958 specs->type = specs->unsigned_p
7959 ? unsigned_accum_type_node
7960 : accum_type_node;
7962 break;
7963 default:
7964 gcc_unreachable ();
7967 return specs;
7970 /* A subroutine of c_write_global_declarations. Perform final processing
7971 on one file scope's declarations (or the external scope's declarations),
7972 GLOBALS. */
7974 static void
7975 c_write_global_declarations_1 (tree globals)
7977 tree decl;
7978 bool reconsider;
7980 /* Process the decls in the order they were written. */
7981 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7983 /* Check for used but undefined static functions using the C
7984 standard's definition of "used", and set TREE_NO_WARNING so
7985 that check_global_declarations doesn't repeat the check. */
7986 if (TREE_CODE (decl) == FUNCTION_DECL
7987 && DECL_INITIAL (decl) == 0
7988 && DECL_EXTERNAL (decl)
7989 && !TREE_PUBLIC (decl)
7990 && C_DECL_USED (decl))
7992 pedwarn ("%q+F used but never defined", decl);
7993 TREE_NO_WARNING (decl) = 1;
7996 wrapup_global_declaration_1 (decl);
8001 reconsider = false;
8002 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8003 reconsider |= wrapup_global_declaration_2 (decl);
8005 while (reconsider);
8007 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8008 check_global_declaration_1 (decl);
8011 /* A subroutine of c_write_global_declarations Emit debug information for each
8012 of the declarations in GLOBALS. */
8014 static void
8015 c_write_global_declarations_2 (tree globals)
8017 tree decl;
8019 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8020 debug_hooks->global_decl (decl);
8023 /* Preserve the external declarations scope across a garbage collect. */
8024 static GTY(()) tree ext_block;
8026 void
8027 c_write_global_declarations (void)
8029 tree t;
8031 /* We don't want to do this if generating a PCH. */
8032 if (pch_file)
8033 return;
8035 /* Don't waste time on further processing if -fsyntax-only or we've
8036 encountered errors. */
8037 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8038 return;
8040 /* Close the external scope. */
8041 ext_block = pop_scope ();
8042 external_scope = 0;
8043 gcc_assert (!current_scope);
8045 if (ext_block)
8047 tree tmp = BLOCK_VARS (ext_block);
8048 int flags;
8049 FILE * stream = dump_begin (TDI_tu, &flags);
8050 if (stream && tmp)
8052 dump_node (tmp, flags & ~TDF_SLIM, stream);
8053 dump_end (TDI_tu, stream);
8057 /* Process all file scopes in this compilation, and the external_scope,
8058 through wrapup_global_declarations and check_global_declarations. */
8059 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8060 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8061 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8063 /* We're done parsing; proceed to optimize and emit assembly.
8064 FIXME: shouldn't be the front end's responsibility to call this. */
8065 cgraph_optimize ();
8067 /* After cgraph has had a chance to emit everything that's going to
8068 be emitted, output debug information for globals. */
8069 if (errorcount == 0 && sorrycount == 0)
8071 timevar_push (TV_SYMOUT);
8072 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8073 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8074 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8075 timevar_pop (TV_SYMOUT);
8078 ext_block = NULL;
8081 #include "gt-c-decl.h"