2008-05-20 Kai Tietz <kai.tietz@onevision.com>
[official-gcc.git] / gcc / c-decl.c
blob9abb4dfc65f2de9973372ea8b1d3c62d2b64e25b
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 DECL_EXTERNAL (newdecl) = 0;
1769 if (DECL_EXTERNAL (newdecl))
1771 TREE_STATIC (newdecl) = TREE_STATIC (olddecl);
1772 DECL_EXTERNAL (newdecl) = DECL_EXTERNAL (olddecl);
1774 /* An extern decl does not override previous storage class. */
1775 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1776 if (!DECL_EXTERNAL (newdecl))
1778 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1779 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1782 else
1784 TREE_STATIC (olddecl) = TREE_STATIC (newdecl);
1785 TREE_PUBLIC (olddecl) = TREE_PUBLIC (newdecl);
1788 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1790 /* If we're redefining a function previously defined as extern
1791 inline, make sure we emit debug info for the inline before we
1792 throw it away, in case it was inlined into a function that
1793 hasn't been written out yet. */
1794 if (new_is_definition && DECL_INITIAL (olddecl))
1796 if (TREE_USED (olddecl)
1797 /* In unit-at-a-time mode we never inline re-defined extern
1798 inline functions. */
1799 && !flag_unit_at_a_time
1800 && cgraph_function_possibly_inlined_p (olddecl))
1801 (*debug_hooks->outlining_inline_function) (olddecl);
1803 /* The new defn must not be inline. */
1804 DECL_INLINE (newdecl) = 0;
1805 DECL_UNINLINABLE (newdecl) = 1;
1807 else
1809 /* If either decl says `inline', this fn is inline, unless
1810 its definition was passed already. */
1811 if (DECL_DECLARED_INLINE_P (newdecl)
1812 || DECL_DECLARED_INLINE_P (olddecl))
1813 DECL_DECLARED_INLINE_P (newdecl) = 1;
1815 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1816 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1818 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1819 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1820 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1821 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1824 if (DECL_BUILT_IN (olddecl))
1826 /* If redeclaring a builtin function, it stays built in.
1827 But it gets tagged as having been declared. */
1828 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1829 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1830 C_DECL_DECLARED_BUILTIN (newdecl) = 1;
1831 if (new_is_prototype)
1832 C_DECL_BUILTIN_PROTOTYPE (newdecl) = 0;
1833 else
1834 C_DECL_BUILTIN_PROTOTYPE (newdecl)
1835 = C_DECL_BUILTIN_PROTOTYPE (olddecl);
1838 /* Also preserve various other info from the definition. */
1839 if (!new_is_definition)
1841 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1842 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1843 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1844 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1845 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1847 /* Set DECL_INLINE on the declaration if we've got a body
1848 from which to instantiate. */
1849 if (DECL_INLINE (olddecl) && !DECL_UNINLINABLE (newdecl))
1851 DECL_INLINE (newdecl) = 1;
1852 DECL_ABSTRACT_ORIGIN (newdecl)
1853 = DECL_ABSTRACT_ORIGIN (olddecl);
1856 else
1858 /* If a previous declaration said inline, mark the
1859 definition as inlinable. */
1860 if (DECL_DECLARED_INLINE_P (newdecl)
1861 && !DECL_UNINLINABLE (newdecl))
1862 DECL_INLINE (newdecl) = 1;
1866 extern_changed = DECL_EXTERNAL (olddecl) && !DECL_EXTERNAL (newdecl);
1868 /* Copy most of the decl-specific fields of NEWDECL into OLDDECL.
1869 But preserve OLDDECL's DECL_UID and DECL_CONTEXT. */
1871 unsigned olddecl_uid = DECL_UID (olddecl);
1872 tree olddecl_context = DECL_CONTEXT (olddecl);
1874 memcpy ((char *) olddecl + sizeof (struct tree_common),
1875 (char *) newdecl + sizeof (struct tree_common),
1876 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1877 switch (TREE_CODE (olddecl))
1879 case FIELD_DECL:
1880 case VAR_DECL:
1881 case PARM_DECL:
1882 case LABEL_DECL:
1883 case RESULT_DECL:
1884 case CONST_DECL:
1885 case TYPE_DECL:
1886 case FUNCTION_DECL:
1887 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1888 (char *) newdecl + sizeof (struct tree_decl_common),
1889 tree_code_size (TREE_CODE (olddecl)) - sizeof (struct tree_decl_common));
1890 break;
1892 default:
1894 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1895 (char *) newdecl + sizeof (struct tree_decl_common),
1896 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common));
1898 DECL_UID (olddecl) = olddecl_uid;
1899 DECL_CONTEXT (olddecl) = olddecl_context;
1902 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1903 so that encode_section_info has a chance to look at the new decl
1904 flags and attributes. */
1905 if (DECL_RTL_SET_P (olddecl)
1906 && (TREE_CODE (olddecl) == FUNCTION_DECL
1907 || (TREE_CODE (olddecl) == VAR_DECL
1908 && TREE_STATIC (olddecl))))
1909 make_decl_rtl (olddecl);
1911 /* If we changed a function from DECL_EXTERNAL to !DECL_EXTERNAL,
1912 and the definition is coming from the old version, cgraph needs
1913 to be called again. */
1914 if (extern_changed && !new_is_definition
1915 && TREE_CODE (olddecl) == FUNCTION_DECL && DECL_INITIAL (olddecl))
1916 cgraph_finalize_function (olddecl, false);
1919 /* Handle when a new declaration NEWDECL has the same name as an old
1920 one OLDDECL in the same binding contour. Prints an error message
1921 if appropriate.
1923 If safely possible, alter OLDDECL to look like NEWDECL, and return
1924 true. Otherwise, return false. */
1926 static bool
1927 duplicate_decls (tree newdecl, tree olddecl)
1929 tree newtype = NULL, oldtype = NULL;
1931 if (!diagnose_mismatched_decls (newdecl, olddecl, &newtype, &oldtype))
1933 /* Avoid `unused variable' and other warnings for OLDDECL. */
1934 TREE_NO_WARNING (olddecl) = 1;
1935 return false;
1938 merge_decls (newdecl, olddecl, newtype, oldtype);
1939 return true;
1943 /* Check whether decl-node NEW_DECL shadows an existing declaration. */
1944 static void
1945 warn_if_shadowing (tree new_decl)
1947 struct c_binding *b;
1949 /* Shadow warnings wanted? */
1950 if (!warn_shadow
1951 /* No shadow warnings for internally generated vars. */
1952 || DECL_IS_BUILTIN (new_decl)
1953 /* No shadow warnings for vars made for inlining. */
1954 || DECL_FROM_INLINE (new_decl))
1955 return;
1957 /* Is anything being shadowed? Invisible decls do not count. */
1958 for (b = I_SYMBOL_BINDING (DECL_NAME (new_decl)); b; b = b->shadowed)
1959 if (b->decl && b->decl != new_decl && !b->invisible)
1961 tree old_decl = b->decl;
1963 if (old_decl == error_mark_node)
1965 warning (OPT_Wshadow, "declaration of %q+D shadows previous "
1966 "non-variable", new_decl);
1967 break;
1969 else if (TREE_CODE (old_decl) == PARM_DECL)
1970 warning (OPT_Wshadow, "declaration of %q+D shadows a parameter",
1971 new_decl);
1972 else if (DECL_FILE_SCOPE_P (old_decl))
1973 warning (OPT_Wshadow, "declaration of %q+D shadows a global "
1974 "declaration", new_decl);
1975 else if (TREE_CODE (old_decl) == FUNCTION_DECL
1976 && DECL_BUILT_IN (old_decl))
1978 warning (OPT_Wshadow, "declaration of %q+D shadows "
1979 "a built-in function", new_decl);
1980 break;
1982 else
1983 warning (OPT_Wshadow, "declaration of %q+D shadows a previous local",
1984 new_decl);
1986 warning (OPT_Wshadow, "%Jshadowed declaration is here", old_decl);
1988 break;
1993 /* Subroutine of pushdecl.
1995 X is a TYPE_DECL for a typedef statement. Create a brand new
1996 ..._TYPE node (which will be just a variant of the existing
1997 ..._TYPE node with identical properties) and then install X
1998 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
2000 The whole point here is to end up with a situation where each
2001 and every ..._TYPE node the compiler creates will be uniquely
2002 associated with AT MOST one node representing a typedef name.
2003 This way, even though the compiler substitutes corresponding
2004 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
2005 early on, later parts of the compiler can always do the reverse
2006 translation and get back the corresponding typedef name. For
2007 example, given:
2009 typedef struct S MY_TYPE;
2010 MY_TYPE object;
2012 Later parts of the compiler might only know that `object' was of
2013 type `struct S' if it were not for code just below. With this
2014 code however, later parts of the compiler see something like:
2016 struct S' == struct S
2017 typedef struct S' MY_TYPE;
2018 struct S' object;
2020 And they can then deduce (from the node for type struct S') that
2021 the original object declaration was:
2023 MY_TYPE object;
2025 Being able to do this is important for proper support of protoize,
2026 and also for generating precise symbolic debugging information
2027 which takes full account of the programmer's (typedef) vocabulary.
2029 Obviously, we don't want to generate a duplicate ..._TYPE node if
2030 the TYPE_DECL node that we are now processing really represents a
2031 standard built-in type. */
2033 static void
2034 clone_underlying_type (tree x)
2036 if (DECL_IS_BUILTIN (x))
2038 if (TYPE_NAME (TREE_TYPE (x)) == 0)
2039 TYPE_NAME (TREE_TYPE (x)) = x;
2041 else if (TREE_TYPE (x) != error_mark_node
2042 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
2044 tree tt = TREE_TYPE (x);
2045 DECL_ORIGINAL_TYPE (x) = tt;
2046 tt = build_variant_type_copy (tt);
2047 TYPE_NAME (tt) = x;
2048 TREE_USED (tt) = TREE_USED (x);
2049 TREE_TYPE (x) = tt;
2053 /* Record a decl-node X as belonging to the current lexical scope.
2054 Check for errors (such as an incompatible declaration for the same
2055 name already seen in the same scope).
2057 Returns either X or an old decl for the same name.
2058 If an old decl is returned, it may have been smashed
2059 to agree with what X says. */
2061 tree
2062 pushdecl (tree x)
2064 tree name = DECL_NAME (x);
2065 struct c_scope *scope = current_scope;
2066 struct c_binding *b;
2067 bool nested = false;
2069 /* Must set DECL_CONTEXT for everything not at file scope or
2070 DECL_FILE_SCOPE_P won't work. Local externs don't count
2071 unless they have initializers (which generate code). */
2072 if (current_function_decl
2073 && ((TREE_CODE (x) != FUNCTION_DECL && TREE_CODE (x) != VAR_DECL)
2074 || DECL_INITIAL (x) || !DECL_EXTERNAL (x)))
2075 DECL_CONTEXT (x) = current_function_decl;
2077 /* If this is of variably modified type, prevent jumping into its
2078 scope. */
2079 if ((TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == TYPE_DECL)
2080 && variably_modified_type_p (TREE_TYPE (x), NULL_TREE))
2081 c_begin_vm_scope (scope->depth);
2083 /* Anonymous decls are just inserted in the scope. */
2084 if (!name)
2086 bind (name, x, scope, /*invisible=*/false, /*nested=*/false);
2087 return x;
2090 /* First, see if there is another declaration with the same name in
2091 the current scope. If there is, duplicate_decls may do all the
2092 work for us. If duplicate_decls returns false, that indicates
2093 two incompatible decls in the same scope; we are to silently
2094 replace the old one (duplicate_decls has issued all appropriate
2095 diagnostics). In particular, we should not consider possible
2096 duplicates in the external scope, or shadowing. */
2097 b = I_SYMBOL_BINDING (name);
2098 if (b && B_IN_SCOPE (b, scope))
2100 struct c_binding *b_ext, *b_use;
2101 tree type = TREE_TYPE (x);
2102 tree visdecl = b->decl;
2103 tree vistype = TREE_TYPE (visdecl);
2104 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
2105 && COMPLETE_TYPE_P (TREE_TYPE (x)))
2106 b->inner_comp = false;
2107 b_use = b;
2108 b_ext = b;
2109 /* If this is an external linkage declaration, we should check
2110 for compatibility with the type in the external scope before
2111 setting the type at this scope based on the visible
2112 information only. */
2113 if (TREE_PUBLIC (x) && TREE_PUBLIC (visdecl))
2115 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
2116 b_ext = b_ext->shadowed;
2117 if (b_ext)
2119 b_use = b_ext;
2120 if (b_use->type)
2121 TREE_TYPE (b_use->decl) = b_use->type;
2124 if (duplicate_decls (x, b_use->decl))
2126 if (b_use != b)
2128 /* Save the updated type in the external scope and
2129 restore the proper type for this scope. */
2130 tree thistype;
2131 if (comptypes (vistype, type))
2132 thistype = composite_type (vistype, type);
2133 else
2134 thistype = TREE_TYPE (b_use->decl);
2135 b_use->type = TREE_TYPE (b_use->decl);
2136 if (TREE_CODE (b_use->decl) == FUNCTION_DECL
2137 && DECL_BUILT_IN (b_use->decl))
2138 thistype
2139 = build_type_attribute_variant (thistype,
2140 TYPE_ATTRIBUTES
2141 (b_use->type));
2142 TREE_TYPE (b_use->decl) = thistype;
2144 return b_use->decl;
2146 else
2147 goto skip_external_and_shadow_checks;
2150 /* All declarations with external linkage, and all external
2151 references, go in the external scope, no matter what scope is
2152 current. However, the binding in that scope is ignored for
2153 purposes of normal name lookup. A separate binding structure is
2154 created in the requested scope; this governs the normal
2155 visibility of the symbol.
2157 The binding in the externals scope is used exclusively for
2158 detecting duplicate declarations of the same object, no matter
2159 what scope they are in; this is what we do here. (C99 6.2.7p2:
2160 All declarations that refer to the same object or function shall
2161 have compatible type; otherwise, the behavior is undefined.) */
2162 if (DECL_EXTERNAL (x) || scope == file_scope)
2164 tree type = TREE_TYPE (x);
2165 tree vistype = 0;
2166 tree visdecl = 0;
2167 bool type_saved = false;
2168 if (b && !B_IN_EXTERNAL_SCOPE (b)
2169 && (TREE_CODE (b->decl) == FUNCTION_DECL
2170 || TREE_CODE (b->decl) == VAR_DECL)
2171 && DECL_FILE_SCOPE_P (b->decl))
2173 visdecl = b->decl;
2174 vistype = TREE_TYPE (visdecl);
2176 if (scope != file_scope
2177 && !DECL_IN_SYSTEM_HEADER (x))
2178 warning (OPT_Wnested_externs, "nested extern declaration of %qD", x);
2180 while (b && !B_IN_EXTERNAL_SCOPE (b))
2182 /* If this decl might be modified, save its type. This is
2183 done here rather than when the decl is first bound
2184 because the type may change after first binding, through
2185 being completed or through attributes being added. If we
2186 encounter multiple such decls, only the first should have
2187 its type saved; the others will already have had their
2188 proper types saved and the types will not have changed as
2189 their scopes will not have been re-entered. */
2190 if (DECL_P (b->decl) && DECL_FILE_SCOPE_P (b->decl) && !type_saved)
2192 b->type = TREE_TYPE (b->decl);
2193 type_saved = true;
2195 if (B_IN_FILE_SCOPE (b)
2196 && TREE_CODE (b->decl) == VAR_DECL
2197 && TREE_STATIC (b->decl)
2198 && TREE_CODE (TREE_TYPE (b->decl)) == ARRAY_TYPE
2199 && !TYPE_DOMAIN (TREE_TYPE (b->decl))
2200 && TREE_CODE (type) == ARRAY_TYPE
2201 && TYPE_DOMAIN (type)
2202 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
2203 && !integer_zerop (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
2205 /* Array type completed in inner scope, which should be
2206 diagnosed if the completion does not have size 1 and
2207 it does not get completed in the file scope. */
2208 b->inner_comp = true;
2210 b = b->shadowed;
2213 /* If a matching external declaration has been found, set its
2214 type to the composite of all the types of that declaration.
2215 After the consistency checks, it will be reset to the
2216 composite of the visible types only. */
2217 if (b && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2218 && b->type)
2219 TREE_TYPE (b->decl) = b->type;
2221 /* The point of the same_translation_unit_p check here is,
2222 we want to detect a duplicate decl for a construct like
2223 foo() { extern bar(); } ... static bar(); but not if
2224 they are in different translation units. In any case,
2225 the static does not go in the externals scope. */
2226 if (b
2227 && (TREE_PUBLIC (x) || same_translation_unit_p (x, b->decl))
2228 && duplicate_decls (x, b->decl))
2230 tree thistype;
2231 if (vistype)
2233 if (comptypes (vistype, type))
2234 thistype = composite_type (vistype, type);
2235 else
2236 thistype = TREE_TYPE (b->decl);
2238 else
2239 thistype = type;
2240 b->type = TREE_TYPE (b->decl);
2241 if (TREE_CODE (b->decl) == FUNCTION_DECL && DECL_BUILT_IN (b->decl))
2242 thistype
2243 = build_type_attribute_variant (thistype,
2244 TYPE_ATTRIBUTES (b->type));
2245 TREE_TYPE (b->decl) = thistype;
2246 bind (name, b->decl, scope, /*invisible=*/false, /*nested=*/true);
2247 return b->decl;
2249 else if (TREE_PUBLIC (x))
2251 if (visdecl && !b && duplicate_decls (x, visdecl))
2253 /* An external declaration at block scope referring to a
2254 visible entity with internal linkage. The composite
2255 type will already be correct for this scope, so we
2256 just need to fall through to make the declaration in
2257 this scope. */
2258 nested = true;
2259 x = visdecl;
2261 else
2263 bind (name, x, external_scope, /*invisible=*/true,
2264 /*nested=*/false);
2265 nested = true;
2270 if (TREE_CODE (x) != PARM_DECL)
2271 warn_if_shadowing (x);
2273 skip_external_and_shadow_checks:
2274 if (TREE_CODE (x) == TYPE_DECL)
2275 clone_underlying_type (x);
2277 bind (name, x, scope, /*invisible=*/false, nested);
2279 /* If x's type is incomplete because it's based on a
2280 structure or union which has not yet been fully declared,
2281 attach it to that structure or union type, so we can go
2282 back and complete the variable declaration later, if the
2283 structure or union gets fully declared.
2285 If the input is erroneous, we can have error_mark in the type
2286 slot (e.g. "f(void a, ...)") - that doesn't count as an
2287 incomplete type. */
2288 if (TREE_TYPE (x) != error_mark_node
2289 && !COMPLETE_TYPE_P (TREE_TYPE (x)))
2291 tree element = TREE_TYPE (x);
2293 while (TREE_CODE (element) == ARRAY_TYPE)
2294 element = TREE_TYPE (element);
2295 element = TYPE_MAIN_VARIANT (element);
2297 if ((TREE_CODE (element) == RECORD_TYPE
2298 || TREE_CODE (element) == UNION_TYPE)
2299 && (TREE_CODE (x) != TYPE_DECL
2300 || TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE)
2301 && !COMPLETE_TYPE_P (element))
2302 C_TYPE_INCOMPLETE_VARS (element)
2303 = tree_cons (NULL_TREE, x, C_TYPE_INCOMPLETE_VARS (element));
2305 return x;
2308 /* Record X as belonging to file scope.
2309 This is used only internally by the Objective-C front end,
2310 and is limited to its needs. duplicate_decls is not called;
2311 if there is any preexisting decl for this identifier, it is an ICE. */
2313 tree
2314 pushdecl_top_level (tree x)
2316 tree name;
2317 bool nested = false;
2318 gcc_assert (TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == CONST_DECL);
2320 name = DECL_NAME (x);
2322 gcc_assert (TREE_CODE (x) == CONST_DECL || !I_SYMBOL_BINDING (name));
2324 if (TREE_PUBLIC (x))
2326 bind (name, x, external_scope, /*invisible=*/true, /*nested=*/false);
2327 nested = true;
2329 if (file_scope)
2330 bind (name, x, file_scope, /*invisible=*/false, nested);
2332 return x;
2335 static void
2336 implicit_decl_warning (tree id, tree olddecl)
2338 if (warn_implicit_function_declaration)
2340 if (flag_isoc99)
2341 pedwarn (G_("implicit declaration of function %qE"), id);
2342 else
2343 warning (OPT_Wimplicit_function_declaration,
2344 G_("implicit declaration of function %qE"), id);
2345 if (olddecl)
2346 locate_old_decl (olddecl, inform);
2350 /* Generate an implicit declaration for identifier FUNCTIONID as a
2351 function of type int (). */
2353 tree
2354 implicitly_declare (tree functionid)
2356 struct c_binding *b;
2357 tree decl = 0;
2358 tree asmspec_tree;
2360 for (b = I_SYMBOL_BINDING (functionid); b; b = b->shadowed)
2362 if (B_IN_SCOPE (b, external_scope))
2364 decl = b->decl;
2365 break;
2369 if (decl)
2371 if (decl == error_mark_node)
2372 return decl;
2374 /* FIXME: Objective-C has weird not-really-builtin functions
2375 which are supposed to be visible automatically. They wind up
2376 in the external scope because they're pushed before the file
2377 scope gets created. Catch this here and rebind them into the
2378 file scope. */
2379 if (!DECL_BUILT_IN (decl) && DECL_IS_BUILTIN (decl))
2381 bind (functionid, decl, file_scope,
2382 /*invisible=*/false, /*nested=*/true);
2383 return decl;
2385 else
2387 tree newtype = default_function_type;
2388 if (b->type)
2389 TREE_TYPE (decl) = b->type;
2390 /* Implicit declaration of a function already declared
2391 (somehow) in a different scope, or as a built-in.
2392 If this is the first time this has happened, warn;
2393 then recycle the old declaration but with the new type. */
2394 if (!C_DECL_IMPLICIT (decl))
2396 implicit_decl_warning (functionid, decl);
2397 C_DECL_IMPLICIT (decl) = 1;
2399 if (DECL_BUILT_IN (decl))
2401 newtype = build_type_attribute_variant (newtype,
2402 TYPE_ATTRIBUTES
2403 (TREE_TYPE (decl)));
2404 if (!comptypes (newtype, TREE_TYPE (decl)))
2406 warning (0, "incompatible implicit declaration of built-in"
2407 " function %qD", decl);
2408 newtype = TREE_TYPE (decl);
2411 else
2413 if (!comptypes (newtype, TREE_TYPE (decl)))
2415 error ("incompatible implicit declaration of function %qD",
2416 decl);
2417 locate_old_decl (decl, error);
2420 b->type = TREE_TYPE (decl);
2421 TREE_TYPE (decl) = newtype;
2422 bind (functionid, decl, current_scope,
2423 /*invisible=*/false, /*nested=*/true);
2424 return decl;
2428 /* Not seen before. */
2429 decl = build_decl (FUNCTION_DECL, functionid, default_function_type);
2430 DECL_EXTERNAL (decl) = 1;
2431 TREE_PUBLIC (decl) = 1;
2432 C_DECL_IMPLICIT (decl) = 1;
2433 implicit_decl_warning (functionid, 0);
2434 asmspec_tree = maybe_apply_renaming_pragma (decl, /*asmname=*/NULL);
2435 if (asmspec_tree)
2436 set_user_assembler_name (decl, TREE_STRING_POINTER (asmspec_tree));
2438 /* C89 says implicit declarations are in the innermost block.
2439 So we record the decl in the standard fashion. */
2440 decl = pushdecl (decl);
2442 /* No need to call objc_check_decl here - it's a function type. */
2443 rest_of_decl_compilation (decl, 0, 0);
2445 /* Write a record describing this implicit function declaration
2446 to the prototypes file (if requested). */
2447 gen_aux_info_record (decl, 0, 1, 0);
2449 /* Possibly apply some default attributes to this implicit declaration. */
2450 decl_attributes (&decl, NULL_TREE, 0);
2452 return decl;
2455 /* Issue an error message for a reference to an undeclared variable
2456 ID, including a reference to a builtin outside of function-call
2457 context. Establish a binding of the identifier to error_mark_node
2458 in an appropriate scope, which will suppress further errors for the
2459 same identifier. The error message should be given location LOC. */
2460 void
2461 undeclared_variable (tree id, location_t loc)
2463 static bool already = false;
2464 struct c_scope *scope;
2466 if (current_function_decl == 0)
2468 error ("%H%qE undeclared here (not in a function)", &loc, id);
2469 scope = current_scope;
2471 else
2473 error ("%H%qE undeclared (first use in this function)", &loc, id);
2475 if (!already)
2477 error ("%H(Each undeclared identifier is reported only once", &loc);
2478 error ("%Hfor each function it appears in.)", &loc);
2479 already = true;
2482 /* If we are parsing old-style parameter decls, current_function_decl
2483 will be nonnull but current_function_scope will be null. */
2484 scope = current_function_scope ? current_function_scope : current_scope;
2486 bind (id, error_mark_node, scope, /*invisible=*/false, /*nested=*/false);
2489 /* Subroutine of lookup_label, declare_label, define_label: construct a
2490 LABEL_DECL with all the proper frills. */
2492 static tree
2493 make_label (tree name, location_t location)
2495 tree label = build_decl (LABEL_DECL, name, void_type_node);
2497 DECL_CONTEXT (label) = current_function_decl;
2498 DECL_MODE (label) = VOIDmode;
2499 DECL_SOURCE_LOCATION (label) = location;
2501 return label;
2504 /* Get the LABEL_DECL corresponding to identifier NAME as a label.
2505 Create one if none exists so far for the current function.
2506 This is called when a label is used in a goto expression or
2507 has its address taken. */
2509 tree
2510 lookup_label (tree name)
2512 tree label;
2514 if (current_function_decl == 0)
2516 error ("label %qE referenced outside of any function", name);
2517 return 0;
2520 /* Use a label already defined or ref'd with this name, but not if
2521 it is inherited from a containing function and wasn't declared
2522 using __label__. */
2523 label = I_LABEL_DECL (name);
2524 if (label && (DECL_CONTEXT (label) == current_function_decl
2525 || C_DECLARED_LABEL_FLAG (label)))
2527 /* If the label has only been declared, update its apparent
2528 location to point here, for better diagnostics if it
2529 turns out not to have been defined. */
2530 if (!TREE_USED (label))
2531 DECL_SOURCE_LOCATION (label) = input_location;
2532 return label;
2535 /* No label binding for that identifier; make one. */
2536 label = make_label (name, input_location);
2538 /* Ordinary labels go in the current function scope. */
2539 bind (name, label, current_function_scope,
2540 /*invisible=*/false, /*nested=*/false);
2541 return label;
2544 /* Make a label named NAME in the current function, shadowing silently
2545 any that may be inherited from containing functions or containing
2546 scopes. This is called for __label__ declarations. */
2548 tree
2549 declare_label (tree name)
2551 struct c_binding *b = I_LABEL_BINDING (name);
2552 tree label;
2554 /* Check to make sure that the label hasn't already been declared
2555 at this scope */
2556 if (b && B_IN_CURRENT_SCOPE (b))
2558 error ("duplicate label declaration %qE", name);
2559 locate_old_decl (b->decl, error);
2561 /* Just use the previous declaration. */
2562 return b->decl;
2565 label = make_label (name, input_location);
2566 C_DECLARED_LABEL_FLAG (label) = 1;
2568 /* Declared labels go in the current scope. */
2569 bind (name, label, current_scope,
2570 /*invisible=*/false, /*nested=*/false);
2571 return label;
2574 /* Define a label, specifying the location in the source file.
2575 Return the LABEL_DECL node for the label, if the definition is valid.
2576 Otherwise return 0. */
2578 tree
2579 define_label (location_t location, tree name)
2581 /* Find any preexisting label with this name. It is an error
2582 if that label has already been defined in this function, or
2583 if there is a containing function with a declared label with
2584 the same name. */
2585 tree label = I_LABEL_DECL (name);
2586 struct c_label_list *nlist_se, *nlist_vm;
2588 if (label
2589 && ((DECL_CONTEXT (label) == current_function_decl
2590 && DECL_INITIAL (label) != 0)
2591 || (DECL_CONTEXT (label) != current_function_decl
2592 && C_DECLARED_LABEL_FLAG (label))))
2594 error ("%Hduplicate label %qD", &location, label);
2595 locate_old_decl (label, error);
2596 return 0;
2598 else if (label && DECL_CONTEXT (label) == current_function_decl)
2600 /* The label has been used or declared already in this function,
2601 but not defined. Update its location to point to this
2602 definition. */
2603 if (C_DECL_UNDEFINABLE_STMT_EXPR (label))
2604 error ("%Jjump into statement expression", label);
2605 if (C_DECL_UNDEFINABLE_VM (label))
2606 error ("%Jjump into scope of identifier with variably modified type",
2607 label);
2608 DECL_SOURCE_LOCATION (label) = location;
2610 else
2612 /* No label binding for that identifier; make one. */
2613 label = make_label (name, location);
2615 /* Ordinary labels go in the current function scope. */
2616 bind (name, label, current_function_scope,
2617 /*invisible=*/false, /*nested=*/false);
2620 if (!in_system_header && lookup_name (name))
2621 warning (OPT_Wtraditional, "%Htraditional C lacks a separate namespace "
2622 "for labels, identifier %qE conflicts", &location, name);
2624 nlist_se = XOBNEW (&parser_obstack, struct c_label_list);
2625 nlist_se->next = label_context_stack_se->labels_def;
2626 nlist_se->label = label;
2627 label_context_stack_se->labels_def = nlist_se;
2629 nlist_vm = XOBNEW (&parser_obstack, struct c_label_list);
2630 nlist_vm->next = label_context_stack_vm->labels_def;
2631 nlist_vm->label = label;
2632 label_context_stack_vm->labels_def = nlist_vm;
2634 /* Mark label as having been defined. */
2635 DECL_INITIAL (label) = error_mark_node;
2636 return label;
2639 /* Given NAME, an IDENTIFIER_NODE,
2640 return the structure (or union or enum) definition for that name.
2641 If THISLEVEL_ONLY is nonzero, searches only the current_scope.
2642 CODE says which kind of type the caller wants;
2643 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
2644 If the wrong kind of type is found, an error is reported. */
2646 static tree
2647 lookup_tag (enum tree_code code, tree name, int thislevel_only)
2649 struct c_binding *b = I_TAG_BINDING (name);
2650 int thislevel = 0;
2652 if (!b || !b->decl)
2653 return 0;
2655 /* We only care about whether it's in this level if
2656 thislevel_only was set or it might be a type clash. */
2657 if (thislevel_only || TREE_CODE (b->decl) != code)
2659 /* For our purposes, a tag in the external scope is the same as
2660 a tag in the file scope. (Primarily relevant to Objective-C
2661 and its builtin structure tags, which get pushed before the
2662 file scope is created.) */
2663 if (B_IN_CURRENT_SCOPE (b)
2664 || (current_scope == file_scope && B_IN_EXTERNAL_SCOPE (b)))
2665 thislevel = 1;
2668 if (thislevel_only && !thislevel)
2669 return 0;
2671 if (TREE_CODE (b->decl) != code)
2673 /* Definition isn't the kind we were looking for. */
2674 pending_invalid_xref = name;
2675 pending_invalid_xref_location = input_location;
2677 /* If in the same binding level as a declaration as a tag
2678 of a different type, this must not be allowed to
2679 shadow that tag, so give the error immediately.
2680 (For example, "struct foo; union foo;" is invalid.) */
2681 if (thislevel)
2682 pending_xref_error ();
2684 return b->decl;
2687 /* Print an error message now
2688 for a recent invalid struct, union or enum cross reference.
2689 We don't print them immediately because they are not invalid
2690 when used in the `struct foo;' construct for shadowing. */
2692 void
2693 pending_xref_error (void)
2695 if (pending_invalid_xref != 0)
2696 error ("%H%qE defined as wrong kind of tag",
2697 &pending_invalid_xref_location, pending_invalid_xref);
2698 pending_invalid_xref = 0;
2702 /* Look up NAME in the current scope and its superiors
2703 in the namespace of variables, functions and typedefs.
2704 Return a ..._DECL node of some kind representing its definition,
2705 or return 0 if it is undefined. */
2707 tree
2708 lookup_name (tree name)
2710 struct c_binding *b = I_SYMBOL_BINDING (name);
2711 if (b && !b->invisible)
2712 return b->decl;
2713 return 0;
2716 /* Similar to `lookup_name' but look only at the indicated scope. */
2718 static tree
2719 lookup_name_in_scope (tree name, struct c_scope *scope)
2721 struct c_binding *b;
2723 for (b = I_SYMBOL_BINDING (name); b; b = b->shadowed)
2724 if (B_IN_SCOPE (b, scope))
2725 return b->decl;
2726 return 0;
2729 /* Create the predefined scalar types of C,
2730 and some nodes representing standard constants (0, 1, (void *) 0).
2731 Initialize the global scope.
2732 Make definitions for built-in primitive functions. */
2734 void
2735 c_init_decl_processing (void)
2737 location_t save_loc = input_location;
2739 /* Initialize reserved words for parser. */
2740 c_parse_init ();
2742 current_function_decl = 0;
2744 gcc_obstack_init (&parser_obstack);
2746 /* Make the externals scope. */
2747 push_scope ();
2748 external_scope = current_scope;
2750 /* Declarations from c_common_nodes_and_builtins must not be associated
2751 with this input file, lest we get differences between using and not
2752 using preprocessed headers. */
2753 input_location = BUILTINS_LOCATION;
2755 build_common_tree_nodes (flag_signed_char, false);
2757 c_common_nodes_and_builtins ();
2759 /* In C, comparisons and TRUTH_* expressions have type int. */
2760 truthvalue_type_node = integer_type_node;
2761 truthvalue_true_node = integer_one_node;
2762 truthvalue_false_node = integer_zero_node;
2764 /* Even in C99, which has a real boolean type. */
2765 pushdecl (build_decl (TYPE_DECL, get_identifier ("_Bool"),
2766 boolean_type_node));
2768 input_location = save_loc;
2770 pedantic_lvalues = true;
2772 make_fname_decl = c_make_fname_decl;
2773 start_fname_decls ();
2776 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
2777 decl, NAME is the initialization string and TYPE_DEP indicates whether
2778 NAME depended on the type of the function. As we don't yet implement
2779 delayed emission of static data, we mark the decl as emitted
2780 so it is not placed in the output. Anything using it must therefore pull
2781 out the STRING_CST initializer directly. FIXME. */
2783 static tree
2784 c_make_fname_decl (tree id, int type_dep)
2786 const char *name = fname_as_string (type_dep);
2787 tree decl, type, init;
2788 size_t length = strlen (name);
2790 type = build_array_type (char_type_node,
2791 build_index_type (size_int (length)));
2792 type = c_build_qualified_type (type, TYPE_QUAL_CONST);
2794 decl = build_decl (VAR_DECL, id, type);
2796 TREE_STATIC (decl) = 1;
2797 TREE_READONLY (decl) = 1;
2798 DECL_ARTIFICIAL (decl) = 1;
2800 init = build_string (length + 1, name);
2801 free (CONST_CAST (char *, name));
2802 TREE_TYPE (init) = type;
2803 DECL_INITIAL (decl) = init;
2805 TREE_USED (decl) = 1;
2807 if (current_function_decl
2808 /* For invalid programs like this:
2810 void foo()
2811 const char* p = __FUNCTION__;
2813 the __FUNCTION__ is believed to appear in K&R style function
2814 parameter declarator. In that case we still don't have
2815 function_scope. */
2816 && (!errorcount || current_function_scope))
2818 DECL_CONTEXT (decl) = current_function_decl;
2819 bind (id, decl, current_function_scope,
2820 /*invisible=*/false, /*nested=*/false);
2823 finish_decl (decl, init, NULL_TREE);
2825 return decl;
2828 tree
2829 c_builtin_function (tree decl)
2831 tree type = TREE_TYPE (decl);
2832 tree id = DECL_NAME (decl);
2834 const char *name = IDENTIFIER_POINTER (id);
2835 C_DECL_BUILTIN_PROTOTYPE (decl) = (TYPE_ARG_TYPES (type) != 0);
2837 /* Should never be called on a symbol with a preexisting meaning. */
2838 gcc_assert (!I_SYMBOL_BINDING (id));
2840 bind (id, decl, external_scope, /*invisible=*/true, /*nested=*/false);
2842 /* Builtins in the implementation namespace are made visible without
2843 needing to be explicitly declared. See push_file_scope. */
2844 if (name[0] == '_' && (name[1] == '_' || ISUPPER (name[1])))
2846 TREE_CHAIN (decl) = visible_builtins;
2847 visible_builtins = decl;
2850 return decl;
2853 /* Called when a declaration is seen that contains no names to declare.
2854 If its type is a reference to a structure, union or enum inherited
2855 from a containing scope, shadow that tag name for the current scope
2856 with a forward reference.
2857 If its type defines a new named structure or union
2858 or defines an enum, it is valid but we need not do anything here.
2859 Otherwise, it is an error. */
2861 void
2862 shadow_tag (const struct c_declspecs *declspecs)
2864 shadow_tag_warned (declspecs, 0);
2867 /* WARNED is 1 if we have done a pedwarn, 2 if we have done a warning,
2868 but no pedwarn. */
2869 void
2870 shadow_tag_warned (const struct c_declspecs *declspecs, int warned)
2872 bool found_tag = false;
2874 if (declspecs->type && !declspecs->default_int_p && !declspecs->typedef_p)
2876 tree value = declspecs->type;
2877 enum tree_code code = TREE_CODE (value);
2879 if (code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
2880 /* Used to test also that TYPE_SIZE (value) != 0.
2881 That caused warning for `struct foo;' at top level in the file. */
2883 tree name = TYPE_NAME (value);
2884 tree t;
2886 found_tag = true;
2888 if (name == 0)
2890 if (warned != 1 && code != ENUMERAL_TYPE)
2891 /* Empty unnamed enum OK */
2893 pedwarn ("unnamed struct/union that defines no instances");
2894 warned = 1;
2897 else if (!declspecs->tag_defined_p
2898 && declspecs->storage_class != csc_none)
2900 if (warned != 1)
2901 pedwarn ("empty declaration with storage class specifier "
2902 "does not redeclare tag");
2903 warned = 1;
2904 pending_xref_error ();
2906 else if (!declspecs->tag_defined_p
2907 && (declspecs->const_p
2908 || declspecs->volatile_p
2909 || declspecs->restrict_p))
2911 if (warned != 1)
2912 pedwarn ("empty declaration with type qualifier "
2913 "does not redeclare tag");
2914 warned = 1;
2915 pending_xref_error ();
2917 else
2919 pending_invalid_xref = 0;
2920 t = lookup_tag (code, name, 1);
2922 if (t == 0)
2924 t = make_node (code);
2925 pushtag (name, t);
2929 else
2931 if (warned != 1 && !in_system_header)
2933 pedwarn ("useless type name in empty declaration");
2934 warned = 1;
2938 else if (warned != 1 && !in_system_header && declspecs->typedef_p)
2940 pedwarn ("useless type name in empty declaration");
2941 warned = 1;
2944 pending_invalid_xref = 0;
2946 if (declspecs->inline_p)
2948 error ("%<inline%> in empty declaration");
2949 warned = 1;
2952 if (current_scope == file_scope && declspecs->storage_class == csc_auto)
2954 error ("%<auto%> in file-scope empty declaration");
2955 warned = 1;
2958 if (current_scope == file_scope && declspecs->storage_class == csc_register)
2960 error ("%<register%> in file-scope empty declaration");
2961 warned = 1;
2964 if (!warned && !in_system_header && declspecs->storage_class != csc_none)
2966 warning (0, "useless storage class specifier in empty declaration");
2967 warned = 2;
2970 if (!warned && !in_system_header && declspecs->thread_p)
2972 warning (0, "useless %<__thread%> in empty declaration");
2973 warned = 2;
2976 if (!warned && !in_system_header && (declspecs->const_p
2977 || declspecs->volatile_p
2978 || declspecs->restrict_p))
2980 warning (0, "useless type qualifier in empty declaration");
2981 warned = 2;
2984 if (warned != 1)
2986 if (!found_tag)
2987 pedwarn ("empty declaration");
2992 /* Return the qualifiers from SPECS as a bitwise OR of TYPE_QUAL_*
2993 bits. SPECS represents declaration specifiers that the grammar
2994 only permits to contain type qualifiers and attributes. */
2997 quals_from_declspecs (const struct c_declspecs *specs)
2999 int quals = ((specs->const_p ? TYPE_QUAL_CONST : 0)
3000 | (specs->volatile_p ? TYPE_QUAL_VOLATILE : 0)
3001 | (specs->restrict_p ? TYPE_QUAL_RESTRICT : 0));
3002 gcc_assert (!specs->type
3003 && !specs->decl_attr
3004 && specs->typespec_word == cts_none
3005 && specs->storage_class == csc_none
3006 && !specs->typedef_p
3007 && !specs->explicit_signed_p
3008 && !specs->deprecated_p
3009 && !specs->long_p
3010 && !specs->long_long_p
3011 && !specs->short_p
3012 && !specs->signed_p
3013 && !specs->unsigned_p
3014 && !specs->complex_p
3015 && !specs->inline_p
3016 && !specs->thread_p);
3017 return quals;
3020 /* Construct an array declarator. EXPR is the expression inside [],
3021 or NULL_TREE. QUALS are the type qualifiers inside the [] (to be
3022 applied to the pointer to which a parameter array is converted).
3023 STATIC_P is true if "static" is inside the [], false otherwise.
3024 VLA_UNSPEC_P is true if the array is [*], a VLA of unspecified
3025 length which is nevertheless a complete type, false otherwise. The
3026 field for the contained declarator is left to be filled in by
3027 set_array_declarator_inner. */
3029 struct c_declarator *
3030 build_array_declarator (tree expr, struct c_declspecs *quals, bool static_p,
3031 bool vla_unspec_p)
3033 struct c_declarator *declarator = XOBNEW (&parser_obstack,
3034 struct c_declarator);
3035 declarator->kind = cdk_array;
3036 declarator->declarator = 0;
3037 declarator->u.array.dimen = expr;
3038 if (quals)
3040 declarator->u.array.attrs = quals->attrs;
3041 declarator->u.array.quals = quals_from_declspecs (quals);
3043 else
3045 declarator->u.array.attrs = NULL_TREE;
3046 declarator->u.array.quals = 0;
3048 declarator->u.array.static_p = static_p;
3049 declarator->u.array.vla_unspec_p = vla_unspec_p;
3050 if (pedantic && !flag_isoc99)
3052 if (static_p || quals != NULL)
3053 pedwarn ("ISO C90 does not support %<static%> or type "
3054 "qualifiers in parameter array declarators");
3055 if (vla_unspec_p)
3056 pedwarn ("ISO C90 does not support %<[*]%> array declarators");
3058 if (vla_unspec_p)
3060 if (!current_scope->parm_flag)
3062 /* C99 6.7.5.2p4 */
3063 error ("%<[*]%> not allowed in other than function prototype scope");
3064 declarator->u.array.vla_unspec_p = false;
3065 return NULL;
3067 current_scope->had_vla_unspec = true;
3069 return declarator;
3072 /* Set the contained declarator of an array declarator. DECL is the
3073 declarator, as constructed by build_array_declarator; INNER is what
3074 appears on the left of the []. */
3076 struct c_declarator *
3077 set_array_declarator_inner (struct c_declarator *decl,
3078 struct c_declarator *inner)
3080 decl->declarator = inner;
3081 return decl;
3084 /* INIT is a constructor that forms DECL's initializer. If the final
3085 element initializes a flexible array field, add the size of that
3086 initializer to DECL's size. */
3088 static void
3089 add_flexible_array_elts_to_size (tree decl, tree init)
3091 tree elt, type;
3093 if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
3094 return;
3096 elt = VEC_last (constructor_elt, CONSTRUCTOR_ELTS (init))->value;
3097 type = TREE_TYPE (elt);
3098 if (TREE_CODE (type) == ARRAY_TYPE
3099 && TYPE_SIZE (type) == NULL_TREE
3100 && TYPE_DOMAIN (type) != NULL_TREE
3101 && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
3103 complete_array_type (&type, elt, false);
3104 DECL_SIZE (decl)
3105 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (type));
3106 DECL_SIZE_UNIT (decl)
3107 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), TYPE_SIZE_UNIT (type));
3111 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */
3113 tree
3114 groktypename (struct c_type_name *type_name)
3116 tree type;
3117 tree attrs = type_name->specs->attrs;
3119 type_name->specs->attrs = NULL_TREE;
3121 type = grokdeclarator (type_name->declarator, type_name->specs, TYPENAME,
3122 false, NULL, &attrs, DEPRECATED_NORMAL);
3124 /* Apply attributes. */
3125 decl_attributes (&type, attrs, 0);
3127 return type;
3130 /* Decode a declarator in an ordinary declaration or data definition.
3131 This is called as soon as the type information and variable name
3132 have been parsed, before parsing the initializer if any.
3133 Here we create the ..._DECL node, fill in its type,
3134 and put it on the list of decls for the current context.
3135 The ..._DECL node is returned as the value.
3137 Exception: for arrays where the length is not specified,
3138 the type is left null, to be filled in by `finish_decl'.
3140 Function definitions do not come here; they go to start_function
3141 instead. However, external and forward declarations of functions
3142 do go through here. Structure field declarations are done by
3143 grokfield and not through here. */
3145 tree
3146 start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
3147 bool initialized, tree attributes)
3149 tree decl;
3150 tree tem;
3151 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
3153 /* An object declared as __attribute__((deprecated)) suppresses
3154 warnings of uses of other deprecated items. */
3155 if (lookup_attribute ("deprecated", attributes))
3156 deprecated_state = DEPRECATED_SUPPRESS;
3158 decl = grokdeclarator (declarator, declspecs,
3159 NORMAL, initialized, NULL, &attributes,
3160 deprecated_state);
3161 if (!decl)
3162 return 0;
3164 if (warn_main > 0 && TREE_CODE (decl) != FUNCTION_DECL
3165 && MAIN_NAME_P (DECL_NAME (decl)))
3166 warning (OPT_Wmain, "%q+D is usually a function", decl);
3168 if (initialized)
3169 /* Is it valid for this decl to have an initializer at all?
3170 If not, set INITIALIZED to zero, which will indirectly
3171 tell 'finish_decl' to ignore the initializer once it is parsed. */
3172 switch (TREE_CODE (decl))
3174 case TYPE_DECL:
3175 error ("typedef %qD is initialized (use __typeof__ instead)", decl);
3176 initialized = 0;
3177 break;
3179 case FUNCTION_DECL:
3180 error ("function %qD is initialized like a variable", decl);
3181 initialized = 0;
3182 break;
3184 case PARM_DECL:
3185 /* DECL_INITIAL in a PARM_DECL is really DECL_ARG_TYPE. */
3186 error ("parameter %qD is initialized", decl);
3187 initialized = 0;
3188 break;
3190 default:
3191 /* Don't allow initializations for incomplete types except for
3192 arrays which might be completed by the initialization. */
3194 /* This can happen if the array size is an undefined macro.
3195 We already gave a warning, so we don't need another one. */
3196 if (TREE_TYPE (decl) == error_mark_node)
3197 initialized = 0;
3198 else if (COMPLETE_TYPE_P (TREE_TYPE (decl)))
3200 /* A complete type is ok if size is fixed. */
3202 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (decl))) != INTEGER_CST
3203 || C_DECL_VARIABLE_SIZE (decl))
3205 error ("variable-sized object may not be initialized");
3206 initialized = 0;
3209 else if (TREE_CODE (TREE_TYPE (decl)) != ARRAY_TYPE)
3211 error ("variable %qD has initializer but incomplete type", decl);
3212 initialized = 0;
3214 else if (C_DECL_VARIABLE_SIZE (decl))
3216 /* Although C99 is unclear about whether incomplete arrays
3217 of VLAs themselves count as VLAs, it does not make
3218 sense to permit them to be initialized given that
3219 ordinary VLAs may not be initialized. */
3220 error ("variable-sized object may not be initialized");
3221 initialized = 0;
3225 if (initialized)
3227 if (current_scope == file_scope)
3228 TREE_STATIC (decl) = 1;
3230 /* Tell 'pushdecl' this is an initialized decl
3231 even though we don't yet have the initializer expression.
3232 Also tell 'finish_decl' it may store the real initializer. */
3233 DECL_INITIAL (decl) = error_mark_node;
3236 /* If this is a function declaration, write a record describing it to the
3237 prototypes file (if requested). */
3239 if (TREE_CODE (decl) == FUNCTION_DECL)
3240 gen_aux_info_record (decl, 0, 0, TYPE_ARG_TYPES (TREE_TYPE (decl)) != 0);
3242 /* ANSI specifies that a tentative definition which is not merged with
3243 a non-tentative definition behaves exactly like a definition with an
3244 initializer equal to zero. (Section 3.7.2)
3246 -fno-common gives strict ANSI behavior, though this tends to break
3247 a large body of code that grew up without this rule.
3249 Thread-local variables are never common, since there's no entrenched
3250 body of code to break, and it allows more efficient variable references
3251 in the presence of dynamic linking. */
3253 if (TREE_CODE (decl) == VAR_DECL
3254 && !initialized
3255 && TREE_PUBLIC (decl)
3256 && !DECL_THREAD_LOCAL_P (decl)
3257 && !flag_no_common)
3258 DECL_COMMON (decl) = 1;
3260 /* Set attributes here so if duplicate decl, will have proper attributes. */
3261 decl_attributes (&decl, attributes, 0);
3263 /* Handle gnu_inline attribute. */
3264 if (declspecs->inline_p
3265 && !flag_gnu89_inline
3266 && TREE_CODE (decl) == FUNCTION_DECL
3267 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
3269 if (declspecs->storage_class == csc_auto && current_scope != file_scope)
3271 else if (declspecs->storage_class != csc_static)
3272 DECL_EXTERNAL (decl) = !DECL_EXTERNAL (decl);
3275 if (TREE_CODE (decl) == FUNCTION_DECL
3276 && targetm.calls.promote_prototypes (TREE_TYPE (decl)))
3278 struct c_declarator *ce = declarator;
3280 if (ce->kind == cdk_pointer)
3281 ce = declarator->declarator;
3282 if (ce->kind == cdk_function)
3284 tree args = ce->u.arg_info->parms;
3285 for (; args; args = TREE_CHAIN (args))
3287 tree type = TREE_TYPE (args);
3288 if (type && INTEGRAL_TYPE_P (type)
3289 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
3290 DECL_ARG_TYPE (args) = integer_type_node;
3295 if (TREE_CODE (decl) == FUNCTION_DECL
3296 && DECL_DECLARED_INLINE_P (decl)
3297 && DECL_UNINLINABLE (decl)
3298 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
3299 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
3300 decl);
3302 /* C99 6.7.4p3: An inline definition of a function with external
3303 linkage shall not contain a definition of a modifiable object
3304 with static storage duration... */
3305 if (TREE_CODE (decl) == VAR_DECL
3306 && current_scope != file_scope
3307 && TREE_STATIC (decl)
3308 && !TREE_READONLY (decl)
3309 && DECL_DECLARED_INLINE_P (current_function_decl)
3310 && DECL_EXTERNAL (current_function_decl))
3311 pedwarn ("%q+D is static but declared in inline function %qD "
3312 "which is not static", decl, current_function_decl);
3314 /* Add this decl to the current scope.
3315 TEM may equal DECL or it may be a previous decl of the same name. */
3316 tem = pushdecl (decl);
3318 if (initialized && DECL_EXTERNAL (tem))
3320 DECL_EXTERNAL (tem) = 0;
3321 TREE_STATIC (tem) = 1;
3324 return tem;
3327 /* Initialize EH if not initialized yet and exceptions are enabled. */
3329 void
3330 c_maybe_initialize_eh (void)
3332 if (!flag_exceptions || c_eh_initialized_p)
3333 return;
3335 c_eh_initialized_p = true;
3336 eh_personality_libfunc
3337 = init_one_libfunc (USING_SJLJ_EXCEPTIONS
3338 ? "__gcc_personality_sj0"
3339 : "__gcc_personality_v0");
3340 default_init_unwind_resume_libfunc ();
3341 using_eh_for_cleanups ();
3344 /* Finish processing of a declaration;
3345 install its initial value.
3346 If the length of an array type is not known before,
3347 it must be determined now, from the initial value, or it is an error. */
3349 void
3350 finish_decl (tree decl, tree init, tree asmspec_tree)
3352 tree type;
3353 int was_incomplete = (DECL_SIZE (decl) == 0);
3354 const char *asmspec = 0;
3356 /* If a name was specified, get the string. */
3357 if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
3358 && DECL_FILE_SCOPE_P (decl))
3359 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
3360 if (asmspec_tree)
3361 asmspec = TREE_STRING_POINTER (asmspec_tree);
3363 /* If `start_decl' didn't like having an initialization, ignore it now. */
3364 if (init != 0 && DECL_INITIAL (decl) == 0)
3365 init = 0;
3367 /* Don't crash if parm is initialized. */
3368 if (TREE_CODE (decl) == PARM_DECL)
3369 init = 0;
3371 if (init)
3372 store_init_value (decl, init);
3374 if (c_dialect_objc () && (TREE_CODE (decl) == VAR_DECL
3375 || TREE_CODE (decl) == FUNCTION_DECL
3376 || TREE_CODE (decl) == FIELD_DECL))
3377 objc_check_decl (decl);
3379 type = TREE_TYPE (decl);
3381 /* Deduce size of array from initialization, if not already known. */
3382 if (TREE_CODE (type) == ARRAY_TYPE
3383 && TYPE_DOMAIN (type) == 0
3384 && TREE_CODE (decl) != TYPE_DECL)
3386 bool do_default
3387 = (TREE_STATIC (decl)
3388 /* Even if pedantic, an external linkage array
3389 may have incomplete type at first. */
3390 ? pedantic && !TREE_PUBLIC (decl)
3391 : !DECL_EXTERNAL (decl));
3392 int failure
3393 = complete_array_type (&TREE_TYPE (decl), DECL_INITIAL (decl),
3394 do_default);
3396 /* Get the completed type made by complete_array_type. */
3397 type = TREE_TYPE (decl);
3399 switch (failure)
3401 case 1:
3402 error ("initializer fails to determine size of %q+D", decl);
3403 break;
3405 case 2:
3406 if (do_default)
3407 error ("array size missing in %q+D", decl);
3408 /* If a `static' var's size isn't known,
3409 make it extern as well as static, so it does not get
3410 allocated.
3411 If it is not `static', then do not mark extern;
3412 finish_incomplete_decl will give it a default size
3413 and it will get allocated. */
3414 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
3415 DECL_EXTERNAL (decl) = 1;
3416 break;
3418 case 3:
3419 error ("zero or negative size array %q+D", decl);
3420 break;
3422 case 0:
3423 /* For global variables, update the copy of the type that
3424 exists in the binding. */
3425 if (TREE_PUBLIC (decl))
3427 struct c_binding *b_ext = I_SYMBOL_BINDING (DECL_NAME (decl));
3428 while (b_ext && !B_IN_EXTERNAL_SCOPE (b_ext))
3429 b_ext = b_ext->shadowed;
3430 if (b_ext)
3432 if (b_ext->type)
3433 b_ext->type = composite_type (b_ext->type, type);
3434 else
3435 b_ext->type = type;
3438 break;
3440 default:
3441 gcc_unreachable ();
3444 if (DECL_INITIAL (decl))
3445 TREE_TYPE (DECL_INITIAL (decl)) = type;
3447 layout_decl (decl, 0);
3450 if (TREE_CODE (decl) == VAR_DECL)
3452 if (init && TREE_CODE (init) == CONSTRUCTOR)
3453 add_flexible_array_elts_to_size (decl, init);
3455 if (DECL_SIZE (decl) == 0 && TREE_TYPE (decl) != error_mark_node
3456 && COMPLETE_TYPE_P (TREE_TYPE (decl)))
3457 layout_decl (decl, 0);
3459 if (DECL_SIZE (decl) == 0
3460 /* Don't give an error if we already gave one earlier. */
3461 && TREE_TYPE (decl) != error_mark_node
3462 && (TREE_STATIC (decl)
3463 /* A static variable with an incomplete type
3464 is an error if it is initialized.
3465 Also if it is not file scope.
3466 Otherwise, let it through, but if it is not `extern'
3467 then it may cause an error message later. */
3468 ? (DECL_INITIAL (decl) != 0
3469 || !DECL_FILE_SCOPE_P (decl))
3470 /* An automatic variable with an incomplete type
3471 is an error. */
3472 : !DECL_EXTERNAL (decl)))
3474 error ("storage size of %q+D isn%'t known", decl);
3475 TREE_TYPE (decl) = error_mark_node;
3478 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
3479 && DECL_SIZE (decl) != 0)
3481 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
3482 constant_expression_warning (DECL_SIZE (decl));
3483 else
3485 error ("storage size of %q+D isn%'t constant", decl);
3486 TREE_TYPE (decl) = error_mark_node;
3490 if (TREE_USED (type))
3491 TREE_USED (decl) = 1;
3494 /* If this is a function and an assembler name is specified, reset DECL_RTL
3495 so we can give it its new name. Also, update built_in_decls if it
3496 was a normal built-in. */
3497 if (TREE_CODE (decl) == FUNCTION_DECL && asmspec)
3499 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
3500 set_builtin_user_assembler_name (decl, asmspec);
3501 set_user_assembler_name (decl, asmspec);
3504 /* If #pragma weak was used, mark the decl weak now. */
3505 maybe_apply_pragma_weak (decl);
3507 /* Output the assembler code and/or RTL code for variables and functions,
3508 unless the type is an undefined structure or union.
3509 If not, it will get done when the type is completed. */
3511 if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL)
3513 /* Determine the ELF visibility. */
3514 if (TREE_PUBLIC (decl))
3515 c_determine_visibility (decl);
3517 /* This is a no-op in c-lang.c or something real in objc-act.c. */
3518 if (c_dialect_objc ())
3519 objc_check_decl (decl);
3521 if (asmspec)
3523 /* If this is not a static variable, issue a warning.
3524 It doesn't make any sense to give an ASMSPEC for an
3525 ordinary, non-register local variable. Historically,
3526 GCC has accepted -- but ignored -- the ASMSPEC in
3527 this case. */
3528 if (!DECL_FILE_SCOPE_P (decl)
3529 && TREE_CODE (decl) == VAR_DECL
3530 && !C_DECL_REGISTER (decl)
3531 && !TREE_STATIC (decl))
3532 warning (0, "ignoring asm-specifier for non-static local "
3533 "variable %q+D", decl);
3534 else
3535 set_user_assembler_name (decl, asmspec);
3538 if (DECL_FILE_SCOPE_P (decl))
3540 if (DECL_INITIAL (decl) == NULL_TREE
3541 || DECL_INITIAL (decl) == error_mark_node)
3542 /* Don't output anything
3543 when a tentative file-scope definition is seen.
3544 But at end of compilation, do output code for them. */
3545 DECL_DEFER_OUTPUT (decl) = 1;
3546 rest_of_decl_compilation (decl, true, 0);
3548 else
3550 /* In conjunction with an ASMSPEC, the `register'
3551 keyword indicates that we should place the variable
3552 in a particular register. */
3553 if (asmspec && C_DECL_REGISTER (decl))
3555 DECL_HARD_REGISTER (decl) = 1;
3556 /* This cannot be done for a structure with volatile
3557 fields, on which DECL_REGISTER will have been
3558 reset. */
3559 if (!DECL_REGISTER (decl))
3560 error ("cannot put object with volatile field into register");
3563 if (TREE_CODE (decl) != FUNCTION_DECL)
3565 /* If we're building a variable sized type, and we might be
3566 reachable other than via the top of the current binding
3567 level, then create a new BIND_EXPR so that we deallocate
3568 the object at the right time. */
3569 /* Note that DECL_SIZE can be null due to errors. */
3570 if (DECL_SIZE (decl)
3571 && !TREE_CONSTANT (DECL_SIZE (decl))
3572 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list))
3574 tree bind;
3575 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL);
3576 TREE_SIDE_EFFECTS (bind) = 1;
3577 add_stmt (bind);
3578 BIND_EXPR_BODY (bind) = push_stmt_list ();
3580 add_stmt (build_stmt (DECL_EXPR, decl));
3585 if (!DECL_FILE_SCOPE_P (decl))
3587 /* Recompute the RTL of a local array now
3588 if it used to be an incomplete type. */
3589 if (was_incomplete
3590 && !TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
3592 /* If we used it already as memory, it must stay in memory. */
3593 TREE_ADDRESSABLE (decl) = TREE_USED (decl);
3594 /* If it's still incomplete now, no init will save it. */
3595 if (DECL_SIZE (decl) == 0)
3596 DECL_INITIAL (decl) = 0;
3601 /* If this was marked 'used', be sure it will be output. */
3602 if (!flag_unit_at_a_time && lookup_attribute ("used", DECL_ATTRIBUTES (decl)))
3603 mark_decl_referenced (decl);
3605 if (TREE_CODE (decl) == TYPE_DECL)
3607 if (!DECL_FILE_SCOPE_P (decl)
3608 && variably_modified_type_p (TREE_TYPE (decl), NULL_TREE))
3609 add_stmt (build_stmt (DECL_EXPR, decl));
3611 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 0);
3614 /* At the end of a declaration, throw away any variable type sizes
3615 of types defined inside that declaration. There is no use
3616 computing them in the following function definition. */
3617 if (current_scope == file_scope)
3618 get_pending_sizes ();
3620 /* Install a cleanup (aka destructor) if one was given. */
3621 if (TREE_CODE (decl) == VAR_DECL && !TREE_STATIC (decl))
3623 tree attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl));
3624 if (attr)
3626 tree cleanup_id = TREE_VALUE (TREE_VALUE (attr));
3627 tree cleanup_decl = lookup_name (cleanup_id);
3628 tree cleanup;
3630 /* Build "cleanup(&decl)" for the destructor. */
3631 cleanup = build_unary_op (ADDR_EXPR, decl, 0);
3632 cleanup = build_tree_list (NULL_TREE, cleanup);
3633 cleanup = build_function_call (cleanup_decl, cleanup);
3635 /* Don't warn about decl unused; the cleanup uses it. */
3636 TREE_USED (decl) = 1;
3637 TREE_USED (cleanup_decl) = 1;
3639 /* Initialize EH, if we've been told to do so. */
3640 c_maybe_initialize_eh ();
3642 push_cleanup (decl, cleanup, false);
3647 /* Given a parsed parameter declaration, decode it into a PARM_DECL. */
3649 tree
3650 grokparm (const struct c_parm *parm)
3652 tree attrs = parm->attrs;
3653 tree decl = grokdeclarator (parm->declarator, parm->specs, PARM, false,
3654 NULL, &attrs, DEPRECATED_NORMAL);
3656 decl_attributes (&decl, attrs, 0);
3658 return decl;
3661 /* Given a parsed parameter declaration, decode it into a PARM_DECL
3662 and push that on the current scope. */
3664 void
3665 push_parm_decl (const struct c_parm *parm)
3667 tree attrs = parm->attrs;
3668 tree decl;
3670 decl = grokdeclarator (parm->declarator, parm->specs, PARM, false, NULL,
3671 &attrs, DEPRECATED_NORMAL);
3672 decl_attributes (&decl, attrs, 0);
3674 decl = pushdecl (decl);
3676 finish_decl (decl, NULL_TREE, NULL_TREE);
3679 /* Mark all the parameter declarations to date as forward decls.
3680 Also diagnose use of this extension. */
3682 void
3683 mark_forward_parm_decls (void)
3685 struct c_binding *b;
3687 if (pedantic && !current_scope->warned_forward_parm_decls)
3689 pedwarn ("ISO C forbids forward parameter declarations");
3690 current_scope->warned_forward_parm_decls = true;
3693 for (b = current_scope->bindings; b; b = b->prev)
3694 if (TREE_CODE (b->decl) == PARM_DECL)
3695 TREE_ASM_WRITTEN (b->decl) = 1;
3698 /* Build a COMPOUND_LITERAL_EXPR. TYPE is the type given in the compound
3699 literal, which may be an incomplete array type completed by the
3700 initializer; INIT is a CONSTRUCTOR that initializes the compound
3701 literal. */
3703 tree
3704 build_compound_literal (tree type, tree init)
3706 /* We do not use start_decl here because we have a type, not a declarator;
3707 and do not use finish_decl because the decl should be stored inside
3708 the COMPOUND_LITERAL_EXPR rather than added elsewhere as a DECL_EXPR. */
3709 tree decl;
3710 tree complit;
3711 tree stmt;
3713 if (type == error_mark_node)
3714 return error_mark_node;
3716 decl = build_decl (VAR_DECL, NULL_TREE, type);
3717 DECL_EXTERNAL (decl) = 0;
3718 TREE_PUBLIC (decl) = 0;
3719 TREE_STATIC (decl) = (current_scope == file_scope);
3720 DECL_CONTEXT (decl) = current_function_decl;
3721 TREE_USED (decl) = 1;
3722 TREE_TYPE (decl) = type;
3723 TREE_READONLY (decl) = TYPE_READONLY (type);
3724 store_init_value (decl, init);
3726 if (TREE_CODE (type) == ARRAY_TYPE && !COMPLETE_TYPE_P (type))
3728 int failure = complete_array_type (&TREE_TYPE (decl),
3729 DECL_INITIAL (decl), true);
3730 gcc_assert (!failure);
3732 type = TREE_TYPE (decl);
3733 TREE_TYPE (DECL_INITIAL (decl)) = type;
3736 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
3737 return error_mark_node;
3739 stmt = build_stmt (DECL_EXPR, decl);
3740 complit = build1 (COMPOUND_LITERAL_EXPR, type, stmt);
3741 TREE_SIDE_EFFECTS (complit) = 1;
3743 layout_decl (decl, 0);
3745 if (TREE_STATIC (decl))
3747 /* This decl needs a name for the assembler output. */
3748 set_compound_literal_name (decl);
3749 DECL_DEFER_OUTPUT (decl) = 1;
3750 DECL_COMDAT (decl) = 1;
3751 DECL_ARTIFICIAL (decl) = 1;
3752 DECL_IGNORED_P (decl) = 1;
3753 pushdecl (decl);
3754 rest_of_decl_compilation (decl, 1, 0);
3757 return complit;
3760 /* Determine whether TYPE is a structure with a flexible array member,
3761 or a union containing such a structure (possibly recursively). */
3763 static bool
3764 flexible_array_type_p (tree type)
3766 tree x;
3767 switch (TREE_CODE (type))
3769 case RECORD_TYPE:
3770 x = TYPE_FIELDS (type);
3771 if (x == NULL_TREE)
3772 return false;
3773 while (TREE_CHAIN (x) != NULL_TREE)
3774 x = TREE_CHAIN (x);
3775 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
3776 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3777 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
3778 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
3779 return true;
3780 return false;
3781 case UNION_TYPE:
3782 for (x = TYPE_FIELDS (type); x != NULL_TREE; x = TREE_CHAIN (x))
3784 if (flexible_array_type_p (TREE_TYPE (x)))
3785 return true;
3787 return false;
3788 default:
3789 return false;
3793 /* Performs sanity checks on the TYPE and WIDTH of the bit-field NAME,
3794 replacing with appropriate values if they are invalid. */
3795 static void
3796 check_bitfield_type_and_width (tree *type, tree *width, const char *orig_name)
3798 tree type_mv;
3799 unsigned int max_width;
3800 unsigned HOST_WIDE_INT w;
3801 const char *name = orig_name ? orig_name: _("<anonymous>");
3803 /* Detect and ignore out of range field width and process valid
3804 field widths. */
3805 if (!INTEGRAL_TYPE_P (TREE_TYPE (*width))
3806 || TREE_CODE (*width) != INTEGER_CST)
3808 error ("bit-field %qs width not an integer constant", name);
3809 *width = integer_one_node;
3811 else
3813 constant_expression_warning (*width);
3814 if (tree_int_cst_sgn (*width) < 0)
3816 error ("negative width in bit-field %qs", name);
3817 *width = integer_one_node;
3819 else if (integer_zerop (*width) && orig_name)
3821 error ("zero width for bit-field %qs", name);
3822 *width = integer_one_node;
3826 /* Detect invalid bit-field type. */
3827 if (TREE_CODE (*type) != INTEGER_TYPE
3828 && TREE_CODE (*type) != BOOLEAN_TYPE
3829 && TREE_CODE (*type) != ENUMERAL_TYPE)
3831 error ("bit-field %qs has invalid type", name);
3832 *type = unsigned_type_node;
3835 type_mv = TYPE_MAIN_VARIANT (*type);
3836 if (pedantic
3837 && !in_system_header
3838 && type_mv != integer_type_node
3839 && type_mv != unsigned_type_node
3840 && type_mv != boolean_type_node)
3841 pedwarn ("type of bit-field %qs is a GCC extension", name);
3843 max_width = TYPE_PRECISION (*type);
3845 if (0 < compare_tree_int (*width, max_width))
3847 error ("width of %qs exceeds its type", name);
3848 w = max_width;
3849 *width = build_int_cst (NULL_TREE, w);
3851 else
3852 w = tree_low_cst (*width, 1);
3854 if (TREE_CODE (*type) == ENUMERAL_TYPE)
3856 struct lang_type *lt = TYPE_LANG_SPECIFIC (*type);
3857 if (!lt
3858 || w < min_precision (lt->enum_min, TYPE_UNSIGNED (*type))
3859 || w < min_precision (lt->enum_max, TYPE_UNSIGNED (*type)))
3860 warning (0, "%qs is narrower than values of its type", name);
3866 /* Print warning about variable length array if necessary. */
3868 static void
3869 warn_variable_length_array (const char *name, tree size)
3871 int ped = !flag_isoc99 && pedantic && warn_vla != 0;
3872 int const_size = TREE_CONSTANT (size);
3874 if (ped)
3876 if (const_size)
3878 if (name)
3879 pedwarn ("ISO C90 forbids array %qs whose size "
3880 "can%'t be evaluated",
3881 name);
3882 else
3883 pedwarn ("ISO C90 forbids array whose size "
3884 "can%'t be evaluated");
3886 else
3888 if (name)
3889 pedwarn ("ISO C90 forbids variable length array %qs",
3890 name);
3891 else
3892 pedwarn ("ISO C90 forbids variable length array");
3895 else if (warn_vla > 0)
3897 if (const_size)
3899 if (name)
3900 warning (OPT_Wvla,
3901 "the size of array %qs can"
3902 "%'t be evaluated", name);
3903 else
3904 warning (OPT_Wvla,
3905 "the size of array can %'t be evaluated");
3907 else
3909 if (name)
3910 warning (OPT_Wvla,
3911 "variable length array %qs is used",
3912 name);
3913 else
3914 warning (OPT_Wvla,
3915 "variable length array is used");
3920 /* Given declspecs and a declarator,
3921 determine the name and type of the object declared
3922 and construct a ..._DECL node for it.
3923 (In one case we can return a ..._TYPE node instead.
3924 For invalid input we sometimes return 0.)
3926 DECLSPECS is a c_declspecs structure for the declaration specifiers.
3928 DECL_CONTEXT says which syntactic context this declaration is in:
3929 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
3930 FUNCDEF for a function definition. Like NORMAL but a few different
3931 error messages in each case. Return value may be zero meaning
3932 this definition is too screwy to try to parse.
3933 PARM for a parameter declaration (either within a function prototype
3934 or before a function body). Make a PARM_DECL, or return void_type_node.
3935 TYPENAME if for a typename (in a cast or sizeof).
3936 Don't make a DECL node; just return the ..._TYPE node.
3937 FIELD for a struct or union field; make a FIELD_DECL.
3938 INITIALIZED is true if the decl has an initializer.
3939 WIDTH is non-NULL for bit-fields, and is a pointer to an INTEGER_CST node
3940 representing the width of the bit-field.
3941 DECL_ATTRS points to the list of attributes that should be added to this
3942 decl. Any nested attributes that belong on the decl itself will be
3943 added to this list.
3944 DEPRECATED_STATE is a deprecated_states value indicating whether
3945 deprecation warnings should be suppressed.
3947 In the TYPENAME case, DECLARATOR is really an absolute declarator.
3948 It may also be so in the PARM case, for a prototype where the
3949 argument type is specified but not the name.
3951 This function is where the complicated C meanings of `static'
3952 and `extern' are interpreted. */
3954 static tree
3955 grokdeclarator (const struct c_declarator *declarator,
3956 struct c_declspecs *declspecs,
3957 enum decl_context decl_context, bool initialized, tree *width,
3958 tree *decl_attrs, enum deprecated_states deprecated_state)
3960 tree type = declspecs->type;
3961 bool threadp = declspecs->thread_p;
3962 enum c_storage_class storage_class = declspecs->storage_class;
3963 int constp;
3964 int restrictp;
3965 int volatilep;
3966 int type_quals = TYPE_UNQUALIFIED;
3967 const char *name, *orig_name;
3968 bool funcdef_flag = false;
3969 bool funcdef_syntax = false;
3970 int size_varies = 0;
3971 tree decl_attr = declspecs->decl_attr;
3972 int array_ptr_quals = TYPE_UNQUALIFIED;
3973 tree array_ptr_attrs = NULL_TREE;
3974 int array_parm_static = 0;
3975 bool array_parm_vla_unspec_p = false;
3976 tree returned_attrs = NULL_TREE;
3977 bool bitfield = width != NULL;
3978 tree element_type;
3979 struct c_arg_info *arg_info = 0;
3981 if (decl_context == FUNCDEF)
3982 funcdef_flag = true, decl_context = NORMAL;
3984 /* Look inside a declarator for the name being declared
3985 and get it as a string, for an error message. */
3987 const struct c_declarator *decl = declarator;
3988 name = 0;
3990 while (decl)
3991 switch (decl->kind)
3993 case cdk_function:
3994 case cdk_array:
3995 case cdk_pointer:
3996 funcdef_syntax = (decl->kind == cdk_function);
3997 decl = decl->declarator;
3998 break;
4000 case cdk_attrs:
4001 decl = decl->declarator;
4002 break;
4004 case cdk_id:
4005 if (decl->u.id)
4006 name = IDENTIFIER_POINTER (decl->u.id);
4007 decl = 0;
4008 break;
4010 default:
4011 gcc_unreachable ();
4013 orig_name = name;
4014 if (name == 0)
4015 name = "type name";
4018 /* A function definition's declarator must have the form of
4019 a function declarator. */
4021 if (funcdef_flag && !funcdef_syntax)
4022 return 0;
4024 /* If this looks like a function definition, make it one,
4025 even if it occurs where parms are expected.
4026 Then store_parm_decls will reject it and not use it as a parm. */
4027 if (decl_context == NORMAL && !funcdef_flag && current_scope->parm_flag)
4028 decl_context = PARM;
4030 if (declspecs->deprecated_p && deprecated_state != DEPRECATED_SUPPRESS)
4031 warn_deprecated_use (declspecs->type);
4033 if ((decl_context == NORMAL || decl_context == FIELD)
4034 && current_scope == file_scope
4035 && variably_modified_type_p (type, NULL_TREE))
4037 error ("variably modified %qs at file scope", name);
4038 type = integer_type_node;
4041 size_varies = C_TYPE_VARIABLE_SIZE (type);
4043 /* Diagnose defaulting to "int". */
4045 if (declspecs->default_int_p && !in_system_header)
4047 /* Issue a warning if this is an ISO C 99 program or if
4048 -Wreturn-type and this is a function, or if -Wimplicit;
4049 prefer the former warning since it is more explicit. */
4050 if ((warn_implicit_int || warn_return_type || flag_isoc99)
4051 && funcdef_flag)
4052 warn_about_return_type = 1;
4053 else if (warn_implicit_int || flag_isoc99)
4054 pedwarn_c99 ("type defaults to %<int%> in declaration of %qs", name);
4057 /* Adjust the type if a bit-field is being declared,
4058 -funsigned-bitfields applied and the type is not explicitly
4059 "signed". */
4060 if (bitfield && !flag_signed_bitfields && !declspecs->explicit_signed_p
4061 && TREE_CODE (type) == INTEGER_TYPE)
4062 type = c_common_unsigned_type (type);
4064 /* Figure out the type qualifiers for the declaration. There are
4065 two ways a declaration can become qualified. One is something
4066 like `const int i' where the `const' is explicit. Another is
4067 something like `typedef const int CI; CI i' where the type of the
4068 declaration contains the `const'. A third possibility is that
4069 there is a type qualifier on the element type of a typedefed
4070 array type, in which case we should extract that qualifier so
4071 that c_apply_type_quals_to_decl receives the full list of
4072 qualifiers to work with (C90 is not entirely clear about whether
4073 duplicate qualifiers should be diagnosed in this case, but it
4074 seems most appropriate to do so). */
4075 element_type = strip_array_types (type);
4076 constp = declspecs->const_p + TYPE_READONLY (element_type);
4077 restrictp = declspecs->restrict_p + TYPE_RESTRICT (element_type);
4078 volatilep = declspecs->volatile_p + TYPE_VOLATILE (element_type);
4079 if (pedantic && !flag_isoc99)
4081 if (constp > 1)
4082 pedwarn ("duplicate %<const%>");
4083 if (restrictp > 1)
4084 pedwarn ("duplicate %<restrict%>");
4085 if (volatilep > 1)
4086 pedwarn ("duplicate %<volatile%>");
4088 if (!flag_gen_aux_info && (TYPE_QUALS (element_type)))
4089 type = TYPE_MAIN_VARIANT (type);
4090 type_quals = ((constp ? TYPE_QUAL_CONST : 0)
4091 | (restrictp ? TYPE_QUAL_RESTRICT : 0)
4092 | (volatilep ? TYPE_QUAL_VOLATILE : 0));
4094 /* Warn about storage classes that are invalid for certain
4095 kinds of declarations (parameters, typenames, etc.). */
4097 if (funcdef_flag
4098 && (threadp
4099 || storage_class == csc_auto
4100 || storage_class == csc_register
4101 || storage_class == csc_typedef))
4103 if (storage_class == csc_auto
4104 && (pedantic || current_scope == file_scope))
4105 pedwarn ("function definition declared %<auto%>");
4106 if (storage_class == csc_register)
4107 error ("function definition declared %<register%>");
4108 if (storage_class == csc_typedef)
4109 error ("function definition declared %<typedef%>");
4110 if (threadp)
4111 error ("function definition declared %<__thread%>");
4112 threadp = false;
4113 if (storage_class == csc_auto
4114 || storage_class == csc_register
4115 || storage_class == csc_typedef)
4116 storage_class = csc_none;
4118 else if (decl_context != NORMAL && (storage_class != csc_none || threadp))
4120 if (decl_context == PARM && storage_class == csc_register)
4122 else
4124 switch (decl_context)
4126 case FIELD:
4127 error ("storage class specified for structure field %qs",
4128 name);
4129 break;
4130 case PARM:
4131 error ("storage class specified for parameter %qs", name);
4132 break;
4133 default:
4134 error ("storage class specified for typename");
4135 break;
4137 storage_class = csc_none;
4138 threadp = false;
4141 else if (storage_class == csc_extern
4142 && initialized
4143 && !funcdef_flag)
4145 /* 'extern' with initialization is invalid if not at file scope. */
4146 if (current_scope == file_scope)
4148 /* It is fine to have 'extern const' when compiling at C
4149 and C++ intersection. */
4150 if (!(warn_cxx_compat && constp))
4151 warning (0, "%qs initialized and declared %<extern%>", name);
4153 else
4154 error ("%qs has both %<extern%> and initializer", name);
4156 else if (current_scope == file_scope)
4158 if (storage_class == csc_auto)
4159 error ("file-scope declaration of %qs specifies %<auto%>", name);
4160 if (pedantic && storage_class == csc_register)
4161 pedwarn ("file-scope declaration of %qs specifies %<register%>", name);
4163 else
4165 if (storage_class == csc_extern && funcdef_flag)
4166 error ("nested function %qs declared %<extern%>", name);
4167 else if (threadp && storage_class == csc_none)
4169 error ("function-scope %qs implicitly auto and declared "
4170 "%<__thread%>",
4171 name);
4172 threadp = false;
4176 /* Now figure out the structure of the declarator proper.
4177 Descend through it, creating more complex types, until we reach
4178 the declared identifier (or NULL_TREE, in an absolute declarator).
4179 At each stage we maintain an unqualified version of the type
4180 together with any qualifiers that should be applied to it with
4181 c_build_qualified_type; this way, array types including
4182 multidimensional array types are first built up in unqualified
4183 form and then the qualified form is created with
4184 TYPE_MAIN_VARIANT pointing to the unqualified form. */
4186 while (declarator && declarator->kind != cdk_id)
4188 if (type == error_mark_node)
4190 declarator = declarator->declarator;
4191 continue;
4194 /* Each level of DECLARATOR is either a cdk_array (for ...[..]),
4195 a cdk_pointer (for *...),
4196 a cdk_function (for ...(...)),
4197 a cdk_attrs (for nested attributes),
4198 or a cdk_id (for the name being declared
4199 or the place in an absolute declarator
4200 where the name was omitted).
4201 For the last case, we have just exited the loop.
4203 At this point, TYPE is the type of elements of an array,
4204 or for a function to return, or for a pointer to point to.
4205 After this sequence of ifs, TYPE is the type of the
4206 array or function or pointer, and DECLARATOR has had its
4207 outermost layer removed. */
4209 if (array_ptr_quals != TYPE_UNQUALIFIED
4210 || array_ptr_attrs != NULL_TREE
4211 || array_parm_static)
4213 /* Only the innermost declarator (making a parameter be of
4214 array type which is converted to pointer type)
4215 may have static or type qualifiers. */
4216 error ("static or type qualifiers in non-parameter array declarator");
4217 array_ptr_quals = TYPE_UNQUALIFIED;
4218 array_ptr_attrs = NULL_TREE;
4219 array_parm_static = 0;
4222 switch (declarator->kind)
4224 case cdk_attrs:
4226 /* A declarator with embedded attributes. */
4227 tree attrs = declarator->u.attrs;
4228 const struct c_declarator *inner_decl;
4229 int attr_flags = 0;
4230 declarator = declarator->declarator;
4231 inner_decl = declarator;
4232 while (inner_decl->kind == cdk_attrs)
4233 inner_decl = inner_decl->declarator;
4234 if (inner_decl->kind == cdk_id)
4235 attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
4236 else if (inner_decl->kind == cdk_function)
4237 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
4238 else if (inner_decl->kind == cdk_array)
4239 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
4240 returned_attrs = decl_attributes (&type,
4241 chainon (returned_attrs, attrs),
4242 attr_flags);
4243 break;
4245 case cdk_array:
4247 tree itype = NULL_TREE;
4248 tree size = declarator->u.array.dimen;
4249 /* The index is a signed object `sizetype' bits wide. */
4250 tree index_type = c_common_signed_type (sizetype);
4252 array_ptr_quals = declarator->u.array.quals;
4253 array_ptr_attrs = declarator->u.array.attrs;
4254 array_parm_static = declarator->u.array.static_p;
4255 array_parm_vla_unspec_p = declarator->u.array.vla_unspec_p;
4257 declarator = declarator->declarator;
4259 /* Check for some types that there cannot be arrays of. */
4261 if (VOID_TYPE_P (type))
4263 error ("declaration of %qs as array of voids", name);
4264 type = error_mark_node;
4267 if (TREE_CODE (type) == FUNCTION_TYPE)
4269 error ("declaration of %qs as array of functions", name);
4270 type = error_mark_node;
4273 if (pedantic && !in_system_header && flexible_array_type_p (type))
4274 pedwarn ("invalid use of structure with flexible array member");
4276 if (size == error_mark_node)
4277 type = error_mark_node;
4279 if (type == error_mark_node)
4280 continue;
4282 /* If size was specified, set ITYPE to a range-type for
4283 that size. Otherwise, ITYPE remains null. finish_decl
4284 may figure it out from an initial value. */
4286 if (size)
4288 /* Strip NON_LVALUE_EXPRs since we aren't using as an
4289 lvalue. */
4290 STRIP_TYPE_NOPS (size);
4292 if (!INTEGRAL_TYPE_P (TREE_TYPE (size)))
4294 error ("size of array %qs has non-integer type", name);
4295 size = integer_one_node;
4298 if (pedantic && integer_zerop (size))
4299 pedwarn ("ISO C forbids zero-size array %qs", name);
4301 if (TREE_CODE (size) == INTEGER_CST)
4303 constant_expression_warning (size);
4304 if (tree_int_cst_sgn (size) < 0)
4306 error ("size of array %qs is negative", name);
4307 size = integer_one_node;
4310 else if ((decl_context == NORMAL || decl_context == FIELD)
4311 && current_scope == file_scope)
4313 error ("variably modified %qs at file scope", name);
4314 size = integer_one_node;
4316 else
4318 /* Make sure the array size remains visibly
4319 nonconstant even if it is (eg) a const variable
4320 with known value. */
4321 size_varies = 1;
4322 warn_variable_length_array (orig_name, size);
4325 if (integer_zerop (size))
4327 /* A zero-length array cannot be represented with
4328 an unsigned index type, which is what we'll
4329 get with build_index_type. Create an
4330 open-ended range instead. */
4331 itype = build_range_type (sizetype, size, NULL_TREE);
4333 else
4335 /* Arrange for the SAVE_EXPR on the inside of the
4336 MINUS_EXPR, which allows the -1 to get folded
4337 with the +1 that happens when building TYPE_SIZE. */
4338 if (size_varies)
4339 size = variable_size (size);
4341 /* Compute the maximum valid index, that is, size
4342 - 1. Do the calculation in index_type, so that
4343 if it is a variable the computations will be
4344 done in the proper mode. */
4345 itype = fold_build2 (MINUS_EXPR, index_type,
4346 convert (index_type, size),
4347 convert (index_type,
4348 size_one_node));
4350 /* If that overflowed, the array is too big. ???
4351 While a size of INT_MAX+1 technically shouldn't
4352 cause an overflow (because we subtract 1), the
4353 overflow is recorded during the conversion to
4354 index_type, before the subtraction. Handling
4355 this case seems like an unnecessary
4356 complication. */
4357 if (TREE_CODE (itype) == INTEGER_CST
4358 && TREE_OVERFLOW (itype))
4360 error ("size of array %qs is too large", name);
4361 type = error_mark_node;
4362 continue;
4365 itype = build_index_type (itype);
4368 else if (decl_context == FIELD)
4370 if (pedantic && !flag_isoc99 && !in_system_header)
4371 pedwarn ("ISO C90 does not support flexible array members");
4373 /* ISO C99 Flexible array members are effectively
4374 identical to GCC's zero-length array extension. */
4375 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4377 else if (decl_context == PARM)
4379 if (array_parm_vla_unspec_p)
4381 if (! orig_name)
4383 /* C99 6.7.5.2p4 */
4384 error ("%<[*]%> not allowed in other than a declaration");
4387 itype = build_range_type (sizetype, size_zero_node, NULL_TREE);
4388 size_varies = 1;
4391 else if (decl_context == TYPENAME)
4393 if (array_parm_vla_unspec_p)
4395 /* The error is printed elsewhere. We use this to
4396 avoid messing up with incomplete array types of
4397 the same type, that would otherwise be modified
4398 below. */
4399 itype = build_range_type (sizetype, size_zero_node,
4400 NULL_TREE);
4404 /* Complain about arrays of incomplete types. */
4405 if (!COMPLETE_TYPE_P (type))
4407 error ("array type has incomplete element type");
4408 type = error_mark_node;
4410 else
4411 /* When itype is NULL, a shared incomplete array type is
4412 returned for all array of a given type. Elsewhere we
4413 make sure we don't complete that type before copying
4414 it, but here we want to make sure we don't ever
4415 modify the shared type, so we gcc_assert (itype)
4416 below. */
4417 type = build_array_type (type, itype);
4419 if (type != error_mark_node)
4421 if (size_varies)
4423 /* It is ok to modify type here even if itype is
4424 NULL: if size_varies, we're in a
4425 multi-dimensional array and the inner type has
4426 variable size, so the enclosing shared array type
4427 must too. */
4428 if (size && TREE_CODE (size) == INTEGER_CST)
4429 type
4430 = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
4431 C_TYPE_VARIABLE_SIZE (type) = 1;
4434 /* The GCC extension for zero-length arrays differs from
4435 ISO flexible array members in that sizeof yields
4436 zero. */
4437 if (size && integer_zerop (size))
4439 gcc_assert (itype);
4440 TYPE_SIZE (type) = bitsize_zero_node;
4441 TYPE_SIZE_UNIT (type) = size_zero_node;
4443 if (array_parm_vla_unspec_p)
4445 gcc_assert (itype);
4446 /* The type is complete. C99 6.7.5.2p4 */
4447 TYPE_SIZE (type) = bitsize_zero_node;
4448 TYPE_SIZE_UNIT (type) = size_zero_node;
4452 if (decl_context != PARM
4453 && (array_ptr_quals != TYPE_UNQUALIFIED
4454 || array_ptr_attrs != NULL_TREE
4455 || array_parm_static))
4457 error ("static or type qualifiers in non-parameter array declarator");
4458 array_ptr_quals = TYPE_UNQUALIFIED;
4459 array_ptr_attrs = NULL_TREE;
4460 array_parm_static = 0;
4462 break;
4464 case cdk_function:
4466 /* Say it's a definition only for the declarator closest
4467 to the identifier, apart possibly from some
4468 attributes. */
4469 bool really_funcdef = false;
4470 tree arg_types;
4471 if (funcdef_flag)
4473 const struct c_declarator *t = declarator->declarator;
4474 while (t->kind == cdk_attrs)
4475 t = t->declarator;
4476 really_funcdef = (t->kind == cdk_id);
4479 /* Declaring a function type. Make sure we have a valid
4480 type for the function to return. */
4481 if (type == error_mark_node)
4482 continue;
4484 size_varies = 0;
4486 /* Warn about some types functions can't return. */
4487 if (TREE_CODE (type) == FUNCTION_TYPE)
4489 error ("%qs declared as function returning a function", name);
4490 type = integer_type_node;
4492 if (TREE_CODE (type) == ARRAY_TYPE)
4494 error ("%qs declared as function returning an array", name);
4495 type = integer_type_node;
4498 /* Construct the function type and go to the next
4499 inner layer of declarator. */
4500 arg_info = declarator->u.arg_info;
4501 arg_types = grokparms (arg_info, really_funcdef);
4502 if (really_funcdef)
4503 put_pending_sizes (arg_info->pending_sizes);
4505 /* Type qualifiers before the return type of the function
4506 qualify the return type, not the function type. */
4507 if (type_quals)
4509 /* Type qualifiers on a function return type are
4510 normally permitted by the standard but have no
4511 effect, so give a warning at -Wreturn-type.
4512 Qualifiers on a void return type are banned on
4513 function definitions in ISO C; GCC used to used
4514 them for noreturn functions. */
4515 if (VOID_TYPE_P (type) && really_funcdef)
4516 pedwarn ("function definition has qualified void return type");
4517 else
4518 warning (OPT_Wignored_qualifiers,
4519 "type qualifiers ignored on function return type");
4521 type = c_build_qualified_type (type, type_quals);
4523 type_quals = TYPE_UNQUALIFIED;
4525 type = build_function_type (type, arg_types);
4526 declarator = declarator->declarator;
4528 /* Set the TYPE_CONTEXTs for each tagged type which is local to
4529 the formal parameter list of this FUNCTION_TYPE to point to
4530 the FUNCTION_TYPE node itself. */
4532 tree link;
4534 for (link = arg_info->tags;
4535 link;
4536 link = TREE_CHAIN (link))
4537 TYPE_CONTEXT (TREE_VALUE (link)) = type;
4539 break;
4541 case cdk_pointer:
4543 /* Merge any constancy or volatility into the target type
4544 for the pointer. */
4546 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4547 && type_quals)
4548 pedwarn ("ISO C forbids qualified function types");
4549 if (type_quals)
4550 type = c_build_qualified_type (type, type_quals);
4551 size_varies = 0;
4553 /* When the pointed-to type involves components of variable size,
4554 care must be taken to ensure that the size evaluation code is
4555 emitted early enough to dominate all the possible later uses
4556 and late enough for the variables on which it depends to have
4557 been assigned.
4559 This is expected to happen automatically when the pointed-to
4560 type has a name/declaration of it's own, but special attention
4561 is required if the type is anonymous.
4563 We handle the NORMAL and FIELD contexts here by attaching an
4564 artificial TYPE_DECL to such pointed-to type. This forces the
4565 sizes evaluation at a safe point and ensures it is not deferred
4566 until e.g. within a deeper conditional context.
4568 We expect nothing to be needed here for PARM or TYPENAME.
4569 Pushing a TYPE_DECL at this point for TYPENAME would actually
4570 be incorrect, as we might be in the middle of an expression
4571 with side effects on the pointed-to type size "arguments" prior
4572 to the pointer declaration point and the fake TYPE_DECL in the
4573 enclosing context would force the size evaluation prior to the
4574 side effects. */
4576 if (!TYPE_NAME (type)
4577 && (decl_context == NORMAL || decl_context == FIELD)
4578 && variably_modified_type_p (type, NULL_TREE))
4580 tree decl = build_decl (TYPE_DECL, NULL_TREE, type);
4581 DECL_ARTIFICIAL (decl) = 1;
4582 pushdecl (decl);
4583 finish_decl (decl, NULL_TREE, NULL_TREE);
4584 TYPE_NAME (type) = decl;
4587 type = build_pointer_type (type);
4589 /* Process type qualifiers (such as const or volatile)
4590 that were given inside the `*'. */
4591 type_quals = declarator->u.pointer_quals;
4593 declarator = declarator->declarator;
4594 break;
4596 default:
4597 gcc_unreachable ();
4600 *decl_attrs = chainon (returned_attrs, *decl_attrs);
4602 /* Now TYPE has the actual type, apart from any qualifiers in
4603 TYPE_QUALS. */
4605 /* Check the type and width of a bit-field. */
4606 if (bitfield)
4607 check_bitfield_type_and_width (&type, width, orig_name);
4609 /* Did array size calculations overflow? */
4611 if (TREE_CODE (type) == ARRAY_TYPE
4612 && COMPLETE_TYPE_P (type)
4613 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
4614 && TREE_OVERFLOW (TYPE_SIZE_UNIT (type)))
4616 error ("size of array %qs is too large", name);
4617 /* If we proceed with the array type as it is, we'll eventually
4618 crash in tree_low_cst(). */
4619 type = error_mark_node;
4622 /* If this is declaring a typedef name, return a TYPE_DECL. */
4624 if (storage_class == csc_typedef)
4626 tree decl;
4627 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4628 && type_quals)
4629 pedwarn ("ISO C forbids qualified function types");
4630 if (type_quals)
4631 type = c_build_qualified_type (type, type_quals);
4632 decl = build_decl (TYPE_DECL, declarator->u.id, type);
4633 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4634 if (declspecs->explicit_signed_p)
4635 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
4636 if (declspecs->inline_p)
4637 pedwarn ("typedef %q+D declared %<inline%>", decl);
4638 return decl;
4641 /* If this is a type name (such as, in a cast or sizeof),
4642 compute the type and return it now. */
4644 if (decl_context == TYPENAME)
4646 /* Note that the grammar rejects storage classes in typenames
4647 and fields. */
4648 gcc_assert (storage_class == csc_none && !threadp
4649 && !declspecs->inline_p);
4650 if (pedantic && TREE_CODE (type) == FUNCTION_TYPE
4651 && type_quals)
4652 pedwarn ("ISO C forbids const or volatile function types");
4653 if (type_quals)
4654 type = c_build_qualified_type (type, type_quals);
4655 return type;
4658 if (pedantic && decl_context == FIELD
4659 && variably_modified_type_p (type, NULL_TREE))
4661 /* C99 6.7.2.1p8 */
4662 pedwarn ("a member of a structure or union cannot have a variably modified type");
4665 /* Aside from typedefs and type names (handle above),
4666 `void' at top level (not within pointer)
4667 is allowed only in public variables.
4668 We don't complain about parms either, but that is because
4669 a better error message can be made later. */
4671 if (VOID_TYPE_P (type) && decl_context != PARM
4672 && !((decl_context != FIELD && TREE_CODE (type) != FUNCTION_TYPE)
4673 && (storage_class == csc_extern
4674 || (current_scope == file_scope
4675 && !(storage_class == csc_static
4676 || storage_class == csc_register)))))
4678 error ("variable or field %qs declared void", name);
4679 type = integer_type_node;
4682 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
4683 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */
4686 tree decl;
4688 if (decl_context == PARM)
4690 tree promoted_type;
4692 /* A parameter declared as an array of T is really a pointer to T.
4693 One declared as a function is really a pointer to a function. */
4695 if (TREE_CODE (type) == ARRAY_TYPE)
4697 /* Transfer const-ness of array into that of type pointed to. */
4698 type = TREE_TYPE (type);
4699 if (type_quals)
4700 type = c_build_qualified_type (type, type_quals);
4701 type = build_pointer_type (type);
4702 type_quals = array_ptr_quals;
4703 if (type_quals)
4704 type = c_build_qualified_type (type, type_quals);
4706 /* We don't yet implement attributes in this context. */
4707 if (array_ptr_attrs != NULL_TREE)
4708 warning (OPT_Wattributes,
4709 "attributes in parameter array declarator ignored");
4711 size_varies = 0;
4713 else if (TREE_CODE (type) == FUNCTION_TYPE)
4715 if (pedantic && type_quals)
4716 pedwarn ("ISO C forbids qualified function types");
4717 if (type_quals)
4718 type = c_build_qualified_type (type, type_quals);
4719 type = build_pointer_type (type);
4720 type_quals = TYPE_UNQUALIFIED;
4722 else if (type_quals)
4723 type = c_build_qualified_type (type, type_quals);
4725 decl = build_decl (PARM_DECL, declarator->u.id, type);
4726 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4727 if (size_varies)
4728 C_DECL_VARIABLE_SIZE (decl) = 1;
4730 /* Compute the type actually passed in the parmlist,
4731 for the case where there is no prototype.
4732 (For example, shorts and chars are passed as ints.)
4733 When there is a prototype, this is overridden later. */
4735 if (type == error_mark_node)
4736 promoted_type = type;
4737 else
4738 promoted_type = c_type_promotes_to (type);
4740 DECL_ARG_TYPE (decl) = promoted_type;
4741 if (declspecs->inline_p)
4742 pedwarn ("parameter %q+D declared %<inline%>", decl);
4744 else if (decl_context == FIELD)
4746 /* Note that the grammar rejects storage classes in typenames
4747 and fields. */
4748 gcc_assert (storage_class == csc_none && !threadp
4749 && !declspecs->inline_p);
4751 /* Structure field. It may not be a function. */
4753 if (TREE_CODE (type) == FUNCTION_TYPE)
4755 error ("field %qs declared as a function", name);
4756 type = build_pointer_type (type);
4758 else if (TREE_CODE (type) != ERROR_MARK
4759 && !COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (type))
4761 error ("field %qs has incomplete type", name);
4762 type = error_mark_node;
4764 type = c_build_qualified_type (type, type_quals);
4765 decl = build_decl (FIELD_DECL, declarator->u.id, type);
4766 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4767 DECL_NONADDRESSABLE_P (decl) = bitfield;
4768 if (bitfield && !declarator->u.id)
4769 TREE_NO_WARNING (decl) = 1;
4771 if (size_varies)
4772 C_DECL_VARIABLE_SIZE (decl) = 1;
4774 else if (TREE_CODE (type) == FUNCTION_TYPE)
4776 if (storage_class == csc_register || threadp)
4778 error ("invalid storage class for function %qs", name);
4780 else if (current_scope != file_scope)
4782 /* Function declaration not at file scope. Storage
4783 classes other than `extern' are not allowed, C99
4784 6.7.1p5, and `extern' makes no difference. However,
4785 GCC allows 'auto', perhaps with 'inline', to support
4786 nested functions. */
4787 if (storage_class == csc_auto)
4789 if (pedantic)
4790 pedwarn ("invalid storage class for function %qs", name);
4792 else if (storage_class == csc_static)
4794 error ("invalid storage class for function %qs", name);
4795 if (funcdef_flag)
4796 storage_class = declspecs->storage_class = csc_none;
4797 else
4798 return 0;
4802 decl = build_decl (FUNCTION_DECL, declarator->u.id, type);
4803 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4804 decl = build_decl_attribute_variant (decl, decl_attr);
4806 if (pedantic && type_quals && !DECL_IN_SYSTEM_HEADER (decl))
4807 pedwarn ("ISO C forbids qualified function types");
4809 /* GNU C interprets a volatile-qualified function type to indicate
4810 that the function does not return. */
4811 if ((type_quals & TYPE_QUAL_VOLATILE)
4812 && !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4813 warning (0, "%<noreturn%> function returns non-void value");
4815 /* Every function declaration is an external reference
4816 (DECL_EXTERNAL) except for those which are not at file
4817 scope and are explicitly declared "auto". This is
4818 forbidden by standard C (C99 6.7.1p5) and is interpreted by
4819 GCC to signify a forward declaration of a nested function. */
4820 if (storage_class == csc_auto && current_scope != file_scope)
4821 DECL_EXTERNAL (decl) = 0;
4822 /* In C99, a function which is declared 'inline' with 'extern'
4823 is not an external reference (which is confusing). It
4824 means that the later definition of the function must be output
4825 in this file, C99 6.7.4p6. In GNU C89, a function declared
4826 'extern inline' is an external reference. */
4827 else if (declspecs->inline_p && storage_class != csc_static)
4828 DECL_EXTERNAL (decl) = ((storage_class == csc_extern)
4829 == flag_gnu89_inline);
4830 else
4831 DECL_EXTERNAL (decl) = !initialized;
4833 /* Record absence of global scope for `static' or `auto'. */
4834 TREE_PUBLIC (decl)
4835 = !(storage_class == csc_static || storage_class == csc_auto);
4837 /* For a function definition, record the argument information
4838 block where store_parm_decls will look for it. */
4839 if (funcdef_flag)
4840 current_function_arg_info = arg_info;
4842 if (declspecs->default_int_p)
4843 C_FUNCTION_IMPLICIT_INT (decl) = 1;
4845 /* Record presence of `inline', if it is reasonable. */
4846 if (flag_hosted && MAIN_NAME_P (declarator->u.id))
4848 if (declspecs->inline_p)
4849 pedwarn ("cannot inline function %<main%>");
4851 else if (declspecs->inline_p)
4853 /* Record that the function is declared `inline'. */
4854 DECL_DECLARED_INLINE_P (decl) = 1;
4856 /* Do not mark bare declarations as DECL_INLINE. Doing so
4857 in the presence of multiple declarations can result in
4858 the abstract origin pointing between the declarations,
4859 which will confuse dwarf2out. */
4860 if (initialized)
4861 DECL_INLINE (decl) = 1;
4863 /* If -finline-functions, assume it can be inlined. This does
4864 two things: let the function be deferred until it is actually
4865 needed, and let dwarf2 know that the function is inlinable. */
4866 else if (flag_inline_trees == 2 && initialized)
4867 DECL_INLINE (decl) = 1;
4869 else
4871 /* It's a variable. */
4872 /* An uninitialized decl with `extern' is a reference. */
4873 int extern_ref = !initialized && storage_class == csc_extern;
4875 type = c_build_qualified_type (type, type_quals);
4877 /* C99 6.2.2p7: It is invalid (compile-time undefined
4878 behavior) to create an 'extern' declaration for a
4879 variable if there is a global declaration that is
4880 'static' and the global declaration is not visible.
4881 (If the static declaration _is_ currently visible,
4882 the 'extern' declaration is taken to refer to that decl.) */
4883 if (extern_ref && current_scope != file_scope)
4885 tree global_decl = identifier_global_value (declarator->u.id);
4886 tree visible_decl = lookup_name (declarator->u.id);
4888 if (global_decl
4889 && global_decl != visible_decl
4890 && TREE_CODE (global_decl) == VAR_DECL
4891 && !TREE_PUBLIC (global_decl))
4892 error ("variable previously declared %<static%> redeclared "
4893 "%<extern%>");
4896 decl = build_decl (VAR_DECL, declarator->u.id, type);
4897 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
4898 if (size_varies)
4899 C_DECL_VARIABLE_SIZE (decl) = 1;
4901 if (declspecs->inline_p)
4902 pedwarn ("variable %q+D declared %<inline%>", decl);
4904 /* At file scope, an initialized extern declaration may follow
4905 a static declaration. In that case, DECL_EXTERNAL will be
4906 reset later in start_decl. */
4907 DECL_EXTERNAL (decl) = (storage_class == csc_extern);
4909 /* At file scope, the presence of a `static' or `register' storage
4910 class specifier, or the absence of all storage class specifiers
4911 makes this declaration a definition (perhaps tentative). Also,
4912 the absence of `static' makes it public. */
4913 if (current_scope == file_scope)
4915 TREE_PUBLIC (decl) = storage_class != csc_static;
4916 TREE_STATIC (decl) = !extern_ref;
4918 /* Not at file scope, only `static' makes a static definition. */
4919 else
4921 TREE_STATIC (decl) = (storage_class == csc_static);
4922 TREE_PUBLIC (decl) = extern_ref;
4925 if (threadp)
4926 DECL_TLS_MODEL (decl) = decl_default_tls_model (decl);
4929 if (storage_class == csc_extern
4930 && variably_modified_type_p (type, NULL_TREE))
4932 /* C99 6.7.5.2p2 */
4933 error ("object with variably modified type must have no linkage");
4936 /* Record `register' declaration for warnings on &
4937 and in case doing stupid register allocation. */
4939 if (storage_class == csc_register)
4941 C_DECL_REGISTER (decl) = 1;
4942 DECL_REGISTER (decl) = 1;
4945 /* Record constancy and volatility. */
4946 c_apply_type_quals_to_decl (type_quals, decl);
4948 /* If a type has volatile components, it should be stored in memory.
4949 Otherwise, the fact that those components are volatile
4950 will be ignored, and would even crash the compiler.
4951 Of course, this only makes sense on VAR,PARM, and RESULT decl's. */
4952 if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (decl))
4953 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
4954 || TREE_CODE (decl) == RESULT_DECL))
4956 /* It is not an error for a structure with volatile fields to
4957 be declared register, but reset DECL_REGISTER since it
4958 cannot actually go in a register. */
4959 int was_reg = C_DECL_REGISTER (decl);
4960 C_DECL_REGISTER (decl) = 0;
4961 DECL_REGISTER (decl) = 0;
4962 c_mark_addressable (decl);
4963 C_DECL_REGISTER (decl) = was_reg;
4966 /* This is the earliest point at which we might know the assembler
4967 name of a variable. Thus, if it's known before this, die horribly. */
4968 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
4970 return decl;
4974 /* Decode the parameter-list info for a function type or function definition.
4975 The argument is the value returned by `get_parm_info' (or made in c-parse.c
4976 if there is an identifier list instead of a parameter decl list).
4977 These two functions are separate because when a function returns
4978 or receives functions then each is called multiple times but the order
4979 of calls is different. The last call to `grokparms' is always the one
4980 that contains the formal parameter names of a function definition.
4982 Return a list of arg types to use in the FUNCTION_TYPE for this function.
4984 FUNCDEF_FLAG is true for a function definition, false for
4985 a mere declaration. A nonempty identifier-list gets an error message
4986 when FUNCDEF_FLAG is false. */
4988 static tree
4989 grokparms (struct c_arg_info *arg_info, bool funcdef_flag)
4991 tree arg_types = arg_info->types;
4993 if (funcdef_flag && arg_info->had_vla_unspec)
4995 /* A function definition isn't function prototype scope C99 6.2.1p4. */
4996 /* C99 6.7.5.2p4 */
4997 error ("%<[*]%> not allowed in other than function prototype scope");
5000 if (arg_types == 0 && !funcdef_flag && !in_system_header)
5001 warning (OPT_Wstrict_prototypes,
5002 "function declaration isn%'t a prototype");
5004 if (arg_types == error_mark_node)
5005 return 0; /* don't set TYPE_ARG_TYPES in this case */
5007 else if (arg_types && TREE_CODE (TREE_VALUE (arg_types)) == IDENTIFIER_NODE)
5009 if (!funcdef_flag)
5010 pedwarn ("parameter names (without types) in function declaration");
5012 arg_info->parms = arg_info->types;
5013 arg_info->types = 0;
5014 return 0;
5016 else
5018 tree parm, type, typelt;
5019 unsigned int parmno;
5021 /* If there is a parameter of incomplete type in a definition,
5022 this is an error. In a declaration this is valid, and a
5023 struct or union type may be completed later, before any calls
5024 or definition of the function. In the case where the tag was
5025 first declared within the parameter list, a warning has
5026 already been given. If a parameter has void type, then
5027 however the function cannot be defined or called, so
5028 warn. */
5030 for (parm = arg_info->parms, typelt = arg_types, parmno = 1;
5031 parm;
5032 parm = TREE_CHAIN (parm), typelt = TREE_CHAIN (typelt), parmno++)
5034 type = TREE_VALUE (typelt);
5035 if (type == error_mark_node)
5036 continue;
5038 if (!COMPLETE_TYPE_P (type))
5040 if (funcdef_flag)
5042 if (DECL_NAME (parm))
5043 error ("parameter %u (%q+D) has incomplete type",
5044 parmno, parm);
5045 else
5046 error ("%Jparameter %u has incomplete type",
5047 parm, parmno);
5049 TREE_VALUE (typelt) = error_mark_node;
5050 TREE_TYPE (parm) = error_mark_node;
5052 else if (VOID_TYPE_P (type))
5054 if (DECL_NAME (parm))
5055 warning (0, "parameter %u (%q+D) has void type",
5056 parmno, parm);
5057 else
5058 warning (0, "%Jparameter %u has void type",
5059 parm, parmno);
5063 if (DECL_NAME (parm) && TREE_USED (parm))
5064 warn_if_shadowing (parm);
5066 return arg_types;
5070 /* Take apart the current scope and return a c_arg_info structure with
5071 info on a parameter list just parsed.
5073 This structure is later fed to 'grokparms' and 'store_parm_decls'.
5075 ELLIPSIS being true means the argument list ended in '...' so don't
5076 append a sentinel (void_list_node) to the end of the type-list. */
5078 struct c_arg_info *
5079 get_parm_info (bool ellipsis)
5081 struct c_binding *b = current_scope->bindings;
5082 struct c_arg_info *arg_info = XOBNEW (&parser_obstack,
5083 struct c_arg_info);
5084 tree parms = 0;
5085 tree tags = 0;
5086 tree types = 0;
5087 tree others = 0;
5089 static bool explained_incomplete_types = false;
5090 bool gave_void_only_once_err = false;
5092 arg_info->parms = 0;
5093 arg_info->tags = 0;
5094 arg_info->types = 0;
5095 arg_info->others = 0;
5096 arg_info->pending_sizes = 0;
5097 arg_info->had_vla_unspec = current_scope->had_vla_unspec;
5099 /* The bindings in this scope must not get put into a block.
5100 We will take care of deleting the binding nodes. */
5101 current_scope->bindings = 0;
5103 /* This function is only called if there was *something* on the
5104 parameter list. */
5105 gcc_assert (b);
5107 /* A parameter list consisting solely of 'void' indicates that the
5108 function takes no arguments. But if the 'void' is qualified
5109 (by 'const' or 'volatile'), or has a storage class specifier
5110 ('register'), then the behavior is undefined; issue an error.
5111 Typedefs for 'void' are OK (see DR#157). */
5112 if (b->prev == 0 /* one binding */
5113 && TREE_CODE (b->decl) == PARM_DECL /* which is a parameter */
5114 && !DECL_NAME (b->decl) /* anonymous */
5115 && VOID_TYPE_P (TREE_TYPE (b->decl))) /* of void type */
5117 if (TREE_THIS_VOLATILE (b->decl)
5118 || TREE_READONLY (b->decl)
5119 || C_DECL_REGISTER (b->decl))
5120 error ("%<void%> as only parameter may not be qualified");
5122 /* There cannot be an ellipsis. */
5123 if (ellipsis)
5124 error ("%<void%> must be the only parameter");
5126 arg_info->types = void_list_node;
5127 return arg_info;
5130 if (!ellipsis)
5131 types = void_list_node;
5133 /* Break up the bindings list into parms, tags, types, and others;
5134 apply sanity checks; purge the name-to-decl bindings. */
5135 while (b)
5137 tree decl = b->decl;
5138 tree type = TREE_TYPE (decl);
5139 const char *keyword;
5141 switch (TREE_CODE (decl))
5143 case PARM_DECL:
5144 if (b->id)
5146 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5147 I_SYMBOL_BINDING (b->id) = b->shadowed;
5150 /* Check for forward decls that never got their actual decl. */
5151 if (TREE_ASM_WRITTEN (decl))
5152 error ("parameter %q+D has just a forward declaration", decl);
5153 /* Check for (..., void, ...) and issue an error. */
5154 else if (VOID_TYPE_P (type) && !DECL_NAME (decl))
5156 if (!gave_void_only_once_err)
5158 error ("%<void%> must be the only parameter");
5159 gave_void_only_once_err = true;
5162 else
5164 /* Valid parameter, add it to the list. */
5165 TREE_CHAIN (decl) = parms;
5166 parms = decl;
5168 /* Since there is a prototype, args are passed in their
5169 declared types. The back end may override this later. */
5170 DECL_ARG_TYPE (decl) = type;
5171 types = tree_cons (0, type, types);
5173 break;
5175 case ENUMERAL_TYPE: keyword = "enum"; goto tag;
5176 case UNION_TYPE: keyword = "union"; goto tag;
5177 case RECORD_TYPE: keyword = "struct"; goto tag;
5178 tag:
5179 /* Types may not have tag-names, in which case the type
5180 appears in the bindings list with b->id NULL. */
5181 if (b->id)
5183 gcc_assert (I_TAG_BINDING (b->id) == b);
5184 I_TAG_BINDING (b->id) = b->shadowed;
5187 /* Warn about any struct, union or enum tags defined in a
5188 parameter list. The scope of such types is limited to
5189 the parameter list, which is rarely if ever desirable
5190 (it's impossible to call such a function with type-
5191 correct arguments). An anonymous union parm type is
5192 meaningful as a GNU extension, so don't warn for that. */
5193 if (TREE_CODE (decl) != UNION_TYPE || b->id != 0)
5195 if (b->id)
5196 /* The %s will be one of 'struct', 'union', or 'enum'. */
5197 warning (0, "%<%s %E%> declared inside parameter list",
5198 keyword, b->id);
5199 else
5200 /* The %s will be one of 'struct', 'union', or 'enum'. */
5201 warning (0, "anonymous %s declared inside parameter list",
5202 keyword);
5204 if (!explained_incomplete_types)
5206 warning (0, "its scope is only this definition or declaration,"
5207 " which is probably not what you want");
5208 explained_incomplete_types = true;
5212 tags = tree_cons (b->id, decl, tags);
5213 break;
5215 case CONST_DECL:
5216 case TYPE_DECL:
5217 case FUNCTION_DECL:
5218 /* CONST_DECLs appear here when we have an embedded enum,
5219 and TYPE_DECLs appear here when we have an embedded struct
5220 or union. No warnings for this - we already warned about the
5221 type itself. FUNCTION_DECLs appear when there is an implicit
5222 function declaration in the parameter list. */
5224 TREE_CHAIN (decl) = others;
5225 others = decl;
5226 /* fall through */
5228 case ERROR_MARK:
5229 /* error_mark_node appears here when we have an undeclared
5230 variable. Just throw it away. */
5231 if (b->id)
5233 gcc_assert (I_SYMBOL_BINDING (b->id) == b);
5234 I_SYMBOL_BINDING (b->id) = b->shadowed;
5236 break;
5238 /* Other things that might be encountered. */
5239 case LABEL_DECL:
5240 case VAR_DECL:
5241 default:
5242 gcc_unreachable ();
5245 b = free_binding_and_advance (b);
5248 arg_info->parms = parms;
5249 arg_info->tags = tags;
5250 arg_info->types = types;
5251 arg_info->others = others;
5252 arg_info->pending_sizes = get_pending_sizes ();
5253 return arg_info;
5256 /* Get the struct, enum or union (CODE says which) with tag NAME.
5257 Define the tag as a forward-reference if it is not defined.
5258 Return a c_typespec structure for the type specifier. */
5260 struct c_typespec
5261 parser_xref_tag (enum tree_code code, tree name)
5263 struct c_typespec ret;
5264 /* If a cross reference is requested, look up the type
5265 already defined for this tag and return it. */
5267 tree ref = lookup_tag (code, name, 0);
5268 /* If this is the right type of tag, return what we found.
5269 (This reference will be shadowed by shadow_tag later if appropriate.)
5270 If this is the wrong type of tag, do not return it. If it was the
5271 wrong type in the same scope, we will have had an error
5272 message already; if in a different scope and declaring
5273 a name, pending_xref_error will give an error message; but if in a
5274 different scope and not declaring a name, this tag should
5275 shadow the previous declaration of a different type of tag, and
5276 this would not work properly if we return the reference found.
5277 (For example, with "struct foo" in an outer scope, "union foo;"
5278 must shadow that tag with a new one of union type.) */
5279 ret.kind = (ref ? ctsk_tagref : ctsk_tagfirstref);
5280 if (ref && TREE_CODE (ref) == code)
5282 ret.spec = ref;
5283 return ret;
5286 /* If no such tag is yet defined, create a forward-reference node
5287 and record it as the "definition".
5288 When a real declaration of this type is found,
5289 the forward-reference will be altered into a real type. */
5291 ref = make_node (code);
5292 if (code == ENUMERAL_TYPE)
5294 /* Give the type a default layout like unsigned int
5295 to avoid crashing if it does not get defined. */
5296 TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
5297 TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
5298 TYPE_USER_ALIGN (ref) = 0;
5299 TYPE_UNSIGNED (ref) = 1;
5300 TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
5301 TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
5302 TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
5305 pushtag (name, ref);
5307 ret.spec = ref;
5308 return ret;
5311 /* Get the struct, enum or union (CODE says which) with tag NAME.
5312 Define the tag as a forward-reference if it is not defined.
5313 Return a tree for the type. */
5315 tree
5316 xref_tag (enum tree_code code, tree name)
5318 return parser_xref_tag (code, name).spec;
5321 /* Make sure that the tag NAME is defined *in the current scope*
5322 at least as a forward reference.
5323 CODE says which kind of tag NAME ought to be. */
5325 tree
5326 start_struct (enum tree_code code, tree name)
5328 /* If there is already a tag defined at this scope
5329 (as a forward reference), just return it. */
5331 tree ref = 0;
5333 if (name != 0)
5334 ref = lookup_tag (code, name, 1);
5335 if (ref && TREE_CODE (ref) == code)
5337 if (TYPE_SIZE (ref))
5339 if (code == UNION_TYPE)
5340 error ("redefinition of %<union %E%>", name);
5341 else
5342 error ("redefinition of %<struct %E%>", name);
5344 else if (C_TYPE_BEING_DEFINED (ref))
5346 if (code == UNION_TYPE)
5347 error ("nested redefinition of %<union %E%>", name);
5348 else
5349 error ("nested redefinition of %<struct %E%>", name);
5350 /* Don't create structures that contain themselves. */
5351 ref = NULL_TREE;
5355 /* Otherwise create a forward-reference just so the tag is in scope. */
5357 if (ref == NULL_TREE || TREE_CODE (ref) != code)
5359 ref = make_node (code);
5360 pushtag (name, ref);
5363 C_TYPE_BEING_DEFINED (ref) = 1;
5364 TYPE_PACKED (ref) = flag_pack_struct;
5365 return ref;
5368 /* Process the specs, declarator and width (NULL if omitted)
5369 of a structure component, returning a FIELD_DECL node.
5370 WIDTH is non-NULL for bit-fields only, and is an INTEGER_CST node.
5371 DECL_ATTRS is as for grokdeclarator.
5373 This is done during the parsing of the struct declaration.
5374 The FIELD_DECL nodes are chained together and the lot of them
5375 are ultimately passed to `build_struct' to make the RECORD_TYPE node. */
5377 tree
5378 grokfield (struct c_declarator *declarator, struct c_declspecs *declspecs,
5379 tree width, tree *decl_attrs)
5381 tree value;
5383 if (declarator->kind == cdk_id && declarator->u.id == NULL_TREE
5384 && width == NULL_TREE)
5386 /* This is an unnamed decl.
5388 If we have something of the form "union { list } ;" then this
5389 is the anonymous union extension. Similarly for struct.
5391 If this is something of the form "struct foo;", then
5392 If MS extensions are enabled, this is handled as an
5393 anonymous struct.
5394 Otherwise this is a forward declaration of a structure tag.
5396 If this is something of the form "foo;" and foo is a TYPE_DECL, then
5397 If MS extensions are enabled and foo names a structure, then
5398 again this is an anonymous struct.
5399 Otherwise this is an error.
5401 Oh what a horrid tangled web we weave. I wonder if MS consciously
5402 took this from Plan 9 or if it was an accident of implementation
5403 that took root before someone noticed the bug... */
5405 tree type = declspecs->type;
5406 bool type_ok = (TREE_CODE (type) == RECORD_TYPE
5407 || TREE_CODE (type) == UNION_TYPE);
5408 bool ok = false;
5410 if (type_ok
5411 && (flag_ms_extensions || !declspecs->typedef_p))
5413 if (flag_ms_extensions)
5414 ok = true;
5415 else if (flag_iso)
5416 ok = false;
5417 else if (TYPE_NAME (type) == NULL)
5418 ok = true;
5419 else
5420 ok = false;
5422 if (!ok)
5424 pedwarn ("declaration does not declare anything");
5425 return NULL_TREE;
5427 if (pedantic)
5428 pedwarn ("ISO C doesn%'t support unnamed structs/unions");
5431 value = grokdeclarator (declarator, declspecs, FIELD, false,
5432 width ? &width : NULL, decl_attrs,
5433 DEPRECATED_NORMAL);
5435 finish_decl (value, NULL_TREE, NULL_TREE);
5436 DECL_INITIAL (value) = width;
5438 return value;
5441 /* Generate an error for any duplicate field names in FIELDLIST. Munge
5442 the list such that this does not present a problem later. */
5444 static void
5445 detect_field_duplicates (tree fieldlist)
5447 tree x, y;
5448 int timeout = 10;
5450 /* First, see if there are more than "a few" fields.
5451 This is trivially true if there are zero or one fields. */
5452 if (!fieldlist)
5453 return;
5454 x = TREE_CHAIN (fieldlist);
5455 if (!x)
5456 return;
5457 do {
5458 timeout--;
5459 x = TREE_CHAIN (x);
5460 } while (timeout > 0 && x);
5462 /* If there were "few" fields, avoid the overhead of allocating
5463 a hash table. Instead just do the nested traversal thing. */
5464 if (timeout > 0)
5466 for (x = TREE_CHAIN (fieldlist); x ; x = TREE_CHAIN (x))
5467 if (DECL_NAME (x))
5469 for (y = fieldlist; y != x; y = TREE_CHAIN (y))
5470 if (DECL_NAME (y) == DECL_NAME (x))
5472 error ("duplicate member %q+D", x);
5473 DECL_NAME (x) = NULL_TREE;
5477 else
5479 htab_t htab = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
5480 void **slot;
5482 for (x = fieldlist; x ; x = TREE_CHAIN (x))
5483 if ((y = DECL_NAME (x)) != 0)
5485 slot = htab_find_slot (htab, y, INSERT);
5486 if (*slot)
5488 error ("duplicate member %q+D", x);
5489 DECL_NAME (x) = NULL_TREE;
5491 *slot = y;
5494 htab_delete (htab);
5498 /* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
5499 FIELDLIST is a chain of FIELD_DECL nodes for the fields.
5500 ATTRIBUTES are attributes to be applied to the structure. */
5502 tree
5503 finish_struct (tree t, tree fieldlist, tree attributes)
5505 tree x;
5506 bool toplevel = file_scope == current_scope;
5507 int saw_named_field;
5509 /* If this type was previously laid out as a forward reference,
5510 make sure we lay it out again. */
5512 TYPE_SIZE (t) = 0;
5514 decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5516 if (pedantic)
5518 for (x = fieldlist; x; x = TREE_CHAIN (x))
5519 if (DECL_NAME (x) != 0)
5520 break;
5522 if (x == 0)
5524 if (TREE_CODE (t) == UNION_TYPE)
5526 if (fieldlist)
5527 pedwarn ("union has no named members");
5528 else
5529 pedwarn ("union has no members");
5531 else
5533 if (fieldlist)
5534 pedwarn ("struct has no named members");
5535 else
5536 pedwarn ("struct has no members");
5541 /* Install struct as DECL_CONTEXT of each field decl.
5542 Also process specified field sizes, found in the DECL_INITIAL,
5543 storing 0 there after the type has been changed to precision equal
5544 to its width, rather than the precision of the specified standard
5545 type. (Correct layout requires the original type to have been preserved
5546 until now.) */
5548 saw_named_field = 0;
5549 for (x = fieldlist; x; x = TREE_CHAIN (x))
5551 if (TREE_TYPE (x) == error_mark_node)
5552 continue;
5554 DECL_CONTEXT (x) = t;
5556 if (TYPE_PACKED (t) && TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
5557 DECL_PACKED (x) = 1;
5559 /* If any field is const, the structure type is pseudo-const. */
5560 if (TREE_READONLY (x))
5561 C_TYPE_FIELDS_READONLY (t) = 1;
5562 else
5564 /* A field that is pseudo-const makes the structure likewise. */
5565 tree t1 = TREE_TYPE (x);
5566 while (TREE_CODE (t1) == ARRAY_TYPE)
5567 t1 = TREE_TYPE (t1);
5568 if ((TREE_CODE (t1) == RECORD_TYPE || TREE_CODE (t1) == UNION_TYPE)
5569 && C_TYPE_FIELDS_READONLY (t1))
5570 C_TYPE_FIELDS_READONLY (t) = 1;
5573 /* Any field that is volatile means variables of this type must be
5574 treated in some ways as volatile. */
5575 if (TREE_THIS_VOLATILE (x))
5576 C_TYPE_FIELDS_VOLATILE (t) = 1;
5578 /* Any field of nominal variable size implies structure is too. */
5579 if (C_DECL_VARIABLE_SIZE (x))
5580 C_TYPE_VARIABLE_SIZE (t) = 1;
5582 if (DECL_INITIAL (x))
5584 unsigned HOST_WIDE_INT width = tree_low_cst (DECL_INITIAL (x), 1);
5585 DECL_SIZE (x) = bitsize_int (width);
5586 DECL_BIT_FIELD (x) = 1;
5587 SET_DECL_C_BIT_FIELD (x);
5590 /* Detect flexible array member in an invalid context. */
5591 if (TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
5592 && TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
5593 && TYPE_DOMAIN (TREE_TYPE (x)) != NULL_TREE
5594 && TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (x))) == NULL_TREE)
5596 if (TREE_CODE (t) == UNION_TYPE)
5598 error ("%Jflexible array member in union", x);
5599 TREE_TYPE (x) = error_mark_node;
5601 else if (TREE_CHAIN (x) != NULL_TREE)
5603 error ("%Jflexible array member not at end of struct", x);
5604 TREE_TYPE (x) = error_mark_node;
5606 else if (!saw_named_field)
5608 error ("%Jflexible array member in otherwise empty struct", x);
5609 TREE_TYPE (x) = error_mark_node;
5613 if (pedantic && !in_system_header && TREE_CODE (t) == RECORD_TYPE
5614 && flexible_array_type_p (TREE_TYPE (x)))
5615 pedwarn ("%Jinvalid use of structure with flexible array member", x);
5617 if (DECL_NAME (x))
5618 saw_named_field = 1;
5621 detect_field_duplicates (fieldlist);
5623 /* Now we have the nearly final fieldlist. Record it,
5624 then lay out the structure or union (including the fields). */
5626 TYPE_FIELDS (t) = fieldlist;
5628 layout_type (t);
5630 /* Give bit-fields their proper types. */
5632 tree *fieldlistp = &fieldlist;
5633 while (*fieldlistp)
5634 if (TREE_CODE (*fieldlistp) == FIELD_DECL && DECL_INITIAL (*fieldlistp)
5635 && TREE_TYPE (*fieldlistp) != error_mark_node)
5637 unsigned HOST_WIDE_INT width
5638 = tree_low_cst (DECL_INITIAL (*fieldlistp), 1);
5639 tree type = TREE_TYPE (*fieldlistp);
5640 if (width != TYPE_PRECISION (type))
5642 TREE_TYPE (*fieldlistp)
5643 = c_build_bitfield_integer_type (width, TYPE_UNSIGNED (type));
5644 DECL_MODE (*fieldlistp) = TYPE_MODE (TREE_TYPE (*fieldlistp));
5646 DECL_INITIAL (*fieldlistp) = 0;
5648 else
5649 fieldlistp = &TREE_CHAIN (*fieldlistp);
5652 /* Now we have the truly final field list.
5653 Store it in this type and in the variants. */
5655 TYPE_FIELDS (t) = fieldlist;
5657 /* If there are lots of fields, sort so we can look through them fast.
5658 We arbitrarily consider 16 or more elts to be "a lot". */
5661 int len = 0;
5663 for (x = fieldlist; x; x = TREE_CHAIN (x))
5665 if (len > 15 || DECL_NAME (x) == NULL)
5666 break;
5667 len += 1;
5670 if (len > 15)
5672 tree *field_array;
5673 struct lang_type *space;
5674 struct sorted_fields_type *space2;
5676 len += list_length (x);
5678 /* Use the same allocation policy here that make_node uses, to
5679 ensure that this lives as long as the rest of the struct decl.
5680 All decls in an inline function need to be saved. */
5682 space = GGC_CNEW (struct lang_type);
5683 space2 = GGC_NEWVAR (struct sorted_fields_type,
5684 sizeof (struct sorted_fields_type) + len * sizeof (tree));
5686 len = 0;
5687 space->s = space2;
5688 field_array = &space2->elts[0];
5689 for (x = fieldlist; x; x = TREE_CHAIN (x))
5691 field_array[len++] = x;
5693 /* If there is anonymous struct or union, break out of the loop. */
5694 if (DECL_NAME (x) == NULL)
5695 break;
5697 /* Found no anonymous struct/union. Add the TYPE_LANG_SPECIFIC. */
5698 if (x == NULL)
5700 TYPE_LANG_SPECIFIC (t) = space;
5701 TYPE_LANG_SPECIFIC (t)->s->len = len;
5702 field_array = TYPE_LANG_SPECIFIC (t)->s->elts;
5703 qsort (field_array, len, sizeof (tree), field_decl_cmp);
5708 for (x = TYPE_MAIN_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x))
5710 TYPE_FIELDS (x) = TYPE_FIELDS (t);
5711 TYPE_LANG_SPECIFIC (x) = TYPE_LANG_SPECIFIC (t);
5712 C_TYPE_FIELDS_READONLY (x) = C_TYPE_FIELDS_READONLY (t);
5713 C_TYPE_FIELDS_VOLATILE (x) = C_TYPE_FIELDS_VOLATILE (t);
5714 C_TYPE_VARIABLE_SIZE (x) = C_TYPE_VARIABLE_SIZE (t);
5717 /* If this was supposed to be a transparent union, but we can't
5718 make it one, warn and turn off the flag. */
5719 if (TREE_CODE (t) == UNION_TYPE
5720 && TYPE_TRANSPARENT_UNION (t)
5721 && (!TYPE_FIELDS (t) || TYPE_MODE (t) != DECL_MODE (TYPE_FIELDS (t))))
5723 TYPE_TRANSPARENT_UNION (t) = 0;
5724 warning (0, "union cannot be made transparent");
5727 /* If this structure or union completes the type of any previous
5728 variable declaration, lay it out and output its rtl. */
5729 for (x = C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t));
5731 x = TREE_CHAIN (x))
5733 tree decl = TREE_VALUE (x);
5734 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
5735 layout_array_type (TREE_TYPE (decl));
5736 if (TREE_CODE (decl) != TYPE_DECL)
5738 layout_decl (decl, 0);
5739 if (c_dialect_objc ())
5740 objc_check_decl (decl);
5741 rest_of_decl_compilation (decl, toplevel, 0);
5742 if (!toplevel)
5743 expand_decl (decl);
5746 C_TYPE_INCOMPLETE_VARS (TYPE_MAIN_VARIANT (t)) = 0;
5748 /* Finish debugging output for this type. */
5749 rest_of_type_compilation (t, toplevel);
5751 /* If we're inside a function proper, i.e. not file-scope and not still
5752 parsing parameters, then arrange for the size of a variable sized type
5753 to be bound now. */
5754 if (cur_stmt_list && variably_modified_type_p (t, NULL_TREE))
5755 add_stmt (build_stmt (DECL_EXPR, build_decl (TYPE_DECL, NULL, t)));
5757 return t;
5760 /* Lay out the type T, and its element type, and so on. */
5762 static void
5763 layout_array_type (tree t)
5765 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5766 layout_array_type (TREE_TYPE (t));
5767 layout_type (t);
5770 /* Begin compiling the definition of an enumeration type.
5771 NAME is its name (or null if anonymous).
5772 Returns the type object, as yet incomplete.
5773 Also records info about it so that build_enumerator
5774 may be used to declare the individual values as they are read. */
5776 tree
5777 start_enum (struct c_enum_contents *the_enum, tree name)
5779 tree enumtype = 0;
5781 /* If this is the real definition for a previous forward reference,
5782 fill in the contents in the same object that used to be the
5783 forward reference. */
5785 if (name != 0)
5786 enumtype = lookup_tag (ENUMERAL_TYPE, name, 1);
5788 if (enumtype == 0 || TREE_CODE (enumtype) != ENUMERAL_TYPE)
5790 enumtype = make_node (ENUMERAL_TYPE);
5791 pushtag (name, enumtype);
5794 if (C_TYPE_BEING_DEFINED (enumtype))
5795 error ("nested redefinition of %<enum %E%>", name);
5797 C_TYPE_BEING_DEFINED (enumtype) = 1;
5799 if (TYPE_VALUES (enumtype) != 0)
5801 /* This enum is a named one that has been declared already. */
5802 error ("redeclaration of %<enum %E%>", name);
5804 /* Completely replace its old definition.
5805 The old enumerators remain defined, however. */
5806 TYPE_VALUES (enumtype) = 0;
5809 the_enum->enum_next_value = integer_zero_node;
5810 the_enum->enum_overflow = 0;
5812 if (flag_short_enums)
5813 TYPE_PACKED (enumtype) = 1;
5815 return enumtype;
5818 /* After processing and defining all the values of an enumeration type,
5819 install their decls in the enumeration type and finish it off.
5820 ENUMTYPE is the type object, VALUES a list of decl-value pairs,
5821 and ATTRIBUTES are the specified attributes.
5822 Returns ENUMTYPE. */
5824 tree
5825 finish_enum (tree enumtype, tree values, tree attributes)
5827 tree pair, tem;
5828 tree minnode = 0, maxnode = 0;
5829 int precision, unsign;
5830 bool toplevel = (file_scope == current_scope);
5831 struct lang_type *lt;
5833 decl_attributes (&enumtype, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5835 /* Calculate the maximum value of any enumerator in this type. */
5837 if (values == error_mark_node)
5838 minnode = maxnode = integer_zero_node;
5839 else
5841 minnode = maxnode = TREE_VALUE (values);
5842 for (pair = TREE_CHAIN (values); pair; pair = TREE_CHAIN (pair))
5844 tree value = TREE_VALUE (pair);
5845 if (tree_int_cst_lt (maxnode, value))
5846 maxnode = value;
5847 if (tree_int_cst_lt (value, minnode))
5848 minnode = value;
5852 /* Construct the final type of this enumeration. It is the same
5853 as one of the integral types - the narrowest one that fits, except
5854 that normally we only go as narrow as int - and signed iff any of
5855 the values are negative. */
5856 unsign = (tree_int_cst_sgn (minnode) >= 0);
5857 precision = MAX (min_precision (minnode, unsign),
5858 min_precision (maxnode, unsign));
5860 if (TYPE_PACKED (enumtype) || precision > TYPE_PRECISION (integer_type_node))
5862 tem = c_common_type_for_size (precision, unsign);
5863 if (tem == NULL)
5865 warning (0, "enumeration values exceed range of largest integer");
5866 tem = long_long_integer_type_node;
5869 else
5870 tem = unsign ? unsigned_type_node : integer_type_node;
5872 TYPE_MIN_VALUE (enumtype) = TYPE_MIN_VALUE (tem);
5873 TYPE_MAX_VALUE (enumtype) = TYPE_MAX_VALUE (tem);
5874 TYPE_UNSIGNED (enumtype) = TYPE_UNSIGNED (tem);
5875 TYPE_SIZE (enumtype) = 0;
5877 /* If the precision of the type was specific with an attribute and it
5878 was too small, give an error. Otherwise, use it. */
5879 if (TYPE_PRECISION (enumtype))
5881 if (precision > TYPE_PRECISION (enumtype))
5882 error ("specified mode too small for enumeral values");
5884 else
5885 TYPE_PRECISION (enumtype) = TYPE_PRECISION (tem);
5887 layout_type (enumtype);
5889 if (values != error_mark_node)
5891 /* Change the type of the enumerators to be the enum type. We
5892 need to do this irrespective of the size of the enum, for
5893 proper type checking. Replace the DECL_INITIALs of the
5894 enumerators, and the value slots of the list, with copies
5895 that have the enum type; they cannot be modified in place
5896 because they may be shared (e.g. integer_zero_node) Finally,
5897 change the purpose slots to point to the names of the decls. */
5898 for (pair = values; pair; pair = TREE_CHAIN (pair))
5900 tree enu = TREE_PURPOSE (pair);
5901 tree ini = DECL_INITIAL (enu);
5903 TREE_TYPE (enu) = enumtype;
5905 /* The ISO C Standard mandates enumerators to have type int,
5906 even though the underlying type of an enum type is
5907 unspecified. Here we convert any enumerators that fit in
5908 an int to type int, to avoid promotions to unsigned types
5909 when comparing integers with enumerators that fit in the
5910 int range. When -pedantic is given, build_enumerator()
5911 would have already taken care of those that don't fit. */
5912 if (int_fits_type_p (ini, integer_type_node))
5913 tem = integer_type_node;
5914 else
5915 tem = enumtype;
5916 ini = convert (tem, ini);
5918 DECL_INITIAL (enu) = ini;
5919 TREE_PURPOSE (pair) = DECL_NAME (enu);
5920 TREE_VALUE (pair) = ini;
5923 TYPE_VALUES (enumtype) = values;
5926 /* Record the min/max values so that we can warn about bit-field
5927 enumerations that are too small for the values. */
5928 lt = GGC_CNEW (struct lang_type);
5929 lt->enum_min = minnode;
5930 lt->enum_max = maxnode;
5931 TYPE_LANG_SPECIFIC (enumtype) = lt;
5933 /* Fix up all variant types of this enum type. */
5934 for (tem = TYPE_MAIN_VARIANT (enumtype); tem; tem = TYPE_NEXT_VARIANT (tem))
5936 if (tem == enumtype)
5937 continue;
5938 TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
5939 TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
5940 TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
5941 TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
5942 TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
5943 TYPE_MODE (tem) = TYPE_MODE (enumtype);
5944 TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
5945 TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
5946 TYPE_USER_ALIGN (tem) = TYPE_USER_ALIGN (enumtype);
5947 TYPE_UNSIGNED (tem) = TYPE_UNSIGNED (enumtype);
5948 TYPE_LANG_SPECIFIC (tem) = TYPE_LANG_SPECIFIC (enumtype);
5951 /* Finish debugging output for this type. */
5952 rest_of_type_compilation (enumtype, toplevel);
5954 return enumtype;
5957 /* Build and install a CONST_DECL for one value of the
5958 current enumeration type (one that was begun with start_enum).
5959 Return a tree-list containing the CONST_DECL and its value.
5960 Assignment of sequential values by default is handled here. */
5962 tree
5963 build_enumerator (struct c_enum_contents *the_enum, tree name, tree value)
5965 tree decl, type;
5967 /* Validate and default VALUE. */
5969 if (value != 0)
5971 /* Don't issue more errors for error_mark_node (i.e. an
5972 undeclared identifier) - just ignore the value expression. */
5973 if (value == error_mark_node)
5974 value = 0;
5975 else if (!INTEGRAL_TYPE_P (TREE_TYPE (value))
5976 || TREE_CODE (value) != INTEGER_CST)
5978 error ("enumerator value for %qE is not an integer constant", name);
5979 value = 0;
5981 else
5983 value = default_conversion (value);
5984 constant_expression_warning (value);
5988 /* Default based on previous value. */
5989 /* It should no longer be possible to have NON_LVALUE_EXPR
5990 in the default. */
5991 if (value == 0)
5993 value = the_enum->enum_next_value;
5994 if (the_enum->enum_overflow)
5995 error ("overflow in enumeration values");
5998 if (pedantic && !int_fits_type_p (value, integer_type_node))
6000 pedwarn ("ISO C restricts enumerator values to range of %<int%>");
6001 /* XXX This causes -pedantic to change the meaning of the program.
6002 Remove? -zw 2004-03-15 */
6003 value = convert (integer_type_node, value);
6006 /* Set basis for default for next value. */
6007 the_enum->enum_next_value = build_binary_op (PLUS_EXPR, value,
6008 integer_one_node, 0);
6009 the_enum->enum_overflow = tree_int_cst_lt (the_enum->enum_next_value, value);
6011 /* Now create a declaration for the enum value name. */
6013 type = TREE_TYPE (value);
6014 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
6015 TYPE_PRECISION (integer_type_node)),
6016 (TYPE_PRECISION (type)
6017 >= TYPE_PRECISION (integer_type_node)
6018 && TYPE_UNSIGNED (type)));
6020 decl = build_decl (CONST_DECL, name, type);
6021 DECL_INITIAL (decl) = convert (type, value);
6022 pushdecl (decl);
6024 return tree_cons (decl, value, NULL_TREE);
6028 /* Create the FUNCTION_DECL for a function definition.
6029 DECLSPECS, DECLARATOR and ATTRIBUTES are the parts of
6030 the declaration; they describe the function's name and the type it returns,
6031 but twisted together in a fashion that parallels the syntax of C.
6033 This function creates a binding context for the function body
6034 as well as setting up the FUNCTION_DECL in current_function_decl.
6036 Returns 1 on success. If the DECLARATOR is not suitable for a function
6037 (it defines a datum instead), we return 0, which tells
6038 yyparse to report a parse error. */
6041 start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
6042 tree attributes)
6044 tree decl1, old_decl;
6045 tree restype, resdecl;
6046 struct c_label_context_se *nstack_se;
6047 struct c_label_context_vm *nstack_vm;
6049 current_function_returns_value = 0; /* Assume, until we see it does. */
6050 current_function_returns_null = 0;
6051 current_function_returns_abnormally = 0;
6052 warn_about_return_type = 0;
6053 c_switch_stack = NULL;
6055 nstack_se = XOBNEW (&parser_obstack, struct c_label_context_se);
6056 nstack_se->labels_def = NULL;
6057 nstack_se->labels_used = NULL;
6058 nstack_se->next = label_context_stack_se;
6059 label_context_stack_se = nstack_se;
6061 nstack_vm = XOBNEW (&parser_obstack, struct c_label_context_vm);
6062 nstack_vm->labels_def = NULL;
6063 nstack_vm->labels_used = NULL;
6064 nstack_vm->scope = 0;
6065 nstack_vm->next = label_context_stack_vm;
6066 label_context_stack_vm = nstack_vm;
6068 /* Indicate no valid break/continue context by setting these variables
6069 to some non-null, non-label value. We'll notice and emit the proper
6070 error message in c_finish_bc_stmt. */
6071 c_break_label = c_cont_label = size_zero_node;
6073 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
6074 &attributes, DEPRECATED_NORMAL);
6076 /* If the declarator is not suitable for a function definition,
6077 cause a syntax error. */
6078 if (decl1 == 0)
6080 label_context_stack_se = label_context_stack_se->next;
6081 label_context_stack_vm = label_context_stack_vm->next;
6082 return 0;
6085 decl_attributes (&decl1, attributes, 0);
6087 if (DECL_DECLARED_INLINE_P (decl1)
6088 && DECL_UNINLINABLE (decl1)
6089 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl1)))
6090 warning (OPT_Wattributes, "inline function %q+D given attribute noinline",
6091 decl1);
6093 /* Handle gnu_inline attribute. */
6094 if (declspecs->inline_p
6095 && !flag_gnu89_inline
6096 && TREE_CODE (decl1) == FUNCTION_DECL
6097 && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl1)))
6099 if (declspecs->storage_class != csc_static)
6100 DECL_EXTERNAL (decl1) = !DECL_EXTERNAL (decl1);
6103 announce_function (decl1);
6105 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl1))))
6107 error ("return type is an incomplete type");
6108 /* Make it return void instead. */
6109 TREE_TYPE (decl1)
6110 = build_function_type (void_type_node,
6111 TYPE_ARG_TYPES (TREE_TYPE (decl1)));
6114 if (warn_about_return_type)
6115 pedwarn_c99 ("return type defaults to %<int%>");
6117 /* Make the init_value nonzero so pushdecl knows this is not tentative.
6118 error_mark_node is replaced below (in pop_scope) with the BLOCK. */
6119 DECL_INITIAL (decl1) = error_mark_node;
6121 /* If this definition isn't a prototype and we had a prototype declaration
6122 before, copy the arg type info from that prototype. */
6123 old_decl = lookup_name_in_scope (DECL_NAME (decl1), current_scope);
6124 if (old_decl && TREE_CODE (old_decl) != FUNCTION_DECL)
6125 old_decl = 0;
6126 current_function_prototype_locus = UNKNOWN_LOCATION;
6127 current_function_prototype_built_in = false;
6128 current_function_prototype_arg_types = NULL_TREE;
6129 if (TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0)
6131 if (old_decl != 0 && TREE_CODE (TREE_TYPE (old_decl)) == FUNCTION_TYPE
6132 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6133 TREE_TYPE (TREE_TYPE (old_decl))))
6135 TREE_TYPE (decl1) = composite_type (TREE_TYPE (old_decl),
6136 TREE_TYPE (decl1));
6137 current_function_prototype_locus = DECL_SOURCE_LOCATION (old_decl);
6138 current_function_prototype_built_in
6139 = C_DECL_BUILTIN_PROTOTYPE (old_decl);
6140 current_function_prototype_arg_types
6141 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
6143 if (TREE_PUBLIC (decl1))
6145 /* If there is an external prototype declaration of this
6146 function, record its location but do not copy information
6147 to this decl. This may be an invisible declaration
6148 (built-in or in a scope which has finished) or simply
6149 have more refined argument types than any declaration
6150 found above. */
6151 struct c_binding *b;
6152 for (b = I_SYMBOL_BINDING (DECL_NAME (decl1)); b; b = b->shadowed)
6153 if (B_IN_SCOPE (b, external_scope))
6154 break;
6155 if (b)
6157 tree ext_decl, ext_type;
6158 ext_decl = b->decl;
6159 ext_type = b->type ? b->type : TREE_TYPE (ext_decl);
6160 if (TREE_CODE (ext_type) == FUNCTION_TYPE
6161 && comptypes (TREE_TYPE (TREE_TYPE (decl1)),
6162 TREE_TYPE (ext_type)))
6164 current_function_prototype_locus
6165 = DECL_SOURCE_LOCATION (ext_decl);
6166 current_function_prototype_built_in
6167 = C_DECL_BUILTIN_PROTOTYPE (ext_decl);
6168 current_function_prototype_arg_types
6169 = TYPE_ARG_TYPES (ext_type);
6175 /* Optionally warn of old-fashioned def with no previous prototype. */
6176 if (warn_strict_prototypes
6177 && old_decl != error_mark_node
6178 && TYPE_ARG_TYPES (TREE_TYPE (decl1)) == 0
6179 && C_DECL_ISNT_PROTOTYPE (old_decl))
6180 warning (OPT_Wstrict_prototypes,
6181 "function declaration isn%'t a prototype");
6182 /* Optionally warn of any global def with no previous prototype. */
6183 else if (warn_missing_prototypes
6184 && old_decl != error_mark_node
6185 && TREE_PUBLIC (decl1)
6186 && !MAIN_NAME_P (DECL_NAME (decl1))
6187 && C_DECL_ISNT_PROTOTYPE (old_decl))
6188 warning (OPT_Wmissing_prototypes, "no previous prototype for %q+D", decl1);
6189 /* Optionally warn of any def with no previous prototype
6190 if the function has already been used. */
6191 else if (warn_missing_prototypes
6192 && old_decl != 0
6193 && old_decl != error_mark_node
6194 && TREE_USED (old_decl)
6195 && TYPE_ARG_TYPES (TREE_TYPE (old_decl)) == 0)
6196 warning (OPT_Wmissing_prototypes,
6197 "%q+D was used with no prototype before its definition", decl1);
6198 /* Optionally warn of any global def with no previous declaration. */
6199 else if (warn_missing_declarations
6200 && TREE_PUBLIC (decl1)
6201 && old_decl == 0
6202 && !MAIN_NAME_P (DECL_NAME (decl1)))
6203 warning (OPT_Wmissing_declarations, "no previous declaration for %q+D",
6204 decl1);
6205 /* Optionally warn of any def with no previous declaration
6206 if the function has already been used. */
6207 else if (warn_missing_declarations
6208 && old_decl != 0
6209 && old_decl != error_mark_node
6210 && TREE_USED (old_decl)
6211 && C_DECL_IMPLICIT (old_decl))
6212 warning (OPT_Wmissing_declarations,
6213 "%q+D was used with no declaration before its definition", decl1);
6215 /* This function exists in static storage.
6216 (This does not mean `static' in the C sense!) */
6217 TREE_STATIC (decl1) = 1;
6219 /* A nested function is not global. */
6220 if (current_function_decl != 0)
6221 TREE_PUBLIC (decl1) = 0;
6223 /* This is the earliest point at which we might know the assembler
6224 name of the function. Thus, if it's set before this, die horribly. */
6225 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl1));
6227 /* If #pragma weak was used, mark the decl weak now. */
6228 if (current_scope == file_scope)
6229 maybe_apply_pragma_weak (decl1);
6231 /* Warn for unlikely, improbable, or stupid declarations of `main'. */
6232 if (warn_main > 0 && MAIN_NAME_P (DECL_NAME (decl1)))
6234 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl1)))
6235 != integer_type_node)
6236 pedwarn ("return type of %q+D is not %<int%>", decl1);
6238 check_main_parameter_types(decl1);
6240 if (!TREE_PUBLIC (decl1))
6241 pedwarn ("%q+D is normally a non-static function", decl1);
6244 /* Record the decl so that the function name is defined.
6245 If we already have a decl for this name, and it is a FUNCTION_DECL,
6246 use the old decl. */
6248 current_function_decl = pushdecl (decl1);
6250 push_scope ();
6251 declare_parm_level ();
6253 restype = TREE_TYPE (TREE_TYPE (current_function_decl));
6254 resdecl = build_decl (RESULT_DECL, NULL_TREE, restype);
6255 DECL_ARTIFICIAL (resdecl) = 1;
6256 DECL_IGNORED_P (resdecl) = 1;
6257 DECL_RESULT (current_function_decl) = resdecl;
6259 start_fname_decls ();
6261 return 1;
6264 /* Subroutine of store_parm_decls which handles new-style function
6265 definitions (prototype format). The parms already have decls, so we
6266 need only record them as in effect and complain if any redundant
6267 old-style parm decls were written. */
6268 static void
6269 store_parm_decls_newstyle (tree fndecl, const struct c_arg_info *arg_info)
6271 tree decl;
6273 if (current_scope->bindings)
6275 error ("%Jold-style parameter declarations in prototyped "
6276 "function definition", fndecl);
6278 /* Get rid of the old-style declarations. */
6279 pop_scope ();
6280 push_scope ();
6282 /* Don't issue this warning for nested functions, and don't issue this
6283 warning if we got here because ARG_INFO_TYPES was error_mark_node
6284 (this happens when a function definition has just an ellipsis in
6285 its parameter list). */
6286 else if (!in_system_header && !current_function_scope
6287 && arg_info->types != error_mark_node)
6288 warning (OPT_Wtraditional,
6289 "%Jtraditional C rejects ISO C style function definitions",
6290 fndecl);
6292 /* Now make all the parameter declarations visible in the function body.
6293 We can bypass most of the grunt work of pushdecl. */
6294 for (decl = arg_info->parms; decl; decl = TREE_CHAIN (decl))
6296 DECL_CONTEXT (decl) = current_function_decl;
6297 if (DECL_NAME (decl))
6299 bind (DECL_NAME (decl), decl, current_scope,
6300 /*invisible=*/false, /*nested=*/false);
6301 if (!TREE_USED (decl))
6302 warn_if_shadowing (decl);
6304 else
6305 error ("%Jparameter name omitted", decl);
6308 /* Record the parameter list in the function declaration. */
6309 DECL_ARGUMENTS (fndecl) = arg_info->parms;
6311 /* Now make all the ancillary declarations visible, likewise. */
6312 for (decl = arg_info->others; decl; decl = TREE_CHAIN (decl))
6314 DECL_CONTEXT (decl) = current_function_decl;
6315 if (DECL_NAME (decl))
6316 bind (DECL_NAME (decl), decl, current_scope,
6317 /*invisible=*/false, /*nested=*/false);
6320 /* And all the tag declarations. */
6321 for (decl = arg_info->tags; decl; decl = TREE_CHAIN (decl))
6322 if (TREE_PURPOSE (decl))
6323 bind (TREE_PURPOSE (decl), TREE_VALUE (decl), current_scope,
6324 /*invisible=*/false, /*nested=*/false);
6327 /* Subroutine of store_parm_decls which handles old-style function
6328 definitions (separate parameter list and declarations). */
6330 static void
6331 store_parm_decls_oldstyle (tree fndecl, const struct c_arg_info *arg_info)
6333 struct c_binding *b;
6334 tree parm, decl, last;
6335 tree parmids = arg_info->parms;
6336 struct pointer_set_t *seen_args = pointer_set_create ();
6338 if (!in_system_header)
6339 warning (OPT_Wold_style_definition, "%Jold-style function definition",
6340 fndecl);
6342 /* Match each formal parameter name with its declaration. Save each
6343 decl in the appropriate TREE_PURPOSE slot of the parmids chain. */
6344 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6346 if (TREE_VALUE (parm) == 0)
6348 error ("%Jparameter name missing from parameter list", fndecl);
6349 TREE_PURPOSE (parm) = 0;
6350 continue;
6353 b = I_SYMBOL_BINDING (TREE_VALUE (parm));
6354 if (b && B_IN_CURRENT_SCOPE (b))
6356 decl = b->decl;
6357 /* If we got something other than a PARM_DECL it is an error. */
6358 if (TREE_CODE (decl) != PARM_DECL)
6359 error ("%q+D declared as a non-parameter", decl);
6360 /* If the declaration is already marked, we have a duplicate
6361 name. Complain and ignore the duplicate. */
6362 else if (pointer_set_contains (seen_args, decl))
6364 error ("multiple parameters named %q+D", decl);
6365 TREE_PURPOSE (parm) = 0;
6366 continue;
6368 /* If the declaration says "void", complain and turn it into
6369 an int. */
6370 else if (VOID_TYPE_P (TREE_TYPE (decl)))
6372 error ("parameter %q+D declared with void type", decl);
6373 TREE_TYPE (decl) = integer_type_node;
6374 DECL_ARG_TYPE (decl) = integer_type_node;
6375 layout_decl (decl, 0);
6377 warn_if_shadowing (decl);
6379 /* If no declaration found, default to int. */
6380 else
6382 decl = build_decl (PARM_DECL, TREE_VALUE (parm), integer_type_node);
6383 DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
6384 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (fndecl);
6385 pushdecl (decl);
6386 warn_if_shadowing (decl);
6388 if (flag_isoc99)
6389 pedwarn ("type of %q+D defaults to %<int%>", decl);
6390 else
6391 warning (OPT_Wmissing_parameter_type, "type of %q+D defaults to %<int%>", decl);
6394 TREE_PURPOSE (parm) = decl;
6395 pointer_set_insert (seen_args, decl);
6398 /* Now examine the parms chain for incomplete declarations
6399 and declarations with no corresponding names. */
6401 for (b = current_scope->bindings; b; b = b->prev)
6403 parm = b->decl;
6404 if (TREE_CODE (parm) != PARM_DECL)
6405 continue;
6407 if (TREE_TYPE (parm) != error_mark_node
6408 && !COMPLETE_TYPE_P (TREE_TYPE (parm)))
6410 error ("parameter %q+D has incomplete type", parm);
6411 TREE_TYPE (parm) = error_mark_node;
6414 if (!pointer_set_contains (seen_args, parm))
6416 error ("declaration for parameter %q+D but no such parameter", parm);
6418 /* Pretend the parameter was not missing.
6419 This gets us to a standard state and minimizes
6420 further error messages. */
6421 parmids = chainon (parmids, tree_cons (parm, 0, 0));
6425 /* Chain the declarations together in the order of the list of
6426 names. Store that chain in the function decl, replacing the
6427 list of names. Update the current scope to match. */
6428 DECL_ARGUMENTS (fndecl) = 0;
6430 for (parm = parmids; parm; parm = TREE_CHAIN (parm))
6431 if (TREE_PURPOSE (parm))
6432 break;
6433 if (parm && TREE_PURPOSE (parm))
6435 last = TREE_PURPOSE (parm);
6436 DECL_ARGUMENTS (fndecl) = last;
6438 for (parm = TREE_CHAIN (parm); parm; parm = TREE_CHAIN (parm))
6439 if (TREE_PURPOSE (parm))
6441 TREE_CHAIN (last) = TREE_PURPOSE (parm);
6442 last = TREE_PURPOSE (parm);
6444 TREE_CHAIN (last) = 0;
6447 pointer_set_destroy (seen_args);
6449 /* If there was a previous prototype,
6450 set the DECL_ARG_TYPE of each argument according to
6451 the type previously specified, and report any mismatches. */
6453 if (current_function_prototype_arg_types)
6455 tree type;
6456 for (parm = DECL_ARGUMENTS (fndecl),
6457 type = current_function_prototype_arg_types;
6458 parm || (type && TREE_VALUE (type) != error_mark_node
6459 && (TYPE_MAIN_VARIANT (TREE_VALUE (type)) != void_type_node));
6460 parm = TREE_CHAIN (parm), type = TREE_CHAIN (type))
6462 if (parm == 0 || type == 0
6463 || TYPE_MAIN_VARIANT (TREE_VALUE (type)) == void_type_node)
6465 if (current_function_prototype_built_in)
6466 warning (0, "number of arguments doesn%'t match "
6467 "built-in prototype");
6468 else
6470 error ("number of arguments doesn%'t match prototype");
6471 error ("%Hprototype declaration",
6472 &current_function_prototype_locus);
6474 break;
6476 /* Type for passing arg must be consistent with that
6477 declared for the arg. ISO C says we take the unqualified
6478 type for parameters declared with qualified type. */
6479 if (TREE_TYPE (parm) != error_mark_node
6480 && TREE_TYPE (type) != error_mark_node
6481 && !comptypes (TYPE_MAIN_VARIANT (DECL_ARG_TYPE (parm)),
6482 TYPE_MAIN_VARIANT (TREE_VALUE (type))))
6484 if (TYPE_MAIN_VARIANT (TREE_TYPE (parm))
6485 == TYPE_MAIN_VARIANT (TREE_VALUE (type)))
6487 /* Adjust argument to match prototype. E.g. a previous
6488 `int foo(float);' prototype causes
6489 `int foo(x) float x; {...}' to be treated like
6490 `int foo(float x) {...}'. This is particularly
6491 useful for argument types like uid_t. */
6492 DECL_ARG_TYPE (parm) = TREE_TYPE (parm);
6494 if (targetm.calls.promote_prototypes (TREE_TYPE (current_function_decl))
6495 && INTEGRAL_TYPE_P (TREE_TYPE (parm))
6496 && TYPE_PRECISION (TREE_TYPE (parm))
6497 < TYPE_PRECISION (integer_type_node))
6498 DECL_ARG_TYPE (parm) = integer_type_node;
6500 if (pedantic)
6502 /* ??? Is it possible to get here with a
6503 built-in prototype or will it always have
6504 been diagnosed as conflicting with an
6505 old-style definition and discarded? */
6506 if (current_function_prototype_built_in)
6507 warning (0, "promoted argument %qD "
6508 "doesn%'t match built-in prototype", parm);
6509 else
6511 pedwarn ("promoted argument %qD "
6512 "doesn%'t match prototype", parm);
6513 pedwarn ("%Hprototype declaration",
6514 &current_function_prototype_locus);
6518 else
6520 if (current_function_prototype_built_in)
6521 warning (0, "argument %qD doesn%'t match "
6522 "built-in prototype", parm);
6523 else
6525 error ("argument %qD doesn%'t match prototype", parm);
6526 error ("%Hprototype declaration",
6527 &current_function_prototype_locus);
6532 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = 0;
6535 /* Otherwise, create a prototype that would match. */
6537 else
6539 tree actual = 0, last = 0, type;
6541 for (parm = DECL_ARGUMENTS (fndecl); parm; parm = TREE_CHAIN (parm))
6543 type = tree_cons (NULL_TREE, DECL_ARG_TYPE (parm), NULL_TREE);
6544 if (last)
6545 TREE_CHAIN (last) = type;
6546 else
6547 actual = type;
6548 last = type;
6550 type = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
6551 if (last)
6552 TREE_CHAIN (last) = type;
6553 else
6554 actual = type;
6556 /* We are going to assign a new value for the TYPE_ACTUAL_ARG_TYPES
6557 of the type of this function, but we need to avoid having this
6558 affect the types of other similarly-typed functions, so we must
6559 first force the generation of an identical (but separate) type
6560 node for the relevant function type. The new node we create
6561 will be a variant of the main variant of the original function
6562 type. */
6564 TREE_TYPE (fndecl) = build_variant_type_copy (TREE_TYPE (fndecl));
6566 TYPE_ACTUAL_ARG_TYPES (TREE_TYPE (fndecl)) = actual;
6570 /* Store parameter declarations passed in ARG_INFO into the current
6571 function declaration. */
6573 void
6574 store_parm_decls_from (struct c_arg_info *arg_info)
6576 current_function_arg_info = arg_info;
6577 store_parm_decls ();
6580 /* Store the parameter declarations into the current function declaration.
6581 This is called after parsing the parameter declarations, before
6582 digesting the body of the function.
6584 For an old-style definition, construct a prototype out of the old-style
6585 parameter declarations and inject it into the function's type. */
6587 void
6588 store_parm_decls (void)
6590 tree fndecl = current_function_decl;
6591 bool proto;
6593 /* The argument information block for FNDECL. */
6594 struct c_arg_info *arg_info = current_function_arg_info;
6595 current_function_arg_info = 0;
6597 /* True if this definition is written with a prototype. Note:
6598 despite C99 6.7.5.3p14, we can *not* treat an empty argument
6599 list in a function definition as equivalent to (void) -- an
6600 empty argument list specifies the function has no parameters,
6601 but only (void) sets up a prototype for future calls. */
6602 proto = arg_info->types != 0;
6604 if (proto)
6605 store_parm_decls_newstyle (fndecl, arg_info);
6606 else
6607 store_parm_decls_oldstyle (fndecl, arg_info);
6609 /* The next call to push_scope will be a function body. */
6611 next_is_function_body = true;
6613 /* Write a record describing this function definition to the prototypes
6614 file (if requested). */
6616 gen_aux_info_record (fndecl, 1, 0, proto);
6618 /* Initialize the RTL code for the function. */
6619 allocate_struct_function (fndecl, false);
6621 /* Begin the statement tree for this function. */
6622 DECL_SAVED_TREE (fndecl) = push_stmt_list ();
6624 /* ??? Insert the contents of the pending sizes list into the function
6625 to be evaluated. The only reason left to have this is
6626 void foo(int n, int array[n++])
6627 because we throw away the array type in favor of a pointer type, and
6628 thus won't naturally see the SAVE_EXPR containing the increment. All
6629 other pending sizes would be handled by gimplify_parameters. */
6631 tree t;
6632 for (t = nreverse (get_pending_sizes ()); t ; t = TREE_CHAIN (t))
6633 add_stmt (TREE_VALUE (t));
6636 /* Even though we're inside a function body, we still don't want to
6637 call expand_expr to calculate the size of a variable-sized array.
6638 We haven't necessarily assigned RTL to all variables yet, so it's
6639 not safe to try to expand expressions involving them. */
6640 cfun->dont_save_pending_sizes_p = 1;
6643 /* Emit diagnostics that require gimple input for detection. Operate on
6644 FNDECL and all its nested functions. */
6646 static void
6647 c_gimple_diagnostics_recursively (tree fndecl)
6649 struct cgraph_node *cgn;
6651 /* Handle attribute((warn_unused_result)). Relies on gimple input. */
6652 c_warn_unused_result (&DECL_SAVED_TREE (fndecl));
6654 /* Notice when OpenMP structured block constraints are violated. */
6655 if (flag_openmp)
6656 diagnose_omp_structured_block_errors (fndecl);
6658 /* Finalize all nested functions now. */
6659 cgn = cgraph_node (fndecl);
6660 for (cgn = cgn->nested; cgn ; cgn = cgn->next_nested)
6661 c_gimple_diagnostics_recursively (cgn->decl);
6664 /* Finish up a function declaration and compile that function
6665 all the way to assembler language output. The free the storage
6666 for the function definition.
6668 This is called after parsing the body of the function definition. */
6670 void
6671 finish_function (void)
6673 tree fndecl = current_function_decl;
6675 label_context_stack_se = label_context_stack_se->next;
6676 label_context_stack_vm = label_context_stack_vm->next;
6678 if (TREE_CODE (fndecl) == FUNCTION_DECL
6679 && targetm.calls.promote_prototypes (TREE_TYPE (fndecl)))
6681 tree args = DECL_ARGUMENTS (fndecl);
6682 for (; args; args = TREE_CHAIN (args))
6684 tree type = TREE_TYPE (args);
6685 if (INTEGRAL_TYPE_P (type)
6686 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6687 DECL_ARG_TYPE (args) = integer_type_node;
6691 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node)
6692 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
6694 /* Must mark the RESULT_DECL as being in this function. */
6696 if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node)
6697 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
6699 if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted)
6701 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
6702 != integer_type_node)
6704 /* If warn_main is 1 (-Wmain) or 2 (-Wall), we have already warned.
6705 If warn_main is -1 (-Wno-main) we don't want to be warned. */
6706 if (!warn_main)
6707 pedwarn ("return type of %q+D is not %<int%>", fndecl);
6709 else
6711 if (flag_isoc99)
6713 tree stmt = c_finish_return (integer_zero_node);
6714 /* Hack. We don't want the middle-end to warn that this return
6715 is unreachable, so we mark its location as special. Using
6716 UNKNOWN_LOCATION has the problem that it gets clobbered in
6717 annotate_one_with_locus. A cleaner solution might be to
6718 ensure ! should_carry_locus_p (stmt), but that needs a flag.
6720 SET_EXPR_LOCATION (stmt, BUILTINS_LOCATION);
6725 /* Tie off the statement tree for this function. */
6726 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl));
6728 finish_fname_decls ();
6730 /* Complain if there's just no return statement. */
6731 if (warn_return_type
6732 && TREE_CODE (TREE_TYPE (TREE_TYPE (fndecl))) != VOID_TYPE
6733 && !current_function_returns_value && !current_function_returns_null
6734 /* Don't complain if we are no-return. */
6735 && !current_function_returns_abnormally
6736 /* Don't warn for main(). */
6737 && !MAIN_NAME_P (DECL_NAME (fndecl))
6738 /* Or if they didn't actually specify a return type. */
6739 && !C_FUNCTION_IMPLICIT_INT (fndecl)
6740 /* Normally, with -Wreturn-type, flow will complain. Unless we're an
6741 inline function, as we might never be compiled separately. */
6742 && DECL_INLINE (fndecl))
6744 warning (OPT_Wreturn_type,
6745 "no return statement in function returning non-void");
6746 TREE_NO_WARNING (fndecl) = 1;
6749 /* Store the end of the function, so that we get good line number
6750 info for the epilogue. */
6751 cfun->function_end_locus = input_location;
6753 /* Finalize the ELF visibility for the function. */
6754 c_determine_visibility (fndecl);
6756 /* For GNU C extern inline functions disregard inline limits. */
6757 if (DECL_EXTERNAL (fndecl)
6758 && DECL_DECLARED_INLINE_P (fndecl))
6759 DECL_DISREGARD_INLINE_LIMITS (fndecl) = 1;
6761 /* Genericize before inlining. Delay genericizing nested functions
6762 until their parent function is genericized. Since finalizing
6763 requires GENERIC, delay that as well. */
6765 if (DECL_INITIAL (fndecl) && DECL_INITIAL (fndecl) != error_mark_node
6766 && !undef_nested_function)
6768 if (!decl_function_context (fndecl))
6770 c_genericize (fndecl);
6771 c_gimple_diagnostics_recursively (fndecl);
6773 /* ??? Objc emits functions after finalizing the compilation unit.
6774 This should be cleaned up later and this conditional removed. */
6775 if (cgraph_global_info_ready)
6777 cgraph_add_new_function (fndecl, false);
6778 return;
6781 cgraph_finalize_function (fndecl, false);
6783 else
6785 /* Register this function with cgraph just far enough to get it
6786 added to our parent's nested function list. Handy, since the
6787 C front end doesn't have such a list. */
6788 (void) cgraph_node (fndecl);
6792 if (!decl_function_context (fndecl))
6793 undef_nested_function = false;
6795 /* We're leaving the context of this function, so zap cfun.
6796 It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
6797 tree_rest_of_compilation. */
6798 set_cfun (NULL);
6799 current_function_decl = NULL;
6802 /* Check the declarations given in a for-loop for satisfying the C99
6803 constraints. If exactly one such decl is found, return it. */
6805 tree
6806 check_for_loop_decls (void)
6808 struct c_binding *b;
6809 tree one_decl = NULL_TREE;
6810 int n_decls = 0;
6813 if (!flag_isoc99)
6815 /* If we get here, declarations have been used in a for loop without
6816 the C99 for loop scope. This doesn't make much sense, so don't
6817 allow it. */
6818 error ("%<for%> loop initial declaration used outside C99 mode");
6819 return NULL_TREE;
6821 /* C99 subclause 6.8.5 paragraph 3:
6823 [#3] The declaration part of a for statement shall only
6824 declare identifiers for objects having storage class auto or
6825 register.
6827 It isn't clear whether, in this sentence, "identifiers" binds to
6828 "shall only declare" or to "objects" - that is, whether all identifiers
6829 declared must be identifiers for objects, or whether the restriction
6830 only applies to those that are. (A question on this in comp.std.c
6831 in November 2000 received no answer.) We implement the strictest
6832 interpretation, to avoid creating an extension which later causes
6833 problems. */
6835 for (b = current_scope->bindings; b; b = b->prev)
6837 tree id = b->id;
6838 tree decl = b->decl;
6840 if (!id)
6841 continue;
6843 switch (TREE_CODE (decl))
6845 case VAR_DECL:
6846 if (TREE_STATIC (decl))
6847 error ("declaration of static variable %q+D in %<for%> loop "
6848 "initial declaration", decl);
6849 else if (DECL_EXTERNAL (decl))
6850 error ("declaration of %<extern%> variable %q+D in %<for%> loop "
6851 "initial declaration", decl);
6852 break;
6854 case RECORD_TYPE:
6855 error ("%<struct %E%> declared in %<for%> loop initial declaration",
6856 id);
6857 break;
6858 case UNION_TYPE:
6859 error ("%<union %E%> declared in %<for%> loop initial declaration",
6860 id);
6861 break;
6862 case ENUMERAL_TYPE:
6863 error ("%<enum %E%> declared in %<for%> loop initial declaration",
6864 id);
6865 break;
6866 default:
6867 error ("declaration of non-variable %q+D in %<for%> loop "
6868 "initial declaration", decl);
6871 n_decls++;
6872 one_decl = decl;
6875 return n_decls == 1 ? one_decl : NULL_TREE;
6878 /* Save and reinitialize the variables
6879 used during compilation of a C function. */
6881 void
6882 c_push_function_context (void)
6884 struct language_function *p;
6885 p = GGC_NEW (struct language_function);
6886 cfun->language = p;
6888 p->base.x_stmt_tree = c_stmt_tree;
6889 p->x_break_label = c_break_label;
6890 p->x_cont_label = c_cont_label;
6891 p->x_switch_stack = c_switch_stack;
6892 p->arg_info = current_function_arg_info;
6893 p->returns_value = current_function_returns_value;
6894 p->returns_null = current_function_returns_null;
6895 p->returns_abnormally = current_function_returns_abnormally;
6896 p->warn_about_return_type = warn_about_return_type;
6898 push_function_context ();
6901 /* Restore the variables used during compilation of a C function. */
6903 void
6904 c_pop_function_context (void)
6906 struct language_function *p;
6908 pop_function_context ();
6909 p = cfun->language;
6910 cfun->language = NULL;
6912 if (DECL_STRUCT_FUNCTION (current_function_decl) == 0
6913 && DECL_SAVED_TREE (current_function_decl) == NULL_TREE)
6915 /* Stop pointing to the local nodes about to be freed. */
6916 /* But DECL_INITIAL must remain nonzero so we know this
6917 was an actual function definition. */
6918 DECL_INITIAL (current_function_decl) = error_mark_node;
6919 DECL_ARGUMENTS (current_function_decl) = 0;
6922 c_stmt_tree = p->base.x_stmt_tree;
6923 c_break_label = p->x_break_label;
6924 c_cont_label = p->x_cont_label;
6925 c_switch_stack = p->x_switch_stack;
6926 current_function_arg_info = p->arg_info;
6927 current_function_returns_value = p->returns_value;
6928 current_function_returns_null = p->returns_null;
6929 current_function_returns_abnormally = p->returns_abnormally;
6930 warn_about_return_type = p->warn_about_return_type;
6933 /* Copy the DECL_LANG_SPECIFIC data associated with DECL. */
6935 void
6936 c_dup_lang_specific_decl (tree decl)
6938 struct lang_decl *ld;
6940 if (!DECL_LANG_SPECIFIC (decl))
6941 return;
6943 ld = GGC_NEW (struct lang_decl);
6944 memcpy (ld, DECL_LANG_SPECIFIC (decl), sizeof (struct lang_decl));
6945 DECL_LANG_SPECIFIC (decl) = ld;
6948 /* The functions below are required for functionality of doing
6949 function at once processing in the C front end. Currently these
6950 functions are not called from anywhere in the C front end, but as
6951 these changes continue, that will change. */
6953 /* Returns the stmt_tree (if any) to which statements are currently
6954 being added. If there is no active statement-tree, NULL is
6955 returned. */
6957 stmt_tree
6958 current_stmt_tree (void)
6960 return &c_stmt_tree;
6963 /* Nonzero if TYPE is an anonymous union or struct type. Always 0 in
6964 C. */
6967 anon_aggr_type_p (const_tree ARG_UNUSED (node))
6969 return 0;
6972 /* Return the global value of T as a symbol. */
6974 tree
6975 identifier_global_value (tree t)
6977 struct c_binding *b;
6979 for (b = I_SYMBOL_BINDING (t); b; b = b->shadowed)
6980 if (B_IN_FILE_SCOPE (b) || B_IN_EXTERNAL_SCOPE (b))
6981 return b->decl;
6983 return 0;
6986 /* Record a builtin type for C. If NAME is non-NULL, it is the name used;
6987 otherwise the name is found in ridpointers from RID_INDEX. */
6989 void
6990 record_builtin_type (enum rid rid_index, const char *name, tree type)
6992 tree id, decl;
6993 if (name == 0)
6994 id = ridpointers[(int) rid_index];
6995 else
6996 id = get_identifier (name);
6997 decl = build_decl (TYPE_DECL, id, type);
6998 pushdecl (decl);
6999 if (debug_hooks->type_decl)
7000 debug_hooks->type_decl (decl, false);
7003 /* Build the void_list_node (void_type_node having been created). */
7004 tree
7005 build_void_list_node (void)
7007 tree t = build_tree_list (NULL_TREE, void_type_node);
7008 return t;
7011 /* Return a c_parm structure with the given SPECS, ATTRS and DECLARATOR. */
7013 struct c_parm *
7014 build_c_parm (struct c_declspecs *specs, tree attrs,
7015 struct c_declarator *declarator)
7017 struct c_parm *ret = XOBNEW (&parser_obstack, struct c_parm);
7018 ret->specs = specs;
7019 ret->attrs = attrs;
7020 ret->declarator = declarator;
7021 return ret;
7024 /* Return a declarator with nested attributes. TARGET is the inner
7025 declarator to which these attributes apply. ATTRS are the
7026 attributes. */
7028 struct c_declarator *
7029 build_attrs_declarator (tree attrs, struct c_declarator *target)
7031 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7032 ret->kind = cdk_attrs;
7033 ret->declarator = target;
7034 ret->u.attrs = attrs;
7035 return ret;
7038 /* Return a declarator for a function with arguments specified by ARGS
7039 and return type specified by TARGET. */
7041 struct c_declarator *
7042 build_function_declarator (struct c_arg_info *args,
7043 struct c_declarator *target)
7045 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7046 ret->kind = cdk_function;
7047 ret->declarator = target;
7048 ret->u.arg_info = args;
7049 return ret;
7052 /* Return a declarator for the identifier IDENT (which may be
7053 NULL_TREE for an abstract declarator). */
7055 struct c_declarator *
7056 build_id_declarator (tree ident)
7058 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7059 ret->kind = cdk_id;
7060 ret->declarator = 0;
7061 ret->u.id = ident;
7062 /* Default value - may get reset to a more precise location. */
7063 ret->id_loc = input_location;
7064 return ret;
7067 /* Return something to represent absolute declarators containing a *.
7068 TARGET is the absolute declarator that the * contains.
7069 TYPE_QUALS_ATTRS is a structure for type qualifiers and attributes
7070 to apply to the pointer type. */
7072 struct c_declarator *
7073 make_pointer_declarator (struct c_declspecs *type_quals_attrs,
7074 struct c_declarator *target)
7076 tree attrs;
7077 int quals = 0;
7078 struct c_declarator *itarget = target;
7079 struct c_declarator *ret = XOBNEW (&parser_obstack, struct c_declarator);
7080 if (type_quals_attrs)
7082 attrs = type_quals_attrs->attrs;
7083 quals = quals_from_declspecs (type_quals_attrs);
7084 if (attrs != NULL_TREE)
7085 itarget = build_attrs_declarator (attrs, target);
7087 ret->kind = cdk_pointer;
7088 ret->declarator = itarget;
7089 ret->u.pointer_quals = quals;
7090 return ret;
7093 /* Return a pointer to a structure for an empty list of declaration
7094 specifiers. */
7096 struct c_declspecs *
7097 build_null_declspecs (void)
7099 struct c_declspecs *ret = XOBNEW (&parser_obstack, struct c_declspecs);
7100 ret->type = 0;
7101 ret->decl_attr = 0;
7102 ret->attrs = 0;
7103 ret->typespec_word = cts_none;
7104 ret->storage_class = csc_none;
7105 ret->declspecs_seen_p = false;
7106 ret->type_seen_p = false;
7107 ret->non_sc_seen_p = false;
7108 ret->typedef_p = false;
7109 ret->tag_defined_p = false;
7110 ret->explicit_signed_p = false;
7111 ret->deprecated_p = false;
7112 ret->default_int_p = false;
7113 ret->long_p = false;
7114 ret->long_long_p = false;
7115 ret->short_p = false;
7116 ret->signed_p = false;
7117 ret->unsigned_p = false;
7118 ret->complex_p = false;
7119 ret->inline_p = false;
7120 ret->thread_p = false;
7121 ret->const_p = false;
7122 ret->volatile_p = false;
7123 ret->restrict_p = false;
7124 ret->saturating_p = false;
7125 return ret;
7128 /* Add the type qualifier QUAL to the declaration specifiers SPECS,
7129 returning SPECS. */
7131 struct c_declspecs *
7132 declspecs_add_qual (struct c_declspecs *specs, tree qual)
7134 enum rid i;
7135 bool dupe = false;
7136 specs->non_sc_seen_p = true;
7137 specs->declspecs_seen_p = true;
7138 gcc_assert (TREE_CODE (qual) == IDENTIFIER_NODE
7139 && C_IS_RESERVED_WORD (qual));
7140 i = C_RID_CODE (qual);
7141 switch (i)
7143 case RID_CONST:
7144 dupe = specs->const_p;
7145 specs->const_p = true;
7146 break;
7147 case RID_VOLATILE:
7148 dupe = specs->volatile_p;
7149 specs->volatile_p = true;
7150 break;
7151 case RID_RESTRICT:
7152 dupe = specs->restrict_p;
7153 specs->restrict_p = true;
7154 break;
7155 default:
7156 gcc_unreachable ();
7158 if (dupe && pedantic && !flag_isoc99)
7159 pedwarn ("duplicate %qE", qual);
7160 return specs;
7163 /* Add the type specifier TYPE to the declaration specifiers SPECS,
7164 returning SPECS. */
7166 struct c_declspecs *
7167 declspecs_add_type (struct c_declspecs *specs, struct c_typespec spec)
7169 tree type = spec.spec;
7170 specs->non_sc_seen_p = true;
7171 specs->declspecs_seen_p = true;
7172 specs->type_seen_p = true;
7173 if (TREE_DEPRECATED (type))
7174 specs->deprecated_p = true;
7176 /* Handle type specifier keywords. */
7177 if (TREE_CODE (type) == IDENTIFIER_NODE && C_IS_RESERVED_WORD (type))
7179 enum rid i = C_RID_CODE (type);
7180 if (specs->type)
7182 error ("two or more data types in declaration specifiers");
7183 return specs;
7185 if ((int) i <= (int) RID_LAST_MODIFIER)
7187 /* "long", "short", "signed", "unsigned", "_Complex" or "_Sat". */
7188 bool dupe = false;
7189 switch (i)
7191 case RID_LONG:
7192 if (specs->long_long_p)
7194 error ("%<long long long%> is too long for GCC");
7195 break;
7197 if (specs->long_p)
7199 if (specs->typespec_word == cts_double)
7201 error ("both %<long long%> and %<double%> in "
7202 "declaration specifiers");
7203 break;
7205 if (pedantic && !flag_isoc99 && !in_system_header
7206 && warn_long_long)
7207 pedwarn ("ISO C90 does not support %<long long%>");
7208 specs->long_long_p = 1;
7209 break;
7211 if (specs->short_p)
7212 error ("both %<long%> and %<short%> in "
7213 "declaration specifiers");
7214 else if (specs->typespec_word == cts_void)
7215 error ("both %<long%> and %<void%> in "
7216 "declaration specifiers");
7217 else if (specs->typespec_word == cts_bool)
7218 error ("both %<long%> and %<_Bool%> in "
7219 "declaration specifiers");
7220 else if (specs->typespec_word == cts_char)
7221 error ("both %<long%> and %<char%> in "
7222 "declaration specifiers");
7223 else if (specs->typespec_word == cts_float)
7224 error ("both %<long%> and %<float%> in "
7225 "declaration specifiers");
7226 else if (specs->typespec_word == cts_dfloat32)
7227 error ("both %<long%> and %<_Decimal32%> in "
7228 "declaration specifiers");
7229 else if (specs->typespec_word == cts_dfloat64)
7230 error ("both %<long%> and %<_Decimal64%> in "
7231 "declaration specifiers");
7232 else if (specs->typespec_word == cts_dfloat128)
7233 error ("both %<long%> and %<_Decimal128%> in "
7234 "declaration specifiers");
7235 else
7236 specs->long_p = true;
7237 break;
7238 case RID_SHORT:
7239 dupe = specs->short_p;
7240 if (specs->long_p)
7241 error ("both %<long%> and %<short%> in "
7242 "declaration specifiers");
7243 else if (specs->typespec_word == cts_void)
7244 error ("both %<short%> and %<void%> in "
7245 "declaration specifiers");
7246 else if (specs->typespec_word == cts_bool)
7247 error ("both %<short%> and %<_Bool%> in "
7248 "declaration specifiers");
7249 else if (specs->typespec_word == cts_char)
7250 error ("both %<short%> and %<char%> in "
7251 "declaration specifiers");
7252 else if (specs->typespec_word == cts_float)
7253 error ("both %<short%> and %<float%> in "
7254 "declaration specifiers");
7255 else if (specs->typespec_word == cts_double)
7256 error ("both %<short%> and %<double%> in "
7257 "declaration specifiers");
7258 else if (specs->typespec_word == cts_dfloat32)
7259 error ("both %<short%> and %<_Decimal32%> in "
7260 "declaration specifiers");
7261 else if (specs->typespec_word == cts_dfloat64)
7262 error ("both %<short%> and %<_Decimal64%> in "
7263 "declaration specifiers");
7264 else if (specs->typespec_word == cts_dfloat128)
7265 error ("both %<short%> and %<_Decimal128%> in "
7266 "declaration specifiers");
7267 else
7268 specs->short_p = true;
7269 break;
7270 case RID_SIGNED:
7271 dupe = specs->signed_p;
7272 if (specs->unsigned_p)
7273 error ("both %<signed%> and %<unsigned%> in "
7274 "declaration specifiers");
7275 else if (specs->typespec_word == cts_void)
7276 error ("both %<signed%> and %<void%> in "
7277 "declaration specifiers");
7278 else if (specs->typespec_word == cts_bool)
7279 error ("both %<signed%> and %<_Bool%> in "
7280 "declaration specifiers");
7281 else if (specs->typespec_word == cts_float)
7282 error ("both %<signed%> and %<float%> in "
7283 "declaration specifiers");
7284 else if (specs->typespec_word == cts_double)
7285 error ("both %<signed%> and %<double%> in "
7286 "declaration specifiers");
7287 else if (specs->typespec_word == cts_dfloat32)
7288 error ("both %<signed%> and %<_Decimal32%> in "
7289 "declaration specifiers");
7290 else if (specs->typespec_word == cts_dfloat64)
7291 error ("both %<signed%> and %<_Decimal64%> in "
7292 "declaration specifiers");
7293 else if (specs->typespec_word == cts_dfloat128)
7294 error ("both %<signed%> and %<_Decimal128%> in "
7295 "declaration specifiers");
7296 else
7297 specs->signed_p = true;
7298 break;
7299 case RID_UNSIGNED:
7300 dupe = specs->unsigned_p;
7301 if (specs->signed_p)
7302 error ("both %<signed%> and %<unsigned%> in "
7303 "declaration specifiers");
7304 else if (specs->typespec_word == cts_void)
7305 error ("both %<unsigned%> and %<void%> in "
7306 "declaration specifiers");
7307 else if (specs->typespec_word == cts_bool)
7308 error ("both %<unsigned%> and %<_Bool%> in "
7309 "declaration specifiers");
7310 else if (specs->typespec_word == cts_float)
7311 error ("both %<unsigned%> and %<float%> in "
7312 "declaration specifiers");
7313 else if (specs->typespec_word == cts_double)
7314 error ("both %<unsigned%> and %<double%> in "
7315 "declaration specifiers");
7316 else if (specs->typespec_word == cts_dfloat32)
7317 error ("both %<unsigned%> and %<_Decimal32%> in "
7318 "declaration specifiers");
7319 else if (specs->typespec_word == cts_dfloat64)
7320 error ("both %<unsigned%> and %<_Decimal64%> in "
7321 "declaration specifiers");
7322 else if (specs->typespec_word == cts_dfloat128)
7323 error ("both %<unsigned%> and %<_Decimal128%> in "
7324 "declaration specifiers");
7325 else
7326 specs->unsigned_p = true;
7327 break;
7328 case RID_COMPLEX:
7329 dupe = specs->complex_p;
7330 if (pedantic && !flag_isoc99 && !in_system_header)
7331 pedwarn ("ISO C90 does not support complex types");
7332 if (specs->typespec_word == cts_void)
7333 error ("both %<complex%> and %<void%> in "
7334 "declaration specifiers");
7335 else if (specs->typespec_word == cts_bool)
7336 error ("both %<complex%> and %<_Bool%> in "
7337 "declaration specifiers");
7338 else if (specs->typespec_word == cts_dfloat32)
7339 error ("both %<complex%> and %<_Decimal32%> in "
7340 "declaration specifiers");
7341 else if (specs->typespec_word == cts_dfloat64)
7342 error ("both %<complex%> and %<_Decimal64%> in "
7343 "declaration specifiers");
7344 else if (specs->typespec_word == cts_dfloat128)
7345 error ("both %<complex%> and %<_Decimal128%> in "
7346 "declaration specifiers");
7347 else if (specs->typespec_word == cts_fract)
7348 error ("both %<complex%> and %<_Fract%> in "
7349 "declaration specifiers");
7350 else if (specs->typespec_word == cts_accum)
7351 error ("both %<complex%> and %<_Accum%> in "
7352 "declaration specifiers");
7353 else if (specs->saturating_p)
7354 error ("both %<complex%> and %<_Sat%> in "
7355 "declaration specifiers");
7356 else
7357 specs->complex_p = true;
7358 break;
7359 case RID_SAT:
7360 dupe = specs->saturating_p;
7361 if (pedantic)
7362 pedwarn ("ISO C does not support saturating types");
7363 if (specs->typespec_word == cts_void)
7364 error ("both %<_Sat%> and %<void%> in "
7365 "declaration specifiers");
7366 else if (specs->typespec_word == cts_bool)
7367 error ("both %<_Sat%> and %<_Bool%> in "
7368 "declaration specifiers");
7369 else if (specs->typespec_word == cts_char)
7370 error ("both %<_Sat%> and %<char%> in "
7371 "declaration specifiers");
7372 else if (specs->typespec_word == cts_int)
7373 error ("both %<_Sat%> and %<int%> in "
7374 "declaration specifiers");
7375 else if (specs->typespec_word == cts_float)
7376 error ("both %<_Sat%> and %<float%> in "
7377 "declaration specifiers");
7378 else if (specs->typespec_word == cts_double)
7379 error ("both %<_Sat%> and %<double%> in "
7380 "declaration specifiers");
7381 else if (specs->typespec_word == cts_dfloat32)
7382 error ("both %<_Sat%> and %<_Decimal32%> in "
7383 "declaration specifiers");
7384 else if (specs->typespec_word == cts_dfloat64)
7385 error ("both %<_Sat%> and %<_Decimal64%> in "
7386 "declaration specifiers");
7387 else if (specs->typespec_word == cts_dfloat128)
7388 error ("both %<_Sat%> and %<_Decimal128%> in "
7389 "declaration specifiers");
7390 else if (specs->complex_p)
7391 error ("both %<_Sat%> and %<complex%> in "
7392 "declaration specifiers");
7393 else
7394 specs->saturating_p = true;
7395 break;
7396 default:
7397 gcc_unreachable ();
7400 if (dupe)
7401 error ("duplicate %qE", type);
7403 return specs;
7405 else
7407 /* "void", "_Bool", "char", "int", "float", "double", "_Decimal32",
7408 "_Decimal64", "_Decimal128", "_Fract" or "_Accum". */
7409 if (specs->typespec_word != cts_none)
7411 error ("two or more data types in declaration specifiers");
7412 return specs;
7414 switch (i)
7416 case RID_VOID:
7417 if (specs->long_p)
7418 error ("both %<long%> and %<void%> in "
7419 "declaration specifiers");
7420 else if (specs->short_p)
7421 error ("both %<short%> and %<void%> in "
7422 "declaration specifiers");
7423 else if (specs->signed_p)
7424 error ("both %<signed%> and %<void%> in "
7425 "declaration specifiers");
7426 else if (specs->unsigned_p)
7427 error ("both %<unsigned%> and %<void%> in "
7428 "declaration specifiers");
7429 else if (specs->complex_p)
7430 error ("both %<complex%> and %<void%> in "
7431 "declaration specifiers");
7432 else if (specs->saturating_p)
7433 error ("both %<_Sat%> and %<void%> in "
7434 "declaration specifiers");
7435 else
7436 specs->typespec_word = cts_void;
7437 return specs;
7438 case RID_BOOL:
7439 if (specs->long_p)
7440 error ("both %<long%> and %<_Bool%> in "
7441 "declaration specifiers");
7442 else if (specs->short_p)
7443 error ("both %<short%> and %<_Bool%> in "
7444 "declaration specifiers");
7445 else if (specs->signed_p)
7446 error ("both %<signed%> and %<_Bool%> in "
7447 "declaration specifiers");
7448 else if (specs->unsigned_p)
7449 error ("both %<unsigned%> and %<_Bool%> in "
7450 "declaration specifiers");
7451 else if (specs->complex_p)
7452 error ("both %<complex%> and %<_Bool%> in "
7453 "declaration specifiers");
7454 else if (specs->saturating_p)
7455 error ("both %<_Sat%> and %<_Bool%> in "
7456 "declaration specifiers");
7457 else
7458 specs->typespec_word = cts_bool;
7459 return specs;
7460 case RID_CHAR:
7461 if (specs->long_p)
7462 error ("both %<long%> and %<char%> in "
7463 "declaration specifiers");
7464 else if (specs->short_p)
7465 error ("both %<short%> and %<char%> in "
7466 "declaration specifiers");
7467 else if (specs->saturating_p)
7468 error ("both %<_Sat%> and %<char%> in "
7469 "declaration specifiers");
7470 else
7471 specs->typespec_word = cts_char;
7472 return specs;
7473 case RID_INT:
7474 if (specs->saturating_p)
7475 error ("both %<_Sat%> and %<int%> in "
7476 "declaration specifiers");
7477 else
7478 specs->typespec_word = cts_int;
7479 return specs;
7480 case RID_FLOAT:
7481 if (specs->long_p)
7482 error ("both %<long%> and %<float%> in "
7483 "declaration specifiers");
7484 else if (specs->short_p)
7485 error ("both %<short%> and %<float%> in "
7486 "declaration specifiers");
7487 else if (specs->signed_p)
7488 error ("both %<signed%> and %<float%> in "
7489 "declaration specifiers");
7490 else if (specs->unsigned_p)
7491 error ("both %<unsigned%> and %<float%> in "
7492 "declaration specifiers");
7493 else if (specs->saturating_p)
7494 error ("both %<_Sat%> and %<float%> in "
7495 "declaration specifiers");
7496 else
7497 specs->typespec_word = cts_float;
7498 return specs;
7499 case RID_DOUBLE:
7500 if (specs->long_long_p)
7501 error ("both %<long long%> and %<double%> in "
7502 "declaration specifiers");
7503 else if (specs->short_p)
7504 error ("both %<short%> and %<double%> in "
7505 "declaration specifiers");
7506 else if (specs->signed_p)
7507 error ("both %<signed%> and %<double%> in "
7508 "declaration specifiers");
7509 else if (specs->unsigned_p)
7510 error ("both %<unsigned%> and %<double%> in "
7511 "declaration specifiers");
7512 else if (specs->saturating_p)
7513 error ("both %<_Sat%> and %<double%> in "
7514 "declaration specifiers");
7515 else
7516 specs->typespec_word = cts_double;
7517 return specs;
7518 case RID_DFLOAT32:
7519 case RID_DFLOAT64:
7520 case RID_DFLOAT128:
7522 const char *str;
7523 if (i == RID_DFLOAT32)
7524 str = "_Decimal32";
7525 else if (i == RID_DFLOAT64)
7526 str = "_Decimal64";
7527 else
7528 str = "_Decimal128";
7529 if (specs->long_long_p)
7530 error ("both %<long long%> and %<%s%> in "
7531 "declaration specifiers", str);
7532 if (specs->long_p)
7533 error ("both %<long%> and %<%s%> in "
7534 "declaration specifiers", str);
7535 else if (specs->short_p)
7536 error ("both %<short%> and %<%s%> in "
7537 "declaration specifiers", str);
7538 else if (specs->signed_p)
7539 error ("both %<signed%> and %<%s%> in "
7540 "declaration specifiers", str);
7541 else if (specs->unsigned_p)
7542 error ("both %<unsigned%> and %<%s%> in "
7543 "declaration specifiers", str);
7544 else if (specs->complex_p)
7545 error ("both %<complex%> and %<%s%> in "
7546 "declaration specifiers", str);
7547 else if (specs->saturating_p)
7548 error ("both %<_Sat%> and %<%s%> in "
7549 "declaration specifiers", str);
7550 else if (i == RID_DFLOAT32)
7551 specs->typespec_word = cts_dfloat32;
7552 else if (i == RID_DFLOAT64)
7553 specs->typespec_word = cts_dfloat64;
7554 else
7555 specs->typespec_word = cts_dfloat128;
7557 if (!targetm.decimal_float_supported_p ())
7558 error ("decimal floating point not supported for this target");
7559 if (pedantic)
7560 pedwarn ("ISO C does not support decimal floating point");
7561 return specs;
7562 case RID_FRACT:
7563 case RID_ACCUM:
7565 const char *str;
7566 if (i == RID_FRACT)
7567 str = "_Fract";
7568 else
7569 str = "_Accum";
7570 if (specs->complex_p)
7571 error ("both %<complex%> and %<%s%> in "
7572 "declaration specifiers", str);
7573 else if (i == RID_FRACT)
7574 specs->typespec_word = cts_fract;
7575 else
7576 specs->typespec_word = cts_accum;
7578 if (!targetm.fixed_point_supported_p ())
7579 error ("fixed-point types not supported for this target");
7580 if (pedantic)
7581 pedwarn ("ISO C does not support fixed-point types");
7582 return specs;
7583 default:
7584 /* ObjC reserved word "id", handled below. */
7585 break;
7590 /* Now we have a typedef (a TYPE_DECL node), an identifier (some
7591 form of ObjC type, cases such as "int" and "long" being handled
7592 above), a TYPE (struct, union, enum and typeof specifiers) or an
7593 ERROR_MARK. In none of these cases may there have previously
7594 been any type specifiers. */
7595 if (specs->type || specs->typespec_word != cts_none
7596 || specs->long_p || specs->short_p || specs->signed_p
7597 || specs->unsigned_p || specs->complex_p)
7598 error ("two or more data types in declaration specifiers");
7599 else if (TREE_CODE (type) == TYPE_DECL)
7601 if (TREE_TYPE (type) == error_mark_node)
7602 ; /* Allow the type to default to int to avoid cascading errors. */
7603 else
7605 specs->type = TREE_TYPE (type);
7606 specs->decl_attr = DECL_ATTRIBUTES (type);
7607 specs->typedef_p = true;
7608 specs->explicit_signed_p = C_TYPEDEF_EXPLICITLY_SIGNED (type);
7611 else if (TREE_CODE (type) == IDENTIFIER_NODE)
7613 tree t = lookup_name (type);
7614 if (!t || TREE_CODE (t) != TYPE_DECL)
7615 error ("%qE fails to be a typedef or built in type", type);
7616 else if (TREE_TYPE (t) == error_mark_node)
7618 else
7619 specs->type = TREE_TYPE (t);
7621 else if (TREE_CODE (type) != ERROR_MARK)
7623 if (spec.kind == ctsk_tagdef || spec.kind == ctsk_tagfirstref)
7624 specs->tag_defined_p = true;
7625 if (spec.kind == ctsk_typeof)
7626 specs->typedef_p = true;
7627 specs->type = type;
7630 return specs;
7633 /* Add the storage class specifier or function specifier SCSPEC to the
7634 declaration specifiers SPECS, returning SPECS. */
7636 struct c_declspecs *
7637 declspecs_add_scspec (struct c_declspecs *specs, tree scspec)
7639 enum rid i;
7640 enum c_storage_class n = csc_none;
7641 bool dupe = false;
7642 specs->declspecs_seen_p = true;
7643 gcc_assert (TREE_CODE (scspec) == IDENTIFIER_NODE
7644 && C_IS_RESERVED_WORD (scspec));
7645 i = C_RID_CODE (scspec);
7646 if (specs->non_sc_seen_p)
7647 warning (OPT_Wold_style_declaration,
7648 "%qE is not at beginning of declaration", scspec);
7649 switch (i)
7651 case RID_INLINE:
7652 /* C99 permits duplicate inline. Although of doubtful utility,
7653 it seems simplest to permit it in gnu89 mode as well, as
7654 there is also little utility in maintaining this as a
7655 difference between gnu89 and C99 inline. */
7656 dupe = false;
7657 specs->inline_p = true;
7658 break;
7659 case RID_THREAD:
7660 dupe = specs->thread_p;
7661 if (specs->storage_class == csc_auto)
7662 error ("%<__thread%> used with %<auto%>");
7663 else if (specs->storage_class == csc_register)
7664 error ("%<__thread%> used with %<register%>");
7665 else if (specs->storage_class == csc_typedef)
7666 error ("%<__thread%> used with %<typedef%>");
7667 else
7668 specs->thread_p = true;
7669 break;
7670 case RID_AUTO:
7671 n = csc_auto;
7672 break;
7673 case RID_EXTERN:
7674 n = csc_extern;
7675 /* Diagnose "__thread extern". */
7676 if (specs->thread_p)
7677 error ("%<__thread%> before %<extern%>");
7678 break;
7679 case RID_REGISTER:
7680 n = csc_register;
7681 break;
7682 case RID_STATIC:
7683 n = csc_static;
7684 /* Diagnose "__thread static". */
7685 if (specs->thread_p)
7686 error ("%<__thread%> before %<static%>");
7687 break;
7688 case RID_TYPEDEF:
7689 n = csc_typedef;
7690 break;
7691 default:
7692 gcc_unreachable ();
7694 if (n != csc_none && n == specs->storage_class)
7695 dupe = true;
7696 if (dupe)
7697 error ("duplicate %qE", scspec);
7698 if (n != csc_none)
7700 if (specs->storage_class != csc_none && n != specs->storage_class)
7702 error ("multiple storage classes in declaration specifiers");
7704 else
7706 specs->storage_class = n;
7707 if (n != csc_extern && n != csc_static && specs->thread_p)
7709 error ("%<__thread%> used with %qE", scspec);
7710 specs->thread_p = false;
7714 return specs;
7717 /* Add the attributes ATTRS to the declaration specifiers SPECS,
7718 returning SPECS. */
7720 struct c_declspecs *
7721 declspecs_add_attrs (struct c_declspecs *specs, tree attrs)
7723 specs->attrs = chainon (attrs, specs->attrs);
7724 specs->declspecs_seen_p = true;
7725 return specs;
7728 /* Combine "long", "short", "signed", "unsigned" and "_Complex" type
7729 specifiers with any other type specifier to determine the resulting
7730 type. This is where ISO C checks on complex types are made, since
7731 "_Complex long" is a prefix of the valid ISO C type "_Complex long
7732 double". */
7734 struct c_declspecs *
7735 finish_declspecs (struct c_declspecs *specs)
7737 /* If a type was specified as a whole, we have no modifiers and are
7738 done. */
7739 if (specs->type != NULL_TREE)
7741 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7742 && !specs->signed_p && !specs->unsigned_p
7743 && !specs->complex_p);
7744 return specs;
7747 /* If none of "void", "_Bool", "char", "int", "float" or "double"
7748 has been specified, treat it as "int" unless "_Complex" is
7749 present and there are no other specifiers. If we just have
7750 "_Complex", it is equivalent to "_Complex double", but e.g.
7751 "_Complex short" is equivalent to "_Complex short int". */
7752 if (specs->typespec_word == cts_none)
7754 if (specs->saturating_p)
7756 error ("%<_Sat%> is used without %<_Fract%> or %<_Accum%>");
7757 specs->typespec_word = cts_fract;
7759 else if (specs->long_p || specs->short_p
7760 || specs->signed_p || specs->unsigned_p)
7762 specs->typespec_word = cts_int;
7764 else if (specs->complex_p)
7766 specs->typespec_word = cts_double;
7767 if (pedantic)
7768 pedwarn ("ISO C does not support plain %<complex%> meaning "
7769 "%<double complex%>");
7771 else
7773 specs->typespec_word = cts_int;
7774 specs->default_int_p = true;
7775 /* We don't diagnose this here because grokdeclarator will
7776 give more specific diagnostics according to whether it is
7777 a function definition. */
7781 /* If "signed" was specified, record this to distinguish "int" and
7782 "signed int" in the case of a bit-field with
7783 -funsigned-bitfields. */
7784 specs->explicit_signed_p = specs->signed_p;
7786 /* Now compute the actual type. */
7787 switch (specs->typespec_word)
7789 case cts_void:
7790 gcc_assert (!specs->long_p && !specs->short_p
7791 && !specs->signed_p && !specs->unsigned_p
7792 && !specs->complex_p);
7793 specs->type = void_type_node;
7794 break;
7795 case cts_bool:
7796 gcc_assert (!specs->long_p && !specs->short_p
7797 && !specs->signed_p && !specs->unsigned_p
7798 && !specs->complex_p);
7799 specs->type = boolean_type_node;
7800 break;
7801 case cts_char:
7802 gcc_assert (!specs->long_p && !specs->short_p);
7803 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7804 if (specs->signed_p)
7805 specs->type = signed_char_type_node;
7806 else if (specs->unsigned_p)
7807 specs->type = unsigned_char_type_node;
7808 else
7809 specs->type = char_type_node;
7810 if (specs->complex_p)
7812 if (pedantic)
7813 pedwarn ("ISO C does not support complex integer types");
7814 specs->type = build_complex_type (specs->type);
7816 break;
7817 case cts_int:
7818 gcc_assert (!(specs->long_p && specs->short_p));
7819 gcc_assert (!(specs->signed_p && specs->unsigned_p));
7820 if (specs->long_long_p)
7821 specs->type = (specs->unsigned_p
7822 ? long_long_unsigned_type_node
7823 : long_long_integer_type_node);
7824 else if (specs->long_p)
7825 specs->type = (specs->unsigned_p
7826 ? long_unsigned_type_node
7827 : long_integer_type_node);
7828 else if (specs->short_p)
7829 specs->type = (specs->unsigned_p
7830 ? short_unsigned_type_node
7831 : short_integer_type_node);
7832 else
7833 specs->type = (specs->unsigned_p
7834 ? unsigned_type_node
7835 : integer_type_node);
7836 if (specs->complex_p)
7838 if (pedantic)
7839 pedwarn ("ISO C does not support complex integer types");
7840 specs->type = build_complex_type (specs->type);
7842 break;
7843 case cts_float:
7844 gcc_assert (!specs->long_p && !specs->short_p
7845 && !specs->signed_p && !specs->unsigned_p);
7846 specs->type = (specs->complex_p
7847 ? complex_float_type_node
7848 : float_type_node);
7849 break;
7850 case cts_double:
7851 gcc_assert (!specs->long_long_p && !specs->short_p
7852 && !specs->signed_p && !specs->unsigned_p);
7853 if (specs->long_p)
7855 specs->type = (specs->complex_p
7856 ? complex_long_double_type_node
7857 : long_double_type_node);
7859 else
7861 specs->type = (specs->complex_p
7862 ? complex_double_type_node
7863 : double_type_node);
7865 break;
7866 case cts_dfloat32:
7867 case cts_dfloat64:
7868 case cts_dfloat128:
7869 gcc_assert (!specs->long_p && !specs->long_long_p && !specs->short_p
7870 && !specs->signed_p && !specs->unsigned_p && !specs->complex_p);
7871 if (specs->typespec_word == cts_dfloat32)
7872 specs->type = dfloat32_type_node;
7873 else if (specs->typespec_word == cts_dfloat64)
7874 specs->type = dfloat64_type_node;
7875 else
7876 specs->type = dfloat128_type_node;
7877 break;
7878 case cts_fract:
7879 gcc_assert (!specs->complex_p);
7880 if (specs->saturating_p)
7882 if (specs->long_long_p)
7883 specs->type = specs->unsigned_p
7884 ? sat_unsigned_long_long_fract_type_node
7885 : sat_long_long_fract_type_node;
7886 else if (specs->long_p)
7887 specs->type = specs->unsigned_p
7888 ? sat_unsigned_long_fract_type_node
7889 : sat_long_fract_type_node;
7890 else if (specs->short_p)
7891 specs->type = specs->unsigned_p
7892 ? sat_unsigned_short_fract_type_node
7893 : sat_short_fract_type_node;
7894 else
7895 specs->type = specs->unsigned_p
7896 ? sat_unsigned_fract_type_node
7897 : sat_fract_type_node;
7899 else
7901 if (specs->long_long_p)
7902 specs->type = specs->unsigned_p
7903 ? unsigned_long_long_fract_type_node
7904 : long_long_fract_type_node;
7905 else if (specs->long_p)
7906 specs->type = specs->unsigned_p
7907 ? unsigned_long_fract_type_node
7908 : long_fract_type_node;
7909 else if (specs->short_p)
7910 specs->type = specs->unsigned_p
7911 ? unsigned_short_fract_type_node
7912 : short_fract_type_node;
7913 else
7914 specs->type = specs->unsigned_p
7915 ? unsigned_fract_type_node
7916 : fract_type_node;
7918 break;
7919 case cts_accum:
7920 gcc_assert (!specs->complex_p);
7921 if (specs->saturating_p)
7923 if (specs->long_long_p)
7924 specs->type = specs->unsigned_p
7925 ? sat_unsigned_long_long_accum_type_node
7926 : sat_long_long_accum_type_node;
7927 else if (specs->long_p)
7928 specs->type = specs->unsigned_p
7929 ? sat_unsigned_long_accum_type_node
7930 : sat_long_accum_type_node;
7931 else if (specs->short_p)
7932 specs->type = specs->unsigned_p
7933 ? sat_unsigned_short_accum_type_node
7934 : sat_short_accum_type_node;
7935 else
7936 specs->type = specs->unsigned_p
7937 ? sat_unsigned_accum_type_node
7938 : sat_accum_type_node;
7940 else
7942 if (specs->long_long_p)
7943 specs->type = specs->unsigned_p
7944 ? unsigned_long_long_accum_type_node
7945 : long_long_accum_type_node;
7946 else if (specs->long_p)
7947 specs->type = specs->unsigned_p
7948 ? unsigned_long_accum_type_node
7949 : long_accum_type_node;
7950 else if (specs->short_p)
7951 specs->type = specs->unsigned_p
7952 ? unsigned_short_accum_type_node
7953 : short_accum_type_node;
7954 else
7955 specs->type = specs->unsigned_p
7956 ? unsigned_accum_type_node
7957 : accum_type_node;
7959 break;
7960 default:
7961 gcc_unreachable ();
7964 return specs;
7967 /* A subroutine of c_write_global_declarations. Perform final processing
7968 on one file scope's declarations (or the external scope's declarations),
7969 GLOBALS. */
7971 static void
7972 c_write_global_declarations_1 (tree globals)
7974 tree decl;
7975 bool reconsider;
7977 /* Process the decls in the order they were written. */
7978 for (decl = globals; decl; decl = TREE_CHAIN (decl))
7980 /* Check for used but undefined static functions using the C
7981 standard's definition of "used", and set TREE_NO_WARNING so
7982 that check_global_declarations doesn't repeat the check. */
7983 if (TREE_CODE (decl) == FUNCTION_DECL
7984 && DECL_INITIAL (decl) == 0
7985 && DECL_EXTERNAL (decl)
7986 && !TREE_PUBLIC (decl)
7987 && C_DECL_USED (decl))
7989 pedwarn ("%q+F used but never defined", decl);
7990 TREE_NO_WARNING (decl) = 1;
7993 wrapup_global_declaration_1 (decl);
7998 reconsider = false;
7999 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8000 reconsider |= wrapup_global_declaration_2 (decl);
8002 while (reconsider);
8004 for (decl = globals; decl; decl = TREE_CHAIN (decl))
8005 check_global_declaration_1 (decl);
8008 /* A subroutine of c_write_global_declarations Emit debug information for each
8009 of the declarations in GLOBALS. */
8011 static void
8012 c_write_global_declarations_2 (tree globals)
8014 tree decl;
8016 for (decl = globals; decl ; decl = TREE_CHAIN (decl))
8017 debug_hooks->global_decl (decl);
8020 /* Preserve the external declarations scope across a garbage collect. */
8021 static GTY(()) tree ext_block;
8023 void
8024 c_write_global_declarations (void)
8026 tree t;
8028 /* We don't want to do this if generating a PCH. */
8029 if (pch_file)
8030 return;
8032 /* Don't waste time on further processing if -fsyntax-only or we've
8033 encountered errors. */
8034 if (flag_syntax_only || errorcount || sorrycount || cpp_errors (parse_in))
8035 return;
8037 /* Close the external scope. */
8038 ext_block = pop_scope ();
8039 external_scope = 0;
8040 gcc_assert (!current_scope);
8042 if (ext_block)
8044 tree tmp = BLOCK_VARS (ext_block);
8045 int flags;
8046 FILE * stream = dump_begin (TDI_tu, &flags);
8047 if (stream && tmp)
8049 dump_node (tmp, flags & ~TDF_SLIM, stream);
8050 dump_end (TDI_tu, stream);
8054 /* Process all file scopes in this compilation, and the external_scope,
8055 through wrapup_global_declarations and check_global_declarations. */
8056 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8057 c_write_global_declarations_1 (BLOCK_VARS (DECL_INITIAL (t)));
8058 c_write_global_declarations_1 (BLOCK_VARS (ext_block));
8060 /* We're done parsing; proceed to optimize and emit assembly.
8061 FIXME: shouldn't be the front end's responsibility to call this. */
8062 cgraph_optimize ();
8064 /* After cgraph has had a chance to emit everything that's going to
8065 be emitted, output debug information for globals. */
8066 if (errorcount == 0 && sorrycount == 0)
8068 timevar_push (TV_SYMOUT);
8069 for (t = all_translation_units; t; t = TREE_CHAIN (t))
8070 c_write_global_declarations_2 (BLOCK_VARS (DECL_INITIAL (t)));
8071 c_write_global_declarations_2 (BLOCK_VARS (ext_block));
8072 timevar_pop (TV_SYMOUT);
8075 ext_block = NULL;
8078 #include "gt-c-decl.h"